2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_bmap_btree.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dinode.h"
52 #include "xfs_inode.h"
53 #include "xfs_btree.h"
54 #include "xfs_ialloc.h"
55 #include "xfs_alloc.h"
56 #include "xfs_rtalloc.h"
57 #include "xfs_error.h"
61 * Log specified fields for the inode given by bp and off.
65 xfs_trans_t *tp, /* transaction pointer */
66 xfs_buf_t *bp, /* inode buffer */
67 int off, /* index of inode in buffer */
68 int fields) /* bitmask of fields to log */
70 int first; /* first byte number */
71 int ioffset; /* off in bytes */
72 int last; /* last byte number */
73 xfs_mount_t *mp; /* mount point structure */
74 static const short offsets[] = { /* field offsets */
75 /* keep in sync with bits */
76 offsetof(xfs_dinode_core_t, di_magic),
77 offsetof(xfs_dinode_core_t, di_mode),
78 offsetof(xfs_dinode_core_t, di_version),
79 offsetof(xfs_dinode_core_t, di_format),
80 offsetof(xfs_dinode_core_t, di_onlink),
81 offsetof(xfs_dinode_core_t, di_uid),
82 offsetof(xfs_dinode_core_t, di_gid),
83 offsetof(xfs_dinode_core_t, di_nlink),
84 offsetof(xfs_dinode_core_t, di_projid),
85 offsetof(xfs_dinode_core_t, di_pad),
86 offsetof(xfs_dinode_core_t, di_atime),
87 offsetof(xfs_dinode_core_t, di_mtime),
88 offsetof(xfs_dinode_core_t, di_ctime),
89 offsetof(xfs_dinode_core_t, di_size),
90 offsetof(xfs_dinode_core_t, di_nblocks),
91 offsetof(xfs_dinode_core_t, di_extsize),
92 offsetof(xfs_dinode_core_t, di_nextents),
93 offsetof(xfs_dinode_core_t, di_anextents),
94 offsetof(xfs_dinode_core_t, di_forkoff),
95 offsetof(xfs_dinode_core_t, di_aformat),
96 offsetof(xfs_dinode_core_t, di_dmevmask),
97 offsetof(xfs_dinode_core_t, di_dmstate),
98 offsetof(xfs_dinode_core_t, di_flags),
99 offsetof(xfs_dinode_core_t, di_gen),
100 offsetof(xfs_dinode_t, di_next_unlinked),
101 offsetof(xfs_dinode_t, di_u),
102 offsetof(xfs_dinode_t, di_a),
107 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
108 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
111 * Get the inode-relative first and last bytes for these fields
113 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
115 * Convert to buffer offsets and log it.
117 ioffset = off << mp->m_sb.sb_inodelog;
120 xfs_trans_log_buf(tp, bp, first, last);
124 * Allocation group level functions.
128 * Allocate new inodes in the allocation group specified by agbp.
129 * Return 0 for success, else error code.
131 STATIC int /* error code or 0 */
133 xfs_trans_t *tp, /* transaction pointer */
134 xfs_buf_t *agbp, /* alloc group buffer */
137 xfs_agi_t *agi; /* allocation group header */
138 xfs_alloc_arg_t args; /* allocation argument structure */
139 int blks_per_cluster; /* fs blocks per inode cluster */
140 xfs_btree_cur_t *cur; /* inode btree cursor */
141 xfs_daddr_t d; /* disk addr of buffer */
143 xfs_buf_t *fbuf; /* new free inodes' buffer */
144 xfs_dinode_t *free; /* new free inode structure */
145 int i; /* inode counter */
146 int j; /* block counter */
147 int nbufs; /* num bufs of new inodes */
148 xfs_agino_t newino; /* new first inode's number */
149 xfs_agino_t newlen; /* new number of inodes */
150 int ninodes; /* num inodes per buf */
151 xfs_agino_t thisino; /* current inode number, for loop */
152 int version; /* inode version number to use */
153 int isaligned; /* inode allocation at stripe unit */
155 xfs_dinode_core_t dic; /* a dinode_core to copy to new */
159 args.mp = tp->t_mountp;
162 * Locking will ensure that we don't have two callers in here
165 newlen = XFS_IALLOC_INODES(args.mp);
166 if (args.mp->m_maxicount &&
167 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
168 return XFS_ERROR(ENOSPC);
169 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
171 * Set the alignment for the allocation.
172 * If stripe alignment is turned on then align at stripe unit
174 * If the cluster size is smaller than a filesystem block
175 * then we're doing I/O for inodes in filesystem block size pieces,
176 * so don't need alignment anyway.
179 if (args.mp->m_sinoalign) {
180 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
181 args.alignment = args.mp->m_dalign;
183 } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
184 args.mp->m_sb.sb_inoalignmt >=
185 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
186 args.alignment = args.mp->m_sb.sb_inoalignmt;
189 agi = XFS_BUF_TO_AGI(agbp);
191 * Need to figure out where to allocate the inode blocks.
192 * Ideally they should be spaced out through the a.g.
193 * For now, just allocate blocks up front.
195 args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
196 args.fsbno = XFS_AGB_TO_FSB(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
199 * Allocate a fixed-size extent of inodes.
201 args.type = XFS_ALLOCTYPE_NEAR_BNO;
202 args.mod = args.total = args.wasdel = args.isfl = args.userdata =
203 args.minalignslop = 0;
206 * Allow space for the inode btree to split.
208 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
209 if ((error = xfs_alloc_vextent(&args)))
213 * If stripe alignment is turned on, then try again with cluster
216 if (isaligned && args.fsbno == NULLFSBLOCK) {
217 args.type = XFS_ALLOCTYPE_NEAR_BNO;
218 args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
219 args.fsbno = XFS_AGB_TO_FSB(args.mp,
220 INT_GET(agi->agi_seqno, ARCH_CONVERT), args.agbno);
221 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
222 args.mp->m_sb.sb_inoalignmt >=
223 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
224 args.alignment = args.mp->m_sb.sb_inoalignmt;
227 if ((error = xfs_alloc_vextent(&args)))
231 if (args.fsbno == NULLFSBLOCK) {
235 ASSERT(args.len == args.minlen);
237 * Convert the results.
239 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
241 * Loop over the new block(s), filling in the inodes.
242 * For small block sizes, manipulate the inodes in buffers
243 * which are multiples of the blocks size.
245 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
246 blks_per_cluster = 1;
247 nbufs = (int)args.len;
248 ninodes = args.mp->m_sb.sb_inopblock;
250 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
251 args.mp->m_sb.sb_blocksize;
252 nbufs = (int)args.len / blks_per_cluster;
253 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
256 * Figure out what version number to use in the inodes we create.
257 * If the superblock version has caught up to the one that supports
258 * the new inode format, then use the new inode version. Otherwise
259 * use the old version so that old kernels will continue to be
260 * able to use the file system.
262 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
263 version = XFS_DINODE_VERSION_2;
265 version = XFS_DINODE_VERSION_1;
267 memset(&dic, 0, sizeof(xfs_dinode_core_t));
268 INT_SET(dic.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
269 INT_SET(dic.di_version, ARCH_CONVERT, version);
271 for (j = 0; j < nbufs; j++) {
275 d = XFS_AGB_TO_DADDR(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
276 args.agbno + (j * blks_per_cluster));
277 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
278 args.mp->m_bsize * blks_per_cluster,
281 ASSERT(!XFS_BUF_GETERROR(fbuf));
283 * Loop over the inodes in this buffer.
286 for (i = 0; i < ninodes; i++) {
287 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
288 memcpy(&(free->di_core), &dic, sizeof(xfs_dinode_core_t));
289 INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
290 xfs_ialloc_log_di(tp, fbuf, i,
291 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
293 xfs_trans_inode_alloc_buf(tp, fbuf);
295 INT_MOD(agi->agi_count, ARCH_CONVERT, newlen);
296 INT_MOD(agi->agi_freecount, ARCH_CONVERT, newlen);
297 down_read(&args.mp->m_peraglock);
298 args.mp->m_perag[INT_GET(agi->agi_seqno, ARCH_CONVERT)].pagi_freecount += newlen;
299 up_read(&args.mp->m_peraglock);
300 INT_SET(agi->agi_newino, ARCH_CONVERT, newino);
302 * Insert records describing the new inode chunk into the btree.
304 cur = xfs_btree_init_cursor(args.mp, tp, agbp,
305 INT_GET(agi->agi_seqno, ARCH_CONVERT),
306 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
307 for (thisino = newino;
308 thisino < newino + newlen;
309 thisino += XFS_INODES_PER_CHUNK) {
310 if ((error = xfs_inobt_lookup_eq(cur, thisino,
311 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
312 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
316 if ((error = xfs_inobt_insert(cur, &i))) {
317 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
322 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
324 * Log allocation group header fields
326 xfs_ialloc_log_agi(tp, agbp,
327 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
329 * Modify/log superblock values for inode count and inode free count.
331 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
332 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
337 STATIC __inline xfs_agnumber_t
343 spin_lock(&mp->m_agirotor_lock);
344 agno = mp->m_agirotor;
345 if (++mp->m_agirotor == mp->m_maxagi)
347 spin_unlock(&mp->m_agirotor_lock);
353 * Select an allocation group to look for a free inode in, based on the parent
354 * inode and then mode. Return the allocation group buffer.
356 STATIC xfs_buf_t * /* allocation group buffer */
357 xfs_ialloc_ag_select(
358 xfs_trans_t *tp, /* transaction pointer */
359 xfs_ino_t parent, /* parent directory inode number */
360 mode_t mode, /* bits set to indicate file type */
361 int okalloc) /* ok to allocate more space */
363 xfs_buf_t *agbp; /* allocation group header buffer */
364 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
365 xfs_agnumber_t agno; /* current ag number */
366 int flags; /* alloc buffer locking flags */
367 xfs_extlen_t ineed; /* blocks needed for inode allocation */
368 xfs_extlen_t longest = 0; /* longest extent available */
369 xfs_mount_t *mp; /* mount point structure */
370 int needspace; /* file mode implies space allocated */
371 xfs_perag_t *pag; /* per allocation group data */
372 xfs_agnumber_t pagno; /* parent (starting) ag number */
375 * Files of these types need at least one block if length > 0
376 * (and they won't fit in the inode, but that's hard to figure out).
378 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
380 agcount = mp->m_maxagi;
382 pagno = xfs_ialloc_next_ag(mp);
384 pagno = XFS_INO_TO_AGNO(mp, parent);
385 if (pagno >= agcount)
388 ASSERT(pagno < agcount);
390 * Loop through allocation groups, looking for one with a little
391 * free space in it. Note we don't look for free inodes, exactly.
392 * Instead, we include whether there is a need to allocate inodes
393 * to mean that blocks must be allocated for them,
394 * if none are currently free.
397 flags = XFS_ALLOC_FLAG_TRYLOCK;
398 down_read(&mp->m_peraglock);
400 pag = &mp->m_perag[agno];
401 if (!pag->pagi_init) {
402 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
409 if (!pag->pagi_inodeok) {
410 xfs_ialloc_next_ag(mp);
415 * Is there enough free space for the file plus a block
416 * of inodes (if we need to allocate some)?
418 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
419 if (ineed && !pag->pagf_init) {
421 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
425 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
427 if (!ineed || pag->pagf_init) {
428 if (ineed && !(longest = pag->pagf_longest))
429 longest = pag->pagf_flcount > 0;
431 (pag->pagf_freeblks >= needspace + ineed &&
435 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
439 up_read(&mp->m_peraglock);
445 xfs_trans_brelse(tp, agbp);
448 * No point in iterating over the rest, if we're shutting
451 if (XFS_FORCED_SHUTDOWN(mp)) {
452 up_read(&mp->m_peraglock);
453 return (xfs_buf_t *)0;
460 up_read(&mp->m_peraglock);
461 return (xfs_buf_t *)0;
469 * Visible inode allocation functions.
473 * Allocate an inode on disk.
474 * Mode is used to tell whether the new inode will need space, and whether
477 * The arguments IO_agbp and alloc_done are defined to work within
478 * the constraint of one allocation per transaction.
479 * xfs_dialloc() is designed to be called twice if it has to do an
480 * allocation to make more free inodes. On the first call,
481 * IO_agbp should be set to NULL. If an inode is available,
482 * i.e., xfs_dialloc() did not need to do an allocation, an inode
483 * number is returned. In this case, IO_agbp would be set to the
484 * current ag_buf and alloc_done set to false.
485 * If an allocation needed to be done, xfs_dialloc would return
486 * the current ag_buf in IO_agbp and set alloc_done to true.
487 * The caller should then commit the current transaction, allocate a new
488 * transaction, and call xfs_dialloc() again, passing in the previous
489 * value of IO_agbp. IO_agbp should be held across the transactions.
490 * Since the agbp is locked across the two calls, the second call is
491 * guaranteed to have a free inode available.
493 * Once we successfully pick an inode its number is returned and the
494 * on-disk data structures are updated. The inode itself is not read
495 * in, since doing so would break ordering constraints with xfs_reclaim.
499 xfs_trans_t *tp, /* transaction pointer */
500 xfs_ino_t parent, /* parent inode (directory) */
501 mode_t mode, /* mode bits for new inode */
502 int okalloc, /* ok to allocate more space */
503 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
504 boolean_t *alloc_done, /* true if we needed to replenish
506 xfs_ino_t *inop) /* inode number allocated */
508 xfs_agnumber_t agcount; /* number of allocation groups */
509 xfs_buf_t *agbp; /* allocation group header's buffer */
510 xfs_agnumber_t agno; /* allocation group number */
511 xfs_agi_t *agi; /* allocation group header structure */
512 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
513 int error; /* error return value */
514 int i; /* result code */
515 int ialloced; /* inode allocation status */
516 int noroom = 0; /* no space for inode blk allocation */
517 xfs_ino_t ino; /* fs-relative inode to be returned */
519 int j; /* result code */
520 xfs_mount_t *mp; /* file system mount structure */
521 int offset; /* index of inode in chunk */
522 xfs_agino_t pagino; /* parent's a.g. relative inode # */
523 xfs_agnumber_t pagno; /* parent's allocation group number */
524 xfs_inobt_rec_t rec; /* inode allocation record */
525 xfs_agnumber_t tagno; /* testing allocation group number */
526 xfs_btree_cur_t *tcur; /* temp cursor */
527 xfs_inobt_rec_t trec; /* temp inode allocation record */
530 if (*IO_agbp == NULL) {
532 * We do not have an agbp, so select an initial allocation
533 * group for inode allocation.
535 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
537 * Couldn't find an allocation group satisfying the
544 agi = XFS_BUF_TO_AGI(agbp);
545 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
548 * Continue where we left off before. In this case, we
549 * know that the allocation group has free inodes.
552 agi = XFS_BUF_TO_AGI(agbp);
553 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
554 ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
557 agcount = mp->m_sb.sb_agcount;
558 agno = INT_GET(agi->agi_seqno, ARCH_CONVERT);
560 pagno = XFS_INO_TO_AGNO(mp, parent);
561 pagino = XFS_INO_TO_AGINO(mp, parent);
564 * If we have already hit the ceiling of inode blocks then clear
565 * okalloc so we scan all available agi structures for a free
569 if (mp->m_maxicount &&
570 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
576 * Loop until we find an allocation group that either has free inodes
577 * or in which we can allocate some inodes. Iterate through the
578 * allocation groups upward, wrapping at the end.
580 *alloc_done = B_FALSE;
581 while (!agi->agi_freecount) {
583 * Don't do anything if we're not supposed to allocate
584 * any blocks, just go on to the next ag.
588 * Try to allocate some new inodes in the allocation
591 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
592 xfs_trans_brelse(tp, agbp);
593 if (error == ENOSPC) {
601 * We successfully allocated some inodes, return
602 * the current context to the caller so that it
603 * can commit the current transaction and call
604 * us again where we left off.
606 ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
607 *alloc_done = B_TRUE;
614 * If it failed, give up on this ag.
616 xfs_trans_brelse(tp, agbp);
618 * Go on to the next ag: get its ag header.
621 if (++tagno == agcount)
625 return noroom ? ENOSPC : 0;
627 down_read(&mp->m_peraglock);
628 if (mp->m_perag[tagno].pagi_inodeok == 0) {
629 up_read(&mp->m_peraglock);
632 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
633 up_read(&mp->m_peraglock);
636 agi = XFS_BUF_TO_AGI(agbp);
637 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
640 * Here with an allocation group that has a free inode.
641 * Reset agno since we may have chosen a new ag in the
646 cur = xfs_btree_init_cursor(mp, tp, agbp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
647 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
649 * If pagino is 0 (this is the root inode allocation) use newino.
650 * This must work because we've just allocated some.
653 pagino = INT_GET(agi->agi_newino, ARCH_CONVERT);
655 if (cur->bc_nlevels == 1) {
658 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
660 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
662 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
663 &rec.ir_freecount, &rec.ir_free, &i)))
665 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
666 freecount += rec.ir_freecount;
667 if ((error = xfs_inobt_increment(cur, 0, &i)))
671 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
672 XFS_FORCED_SHUTDOWN(mp));
676 * If in the same a.g. as the parent, try to get near the parent.
679 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
682 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
683 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
685 rec.ir_freecount > 0) {
687 * Found a free inode in the same chunk
692 * In the same a.g. as parent, but parent's chunk is full.
695 int doneleft; /* done, to the left */
696 int doneright; /* done, to the right */
703 * Duplicate the cursor, search left & right
706 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
709 * Search left with tcur, back up 1 record.
711 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
715 if ((error = xfs_inobt_get_rec(tcur,
720 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
723 * Search right with cur, go forward 1 record.
725 if ((error = xfs_inobt_increment(cur, 0, &i)))
729 if ((error = xfs_inobt_get_rec(cur,
734 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
737 * Loop until we find the closest inode chunk
740 while (!doneleft || !doneright) {
741 int useleft; /* using left inode
745 * Figure out which block is closer,
748 if (!doneleft && !doneright)
752 XFS_INODES_PER_CHUNK - 1) <
753 rec.ir_startino - pagino;
757 * If checking the left, does it have
760 if (useleft && trec.ir_freecount) {
762 * Yes, set it up as the chunk to use.
765 xfs_btree_del_cursor(cur,
771 * If checking the right, does it have
774 if (!useleft && rec.ir_freecount) {
776 * Yes, it's already set up.
778 xfs_btree_del_cursor(tcur,
783 * If used the left, get another one
787 if ((error = xfs_inobt_decrement(tcur, 0,
792 if ((error = xfs_inobt_get_rec(
798 XFS_WANT_CORRUPTED_GOTO(i == 1,
803 * If used the right, get another one
807 if ((error = xfs_inobt_increment(cur, 0,
812 if ((error = xfs_inobt_get_rec(
818 XFS_WANT_CORRUPTED_GOTO(i == 1,
823 ASSERT(!doneleft || !doneright);
827 * In a different a.g. from the parent.
828 * See if the most recently allocated block has any free.
830 else if (INT_GET(agi->agi_newino, ARCH_CONVERT) != NULLAGINO) {
831 if ((error = xfs_inobt_lookup_eq(cur,
832 INT_GET(agi->agi_newino, ARCH_CONVERT), 0, 0, &i)))
835 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
836 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
838 rec.ir_freecount > 0) {
840 * The last chunk allocated in the group still has
845 * None left in the last group, search the whole a.g.
850 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
854 if ((error = xfs_inobt_get_rec(cur,
856 &rec.ir_freecount, &rec.ir_free,
859 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
860 if (rec.ir_freecount > 0)
862 if ((error = xfs_inobt_increment(cur, 0, &i)))
864 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
868 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
870 ASSERT(offset < XFS_INODES_PER_CHUNK);
871 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
872 XFS_INODES_PER_CHUNK) == 0);
873 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
874 XFS_INOBT_CLR_FREE(&rec, offset);
876 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
879 INT_MOD(agi->agi_freecount, ARCH_CONVERT, -1);
880 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
881 down_read(&mp->m_peraglock);
882 mp->m_perag[tagno].pagi_freecount--;
883 up_read(&mp->m_peraglock);
885 if (cur->bc_nlevels == 1) {
888 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
891 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
892 &rec.ir_freecount, &rec.ir_free, &i)))
894 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
895 freecount += rec.ir_freecount;
896 if ((error = xfs_inobt_increment(cur, 0, &i)))
899 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
900 XFS_FORCED_SHUTDOWN(mp));
903 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
904 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
908 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
910 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
915 * Free disk inode. Carefully avoids touching the incore inode, all
916 * manipulations incore are the caller's responsibility.
917 * The on-disk inode is not changed by this operation, only the
918 * btree (free inode mask) is changed.
922 xfs_trans_t *tp, /* transaction pointer */
923 xfs_ino_t inode, /* inode to be freed */
924 xfs_bmap_free_t *flist, /* extents to free */
925 int *delete, /* set if inode cluster was deleted */
926 xfs_ino_t *first_ino) /* first inode in deleted cluster */
929 xfs_agblock_t agbno; /* block number containing inode */
930 xfs_buf_t *agbp; /* buffer containing allocation group header */
931 xfs_agino_t agino; /* inode number relative to allocation group */
932 xfs_agnumber_t agno; /* allocation group number */
933 xfs_agi_t *agi; /* allocation group header */
934 xfs_btree_cur_t *cur; /* inode btree cursor */
935 int error; /* error return value */
936 int i; /* result code */
937 int ilen; /* inodes in an inode cluster */
938 xfs_mount_t *mp; /* mount structure for filesystem */
939 int off; /* offset of inode in inode chunk */
940 xfs_inobt_rec_t rec; /* btree record */
945 * Break up inode number into its components.
947 agno = XFS_INO_TO_AGNO(mp, inode);
948 if (agno >= mp->m_sb.sb_agcount) {
950 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
951 agno, mp->m_sb.sb_agcount, mp->m_fsname);
953 return XFS_ERROR(EINVAL);
955 agino = XFS_INO_TO_AGINO(mp, inode);
956 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
958 "xfs_difree: inode != XFS_AGINO_TO_INO() "
959 "(%llu != %llu) on %s. Returning EINVAL.",
960 (unsigned long long)inode,
961 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
964 return XFS_ERROR(EINVAL);
966 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
967 if (agbno >= mp->m_sb.sb_agblocks) {
969 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
970 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
972 return XFS_ERROR(EINVAL);
975 * Get the allocation group header.
977 down_read(&mp->m_peraglock);
978 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
979 up_read(&mp->m_peraglock);
982 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
983 error, mp->m_fsname);
986 agi = XFS_BUF_TO_AGI(agbp);
987 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
988 ASSERT(agbno < INT_GET(agi->agi_length, ARCH_CONVERT));
990 * Initialize the cursor.
992 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
993 (xfs_inode_t *)0, 0);
995 if (cur->bc_nlevels == 1) {
998 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1001 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1002 &rec.ir_freecount, &rec.ir_free, &i)))
1005 freecount += rec.ir_freecount;
1006 if ((error = xfs_inobt_increment(cur, 0, &i)))
1010 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1011 XFS_FORCED_SHUTDOWN(mp));
1015 * Look for the entry describing this inode.
1017 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1019 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1020 error, mp->m_fsname);
1023 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1024 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1025 &rec.ir_free, &i))) {
1027 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1028 error, mp->m_fsname);
1031 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1033 * Get the offset in the inode chunk.
1035 off = agino - rec.ir_startino;
1036 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1037 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1039 * Mark the inode free & increment the count.
1041 XFS_INOBT_SET_FREE(&rec, off);
1045 * When an inode cluster is free, it becomes elgible for removal
1047 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1048 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1051 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1054 * Remove the inode cluster from the AGI B+Tree, adjust the
1055 * AGI and Superblock inode counts, and mark the disk space
1056 * to be freed when the transaction is committed.
1058 ilen = XFS_IALLOC_INODES(mp);
1059 INT_MOD(agi->agi_count, ARCH_CONVERT, -ilen);
1060 INT_MOD(agi->agi_freecount, ARCH_CONVERT, -(ilen - 1));
1061 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1062 down_read(&mp->m_peraglock);
1063 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1064 up_read(&mp->m_peraglock);
1065 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1066 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1068 if ((error = xfs_inobt_delete(cur, &i))) {
1069 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1070 error, mp->m_fsname);
1074 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1075 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1076 XFS_IALLOC_BLOCKS(mp), flist, mp);
1080 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1082 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1083 error, mp->m_fsname);
1087 * Change the inode free counts and log the ag/sb changes.
1089 INT_MOD(agi->agi_freecount, ARCH_CONVERT, 1);
1090 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1091 down_read(&mp->m_peraglock);
1092 mp->m_perag[agno].pagi_freecount++;
1093 up_read(&mp->m_peraglock);
1094 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1098 if (cur->bc_nlevels == 1) {
1101 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1104 if ((error = xfs_inobt_get_rec(cur,
1110 freecount += rec.ir_freecount;
1111 if ((error = xfs_inobt_increment(cur, 0, &i)))
1115 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1116 XFS_FORCED_SHUTDOWN(mp));
1119 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1123 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1128 * Return the location of the inode in bno/off, for mapping it into a buffer.
1133 xfs_mount_t *mp, /* file system mount structure */
1134 xfs_trans_t *tp, /* transaction pointer */
1135 xfs_ino_t ino, /* inode to locate */
1136 xfs_fsblock_t *bno, /* output: block containing inode */
1137 int *len, /* output: num blocks in inode cluster */
1138 int *off, /* output: index in block of inode */
1139 uint flags) /* flags concerning inode lookup */
1141 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1142 xfs_buf_t *agbp; /* agi buffer */
1143 xfs_agino_t agino; /* inode number within alloc group */
1144 xfs_agnumber_t agno; /* allocation group number */
1145 int blks_per_cluster; /* num blocks per inode cluster */
1146 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1147 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1148 __int32_t chunk_cnt; /* count of free inodes in chunk */
1149 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1150 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1151 xfs_btree_cur_t *cur; /* inode btree cursor */
1152 int error; /* error code */
1153 int i; /* temp state */
1154 int offset; /* index of inode in its buffer */
1155 int offset_agbno; /* blks from chunk start to inode */
1157 ASSERT(ino != NULLFSINO);
1159 * Split up the inode number into its parts.
1161 agno = XFS_INO_TO_AGNO(mp, ino);
1162 agino = XFS_INO_TO_AGINO(mp, ino);
1163 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1164 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1165 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1167 if (agno >= mp->m_sb.sb_agcount) {
1168 xfs_fs_cmn_err(CE_ALERT, mp,
1169 "xfs_dilocate: agno (%d) >= "
1170 "mp->m_sb.sb_agcount (%d)",
1171 agno, mp->m_sb.sb_agcount);
1173 if (agbno >= mp->m_sb.sb_agblocks) {
1174 xfs_fs_cmn_err(CE_ALERT, mp,
1175 "xfs_dilocate: agbno (0x%llx) >= "
1176 "mp->m_sb.sb_agblocks (0x%lx)",
1177 (unsigned long long) agbno,
1178 (unsigned long) mp->m_sb.sb_agblocks);
1180 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1181 xfs_fs_cmn_err(CE_ALERT, mp,
1182 "xfs_dilocate: ino (0x%llx) != "
1183 "XFS_AGINO_TO_INO(mp, agno, agino) "
1185 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1188 return XFS_ERROR(EINVAL);
1190 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1191 !(flags & XFS_IMAP_LOOKUP)) {
1192 offset = XFS_INO_TO_OFFSET(mp, ino);
1193 ASSERT(offset < mp->m_sb.sb_inopblock);
1194 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1199 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1200 if (*bno != NULLFSBLOCK) {
1201 offset = XFS_INO_TO_OFFSET(mp, ino);
1202 ASSERT(offset < mp->m_sb.sb_inopblock);
1203 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1204 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1206 *len = blks_per_cluster;
1209 if (mp->m_inoalign_mask) {
1210 offset_agbno = agbno & mp->m_inoalign_mask;
1211 chunk_agbno = agbno - offset_agbno;
1213 down_read(&mp->m_peraglock);
1214 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1215 up_read(&mp->m_peraglock);
1218 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1219 "xfs_ialloc_read_agi() returned "
1220 "error %d, agno %d",
1225 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1226 (xfs_inode_t *)0, 0);
1227 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1229 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1230 "xfs_inobt_lookup_le() failed");
1234 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1235 &chunk_free, &i))) {
1237 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1238 "xfs_inobt_get_rec() failed");
1244 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1245 "xfs_inobt_get_rec() failed");
1247 error = XFS_ERROR(EINVAL);
1249 xfs_trans_brelse(tp, agbp);
1250 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1253 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1254 offset_agbno = agbno - chunk_agbno;
1256 ASSERT(agbno >= chunk_agbno);
1257 cluster_agbno = chunk_agbno +
1258 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1259 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1260 XFS_INO_TO_OFFSET(mp, ino);
1261 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1263 *len = blks_per_cluster;
1266 xfs_trans_brelse(tp, agbp);
1267 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1272 * Compute and fill in value of m_in_maxlevels.
1275 xfs_ialloc_compute_maxlevels(
1276 xfs_mount_t *mp) /* file system mount structure */
1284 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1285 XFS_INODES_PER_CHUNK_LOG;
1286 minleafrecs = mp->m_alloc_mnr[0];
1287 minnoderecs = mp->m_alloc_mnr[1];
1288 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1289 for (level = 1; maxblocks > 1; level++)
1290 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1291 mp->m_in_maxlevels = level;
1295 * Log specified fields for the ag hdr (inode section)
1299 xfs_trans_t *tp, /* transaction pointer */
1300 xfs_buf_t *bp, /* allocation group header buffer */
1301 int fields) /* bitmask of fields to log */
1303 int first; /* first byte number */
1304 int last; /* last byte number */
1305 static const short offsets[] = { /* field starting offsets */
1306 /* keep in sync with bit definitions */
1307 offsetof(xfs_agi_t, agi_magicnum),
1308 offsetof(xfs_agi_t, agi_versionnum),
1309 offsetof(xfs_agi_t, agi_seqno),
1310 offsetof(xfs_agi_t, agi_length),
1311 offsetof(xfs_agi_t, agi_count),
1312 offsetof(xfs_agi_t, agi_root),
1313 offsetof(xfs_agi_t, agi_level),
1314 offsetof(xfs_agi_t, agi_freecount),
1315 offsetof(xfs_agi_t, agi_newino),
1316 offsetof(xfs_agi_t, agi_dirino),
1317 offsetof(xfs_agi_t, agi_unlinked),
1321 xfs_agi_t *agi; /* allocation group header */
1323 agi = XFS_BUF_TO_AGI(bp);
1324 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
1327 * Compute byte offsets for the first and last fields.
1329 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1331 * Log the allocation group inode header buffer.
1333 xfs_trans_log_buf(tp, bp, first, last);
1337 * Read in the allocation group header (inode allocation section)
1340 xfs_ialloc_read_agi(
1341 xfs_mount_t *mp, /* file system mount structure */
1342 xfs_trans_t *tp, /* transaction pointer */
1343 xfs_agnumber_t agno, /* allocation group number */
1344 xfs_buf_t **bpp) /* allocation group hdr buf */
1346 xfs_agi_t *agi; /* allocation group header */
1347 int agi_ok; /* agi is consistent */
1348 xfs_buf_t *bp; /* allocation group hdr buf */
1349 xfs_perag_t *pag; /* per allocation group data */
1352 ASSERT(agno != NULLAGNUMBER);
1353 error = xfs_trans_read_buf(
1354 mp, tp, mp->m_ddev_targp,
1355 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1356 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1359 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1362 * Validate the magic number of the agi block.
1364 agi = XFS_BUF_TO_AGI(bp);
1366 INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC &&
1367 XFS_AGI_GOOD_VERSION(
1368 INT_GET(agi->agi_versionnum, ARCH_CONVERT));
1369 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1370 XFS_RANDOM_IALLOC_READ_AGI))) {
1371 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1373 xfs_trans_brelse(tp, bp);
1374 return XFS_ERROR(EFSCORRUPTED);
1376 pag = &mp->m_perag[agno];
1377 if (!pag->pagi_init) {
1378 pag->pagi_freecount = INT_GET(agi->agi_freecount, ARCH_CONVERT);
1382 * It's possible for these to be out of sync if
1383 * we are in the middle of a forced shutdown.
1385 ASSERT(pag->pagi_freecount ==
1386 INT_GET(agi->agi_freecount, ARCH_CONVERT)
1387 || XFS_FORCED_SHUTDOWN(mp));
1394 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1395 ASSERT(agi->agi_unlinked[i]);
1399 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);