2 * Copyright (c) 2000 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/
40 * Log specified fields for the inode given by bp and off.
44 xfs_trans_t
*tp
, /* transaction pointer */
45 xfs_buf_t
*bp
, /* inode buffer */
46 int off
, /* index of inode in buffer */
47 int fields
) /* bitmask of fields to log */
49 int first
; /* first byte number */
50 int ioffset
; /* off in bytes */
51 int last
; /* last byte number */
52 xfs_mount_t
*mp
; /* mount point structure */
53 static const short offsets
[] = { /* field offsets */
54 /* keep in sync with bits */
55 offsetof(xfs_dinode_core_t
, di_magic
),
56 offsetof(xfs_dinode_core_t
, di_mode
),
57 offsetof(xfs_dinode_core_t
, di_version
),
58 offsetof(xfs_dinode_core_t
, di_format
),
59 offsetof(xfs_dinode_core_t
, di_onlink
),
60 offsetof(xfs_dinode_core_t
, di_uid
),
61 offsetof(xfs_dinode_core_t
, di_gid
),
62 offsetof(xfs_dinode_core_t
, di_nlink
),
63 offsetof(xfs_dinode_core_t
, di_projid
),
64 offsetof(xfs_dinode_core_t
, di_pad
),
65 offsetof(xfs_dinode_core_t
, di_atime
),
66 offsetof(xfs_dinode_core_t
, di_mtime
),
67 offsetof(xfs_dinode_core_t
, di_ctime
),
68 offsetof(xfs_dinode_core_t
, di_size
),
69 offsetof(xfs_dinode_core_t
, di_nblocks
),
70 offsetof(xfs_dinode_core_t
, di_extsize
),
71 offsetof(xfs_dinode_core_t
, di_nextents
),
72 offsetof(xfs_dinode_core_t
, di_anextents
),
73 offsetof(xfs_dinode_core_t
, di_forkoff
),
74 offsetof(xfs_dinode_core_t
, di_aformat
),
75 offsetof(xfs_dinode_core_t
, di_dmevmask
),
76 offsetof(xfs_dinode_core_t
, di_dmstate
),
77 offsetof(xfs_dinode_core_t
, di_flags
),
78 offsetof(xfs_dinode_core_t
, di_gen
),
79 offsetof(xfs_dinode_t
, di_next_unlinked
),
80 offsetof(xfs_dinode_t
, di_u
),
81 offsetof(xfs_dinode_t
, di_a
),
86 ASSERT(offsetof(xfs_dinode_t
, di_core
) == 0);
87 ASSERT((fields
& (XFS_DI_U
|XFS_DI_A
)) == 0);
90 * Get the inode-relative first and last bytes for these fields
92 xfs_btree_offsets(fields
, offsets
, XFS_DI_NUM_BITS
, &first
, &last
);
94 * Convert to buffer offsets and log it.
96 ioffset
= off
<< mp
->m_sb
.sb_inodelog
;
99 xfs_trans_log_buf(tp
, bp
, first
, last
);
103 * Allocation group level functions.
107 * Allocate new inodes in the allocation group specified by agbp.
108 * Return 0 for success, else error code.
110 STATIC
int /* error code or 0 */
112 xfs_trans_t
*tp
, /* transaction pointer */
113 xfs_buf_t
*agbp
, /* alloc group buffer */
116 xfs_agi_t
*agi
; /* allocation group header */
117 xfs_alloc_arg_t args
; /* allocation argument structure */
118 int blks_per_cluster
; /* fs blocks per inode cluster */
119 xfs_btree_cur_t
*cur
; /* inode btree cursor */
120 xfs_daddr_t d
; /* disk addr of buffer */
122 xfs_buf_t
*fbuf
; /* new free inodes' buffer */
123 xfs_dinode_t
*free
; /* new free inode structure */
124 int i
; /* inode counter */
125 int j
; /* block counter */
126 int nbufs
; /* num bufs of new inodes */
127 xfs_agino_t newino
; /* new first inode's number */
128 xfs_agino_t newlen
; /* new number of inodes */
129 int ninodes
; /* num inodes per buf */
130 xfs_agino_t thisino
; /* current inode number, for loop */
131 int version
; /* inode version number to use */
132 static xfs_timestamp_t ztime
; /* zero xfs timestamp */
133 int isaligned
; /* inode allocation at stripe unit */
135 xfs_dinode_core_t dic
; /* a dinode_core to copy to new */
139 args
.mp
= tp
->t_mountp
;
142 * Locking will ensure that we don't have two callers in here
145 newlen
= XFS_IALLOC_INODES(args
.mp
);
146 if (args
.mp
->m_maxicount
&&
147 args
.mp
->m_sb
.sb_icount
+ newlen
> args
.mp
->m_maxicount
)
148 return XFS_ERROR(ENOSPC
);
149 args
.minlen
= args
.maxlen
= XFS_IALLOC_BLOCKS(args
.mp
);
151 * Set the alignment for the allocation.
152 * If stripe alignment is turned on then align at stripe unit
154 * If the cluster size is smaller than a filesystem block
155 * then we're doing I/O for inodes in filesystem block size pieces,
156 * so don't need alignment anyway.
159 if (args
.mp
->m_sinoalign
) {
160 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
161 args
.alignment
= args
.mp
->m_dalign
;
163 } else if (XFS_SB_VERSION_HASALIGN(&args
.mp
->m_sb
) &&
164 args
.mp
->m_sb
.sb_inoalignmt
>=
165 XFS_B_TO_FSBT(args
.mp
, XFS_INODE_CLUSTER_SIZE(args
.mp
)))
166 args
.alignment
= args
.mp
->m_sb
.sb_inoalignmt
;
169 agi
= XFS_BUF_TO_AGI(agbp
);
171 * Need to figure out where to allocate the inode blocks.
172 * Ideally they should be spaced out through the a.g.
173 * For now, just allocate blocks up front.
175 args
.agbno
= INT_GET(agi
->agi_root
, ARCH_CONVERT
);
176 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, INT_GET(agi
->agi_seqno
, ARCH_CONVERT
),
179 * Allocate a fixed-size extent of inodes.
181 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
182 args
.mod
= args
.total
= args
.wasdel
= args
.isfl
= args
.userdata
=
183 args
.minalignslop
= 0;
186 * Allow space for the inode btree to split.
188 args
.minleft
= XFS_IN_MAXLEVELS(args
.mp
) - 1;
189 if ((error
= xfs_alloc_vextent(&args
)))
193 * If stripe alignment is turned on, then try again with cluster
196 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
197 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
198 args
.agbno
= INT_GET(agi
->agi_root
, ARCH_CONVERT
);
199 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
,
200 INT_GET(agi
->agi_seqno
, ARCH_CONVERT
), args
.agbno
);
201 if (XFS_SB_VERSION_HASALIGN(&args
.mp
->m_sb
) &&
202 args
.mp
->m_sb
.sb_inoalignmt
>=
203 XFS_B_TO_FSBT(args
.mp
, XFS_INODE_CLUSTER_SIZE(args
.mp
)))
204 args
.alignment
= args
.mp
->m_sb
.sb_inoalignmt
;
207 if ((error
= xfs_alloc_vextent(&args
)))
211 if (args
.fsbno
== NULLFSBLOCK
) {
215 ASSERT(args
.len
== args
.minlen
);
217 * Convert the results.
219 newino
= XFS_OFFBNO_TO_AGINO(args
.mp
, args
.agbno
, 0);
221 * Loop over the new block(s), filling in the inodes.
222 * For small block sizes, manipulate the inodes in buffers
223 * which are multiples of the blocks size.
225 if (args
.mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(args
.mp
)) {
226 blks_per_cluster
= 1;
227 nbufs
= (int)args
.len
;
228 ninodes
= args
.mp
->m_sb
.sb_inopblock
;
230 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(args
.mp
) /
231 args
.mp
->m_sb
.sb_blocksize
;
232 nbufs
= (int)args
.len
/ blks_per_cluster
;
233 ninodes
= blks_per_cluster
* args
.mp
->m_sb
.sb_inopblock
;
236 * Figure out what version number to use in the inodes we create.
237 * If the superblock version has caught up to the one that supports
238 * the new inode format, then use the new inode version. Otherwise
239 * use the old version so that old kernels will continue to be
240 * able to use the file system.
242 if (XFS_SB_VERSION_HASNLINK(&args
.mp
->m_sb
))
243 version
= XFS_DINODE_VERSION_2
;
245 version
= XFS_DINODE_VERSION_1
;
246 for (j
= 0; j
< nbufs
; j
++) {
250 d
= XFS_AGB_TO_DADDR(args
.mp
, INT_GET(agi
->agi_seqno
, ARCH_CONVERT
),
251 args
.agbno
+ (j
* blks_per_cluster
));
252 fbuf
= xfs_trans_get_buf(tp
, args
.mp
->m_ddev_targp
, d
,
253 args
.mp
->m_bsize
* blks_per_cluster
,
256 ASSERT(!XFS_BUF_GETERROR(fbuf
));
258 * Loop over the inodes in this buffer.
260 INT_SET(dic
.di_magic
, ARCH_CONVERT
, XFS_DINODE_MAGIC
);
261 INT_ZERO(dic
.di_mode
, ARCH_CONVERT
);
262 INT_SET(dic
.di_version
, ARCH_CONVERT
, version
);
263 INT_ZERO(dic
.di_format
, ARCH_CONVERT
);
264 INT_ZERO(dic
.di_onlink
, ARCH_CONVERT
);
265 INT_ZERO(dic
.di_uid
, ARCH_CONVERT
);
266 INT_ZERO(dic
.di_gid
, ARCH_CONVERT
);
267 INT_ZERO(dic
.di_nlink
, ARCH_CONVERT
);
268 INT_ZERO(dic
.di_projid
, ARCH_CONVERT
);
269 bzero(&(dic
.di_pad
[0]),sizeof(dic
.di_pad
));
270 INT_SET(dic
.di_atime
.t_sec
, ARCH_CONVERT
, ztime
.t_sec
);
271 INT_SET(dic
.di_atime
.t_nsec
, ARCH_CONVERT
, ztime
.t_nsec
);
273 INT_SET(dic
.di_mtime
.t_sec
, ARCH_CONVERT
, ztime
.t_sec
);
274 INT_SET(dic
.di_mtime
.t_nsec
, ARCH_CONVERT
, ztime
.t_nsec
);
276 INT_SET(dic
.di_ctime
.t_sec
, ARCH_CONVERT
, ztime
.t_sec
);
277 INT_SET(dic
.di_ctime
.t_nsec
, ARCH_CONVERT
, ztime
.t_nsec
);
279 INT_ZERO(dic
.di_size
, ARCH_CONVERT
);
280 INT_ZERO(dic
.di_nblocks
, ARCH_CONVERT
);
281 INT_ZERO(dic
.di_extsize
, ARCH_CONVERT
);
282 INT_ZERO(dic
.di_nextents
, ARCH_CONVERT
);
283 INT_ZERO(dic
.di_anextents
, ARCH_CONVERT
);
284 INT_ZERO(dic
.di_forkoff
, ARCH_CONVERT
);
285 INT_ZERO(dic
.di_aformat
, ARCH_CONVERT
);
286 INT_ZERO(dic
.di_dmevmask
, ARCH_CONVERT
);
287 INT_ZERO(dic
.di_dmstate
, ARCH_CONVERT
);
288 INT_ZERO(dic
.di_flags
, ARCH_CONVERT
);
289 INT_ZERO(dic
.di_gen
, ARCH_CONVERT
);
291 for (i
= 0; i
< ninodes
; i
++) {
292 free
= XFS_MAKE_IPTR(args
.mp
, fbuf
, i
);
293 bcopy (&dic
, &(free
->di_core
), sizeof(xfs_dinode_core_t
));
294 INT_SET(free
->di_next_unlinked
, ARCH_CONVERT
, NULLAGINO
);
295 xfs_ialloc_log_di(tp
, fbuf
, i
,
296 XFS_DI_CORE_BITS
| XFS_DI_NEXT_UNLINKED
);
298 xfs_trans_inode_alloc_buf(tp
, fbuf
);
300 INT_MOD(agi
->agi_count
, ARCH_CONVERT
, newlen
);
301 INT_MOD(agi
->agi_freecount
, ARCH_CONVERT
, newlen
);
302 mraccess(&args
.mp
->m_peraglock
);
303 args
.mp
->m_perag
[INT_GET(agi
->agi_seqno
, ARCH_CONVERT
)].pagi_freecount
+= newlen
;
304 mraccunlock(&args
.mp
->m_peraglock
);
305 INT_SET(agi
->agi_newino
, ARCH_CONVERT
, newino
);
307 * Insert records describing the new inode chunk into the btree.
309 cur
= xfs_btree_init_cursor(args
.mp
, tp
, agbp
,
310 INT_GET(agi
->agi_seqno
, ARCH_CONVERT
),
311 XFS_BTNUM_INO
, (xfs_inode_t
*)0, 0);
312 for (thisino
= newino
;
313 thisino
< newino
+ newlen
;
314 thisino
+= XFS_INODES_PER_CHUNK
) {
315 if ((error
= xfs_inobt_lookup_eq(cur
, thisino
,
316 XFS_INODES_PER_CHUNK
, XFS_INOBT_ALL_FREE
, &i
))) {
317 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
321 if ((error
= xfs_inobt_insert(cur
, &i
))) {
322 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
327 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
329 * Log allocation group header fields
331 xfs_ialloc_log_agi(tp
, agbp
,
332 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
334 * Modify/log superblock values for inode count and inode free count.
336 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
337 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
343 * Select an allocation group to look for a free inode in, based on the parent
344 * inode and then mode. Return the allocation group buffer.
346 STATIC xfs_buf_t
* /* allocation group buffer */
347 xfs_ialloc_ag_select(
348 xfs_trans_t
*tp
, /* transaction pointer */
349 xfs_ino_t parent
, /* parent directory inode number */
350 mode_t mode
, /* bits set to indicate file type */
351 int okalloc
) /* ok to allocate more space */
353 xfs_buf_t
*agbp
; /* allocation group header buffer */
354 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
355 xfs_agnumber_t agno
; /* current ag number */
356 int flags
; /* alloc buffer locking flags */
357 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
358 xfs_extlen_t longest
= 0; /* longest extent available */
359 xfs_mount_t
*mp
; /* mount point structure */
360 int needspace
; /* file mode implies space allocated */
361 xfs_perag_t
*pag
; /* per allocation group data */
362 xfs_agnumber_t pagno
; /* parent (starting) ag number */
365 * Files of these types need at least one block if length > 0
366 * (and they won't fit in the inode, but that's hard to figure out).
368 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
370 agcount
= mp
->m_sb
.sb_agcount
;
372 pagno
= atomicIncWithWrap((int *)&mp
->m_agirotor
, agcount
);
374 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
375 ASSERT(pagno
< agcount
);
377 * Loop through allocation groups, looking for one with a little
378 * free space in it. Note we don't look for free inodes, exactly.
379 * Instead, we include whether there is a need to allocate inodes
380 * to mean that blocks must be allocated for them,
381 * if none are currently free.
384 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
386 mraccess(&mp
->m_peraglock
);
387 pag
= &mp
->m_perag
[agno
];
388 if (!pag
->pagi_init
) {
389 if (xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
391 mraccunlock(&mp
->m_peraglock
);
397 * Is there enough free space for the file plus a block
398 * of inodes (if we need to allocate some)?
400 ineed
= pag
->pagi_freecount
? 0 : XFS_IALLOC_BLOCKS(mp
);
401 if (ineed
&& !pag
->pagf_init
) {
403 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
405 mraccunlock(&mp
->m_peraglock
);
408 (void)xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
410 if (!ineed
|| pag
->pagf_init
) {
411 if (ineed
&& !(longest
= pag
->pagf_longest
))
412 longest
= pag
->pagf_flcount
> 0;
414 (pag
->pagf_freeblks
>= needspace
+ ineed
&&
418 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
420 mraccunlock(&mp
->m_peraglock
);
423 mraccunlock(&mp
->m_peraglock
);
427 mraccunlock(&mp
->m_peraglock
);
429 xfs_trans_brelse(tp
, agbp
);
432 * No point in iterating over the rest, if we're shutting
435 if (XFS_FORCED_SHUTDOWN(mp
))
436 return (xfs_buf_t
*)0;
442 return (xfs_buf_t
*)0;
449 * Visible inode allocation functions.
453 * Allocate an inode on disk.
454 * Mode is used to tell whether the new inode will need space, and whether
457 * The arguments IO_agbp and alloc_done are defined to work within
458 * the constraint of one allocation per transaction.
459 * xfs_dialloc() is designed to be called twice if it has to do an
460 * allocation to make more free inodes. On the first call,
461 * IO_agbp should be set to NULL. If an inode is available,
462 * i.e., xfs_dialloc() did not need to do an allocation, an inode
463 * number is returned. In this case, IO_agbp would be set to the
464 * current ag_buf and alloc_done set to false.
465 * If an allocation needed to be done, xfs_dialloc would return
466 * the current ag_buf in IO_agbp and set alloc_done to true.
467 * The caller should then commit the current transaction, allocate a new
468 * transaction, and call xfs_dialloc() again, passing in the previous
469 * value of IO_agbp. IO_agbp should be held across the transactions.
470 * Since the agbp is locked across the two calls, the second call is
471 * guaranteed to have a free inode available.
473 * Once we successfully pick an inode its number is returned and the
474 * on-disk data structures are updated. The inode itself is not read
475 * in, since doing so would break ordering constraints with xfs_reclaim.
479 xfs_trans_t
*tp
, /* transaction pointer */
480 xfs_ino_t parent
, /* parent inode (directory) */
481 mode_t mode
, /* mode bits for new inode */
482 int okalloc
, /* ok to allocate more space */
483 xfs_buf_t
**IO_agbp
, /* in/out ag header's buffer */
484 boolean_t
*alloc_done
, /* true if we needed to replenish
486 xfs_ino_t
*inop
) /* inode number allocated */
488 xfs_agnumber_t agcount
; /* number of allocation groups */
489 xfs_buf_t
*agbp
; /* allocation group header's buffer */
490 xfs_agnumber_t agno
; /* allocation group number */
491 xfs_agi_t
*agi
; /* allocation group header structure */
492 xfs_btree_cur_t
*cur
; /* inode allocation btree cursor */
493 int error
; /* error return value */
494 int i
; /* result code */
495 int ialloced
; /* inode allocation status */
496 int noroom
= 0; /* no space for inode blk allocation */
497 xfs_ino_t ino
; /* fs-relative inode to be returned */
499 int j
; /* result code */
500 xfs_mount_t
*mp
; /* file system mount structure */
501 int offset
; /* index of inode in chunk */
502 xfs_agino_t pagino
; /* parent's a.g. relative inode # */
503 xfs_agnumber_t pagno
; /* parent's allocation group number */
504 xfs_inobt_rec_t rec
; /* inode allocation record */
505 xfs_agnumber_t tagno
; /* testing allocation group number */
506 xfs_btree_cur_t
*tcur
; /* temp cursor */
507 xfs_inobt_rec_t trec
; /* temp inode allocation record */
510 if (*IO_agbp
== NULL
) {
512 * We do not have an agbp, so select an initial allocation
513 * group for inode allocation.
515 agbp
= xfs_ialloc_ag_select(tp
, parent
, mode
, okalloc
);
517 * Couldn't find an allocation group satisfying the
524 agi
= XFS_BUF_TO_AGI(agbp
);
525 ASSERT(INT_GET(agi
->agi_magicnum
, ARCH_CONVERT
) == XFS_AGI_MAGIC
);
528 * Continue where we left off before. In this case, we
529 * know that the allocation group has free inodes.
532 agi
= XFS_BUF_TO_AGI(agbp
);
533 ASSERT(INT_GET(agi
->agi_magicnum
, ARCH_CONVERT
) == XFS_AGI_MAGIC
);
534 ASSERT(INT_GET(agi
->agi_freecount
, ARCH_CONVERT
) > 0);
537 agcount
= mp
->m_sb
.sb_agcount
;
538 agno
= INT_GET(agi
->agi_seqno
, ARCH_CONVERT
);
540 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
541 pagino
= XFS_INO_TO_AGINO(mp
, parent
);
544 * If we have already hit the ceiling of inode blocks then clear
545 * okalloc so we scan all available agi structures for a free
549 if (mp
->m_maxicount
&&
550 mp
->m_sb
.sb_icount
+ XFS_IALLOC_INODES(mp
) > mp
->m_maxicount
) {
556 * Loop until we find an allocation group that either has free inodes
557 * or in which we can allocate some inodes. Iterate through the
558 * allocation groups upward, wrapping at the end.
560 *alloc_done
= B_FALSE
;
561 while (INT_GET(agi
->agi_freecount
, ARCH_CONVERT
) == 0) {
563 * Don't do anything if we're not supposed to allocate
564 * any blocks, just go on to the next ag.
568 * Try to allocate some new inodes in the allocation
571 if ((error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
))) {
572 xfs_trans_brelse(tp
, agbp
);
573 if (error
== ENOSPC
) {
581 * We successfully allocated some inodes, return
582 * the current context to the caller so that it
583 * can commit the current transaction and call
584 * us again where we left off.
586 ASSERT(INT_GET(agi
->agi_freecount
, ARCH_CONVERT
) > 0);
587 *alloc_done
= B_TRUE
;
594 * If it failed, give up on this ag.
596 xfs_trans_brelse(tp
, agbp
);
598 * Go on to the next ag: get its ag header.
601 if (++tagno
== agcount
)
605 return noroom
? ENOSPC
: 0;
607 mraccess(&mp
->m_peraglock
);
608 error
= xfs_ialloc_read_agi(mp
, tp
, tagno
, &agbp
);
609 mraccunlock(&mp
->m_peraglock
);
612 agi
= XFS_BUF_TO_AGI(agbp
);
613 ASSERT(INT_GET(agi
->agi_magicnum
, ARCH_CONVERT
) == XFS_AGI_MAGIC
);
616 * Here with an allocation group that has a free inode.
617 * Reset agno since we may have chosen a new ag in the
622 cur
= xfs_btree_init_cursor(mp
, tp
, agbp
, INT_GET(agi
->agi_seqno
, ARCH_CONVERT
),
623 XFS_BTNUM_INO
, (xfs_inode_t
*)0, 0);
625 * If pagino is 0 (this is the root inode allocation) use newino.
626 * This must work because we've just allocated some.
629 pagino
= INT_GET(agi
->agi_newino
, ARCH_CONVERT
);
631 if (cur
->bc_nlevels
== 1) {
634 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
636 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
638 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
639 &rec
.ir_freecount
, &rec
.ir_free
, &i
, ARCH_NOCONVERT
)))
641 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
642 freecount
+= rec
.ir_freecount
;
643 if ((error
= xfs_inobt_increment(cur
, 0, &i
)))
647 ASSERT(freecount
== INT_GET(agi
->agi_freecount
, ARCH_CONVERT
) ||
648 XFS_FORCED_SHUTDOWN(mp
));
652 * If in the same a.g. as the parent, try to get near the parent.
655 if ((error
= xfs_inobt_lookup_le(cur
, pagino
, 0, 0, &i
)))
658 (error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
659 &rec
.ir_freecount
, &rec
.ir_free
, &j
, ARCH_NOCONVERT
)) == 0 &&
661 rec
.ir_freecount
> 0) {
663 * Found a free inode in the same chunk
668 * In the same a.g. as parent, but parent's chunk is full.
671 int doneleft
; /* done, to the left */
672 int doneright
; /* done, to the right */
679 * Duplicate the cursor, search left & right
682 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
)))
685 * Search left with tcur, back up 1 record.
687 if ((error
= xfs_inobt_decrement(tcur
, 0, &i
)))
691 if ((error
= xfs_inobt_get_rec(tcur
,
694 &trec
.ir_free
, &i
, ARCH_NOCONVERT
)))
696 XFS_WANT_CORRUPTED_GOTO(i
== 1, error1
);
699 * Search right with cur, go forward 1 record.
701 if ((error
= xfs_inobt_increment(cur
, 0, &i
)))
705 if ((error
= xfs_inobt_get_rec(cur
,
708 &rec
.ir_free
, &i
, ARCH_NOCONVERT
)))
710 XFS_WANT_CORRUPTED_GOTO(i
== 1, error1
);
713 * Loop until we find the closest inode chunk
716 while (!doneleft
|| !doneright
) {
717 int useleft
; /* using left inode
721 * Figure out which block is closer,
724 if (!doneleft
&& !doneright
)
728 XFS_INODES_PER_CHUNK
- 1) <
729 rec
.ir_startino
- pagino
;
733 * If checking the left, does it have
736 if (useleft
&& trec
.ir_freecount
) {
738 * Yes, set it up as the chunk to use.
741 xfs_btree_del_cursor(cur
,
747 * If checking the right, does it have
750 if (!useleft
&& rec
.ir_freecount
) {
752 * Yes, it's already set up.
754 xfs_btree_del_cursor(tcur
,
759 * If used the left, get another one
763 if ((error
= xfs_inobt_decrement(tcur
, 0,
768 if ((error
= xfs_inobt_get_rec(
772 &trec
.ir_free
, &i
, ARCH_NOCONVERT
)))
774 XFS_WANT_CORRUPTED_GOTO(i
== 1,
779 * If used the right, get another one
783 if ((error
= xfs_inobt_increment(cur
, 0,
788 if ((error
= xfs_inobt_get_rec(
792 &rec
.ir_free
, &i
, ARCH_NOCONVERT
)))
794 XFS_WANT_CORRUPTED_GOTO(i
== 1,
799 ASSERT(!doneleft
|| !doneright
);
803 * In a different a.g. from the parent.
804 * See if the most recently allocated block has any free.
806 else if (INT_GET(agi
->agi_newino
, ARCH_CONVERT
) != NULLAGINO
) {
807 if ((error
= xfs_inobt_lookup_eq(cur
,
808 INT_GET(agi
->agi_newino
, ARCH_CONVERT
), 0, 0, &i
)))
811 (error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
812 &rec
.ir_freecount
, &rec
.ir_free
, &j
, ARCH_NOCONVERT
)) == 0 &&
814 rec
.ir_freecount
> 0) {
816 * The last chunk allocated in the group still has
821 * None left in the last group, search the whole a.g.
826 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
830 if ((error
= xfs_inobt_get_rec(cur
,
832 &rec
.ir_freecount
, &rec
.ir_free
,
833 &i
, ARCH_NOCONVERT
)))
835 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
836 if (rec
.ir_freecount
> 0)
838 if ((error
= xfs_inobt_increment(cur
, 0, &i
)))
840 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
844 offset
= XFS_IALLOC_FIND_FREE(&rec
.ir_free
);
846 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
847 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
848 XFS_INODES_PER_CHUNK
) == 0);
849 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
850 XFS_INOBT_CLR_FREE(&rec
, offset
, ARCH_NOCONVERT
);
852 if ((error
= xfs_inobt_update(cur
, rec
.ir_startino
, rec
.ir_freecount
,
855 INT_MOD(agi
->agi_freecount
, ARCH_CONVERT
, -1);
856 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
857 mraccess(&mp
->m_peraglock
);
858 mp
->m_perag
[tagno
].pagi_freecount
--;
859 mraccunlock(&mp
->m_peraglock
);
861 if (cur
->bc_nlevels
== 1) {
864 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
867 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
868 &rec
.ir_freecount
, &rec
.ir_free
, &i
, ARCH_NOCONVERT
)))
870 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
871 freecount
+= rec
.ir_freecount
;
872 if ((error
= xfs_inobt_increment(cur
, 0, &i
)))
875 ASSERT(freecount
== INT_GET(agi
->agi_freecount
, ARCH_CONVERT
) ||
876 XFS_FORCED_SHUTDOWN(mp
));
879 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
880 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
884 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
886 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
892 * Return the location of the inode in bno/off, for mapping it into a buffer.
897 xfs_mount_t
*mp
, /* file system mount structure */
898 xfs_trans_t
*tp
, /* transaction pointer */
899 xfs_ino_t ino
, /* inode to locate */
900 xfs_fsblock_t
*bno
, /* output: block containing inode */
901 int *len
, /* output: num blocks in inode cluster */
902 int *off
, /* output: index in block of inode */
903 uint flags
) /* flags concerning inode lookup */
905 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
906 xfs_buf_t
*agbp
; /* agi buffer */
907 xfs_agino_t agino
; /* inode number within alloc group */
908 xfs_agnumber_t agno
; /* allocation group number */
909 int blks_per_cluster
; /* num blocks per inode cluster */
910 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
911 xfs_agino_t chunk_agino
; /* first agino in inode chunk */
912 __int32_t chunk_cnt
; /* count of free inodes in chunk */
913 xfs_inofree_t chunk_free
; /* mask of free inodes in chunk */
914 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
915 xfs_btree_cur_t
*cur
; /* inode btree cursor */
916 int error
; /* error code */
917 int i
; /* temp state */
918 int offset
; /* index of inode in its buffer */
919 int offset_agbno
; /* blks from chunk start to inode */
921 ASSERT(ino
!= NULLFSINO
);
923 * Split up the inode number into its parts.
925 agno
= XFS_INO_TO_AGNO(mp
, ino
);
926 agino
= XFS_INO_TO_AGINO(mp
, ino
);
927 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
928 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
929 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
))
930 return XFS_ERROR(EINVAL
);
931 if ((mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(mp
)) ||
932 !(flags
& XFS_IMAP_LOOKUP
)) {
933 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
934 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
935 *bno
= XFS_AGB_TO_FSB(mp
, agno
, agbno
);
940 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) >> mp
->m_sb
.sb_blocklog
;
941 if (*bno
!= NULLFSBLOCK
) {
942 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
943 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
944 cluster_agbno
= XFS_FSB_TO_AGBNO(mp
, *bno
);
945 *off
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
947 *len
= blks_per_cluster
;
950 if (mp
->m_inoalign_mask
) {
951 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
952 chunk_agbno
= agbno
- offset_agbno
;
954 mraccess(&mp
->m_peraglock
);
955 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
956 mraccunlock(&mp
->m_peraglock
);
959 cur
= xfs_btree_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
,
960 (xfs_inode_t
*)0, 0);
961 if ((error
= xfs_inobt_lookup_le(cur
, agino
, 0, 0, &i
)))
963 if ((error
= xfs_inobt_get_rec(cur
, &chunk_agino
, &chunk_cnt
,
964 &chunk_free
, &i
, ARCH_NOCONVERT
)))
967 error
= XFS_ERROR(EINVAL
);
968 xfs_trans_brelse(tp
, agbp
);
969 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
972 chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, chunk_agino
);
973 offset_agbno
= agbno
- chunk_agbno
;
975 ASSERT(agbno
>= chunk_agbno
);
976 cluster_agbno
= chunk_agbno
+
977 ((offset_agbno
/ blks_per_cluster
) * blks_per_cluster
);
978 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
979 XFS_INO_TO_OFFSET(mp
, ino
);
980 *bno
= XFS_AGB_TO_FSB(mp
, agno
, cluster_agbno
);
982 *len
= blks_per_cluster
;
985 xfs_trans_brelse(tp
, agbp
);
986 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
991 * Compute and fill in value of m_in_maxlevels.
994 xfs_ialloc_compute_maxlevels(
995 xfs_mount_t
*mp
) /* file system mount structure */
1003 maxleafents
= (1LL << XFS_INO_AGINO_BITS(mp
)) >>
1004 XFS_INODES_PER_CHUNK_LOG
;
1005 minleafrecs
= mp
->m_alloc_mnr
[0];
1006 minnoderecs
= mp
->m_alloc_mnr
[1];
1007 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1008 for (level
= 1; maxblocks
> 1; level
++)
1009 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1010 mp
->m_in_maxlevels
= level
;
1014 * Log specified fields for the ag hdr (inode section)
1018 xfs_trans_t
*tp
, /* transaction pointer */
1019 xfs_buf_t
*bp
, /* allocation group header buffer */
1020 int fields
) /* bitmask of fields to log */
1022 int first
; /* first byte number */
1023 int last
; /* last byte number */
1024 static const short offsets
[] = { /* field starting offsets */
1025 /* keep in sync with bit definitions */
1026 offsetof(xfs_agi_t
, agi_magicnum
),
1027 offsetof(xfs_agi_t
, agi_versionnum
),
1028 offsetof(xfs_agi_t
, agi_seqno
),
1029 offsetof(xfs_agi_t
, agi_length
),
1030 offsetof(xfs_agi_t
, agi_count
),
1031 offsetof(xfs_agi_t
, agi_root
),
1032 offsetof(xfs_agi_t
, agi_level
),
1033 offsetof(xfs_agi_t
, agi_freecount
),
1034 offsetof(xfs_agi_t
, agi_newino
),
1035 offsetof(xfs_agi_t
, agi_dirino
),
1036 offsetof(xfs_agi_t
, agi_unlinked
),
1040 xfs_agi_t
*agi
; /* allocation group header */
1042 agi
= XFS_BUF_TO_AGI(bp
);
1043 ASSERT(INT_GET(agi
->agi_magicnum
, ARCH_CONVERT
) ==
1047 * Compute byte offsets for the first and last fields.
1049 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS
, &first
, &last
);
1051 * Log the allocation group inode header buffer.
1053 xfs_trans_log_buf(tp
, bp
, first
, last
);
1057 * Read in the allocation group header (inode allocation section)
1060 xfs_ialloc_read_agi(
1061 xfs_mount_t
*mp
, /* file system mount structure */
1062 xfs_trans_t
*tp
, /* transaction pointer */
1063 xfs_agnumber_t agno
, /* allocation group number */
1064 xfs_buf_t
**bpp
) /* allocation group hdr buf */
1066 xfs_agi_t
*agi
; /* allocation group header */
1067 int agi_ok
; /* agi is consistent */
1068 xfs_buf_t
*bp
; /* allocation group hdr buf */
1069 xfs_daddr_t d
; /* disk block address */
1074 xfs_perag_t
*pag
; /* per allocation group data */
1077 ASSERT(agno
!= NULLAGNUMBER
);
1078 d
= XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR
);
1079 if ((error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
, d
, 1, 0, &bp
)))
1081 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
1083 * Validate the magic number of the agi block.
1085 agi
= XFS_BUF_TO_AGI(bp
);
1087 INT_GET(agi
->agi_magicnum
, ARCH_CONVERT
) == XFS_AGI_MAGIC
&&
1088 XFS_AGI_GOOD_VERSION(INT_GET(agi
->agi_versionnum
, ARCH_CONVERT
));
1089 if (XFS_TEST_ERROR(!agi_ok
, mp
, XFS_ERRTAG_IALLOC_READ_AGI
,
1090 XFS_RANDOM_IALLOC_READ_AGI
)) {
1091 xfs_trans_brelse(tp
, bp
);
1092 #ifdef __KERNEL__ /* additional, temporary, debugging code */
1094 "EFSCORRUPTED returned from file %s line %d",
1095 __FILE__
, __LINE__
);
1097 return XFS_ERROR(EFSCORRUPTED
);
1099 pag
= &mp
->m_perag
[agno
];
1100 if (!pag
->pagi_init
) {
1101 pag
->pagi_freecount
= INT_GET(agi
->agi_freecount
, ARCH_CONVERT
);
1105 * It's possible for these to be out of sync if
1106 * we are in the middle of a forced shutdown.
1108 ASSERT(pag
->pagi_freecount
== INT_GET(agi
->agi_freecount
, ARCH_CONVERT
)
1109 || XFS_FORCED_SHUTDOWN(mp
));
1112 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++)
1113 ASSERT(INT_GET(agi
->agi_unlinked
[i
], ARCH_CONVERT
) != 0);
1115 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGI
, XFS_AGI_REF
);