x86, acpi/irq: Handle isa irqs that are not identity mapped to gsi's.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_alloc_btree.c
blobb726e10d2c1c18bafa355663d822f8b9ef441d67
1 /*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_btree_trace.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_alloc.h"
41 #include "xfs_error.h"
42 #include "xfs_trace.h"
45 STATIC struct xfs_btree_cur *
46 xfs_allocbt_dup_cursor(
47 struct xfs_btree_cur *cur)
49 return xfs_allocbt_init_cursor(cur->bc_mp, cur->bc_tp,
50 cur->bc_private.a.agbp, cur->bc_private.a.agno,
51 cur->bc_btnum);
54 STATIC void
55 xfs_allocbt_set_root(
56 struct xfs_btree_cur *cur,
57 union xfs_btree_ptr *ptr,
58 int inc)
60 struct xfs_buf *agbp = cur->bc_private.a.agbp;
61 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
62 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
63 int btnum = cur->bc_btnum;
64 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
66 ASSERT(ptr->s != 0);
68 agf->agf_roots[btnum] = ptr->s;
69 be32_add_cpu(&agf->agf_levels[btnum], inc);
70 pag->pagf_levels[btnum] += inc;
71 xfs_perag_put(pag);
73 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
76 STATIC int
77 xfs_allocbt_alloc_block(
78 struct xfs_btree_cur *cur,
79 union xfs_btree_ptr *start,
80 union xfs_btree_ptr *new,
81 int length,
82 int *stat)
84 int error;
85 xfs_agblock_t bno;
87 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
89 /* Allocate the new block from the freelist. If we can't, give up. */
90 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
91 &bno, 1);
92 if (error) {
93 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
94 return error;
97 if (bno == NULLAGBLOCK) {
98 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
99 *stat = 0;
100 return 0;
103 xfs_trans_agbtree_delta(cur->bc_tp, 1);
104 new->s = cpu_to_be32(bno);
106 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
107 *stat = 1;
108 return 0;
111 STATIC int
112 xfs_allocbt_free_block(
113 struct xfs_btree_cur *cur,
114 struct xfs_buf *bp)
116 struct xfs_buf *agbp = cur->bc_private.a.agbp;
117 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
118 xfs_agblock_t bno;
119 int error;
121 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
122 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
123 if (error)
124 return error;
127 * Since blocks move to the free list without the coordination used in
128 * xfs_bmap_finish, we can't allow block to be available for
129 * reallocation and non-transaction writing (user data) until we know
130 * that the transaction that moved it to the free list is permanently
131 * on disk. We track the blocks by declaring these blocks as "busy";
132 * the busy list is maintained on a per-ag basis and each transaction
133 * records which entries should be removed when the iclog commits to
134 * disk. If a busy block is allocated, the iclog is pushed up to the
135 * LSN that freed the block.
137 xfs_alloc_mark_busy(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1);
138 xfs_trans_agbtree_delta(cur->bc_tp, -1);
139 return 0;
143 * Update the longest extent in the AGF
145 STATIC void
146 xfs_allocbt_update_lastrec(
147 struct xfs_btree_cur *cur,
148 struct xfs_btree_block *block,
149 union xfs_btree_rec *rec,
150 int ptr,
151 int reason)
153 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
154 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
155 struct xfs_perag *pag;
156 __be32 len;
157 int numrecs;
159 ASSERT(cur->bc_btnum == XFS_BTNUM_CNT);
161 switch (reason) {
162 case LASTREC_UPDATE:
164 * If this is the last leaf block and it's the last record,
165 * then update the size of the longest extent in the AG.
167 if (ptr != xfs_btree_get_numrecs(block))
168 return;
169 len = rec->alloc.ar_blockcount;
170 break;
171 case LASTREC_INSREC:
172 if (be32_to_cpu(rec->alloc.ar_blockcount) <=
173 be32_to_cpu(agf->agf_longest))
174 return;
175 len = rec->alloc.ar_blockcount;
176 break;
177 case LASTREC_DELREC:
178 numrecs = xfs_btree_get_numrecs(block);
179 if (ptr <= numrecs)
180 return;
181 ASSERT(ptr == numrecs + 1);
183 if (numrecs) {
184 xfs_alloc_rec_t *rrp;
186 rrp = XFS_ALLOC_REC_ADDR(cur->bc_mp, block, numrecs);
187 len = rrp->ar_blockcount;
188 } else {
189 len = 0;
192 break;
193 default:
194 ASSERT(0);
195 return;
198 agf->agf_longest = len;
199 pag = xfs_perag_get(cur->bc_mp, seqno);
200 pag->pagf_longest = be32_to_cpu(len);
201 xfs_perag_put(pag);
202 xfs_alloc_log_agf(cur->bc_tp, cur->bc_private.a.agbp, XFS_AGF_LONGEST);
205 STATIC int
206 xfs_allocbt_get_minrecs(
207 struct xfs_btree_cur *cur,
208 int level)
210 return cur->bc_mp->m_alloc_mnr[level != 0];
213 STATIC int
214 xfs_allocbt_get_maxrecs(
215 struct xfs_btree_cur *cur,
216 int level)
218 return cur->bc_mp->m_alloc_mxr[level != 0];
221 STATIC void
222 xfs_allocbt_init_key_from_rec(
223 union xfs_btree_key *key,
224 union xfs_btree_rec *rec)
226 ASSERT(rec->alloc.ar_startblock != 0);
228 key->alloc.ar_startblock = rec->alloc.ar_startblock;
229 key->alloc.ar_blockcount = rec->alloc.ar_blockcount;
232 STATIC void
233 xfs_allocbt_init_rec_from_key(
234 union xfs_btree_key *key,
235 union xfs_btree_rec *rec)
237 ASSERT(key->alloc.ar_startblock != 0);
239 rec->alloc.ar_startblock = key->alloc.ar_startblock;
240 rec->alloc.ar_blockcount = key->alloc.ar_blockcount;
243 STATIC void
244 xfs_allocbt_init_rec_from_cur(
245 struct xfs_btree_cur *cur,
246 union xfs_btree_rec *rec)
248 ASSERT(cur->bc_rec.a.ar_startblock != 0);
250 rec->alloc.ar_startblock = cpu_to_be32(cur->bc_rec.a.ar_startblock);
251 rec->alloc.ar_blockcount = cpu_to_be32(cur->bc_rec.a.ar_blockcount);
254 STATIC void
255 xfs_allocbt_init_ptr_from_cur(
256 struct xfs_btree_cur *cur,
257 union xfs_btree_ptr *ptr)
259 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
261 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
262 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
264 ptr->s = agf->agf_roots[cur->bc_btnum];
267 STATIC __int64_t
268 xfs_allocbt_key_diff(
269 struct xfs_btree_cur *cur,
270 union xfs_btree_key *key)
272 xfs_alloc_rec_incore_t *rec = &cur->bc_rec.a;
273 xfs_alloc_key_t *kp = &key->alloc;
274 __int64_t diff;
276 if (cur->bc_btnum == XFS_BTNUM_BNO) {
277 return (__int64_t)be32_to_cpu(kp->ar_startblock) -
278 rec->ar_startblock;
281 diff = (__int64_t)be32_to_cpu(kp->ar_blockcount) - rec->ar_blockcount;
282 if (diff)
283 return diff;
285 return (__int64_t)be32_to_cpu(kp->ar_startblock) - rec->ar_startblock;
288 STATIC int
289 xfs_allocbt_kill_root(
290 struct xfs_btree_cur *cur,
291 struct xfs_buf *bp,
292 int level,
293 union xfs_btree_ptr *newroot)
295 int error;
297 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
298 XFS_BTREE_STATS_INC(cur, killroot);
301 * Update the root pointer, decreasing the level by 1 and then
302 * free the old root.
304 xfs_allocbt_set_root(cur, newroot, -1);
305 error = xfs_allocbt_free_block(cur, bp);
306 if (error) {
307 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
308 return error;
311 XFS_BTREE_STATS_INC(cur, free);
313 xfs_btree_setbuf(cur, level, NULL);
314 cur->bc_nlevels--;
316 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
317 return 0;
320 #ifdef DEBUG
321 STATIC int
322 xfs_allocbt_keys_inorder(
323 struct xfs_btree_cur *cur,
324 union xfs_btree_key *k1,
325 union xfs_btree_key *k2)
327 if (cur->bc_btnum == XFS_BTNUM_BNO) {
328 return be32_to_cpu(k1->alloc.ar_startblock) <
329 be32_to_cpu(k2->alloc.ar_startblock);
330 } else {
331 return be32_to_cpu(k1->alloc.ar_blockcount) <
332 be32_to_cpu(k2->alloc.ar_blockcount) ||
333 (k1->alloc.ar_blockcount == k2->alloc.ar_blockcount &&
334 be32_to_cpu(k1->alloc.ar_startblock) <
335 be32_to_cpu(k2->alloc.ar_startblock));
339 STATIC int
340 xfs_allocbt_recs_inorder(
341 struct xfs_btree_cur *cur,
342 union xfs_btree_rec *r1,
343 union xfs_btree_rec *r2)
345 if (cur->bc_btnum == XFS_BTNUM_BNO) {
346 return be32_to_cpu(r1->alloc.ar_startblock) +
347 be32_to_cpu(r1->alloc.ar_blockcount) <=
348 be32_to_cpu(r2->alloc.ar_startblock);
349 } else {
350 return be32_to_cpu(r1->alloc.ar_blockcount) <
351 be32_to_cpu(r2->alloc.ar_blockcount) ||
352 (r1->alloc.ar_blockcount == r2->alloc.ar_blockcount &&
353 be32_to_cpu(r1->alloc.ar_startblock) <
354 be32_to_cpu(r2->alloc.ar_startblock));
357 #endif /* DEBUG */
359 #ifdef XFS_BTREE_TRACE
360 ktrace_t *xfs_allocbt_trace_buf;
362 STATIC void
363 xfs_allocbt_trace_enter(
364 struct xfs_btree_cur *cur,
365 const char *func,
366 char *s,
367 int type,
368 int line,
369 __psunsigned_t a0,
370 __psunsigned_t a1,
371 __psunsigned_t a2,
372 __psunsigned_t a3,
373 __psunsigned_t a4,
374 __psunsigned_t a5,
375 __psunsigned_t a6,
376 __psunsigned_t a7,
377 __psunsigned_t a8,
378 __psunsigned_t a9,
379 __psunsigned_t a10)
381 ktrace_enter(xfs_allocbt_trace_buf, (void *)(__psint_t)type,
382 (void *)func, (void *)s, NULL, (void *)cur,
383 (void *)a0, (void *)a1, (void *)a2, (void *)a3,
384 (void *)a4, (void *)a5, (void *)a6, (void *)a7,
385 (void *)a8, (void *)a9, (void *)a10);
388 STATIC void
389 xfs_allocbt_trace_cursor(
390 struct xfs_btree_cur *cur,
391 __uint32_t *s0,
392 __uint64_t *l0,
393 __uint64_t *l1)
395 *s0 = cur->bc_private.a.agno;
396 *l0 = cur->bc_rec.a.ar_startblock;
397 *l1 = cur->bc_rec.a.ar_blockcount;
400 STATIC void
401 xfs_allocbt_trace_key(
402 struct xfs_btree_cur *cur,
403 union xfs_btree_key *key,
404 __uint64_t *l0,
405 __uint64_t *l1)
407 *l0 = be32_to_cpu(key->alloc.ar_startblock);
408 *l1 = be32_to_cpu(key->alloc.ar_blockcount);
411 STATIC void
412 xfs_allocbt_trace_record(
413 struct xfs_btree_cur *cur,
414 union xfs_btree_rec *rec,
415 __uint64_t *l0,
416 __uint64_t *l1,
417 __uint64_t *l2)
419 *l0 = be32_to_cpu(rec->alloc.ar_startblock);
420 *l1 = be32_to_cpu(rec->alloc.ar_blockcount);
421 *l2 = 0;
423 #endif /* XFS_BTREE_TRACE */
425 static const struct xfs_btree_ops xfs_allocbt_ops = {
426 .rec_len = sizeof(xfs_alloc_rec_t),
427 .key_len = sizeof(xfs_alloc_key_t),
429 .dup_cursor = xfs_allocbt_dup_cursor,
430 .set_root = xfs_allocbt_set_root,
431 .kill_root = xfs_allocbt_kill_root,
432 .alloc_block = xfs_allocbt_alloc_block,
433 .free_block = xfs_allocbt_free_block,
434 .update_lastrec = xfs_allocbt_update_lastrec,
435 .get_minrecs = xfs_allocbt_get_minrecs,
436 .get_maxrecs = xfs_allocbt_get_maxrecs,
437 .init_key_from_rec = xfs_allocbt_init_key_from_rec,
438 .init_rec_from_key = xfs_allocbt_init_rec_from_key,
439 .init_rec_from_cur = xfs_allocbt_init_rec_from_cur,
440 .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur,
441 .key_diff = xfs_allocbt_key_diff,
443 #ifdef DEBUG
444 .keys_inorder = xfs_allocbt_keys_inorder,
445 .recs_inorder = xfs_allocbt_recs_inorder,
446 #endif
448 #ifdef XFS_BTREE_TRACE
449 .trace_enter = xfs_allocbt_trace_enter,
450 .trace_cursor = xfs_allocbt_trace_cursor,
451 .trace_key = xfs_allocbt_trace_key,
452 .trace_record = xfs_allocbt_trace_record,
453 #endif
457 * Allocate a new allocation btree cursor.
459 struct xfs_btree_cur * /* new alloc btree cursor */
460 xfs_allocbt_init_cursor(
461 struct xfs_mount *mp, /* file system mount point */
462 struct xfs_trans *tp, /* transaction pointer */
463 struct xfs_buf *agbp, /* buffer for agf structure */
464 xfs_agnumber_t agno, /* allocation group number */
465 xfs_btnum_t btnum) /* btree identifier */
467 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
468 struct xfs_btree_cur *cur;
470 ASSERT(btnum == XFS_BTNUM_BNO || btnum == XFS_BTNUM_CNT);
472 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP);
474 cur->bc_tp = tp;
475 cur->bc_mp = mp;
476 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[btnum]);
477 cur->bc_btnum = btnum;
478 cur->bc_blocklog = mp->m_sb.sb_blocklog;
480 cur->bc_ops = &xfs_allocbt_ops;
481 if (btnum == XFS_BTNUM_CNT)
482 cur->bc_flags = XFS_BTREE_LASTREC_UPDATE;
484 cur->bc_private.a.agbp = agbp;
485 cur->bc_private.a.agno = agno;
487 return cur;
491 * Calculate number of records in an alloc btree block.
494 xfs_allocbt_maxrecs(
495 struct xfs_mount *mp,
496 int blocklen,
497 int leaf)
499 blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
501 if (leaf)
502 return blocklen / sizeof(xfs_alloc_rec_t);
503 return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));