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/
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
39 #include "xfs_trans.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_alloc.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
63 * Log specified fields for the inode given by bp and off.
67 xfs_trans_t *tp, /* transaction pointer */
68 xfs_buf_t *bp, /* inode buffer */
69 int off, /* index of inode in buffer */
70 int fields) /* bitmask of fields to log */
72 int first; /* first byte number */
73 int ioffset; /* off in bytes */
74 int last; /* last byte number */
75 xfs_mount_t *mp; /* mount point structure */
76 static const short offsets[] = { /* field offsets */
77 /* keep in sync with bits */
78 offsetof(xfs_dinode_core_t, di_magic),
79 offsetof(xfs_dinode_core_t, di_mode),
80 offsetof(xfs_dinode_core_t, di_version),
81 offsetof(xfs_dinode_core_t, di_format),
82 offsetof(xfs_dinode_core_t, di_onlink),
83 offsetof(xfs_dinode_core_t, di_uid),
84 offsetof(xfs_dinode_core_t, di_gid),
85 offsetof(xfs_dinode_core_t, di_nlink),
86 offsetof(xfs_dinode_core_t, di_projid),
87 offsetof(xfs_dinode_core_t, di_pad),
88 offsetof(xfs_dinode_core_t, di_atime),
89 offsetof(xfs_dinode_core_t, di_mtime),
90 offsetof(xfs_dinode_core_t, di_ctime),
91 offsetof(xfs_dinode_core_t, di_size),
92 offsetof(xfs_dinode_core_t, di_nblocks),
93 offsetof(xfs_dinode_core_t, di_extsize),
94 offsetof(xfs_dinode_core_t, di_nextents),
95 offsetof(xfs_dinode_core_t, di_anextents),
96 offsetof(xfs_dinode_core_t, di_forkoff),
97 offsetof(xfs_dinode_core_t, di_aformat),
98 offsetof(xfs_dinode_core_t, di_dmevmask),
99 offsetof(xfs_dinode_core_t, di_dmstate),
100 offsetof(xfs_dinode_core_t, di_flags),
101 offsetof(xfs_dinode_core_t, di_gen),
102 offsetof(xfs_dinode_t, di_next_unlinked),
103 offsetof(xfs_dinode_t, di_u),
104 offsetof(xfs_dinode_t, di_a),
109 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
110 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
113 * Get the inode-relative first and last bytes for these fields
115 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
117 * Convert to buffer offsets and log it.
119 ioffset = off << mp->m_sb.sb_inodelog;
122 xfs_trans_log_buf(tp, bp, first, last);
126 * Allocation group level functions.
130 * Allocate new inodes in the allocation group specified by agbp.
131 * Return 0 for success, else error code.
133 STATIC int /* error code or 0 */
135 xfs_trans_t *tp, /* transaction pointer */
136 xfs_buf_t *agbp, /* alloc group buffer */
139 xfs_agi_t *agi; /* allocation group header */
140 xfs_alloc_arg_t args; /* allocation argument structure */
141 int blks_per_cluster; /* fs blocks per inode cluster */
142 xfs_btree_cur_t *cur; /* inode btree cursor */
143 xfs_daddr_t d; /* disk addr of buffer */
145 xfs_buf_t *fbuf; /* new free inodes' buffer */
146 xfs_dinode_t *free; /* new free inode structure */
147 int i; /* inode counter */
148 int j; /* block counter */
149 int nbufs; /* num bufs of new inodes */
150 xfs_agino_t newino; /* new first inode's number */
151 xfs_agino_t newlen; /* new number of inodes */
152 int ninodes; /* num inodes per buf */
153 xfs_agino_t thisino; /* current inode number, for loop */
154 int version; /* inode version number to use */
155 int isaligned; /* inode allocation at stripe unit */
157 xfs_dinode_core_t dic; /* a dinode_core to copy to new */
161 args.mp = tp->t_mountp;
164 * Locking will ensure that we don't have two callers in here
167 newlen = XFS_IALLOC_INODES(args.mp);
168 if (args.mp->m_maxicount &&
169 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
170 return XFS_ERROR(ENOSPC);
171 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
173 * Set the alignment for the allocation.
174 * If stripe alignment is turned on then align at stripe unit
176 * If the cluster size is smaller than a filesystem block
177 * then we're doing I/O for inodes in filesystem block size pieces,
178 * so don't need alignment anyway.
181 if (args.mp->m_sinoalign) {
182 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
183 args.alignment = args.mp->m_dalign;
185 } else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
186 args.mp->m_sb.sb_inoalignmt >=
187 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
188 args.alignment = args.mp->m_sb.sb_inoalignmt;
191 agi = XFS_BUF_TO_AGI(agbp);
193 * Need to figure out where to allocate the inode blocks.
194 * Ideally they should be spaced out through the a.g.
195 * For now, just allocate blocks up front.
197 args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
198 args.fsbno = XFS_AGB_TO_FSB(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
201 * Allocate a fixed-size extent of inodes.
203 args.type = XFS_ALLOCTYPE_NEAR_BNO;
204 args.mod = args.total = args.wasdel = args.isfl = args.userdata =
205 args.minalignslop = 0;
208 * Allow space for the inode btree to split.
210 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
211 if ((error = xfs_alloc_vextent(&args)))
215 * If stripe alignment is turned on, then try again with cluster
218 if (isaligned && args.fsbno == NULLFSBLOCK) {
219 args.type = XFS_ALLOCTYPE_NEAR_BNO;
220 args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
221 args.fsbno = XFS_AGB_TO_FSB(args.mp,
222 INT_GET(agi->agi_seqno, ARCH_CONVERT), args.agbno);
223 if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
224 args.mp->m_sb.sb_inoalignmt >=
225 XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
226 args.alignment = args.mp->m_sb.sb_inoalignmt;
229 if ((error = xfs_alloc_vextent(&args)))
233 if (args.fsbno == NULLFSBLOCK) {
237 ASSERT(args.len == args.minlen);
239 * Convert the results.
241 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
243 * Loop over the new block(s), filling in the inodes.
244 * For small block sizes, manipulate the inodes in buffers
245 * which are multiples of the blocks size.
247 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
248 blks_per_cluster = 1;
249 nbufs = (int)args.len;
250 ninodes = args.mp->m_sb.sb_inopblock;
252 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
253 args.mp->m_sb.sb_blocksize;
254 nbufs = (int)args.len / blks_per_cluster;
255 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
258 * Figure out what version number to use in the inodes we create.
259 * If the superblock version has caught up to the one that supports
260 * the new inode format, then use the new inode version. Otherwise
261 * use the old version so that old kernels will continue to be
262 * able to use the file system.
264 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
265 version = XFS_DINODE_VERSION_2;
267 version = XFS_DINODE_VERSION_1;
269 memset(&dic, 0, sizeof(xfs_dinode_core_t));
270 INT_SET(dic.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
271 INT_SET(dic.di_version, ARCH_CONVERT, version);
273 for (j = 0; j < nbufs; j++) {
277 d = XFS_AGB_TO_DADDR(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
278 args.agbno + (j * blks_per_cluster));
279 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
280 args.mp->m_bsize * blks_per_cluster,
283 ASSERT(!XFS_BUF_GETERROR(fbuf));
285 * Loop over the inodes in this buffer.
288 for (i = 0; i < ninodes; i++) {
289 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
290 memcpy(&(free->di_core), &dic, sizeof(xfs_dinode_core_t));
291 INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
292 xfs_ialloc_log_di(tp, fbuf, i,
293 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
295 xfs_trans_inode_alloc_buf(tp, fbuf);
297 INT_MOD(agi->agi_count, ARCH_CONVERT, newlen);
298 INT_MOD(agi->agi_freecount, ARCH_CONVERT, newlen);
299 down_read(&args.mp->m_peraglock);
300 args.mp->m_perag[INT_GET(agi->agi_seqno, ARCH_CONVERT)].pagi_freecount += newlen;
301 up_read(&args.mp->m_peraglock);
302 INT_SET(agi->agi_newino, ARCH_CONVERT, newino);
304 * Insert records describing the new inode chunk into the btree.
306 cur = xfs_btree_init_cursor(args.mp, tp, agbp,
307 INT_GET(agi->agi_seqno, ARCH_CONVERT),
308 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
309 for (thisino = newino;
310 thisino < newino + newlen;
311 thisino += XFS_INODES_PER_CHUNK) {
312 if ((error = xfs_inobt_lookup_eq(cur, thisino,
313 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
314 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
318 if ((error = xfs_inobt_insert(cur, &i))) {
319 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
324 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
326 * Log allocation group header fields
328 xfs_ialloc_log_agi(tp, agbp,
329 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
331 * Modify/log superblock values for inode count and inode free count.
333 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
334 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
339 STATIC __inline xfs_agnumber_t
345 spin_lock(&mp->m_agirotor_lock);
346 agno = mp->m_agirotor;
347 if (++mp->m_agirotor == mp->m_maxagi)
349 spin_unlock(&mp->m_agirotor_lock);
355 * Select an allocation group to look for a free inode in, based on the parent
356 * inode and then mode. Return the allocation group buffer.
358 STATIC xfs_buf_t * /* allocation group buffer */
359 xfs_ialloc_ag_select(
360 xfs_trans_t *tp, /* transaction pointer */
361 xfs_ino_t parent, /* parent directory inode number */
362 mode_t mode, /* bits set to indicate file type */
363 int okalloc) /* ok to allocate more space */
365 xfs_buf_t *agbp; /* allocation group header buffer */
366 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
367 xfs_agnumber_t agno; /* current ag number */
368 int flags; /* alloc buffer locking flags */
369 xfs_extlen_t ineed; /* blocks needed for inode allocation */
370 xfs_extlen_t longest = 0; /* longest extent available */
371 xfs_mount_t *mp; /* mount point structure */
372 int needspace; /* file mode implies space allocated */
373 xfs_perag_t *pag; /* per allocation group data */
374 xfs_agnumber_t pagno; /* parent (starting) ag number */
377 * Files of these types need at least one block if length > 0
378 * (and they won't fit in the inode, but that's hard to figure out).
380 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
382 agcount = mp->m_maxagi;
384 pagno = xfs_ialloc_next_ag(mp);
386 pagno = XFS_INO_TO_AGNO(mp, parent);
387 if (pagno >= agcount)
390 ASSERT(pagno < agcount);
392 * Loop through allocation groups, looking for one with a little
393 * free space in it. Note we don't look for free inodes, exactly.
394 * Instead, we include whether there is a need to allocate inodes
395 * to mean that blocks must be allocated for them,
396 * if none are currently free.
399 flags = XFS_ALLOC_FLAG_TRYLOCK;
400 down_read(&mp->m_peraglock);
402 pag = &mp->m_perag[agno];
403 if (!pag->pagi_init) {
404 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
411 if (!pag->pagi_inodeok) {
412 xfs_ialloc_next_ag(mp);
417 * Is there enough free space for the file plus a block
418 * of inodes (if we need to allocate some)?
420 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
421 if (ineed && !pag->pagf_init) {
423 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
427 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
429 if (!ineed || pag->pagf_init) {
430 if (ineed && !(longest = pag->pagf_longest))
431 longest = pag->pagf_flcount > 0;
433 (pag->pagf_freeblks >= needspace + ineed &&
437 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
441 up_read(&mp->m_peraglock);
447 xfs_trans_brelse(tp, agbp);
450 * No point in iterating over the rest, if we're shutting
453 if (XFS_FORCED_SHUTDOWN(mp)) {
454 up_read(&mp->m_peraglock);
455 return (xfs_buf_t *)0;
462 up_read(&mp->m_peraglock);
463 return (xfs_buf_t *)0;
471 * Visible inode allocation functions.
475 * Allocate an inode on disk.
476 * Mode is used to tell whether the new inode will need space, and whether
479 * The arguments IO_agbp and alloc_done are defined to work within
480 * the constraint of one allocation per transaction.
481 * xfs_dialloc() is designed to be called twice if it has to do an
482 * allocation to make more free inodes. On the first call,
483 * IO_agbp should be set to NULL. If an inode is available,
484 * i.e., xfs_dialloc() did not need to do an allocation, an inode
485 * number is returned. In this case, IO_agbp would be set to the
486 * current ag_buf and alloc_done set to false.
487 * If an allocation needed to be done, xfs_dialloc would return
488 * the current ag_buf in IO_agbp and set alloc_done to true.
489 * The caller should then commit the current transaction, allocate a new
490 * transaction, and call xfs_dialloc() again, passing in the previous
491 * value of IO_agbp. IO_agbp should be held across the transactions.
492 * Since the agbp is locked across the two calls, the second call is
493 * guaranteed to have a free inode available.
495 * Once we successfully pick an inode its number is returned and the
496 * on-disk data structures are updated. The inode itself is not read
497 * in, since doing so would break ordering constraints with xfs_reclaim.
501 xfs_trans_t *tp, /* transaction pointer */
502 xfs_ino_t parent, /* parent inode (directory) */
503 mode_t mode, /* mode bits for new inode */
504 int okalloc, /* ok to allocate more space */
505 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
506 boolean_t *alloc_done, /* true if we needed to replenish
508 xfs_ino_t *inop) /* inode number allocated */
510 xfs_agnumber_t agcount; /* number of allocation groups */
511 xfs_buf_t *agbp; /* allocation group header's buffer */
512 xfs_agnumber_t agno; /* allocation group number */
513 xfs_agi_t *agi; /* allocation group header structure */
514 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
515 int error; /* error return value */
516 int i; /* result code */
517 int ialloced; /* inode allocation status */
518 int noroom = 0; /* no space for inode blk allocation */
519 xfs_ino_t ino; /* fs-relative inode to be returned */
521 int j; /* result code */
522 xfs_mount_t *mp; /* file system mount structure */
523 int offset; /* index of inode in chunk */
524 xfs_agino_t pagino; /* parent's a.g. relative inode # */
525 xfs_agnumber_t pagno; /* parent's allocation group number */
526 xfs_inobt_rec_t rec; /* inode allocation record */
527 xfs_agnumber_t tagno; /* testing allocation group number */
528 xfs_btree_cur_t *tcur; /* temp cursor */
529 xfs_inobt_rec_t trec; /* temp inode allocation record */
532 if (*IO_agbp == NULL) {
534 * We do not have an agbp, so select an initial allocation
535 * group for inode allocation.
537 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
539 * Couldn't find an allocation group satisfying the
546 agi = XFS_BUF_TO_AGI(agbp);
547 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
550 * Continue where we left off before. In this case, we
551 * know that the allocation group has free inodes.
554 agi = XFS_BUF_TO_AGI(agbp);
555 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
556 ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
559 agcount = mp->m_sb.sb_agcount;
560 agno = INT_GET(agi->agi_seqno, ARCH_CONVERT);
562 pagno = XFS_INO_TO_AGNO(mp, parent);
563 pagino = XFS_INO_TO_AGINO(mp, parent);
566 * If we have already hit the ceiling of inode blocks then clear
567 * okalloc so we scan all available agi structures for a free
571 if (mp->m_maxicount &&
572 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
578 * Loop until we find an allocation group that either has free inodes
579 * or in which we can allocate some inodes. Iterate through the
580 * allocation groups upward, wrapping at the end.
582 *alloc_done = B_FALSE;
583 while (!agi->agi_freecount) {
585 * Don't do anything if we're not supposed to allocate
586 * any blocks, just go on to the next ag.
590 * Try to allocate some new inodes in the allocation
593 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
594 xfs_trans_brelse(tp, agbp);
595 if (error == ENOSPC) {
603 * We successfully allocated some inodes, return
604 * the current context to the caller so that it
605 * can commit the current transaction and call
606 * us again where we left off.
608 ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
609 *alloc_done = B_TRUE;
616 * If it failed, give up on this ag.
618 xfs_trans_brelse(tp, agbp);
620 * Go on to the next ag: get its ag header.
623 if (++tagno == agcount)
627 return noroom ? ENOSPC : 0;
629 down_read(&mp->m_peraglock);
630 if (mp->m_perag[tagno].pagi_inodeok == 0) {
631 up_read(&mp->m_peraglock);
634 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
635 up_read(&mp->m_peraglock);
638 agi = XFS_BUF_TO_AGI(agbp);
639 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
642 * Here with an allocation group that has a free inode.
643 * Reset agno since we may have chosen a new ag in the
648 cur = xfs_btree_init_cursor(mp, tp, agbp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
649 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
651 * If pagino is 0 (this is the root inode allocation) use newino.
652 * This must work because we've just allocated some.
655 pagino = INT_GET(agi->agi_newino, ARCH_CONVERT);
657 if (cur->bc_nlevels == 1) {
660 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
662 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
664 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
665 &rec.ir_freecount, &rec.ir_free, &i)))
667 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
668 freecount += rec.ir_freecount;
669 if ((error = xfs_inobt_increment(cur, 0, &i)))
673 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
674 XFS_FORCED_SHUTDOWN(mp));
678 * If in the same a.g. as the parent, try to get near the parent.
681 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
684 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
685 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
687 rec.ir_freecount > 0) {
689 * Found a free inode in the same chunk
694 * In the same a.g. as parent, but parent's chunk is full.
697 int doneleft; /* done, to the left */
698 int doneright; /* done, to the right */
705 * Duplicate the cursor, search left & right
708 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
711 * Search left with tcur, back up 1 record.
713 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
717 if ((error = xfs_inobt_get_rec(tcur,
722 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
725 * Search right with cur, go forward 1 record.
727 if ((error = xfs_inobt_increment(cur, 0, &i)))
731 if ((error = xfs_inobt_get_rec(cur,
736 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
739 * Loop until we find the closest inode chunk
742 while (!doneleft || !doneright) {
743 int useleft; /* using left inode
747 * Figure out which block is closer,
750 if (!doneleft && !doneright)
754 XFS_INODES_PER_CHUNK - 1) <
755 rec.ir_startino - pagino;
759 * If checking the left, does it have
762 if (useleft && trec.ir_freecount) {
764 * Yes, set it up as the chunk to use.
767 xfs_btree_del_cursor(cur,
773 * If checking the right, does it have
776 if (!useleft && rec.ir_freecount) {
778 * Yes, it's already set up.
780 xfs_btree_del_cursor(tcur,
785 * If used the left, get another one
789 if ((error = xfs_inobt_decrement(tcur, 0,
794 if ((error = xfs_inobt_get_rec(
800 XFS_WANT_CORRUPTED_GOTO(i == 1,
805 * If used the right, get another one
809 if ((error = xfs_inobt_increment(cur, 0,
814 if ((error = xfs_inobt_get_rec(
820 XFS_WANT_CORRUPTED_GOTO(i == 1,
825 ASSERT(!doneleft || !doneright);
829 * In a different a.g. from the parent.
830 * See if the most recently allocated block has any free.
832 else if (INT_GET(agi->agi_newino, ARCH_CONVERT) != NULLAGINO) {
833 if ((error = xfs_inobt_lookup_eq(cur,
834 INT_GET(agi->agi_newino, ARCH_CONVERT), 0, 0, &i)))
837 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
838 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
840 rec.ir_freecount > 0) {
842 * The last chunk allocated in the group still has
847 * None left in the last group, search the whole a.g.
852 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
856 if ((error = xfs_inobt_get_rec(cur,
858 &rec.ir_freecount, &rec.ir_free,
861 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
862 if (rec.ir_freecount > 0)
864 if ((error = xfs_inobt_increment(cur, 0, &i)))
866 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
870 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
872 ASSERT(offset < XFS_INODES_PER_CHUNK);
873 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
874 XFS_INODES_PER_CHUNK) == 0);
875 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
876 XFS_INOBT_CLR_FREE(&rec, offset);
878 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
881 INT_MOD(agi->agi_freecount, ARCH_CONVERT, -1);
882 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
883 down_read(&mp->m_peraglock);
884 mp->m_perag[tagno].pagi_freecount--;
885 up_read(&mp->m_peraglock);
887 if (cur->bc_nlevels == 1) {
890 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
893 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
894 &rec.ir_freecount, &rec.ir_free, &i)))
896 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
897 freecount += rec.ir_freecount;
898 if ((error = xfs_inobt_increment(cur, 0, &i)))
901 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
902 XFS_FORCED_SHUTDOWN(mp));
905 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
906 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
910 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
912 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
917 * Free disk inode. Carefully avoids touching the incore inode, all
918 * manipulations incore are the caller's responsibility.
919 * The on-disk inode is not changed by this operation, only the
920 * btree (free inode mask) is changed.
924 xfs_trans_t *tp, /* transaction pointer */
925 xfs_ino_t inode, /* inode to be freed */
926 xfs_bmap_free_t *flist, /* extents to free */
927 int *delete, /* set if inode cluster was deleted */
928 xfs_ino_t *first_ino) /* first inode in deleted cluster */
931 xfs_agblock_t agbno; /* block number containing inode */
932 xfs_buf_t *agbp; /* buffer containing allocation group header */
933 xfs_agino_t agino; /* inode number relative to allocation group */
934 xfs_agnumber_t agno; /* allocation group number */
935 xfs_agi_t *agi; /* allocation group header */
936 xfs_btree_cur_t *cur; /* inode btree cursor */
937 int error; /* error return value */
938 int i; /* result code */
939 int ilen; /* inodes in an inode cluster */
940 xfs_mount_t *mp; /* mount structure for filesystem */
941 int off; /* offset of inode in inode chunk */
942 xfs_inobt_rec_t rec; /* btree record */
947 * Break up inode number into its components.
949 agno = XFS_INO_TO_AGNO(mp, inode);
950 if (agno >= mp->m_sb.sb_agcount) {
952 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
953 agno, mp->m_sb.sb_agcount, mp->m_fsname);
955 return XFS_ERROR(EINVAL);
957 agino = XFS_INO_TO_AGINO(mp, inode);
958 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
960 "xfs_difree: inode != XFS_AGINO_TO_INO() (%d != %d) on %s. Returning EINVAL.",
961 inode, XFS_AGINO_TO_INO(mp, agno, agino), mp->m_fsname);
963 return XFS_ERROR(EINVAL);
965 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
966 if (agbno >= mp->m_sb.sb_agblocks) {
968 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
969 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
971 return XFS_ERROR(EINVAL);
974 * Get the allocation group header.
976 down_read(&mp->m_peraglock);
977 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
978 up_read(&mp->m_peraglock);
981 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
982 error, mp->m_fsname);
985 agi = XFS_BUF_TO_AGI(agbp);
986 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
987 ASSERT(agbno < INT_GET(agi->agi_length, ARCH_CONVERT));
989 * Initialize the cursor.
991 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
992 (xfs_inode_t *)0, 0);
994 if (cur->bc_nlevels == 1) {
997 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1000 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1001 &rec.ir_freecount, &rec.ir_free, &i)))
1004 freecount += rec.ir_freecount;
1005 if ((error = xfs_inobt_increment(cur, 0, &i)))
1009 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1010 XFS_FORCED_SHUTDOWN(mp));
1014 * Look for the entry describing this inode.
1016 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1018 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1019 error, mp->m_fsname);
1022 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1023 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1024 &rec.ir_free, &i))) {
1026 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1027 error, mp->m_fsname);
1030 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1032 * Get the offset in the inode chunk.
1034 off = agino - rec.ir_startino;
1035 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1036 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1038 * Mark the inode free & increment the count.
1040 XFS_INOBT_SET_FREE(&rec, off);
1044 * When an inode cluster is free, it becomes elgible for removal
1046 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1047 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1050 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1053 * Remove the inode cluster from the AGI B+Tree, adjust the
1054 * AGI and Superblock inode counts, and mark the disk space
1055 * to be freed when the transaction is committed.
1057 ilen = XFS_IALLOC_INODES(mp);
1058 INT_MOD(agi->agi_count, ARCH_CONVERT, -ilen);
1059 INT_MOD(agi->agi_freecount, ARCH_CONVERT, -(ilen - 1));
1060 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1061 down_read(&mp->m_peraglock);
1062 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1063 up_read(&mp->m_peraglock);
1064 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1065 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1067 if ((error = xfs_inobt_delete(cur, &i))) {
1068 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1069 error, mp->m_fsname);
1073 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1074 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1075 XFS_IALLOC_BLOCKS(mp), flist, mp);
1079 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1081 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1082 error, mp->m_fsname);
1086 * Change the inode free counts and log the ag/sb changes.
1088 INT_MOD(agi->agi_freecount, ARCH_CONVERT, 1);
1089 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1090 down_read(&mp->m_peraglock);
1091 mp->m_perag[agno].pagi_freecount++;
1092 up_read(&mp->m_peraglock);
1093 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1097 if (cur->bc_nlevels == 1) {
1100 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1103 if ((error = xfs_inobt_get_rec(cur,
1109 freecount += rec.ir_freecount;
1110 if ((error = xfs_inobt_increment(cur, 0, &i)))
1114 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1115 XFS_FORCED_SHUTDOWN(mp));
1118 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1122 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1127 * Return the location of the inode in bno/off, for mapping it into a buffer.
1132 xfs_mount_t *mp, /* file system mount structure */
1133 xfs_trans_t *tp, /* transaction pointer */
1134 xfs_ino_t ino, /* inode to locate */
1135 xfs_fsblock_t *bno, /* output: block containing inode */
1136 int *len, /* output: num blocks in inode cluster */
1137 int *off, /* output: index in block of inode */
1138 uint flags) /* flags concerning inode lookup */
1140 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1141 xfs_buf_t *agbp; /* agi buffer */
1142 xfs_agino_t agino; /* inode number within alloc group */
1143 xfs_agnumber_t agno; /* allocation group number */
1144 int blks_per_cluster; /* num blocks per inode cluster */
1145 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1146 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1147 __int32_t chunk_cnt; /* count of free inodes in chunk */
1148 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1149 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1150 xfs_btree_cur_t *cur; /* inode btree cursor */
1151 int error; /* error code */
1152 int i; /* temp state */
1153 int offset; /* index of inode in its buffer */
1154 int offset_agbno; /* blks from chunk start to inode */
1156 ASSERT(ino != NULLFSINO);
1158 * Split up the inode number into its parts.
1160 agno = XFS_INO_TO_AGNO(mp, ino);
1161 agino = XFS_INO_TO_AGINO(mp, ino);
1162 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1163 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1164 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1166 if (agno >= mp->m_sb.sb_agcount) {
1167 xfs_fs_cmn_err(CE_ALERT, mp,
1168 "xfs_dilocate: agno (%d) >= "
1169 "mp->m_sb.sb_agcount (%d)",
1170 agno, mp->m_sb.sb_agcount);
1172 if (agbno >= mp->m_sb.sb_agblocks) {
1173 xfs_fs_cmn_err(CE_ALERT, mp,
1174 "xfs_dilocate: agbno (0x%llx) >= "
1175 "mp->m_sb.sb_agblocks (0x%lx)",
1176 (unsigned long long) agbno,
1177 (unsigned long) mp->m_sb.sb_agblocks);
1179 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1180 xfs_fs_cmn_err(CE_ALERT, mp,
1181 "xfs_dilocate: ino (0x%llx) != "
1182 "XFS_AGINO_TO_INO(mp, agno, agino) "
1184 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1187 return XFS_ERROR(EINVAL);
1189 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1190 !(flags & XFS_IMAP_LOOKUP)) {
1191 offset = XFS_INO_TO_OFFSET(mp, ino);
1192 ASSERT(offset < mp->m_sb.sb_inopblock);
1193 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1198 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1199 if (*bno != NULLFSBLOCK) {
1200 offset = XFS_INO_TO_OFFSET(mp, ino);
1201 ASSERT(offset < mp->m_sb.sb_inopblock);
1202 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1203 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1205 *len = blks_per_cluster;
1208 if (mp->m_inoalign_mask) {
1209 offset_agbno = agbno & mp->m_inoalign_mask;
1210 chunk_agbno = agbno - offset_agbno;
1212 down_read(&mp->m_peraglock);
1213 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1214 up_read(&mp->m_peraglock);
1217 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1218 "xfs_ialloc_read_agi() returned "
1219 "error %d, agno %d",
1224 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1225 (xfs_inode_t *)0, 0);
1226 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1228 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1229 "xfs_inobt_lookup_le() failed");
1233 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1234 &chunk_free, &i))) {
1236 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1237 "xfs_inobt_get_rec() failed");
1243 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1244 "xfs_inobt_get_rec() failed");
1246 error = XFS_ERROR(EINVAL);
1248 xfs_trans_brelse(tp, agbp);
1249 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1252 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1253 offset_agbno = agbno - chunk_agbno;
1255 ASSERT(agbno >= chunk_agbno);
1256 cluster_agbno = chunk_agbno +
1257 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1258 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1259 XFS_INO_TO_OFFSET(mp, ino);
1260 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1262 *len = blks_per_cluster;
1265 xfs_trans_brelse(tp, agbp);
1266 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1271 * Compute and fill in value of m_in_maxlevels.
1274 xfs_ialloc_compute_maxlevels(
1275 xfs_mount_t *mp) /* file system mount structure */
1283 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1284 XFS_INODES_PER_CHUNK_LOG;
1285 minleafrecs = mp->m_alloc_mnr[0];
1286 minnoderecs = mp->m_alloc_mnr[1];
1287 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1288 for (level = 1; maxblocks > 1; level++)
1289 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1290 mp->m_in_maxlevels = level;
1294 * Log specified fields for the ag hdr (inode section)
1298 xfs_trans_t *tp, /* transaction pointer */
1299 xfs_buf_t *bp, /* allocation group header buffer */
1300 int fields) /* bitmask of fields to log */
1302 int first; /* first byte number */
1303 int last; /* last byte number */
1304 static const short offsets[] = { /* field starting offsets */
1305 /* keep in sync with bit definitions */
1306 offsetof(xfs_agi_t, agi_magicnum),
1307 offsetof(xfs_agi_t, agi_versionnum),
1308 offsetof(xfs_agi_t, agi_seqno),
1309 offsetof(xfs_agi_t, agi_length),
1310 offsetof(xfs_agi_t, agi_count),
1311 offsetof(xfs_agi_t, agi_root),
1312 offsetof(xfs_agi_t, agi_level),
1313 offsetof(xfs_agi_t, agi_freecount),
1314 offsetof(xfs_agi_t, agi_newino),
1315 offsetof(xfs_agi_t, agi_dirino),
1316 offsetof(xfs_agi_t, agi_unlinked),
1320 xfs_agi_t *agi; /* allocation group header */
1322 agi = XFS_BUF_TO_AGI(bp);
1323 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
1326 * Compute byte offsets for the first and last fields.
1328 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1330 * Log the allocation group inode header buffer.
1332 xfs_trans_log_buf(tp, bp, first, last);
1336 * Read in the allocation group header (inode allocation section)
1339 xfs_ialloc_read_agi(
1340 xfs_mount_t *mp, /* file system mount structure */
1341 xfs_trans_t *tp, /* transaction pointer */
1342 xfs_agnumber_t agno, /* allocation group number */
1343 xfs_buf_t **bpp) /* allocation group hdr buf */
1345 xfs_agi_t *agi; /* allocation group header */
1346 int agi_ok; /* agi is consistent */
1347 xfs_buf_t *bp; /* allocation group hdr buf */
1348 xfs_perag_t *pag; /* per allocation group data */
1351 ASSERT(agno != NULLAGNUMBER);
1352 error = xfs_trans_read_buf(
1353 mp, tp, mp->m_ddev_targp,
1354 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1355 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1358 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1361 * Validate the magic number of the agi block.
1363 agi = XFS_BUF_TO_AGI(bp);
1365 INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC &&
1366 XFS_AGI_GOOD_VERSION(
1367 INT_GET(agi->agi_versionnum, ARCH_CONVERT));
1368 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1369 XFS_RANDOM_IALLOC_READ_AGI))) {
1370 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1372 xfs_trans_brelse(tp, bp);
1373 return XFS_ERROR(EFSCORRUPTED);
1375 pag = &mp->m_perag[agno];
1376 if (!pag->pagi_init) {
1377 pag->pagi_freecount = INT_GET(agi->agi_freecount, ARCH_CONVERT);
1381 * It's possible for these to be out of sync if
1382 * we are in the middle of a forced shutdown.
1384 ASSERT(pag->pagi_freecount ==
1385 INT_GET(agi->agi_freecount, ARCH_CONVERT)
1386 || XFS_FORCED_SHUTDOWN(mp));
1393 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1394 ASSERT(agi->agi_unlinked[i]);
1398 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);