Linux v2.6.13
[linux-2.6/next.git] / fs / xfs / xfs_ialloc.c
blobce5fee9eaec5ee2529ffa95936ba8b7caa736242
1 /*
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:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
37 #include "xfs_inum.h"
38 #include "xfs_log.h"
39 #include "xfs_trans.h"
40 #include "xfs_sb.h"
41 #include "xfs_ag.h"
42 #include "xfs_dir.h"
43 #include "xfs_dir2.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"
57 #include "xfs_bit.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_bmap.h"
63 * Log specified fields for the inode given by bp and off.
65 STATIC void
66 xfs_ialloc_log_di(
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),
105 sizeof(xfs_dinode_t)
109 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
110 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
111 mp = tp->t_mountp;
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;
120 first += ioffset;
121 last += ioffset;
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 */
134 xfs_ialloc_ag_alloc(
135 xfs_trans_t *tp, /* transaction pointer */
136 xfs_buf_t *agbp, /* alloc group buffer */
137 int *alloc)
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 */
144 int error;
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 */
156 /* boundary */
157 xfs_dinode_core_t dic; /* a dinode_core to copy to new */
158 /* inodes */
160 args.tp = tp;
161 args.mp = tp->t_mountp;
164 * Locking will ensure that we don't have two callers in here
165 * at one time.
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
175 * boundary.
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.
180 isaligned = 0;
181 if (args.mp->m_sinoalign) {
182 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
183 args.alignment = args.mp->m_dalign;
184 isaligned = 1;
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;
189 else
190 args.alignment = 1;
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),
199 args.agbno);
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;
206 args.prod = 1;
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)))
212 return error;
215 * If stripe alignment is turned on, then try again with cluster
216 * alignment.
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;
227 else
228 args.alignment = 1;
229 if ((error = xfs_alloc_vextent(&args)))
230 return error;
233 if (args.fsbno == NULLFSBLOCK) {
234 *alloc = 0;
235 return 0;
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;
251 } else {
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;
266 else
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++) {
275 * Get the block.
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,
281 XFS_BUF_LOCK);
282 ASSERT(fbuf);
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);
315 return error;
317 ASSERT(i == 0);
318 if ((error = xfs_inobt_insert(cur, &i))) {
319 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
320 return error;
322 ASSERT(i == 1);
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);
335 *alloc = 1;
336 return 0;
339 STATIC __inline xfs_agnumber_t
340 xfs_ialloc_next_ag(
341 xfs_mount_t *mp)
343 xfs_agnumber_t agno;
345 spin_lock(&mp->m_agirotor_lock);
346 agno = mp->m_agirotor;
347 if (++mp->m_agirotor == mp->m_maxagi)
348 mp->m_agirotor = 0;
349 spin_unlock(&mp->m_agirotor_lock);
351 return agno;
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);
381 mp = tp->t_mountp;
382 agcount = mp->m_maxagi;
383 if (S_ISDIR(mode))
384 pagno = xfs_ialloc_next_ag(mp);
385 else {
386 pagno = XFS_INO_TO_AGNO(mp, parent);
387 if (pagno >= agcount)
388 pagno = 0;
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.
398 agno = pagno;
399 flags = XFS_ALLOC_FLAG_TRYLOCK;
400 down_read(&mp->m_peraglock);
401 for (;;) {
402 pag = &mp->m_perag[agno];
403 if (!pag->pagi_init) {
404 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
405 agbp = NULL;
406 goto nextag;
408 } else
409 agbp = NULL;
411 if (!pag->pagi_inodeok) {
412 xfs_ialloc_next_ag(mp);
413 goto unlock_nextag;
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) {
422 if (agbp == NULL &&
423 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
424 agbp = NULL;
425 goto nextag;
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;
432 if (!ineed ||
433 (pag->pagf_freeblks >= needspace + ineed &&
434 longest >= ineed &&
435 okalloc)) {
436 if (agbp == NULL &&
437 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
438 agbp = NULL;
439 goto nextag;
441 up_read(&mp->m_peraglock);
442 return agbp;
445 unlock_nextag:
446 if (agbp)
447 xfs_trans_brelse(tp, agbp);
448 nextag:
450 * No point in iterating over the rest, if we're shutting
451 * down.
453 if (XFS_FORCED_SHUTDOWN(mp)) {
454 up_read(&mp->m_peraglock);
455 return (xfs_buf_t *)0;
457 agno++;
458 if (agno >= agcount)
459 agno = 0;
460 if (agno == pagno) {
461 if (flags == 0) {
462 up_read(&mp->m_peraglock);
463 return (xfs_buf_t *)0;
465 flags = 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
477 * it is a directory.
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.
500 xfs_dialloc(
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
507 inode freelist */
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 */
520 /* REFERENCED */
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
540 * criteria, give up.
542 if (!agbp) {
543 *inop = NULLFSINO;
544 return 0;
546 agi = XFS_BUF_TO_AGI(agbp);
547 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
548 } else {
550 * Continue where we left off before. In this case, we
551 * know that the allocation group has free inodes.
553 agbp = *IO_agbp;
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);
558 mp = tp->t_mountp;
559 agcount = mp->m_sb.sb_agcount;
560 agno = INT_GET(agi->agi_seqno, ARCH_CONVERT);
561 tagno = agno;
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
568 * inode.
571 if (mp->m_maxicount &&
572 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
573 noroom = 1;
574 okalloc = 0;
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.
588 if (okalloc) {
590 * Try to allocate some new inodes in the allocation
591 * group.
593 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
594 xfs_trans_brelse(tp, agbp);
595 if (error == ENOSPC) {
596 *inop = NULLFSINO;
597 return 0;
598 } else
599 return error;
601 if (ialloced) {
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;
610 *IO_agbp = agbp;
611 *inop = NULLFSINO;
612 return 0;
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.
622 nextag:
623 if (++tagno == agcount)
624 tagno = 0;
625 if (tagno == agno) {
626 *inop = NULLFSINO;
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);
632 goto nextag;
634 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
635 up_read(&mp->m_peraglock);
636 if (error)
637 goto nextag;
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
644 * loop above.
646 agno = tagno;
647 *IO_agbp = NULL;
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.
654 if (!pagino)
655 pagino = INT_GET(agi->agi_newino, ARCH_CONVERT);
656 #ifdef DEBUG
657 if (cur->bc_nlevels == 1) {
658 int freecount = 0;
660 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
661 goto error0;
662 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
663 do {
664 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
665 &rec.ir_freecount, &rec.ir_free, &i)))
666 goto error0;
667 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
668 freecount += rec.ir_freecount;
669 if ((error = xfs_inobt_increment(cur, 0, &i)))
670 goto error0;
671 } while (i == 1);
673 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
674 XFS_FORCED_SHUTDOWN(mp));
676 #endif
678 * If in the same a.g. as the parent, try to get near the parent.
680 if (pagno == agno) {
681 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
682 goto error0;
683 if (i != 0 &&
684 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
685 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
686 j == 1 &&
687 rec.ir_freecount > 0) {
689 * Found a free inode in the same chunk
690 * as parent, done.
694 * In the same a.g. as parent, but parent's chunk is full.
696 else {
697 int doneleft; /* done, to the left */
698 int doneright; /* done, to the right */
700 if (error)
701 goto error0;
702 ASSERT(i == 1);
703 ASSERT(j == 1);
705 * Duplicate the cursor, search left & right
706 * simultaneously.
708 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
709 goto error0;
711 * Search left with tcur, back up 1 record.
713 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
714 goto error1;
715 doneleft = !i;
716 if (!doneleft) {
717 if ((error = xfs_inobt_get_rec(tcur,
718 &trec.ir_startino,
719 &trec.ir_freecount,
720 &trec.ir_free, &i)))
721 goto error1;
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)))
728 goto error1;
729 doneright = !i;
730 if (!doneright) {
731 if ((error = xfs_inobt_get_rec(cur,
732 &rec.ir_startino,
733 &rec.ir_freecount,
734 &rec.ir_free, &i)))
735 goto error1;
736 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
739 * Loop until we find the closest inode chunk
740 * with a free one.
742 while (!doneleft || !doneright) {
743 int useleft; /* using left inode
744 chunk this time */
747 * Figure out which block is closer,
748 * if both are valid.
750 if (!doneleft && !doneright)
751 useleft =
752 pagino -
753 (trec.ir_startino +
754 XFS_INODES_PER_CHUNK - 1) <
755 rec.ir_startino - pagino;
756 else
757 useleft = !doneleft;
759 * If checking the left, does it have
760 * free inodes?
762 if (useleft && trec.ir_freecount) {
764 * Yes, set it up as the chunk to use.
766 rec = trec;
767 xfs_btree_del_cursor(cur,
768 XFS_BTREE_NOERROR);
769 cur = tcur;
770 break;
773 * If checking the right, does it have
774 * free inodes?
776 if (!useleft && rec.ir_freecount) {
778 * Yes, it's already set up.
780 xfs_btree_del_cursor(tcur,
781 XFS_BTREE_NOERROR);
782 break;
785 * If used the left, get another one
786 * further left.
788 if (useleft) {
789 if ((error = xfs_inobt_decrement(tcur, 0,
790 &i)))
791 goto error1;
792 doneleft = !i;
793 if (!doneleft) {
794 if ((error = xfs_inobt_get_rec(
795 tcur,
796 &trec.ir_startino,
797 &trec.ir_freecount,
798 &trec.ir_free, &i)))
799 goto error1;
800 XFS_WANT_CORRUPTED_GOTO(i == 1,
801 error1);
805 * If used the right, get another one
806 * further right.
808 else {
809 if ((error = xfs_inobt_increment(cur, 0,
810 &i)))
811 goto error1;
812 doneright = !i;
813 if (!doneright) {
814 if ((error = xfs_inobt_get_rec(
815 cur,
816 &rec.ir_startino,
817 &rec.ir_freecount,
818 &rec.ir_free, &i)))
819 goto error1;
820 XFS_WANT_CORRUPTED_GOTO(i == 1,
821 error1);
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)))
835 goto error0;
836 if (i == 1 &&
837 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
838 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
839 j == 1 &&
840 rec.ir_freecount > 0) {
842 * The last chunk allocated in the group still has
843 * a free inode.
847 * None left in the last group, search the whole a.g.
849 else {
850 if (error)
851 goto error0;
852 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
853 goto error0;
854 ASSERT(i == 1);
855 for (;;) {
856 if ((error = xfs_inobt_get_rec(cur,
857 &rec.ir_startino,
858 &rec.ir_freecount, &rec.ir_free,
859 &i)))
860 goto error0;
861 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
862 if (rec.ir_freecount > 0)
863 break;
864 if ((error = xfs_inobt_increment(cur, 0, &i)))
865 goto error0;
866 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
870 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
871 ASSERT(offset >= 0);
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);
877 rec.ir_freecount--;
878 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
879 rec.ir_free)))
880 goto error0;
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);
886 #ifdef DEBUG
887 if (cur->bc_nlevels == 1) {
888 int freecount = 0;
890 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
891 goto error0;
892 do {
893 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
894 &rec.ir_freecount, &rec.ir_free, &i)))
895 goto error0;
896 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
897 freecount += rec.ir_freecount;
898 if ((error = xfs_inobt_increment(cur, 0, &i)))
899 goto error0;
900 } while (i == 1);
901 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
902 XFS_FORCED_SHUTDOWN(mp));
904 #endif
905 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
906 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
907 *inop = ino;
908 return 0;
909 error1:
910 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
911 error0:
912 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
913 return 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.
923 xfs_difree(
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 */
930 /* REFERENCED */
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 */
944 mp = tp->t_mountp;
947 * Break up inode number into its components.
949 agno = XFS_INO_TO_AGNO(mp, inode);
950 if (agno >= mp->m_sb.sb_agcount) {
951 cmn_err(CE_WARN,
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);
954 ASSERT(0);
955 return XFS_ERROR(EINVAL);
957 agino = XFS_INO_TO_AGINO(mp, inode);
958 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
959 cmn_err(CE_WARN,
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);
962 ASSERT(0);
963 return XFS_ERROR(EINVAL);
965 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
966 if (agbno >= mp->m_sb.sb_agblocks) {
967 cmn_err(CE_WARN,
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);
970 ASSERT(0);
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);
979 if (error) {
980 cmn_err(CE_WARN,
981 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
982 error, mp->m_fsname);
983 return error;
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);
993 #ifdef DEBUG
994 if (cur->bc_nlevels == 1) {
995 int freecount = 0;
997 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
998 goto error0;
999 do {
1000 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1001 &rec.ir_freecount, &rec.ir_free, &i)))
1002 goto error0;
1003 if (i) {
1004 freecount += rec.ir_freecount;
1005 if ((error = xfs_inobt_increment(cur, 0, &i)))
1006 goto error0;
1008 } while (i == 1);
1009 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1010 XFS_FORCED_SHUTDOWN(mp));
1012 #endif
1014 * Look for the entry describing this inode.
1016 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1017 cmn_err(CE_WARN,
1018 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1019 error, mp->m_fsname);
1020 goto error0;
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))) {
1025 cmn_err(CE_WARN,
1026 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1027 error, mp->m_fsname);
1028 goto error0;
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);
1041 rec.ir_freecount++;
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))) {
1049 *delete = 1;
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);
1070 goto error0;
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);
1076 } else {
1077 *delete = 0;
1079 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1080 cmn_err(CE_WARN,
1081 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1082 error, mp->m_fsname);
1083 goto error0;
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);
1096 #ifdef DEBUG
1097 if (cur->bc_nlevels == 1) {
1098 int freecount = 0;
1100 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1101 goto error0;
1102 do {
1103 if ((error = xfs_inobt_get_rec(cur,
1104 &rec.ir_startino,
1105 &rec.ir_freecount,
1106 &rec.ir_free, &i)))
1107 goto error0;
1108 if (i) {
1109 freecount += rec.ir_freecount;
1110 if ((error = xfs_inobt_increment(cur, 0, &i)))
1111 goto error0;
1113 } while (i == 1);
1114 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1115 XFS_FORCED_SHUTDOWN(mp));
1117 #endif
1118 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1119 return 0;
1121 error0:
1122 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1123 return error;
1127 * Return the location of the inode in bno/off, for mapping it into a buffer.
1129 /*ARGSUSED*/
1131 xfs_dilocate(
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)) {
1165 #ifdef DEBUG
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) "
1183 "(0x%llx)",
1184 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1186 #endif /* DEBUG */
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);
1194 *off = offset;
1195 *len = 1;
1196 return 0;
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) +
1204 offset;
1205 *len = blks_per_cluster;
1206 return 0;
1208 if (mp->m_inoalign_mask) {
1209 offset_agbno = agbno & mp->m_inoalign_mask;
1210 chunk_agbno = agbno - offset_agbno;
1211 } else {
1212 down_read(&mp->m_peraglock);
1213 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1214 up_read(&mp->m_peraglock);
1215 if (error) {
1216 #ifdef DEBUG
1217 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1218 "xfs_ialloc_read_agi() returned "
1219 "error %d, agno %d",
1220 error, agno);
1221 #endif /* DEBUG */
1222 return error;
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))) {
1227 #ifdef DEBUG
1228 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1229 "xfs_inobt_lookup_le() failed");
1230 #endif /* DEBUG */
1231 goto error0;
1233 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1234 &chunk_free, &i))) {
1235 #ifdef DEBUG
1236 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1237 "xfs_inobt_get_rec() failed");
1238 #endif /* DEBUG */
1239 goto error0;
1241 if (i == 0) {
1242 #ifdef DEBUG
1243 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1244 "xfs_inobt_get_rec() failed");
1245 #endif /* DEBUG */
1246 error = XFS_ERROR(EINVAL);
1248 xfs_trans_brelse(tp, agbp);
1249 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1250 if (error)
1251 return error;
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);
1261 *off = offset;
1262 *len = blks_per_cluster;
1263 return 0;
1264 error0:
1265 xfs_trans_brelse(tp, agbp);
1266 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1267 return error;
1271 * Compute and fill in value of m_in_maxlevels.
1273 void
1274 xfs_ialloc_compute_maxlevels(
1275 xfs_mount_t *mp) /* file system mount structure */
1277 int level;
1278 uint maxblocks;
1279 uint maxleafents;
1280 int minleafrecs;
1281 int minnoderecs;
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)
1296 void
1297 xfs_ialloc_log_agi(
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),
1317 sizeof(xfs_agi_t)
1319 #ifdef DEBUG
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);
1324 #endif
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 */
1349 int error;
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);
1356 if (error)
1357 return error;
1358 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1361 * Validate the magic number of the agi block.
1363 agi = XFS_BUF_TO_AGI(bp);
1364 agi_ok =
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,
1371 mp, agi);
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);
1378 pag->pagi_init = 1;
1379 } else {
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));
1389 #ifdef DEBUG
1391 int i;
1393 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1394 ASSERT(agi->agi_unlinked[i]);
1396 #endif
1398 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1399 *bpp = bp;
1400 return 0;