]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xfs/xfs_ialloc.c
xfs: Inode create transaction reservations
[people/ms/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 27#include "xfs_mount.h"
1da177e4 28#include "xfs_bmap_btree.h"
a844f451 29#include "xfs_alloc_btree.h"
1da177e4 30#include "xfs_ialloc_btree.h"
1da177e4
LT
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
a844f451
NS
33#include "xfs_btree.h"
34#include "xfs_ialloc.h"
1da177e4 35#include "xfs_alloc.h"
1da177e4
LT
36#include "xfs_rtalloc.h"
37#include "xfs_error.h"
38#include "xfs_bmap.h"
983d09ff
DC
39#include "xfs_cksum.h"
40#include "xfs_buf_item.h"
1da177e4 41
1da177e4
LT
42
43/*
44 * Allocation group level functions.
45 */
75de2a91
DC
46static inline int
47xfs_ialloc_cluster_alignment(
48 xfs_alloc_arg_t *args)
49{
50 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
51 args->mp->m_sb.sb_inoalignmt >=
52 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
53 return args->mp->m_sb.sb_inoalignmt;
54 return 1;
55}
1da177e4 56
fe033cc8 57/*
21875505 58 * Lookup a record by ino in the btree given by cur.
fe033cc8 59 */
81e25176 60int /* error */
21875505 61xfs_inobt_lookup(
fe033cc8
CH
62 struct xfs_btree_cur *cur, /* btree cursor */
63 xfs_agino_t ino, /* starting inode of chunk */
21875505 64 xfs_lookup_t dir, /* <=, >=, == */
fe033cc8
CH
65 int *stat) /* success/failure */
66{
67 cur->bc_rec.i.ir_startino = ino;
21875505
CH
68 cur->bc_rec.i.ir_freecount = 0;
69 cur->bc_rec.i.ir_free = 0;
70 return xfs_btree_lookup(cur, dir, stat);
fe033cc8
CH
71}
72
278d0ca1 73/*
afabc24a 74 * Update the record referred to by cur to the value given.
278d0ca1
CH
75 * This either works (return 0) or gets an EFSCORRUPTED error.
76 */
77STATIC int /* error */
78xfs_inobt_update(
79 struct xfs_btree_cur *cur, /* btree cursor */
afabc24a 80 xfs_inobt_rec_incore_t *irec) /* btree record */
278d0ca1
CH
81{
82 union xfs_btree_rec rec;
83
afabc24a
CH
84 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
85 rec.inobt.ir_freecount = cpu_to_be32(irec->ir_freecount);
86 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
278d0ca1
CH
87 return xfs_btree_update(cur, &rec);
88}
89
8cc938fe
CH
90/*
91 * Get the data from the pointed-to record.
92 */
93int /* error */
94xfs_inobt_get_rec(
95 struct xfs_btree_cur *cur, /* btree cursor */
2e287a73 96 xfs_inobt_rec_incore_t *irec, /* btree record */
8cc938fe
CH
97 int *stat) /* output: success/failure */
98{
99 union xfs_btree_rec *rec;
100 int error;
101
102 error = xfs_btree_get_rec(cur, &rec, stat);
103 if (!error && *stat == 1) {
2e287a73
CH
104 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
105 irec->ir_freecount = be32_to_cpu(rec->inobt.ir_freecount);
106 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
8cc938fe
CH
107 }
108 return error;
109}
110
0b48db80
DC
111/*
112 * Verify that the number of free inodes in the AGI is correct.
113 */
114#ifdef DEBUG
115STATIC int
116xfs_check_agi_freecount(
117 struct xfs_btree_cur *cur,
118 struct xfs_agi *agi)
119{
120 if (cur->bc_nlevels == 1) {
121 xfs_inobt_rec_incore_t rec;
122 int freecount = 0;
123 int error;
124 int i;
125
21875505 126 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
0b48db80
DC
127 if (error)
128 return error;
129
130 do {
131 error = xfs_inobt_get_rec(cur, &rec, &i);
132 if (error)
133 return error;
134
135 if (i) {
136 freecount += rec.ir_freecount;
137 error = xfs_btree_increment(cur, 0, &i);
138 if (error)
139 return error;
140 }
141 } while (i == 1);
142
143 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
144 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
145 }
146 return 0;
147}
148#else
149#define xfs_check_agi_freecount(cur, agi) 0
150#endif
151
85c0b2ab
DC
152/*
153 * Initialise a new set of inodes.
154 */
2a30f36d 155STATIC int
85c0b2ab
DC
156xfs_ialloc_inode_init(
157 struct xfs_mount *mp,
158 struct xfs_trans *tp,
159 xfs_agnumber_t agno,
160 xfs_agblock_t agbno,
161 xfs_agblock_t length,
162 unsigned int gen)
163{
164 struct xfs_buf *fbuf;
165 struct xfs_dinode *free;
166 int blks_per_cluster, nbufs, ninodes;
167 int version;
168 int i, j;
169 xfs_daddr_t d;
93848a99 170 xfs_ino_t ino = 0;
85c0b2ab
DC
171
172 /*
173 * Loop over the new block(s), filling in the inodes.
174 * For small block sizes, manipulate the inodes in buffers
175 * which are multiples of the blocks size.
176 */
177 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
178 blks_per_cluster = 1;
179 nbufs = length;
180 ninodes = mp->m_sb.sb_inopblock;
181 } else {
182 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
183 mp->m_sb.sb_blocksize;
184 nbufs = length / blks_per_cluster;
185 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
186 }
187
188 /*
93848a99
CH
189 * Figure out what version number to use in the inodes we create. If
190 * the superblock version has caught up to the one that supports the new
191 * inode format, then use the new inode version. Otherwise use the old
192 * version so that old kernels will continue to be able to use the file
193 * system.
194 *
195 * For v3 inodes, we also need to write the inode number into the inode,
196 * so calculate the first inode number of the chunk here as
197 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
198 * across multiple filesystem blocks (such as a cluster) and so cannot
199 * be used in the cluster buffer loop below.
200 *
201 * Further, because we are writing the inode directly into the buffer
202 * and calculating a CRC on the entire inode, we have ot log the entire
203 * inode so that the entire range the CRC covers is present in the log.
204 * That means for v3 inode we log the entire buffer rather than just the
205 * inode cores.
85c0b2ab 206 */
93848a99
CH
207 if (xfs_sb_version_hascrc(&mp->m_sb)) {
208 version = 3;
209 ino = XFS_AGINO_TO_INO(mp, agno,
210 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
211 } else if (xfs_sb_version_hasnlink(&mp->m_sb))
85c0b2ab
DC
212 version = 2;
213 else
214 version = 1;
215
216 for (j = 0; j < nbufs; j++) {
217 /*
218 * Get the block.
219 */
220 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
221 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
7c4cebe8
DC
222 mp->m_bsize * blks_per_cluster,
223 XBF_UNMAPPED);
2a30f36d
CS
224 if (!fbuf)
225 return ENOMEM;
85c0b2ab
DC
226 /*
227 * Initialize all inodes in this buffer and then log them.
228 *
229 * XXX: It would be much better if we had just one transaction
230 * to log a whole cluster of inodes instead of all the
231 * individual transactions causing a lot of log traffic.
232 */
1813dd64 233 fbuf->b_ops = &xfs_inode_buf_ops;
93848a99 234 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
85c0b2ab
DC
235 for (i = 0; i < ninodes; i++) {
236 int ioffset = i << mp->m_sb.sb_inodelog;
93848a99 237 uint isize = xfs_dinode_size(version);
85c0b2ab
DC
238
239 free = xfs_make_iptr(mp, fbuf, i);
240 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
241 free->di_version = version;
242 free->di_gen = cpu_to_be32(gen);
243 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
93848a99
CH
244
245 if (version == 3) {
246 free->di_ino = cpu_to_be64(ino);
247 ino++;
248 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
249 xfs_dinode_calc_crc(mp, free);
250 } else {
251 /* just log the inode core */
252 xfs_trans_log_buf(tp, fbuf, ioffset,
253 ioffset + isize - 1);
254 }
255 }
256 if (version == 3) {
257 /* need to log the entire buffer */
258 xfs_trans_log_buf(tp, fbuf, 0,
259 BBTOB(fbuf->b_length) - 1);
85c0b2ab
DC
260 }
261 xfs_trans_inode_alloc_buf(tp, fbuf);
262 }
2a30f36d 263 return 0;
85c0b2ab
DC
264}
265
1da177e4
LT
266/*
267 * Allocate new inodes in the allocation group specified by agbp.
268 * Return 0 for success, else error code.
269 */
270STATIC int /* error code or 0 */
271xfs_ialloc_ag_alloc(
272 xfs_trans_t *tp, /* transaction pointer */
273 xfs_buf_t *agbp, /* alloc group buffer */
274 int *alloc)
275{
276 xfs_agi_t *agi; /* allocation group header */
277 xfs_alloc_arg_t args; /* allocation argument structure */
1da177e4 278 xfs_btree_cur_t *cur; /* inode btree cursor */
92821e2b 279 xfs_agnumber_t agno;
1da177e4 280 int error;
85c0b2ab 281 int i;
1da177e4
LT
282 xfs_agino_t newino; /* new first inode's number */
283 xfs_agino_t newlen; /* new number of inodes */
1da177e4 284 xfs_agino_t thisino; /* current inode number, for loop */
3ccb8b5f 285 int isaligned = 0; /* inode allocation at stripe unit */
1da177e4 286 /* boundary */
44b56e0a 287 struct xfs_perag *pag;
1da177e4 288
a0041684 289 memset(&args, 0, sizeof(args));
1da177e4
LT
290 args.tp = tp;
291 args.mp = tp->t_mountp;
292
293 /*
294 * Locking will ensure that we don't have two callers in here
295 * at one time.
296 */
297 newlen = XFS_IALLOC_INODES(args.mp);
298 if (args.mp->m_maxicount &&
299 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
300 return XFS_ERROR(ENOSPC);
301 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
302 /*
3ccb8b5f
GO
303 * First try to allocate inodes contiguous with the last-allocated
304 * chunk of inodes. If the filesystem is striped, this will fill
305 * an entire stripe unit with inodes.
306 */
1da177e4 307 agi = XFS_BUF_TO_AGI(agbp);
3ccb8b5f 308 newino = be32_to_cpu(agi->agi_newino);
85c0b2ab 309 agno = be32_to_cpu(agi->agi_seqno);
019ff2d5
NS
310 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
311 XFS_IALLOC_BLOCKS(args.mp);
312 if (likely(newino != NULLAGINO &&
313 (args.agbno < be32_to_cpu(agi->agi_length)))) {
85c0b2ab 314 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
3ccb8b5f 315 args.type = XFS_ALLOCTYPE_THIS_BNO;
3ccb8b5f 316 args.prod = 1;
75de2a91 317
3ccb8b5f 318 /*
75de2a91
DC
319 * We need to take into account alignment here to ensure that
320 * we don't modify the free list if we fail to have an exact
321 * block. If we don't have an exact match, and every oher
322 * attempt allocation attempt fails, we'll end up cancelling
323 * a dirty transaction and shutting down.
324 *
325 * For an exact allocation, alignment must be 1,
326 * however we need to take cluster alignment into account when
327 * fixing up the freelist. Use the minalignslop field to
328 * indicate that extra blocks might be required for alignment,
329 * but not to use them in the actual exact allocation.
3ccb8b5f 330 */
75de2a91
DC
331 args.alignment = 1;
332 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
333
334 /* Allow space for the inode btree to split. */
0d87e656 335 args.minleft = args.mp->m_in_maxlevels - 1;
3ccb8b5f
GO
336 if ((error = xfs_alloc_vextent(&args)))
337 return error;
338 } else
339 args.fsbno = NULLFSBLOCK;
1da177e4 340
3ccb8b5f
GO
341 if (unlikely(args.fsbno == NULLFSBLOCK)) {
342 /*
343 * Set the alignment for the allocation.
344 * If stripe alignment is turned on then align at stripe unit
345 * boundary.
019ff2d5
NS
346 * If the cluster size is smaller than a filesystem block
347 * then we're doing I/O for inodes in filesystem block size
3ccb8b5f
GO
348 * pieces, so don't need alignment anyway.
349 */
350 isaligned = 0;
351 if (args.mp->m_sinoalign) {
352 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
353 args.alignment = args.mp->m_dalign;
354 isaligned = 1;
75de2a91
DC
355 } else
356 args.alignment = xfs_ialloc_cluster_alignment(&args);
3ccb8b5f
GO
357 /*
358 * Need to figure out where to allocate the inode blocks.
359 * Ideally they should be spaced out through the a.g.
360 * For now, just allocate blocks up front.
361 */
362 args.agbno = be32_to_cpu(agi->agi_root);
85c0b2ab 363 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
3ccb8b5f
GO
364 /*
365 * Allocate a fixed-size extent of inodes.
366 */
367 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3ccb8b5f
GO
368 args.prod = 1;
369 /*
370 * Allow space for the inode btree to split.
371 */
0d87e656 372 args.minleft = args.mp->m_in_maxlevels - 1;
3ccb8b5f
GO
373 if ((error = xfs_alloc_vextent(&args)))
374 return error;
375 }
019ff2d5 376
1da177e4
LT
377 /*
378 * If stripe alignment is turned on, then try again with cluster
379 * alignment.
380 */
381 if (isaligned && args.fsbno == NULLFSBLOCK) {
382 args.type = XFS_ALLOCTYPE_NEAR_BNO;
16259e7d 383 args.agbno = be32_to_cpu(agi->agi_root);
85c0b2ab 384 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
75de2a91 385 args.alignment = xfs_ialloc_cluster_alignment(&args);
1da177e4
LT
386 if ((error = xfs_alloc_vextent(&args)))
387 return error;
388 }
389
390 if (args.fsbno == NULLFSBLOCK) {
391 *alloc = 0;
392 return 0;
393 }
394 ASSERT(args.len == args.minlen);
1da177e4 395
359346a9 396 /*
85c0b2ab
DC
397 * Stamp and write the inode buffers.
398 *
359346a9
DC
399 * Seed the new inode cluster with a random generation number. This
400 * prevents short-term reuse of generation numbers if a chunk is
401 * freed and then immediately reallocated. We use random numbers
402 * rather than a linear progression to prevent the next generation
403 * number from being easily guessable.
404 */
2a30f36d 405 error = xfs_ialloc_inode_init(args.mp, tp, agno, args.agbno,
ecb3403d 406 args.len, prandom_u32());
d42f08f6 407
2a30f36d
CS
408 if (error)
409 return error;
85c0b2ab
DC
410 /*
411 * Convert the results.
412 */
413 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
413d57c9
MS
414 be32_add_cpu(&agi->agi_count, newlen);
415 be32_add_cpu(&agi->agi_freecount, newlen);
44b56e0a
DC
416 pag = xfs_perag_get(args.mp, agno);
417 pag->pagi_freecount += newlen;
418 xfs_perag_put(pag);
16259e7d 419 agi->agi_newino = cpu_to_be32(newino);
85c0b2ab 420
1da177e4
LT
421 /*
422 * Insert records describing the new inode chunk into the btree.
423 */
561f7d17 424 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
1da177e4
LT
425 for (thisino = newino;
426 thisino < newino + newlen;
427 thisino += XFS_INODES_PER_CHUNK) {
21875505
CH
428 cur->bc_rec.i.ir_startino = thisino;
429 cur->bc_rec.i.ir_freecount = XFS_INODES_PER_CHUNK;
430 cur->bc_rec.i.ir_free = XFS_INOBT_ALL_FREE;
431 error = xfs_btree_lookup(cur, XFS_LOOKUP_EQ, &i);
432 if (error) {
1da177e4
LT
433 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
434 return error;
435 }
436 ASSERT(i == 0);
21875505
CH
437 error = xfs_btree_insert(cur, &i);
438 if (error) {
1da177e4
LT
439 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
440 return error;
441 }
442 ASSERT(i == 1);
443 }
444 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
445 /*
446 * Log allocation group header fields
447 */
448 xfs_ialloc_log_agi(tp, agbp,
449 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
450 /*
451 * Modify/log superblock values for inode count and inode free count.
452 */
453 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
454 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
455 *alloc = 1;
456 return 0;
457}
458
b8f82a4a 459STATIC xfs_agnumber_t
1da177e4
LT
460xfs_ialloc_next_ag(
461 xfs_mount_t *mp)
462{
463 xfs_agnumber_t agno;
464
465 spin_lock(&mp->m_agirotor_lock);
466 agno = mp->m_agirotor;
8aea3ff4 467 if (++mp->m_agirotor >= mp->m_maxagi)
1da177e4
LT
468 mp->m_agirotor = 0;
469 spin_unlock(&mp->m_agirotor_lock);
470
471 return agno;
472}
473
474/*
475 * Select an allocation group to look for a free inode in, based on the parent
476 * inode and then mode. Return the allocation group buffer.
477 */
55d6af64 478STATIC xfs_agnumber_t
1da177e4
LT
479xfs_ialloc_ag_select(
480 xfs_trans_t *tp, /* transaction pointer */
481 xfs_ino_t parent, /* parent directory inode number */
576b1d67 482 umode_t mode, /* bits set to indicate file type */
1da177e4
LT
483 int okalloc) /* ok to allocate more space */
484{
1da177e4
LT
485 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
486 xfs_agnumber_t agno; /* current ag number */
487 int flags; /* alloc buffer locking flags */
488 xfs_extlen_t ineed; /* blocks needed for inode allocation */
489 xfs_extlen_t longest = 0; /* longest extent available */
490 xfs_mount_t *mp; /* mount point structure */
491 int needspace; /* file mode implies space allocated */
492 xfs_perag_t *pag; /* per allocation group data */
493 xfs_agnumber_t pagno; /* parent (starting) ag number */
55d6af64 494 int error;
1da177e4
LT
495
496 /*
497 * Files of these types need at least one block if length > 0
498 * (and they won't fit in the inode, but that's hard to figure out).
499 */
500 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
501 mp = tp->t_mountp;
502 agcount = mp->m_maxagi;
503 if (S_ISDIR(mode))
504 pagno = xfs_ialloc_next_ag(mp);
505 else {
506 pagno = XFS_INO_TO_AGNO(mp, parent);
507 if (pagno >= agcount)
508 pagno = 0;
509 }
55d6af64 510
1da177e4 511 ASSERT(pagno < agcount);
55d6af64 512
1da177e4
LT
513 /*
514 * Loop through allocation groups, looking for one with a little
515 * free space in it. Note we don't look for free inodes, exactly.
516 * Instead, we include whether there is a need to allocate inodes
517 * to mean that blocks must be allocated for them,
518 * if none are currently free.
519 */
520 agno = pagno;
521 flags = XFS_ALLOC_FLAG_TRYLOCK;
1da177e4 522 for (;;) {
44b56e0a 523 pag = xfs_perag_get(mp, agno);
55d6af64
CH
524 if (!pag->pagi_inodeok) {
525 xfs_ialloc_next_ag(mp);
526 goto nextag;
527 }
528
1da177e4 529 if (!pag->pagi_init) {
55d6af64
CH
530 error = xfs_ialloc_pagi_init(mp, tp, agno);
531 if (error)
1da177e4 532 goto nextag;
55d6af64 533 }
1da177e4 534
55d6af64
CH
535 if (pag->pagi_freecount) {
536 xfs_perag_put(pag);
537 return agno;
1da177e4
LT
538 }
539
55d6af64
CH
540 if (!okalloc)
541 goto nextag;
542
543 if (!pag->pagf_init) {
544 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
545 if (error)
1da177e4 546 goto nextag;
1da177e4 547 }
55d6af64
CH
548
549 /*
550 * Is there enough free space for the file plus a block of
551 * inodes? (if we need to allocate some)?
552 */
553 ineed = XFS_IALLOC_BLOCKS(mp);
554 longest = pag->pagf_longest;
555 if (!longest)
556 longest = pag->pagf_flcount > 0;
557
558 if (pag->pagf_freeblks >= needspace + ineed &&
559 longest >= ineed) {
560 xfs_perag_put(pag);
561 return agno;
1da177e4 562 }
1da177e4 563nextag:
44b56e0a 564 xfs_perag_put(pag);
1da177e4
LT
565 /*
566 * No point in iterating over the rest, if we're shutting
567 * down.
568 */
1c1c6ebc 569 if (XFS_FORCED_SHUTDOWN(mp))
55d6af64 570 return NULLAGNUMBER;
1da177e4
LT
571 agno++;
572 if (agno >= agcount)
573 agno = 0;
574 if (agno == pagno) {
1c1c6ebc 575 if (flags == 0)
55d6af64 576 return NULLAGNUMBER;
1da177e4
LT
577 flags = 0;
578 }
579 }
580}
581
4254b0bb
CH
582/*
583 * Try to retrieve the next record to the left/right from the current one.
584 */
585STATIC int
586xfs_ialloc_next_rec(
587 struct xfs_btree_cur *cur,
588 xfs_inobt_rec_incore_t *rec,
589 int *done,
590 int left)
591{
592 int error;
593 int i;
594
595 if (left)
596 error = xfs_btree_decrement(cur, 0, &i);
597 else
598 error = xfs_btree_increment(cur, 0, &i);
599
600 if (error)
601 return error;
602 *done = !i;
603 if (i) {
604 error = xfs_inobt_get_rec(cur, rec, &i);
605 if (error)
606 return error;
607 XFS_WANT_CORRUPTED_RETURN(i == 1);
608 }
609
610 return 0;
611}
612
bd169565
DC
613STATIC int
614xfs_ialloc_get_rec(
615 struct xfs_btree_cur *cur,
616 xfs_agino_t agino,
617 xfs_inobt_rec_incore_t *rec,
43df2ee6 618 int *done)
bd169565
DC
619{
620 int error;
621 int i;
622
623 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
624 if (error)
625 return error;
626 *done = !i;
627 if (i) {
628 error = xfs_inobt_get_rec(cur, rec, &i);
629 if (error)
630 return error;
631 XFS_WANT_CORRUPTED_RETURN(i == 1);
632 }
633
634 return 0;
635}
0b48db80 636
1da177e4 637/*
f2ecc5e4 638 * Allocate an inode.
1da177e4 639 *
f2ecc5e4
CH
640 * The caller selected an AG for us, and made sure that free inodes are
641 * available.
1da177e4 642 */
f2ecc5e4
CH
643STATIC int
644xfs_dialloc_ag(
645 struct xfs_trans *tp,
646 struct xfs_buf *agbp,
647 xfs_ino_t parent,
648 xfs_ino_t *inop)
1da177e4 649{
f2ecc5e4
CH
650 struct xfs_mount *mp = tp->t_mountp;
651 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
652 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
653 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
654 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
655 struct xfs_perag *pag;
656 struct xfs_btree_cur *cur, *tcur;
657 struct xfs_inobt_rec_incore rec, trec;
658 xfs_ino_t ino;
659 int error;
660 int offset;
661 int i, j;
1da177e4 662
44b56e0a 663 pag = xfs_perag_get(mp, agno);
bd169565 664
4bb61069
CH
665 ASSERT(pag->pagi_init);
666 ASSERT(pag->pagi_inodeok);
667 ASSERT(pag->pagi_freecount > 0);
668
bd169565 669 restart_pagno:
f2ecc5e4 670 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1da177e4
LT
671 /*
672 * If pagino is 0 (this is the root inode allocation) use newino.
673 * This must work because we've just allocated some.
674 */
675 if (!pagino)
16259e7d 676 pagino = be32_to_cpu(agi->agi_newino);
1da177e4 677
0b48db80
DC
678 error = xfs_check_agi_freecount(cur, agi);
679 if (error)
680 goto error0;
1da177e4 681
1da177e4 682 /*
4254b0bb 683 * If in the same AG as the parent, try to get near the parent.
1da177e4
LT
684 */
685 if (pagno == agno) {
4254b0bb
CH
686 int doneleft; /* done, to the left */
687 int doneright; /* done, to the right */
bd169565 688 int searchdistance = 10;
4254b0bb 689
21875505 690 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
4254b0bb 691 if (error)
1da177e4 692 goto error0;
4254b0bb
CH
693 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
694
695 error = xfs_inobt_get_rec(cur, &rec, &j);
696 if (error)
697 goto error0;
698 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
699
700 if (rec.ir_freecount > 0) {
1da177e4
LT
701 /*
702 * Found a free inode in the same chunk
4254b0bb 703 * as the parent, done.
1da177e4 704 */
4254b0bb 705 goto alloc_inode;
1da177e4 706 }
4254b0bb
CH
707
708
1da177e4 709 /*
4254b0bb 710 * In the same AG as parent, but parent's chunk is full.
1da177e4 711 */
1da177e4 712
4254b0bb
CH
713 /* duplicate the cursor, search left & right simultaneously */
714 error = xfs_btree_dup_cursor(cur, &tcur);
715 if (error)
716 goto error0;
717
bd169565
DC
718 /*
719 * Skip to last blocks looked up if same parent inode.
720 */
721 if (pagino != NULLAGINO &&
722 pag->pagl_pagino == pagino &&
723 pag->pagl_leftrec != NULLAGINO &&
724 pag->pagl_rightrec != NULLAGINO) {
725 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
43df2ee6 726 &trec, &doneleft);
bd169565
DC
727 if (error)
728 goto error1;
4254b0bb 729
bd169565 730 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
43df2ee6 731 &rec, &doneright);
bd169565
DC
732 if (error)
733 goto error1;
734 } else {
735 /* search left with tcur, back up 1 record */
736 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
737 if (error)
738 goto error1;
739
740 /* search right with cur, go forward 1 record. */
741 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
742 if (error)
743 goto error1;
744 }
4254b0bb
CH
745
746 /*
747 * Loop until we find an inode chunk with a free inode.
748 */
749 while (!doneleft || !doneright) {
750 int useleft; /* using left inode chunk this time */
751
bd169565
DC
752 if (!--searchdistance) {
753 /*
754 * Not in range - save last search
755 * location and allocate a new inode
756 */
3b826386 757 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
bd169565
DC
758 pag->pagl_leftrec = trec.ir_startino;
759 pag->pagl_rightrec = rec.ir_startino;
760 pag->pagl_pagino = pagino;
761 goto newino;
762 }
763
4254b0bb
CH
764 /* figure out the closer block if both are valid. */
765 if (!doneleft && !doneright) {
766 useleft = pagino -
767 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
768 rec.ir_startino - pagino;
769 } else {
770 useleft = !doneleft;
1da177e4 771 }
4254b0bb
CH
772
773 /* free inodes to the left? */
774 if (useleft && trec.ir_freecount) {
775 rec = trec;
776 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
777 cur = tcur;
bd169565
DC
778
779 pag->pagl_leftrec = trec.ir_startino;
780 pag->pagl_rightrec = rec.ir_startino;
781 pag->pagl_pagino = pagino;
4254b0bb 782 goto alloc_inode;
1da177e4 783 }
1da177e4 784
4254b0bb
CH
785 /* free inodes to the right? */
786 if (!useleft && rec.ir_freecount) {
787 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
bd169565
DC
788
789 pag->pagl_leftrec = trec.ir_startino;
790 pag->pagl_rightrec = rec.ir_startino;
791 pag->pagl_pagino = pagino;
4254b0bb 792 goto alloc_inode;
1da177e4 793 }
4254b0bb
CH
794
795 /* get next record to check */
796 if (useleft) {
797 error = xfs_ialloc_next_rec(tcur, &trec,
798 &doneleft, 1);
799 } else {
800 error = xfs_ialloc_next_rec(cur, &rec,
801 &doneright, 0);
802 }
803 if (error)
804 goto error1;
1da177e4 805 }
bd169565
DC
806
807 /*
808 * We've reached the end of the btree. because
809 * we are only searching a small chunk of the
810 * btree each search, there is obviously free
811 * inodes closer to the parent inode than we
812 * are now. restart the search again.
813 */
814 pag->pagl_pagino = NULLAGINO;
815 pag->pagl_leftrec = NULLAGINO;
816 pag->pagl_rightrec = NULLAGINO;
817 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
818 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
819 goto restart_pagno;
1da177e4 820 }
4254b0bb 821
1da177e4 822 /*
4254b0bb 823 * In a different AG from the parent.
1da177e4
LT
824 * See if the most recently allocated block has any free.
825 */
bd169565 826newino:
69ef921b 827 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
21875505
CH
828 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
829 XFS_LOOKUP_EQ, &i);
4254b0bb 830 if (error)
1da177e4 831 goto error0;
4254b0bb
CH
832
833 if (i == 1) {
834 error = xfs_inobt_get_rec(cur, &rec, &j);
835 if (error)
836 goto error0;
837
838 if (j == 1 && rec.ir_freecount > 0) {
839 /*
840 * The last chunk allocated in the group
841 * still has a free inode.
842 */
843 goto alloc_inode;
844 }
1da177e4 845 }
bd169565 846 }
4254b0bb 847
bd169565
DC
848 /*
849 * None left in the last group, search the whole AG
850 */
851 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
852 if (error)
853 goto error0;
854 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
855
856 for (;;) {
857 error = xfs_inobt_get_rec(cur, &rec, &i);
858 if (error)
859 goto error0;
860 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
861 if (rec.ir_freecount > 0)
862 break;
863 error = xfs_btree_increment(cur, 0, &i);
4254b0bb
CH
864 if (error)
865 goto error0;
866 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1da177e4 867 }
4254b0bb
CH
868
869alloc_inode:
824c3131 870 offset = xfs_lowbit64(rec.ir_free);
1da177e4
LT
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);
0d87e656 876 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1da177e4 877 rec.ir_freecount--;
afabc24a
CH
878 error = xfs_inobt_update(cur, &rec);
879 if (error)
1da177e4 880 goto error0;
413d57c9 881 be32_add_cpu(&agi->agi_freecount, -1);
1da177e4 882 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
44b56e0a 883 pag->pagi_freecount--;
1da177e4 884
0b48db80
DC
885 error = xfs_check_agi_freecount(cur, agi);
886 if (error)
887 goto error0;
888
1da177e4
LT
889 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
890 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
44b56e0a 891 xfs_perag_put(pag);
1da177e4
LT
892 *inop = ino;
893 return 0;
894error1:
895 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
896error0:
897 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
44b56e0a 898 xfs_perag_put(pag);
1da177e4
LT
899 return error;
900}
901
f2ecc5e4
CH
902/*
903 * Allocate an inode on disk.
904 *
905 * Mode is used to tell whether the new inode will need space, and whether it
906 * is a directory.
907 *
908 * This function is designed to be called twice if it has to do an allocation
909 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
910 * If an inode is available without having to performn an allocation, an inode
cd856db6
CM
911 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
912 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
913 * The caller should then commit the current transaction, allocate a
f2ecc5e4
CH
914 * new transaction, and call xfs_dialloc() again, passing in the previous value
915 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
916 * buffer is locked across the two calls, the second call is guaranteed to have
917 * a free inode available.
918 *
919 * Once we successfully pick an inode its number is returned and the on-disk
920 * data structures are updated. The inode itself is not read in, since doing so
921 * would break ordering constraints with xfs_reclaim.
922 */
923int
924xfs_dialloc(
925 struct xfs_trans *tp,
926 xfs_ino_t parent,
927 umode_t mode,
928 int okalloc,
929 struct xfs_buf **IO_agbp,
f2ecc5e4
CH
930 xfs_ino_t *inop)
931{
55d6af64 932 struct xfs_mount *mp = tp->t_mountp;
f2ecc5e4
CH
933 struct xfs_buf *agbp;
934 xfs_agnumber_t agno;
f2ecc5e4
CH
935 int error;
936 int ialloced;
937 int noroom = 0;
be60fe54 938 xfs_agnumber_t start_agno;
f2ecc5e4
CH
939 struct xfs_perag *pag;
940
4bb61069 941 if (*IO_agbp) {
f2ecc5e4 942 /*
4bb61069
CH
943 * If the caller passes in a pointer to the AGI buffer,
944 * continue where we left off before. In this case, we
f2ecc5e4
CH
945 * know that the allocation group has free inodes.
946 */
947 agbp = *IO_agbp;
4bb61069 948 goto out_alloc;
f2ecc5e4 949 }
4bb61069
CH
950
951 /*
952 * We do not have an agbp, so select an initial allocation
953 * group for inode allocation.
954 */
be60fe54
CH
955 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
956 if (start_agno == NULLAGNUMBER) {
4bb61069
CH
957 *inop = NULLFSINO;
958 return 0;
959 }
55d6af64 960
f2ecc5e4
CH
961 /*
962 * If we have already hit the ceiling of inode blocks then clear
963 * okalloc so we scan all available agi structures for a free
964 * inode.
965 */
f2ecc5e4
CH
966 if (mp->m_maxicount &&
967 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
968 noroom = 1;
969 okalloc = 0;
970 }
971
972 /*
973 * Loop until we find an allocation group that either has free inodes
974 * or in which we can allocate some inodes. Iterate through the
975 * allocation groups upward, wrapping at the end.
976 */
be60fe54
CH
977 agno = start_agno;
978 for (;;) {
979 pag = xfs_perag_get(mp, agno);
980 if (!pag->pagi_inodeok) {
981 xfs_ialloc_next_ag(mp);
982 goto nextag;
983 }
984
985 if (!pag->pagi_init) {
986 error = xfs_ialloc_pagi_init(mp, tp, agno);
987 if (error)
988 goto out_error;
f2ecc5e4 989 }
be60fe54 990
f2ecc5e4 991 /*
be60fe54 992 * Do a first racy fast path check if this AG is usable.
f2ecc5e4 993 */
be60fe54
CH
994 if (!pag->pagi_freecount && !okalloc)
995 goto nextag;
996
c4982110
CH
997 /*
998 * Then read in the AGI buffer and recheck with the AGI buffer
999 * lock held.
1000 */
be60fe54
CH
1001 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1002 if (error)
1003 goto out_error;
1004
be60fe54
CH
1005 if (pag->pagi_freecount) {
1006 xfs_perag_put(pag);
1007 goto out_alloc;
1008 }
1009
c4982110
CH
1010 if (!okalloc)
1011 goto nextag_relse_buffer;
1012
be60fe54
CH
1013
1014 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1015 if (error) {
1016 xfs_trans_brelse(tp, agbp);
1017
1018 if (error != ENOSPC)
1019 goto out_error;
1020
1021 xfs_perag_put(pag);
f2ecc5e4 1022 *inop = NULLFSINO;
be60fe54 1023 return 0;
f2ecc5e4 1024 }
be60fe54
CH
1025
1026 if (ialloced) {
1027 /*
1028 * We successfully allocated some inodes, return
1029 * the current context to the caller so that it
1030 * can commit the current transaction and call
1031 * us again where we left off.
1032 */
1033 ASSERT(pag->pagi_freecount > 0);
f2ecc5e4 1034 xfs_perag_put(pag);
be60fe54
CH
1035
1036 *IO_agbp = agbp;
1037 *inop = NULLFSINO;
1038 return 0;
f2ecc5e4 1039 }
be60fe54 1040
c4982110
CH
1041nextag_relse_buffer:
1042 xfs_trans_brelse(tp, agbp);
be60fe54 1043nextag:
f2ecc5e4 1044 xfs_perag_put(pag);
be60fe54
CH
1045 if (++agno == mp->m_sb.sb_agcount)
1046 agno = 0;
1047 if (agno == start_agno) {
1048 *inop = NULLFSINO;
1049 return noroom ? ENOSPC : 0;
1050 }
f2ecc5e4
CH
1051 }
1052
4bb61069 1053out_alloc:
f2ecc5e4
CH
1054 *IO_agbp = NULL;
1055 return xfs_dialloc_ag(tp, agbp, parent, inop);
be60fe54
CH
1056out_error:
1057 xfs_perag_put(pag);
1058 return XFS_ERROR(error);
f2ecc5e4
CH
1059}
1060
1da177e4
LT
1061/*
1062 * Free disk inode. Carefully avoids touching the incore inode, all
1063 * manipulations incore are the caller's responsibility.
1064 * The on-disk inode is not changed by this operation, only the
1065 * btree (free inode mask) is changed.
1066 */
1067int
1068xfs_difree(
1069 xfs_trans_t *tp, /* transaction pointer */
1070 xfs_ino_t inode, /* inode to be freed */
1071 xfs_bmap_free_t *flist, /* extents to free */
1072 int *delete, /* set if inode cluster was deleted */
1073 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1074{
1075 /* REFERENCED */
1076 xfs_agblock_t agbno; /* block number containing inode */
1077 xfs_buf_t *agbp; /* buffer containing allocation group header */
1078 xfs_agino_t agino; /* inode number relative to allocation group */
1079 xfs_agnumber_t agno; /* allocation group number */
1080 xfs_agi_t *agi; /* allocation group header */
1081 xfs_btree_cur_t *cur; /* inode btree cursor */
1082 int error; /* error return value */
1083 int i; /* result code */
1084 int ilen; /* inodes in an inode cluster */
1085 xfs_mount_t *mp; /* mount structure for filesystem */
1086 int off; /* offset of inode in inode chunk */
61a25848 1087 xfs_inobt_rec_incore_t rec; /* btree record */
44b56e0a 1088 struct xfs_perag *pag;
1da177e4
LT
1089
1090 mp = tp->t_mountp;
1091
1092 /*
1093 * Break up inode number into its components.
1094 */
1095 agno = XFS_INO_TO_AGNO(mp, inode);
1096 if (agno >= mp->m_sb.sb_agcount) {
0b932ccc
DC
1097 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1098 __func__, agno, mp->m_sb.sb_agcount);
1da177e4
LT
1099 ASSERT(0);
1100 return XFS_ERROR(EINVAL);
1101 }
1102 agino = XFS_INO_TO_AGINO(mp, inode);
1103 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
0b932ccc
DC
1104 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1105 __func__, (unsigned long long)inode,
1106 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
1da177e4
LT
1107 ASSERT(0);
1108 return XFS_ERROR(EINVAL);
1109 }
1110 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1111 if (agbno >= mp->m_sb.sb_agblocks) {
0b932ccc
DC
1112 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1113 __func__, agbno, mp->m_sb.sb_agblocks);
1da177e4
LT
1114 ASSERT(0);
1115 return XFS_ERROR(EINVAL);
1116 }
1117 /*
1118 * Get the allocation group header.
1119 */
1da177e4 1120 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1da177e4 1121 if (error) {
0b932ccc
DC
1122 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1123 __func__, error);
1da177e4
LT
1124 return error;
1125 }
1126 agi = XFS_BUF_TO_AGI(agbp);
69ef921b 1127 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
16259e7d 1128 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1da177e4
LT
1129 /*
1130 * Initialize the cursor.
1131 */
561f7d17 1132 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1da177e4 1133
0b48db80
DC
1134 error = xfs_check_agi_freecount(cur, agi);
1135 if (error)
1136 goto error0;
1137
1da177e4
LT
1138 /*
1139 * Look for the entry describing this inode.
1140 */
21875505 1141 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
0b932ccc
DC
1142 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1143 __func__, error);
1da177e4
LT
1144 goto error0;
1145 }
1146 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2e287a73
CH
1147 error = xfs_inobt_get_rec(cur, &rec, &i);
1148 if (error) {
0b932ccc
DC
1149 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1150 __func__, error);
1da177e4
LT
1151 goto error0;
1152 }
1153 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1154 /*
1155 * Get the offset in the inode chunk.
1156 */
1157 off = agino - rec.ir_startino;
1158 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
0d87e656 1159 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1da177e4
LT
1160 /*
1161 * Mark the inode free & increment the count.
1162 */
0d87e656 1163 rec.ir_free |= XFS_INOBT_MASK(off);
1da177e4
LT
1164 rec.ir_freecount++;
1165
1166 /*
c41564b5 1167 * When an inode cluster is free, it becomes eligible for removal
1da177e4 1168 */
1bd960ee 1169 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1da177e4
LT
1170 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1171
1172 *delete = 1;
1173 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1174
1175 /*
1176 * Remove the inode cluster from the AGI B+Tree, adjust the
1177 * AGI and Superblock inode counts, and mark the disk space
1178 * to be freed when the transaction is committed.
1179 */
1180 ilen = XFS_IALLOC_INODES(mp);
413d57c9
MS
1181 be32_add_cpu(&agi->agi_count, -ilen);
1182 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1da177e4 1183 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
44b56e0a
DC
1184 pag = xfs_perag_get(mp, agno);
1185 pag->pagi_freecount -= ilen - 1;
1186 xfs_perag_put(pag);
1da177e4
LT
1187 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1188 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1189
91cca5df 1190 if ((error = xfs_btree_delete(cur, &i))) {
0b932ccc
DC
1191 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1192 __func__, error);
1da177e4
LT
1193 goto error0;
1194 }
1195
1196 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1197 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1198 XFS_IALLOC_BLOCKS(mp), flist, mp);
1199 } else {
1200 *delete = 0;
1201
afabc24a
CH
1202 error = xfs_inobt_update(cur, &rec);
1203 if (error) {
0b932ccc
DC
1204 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1205 __func__, error);
1da177e4
LT
1206 goto error0;
1207 }
afabc24a 1208
1da177e4
LT
1209 /*
1210 * Change the inode free counts and log the ag/sb changes.
1211 */
413d57c9 1212 be32_add_cpu(&agi->agi_freecount, 1);
1da177e4 1213 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
44b56e0a
DC
1214 pag = xfs_perag_get(mp, agno);
1215 pag->pagi_freecount++;
1216 xfs_perag_put(pag);
1da177e4
LT
1217 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1218 }
1219
0b48db80
DC
1220 error = xfs_check_agi_freecount(cur, agi);
1221 if (error)
1222 goto error0;
1da177e4 1223
1da177e4
LT
1224 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1225 return 0;
1226
1227error0:
1228 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1229 return error;
1230}
1231
7124fe0a
DC
1232STATIC int
1233xfs_imap_lookup(
1234 struct xfs_mount *mp,
1235 struct xfs_trans *tp,
1236 xfs_agnumber_t agno,
1237 xfs_agino_t agino,
1238 xfs_agblock_t agbno,
1239 xfs_agblock_t *chunk_agbno,
1240 xfs_agblock_t *offset_agbno,
1241 int flags)
1242{
1243 struct xfs_inobt_rec_incore rec;
1244 struct xfs_btree_cur *cur;
1245 struct xfs_buf *agbp;
7124fe0a
DC
1246 int error;
1247 int i;
1248
1249 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1250 if (error) {
53487786
DC
1251 xfs_alert(mp,
1252 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1253 __func__, error, agno);
7124fe0a
DC
1254 return error;
1255 }
1256
1257 /*
4536f2ad
DC
1258 * Lookup the inode record for the given agino. If the record cannot be
1259 * found, then it's an invalid inode number and we should abort. Once
1260 * we have a record, we need to ensure it contains the inode number
1261 * we are looking up.
7124fe0a
DC
1262 */
1263 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
4536f2ad 1264 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
7124fe0a
DC
1265 if (!error) {
1266 if (i)
1267 error = xfs_inobt_get_rec(cur, &rec, &i);
1268 if (!error && i == 0)
1269 error = EINVAL;
1270 }
1271
1272 xfs_trans_brelse(tp, agbp);
1273 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1274 if (error)
1275 return error;
1276
4536f2ad
DC
1277 /* check that the returned record contains the required inode */
1278 if (rec.ir_startino > agino ||
1279 rec.ir_startino + XFS_IALLOC_INODES(mp) <= agino)
1280 return EINVAL;
1281
7124fe0a 1282 /* for untrusted inodes check it is allocated first */
1920779e 1283 if ((flags & XFS_IGET_UNTRUSTED) &&
7124fe0a
DC
1284 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
1285 return EINVAL;
1286
1287 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1288 *offset_agbno = agbno - *chunk_agbno;
1289 return 0;
1290}
1291
1da177e4 1292/*
94e1b69d 1293 * Return the location of the inode in imap, for mapping it into a buffer.
1da177e4 1294 */
1da177e4 1295int
94e1b69d
CH
1296xfs_imap(
1297 xfs_mount_t *mp, /* file system mount structure */
1298 xfs_trans_t *tp, /* transaction pointer */
1da177e4 1299 xfs_ino_t ino, /* inode to locate */
94e1b69d
CH
1300 struct xfs_imap *imap, /* location map structure */
1301 uint flags) /* flags for inode btree lookup */
1da177e4
LT
1302{
1303 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1da177e4
LT
1304 xfs_agino_t agino; /* inode number within alloc group */
1305 xfs_agnumber_t agno; /* allocation group number */
1306 int blks_per_cluster; /* num blocks per inode cluster */
1307 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1da177e4 1308 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1da177e4 1309 int error; /* error code */
1da177e4
LT
1310 int offset; /* index of inode in its buffer */
1311 int offset_agbno; /* blks from chunk start to inode */
1312
1313 ASSERT(ino != NULLFSINO);
94e1b69d 1314
1da177e4
LT
1315 /*
1316 * Split up the inode number into its parts.
1317 */
1318 agno = XFS_INO_TO_AGNO(mp, ino);
1319 agino = XFS_INO_TO_AGINO(mp, ino);
1320 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1321 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1322 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1323#ifdef DEBUG
1920779e
DC
1324 /*
1325 * Don't output diagnostic information for untrusted inodes
1326 * as they can be invalid without implying corruption.
1327 */
1328 if (flags & XFS_IGET_UNTRUSTED)
4d1a2ed3 1329 return XFS_ERROR(EINVAL);
1da177e4 1330 if (agno >= mp->m_sb.sb_agcount) {
53487786
DC
1331 xfs_alert(mp,
1332 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1333 __func__, agno, mp->m_sb.sb_agcount);
1da177e4
LT
1334 }
1335 if (agbno >= mp->m_sb.sb_agblocks) {
53487786
DC
1336 xfs_alert(mp,
1337 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1338 __func__, (unsigned long long)agbno,
1339 (unsigned long)mp->m_sb.sb_agblocks);
1da177e4
LT
1340 }
1341 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
53487786
DC
1342 xfs_alert(mp,
1343 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1344 __func__, ino,
1345 XFS_AGINO_TO_INO(mp, agno, agino));
1da177e4 1346 }
745b1f47 1347 xfs_stack_trace();
1da177e4
LT
1348#endif /* DEBUG */
1349 return XFS_ERROR(EINVAL);
1350 }
94e1b69d 1351
7124fe0a
DC
1352 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1353
1354 /*
1355 * For bulkstat and handle lookups, we have an untrusted inode number
1356 * that we have to verify is valid. We cannot do this just by reading
1357 * the inode buffer as it may have been unlinked and removed leaving
1358 * inodes in stale state on disk. Hence we have to do a btree lookup
1359 * in all cases where an untrusted inode number is passed.
1360 */
1920779e 1361 if (flags & XFS_IGET_UNTRUSTED) {
7124fe0a
DC
1362 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1363 &chunk_agbno, &offset_agbno, flags);
1364 if (error)
1365 return error;
1366 goto out_map;
1367 }
1368
94e1b69d
CH
1369 /*
1370 * If the inode cluster size is the same as the blocksize or
1371 * smaller we get to the buffer by simple arithmetics.
1372 */
1373 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
1da177e4
LT
1374 offset = XFS_INO_TO_OFFSET(mp, ino);
1375 ASSERT(offset < mp->m_sb.sb_inopblock);
94e1b69d
CH
1376
1377 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1378 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1379 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1da177e4
LT
1380 return 0;
1381 }
94e1b69d 1382
94e1b69d
CH
1383 /*
1384 * If the inode chunks are aligned then use simple maths to
1385 * find the location. Otherwise we have to do a btree
1386 * lookup to find the location.
1387 */
1da177e4
LT
1388 if (mp->m_inoalign_mask) {
1389 offset_agbno = agbno & mp->m_inoalign_mask;
1390 chunk_agbno = agbno - offset_agbno;
1391 } else {
7124fe0a
DC
1392 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1393 &chunk_agbno, &offset_agbno, flags);
1da177e4
LT
1394 if (error)
1395 return error;
1da177e4 1396 }
94e1b69d 1397
7124fe0a 1398out_map:
1da177e4
LT
1399 ASSERT(agbno >= chunk_agbno);
1400 cluster_agbno = chunk_agbno +
1401 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1402 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1403 XFS_INO_TO_OFFSET(mp, ino);
94e1b69d
CH
1404
1405 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1406 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1407 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1408
1409 /*
1410 * If the inode number maps to a block outside the bounds
1411 * of the file system then return NULL rather than calling
1412 * read_buf and panicing when we get an error from the
1413 * driver.
1414 */
1415 if ((imap->im_blkno + imap->im_len) >
1416 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
53487786
DC
1417 xfs_alert(mp,
1418 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1419 __func__, (unsigned long long) imap->im_blkno,
94e1b69d
CH
1420 (unsigned long long) imap->im_len,
1421 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1422 return XFS_ERROR(EINVAL);
1423 }
1da177e4 1424 return 0;
1da177e4
LT
1425}
1426
1427/*
1428 * Compute and fill in value of m_in_maxlevels.
1429 */
1430void
1431xfs_ialloc_compute_maxlevels(
1432 xfs_mount_t *mp) /* file system mount structure */
1433{
1434 int level;
1435 uint maxblocks;
1436 uint maxleafents;
1437 int minleafrecs;
1438 int minnoderecs;
1439
1440 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1441 XFS_INODES_PER_CHUNK_LOG;
1442 minleafrecs = mp->m_alloc_mnr[0];
1443 minnoderecs = mp->m_alloc_mnr[1];
1444 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1445 for (level = 1; maxblocks > 1; level++)
1446 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1447 mp->m_in_maxlevels = level;
1448}
1449
1450/*
1451 * Log specified fields for the ag hdr (inode section)
1452 */
1453void
1454xfs_ialloc_log_agi(
1455 xfs_trans_t *tp, /* transaction pointer */
1456 xfs_buf_t *bp, /* allocation group header buffer */
1457 int fields) /* bitmask of fields to log */
1458{
1459 int first; /* first byte number */
1460 int last; /* last byte number */
1461 static const short offsets[] = { /* field starting offsets */
1462 /* keep in sync with bit definitions */
1463 offsetof(xfs_agi_t, agi_magicnum),
1464 offsetof(xfs_agi_t, agi_versionnum),
1465 offsetof(xfs_agi_t, agi_seqno),
1466 offsetof(xfs_agi_t, agi_length),
1467 offsetof(xfs_agi_t, agi_count),
1468 offsetof(xfs_agi_t, agi_root),
1469 offsetof(xfs_agi_t, agi_level),
1470 offsetof(xfs_agi_t, agi_freecount),
1471 offsetof(xfs_agi_t, agi_newino),
1472 offsetof(xfs_agi_t, agi_dirino),
1473 offsetof(xfs_agi_t, agi_unlinked),
1474 sizeof(xfs_agi_t)
1475 };
1476#ifdef DEBUG
1477 xfs_agi_t *agi; /* allocation group header */
1478
1479 agi = XFS_BUF_TO_AGI(bp);
69ef921b 1480 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1da177e4
LT
1481#endif
1482 /*
1483 * Compute byte offsets for the first and last fields.
1484 */
1485 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1486 /*
1487 * Log the allocation group inode header buffer.
1488 */
61fe135c 1489 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
1da177e4
LT
1490 xfs_trans_log_buf(tp, bp, first, last);
1491}
1492
5e1be0fb
CH
1493#ifdef DEBUG
1494STATIC void
1495xfs_check_agi_unlinked(
1496 struct xfs_agi *agi)
1497{
1498 int i;
1499
1500 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1501 ASSERT(agi->agi_unlinked[i]);
1502}
1503#else
1504#define xfs_check_agi_unlinked(agi)
1505#endif
1506
983d09ff 1507static bool
612cfbfe 1508xfs_agi_verify(
3702ce6e
DC
1509 struct xfs_buf *bp)
1510{
1511 struct xfs_mount *mp = bp->b_target->bt_mount;
1512 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
3702ce6e 1513
983d09ff
DC
1514 if (xfs_sb_version_hascrc(&mp->m_sb) &&
1515 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
1516 return false;
3702ce6e
DC
1517 /*
1518 * Validate the magic number of the agi block.
1519 */
983d09ff
DC
1520 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
1521 return false;
1522 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
1523 return false;
3702ce6e
DC
1524
1525 /*
1526 * during growfs operations, the perag is not fully initialised,
1527 * so we can't use it for any useful checking. growfs ensures we can't
1528 * use it by using uncached buffers that don't have the perag attached
1529 * so we can detect and avoid this problem.
1530 */
983d09ff
DC
1531 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
1532 return false;
3702ce6e 1533
3702ce6e 1534 xfs_check_agi_unlinked(agi);
983d09ff 1535 return true;
612cfbfe
DC
1536}
1537
1813dd64
DC
1538static void
1539xfs_agi_read_verify(
612cfbfe
DC
1540 struct xfs_buf *bp)
1541{
983d09ff
DC
1542 struct xfs_mount *mp = bp->b_target->bt_mount;
1543 int agi_ok = 1;
1544
1545 if (xfs_sb_version_hascrc(&mp->m_sb))
1546 agi_ok = xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
1547 offsetof(struct xfs_agi, agi_crc));
1548 agi_ok = agi_ok && xfs_agi_verify(bp);
1549
1550 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1551 XFS_RANDOM_IALLOC_READ_AGI))) {
1552 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1553 xfs_buf_ioerror(bp, EFSCORRUPTED);
1554 }
612cfbfe
DC
1555}
1556
b0f539de 1557static void
1813dd64 1558xfs_agi_write_verify(
612cfbfe
DC
1559 struct xfs_buf *bp)
1560{
983d09ff
DC
1561 struct xfs_mount *mp = bp->b_target->bt_mount;
1562 struct xfs_buf_log_item *bip = bp->b_fspriv;
1563
1564 if (!xfs_agi_verify(bp)) {
1565 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
1566 xfs_buf_ioerror(bp, EFSCORRUPTED);
1567 return;
1568 }
1569
1570 if (!xfs_sb_version_hascrc(&mp->m_sb))
1571 return;
1572
1573 if (bip)
1574 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1575 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
1576 offsetof(struct xfs_agi, agi_crc));
3702ce6e
DC
1577}
1578
1813dd64
DC
1579const struct xfs_buf_ops xfs_agi_buf_ops = {
1580 .verify_read = xfs_agi_read_verify,
1581 .verify_write = xfs_agi_write_verify,
1582};
1583
1da177e4
LT
1584/*
1585 * Read in the allocation group header (inode allocation section)
1586 */
1587int
5e1be0fb
CH
1588xfs_read_agi(
1589 struct xfs_mount *mp, /* file system mount structure */
1590 struct xfs_trans *tp, /* transaction pointer */
1591 xfs_agnumber_t agno, /* allocation group number */
1592 struct xfs_buf **bpp) /* allocation group hdr buf */
1da177e4 1593{
5e1be0fb 1594 int error;
1da177e4
LT
1595
1596 ASSERT(agno != NULLAGNUMBER);
5e1be0fb
CH
1597
1598 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1da177e4 1599 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1813dd64 1600 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
1da177e4
LT
1601 if (error)
1602 return error;
5e1be0fb 1603
5a52c2a5 1604 ASSERT(!xfs_buf_geterror(*bpp));
38f23232 1605 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
5e1be0fb
CH
1606 return 0;
1607}
1608
1609int
1610xfs_ialloc_read_agi(
1611 struct xfs_mount *mp, /* file system mount structure */
1612 struct xfs_trans *tp, /* transaction pointer */
1613 xfs_agnumber_t agno, /* allocation group number */
1614 struct xfs_buf **bpp) /* allocation group hdr buf */
1615{
1616 struct xfs_agi *agi; /* allocation group header */
1617 struct xfs_perag *pag; /* per allocation group data */
1618 int error;
1619
1620 error = xfs_read_agi(mp, tp, agno, bpp);
1621 if (error)
1622 return error;
1623
1624 agi = XFS_BUF_TO_AGI(*bpp);
44b56e0a 1625 pag = xfs_perag_get(mp, agno);
1da177e4 1626 if (!pag->pagi_init) {
16259e7d 1627 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
92821e2b 1628 pag->pagi_count = be32_to_cpu(agi->agi_count);
1da177e4 1629 pag->pagi_init = 1;
1da177e4 1630 }
1da177e4 1631
5e1be0fb
CH
1632 /*
1633 * It's possible for these to be out of sync if
1634 * we are in the middle of a forced shutdown.
1635 */
1636 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1637 XFS_FORCED_SHUTDOWN(mp));
44b56e0a 1638 xfs_perag_put(pag);
1da177e4
LT
1639 return 0;
1640}
92821e2b
DC
1641
1642/*
1643 * Read in the agi to initialise the per-ag data in the mount structure
1644 */
1645int
1646xfs_ialloc_pagi_init(
1647 xfs_mount_t *mp, /* file system mount structure */
1648 xfs_trans_t *tp, /* transaction pointer */
1649 xfs_agnumber_t agno) /* allocation group number */
1650{
1651 xfs_buf_t *bp = NULL;
1652 int error;
1653
1654 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1655 if (error)
1656 return error;
1657 if (bp)
1658 xfs_trans_brelse(tp, bp);
1659 return 0;
1660}