blob: 02989691e4abda2655dafa6cf6a992d7b87a5110 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
3 *
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.
7 *
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.
11 *
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.
18 *
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.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#include "xfs.h"
34
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"
61
62/*
63 * Log specified fields for the inode given by bp and off.
64 */
65STATIC void
66xfs_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 */
71{
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)
106 };
107
108
109 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
110 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
111 mp = tp->t_mountp;
112 /*
113 * Get the inode-relative first and last bytes for these fields
114 */
115 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
116 /*
117 * Convert to buffer offsets and log it.
118 */
119 ioffset = off << mp->m_sb.sb_inodelog;
120 first += ioffset;
121 last += ioffset;
122 xfs_trans_log_buf(tp, bp, first, last);
123}
124
125/*
126 * Allocation group level functions.
127 */
128
129/*
130 * Allocate new inodes in the allocation group specified by agbp.
131 * Return 0 for success, else error code.
132 */
133STATIC int /* error code or 0 */
134xfs_ialloc_ag_alloc(
135 xfs_trans_t *tp, /* transaction pointer */
136 xfs_buf_t *agbp, /* alloc group buffer */
137 int *alloc)
138{
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 */
159
160 args.tp = tp;
161 args.mp = tp->t_mountp;
162
163 /*
164 * Locking will ensure that we don't have two callers in here
165 * at one time.
166 */
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);
172 /*
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.
179 */
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);
192 /*
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.
196 */
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);
200 /*
201 * Allocate a fixed-size extent of inodes.
202 */
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;
207 /*
208 * Allow space for the inode btree to split.
209 */
210 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
211 if ((error = xfs_alloc_vextent(&args)))
212 return error;
213
214 /*
215 * If stripe alignment is turned on, then try again with cluster
216 * alignment.
217 */
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;
231 }
232
233 if (args.fsbno == NULLFSBLOCK) {
234 *alloc = 0;
235 return 0;
236 }
237 ASSERT(args.len == args.minlen);
238 /*
239 * Convert the results.
240 */
241 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
242 /*
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.
246 */
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;
256 }
257 /*
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.
263 */
264 if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
265 version = XFS_DINODE_VERSION_2;
266 else
267 version = XFS_DINODE_VERSION_1;
268
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);
272
273 for (j = 0; j < nbufs; j++) {
274 /*
275 * Get the block.
276 */
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));
284 /*
285 * Loop over the inodes in this buffer.
286 */
287
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);
294 }
295 xfs_trans_inode_alloc_buf(tp, fbuf);
296 }
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);
303 /*
304 * Insert records describing the new inode chunk into the btree.
305 */
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;
316 }
317 ASSERT(i == 0);
318 if ((error = xfs_inobt_insert(cur, &i))) {
319 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
320 return error;
321 }
322 ASSERT(i == 1);
323 }
324 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
325 /*
326 * Log allocation group header fields
327 */
328 xfs_ialloc_log_agi(tp, agbp,
329 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
330 /*
331 * Modify/log superblock values for inode count and inode free count.
332 */
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;
337}
338
339STATIC __inline xfs_agnumber_t
340xfs_ialloc_next_ag(
341 xfs_mount_t *mp)
342{
343 xfs_agnumber_t agno;
344
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);
350
351 return agno;
352}
353
354/*
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.
357 */
358STATIC xfs_buf_t * /* allocation group buffer */
359xfs_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 */
364{
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 */
375
376 /*
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).
379 */
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;
389 }
390 ASSERT(pagno < agcount);
391 /*
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.
397 */
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;
407 }
408 } else
409 agbp = NULL;
410
411 if (!pag->pagi_inodeok) {
412 xfs_ialloc_next_ag(mp);
413 goto unlock_nextag;
414 }
415
416 /*
417 * Is there enough free space for the file plus a block
418 * of inodes (if we need to allocate some)?
419 */
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;
426 }
427 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
428 }
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;
440 }
441 up_read(&mp->m_peraglock);
442 return agbp;
443 }
444 }
445unlock_nextag:
446 if (agbp)
447 xfs_trans_brelse(tp, agbp);
448nextag:
449 /*
450 * No point in iterating over the rest, if we're shutting
451 * down.
452 */
453 if (XFS_FORCED_SHUTDOWN(mp)) {
454 up_read(&mp->m_peraglock);
455 return (xfs_buf_t *)0;
456 }
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;
464 }
465 flags = 0;
466 }
467 }
468}
469
470/*
471 * Visible inode allocation functions.
472 */
473
474/*
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.
478 *
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.
494 *
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.
498 */
499int
500xfs_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 */
509{
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 */
530
531
532 if (*IO_agbp == NULL) {
533 /*
534 * We do not have an agbp, so select an initial allocation
535 * group for inode allocation.
536 */
537 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
538 /*
539 * Couldn't find an allocation group satisfying the
540 * criteria, give up.
541 */
542 if (!agbp) {
543 *inop = NULLFSINO;
544 return 0;
545 }
546 agi = XFS_BUF_TO_AGI(agbp);
547 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
548 } else {
549 /*
550 * Continue where we left off before. In this case, we
551 * know that the allocation group has free inodes.
552 */
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);
557 }
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);
564
565 /*
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.
569 */
570
571 if (mp->m_maxicount &&
572 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
573 noroom = 1;
574 okalloc = 0;
575 }
576
577 /*
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.
581 */
582 *alloc_done = B_FALSE;
583 while (!agi->agi_freecount) {
584 /*
585 * Don't do anything if we're not supposed to allocate
586 * any blocks, just go on to the next ag.
587 */
588 if (okalloc) {
589 /*
590 * Try to allocate some new inodes in the allocation
591 * group.
592 */
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;
600 }
601 if (ialloced) {
602 /*
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.
607 */
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;
613 }
614 }
615 /*
616 * If it failed, give up on this ag.
617 */
618 xfs_trans_brelse(tp, agbp);
619 /*
620 * Go on to the next ag: get its ag header.
621 */
622nextag:
623 if (++tagno == agcount)
624 tagno = 0;
625 if (tagno == agno) {
626 *inop = NULLFSINO;
627 return noroom ? ENOSPC : 0;
628 }
629 down_read(&mp->m_peraglock);
630 if (mp->m_perag[tagno].pagi_inodeok == 0) {
631 up_read(&mp->m_peraglock);
632 goto nextag;
633 }
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);
640 }
641 /*
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.
645 */
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);
650 /*
651 * If pagino is 0 (this is the root inode allocation) use newino.
652 * This must work because we've just allocated some.
653 */
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;
659
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);
672
673 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
674 XFS_FORCED_SHUTDOWN(mp));
675 }
676#endif
677 /*
678 * If in the same a.g. as the parent, try to get near the parent.
679 */
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) {
688 /*
689 * Found a free inode in the same chunk
690 * as parent, done.
691 */
692 }
693 /*
694 * In the same a.g. as parent, but parent's chunk is full.
695 */
696 else {
697 int doneleft; /* done, to the left */
698 int doneright; /* done, to the right */
699
700 if (error)
701 goto error0;
702 ASSERT(i == 1);
703 ASSERT(j == 1);
704 /*
705 * Duplicate the cursor, search left & right
706 * simultaneously.
707 */
708 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
709 goto error0;
710 /*
711 * Search left with tcur, back up 1 record.
712 */
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);
723 }
724 /*
725 * Search right with cur, go forward 1 record.
726 */
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);
737 }
738 /*
739 * Loop until we find the closest inode chunk
740 * with a free one.
741 */
742 while (!doneleft || !doneright) {
743 int useleft; /* using left inode
744 chunk this time */
745
746 /*
747 * Figure out which block is closer,
748 * if both are valid.
749 */
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;
758 /*
759 * If checking the left, does it have
760 * free inodes?
761 */
762 if (useleft && trec.ir_freecount) {
763 /*
764 * Yes, set it up as the chunk to use.
765 */
766 rec = trec;
767 xfs_btree_del_cursor(cur,
768 XFS_BTREE_NOERROR);
769 cur = tcur;
770 break;
771 }
772 /*
773 * If checking the right, does it have
774 * free inodes?
775 */
776 if (!useleft && rec.ir_freecount) {
777 /*
778 * Yes, it's already set up.
779 */
780 xfs_btree_del_cursor(tcur,
781 XFS_BTREE_NOERROR);
782 break;
783 }
784 /*
785 * If used the left, get another one
786 * further left.
787 */
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);
802 }
803 }
804 /*
805 * If used the right, get another one
806 * further right.
807 */
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);
822 }
823 }
824 }
825 ASSERT(!doneleft || !doneright);
826 }
827 }
828 /*
829 * In a different a.g. from the parent.
830 * See if the most recently allocated block has any free.
831 */
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) {
841 /*
842 * The last chunk allocated in the group still has
843 * a free inode.
844 */
845 }
846 /*
847 * None left in the last group, search the whole a.g.
848 */
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);
867 }
868 }
869 }
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;
889
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));
903 }
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;
909error1:
910 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
911error0:
912 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
913 return error;
914}
915
916/*
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.
921 */
922int
923xfs_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 */
929{
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 */
943
944 mp = tp->t_mountp;
945
946 /*
947 * Break up inode number into its components.
948 */
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);
956 }
957 agino = XFS_INO_TO_AGINO(mp, inode);
958 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
959 cmn_err(CE_WARN,
Christoph Hellwigda1650a2005-11-02 10:21:35 +1100960 "xfs_difree: inode != XFS_AGINO_TO_INO() "
961 "(%llu != %llu) on %s. Returning EINVAL.",
962 (unsigned long long)inode,
963 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
964 mp->m_fsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 ASSERT(0);
966 return XFS_ERROR(EINVAL);
967 }
968 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
969 if (agbno >= mp->m_sb.sb_agblocks) {
970 cmn_err(CE_WARN,
971 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
972 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
973 ASSERT(0);
974 return XFS_ERROR(EINVAL);
975 }
976 /*
977 * Get the allocation group header.
978 */
979 down_read(&mp->m_peraglock);
980 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
981 up_read(&mp->m_peraglock);
982 if (error) {
983 cmn_err(CE_WARN,
984 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
985 error, mp->m_fsname);
986 return error;
987 }
988 agi = XFS_BUF_TO_AGI(agbp);
989 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
990 ASSERT(agbno < INT_GET(agi->agi_length, ARCH_CONVERT));
991 /*
992 * Initialize the cursor.
993 */
994 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
995 (xfs_inode_t *)0, 0);
996#ifdef DEBUG
997 if (cur->bc_nlevels == 1) {
998 int freecount = 0;
999
1000 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1001 goto error0;
1002 do {
1003 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1004 &rec.ir_freecount, &rec.ir_free, &i)))
1005 goto error0;
1006 if (i) {
1007 freecount += rec.ir_freecount;
1008 if ((error = xfs_inobt_increment(cur, 0, &i)))
1009 goto error0;
1010 }
1011 } while (i == 1);
1012 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1013 XFS_FORCED_SHUTDOWN(mp));
1014 }
1015#endif
1016 /*
1017 * Look for the entry describing this inode.
1018 */
1019 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1020 cmn_err(CE_WARN,
1021 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1022 error, mp->m_fsname);
1023 goto error0;
1024 }
1025 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1026 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1027 &rec.ir_free, &i))) {
1028 cmn_err(CE_WARN,
1029 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1030 error, mp->m_fsname);
1031 goto error0;
1032 }
1033 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1034 /*
1035 * Get the offset in the inode chunk.
1036 */
1037 off = agino - rec.ir_startino;
1038 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1039 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1040 /*
1041 * Mark the inode free & increment the count.
1042 */
1043 XFS_INOBT_SET_FREE(&rec, off);
1044 rec.ir_freecount++;
1045
1046 /*
1047 * When an inode cluster is free, it becomes elgible for removal
1048 */
1049 if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1050 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1051
1052 *delete = 1;
1053 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1054
1055 /*
1056 * Remove the inode cluster from the AGI B+Tree, adjust the
1057 * AGI and Superblock inode counts, and mark the disk space
1058 * to be freed when the transaction is committed.
1059 */
1060 ilen = XFS_IALLOC_INODES(mp);
1061 INT_MOD(agi->agi_count, ARCH_CONVERT, -ilen);
1062 INT_MOD(agi->agi_freecount, ARCH_CONVERT, -(ilen - 1));
1063 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1064 down_read(&mp->m_peraglock);
1065 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1066 up_read(&mp->m_peraglock);
1067 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1068 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1069
1070 if ((error = xfs_inobt_delete(cur, &i))) {
1071 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1072 error, mp->m_fsname);
1073 goto error0;
1074 }
1075
1076 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1077 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1078 XFS_IALLOC_BLOCKS(mp), flist, mp);
1079 } else {
1080 *delete = 0;
1081
1082 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1083 cmn_err(CE_WARN,
1084 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1085 error, mp->m_fsname);
1086 goto error0;
1087 }
1088 /*
1089 * Change the inode free counts and log the ag/sb changes.
1090 */
1091 INT_MOD(agi->agi_freecount, ARCH_CONVERT, 1);
1092 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1093 down_read(&mp->m_peraglock);
1094 mp->m_perag[agno].pagi_freecount++;
1095 up_read(&mp->m_peraglock);
1096 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1097 }
1098
1099#ifdef DEBUG
1100 if (cur->bc_nlevels == 1) {
1101 int freecount = 0;
1102
1103 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1104 goto error0;
1105 do {
1106 if ((error = xfs_inobt_get_rec(cur,
1107 &rec.ir_startino,
1108 &rec.ir_freecount,
1109 &rec.ir_free, &i)))
1110 goto error0;
1111 if (i) {
1112 freecount += rec.ir_freecount;
1113 if ((error = xfs_inobt_increment(cur, 0, &i)))
1114 goto error0;
1115 }
1116 } while (i == 1);
1117 ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1118 XFS_FORCED_SHUTDOWN(mp));
1119 }
1120#endif
1121 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1122 return 0;
1123
1124error0:
1125 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1126 return error;
1127}
1128
1129/*
1130 * Return the location of the inode in bno/off, for mapping it into a buffer.
1131 */
1132/*ARGSUSED*/
1133int
1134xfs_dilocate(
1135 xfs_mount_t *mp, /* file system mount structure */
1136 xfs_trans_t *tp, /* transaction pointer */
1137 xfs_ino_t ino, /* inode to locate */
1138 xfs_fsblock_t *bno, /* output: block containing inode */
1139 int *len, /* output: num blocks in inode cluster */
1140 int *off, /* output: index in block of inode */
1141 uint flags) /* flags concerning inode lookup */
1142{
1143 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1144 xfs_buf_t *agbp; /* agi buffer */
1145 xfs_agino_t agino; /* inode number within alloc group */
1146 xfs_agnumber_t agno; /* allocation group number */
1147 int blks_per_cluster; /* num blocks per inode cluster */
1148 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1149 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1150 __int32_t chunk_cnt; /* count of free inodes in chunk */
1151 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1152 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1153 xfs_btree_cur_t *cur; /* inode btree cursor */
1154 int error; /* error code */
1155 int i; /* temp state */
1156 int offset; /* index of inode in its buffer */
1157 int offset_agbno; /* blks from chunk start to inode */
1158
1159 ASSERT(ino != NULLFSINO);
1160 /*
1161 * Split up the inode number into its parts.
1162 */
1163 agno = XFS_INO_TO_AGNO(mp, ino);
1164 agino = XFS_INO_TO_AGINO(mp, ino);
1165 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1166 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1167 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1168#ifdef DEBUG
1169 if (agno >= mp->m_sb.sb_agcount) {
1170 xfs_fs_cmn_err(CE_ALERT, mp,
1171 "xfs_dilocate: agno (%d) >= "
1172 "mp->m_sb.sb_agcount (%d)",
1173 agno, mp->m_sb.sb_agcount);
1174 }
1175 if (agbno >= mp->m_sb.sb_agblocks) {
1176 xfs_fs_cmn_err(CE_ALERT, mp,
1177 "xfs_dilocate: agbno (0x%llx) >= "
1178 "mp->m_sb.sb_agblocks (0x%lx)",
1179 (unsigned long long) agbno,
1180 (unsigned long) mp->m_sb.sb_agblocks);
1181 }
1182 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1183 xfs_fs_cmn_err(CE_ALERT, mp,
1184 "xfs_dilocate: ino (0x%llx) != "
1185 "XFS_AGINO_TO_INO(mp, agno, agino) "
1186 "(0x%llx)",
1187 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1188 }
1189#endif /* DEBUG */
1190 return XFS_ERROR(EINVAL);
1191 }
1192 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1193 !(flags & XFS_IMAP_LOOKUP)) {
1194 offset = XFS_INO_TO_OFFSET(mp, ino);
1195 ASSERT(offset < mp->m_sb.sb_inopblock);
1196 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1197 *off = offset;
1198 *len = 1;
1199 return 0;
1200 }
1201 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1202 if (*bno != NULLFSBLOCK) {
1203 offset = XFS_INO_TO_OFFSET(mp, ino);
1204 ASSERT(offset < mp->m_sb.sb_inopblock);
1205 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1206 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1207 offset;
1208 *len = blks_per_cluster;
1209 return 0;
1210 }
1211 if (mp->m_inoalign_mask) {
1212 offset_agbno = agbno & mp->m_inoalign_mask;
1213 chunk_agbno = agbno - offset_agbno;
1214 } else {
1215 down_read(&mp->m_peraglock);
1216 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1217 up_read(&mp->m_peraglock);
1218 if (error) {
1219#ifdef DEBUG
1220 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1221 "xfs_ialloc_read_agi() returned "
1222 "error %d, agno %d",
1223 error, agno);
1224#endif /* DEBUG */
1225 return error;
1226 }
1227 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1228 (xfs_inode_t *)0, 0);
1229 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1230#ifdef DEBUG
1231 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1232 "xfs_inobt_lookup_le() failed");
1233#endif /* DEBUG */
1234 goto error0;
1235 }
1236 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1237 &chunk_free, &i))) {
1238#ifdef DEBUG
1239 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1240 "xfs_inobt_get_rec() failed");
1241#endif /* DEBUG */
1242 goto error0;
1243 }
1244 if (i == 0) {
1245#ifdef DEBUG
1246 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1247 "xfs_inobt_get_rec() failed");
1248#endif /* DEBUG */
1249 error = XFS_ERROR(EINVAL);
1250 }
1251 xfs_trans_brelse(tp, agbp);
1252 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1253 if (error)
1254 return error;
1255 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1256 offset_agbno = agbno - chunk_agbno;
1257 }
1258 ASSERT(agbno >= chunk_agbno);
1259 cluster_agbno = chunk_agbno +
1260 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1261 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1262 XFS_INO_TO_OFFSET(mp, ino);
1263 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1264 *off = offset;
1265 *len = blks_per_cluster;
1266 return 0;
1267error0:
1268 xfs_trans_brelse(tp, agbp);
1269 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1270 return error;
1271}
1272
1273/*
1274 * Compute and fill in value of m_in_maxlevels.
1275 */
1276void
1277xfs_ialloc_compute_maxlevels(
1278 xfs_mount_t *mp) /* file system mount structure */
1279{
1280 int level;
1281 uint maxblocks;
1282 uint maxleafents;
1283 int minleafrecs;
1284 int minnoderecs;
1285
1286 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1287 XFS_INODES_PER_CHUNK_LOG;
1288 minleafrecs = mp->m_alloc_mnr[0];
1289 minnoderecs = mp->m_alloc_mnr[1];
1290 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1291 for (level = 1; maxblocks > 1; level++)
1292 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1293 mp->m_in_maxlevels = level;
1294}
1295
1296/*
1297 * Log specified fields for the ag hdr (inode section)
1298 */
1299void
1300xfs_ialloc_log_agi(
1301 xfs_trans_t *tp, /* transaction pointer */
1302 xfs_buf_t *bp, /* allocation group header buffer */
1303 int fields) /* bitmask of fields to log */
1304{
1305 int first; /* first byte number */
1306 int last; /* last byte number */
1307 static const short offsets[] = { /* field starting offsets */
1308 /* keep in sync with bit definitions */
1309 offsetof(xfs_agi_t, agi_magicnum),
1310 offsetof(xfs_agi_t, agi_versionnum),
1311 offsetof(xfs_agi_t, agi_seqno),
1312 offsetof(xfs_agi_t, agi_length),
1313 offsetof(xfs_agi_t, agi_count),
1314 offsetof(xfs_agi_t, agi_root),
1315 offsetof(xfs_agi_t, agi_level),
1316 offsetof(xfs_agi_t, agi_freecount),
1317 offsetof(xfs_agi_t, agi_newino),
1318 offsetof(xfs_agi_t, agi_dirino),
1319 offsetof(xfs_agi_t, agi_unlinked),
1320 sizeof(xfs_agi_t)
1321 };
1322#ifdef DEBUG
1323 xfs_agi_t *agi; /* allocation group header */
1324
1325 agi = XFS_BUF_TO_AGI(bp);
1326 ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
1327#endif
1328 /*
1329 * Compute byte offsets for the first and last fields.
1330 */
1331 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1332 /*
1333 * Log the allocation group inode header buffer.
1334 */
1335 xfs_trans_log_buf(tp, bp, first, last);
1336}
1337
1338/*
1339 * Read in the allocation group header (inode allocation section)
1340 */
1341int
1342xfs_ialloc_read_agi(
1343 xfs_mount_t *mp, /* file system mount structure */
1344 xfs_trans_t *tp, /* transaction pointer */
1345 xfs_agnumber_t agno, /* allocation group number */
1346 xfs_buf_t **bpp) /* allocation group hdr buf */
1347{
1348 xfs_agi_t *agi; /* allocation group header */
1349 int agi_ok; /* agi is consistent */
1350 xfs_buf_t *bp; /* allocation group hdr buf */
1351 xfs_perag_t *pag; /* per allocation group data */
1352 int error;
1353
1354 ASSERT(agno != NULLAGNUMBER);
1355 error = xfs_trans_read_buf(
1356 mp, tp, mp->m_ddev_targp,
1357 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1358 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1359 if (error)
1360 return error;
1361 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1362
1363 /*
1364 * Validate the magic number of the agi block.
1365 */
1366 agi = XFS_BUF_TO_AGI(bp);
1367 agi_ok =
1368 INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC &&
1369 XFS_AGI_GOOD_VERSION(
1370 INT_GET(agi->agi_versionnum, ARCH_CONVERT));
1371 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1372 XFS_RANDOM_IALLOC_READ_AGI))) {
1373 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1374 mp, agi);
1375 xfs_trans_brelse(tp, bp);
1376 return XFS_ERROR(EFSCORRUPTED);
1377 }
1378 pag = &mp->m_perag[agno];
1379 if (!pag->pagi_init) {
1380 pag->pagi_freecount = INT_GET(agi->agi_freecount, ARCH_CONVERT);
1381 pag->pagi_init = 1;
1382 } else {
1383 /*
1384 * It's possible for these to be out of sync if
1385 * we are in the middle of a forced shutdown.
1386 */
1387 ASSERT(pag->pagi_freecount ==
1388 INT_GET(agi->agi_freecount, ARCH_CONVERT)
1389 || XFS_FORCED_SHUTDOWN(mp));
1390 }
1391
1392#ifdef DEBUG
1393 {
1394 int i;
1395
1396 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1397 ASSERT(agi->agi_unlinked[i]);
1398 }
1399#endif
1400
1401 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1402 *bpp = bp;
1403 return 0;
1404}