[PATCH] x86-64/i386: Don't process APICs/IO-APICs in ACPI when APIC is disabled.
[linux-2.6/verdex.git] / fs / xfs / xfs_ialloc.c
blobdeddbd03c1667e61b6f28e12e012e5c2bd1724e3
1 /*
2 * Copyright (c) 2000-2002,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_dir.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_alloc.h"
42 #include "xfs_rtalloc.h"
43 #include "xfs_error.h"
44 #include "xfs_bmap.h"
47 * Log specified fields for the inode given by bp and off.
49 STATIC void
50 xfs_ialloc_log_di(
51 xfs_trans_t *tp, /* transaction pointer */
52 xfs_buf_t *bp, /* inode buffer */
53 int off, /* index of inode in buffer */
54 int fields) /* bitmask of fields to log */
56 int first; /* first byte number */
57 int ioffset; /* off in bytes */
58 int last; /* last byte number */
59 xfs_mount_t *mp; /* mount point structure */
60 static const short offsets[] = { /* field offsets */
61 /* keep in sync with bits */
62 offsetof(xfs_dinode_core_t, di_magic),
63 offsetof(xfs_dinode_core_t, di_mode),
64 offsetof(xfs_dinode_core_t, di_version),
65 offsetof(xfs_dinode_core_t, di_format),
66 offsetof(xfs_dinode_core_t, di_onlink),
67 offsetof(xfs_dinode_core_t, di_uid),
68 offsetof(xfs_dinode_core_t, di_gid),
69 offsetof(xfs_dinode_core_t, di_nlink),
70 offsetof(xfs_dinode_core_t, di_projid),
71 offsetof(xfs_dinode_core_t, di_pad),
72 offsetof(xfs_dinode_core_t, di_atime),
73 offsetof(xfs_dinode_core_t, di_mtime),
74 offsetof(xfs_dinode_core_t, di_ctime),
75 offsetof(xfs_dinode_core_t, di_size),
76 offsetof(xfs_dinode_core_t, di_nblocks),
77 offsetof(xfs_dinode_core_t, di_extsize),
78 offsetof(xfs_dinode_core_t, di_nextents),
79 offsetof(xfs_dinode_core_t, di_anextents),
80 offsetof(xfs_dinode_core_t, di_forkoff),
81 offsetof(xfs_dinode_core_t, di_aformat),
82 offsetof(xfs_dinode_core_t, di_dmevmask),
83 offsetof(xfs_dinode_core_t, di_dmstate),
84 offsetof(xfs_dinode_core_t, di_flags),
85 offsetof(xfs_dinode_core_t, di_gen),
86 offsetof(xfs_dinode_t, di_next_unlinked),
87 offsetof(xfs_dinode_t, di_u),
88 offsetof(xfs_dinode_t, di_a),
89 sizeof(xfs_dinode_t)
93 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
94 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
95 mp = tp->t_mountp;
97 * Get the inode-relative first and last bytes for these fields
99 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
101 * Convert to buffer offsets and log it.
103 ioffset = off << mp->m_sb.sb_inodelog;
104 first += ioffset;
105 last += ioffset;
106 xfs_trans_log_buf(tp, bp, first, last);
110 * Allocation group level functions.
114 * Allocate new inodes in the allocation group specified by agbp.
115 * Return 0 for success, else error code.
117 STATIC int /* error code or 0 */
118 xfs_ialloc_ag_alloc(
119 xfs_trans_t *tp, /* transaction pointer */
120 xfs_buf_t *agbp, /* alloc group buffer */
121 int *alloc)
123 xfs_agi_t *agi; /* allocation group header */
124 xfs_alloc_arg_t args; /* allocation argument structure */
125 int blks_per_cluster; /* fs blocks per inode cluster */
126 xfs_btree_cur_t *cur; /* inode btree cursor */
127 xfs_daddr_t d; /* disk addr of buffer */
128 int error;
129 xfs_buf_t *fbuf; /* new free inodes' buffer */
130 xfs_dinode_t *free; /* new free inode structure */
131 int i; /* inode counter */
132 int j; /* block counter */
133 int nbufs; /* num bufs of new inodes */
134 xfs_agino_t newino; /* new first inode's number */
135 xfs_agino_t newlen; /* new number of inodes */
136 int ninodes; /* num inodes per buf */
137 xfs_agino_t thisino; /* current inode number, for loop */
138 int version; /* inode version number to use */
139 int isaligned = 0; /* inode allocation at stripe unit */
140 /* boundary */
142 args.tp = tp;
143 args.mp = tp->t_mountp;
146 * Locking will ensure that we don't have two callers in here
147 * at one time.
149 newlen = XFS_IALLOC_INODES(args.mp);
150 if (args.mp->m_maxicount &&
151 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
152 return XFS_ERROR(ENOSPC);
153 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
155 * First try to allocate inodes contiguous with the last-allocated
156 * chunk of inodes. If the filesystem is striped, this will fill
157 * an entire stripe unit with inodes.
159 agi = XFS_BUF_TO_AGI(agbp);
160 newino = be32_to_cpu(agi->agi_newino);
161 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
162 XFS_IALLOC_BLOCKS(args.mp);
163 if (likely(newino != NULLAGINO &&
164 (args.agbno < be32_to_cpu(agi->agi_length)))) {
165 args.fsbno = XFS_AGB_TO_FSB(args.mp,
166 be32_to_cpu(agi->agi_seqno), args.agbno);
167 args.type = XFS_ALLOCTYPE_THIS_BNO;
168 args.mod = args.total = args.wasdel = args.isfl =
169 args.userdata = args.minalignslop = 0;
170 args.prod = 1;
171 args.alignment = 1;
173 * Allow space for the inode btree to split.
175 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
176 if ((error = xfs_alloc_vextent(&args)))
177 return error;
178 } else
179 args.fsbno = NULLFSBLOCK;
181 if (unlikely(args.fsbno == NULLFSBLOCK)) {
183 * Set the alignment for the allocation.
184 * If stripe alignment is turned on then align at stripe unit
185 * boundary.
186 * If the cluster size is smaller than a filesystem block
187 * then we're doing I/O for inodes in filesystem block size
188 * pieces, so don't need alignment anyway.
190 isaligned = 0;
191 if (args.mp->m_sinoalign) {
192 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
193 args.alignment = args.mp->m_dalign;
194 isaligned = 1;
195 } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
196 args.mp->m_sb.sb_inoalignmt >=
197 XFS_B_TO_FSBT(args.mp,
198 XFS_INODE_CLUSTER_SIZE(args.mp)))
199 args.alignment = args.mp->m_sb.sb_inoalignmt;
200 else
201 args.alignment = 1;
203 * Need to figure out where to allocate the inode blocks.
204 * Ideally they should be spaced out through the a.g.
205 * For now, just allocate blocks up front.
207 args.agbno = be32_to_cpu(agi->agi_root);
208 args.fsbno = XFS_AGB_TO_FSB(args.mp,
209 be32_to_cpu(agi->agi_seqno), args.agbno);
211 * Allocate a fixed-size extent of inodes.
213 args.type = XFS_ALLOCTYPE_NEAR_BNO;
214 args.mod = args.total = args.wasdel = args.isfl =
215 args.userdata = args.minalignslop = 0;
216 args.prod = 1;
218 * Allow space for the inode btree to split.
220 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
221 if ((error = xfs_alloc_vextent(&args)))
222 return error;
226 * If stripe alignment is turned on, then try again with cluster
227 * alignment.
229 if (isaligned && args.fsbno == NULLFSBLOCK) {
230 args.type = XFS_ALLOCTYPE_NEAR_BNO;
231 args.agbno = be32_to_cpu(agi->agi_root);
232 args.fsbno = XFS_AGB_TO_FSB(args.mp,
233 be32_to_cpu(agi->agi_seqno), args.agbno);
234 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
235 args.mp->m_sb.sb_inoalignmt >=
236 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
237 args.alignment = args.mp->m_sb.sb_inoalignmt;
238 else
239 args.alignment = 1;
240 if ((error = xfs_alloc_vextent(&args)))
241 return error;
244 if (args.fsbno == NULLFSBLOCK) {
245 *alloc = 0;
246 return 0;
248 ASSERT(args.len == args.minlen);
250 * Convert the results.
252 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
254 * Loop over the new block(s), filling in the inodes.
255 * For small block sizes, manipulate the inodes in buffers
256 * which are multiples of the blocks size.
258 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
259 blks_per_cluster = 1;
260 nbufs = (int)args.len;
261 ninodes = args.mp->m_sb.sb_inopblock;
262 } else {
263 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
264 args.mp->m_sb.sb_blocksize;
265 nbufs = (int)args.len / blks_per_cluster;
266 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
269 * Figure out what version number to use in the inodes we create.
270 * If the superblock version has caught up to the one that supports
271 * the new inode format, then use the new inode version. Otherwise
272 * use the old version so that old kernels will continue to be
273 * able to use the file system.
275 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
276 version = XFS_DINODE_VERSION_2;
277 else
278 version = XFS_DINODE_VERSION_1;
280 for (j = 0; j < nbufs; j++) {
282 * Get the block.
284 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
285 args.agbno + (j * blks_per_cluster));
286 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
287 args.mp->m_bsize * blks_per_cluster,
288 XFS_BUF_LOCK);
289 ASSERT(fbuf);
290 ASSERT(!XFS_BUF_GETERROR(fbuf));
292 * Set initial values for the inodes in this buffer.
294 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
295 for (i = 0; i < ninodes; i++) {
296 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
297 INT_SET(free->di_core.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
298 INT_SET(free->di_core.di_version, ARCH_CONVERT, version);
299 INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
300 xfs_ialloc_log_di(tp, fbuf, i,
301 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
303 xfs_trans_inode_alloc_buf(tp, fbuf);
305 be32_add(&agi->agi_count, newlen);
306 be32_add(&agi->agi_freecount, newlen);
307 down_read(&args.mp->m_peraglock);
308 args.mp->m_perag[be32_to_cpu(agi->agi_seqno)].pagi_freecount += newlen;
309 up_read(&args.mp->m_peraglock);
310 agi->agi_newino = cpu_to_be32(newino);
312 * Insert records describing the new inode chunk into the btree.
314 cur = xfs_btree_init_cursor(args.mp, tp, agbp,
315 be32_to_cpu(agi->agi_seqno),
316 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
317 for (thisino = newino;
318 thisino < newino + newlen;
319 thisino += XFS_INODES_PER_CHUNK) {
320 if ((error = xfs_inobt_lookup_eq(cur, thisino,
321 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
322 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
323 return error;
325 ASSERT(i == 0);
326 if ((error = xfs_inobt_insert(cur, &i))) {
327 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
328 return error;
330 ASSERT(i == 1);
332 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
334 * Log allocation group header fields
336 xfs_ialloc_log_agi(tp, agbp,
337 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
339 * Modify/log superblock values for inode count and inode free count.
341 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
342 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
343 *alloc = 1;
344 return 0;
347 STATIC __inline xfs_agnumber_t
348 xfs_ialloc_next_ag(
349 xfs_mount_t *mp)
351 xfs_agnumber_t agno;
353 spin_lock(&mp->m_agirotor_lock);
354 agno = mp->m_agirotor;
355 if (++mp->m_agirotor == mp->m_maxagi)
356 mp->m_agirotor = 0;
357 spin_unlock(&mp->m_agirotor_lock);
359 return agno;
363 * Select an allocation group to look for a free inode in, based on the parent
364 * inode and then mode. Return the allocation group buffer.
366 STATIC xfs_buf_t * /* allocation group buffer */
367 xfs_ialloc_ag_select(
368 xfs_trans_t *tp, /* transaction pointer */
369 xfs_ino_t parent, /* parent directory inode number */
370 mode_t mode, /* bits set to indicate file type */
371 int okalloc) /* ok to allocate more space */
373 xfs_buf_t *agbp; /* allocation group header buffer */
374 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
375 xfs_agnumber_t agno; /* current ag number */
376 int flags; /* alloc buffer locking flags */
377 xfs_extlen_t ineed; /* blocks needed for inode allocation */
378 xfs_extlen_t longest = 0; /* longest extent available */
379 xfs_mount_t *mp; /* mount point structure */
380 int needspace; /* file mode implies space allocated */
381 xfs_perag_t *pag; /* per allocation group data */
382 xfs_agnumber_t pagno; /* parent (starting) ag number */
385 * Files of these types need at least one block if length > 0
386 * (and they won't fit in the inode, but that's hard to figure out).
388 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
389 mp = tp->t_mountp;
390 agcount = mp->m_maxagi;
391 if (S_ISDIR(mode))
392 pagno = xfs_ialloc_next_ag(mp);
393 else {
394 pagno = XFS_INO_TO_AGNO(mp, parent);
395 if (pagno >= agcount)
396 pagno = 0;
398 ASSERT(pagno < agcount);
400 * Loop through allocation groups, looking for one with a little
401 * free space in it. Note we don't look for free inodes, exactly.
402 * Instead, we include whether there is a need to allocate inodes
403 * to mean that blocks must be allocated for them,
404 * if none are currently free.
406 agno = pagno;
407 flags = XFS_ALLOC_FLAG_TRYLOCK;
408 down_read(&mp->m_peraglock);
409 for (;;) {
410 pag = &mp->m_perag[agno];
411 if (!pag->pagi_init) {
412 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
413 agbp = NULL;
414 goto nextag;
416 } else
417 agbp = NULL;
419 if (!pag->pagi_inodeok) {
420 xfs_ialloc_next_ag(mp);
421 goto unlock_nextag;
425 * Is there enough free space for the file plus a block
426 * of inodes (if we need to allocate some)?
428 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
429 if (ineed && !pag->pagf_init) {
430 if (agbp == NULL &&
431 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
432 agbp = NULL;
433 goto nextag;
435 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
437 if (!ineed || pag->pagf_init) {
438 if (ineed && !(longest = pag->pagf_longest))
439 longest = pag->pagf_flcount > 0;
440 if (!ineed ||
441 (pag->pagf_freeblks >= needspace + ineed &&
442 longest >= ineed &&
443 okalloc)) {
444 if (agbp == NULL &&
445 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
446 agbp = NULL;
447 goto nextag;
449 up_read(&mp->m_peraglock);
450 return agbp;
453 unlock_nextag:
454 if (agbp)
455 xfs_trans_brelse(tp, agbp);
456 nextag:
458 * No point in iterating over the rest, if we're shutting
459 * down.
461 if (XFS_FORCED_SHUTDOWN(mp)) {
462 up_read(&mp->m_peraglock);
463 return (xfs_buf_t *)0;
465 agno++;
466 if (agno >= agcount)
467 agno = 0;
468 if (agno == pagno) {
469 if (flags == 0) {
470 up_read(&mp->m_peraglock);
471 return (xfs_buf_t *)0;
473 flags = 0;
479 * Visible inode allocation functions.
483 * Allocate an inode on disk.
484 * Mode is used to tell whether the new inode will need space, and whether
485 * it is a directory.
487 * The arguments IO_agbp and alloc_done are defined to work within
488 * the constraint of one allocation per transaction.
489 * xfs_dialloc() is designed to be called twice if it has to do an
490 * allocation to make more free inodes. On the first call,
491 * IO_agbp should be set to NULL. If an inode is available,
492 * i.e., xfs_dialloc() did not need to do an allocation, an inode
493 * number is returned. In this case, IO_agbp would be set to the
494 * current ag_buf and alloc_done set to false.
495 * If an allocation needed to be done, xfs_dialloc would return
496 * the current ag_buf in IO_agbp and set alloc_done to true.
497 * The caller should then commit the current transaction, allocate a new
498 * transaction, and call xfs_dialloc() again, passing in the previous
499 * value of IO_agbp. IO_agbp should be held across the transactions.
500 * Since the agbp is locked across the two calls, the second call is
501 * guaranteed to have a free inode available.
503 * Once we successfully pick an inode its number is returned and the
504 * on-disk data structures are updated. The inode itself is not read
505 * in, since doing so would break ordering constraints with xfs_reclaim.
508 xfs_dialloc(
509 xfs_trans_t *tp, /* transaction pointer */
510 xfs_ino_t parent, /* parent inode (directory) */
511 mode_t mode, /* mode bits for new inode */
512 int okalloc, /* ok to allocate more space */
513 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
514 boolean_t *alloc_done, /* true if we needed to replenish
515 inode freelist */
516 xfs_ino_t *inop) /* inode number allocated */
518 xfs_agnumber_t agcount; /* number of allocation groups */
519 xfs_buf_t *agbp; /* allocation group header's buffer */
520 xfs_agnumber_t agno; /* allocation group number */
521 xfs_agi_t *agi; /* allocation group header structure */
522 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
523 int error; /* error return value */
524 int i; /* result code */
525 int ialloced; /* inode allocation status */
526 int noroom = 0; /* no space for inode blk allocation */
527 xfs_ino_t ino; /* fs-relative inode to be returned */
528 /* REFERENCED */
529 int j; /* result code */
530 xfs_mount_t *mp; /* file system mount structure */
531 int offset; /* index of inode in chunk */
532 xfs_agino_t pagino; /* parent's a.g. relative inode # */
533 xfs_agnumber_t pagno; /* parent's allocation group number */
534 xfs_inobt_rec_t rec; /* inode allocation record */
535 xfs_agnumber_t tagno; /* testing allocation group number */
536 xfs_btree_cur_t *tcur; /* temp cursor */
537 xfs_inobt_rec_t trec; /* temp inode allocation record */
540 if (*IO_agbp == NULL) {
542 * We do not have an agbp, so select an initial allocation
543 * group for inode allocation.
545 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
547 * Couldn't find an allocation group satisfying the
548 * criteria, give up.
550 if (!agbp) {
551 *inop = NULLFSINO;
552 return 0;
554 agi = XFS_BUF_TO_AGI(agbp);
555 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
556 } else {
558 * Continue where we left off before. In this case, we
559 * know that the allocation group has free inodes.
561 agbp = *IO_agbp;
562 agi = XFS_BUF_TO_AGI(agbp);
563 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
564 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
566 mp = tp->t_mountp;
567 agcount = mp->m_sb.sb_agcount;
568 agno = be32_to_cpu(agi->agi_seqno);
569 tagno = agno;
570 pagno = XFS_INO_TO_AGNO(mp, parent);
571 pagino = XFS_INO_TO_AGINO(mp, parent);
574 * If we have already hit the ceiling of inode blocks then clear
575 * okalloc so we scan all available agi structures for a free
576 * inode.
579 if (mp->m_maxicount &&
580 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
581 noroom = 1;
582 okalloc = 0;
586 * Loop until we find an allocation group that either has free inodes
587 * or in which we can allocate some inodes. Iterate through the
588 * allocation groups upward, wrapping at the end.
590 *alloc_done = B_FALSE;
591 while (!agi->agi_freecount) {
593 * Don't do anything if we're not supposed to allocate
594 * any blocks, just go on to the next ag.
596 if (okalloc) {
598 * Try to allocate some new inodes in the allocation
599 * group.
601 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
602 xfs_trans_brelse(tp, agbp);
603 if (error == ENOSPC) {
604 *inop = NULLFSINO;
605 return 0;
606 } else
607 return error;
609 if (ialloced) {
611 * We successfully allocated some inodes, return
612 * the current context to the caller so that it
613 * can commit the current transaction and call
614 * us again where we left off.
616 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
617 *alloc_done = B_TRUE;
618 *IO_agbp = agbp;
619 *inop = NULLFSINO;
620 return 0;
624 * If it failed, give up on this ag.
626 xfs_trans_brelse(tp, agbp);
628 * Go on to the next ag: get its ag header.
630 nextag:
631 if (++tagno == agcount)
632 tagno = 0;
633 if (tagno == agno) {
634 *inop = NULLFSINO;
635 return noroom ? ENOSPC : 0;
637 down_read(&mp->m_peraglock);
638 if (mp->m_perag[tagno].pagi_inodeok == 0) {
639 up_read(&mp->m_peraglock);
640 goto nextag;
642 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
643 up_read(&mp->m_peraglock);
644 if (error)
645 goto nextag;
646 agi = XFS_BUF_TO_AGI(agbp);
647 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
650 * Here with an allocation group that has a free inode.
651 * Reset agno since we may have chosen a new ag in the
652 * loop above.
654 agno = tagno;
655 *IO_agbp = NULL;
656 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
657 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
659 * If pagino is 0 (this is the root inode allocation) use newino.
660 * This must work because we've just allocated some.
662 if (!pagino)
663 pagino = be32_to_cpu(agi->agi_newino);
664 #ifdef DEBUG
665 if (cur->bc_nlevels == 1) {
666 int freecount = 0;
668 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
669 goto error0;
670 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
671 do {
672 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
673 &rec.ir_freecount, &rec.ir_free, &i)))
674 goto error0;
675 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
676 freecount += rec.ir_freecount;
677 if ((error = xfs_inobt_increment(cur, 0, &i)))
678 goto error0;
679 } while (i == 1);
681 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
682 XFS_FORCED_SHUTDOWN(mp));
684 #endif
686 * If in the same a.g. as the parent, try to get near the parent.
688 if (pagno == agno) {
689 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
690 goto error0;
691 if (i != 0 &&
692 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
693 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
694 j == 1 &&
695 rec.ir_freecount > 0) {
697 * Found a free inode in the same chunk
698 * as parent, done.
702 * In the same a.g. as parent, but parent's chunk is full.
704 else {
705 int doneleft; /* done, to the left */
706 int doneright; /* done, to the right */
708 if (error)
709 goto error0;
710 ASSERT(i == 1);
711 ASSERT(j == 1);
713 * Duplicate the cursor, search left & right
714 * simultaneously.
716 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
717 goto error0;
719 * Search left with tcur, back up 1 record.
721 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
722 goto error1;
723 doneleft = !i;
724 if (!doneleft) {
725 if ((error = xfs_inobt_get_rec(tcur,
726 &trec.ir_startino,
727 &trec.ir_freecount,
728 &trec.ir_free, &i)))
729 goto error1;
730 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
733 * Search right with cur, go forward 1 record.
735 if ((error = xfs_inobt_increment(cur, 0, &i)))
736 goto error1;
737 doneright = !i;
738 if (!doneright) {
739 if ((error = xfs_inobt_get_rec(cur,
740 &rec.ir_startino,
741 &rec.ir_freecount,
742 &rec.ir_free, &i)))
743 goto error1;
744 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
747 * Loop until we find the closest inode chunk
748 * with a free one.
750 while (!doneleft || !doneright) {
751 int useleft; /* using left inode
752 chunk this time */
755 * Figure out which block is closer,
756 * if both are valid.
758 if (!doneleft && !doneright)
759 useleft =
760 pagino -
761 (trec.ir_startino +
762 XFS_INODES_PER_CHUNK - 1) <
763 rec.ir_startino - pagino;
764 else
765 useleft = !doneleft;
767 * If checking the left, does it have
768 * free inodes?
770 if (useleft && trec.ir_freecount) {
772 * Yes, set it up as the chunk to use.
774 rec = trec;
775 xfs_btree_del_cursor(cur,
776 XFS_BTREE_NOERROR);
777 cur = tcur;
778 break;
781 * If checking the right, does it have
782 * free inodes?
784 if (!useleft && rec.ir_freecount) {
786 * Yes, it's already set up.
788 xfs_btree_del_cursor(tcur,
789 XFS_BTREE_NOERROR);
790 break;
793 * If used the left, get another one
794 * further left.
796 if (useleft) {
797 if ((error = xfs_inobt_decrement(tcur, 0,
798 &i)))
799 goto error1;
800 doneleft = !i;
801 if (!doneleft) {
802 if ((error = xfs_inobt_get_rec(
803 tcur,
804 &trec.ir_startino,
805 &trec.ir_freecount,
806 &trec.ir_free, &i)))
807 goto error1;
808 XFS_WANT_CORRUPTED_GOTO(i == 1,
809 error1);
813 * If used the right, get another one
814 * further right.
816 else {
817 if ((error = xfs_inobt_increment(cur, 0,
818 &i)))
819 goto error1;
820 doneright = !i;
821 if (!doneright) {
822 if ((error = xfs_inobt_get_rec(
823 cur,
824 &rec.ir_startino,
825 &rec.ir_freecount,
826 &rec.ir_free, &i)))
827 goto error1;
828 XFS_WANT_CORRUPTED_GOTO(i == 1,
829 error1);
833 ASSERT(!doneleft || !doneright);
837 * In a different a.g. from the parent.
838 * See if the most recently allocated block has any free.
840 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
841 if ((error = xfs_inobt_lookup_eq(cur,
842 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
843 goto error0;
844 if (i == 1 &&
845 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
846 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
847 j == 1 &&
848 rec.ir_freecount > 0) {
850 * The last chunk allocated in the group still has
851 * a free inode.
855 * None left in the last group, search the whole a.g.
857 else {
858 if (error)
859 goto error0;
860 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
861 goto error0;
862 ASSERT(i == 1);
863 for (;;) {
864 if ((error = xfs_inobt_get_rec(cur,
865 &rec.ir_startino,
866 &rec.ir_freecount, &rec.ir_free,
867 &i)))
868 goto error0;
869 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
870 if (rec.ir_freecount > 0)
871 break;
872 if ((error = xfs_inobt_increment(cur, 0, &i)))
873 goto error0;
874 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
878 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
879 ASSERT(offset >= 0);
880 ASSERT(offset < XFS_INODES_PER_CHUNK);
881 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
882 XFS_INODES_PER_CHUNK) == 0);
883 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
884 XFS_INOBT_CLR_FREE(&rec, offset);
885 rec.ir_freecount--;
886 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
887 rec.ir_free)))
888 goto error0;
889 be32_add(&agi->agi_freecount, -1);
890 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
891 down_read(&mp->m_peraglock);
892 mp->m_perag[tagno].pagi_freecount--;
893 up_read(&mp->m_peraglock);
894 #ifdef DEBUG
895 if (cur->bc_nlevels == 1) {
896 int freecount = 0;
898 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
899 goto error0;
900 do {
901 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
902 &rec.ir_freecount, &rec.ir_free, &i)))
903 goto error0;
904 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
905 freecount += rec.ir_freecount;
906 if ((error = xfs_inobt_increment(cur, 0, &i)))
907 goto error0;
908 } while (i == 1);
909 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
910 XFS_FORCED_SHUTDOWN(mp));
912 #endif
913 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
914 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
915 *inop = ino;
916 return 0;
917 error1:
918 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
919 error0:
920 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
921 return error;
925 * Free disk inode. Carefully avoids touching the incore inode, all
926 * manipulations incore are the caller's responsibility.
927 * The on-disk inode is not changed by this operation, only the
928 * btree (free inode mask) is changed.
931 xfs_difree(
932 xfs_trans_t *tp, /* transaction pointer */
933 xfs_ino_t inode, /* inode to be freed */
934 xfs_bmap_free_t *flist, /* extents to free */
935 int *delete, /* set if inode cluster was deleted */
936 xfs_ino_t *first_ino) /* first inode in deleted cluster */
938 /* REFERENCED */
939 xfs_agblock_t agbno; /* block number containing inode */
940 xfs_buf_t *agbp; /* buffer containing allocation group header */
941 xfs_agino_t agino; /* inode number relative to allocation group */
942 xfs_agnumber_t agno; /* allocation group number */
943 xfs_agi_t *agi; /* allocation group header */
944 xfs_btree_cur_t *cur; /* inode btree cursor */
945 int error; /* error return value */
946 int i; /* result code */
947 int ilen; /* inodes in an inode cluster */
948 xfs_mount_t *mp; /* mount structure for filesystem */
949 int off; /* offset of inode in inode chunk */
950 xfs_inobt_rec_t rec; /* btree record */
952 mp = tp->t_mountp;
955 * Break up inode number into its components.
957 agno = XFS_INO_TO_AGNO(mp, inode);
958 if (agno >= mp->m_sb.sb_agcount) {
959 cmn_err(CE_WARN,
960 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
961 agno, mp->m_sb.sb_agcount, mp->m_fsname);
962 ASSERT(0);
963 return XFS_ERROR(EINVAL);
965 agino = XFS_INO_TO_AGINO(mp, inode);
966 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
967 cmn_err(CE_WARN,
968 "xfs_difree: inode != XFS_AGINO_TO_INO() "
969 "(%llu != %llu) on %s. Returning EINVAL.",
970 (unsigned long long)inode,
971 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
972 mp->m_fsname);
973 ASSERT(0);
974 return XFS_ERROR(EINVAL);
976 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
977 if (agbno >= mp->m_sb.sb_agblocks) {
978 cmn_err(CE_WARN,
979 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
980 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
981 ASSERT(0);
982 return XFS_ERROR(EINVAL);
985 * Get the allocation group header.
987 down_read(&mp->m_peraglock);
988 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
989 up_read(&mp->m_peraglock);
990 if (error) {
991 cmn_err(CE_WARN,
992 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
993 error, mp->m_fsname);
994 return error;
996 agi = XFS_BUF_TO_AGI(agbp);
997 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
998 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1000 * Initialize the cursor.
1002 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1003 (xfs_inode_t *)0, 0);
1004 #ifdef DEBUG
1005 if (cur->bc_nlevels == 1) {
1006 int freecount = 0;
1008 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1009 goto error0;
1010 do {
1011 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1012 &rec.ir_freecount, &rec.ir_free, &i)))
1013 goto error0;
1014 if (i) {
1015 freecount += rec.ir_freecount;
1016 if ((error = xfs_inobt_increment(cur, 0, &i)))
1017 goto error0;
1019 } while (i == 1);
1020 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1021 XFS_FORCED_SHUTDOWN(mp));
1023 #endif
1025 * Look for the entry describing this inode.
1027 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1028 cmn_err(CE_WARN,
1029 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1030 error, mp->m_fsname);
1031 goto error0;
1033 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1034 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1035 &rec.ir_free, &i))) {
1036 cmn_err(CE_WARN,
1037 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1038 error, mp->m_fsname);
1039 goto error0;
1041 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1043 * Get the offset in the inode chunk.
1045 off = agino - rec.ir_startino;
1046 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1047 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1049 * Mark the inode free & increment the count.
1051 XFS_INOBT_SET_FREE(&rec, off);
1052 rec.ir_freecount++;
1055 * When an inode cluster is free, it becomes eligible for removal
1057 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1058 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1060 *delete = 1;
1061 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1064 * Remove the inode cluster from the AGI B+Tree, adjust the
1065 * AGI and Superblock inode counts, and mark the disk space
1066 * to be freed when the transaction is committed.
1068 ilen = XFS_IALLOC_INODES(mp);
1069 be32_add(&agi->agi_count, -ilen);
1070 be32_add(&agi->agi_freecount, -(ilen - 1));
1071 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1072 down_read(&mp->m_peraglock);
1073 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1074 up_read(&mp->m_peraglock);
1075 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1076 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1078 if ((error = xfs_inobt_delete(cur, &i))) {
1079 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1080 error, mp->m_fsname);
1081 goto error0;
1084 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1085 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1086 XFS_IALLOC_BLOCKS(mp), flist, mp);
1087 } else {
1088 *delete = 0;
1090 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1091 cmn_err(CE_WARN,
1092 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1093 error, mp->m_fsname);
1094 goto error0;
1097 * Change the inode free counts and log the ag/sb changes.
1099 be32_add(&agi->agi_freecount, 1);
1100 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1101 down_read(&mp->m_peraglock);
1102 mp->m_perag[agno].pagi_freecount++;
1103 up_read(&mp->m_peraglock);
1104 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1107 #ifdef DEBUG
1108 if (cur->bc_nlevels == 1) {
1109 int freecount = 0;
1111 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1112 goto error0;
1113 do {
1114 if ((error = xfs_inobt_get_rec(cur,
1115 &rec.ir_startino,
1116 &rec.ir_freecount,
1117 &rec.ir_free, &i)))
1118 goto error0;
1119 if (i) {
1120 freecount += rec.ir_freecount;
1121 if ((error = xfs_inobt_increment(cur, 0, &i)))
1122 goto error0;
1124 } while (i == 1);
1125 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1126 XFS_FORCED_SHUTDOWN(mp));
1128 #endif
1129 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1130 return 0;
1132 error0:
1133 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1134 return error;
1138 * Return the location of the inode in bno/off, for mapping it into a buffer.
1140 /*ARGSUSED*/
1142 xfs_dilocate(
1143 xfs_mount_t *mp, /* file system mount structure */
1144 xfs_trans_t *tp, /* transaction pointer */
1145 xfs_ino_t ino, /* inode to locate */
1146 xfs_fsblock_t *bno, /* output: block containing inode */
1147 int *len, /* output: num blocks in inode cluster */
1148 int *off, /* output: index in block of inode */
1149 uint flags) /* flags concerning inode lookup */
1151 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1152 xfs_buf_t *agbp; /* agi buffer */
1153 xfs_agino_t agino; /* inode number within alloc group */
1154 xfs_agnumber_t agno; /* allocation group number */
1155 int blks_per_cluster; /* num blocks per inode cluster */
1156 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1157 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1158 __int32_t chunk_cnt; /* count of free inodes in chunk */
1159 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1160 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1161 xfs_btree_cur_t *cur; /* inode btree cursor */
1162 int error; /* error code */
1163 int i; /* temp state */
1164 int offset; /* index of inode in its buffer */
1165 int offset_agbno; /* blks from chunk start to inode */
1167 ASSERT(ino != NULLFSINO);
1169 * Split up the inode number into its parts.
1171 agno = XFS_INO_TO_AGNO(mp, ino);
1172 agino = XFS_INO_TO_AGINO(mp, ino);
1173 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1174 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1175 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1176 #ifdef DEBUG
1177 if (agno >= mp->m_sb.sb_agcount) {
1178 xfs_fs_cmn_err(CE_ALERT, mp,
1179 "xfs_dilocate: agno (%d) >= "
1180 "mp->m_sb.sb_agcount (%d)",
1181 agno, mp->m_sb.sb_agcount);
1183 if (agbno >= mp->m_sb.sb_agblocks) {
1184 xfs_fs_cmn_err(CE_ALERT, mp,
1185 "xfs_dilocate: agbno (0x%llx) >= "
1186 "mp->m_sb.sb_agblocks (0x%lx)",
1187 (unsigned long long) agbno,
1188 (unsigned long) mp->m_sb.sb_agblocks);
1190 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1191 xfs_fs_cmn_err(CE_ALERT, mp,
1192 "xfs_dilocate: ino (0x%llx) != "
1193 "XFS_AGINO_TO_INO(mp, agno, agino) "
1194 "(0x%llx)",
1195 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1197 #endif /* DEBUG */
1198 return XFS_ERROR(EINVAL);
1200 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1201 !(flags & XFS_IMAP_LOOKUP)) {
1202 offset = XFS_INO_TO_OFFSET(mp, ino);
1203 ASSERT(offset < mp->m_sb.sb_inopblock);
1204 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1205 *off = offset;
1206 *len = 1;
1207 return 0;
1209 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1210 if (*bno != NULLFSBLOCK) {
1211 offset = XFS_INO_TO_OFFSET(mp, ino);
1212 ASSERT(offset < mp->m_sb.sb_inopblock);
1213 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1214 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1215 offset;
1216 *len = blks_per_cluster;
1217 return 0;
1219 if (mp->m_inoalign_mask) {
1220 offset_agbno = agbno & mp->m_inoalign_mask;
1221 chunk_agbno = agbno - offset_agbno;
1222 } else {
1223 down_read(&mp->m_peraglock);
1224 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1225 up_read(&mp->m_peraglock);
1226 if (error) {
1227 #ifdef DEBUG
1228 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1229 "xfs_ialloc_read_agi() returned "
1230 "error %d, agno %d",
1231 error, agno);
1232 #endif /* DEBUG */
1233 return error;
1235 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1236 (xfs_inode_t *)0, 0);
1237 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1238 #ifdef DEBUG
1239 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1240 "xfs_inobt_lookup_le() failed");
1241 #endif /* DEBUG */
1242 goto error0;
1244 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1245 &chunk_free, &i))) {
1246 #ifdef DEBUG
1247 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1248 "xfs_inobt_get_rec() failed");
1249 #endif /* DEBUG */
1250 goto error0;
1252 if (i == 0) {
1253 #ifdef DEBUG
1254 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1255 "xfs_inobt_get_rec() failed");
1256 #endif /* DEBUG */
1257 error = XFS_ERROR(EINVAL);
1259 xfs_trans_brelse(tp, agbp);
1260 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1261 if (error)
1262 return error;
1263 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1264 offset_agbno = agbno - chunk_agbno;
1266 ASSERT(agbno >= chunk_agbno);
1267 cluster_agbno = chunk_agbno +
1268 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1269 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1270 XFS_INO_TO_OFFSET(mp, ino);
1271 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1272 *off = offset;
1273 *len = blks_per_cluster;
1274 return 0;
1275 error0:
1276 xfs_trans_brelse(tp, agbp);
1277 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1278 return error;
1282 * Compute and fill in value of m_in_maxlevels.
1284 void
1285 xfs_ialloc_compute_maxlevels(
1286 xfs_mount_t *mp) /* file system mount structure */
1288 int level;
1289 uint maxblocks;
1290 uint maxleafents;
1291 int minleafrecs;
1292 int minnoderecs;
1294 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1295 XFS_INODES_PER_CHUNK_LOG;
1296 minleafrecs = mp->m_alloc_mnr[0];
1297 minnoderecs = mp->m_alloc_mnr[1];
1298 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1299 for (level = 1; maxblocks > 1; level++)
1300 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1301 mp->m_in_maxlevels = level;
1305 * Log specified fields for the ag hdr (inode section)
1307 void
1308 xfs_ialloc_log_agi(
1309 xfs_trans_t *tp, /* transaction pointer */
1310 xfs_buf_t *bp, /* allocation group header buffer */
1311 int fields) /* bitmask of fields to log */
1313 int first; /* first byte number */
1314 int last; /* last byte number */
1315 static const short offsets[] = { /* field starting offsets */
1316 /* keep in sync with bit definitions */
1317 offsetof(xfs_agi_t, agi_magicnum),
1318 offsetof(xfs_agi_t, agi_versionnum),
1319 offsetof(xfs_agi_t, agi_seqno),
1320 offsetof(xfs_agi_t, agi_length),
1321 offsetof(xfs_agi_t, agi_count),
1322 offsetof(xfs_agi_t, agi_root),
1323 offsetof(xfs_agi_t, agi_level),
1324 offsetof(xfs_agi_t, agi_freecount),
1325 offsetof(xfs_agi_t, agi_newino),
1326 offsetof(xfs_agi_t, agi_dirino),
1327 offsetof(xfs_agi_t, agi_unlinked),
1328 sizeof(xfs_agi_t)
1330 #ifdef DEBUG
1331 xfs_agi_t *agi; /* allocation group header */
1333 agi = XFS_BUF_TO_AGI(bp);
1334 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1335 #endif
1337 * Compute byte offsets for the first and last fields.
1339 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1341 * Log the allocation group inode header buffer.
1343 xfs_trans_log_buf(tp, bp, first, last);
1347 * Read in the allocation group header (inode allocation section)
1350 xfs_ialloc_read_agi(
1351 xfs_mount_t *mp, /* file system mount structure */
1352 xfs_trans_t *tp, /* transaction pointer */
1353 xfs_agnumber_t agno, /* allocation group number */
1354 xfs_buf_t **bpp) /* allocation group hdr buf */
1356 xfs_agi_t *agi; /* allocation group header */
1357 int agi_ok; /* agi is consistent */
1358 xfs_buf_t *bp; /* allocation group hdr buf */
1359 xfs_perag_t *pag; /* per allocation group data */
1360 int error;
1362 ASSERT(agno != NULLAGNUMBER);
1363 error = xfs_trans_read_buf(
1364 mp, tp, mp->m_ddev_targp,
1365 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1366 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1367 if (error)
1368 return error;
1369 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1372 * Validate the magic number of the agi block.
1374 agi = XFS_BUF_TO_AGI(bp);
1375 agi_ok =
1376 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1377 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1378 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1379 XFS_RANDOM_IALLOC_READ_AGI))) {
1380 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1381 mp, agi);
1382 xfs_trans_brelse(tp, bp);
1383 return XFS_ERROR(EFSCORRUPTED);
1385 pag = &mp->m_perag[agno];
1386 if (!pag->pagi_init) {
1387 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1388 pag->pagi_init = 1;
1389 } else {
1391 * It's possible for these to be out of sync if
1392 * we are in the middle of a forced shutdown.
1394 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1395 XFS_FORCED_SHUTDOWN(mp));
1398 #ifdef DEBUG
1400 int i;
1402 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1403 ASSERT(agi->agi_unlinked[i]);
1405 #endif
1407 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1408 *bpp = bp;
1409 return 0;