Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / fs / xfs / xfs_ialloc.c
blobaad8c5da38afadb158518d7ee5dc51a3309726f2
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
42 #include "xfs_bmap.h"
45 * Log specified fields for the inode given by bp and off.
47 STATIC void
48 xfs_ialloc_log_di(
49 xfs_trans_t *tp, /* transaction pointer */
50 xfs_buf_t *bp, /* inode buffer */
51 int off, /* index of inode in buffer */
52 int fields) /* bitmask of fields to log */
54 int first; /* first byte number */
55 int ioffset; /* off in bytes */
56 int last; /* last byte number */
57 xfs_mount_t *mp; /* mount point structure */
58 static const short offsets[] = { /* field offsets */
59 /* keep in sync with bits */
60 offsetof(xfs_dinode_core_t, di_magic),
61 offsetof(xfs_dinode_core_t, di_mode),
62 offsetof(xfs_dinode_core_t, di_version),
63 offsetof(xfs_dinode_core_t, di_format),
64 offsetof(xfs_dinode_core_t, di_onlink),
65 offsetof(xfs_dinode_core_t, di_uid),
66 offsetof(xfs_dinode_core_t, di_gid),
67 offsetof(xfs_dinode_core_t, di_nlink),
68 offsetof(xfs_dinode_core_t, di_projid),
69 offsetof(xfs_dinode_core_t, di_pad),
70 offsetof(xfs_dinode_core_t, di_atime),
71 offsetof(xfs_dinode_core_t, di_mtime),
72 offsetof(xfs_dinode_core_t, di_ctime),
73 offsetof(xfs_dinode_core_t, di_size),
74 offsetof(xfs_dinode_core_t, di_nblocks),
75 offsetof(xfs_dinode_core_t, di_extsize),
76 offsetof(xfs_dinode_core_t, di_nextents),
77 offsetof(xfs_dinode_core_t, di_anextents),
78 offsetof(xfs_dinode_core_t, di_forkoff),
79 offsetof(xfs_dinode_core_t, di_aformat),
80 offsetof(xfs_dinode_core_t, di_dmevmask),
81 offsetof(xfs_dinode_core_t, di_dmstate),
82 offsetof(xfs_dinode_core_t, di_flags),
83 offsetof(xfs_dinode_core_t, di_gen),
84 offsetof(xfs_dinode_t, di_next_unlinked),
85 offsetof(xfs_dinode_t, di_u),
86 offsetof(xfs_dinode_t, di_a),
87 sizeof(xfs_dinode_t)
91 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
92 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
93 mp = tp->t_mountp;
95 * Get the inode-relative first and last bytes for these fields
97 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
99 * Convert to buffer offsets and log it.
101 ioffset = off << mp->m_sb.sb_inodelog;
102 first += ioffset;
103 last += ioffset;
104 xfs_trans_log_buf(tp, bp, first, last);
108 * Allocation group level functions.
110 static inline int
111 xfs_ialloc_cluster_alignment(
112 xfs_alloc_arg_t *args)
114 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
115 args->mp->m_sb.sb_inoalignmt >=
116 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
117 return args->mp->m_sb.sb_inoalignmt;
118 return 1;
122 * Allocate new inodes in the allocation group specified by agbp.
123 * Return 0 for success, else error code.
125 STATIC int /* error code or 0 */
126 xfs_ialloc_ag_alloc(
127 xfs_trans_t *tp, /* transaction pointer */
128 xfs_buf_t *agbp, /* alloc group buffer */
129 int *alloc)
131 xfs_agi_t *agi; /* allocation group header */
132 xfs_alloc_arg_t args; /* allocation argument structure */
133 int blks_per_cluster; /* fs blocks per inode cluster */
134 xfs_btree_cur_t *cur; /* inode btree cursor */
135 xfs_daddr_t d; /* disk addr of buffer */
136 xfs_agnumber_t agno;
137 int error;
138 xfs_buf_t *fbuf; /* new free inodes' buffer */
139 xfs_dinode_t *free; /* new free inode structure */
140 int i; /* inode counter */
141 int j; /* block counter */
142 int nbufs; /* num bufs of new inodes */
143 xfs_agino_t newino; /* new first inode's number */
144 xfs_agino_t newlen; /* new number of inodes */
145 int ninodes; /* num inodes per buf */
146 xfs_agino_t thisino; /* current inode number, for loop */
147 int version; /* inode version number to use */
148 int isaligned = 0; /* inode allocation at stripe unit */
149 /* boundary */
150 unsigned int gen;
152 args.tp = tp;
153 args.mp = tp->t_mountp;
156 * Locking will ensure that we don't have two callers in here
157 * at one time.
159 newlen = XFS_IALLOC_INODES(args.mp);
160 if (args.mp->m_maxicount &&
161 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
162 return XFS_ERROR(ENOSPC);
163 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
165 * First try to allocate inodes contiguous with the last-allocated
166 * chunk of inodes. If the filesystem is striped, this will fill
167 * an entire stripe unit with inodes.
169 agi = XFS_BUF_TO_AGI(agbp);
170 newino = be32_to_cpu(agi->agi_newino);
171 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
172 XFS_IALLOC_BLOCKS(args.mp);
173 if (likely(newino != NULLAGINO &&
174 (args.agbno < be32_to_cpu(agi->agi_length)))) {
175 args.fsbno = XFS_AGB_TO_FSB(args.mp,
176 be32_to_cpu(agi->agi_seqno), args.agbno);
177 args.type = XFS_ALLOCTYPE_THIS_BNO;
178 args.mod = args.total = args.wasdel = args.isfl =
179 args.userdata = args.minalignslop = 0;
180 args.prod = 1;
183 * We need to take into account alignment here to ensure that
184 * we don't modify the free list if we fail to have an exact
185 * block. If we don't have an exact match, and every oher
186 * attempt allocation attempt fails, we'll end up cancelling
187 * a dirty transaction and shutting down.
189 * For an exact allocation, alignment must be 1,
190 * however we need to take cluster alignment into account when
191 * fixing up the freelist. Use the minalignslop field to
192 * indicate that extra blocks might be required for alignment,
193 * but not to use them in the actual exact allocation.
195 args.alignment = 1;
196 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
198 /* Allow space for the inode btree to split. */
199 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
200 if ((error = xfs_alloc_vextent(&args)))
201 return error;
202 } else
203 args.fsbno = NULLFSBLOCK;
205 if (unlikely(args.fsbno == NULLFSBLOCK)) {
207 * Set the alignment for the allocation.
208 * If stripe alignment is turned on then align at stripe unit
209 * boundary.
210 * If the cluster size is smaller than a filesystem block
211 * then we're doing I/O for inodes in filesystem block size
212 * pieces, so don't need alignment anyway.
214 isaligned = 0;
215 if (args.mp->m_sinoalign) {
216 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
217 args.alignment = args.mp->m_dalign;
218 isaligned = 1;
219 } else
220 args.alignment = xfs_ialloc_cluster_alignment(&args);
222 * Need to figure out where to allocate the inode blocks.
223 * Ideally they should be spaced out through the a.g.
224 * For now, just allocate blocks up front.
226 args.agbno = be32_to_cpu(agi->agi_root);
227 args.fsbno = XFS_AGB_TO_FSB(args.mp,
228 be32_to_cpu(agi->agi_seqno), args.agbno);
230 * Allocate a fixed-size extent of inodes.
232 args.type = XFS_ALLOCTYPE_NEAR_BNO;
233 args.mod = args.total = args.wasdel = args.isfl =
234 args.userdata = args.minalignslop = 0;
235 args.prod = 1;
237 * Allow space for the inode btree to split.
239 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
240 if ((error = xfs_alloc_vextent(&args)))
241 return error;
245 * If stripe alignment is turned on, then try again with cluster
246 * alignment.
248 if (isaligned && args.fsbno == NULLFSBLOCK) {
249 args.type = XFS_ALLOCTYPE_NEAR_BNO;
250 args.agbno = be32_to_cpu(agi->agi_root);
251 args.fsbno = XFS_AGB_TO_FSB(args.mp,
252 be32_to_cpu(agi->agi_seqno), args.agbno);
253 args.alignment = xfs_ialloc_cluster_alignment(&args);
254 if ((error = xfs_alloc_vextent(&args)))
255 return error;
258 if (args.fsbno == NULLFSBLOCK) {
259 *alloc = 0;
260 return 0;
262 ASSERT(args.len == args.minlen);
264 * Convert the results.
266 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
268 * Loop over the new block(s), filling in the inodes.
269 * For small block sizes, manipulate the inodes in buffers
270 * which are multiples of the blocks size.
272 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
273 blks_per_cluster = 1;
274 nbufs = (int)args.len;
275 ninodes = args.mp->m_sb.sb_inopblock;
276 } else {
277 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
278 args.mp->m_sb.sb_blocksize;
279 nbufs = (int)args.len / blks_per_cluster;
280 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
283 * Figure out what version number to use in the inodes we create.
284 * If the superblock version has caught up to the one that supports
285 * the new inode format, then use the new inode version. Otherwise
286 * use the old version so that old kernels will continue to be
287 * able to use the file system.
289 if (xfs_sb_version_hasnlink(&args.mp->m_sb))
290 version = XFS_DINODE_VERSION_2;
291 else
292 version = XFS_DINODE_VERSION_1;
295 * Seed the new inode cluster with a random generation number. This
296 * prevents short-term reuse of generation numbers if a chunk is
297 * freed and then immediately reallocated. We use random numbers
298 * rather than a linear progression to prevent the next generation
299 * number from being easily guessable.
301 gen = random32();
302 for (j = 0; j < nbufs; j++) {
304 * Get the block.
306 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
307 args.agbno + (j * blks_per_cluster));
308 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
309 args.mp->m_bsize * blks_per_cluster,
310 XFS_BUF_LOCK);
311 ASSERT(fbuf);
312 ASSERT(!XFS_BUF_GETERROR(fbuf));
314 * Set initial values for the inodes in this buffer.
316 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
317 for (i = 0; i < ninodes; i++) {
318 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
319 free->di_core.di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
320 free->di_core.di_version = version;
321 free->di_core.di_gen = cpu_to_be32(gen);
322 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
323 xfs_ialloc_log_di(tp, fbuf, i,
324 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
326 xfs_trans_inode_alloc_buf(tp, fbuf);
328 be32_add_cpu(&agi->agi_count, newlen);
329 be32_add_cpu(&agi->agi_freecount, newlen);
330 agno = be32_to_cpu(agi->agi_seqno);
331 down_read(&args.mp->m_peraglock);
332 args.mp->m_perag[agno].pagi_freecount += newlen;
333 up_read(&args.mp->m_peraglock);
334 agi->agi_newino = cpu_to_be32(newino);
336 * Insert records describing the new inode chunk into the btree.
338 cur = xfs_btree_init_cursor(args.mp, tp, agbp, agno,
339 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
340 for (thisino = newino;
341 thisino < newino + newlen;
342 thisino += XFS_INODES_PER_CHUNK) {
343 if ((error = xfs_inobt_lookup_eq(cur, thisino,
344 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
345 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
346 return error;
348 ASSERT(i == 0);
349 if ((error = xfs_inobt_insert(cur, &i))) {
350 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
351 return error;
353 ASSERT(i == 1);
355 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
357 * Log allocation group header fields
359 xfs_ialloc_log_agi(tp, agbp,
360 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
362 * Modify/log superblock values for inode count and inode free count.
364 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
365 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
366 *alloc = 1;
367 return 0;
370 STATIC_INLINE xfs_agnumber_t
371 xfs_ialloc_next_ag(
372 xfs_mount_t *mp)
374 xfs_agnumber_t agno;
376 spin_lock(&mp->m_agirotor_lock);
377 agno = mp->m_agirotor;
378 if (++mp->m_agirotor == mp->m_maxagi)
379 mp->m_agirotor = 0;
380 spin_unlock(&mp->m_agirotor_lock);
382 return agno;
386 * Select an allocation group to look for a free inode in, based on the parent
387 * inode and then mode. Return the allocation group buffer.
389 STATIC xfs_buf_t * /* allocation group buffer */
390 xfs_ialloc_ag_select(
391 xfs_trans_t *tp, /* transaction pointer */
392 xfs_ino_t parent, /* parent directory inode number */
393 mode_t mode, /* bits set to indicate file type */
394 int okalloc) /* ok to allocate more space */
396 xfs_buf_t *agbp; /* allocation group header buffer */
397 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
398 xfs_agnumber_t agno; /* current ag number */
399 int flags; /* alloc buffer locking flags */
400 xfs_extlen_t ineed; /* blocks needed for inode allocation */
401 xfs_extlen_t longest = 0; /* longest extent available */
402 xfs_mount_t *mp; /* mount point structure */
403 int needspace; /* file mode implies space allocated */
404 xfs_perag_t *pag; /* per allocation group data */
405 xfs_agnumber_t pagno; /* parent (starting) ag number */
408 * Files of these types need at least one block if length > 0
409 * (and they won't fit in the inode, but that's hard to figure out).
411 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
412 mp = tp->t_mountp;
413 agcount = mp->m_maxagi;
414 if (S_ISDIR(mode))
415 pagno = xfs_ialloc_next_ag(mp);
416 else {
417 pagno = XFS_INO_TO_AGNO(mp, parent);
418 if (pagno >= agcount)
419 pagno = 0;
421 ASSERT(pagno < agcount);
423 * Loop through allocation groups, looking for one with a little
424 * free space in it. Note we don't look for free inodes, exactly.
425 * Instead, we include whether there is a need to allocate inodes
426 * to mean that blocks must be allocated for them,
427 * if none are currently free.
429 agno = pagno;
430 flags = XFS_ALLOC_FLAG_TRYLOCK;
431 down_read(&mp->m_peraglock);
432 for (;;) {
433 pag = &mp->m_perag[agno];
434 if (!pag->pagi_init) {
435 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
436 agbp = NULL;
437 goto nextag;
439 } else
440 agbp = NULL;
442 if (!pag->pagi_inodeok) {
443 xfs_ialloc_next_ag(mp);
444 goto unlock_nextag;
448 * Is there enough free space for the file plus a block
449 * of inodes (if we need to allocate some)?
451 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
452 if (ineed && !pag->pagf_init) {
453 if (agbp == NULL &&
454 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
455 agbp = NULL;
456 goto nextag;
458 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
460 if (!ineed || pag->pagf_init) {
461 if (ineed && !(longest = pag->pagf_longest))
462 longest = pag->pagf_flcount > 0;
463 if (!ineed ||
464 (pag->pagf_freeblks >= needspace + ineed &&
465 longest >= ineed &&
466 okalloc)) {
467 if (agbp == NULL &&
468 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
469 agbp = NULL;
470 goto nextag;
472 up_read(&mp->m_peraglock);
473 return agbp;
476 unlock_nextag:
477 if (agbp)
478 xfs_trans_brelse(tp, agbp);
479 nextag:
481 * No point in iterating over the rest, if we're shutting
482 * down.
484 if (XFS_FORCED_SHUTDOWN(mp)) {
485 up_read(&mp->m_peraglock);
486 return NULL;
488 agno++;
489 if (agno >= agcount)
490 agno = 0;
491 if (agno == pagno) {
492 if (flags == 0) {
493 up_read(&mp->m_peraglock);
494 return NULL;
496 flags = 0;
502 * Visible inode allocation functions.
506 * Allocate an inode on disk.
507 * Mode is used to tell whether the new inode will need space, and whether
508 * it is a directory.
510 * The arguments IO_agbp and alloc_done are defined to work within
511 * the constraint of one allocation per transaction.
512 * xfs_dialloc() is designed to be called twice if it has to do an
513 * allocation to make more free inodes. On the first call,
514 * IO_agbp should be set to NULL. If an inode is available,
515 * i.e., xfs_dialloc() did not need to do an allocation, an inode
516 * number is returned. In this case, IO_agbp would be set to the
517 * current ag_buf and alloc_done set to false.
518 * If an allocation needed to be done, xfs_dialloc would return
519 * the current ag_buf in IO_agbp and set alloc_done to true.
520 * The caller should then commit the current transaction, allocate a new
521 * transaction, and call xfs_dialloc() again, passing in the previous
522 * value of IO_agbp. IO_agbp should be held across the transactions.
523 * Since the agbp is locked across the two calls, the second call is
524 * guaranteed to have a free inode available.
526 * Once we successfully pick an inode its number is returned and the
527 * on-disk data structures are updated. The inode itself is not read
528 * in, since doing so would break ordering constraints with xfs_reclaim.
531 xfs_dialloc(
532 xfs_trans_t *tp, /* transaction pointer */
533 xfs_ino_t parent, /* parent inode (directory) */
534 mode_t mode, /* mode bits for new inode */
535 int okalloc, /* ok to allocate more space */
536 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
537 boolean_t *alloc_done, /* true if we needed to replenish
538 inode freelist */
539 xfs_ino_t *inop) /* inode number allocated */
541 xfs_agnumber_t agcount; /* number of allocation groups */
542 xfs_buf_t *agbp; /* allocation group header's buffer */
543 xfs_agnumber_t agno; /* allocation group number */
544 xfs_agi_t *agi; /* allocation group header structure */
545 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
546 int error; /* error return value */
547 int i; /* result code */
548 int ialloced; /* inode allocation status */
549 int noroom = 0; /* no space for inode blk allocation */
550 xfs_ino_t ino; /* fs-relative inode to be returned */
551 /* REFERENCED */
552 int j; /* result code */
553 xfs_mount_t *mp; /* file system mount structure */
554 int offset; /* index of inode in chunk */
555 xfs_agino_t pagino; /* parent's a.g. relative inode # */
556 xfs_agnumber_t pagno; /* parent's allocation group number */
557 xfs_inobt_rec_incore_t rec; /* inode allocation record */
558 xfs_agnumber_t tagno; /* testing allocation group number */
559 xfs_btree_cur_t *tcur; /* temp cursor */
560 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
563 if (*IO_agbp == NULL) {
565 * We do not have an agbp, so select an initial allocation
566 * group for inode allocation.
568 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
570 * Couldn't find an allocation group satisfying the
571 * criteria, give up.
573 if (!agbp) {
574 *inop = NULLFSINO;
575 return 0;
577 agi = XFS_BUF_TO_AGI(agbp);
578 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
579 } else {
581 * Continue where we left off before. In this case, we
582 * know that the allocation group has free inodes.
584 agbp = *IO_agbp;
585 agi = XFS_BUF_TO_AGI(agbp);
586 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
587 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
589 mp = tp->t_mountp;
590 agcount = mp->m_sb.sb_agcount;
591 agno = be32_to_cpu(agi->agi_seqno);
592 tagno = agno;
593 pagno = XFS_INO_TO_AGNO(mp, parent);
594 pagino = XFS_INO_TO_AGINO(mp, parent);
597 * If we have already hit the ceiling of inode blocks then clear
598 * okalloc so we scan all available agi structures for a free
599 * inode.
602 if (mp->m_maxicount &&
603 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
604 noroom = 1;
605 okalloc = 0;
609 * Loop until we find an allocation group that either has free inodes
610 * or in which we can allocate some inodes. Iterate through the
611 * allocation groups upward, wrapping at the end.
613 *alloc_done = B_FALSE;
614 while (!agi->agi_freecount) {
616 * Don't do anything if we're not supposed to allocate
617 * any blocks, just go on to the next ag.
619 if (okalloc) {
621 * Try to allocate some new inodes in the allocation
622 * group.
624 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
625 xfs_trans_brelse(tp, agbp);
626 if (error == ENOSPC) {
627 *inop = NULLFSINO;
628 return 0;
629 } else
630 return error;
632 if (ialloced) {
634 * We successfully allocated some inodes, return
635 * the current context to the caller so that it
636 * can commit the current transaction and call
637 * us again where we left off.
639 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
640 *alloc_done = B_TRUE;
641 *IO_agbp = agbp;
642 *inop = NULLFSINO;
643 return 0;
647 * If it failed, give up on this ag.
649 xfs_trans_brelse(tp, agbp);
651 * Go on to the next ag: get its ag header.
653 nextag:
654 if (++tagno == agcount)
655 tagno = 0;
656 if (tagno == agno) {
657 *inop = NULLFSINO;
658 return noroom ? ENOSPC : 0;
660 down_read(&mp->m_peraglock);
661 if (mp->m_perag[tagno].pagi_inodeok == 0) {
662 up_read(&mp->m_peraglock);
663 goto nextag;
665 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
666 up_read(&mp->m_peraglock);
667 if (error)
668 goto nextag;
669 agi = XFS_BUF_TO_AGI(agbp);
670 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
673 * Here with an allocation group that has a free inode.
674 * Reset agno since we may have chosen a new ag in the
675 * loop above.
677 agno = tagno;
678 *IO_agbp = NULL;
679 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
680 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
682 * If pagino is 0 (this is the root inode allocation) use newino.
683 * This must work because we've just allocated some.
685 if (!pagino)
686 pagino = be32_to_cpu(agi->agi_newino);
687 #ifdef DEBUG
688 if (cur->bc_nlevels == 1) {
689 int freecount = 0;
691 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
692 goto error0;
693 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
694 do {
695 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
696 &rec.ir_freecount, &rec.ir_free, &i)))
697 goto error0;
698 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
699 freecount += rec.ir_freecount;
700 if ((error = xfs_inobt_increment(cur, 0, &i)))
701 goto error0;
702 } while (i == 1);
704 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
705 XFS_FORCED_SHUTDOWN(mp));
707 #endif
709 * If in the same a.g. as the parent, try to get near the parent.
711 if (pagno == agno) {
712 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
713 goto error0;
714 if (i != 0 &&
715 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
716 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
717 j == 1 &&
718 rec.ir_freecount > 0) {
720 * Found a free inode in the same chunk
721 * as parent, done.
725 * In the same a.g. as parent, but parent's chunk is full.
727 else {
728 int doneleft; /* done, to the left */
729 int doneright; /* done, to the right */
731 if (error)
732 goto error0;
733 ASSERT(i == 1);
734 ASSERT(j == 1);
736 * Duplicate the cursor, search left & right
737 * simultaneously.
739 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
740 goto error0;
742 * Search left with tcur, back up 1 record.
744 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
745 goto error1;
746 doneleft = !i;
747 if (!doneleft) {
748 if ((error = xfs_inobt_get_rec(tcur,
749 &trec.ir_startino,
750 &trec.ir_freecount,
751 &trec.ir_free, &i)))
752 goto error1;
753 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
756 * Search right with cur, go forward 1 record.
758 if ((error = xfs_inobt_increment(cur, 0, &i)))
759 goto error1;
760 doneright = !i;
761 if (!doneright) {
762 if ((error = xfs_inobt_get_rec(cur,
763 &rec.ir_startino,
764 &rec.ir_freecount,
765 &rec.ir_free, &i)))
766 goto error1;
767 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
770 * Loop until we find the closest inode chunk
771 * with a free one.
773 while (!doneleft || !doneright) {
774 int useleft; /* using left inode
775 chunk this time */
778 * Figure out which block is closer,
779 * if both are valid.
781 if (!doneleft && !doneright)
782 useleft =
783 pagino -
784 (trec.ir_startino +
785 XFS_INODES_PER_CHUNK - 1) <
786 rec.ir_startino - pagino;
787 else
788 useleft = !doneleft;
790 * If checking the left, does it have
791 * free inodes?
793 if (useleft && trec.ir_freecount) {
795 * Yes, set it up as the chunk to use.
797 rec = trec;
798 xfs_btree_del_cursor(cur,
799 XFS_BTREE_NOERROR);
800 cur = tcur;
801 break;
804 * If checking the right, does it have
805 * free inodes?
807 if (!useleft && rec.ir_freecount) {
809 * Yes, it's already set up.
811 xfs_btree_del_cursor(tcur,
812 XFS_BTREE_NOERROR);
813 break;
816 * If used the left, get another one
817 * further left.
819 if (useleft) {
820 if ((error = xfs_inobt_decrement(tcur, 0,
821 &i)))
822 goto error1;
823 doneleft = !i;
824 if (!doneleft) {
825 if ((error = xfs_inobt_get_rec(
826 tcur,
827 &trec.ir_startino,
828 &trec.ir_freecount,
829 &trec.ir_free, &i)))
830 goto error1;
831 XFS_WANT_CORRUPTED_GOTO(i == 1,
832 error1);
836 * If used the right, get another one
837 * further right.
839 else {
840 if ((error = xfs_inobt_increment(cur, 0,
841 &i)))
842 goto error1;
843 doneright = !i;
844 if (!doneright) {
845 if ((error = xfs_inobt_get_rec(
846 cur,
847 &rec.ir_startino,
848 &rec.ir_freecount,
849 &rec.ir_free, &i)))
850 goto error1;
851 XFS_WANT_CORRUPTED_GOTO(i == 1,
852 error1);
856 ASSERT(!doneleft || !doneright);
860 * In a different a.g. from the parent.
861 * See if the most recently allocated block has any free.
863 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
864 if ((error = xfs_inobt_lookup_eq(cur,
865 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
866 goto error0;
867 if (i == 1 &&
868 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
869 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
870 j == 1 &&
871 rec.ir_freecount > 0) {
873 * The last chunk allocated in the group still has
874 * a free inode.
878 * None left in the last group, search the whole a.g.
880 else {
881 if (error)
882 goto error0;
883 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
884 goto error0;
885 ASSERT(i == 1);
886 for (;;) {
887 if ((error = xfs_inobt_get_rec(cur,
888 &rec.ir_startino,
889 &rec.ir_freecount, &rec.ir_free,
890 &i)))
891 goto error0;
892 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
893 if (rec.ir_freecount > 0)
894 break;
895 if ((error = xfs_inobt_increment(cur, 0, &i)))
896 goto error0;
897 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
901 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
902 ASSERT(offset >= 0);
903 ASSERT(offset < XFS_INODES_PER_CHUNK);
904 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
905 XFS_INODES_PER_CHUNK) == 0);
906 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
907 XFS_INOBT_CLR_FREE(&rec, offset);
908 rec.ir_freecount--;
909 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
910 rec.ir_free)))
911 goto error0;
912 be32_add_cpu(&agi->agi_freecount, -1);
913 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
914 down_read(&mp->m_peraglock);
915 mp->m_perag[tagno].pagi_freecount--;
916 up_read(&mp->m_peraglock);
917 #ifdef DEBUG
918 if (cur->bc_nlevels == 1) {
919 int freecount = 0;
921 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
922 goto error0;
923 do {
924 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
925 &rec.ir_freecount, &rec.ir_free, &i)))
926 goto error0;
927 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
928 freecount += rec.ir_freecount;
929 if ((error = xfs_inobt_increment(cur, 0, &i)))
930 goto error0;
931 } while (i == 1);
932 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
933 XFS_FORCED_SHUTDOWN(mp));
935 #endif
936 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
937 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
938 *inop = ino;
939 return 0;
940 error1:
941 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
942 error0:
943 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
944 return error;
948 * Free disk inode. Carefully avoids touching the incore inode, all
949 * manipulations incore are the caller's responsibility.
950 * The on-disk inode is not changed by this operation, only the
951 * btree (free inode mask) is changed.
954 xfs_difree(
955 xfs_trans_t *tp, /* transaction pointer */
956 xfs_ino_t inode, /* inode to be freed */
957 xfs_bmap_free_t *flist, /* extents to free */
958 int *delete, /* set if inode cluster was deleted */
959 xfs_ino_t *first_ino) /* first inode in deleted cluster */
961 /* REFERENCED */
962 xfs_agblock_t agbno; /* block number containing inode */
963 xfs_buf_t *agbp; /* buffer containing allocation group header */
964 xfs_agino_t agino; /* inode number relative to allocation group */
965 xfs_agnumber_t agno; /* allocation group number */
966 xfs_agi_t *agi; /* allocation group header */
967 xfs_btree_cur_t *cur; /* inode btree cursor */
968 int error; /* error return value */
969 int i; /* result code */
970 int ilen; /* inodes in an inode cluster */
971 xfs_mount_t *mp; /* mount structure for filesystem */
972 int off; /* offset of inode in inode chunk */
973 xfs_inobt_rec_incore_t rec; /* btree record */
975 mp = tp->t_mountp;
978 * Break up inode number into its components.
980 agno = XFS_INO_TO_AGNO(mp, inode);
981 if (agno >= mp->m_sb.sb_agcount) {
982 cmn_err(CE_WARN,
983 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
984 agno, mp->m_sb.sb_agcount, mp->m_fsname);
985 ASSERT(0);
986 return XFS_ERROR(EINVAL);
988 agino = XFS_INO_TO_AGINO(mp, inode);
989 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
990 cmn_err(CE_WARN,
991 "xfs_difree: inode != XFS_AGINO_TO_INO() "
992 "(%llu != %llu) on %s. Returning EINVAL.",
993 (unsigned long long)inode,
994 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
995 mp->m_fsname);
996 ASSERT(0);
997 return XFS_ERROR(EINVAL);
999 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1000 if (agbno >= mp->m_sb.sb_agblocks) {
1001 cmn_err(CE_WARN,
1002 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1003 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1004 ASSERT(0);
1005 return XFS_ERROR(EINVAL);
1008 * Get the allocation group header.
1010 down_read(&mp->m_peraglock);
1011 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1012 up_read(&mp->m_peraglock);
1013 if (error) {
1014 cmn_err(CE_WARN,
1015 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1016 error, mp->m_fsname);
1017 return error;
1019 agi = XFS_BUF_TO_AGI(agbp);
1020 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1021 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1023 * Initialize the cursor.
1025 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1026 (xfs_inode_t *)0, 0);
1027 #ifdef DEBUG
1028 if (cur->bc_nlevels == 1) {
1029 int freecount = 0;
1031 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1032 goto error0;
1033 do {
1034 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1035 &rec.ir_freecount, &rec.ir_free, &i)))
1036 goto error0;
1037 if (i) {
1038 freecount += rec.ir_freecount;
1039 if ((error = xfs_inobt_increment(cur, 0, &i)))
1040 goto error0;
1042 } while (i == 1);
1043 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1044 XFS_FORCED_SHUTDOWN(mp));
1046 #endif
1048 * Look for the entry describing this inode.
1050 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1051 cmn_err(CE_WARN,
1052 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1053 error, mp->m_fsname);
1054 goto error0;
1056 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1057 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1058 &rec.ir_free, &i))) {
1059 cmn_err(CE_WARN,
1060 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1061 error, mp->m_fsname);
1062 goto error0;
1064 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1066 * Get the offset in the inode chunk.
1068 off = agino - rec.ir_startino;
1069 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1070 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1072 * Mark the inode free & increment the count.
1074 XFS_INOBT_SET_FREE(&rec, off);
1075 rec.ir_freecount++;
1078 * When an inode cluster is free, it becomes eligible for removal
1080 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1081 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1083 *delete = 1;
1084 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1087 * Remove the inode cluster from the AGI B+Tree, adjust the
1088 * AGI and Superblock inode counts, and mark the disk space
1089 * to be freed when the transaction is committed.
1091 ilen = XFS_IALLOC_INODES(mp);
1092 be32_add_cpu(&agi->agi_count, -ilen);
1093 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1094 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1095 down_read(&mp->m_peraglock);
1096 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1097 up_read(&mp->m_peraglock);
1098 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1099 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1101 if ((error = xfs_inobt_delete(cur, &i))) {
1102 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1103 error, mp->m_fsname);
1104 goto error0;
1107 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1108 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1109 XFS_IALLOC_BLOCKS(mp), flist, mp);
1110 } else {
1111 *delete = 0;
1113 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1114 cmn_err(CE_WARN,
1115 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1116 error, mp->m_fsname);
1117 goto error0;
1120 * Change the inode free counts and log the ag/sb changes.
1122 be32_add_cpu(&agi->agi_freecount, 1);
1123 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1124 down_read(&mp->m_peraglock);
1125 mp->m_perag[agno].pagi_freecount++;
1126 up_read(&mp->m_peraglock);
1127 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1130 #ifdef DEBUG
1131 if (cur->bc_nlevels == 1) {
1132 int freecount = 0;
1134 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1135 goto error0;
1136 do {
1137 if ((error = xfs_inobt_get_rec(cur,
1138 &rec.ir_startino,
1139 &rec.ir_freecount,
1140 &rec.ir_free, &i)))
1141 goto error0;
1142 if (i) {
1143 freecount += rec.ir_freecount;
1144 if ((error = xfs_inobt_increment(cur, 0, &i)))
1145 goto error0;
1147 } while (i == 1);
1148 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1149 XFS_FORCED_SHUTDOWN(mp));
1151 #endif
1152 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1153 return 0;
1155 error0:
1156 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1157 return error;
1161 * Return the location of the inode in bno/off, for mapping it into a buffer.
1163 /*ARGSUSED*/
1165 xfs_dilocate(
1166 xfs_mount_t *mp, /* file system mount structure */
1167 xfs_trans_t *tp, /* transaction pointer */
1168 xfs_ino_t ino, /* inode to locate */
1169 xfs_fsblock_t *bno, /* output: block containing inode */
1170 int *len, /* output: num blocks in inode cluster */
1171 int *off, /* output: index in block of inode */
1172 uint flags) /* flags concerning inode lookup */
1174 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1175 xfs_buf_t *agbp; /* agi buffer */
1176 xfs_agino_t agino; /* inode number within alloc group */
1177 xfs_agnumber_t agno; /* allocation group number */
1178 int blks_per_cluster; /* num blocks per inode cluster */
1179 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1180 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1181 __int32_t chunk_cnt; /* count of free inodes in chunk */
1182 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1183 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1184 xfs_btree_cur_t *cur; /* inode btree cursor */
1185 int error; /* error code */
1186 int i; /* temp state */
1187 int offset; /* index of inode in its buffer */
1188 int offset_agbno; /* blks from chunk start to inode */
1190 ASSERT(ino != NULLFSINO);
1192 * Split up the inode number into its parts.
1194 agno = XFS_INO_TO_AGNO(mp, ino);
1195 agino = XFS_INO_TO_AGINO(mp, ino);
1196 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1197 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1198 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1199 #ifdef DEBUG
1200 /* no diagnostics for bulkstat, ino comes from userspace */
1201 if (flags & XFS_IMAP_BULKSTAT)
1202 return XFS_ERROR(EINVAL);
1203 if (agno >= mp->m_sb.sb_agcount) {
1204 xfs_fs_cmn_err(CE_ALERT, mp,
1205 "xfs_dilocate: agno (%d) >= "
1206 "mp->m_sb.sb_agcount (%d)",
1207 agno, mp->m_sb.sb_agcount);
1209 if (agbno >= mp->m_sb.sb_agblocks) {
1210 xfs_fs_cmn_err(CE_ALERT, mp,
1211 "xfs_dilocate: agbno (0x%llx) >= "
1212 "mp->m_sb.sb_agblocks (0x%lx)",
1213 (unsigned long long) agbno,
1214 (unsigned long) mp->m_sb.sb_agblocks);
1216 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1217 xfs_fs_cmn_err(CE_ALERT, mp,
1218 "xfs_dilocate: ino (0x%llx) != "
1219 "XFS_AGINO_TO_INO(mp, agno, agino) "
1220 "(0x%llx)",
1221 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1223 xfs_stack_trace();
1224 #endif /* DEBUG */
1225 return XFS_ERROR(EINVAL);
1227 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1228 !(flags & XFS_IMAP_LOOKUP)) {
1229 offset = XFS_INO_TO_OFFSET(mp, ino);
1230 ASSERT(offset < mp->m_sb.sb_inopblock);
1231 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1232 *off = offset;
1233 *len = 1;
1234 return 0;
1236 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1237 if (*bno != NULLFSBLOCK) {
1238 offset = XFS_INO_TO_OFFSET(mp, ino);
1239 ASSERT(offset < mp->m_sb.sb_inopblock);
1240 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1241 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1242 offset;
1243 *len = blks_per_cluster;
1244 return 0;
1246 if (mp->m_inoalign_mask) {
1247 offset_agbno = agbno & mp->m_inoalign_mask;
1248 chunk_agbno = agbno - offset_agbno;
1249 } else {
1250 down_read(&mp->m_peraglock);
1251 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1252 up_read(&mp->m_peraglock);
1253 if (error) {
1254 #ifdef DEBUG
1255 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1256 "xfs_ialloc_read_agi() returned "
1257 "error %d, agno %d",
1258 error, agno);
1259 #endif /* DEBUG */
1260 return error;
1262 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1263 (xfs_inode_t *)0, 0);
1264 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1265 #ifdef DEBUG
1266 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1267 "xfs_inobt_lookup_le() failed");
1268 #endif /* DEBUG */
1269 goto error0;
1271 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1272 &chunk_free, &i))) {
1273 #ifdef DEBUG
1274 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1275 "xfs_inobt_get_rec() failed");
1276 #endif /* DEBUG */
1277 goto error0;
1279 if (i == 0) {
1280 #ifdef DEBUG
1281 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1282 "xfs_inobt_get_rec() failed");
1283 #endif /* DEBUG */
1284 error = XFS_ERROR(EINVAL);
1286 xfs_trans_brelse(tp, agbp);
1287 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1288 if (error)
1289 return error;
1290 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1291 offset_agbno = agbno - chunk_agbno;
1293 ASSERT(agbno >= chunk_agbno);
1294 cluster_agbno = chunk_agbno +
1295 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1296 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1297 XFS_INO_TO_OFFSET(mp, ino);
1298 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1299 *off = offset;
1300 *len = blks_per_cluster;
1301 return 0;
1302 error0:
1303 xfs_trans_brelse(tp, agbp);
1304 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1305 return error;
1309 * Compute and fill in value of m_in_maxlevels.
1311 void
1312 xfs_ialloc_compute_maxlevels(
1313 xfs_mount_t *mp) /* file system mount structure */
1315 int level;
1316 uint maxblocks;
1317 uint maxleafents;
1318 int minleafrecs;
1319 int minnoderecs;
1321 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1322 XFS_INODES_PER_CHUNK_LOG;
1323 minleafrecs = mp->m_alloc_mnr[0];
1324 minnoderecs = mp->m_alloc_mnr[1];
1325 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1326 for (level = 1; maxblocks > 1; level++)
1327 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1328 mp->m_in_maxlevels = level;
1332 * Log specified fields for the ag hdr (inode section)
1334 void
1335 xfs_ialloc_log_agi(
1336 xfs_trans_t *tp, /* transaction pointer */
1337 xfs_buf_t *bp, /* allocation group header buffer */
1338 int fields) /* bitmask of fields to log */
1340 int first; /* first byte number */
1341 int last; /* last byte number */
1342 static const short offsets[] = { /* field starting offsets */
1343 /* keep in sync with bit definitions */
1344 offsetof(xfs_agi_t, agi_magicnum),
1345 offsetof(xfs_agi_t, agi_versionnum),
1346 offsetof(xfs_agi_t, agi_seqno),
1347 offsetof(xfs_agi_t, agi_length),
1348 offsetof(xfs_agi_t, agi_count),
1349 offsetof(xfs_agi_t, agi_root),
1350 offsetof(xfs_agi_t, agi_level),
1351 offsetof(xfs_agi_t, agi_freecount),
1352 offsetof(xfs_agi_t, agi_newino),
1353 offsetof(xfs_agi_t, agi_dirino),
1354 offsetof(xfs_agi_t, agi_unlinked),
1355 sizeof(xfs_agi_t)
1357 #ifdef DEBUG
1358 xfs_agi_t *agi; /* allocation group header */
1360 agi = XFS_BUF_TO_AGI(bp);
1361 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1362 #endif
1364 * Compute byte offsets for the first and last fields.
1366 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1368 * Log the allocation group inode header buffer.
1370 xfs_trans_log_buf(tp, bp, first, last);
1374 * Read in the allocation group header (inode allocation section)
1377 xfs_ialloc_read_agi(
1378 xfs_mount_t *mp, /* file system mount structure */
1379 xfs_trans_t *tp, /* transaction pointer */
1380 xfs_agnumber_t agno, /* allocation group number */
1381 xfs_buf_t **bpp) /* allocation group hdr buf */
1383 xfs_agi_t *agi; /* allocation group header */
1384 int agi_ok; /* agi is consistent */
1385 xfs_buf_t *bp; /* allocation group hdr buf */
1386 xfs_perag_t *pag; /* per allocation group data */
1387 int error;
1389 ASSERT(agno != NULLAGNUMBER);
1390 error = xfs_trans_read_buf(
1391 mp, tp, mp->m_ddev_targp,
1392 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1393 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1394 if (error)
1395 return error;
1396 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1399 * Validate the magic number of the agi block.
1401 agi = XFS_BUF_TO_AGI(bp);
1402 agi_ok =
1403 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1404 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1405 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1406 XFS_RANDOM_IALLOC_READ_AGI))) {
1407 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1408 mp, agi);
1409 xfs_trans_brelse(tp, bp);
1410 return XFS_ERROR(EFSCORRUPTED);
1412 pag = &mp->m_perag[agno];
1413 if (!pag->pagi_init) {
1414 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1415 pag->pagi_count = be32_to_cpu(agi->agi_count);
1416 pag->pagi_init = 1;
1417 } else {
1419 * It's possible for these to be out of sync if
1420 * we are in the middle of a forced shutdown.
1422 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1423 XFS_FORCED_SHUTDOWN(mp));
1426 #ifdef DEBUG
1428 int i;
1430 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1431 ASSERT(agi->agi_unlinked[i]);
1433 #endif
1435 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1436 *bpp = bp;
1437 return 0;
1441 * Read in the agi to initialise the per-ag data in the mount structure
1444 xfs_ialloc_pagi_init(
1445 xfs_mount_t *mp, /* file system mount structure */
1446 xfs_trans_t *tp, /* transaction pointer */
1447 xfs_agnumber_t agno) /* allocation group number */
1449 xfs_buf_t *bp = NULL;
1450 int error;
1452 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1453 if (error)
1454 return error;
1455 if (bp)
1456 xfs_trans_brelse(tp, bp);
1457 return 0;