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 */
141 xfs_dinode_core_t dic; /* a dinode_core to copy to new */
145 args.mp = tp->t_mountp;
148 * Locking will ensure that we don't have two callers in here
151 newlen = XFS_IALLOC_INODES(args.mp);
152 if (args.mp->m_maxicount &&
153 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
154 return XFS_ERROR(ENOSPC);
155 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
157 * Set the alignment for the allocation.
158 * If stripe alignment is turned on then align at stripe unit
160 * If the cluster size is smaller than a filesystem block
161 * then we're doing I/O for inodes in filesystem block size pieces,
162 * so don't need alignment anyway.
165 if (args.mp->m_sinoalign) {
166 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
167 args.alignment = args.mp->m_dalign;
169 } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
170 args.mp->m_sb.sb_inoalignmt >=
171 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
172 args.alignment = args.mp->m_sb.sb_inoalignmt;
175 agi = XFS_BUF_TO_AGI(agbp);
177 * Need to figure out where to allocate the inode blocks.
178 * Ideally they should be spaced out through the a.g.
179 * For now, just allocate blocks up front.
181 args.agbno = be32_to_cpu(agi->agi_root);
182 args.fsbno = XFS_AGB_TO_FSB(args.mp, be32_to_cpu(agi->agi_seqno),
185 * Allocate a fixed-size extent of inodes.
187 args.type = XFS_ALLOCTYPE_NEAR_BNO;
188 args.mod = args.total = args.wasdel = args.isfl = args.userdata =
189 args.minalignslop = 0;
192 * Allow space for the inode btree to split.
194 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
195 if ((error = xfs_alloc_vextent(&args)))
199 * If stripe alignment is turned on, then try again with cluster
202 if (isaligned && args.fsbno == NULLFSBLOCK) {
203 args.type = XFS_ALLOCTYPE_NEAR_BNO;
204 args.agbno = be32_to_cpu(agi->agi_root);
205 args.fsbno = XFS_AGB_TO_FSB(args.mp,
206 be32_to_cpu(agi->agi_seqno), args.agbno);
207 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
208 args.mp->m_sb.sb_inoalignmt >=
209 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
210 args.alignment = args.mp->m_sb.sb_inoalignmt;
213 if ((error = xfs_alloc_vextent(&args)))
217 if (args.fsbno == NULLFSBLOCK) {
221 ASSERT(args.len == args.minlen);
223 * Convert the results.
225 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
227 * Loop over the new block(s), filling in the inodes.
228 * For small block sizes, manipulate the inodes in buffers
229 * which are multiples of the blocks size.
231 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
232 blks_per_cluster = 1;
233 nbufs = (int)args.len;
234 ninodes = args.mp->m_sb.sb_inopblock;
236 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
237 args.mp->m_sb.sb_blocksize;
238 nbufs = (int)args.len / blks_per_cluster;
239 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
242 * Figure out what version number to use in the inodes we create.
243 * If the superblock version has caught up to the one that supports
244 * the new inode format, then use the new inode version. Otherwise
245 * use the old version so that old kernels will continue to be
246 * able to use the file system.
248 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
249 version = XFS_DINODE_VERSION_2;
251 version = XFS_DINODE_VERSION_1;
253 memset(&dic, 0, sizeof(xfs_dinode_core_t));
254 INT_SET(dic.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
255 INT_SET(dic.di_version, ARCH_CONVERT, version);
257 for (j = 0; j < nbufs; j++) {
261 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
262 args.agbno + (j * blks_per_cluster));
263 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
264 args.mp->m_bsize * blks_per_cluster,
267 ASSERT(!XFS_BUF_GETERROR(fbuf));
269 * Loop over the inodes in this buffer.
272 for (i = 0; i < ninodes; i++) {
273 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
274 memcpy(&(free->di_core), &dic, sizeof(xfs_dinode_core_t));
275 INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
276 xfs_ialloc_log_di(tp, fbuf, i,
277 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
279 xfs_trans_inode_alloc_buf(tp, fbuf);
281 be32_add(&agi->agi_count, newlen);
282 be32_add(&agi->agi_freecount, newlen);
283 down_read(&args.mp->m_peraglock);
284 args.mp->m_perag[be32_to_cpu(agi->agi_seqno)].pagi_freecount += newlen;
285 up_read(&args.mp->m_peraglock);
286 agi->agi_newino = cpu_to_be32(newino);
288 * Insert records describing the new inode chunk into the btree.
290 cur = xfs_btree_init_cursor(args.mp, tp, agbp,
291 be32_to_cpu(agi->agi_seqno),
292 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
293 for (thisino = newino;
294 thisino < newino + newlen;
295 thisino += XFS_INODES_PER_CHUNK) {
296 if ((error = xfs_inobt_lookup_eq(cur, thisino,
297 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
298 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
302 if ((error = xfs_inobt_insert(cur, &i))) {
303 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
308 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
310 * Log allocation group header fields
312 xfs_ialloc_log_agi(tp, agbp,
313 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
315 * Modify/log superblock values for inode count and inode free count.
317 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
318 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
323 STATIC __inline xfs_agnumber_t
329 spin_lock(&mp->m_agirotor_lock);
330 agno = mp->m_agirotor;
331 if (++mp->m_agirotor == mp->m_maxagi)
333 spin_unlock(&mp->m_agirotor_lock);
339 * Select an allocation group to look for a free inode in, based on the parent
340 * inode and then mode. Return the allocation group buffer.
342 STATIC xfs_buf_t * /* allocation group buffer */
343 xfs_ialloc_ag_select(
344 xfs_trans_t *tp, /* transaction pointer */
345 xfs_ino_t parent, /* parent directory inode number */
346 mode_t mode, /* bits set to indicate file type */
347 int okalloc) /* ok to allocate more space */
349 xfs_buf_t *agbp; /* allocation group header buffer */
350 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
351 xfs_agnumber_t agno; /* current ag number */
352 int flags; /* alloc buffer locking flags */
353 xfs_extlen_t ineed; /* blocks needed for inode allocation */
354 xfs_extlen_t longest = 0; /* longest extent available */
355 xfs_mount_t *mp; /* mount point structure */
356 int needspace; /* file mode implies space allocated */
357 xfs_perag_t *pag; /* per allocation group data */
358 xfs_agnumber_t pagno; /* parent (starting) ag number */
361 * Files of these types need at least one block if length > 0
362 * (and they won't fit in the inode, but that's hard to figure out).
364 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
366 agcount = mp->m_maxagi;
368 pagno = xfs_ialloc_next_ag(mp);
370 pagno = XFS_INO_TO_AGNO(mp, parent);
371 if (pagno >= agcount)
374 ASSERT(pagno < agcount);
376 * Loop through allocation groups, looking for one with a little
377 * free space in it. Note we don't look for free inodes, exactly.
378 * Instead, we include whether there is a need to allocate inodes
379 * to mean that blocks must be allocated for them,
380 * if none are currently free.
383 flags = XFS_ALLOC_FLAG_TRYLOCK;
384 down_read(&mp->m_peraglock);
386 pag = &mp->m_perag[agno];
387 if (!pag->pagi_init) {
388 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
395 if (!pag->pagi_inodeok) {
396 xfs_ialloc_next_ag(mp);
401 * Is there enough free space for the file plus a block
402 * of inodes (if we need to allocate some)?
404 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
405 if (ineed && !pag->pagf_init) {
407 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
411 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
413 if (!ineed || pag->pagf_init) {
414 if (ineed && !(longest = pag->pagf_longest))
415 longest = pag->pagf_flcount > 0;
417 (pag->pagf_freeblks >= needspace + ineed &&
421 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
425 up_read(&mp->m_peraglock);
431 xfs_trans_brelse(tp, agbp);
434 * No point in iterating over the rest, if we're shutting
437 if (XFS_FORCED_SHUTDOWN(mp)) {
438 up_read(&mp->m_peraglock);
439 return (xfs_buf_t *)0;
446 up_read(&mp->m_peraglock);
447 return (xfs_buf_t *)0;
455 * Visible inode allocation functions.
459 * Allocate an inode on disk.
460 * Mode is used to tell whether the new inode will need space, and whether
463 * The arguments IO_agbp and alloc_done are defined to work within
464 * the constraint of one allocation per transaction.
465 * xfs_dialloc() is designed to be called twice if it has to do an
466 * allocation to make more free inodes. On the first call,
467 * IO_agbp should be set to NULL. If an inode is available,
468 * i.e., xfs_dialloc() did not need to do an allocation, an inode
469 * number is returned. In this case, IO_agbp would be set to the
470 * current ag_buf and alloc_done set to false.
471 * If an allocation needed to be done, xfs_dialloc would return
472 * the current ag_buf in IO_agbp and set alloc_done to true.
473 * The caller should then commit the current transaction, allocate a new
474 * transaction, and call xfs_dialloc() again, passing in the previous
475 * value of IO_agbp. IO_agbp should be held across the transactions.
476 * Since the agbp is locked across the two calls, the second call is
477 * guaranteed to have a free inode available.
479 * Once we successfully pick an inode its number is returned and the
480 * on-disk data structures are updated. The inode itself is not read
481 * in, since doing so would break ordering constraints with xfs_reclaim.
485 xfs_trans_t *tp, /* transaction pointer */
486 xfs_ino_t parent, /* parent inode (directory) */
487 mode_t mode, /* mode bits for new inode */
488 int okalloc, /* ok to allocate more space */
489 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
490 boolean_t *alloc_done, /* true if we needed to replenish
492 xfs_ino_t *inop) /* inode number allocated */
494 xfs_agnumber_t agcount; /* number of allocation groups */
495 xfs_buf_t *agbp; /* allocation group header's buffer */
496 xfs_agnumber_t agno; /* allocation group number */
497 xfs_agi_t *agi; /* allocation group header structure */
498 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
499 int error; /* error return value */
500 int i; /* result code */
501 int ialloced; /* inode allocation status */
502 int noroom = 0; /* no space for inode blk allocation */
503 xfs_ino_t ino; /* fs-relative inode to be returned */
505 int j; /* result code */
506 xfs_mount_t *mp; /* file system mount structure */
507 int offset; /* index of inode in chunk */
508 xfs_agino_t pagino; /* parent's a.g. relative inode # */
509 xfs_agnumber_t pagno; /* parent's allocation group number */
510 xfs_inobt_rec_t rec; /* inode allocation record */
511 xfs_agnumber_t tagno; /* testing allocation group number */
512 xfs_btree_cur_t *tcur; /* temp cursor */
513 xfs_inobt_rec_t trec; /* temp inode allocation record */
516 if (*IO_agbp == NULL) {
518 * We do not have an agbp, so select an initial allocation
519 * group for inode allocation.
521 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
523 * Couldn't find an allocation group satisfying the
530 agi = XFS_BUF_TO_AGI(agbp);
531 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
534 * Continue where we left off before. In this case, we
535 * know that the allocation group has free inodes.
538 agi = XFS_BUF_TO_AGI(agbp);
539 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
540 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
543 agcount = mp->m_sb.sb_agcount;
544 agno = be32_to_cpu(agi->agi_seqno);
546 pagno = XFS_INO_TO_AGNO(mp, parent);
547 pagino = XFS_INO_TO_AGINO(mp, parent);
550 * If we have already hit the ceiling of inode blocks then clear
551 * okalloc so we scan all available agi structures for a free
555 if (mp->m_maxicount &&
556 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
562 * Loop until we find an allocation group that either has free inodes
563 * or in which we can allocate some inodes. Iterate through the
564 * allocation groups upward, wrapping at the end.
566 *alloc_done = B_FALSE;
567 while (!agi->agi_freecount) {
569 * Don't do anything if we're not supposed to allocate
570 * any blocks, just go on to the next ag.
574 * Try to allocate some new inodes in the allocation
577 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
578 xfs_trans_brelse(tp, agbp);
579 if (error == ENOSPC) {
587 * We successfully allocated some inodes, return
588 * the current context to the caller so that it
589 * can commit the current transaction and call
590 * us again where we left off.
592 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
593 *alloc_done = B_TRUE;
600 * If it failed, give up on this ag.
602 xfs_trans_brelse(tp, agbp);
604 * Go on to the next ag: get its ag header.
607 if (++tagno == agcount)
611 return noroom ? ENOSPC : 0;
613 down_read(&mp->m_peraglock);
614 if (mp->m_perag[tagno].pagi_inodeok == 0) {
615 up_read(&mp->m_peraglock);
618 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
619 up_read(&mp->m_peraglock);
622 agi = XFS_BUF_TO_AGI(agbp);
623 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
626 * Here with an allocation group that has a free inode.
627 * Reset agno since we may have chosen a new ag in the
632 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
633 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
635 * If pagino is 0 (this is the root inode allocation) use newino.
636 * This must work because we've just allocated some.
639 pagino = be32_to_cpu(agi->agi_newino);
641 if (cur->bc_nlevels == 1) {
644 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
646 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
648 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
649 &rec.ir_freecount, &rec.ir_free, &i)))
651 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
652 freecount += rec.ir_freecount;
653 if ((error = xfs_inobt_increment(cur, 0, &i)))
657 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
658 XFS_FORCED_SHUTDOWN(mp));
662 * If in the same a.g. as the parent, try to get near the parent.
665 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
668 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
669 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
671 rec.ir_freecount > 0) {
673 * Found a free inode in the same chunk
678 * In the same a.g. as parent, but parent's chunk is full.
681 int doneleft; /* done, to the left */
682 int doneright; /* done, to the right */
689 * Duplicate the cursor, search left & right
692 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
695 * Search left with tcur, back up 1 record.
697 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
701 if ((error = xfs_inobt_get_rec(tcur,
706 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
709 * Search right with cur, go forward 1 record.
711 if ((error = xfs_inobt_increment(cur, 0, &i)))
715 if ((error = xfs_inobt_get_rec(cur,
720 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
723 * Loop until we find the closest inode chunk
726 while (!doneleft || !doneright) {
727 int useleft; /* using left inode
731 * Figure out which block is closer,
734 if (!doneleft && !doneright)
738 XFS_INODES_PER_CHUNK - 1) <
739 rec.ir_startino - pagino;
743 * If checking the left, does it have
746 if (useleft && trec.ir_freecount) {
748 * Yes, set it up as the chunk to use.
751 xfs_btree_del_cursor(cur,
757 * If checking the right, does it have
760 if (!useleft && rec.ir_freecount) {
762 * Yes, it's already set up.
764 xfs_btree_del_cursor(tcur,
769 * If used the left, get another one
773 if ((error = xfs_inobt_decrement(tcur, 0,
778 if ((error = xfs_inobt_get_rec(
784 XFS_WANT_CORRUPTED_GOTO(i == 1,
789 * If used the right, get another one
793 if ((error = xfs_inobt_increment(cur, 0,
798 if ((error = xfs_inobt_get_rec(
804 XFS_WANT_CORRUPTED_GOTO(i == 1,
809 ASSERT(!doneleft || !doneright);
813 * In a different a.g. from the parent.
814 * See if the most recently allocated block has any free.
816 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
817 if ((error = xfs_inobt_lookup_eq(cur,
818 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
821 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
822 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
824 rec.ir_freecount > 0) {
826 * The last chunk allocated in the group still has
831 * None left in the last group, search the whole a.g.
836 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
840 if ((error = xfs_inobt_get_rec(cur,
842 &rec.ir_freecount, &rec.ir_free,
845 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
846 if (rec.ir_freecount > 0)
848 if ((error = xfs_inobt_increment(cur, 0, &i)))
850 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
854 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
856 ASSERT(offset < XFS_INODES_PER_CHUNK);
857 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
858 XFS_INODES_PER_CHUNK) == 0);
859 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
860 XFS_INOBT_CLR_FREE(&rec, offset);
862 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
865 be32_add(&agi->agi_freecount, -1);
866 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
867 down_read(&mp->m_peraglock);
868 mp->m_perag[tagno].pagi_freecount--;
869 up_read(&mp->m_peraglock);
871 if (cur->bc_nlevels == 1) {
874 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
877 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
878 &rec.ir_freecount, &rec.ir_free, &i)))
880 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
881 freecount += rec.ir_freecount;
882 if ((error = xfs_inobt_increment(cur, 0, &i)))
885 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
886 XFS_FORCED_SHUTDOWN(mp));
889 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
890 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
894 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
896 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
901 * Free disk inode. Carefully avoids touching the incore inode, all
902 * manipulations incore are the caller's responsibility.
903 * The on-disk inode is not changed by this operation, only the
904 * btree (free inode mask) is changed.
908 xfs_trans_t *tp, /* transaction pointer */
909 xfs_ino_t inode, /* inode to be freed */
910 xfs_bmap_free_t *flist, /* extents to free */
911 int *delete, /* set if inode cluster was deleted */
912 xfs_ino_t *first_ino) /* first inode in deleted cluster */
915 xfs_agblock_t agbno; /* block number containing inode */
916 xfs_buf_t *agbp; /* buffer containing allocation group header */
917 xfs_agino_t agino; /* inode number relative to allocation group */
918 xfs_agnumber_t agno; /* allocation group number */
919 xfs_agi_t *agi; /* allocation group header */
920 xfs_btree_cur_t *cur; /* inode btree cursor */
921 int error; /* error return value */
922 int i; /* result code */
923 int ilen; /* inodes in an inode cluster */
924 xfs_mount_t *mp; /* mount structure for filesystem */
925 int off; /* offset of inode in inode chunk */
926 xfs_inobt_rec_t rec; /* btree record */
931 * Break up inode number into its components.
933 agno = XFS_INO_TO_AGNO(mp, inode);
934 if (agno >= mp->m_sb.sb_agcount) {
936 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
937 agno, mp->m_sb.sb_agcount, mp->m_fsname);
939 return XFS_ERROR(EINVAL);
941 agino = XFS_INO_TO_AGINO(mp, inode);
942 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
944 "xfs_difree: inode != XFS_AGINO_TO_INO() "
945 "(%llu != %llu) on %s. Returning EINVAL.",
946 (unsigned long long)inode,
947 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
950 return XFS_ERROR(EINVAL);
952 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
953 if (agbno >= mp->m_sb.sb_agblocks) {
955 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
956 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
958 return XFS_ERROR(EINVAL);
961 * Get the allocation group header.
963 down_read(&mp->m_peraglock);
964 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
965 up_read(&mp->m_peraglock);
968 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
969 error, mp->m_fsname);
972 agi = XFS_BUF_TO_AGI(agbp);
973 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
974 ASSERT(agbno < be32_to_cpu(agi->agi_length));
976 * Initialize the cursor.
978 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
979 (xfs_inode_t *)0, 0);
981 if (cur->bc_nlevels == 1) {
984 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
987 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
988 &rec.ir_freecount, &rec.ir_free, &i)))
991 freecount += rec.ir_freecount;
992 if ((error = xfs_inobt_increment(cur, 0, &i)))
996 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
997 XFS_FORCED_SHUTDOWN(mp));
1001 * Look for the entry describing this inode.
1003 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1005 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1006 error, mp->m_fsname);
1009 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1010 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1011 &rec.ir_free, &i))) {
1013 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1014 error, mp->m_fsname);
1017 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1019 * Get the offset in the inode chunk.
1021 off = agino - rec.ir_startino;
1022 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1023 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1025 * Mark the inode free & increment the count.
1027 XFS_INOBT_SET_FREE(&rec, off);
1031 * When an inode cluster is free, it becomes elgible for removal
1033 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1034 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1037 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1040 * Remove the inode cluster from the AGI B+Tree, adjust the
1041 * AGI and Superblock inode counts, and mark the disk space
1042 * to be freed when the transaction is committed.
1044 ilen = XFS_IALLOC_INODES(mp);
1045 be32_add(&agi->agi_count, -ilen);
1046 be32_add(&agi->agi_freecount, -(ilen - 1));
1047 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1048 down_read(&mp->m_peraglock);
1049 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1050 up_read(&mp->m_peraglock);
1051 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1052 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1054 if ((error = xfs_inobt_delete(cur, &i))) {
1055 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1056 error, mp->m_fsname);
1060 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1061 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1062 XFS_IALLOC_BLOCKS(mp), flist, mp);
1066 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1068 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1069 error, mp->m_fsname);
1073 * Change the inode free counts and log the ag/sb changes.
1075 be32_add(&agi->agi_freecount, 1);
1076 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1077 down_read(&mp->m_peraglock);
1078 mp->m_perag[agno].pagi_freecount++;
1079 up_read(&mp->m_peraglock);
1080 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1084 if (cur->bc_nlevels == 1) {
1087 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1090 if ((error = xfs_inobt_get_rec(cur,
1096 freecount += rec.ir_freecount;
1097 if ((error = xfs_inobt_increment(cur, 0, &i)))
1101 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1102 XFS_FORCED_SHUTDOWN(mp));
1105 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1109 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1114 * Return the location of the inode in bno/off, for mapping it into a buffer.
1119 xfs_mount_t *mp, /* file system mount structure */
1120 xfs_trans_t *tp, /* transaction pointer */
1121 xfs_ino_t ino, /* inode to locate */
1122 xfs_fsblock_t *bno, /* output: block containing inode */
1123 int *len, /* output: num blocks in inode cluster */
1124 int *off, /* output: index in block of inode */
1125 uint flags) /* flags concerning inode lookup */
1127 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1128 xfs_buf_t *agbp; /* agi buffer */
1129 xfs_agino_t agino; /* inode number within alloc group */
1130 xfs_agnumber_t agno; /* allocation group number */
1131 int blks_per_cluster; /* num blocks per inode cluster */
1132 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1133 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1134 __int32_t chunk_cnt; /* count of free inodes in chunk */
1135 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1136 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1137 xfs_btree_cur_t *cur; /* inode btree cursor */
1138 int error; /* error code */
1139 int i; /* temp state */
1140 int offset; /* index of inode in its buffer */
1141 int offset_agbno; /* blks from chunk start to inode */
1143 ASSERT(ino != NULLFSINO);
1145 * Split up the inode number into its parts.
1147 agno = XFS_INO_TO_AGNO(mp, ino);
1148 agino = XFS_INO_TO_AGINO(mp, ino);
1149 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1150 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1151 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1153 if (agno >= mp->m_sb.sb_agcount) {
1154 xfs_fs_cmn_err(CE_ALERT, mp,
1155 "xfs_dilocate: agno (%d) >= "
1156 "mp->m_sb.sb_agcount (%d)",
1157 agno, mp->m_sb.sb_agcount);
1159 if (agbno >= mp->m_sb.sb_agblocks) {
1160 xfs_fs_cmn_err(CE_ALERT, mp,
1161 "xfs_dilocate: agbno (0x%llx) >= "
1162 "mp->m_sb.sb_agblocks (0x%lx)",
1163 (unsigned long long) agbno,
1164 (unsigned long) mp->m_sb.sb_agblocks);
1166 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1167 xfs_fs_cmn_err(CE_ALERT, mp,
1168 "xfs_dilocate: ino (0x%llx) != "
1169 "XFS_AGINO_TO_INO(mp, agno, agino) "
1171 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1174 return XFS_ERROR(EINVAL);
1176 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1177 !(flags & XFS_IMAP_LOOKUP)) {
1178 offset = XFS_INO_TO_OFFSET(mp, ino);
1179 ASSERT(offset < mp->m_sb.sb_inopblock);
1180 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1185 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1186 if (*bno != NULLFSBLOCK) {
1187 offset = XFS_INO_TO_OFFSET(mp, ino);
1188 ASSERT(offset < mp->m_sb.sb_inopblock);
1189 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1190 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1192 *len = blks_per_cluster;
1195 if (mp->m_inoalign_mask) {
1196 offset_agbno = agbno & mp->m_inoalign_mask;
1197 chunk_agbno = agbno - offset_agbno;
1199 down_read(&mp->m_peraglock);
1200 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1201 up_read(&mp->m_peraglock);
1204 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1205 "xfs_ialloc_read_agi() returned "
1206 "error %d, agno %d",
1211 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1212 (xfs_inode_t *)0, 0);
1213 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1215 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1216 "xfs_inobt_lookup_le() failed");
1220 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1221 &chunk_free, &i))) {
1223 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1224 "xfs_inobt_get_rec() failed");
1230 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1231 "xfs_inobt_get_rec() failed");
1233 error = XFS_ERROR(EINVAL);
1235 xfs_trans_brelse(tp, agbp);
1236 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1239 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1240 offset_agbno = agbno - chunk_agbno;
1242 ASSERT(agbno >= chunk_agbno);
1243 cluster_agbno = chunk_agbno +
1244 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1245 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1246 XFS_INO_TO_OFFSET(mp, ino);
1247 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1249 *len = blks_per_cluster;
1252 xfs_trans_brelse(tp, agbp);
1253 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1258 * Compute and fill in value of m_in_maxlevels.
1261 xfs_ialloc_compute_maxlevels(
1262 xfs_mount_t *mp) /* file system mount structure */
1270 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1271 XFS_INODES_PER_CHUNK_LOG;
1272 minleafrecs = mp->m_alloc_mnr[0];
1273 minnoderecs = mp->m_alloc_mnr[1];
1274 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1275 for (level = 1; maxblocks > 1; level++)
1276 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1277 mp->m_in_maxlevels = level;
1281 * Log specified fields for the ag hdr (inode section)
1285 xfs_trans_t *tp, /* transaction pointer */
1286 xfs_buf_t *bp, /* allocation group header buffer */
1287 int fields) /* bitmask of fields to log */
1289 int first; /* first byte number */
1290 int last; /* last byte number */
1291 static const short offsets[] = { /* field starting offsets */
1292 /* keep in sync with bit definitions */
1293 offsetof(xfs_agi_t, agi_magicnum),
1294 offsetof(xfs_agi_t, agi_versionnum),
1295 offsetof(xfs_agi_t, agi_seqno),
1296 offsetof(xfs_agi_t, agi_length),
1297 offsetof(xfs_agi_t, agi_count),
1298 offsetof(xfs_agi_t, agi_root),
1299 offsetof(xfs_agi_t, agi_level),
1300 offsetof(xfs_agi_t, agi_freecount),
1301 offsetof(xfs_agi_t, agi_newino),
1302 offsetof(xfs_agi_t, agi_dirino),
1303 offsetof(xfs_agi_t, agi_unlinked),
1307 xfs_agi_t *agi; /* allocation group header */
1309 agi = XFS_BUF_TO_AGI(bp);
1310 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1313 * Compute byte offsets for the first and last fields.
1315 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1317 * Log the allocation group inode header buffer.
1319 xfs_trans_log_buf(tp, bp, first, last);
1323 * Read in the allocation group header (inode allocation section)
1326 xfs_ialloc_read_agi(
1327 xfs_mount_t *mp, /* file system mount structure */
1328 xfs_trans_t *tp, /* transaction pointer */
1329 xfs_agnumber_t agno, /* allocation group number */
1330 xfs_buf_t **bpp) /* allocation group hdr buf */
1332 xfs_agi_t *agi; /* allocation group header */
1333 int agi_ok; /* agi is consistent */
1334 xfs_buf_t *bp; /* allocation group hdr buf */
1335 xfs_perag_t *pag; /* per allocation group data */
1338 ASSERT(agno != NULLAGNUMBER);
1339 error = xfs_trans_read_buf(
1340 mp, tp, mp->m_ddev_targp,
1341 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1342 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1345 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1348 * Validate the magic number of the agi block.
1350 agi = XFS_BUF_TO_AGI(bp);
1352 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1353 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1354 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1355 XFS_RANDOM_IALLOC_READ_AGI))) {
1356 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1358 xfs_trans_brelse(tp, bp);
1359 return XFS_ERROR(EFSCORRUPTED);
1361 pag = &mp->m_perag[agno];
1362 if (!pag->pagi_init) {
1363 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1367 * It's possible for these to be out of sync if
1368 * we are in the middle of a forced shutdown.
1370 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1371 XFS_FORCED_SHUTDOWN(mp));
1378 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1379 ASSERT(agi->agi_unlinked[i]);
1383 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);