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 = 0; /* 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 * First try to allocate inodes contiguous with the last-allocated
156 * chunk of inodes. If the filesystem is striped, this will fill
157 * an entire stripe unit with inodes.
159 agi = XFS_BUF_TO_AGI(agbp);
160 newino = be32_to_cpu(agi->agi_newino);
161 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
162 XFS_IALLOC_BLOCKS(args.mp);
163 if (likely(newino != NULLAGINO &&
164 (args.agbno < be32_to_cpu(agi->agi_length)))) {
165 args.fsbno = XFS_AGB_TO_FSB(args.mp,
166 be32_to_cpu(agi->agi_seqno), args.agbno);
167 args.type = XFS_ALLOCTYPE_THIS_BNO;
168 args.mod = args.total = args.wasdel = args.isfl =
169 args.userdata = args.minalignslop = 0;
173 * Allow space for the inode btree to split.
175 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
176 if ((error = xfs_alloc_vextent(&args)))
179 args.fsbno = NULLFSBLOCK;
181 if (unlikely(args.fsbno == NULLFSBLOCK)) {
183 * Set the alignment for the allocation.
184 * If stripe alignment is turned on then align at stripe unit
186 * If the cluster size is smaller than a filesystem block
187 * then we're doing I/O for inodes in filesystem block size
188 * pieces, so don't need alignment anyway.
191 if (args.mp->m_sinoalign) {
192 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
193 args.alignment = args.mp->m_dalign;
195 } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
196 args.mp->m_sb.sb_inoalignmt >=
197 XFS_B_TO_FSBT(args.mp,
198 XFS_INODE_CLUSTER_SIZE(args.mp)))
199 args.alignment = args.mp->m_sb.sb_inoalignmt;
203 * Need to figure out where to allocate the inode blocks.
204 * Ideally they should be spaced out through the a.g.
205 * For now, just allocate blocks up front.
207 args.agbno = be32_to_cpu(agi->agi_root);
208 args.fsbno = XFS_AGB_TO_FSB(args.mp,
209 be32_to_cpu(agi->agi_seqno), args.agbno);
211 * Allocate a fixed-size extent of inodes.
213 args.type = XFS_ALLOCTYPE_NEAR_BNO;
214 args.mod = args.total = args.wasdel = args.isfl =
215 args.userdata = args.minalignslop = 0;
218 * Allow space for the inode btree to split.
220 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
221 if ((error = xfs_alloc_vextent(&args)))
226 * If stripe alignment is turned on, then try again with cluster
229 if (isaligned && args.fsbno == NULLFSBLOCK) {
230 args.type = XFS_ALLOCTYPE_NEAR_BNO;
231 args.agbno = be32_to_cpu(agi->agi_root);
232 args.fsbno = XFS_AGB_TO_FSB(args.mp,
233 be32_to_cpu(agi->agi_seqno), args.agbno);
234 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
235 args.mp->m_sb.sb_inoalignmt >=
236 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
237 args.alignment = args.mp->m_sb.sb_inoalignmt;
240 if ((error = xfs_alloc_vextent(&args)))
244 if (args.fsbno == NULLFSBLOCK) {
248 ASSERT(args.len == args.minlen);
250 * Convert the results.
252 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
254 * Loop over the new block(s), filling in the inodes.
255 * For small block sizes, manipulate the inodes in buffers
256 * which are multiples of the blocks size.
258 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
259 blks_per_cluster = 1;
260 nbufs = (int)args.len;
261 ninodes = args.mp->m_sb.sb_inopblock;
263 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
264 args.mp->m_sb.sb_blocksize;
265 nbufs = (int)args.len / blks_per_cluster;
266 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
269 * Figure out what version number to use in the inodes we create.
270 * If the superblock version has caught up to the one that supports
271 * the new inode format, then use the new inode version. Otherwise
272 * use the old version so that old kernels will continue to be
273 * able to use the file system.
275 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
276 version = XFS_DINODE_VERSION_2;
278 version = XFS_DINODE_VERSION_1;
280 for (j = 0; j < nbufs; j++) {
284 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
285 args.agbno + (j * blks_per_cluster));
286 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
287 args.mp->m_bsize * blks_per_cluster,
290 ASSERT(!XFS_BUF_GETERROR(fbuf));
292 * Set initial values for the inodes in this buffer.
294 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
295 for (i = 0; i < ninodes; i++) {
296 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
297 INT_SET(free->di_core.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
298 INT_SET(free->di_core.di_version, ARCH_CONVERT, version);
299 INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
300 xfs_ialloc_log_di(tp, fbuf, i,
301 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
303 xfs_trans_inode_alloc_buf(tp, fbuf);
305 be32_add(&agi->agi_count, newlen);
306 be32_add(&agi->agi_freecount, newlen);
307 down_read(&args.mp->m_peraglock);
308 args.mp->m_perag[be32_to_cpu(agi->agi_seqno)].pagi_freecount += newlen;
309 up_read(&args.mp->m_peraglock);
310 agi->agi_newino = cpu_to_be32(newino);
312 * Insert records describing the new inode chunk into the btree.
314 cur = xfs_btree_init_cursor(args.mp, tp, agbp,
315 be32_to_cpu(agi->agi_seqno),
316 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
317 for (thisino = newino;
318 thisino < newino + newlen;
319 thisino += XFS_INODES_PER_CHUNK) {
320 if ((error = xfs_inobt_lookup_eq(cur, thisino,
321 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
322 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
326 if ((error = xfs_inobt_insert(cur, &i))) {
327 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
332 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
334 * Log allocation group header fields
336 xfs_ialloc_log_agi(tp, agbp,
337 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
339 * Modify/log superblock values for inode count and inode free count.
341 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
342 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
347 STATIC __inline xfs_agnumber_t
353 spin_lock(&mp->m_agirotor_lock);
354 agno = mp->m_agirotor;
355 if (++mp->m_agirotor == mp->m_maxagi)
357 spin_unlock(&mp->m_agirotor_lock);
363 * Select an allocation group to look for a free inode in, based on the parent
364 * inode and then mode. Return the allocation group buffer.
366 STATIC xfs_buf_t * /* allocation group buffer */
367 xfs_ialloc_ag_select(
368 xfs_trans_t *tp, /* transaction pointer */
369 xfs_ino_t parent, /* parent directory inode number */
370 mode_t mode, /* bits set to indicate file type */
371 int okalloc) /* ok to allocate more space */
373 xfs_buf_t *agbp; /* allocation group header buffer */
374 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
375 xfs_agnumber_t agno; /* current ag number */
376 int flags; /* alloc buffer locking flags */
377 xfs_extlen_t ineed; /* blocks needed for inode allocation */
378 xfs_extlen_t longest = 0; /* longest extent available */
379 xfs_mount_t *mp; /* mount point structure */
380 int needspace; /* file mode implies space allocated */
381 xfs_perag_t *pag; /* per allocation group data */
382 xfs_agnumber_t pagno; /* parent (starting) ag number */
385 * Files of these types need at least one block if length > 0
386 * (and they won't fit in the inode, but that's hard to figure out).
388 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
390 agcount = mp->m_maxagi;
392 pagno = xfs_ialloc_next_ag(mp);
394 pagno = XFS_INO_TO_AGNO(mp, parent);
395 if (pagno >= agcount)
398 ASSERT(pagno < agcount);
400 * Loop through allocation groups, looking for one with a little
401 * free space in it. Note we don't look for free inodes, exactly.
402 * Instead, we include whether there is a need to allocate inodes
403 * to mean that blocks must be allocated for them,
404 * if none are currently free.
407 flags = XFS_ALLOC_FLAG_TRYLOCK;
408 down_read(&mp->m_peraglock);
410 pag = &mp->m_perag[agno];
411 if (!pag->pagi_init) {
412 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
419 if (!pag->pagi_inodeok) {
420 xfs_ialloc_next_ag(mp);
425 * Is there enough free space for the file plus a block
426 * of inodes (if we need to allocate some)?
428 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
429 if (ineed && !pag->pagf_init) {
431 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
435 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
437 if (!ineed || pag->pagf_init) {
438 if (ineed && !(longest = pag->pagf_longest))
439 longest = pag->pagf_flcount > 0;
441 (pag->pagf_freeblks >= needspace + ineed &&
445 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
449 up_read(&mp->m_peraglock);
455 xfs_trans_brelse(tp, agbp);
458 * No point in iterating over the rest, if we're shutting
461 if (XFS_FORCED_SHUTDOWN(mp)) {
462 up_read(&mp->m_peraglock);
463 return (xfs_buf_t *)0;
470 up_read(&mp->m_peraglock);
471 return (xfs_buf_t *)0;
479 * Visible inode allocation functions.
483 * Allocate an inode on disk.
484 * Mode is used to tell whether the new inode will need space, and whether
487 * The arguments IO_agbp and alloc_done are defined to work within
488 * the constraint of one allocation per transaction.
489 * xfs_dialloc() is designed to be called twice if it has to do an
490 * allocation to make more free inodes. On the first call,
491 * IO_agbp should be set to NULL. If an inode is available,
492 * i.e., xfs_dialloc() did not need to do an allocation, an inode
493 * number is returned. In this case, IO_agbp would be set to the
494 * current ag_buf and alloc_done set to false.
495 * If an allocation needed to be done, xfs_dialloc would return
496 * the current ag_buf in IO_agbp and set alloc_done to true.
497 * The caller should then commit the current transaction, allocate a new
498 * transaction, and call xfs_dialloc() again, passing in the previous
499 * value of IO_agbp. IO_agbp should be held across the transactions.
500 * Since the agbp is locked across the two calls, the second call is
501 * guaranteed to have a free inode available.
503 * Once we successfully pick an inode its number is returned and the
504 * on-disk data structures are updated. The inode itself is not read
505 * in, since doing so would break ordering constraints with xfs_reclaim.
509 xfs_trans_t *tp, /* transaction pointer */
510 xfs_ino_t parent, /* parent inode (directory) */
511 mode_t mode, /* mode bits for new inode */
512 int okalloc, /* ok to allocate more space */
513 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
514 boolean_t *alloc_done, /* true if we needed to replenish
516 xfs_ino_t *inop) /* inode number allocated */
518 xfs_agnumber_t agcount; /* number of allocation groups */
519 xfs_buf_t *agbp; /* allocation group header's buffer */
520 xfs_agnumber_t agno; /* allocation group number */
521 xfs_agi_t *agi; /* allocation group header structure */
522 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
523 int error; /* error return value */
524 int i; /* result code */
525 int ialloced; /* inode allocation status */
526 int noroom = 0; /* no space for inode blk allocation */
527 xfs_ino_t ino; /* fs-relative inode to be returned */
529 int j; /* result code */
530 xfs_mount_t *mp; /* file system mount structure */
531 int offset; /* index of inode in chunk */
532 xfs_agino_t pagino; /* parent's a.g. relative inode # */
533 xfs_agnumber_t pagno; /* parent's allocation group number */
534 xfs_inobt_rec_t rec; /* inode allocation record */
535 xfs_agnumber_t tagno; /* testing allocation group number */
536 xfs_btree_cur_t *tcur; /* temp cursor */
537 xfs_inobt_rec_t trec; /* temp inode allocation record */
540 if (*IO_agbp == NULL) {
542 * We do not have an agbp, so select an initial allocation
543 * group for inode allocation.
545 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
547 * Couldn't find an allocation group satisfying the
554 agi = XFS_BUF_TO_AGI(agbp);
555 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
558 * Continue where we left off before. In this case, we
559 * know that the allocation group has free inodes.
562 agi = XFS_BUF_TO_AGI(agbp);
563 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
564 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
567 agcount = mp->m_sb.sb_agcount;
568 agno = be32_to_cpu(agi->agi_seqno);
570 pagno = XFS_INO_TO_AGNO(mp, parent);
571 pagino = XFS_INO_TO_AGINO(mp, parent);
574 * If we have already hit the ceiling of inode blocks then clear
575 * okalloc so we scan all available agi structures for a free
579 if (mp->m_maxicount &&
580 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
586 * Loop until we find an allocation group that either has free inodes
587 * or in which we can allocate some inodes. Iterate through the
588 * allocation groups upward, wrapping at the end.
590 *alloc_done = B_FALSE;
591 while (!agi->agi_freecount) {
593 * Don't do anything if we're not supposed to allocate
594 * any blocks, just go on to the next ag.
598 * Try to allocate some new inodes in the allocation
601 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
602 xfs_trans_brelse(tp, agbp);
603 if (error == ENOSPC) {
611 * We successfully allocated some inodes, return
612 * the current context to the caller so that it
613 * can commit the current transaction and call
614 * us again where we left off.
616 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
617 *alloc_done = B_TRUE;
624 * If it failed, give up on this ag.
626 xfs_trans_brelse(tp, agbp);
628 * Go on to the next ag: get its ag header.
631 if (++tagno == agcount)
635 return noroom ? ENOSPC : 0;
637 down_read(&mp->m_peraglock);
638 if (mp->m_perag[tagno].pagi_inodeok == 0) {
639 up_read(&mp->m_peraglock);
642 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
643 up_read(&mp->m_peraglock);
646 agi = XFS_BUF_TO_AGI(agbp);
647 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
650 * Here with an allocation group that has a free inode.
651 * Reset agno since we may have chosen a new ag in the
656 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
657 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
659 * If pagino is 0 (this is the root inode allocation) use newino.
660 * This must work because we've just allocated some.
663 pagino = be32_to_cpu(agi->agi_newino);
665 if (cur->bc_nlevels == 1) {
668 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
670 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
672 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
673 &rec.ir_freecount, &rec.ir_free, &i)))
675 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
676 freecount += rec.ir_freecount;
677 if ((error = xfs_inobt_increment(cur, 0, &i)))
681 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
682 XFS_FORCED_SHUTDOWN(mp));
686 * If in the same a.g. as the parent, try to get near the parent.
689 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
692 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
693 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
695 rec.ir_freecount > 0) {
697 * Found a free inode in the same chunk
702 * In the same a.g. as parent, but parent's chunk is full.
705 int doneleft; /* done, to the left */
706 int doneright; /* done, to the right */
713 * Duplicate the cursor, search left & right
716 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
719 * Search left with tcur, back up 1 record.
721 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
725 if ((error = xfs_inobt_get_rec(tcur,
730 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
733 * Search right with cur, go forward 1 record.
735 if ((error = xfs_inobt_increment(cur, 0, &i)))
739 if ((error = xfs_inobt_get_rec(cur,
744 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
747 * Loop until we find the closest inode chunk
750 while (!doneleft || !doneright) {
751 int useleft; /* using left inode
755 * Figure out which block is closer,
758 if (!doneleft && !doneright)
762 XFS_INODES_PER_CHUNK - 1) <
763 rec.ir_startino - pagino;
767 * If checking the left, does it have
770 if (useleft && trec.ir_freecount) {
772 * Yes, set it up as the chunk to use.
775 xfs_btree_del_cursor(cur,
781 * If checking the right, does it have
784 if (!useleft && rec.ir_freecount) {
786 * Yes, it's already set up.
788 xfs_btree_del_cursor(tcur,
793 * If used the left, get another one
797 if ((error = xfs_inobt_decrement(tcur, 0,
802 if ((error = xfs_inobt_get_rec(
808 XFS_WANT_CORRUPTED_GOTO(i == 1,
813 * If used the right, get another one
817 if ((error = xfs_inobt_increment(cur, 0,
822 if ((error = xfs_inobt_get_rec(
828 XFS_WANT_CORRUPTED_GOTO(i == 1,
833 ASSERT(!doneleft || !doneright);
837 * In a different a.g. from the parent.
838 * See if the most recently allocated block has any free.
840 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
841 if ((error = xfs_inobt_lookup_eq(cur,
842 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
845 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
846 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
848 rec.ir_freecount > 0) {
850 * The last chunk allocated in the group still has
855 * None left in the last group, search the whole a.g.
860 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
864 if ((error = xfs_inobt_get_rec(cur,
866 &rec.ir_freecount, &rec.ir_free,
869 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
870 if (rec.ir_freecount > 0)
872 if ((error = xfs_inobt_increment(cur, 0, &i)))
874 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
878 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
880 ASSERT(offset < XFS_INODES_PER_CHUNK);
881 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
882 XFS_INODES_PER_CHUNK) == 0);
883 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
884 XFS_INOBT_CLR_FREE(&rec, offset);
886 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
889 be32_add(&agi->agi_freecount, -1);
890 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
891 down_read(&mp->m_peraglock);
892 mp->m_perag[tagno].pagi_freecount--;
893 up_read(&mp->m_peraglock);
895 if (cur->bc_nlevels == 1) {
898 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
901 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
902 &rec.ir_freecount, &rec.ir_free, &i)))
904 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
905 freecount += rec.ir_freecount;
906 if ((error = xfs_inobt_increment(cur, 0, &i)))
909 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
910 XFS_FORCED_SHUTDOWN(mp));
913 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
914 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
918 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
920 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
925 * Free disk inode. Carefully avoids touching the incore inode, all
926 * manipulations incore are the caller's responsibility.
927 * The on-disk inode is not changed by this operation, only the
928 * btree (free inode mask) is changed.
932 xfs_trans_t *tp, /* transaction pointer */
933 xfs_ino_t inode, /* inode to be freed */
934 xfs_bmap_free_t *flist, /* extents to free */
935 int *delete, /* set if inode cluster was deleted */
936 xfs_ino_t *first_ino) /* first inode in deleted cluster */
939 xfs_agblock_t agbno; /* block number containing inode */
940 xfs_buf_t *agbp; /* buffer containing allocation group header */
941 xfs_agino_t agino; /* inode number relative to allocation group */
942 xfs_agnumber_t agno; /* allocation group number */
943 xfs_agi_t *agi; /* allocation group header */
944 xfs_btree_cur_t *cur; /* inode btree cursor */
945 int error; /* error return value */
946 int i; /* result code */
947 int ilen; /* inodes in an inode cluster */
948 xfs_mount_t *mp; /* mount structure for filesystem */
949 int off; /* offset of inode in inode chunk */
950 xfs_inobt_rec_t rec; /* btree record */
955 * Break up inode number into its components.
957 agno = XFS_INO_TO_AGNO(mp, inode);
958 if (agno >= mp->m_sb.sb_agcount) {
960 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
961 agno, mp->m_sb.sb_agcount, mp->m_fsname);
963 return XFS_ERROR(EINVAL);
965 agino = XFS_INO_TO_AGINO(mp, inode);
966 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
968 "xfs_difree: inode != XFS_AGINO_TO_INO() "
969 "(%llu != %llu) on %s. Returning EINVAL.",
970 (unsigned long long)inode,
971 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
974 return XFS_ERROR(EINVAL);
976 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
977 if (agbno >= mp->m_sb.sb_agblocks) {
979 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
980 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
982 return XFS_ERROR(EINVAL);
985 * Get the allocation group header.
987 down_read(&mp->m_peraglock);
988 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
989 up_read(&mp->m_peraglock);
992 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
993 error, mp->m_fsname);
996 agi = XFS_BUF_TO_AGI(agbp);
997 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
998 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1000 * Initialize the cursor.
1002 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1003 (xfs_inode_t *)0, 0);
1005 if (cur->bc_nlevels == 1) {
1008 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1011 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1012 &rec.ir_freecount, &rec.ir_free, &i)))
1015 freecount += rec.ir_freecount;
1016 if ((error = xfs_inobt_increment(cur, 0, &i)))
1020 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1021 XFS_FORCED_SHUTDOWN(mp));
1025 * Look for the entry describing this inode.
1027 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1029 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1030 error, mp->m_fsname);
1033 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1034 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1035 &rec.ir_free, &i))) {
1037 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1038 error, mp->m_fsname);
1041 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1043 * Get the offset in the inode chunk.
1045 off = agino - rec.ir_startino;
1046 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1047 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1049 * Mark the inode free & increment the count.
1051 XFS_INOBT_SET_FREE(&rec, off);
1055 * When an inode cluster is free, it becomes eligible for removal
1057 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1058 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1061 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1064 * Remove the inode cluster from the AGI B+Tree, adjust the
1065 * AGI and Superblock inode counts, and mark the disk space
1066 * to be freed when the transaction is committed.
1068 ilen = XFS_IALLOC_INODES(mp);
1069 be32_add(&agi->agi_count, -ilen);
1070 be32_add(&agi->agi_freecount, -(ilen - 1));
1071 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1072 down_read(&mp->m_peraglock);
1073 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1074 up_read(&mp->m_peraglock);
1075 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1076 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1078 if ((error = xfs_inobt_delete(cur, &i))) {
1079 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1080 error, mp->m_fsname);
1084 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1085 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1086 XFS_IALLOC_BLOCKS(mp), flist, mp);
1090 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1092 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1093 error, mp->m_fsname);
1097 * Change the inode free counts and log the ag/sb changes.
1099 be32_add(&agi->agi_freecount, 1);
1100 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1101 down_read(&mp->m_peraglock);
1102 mp->m_perag[agno].pagi_freecount++;
1103 up_read(&mp->m_peraglock);
1104 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1108 if (cur->bc_nlevels == 1) {
1111 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1114 if ((error = xfs_inobt_get_rec(cur,
1120 freecount += rec.ir_freecount;
1121 if ((error = xfs_inobt_increment(cur, 0, &i)))
1125 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1126 XFS_FORCED_SHUTDOWN(mp));
1129 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1133 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1138 * Return the location of the inode in bno/off, for mapping it into a buffer.
1143 xfs_mount_t *mp, /* file system mount structure */
1144 xfs_trans_t *tp, /* transaction pointer */
1145 xfs_ino_t ino, /* inode to locate */
1146 xfs_fsblock_t *bno, /* output: block containing inode */
1147 int *len, /* output: num blocks in inode cluster */
1148 int *off, /* output: index in block of inode */
1149 uint flags) /* flags concerning inode lookup */
1151 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1152 xfs_buf_t *agbp; /* agi buffer */
1153 xfs_agino_t agino; /* inode number within alloc group */
1154 xfs_agnumber_t agno; /* allocation group number */
1155 int blks_per_cluster; /* num blocks per inode cluster */
1156 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1157 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1158 __int32_t chunk_cnt; /* count of free inodes in chunk */
1159 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1160 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1161 xfs_btree_cur_t *cur; /* inode btree cursor */
1162 int error; /* error code */
1163 int i; /* temp state */
1164 int offset; /* index of inode in its buffer */
1165 int offset_agbno; /* blks from chunk start to inode */
1167 ASSERT(ino != NULLFSINO);
1169 * Split up the inode number into its parts.
1171 agno = XFS_INO_TO_AGNO(mp, ino);
1172 agino = XFS_INO_TO_AGINO(mp, ino);
1173 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1174 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1175 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1177 if (agno >= mp->m_sb.sb_agcount) {
1178 xfs_fs_cmn_err(CE_ALERT, mp,
1179 "xfs_dilocate: agno (%d) >= "
1180 "mp->m_sb.sb_agcount (%d)",
1181 agno, mp->m_sb.sb_agcount);
1183 if (agbno >= mp->m_sb.sb_agblocks) {
1184 xfs_fs_cmn_err(CE_ALERT, mp,
1185 "xfs_dilocate: agbno (0x%llx) >= "
1186 "mp->m_sb.sb_agblocks (0x%lx)",
1187 (unsigned long long) agbno,
1188 (unsigned long) mp->m_sb.sb_agblocks);
1190 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1191 xfs_fs_cmn_err(CE_ALERT, mp,
1192 "xfs_dilocate: ino (0x%llx) != "
1193 "XFS_AGINO_TO_INO(mp, agno, agino) "
1195 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1198 return XFS_ERROR(EINVAL);
1200 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1201 !(flags & XFS_IMAP_LOOKUP)) {
1202 offset = XFS_INO_TO_OFFSET(mp, ino);
1203 ASSERT(offset < mp->m_sb.sb_inopblock);
1204 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1209 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1210 if (*bno != NULLFSBLOCK) {
1211 offset = XFS_INO_TO_OFFSET(mp, ino);
1212 ASSERT(offset < mp->m_sb.sb_inopblock);
1213 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1214 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1216 *len = blks_per_cluster;
1219 if (mp->m_inoalign_mask) {
1220 offset_agbno = agbno & mp->m_inoalign_mask;
1221 chunk_agbno = agbno - offset_agbno;
1223 down_read(&mp->m_peraglock);
1224 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1225 up_read(&mp->m_peraglock);
1228 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1229 "xfs_ialloc_read_agi() returned "
1230 "error %d, agno %d",
1235 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1236 (xfs_inode_t *)0, 0);
1237 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1239 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1240 "xfs_inobt_lookup_le() failed");
1244 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1245 &chunk_free, &i))) {
1247 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1248 "xfs_inobt_get_rec() failed");
1254 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1255 "xfs_inobt_get_rec() failed");
1257 error = XFS_ERROR(EINVAL);
1259 xfs_trans_brelse(tp, agbp);
1260 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1263 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1264 offset_agbno = agbno - chunk_agbno;
1266 ASSERT(agbno >= chunk_agbno);
1267 cluster_agbno = chunk_agbno +
1268 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1269 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1270 XFS_INO_TO_OFFSET(mp, ino);
1271 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1273 *len = blks_per_cluster;
1276 xfs_trans_brelse(tp, agbp);
1277 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1282 * Compute and fill in value of m_in_maxlevels.
1285 xfs_ialloc_compute_maxlevels(
1286 xfs_mount_t *mp) /* file system mount structure */
1294 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1295 XFS_INODES_PER_CHUNK_LOG;
1296 minleafrecs = mp->m_alloc_mnr[0];
1297 minnoderecs = mp->m_alloc_mnr[1];
1298 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1299 for (level = 1; maxblocks > 1; level++)
1300 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1301 mp->m_in_maxlevels = level;
1305 * Log specified fields for the ag hdr (inode section)
1309 xfs_trans_t *tp, /* transaction pointer */
1310 xfs_buf_t *bp, /* allocation group header buffer */
1311 int fields) /* bitmask of fields to log */
1313 int first; /* first byte number */
1314 int last; /* last byte number */
1315 static const short offsets[] = { /* field starting offsets */
1316 /* keep in sync with bit definitions */
1317 offsetof(xfs_agi_t, agi_magicnum),
1318 offsetof(xfs_agi_t, agi_versionnum),
1319 offsetof(xfs_agi_t, agi_seqno),
1320 offsetof(xfs_agi_t, agi_length),
1321 offsetof(xfs_agi_t, agi_count),
1322 offsetof(xfs_agi_t, agi_root),
1323 offsetof(xfs_agi_t, agi_level),
1324 offsetof(xfs_agi_t, agi_freecount),
1325 offsetof(xfs_agi_t, agi_newino),
1326 offsetof(xfs_agi_t, agi_dirino),
1327 offsetof(xfs_agi_t, agi_unlinked),
1331 xfs_agi_t *agi; /* allocation group header */
1333 agi = XFS_BUF_TO_AGI(bp);
1334 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1337 * Compute byte offsets for the first and last fields.
1339 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1341 * Log the allocation group inode header buffer.
1343 xfs_trans_log_buf(tp, bp, first, last);
1347 * Read in the allocation group header (inode allocation section)
1350 xfs_ialloc_read_agi(
1351 xfs_mount_t *mp, /* file system mount structure */
1352 xfs_trans_t *tp, /* transaction pointer */
1353 xfs_agnumber_t agno, /* allocation group number */
1354 xfs_buf_t **bpp) /* allocation group hdr buf */
1356 xfs_agi_t *agi; /* allocation group header */
1357 int agi_ok; /* agi is consistent */
1358 xfs_buf_t *bp; /* allocation group hdr buf */
1359 xfs_perag_t *pag; /* per allocation group data */
1362 ASSERT(agno != NULLAGNUMBER);
1363 error = xfs_trans_read_buf(
1364 mp, tp, mp->m_ddev_targp,
1365 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1366 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1369 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1372 * Validate the magic number of the agi block.
1374 agi = XFS_BUF_TO_AGI(bp);
1376 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1377 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1378 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1379 XFS_RANDOM_IALLOC_READ_AGI))) {
1380 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1382 xfs_trans_brelse(tp, bp);
1383 return XFS_ERROR(EFSCORRUPTED);
1385 pag = &mp->m_perag[agno];
1386 if (!pag->pagi_init) {
1387 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1391 * It's possible for these to be out of sync if
1392 * we are in the middle of a forced shutdown.
1394 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1395 XFS_FORCED_SHUTDOWN(mp));
1402 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1403 ASSERT(agi->agi_unlinked[i]);
1407 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);