[XFS] move xfs_bmbt_killroot to common code
[deliverable/linux.git] / fs / xfs / xfs_ialloc.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
1da177e4 30#include "xfs_bmap_btree.h"
a844f451 31#include "xfs_alloc_btree.h"
1da177e4 32#include "xfs_ialloc_btree.h"
1da177e4 33#include "xfs_dir2_sf.h"
a844f451 34#include "xfs_attr_sf.h"
1da177e4
LT
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_btree.h"
38#include "xfs_ialloc.h"
1da177e4 39#include "xfs_alloc.h"
1da177e4
LT
40#include "xfs_rtalloc.h"
41#include "xfs_error.h"
42#include "xfs_bmap.h"
43
44/*
45 * Log specified fields for the inode given by bp and off.
46 */
47STATIC void
48xfs_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 */
53{
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)
88 };
89
90
91 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
92 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
93 mp = tp->t_mountp;
94 /*
95 * Get the inode-relative first and last bytes for these fields
96 */
97 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
98 /*
99 * Convert to buffer offsets and log it.
100 */
101 ioffset = off << mp->m_sb.sb_inodelog;
102 first += ioffset;
103 last += ioffset;
104 xfs_trans_log_buf(tp, bp, first, last);
105}
106
107/*
108 * Allocation group level functions.
109 */
75de2a91
DC
110static inline int
111xfs_ialloc_cluster_alignment(
112 xfs_alloc_arg_t *args)
113{
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;
119}
1da177e4 120
fe033cc8
CH
121/*
122 * Lookup the record equal to ino in the btree given by cur.
123 */
124STATIC int /* error */
125xfs_inobt_lookup_eq(
126 struct xfs_btree_cur *cur, /* btree cursor */
127 xfs_agino_t ino, /* starting inode of chunk */
128 __int32_t fcnt, /* free inode count */
129 xfs_inofree_t free, /* free inode mask */
130 int *stat) /* success/failure */
131{
132 cur->bc_rec.i.ir_startino = ino;
133 cur->bc_rec.i.ir_freecount = fcnt;
134 cur->bc_rec.i.ir_free = free;
135 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
136}
137
138/*
139 * Lookup the first record greater than or equal to ino
140 * in the btree given by cur.
141 */
142int /* error */
143xfs_inobt_lookup_ge(
144 struct xfs_btree_cur *cur, /* btree cursor */
145 xfs_agino_t ino, /* starting inode of chunk */
146 __int32_t fcnt, /* free inode count */
147 xfs_inofree_t free, /* free inode mask */
148 int *stat) /* success/failure */
149{
150 cur->bc_rec.i.ir_startino = ino;
151 cur->bc_rec.i.ir_freecount = fcnt;
152 cur->bc_rec.i.ir_free = free;
153 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
154}
155
156/*
157 * Lookup the first record less than or equal to ino
158 * in the btree given by cur.
159 */
160int /* error */
161xfs_inobt_lookup_le(
162 struct xfs_btree_cur *cur, /* btree cursor */
163 xfs_agino_t ino, /* starting inode of chunk */
164 __int32_t fcnt, /* free inode count */
165 xfs_inofree_t free, /* free inode mask */
166 int *stat) /* success/failure */
167{
168 cur->bc_rec.i.ir_startino = ino;
169 cur->bc_rec.i.ir_freecount = fcnt;
170 cur->bc_rec.i.ir_free = free;
171 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
172}
173
278d0ca1
CH
174/*
175 * Update the record referred to by cur to the value given
176 * by [ino, fcnt, free].
177 * This either works (return 0) or gets an EFSCORRUPTED error.
178 */
179STATIC int /* error */
180xfs_inobt_update(
181 struct xfs_btree_cur *cur, /* btree cursor */
182 xfs_agino_t ino, /* starting inode of chunk */
183 __int32_t fcnt, /* free inode count */
184 xfs_inofree_t free) /* free inode mask */
185{
186 union xfs_btree_rec rec;
187
188 rec.inobt.ir_startino = cpu_to_be32(ino);
189 rec.inobt.ir_freecount = cpu_to_be32(fcnt);
190 rec.inobt.ir_free = cpu_to_be64(free);
191 return xfs_btree_update(cur, &rec);
192}
193
1da177e4
LT
194/*
195 * Allocate new inodes in the allocation group specified by agbp.
196 * Return 0 for success, else error code.
197 */
198STATIC int /* error code or 0 */
199xfs_ialloc_ag_alloc(
200 xfs_trans_t *tp, /* transaction pointer */
201 xfs_buf_t *agbp, /* alloc group buffer */
202 int *alloc)
203{
204 xfs_agi_t *agi; /* allocation group header */
205 xfs_alloc_arg_t args; /* allocation argument structure */
206 int blks_per_cluster; /* fs blocks per inode cluster */
207 xfs_btree_cur_t *cur; /* inode btree cursor */
208 xfs_daddr_t d; /* disk addr of buffer */
92821e2b 209 xfs_agnumber_t agno;
1da177e4
LT
210 int error;
211 xfs_buf_t *fbuf; /* new free inodes' buffer */
212 xfs_dinode_t *free; /* new free inode structure */
213 int i; /* inode counter */
214 int j; /* block counter */
215 int nbufs; /* num bufs of new inodes */
216 xfs_agino_t newino; /* new first inode's number */
217 xfs_agino_t newlen; /* new number of inodes */
218 int ninodes; /* num inodes per buf */
219 xfs_agino_t thisino; /* current inode number, for loop */
220 int version; /* inode version number to use */
3ccb8b5f 221 int isaligned = 0; /* inode allocation at stripe unit */
1da177e4 222 /* boundary */
359346a9 223 unsigned int gen;
1da177e4
LT
224
225 args.tp = tp;
226 args.mp = tp->t_mountp;
227
228 /*
229 * Locking will ensure that we don't have two callers in here
230 * at one time.
231 */
232 newlen = XFS_IALLOC_INODES(args.mp);
233 if (args.mp->m_maxicount &&
234 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
235 return XFS_ERROR(ENOSPC);
236 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
237 /*
3ccb8b5f
GO
238 * First try to allocate inodes contiguous with the last-allocated
239 * chunk of inodes. If the filesystem is striped, this will fill
240 * an entire stripe unit with inodes.
241 */
1da177e4 242 agi = XFS_BUF_TO_AGI(agbp);
3ccb8b5f 243 newino = be32_to_cpu(agi->agi_newino);
019ff2d5
NS
244 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
245 XFS_IALLOC_BLOCKS(args.mp);
246 if (likely(newino != NULLAGINO &&
247 (args.agbno < be32_to_cpu(agi->agi_length)))) {
3ccb8b5f
GO
248 args.fsbno = XFS_AGB_TO_FSB(args.mp,
249 be32_to_cpu(agi->agi_seqno), args.agbno);
250 args.type = XFS_ALLOCTYPE_THIS_BNO;
251 args.mod = args.total = args.wasdel = args.isfl =
252 args.userdata = args.minalignslop = 0;
253 args.prod = 1;
75de2a91 254
3ccb8b5f 255 /*
75de2a91
DC
256 * We need to take into account alignment here to ensure that
257 * we don't modify the free list if we fail to have an exact
258 * block. If we don't have an exact match, and every oher
259 * attempt allocation attempt fails, we'll end up cancelling
260 * a dirty transaction and shutting down.
261 *
262 * For an exact allocation, alignment must be 1,
263 * however we need to take cluster alignment into account when
264 * fixing up the freelist. Use the minalignslop field to
265 * indicate that extra blocks might be required for alignment,
266 * but not to use them in the actual exact allocation.
3ccb8b5f 267 */
75de2a91
DC
268 args.alignment = 1;
269 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
270
271 /* Allow space for the inode btree to split. */
3ccb8b5f
GO
272 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
273 if ((error = xfs_alloc_vextent(&args)))
274 return error;
275 } else
276 args.fsbno = NULLFSBLOCK;
1da177e4 277
3ccb8b5f
GO
278 if (unlikely(args.fsbno == NULLFSBLOCK)) {
279 /*
280 * Set the alignment for the allocation.
281 * If stripe alignment is turned on then align at stripe unit
282 * boundary.
019ff2d5
NS
283 * If the cluster size is smaller than a filesystem block
284 * then we're doing I/O for inodes in filesystem block size
3ccb8b5f
GO
285 * pieces, so don't need alignment anyway.
286 */
287 isaligned = 0;
288 if (args.mp->m_sinoalign) {
289 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
290 args.alignment = args.mp->m_dalign;
291 isaligned = 1;
75de2a91
DC
292 } else
293 args.alignment = xfs_ialloc_cluster_alignment(&args);
3ccb8b5f
GO
294 /*
295 * Need to figure out where to allocate the inode blocks.
296 * Ideally they should be spaced out through the a.g.
297 * For now, just allocate blocks up front.
298 */
299 args.agbno = be32_to_cpu(agi->agi_root);
300 args.fsbno = XFS_AGB_TO_FSB(args.mp,
301 be32_to_cpu(agi->agi_seqno), args.agbno);
302 /*
303 * Allocate a fixed-size extent of inodes.
304 */
305 args.type = XFS_ALLOCTYPE_NEAR_BNO;
306 args.mod = args.total = args.wasdel = args.isfl =
307 args.userdata = args.minalignslop = 0;
308 args.prod = 1;
309 /*
310 * Allow space for the inode btree to split.
311 */
312 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
313 if ((error = xfs_alloc_vextent(&args)))
314 return error;
315 }
019ff2d5 316
1da177e4
LT
317 /*
318 * If stripe alignment is turned on, then try again with cluster
319 * alignment.
320 */
321 if (isaligned && args.fsbno == NULLFSBLOCK) {
322 args.type = XFS_ALLOCTYPE_NEAR_BNO;
16259e7d 323 args.agbno = be32_to_cpu(agi->agi_root);
1da177e4 324 args.fsbno = XFS_AGB_TO_FSB(args.mp,
16259e7d 325 be32_to_cpu(agi->agi_seqno), args.agbno);
75de2a91 326 args.alignment = xfs_ialloc_cluster_alignment(&args);
1da177e4
LT
327 if ((error = xfs_alloc_vextent(&args)))
328 return error;
329 }
330
331 if (args.fsbno == NULLFSBLOCK) {
332 *alloc = 0;
333 return 0;
334 }
335 ASSERT(args.len == args.minlen);
336 /*
337 * Convert the results.
338 */
339 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
340 /*
341 * Loop over the new block(s), filling in the inodes.
342 * For small block sizes, manipulate the inodes in buffers
343 * which are multiples of the blocks size.
344 */
345 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
346 blks_per_cluster = 1;
347 nbufs = (int)args.len;
348 ninodes = args.mp->m_sb.sb_inopblock;
349 } else {
350 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
351 args.mp->m_sb.sb_blocksize;
352 nbufs = (int)args.len / blks_per_cluster;
353 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
354 }
355 /*
356 * Figure out what version number to use in the inodes we create.
357 * If the superblock version has caught up to the one that supports
358 * the new inode format, then use the new inode version. Otherwise
359 * use the old version so that old kernels will continue to be
360 * able to use the file system.
361 */
62118709 362 if (xfs_sb_version_hasnlink(&args.mp->m_sb))
1da177e4
LT
363 version = XFS_DINODE_VERSION_2;
364 else
365 version = XFS_DINODE_VERSION_1;
366
359346a9
DC
367 /*
368 * Seed the new inode cluster with a random generation number. This
369 * prevents short-term reuse of generation numbers if a chunk is
370 * freed and then immediately reallocated. We use random numbers
371 * rather than a linear progression to prevent the next generation
372 * number from being easily guessable.
373 */
374 gen = random32();
1da177e4
LT
375 for (j = 0; j < nbufs; j++) {
376 /*
377 * Get the block.
378 */
16259e7d 379 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
1da177e4
LT
380 args.agbno + (j * blks_per_cluster));
381 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
382 args.mp->m_bsize * blks_per_cluster,
383 XFS_BUF_LOCK);
384 ASSERT(fbuf);
385 ASSERT(!XFS_BUF_GETERROR(fbuf));
386 /*
f30a1211 387 * Set initial values for the inodes in this buffer.
1da177e4 388 */
f30a1211 389 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
1da177e4
LT
390 for (i = 0; i < ninodes; i++) {
391 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
347d1c01
CH
392 free->di_core.di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
393 free->di_core.di_version = version;
359346a9 394 free->di_core.di_gen = cpu_to_be32(gen);
347d1c01 395 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
1da177e4
LT
396 xfs_ialloc_log_di(tp, fbuf, i,
397 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
398 }
399 xfs_trans_inode_alloc_buf(tp, fbuf);
400 }
413d57c9
MS
401 be32_add_cpu(&agi->agi_count, newlen);
402 be32_add_cpu(&agi->agi_freecount, newlen);
92821e2b 403 agno = be32_to_cpu(agi->agi_seqno);
1da177e4 404 down_read(&args.mp->m_peraglock);
92821e2b 405 args.mp->m_perag[agno].pagi_freecount += newlen;
1da177e4 406 up_read(&args.mp->m_peraglock);
16259e7d 407 agi->agi_newino = cpu_to_be32(newino);
1da177e4
LT
408 /*
409 * Insert records describing the new inode chunk into the btree.
410 */
561f7d17 411 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
1da177e4
LT
412 for (thisino = newino;
413 thisino < newino + newlen;
414 thisino += XFS_INODES_PER_CHUNK) {
415 if ((error = xfs_inobt_lookup_eq(cur, thisino,
416 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
417 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
418 return error;
419 }
420 ASSERT(i == 0);
4b22a571 421 if ((error = xfs_btree_insert(cur, &i))) {
1da177e4
LT
422 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
423 return error;
424 }
425 ASSERT(i == 1);
426 }
427 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
428 /*
429 * Log allocation group header fields
430 */
431 xfs_ialloc_log_agi(tp, agbp,
432 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
433 /*
434 * Modify/log superblock values for inode count and inode free count.
435 */
436 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
437 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
438 *alloc = 1;
439 return 0;
440}
441
7989cb8e 442STATIC_INLINE xfs_agnumber_t
1da177e4
LT
443xfs_ialloc_next_ag(
444 xfs_mount_t *mp)
445{
446 xfs_agnumber_t agno;
447
448 spin_lock(&mp->m_agirotor_lock);
449 agno = mp->m_agirotor;
450 if (++mp->m_agirotor == mp->m_maxagi)
451 mp->m_agirotor = 0;
452 spin_unlock(&mp->m_agirotor_lock);
453
454 return agno;
455}
456
457/*
458 * Select an allocation group to look for a free inode in, based on the parent
459 * inode and then mode. Return the allocation group buffer.
460 */
461STATIC xfs_buf_t * /* allocation group buffer */
462xfs_ialloc_ag_select(
463 xfs_trans_t *tp, /* transaction pointer */
464 xfs_ino_t parent, /* parent directory inode number */
465 mode_t mode, /* bits set to indicate file type */
466 int okalloc) /* ok to allocate more space */
467{
468 xfs_buf_t *agbp; /* allocation group header buffer */
469 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
470 xfs_agnumber_t agno; /* current ag number */
471 int flags; /* alloc buffer locking flags */
472 xfs_extlen_t ineed; /* blocks needed for inode allocation */
473 xfs_extlen_t longest = 0; /* longest extent available */
474 xfs_mount_t *mp; /* mount point structure */
475 int needspace; /* file mode implies space allocated */
476 xfs_perag_t *pag; /* per allocation group data */
477 xfs_agnumber_t pagno; /* parent (starting) ag number */
478
479 /*
480 * Files of these types need at least one block if length > 0
481 * (and they won't fit in the inode, but that's hard to figure out).
482 */
483 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
484 mp = tp->t_mountp;
485 agcount = mp->m_maxagi;
486 if (S_ISDIR(mode))
487 pagno = xfs_ialloc_next_ag(mp);
488 else {
489 pagno = XFS_INO_TO_AGNO(mp, parent);
490 if (pagno >= agcount)
491 pagno = 0;
492 }
493 ASSERT(pagno < agcount);
494 /*
495 * Loop through allocation groups, looking for one with a little
496 * free space in it. Note we don't look for free inodes, exactly.
497 * Instead, we include whether there is a need to allocate inodes
498 * to mean that blocks must be allocated for them,
499 * if none are currently free.
500 */
501 agno = pagno;
502 flags = XFS_ALLOC_FLAG_TRYLOCK;
503 down_read(&mp->m_peraglock);
504 for (;;) {
505 pag = &mp->m_perag[agno];
506 if (!pag->pagi_init) {
507 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
508 agbp = NULL;
509 goto nextag;
510 }
511 } else
512 agbp = NULL;
513
514 if (!pag->pagi_inodeok) {
515 xfs_ialloc_next_ag(mp);
516 goto unlock_nextag;
517 }
518
519 /*
520 * Is there enough free space for the file plus a block
521 * of inodes (if we need to allocate some)?
522 */
523 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
524 if (ineed && !pag->pagf_init) {
525 if (agbp == NULL &&
526 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
527 agbp = NULL;
528 goto nextag;
529 }
530 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
531 }
532 if (!ineed || pag->pagf_init) {
533 if (ineed && !(longest = pag->pagf_longest))
534 longest = pag->pagf_flcount > 0;
535 if (!ineed ||
536 (pag->pagf_freeblks >= needspace + ineed &&
537 longest >= ineed &&
538 okalloc)) {
539 if (agbp == NULL &&
540 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
541 agbp = NULL;
542 goto nextag;
543 }
544 up_read(&mp->m_peraglock);
545 return agbp;
546 }
547 }
548unlock_nextag:
549 if (agbp)
550 xfs_trans_brelse(tp, agbp);
551nextag:
552 /*
553 * No point in iterating over the rest, if we're shutting
554 * down.
555 */
556 if (XFS_FORCED_SHUTDOWN(mp)) {
557 up_read(&mp->m_peraglock);
1121b219 558 return NULL;
1da177e4
LT
559 }
560 agno++;
561 if (agno >= agcount)
562 agno = 0;
563 if (agno == pagno) {
564 if (flags == 0) {
565 up_read(&mp->m_peraglock);
1121b219 566 return NULL;
1da177e4
LT
567 }
568 flags = 0;
569 }
570 }
571}
572
573/*
574 * Visible inode allocation functions.
575 */
576
577/*
578 * Allocate an inode on disk.
579 * Mode is used to tell whether the new inode will need space, and whether
580 * it is a directory.
581 *
582 * The arguments IO_agbp and alloc_done are defined to work within
583 * the constraint of one allocation per transaction.
584 * xfs_dialloc() is designed to be called twice if it has to do an
585 * allocation to make more free inodes. On the first call,
586 * IO_agbp should be set to NULL. If an inode is available,
587 * i.e., xfs_dialloc() did not need to do an allocation, an inode
588 * number is returned. In this case, IO_agbp would be set to the
589 * current ag_buf and alloc_done set to false.
590 * If an allocation needed to be done, xfs_dialloc would return
591 * the current ag_buf in IO_agbp and set alloc_done to true.
592 * The caller should then commit the current transaction, allocate a new
593 * transaction, and call xfs_dialloc() again, passing in the previous
594 * value of IO_agbp. IO_agbp should be held across the transactions.
595 * Since the agbp is locked across the two calls, the second call is
596 * guaranteed to have a free inode available.
597 *
598 * Once we successfully pick an inode its number is returned and the
599 * on-disk data structures are updated. The inode itself is not read
600 * in, since doing so would break ordering constraints with xfs_reclaim.
601 */
602int
603xfs_dialloc(
604 xfs_trans_t *tp, /* transaction pointer */
605 xfs_ino_t parent, /* parent inode (directory) */
606 mode_t mode, /* mode bits for new inode */
607 int okalloc, /* ok to allocate more space */
608 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
609 boolean_t *alloc_done, /* true if we needed to replenish
610 inode freelist */
611 xfs_ino_t *inop) /* inode number allocated */
612{
613 xfs_agnumber_t agcount; /* number of allocation groups */
614 xfs_buf_t *agbp; /* allocation group header's buffer */
615 xfs_agnumber_t agno; /* allocation group number */
616 xfs_agi_t *agi; /* allocation group header structure */
617 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
618 int error; /* error return value */
619 int i; /* result code */
620 int ialloced; /* inode allocation status */
621 int noroom = 0; /* no space for inode blk allocation */
622 xfs_ino_t ino; /* fs-relative inode to be returned */
623 /* REFERENCED */
624 int j; /* result code */
625 xfs_mount_t *mp; /* file system mount structure */
626 int offset; /* index of inode in chunk */
627 xfs_agino_t pagino; /* parent's a.g. relative inode # */
628 xfs_agnumber_t pagno; /* parent's allocation group number */
61a25848 629 xfs_inobt_rec_incore_t rec; /* inode allocation record */
1da177e4
LT
630 xfs_agnumber_t tagno; /* testing allocation group number */
631 xfs_btree_cur_t *tcur; /* temp cursor */
61a25848 632 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
1da177e4
LT
633
634
635 if (*IO_agbp == NULL) {
636 /*
637 * We do not have an agbp, so select an initial allocation
638 * group for inode allocation.
639 */
640 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
641 /*
642 * Couldn't find an allocation group satisfying the
643 * criteria, give up.
644 */
645 if (!agbp) {
646 *inop = NULLFSINO;
647 return 0;
648 }
649 agi = XFS_BUF_TO_AGI(agbp);
16259e7d 650 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1da177e4
LT
651 } else {
652 /*
653 * Continue where we left off before. In this case, we
654 * know that the allocation group has free inodes.
655 */
656 agbp = *IO_agbp;
657 agi = XFS_BUF_TO_AGI(agbp);
16259e7d
CH
658 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
659 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
1da177e4
LT
660 }
661 mp = tp->t_mountp;
662 agcount = mp->m_sb.sb_agcount;
16259e7d 663 agno = be32_to_cpu(agi->agi_seqno);
1da177e4
LT
664 tagno = agno;
665 pagno = XFS_INO_TO_AGNO(mp, parent);
666 pagino = XFS_INO_TO_AGINO(mp, parent);
667
668 /*
669 * If we have already hit the ceiling of inode blocks then clear
670 * okalloc so we scan all available agi structures for a free
671 * inode.
672 */
673
674 if (mp->m_maxicount &&
675 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
676 noroom = 1;
677 okalloc = 0;
678 }
679
680 /*
681 * Loop until we find an allocation group that either has free inodes
682 * or in which we can allocate some inodes. Iterate through the
683 * allocation groups upward, wrapping at the end.
684 */
685 *alloc_done = B_FALSE;
686 while (!agi->agi_freecount) {
687 /*
688 * Don't do anything if we're not supposed to allocate
689 * any blocks, just go on to the next ag.
690 */
691 if (okalloc) {
692 /*
693 * Try to allocate some new inodes in the allocation
694 * group.
695 */
696 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
697 xfs_trans_brelse(tp, agbp);
698 if (error == ENOSPC) {
699 *inop = NULLFSINO;
700 return 0;
701 } else
702 return error;
703 }
704 if (ialloced) {
705 /*
706 * We successfully allocated some inodes, return
707 * the current context to the caller so that it
708 * can commit the current transaction and call
709 * us again where we left off.
710 */
16259e7d 711 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
1da177e4
LT
712 *alloc_done = B_TRUE;
713 *IO_agbp = agbp;
714 *inop = NULLFSINO;
715 return 0;
716 }
717 }
718 /*
719 * If it failed, give up on this ag.
720 */
721 xfs_trans_brelse(tp, agbp);
722 /*
723 * Go on to the next ag: get its ag header.
724 */
725nextag:
726 if (++tagno == agcount)
727 tagno = 0;
728 if (tagno == agno) {
729 *inop = NULLFSINO;
730 return noroom ? ENOSPC : 0;
731 }
732 down_read(&mp->m_peraglock);
733 if (mp->m_perag[tagno].pagi_inodeok == 0) {
734 up_read(&mp->m_peraglock);
735 goto nextag;
736 }
737 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
738 up_read(&mp->m_peraglock);
739 if (error)
740 goto nextag;
741 agi = XFS_BUF_TO_AGI(agbp);
16259e7d 742 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1da177e4
LT
743 }
744 /*
745 * Here with an allocation group that has a free inode.
746 * Reset agno since we may have chosen a new ag in the
747 * loop above.
748 */
749 agno = tagno;
750 *IO_agbp = NULL;
561f7d17 751 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
1da177e4
LT
752 /*
753 * If pagino is 0 (this is the root inode allocation) use newino.
754 * This must work because we've just allocated some.
755 */
756 if (!pagino)
16259e7d 757 pagino = be32_to_cpu(agi->agi_newino);
1da177e4
LT
758#ifdef DEBUG
759 if (cur->bc_nlevels == 1) {
760 int freecount = 0;
761
762 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
763 goto error0;
764 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
765 do {
766 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
767 &rec.ir_freecount, &rec.ir_free, &i)))
768 goto error0;
769 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
770 freecount += rec.ir_freecount;
637aa50f 771 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
772 goto error0;
773 } while (i == 1);
774
16259e7d 775 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1da177e4
LT
776 XFS_FORCED_SHUTDOWN(mp));
777 }
778#endif
779 /*
780 * If in the same a.g. as the parent, try to get near the parent.
781 */
782 if (pagno == agno) {
783 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
784 goto error0;
785 if (i != 0 &&
786 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
787 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
788 j == 1 &&
789 rec.ir_freecount > 0) {
790 /*
791 * Found a free inode in the same chunk
792 * as parent, done.
793 */
794 }
795 /*
796 * In the same a.g. as parent, but parent's chunk is full.
797 */
798 else {
799 int doneleft; /* done, to the left */
800 int doneright; /* done, to the right */
801
802 if (error)
803 goto error0;
804 ASSERT(i == 1);
805 ASSERT(j == 1);
806 /*
807 * Duplicate the cursor, search left & right
808 * simultaneously.
809 */
810 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
811 goto error0;
812 /*
813 * Search left with tcur, back up 1 record.
814 */
8df4da4a 815 if ((error = xfs_btree_decrement(tcur, 0, &i)))
1da177e4
LT
816 goto error1;
817 doneleft = !i;
818 if (!doneleft) {
819 if ((error = xfs_inobt_get_rec(tcur,
820 &trec.ir_startino,
821 &trec.ir_freecount,
822 &trec.ir_free, &i)))
823 goto error1;
824 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
825 }
826 /*
827 * Search right with cur, go forward 1 record.
828 */
637aa50f 829 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
830 goto error1;
831 doneright = !i;
832 if (!doneright) {
833 if ((error = xfs_inobt_get_rec(cur,
834 &rec.ir_startino,
835 &rec.ir_freecount,
836 &rec.ir_free, &i)))
837 goto error1;
838 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
839 }
840 /*
841 * Loop until we find the closest inode chunk
842 * with a free one.
843 */
844 while (!doneleft || !doneright) {
845 int useleft; /* using left inode
846 chunk this time */
847
848 /*
849 * Figure out which block is closer,
850 * if both are valid.
851 */
852 if (!doneleft && !doneright)
853 useleft =
854 pagino -
855 (trec.ir_startino +
856 XFS_INODES_PER_CHUNK - 1) <
857 rec.ir_startino - pagino;
858 else
859 useleft = !doneleft;
860 /*
861 * If checking the left, does it have
862 * free inodes?
863 */
864 if (useleft && trec.ir_freecount) {
865 /*
866 * Yes, set it up as the chunk to use.
867 */
868 rec = trec;
869 xfs_btree_del_cursor(cur,
870 XFS_BTREE_NOERROR);
871 cur = tcur;
872 break;
873 }
874 /*
875 * If checking the right, does it have
876 * free inodes?
877 */
878 if (!useleft && rec.ir_freecount) {
879 /*
880 * Yes, it's already set up.
881 */
882 xfs_btree_del_cursor(tcur,
883 XFS_BTREE_NOERROR);
884 break;
885 }
886 /*
887 * If used the left, get another one
888 * further left.
889 */
890 if (useleft) {
8df4da4a 891 if ((error = xfs_btree_decrement(tcur, 0,
1da177e4
LT
892 &i)))
893 goto error1;
894 doneleft = !i;
895 if (!doneleft) {
896 if ((error = xfs_inobt_get_rec(
897 tcur,
898 &trec.ir_startino,
899 &trec.ir_freecount,
900 &trec.ir_free, &i)))
901 goto error1;
902 XFS_WANT_CORRUPTED_GOTO(i == 1,
903 error1);
904 }
905 }
906 /*
907 * If used the right, get another one
908 * further right.
909 */
910 else {
637aa50f 911 if ((error = xfs_btree_increment(cur, 0,
1da177e4
LT
912 &i)))
913 goto error1;
914 doneright = !i;
915 if (!doneright) {
916 if ((error = xfs_inobt_get_rec(
917 cur,
918 &rec.ir_startino,
919 &rec.ir_freecount,
920 &rec.ir_free, &i)))
921 goto error1;
922 XFS_WANT_CORRUPTED_GOTO(i == 1,
923 error1);
924 }
925 }
926 }
927 ASSERT(!doneleft || !doneright);
928 }
929 }
930 /*
931 * In a different a.g. from the parent.
932 * See if the most recently allocated block has any free.
933 */
16259e7d 934 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
1da177e4 935 if ((error = xfs_inobt_lookup_eq(cur,
16259e7d 936 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
1da177e4
LT
937 goto error0;
938 if (i == 1 &&
939 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
940 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
941 j == 1 &&
942 rec.ir_freecount > 0) {
943 /*
944 * The last chunk allocated in the group still has
945 * a free inode.
946 */
947 }
948 /*
949 * None left in the last group, search the whole a.g.
950 */
951 else {
952 if (error)
953 goto error0;
954 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
955 goto error0;
956 ASSERT(i == 1);
957 for (;;) {
958 if ((error = xfs_inobt_get_rec(cur,
959 &rec.ir_startino,
960 &rec.ir_freecount, &rec.ir_free,
961 &i)))
962 goto error0;
963 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
964 if (rec.ir_freecount > 0)
965 break;
637aa50f 966 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
967 goto error0;
968 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
969 }
970 }
971 }
972 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
973 ASSERT(offset >= 0);
974 ASSERT(offset < XFS_INODES_PER_CHUNK);
975 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
976 XFS_INODES_PER_CHUNK) == 0);
977 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
978 XFS_INOBT_CLR_FREE(&rec, offset);
979 rec.ir_freecount--;
980 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
981 rec.ir_free)))
982 goto error0;
413d57c9 983 be32_add_cpu(&agi->agi_freecount, -1);
1da177e4
LT
984 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
985 down_read(&mp->m_peraglock);
986 mp->m_perag[tagno].pagi_freecount--;
987 up_read(&mp->m_peraglock);
988#ifdef DEBUG
989 if (cur->bc_nlevels == 1) {
990 int freecount = 0;
991
992 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
993 goto error0;
994 do {
995 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
996 &rec.ir_freecount, &rec.ir_free, &i)))
997 goto error0;
998 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
999 freecount += rec.ir_freecount;
637aa50f 1000 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
1001 goto error0;
1002 } while (i == 1);
16259e7d 1003 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1da177e4
LT
1004 XFS_FORCED_SHUTDOWN(mp));
1005 }
1006#endif
1007 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1008 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1009 *inop = ino;
1010 return 0;
1011error1:
1012 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1013error0:
1014 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1015 return error;
1016}
1017
1018/*
1019 * Free disk inode. Carefully avoids touching the incore inode, all
1020 * manipulations incore are the caller's responsibility.
1021 * The on-disk inode is not changed by this operation, only the
1022 * btree (free inode mask) is changed.
1023 */
1024int
1025xfs_difree(
1026 xfs_trans_t *tp, /* transaction pointer */
1027 xfs_ino_t inode, /* inode to be freed */
1028 xfs_bmap_free_t *flist, /* extents to free */
1029 int *delete, /* set if inode cluster was deleted */
1030 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1031{
1032 /* REFERENCED */
1033 xfs_agblock_t agbno; /* block number containing inode */
1034 xfs_buf_t *agbp; /* buffer containing allocation group header */
1035 xfs_agino_t agino; /* inode number relative to allocation group */
1036 xfs_agnumber_t agno; /* allocation group number */
1037 xfs_agi_t *agi; /* allocation group header */
1038 xfs_btree_cur_t *cur; /* inode btree cursor */
1039 int error; /* error return value */
1040 int i; /* result code */
1041 int ilen; /* inodes in an inode cluster */
1042 xfs_mount_t *mp; /* mount structure for filesystem */
1043 int off; /* offset of inode in inode chunk */
61a25848 1044 xfs_inobt_rec_incore_t rec; /* btree record */
1da177e4
LT
1045
1046 mp = tp->t_mountp;
1047
1048 /*
1049 * Break up inode number into its components.
1050 */
1051 agno = XFS_INO_TO_AGNO(mp, inode);
1052 if (agno >= mp->m_sb.sb_agcount) {
1053 cmn_err(CE_WARN,
1054 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1055 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1056 ASSERT(0);
1057 return XFS_ERROR(EINVAL);
1058 }
1059 agino = XFS_INO_TO_AGINO(mp, inode);
1060 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1061 cmn_err(CE_WARN,
da1650a5
CH
1062 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1063 "(%llu != %llu) on %s. Returning EINVAL.",
1064 (unsigned long long)inode,
1065 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1066 mp->m_fsname);
1da177e4
LT
1067 ASSERT(0);
1068 return XFS_ERROR(EINVAL);
1069 }
1070 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1071 if (agbno >= mp->m_sb.sb_agblocks) {
1072 cmn_err(CE_WARN,
1073 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1074 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1075 ASSERT(0);
1076 return XFS_ERROR(EINVAL);
1077 }
1078 /*
1079 * Get the allocation group header.
1080 */
1081 down_read(&mp->m_peraglock);
1082 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1083 up_read(&mp->m_peraglock);
1084 if (error) {
1085 cmn_err(CE_WARN,
1086 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1087 error, mp->m_fsname);
1088 return error;
1089 }
1090 agi = XFS_BUF_TO_AGI(agbp);
16259e7d
CH
1091 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1092 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1da177e4
LT
1093 /*
1094 * Initialize the cursor.
1095 */
561f7d17 1096 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1da177e4
LT
1097#ifdef DEBUG
1098 if (cur->bc_nlevels == 1) {
1099 int freecount = 0;
1100
1101 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1102 goto error0;
1103 do {
1104 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1105 &rec.ir_freecount, &rec.ir_free, &i)))
1106 goto error0;
1107 if (i) {
1108 freecount += rec.ir_freecount;
637aa50f 1109 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
1110 goto error0;
1111 }
1112 } while (i == 1);
16259e7d 1113 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1da177e4
LT
1114 XFS_FORCED_SHUTDOWN(mp));
1115 }
1116#endif
1117 /*
1118 * Look for the entry describing this inode.
1119 */
1120 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1121 cmn_err(CE_WARN,
1122 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1123 error, mp->m_fsname);
1124 goto error0;
1125 }
1126 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1127 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1128 &rec.ir_free, &i))) {
1129 cmn_err(CE_WARN,
1130 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1131 error, mp->m_fsname);
1132 goto error0;
1133 }
1134 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1135 /*
1136 * Get the offset in the inode chunk.
1137 */
1138 off = agino - rec.ir_startino;
1139 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1140 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1141 /*
1142 * Mark the inode free & increment the count.
1143 */
1144 XFS_INOBT_SET_FREE(&rec, off);
1145 rec.ir_freecount++;
1146
1147 /*
c41564b5 1148 * When an inode cluster is free, it becomes eligible for removal
1da177e4 1149 */
1bd960ee 1150 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1da177e4
LT
1151 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1152
1153 *delete = 1;
1154 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1155
1156 /*
1157 * Remove the inode cluster from the AGI B+Tree, adjust the
1158 * AGI and Superblock inode counts, and mark the disk space
1159 * to be freed when the transaction is committed.
1160 */
1161 ilen = XFS_IALLOC_INODES(mp);
413d57c9
MS
1162 be32_add_cpu(&agi->agi_count, -ilen);
1163 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1da177e4
LT
1164 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1165 down_read(&mp->m_peraglock);
1166 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1167 up_read(&mp->m_peraglock);
1168 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1169 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1170
1171 if ((error = xfs_inobt_delete(cur, &i))) {
1172 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1173 error, mp->m_fsname);
1174 goto error0;
1175 }
1176
1177 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1178 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1179 XFS_IALLOC_BLOCKS(mp), flist, mp);
1180 } else {
1181 *delete = 0;
1182
1183 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1184 cmn_err(CE_WARN,
1185 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1186 error, mp->m_fsname);
1187 goto error0;
1188 }
1189 /*
1190 * Change the inode free counts and log the ag/sb changes.
1191 */
413d57c9 1192 be32_add_cpu(&agi->agi_freecount, 1);
1da177e4
LT
1193 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1194 down_read(&mp->m_peraglock);
1195 mp->m_perag[agno].pagi_freecount++;
1196 up_read(&mp->m_peraglock);
1197 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1198 }
1199
1200#ifdef DEBUG
1201 if (cur->bc_nlevels == 1) {
1202 int freecount = 0;
1203
1204 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1205 goto error0;
1206 do {
1207 if ((error = xfs_inobt_get_rec(cur,
1208 &rec.ir_startino,
1209 &rec.ir_freecount,
1210 &rec.ir_free, &i)))
1211 goto error0;
1212 if (i) {
1213 freecount += rec.ir_freecount;
637aa50f 1214 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
1215 goto error0;
1216 }
1217 } while (i == 1);
16259e7d 1218 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1da177e4
LT
1219 XFS_FORCED_SHUTDOWN(mp));
1220 }
1221#endif
1222 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1223 return 0;
1224
1225error0:
1226 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1227 return error;
1228}
1229
1230/*
1231 * Return the location of the inode in bno/off, for mapping it into a buffer.
1232 */
1233/*ARGSUSED*/
1234int
1235xfs_dilocate(
1236 xfs_mount_t *mp, /* file system mount structure */
1237 xfs_trans_t *tp, /* transaction pointer */
1238 xfs_ino_t ino, /* inode to locate */
1239 xfs_fsblock_t *bno, /* output: block containing inode */
1240 int *len, /* output: num blocks in inode cluster */
1241 int *off, /* output: index in block of inode */
1242 uint flags) /* flags concerning inode lookup */
1243{
1244 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1245 xfs_buf_t *agbp; /* agi buffer */
1246 xfs_agino_t agino; /* inode number within alloc group */
1247 xfs_agnumber_t agno; /* allocation group number */
1248 int blks_per_cluster; /* num blocks per inode cluster */
1249 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1250 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1251 __int32_t chunk_cnt; /* count of free inodes in chunk */
1252 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1253 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1254 xfs_btree_cur_t *cur; /* inode btree cursor */
1255 int error; /* error code */
1256 int i; /* temp state */
1257 int offset; /* index of inode in its buffer */
1258 int offset_agbno; /* blks from chunk start to inode */
1259
1260 ASSERT(ino != NULLFSINO);
1261 /*
1262 * Split up the inode number into its parts.
1263 */
1264 agno = XFS_INO_TO_AGNO(mp, ino);
1265 agino = XFS_INO_TO_AGINO(mp, ino);
1266 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1267 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1268 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1269#ifdef DEBUG
4d1a2ed3
NS
1270 /* no diagnostics for bulkstat, ino comes from userspace */
1271 if (flags & XFS_IMAP_BULKSTAT)
1272 return XFS_ERROR(EINVAL);
1da177e4
LT
1273 if (agno >= mp->m_sb.sb_agcount) {
1274 xfs_fs_cmn_err(CE_ALERT, mp,
1275 "xfs_dilocate: agno (%d) >= "
1276 "mp->m_sb.sb_agcount (%d)",
1277 agno, mp->m_sb.sb_agcount);
1278 }
1279 if (agbno >= mp->m_sb.sb_agblocks) {
1280 xfs_fs_cmn_err(CE_ALERT, mp,
1281 "xfs_dilocate: agbno (0x%llx) >= "
1282 "mp->m_sb.sb_agblocks (0x%lx)",
1283 (unsigned long long) agbno,
1284 (unsigned long) mp->m_sb.sb_agblocks);
1285 }
1286 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1287 xfs_fs_cmn_err(CE_ALERT, mp,
1288 "xfs_dilocate: ino (0x%llx) != "
1289 "XFS_AGINO_TO_INO(mp, agno, agino) "
1290 "(0x%llx)",
1291 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1292 }
745b1f47 1293 xfs_stack_trace();
1da177e4
LT
1294#endif /* DEBUG */
1295 return XFS_ERROR(EINVAL);
1296 }
1297 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1298 !(flags & XFS_IMAP_LOOKUP)) {
1299 offset = XFS_INO_TO_OFFSET(mp, ino);
1300 ASSERT(offset < mp->m_sb.sb_inopblock);
1301 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1302 *off = offset;
1303 *len = 1;
1304 return 0;
1305 }
1306 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1307 if (*bno != NULLFSBLOCK) {
1308 offset = XFS_INO_TO_OFFSET(mp, ino);
1309 ASSERT(offset < mp->m_sb.sb_inopblock);
1310 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1311 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1312 offset;
1313 *len = blks_per_cluster;
1314 return 0;
1315 }
1316 if (mp->m_inoalign_mask) {
1317 offset_agbno = agbno & mp->m_inoalign_mask;
1318 chunk_agbno = agbno - offset_agbno;
1319 } else {
1320 down_read(&mp->m_peraglock);
1321 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1322 up_read(&mp->m_peraglock);
1323 if (error) {
1324#ifdef DEBUG
1325 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1326 "xfs_ialloc_read_agi() returned "
1327 "error %d, agno %d",
1328 error, agno);
1329#endif /* DEBUG */
1330 return error;
1331 }
561f7d17 1332 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1da177e4
LT
1333 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1334#ifdef DEBUG
1335 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1336 "xfs_inobt_lookup_le() failed");
1337#endif /* DEBUG */
1338 goto error0;
1339 }
1340 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1341 &chunk_free, &i))) {
1342#ifdef DEBUG
1343 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1344 "xfs_inobt_get_rec() failed");
1345#endif /* DEBUG */
1346 goto error0;
1347 }
1348 if (i == 0) {
1349#ifdef DEBUG
1350 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1351 "xfs_inobt_get_rec() failed");
1352#endif /* DEBUG */
1353 error = XFS_ERROR(EINVAL);
1354 }
1355 xfs_trans_brelse(tp, agbp);
1356 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1357 if (error)
1358 return error;
1359 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1360 offset_agbno = agbno - chunk_agbno;
1361 }
1362 ASSERT(agbno >= chunk_agbno);
1363 cluster_agbno = chunk_agbno +
1364 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1365 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1366 XFS_INO_TO_OFFSET(mp, ino);
1367 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1368 *off = offset;
1369 *len = blks_per_cluster;
1370 return 0;
1371error0:
1372 xfs_trans_brelse(tp, agbp);
1373 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1374 return error;
1375}
1376
1377/*
1378 * Compute and fill in value of m_in_maxlevels.
1379 */
1380void
1381xfs_ialloc_compute_maxlevels(
1382 xfs_mount_t *mp) /* file system mount structure */
1383{
1384 int level;
1385 uint maxblocks;
1386 uint maxleafents;
1387 int minleafrecs;
1388 int minnoderecs;
1389
1390 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1391 XFS_INODES_PER_CHUNK_LOG;
1392 minleafrecs = mp->m_alloc_mnr[0];
1393 minnoderecs = mp->m_alloc_mnr[1];
1394 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1395 for (level = 1; maxblocks > 1; level++)
1396 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1397 mp->m_in_maxlevels = level;
1398}
1399
1400/*
1401 * Log specified fields for the ag hdr (inode section)
1402 */
1403void
1404xfs_ialloc_log_agi(
1405 xfs_trans_t *tp, /* transaction pointer */
1406 xfs_buf_t *bp, /* allocation group header buffer */
1407 int fields) /* bitmask of fields to log */
1408{
1409 int first; /* first byte number */
1410 int last; /* last byte number */
1411 static const short offsets[] = { /* field starting offsets */
1412 /* keep in sync with bit definitions */
1413 offsetof(xfs_agi_t, agi_magicnum),
1414 offsetof(xfs_agi_t, agi_versionnum),
1415 offsetof(xfs_agi_t, agi_seqno),
1416 offsetof(xfs_agi_t, agi_length),
1417 offsetof(xfs_agi_t, agi_count),
1418 offsetof(xfs_agi_t, agi_root),
1419 offsetof(xfs_agi_t, agi_level),
1420 offsetof(xfs_agi_t, agi_freecount),
1421 offsetof(xfs_agi_t, agi_newino),
1422 offsetof(xfs_agi_t, agi_dirino),
1423 offsetof(xfs_agi_t, agi_unlinked),
1424 sizeof(xfs_agi_t)
1425 };
1426#ifdef DEBUG
1427 xfs_agi_t *agi; /* allocation group header */
1428
1429 agi = XFS_BUF_TO_AGI(bp);
16259e7d 1430 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1da177e4
LT
1431#endif
1432 /*
1433 * Compute byte offsets for the first and last fields.
1434 */
1435 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1436 /*
1437 * Log the allocation group inode header buffer.
1438 */
1439 xfs_trans_log_buf(tp, bp, first, last);
1440}
1441
1442/*
1443 * Read in the allocation group header (inode allocation section)
1444 */
1445int
1446xfs_ialloc_read_agi(
1447 xfs_mount_t *mp, /* file system mount structure */
1448 xfs_trans_t *tp, /* transaction pointer */
1449 xfs_agnumber_t agno, /* allocation group number */
1450 xfs_buf_t **bpp) /* allocation group hdr buf */
1451{
1452 xfs_agi_t *agi; /* allocation group header */
1453 int agi_ok; /* agi is consistent */
1454 xfs_buf_t *bp; /* allocation group hdr buf */
1455 xfs_perag_t *pag; /* per allocation group data */
1456 int error;
1457
1458 ASSERT(agno != NULLAGNUMBER);
1459 error = xfs_trans_read_buf(
1460 mp, tp, mp->m_ddev_targp,
1461 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1462 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1463 if (error)
1464 return error;
1465 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1466
1467 /*
1468 * Validate the magic number of the agi block.
1469 */
1470 agi = XFS_BUF_TO_AGI(bp);
1471 agi_ok =
16259e7d
CH
1472 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1473 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1da177e4
LT
1474 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1475 XFS_RANDOM_IALLOC_READ_AGI))) {
1476 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1477 mp, agi);
1478 xfs_trans_brelse(tp, bp);
1479 return XFS_ERROR(EFSCORRUPTED);
1480 }
1481 pag = &mp->m_perag[agno];
1482 if (!pag->pagi_init) {
16259e7d 1483 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
92821e2b 1484 pag->pagi_count = be32_to_cpu(agi->agi_count);
1da177e4
LT
1485 pag->pagi_init = 1;
1486 } else {
1487 /*
1488 * It's possible for these to be out of sync if
1489 * we are in the middle of a forced shutdown.
1490 */
16259e7d
CH
1491 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1492 XFS_FORCED_SHUTDOWN(mp));
1da177e4
LT
1493 }
1494
1495#ifdef DEBUG
1496 {
1497 int i;
1498
1499 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1500 ASSERT(agi->agi_unlinked[i]);
1501 }
1502#endif
1503
1504 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1505 *bpp = bp;
1506 return 0;
1507}
92821e2b
DC
1508
1509/*
1510 * Read in the agi to initialise the per-ag data in the mount structure
1511 */
1512int
1513xfs_ialloc_pagi_init(
1514 xfs_mount_t *mp, /* file system mount structure */
1515 xfs_trans_t *tp, /* transaction pointer */
1516 xfs_agnumber_t agno) /* allocation group number */
1517{
1518 xfs_buf_t *bp = NULL;
1519 int error;
1520
1521 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1522 if (error)
1523 return error;
1524 if (bp)
1525 xfs_trans_brelse(tp, bp);
1526 return 0;
1527}
This page took 0.411199 seconds and 5 git commands to generate.