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"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
45 * Log specified fields for the inode given by bp and off.
49 xfs_trans_t *tp, /* transaction pointer */
50 xfs_buf_t *bp, /* inode buffer */
51 int off, /* index of inode in buffer */
52 int fields) /* bitmask of fields to log */
54 int first; /* first byte number */
55 int ioffset; /* off in bytes */
56 int last; /* last byte number */
57 xfs_mount_t *mp; /* mount point structure */
58 static const short offsets[] = { /* field offsets */
59 /* keep in sync with bits */
60 offsetof(xfs_dinode_core_t, di_magic),
61 offsetof(xfs_dinode_core_t, di_mode),
62 offsetof(xfs_dinode_core_t, di_version),
63 offsetof(xfs_dinode_core_t, di_format),
64 offsetof(xfs_dinode_core_t, di_onlink),
65 offsetof(xfs_dinode_core_t, di_uid),
66 offsetof(xfs_dinode_core_t, di_gid),
67 offsetof(xfs_dinode_core_t, di_nlink),
68 offsetof(xfs_dinode_core_t, di_projid),
69 offsetof(xfs_dinode_core_t, di_pad),
70 offsetof(xfs_dinode_core_t, di_atime),
71 offsetof(xfs_dinode_core_t, di_mtime),
72 offsetof(xfs_dinode_core_t, di_ctime),
73 offsetof(xfs_dinode_core_t, di_size),
74 offsetof(xfs_dinode_core_t, di_nblocks),
75 offsetof(xfs_dinode_core_t, di_extsize),
76 offsetof(xfs_dinode_core_t, di_nextents),
77 offsetof(xfs_dinode_core_t, di_anextents),
78 offsetof(xfs_dinode_core_t, di_forkoff),
79 offsetof(xfs_dinode_core_t, di_aformat),
80 offsetof(xfs_dinode_core_t, di_dmevmask),
81 offsetof(xfs_dinode_core_t, di_dmstate),
82 offsetof(xfs_dinode_core_t, di_flags),
83 offsetof(xfs_dinode_core_t, di_gen),
84 offsetof(xfs_dinode_t, di_next_unlinked),
85 offsetof(xfs_dinode_t, di_u),
86 offsetof(xfs_dinode_t, di_a),
91 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
92 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
95 * Get the inode-relative first and last bytes for these fields
97 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
99 * Convert to buffer offsets and log it.
101 ioffset = off << mp->m_sb.sb_inodelog;
104 xfs_trans_log_buf(tp, bp, first, last);
108 * Allocation group level functions.
111 xfs_ialloc_cluster_alignment(
112 xfs_alloc_arg_t *args)
114 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
115 args->mp->m_sb.sb_inoalignmt >=
116 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
117 return args->mp->m_sb.sb_inoalignmt;
122 * Allocate new inodes in the allocation group specified by agbp.
123 * Return 0 for success, else error code.
125 STATIC int /* error code or 0 */
127 xfs_trans_t *tp, /* transaction pointer */
128 xfs_buf_t *agbp, /* alloc group buffer */
131 xfs_agi_t *agi; /* allocation group header */
132 xfs_alloc_arg_t args; /* allocation argument structure */
133 int blks_per_cluster; /* fs blocks per inode cluster */
134 xfs_btree_cur_t *cur; /* inode btree cursor */
135 xfs_daddr_t d; /* disk addr of buffer */
138 xfs_buf_t *fbuf; /* new free inodes' buffer */
139 xfs_dinode_t *free; /* new free inode structure */
140 int i; /* inode counter */
141 int j; /* block counter */
142 int nbufs; /* num bufs of new inodes */
143 xfs_agino_t newino; /* new first inode's number */
144 xfs_agino_t newlen; /* new number of inodes */
145 int ninodes; /* num inodes per buf */
146 xfs_agino_t thisino; /* current inode number, for loop */
147 int version; /* inode version number to use */
148 int isaligned = 0; /* inode allocation at stripe unit */
152 args.mp = tp->t_mountp;
155 * Locking will ensure that we don't have two callers in here
158 newlen = XFS_IALLOC_INODES(args.mp);
159 if (args.mp->m_maxicount &&
160 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
161 return XFS_ERROR(ENOSPC);
162 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
164 * First try to allocate inodes contiguous with the last-allocated
165 * chunk of inodes. If the filesystem is striped, this will fill
166 * an entire stripe unit with inodes.
168 agi = XFS_BUF_TO_AGI(agbp);
169 newino = be32_to_cpu(agi->agi_newino);
170 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
171 XFS_IALLOC_BLOCKS(args.mp);
172 if (likely(newino != NULLAGINO &&
173 (args.agbno < be32_to_cpu(agi->agi_length)))) {
174 args.fsbno = XFS_AGB_TO_FSB(args.mp,
175 be32_to_cpu(agi->agi_seqno), args.agbno);
176 args.type = XFS_ALLOCTYPE_THIS_BNO;
177 args.mod = args.total = args.wasdel = args.isfl =
178 args.userdata = args.minalignslop = 0;
182 * We need to take into account alignment here to ensure that
183 * we don't modify the free list if we fail to have an exact
184 * block. If we don't have an exact match, and every oher
185 * attempt allocation attempt fails, we'll end up cancelling
186 * a dirty transaction and shutting down.
188 * For an exact allocation, alignment must be 1,
189 * however we need to take cluster alignment into account when
190 * fixing up the freelist. Use the minalignslop field to
191 * indicate that extra blocks might be required for alignment,
192 * but not to use them in the actual exact allocation.
195 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
197 /* Allow space for the inode btree to split. */
198 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
199 if ((error = xfs_alloc_vextent(&args)))
202 args.fsbno = NULLFSBLOCK;
204 if (unlikely(args.fsbno == NULLFSBLOCK)) {
206 * Set the alignment for the allocation.
207 * If stripe alignment is turned on then align at stripe unit
209 * If the cluster size is smaller than a filesystem block
210 * then we're doing I/O for inodes in filesystem block size
211 * pieces, so don't need alignment anyway.
214 if (args.mp->m_sinoalign) {
215 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
216 args.alignment = args.mp->m_dalign;
219 args.alignment = xfs_ialloc_cluster_alignment(&args);
221 * Need to figure out where to allocate the inode blocks.
222 * Ideally they should be spaced out through the a.g.
223 * For now, just allocate blocks up front.
225 args.agbno = be32_to_cpu(agi->agi_root);
226 args.fsbno = XFS_AGB_TO_FSB(args.mp,
227 be32_to_cpu(agi->agi_seqno), args.agbno);
229 * Allocate a fixed-size extent of inodes.
231 args.type = XFS_ALLOCTYPE_NEAR_BNO;
232 args.mod = args.total = args.wasdel = args.isfl =
233 args.userdata = args.minalignslop = 0;
236 * Allow space for the inode btree to split.
238 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
239 if ((error = xfs_alloc_vextent(&args)))
244 * If stripe alignment is turned on, then try again with cluster
247 if (isaligned && args.fsbno == NULLFSBLOCK) {
248 args.type = XFS_ALLOCTYPE_NEAR_BNO;
249 args.agbno = be32_to_cpu(agi->agi_root);
250 args.fsbno = XFS_AGB_TO_FSB(args.mp,
251 be32_to_cpu(agi->agi_seqno), args.agbno);
252 args.alignment = xfs_ialloc_cluster_alignment(&args);
253 if ((error = xfs_alloc_vextent(&args)))
257 if (args.fsbno == NULLFSBLOCK) {
261 ASSERT(args.len == args.minlen);
263 * Convert the results.
265 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
267 * Loop over the new block(s), filling in the inodes.
268 * For small block sizes, manipulate the inodes in buffers
269 * which are multiples of the blocks size.
271 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
272 blks_per_cluster = 1;
273 nbufs = (int)args.len;
274 ninodes = args.mp->m_sb.sb_inopblock;
276 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
277 args.mp->m_sb.sb_blocksize;
278 nbufs = (int)args.len / blks_per_cluster;
279 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
282 * Figure out what version number to use in the inodes we create.
283 * If the superblock version has caught up to the one that supports
284 * the new inode format, then use the new inode version. Otherwise
285 * use the old version so that old kernels will continue to be
286 * able to use the file system.
288 if (xfs_sb_version_hasnlink(&args.mp->m_sb))
289 version = XFS_DINODE_VERSION_2;
291 version = XFS_DINODE_VERSION_1;
293 for (j = 0; j < nbufs; j++) {
297 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
298 args.agbno + (j * blks_per_cluster));
299 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
300 args.mp->m_bsize * blks_per_cluster,
303 ASSERT(!XFS_BUF_GETERROR(fbuf));
305 * Set initial values for the inodes in this buffer.
307 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
308 for (i = 0; i < ninodes; i++) {
309 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
310 free->di_core.di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
311 free->di_core.di_version = version;
312 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
313 xfs_ialloc_log_di(tp, fbuf, i,
314 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
316 xfs_trans_inode_alloc_buf(tp, fbuf);
318 be32_add_cpu(&agi->agi_count, newlen);
319 be32_add_cpu(&agi->agi_freecount, newlen);
320 agno = be32_to_cpu(agi->agi_seqno);
321 down_read(&args.mp->m_peraglock);
322 args.mp->m_perag[agno].pagi_freecount += newlen;
323 up_read(&args.mp->m_peraglock);
324 agi->agi_newino = cpu_to_be32(newino);
326 * Insert records describing the new inode chunk into the btree.
328 cur = xfs_btree_init_cursor(args.mp, tp, agbp, agno,
329 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
330 for (thisino = newino;
331 thisino < newino + newlen;
332 thisino += XFS_INODES_PER_CHUNK) {
333 if ((error = xfs_inobt_lookup_eq(cur, thisino,
334 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
335 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
339 if ((error = xfs_inobt_insert(cur, &i))) {
340 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
345 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
347 * Log allocation group header fields
349 xfs_ialloc_log_agi(tp, agbp,
350 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
352 * Modify/log superblock values for inode count and inode free count.
354 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
355 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
360 STATIC_INLINE xfs_agnumber_t
366 spin_lock(&mp->m_agirotor_lock);
367 agno = mp->m_agirotor;
368 if (++mp->m_agirotor == mp->m_maxagi)
370 spin_unlock(&mp->m_agirotor_lock);
376 * Select an allocation group to look for a free inode in, based on the parent
377 * inode and then mode. Return the allocation group buffer.
379 STATIC xfs_buf_t * /* allocation group buffer */
380 xfs_ialloc_ag_select(
381 xfs_trans_t *tp, /* transaction pointer */
382 xfs_ino_t parent, /* parent directory inode number */
383 mode_t mode, /* bits set to indicate file type */
384 int okalloc) /* ok to allocate more space */
386 xfs_buf_t *agbp; /* allocation group header buffer */
387 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
388 xfs_agnumber_t agno; /* current ag number */
389 int flags; /* alloc buffer locking flags */
390 xfs_extlen_t ineed; /* blocks needed for inode allocation */
391 xfs_extlen_t longest = 0; /* longest extent available */
392 xfs_mount_t *mp; /* mount point structure */
393 int needspace; /* file mode implies space allocated */
394 xfs_perag_t *pag; /* per allocation group data */
395 xfs_agnumber_t pagno; /* parent (starting) ag number */
398 * Files of these types need at least one block if length > 0
399 * (and they won't fit in the inode, but that's hard to figure out).
401 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
403 agcount = mp->m_maxagi;
405 pagno = xfs_ialloc_next_ag(mp);
407 pagno = XFS_INO_TO_AGNO(mp, parent);
408 if (pagno >= agcount)
411 ASSERT(pagno < agcount);
413 * Loop through allocation groups, looking for one with a little
414 * free space in it. Note we don't look for free inodes, exactly.
415 * Instead, we include whether there is a need to allocate inodes
416 * to mean that blocks must be allocated for them,
417 * if none are currently free.
420 flags = XFS_ALLOC_FLAG_TRYLOCK;
421 down_read(&mp->m_peraglock);
423 pag = &mp->m_perag[agno];
424 if (!pag->pagi_init) {
425 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
432 if (!pag->pagi_inodeok) {
433 xfs_ialloc_next_ag(mp);
438 * Is there enough free space for the file plus a block
439 * of inodes (if we need to allocate some)?
441 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
442 if (ineed && !pag->pagf_init) {
444 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
448 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
450 if (!ineed || pag->pagf_init) {
451 if (ineed && !(longest = pag->pagf_longest))
452 longest = pag->pagf_flcount > 0;
454 (pag->pagf_freeblks >= needspace + ineed &&
458 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
462 up_read(&mp->m_peraglock);
468 xfs_trans_brelse(tp, agbp);
471 * No point in iterating over the rest, if we're shutting
474 if (XFS_FORCED_SHUTDOWN(mp)) {
475 up_read(&mp->m_peraglock);
483 up_read(&mp->m_peraglock);
492 * Visible inode allocation functions.
496 * Allocate an inode on disk.
497 * Mode is used to tell whether the new inode will need space, and whether
500 * The arguments IO_agbp and alloc_done are defined to work within
501 * the constraint of one allocation per transaction.
502 * xfs_dialloc() is designed to be called twice if it has to do an
503 * allocation to make more free inodes. On the first call,
504 * IO_agbp should be set to NULL. If an inode is available,
505 * i.e., xfs_dialloc() did not need to do an allocation, an inode
506 * number is returned. In this case, IO_agbp would be set to the
507 * current ag_buf and alloc_done set to false.
508 * If an allocation needed to be done, xfs_dialloc would return
509 * the current ag_buf in IO_agbp and set alloc_done to true.
510 * The caller should then commit the current transaction, allocate a new
511 * transaction, and call xfs_dialloc() again, passing in the previous
512 * value of IO_agbp. IO_agbp should be held across the transactions.
513 * Since the agbp is locked across the two calls, the second call is
514 * guaranteed to have a free inode available.
516 * Once we successfully pick an inode its number is returned and the
517 * on-disk data structures are updated. The inode itself is not read
518 * in, since doing so would break ordering constraints with xfs_reclaim.
522 xfs_trans_t *tp, /* transaction pointer */
523 xfs_ino_t parent, /* parent inode (directory) */
524 mode_t mode, /* mode bits for new inode */
525 int okalloc, /* ok to allocate more space */
526 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
527 boolean_t *alloc_done, /* true if we needed to replenish
529 xfs_ino_t *inop) /* inode number allocated */
531 xfs_agnumber_t agcount; /* number of allocation groups */
532 xfs_buf_t *agbp; /* allocation group header's buffer */
533 xfs_agnumber_t agno; /* allocation group number */
534 xfs_agi_t *agi; /* allocation group header structure */
535 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
536 int error; /* error return value */
537 int i; /* result code */
538 int ialloced; /* inode allocation status */
539 int noroom = 0; /* no space for inode blk allocation */
540 xfs_ino_t ino; /* fs-relative inode to be returned */
542 int j; /* result code */
543 xfs_mount_t *mp; /* file system mount structure */
544 int offset; /* index of inode in chunk */
545 xfs_agino_t pagino; /* parent's a.g. relative inode # */
546 xfs_agnumber_t pagno; /* parent's allocation group number */
547 xfs_inobt_rec_incore_t rec; /* inode allocation record */
548 xfs_agnumber_t tagno; /* testing allocation group number */
549 xfs_btree_cur_t *tcur; /* temp cursor */
550 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
553 if (*IO_agbp == NULL) {
555 * We do not have an agbp, so select an initial allocation
556 * group for inode allocation.
558 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
560 * Couldn't find an allocation group satisfying the
567 agi = XFS_BUF_TO_AGI(agbp);
568 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
571 * Continue where we left off before. In this case, we
572 * know that the allocation group has free inodes.
575 agi = XFS_BUF_TO_AGI(agbp);
576 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
577 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
580 agcount = mp->m_sb.sb_agcount;
581 agno = be32_to_cpu(agi->agi_seqno);
583 pagno = XFS_INO_TO_AGNO(mp, parent);
584 pagino = XFS_INO_TO_AGINO(mp, parent);
587 * If we have already hit the ceiling of inode blocks then clear
588 * okalloc so we scan all available agi structures for a free
592 if (mp->m_maxicount &&
593 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
599 * Loop until we find an allocation group that either has free inodes
600 * or in which we can allocate some inodes. Iterate through the
601 * allocation groups upward, wrapping at the end.
603 *alloc_done = B_FALSE;
604 while (!agi->agi_freecount) {
606 * Don't do anything if we're not supposed to allocate
607 * any blocks, just go on to the next ag.
611 * Try to allocate some new inodes in the allocation
614 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
615 xfs_trans_brelse(tp, agbp);
616 if (error == ENOSPC) {
624 * We successfully allocated some inodes, return
625 * the current context to the caller so that it
626 * can commit the current transaction and call
627 * us again where we left off.
629 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
630 *alloc_done = B_TRUE;
637 * If it failed, give up on this ag.
639 xfs_trans_brelse(tp, agbp);
641 * Go on to the next ag: get its ag header.
644 if (++tagno == agcount)
648 return noroom ? ENOSPC : 0;
650 down_read(&mp->m_peraglock);
651 if (mp->m_perag[tagno].pagi_inodeok == 0) {
652 up_read(&mp->m_peraglock);
655 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
656 up_read(&mp->m_peraglock);
659 agi = XFS_BUF_TO_AGI(agbp);
660 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
663 * Here with an allocation group that has a free inode.
664 * Reset agno since we may have chosen a new ag in the
669 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
670 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
672 * If pagino is 0 (this is the root inode allocation) use newino.
673 * This must work because we've just allocated some.
676 pagino = be32_to_cpu(agi->agi_newino);
678 if (cur->bc_nlevels == 1) {
681 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
683 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
685 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
686 &rec.ir_freecount, &rec.ir_free, &i)))
688 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
689 freecount += rec.ir_freecount;
690 if ((error = xfs_inobt_increment(cur, 0, &i)))
694 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
695 XFS_FORCED_SHUTDOWN(mp));
699 * If in the same a.g. as the parent, try to get near the parent.
702 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
705 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
706 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
708 rec.ir_freecount > 0) {
710 * Found a free inode in the same chunk
715 * In the same a.g. as parent, but parent's chunk is full.
718 int doneleft; /* done, to the left */
719 int doneright; /* done, to the right */
726 * Duplicate the cursor, search left & right
729 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
732 * Search left with tcur, back up 1 record.
734 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
738 if ((error = xfs_inobt_get_rec(tcur,
743 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
746 * Search right with cur, go forward 1 record.
748 if ((error = xfs_inobt_increment(cur, 0, &i)))
752 if ((error = xfs_inobt_get_rec(cur,
757 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
760 * Loop until we find the closest inode chunk
763 while (!doneleft || !doneright) {
764 int useleft; /* using left inode
768 * Figure out which block is closer,
771 if (!doneleft && !doneright)
775 XFS_INODES_PER_CHUNK - 1) <
776 rec.ir_startino - pagino;
780 * If checking the left, does it have
783 if (useleft && trec.ir_freecount) {
785 * Yes, set it up as the chunk to use.
788 xfs_btree_del_cursor(cur,
794 * If checking the right, does it have
797 if (!useleft && rec.ir_freecount) {
799 * Yes, it's already set up.
801 xfs_btree_del_cursor(tcur,
806 * If used the left, get another one
810 if ((error = xfs_inobt_decrement(tcur, 0,
815 if ((error = xfs_inobt_get_rec(
821 XFS_WANT_CORRUPTED_GOTO(i == 1,
826 * If used the right, get another one
830 if ((error = xfs_inobt_increment(cur, 0,
835 if ((error = xfs_inobt_get_rec(
841 XFS_WANT_CORRUPTED_GOTO(i == 1,
846 ASSERT(!doneleft || !doneright);
850 * In a different a.g. from the parent.
851 * See if the most recently allocated block has any free.
853 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
854 if ((error = xfs_inobt_lookup_eq(cur,
855 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
858 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
859 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
861 rec.ir_freecount > 0) {
863 * The last chunk allocated in the group still has
868 * None left in the last group, search the whole a.g.
873 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
877 if ((error = xfs_inobt_get_rec(cur,
879 &rec.ir_freecount, &rec.ir_free,
882 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
883 if (rec.ir_freecount > 0)
885 if ((error = xfs_inobt_increment(cur, 0, &i)))
887 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
891 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
893 ASSERT(offset < XFS_INODES_PER_CHUNK);
894 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
895 XFS_INODES_PER_CHUNK) == 0);
896 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
897 XFS_INOBT_CLR_FREE(&rec, offset);
899 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
902 be32_add_cpu(&agi->agi_freecount, -1);
903 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
904 down_read(&mp->m_peraglock);
905 mp->m_perag[tagno].pagi_freecount--;
906 up_read(&mp->m_peraglock);
908 if (cur->bc_nlevels == 1) {
911 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
914 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
915 &rec.ir_freecount, &rec.ir_free, &i)))
917 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
918 freecount += rec.ir_freecount;
919 if ((error = xfs_inobt_increment(cur, 0, &i)))
922 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
923 XFS_FORCED_SHUTDOWN(mp));
926 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
927 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
931 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
933 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
938 * Free disk inode. Carefully avoids touching the incore inode, all
939 * manipulations incore are the caller's responsibility.
940 * The on-disk inode is not changed by this operation, only the
941 * btree (free inode mask) is changed.
945 xfs_trans_t *tp, /* transaction pointer */
946 xfs_ino_t inode, /* inode to be freed */
947 xfs_bmap_free_t *flist, /* extents to free */
948 int *delete, /* set if inode cluster was deleted */
949 xfs_ino_t *first_ino) /* first inode in deleted cluster */
952 xfs_agblock_t agbno; /* block number containing inode */
953 xfs_buf_t *agbp; /* buffer containing allocation group header */
954 xfs_agino_t agino; /* inode number relative to allocation group */
955 xfs_agnumber_t agno; /* allocation group number */
956 xfs_agi_t *agi; /* allocation group header */
957 xfs_btree_cur_t *cur; /* inode btree cursor */
958 int error; /* error return value */
959 int i; /* result code */
960 int ilen; /* inodes in an inode cluster */
961 xfs_mount_t *mp; /* mount structure for filesystem */
962 int off; /* offset of inode in inode chunk */
963 xfs_inobt_rec_incore_t rec; /* btree record */
968 * Break up inode number into its components.
970 agno = XFS_INO_TO_AGNO(mp, inode);
971 if (agno >= mp->m_sb.sb_agcount) {
973 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
974 agno, mp->m_sb.sb_agcount, mp->m_fsname);
976 return XFS_ERROR(EINVAL);
978 agino = XFS_INO_TO_AGINO(mp, inode);
979 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
981 "xfs_difree: inode != XFS_AGINO_TO_INO() "
982 "(%llu != %llu) on %s. Returning EINVAL.",
983 (unsigned long long)inode,
984 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
987 return XFS_ERROR(EINVAL);
989 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
990 if (agbno >= mp->m_sb.sb_agblocks) {
992 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
993 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
995 return XFS_ERROR(EINVAL);
998 * Get the allocation group header.
1000 down_read(&mp->m_peraglock);
1001 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1002 up_read(&mp->m_peraglock);
1005 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1006 error, mp->m_fsname);
1009 agi = XFS_BUF_TO_AGI(agbp);
1010 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1011 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1013 * Initialize the cursor.
1015 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1016 (xfs_inode_t *)0, 0);
1018 if (cur->bc_nlevels == 1) {
1021 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1024 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1025 &rec.ir_freecount, &rec.ir_free, &i)))
1028 freecount += rec.ir_freecount;
1029 if ((error = xfs_inobt_increment(cur, 0, &i)))
1033 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1034 XFS_FORCED_SHUTDOWN(mp));
1038 * Look for the entry describing this inode.
1040 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1042 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1043 error, mp->m_fsname);
1046 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1047 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1048 &rec.ir_free, &i))) {
1050 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1051 error, mp->m_fsname);
1054 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1056 * Get the offset in the inode chunk.
1058 off = agino - rec.ir_startino;
1059 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1060 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1062 * Mark the inode free & increment the count.
1064 XFS_INOBT_SET_FREE(&rec, off);
1068 * When an inode cluster is free, it becomes eligible for removal
1070 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1071 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1074 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1077 * Remove the inode cluster from the AGI B+Tree, adjust the
1078 * AGI and Superblock inode counts, and mark the disk space
1079 * to be freed when the transaction is committed.
1081 ilen = XFS_IALLOC_INODES(mp);
1082 be32_add_cpu(&agi->agi_count, -ilen);
1083 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1084 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1085 down_read(&mp->m_peraglock);
1086 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1087 up_read(&mp->m_peraglock);
1088 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1089 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1091 if ((error = xfs_inobt_delete(cur, &i))) {
1092 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1093 error, mp->m_fsname);
1097 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1098 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1099 XFS_IALLOC_BLOCKS(mp), flist, mp);
1103 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1105 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1106 error, mp->m_fsname);
1110 * Change the inode free counts and log the ag/sb changes.
1112 be32_add_cpu(&agi->agi_freecount, 1);
1113 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1114 down_read(&mp->m_peraglock);
1115 mp->m_perag[agno].pagi_freecount++;
1116 up_read(&mp->m_peraglock);
1117 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1121 if (cur->bc_nlevels == 1) {
1124 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1127 if ((error = xfs_inobt_get_rec(cur,
1133 freecount += rec.ir_freecount;
1134 if ((error = xfs_inobt_increment(cur, 0, &i)))
1138 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1139 XFS_FORCED_SHUTDOWN(mp));
1142 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1146 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1151 * Return the location of the inode in bno/off, for mapping it into a buffer.
1156 xfs_mount_t *mp, /* file system mount structure */
1157 xfs_trans_t *tp, /* transaction pointer */
1158 xfs_ino_t ino, /* inode to locate */
1159 xfs_fsblock_t *bno, /* output: block containing inode */
1160 int *len, /* output: num blocks in inode cluster */
1161 int *off, /* output: index in block of inode */
1162 uint flags) /* flags concerning inode lookup */
1164 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1165 xfs_buf_t *agbp; /* agi buffer */
1166 xfs_agino_t agino; /* inode number within alloc group */
1167 xfs_agnumber_t agno; /* allocation group number */
1168 int blks_per_cluster; /* num blocks per inode cluster */
1169 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1170 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1171 __int32_t chunk_cnt; /* count of free inodes in chunk */
1172 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1173 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1174 xfs_btree_cur_t *cur; /* inode btree cursor */
1175 int error; /* error code */
1176 int i; /* temp state */
1177 int offset; /* index of inode in its buffer */
1178 int offset_agbno; /* blks from chunk start to inode */
1180 ASSERT(ino != NULLFSINO);
1182 * Split up the inode number into its parts.
1184 agno = XFS_INO_TO_AGNO(mp, ino);
1185 agino = XFS_INO_TO_AGINO(mp, ino);
1186 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1187 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1188 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1190 /* no diagnostics for bulkstat, ino comes from userspace */
1191 if (flags & XFS_IMAP_BULKSTAT)
1192 return XFS_ERROR(EINVAL);
1193 if (agno >= mp->m_sb.sb_agcount) {
1194 xfs_fs_cmn_err(CE_ALERT, mp,
1195 "xfs_dilocate: agno (%d) >= "
1196 "mp->m_sb.sb_agcount (%d)",
1197 agno, mp->m_sb.sb_agcount);
1199 if (agbno >= mp->m_sb.sb_agblocks) {
1200 xfs_fs_cmn_err(CE_ALERT, mp,
1201 "xfs_dilocate: agbno (0x%llx) >= "
1202 "mp->m_sb.sb_agblocks (0x%lx)",
1203 (unsigned long long) agbno,
1204 (unsigned long) mp->m_sb.sb_agblocks);
1206 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1207 xfs_fs_cmn_err(CE_ALERT, mp,
1208 "xfs_dilocate: ino (0x%llx) != "
1209 "XFS_AGINO_TO_INO(mp, agno, agino) "
1211 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1215 return XFS_ERROR(EINVAL);
1217 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1218 !(flags & XFS_IMAP_LOOKUP)) {
1219 offset = XFS_INO_TO_OFFSET(mp, ino);
1220 ASSERT(offset < mp->m_sb.sb_inopblock);
1221 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1226 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1227 if (*bno != NULLFSBLOCK) {
1228 offset = XFS_INO_TO_OFFSET(mp, ino);
1229 ASSERT(offset < mp->m_sb.sb_inopblock);
1230 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1231 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1233 *len = blks_per_cluster;
1236 if (mp->m_inoalign_mask) {
1237 offset_agbno = agbno & mp->m_inoalign_mask;
1238 chunk_agbno = agbno - offset_agbno;
1240 down_read(&mp->m_peraglock);
1241 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1242 up_read(&mp->m_peraglock);
1245 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1246 "xfs_ialloc_read_agi() returned "
1247 "error %d, agno %d",
1252 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1253 (xfs_inode_t *)0, 0);
1254 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1256 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1257 "xfs_inobt_lookup_le() failed");
1261 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1262 &chunk_free, &i))) {
1264 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1265 "xfs_inobt_get_rec() failed");
1271 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1272 "xfs_inobt_get_rec() failed");
1274 error = XFS_ERROR(EINVAL);
1276 xfs_trans_brelse(tp, agbp);
1277 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1280 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1281 offset_agbno = agbno - chunk_agbno;
1283 ASSERT(agbno >= chunk_agbno);
1284 cluster_agbno = chunk_agbno +
1285 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1286 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1287 XFS_INO_TO_OFFSET(mp, ino);
1288 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1290 *len = blks_per_cluster;
1293 xfs_trans_brelse(tp, agbp);
1294 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1299 * Compute and fill in value of m_in_maxlevels.
1302 xfs_ialloc_compute_maxlevels(
1303 xfs_mount_t *mp) /* file system mount structure */
1311 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1312 XFS_INODES_PER_CHUNK_LOG;
1313 minleafrecs = mp->m_alloc_mnr[0];
1314 minnoderecs = mp->m_alloc_mnr[1];
1315 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1316 for (level = 1; maxblocks > 1; level++)
1317 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1318 mp->m_in_maxlevels = level;
1322 * Log specified fields for the ag hdr (inode section)
1326 xfs_trans_t *tp, /* transaction pointer */
1327 xfs_buf_t *bp, /* allocation group header buffer */
1328 int fields) /* bitmask of fields to log */
1330 int first; /* first byte number */
1331 int last; /* last byte number */
1332 static const short offsets[] = { /* field starting offsets */
1333 /* keep in sync with bit definitions */
1334 offsetof(xfs_agi_t, agi_magicnum),
1335 offsetof(xfs_agi_t, agi_versionnum),
1336 offsetof(xfs_agi_t, agi_seqno),
1337 offsetof(xfs_agi_t, agi_length),
1338 offsetof(xfs_agi_t, agi_count),
1339 offsetof(xfs_agi_t, agi_root),
1340 offsetof(xfs_agi_t, agi_level),
1341 offsetof(xfs_agi_t, agi_freecount),
1342 offsetof(xfs_agi_t, agi_newino),
1343 offsetof(xfs_agi_t, agi_dirino),
1344 offsetof(xfs_agi_t, agi_unlinked),
1348 xfs_agi_t *agi; /* allocation group header */
1350 agi = XFS_BUF_TO_AGI(bp);
1351 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1354 * Compute byte offsets for the first and last fields.
1356 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1358 * Log the allocation group inode header buffer.
1360 xfs_trans_log_buf(tp, bp, first, last);
1364 * Read in the allocation group header (inode allocation section)
1367 xfs_ialloc_read_agi(
1368 xfs_mount_t *mp, /* file system mount structure */
1369 xfs_trans_t *tp, /* transaction pointer */
1370 xfs_agnumber_t agno, /* allocation group number */
1371 xfs_buf_t **bpp) /* allocation group hdr buf */
1373 xfs_agi_t *agi; /* allocation group header */
1374 int agi_ok; /* agi is consistent */
1375 xfs_buf_t *bp; /* allocation group hdr buf */
1376 xfs_perag_t *pag; /* per allocation group data */
1379 ASSERT(agno != NULLAGNUMBER);
1380 error = xfs_trans_read_buf(
1381 mp, tp, mp->m_ddev_targp,
1382 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1383 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1386 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1389 * Validate the magic number of the agi block.
1391 agi = XFS_BUF_TO_AGI(bp);
1393 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1394 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1395 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1396 XFS_RANDOM_IALLOC_READ_AGI))) {
1397 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1399 xfs_trans_brelse(tp, bp);
1400 return XFS_ERROR(EFSCORRUPTED);
1402 pag = &mp->m_perag[agno];
1403 if (!pag->pagi_init) {
1404 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1405 pag->pagi_count = be32_to_cpu(agi->agi_count);
1409 * It's possible for these to be out of sync if
1410 * we are in the middle of a forced shutdown.
1412 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1413 XFS_FORCED_SHUTDOWN(mp));
1420 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1421 ASSERT(agi->agi_unlinked[i]);
1425 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1431 * Read in the agi to initialise the per-ag data in the mount structure
1434 xfs_ialloc_pagi_init(
1435 xfs_mount_t *mp, /* file system mount structure */
1436 xfs_trans_t *tp, /* transaction pointer */
1437 xfs_agnumber_t agno) /* allocation group number */
1439 xfs_buf_t *bp = NULL;
1442 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1446 xfs_trans_brelse(tp, bp);