2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.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"
47 * Log specified fields for the inode given by bp and off.
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),
93 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
94 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
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;
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 */
119 xfs_trans_t *tp, /* transaction pointer */
120 xfs_buf_t *agbp, /* alloc group buffer */
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 */
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; /* inode allocation at stripe unit */
143 args.mp = tp->t_mountp;
146 * Locking will ensure that we don't have two callers in here
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 * Set the alignment for the allocation.
156 * If stripe alignment is turned on then align at stripe unit
158 * If the cluster size is smaller than a filesystem block
159 * then we're doing I/O for inodes in filesystem block size pieces,
160 * so don't need alignment anyway.
163 if (args.mp->m_sinoalign) {
164 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
165 args.alignment = args.mp->m_dalign;
167 } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
168 args.mp->m_sb.sb_inoalignmt >=
169 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
170 args.alignment = args.mp->m_sb.sb_inoalignmt;
173 agi = XFS_BUF_TO_AGI(agbp);
175 * Need to figure out where to allocate the inode blocks.
176 * Ideally they should be spaced out through the a.g.
177 * For now, just allocate blocks up front.
179 args.agbno = be32_to_cpu(agi->agi_root);
180 args.fsbno = XFS_AGB_TO_FSB(args.mp, be32_to_cpu(agi->agi_seqno),
183 * Allocate a fixed-size extent of inodes.
185 args.type = XFS_ALLOCTYPE_NEAR_BNO;
186 args.mod = args.total = args.wasdel = args.isfl = args.userdata =
187 args.minalignslop = 0;
190 * Allow space for the inode btree to split.
192 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
193 if ((error = xfs_alloc_vextent(&args)))
197 * If stripe alignment is turned on, then try again with cluster
200 if (isaligned && args.fsbno == NULLFSBLOCK) {
201 args.type = XFS_ALLOCTYPE_NEAR_BNO;
202 args.agbno = be32_to_cpu(agi->agi_root);
203 args.fsbno = XFS_AGB_TO_FSB(args.mp,
204 be32_to_cpu(agi->agi_seqno), args.agbno);
205 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
206 args.mp->m_sb.sb_inoalignmt >=
207 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
208 args.alignment = args.mp->m_sb.sb_inoalignmt;
211 if ((error = xfs_alloc_vextent(&args)))
215 if (args.fsbno == NULLFSBLOCK) {
219 ASSERT(args.len == args.minlen);
221 * Convert the results.
223 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
225 * Loop over the new block(s), filling in the inodes.
226 * For small block sizes, manipulate the inodes in buffers
227 * which are multiples of the blocks size.
229 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
230 blks_per_cluster = 1;
231 nbufs = (int)args.len;
232 ninodes = args.mp->m_sb.sb_inopblock;
234 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
235 args.mp->m_sb.sb_blocksize;
236 nbufs = (int)args.len / blks_per_cluster;
237 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
240 * Figure out what version number to use in the inodes we create.
241 * If the superblock version has caught up to the one that supports
242 * the new inode format, then use the new inode version. Otherwise
243 * use the old version so that old kernels will continue to be
244 * able to use the file system.
246 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
247 version = XFS_DINODE_VERSION_2;
249 version = XFS_DINODE_VERSION_1;
251 for (j = 0; j < nbufs; j++) {
255 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
256 args.agbno + (j * blks_per_cluster));
257 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
258 args.mp->m_bsize * blks_per_cluster,
261 ASSERT(!XFS_BUF_GETERROR(fbuf));
263 * Set initial values for the inodes in this buffer.
265 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
266 for (i = 0; i < ninodes; i++) {
267 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
268 INT_SET(free->di_core.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
269 INT_SET(free->di_core.di_version, ARCH_CONVERT, version);
270 INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
271 xfs_ialloc_log_di(tp, fbuf, i,
272 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
274 xfs_trans_inode_alloc_buf(tp, fbuf);
276 be32_add(&agi->agi_count, newlen);
277 be32_add(&agi->agi_freecount, newlen);
278 down_read(&args.mp->m_peraglock);
279 args.mp->m_perag[be32_to_cpu(agi->agi_seqno)].pagi_freecount += newlen;
280 up_read(&args.mp->m_peraglock);
281 agi->agi_newino = cpu_to_be32(newino);
283 * Insert records describing the new inode chunk into the btree.
285 cur = xfs_btree_init_cursor(args.mp, tp, agbp,
286 be32_to_cpu(agi->agi_seqno),
287 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
288 for (thisino = newino;
289 thisino < newino + newlen;
290 thisino += XFS_INODES_PER_CHUNK) {
291 if ((error = xfs_inobt_lookup_eq(cur, thisino,
292 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
293 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
297 if ((error = xfs_inobt_insert(cur, &i))) {
298 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
303 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
305 * Log allocation group header fields
307 xfs_ialloc_log_agi(tp, agbp,
308 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
310 * Modify/log superblock values for inode count and inode free count.
312 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
313 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
318 STATIC __inline xfs_agnumber_t
324 spin_lock(&mp->m_agirotor_lock);
325 agno = mp->m_agirotor;
326 if (++mp->m_agirotor == mp->m_maxagi)
328 spin_unlock(&mp->m_agirotor_lock);
334 * Select an allocation group to look for a free inode in, based on the parent
335 * inode and then mode. Return the allocation group buffer.
337 STATIC xfs_buf_t * /* allocation group buffer */
338 xfs_ialloc_ag_select(
339 xfs_trans_t *tp, /* transaction pointer */
340 xfs_ino_t parent, /* parent directory inode number */
341 mode_t mode, /* bits set to indicate file type */
342 int okalloc) /* ok to allocate more space */
344 xfs_buf_t *agbp; /* allocation group header buffer */
345 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
346 xfs_agnumber_t agno; /* current ag number */
347 int flags; /* alloc buffer locking flags */
348 xfs_extlen_t ineed; /* blocks needed for inode allocation */
349 xfs_extlen_t longest = 0; /* longest extent available */
350 xfs_mount_t *mp; /* mount point structure */
351 int needspace; /* file mode implies space allocated */
352 xfs_perag_t *pag; /* per allocation group data */
353 xfs_agnumber_t pagno; /* parent (starting) ag number */
356 * Files of these types need at least one block if length > 0
357 * (and they won't fit in the inode, but that's hard to figure out).
359 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
361 agcount = mp->m_maxagi;
363 pagno = xfs_ialloc_next_ag(mp);
365 pagno = XFS_INO_TO_AGNO(mp, parent);
366 if (pagno >= agcount)
369 ASSERT(pagno < agcount);
371 * Loop through allocation groups, looking for one with a little
372 * free space in it. Note we don't look for free inodes, exactly.
373 * Instead, we include whether there is a need to allocate inodes
374 * to mean that blocks must be allocated for them,
375 * if none are currently free.
378 flags = XFS_ALLOC_FLAG_TRYLOCK;
379 down_read(&mp->m_peraglock);
381 pag = &mp->m_perag[agno];
382 if (!pag->pagi_init) {
383 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
390 if (!pag->pagi_inodeok) {
391 xfs_ialloc_next_ag(mp);
396 * Is there enough free space for the file plus a block
397 * of inodes (if we need to allocate some)?
399 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
400 if (ineed && !pag->pagf_init) {
402 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
406 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
408 if (!ineed || pag->pagf_init) {
409 if (ineed && !(longest = pag->pagf_longest))
410 longest = pag->pagf_flcount > 0;
412 (pag->pagf_freeblks >= needspace + ineed &&
416 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
420 up_read(&mp->m_peraglock);
426 xfs_trans_brelse(tp, agbp);
429 * No point in iterating over the rest, if we're shutting
432 if (XFS_FORCED_SHUTDOWN(mp)) {
433 up_read(&mp->m_peraglock);
434 return (xfs_buf_t *)0;
441 up_read(&mp->m_peraglock);
442 return (xfs_buf_t *)0;
450 * Visible inode allocation functions.
454 * Allocate an inode on disk.
455 * Mode is used to tell whether the new inode will need space, and whether
458 * The arguments IO_agbp and alloc_done are defined to work within
459 * the constraint of one allocation per transaction.
460 * xfs_dialloc() is designed to be called twice if it has to do an
461 * allocation to make more free inodes. On the first call,
462 * IO_agbp should be set to NULL. If an inode is available,
463 * i.e., xfs_dialloc() did not need to do an allocation, an inode
464 * number is returned. In this case, IO_agbp would be set to the
465 * current ag_buf and alloc_done set to false.
466 * If an allocation needed to be done, xfs_dialloc would return
467 * the current ag_buf in IO_agbp and set alloc_done to true.
468 * The caller should then commit the current transaction, allocate a new
469 * transaction, and call xfs_dialloc() again, passing in the previous
470 * value of IO_agbp. IO_agbp should be held across the transactions.
471 * Since the agbp is locked across the two calls, the second call is
472 * guaranteed to have a free inode available.
474 * Once we successfully pick an inode its number is returned and the
475 * on-disk data structures are updated. The inode itself is not read
476 * in, since doing so would break ordering constraints with xfs_reclaim.
480 xfs_trans_t *tp, /* transaction pointer */
481 xfs_ino_t parent, /* parent inode (directory) */
482 mode_t mode, /* mode bits for new inode */
483 int okalloc, /* ok to allocate more space */
484 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
485 boolean_t *alloc_done, /* true if we needed to replenish
487 xfs_ino_t *inop) /* inode number allocated */
489 xfs_agnumber_t agcount; /* number of allocation groups */
490 xfs_buf_t *agbp; /* allocation group header's buffer */
491 xfs_agnumber_t agno; /* allocation group number */
492 xfs_agi_t *agi; /* allocation group header structure */
493 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
494 int error; /* error return value */
495 int i; /* result code */
496 int ialloced; /* inode allocation status */
497 int noroom = 0; /* no space for inode blk allocation */
498 xfs_ino_t ino; /* fs-relative inode to be returned */
500 int j; /* result code */
501 xfs_mount_t *mp; /* file system mount structure */
502 int offset; /* index of inode in chunk */
503 xfs_agino_t pagino; /* parent's a.g. relative inode # */
504 xfs_agnumber_t pagno; /* parent's allocation group number */
505 xfs_inobt_rec_t rec; /* inode allocation record */
506 xfs_agnumber_t tagno; /* testing allocation group number */
507 xfs_btree_cur_t *tcur; /* temp cursor */
508 xfs_inobt_rec_t trec; /* temp inode allocation record */
511 if (*IO_agbp == NULL) {
513 * We do not have an agbp, so select an initial allocation
514 * group for inode allocation.
516 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
518 * Couldn't find an allocation group satisfying the
525 agi = XFS_BUF_TO_AGI(agbp);
526 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
529 * Continue where we left off before. In this case, we
530 * know that the allocation group has free inodes.
533 agi = XFS_BUF_TO_AGI(agbp);
534 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
535 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
538 agcount = mp->m_sb.sb_agcount;
539 agno = be32_to_cpu(agi->agi_seqno);
541 pagno = XFS_INO_TO_AGNO(mp, parent);
542 pagino = XFS_INO_TO_AGINO(mp, parent);
545 * If we have already hit the ceiling of inode blocks then clear
546 * okalloc so we scan all available agi structures for a free
550 if (mp->m_maxicount &&
551 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
557 * Loop until we find an allocation group that either has free inodes
558 * or in which we can allocate some inodes. Iterate through the
559 * allocation groups upward, wrapping at the end.
561 *alloc_done = B_FALSE;
562 while (!agi->agi_freecount) {
564 * Don't do anything if we're not supposed to allocate
565 * any blocks, just go on to the next ag.
569 * Try to allocate some new inodes in the allocation
572 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
573 xfs_trans_brelse(tp, agbp);
574 if (error == ENOSPC) {
582 * We successfully allocated some inodes, return
583 * the current context to the caller so that it
584 * can commit the current transaction and call
585 * us again where we left off.
587 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
588 *alloc_done = B_TRUE;
595 * If it failed, give up on this ag.
597 xfs_trans_brelse(tp, agbp);
599 * Go on to the next ag: get its ag header.
602 if (++tagno == agcount)
606 return noroom ? ENOSPC : 0;
608 down_read(&mp->m_peraglock);
609 if (mp->m_perag[tagno].pagi_inodeok == 0) {
610 up_read(&mp->m_peraglock);
613 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
614 up_read(&mp->m_peraglock);
617 agi = XFS_BUF_TO_AGI(agbp);
618 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
621 * Here with an allocation group that has a free inode.
622 * Reset agno since we may have chosen a new ag in the
627 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
628 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
630 * If pagino is 0 (this is the root inode allocation) use newino.
631 * This must work because we've just allocated some.
634 pagino = be32_to_cpu(agi->agi_newino);
636 if (cur->bc_nlevels == 1) {
639 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
641 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
643 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
644 &rec.ir_freecount, &rec.ir_free, &i)))
646 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
647 freecount += rec.ir_freecount;
648 if ((error = xfs_inobt_increment(cur, 0, &i)))
652 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
653 XFS_FORCED_SHUTDOWN(mp));
657 * If in the same a.g. as the parent, try to get near the parent.
660 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
663 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
664 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
666 rec.ir_freecount > 0) {
668 * Found a free inode in the same chunk
673 * In the same a.g. as parent, but parent's chunk is full.
676 int doneleft; /* done, to the left */
677 int doneright; /* done, to the right */
684 * Duplicate the cursor, search left & right
687 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
690 * Search left with tcur, back up 1 record.
692 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
696 if ((error = xfs_inobt_get_rec(tcur,
701 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
704 * Search right with cur, go forward 1 record.
706 if ((error = xfs_inobt_increment(cur, 0, &i)))
710 if ((error = xfs_inobt_get_rec(cur,
715 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
718 * Loop until we find the closest inode chunk
721 while (!doneleft || !doneright) {
722 int useleft; /* using left inode
726 * Figure out which block is closer,
729 if (!doneleft && !doneright)
733 XFS_INODES_PER_CHUNK - 1) <
734 rec.ir_startino - pagino;
738 * If checking the left, does it have
741 if (useleft && trec.ir_freecount) {
743 * Yes, set it up as the chunk to use.
746 xfs_btree_del_cursor(cur,
752 * If checking the right, does it have
755 if (!useleft && rec.ir_freecount) {
757 * Yes, it's already set up.
759 xfs_btree_del_cursor(tcur,
764 * If used the left, get another one
768 if ((error = xfs_inobt_decrement(tcur, 0,
773 if ((error = xfs_inobt_get_rec(
779 XFS_WANT_CORRUPTED_GOTO(i == 1,
784 * If used the right, get another one
788 if ((error = xfs_inobt_increment(cur, 0,
793 if ((error = xfs_inobt_get_rec(
799 XFS_WANT_CORRUPTED_GOTO(i == 1,
804 ASSERT(!doneleft || !doneright);
808 * In a different a.g. from the parent.
809 * See if the most recently allocated block has any free.
811 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
812 if ((error = xfs_inobt_lookup_eq(cur,
813 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
816 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
817 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
819 rec.ir_freecount > 0) {
821 * The last chunk allocated in the group still has
826 * None left in the last group, search the whole a.g.
831 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
835 if ((error = xfs_inobt_get_rec(cur,
837 &rec.ir_freecount, &rec.ir_free,
840 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
841 if (rec.ir_freecount > 0)
843 if ((error = xfs_inobt_increment(cur, 0, &i)))
845 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
849 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
851 ASSERT(offset < XFS_INODES_PER_CHUNK);
852 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
853 XFS_INODES_PER_CHUNK) == 0);
854 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
855 XFS_INOBT_CLR_FREE(&rec, offset);
857 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
860 be32_add(&agi->agi_freecount, -1);
861 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
862 down_read(&mp->m_peraglock);
863 mp->m_perag[tagno].pagi_freecount--;
864 up_read(&mp->m_peraglock);
866 if (cur->bc_nlevels == 1) {
869 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
872 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
873 &rec.ir_freecount, &rec.ir_free, &i)))
875 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
876 freecount += rec.ir_freecount;
877 if ((error = xfs_inobt_increment(cur, 0, &i)))
880 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
881 XFS_FORCED_SHUTDOWN(mp));
884 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
885 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
889 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
891 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
896 * Free disk inode. Carefully avoids touching the incore inode, all
897 * manipulations incore are the caller's responsibility.
898 * The on-disk inode is not changed by this operation, only the
899 * btree (free inode mask) is changed.
903 xfs_trans_t *tp, /* transaction pointer */
904 xfs_ino_t inode, /* inode to be freed */
905 xfs_bmap_free_t *flist, /* extents to free */
906 int *delete, /* set if inode cluster was deleted */
907 xfs_ino_t *first_ino) /* first inode in deleted cluster */
910 xfs_agblock_t agbno; /* block number containing inode */
911 xfs_buf_t *agbp; /* buffer containing allocation group header */
912 xfs_agino_t agino; /* inode number relative to allocation group */
913 xfs_agnumber_t agno; /* allocation group number */
914 xfs_agi_t *agi; /* allocation group header */
915 xfs_btree_cur_t *cur; /* inode btree cursor */
916 int error; /* error return value */
917 int i; /* result code */
918 int ilen; /* inodes in an inode cluster */
919 xfs_mount_t *mp; /* mount structure for filesystem */
920 int off; /* offset of inode in inode chunk */
921 xfs_inobt_rec_t rec; /* btree record */
926 * Break up inode number into its components.
928 agno = XFS_INO_TO_AGNO(mp, inode);
929 if (agno >= mp->m_sb.sb_agcount) {
931 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
932 agno, mp->m_sb.sb_agcount, mp->m_fsname);
934 return XFS_ERROR(EINVAL);
936 agino = XFS_INO_TO_AGINO(mp, inode);
937 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
939 "xfs_difree: inode != XFS_AGINO_TO_INO() "
940 "(%llu != %llu) on %s. Returning EINVAL.",
941 (unsigned long long)inode,
942 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
945 return XFS_ERROR(EINVAL);
947 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
948 if (agbno >= mp->m_sb.sb_agblocks) {
950 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
951 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
953 return XFS_ERROR(EINVAL);
956 * Get the allocation group header.
958 down_read(&mp->m_peraglock);
959 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
960 up_read(&mp->m_peraglock);
963 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
964 error, mp->m_fsname);
967 agi = XFS_BUF_TO_AGI(agbp);
968 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
969 ASSERT(agbno < be32_to_cpu(agi->agi_length));
971 * Initialize the cursor.
973 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
974 (xfs_inode_t *)0, 0);
976 if (cur->bc_nlevels == 1) {
979 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
982 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
983 &rec.ir_freecount, &rec.ir_free, &i)))
986 freecount += rec.ir_freecount;
987 if ((error = xfs_inobt_increment(cur, 0, &i)))
991 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
992 XFS_FORCED_SHUTDOWN(mp));
996 * Look for the entry describing this inode.
998 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1000 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1001 error, mp->m_fsname);
1004 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1005 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1006 &rec.ir_free, &i))) {
1008 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1009 error, mp->m_fsname);
1012 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1014 * Get the offset in the inode chunk.
1016 off = agino - rec.ir_startino;
1017 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1018 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1020 * Mark the inode free & increment the count.
1022 XFS_INOBT_SET_FREE(&rec, off);
1026 * When an inode cluster is free, it becomes elgible for removal
1028 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1029 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1032 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1035 * Remove the inode cluster from the AGI B+Tree, adjust the
1036 * AGI and Superblock inode counts, and mark the disk space
1037 * to be freed when the transaction is committed.
1039 ilen = XFS_IALLOC_INODES(mp);
1040 be32_add(&agi->agi_count, -ilen);
1041 be32_add(&agi->agi_freecount, -(ilen - 1));
1042 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1043 down_read(&mp->m_peraglock);
1044 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1045 up_read(&mp->m_peraglock);
1046 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1047 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1049 if ((error = xfs_inobt_delete(cur, &i))) {
1050 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1051 error, mp->m_fsname);
1055 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1056 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1057 XFS_IALLOC_BLOCKS(mp), flist, mp);
1061 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1063 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1064 error, mp->m_fsname);
1068 * Change the inode free counts and log the ag/sb changes.
1070 be32_add(&agi->agi_freecount, 1);
1071 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1072 down_read(&mp->m_peraglock);
1073 mp->m_perag[agno].pagi_freecount++;
1074 up_read(&mp->m_peraglock);
1075 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1079 if (cur->bc_nlevels == 1) {
1082 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1085 if ((error = xfs_inobt_get_rec(cur,
1091 freecount += rec.ir_freecount;
1092 if ((error = xfs_inobt_increment(cur, 0, &i)))
1096 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1097 XFS_FORCED_SHUTDOWN(mp));
1100 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1104 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1109 * Return the location of the inode in bno/off, for mapping it into a buffer.
1114 xfs_mount_t *mp, /* file system mount structure */
1115 xfs_trans_t *tp, /* transaction pointer */
1116 xfs_ino_t ino, /* inode to locate */
1117 xfs_fsblock_t *bno, /* output: block containing inode */
1118 int *len, /* output: num blocks in inode cluster */
1119 int *off, /* output: index in block of inode */
1120 uint flags) /* flags concerning inode lookup */
1122 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1123 xfs_buf_t *agbp; /* agi buffer */
1124 xfs_agino_t agino; /* inode number within alloc group */
1125 xfs_agnumber_t agno; /* allocation group number */
1126 int blks_per_cluster; /* num blocks per inode cluster */
1127 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1128 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1129 __int32_t chunk_cnt; /* count of free inodes in chunk */
1130 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1131 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1132 xfs_btree_cur_t *cur; /* inode btree cursor */
1133 int error; /* error code */
1134 int i; /* temp state */
1135 int offset; /* index of inode in its buffer */
1136 int offset_agbno; /* blks from chunk start to inode */
1138 ASSERT(ino != NULLFSINO);
1140 * Split up the inode number into its parts.
1142 agno = XFS_INO_TO_AGNO(mp, ino);
1143 agino = XFS_INO_TO_AGINO(mp, ino);
1144 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1145 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1146 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1148 if (agno >= mp->m_sb.sb_agcount) {
1149 xfs_fs_cmn_err(CE_ALERT, mp,
1150 "xfs_dilocate: agno (%d) >= "
1151 "mp->m_sb.sb_agcount (%d)",
1152 agno, mp->m_sb.sb_agcount);
1154 if (agbno >= mp->m_sb.sb_agblocks) {
1155 xfs_fs_cmn_err(CE_ALERT, mp,
1156 "xfs_dilocate: agbno (0x%llx) >= "
1157 "mp->m_sb.sb_agblocks (0x%lx)",
1158 (unsigned long long) agbno,
1159 (unsigned long) mp->m_sb.sb_agblocks);
1161 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1162 xfs_fs_cmn_err(CE_ALERT, mp,
1163 "xfs_dilocate: ino (0x%llx) != "
1164 "XFS_AGINO_TO_INO(mp, agno, agino) "
1166 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1169 return XFS_ERROR(EINVAL);
1171 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1172 !(flags & XFS_IMAP_LOOKUP)) {
1173 offset = XFS_INO_TO_OFFSET(mp, ino);
1174 ASSERT(offset < mp->m_sb.sb_inopblock);
1175 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1180 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1181 if (*bno != NULLFSBLOCK) {
1182 offset = XFS_INO_TO_OFFSET(mp, ino);
1183 ASSERT(offset < mp->m_sb.sb_inopblock);
1184 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1185 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1187 *len = blks_per_cluster;
1190 if (mp->m_inoalign_mask) {
1191 offset_agbno = agbno & mp->m_inoalign_mask;
1192 chunk_agbno = agbno - offset_agbno;
1194 down_read(&mp->m_peraglock);
1195 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1196 up_read(&mp->m_peraglock);
1199 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1200 "xfs_ialloc_read_agi() returned "
1201 "error %d, agno %d",
1206 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1207 (xfs_inode_t *)0, 0);
1208 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1210 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1211 "xfs_inobt_lookup_le() failed");
1215 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1216 &chunk_free, &i))) {
1218 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1219 "xfs_inobt_get_rec() failed");
1225 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1226 "xfs_inobt_get_rec() failed");
1228 error = XFS_ERROR(EINVAL);
1230 xfs_trans_brelse(tp, agbp);
1231 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1234 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1235 offset_agbno = agbno - chunk_agbno;
1237 ASSERT(agbno >= chunk_agbno);
1238 cluster_agbno = chunk_agbno +
1239 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1240 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1241 XFS_INO_TO_OFFSET(mp, ino);
1242 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1244 *len = blks_per_cluster;
1247 xfs_trans_brelse(tp, agbp);
1248 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1253 * Compute and fill in value of m_in_maxlevels.
1256 xfs_ialloc_compute_maxlevels(
1257 xfs_mount_t *mp) /* file system mount structure */
1265 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1266 XFS_INODES_PER_CHUNK_LOG;
1267 minleafrecs = mp->m_alloc_mnr[0];
1268 minnoderecs = mp->m_alloc_mnr[1];
1269 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1270 for (level = 1; maxblocks > 1; level++)
1271 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1272 mp->m_in_maxlevels = level;
1276 * Log specified fields for the ag hdr (inode section)
1280 xfs_trans_t *tp, /* transaction pointer */
1281 xfs_buf_t *bp, /* allocation group header buffer */
1282 int fields) /* bitmask of fields to log */
1284 int first; /* first byte number */
1285 int last; /* last byte number */
1286 static const short offsets[] = { /* field starting offsets */
1287 /* keep in sync with bit definitions */
1288 offsetof(xfs_agi_t, agi_magicnum),
1289 offsetof(xfs_agi_t, agi_versionnum),
1290 offsetof(xfs_agi_t, agi_seqno),
1291 offsetof(xfs_agi_t, agi_length),
1292 offsetof(xfs_agi_t, agi_count),
1293 offsetof(xfs_agi_t, agi_root),
1294 offsetof(xfs_agi_t, agi_level),
1295 offsetof(xfs_agi_t, agi_freecount),
1296 offsetof(xfs_agi_t, agi_newino),
1297 offsetof(xfs_agi_t, agi_dirino),
1298 offsetof(xfs_agi_t, agi_unlinked),
1302 xfs_agi_t *agi; /* allocation group header */
1304 agi = XFS_BUF_TO_AGI(bp);
1305 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1308 * Compute byte offsets for the first and last fields.
1310 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1312 * Log the allocation group inode header buffer.
1314 xfs_trans_log_buf(tp, bp, first, last);
1318 * Read in the allocation group header (inode allocation section)
1321 xfs_ialloc_read_agi(
1322 xfs_mount_t *mp, /* file system mount structure */
1323 xfs_trans_t *tp, /* transaction pointer */
1324 xfs_agnumber_t agno, /* allocation group number */
1325 xfs_buf_t **bpp) /* allocation group hdr buf */
1327 xfs_agi_t *agi; /* allocation group header */
1328 int agi_ok; /* agi is consistent */
1329 xfs_buf_t *bp; /* allocation group hdr buf */
1330 xfs_perag_t *pag; /* per allocation group data */
1333 ASSERT(agno != NULLAGNUMBER);
1334 error = xfs_trans_read_buf(
1335 mp, tp, mp->m_ddev_targp,
1336 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1337 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1340 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1343 * Validate the magic number of the agi block.
1345 agi = XFS_BUF_TO_AGI(bp);
1347 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1348 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1349 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1350 XFS_RANDOM_IALLOC_READ_AGI))) {
1351 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1353 xfs_trans_brelse(tp, bp);
1354 return XFS_ERROR(EFSCORRUPTED);
1356 pag = &mp->m_perag[agno];
1357 if (!pag->pagi_init) {
1358 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1362 * It's possible for these to be out of sync if
1363 * we are in the middle of a forced shutdown.
1365 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1366 XFS_FORCED_SHUTDOWN(mp));
1373 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1374 ASSERT(agi->agi_unlinked[i]);
1378 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);