]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/libxfs/xfs_ialloc.c
xfs: helper to convert holemask to inode alloc. bitmap
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / libxfs / 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"
70a9883c 20#include "xfs_shared.h"
239880ef
DC
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
a844f451 24#include "xfs_bit.h"
1da177e4 25#include "xfs_sb.h"
1da177e4 26#include "xfs_mount.h"
1da177e4 27#include "xfs_inode.h"
a844f451
NS
28#include "xfs_btree.h"
29#include "xfs_ialloc.h"
a4fbe6ab 30#include "xfs_ialloc_btree.h"
1da177e4 31#include "xfs_alloc.h"
1da177e4
LT
32#include "xfs_rtalloc.h"
33#include "xfs_error.h"
34#include "xfs_bmap.h"
983d09ff 35#include "xfs_cksum.h"
239880ef 36#include "xfs_trans.h"
983d09ff 37#include "xfs_buf_item.h"
ddf6ad01 38#include "xfs_icreate_item.h"
7bb85ef3 39#include "xfs_icache.h"
d123031a 40#include "xfs_trace.h"
1da177e4 41
1da177e4
LT
42
43/*
44 * Allocation group level functions.
45 */
75de2a91
DC
46static inline int
47xfs_ialloc_cluster_alignment(
7a1df156 48 struct xfs_mount *mp)
75de2a91 49{
7a1df156
DC
50 if (xfs_sb_version_hasalign(&mp->m_sb) &&
51 mp->m_sb.sb_inoalignmt >=
52 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
53 return mp->m_sb.sb_inoalignmt;
75de2a91
DC
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;
5419040f
BF
68 cur->bc_rec.i.ir_holemask = 0;
69 cur->bc_rec.i.ir_count = 0;
21875505
CH
70 cur->bc_rec.i.ir_freecount = 0;
71 cur->bc_rec.i.ir_free = 0;
72 return xfs_btree_lookup(cur, dir, stat);
fe033cc8
CH
73}
74
278d0ca1 75/*
afabc24a 76 * Update the record referred to by cur to the value given.
278d0ca1
CH
77 * This either works (return 0) or gets an EFSCORRUPTED error.
78 */
79STATIC int /* error */
80xfs_inobt_update(
81 struct xfs_btree_cur *cur, /* btree cursor */
afabc24a 82 xfs_inobt_rec_incore_t *irec) /* btree record */
278d0ca1
CH
83{
84 union xfs_btree_rec rec;
85
afabc24a 86 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
5419040f
BF
87 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
88 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
89 rec.inobt.ir_u.sp.ir_count = irec->ir_count;
90 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
91 } else {
92 /* ir_holemask/ir_count not supported on-disk */
93 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
94 }
afabc24a 95 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
278d0ca1
CH
96 return xfs_btree_update(cur, &rec);
97}
98
8cc938fe
CH
99/*
100 * Get the data from the pointed-to record.
101 */
102int /* error */
103xfs_inobt_get_rec(
104 struct xfs_btree_cur *cur, /* btree cursor */
2e287a73 105 xfs_inobt_rec_incore_t *irec, /* btree record */
8cc938fe
CH
106 int *stat) /* output: success/failure */
107{
108 union xfs_btree_rec *rec;
109 int error;
110
111 error = xfs_btree_get_rec(cur, &rec, stat);
5419040f
BF
112 if (error || *stat == 0)
113 return error;
114
115 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
116 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
117 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
118 irec->ir_count = rec->inobt.ir_u.sp.ir_count;
119 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
120 } else {
121 /*
122 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
123 * values for full inode chunks.
124 */
125 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
126 irec->ir_count = XFS_INODES_PER_CHUNK;
127 irec->ir_freecount =
128 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
8cc938fe 129 }
5419040f
BF
130 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
131
132 return 0;
8cc938fe
CH
133}
134
0aa0a756
BF
135/*
136 * Insert a single inobt record. Cursor must already point to desired location.
137 */
138STATIC int
139xfs_inobt_insert_rec(
140 struct xfs_btree_cur *cur,
5419040f
BF
141 __uint16_t holemask,
142 __uint8_t count,
0aa0a756
BF
143 __int32_t freecount,
144 xfs_inofree_t free,
145 int *stat)
146{
5419040f
BF
147 cur->bc_rec.i.ir_holemask = holemask;
148 cur->bc_rec.i.ir_count = count;
0aa0a756
BF
149 cur->bc_rec.i.ir_freecount = freecount;
150 cur->bc_rec.i.ir_free = free;
151 return xfs_btree_insert(cur, stat);
152}
153
154/*
155 * Insert records describing a newly allocated inode chunk into the inobt.
156 */
157STATIC int
158xfs_inobt_insert(
159 struct xfs_mount *mp,
160 struct xfs_trans *tp,
161 struct xfs_buf *agbp,
162 xfs_agino_t newino,
163 xfs_agino_t newlen,
164 xfs_btnum_t btnum)
165{
166 struct xfs_btree_cur *cur;
167 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
168 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
169 xfs_agino_t thisino;
170 int i;
171 int error;
172
173 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
174
175 for (thisino = newino;
176 thisino < newino + newlen;
177 thisino += XFS_INODES_PER_CHUNK) {
178 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
179 if (error) {
180 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
181 return error;
182 }
183 ASSERT(i == 0);
184
5419040f
BF
185 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
186 XFS_INODES_PER_CHUNK,
187 XFS_INODES_PER_CHUNK,
0aa0a756
BF
188 XFS_INOBT_ALL_FREE, &i);
189 if (error) {
190 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
191 return error;
192 }
193 ASSERT(i == 1);
194 }
195
196 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
197
198 return 0;
199}
200
0b48db80
DC
201/*
202 * Verify that the number of free inodes in the AGI is correct.
203 */
204#ifdef DEBUG
205STATIC int
206xfs_check_agi_freecount(
207 struct xfs_btree_cur *cur,
208 struct xfs_agi *agi)
209{
210 if (cur->bc_nlevels == 1) {
211 xfs_inobt_rec_incore_t rec;
212 int freecount = 0;
213 int error;
214 int i;
215
21875505 216 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
0b48db80
DC
217 if (error)
218 return error;
219
220 do {
221 error = xfs_inobt_get_rec(cur, &rec, &i);
222 if (error)
223 return error;
224
225 if (i) {
226 freecount += rec.ir_freecount;
227 error = xfs_btree_increment(cur, 0, &i);
228 if (error)
229 return error;
230 }
231 } while (i == 1);
232
233 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
234 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
235 }
236 return 0;
237}
238#else
239#define xfs_check_agi_freecount(cur, agi) 0
240#endif
241
85c0b2ab 242/*
28c8e41a
DC
243 * Initialise a new set of inodes. When called without a transaction context
244 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
245 * than logging them (which in a transaction context puts them into the AIL
246 * for writeback rather than the xfsbufd queue).
85c0b2ab 247 */
ddf6ad01 248int
85c0b2ab
DC
249xfs_ialloc_inode_init(
250 struct xfs_mount *mp,
251 struct xfs_trans *tp,
28c8e41a 252 struct list_head *buffer_list,
463958af 253 int icount,
85c0b2ab
DC
254 xfs_agnumber_t agno,
255 xfs_agblock_t agbno,
256 xfs_agblock_t length,
257 unsigned int gen)
258{
259 struct xfs_buf *fbuf;
260 struct xfs_dinode *free;
6e0c7b8c 261 int nbufs, blks_per_cluster, inodes_per_cluster;
85c0b2ab
DC
262 int version;
263 int i, j;
264 xfs_daddr_t d;
93848a99 265 xfs_ino_t ino = 0;
85c0b2ab
DC
266
267 /*
6e0c7b8c
JL
268 * Loop over the new block(s), filling in the inodes. For small block
269 * sizes, manipulate the inodes in buffers which are multiples of the
270 * blocks size.
85c0b2ab 271 */
6e0c7b8c
JL
272 blks_per_cluster = xfs_icluster_size_fsb(mp);
273 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
274 nbufs = length / blks_per_cluster;
85c0b2ab
DC
275
276 /*
93848a99
CH
277 * Figure out what version number to use in the inodes we create. If
278 * the superblock version has caught up to the one that supports the new
279 * inode format, then use the new inode version. Otherwise use the old
280 * version so that old kernels will continue to be able to use the file
281 * system.
282 *
283 * For v3 inodes, we also need to write the inode number into the inode,
284 * so calculate the first inode number of the chunk here as
285 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
286 * across multiple filesystem blocks (such as a cluster) and so cannot
287 * be used in the cluster buffer loop below.
288 *
289 * Further, because we are writing the inode directly into the buffer
290 * and calculating a CRC on the entire inode, we have ot log the entire
291 * inode so that the entire range the CRC covers is present in the log.
292 * That means for v3 inode we log the entire buffer rather than just the
293 * inode cores.
85c0b2ab 294 */
93848a99
CH
295 if (xfs_sb_version_hascrc(&mp->m_sb)) {
296 version = 3;
297 ino = XFS_AGINO_TO_INO(mp, agno,
298 XFS_OFFBNO_TO_AGINO(mp, agbno, 0));
ddf6ad01
DC
299
300 /*
301 * log the initialisation that is about to take place as an
302 * logical operation. This means the transaction does not
303 * need to log the physical changes to the inode buffers as log
304 * recovery will know what initialisation is actually needed.
305 * Hence we only need to log the buffers as "ordered" buffers so
306 * they track in the AIL as if they were physically logged.
307 */
308 if (tp)
463958af 309 xfs_icreate_log(tp, agno, agbno, icount,
ddf6ad01 310 mp->m_sb.sb_inodesize, length, gen);
263997a6 311 } else
85c0b2ab 312 version = 2;
85c0b2ab
DC
313
314 for (j = 0; j < nbufs; j++) {
315 /*
316 * Get the block.
317 */
318 d = XFS_AGB_TO_DADDR(mp, agno, agbno + (j * blks_per_cluster));
319 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
7c4cebe8
DC
320 mp->m_bsize * blks_per_cluster,
321 XBF_UNMAPPED);
2a30f36d 322 if (!fbuf)
2451337d 323 return -ENOMEM;
ddf6ad01
DC
324
325 /* Initialize the inode buffers and log them appropriately. */
1813dd64 326 fbuf->b_ops = &xfs_inode_buf_ops;
93848a99 327 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
6e0c7b8c 328 for (i = 0; i < inodes_per_cluster; i++) {
85c0b2ab 329 int ioffset = i << mp->m_sb.sb_inodelog;
93848a99 330 uint isize = xfs_dinode_size(version);
85c0b2ab
DC
331
332 free = xfs_make_iptr(mp, fbuf, i);
333 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
334 free->di_version = version;
335 free->di_gen = cpu_to_be32(gen);
336 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
93848a99
CH
337
338 if (version == 3) {
339 free->di_ino = cpu_to_be64(ino);
340 ino++;
341 uuid_copy(&free->di_uuid, &mp->m_sb.sb_uuid);
342 xfs_dinode_calc_crc(mp, free);
28c8e41a 343 } else if (tp) {
93848a99
CH
344 /* just log the inode core */
345 xfs_trans_log_buf(tp, fbuf, ioffset,
346 ioffset + isize - 1);
347 }
348 }
28c8e41a
DC
349
350 if (tp) {
351 /*
352 * Mark the buffer as an inode allocation buffer so it
353 * sticks in AIL at the point of this allocation
354 * transaction. This ensures the they are on disk before
355 * the tail of the log can be moved past this
356 * transaction (i.e. by preventing relogging from moving
357 * it forward in the log).
358 */
359 xfs_trans_inode_alloc_buf(tp, fbuf);
360 if (version == 3) {
ddf6ad01
DC
361 /*
362 * Mark the buffer as ordered so that they are
363 * not physically logged in the transaction but
364 * still tracked in the AIL as part of the
365 * transaction and pin the log appropriately.
366 */
367 xfs_trans_ordered_buf(tp, fbuf);
28c8e41a
DC
368 xfs_trans_log_buf(tp, fbuf, 0,
369 BBTOB(fbuf->b_length) - 1);
370 }
371 } else {
372 fbuf->b_flags |= XBF_DONE;
373 xfs_buf_delwri_queue(fbuf, buffer_list);
374 xfs_buf_relse(fbuf);
85c0b2ab 375 }
85c0b2ab 376 }
2a30f36d 377 return 0;
85c0b2ab
DC
378}
379
1da177e4
LT
380/*
381 * Allocate new inodes in the allocation group specified by agbp.
382 * Return 0 for success, else error code.
383 */
384STATIC int /* error code or 0 */
385xfs_ialloc_ag_alloc(
386 xfs_trans_t *tp, /* transaction pointer */
387 xfs_buf_t *agbp, /* alloc group buffer */
388 int *alloc)
389{
390 xfs_agi_t *agi; /* allocation group header */
391 xfs_alloc_arg_t args; /* allocation argument structure */
92821e2b 392 xfs_agnumber_t agno;
1da177e4 393 int error;
1da177e4
LT
394 xfs_agino_t newino; /* new first inode's number */
395 xfs_agino_t newlen; /* new number of inodes */
3ccb8b5f 396 int isaligned = 0; /* inode allocation at stripe unit */
1da177e4 397 /* boundary */
44b56e0a 398 struct xfs_perag *pag;
1da177e4 399
a0041684 400 memset(&args, 0, sizeof(args));
1da177e4
LT
401 args.tp = tp;
402 args.mp = tp->t_mountp;
403
404 /*
405 * Locking will ensure that we don't have two callers in here
406 * at one time.
407 */
71783438 408 newlen = args.mp->m_ialloc_inos;
1da177e4 409 if (args.mp->m_maxicount &&
501ab323
DC
410 percpu_counter_read(&args.mp->m_icount) + newlen >
411 args.mp->m_maxicount)
2451337d 412 return -ENOSPC;
126cd105 413 args.minlen = args.maxlen = args.mp->m_ialloc_blks;
1da177e4 414 /*
3ccb8b5f
GO
415 * First try to allocate inodes contiguous with the last-allocated
416 * chunk of inodes. If the filesystem is striped, this will fill
417 * an entire stripe unit with inodes.
28c8e41a 418 */
1da177e4 419 agi = XFS_BUF_TO_AGI(agbp);
3ccb8b5f 420 newino = be32_to_cpu(agi->agi_newino);
85c0b2ab 421 agno = be32_to_cpu(agi->agi_seqno);
019ff2d5 422 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
126cd105 423 args.mp->m_ialloc_blks;
019ff2d5
NS
424 if (likely(newino != NULLAGINO &&
425 (args.agbno < be32_to_cpu(agi->agi_length)))) {
85c0b2ab 426 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
3ccb8b5f 427 args.type = XFS_ALLOCTYPE_THIS_BNO;
3ccb8b5f 428 args.prod = 1;
75de2a91 429
3ccb8b5f 430 /*
75de2a91
DC
431 * We need to take into account alignment here to ensure that
432 * we don't modify the free list if we fail to have an exact
433 * block. If we don't have an exact match, and every oher
434 * attempt allocation attempt fails, we'll end up cancelling
435 * a dirty transaction and shutting down.
436 *
437 * For an exact allocation, alignment must be 1,
438 * however we need to take cluster alignment into account when
439 * fixing up the freelist. Use the minalignslop field to
440 * indicate that extra blocks might be required for alignment,
441 * but not to use them in the actual exact allocation.
3ccb8b5f 442 */
75de2a91 443 args.alignment = 1;
7a1df156 444 args.minalignslop = xfs_ialloc_cluster_alignment(args.mp) - 1;
75de2a91
DC
445
446 /* Allow space for the inode btree to split. */
0d87e656 447 args.minleft = args.mp->m_in_maxlevels - 1;
3ccb8b5f
GO
448 if ((error = xfs_alloc_vextent(&args)))
449 return error;
e480a723
BF
450
451 /*
452 * This request might have dirtied the transaction if the AG can
453 * satisfy the request, but the exact block was not available.
454 * If the allocation did fail, subsequent requests will relax
455 * the exact agbno requirement and increase the alignment
456 * instead. It is critical that the total size of the request
457 * (len + alignment + slop) does not increase from this point
458 * on, so reset minalignslop to ensure it is not included in
459 * subsequent requests.
460 */
461 args.minalignslop = 0;
3ccb8b5f
GO
462 } else
463 args.fsbno = NULLFSBLOCK;
1da177e4 464
3ccb8b5f
GO
465 if (unlikely(args.fsbno == NULLFSBLOCK)) {
466 /*
467 * Set the alignment for the allocation.
468 * If stripe alignment is turned on then align at stripe unit
469 * boundary.
019ff2d5
NS
470 * If the cluster size is smaller than a filesystem block
471 * then we're doing I/O for inodes in filesystem block size
3ccb8b5f
GO
472 * pieces, so don't need alignment anyway.
473 */
474 isaligned = 0;
475 if (args.mp->m_sinoalign) {
476 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
477 args.alignment = args.mp->m_dalign;
478 isaligned = 1;
75de2a91 479 } else
7a1df156 480 args.alignment = xfs_ialloc_cluster_alignment(args.mp);
3ccb8b5f
GO
481 /*
482 * Need to figure out where to allocate the inode blocks.
483 * Ideally they should be spaced out through the a.g.
484 * For now, just allocate blocks up front.
485 */
486 args.agbno = be32_to_cpu(agi->agi_root);
85c0b2ab 487 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
3ccb8b5f
GO
488 /*
489 * Allocate a fixed-size extent of inodes.
490 */
491 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3ccb8b5f
GO
492 args.prod = 1;
493 /*
494 * Allow space for the inode btree to split.
495 */
0d87e656 496 args.minleft = args.mp->m_in_maxlevels - 1;
3ccb8b5f
GO
497 if ((error = xfs_alloc_vextent(&args)))
498 return error;
499 }
019ff2d5 500
1da177e4
LT
501 /*
502 * If stripe alignment is turned on, then try again with cluster
503 * alignment.
504 */
505 if (isaligned && args.fsbno == NULLFSBLOCK) {
506 args.type = XFS_ALLOCTYPE_NEAR_BNO;
16259e7d 507 args.agbno = be32_to_cpu(agi->agi_root);
85c0b2ab 508 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
7a1df156 509 args.alignment = xfs_ialloc_cluster_alignment(args.mp);
1da177e4
LT
510 if ((error = xfs_alloc_vextent(&args)))
511 return error;
512 }
513
514 if (args.fsbno == NULLFSBLOCK) {
515 *alloc = 0;
516 return 0;
517 }
518 ASSERT(args.len == args.minlen);
1da177e4 519
359346a9 520 /*
85c0b2ab
DC
521 * Stamp and write the inode buffers.
522 *
359346a9
DC
523 * Seed the new inode cluster with a random generation number. This
524 * prevents short-term reuse of generation numbers if a chunk is
525 * freed and then immediately reallocated. We use random numbers
526 * rather than a linear progression to prevent the next generation
527 * number from being easily guessable.
528 */
463958af
BF
529 error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
530 args.agbno, args.len, prandom_u32());
d42f08f6 531
2a30f36d
CS
532 if (error)
533 return error;
85c0b2ab
DC
534 /*
535 * Convert the results.
536 */
537 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
413d57c9
MS
538 be32_add_cpu(&agi->agi_count, newlen);
539 be32_add_cpu(&agi->agi_freecount, newlen);
44b56e0a
DC
540 pag = xfs_perag_get(args.mp, agno);
541 pag->pagi_freecount += newlen;
542 xfs_perag_put(pag);
16259e7d 543 agi->agi_newino = cpu_to_be32(newino);
85c0b2ab 544
1da177e4 545 /*
0aa0a756 546 * Insert records describing the new inode chunk into the btrees.
1da177e4 547 */
0aa0a756
BF
548 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
549 XFS_BTNUM_INO);
550 if (error)
551 return error;
552
553 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
554 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
555 XFS_BTNUM_FINO);
556 if (error)
1da177e4 557 return error;
1da177e4 558 }
1da177e4
LT
559 /*
560 * Log allocation group header fields
561 */
562 xfs_ialloc_log_agi(tp, agbp,
563 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
564 /*
565 * Modify/log superblock values for inode count and inode free count.
566 */
567 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
568 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
569 *alloc = 1;
570 return 0;
571}
572
b8f82a4a 573STATIC xfs_agnumber_t
1da177e4
LT
574xfs_ialloc_next_ag(
575 xfs_mount_t *mp)
576{
577 xfs_agnumber_t agno;
578
579 spin_lock(&mp->m_agirotor_lock);
580 agno = mp->m_agirotor;
8aea3ff4 581 if (++mp->m_agirotor >= mp->m_maxagi)
1da177e4
LT
582 mp->m_agirotor = 0;
583 spin_unlock(&mp->m_agirotor_lock);
584
585 return agno;
586}
587
588/*
589 * Select an allocation group to look for a free inode in, based on the parent
2f21ff1c 590 * inode and the mode. Return the allocation group buffer.
1da177e4 591 */
55d6af64 592STATIC xfs_agnumber_t
1da177e4
LT
593xfs_ialloc_ag_select(
594 xfs_trans_t *tp, /* transaction pointer */
595 xfs_ino_t parent, /* parent directory inode number */
576b1d67 596 umode_t mode, /* bits set to indicate file type */
1da177e4
LT
597 int okalloc) /* ok to allocate more space */
598{
1da177e4
LT
599 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
600 xfs_agnumber_t agno; /* current ag number */
601 int flags; /* alloc buffer locking flags */
602 xfs_extlen_t ineed; /* blocks needed for inode allocation */
603 xfs_extlen_t longest = 0; /* longest extent available */
604 xfs_mount_t *mp; /* mount point structure */
605 int needspace; /* file mode implies space allocated */
606 xfs_perag_t *pag; /* per allocation group data */
607 xfs_agnumber_t pagno; /* parent (starting) ag number */
55d6af64 608 int error;
1da177e4
LT
609
610 /*
611 * Files of these types need at least one block if length > 0
612 * (and they won't fit in the inode, but that's hard to figure out).
613 */
614 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
615 mp = tp->t_mountp;
616 agcount = mp->m_maxagi;
617 if (S_ISDIR(mode))
618 pagno = xfs_ialloc_next_ag(mp);
619 else {
620 pagno = XFS_INO_TO_AGNO(mp, parent);
621 if (pagno >= agcount)
622 pagno = 0;
623 }
55d6af64 624
1da177e4 625 ASSERT(pagno < agcount);
55d6af64 626
1da177e4
LT
627 /*
628 * Loop through allocation groups, looking for one with a little
629 * free space in it. Note we don't look for free inodes, exactly.
630 * Instead, we include whether there is a need to allocate inodes
631 * to mean that blocks must be allocated for them,
632 * if none are currently free.
633 */
634 agno = pagno;
635 flags = XFS_ALLOC_FLAG_TRYLOCK;
1da177e4 636 for (;;) {
44b56e0a 637 pag = xfs_perag_get(mp, agno);
55d6af64
CH
638 if (!pag->pagi_inodeok) {
639 xfs_ialloc_next_ag(mp);
640 goto nextag;
641 }
642
1da177e4 643 if (!pag->pagi_init) {
55d6af64
CH
644 error = xfs_ialloc_pagi_init(mp, tp, agno);
645 if (error)
1da177e4 646 goto nextag;
55d6af64 647 }
1da177e4 648
55d6af64
CH
649 if (pag->pagi_freecount) {
650 xfs_perag_put(pag);
651 return agno;
1da177e4
LT
652 }
653
55d6af64
CH
654 if (!okalloc)
655 goto nextag;
656
657 if (!pag->pagf_init) {
658 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
659 if (error)
1da177e4 660 goto nextag;
1da177e4 661 }
55d6af64
CH
662
663 /*
7a1df156
DC
664 * Check that there is enough free space for the file plus a
665 * chunk of inodes if we need to allocate some. If this is the
666 * first pass across the AGs, take into account the potential
667 * space needed for alignment of inode chunks when checking the
668 * longest contiguous free space in the AG - this prevents us
669 * from getting ENOSPC because we have free space larger than
670 * m_ialloc_blks but alignment constraints prevent us from using
671 * it.
672 *
673 * If we can't find an AG with space for full alignment slack to
674 * be taken into account, we must be near ENOSPC in all AGs.
675 * Hence we don't include alignment for the second pass and so
676 * if we fail allocation due to alignment issues then it is most
677 * likely a real ENOSPC condition.
55d6af64 678 */
066a1884 679 ineed = mp->m_ialloc_min_blks;
7a1df156
DC
680 if (flags && ineed > 1)
681 ineed += xfs_ialloc_cluster_alignment(mp);
55d6af64
CH
682 longest = pag->pagf_longest;
683 if (!longest)
684 longest = pag->pagf_flcount > 0;
685
686 if (pag->pagf_freeblks >= needspace + ineed &&
687 longest >= ineed) {
688 xfs_perag_put(pag);
689 return agno;
1da177e4 690 }
1da177e4 691nextag:
44b56e0a 692 xfs_perag_put(pag);
1da177e4
LT
693 /*
694 * No point in iterating over the rest, if we're shutting
695 * down.
696 */
1c1c6ebc 697 if (XFS_FORCED_SHUTDOWN(mp))
55d6af64 698 return NULLAGNUMBER;
1da177e4
LT
699 agno++;
700 if (agno >= agcount)
701 agno = 0;
702 if (agno == pagno) {
1c1c6ebc 703 if (flags == 0)
55d6af64 704 return NULLAGNUMBER;
1da177e4
LT
705 flags = 0;
706 }
707 }
708}
709
4254b0bb
CH
710/*
711 * Try to retrieve the next record to the left/right from the current one.
712 */
713STATIC int
714xfs_ialloc_next_rec(
715 struct xfs_btree_cur *cur,
716 xfs_inobt_rec_incore_t *rec,
717 int *done,
718 int left)
719{
720 int error;
721 int i;
722
723 if (left)
724 error = xfs_btree_decrement(cur, 0, &i);
725 else
726 error = xfs_btree_increment(cur, 0, &i);
727
728 if (error)
729 return error;
730 *done = !i;
731 if (i) {
732 error = xfs_inobt_get_rec(cur, rec, &i);
733 if (error)
734 return error;
5fb5aeee 735 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
4254b0bb
CH
736 }
737
738 return 0;
739}
740
bd169565
DC
741STATIC int
742xfs_ialloc_get_rec(
743 struct xfs_btree_cur *cur,
744 xfs_agino_t agino,
745 xfs_inobt_rec_incore_t *rec,
43df2ee6 746 int *done)
bd169565
DC
747{
748 int error;
749 int i;
750
751 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
752 if (error)
753 return error;
754 *done = !i;
755 if (i) {
756 error = xfs_inobt_get_rec(cur, rec, &i);
757 if (error)
758 return error;
5fb5aeee 759 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
bd169565
DC
760 }
761
762 return 0;
763}
0b48db80 764
d4cc540b
BF
765/*
766 * Return the offset of the first free inode in the record.
767 */
768STATIC int
769xfs_inobt_first_free_inode(
770 struct xfs_inobt_rec_incore *rec)
771{
772 return xfs_lowbit64(rec->ir_free);
773}
774
1da177e4 775/*
6dd8638e 776 * Allocate an inode using the inobt-only algorithm.
1da177e4 777 */
f2ecc5e4 778STATIC int
6dd8638e 779xfs_dialloc_ag_inobt(
f2ecc5e4
CH
780 struct xfs_trans *tp,
781 struct xfs_buf *agbp,
782 xfs_ino_t parent,
783 xfs_ino_t *inop)
1da177e4 784{
f2ecc5e4
CH
785 struct xfs_mount *mp = tp->t_mountp;
786 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
787 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
788 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
789 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
790 struct xfs_perag *pag;
791 struct xfs_btree_cur *cur, *tcur;
792 struct xfs_inobt_rec_incore rec, trec;
793 xfs_ino_t ino;
794 int error;
795 int offset;
796 int i, j;
1da177e4 797
44b56e0a 798 pag = xfs_perag_get(mp, agno);
bd169565 799
4bb61069
CH
800 ASSERT(pag->pagi_init);
801 ASSERT(pag->pagi_inodeok);
802 ASSERT(pag->pagi_freecount > 0);
803
bd169565 804 restart_pagno:
57bd3dbe 805 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1da177e4
LT
806 /*
807 * If pagino is 0 (this is the root inode allocation) use newino.
808 * This must work because we've just allocated some.
809 */
810 if (!pagino)
16259e7d 811 pagino = be32_to_cpu(agi->agi_newino);
1da177e4 812
0b48db80
DC
813 error = xfs_check_agi_freecount(cur, agi);
814 if (error)
815 goto error0;
1da177e4 816
1da177e4 817 /*
4254b0bb 818 * If in the same AG as the parent, try to get near the parent.
1da177e4
LT
819 */
820 if (pagno == agno) {
4254b0bb
CH
821 int doneleft; /* done, to the left */
822 int doneright; /* done, to the right */
bd169565 823 int searchdistance = 10;
4254b0bb 824
21875505 825 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
4254b0bb 826 if (error)
1da177e4 827 goto error0;
c29aad41 828 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
4254b0bb
CH
829
830 error = xfs_inobt_get_rec(cur, &rec, &j);
831 if (error)
832 goto error0;
c29aad41 833 XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
4254b0bb
CH
834
835 if (rec.ir_freecount > 0) {
1da177e4
LT
836 /*
837 * Found a free inode in the same chunk
4254b0bb 838 * as the parent, done.
1da177e4 839 */
4254b0bb 840 goto alloc_inode;
1da177e4 841 }
4254b0bb
CH
842
843
1da177e4 844 /*
4254b0bb 845 * In the same AG as parent, but parent's chunk is full.
1da177e4 846 */
1da177e4 847
4254b0bb
CH
848 /* duplicate the cursor, search left & right simultaneously */
849 error = xfs_btree_dup_cursor(cur, &tcur);
850 if (error)
851 goto error0;
852
bd169565
DC
853 /*
854 * Skip to last blocks looked up if same parent inode.
855 */
856 if (pagino != NULLAGINO &&
857 pag->pagl_pagino == pagino &&
858 pag->pagl_leftrec != NULLAGINO &&
859 pag->pagl_rightrec != NULLAGINO) {
860 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
43df2ee6 861 &trec, &doneleft);
bd169565
DC
862 if (error)
863 goto error1;
4254b0bb 864
bd169565 865 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
43df2ee6 866 &rec, &doneright);
bd169565
DC
867 if (error)
868 goto error1;
869 } else {
870 /* search left with tcur, back up 1 record */
871 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
872 if (error)
873 goto error1;
874
875 /* search right with cur, go forward 1 record. */
876 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
877 if (error)
878 goto error1;
879 }
4254b0bb
CH
880
881 /*
882 * Loop until we find an inode chunk with a free inode.
883 */
884 while (!doneleft || !doneright) {
885 int useleft; /* using left inode chunk this time */
886
bd169565
DC
887 if (!--searchdistance) {
888 /*
889 * Not in range - save last search
890 * location and allocate a new inode
891 */
3b826386 892 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
bd169565
DC
893 pag->pagl_leftrec = trec.ir_startino;
894 pag->pagl_rightrec = rec.ir_startino;
895 pag->pagl_pagino = pagino;
896 goto newino;
897 }
898
4254b0bb
CH
899 /* figure out the closer block if both are valid. */
900 if (!doneleft && !doneright) {
901 useleft = pagino -
902 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
903 rec.ir_startino - pagino;
904 } else {
905 useleft = !doneleft;
1da177e4 906 }
4254b0bb
CH
907
908 /* free inodes to the left? */
909 if (useleft && trec.ir_freecount) {
910 rec = trec;
911 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
912 cur = tcur;
bd169565
DC
913
914 pag->pagl_leftrec = trec.ir_startino;
915 pag->pagl_rightrec = rec.ir_startino;
916 pag->pagl_pagino = pagino;
4254b0bb 917 goto alloc_inode;
1da177e4 918 }
1da177e4 919
4254b0bb
CH
920 /* free inodes to the right? */
921 if (!useleft && rec.ir_freecount) {
922 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
bd169565
DC
923
924 pag->pagl_leftrec = trec.ir_startino;
925 pag->pagl_rightrec = rec.ir_startino;
926 pag->pagl_pagino = pagino;
4254b0bb 927 goto alloc_inode;
1da177e4 928 }
4254b0bb
CH
929
930 /* get next record to check */
931 if (useleft) {
932 error = xfs_ialloc_next_rec(tcur, &trec,
933 &doneleft, 1);
934 } else {
935 error = xfs_ialloc_next_rec(cur, &rec,
936 &doneright, 0);
937 }
938 if (error)
939 goto error1;
1da177e4 940 }
bd169565
DC
941
942 /*
943 * We've reached the end of the btree. because
944 * we are only searching a small chunk of the
945 * btree each search, there is obviously free
946 * inodes closer to the parent inode than we
947 * are now. restart the search again.
948 */
949 pag->pagl_pagino = NULLAGINO;
950 pag->pagl_leftrec = NULLAGINO;
951 pag->pagl_rightrec = NULLAGINO;
952 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
953 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
954 goto restart_pagno;
1da177e4 955 }
4254b0bb 956
1da177e4 957 /*
4254b0bb 958 * In a different AG from the parent.
1da177e4
LT
959 * See if the most recently allocated block has any free.
960 */
bd169565 961newino:
69ef921b 962 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
21875505
CH
963 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
964 XFS_LOOKUP_EQ, &i);
4254b0bb 965 if (error)
1da177e4 966 goto error0;
4254b0bb
CH
967
968 if (i == 1) {
969 error = xfs_inobt_get_rec(cur, &rec, &j);
970 if (error)
971 goto error0;
972
973 if (j == 1 && rec.ir_freecount > 0) {
974 /*
975 * The last chunk allocated in the group
976 * still has a free inode.
977 */
978 goto alloc_inode;
979 }
1da177e4 980 }
bd169565 981 }
4254b0bb 982
bd169565
DC
983 /*
984 * None left in the last group, search the whole AG
985 */
986 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
987 if (error)
988 goto error0;
c29aad41 989 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
bd169565
DC
990
991 for (;;) {
992 error = xfs_inobt_get_rec(cur, &rec, &i);
993 if (error)
994 goto error0;
c29aad41 995 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
bd169565
DC
996 if (rec.ir_freecount > 0)
997 break;
998 error = xfs_btree_increment(cur, 0, &i);
4254b0bb
CH
999 if (error)
1000 goto error0;
c29aad41 1001 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1da177e4 1002 }
4254b0bb
CH
1003
1004alloc_inode:
d4cc540b 1005 offset = xfs_inobt_first_free_inode(&rec);
1da177e4
LT
1006 ASSERT(offset >= 0);
1007 ASSERT(offset < XFS_INODES_PER_CHUNK);
1008 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1009 XFS_INODES_PER_CHUNK) == 0);
1010 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
0d87e656 1011 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1da177e4 1012 rec.ir_freecount--;
afabc24a
CH
1013 error = xfs_inobt_update(cur, &rec);
1014 if (error)
1da177e4 1015 goto error0;
413d57c9 1016 be32_add_cpu(&agi->agi_freecount, -1);
1da177e4 1017 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
44b56e0a 1018 pag->pagi_freecount--;
1da177e4 1019
0b48db80
DC
1020 error = xfs_check_agi_freecount(cur, agi);
1021 if (error)
1022 goto error0;
1023
1da177e4
LT
1024 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1025 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
44b56e0a 1026 xfs_perag_put(pag);
1da177e4
LT
1027 *inop = ino;
1028 return 0;
1029error1:
1030 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1031error0:
1032 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
44b56e0a 1033 xfs_perag_put(pag);
1da177e4
LT
1034 return error;
1035}
1036
6dd8638e
BF
1037/*
1038 * Use the free inode btree to allocate an inode based on distance from the
1039 * parent. Note that the provided cursor may be deleted and replaced.
1040 */
1041STATIC int
1042xfs_dialloc_ag_finobt_near(
1043 xfs_agino_t pagino,
1044 struct xfs_btree_cur **ocur,
1045 struct xfs_inobt_rec_incore *rec)
1046{
1047 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
1048 struct xfs_btree_cur *rcur; /* right search cursor */
1049 struct xfs_inobt_rec_incore rrec;
1050 int error;
1051 int i, j;
1052
1053 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1054 if (error)
1055 return error;
1056
1057 if (i == 1) {
1058 error = xfs_inobt_get_rec(lcur, rec, &i);
1059 if (error)
1060 return error;
5fb5aeee 1061 XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
6dd8638e
BF
1062
1063 /*
1064 * See if we've landed in the parent inode record. The finobt
1065 * only tracks chunks with at least one free inode, so record
1066 * existence is enough.
1067 */
1068 if (pagino >= rec->ir_startino &&
1069 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1070 return 0;
1071 }
1072
1073 error = xfs_btree_dup_cursor(lcur, &rcur);
1074 if (error)
1075 return error;
1076
1077 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1078 if (error)
1079 goto error_rcur;
1080 if (j == 1) {
1081 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1082 if (error)
1083 goto error_rcur;
c29aad41 1084 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
6dd8638e
BF
1085 }
1086
c29aad41 1087 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
6dd8638e
BF
1088 if (i == 1 && j == 1) {
1089 /*
1090 * Both the left and right records are valid. Choose the closer
1091 * inode chunk to the target.
1092 */
1093 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1094 (rrec.ir_startino - pagino)) {
1095 *rec = rrec;
1096 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1097 *ocur = rcur;
1098 } else {
1099 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1100 }
1101 } else if (j == 1) {
1102 /* only the right record is valid */
1103 *rec = rrec;
1104 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1105 *ocur = rcur;
1106 } else if (i == 1) {
1107 /* only the left record is valid */
1108 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1109 }
1110
1111 return 0;
1112
1113error_rcur:
1114 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1115 return error;
1116}
1117
1118/*
1119 * Use the free inode btree to find a free inode based on a newino hint. If
1120 * the hint is NULL, find the first free inode in the AG.
1121 */
1122STATIC int
1123xfs_dialloc_ag_finobt_newino(
1124 struct xfs_agi *agi,
1125 struct xfs_btree_cur *cur,
1126 struct xfs_inobt_rec_incore *rec)
1127{
1128 int error;
1129 int i;
1130
1131 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
e68ed775
DC
1132 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1133 XFS_LOOKUP_EQ, &i);
6dd8638e
BF
1134 if (error)
1135 return error;
1136 if (i == 1) {
1137 error = xfs_inobt_get_rec(cur, rec, &i);
1138 if (error)
1139 return error;
5fb5aeee 1140 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
6dd8638e
BF
1141 return 0;
1142 }
1143 }
1144
1145 /*
1146 * Find the first inode available in the AG.
1147 */
1148 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1149 if (error)
1150 return error;
5fb5aeee 1151 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
6dd8638e
BF
1152
1153 error = xfs_inobt_get_rec(cur, rec, &i);
1154 if (error)
1155 return error;
5fb5aeee 1156 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
6dd8638e
BF
1157
1158 return 0;
1159}
1160
1161/*
1162 * Update the inobt based on a modification made to the finobt. Also ensure that
1163 * the records from both trees are equivalent post-modification.
1164 */
1165STATIC int
1166xfs_dialloc_ag_update_inobt(
1167 struct xfs_btree_cur *cur, /* inobt cursor */
1168 struct xfs_inobt_rec_incore *frec, /* finobt record */
1169 int offset) /* inode offset */
1170{
1171 struct xfs_inobt_rec_incore rec;
1172 int error;
1173 int i;
1174
1175 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1176 if (error)
1177 return error;
5fb5aeee 1178 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
6dd8638e
BF
1179
1180 error = xfs_inobt_get_rec(cur, &rec, &i);
1181 if (error)
1182 return error;
5fb5aeee 1183 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
6dd8638e
BF
1184 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1185 XFS_INODES_PER_CHUNK) == 0);
1186
1187 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1188 rec.ir_freecount--;
1189
5fb5aeee 1190 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
6dd8638e
BF
1191 (rec.ir_freecount == frec->ir_freecount));
1192
b72091f2 1193 return xfs_inobt_update(cur, &rec);
6dd8638e
BF
1194}
1195
1196/*
1197 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1198 * back to the inobt search algorithm.
1199 *
1200 * The caller selected an AG for us, and made sure that free inodes are
1201 * available.
1202 */
1203STATIC int
1204xfs_dialloc_ag(
1205 struct xfs_trans *tp,
1206 struct xfs_buf *agbp,
1207 xfs_ino_t parent,
1208 xfs_ino_t *inop)
1209{
1210 struct xfs_mount *mp = tp->t_mountp;
1211 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1212 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1213 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1214 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1215 struct xfs_perag *pag;
1216 struct xfs_btree_cur *cur; /* finobt cursor */
1217 struct xfs_btree_cur *icur; /* inobt cursor */
1218 struct xfs_inobt_rec_incore rec;
1219 xfs_ino_t ino;
1220 int error;
1221 int offset;
1222 int i;
1223
1224 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
1225 return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
1226
1227 pag = xfs_perag_get(mp, agno);
1228
1229 /*
1230 * If pagino is 0 (this is the root inode allocation) use newino.
1231 * This must work because we've just allocated some.
1232 */
1233 if (!pagino)
1234 pagino = be32_to_cpu(agi->agi_newino);
1235
1236 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1237
1238 error = xfs_check_agi_freecount(cur, agi);
1239 if (error)
1240 goto error_cur;
1241
1242 /*
1243 * The search algorithm depends on whether we're in the same AG as the
1244 * parent. If so, find the closest available inode to the parent. If
1245 * not, consider the agi hint or find the first free inode in the AG.
1246 */
1247 if (agno == pagno)
1248 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1249 else
1250 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1251 if (error)
1252 goto error_cur;
1253
d4cc540b 1254 offset = xfs_inobt_first_free_inode(&rec);
6dd8638e
BF
1255 ASSERT(offset >= 0);
1256 ASSERT(offset < XFS_INODES_PER_CHUNK);
1257 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1258 XFS_INODES_PER_CHUNK) == 0);
1259 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1260
1261 /*
1262 * Modify or remove the finobt record.
1263 */
1264 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1265 rec.ir_freecount--;
1266 if (rec.ir_freecount)
1267 error = xfs_inobt_update(cur, &rec);
1268 else
1269 error = xfs_btree_delete(cur, &i);
1270 if (error)
1271 goto error_cur;
1272
1273 /*
1274 * The finobt has now been updated appropriately. We haven't updated the
1275 * agi and superblock yet, so we can create an inobt cursor and validate
1276 * the original freecount. If all is well, make the equivalent update to
1277 * the inobt using the finobt record and offset information.
1278 */
1279 icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1280
1281 error = xfs_check_agi_freecount(icur, agi);
1282 if (error)
1283 goto error_icur;
1284
1285 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
1286 if (error)
1287 goto error_icur;
1288
1289 /*
1290 * Both trees have now been updated. We must update the perag and
1291 * superblock before we can check the freecount for each btree.
1292 */
1293 be32_add_cpu(&agi->agi_freecount, -1);
1294 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1295 pag->pagi_freecount--;
1296
1297 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1298
1299 error = xfs_check_agi_freecount(icur, agi);
1300 if (error)
1301 goto error_icur;
1302 error = xfs_check_agi_freecount(cur, agi);
1303 if (error)
1304 goto error_icur;
1305
1306 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
1307 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1308 xfs_perag_put(pag);
1309 *inop = ino;
1310 return 0;
1311
1312error_icur:
1313 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
1314error_cur:
1315 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1316 xfs_perag_put(pag);
1317 return error;
1318}
1319
f2ecc5e4
CH
1320/*
1321 * Allocate an inode on disk.
1322 *
1323 * Mode is used to tell whether the new inode will need space, and whether it
1324 * is a directory.
1325 *
1326 * This function is designed to be called twice if it has to do an allocation
1327 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1328 * If an inode is available without having to performn an allocation, an inode
cd856db6
CM
1329 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1330 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1331 * The caller should then commit the current transaction, allocate a
f2ecc5e4
CH
1332 * new transaction, and call xfs_dialloc() again, passing in the previous value
1333 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1334 * buffer is locked across the two calls, the second call is guaranteed to have
1335 * a free inode available.
1336 *
1337 * Once we successfully pick an inode its number is returned and the on-disk
1338 * data structures are updated. The inode itself is not read in, since doing so
1339 * would break ordering constraints with xfs_reclaim.
1340 */
1341int
1342xfs_dialloc(
1343 struct xfs_trans *tp,
1344 xfs_ino_t parent,
1345 umode_t mode,
1346 int okalloc,
1347 struct xfs_buf **IO_agbp,
f2ecc5e4
CH
1348 xfs_ino_t *inop)
1349{
55d6af64 1350 struct xfs_mount *mp = tp->t_mountp;
f2ecc5e4
CH
1351 struct xfs_buf *agbp;
1352 xfs_agnumber_t agno;
f2ecc5e4
CH
1353 int error;
1354 int ialloced;
1355 int noroom = 0;
be60fe54 1356 xfs_agnumber_t start_agno;
f2ecc5e4
CH
1357 struct xfs_perag *pag;
1358
4bb61069 1359 if (*IO_agbp) {
f2ecc5e4 1360 /*
4bb61069
CH
1361 * If the caller passes in a pointer to the AGI buffer,
1362 * continue where we left off before. In this case, we
f2ecc5e4
CH
1363 * know that the allocation group has free inodes.
1364 */
1365 agbp = *IO_agbp;
4bb61069 1366 goto out_alloc;
f2ecc5e4 1367 }
4bb61069
CH
1368
1369 /*
1370 * We do not have an agbp, so select an initial allocation
1371 * group for inode allocation.
1372 */
be60fe54
CH
1373 start_agno = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
1374 if (start_agno == NULLAGNUMBER) {
4bb61069
CH
1375 *inop = NULLFSINO;
1376 return 0;
1377 }
55d6af64 1378
f2ecc5e4
CH
1379 /*
1380 * If we have already hit the ceiling of inode blocks then clear
1381 * okalloc so we scan all available agi structures for a free
1382 * inode.
1383 */
f2ecc5e4 1384 if (mp->m_maxicount &&
501ab323
DC
1385 percpu_counter_read(&mp->m_icount) + mp->m_ialloc_inos >
1386 mp->m_maxicount) {
f2ecc5e4
CH
1387 noroom = 1;
1388 okalloc = 0;
1389 }
1390
1391 /*
1392 * Loop until we find an allocation group that either has free inodes
1393 * or in which we can allocate some inodes. Iterate through the
1394 * allocation groups upward, wrapping at the end.
1395 */
be60fe54
CH
1396 agno = start_agno;
1397 for (;;) {
1398 pag = xfs_perag_get(mp, agno);
1399 if (!pag->pagi_inodeok) {
1400 xfs_ialloc_next_ag(mp);
1401 goto nextag;
1402 }
1403
1404 if (!pag->pagi_init) {
1405 error = xfs_ialloc_pagi_init(mp, tp, agno);
1406 if (error)
1407 goto out_error;
f2ecc5e4 1408 }
be60fe54 1409
f2ecc5e4 1410 /*
be60fe54 1411 * Do a first racy fast path check if this AG is usable.
f2ecc5e4 1412 */
be60fe54
CH
1413 if (!pag->pagi_freecount && !okalloc)
1414 goto nextag;
1415
c4982110
CH
1416 /*
1417 * Then read in the AGI buffer and recheck with the AGI buffer
1418 * lock held.
1419 */
be60fe54
CH
1420 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1421 if (error)
1422 goto out_error;
1423
be60fe54
CH
1424 if (pag->pagi_freecount) {
1425 xfs_perag_put(pag);
1426 goto out_alloc;
1427 }
1428
c4982110
CH
1429 if (!okalloc)
1430 goto nextag_relse_buffer;
1431
be60fe54
CH
1432
1433 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1434 if (error) {
1435 xfs_trans_brelse(tp, agbp);
1436
2451337d 1437 if (error != -ENOSPC)
be60fe54
CH
1438 goto out_error;
1439
1440 xfs_perag_put(pag);
f2ecc5e4 1441 *inop = NULLFSINO;
be60fe54 1442 return 0;
f2ecc5e4 1443 }
be60fe54
CH
1444
1445 if (ialloced) {
1446 /*
1447 * We successfully allocated some inodes, return
1448 * the current context to the caller so that it
1449 * can commit the current transaction and call
1450 * us again where we left off.
1451 */
1452 ASSERT(pag->pagi_freecount > 0);
f2ecc5e4 1453 xfs_perag_put(pag);
be60fe54
CH
1454
1455 *IO_agbp = agbp;
1456 *inop = NULLFSINO;
1457 return 0;
f2ecc5e4 1458 }
be60fe54 1459
c4982110
CH
1460nextag_relse_buffer:
1461 xfs_trans_brelse(tp, agbp);
be60fe54 1462nextag:
f2ecc5e4 1463 xfs_perag_put(pag);
be60fe54
CH
1464 if (++agno == mp->m_sb.sb_agcount)
1465 agno = 0;
1466 if (agno == start_agno) {
1467 *inop = NULLFSINO;
2451337d 1468 return noroom ? -ENOSPC : 0;
be60fe54 1469 }
f2ecc5e4
CH
1470 }
1471
4bb61069 1472out_alloc:
f2ecc5e4
CH
1473 *IO_agbp = NULL;
1474 return xfs_dialloc_ag(tp, agbp, parent, inop);
be60fe54
CH
1475out_error:
1476 xfs_perag_put(pag);
b474c7ae 1477 return error;
f2ecc5e4
CH
1478}
1479
2b64ee5c
BF
1480STATIC int
1481xfs_difree_inobt(
1482 struct xfs_mount *mp,
1483 struct xfs_trans *tp,
1484 struct xfs_buf *agbp,
1485 xfs_agino_t agino,
1486 struct xfs_bmap_free *flist,
0d907a3b 1487 int *deleted,
2b64ee5c
BF
1488 xfs_ino_t *first_ino,
1489 struct xfs_inobt_rec_incore *orec)
1da177e4 1490{
2b64ee5c
BF
1491 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1492 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1493 struct xfs_perag *pag;
1494 struct xfs_btree_cur *cur;
1495 struct xfs_inobt_rec_incore rec;
1496 int ilen;
1497 int error;
1498 int i;
1499 int off;
1da177e4 1500
69ef921b 1501 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
2b64ee5c
BF
1502 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1503
1da177e4
LT
1504 /*
1505 * Initialize the cursor.
1506 */
57bd3dbe 1507 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1da177e4 1508
0b48db80
DC
1509 error = xfs_check_agi_freecount(cur, agi);
1510 if (error)
1511 goto error0;
1512
1da177e4
LT
1513 /*
1514 * Look for the entry describing this inode.
1515 */
21875505 1516 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
0b932ccc
DC
1517 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1518 __func__, error);
1da177e4
LT
1519 goto error0;
1520 }
c29aad41 1521 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
2e287a73
CH
1522 error = xfs_inobt_get_rec(cur, &rec, &i);
1523 if (error) {
0b932ccc
DC
1524 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1525 __func__, error);
1da177e4
LT
1526 goto error0;
1527 }
c29aad41 1528 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
1da177e4
LT
1529 /*
1530 * Get the offset in the inode chunk.
1531 */
1532 off = agino - rec.ir_startino;
1533 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
0d87e656 1534 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1da177e4
LT
1535 /*
1536 * Mark the inode free & increment the count.
1537 */
0d87e656 1538 rec.ir_free |= XFS_INOBT_MASK(off);
1da177e4
LT
1539 rec.ir_freecount++;
1540
1541 /*
999633d3
BF
1542 * When an inode chunk is free, it becomes eligible for removal. Don't
1543 * remove the chunk if the block size is large enough for multiple inode
1544 * chunks (that might not be free).
1da177e4 1545 */
1bd960ee 1546 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
999633d3
BF
1547 rec.ir_free == XFS_INOBT_ALL_FREE &&
1548 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
1da177e4 1549
376c2f3a 1550 *deleted = 1;
1da177e4
LT
1551 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1552
1553 /*
1554 * Remove the inode cluster from the AGI B+Tree, adjust the
1555 * AGI and Superblock inode counts, and mark the disk space
1556 * to be freed when the transaction is committed.
1557 */
999633d3 1558 ilen = rec.ir_freecount;
413d57c9
MS
1559 be32_add_cpu(&agi->agi_count, -ilen);
1560 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1da177e4 1561 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
44b56e0a
DC
1562 pag = xfs_perag_get(mp, agno);
1563 pag->pagi_freecount -= ilen - 1;
1564 xfs_perag_put(pag);
1da177e4
LT
1565 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1566 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1567
91cca5df 1568 if ((error = xfs_btree_delete(cur, &i))) {
0b932ccc
DC
1569 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1570 __func__, error);
1da177e4
LT
1571 goto error0;
1572 }
1573
126cd105
JL
1574 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp, agno,
1575 XFS_AGINO_TO_AGBNO(mp, rec.ir_startino)),
1576 mp->m_ialloc_blks, flist, mp);
1da177e4 1577 } else {
376c2f3a 1578 *deleted = 0;
1da177e4 1579
afabc24a
CH
1580 error = xfs_inobt_update(cur, &rec);
1581 if (error) {
0b932ccc
DC
1582 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
1583 __func__, error);
1da177e4
LT
1584 goto error0;
1585 }
afabc24a 1586
1da177e4
LT
1587 /*
1588 * Change the inode free counts and log the ag/sb changes.
1589 */
413d57c9 1590 be32_add_cpu(&agi->agi_freecount, 1);
1da177e4 1591 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
44b56e0a
DC
1592 pag = xfs_perag_get(mp, agno);
1593 pag->pagi_freecount++;
1594 xfs_perag_put(pag);
1da177e4
LT
1595 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1596 }
1597
0b48db80
DC
1598 error = xfs_check_agi_freecount(cur, agi);
1599 if (error)
1600 goto error0;
1da177e4 1601
2b64ee5c 1602 *orec = rec;
1da177e4
LT
1603 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1604 return 0;
1605
1606error0:
1607 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1608 return error;
1609}
1610
3efa4ffd
BF
1611/*
1612 * Free an inode in the free inode btree.
1613 */
1614STATIC int
1615xfs_difree_finobt(
1616 struct xfs_mount *mp,
1617 struct xfs_trans *tp,
1618 struct xfs_buf *agbp,
1619 xfs_agino_t agino,
1620 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
1621{
1622 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1623 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1624 struct xfs_btree_cur *cur;
1625 struct xfs_inobt_rec_incore rec;
1626 int offset = agino - ibtrec->ir_startino;
1627 int error;
1628 int i;
1629
1630 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1631
1632 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
1633 if (error)
1634 goto error;
1635 if (i == 0) {
1636 /*
1637 * If the record does not exist in the finobt, we must have just
1638 * freed an inode in a previously fully allocated chunk. If not,
1639 * something is out of sync.
1640 */
c29aad41 1641 XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
3efa4ffd 1642
5419040f
BF
1643 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
1644 ibtrec->ir_count,
1645 ibtrec->ir_freecount,
3efa4ffd
BF
1646 ibtrec->ir_free, &i);
1647 if (error)
1648 goto error;
1649 ASSERT(i == 1);
1650
1651 goto out;
1652 }
1653
1654 /*
1655 * Read and update the existing record. We could just copy the ibtrec
1656 * across here, but that would defeat the purpose of having redundant
1657 * metadata. By making the modifications independently, we can catch
1658 * corruptions that we wouldn't see if we just copied from one record
1659 * to another.
1660 */
1661 error = xfs_inobt_get_rec(cur, &rec, &i);
1662 if (error)
1663 goto error;
c29aad41 1664 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
3efa4ffd
BF
1665
1666 rec.ir_free |= XFS_INOBT_MASK(offset);
1667 rec.ir_freecount++;
1668
c29aad41 1669 XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
3efa4ffd
BF
1670 (rec.ir_freecount == ibtrec->ir_freecount),
1671 error);
1672
1673 /*
1674 * The content of inobt records should always match between the inobt
1675 * and finobt. The lifecycle of records in the finobt is different from
1676 * the inobt in that the finobt only tracks records with at least one
1677 * free inode. Hence, if all of the inodes are free and we aren't
1678 * keeping inode chunks permanently on disk, remove the record.
1679 * Otherwise, update the record with the new information.
999633d3
BF
1680 *
1681 * Note that we currently can't free chunks when the block size is large
1682 * enough for multiple chunks. Leave the finobt record to remain in sync
1683 * with the inobt.
3efa4ffd 1684 */
999633d3
BF
1685 if (rec.ir_free == XFS_INOBT_ALL_FREE &&
1686 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
3efa4ffd
BF
1687 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
1688 error = xfs_btree_delete(cur, &i);
1689 if (error)
1690 goto error;
1691 ASSERT(i == 1);
1692 } else {
1693 error = xfs_inobt_update(cur, &rec);
1694 if (error)
1695 goto error;
1696 }
1697
1698out:
1699 error = xfs_check_agi_freecount(cur, agi);
1700 if (error)
1701 goto error;
1702
1703 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1704 return 0;
1705
1706error:
1707 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1708 return error;
1709}
1710
2b64ee5c
BF
1711/*
1712 * Free disk inode. Carefully avoids touching the incore inode, all
1713 * manipulations incore are the caller's responsibility.
1714 * The on-disk inode is not changed by this operation, only the
1715 * btree (free inode mask) is changed.
1716 */
1717int
1718xfs_difree(
1719 struct xfs_trans *tp, /* transaction pointer */
1720 xfs_ino_t inode, /* inode to be freed */
1721 struct xfs_bmap_free *flist, /* extents to free */
0d907a3b 1722 int *deleted,/* set if inode cluster was deleted */
2b64ee5c
BF
1723 xfs_ino_t *first_ino)/* first inode in deleted cluster */
1724{
1725 /* REFERENCED */
1726 xfs_agblock_t agbno; /* block number containing inode */
1727 struct xfs_buf *agbp; /* buffer for allocation group header */
1728 xfs_agino_t agino; /* allocation group inode number */
1729 xfs_agnumber_t agno; /* allocation group number */
1730 int error; /* error return value */
1731 struct xfs_mount *mp; /* mount structure for filesystem */
1732 struct xfs_inobt_rec_incore rec;/* btree record */
1733
1734 mp = tp->t_mountp;
1735
1736 /*
1737 * Break up inode number into its components.
1738 */
1739 agno = XFS_INO_TO_AGNO(mp, inode);
1740 if (agno >= mp->m_sb.sb_agcount) {
1741 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1742 __func__, agno, mp->m_sb.sb_agcount);
1743 ASSERT(0);
2451337d 1744 return -EINVAL;
2b64ee5c
BF
1745 }
1746 agino = XFS_INO_TO_AGINO(mp, inode);
1747 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1748 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1749 __func__, (unsigned long long)inode,
1750 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
1751 ASSERT(0);
2451337d 1752 return -EINVAL;
2b64ee5c
BF
1753 }
1754 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1755 if (agbno >= mp->m_sb.sb_agblocks) {
1756 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1757 __func__, agbno, mp->m_sb.sb_agblocks);
1758 ASSERT(0);
2451337d 1759 return -EINVAL;
2b64ee5c
BF
1760 }
1761 /*
1762 * Get the allocation group header.
1763 */
1764 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1765 if (error) {
1766 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
1767 __func__, error);
1768 return error;
1769 }
1770
1771 /*
1772 * Fix up the inode allocation btree.
1773 */
0d907a3b 1774 error = xfs_difree_inobt(mp, tp, agbp, agino, flist, deleted, first_ino,
2b64ee5c
BF
1775 &rec);
1776 if (error)
1777 goto error0;
1778
3efa4ffd
BF
1779 /*
1780 * Fix up the free inode btree.
1781 */
1782 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
1783 error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
1784 if (error)
1785 goto error0;
1786 }
1787
2b64ee5c
BF
1788 return 0;
1789
1790error0:
1791 return error;
1792}
1793
7124fe0a
DC
1794STATIC int
1795xfs_imap_lookup(
1796 struct xfs_mount *mp,
1797 struct xfs_trans *tp,
1798 xfs_agnumber_t agno,
1799 xfs_agino_t agino,
1800 xfs_agblock_t agbno,
1801 xfs_agblock_t *chunk_agbno,
1802 xfs_agblock_t *offset_agbno,
1803 int flags)
1804{
1805 struct xfs_inobt_rec_incore rec;
1806 struct xfs_btree_cur *cur;
1807 struct xfs_buf *agbp;
7124fe0a
DC
1808 int error;
1809 int i;
1810
1811 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1812 if (error) {
53487786
DC
1813 xfs_alert(mp,
1814 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1815 __func__, error, agno);
7124fe0a
DC
1816 return error;
1817 }
1818
1819 /*
4536f2ad
DC
1820 * Lookup the inode record for the given agino. If the record cannot be
1821 * found, then it's an invalid inode number and we should abort. Once
1822 * we have a record, we need to ensure it contains the inode number
1823 * we are looking up.
7124fe0a 1824 */
57bd3dbe 1825 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
4536f2ad 1826 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
7124fe0a
DC
1827 if (!error) {
1828 if (i)
1829 error = xfs_inobt_get_rec(cur, &rec, &i);
1830 if (!error && i == 0)
2451337d 1831 error = -EINVAL;
7124fe0a
DC
1832 }
1833
1834 xfs_trans_brelse(tp, agbp);
1835 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1836 if (error)
1837 return error;
1838
4536f2ad
DC
1839 /* check that the returned record contains the required inode */
1840 if (rec.ir_startino > agino ||
71783438 1841 rec.ir_startino + mp->m_ialloc_inos <= agino)
2451337d 1842 return -EINVAL;
4536f2ad 1843
7124fe0a 1844 /* for untrusted inodes check it is allocated first */
1920779e 1845 if ((flags & XFS_IGET_UNTRUSTED) &&
7124fe0a 1846 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
2451337d 1847 return -EINVAL;
7124fe0a
DC
1848
1849 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
1850 *offset_agbno = agbno - *chunk_agbno;
1851 return 0;
1852}
1853
1da177e4 1854/*
94e1b69d 1855 * Return the location of the inode in imap, for mapping it into a buffer.
1da177e4 1856 */
1da177e4 1857int
94e1b69d
CH
1858xfs_imap(
1859 xfs_mount_t *mp, /* file system mount structure */
1860 xfs_trans_t *tp, /* transaction pointer */
1da177e4 1861 xfs_ino_t ino, /* inode to locate */
94e1b69d
CH
1862 struct xfs_imap *imap, /* location map structure */
1863 uint flags) /* flags for inode btree lookup */
1da177e4
LT
1864{
1865 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1da177e4
LT
1866 xfs_agino_t agino; /* inode number within alloc group */
1867 xfs_agnumber_t agno; /* allocation group number */
1868 int blks_per_cluster; /* num blocks per inode cluster */
1869 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1da177e4 1870 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1da177e4 1871 int error; /* error code */
1da177e4 1872 int offset; /* index of inode in its buffer */
836a94ad 1873 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
1da177e4
LT
1874
1875 ASSERT(ino != NULLFSINO);
94e1b69d 1876
1da177e4
LT
1877 /*
1878 * Split up the inode number into its parts.
1879 */
1880 agno = XFS_INO_TO_AGNO(mp, ino);
1881 agino = XFS_INO_TO_AGINO(mp, ino);
1882 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1883 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1884 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1885#ifdef DEBUG
1920779e
DC
1886 /*
1887 * Don't output diagnostic information for untrusted inodes
1888 * as they can be invalid without implying corruption.
1889 */
1890 if (flags & XFS_IGET_UNTRUSTED)
2451337d 1891 return -EINVAL;
1da177e4 1892 if (agno >= mp->m_sb.sb_agcount) {
53487786
DC
1893 xfs_alert(mp,
1894 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1895 __func__, agno, mp->m_sb.sb_agcount);
1da177e4
LT
1896 }
1897 if (agbno >= mp->m_sb.sb_agblocks) {
53487786
DC
1898 xfs_alert(mp,
1899 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1900 __func__, (unsigned long long)agbno,
1901 (unsigned long)mp->m_sb.sb_agblocks);
1da177e4
LT
1902 }
1903 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
53487786
DC
1904 xfs_alert(mp,
1905 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1906 __func__, ino,
1907 XFS_AGINO_TO_INO(mp, agno, agino));
1da177e4 1908 }
745b1f47 1909 xfs_stack_trace();
1da177e4 1910#endif /* DEBUG */
2451337d 1911 return -EINVAL;
1da177e4 1912 }
94e1b69d 1913
f9e5abcf 1914 blks_per_cluster = xfs_icluster_size_fsb(mp);
7124fe0a
DC
1915
1916 /*
1917 * For bulkstat and handle lookups, we have an untrusted inode number
1918 * that we have to verify is valid. We cannot do this just by reading
1919 * the inode buffer as it may have been unlinked and removed leaving
1920 * inodes in stale state on disk. Hence we have to do a btree lookup
1921 * in all cases where an untrusted inode number is passed.
1922 */
1920779e 1923 if (flags & XFS_IGET_UNTRUSTED) {
7124fe0a
DC
1924 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1925 &chunk_agbno, &offset_agbno, flags);
1926 if (error)
1927 return error;
1928 goto out_map;
1929 }
1930
94e1b69d
CH
1931 /*
1932 * If the inode cluster size is the same as the blocksize or
1933 * smaller we get to the buffer by simple arithmetics.
1934 */
f9e5abcf 1935 if (blks_per_cluster == 1) {
1da177e4
LT
1936 offset = XFS_INO_TO_OFFSET(mp, ino);
1937 ASSERT(offset < mp->m_sb.sb_inopblock);
94e1b69d
CH
1938
1939 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1940 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1941 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1da177e4
LT
1942 return 0;
1943 }
94e1b69d 1944
94e1b69d
CH
1945 /*
1946 * If the inode chunks are aligned then use simple maths to
1947 * find the location. Otherwise we have to do a btree
1948 * lookup to find the location.
1949 */
1da177e4
LT
1950 if (mp->m_inoalign_mask) {
1951 offset_agbno = agbno & mp->m_inoalign_mask;
1952 chunk_agbno = agbno - offset_agbno;
1953 } else {
7124fe0a
DC
1954 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
1955 &chunk_agbno, &offset_agbno, flags);
1da177e4
LT
1956 if (error)
1957 return error;
1da177e4 1958 }
94e1b69d 1959
7124fe0a 1960out_map:
1da177e4
LT
1961 ASSERT(agbno >= chunk_agbno);
1962 cluster_agbno = chunk_agbno +
1963 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1964 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1965 XFS_INO_TO_OFFSET(mp, ino);
94e1b69d
CH
1966
1967 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1968 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1969 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1970
1971 /*
1972 * If the inode number maps to a block outside the bounds
1973 * of the file system then return NULL rather than calling
1974 * read_buf and panicing when we get an error from the
1975 * driver.
1976 */
1977 if ((imap->im_blkno + imap->im_len) >
1978 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
53487786
DC
1979 xfs_alert(mp,
1980 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1981 __func__, (unsigned long long) imap->im_blkno,
94e1b69d
CH
1982 (unsigned long long) imap->im_len,
1983 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
2451337d 1984 return -EINVAL;
94e1b69d 1985 }
1da177e4 1986 return 0;
1da177e4
LT
1987}
1988
1989/*
1990 * Compute and fill in value of m_in_maxlevels.
1991 */
1992void
1993xfs_ialloc_compute_maxlevels(
1994 xfs_mount_t *mp) /* file system mount structure */
1995{
1996 int level;
1997 uint maxblocks;
1998 uint maxleafents;
1999 int minleafrecs;
2000 int minnoderecs;
2001
2002 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
2003 XFS_INODES_PER_CHUNK_LOG;
2004 minleafrecs = mp->m_alloc_mnr[0];
2005 minnoderecs = mp->m_alloc_mnr[1];
2006 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
2007 for (level = 1; maxblocks > 1; level++)
2008 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
2009 mp->m_in_maxlevels = level;
2010}
2011
2012/*
aafc3c24
BF
2013 * Log specified fields for the ag hdr (inode section). The growth of the agi
2014 * structure over time requires that we interpret the buffer as two logical
2015 * regions delineated by the end of the unlinked list. This is due to the size
2016 * of the hash table and its location in the middle of the agi.
2017 *
2018 * For example, a request to log a field before agi_unlinked and a field after
2019 * agi_unlinked could cause us to log the entire hash table and use an excessive
2020 * amount of log space. To avoid this behavior, log the region up through
2021 * agi_unlinked in one call and the region after agi_unlinked through the end of
2022 * the structure in another.
1da177e4
LT
2023 */
2024void
2025xfs_ialloc_log_agi(
2026 xfs_trans_t *tp, /* transaction pointer */
2027 xfs_buf_t *bp, /* allocation group header buffer */
2028 int fields) /* bitmask of fields to log */
2029{
2030 int first; /* first byte number */
2031 int last; /* last byte number */
2032 static const short offsets[] = { /* field starting offsets */
2033 /* keep in sync with bit definitions */
2034 offsetof(xfs_agi_t, agi_magicnum),
2035 offsetof(xfs_agi_t, agi_versionnum),
2036 offsetof(xfs_agi_t, agi_seqno),
2037 offsetof(xfs_agi_t, agi_length),
2038 offsetof(xfs_agi_t, agi_count),
2039 offsetof(xfs_agi_t, agi_root),
2040 offsetof(xfs_agi_t, agi_level),
2041 offsetof(xfs_agi_t, agi_freecount),
2042 offsetof(xfs_agi_t, agi_newino),
2043 offsetof(xfs_agi_t, agi_dirino),
2044 offsetof(xfs_agi_t, agi_unlinked),
aafc3c24
BF
2045 offsetof(xfs_agi_t, agi_free_root),
2046 offsetof(xfs_agi_t, agi_free_level),
1da177e4
LT
2047 sizeof(xfs_agi_t)
2048 };
2049#ifdef DEBUG
2050 xfs_agi_t *agi; /* allocation group header */
2051
2052 agi = XFS_BUF_TO_AGI(bp);
69ef921b 2053 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1da177e4 2054#endif
aafc3c24
BF
2055
2056 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_AGI_BUF);
2057
1da177e4 2058 /*
aafc3c24
BF
2059 * Compute byte offsets for the first and last fields in the first
2060 * region and log the agi buffer. This only logs up through
2061 * agi_unlinked.
1da177e4 2062 */
aafc3c24
BF
2063 if (fields & XFS_AGI_ALL_BITS_R1) {
2064 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2065 &first, &last);
2066 xfs_trans_log_buf(tp, bp, first, last);
2067 }
2068
1da177e4 2069 /*
aafc3c24
BF
2070 * Mask off the bits in the first region and calculate the first and
2071 * last field offsets for any bits in the second region.
1da177e4 2072 */
aafc3c24
BF
2073 fields &= ~XFS_AGI_ALL_BITS_R1;
2074 if (fields) {
2075 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2076 &first, &last);
2077 xfs_trans_log_buf(tp, bp, first, last);
2078 }
1da177e4
LT
2079}
2080
5e1be0fb
CH
2081#ifdef DEBUG
2082STATIC void
2083xfs_check_agi_unlinked(
2084 struct xfs_agi *agi)
2085{
2086 int i;
2087
2088 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
2089 ASSERT(agi->agi_unlinked[i]);
2090}
2091#else
2092#define xfs_check_agi_unlinked(agi)
2093#endif
2094
983d09ff 2095static bool
612cfbfe 2096xfs_agi_verify(
3702ce6e
DC
2097 struct xfs_buf *bp)
2098{
2099 struct xfs_mount *mp = bp->b_target->bt_mount;
2100 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
3702ce6e 2101
983d09ff
DC
2102 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2103 !uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_uuid))
2104 return false;
3702ce6e
DC
2105 /*
2106 * Validate the magic number of the agi block.
2107 */
983d09ff
DC
2108 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
2109 return false;
2110 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2111 return false;
3702ce6e 2112
e1b05723
ES
2113 if (be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
2114 return false;
3702ce6e
DC
2115 /*
2116 * during growfs operations, the perag is not fully initialised,
2117 * so we can't use it for any useful checking. growfs ensures we can't
2118 * use it by using uncached buffers that don't have the perag attached
2119 * so we can detect and avoid this problem.
2120 */
983d09ff
DC
2121 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
2122 return false;
3702ce6e 2123
3702ce6e 2124 xfs_check_agi_unlinked(agi);
983d09ff 2125 return true;
612cfbfe
DC
2126}
2127
1813dd64
DC
2128static void
2129xfs_agi_read_verify(
612cfbfe
DC
2130 struct xfs_buf *bp)
2131{
983d09ff 2132 struct xfs_mount *mp = bp->b_target->bt_mount;
983d09ff 2133
ce5028cf
ES
2134 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2135 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
2451337d 2136 xfs_buf_ioerror(bp, -EFSBADCRC);
ce5028cf
ES
2137 else if (XFS_TEST_ERROR(!xfs_agi_verify(bp), mp,
2138 XFS_ERRTAG_IALLOC_READ_AGI,
2139 XFS_RANDOM_IALLOC_READ_AGI))
2451337d 2140 xfs_buf_ioerror(bp, -EFSCORRUPTED);
ce5028cf
ES
2141
2142 if (bp->b_error)
2143 xfs_verifier_error(bp);
612cfbfe
DC
2144}
2145
b0f539de 2146static void
1813dd64 2147xfs_agi_write_verify(
612cfbfe
DC
2148 struct xfs_buf *bp)
2149{
983d09ff
DC
2150 struct xfs_mount *mp = bp->b_target->bt_mount;
2151 struct xfs_buf_log_item *bip = bp->b_fspriv;
2152
2153 if (!xfs_agi_verify(bp)) {
2451337d 2154 xfs_buf_ioerror(bp, -EFSCORRUPTED);
ce5028cf 2155 xfs_verifier_error(bp);
983d09ff
DC
2156 return;
2157 }
2158
2159 if (!xfs_sb_version_hascrc(&mp->m_sb))
2160 return;
2161
2162 if (bip)
2163 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
f1dbcd7e 2164 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
3702ce6e
DC
2165}
2166
1813dd64
DC
2167const struct xfs_buf_ops xfs_agi_buf_ops = {
2168 .verify_read = xfs_agi_read_verify,
2169 .verify_write = xfs_agi_write_verify,
2170};
2171
1da177e4
LT
2172/*
2173 * Read in the allocation group header (inode allocation section)
2174 */
2175int
5e1be0fb
CH
2176xfs_read_agi(
2177 struct xfs_mount *mp, /* file system mount structure */
2178 struct xfs_trans *tp, /* transaction pointer */
2179 xfs_agnumber_t agno, /* allocation group number */
2180 struct xfs_buf **bpp) /* allocation group hdr buf */
1da177e4 2181{
5e1be0fb 2182 int error;
1da177e4 2183
d123031a 2184 trace_xfs_read_agi(mp, agno);
5e1be0fb 2185
d123031a 2186 ASSERT(agno != NULLAGNUMBER);
5e1be0fb 2187 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1da177e4 2188 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1813dd64 2189 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
1da177e4
LT
2190 if (error)
2191 return error;
5e1be0fb 2192
38f23232 2193 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
5e1be0fb
CH
2194 return 0;
2195}
2196
2197int
2198xfs_ialloc_read_agi(
2199 struct xfs_mount *mp, /* file system mount structure */
2200 struct xfs_trans *tp, /* transaction pointer */
2201 xfs_agnumber_t agno, /* allocation group number */
2202 struct xfs_buf **bpp) /* allocation group hdr buf */
2203{
2204 struct xfs_agi *agi; /* allocation group header */
2205 struct xfs_perag *pag; /* per allocation group data */
2206 int error;
2207
d123031a
DC
2208 trace_xfs_ialloc_read_agi(mp, agno);
2209
5e1be0fb
CH
2210 error = xfs_read_agi(mp, tp, agno, bpp);
2211 if (error)
2212 return error;
2213
2214 agi = XFS_BUF_TO_AGI(*bpp);
44b56e0a 2215 pag = xfs_perag_get(mp, agno);
1da177e4 2216 if (!pag->pagi_init) {
16259e7d 2217 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
92821e2b 2218 pag->pagi_count = be32_to_cpu(agi->agi_count);
1da177e4 2219 pag->pagi_init = 1;
1da177e4 2220 }
1da177e4 2221
5e1be0fb
CH
2222 /*
2223 * It's possible for these to be out of sync if
2224 * we are in the middle of a forced shutdown.
2225 */
2226 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2227 XFS_FORCED_SHUTDOWN(mp));
44b56e0a 2228 xfs_perag_put(pag);
1da177e4
LT
2229 return 0;
2230}
92821e2b
DC
2231
2232/*
2233 * Read in the agi to initialise the per-ag data in the mount structure
2234 */
2235int
2236xfs_ialloc_pagi_init(
2237 xfs_mount_t *mp, /* file system mount structure */
2238 xfs_trans_t *tp, /* transaction pointer */
2239 xfs_agnumber_t agno) /* allocation group number */
2240{
2241 xfs_buf_t *bp = NULL;
2242 int error;
2243
2244 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2245 if (error)
2246 return error;
2247 if (bp)
2248 xfs_trans_brelse(tp, bp);
2249 return 0;
2250}