]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_ialloc.c
libxfs: refactor manage_zones()
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_ialloc.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
da23017d
NS
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
2bd0ea18 5 */
9c799827 6#include "libxfs_priv.h"
b626fb59
DC
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
12#include "xfs_bit.h"
13#include "xfs_sb.h"
14#include "xfs_mount.h"
f944d3d0 15#include "xfs_defer.h"
b626fb59
DC
16#include "xfs_inode.h"
17#include "xfs_btree.h"
18#include "xfs_ialloc.h"
19#include "xfs_ialloc_btree.h"
20#include "xfs_alloc.h"
56d3fc2b 21#include "xfs_errortag.h"
b626fb59
DC
22#include "xfs_bmap.h"
23#include "xfs_cksum.h"
24#include "xfs_trans.h"
25#include "xfs_trace.h"
85aec44f 26#include "xfs_rmap.h"
2bd0ea18 27
2bd0ea18
NS
28
29/*
30 * Allocation group level functions.
31 */
b5f6747c 32int
5e656dbb 33xfs_ialloc_cluster_alignment(
5a35bf2c 34 struct xfs_mount *mp)
5e656dbb 35{
5a35bf2c 36 if (xfs_sb_version_hasalign(&mp->m_sb) &&
e32fb241 37 mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp))
5a35bf2c 38 return mp->m_sb.sb_inoalignmt;
5e656dbb
BN
39 return 1;
40}
2bd0ea18 41
b194c7d8 42/*
56b2de80 43 * Lookup a record by ino in the btree given by cur.
b194c7d8
BN
44 */
45int /* error */
56b2de80 46xfs_inobt_lookup(
b194c7d8
BN
47 struct xfs_btree_cur *cur, /* btree cursor */
48 xfs_agino_t ino, /* starting inode of chunk */
56b2de80 49 xfs_lookup_t dir, /* <=, >=, == */
b194c7d8
BN
50 int *stat) /* success/failure */
51{
52 cur->bc_rec.i.ir_startino = ino;
11640e30
BF
53 cur->bc_rec.i.ir_holemask = 0;
54 cur->bc_rec.i.ir_count = 0;
56b2de80
DC
55 cur->bc_rec.i.ir_freecount = 0;
56 cur->bc_rec.i.ir_free = 0;
57 return xfs_btree_lookup(cur, dir, stat);
b194c7d8
BN
58}
59
60/*
56b2de80 61 * Update the record referred to by cur to the value given.
b194c7d8
BN
62 * This either works (return 0) or gets an EFSCORRUPTED error.
63 */
64STATIC int /* error */
65xfs_inobt_update(
66 struct xfs_btree_cur *cur, /* btree cursor */
56b2de80 67 xfs_inobt_rec_incore_t *irec) /* btree record */
b194c7d8
BN
68{
69 union xfs_btree_rec rec;
70
56b2de80 71 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
11640e30
BF
72 if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
73 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
74 rec.inobt.ir_u.sp.ir_count = irec->ir_count;
75 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
76 } else {
77 /* ir_holemask/ir_count not supported on-disk */
78 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
79 }
56b2de80 80 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
b194c7d8
BN
81 return xfs_btree_update(cur, &rec);
82}
83
b5f6747c
DW
84/* Convert on-disk btree record to incore inobt record. */
85void
86xfs_inobt_btrec_to_irec(
87 struct xfs_mount *mp,
88 union xfs_btree_rec *rec,
89 struct xfs_inobt_rec_incore *irec)
b194c7d8 90{
11640e30 91 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
b5f6747c 92 if (xfs_sb_version_hassparseinodes(&mp->m_sb)) {
11640e30
BF
93 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
94 irec->ir_count = rec->inobt.ir_u.sp.ir_count;
95 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
96 } else {
97 /*
98 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
99 * values for full inode chunks.
100 */
101 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
102 irec->ir_count = XFS_INODES_PER_CHUNK;
103 irec->ir_freecount =
104 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
b194c7d8 105 }
11640e30 106 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
b5f6747c
DW
107}
108
109/*
110 * Get the data from the pointed-to record.
111 */
112int
113xfs_inobt_get_rec(
114 struct xfs_btree_cur *cur,
115 struct xfs_inobt_rec_incore *irec,
116 int *stat)
117{
ec291989
DC
118 struct xfs_mount *mp = cur->bc_mp;
119 xfs_agnumber_t agno = cur->bc_private.a.agno;
b5f6747c
DW
120 union xfs_btree_rec *rec;
121 int error;
ec291989 122 uint64_t realfree;
b5f6747c
DW
123
124 error = xfs_btree_get_rec(cur, &rec, stat);
125 if (error || *stat == 0)
126 return error;
127
ec291989
DC
128 xfs_inobt_btrec_to_irec(mp, rec, irec);
129
130 if (!xfs_verify_agino(mp, agno, irec->ir_startino))
131 goto out_bad_rec;
132 if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
133 irec->ir_count > XFS_INODES_PER_CHUNK)
134 goto out_bad_rec;
135 if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
136 goto out_bad_rec;
137
138 /* if there are no holes, return the first available offset */
139 if (!xfs_inobt_issparse(irec->ir_holemask))
140 realfree = irec->ir_free;
141 else
142 realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
143 if (hweight64(realfree) != irec->ir_freecount)
144 goto out_bad_rec;
11640e30
BF
145
146 return 0;
ec291989
DC
147
148out_bad_rec:
149 xfs_warn(mp,
150 "%s Inode BTree record corruption in AG %d detected!",
151 cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free", agno);
152 xfs_warn(mp,
153"start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
154 irec->ir_startino, irec->ir_count, irec->ir_freecount,
155 irec->ir_free, irec->ir_holemask);
156 return -EFSCORRUPTED;
b194c7d8
BN
157}
158
3c699279
BF
159/*
160 * Insert a single inobt record. Cursor must already point to desired location.
161 */
aa465192 162int
3c699279
BF
163xfs_inobt_insert_rec(
164 struct xfs_btree_cur *cur,
4a492e72
DW
165 uint16_t holemask,
166 uint8_t count,
167 int32_t freecount,
3c699279
BF
168 xfs_inofree_t free,
169 int *stat)
170{
11640e30
BF
171 cur->bc_rec.i.ir_holemask = holemask;
172 cur->bc_rec.i.ir_count = count;
3c699279
BF
173 cur->bc_rec.i.ir_freecount = freecount;
174 cur->bc_rec.i.ir_free = free;
175 return xfs_btree_insert(cur, stat);
176}
177
178/*
179 * Insert records describing a newly allocated inode chunk into the inobt.
180 */
181STATIC int
182xfs_inobt_insert(
183 struct xfs_mount *mp,
184 struct xfs_trans *tp,
185 struct xfs_buf *agbp,
186 xfs_agino_t newino,
187 xfs_agino_t newlen,
188 xfs_btnum_t btnum)
189{
190 struct xfs_btree_cur *cur;
191 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
192 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
193 xfs_agino_t thisino;
194 int i;
195 int error;
196
197 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
198
199 for (thisino = newino;
200 thisino < newino + newlen;
201 thisino += XFS_INODES_PER_CHUNK) {
202 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
203 if (error) {
204 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
205 return error;
206 }
207 ASSERT(i == 0);
208
11640e30
BF
209 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
210 XFS_INODES_PER_CHUNK,
211 XFS_INODES_PER_CHUNK,
3c699279
BF
212 XFS_INOBT_ALL_FREE, &i);
213 if (error) {
214 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
215 return error;
216 }
217 ASSERT(i == 1);
218 }
219
220 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
221
222 return 0;
223}
224
56b2de80
DC
225/*
226 * Verify that the number of free inodes in the AGI is correct.
227 */
228#ifdef DEBUG
229STATIC int
230xfs_check_agi_freecount(
231 struct xfs_btree_cur *cur,
232 struct xfs_agi *agi)
233{
234 if (cur->bc_nlevels == 1) {
235 xfs_inobt_rec_incore_t rec;
236 int freecount = 0;
237 int error;
238 int i;
239
240 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
241 if (error)
242 return error;
243
244 do {
245 error = xfs_inobt_get_rec(cur, &rec, &i);
246 if (error)
247 return error;
248
249 if (i) {
250 freecount += rec.ir_freecount;
251 error = xfs_btree_increment(cur, 0, &i);
252 if (error)
253 return error;
254 }
255 } while (i == 1);
256
257 if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
258 ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
259 }
260 return 0;
261}
262#else
263#define xfs_check_agi_freecount(cur, agi) 0
264#endif
265
266/*
e9d35108
DC
267 * Initialise a new set of inodes. When called without a transaction context
268 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
269 * than logging them (which in a transaction context puts them into the AIL
270 * for writeback rather than the xfsbufd queue).
56b2de80 271 */
e9d35108 272int
56b2de80
DC
273xfs_ialloc_inode_init(
274 struct xfs_mount *mp,
275 struct xfs_trans *tp,
e9d35108 276 struct list_head *buffer_list,
fe8d48ac 277 int icount,
56b2de80
DC
278 xfs_agnumber_t agno,
279 xfs_agblock_t agbno,
280 xfs_agblock_t length,
281 unsigned int gen)
282{
283 struct xfs_buf *fbuf;
284 struct xfs_dinode *free;
42d896cf 285 int nbufs;
56b2de80
DC
286 int version;
287 int i, j;
288 xfs_daddr_t d;
41ce5f36 289 xfs_ino_t ino = 0;
56b2de80
DC
290
291 /*
ff105f75
DC
292 * Loop over the new block(s), filling in the inodes. For small block
293 * sizes, manipulate the inodes in buffers which are multiples of the
294 * blocks size.
56b2de80 295 */
42d896cf 296 nbufs = length / mp->m_blocks_per_cluster;
56b2de80
DC
297
298 /*
e9d35108
DC
299 * Figure out what version number to use in the inodes we create. If
300 * the superblock version has caught up to the one that supports the new
301 * inode format, then use the new inode version. Otherwise use the old
302 * version so that old kernels will continue to be able to use the file
303 * system.
41ce5f36
DC
304 *
305 * For v3 inodes, we also need to write the inode number into the inode,
306 * so calculate the first inode number of the chunk here as
7516da71 307 * XFS_AGB_TO_AGINO() only works within a filesystem block, not
e9d35108
DC
308 * across multiple filesystem blocks (such as a cluster) and so cannot
309 * be used in the cluster buffer loop below.
310 *
311 * Further, because we are writing the inode directly into the buffer
312 * and calculating a CRC on the entire inode, we have ot log the entire
313 * inode so that the entire range the CRC covers is present in the log.
314 * That means for v3 inode we log the entire buffer rather than just the
315 * inode cores.
56b2de80 316 */
41ce5f36
DC
317 if (xfs_sb_version_hascrc(&mp->m_sb)) {
318 version = 3;
7516da71 319 ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno));
e9d35108
DC
320
321 /*
322 * log the initialisation that is about to take place as an
323 * logical operation. This means the transaction does not
324 * need to log the physical changes to the inode buffers as log
325 * recovery will know what initialisation is actually needed.
326 * Hence we only need to log the buffers as "ordered" buffers so
327 * they track in the AIL as if they were physically logged.
328 */
329 if (tp)
fe8d48ac 330 xfs_icreate_log(tp, agno, agbno, icount,
e9d35108 331 mp->m_sb.sb_inodesize, length, gen);
ff105f75 332 } else
56b2de80 333 version = 2;
56b2de80
DC
334
335 for (j = 0; j < nbufs; j++) {
336 /*
337 * Get the block.
338 */
42d896cf
DW
339 d = XFS_AGB_TO_DADDR(mp, agno, agbno +
340 (j * mp->m_blocks_per_cluster));
56b2de80 341 fbuf = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
42d896cf 342 mp->m_bsize * mp->m_blocks_per_cluster,
a2ceac1f
DC
343 XBF_UNMAPPED);
344 if (!fbuf)
12b53197 345 return -ENOMEM;
e9d35108
DC
346
347 /* Initialize the inode buffers and log them appropriately. */
a2ceac1f 348 fbuf->b_ops = &xfs_inode_buf_ops;
e9d35108 349 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
42d896cf 350 for (i = 0; i < mp->m_inodes_per_cluster; i++) {
56b2de80 351 int ioffset = i << mp->m_sb.sb_inodelog;
41ce5f36 352 uint isize = xfs_dinode_size(version);
56b2de80
DC
353
354 free = xfs_make_iptr(mp, fbuf, i);
355 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
356 free->di_version = version;
357 free->di_gen = cpu_to_be32(gen);
358 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
41ce5f36
DC
359
360 if (version == 3) {
361 free->di_ino = cpu_to_be64(ino);
362 ino++;
9c4e12fb
ES
363 uuid_copy(&free->di_uuid,
364 &mp->m_sb.sb_meta_uuid);
41ce5f36 365 xfs_dinode_calc_crc(mp, free);
e9d35108
DC
366 } else if (tp) {
367 /* just log the inode core */
368 xfs_trans_log_buf(tp, fbuf, ioffset,
369 ioffset + isize - 1);
41ce5f36 370 }
e9d35108 371 }
41ce5f36 372
e9d35108
DC
373 if (tp) {
374 /*
375 * Mark the buffer as an inode allocation buffer so it
376 * sticks in AIL at the point of this allocation
377 * transaction. This ensures the they are on disk before
378 * the tail of the log can be moved past this
379 * transaction (i.e. by preventing relogging from moving
380 * it forward in the log).
381 */
382 xfs_trans_inode_alloc_buf(tp, fbuf);
383 if (version == 3) {
384 /*
385 * Mark the buffer as ordered so that they are
386 * not physically logged in the transaction but
387 * still tracked in the AIL as part of the
388 * transaction and pin the log appropriately.
389 */
390 xfs_trans_ordered_buf(tp, fbuf);
e9d35108
DC
391 }
392 } else {
393 fbuf->b_flags |= XBF_DONE;
394 xfs_buf_delwri_queue(fbuf, buffer_list);
395 xfs_buf_relse(fbuf);
56b2de80 396 }
56b2de80 397 }
a2ceac1f 398 return 0;
56b2de80
DC
399}
400
6f4c54a4
BF
401/*
402 * Align startino and allocmask for a recently allocated sparse chunk such that
403 * they are fit for insertion (or merge) into the on-disk inode btrees.
404 *
405 * Background:
406 *
407 * When enabled, sparse inode support increases the inode alignment from cluster
408 * size to inode chunk size. This means that the minimum range between two
409 * non-adjacent inode records in the inobt is large enough for a full inode
410 * record. This allows for cluster sized, cluster aligned block allocation
411 * without need to worry about whether the resulting inode record overlaps with
412 * another record in the tree. Without this basic rule, we would have to deal
413 * with the consequences of overlap by potentially undoing recent allocations in
414 * the inode allocation codepath.
415 *
416 * Because of this alignment rule (which is enforced on mount), there are two
417 * inobt possibilities for newly allocated sparse chunks. One is that the
418 * aligned inode record for the chunk covers a range of inodes not already
419 * covered in the inobt (i.e., it is safe to insert a new sparse record). The
420 * other is that a record already exists at the aligned startino that considers
421 * the newly allocated range as sparse. In the latter case, record content is
422 * merged in hope that sparse inode chunks fill to full chunks over time.
423 */
424STATIC void
425xfs_align_sparse_ino(
426 struct xfs_mount *mp,
427 xfs_agino_t *startino,
428 uint16_t *allocmask)
429{
430 xfs_agblock_t agbno;
431 xfs_agblock_t mod;
432 int offset;
433
434 agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
435 mod = agbno % mp->m_sb.sb_inoalignmt;
436 if (!mod)
437 return;
438
439 /* calculate the inode offset and align startino */
7516da71 440 offset = XFS_AGB_TO_AGINO(mp, mod);
6f4c54a4
BF
441 *startino -= offset;
442
443 /*
444 * Since startino has been aligned down, left shift allocmask such that
445 * it continues to represent the same physical inodes relative to the
446 * new startino.
447 */
448 *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
449}
450
451/*
452 * Determine whether the source inode record can merge into the target. Both
453 * records must be sparse, the inode ranges must match and there must be no
454 * allocation overlap between the records.
455 */
456STATIC bool
457__xfs_inobt_can_merge(
458 struct xfs_inobt_rec_incore *trec, /* tgt record */
459 struct xfs_inobt_rec_incore *srec) /* src record */
460{
461 uint64_t talloc;
462 uint64_t salloc;
463
464 /* records must cover the same inode range */
465 if (trec->ir_startino != srec->ir_startino)
466 return false;
467
468 /* both records must be sparse */
469 if (!xfs_inobt_issparse(trec->ir_holemask) ||
470 !xfs_inobt_issparse(srec->ir_holemask))
471 return false;
472
473 /* both records must track some inodes */
474 if (!trec->ir_count || !srec->ir_count)
475 return false;
476
477 /* can't exceed capacity of a full record */
478 if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
479 return false;
480
481 /* verify there is no allocation overlap */
482 talloc = xfs_inobt_irec_to_allocmask(trec);
483 salloc = xfs_inobt_irec_to_allocmask(srec);
484 if (talloc & salloc)
485 return false;
486
487 return true;
488}
489
490/*
491 * Merge the source inode record into the target. The caller must call
492 * __xfs_inobt_can_merge() to ensure the merge is valid.
493 */
494STATIC void
495__xfs_inobt_rec_merge(
496 struct xfs_inobt_rec_incore *trec, /* target */
497 struct xfs_inobt_rec_incore *srec) /* src */
498{
499 ASSERT(trec->ir_startino == srec->ir_startino);
500
501 /* combine the counts */
502 trec->ir_count += srec->ir_count;
503 trec->ir_freecount += srec->ir_freecount;
504
505 /*
506 * Merge the holemask and free mask. For both fields, 0 bits refer to
507 * allocated inodes. We combine the allocated ranges with bitwise AND.
508 */
509 trec->ir_holemask &= srec->ir_holemask;
510 trec->ir_free &= srec->ir_free;
511}
512
513/*
514 * Insert a new sparse inode chunk into the associated inode btree. The inode
515 * record for the sparse chunk is pre-aligned to a startino that should match
516 * any pre-existing sparse inode record in the tree. This allows sparse chunks
517 * to fill over time.
518 *
519 * This function supports two modes of handling preexisting records depending on
520 * the merge flag. If merge is true, the provided record is merged with the
521 * existing record and updated in place. The merged record is returned in nrec.
522 * If merge is false, an existing record is replaced with the provided record.
523 * If no preexisting record exists, the provided record is always inserted.
524 *
525 * It is considered corruption if a merge is requested and not possible. Given
526 * the sparse inode alignment constraints, this should never happen.
527 */
528STATIC int
529xfs_inobt_insert_sprec(
530 struct xfs_mount *mp,
531 struct xfs_trans *tp,
532 struct xfs_buf *agbp,
533 int btnum,
534 struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */
535 bool merge) /* merge or replace */
536{
537 struct xfs_btree_cur *cur;
538 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
539 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
540 int error;
541 int i;
542 struct xfs_inobt_rec_incore rec;
543
544 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
545
546 /* the new record is pre-aligned so we know where to look */
547 error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
548 if (error)
549 goto error;
550 /* if nothing there, insert a new record and return */
551 if (i == 0) {
552 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
553 nrec->ir_count, nrec->ir_freecount,
554 nrec->ir_free, &i);
555 if (error)
556 goto error;
557 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
558
559 goto out;
560 }
561
562 /*
563 * A record exists at this startino. Merge or replace the record
564 * depending on what we've been asked to do.
565 */
566 if (merge) {
567 error = xfs_inobt_get_rec(cur, &rec, &i);
568 if (error)
569 goto error;
570 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
571 XFS_WANT_CORRUPTED_GOTO(mp,
572 rec.ir_startino == nrec->ir_startino,
573 error);
574
575 /*
576 * This should never fail. If we have coexisting records that
577 * cannot merge, something is seriously wrong.
578 */
579 XFS_WANT_CORRUPTED_GOTO(mp, __xfs_inobt_can_merge(nrec, &rec),
580 error);
581
582 trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino,
583 rec.ir_holemask, nrec->ir_startino,
584 nrec->ir_holemask);
585
586 /* merge to nrec to output the updated record */
587 __xfs_inobt_rec_merge(nrec, &rec);
588
589 trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino,
590 nrec->ir_holemask);
591
592 error = xfs_inobt_rec_check_count(mp, nrec);
593 if (error)
594 goto error;
595 }
596
597 error = xfs_inobt_update(cur, nrec);
598 if (error)
599 goto error;
600
601out:
602 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
603 return 0;
604error:
605 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
606 return error;
607}
608
2bd0ea18
NS
609/*
610 * Allocate new inodes in the allocation group specified by agbp.
611 * Return 0 for success, else error code.
612 */
613STATIC int /* error code or 0 */
614xfs_ialloc_ag_alloc(
615 xfs_trans_t *tp, /* transaction pointer */
616 xfs_buf_t *agbp, /* alloc group buffer */
617 int *alloc)
618{
619 xfs_agi_t *agi; /* allocation group header */
dfc130f3 620 xfs_alloc_arg_t args; /* allocation argument structure */
5e656dbb 621 xfs_agnumber_t agno;
2bd0ea18 622 int error;
2bd0ea18
NS
623 xfs_agino_t newino; /* new first inode's number */
624 xfs_agino_t newlen; /* new number of inodes */
5e656dbb 625 int isaligned = 0; /* inode allocation at stripe unit */
2bd0ea18 626 /* boundary */
6f4c54a4
BF
627 uint16_t allocmask = (uint16_t) -1; /* init. to full chunk */
628 struct xfs_inobt_rec_incore rec;
56b2de80 629 struct xfs_perag *pag;
c9005f41
BF
630 int do_sparse = 0;
631
a2ceac1f 632 memset(&args, 0, sizeof(args));
2bd0ea18
NS
633 args.tp = tp;
634 args.mp = tp->t_mountp;
c9005f41 635 args.fsbno = NULLFSBLOCK;
007347e3 636 args.oinfo = XFS_RMAP_OINFO_INODES;
2bd0ea18 637
0d995d03
BF
638#ifdef DEBUG
639 /* randomly do sparse inode allocations */
640 if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) &&
641 args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks)
642 do_sparse = prandom_u32() & 1;
643#endif
644
2bd0ea18
NS
645 /*
646 * Locking will ensure that we don't have two callers in here
647 * at one time.
648 */
ff105f75 649 newlen = args.mp->m_ialloc_inos;
2bd0ea18 650 if (args.mp->m_maxicount &&
cbf3beaa 651 percpu_counter_read_positive(&args.mp->m_icount) + newlen >
19ebedcf 652 args.mp->m_maxicount)
12b53197 653 return -ENOSPC;
ff105f75 654 args.minlen = args.maxlen = args.mp->m_ialloc_blks;
2bd0ea18 655 /*
5e656dbb
BN
656 * First try to allocate inodes contiguous with the last-allocated
657 * chunk of inodes. If the filesystem is striped, this will fill
658 * an entire stripe unit with inodes.
3439d03a 659 */
2bd0ea18 660 agi = XFS_BUF_TO_AGI(agbp);
5e656dbb 661 newino = be32_to_cpu(agi->agi_newino);
56b2de80 662 agno = be32_to_cpu(agi->agi_seqno);
5e656dbb 663 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
ff105f75 664 args.mp->m_ialloc_blks;
c9005f41
BF
665 if (do_sparse)
666 goto sparse_alloc;
5e656dbb
BN
667 if (likely(newino != NULLAGINO &&
668 (args.agbno < be32_to_cpu(agi->agi_length)))) {
56b2de80 669 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
5e656dbb 670 args.type = XFS_ALLOCTYPE_THIS_BNO;
5e656dbb
BN
671 args.prod = 1;
672
673 /*
674 * We need to take into account alignment here to ensure that
675 * we don't modify the free list if we fail to have an exact
676 * block. If we don't have an exact match, and every oher
677 * attempt allocation attempt fails, we'll end up cancelling
678 * a dirty transaction and shutting down.
679 *
680 * For an exact allocation, alignment must be 1,
681 * however we need to take cluster alignment into account when
682 * fixing up the freelist. Use the minalignslop field to
683 * indicate that extra blocks might be required for alignment,
684 * but not to use them in the actual exact allocation.
685 */
686 args.alignment = 1;
bafa1332 687 args.minalignslop = args.mp->m_cluster_align - 1;
5e656dbb
BN
688
689 /* Allow space for the inode btree to split. */
56b2de80 690 args.minleft = args.mp->m_in_maxlevels - 1;
5e656dbb
BN
691 if ((error = xfs_alloc_vextent(&args)))
692 return error;
ff105f75
DC
693
694 /*
695 * This request might have dirtied the transaction if the AG can
696 * satisfy the request, but the exact block was not available.
697 * If the allocation did fail, subsequent requests will relax
698 * the exact agbno requirement and increase the alignment
699 * instead. It is critical that the total size of the request
700 * (len + alignment + slop) does not increase from this point
701 * on, so reset minalignslop to ensure it is not included in
702 * subsequent requests.
703 */
704 args.minalignslop = 0;
c9005f41 705 }
5e656dbb
BN
706
707 if (unlikely(args.fsbno == NULLFSBLOCK)) {
708 /*
709 * Set the alignment for the allocation.
710 * If stripe alignment is turned on then align at stripe unit
711 * boundary.
712 * If the cluster size is smaller than a filesystem block
713 * then we're doing I/O for inodes in filesystem block size
714 * pieces, so don't need alignment anyway.
715 */
716 isaligned = 0;
717 if (args.mp->m_sinoalign) {
718 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
719 args.alignment = args.mp->m_dalign;
720 isaligned = 1;
721 } else
bafa1332 722 args.alignment = args.mp->m_cluster_align;
5e656dbb
BN
723 /*
724 * Need to figure out where to allocate the inode blocks.
725 * Ideally they should be spaced out through the a.g.
726 * For now, just allocate blocks up front.
727 */
728 args.agbno = be32_to_cpu(agi->agi_root);
56b2de80 729 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
5e656dbb
BN
730 /*
731 * Allocate a fixed-size extent of inodes.
732 */
733 args.type = XFS_ALLOCTYPE_NEAR_BNO;
5e656dbb
BN
734 args.prod = 1;
735 /*
736 * Allow space for the inode btree to split.
737 */
56b2de80 738 args.minleft = args.mp->m_in_maxlevels - 1;
5e656dbb
BN
739 if ((error = xfs_alloc_vextent(&args)))
740 return error;
741 }
2bd0ea18
NS
742
743 /*
744 * If stripe alignment is turned on, then try again with cluster
745 * alignment.
746 */
747 if (isaligned && args.fsbno == NULLFSBLOCK) {
748 args.type = XFS_ALLOCTYPE_NEAR_BNO;
6e3140c7 749 args.agbno = be32_to_cpu(agi->agi_root);
56b2de80 750 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
bafa1332 751 args.alignment = args.mp->m_cluster_align;
0e266570 752 if ((error = xfs_alloc_vextent(&args)))
dfc130f3 753 return error;
2bd0ea18 754 }
5000d01d 755
6f4c54a4
BF
756 /*
757 * Finally, try a sparse allocation if the filesystem supports it and
758 * the sparse allocation length is smaller than a full chunk.
759 */
760 if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) &&
761 args.mp->m_ialloc_min_blks < args.mp->m_ialloc_blks &&
762 args.fsbno == NULLFSBLOCK) {
c9005f41 763sparse_alloc:
6f4c54a4
BF
764 args.type = XFS_ALLOCTYPE_NEAR_BNO;
765 args.agbno = be32_to_cpu(agi->agi_root);
766 args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
767 args.alignment = args.mp->m_sb.sb_spino_align;
768 args.prod = 1;
769
770 args.minlen = args.mp->m_ialloc_min_blks;
771 args.maxlen = args.minlen;
772
773 /*
774 * The inode record will be aligned to full chunk size. We must
775 * prevent sparse allocation from AG boundaries that result in
776 * invalid inode records, such as records that start at agbno 0
777 * or extend beyond the AG.
778 *
779 * Set min agbno to the first aligned, non-zero agbno and max to
780 * the last aligned agbno that is at least one full chunk from
781 * the end of the AG.
782 */
783 args.min_agbno = args.mp->m_sb.sb_inoalignmt;
784 args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
785 args.mp->m_sb.sb_inoalignmt) -
786 args.mp->m_ialloc_blks;
787
788 error = xfs_alloc_vextent(&args);
789 if (error)
790 return error;
791
7516da71 792 newlen = XFS_AGB_TO_AGINO(args.mp, args.len);
0d995d03 793 ASSERT(newlen <= XFS_INODES_PER_CHUNK);
6f4c54a4
BF
794 allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
795 }
796
2bd0ea18
NS
797 if (args.fsbno == NULLFSBLOCK) {
798 *alloc = 0;
799 return 0;
800 }
801 ASSERT(args.len == args.minlen);
a562a63b 802
5e656dbb 803 /*
56b2de80
DC
804 * Stamp and write the inode buffers.
805 *
5e656dbb
BN
806 * Seed the new inode cluster with a random generation number. This
807 * prevents short-term reuse of generation numbers if a chunk is
808 * freed and then immediately reallocated. We use random numbers
809 * rather than a linear progression to prevent the next generation
810 * number from being easily guessable.
811 */
fe8d48ac
BF
812 error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
813 args.agbno, args.len, prandom_u32());
56b2de80 814
a2ceac1f
DC
815 if (error)
816 return error;
56b2de80
DC
817 /*
818 * Convert the results.
819 */
7516da71 820 newino = XFS_AGB_TO_AGINO(args.mp, args.agbno);
6f4c54a4
BF
821
822 if (xfs_inobt_issparse(~allocmask)) {
823 /*
824 * We've allocated a sparse chunk. Align the startino and mask.
825 */
826 xfs_align_sparse_ino(args.mp, &newino, &allocmask);
827
828 rec.ir_startino = newino;
829 rec.ir_holemask = ~allocmask;
830 rec.ir_count = newlen;
831 rec.ir_freecount = newlen;
832 rec.ir_free = XFS_INOBT_ALL_FREE;
833
834 /*
835 * Insert the sparse record into the inobt and allow for a merge
836 * if necessary. If a merge does occur, rec is updated to the
837 * merged record.
838 */
839 error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO,
840 &rec, true);
841 if (error == -EFSCORRUPTED) {
842 xfs_alert(args.mp,
843 "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
844 XFS_AGINO_TO_INO(args.mp, agno,
845 rec.ir_startino),
846 rec.ir_holemask, rec.ir_count);
847 xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
848 }
849 if (error)
850 return error;
851
852 /*
853 * We can't merge the part we've just allocated as for the inobt
854 * due to finobt semantics. The original record may or may not
855 * exist independent of whether physical inodes exist in this
856 * sparse chunk.
857 *
858 * We must update the finobt record based on the inobt record.
859 * rec contains the fully merged and up to date inobt record
860 * from the previous call. Set merge false to replace any
861 * existing record with this one.
862 */
863 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
864 error = xfs_inobt_insert_sprec(args.mp, tp, agbp,
865 XFS_BTNUM_FINO, &rec,
866 false);
867 if (error)
868 return error;
869 }
870 } else {
871 /* full chunk - insert new records to both btrees */
872 error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
873 XFS_BTNUM_INO);
874 if (error)
875 return error;
876
877 if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
878 error = xfs_inobt_insert(args.mp, tp, agbp, newino,
879 newlen, XFS_BTNUM_FINO);
880 if (error)
881 return error;
882 }
883 }
884
885 /*
886 * Update AGI counts and newino.
887 */
5e656dbb
BN
888 be32_add_cpu(&agi->agi_count, newlen);
889 be32_add_cpu(&agi->agi_freecount, newlen);
56b2de80
DC
890 pag = xfs_perag_get(args.mp, agno);
891 pag->pagi_freecount += newlen;
a3204ee7 892 pag->pagi_count += newlen;
56b2de80 893 xfs_perag_put(pag);
6e3140c7 894 agi->agi_newino = cpu_to_be32(newino);
56b2de80 895
2bd0ea18
NS
896 /*
897 * Log allocation group header fields
898 */
899 xfs_ialloc_log_agi(tp, agbp,
900 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
901 /*
902 * Modify/log superblock values for inode count and inode free count.
903 */
904 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
905 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
906 *alloc = 1;
907 return 0;
908}
909
56b2de80 910STATIC xfs_agnumber_t
321717ae
NS
911xfs_ialloc_next_ag(
912 xfs_mount_t *mp)
913{
914 xfs_agnumber_t agno;
915
916 spin_lock(&mp->m_agirotor_lock);
917 agno = mp->m_agirotor;
a2ceac1f 918 if (++mp->m_agirotor >= mp->m_maxagi)
321717ae
NS
919 mp->m_agirotor = 0;
920 spin_unlock(&mp->m_agirotor_lock);
921
922 return agno;
923}
924
2bd0ea18
NS
925/*
926 * Select an allocation group to look for a free inode in, based on the parent
e6d77a21 927 * inode and the mode. Return the allocation group buffer.
2bd0ea18 928 */
a2ceac1f 929STATIC xfs_agnumber_t
2bd0ea18
NS
930xfs_ialloc_ag_select(
931 xfs_trans_t *tp, /* transaction pointer */
932 xfs_ino_t parent, /* parent directory inode number */
c1a29e92 933 umode_t mode) /* bits set to indicate file type */
2bd0ea18 934{
2bd0ea18
NS
935 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
936 xfs_agnumber_t agno; /* current ag number */
937 int flags; /* alloc buffer locking flags */
938 xfs_extlen_t ineed; /* blocks needed for inode allocation */
275ae71f 939 xfs_extlen_t longest = 0; /* longest extent available */
2bd0ea18
NS
940 xfs_mount_t *mp; /* mount point structure */
941 int needspace; /* file mode implies space allocated */
942 xfs_perag_t *pag; /* per allocation group data */
943 xfs_agnumber_t pagno; /* parent (starting) ag number */
a2ceac1f 944 int error;
2bd0ea18
NS
945
946 /*
947 * Files of these types need at least one block if length > 0
948 * (and they won't fit in the inode, but that's hard to figure out).
949 */
950 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
951 mp = tp->t_mountp;
34317449 952 agcount = mp->m_maxagi;
2bd0ea18 953 if (S_ISDIR(mode))
321717ae 954 pagno = xfs_ialloc_next_ag(mp);
5ce1d1f7 955 else {
2bd0ea18 956 pagno = XFS_INO_TO_AGNO(mp, parent);
5ce1d1f7
NS
957 if (pagno >= agcount)
958 pagno = 0;
959 }
a2ceac1f 960
2bd0ea18 961 ASSERT(pagno < agcount);
a2ceac1f 962
2bd0ea18
NS
963 /*
964 * Loop through allocation groups, looking for one with a little
965 * free space in it. Note we don't look for free inodes, exactly.
966 * Instead, we include whether there is a need to allocate inodes
5000d01d 967 * to mean that blocks must be allocated for them,
2bd0ea18
NS
968 * if none are currently free.
969 */
970 agno = pagno;
971 flags = XFS_ALLOC_FLAG_TRYLOCK;
972 for (;;) {
56b2de80 973 pag = xfs_perag_get(mp, agno);
a2ceac1f
DC
974 if (!pag->pagi_inodeok) {
975 xfs_ialloc_next_ag(mp);
976 goto nextag;
977 }
978
2bd0ea18 979 if (!pag->pagi_init) {
a2ceac1f
DC
980 error = xfs_ialloc_pagi_init(mp, tp, agno);
981 if (error)
2bd0ea18 982 goto nextag;
a2ceac1f 983 }
34317449 984
a2ceac1f
DC
985 if (pag->pagi_freecount) {
986 xfs_perag_put(pag);
987 return agno;
34317449
NS
988 }
989
a2ceac1f
DC
990 if (!pag->pagf_init) {
991 error = xfs_alloc_pagf_init(mp, tp, agno, flags);
992 if (error)
2bd0ea18 993 goto nextag;
2bd0ea18 994 }
a2ceac1f
DC
995
996 /*
5a35bf2c
DC
997 * Check that there is enough free space for the file plus a
998 * chunk of inodes if we need to allocate some. If this is the
999 * first pass across the AGs, take into account the potential
1000 * space needed for alignment of inode chunks when checking the
1001 * longest contiguous free space in the AG - this prevents us
1002 * from getting ENOSPC because we have free space larger than
1003 * m_ialloc_blks but alignment constraints prevent us from using
1004 * it.
1005 *
1006 * If we can't find an AG with space for full alignment slack to
1007 * be taken into account, we must be near ENOSPC in all AGs.
1008 * Hence we don't include alignment for the second pass and so
1009 * if we fail allocation due to alignment issues then it is most
1010 * likely a real ENOSPC condition.
a2ceac1f 1011 */
62dc6cdb 1012 ineed = mp->m_ialloc_min_blks;
5a35bf2c 1013 if (flags && ineed > 1)
bafa1332 1014 ineed += mp->m_cluster_align;
a2ceac1f
DC
1015 longest = pag->pagf_longest;
1016 if (!longest)
1017 longest = pag->pagf_flcount > 0;
1018
1019 if (pag->pagf_freeblks >= needspace + ineed &&
1020 longest >= ineed) {
1021 xfs_perag_put(pag);
1022 return agno;
2bd0ea18 1023 }
5000d01d 1024nextag:
56b2de80 1025 xfs_perag_put(pag);
5000d01d 1026 /*
2bd0ea18
NS
1027 * No point in iterating over the rest, if we're shutting
1028 * down.
1029 */
56b2de80 1030 if (XFS_FORCED_SHUTDOWN(mp))
a2ceac1f 1031 return NULLAGNUMBER;
2bd0ea18 1032 agno++;
5ce1d1f7 1033 if (agno >= agcount)
2bd0ea18
NS
1034 agno = 0;
1035 if (agno == pagno) {
56b2de80 1036 if (flags == 0)
a2ceac1f 1037 return NULLAGNUMBER;
2bd0ea18
NS
1038 flags = 0;
1039 }
1040 }
1041}
1042
56b2de80
DC
1043/*
1044 * Try to retrieve the next record to the left/right from the current one.
1045 */
1046STATIC int
1047xfs_ialloc_next_rec(
1048 struct xfs_btree_cur *cur,
1049 xfs_inobt_rec_incore_t *rec,
1050 int *done,
1051 int left)
1052{
1053 int error;
1054 int i;
1055
1056 if (left)
1057 error = xfs_btree_decrement(cur, 0, &i);
1058 else
1059 error = xfs_btree_increment(cur, 0, &i);
1060
1061 if (error)
1062 return error;
1063 *done = !i;
1064 if (i) {
1065 error = xfs_inobt_get_rec(cur, rec, &i);
1066 if (error)
1067 return error;
19ebedcf 1068 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
56b2de80
DC
1069 }
1070
1071 return 0;
1072}
1073
1074STATIC int
1075xfs_ialloc_get_rec(
1076 struct xfs_btree_cur *cur,
1077 xfs_agino_t agino,
1078 xfs_inobt_rec_incore_t *rec,
3439d03a 1079 int *done)
56b2de80
DC
1080{
1081 int error;
1082 int i;
1083
1084 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
1085 if (error)
1086 return error;
1087 *done = !i;
1088 if (i) {
1089 error = xfs_inobt_get_rec(cur, rec, &i);
1090 if (error)
1091 return error;
19ebedcf 1092 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
56b2de80
DC
1093 }
1094
1095 return 0;
1096}
1097
01792d3b 1098/*
fa58183c
BF
1099 * Return the offset of the first free inode in the record. If the inode chunk
1100 * is sparsely allocated, we convert the record holemask to inode granularity
1101 * and mask off the unallocated regions from the inode free mask.
01792d3b
BF
1102 */
1103STATIC int
1104xfs_inobt_first_free_inode(
1105 struct xfs_inobt_rec_incore *rec)
1106{
fa58183c
BF
1107 xfs_inofree_t realfree;
1108
1109 /* if there are no holes, return the first available offset */
1110 if (!xfs_inobt_issparse(rec->ir_holemask))
1111 return xfs_lowbit64(rec->ir_free);
1112
1113 realfree = xfs_inobt_irec_to_allocmask(rec);
1114 realfree &= rec->ir_free;
1115
1116 return xfs_lowbit64(realfree);
01792d3b
BF
1117}
1118
5000d01d 1119/*
ff105f75 1120 * Allocate an inode using the inobt-only algorithm.
2bd0ea18 1121 */
a2ceac1f 1122STATIC int
ff105f75 1123xfs_dialloc_ag_inobt(
a2ceac1f
DC
1124 struct xfs_trans *tp,
1125 struct xfs_buf *agbp,
1126 xfs_ino_t parent,
1127 xfs_ino_t *inop)
2bd0ea18 1128{
a2ceac1f
DC
1129 struct xfs_mount *mp = tp->t_mountp;
1130 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1131 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1132 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1133 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1134 struct xfs_perag *pag;
1135 struct xfs_btree_cur *cur, *tcur;
1136 struct xfs_inobt_rec_incore rec, trec;
1137 xfs_ino_t ino;
1138 int error;
1139 int offset;
1140 int i, j;
d8c47890 1141 int searchdistance = 10;
2bd0ea18 1142
56b2de80
DC
1143 pag = xfs_perag_get(mp, agno);
1144
a2ceac1f
DC
1145 ASSERT(pag->pagi_init);
1146 ASSERT(pag->pagi_inodeok);
1147 ASSERT(pag->pagi_freecount > 0);
1148
56b2de80 1149 restart_pagno:
70eb7337 1150 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
2bd0ea18
NS
1151 /*
1152 * If pagino is 0 (this is the root inode allocation) use newino.
1153 * This must work because we've just allocated some.
1154 */
1155 if (!pagino)
6e3140c7 1156 pagino = be32_to_cpu(agi->agi_newino);
2bd0ea18 1157
56b2de80
DC
1158 error = xfs_check_agi_freecount(cur, agi);
1159 if (error)
1160 goto error0;
2bd0ea18 1161
2bd0ea18 1162 /*
56b2de80 1163 * If in the same AG as the parent, try to get near the parent.
2bd0ea18
NS
1164 */
1165 if (pagno == agno) {
56b2de80
DC
1166 int doneleft; /* done, to the left */
1167 int doneright; /* done, to the right */
56b2de80
DC
1168
1169 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
1170 if (error)
2bd0ea18 1171 goto error0;
19ebedcf 1172 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
56b2de80
DC
1173
1174 error = xfs_inobt_get_rec(cur, &rec, &j);
1175 if (error)
1176 goto error0;
19ebedcf 1177 XFS_WANT_CORRUPTED_GOTO(mp, j == 1, error0);
56b2de80
DC
1178
1179 if (rec.ir_freecount > 0) {
2bd0ea18
NS
1180 /*
1181 * Found a free inode in the same chunk
56b2de80 1182 * as the parent, done.
2bd0ea18 1183 */
56b2de80 1184 goto alloc_inode;
2bd0ea18 1185 }
56b2de80
DC
1186
1187
1188 /*
1189 * In the same AG as parent, but parent's chunk is full.
1190 */
1191
1192 /* duplicate the cursor, search left & right simultaneously */
1193 error = xfs_btree_dup_cursor(cur, &tcur);
1194 if (error)
1195 goto error0;
1196
2bd0ea18 1197 /*
56b2de80 1198 * Skip to last blocks looked up if same parent inode.
2bd0ea18 1199 */
56b2de80
DC
1200 if (pagino != NULLAGINO &&
1201 pag->pagl_pagino == pagino &&
1202 pag->pagl_leftrec != NULLAGINO &&
1203 pag->pagl_rightrec != NULLAGINO) {
1204 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
3439d03a 1205 &trec, &doneleft);
56b2de80
DC
1206 if (error)
1207 goto error1;
2bd0ea18 1208
56b2de80 1209 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
3439d03a 1210 &rec, &doneright);
2bd0ea18 1211 if (error)
2bd0ea18 1212 goto error1;
56b2de80
DC
1213 } else {
1214 /* search left with tcur, back up 1 record */
1215 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
1216 if (error)
2bd0ea18 1217 goto error1;
2bd0ea18 1218
56b2de80
DC
1219 /* search right with cur, go forward 1 record. */
1220 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
1221 if (error)
1222 goto error1;
1223 }
1224
1225 /*
1226 * Loop until we find an inode chunk with a free inode.
1227 */
d8c47890 1228 while (--searchdistance > 0 && (!doneleft || !doneright)) {
56b2de80
DC
1229 int useleft; /* using left inode chunk this time */
1230
56b2de80
DC
1231 /* figure out the closer block if both are valid. */
1232 if (!doneleft && !doneright) {
1233 useleft = pagino -
1234 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
1235 rec.ir_startino - pagino;
1236 } else {
1237 useleft = !doneleft;
1238 }
1239
1240 /* free inodes to the left? */
1241 if (useleft && trec.ir_freecount) {
56b2de80
DC
1242 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1243 cur = tcur;
1244
1245 pag->pagl_leftrec = trec.ir_startino;
1246 pag->pagl_rightrec = rec.ir_startino;
1247 pag->pagl_pagino = pagino;
ef54efb5 1248 rec = trec;
56b2de80
DC
1249 goto alloc_inode;
1250 }
1251
1252 /* free inodes to the right? */
1253 if (!useleft && rec.ir_freecount) {
1254 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1255
1256 pag->pagl_leftrec = trec.ir_startino;
1257 pag->pagl_rightrec = rec.ir_startino;
1258 pag->pagl_pagino = pagino;
1259 goto alloc_inode;
1260 }
1261
1262 /* get next record to check */
1263 if (useleft) {
1264 error = xfs_ialloc_next_rec(tcur, &trec,
1265 &doneleft, 1);
1266 } else {
1267 error = xfs_ialloc_next_rec(cur, &rec,
1268 &doneright, 0);
1269 }
1270 if (error)
1271 goto error1;
2bd0ea18 1272 }
56b2de80 1273
d8c47890
CM
1274 if (searchdistance <= 0) {
1275 /*
1276 * Not in range - save last search
1277 * location and allocate a new inode
1278 */
1279 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1280 pag->pagl_leftrec = trec.ir_startino;
1281 pag->pagl_rightrec = rec.ir_startino;
1282 pag->pagl_pagino = pagino;
1283
1284 } else {
1285 /*
1286 * We've reached the end of the btree. because
1287 * we are only searching a small chunk of the
1288 * btree each search, there is obviously free
1289 * inodes closer to the parent inode than we
1290 * are now. restart the search again.
1291 */
1292 pag->pagl_pagino = NULLAGINO;
1293 pag->pagl_leftrec = NULLAGINO;
1294 pag->pagl_rightrec = NULLAGINO;
1295 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1296 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1297 goto restart_pagno;
1298 }
2bd0ea18 1299 }
56b2de80 1300
2bd0ea18 1301 /*
56b2de80 1302 * In a different AG from the parent.
2bd0ea18
NS
1303 * See if the most recently allocated block has any free.
1304 */
a2ceac1f 1305 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
56b2de80
DC
1306 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1307 XFS_LOOKUP_EQ, &i);
1308 if (error)
2bd0ea18 1309 goto error0;
56b2de80
DC
1310
1311 if (i == 1) {
1312 error = xfs_inobt_get_rec(cur, &rec, &j);
2bd0ea18
NS
1313 if (error)
1314 goto error0;
56b2de80
DC
1315
1316 if (j == 1 && rec.ir_freecount > 0) {
1317 /*
1318 * The last chunk allocated in the group
1319 * still has a free inode.
1320 */
1321 goto alloc_inode;
2bd0ea18
NS
1322 }
1323 }
1324 }
56b2de80
DC
1325
1326 /*
1327 * None left in the last group, search the whole AG
1328 */
1329 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1330 if (error)
1331 goto error0;
19ebedcf 1332 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
56b2de80
DC
1333
1334 for (;;) {
1335 error = xfs_inobt_get_rec(cur, &rec, &i);
1336 if (error)
1337 goto error0;
19ebedcf 1338 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
56b2de80
DC
1339 if (rec.ir_freecount > 0)
1340 break;
1341 error = xfs_btree_increment(cur, 0, &i);
1342 if (error)
1343 goto error0;
19ebedcf 1344 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
56b2de80
DC
1345 }
1346
1347alloc_inode:
01792d3b 1348 offset = xfs_inobt_first_free_inode(&rec);
2bd0ea18
NS
1349 ASSERT(offset >= 0);
1350 ASSERT(offset < XFS_INODES_PER_CHUNK);
1351 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1352 XFS_INODES_PER_CHUNK) == 0);
1353 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
56b2de80 1354 rec.ir_free &= ~XFS_INOBT_MASK(offset);
2bd0ea18 1355 rec.ir_freecount--;
56b2de80
DC
1356 error = xfs_inobt_update(cur, &rec);
1357 if (error)
2bd0ea18 1358 goto error0;
5e656dbb 1359 be32_add_cpu(&agi->agi_freecount, -1);
2bd0ea18 1360 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
56b2de80
DC
1361 pag->pagi_freecount--;
1362
1363 error = xfs_check_agi_freecount(cur, agi);
1364 if (error)
1365 goto error0;
2bd0ea18 1366
2bd0ea18
NS
1367 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1368 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
56b2de80 1369 xfs_perag_put(pag);
2bd0ea18
NS
1370 *inop = ino;
1371 return 0;
1372error1:
1373 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1374error0:
1375 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
56b2de80 1376 xfs_perag_put(pag);
2bd0ea18
NS
1377 return error;
1378}
1379
ff105f75
DC
1380/*
1381 * Use the free inode btree to allocate an inode based on distance from the
1382 * parent. Note that the provided cursor may be deleted and replaced.
1383 */
1384STATIC int
1385xfs_dialloc_ag_finobt_near(
1386 xfs_agino_t pagino,
1387 struct xfs_btree_cur **ocur,
1388 struct xfs_inobt_rec_incore *rec)
1389{
1390 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
1391 struct xfs_btree_cur *rcur; /* right search cursor */
1392 struct xfs_inobt_rec_incore rrec;
1393 int error;
1394 int i, j;
1395
1396 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1397 if (error)
1398 return error;
1399
1400 if (i == 1) {
1401 error = xfs_inobt_get_rec(lcur, rec, &i);
1402 if (error)
1403 return error;
19ebedcf 1404 XFS_WANT_CORRUPTED_RETURN(lcur->bc_mp, i == 1);
ff105f75
DC
1405
1406 /*
1407 * See if we've landed in the parent inode record. The finobt
1408 * only tracks chunks with at least one free inode, so record
1409 * existence is enough.
1410 */
1411 if (pagino >= rec->ir_startino &&
1412 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1413 return 0;
1414 }
1415
1416 error = xfs_btree_dup_cursor(lcur, &rcur);
1417 if (error)
1418 return error;
1419
1420 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1421 if (error)
1422 goto error_rcur;
1423 if (j == 1) {
1424 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1425 if (error)
1426 goto error_rcur;
19ebedcf 1427 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, j == 1, error_rcur);
ff105f75
DC
1428 }
1429
19ebedcf 1430 XFS_WANT_CORRUPTED_GOTO(lcur->bc_mp, i == 1 || j == 1, error_rcur);
ff105f75
DC
1431 if (i == 1 && j == 1) {
1432 /*
1433 * Both the left and right records are valid. Choose the closer
1434 * inode chunk to the target.
1435 */
1436 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1437 (rrec.ir_startino - pagino)) {
1438 *rec = rrec;
1439 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1440 *ocur = rcur;
1441 } else {
1442 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1443 }
1444 } else if (j == 1) {
1445 /* only the right record is valid */
1446 *rec = rrec;
1447 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1448 *ocur = rcur;
1449 } else if (i == 1) {
1450 /* only the left record is valid */
1451 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1452 }
1453
1454 return 0;
1455
1456error_rcur:
1457 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1458 return error;
1459}
1460
1461/*
1462 * Use the free inode btree to find a free inode based on a newino hint. If
1463 * the hint is NULL, find the first free inode in the AG.
1464 */
1465STATIC int
1466xfs_dialloc_ag_finobt_newino(
1467 struct xfs_agi *agi,
1468 struct xfs_btree_cur *cur,
1469 struct xfs_inobt_rec_incore *rec)
1470{
1471 int error;
1472 int i;
1473
1474 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1475 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1476 XFS_LOOKUP_EQ, &i);
1477 if (error)
1478 return error;
1479 if (i == 1) {
1480 error = xfs_inobt_get_rec(cur, rec, &i);
1481 if (error)
1482 return error;
19ebedcf 1483 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
ff105f75
DC
1484 return 0;
1485 }
1486 }
1487
1488 /*
1489 * Find the first inode available in the AG.
1490 */
1491 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1492 if (error)
1493 return error;
19ebedcf 1494 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
ff105f75
DC
1495
1496 error = xfs_inobt_get_rec(cur, rec, &i);
1497 if (error)
1498 return error;
19ebedcf 1499 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
ff105f75
DC
1500
1501 return 0;
1502}
1503
1504/*
1505 * Update the inobt based on a modification made to the finobt. Also ensure that
1506 * the records from both trees are equivalent post-modification.
1507 */
1508STATIC int
1509xfs_dialloc_ag_update_inobt(
1510 struct xfs_btree_cur *cur, /* inobt cursor */
1511 struct xfs_inobt_rec_incore *frec, /* finobt record */
1512 int offset) /* inode offset */
1513{
1514 struct xfs_inobt_rec_incore rec;
1515 int error;
1516 int i;
1517
1518 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1519 if (error)
1520 return error;
19ebedcf 1521 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
ff105f75
DC
1522
1523 error = xfs_inobt_get_rec(cur, &rec, &i);
1524 if (error)
1525 return error;
19ebedcf 1526 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
ff105f75
DC
1527 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1528 XFS_INODES_PER_CHUNK) == 0);
1529
1530 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1531 rec.ir_freecount--;
1532
19ebedcf 1533 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, (rec.ir_free == frec->ir_free) &&
ff105f75
DC
1534 (rec.ir_freecount == frec->ir_freecount));
1535
5a35bf2c 1536 return xfs_inobt_update(cur, &rec);
ff105f75
DC
1537}
1538
1539/*
1540 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1541 * back to the inobt search algorithm.
1542 *
1543 * The caller selected an AG for us, and made sure that free inodes are
1544 * available.
1545 */
88fc7306
BF
1546STATIC int
1547xfs_dialloc_ag(
1548 struct xfs_trans *tp,
1549 struct xfs_buf *agbp,
1550 xfs_ino_t parent,
1551 xfs_ino_t *inop)
1552{
1553 struct xfs_mount *mp = tp->t_mountp;
1554 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1555 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1556 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1557 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1558 struct xfs_perag *pag;
ff105f75
DC
1559 struct xfs_btree_cur *cur; /* finobt cursor */
1560 struct xfs_btree_cur *icur; /* inobt cursor */
88fc7306 1561 struct xfs_inobt_rec_incore rec;
88fc7306
BF
1562 xfs_ino_t ino;
1563 int error;
1564 int offset;
ff105f75 1565 int i;
88fc7306
BF
1566
1567 if (!xfs_sb_version_hasfinobt(&mp->m_sb))
ff105f75 1568 return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
88fc7306
BF
1569
1570 pag = xfs_perag_get(mp, agno);
1571
1572 /*
1573 * If pagino is 0 (this is the root inode allocation) use newino.
1574 * This must work because we've just allocated some.
1575 */
1576 if (!pagino)
1577 pagino = be32_to_cpu(agi->agi_newino);
1578
1579 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1580
1581 error = xfs_check_agi_freecount(cur, agi);
1582 if (error)
1583 goto error_cur;
1584
ff105f75
DC
1585 /*
1586 * The search algorithm depends on whether we're in the same AG as the
1587 * parent. If so, find the closest available inode to the parent. If
1588 * not, consider the agi hint or find the first free inode in the AG.
1589 */
1590 if (agno == pagno)
1591 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1592 else
1593 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1594 if (error)
1595 goto error_cur;
88fc7306 1596
01792d3b 1597 offset = xfs_inobt_first_free_inode(&rec);
88fc7306
BF
1598 ASSERT(offset >= 0);
1599 ASSERT(offset < XFS_INODES_PER_CHUNK);
1600 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1601 XFS_INODES_PER_CHUNK) == 0);
1602 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1603
1604 /*
1605 * Modify or remove the finobt record.
1606 */
1607 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1608 rec.ir_freecount--;
ff105f75 1609 if (rec.ir_freecount)
88fc7306
BF
1610 error = xfs_inobt_update(cur, &rec);
1611 else
1612 error = xfs_btree_delete(cur, &i);
1613 if (error)
1614 goto error_cur;
1615
1616 /*
ff105f75
DC
1617 * The finobt has now been updated appropriately. We haven't updated the
1618 * agi and superblock yet, so we can create an inobt cursor and validate
1619 * the original freecount. If all is well, make the equivalent update to
1620 * the inobt using the finobt record and offset information.
88fc7306 1621 */
ff105f75 1622 icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
88fc7306 1623
ff105f75 1624 error = xfs_check_agi_freecount(icur, agi);
88fc7306 1625 if (error)
ff105f75 1626 goto error_icur;
88fc7306 1627
ff105f75 1628 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
88fc7306 1629 if (error)
ff105f75 1630 goto error_icur;
88fc7306
BF
1631
1632 /*
ff105f75
DC
1633 * Both trees have now been updated. We must update the perag and
1634 * superblock before we can check the freecount for each btree.
88fc7306
BF
1635 */
1636 be32_add_cpu(&agi->agi_freecount, -1);
1637 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1638 pag->pagi_freecount--;
1639
1640 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1641
ff105f75 1642 error = xfs_check_agi_freecount(icur, agi);
88fc7306 1643 if (error)
ff105f75 1644 goto error_icur;
88fc7306
BF
1645 error = xfs_check_agi_freecount(cur, agi);
1646 if (error)
ff105f75 1647 goto error_icur;
88fc7306 1648
ff105f75 1649 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
88fc7306
BF
1650 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1651 xfs_perag_put(pag);
1652 *inop = ino;
1653 return 0;
1654
ff105f75
DC
1655error_icur:
1656 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
88fc7306
BF
1657error_cur:
1658 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1659 xfs_perag_put(pag);
1660 return error;
1661}
1662
a2ceac1f
DC
1663/*
1664 * Allocate an inode on disk.
1665 *
1666 * Mode is used to tell whether the new inode will need space, and whether it
1667 * is a directory.
1668 *
1669 * This function is designed to be called twice if it has to do an allocation
1670 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1671 * If an inode is available without having to performn an allocation, an inode
1672 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1673 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1674 * The caller should then commit the current transaction, allocate a
1675 * new transaction, and call xfs_dialloc() again, passing in the previous value
1676 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1677 * buffer is locked across the two calls, the second call is guaranteed to have
1678 * a free inode available.
1679 *
1680 * Once we successfully pick an inode its number is returned and the on-disk
1681 * data structures are updated. The inode itself is not read in, since doing so
1682 * would break ordering constraints with xfs_reclaim.
1683 */
1684int
1685xfs_dialloc(
1686 struct xfs_trans *tp,
1687 xfs_ino_t parent,
1688 umode_t mode,
a2ceac1f
DC
1689 struct xfs_buf **IO_agbp,
1690 xfs_ino_t *inop)
1691{
1692 struct xfs_mount *mp = tp->t_mountp;
1693 struct xfs_buf *agbp;
1694 xfs_agnumber_t agno;
1695 int error;
1696 int ialloced;
1697 int noroom = 0;
1698 xfs_agnumber_t start_agno;
1699 struct xfs_perag *pag;
c1a29e92 1700 int okalloc = 1;
a2ceac1f
DC
1701
1702 if (*IO_agbp) {
1703 /*
1704 * If the caller passes in a pointer to the AGI buffer,
1705 * continue where we left off before. In this case, we
1706 * know that the allocation group has free inodes.
1707 */
1708 agbp = *IO_agbp;
1709 goto out_alloc;
1710 }
1711
1712 /*
1713 * We do not have an agbp, so select an initial allocation
1714 * group for inode allocation.
1715 */
c1a29e92 1716 start_agno = xfs_ialloc_ag_select(tp, parent, mode);
a2ceac1f
DC
1717 if (start_agno == NULLAGNUMBER) {
1718 *inop = NULLFSINO;
1719 return 0;
1720 }
1721
1722 /*
1723 * If we have already hit the ceiling of inode blocks then clear
1724 * okalloc so we scan all available agi structures for a free
1725 * inode.
cbf3beaa
GW
1726 *
1727 * Read rough value of mp->m_icount by percpu_counter_read_positive,
1728 * which will sacrifice the preciseness but improve the performance.
a2ceac1f
DC
1729 */
1730 if (mp->m_maxicount &&
cbf3beaa
GW
1731 percpu_counter_read_positive(&mp->m_icount) + mp->m_ialloc_inos
1732 > mp->m_maxicount) {
a2ceac1f
DC
1733 noroom = 1;
1734 okalloc = 0;
1735 }
1736
1737 /*
1738 * Loop until we find an allocation group that either has free inodes
1739 * or in which we can allocate some inodes. Iterate through the
1740 * allocation groups upward, wrapping at the end.
1741 */
1742 agno = start_agno;
1743 for (;;) {
1744 pag = xfs_perag_get(mp, agno);
1745 if (!pag->pagi_inodeok) {
1746 xfs_ialloc_next_ag(mp);
1747 goto nextag;
1748 }
1749
1750 if (!pag->pagi_init) {
1751 error = xfs_ialloc_pagi_init(mp, tp, agno);
1752 if (error)
1753 goto out_error;
1754 }
1755
1756 /*
1757 * Do a first racy fast path check if this AG is usable.
1758 */
1759 if (!pag->pagi_freecount && !okalloc)
1760 goto nextag;
1761
1762 /*
1763 * Then read in the AGI buffer and recheck with the AGI buffer
1764 * lock held.
1765 */
1766 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1767 if (error)
1768 goto out_error;
1769
1770 if (pag->pagi_freecount) {
1771 xfs_perag_put(pag);
1772 goto out_alloc;
1773 }
1774
1775 if (!okalloc)
1776 goto nextag_relse_buffer;
1777
1778
1779 error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1780 if (error) {
1781 xfs_trans_brelse(tp, agbp);
1782
12b53197 1783 if (error != -ENOSPC)
a2ceac1f
DC
1784 goto out_error;
1785
1786 xfs_perag_put(pag);
1787 *inop = NULLFSINO;
1788 return 0;
1789 }
1790
1791 if (ialloced) {
1792 /*
1793 * We successfully allocated some inodes, return
1794 * the current context to the caller so that it
1795 * can commit the current transaction and call
1796 * us again where we left off.
1797 */
1798 ASSERT(pag->pagi_freecount > 0);
1799 xfs_perag_put(pag);
1800
1801 *IO_agbp = agbp;
1802 *inop = NULLFSINO;
1803 return 0;
1804 }
1805
1806nextag_relse_buffer:
1807 xfs_trans_brelse(tp, agbp);
1808nextag:
1809 xfs_perag_put(pag);
1810 if (++agno == mp->m_sb.sb_agcount)
1811 agno = 0;
1812 if (agno == start_agno) {
1813 *inop = NULLFSINO;
12b53197 1814 return noroom ? -ENOSPC : 0;
a2ceac1f
DC
1815 }
1816 }
1817
1818out_alloc:
1819 *IO_agbp = NULL;
1820 return xfs_dialloc_ag(tp, agbp, parent, inop);
1821out_error:
1822 xfs_perag_put(pag);
1e68581b 1823 return error;
a2ceac1f
DC
1824}
1825
7338c4b8
BF
1826/*
1827 * Free the blocks of an inode chunk. We must consider that the inode chunk
1828 * might be sparse and only free the regions that are allocated as part of the
1829 * chunk.
1830 */
1831STATIC void
1832xfs_difree_inode_chunk(
21375e5d 1833 struct xfs_trans *tp,
7338c4b8 1834 xfs_agnumber_t agno,
21375e5d 1835 struct xfs_inobt_rec_incore *rec)
7338c4b8 1836{
21375e5d
BF
1837 struct xfs_mount *mp = tp->t_mountp;
1838 xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp,
1839 rec->ir_startino);
1840 int startidx, endidx;
1841 int nextbit;
1842 xfs_agblock_t agbno;
1843 int contigblk;
7338c4b8
BF
1844 DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
1845
1846 if (!xfs_inobt_issparse(rec->ir_holemask)) {
1847 /* not sparse, calculate extent info directly */
21375e5d 1848 xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, sagbno),
007347e3 1849 mp->m_ialloc_blks, &XFS_RMAP_OINFO_INODES);
7338c4b8
BF
1850 return;
1851 }
1852
1853 /* holemask is only 16-bits (fits in an unsigned long) */
1854 ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
1855 holemask[0] = rec->ir_holemask;
1856
1857 /*
1858 * Find contiguous ranges of zeroes (i.e., allocated regions) in the
1859 * holemask and convert the start/end index of each range to an extent.
1860 * We start with the start and end index both pointing at the first 0 in
1861 * the mask.
1862 */
1863 startidx = endidx = find_first_zero_bit(holemask,
1864 XFS_INOBT_HOLEMASK_BITS);
1865 nextbit = startidx + 1;
1866 while (startidx < XFS_INOBT_HOLEMASK_BITS) {
1867 nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
1868 nextbit);
1869 /*
1870 * If the next zero bit is contiguous, update the end index of
1871 * the current range and continue.
1872 */
1873 if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
1874 nextbit == endidx + 1) {
1875 endidx = nextbit;
1876 goto next;
1877 }
1878
1879 /*
1880 * nextbit is not contiguous with the current end index. Convert
1881 * the current start/end to an extent and add it to the free
1882 * list.
1883 */
1884 agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
1885 mp->m_sb.sb_inopblock;
1886 contigblk = ((endidx - startidx + 1) *
1887 XFS_INODES_PER_HOLEMASK_BIT) /
1888 mp->m_sb.sb_inopblock;
1889
1890 ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
1891 ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
21375e5d 1892 xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, agbno),
007347e3 1893 contigblk, &XFS_RMAP_OINFO_INODES);
7338c4b8
BF
1894
1895 /* reset range to current bit and carry on... */
1896 startidx = endidx = nextbit;
1897
1898next:
1899 nextbit++;
1900 }
1901}
1902
eb9a297a
BF
1903STATIC int
1904xfs_difree_inobt(
1905 struct xfs_mount *mp,
1906 struct xfs_trans *tp,
1907 struct xfs_buf *agbp,
1908 xfs_agino_t agino,
5a3b2e0a 1909 struct xfs_icluster *xic,
eb9a297a 1910 struct xfs_inobt_rec_incore *orec)
3439d03a 1911{
eb9a297a
BF
1912 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
1913 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1914 struct xfs_perag *pag;
1915 struct xfs_btree_cur *cur;
1916 struct xfs_inobt_rec_incore rec;
1917 int ilen;
1918 int error;
1919 int i;
1920 int off;
3439d03a 1921
3439d03a 1922 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
eb9a297a
BF
1923 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1924
3439d03a
DC
1925 /*
1926 * Initialize the cursor.
1927 */
70eb7337 1928 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
3439d03a
DC
1929
1930 error = xfs_check_agi_freecount(cur, agi);
1931 if (error)
1932 goto error0;
1933
1934 /*
1935 * Look for the entry describing this inode.
1936 */
1937 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
1938 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1939 __func__, error);
1940 goto error0;
1941 }
19ebedcf 1942 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
3439d03a
DC
1943 error = xfs_inobt_get_rec(cur, &rec, &i);
1944 if (error) {
1945 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1946 __func__, error);
1947 goto error0;
1948 }
19ebedcf 1949 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error0);
3439d03a
DC
1950 /*
1951 * Get the offset in the inode chunk.
1952 */
1953 off = agino - rec.ir_startino;
1954 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1955 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1956 /*
1957 * Mark the inode free & increment the count.
1958 */
1959 rec.ir_free |= XFS_INOBT_MASK(off);
1960 rec.ir_freecount++;
1961
1962 /*
f6580bcf
BF
1963 * When an inode chunk is free, it becomes eligible for removal. Don't
1964 * remove the chunk if the block size is large enough for multiple inode
1965 * chunks (that might not be free).
3439d03a
DC
1966 */
1967 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
f6580bcf
BF
1968 rec.ir_free == XFS_INOBT_ALL_FREE &&
1969 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
180d3cd8 1970 xic->deleted = true;
5a3b2e0a
BF
1971 xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1972 xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
3439d03a
DC
1973
1974 /*
1975 * Remove the inode cluster from the AGI B+Tree, adjust the
1976 * AGI and Superblock inode counts, and mark the disk space
1977 * to be freed when the transaction is committed.
1978 */
f6580bcf 1979 ilen = rec.ir_freecount;
3439d03a
DC
1980 be32_add_cpu(&agi->agi_count, -ilen);
1981 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1982 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1983 pag = xfs_perag_get(mp, agno);
1984 pag->pagi_freecount -= ilen - 1;
a3204ee7 1985 pag->pagi_count -= ilen;
3439d03a
DC
1986 xfs_perag_put(pag);
1987 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1988 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1989
1990 if ((error = xfs_btree_delete(cur, &i))) {
1991 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
1992 __func__, error);
1993 goto error0;
1994 }
1995
21375e5d 1996 xfs_difree_inode_chunk(tp, agno, &rec);
3439d03a 1997 } else {
180d3cd8 1998 xic->deleted = false;
3439d03a
DC
1999
2000 error = xfs_inobt_update(cur, &rec);
2001 if (error) {
2002 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
2003 __func__, error);
2004 goto error0;
2005 }
2006
f8149110 2007 /*
3439d03a
DC
2008 * Change the inode free counts and log the ag/sb changes.
2009 */
2010 be32_add_cpu(&agi->agi_freecount, 1);
2011 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
2012 pag = xfs_perag_get(mp, agno);
2013 pag->pagi_freecount++;
2014 xfs_perag_put(pag);
2015 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
2016 }
2017
2018 error = xfs_check_agi_freecount(cur, agi);
2019 if (error)
2020 goto error0;
2021
eb9a297a 2022 *orec = rec;
3439d03a
DC
2023 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2024 return 0;
2025
2026error0:
2027 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2028 return error;
2029}
2030
1bb93fd1
BF
2031/*
2032 * Free an inode in the free inode btree.
2033 */
2034STATIC int
2035xfs_difree_finobt(
2036 struct xfs_mount *mp,
2037 struct xfs_trans *tp,
2038 struct xfs_buf *agbp,
2039 xfs_agino_t agino,
2040 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
2041{
2042 struct xfs_agi *agi = XFS_BUF_TO_AGI(agbp);
2043 xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
2044 struct xfs_btree_cur *cur;
2045 struct xfs_inobt_rec_incore rec;
2046 int offset = agino - ibtrec->ir_startino;
2047 int error;
2048 int i;
2049
2050 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
2051
2052 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
2053 if (error)
2054 goto error;
2055 if (i == 0) {
2056 /*
2057 * If the record does not exist in the finobt, we must have just
2058 * freed an inode in a previously fully allocated chunk. If not,
2059 * something is out of sync.
2060 */
19ebedcf 2061 XFS_WANT_CORRUPTED_GOTO(mp, ibtrec->ir_freecount == 1, error);
1bb93fd1 2062
11640e30
BF
2063 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
2064 ibtrec->ir_count,
2065 ibtrec->ir_freecount,
1bb93fd1
BF
2066 ibtrec->ir_free, &i);
2067 if (error)
2068 goto error;
2069 ASSERT(i == 1);
2070
2071 goto out;
2072 }
2073
2074 /*
ff105f75
DC
2075 * Read and update the existing record. We could just copy the ibtrec
2076 * across here, but that would defeat the purpose of having redundant
2077 * metadata. By making the modifications independently, we can catch
2078 * corruptions that we wouldn't see if we just copied from one record
2079 * to another.
1bb93fd1
BF
2080 */
2081 error = xfs_inobt_get_rec(cur, &rec, &i);
2082 if (error)
2083 goto error;
19ebedcf 2084 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, error);
1bb93fd1
BF
2085
2086 rec.ir_free |= XFS_INOBT_MASK(offset);
2087 rec.ir_freecount++;
2088
19ebedcf 2089 XFS_WANT_CORRUPTED_GOTO(mp, (rec.ir_free == ibtrec->ir_free) &&
1bb93fd1
BF
2090 (rec.ir_freecount == ibtrec->ir_freecount),
2091 error);
2092
2093 /*
2094 * The content of inobt records should always match between the inobt
2095 * and finobt. The lifecycle of records in the finobt is different from
2096 * the inobt in that the finobt only tracks records with at least one
ff105f75
DC
2097 * free inode. Hence, if all of the inodes are free and we aren't
2098 * keeping inode chunks permanently on disk, remove the record.
2099 * Otherwise, update the record with the new information.
f6580bcf
BF
2100 *
2101 * Note that we currently can't free chunks when the block size is large
2102 * enough for multiple chunks. Leave the finobt record to remain in sync
2103 * with the inobt.
1bb93fd1 2104 */
f6580bcf
BF
2105 if (rec.ir_free == XFS_INOBT_ALL_FREE &&
2106 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
1bb93fd1 2107 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
1bb93fd1
BF
2108 error = xfs_btree_delete(cur, &i);
2109 if (error)
2110 goto error;
2111 ASSERT(i == 1);
2112 } else {
1bb93fd1
BF
2113 error = xfs_inobt_update(cur, &rec);
2114 if (error)
2115 goto error;
2116 }
2117
2118out:
2119 error = xfs_check_agi_freecount(cur, agi);
2120 if (error)
2121 goto error;
2122
2123 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2124 return 0;
2125
2126error:
2127 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2128 return error;
2129}
2130
eb9a297a
BF
2131/*
2132 * Free disk inode. Carefully avoids touching the incore inode, all
2133 * manipulations incore are the caller's responsibility.
2134 * The on-disk inode is not changed by this operation, only the
2135 * btree (free inode mask) is changed.
2136 */
2137int
2138xfs_difree(
2139 struct xfs_trans *tp, /* transaction pointer */
2140 xfs_ino_t inode, /* inode to be freed */
5a3b2e0a 2141 struct xfs_icluster *xic) /* cluster info if deleted */
eb9a297a
BF
2142{
2143 /* REFERENCED */
2144 xfs_agblock_t agbno; /* block number containing inode */
2145 struct xfs_buf *agbp; /* buffer for allocation group header */
2146 xfs_agino_t agino; /* allocation group inode number */
2147 xfs_agnumber_t agno; /* allocation group number */
2148 int error; /* error return value */
2149 struct xfs_mount *mp; /* mount structure for filesystem */
2150 struct xfs_inobt_rec_incore rec;/* btree record */
2151
2152 mp = tp->t_mountp;
2153
2154 /*
2155 * Break up inode number into its components.
2156 */
2157 agno = XFS_INO_TO_AGNO(mp, inode);
2158 if (agno >= mp->m_sb.sb_agcount) {
2159 xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
2160 __func__, agno, mp->m_sb.sb_agcount);
2161 ASSERT(0);
12b53197 2162 return -EINVAL;
eb9a297a
BF
2163 }
2164 agino = XFS_INO_TO_AGINO(mp, inode);
2165 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
2166 xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
2167 __func__, (unsigned long long)inode,
2168 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
2169 ASSERT(0);
12b53197 2170 return -EINVAL;
eb9a297a
BF
2171 }
2172 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2173 if (agbno >= mp->m_sb.sb_agblocks) {
2174 xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
2175 __func__, agbno, mp->m_sb.sb_agblocks);
2176 ASSERT(0);
12b53197 2177 return -EINVAL;
eb9a297a
BF
2178 }
2179 /*
2180 * Get the allocation group header.
2181 */
2182 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2183 if (error) {
2184 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
2185 __func__, error);
2186 return error;
2187 }
2188
2189 /*
2190 * Fix up the inode allocation btree.
2191 */
c5f95473 2192 error = xfs_difree_inobt(mp, tp, agbp, agino, xic, &rec);
eb9a297a
BF
2193 if (error)
2194 goto error0;
2195
1bb93fd1
BF
2196 /*
2197 * Fix up the free inode btree.
2198 */
2199 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
2200 error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
2201 if (error)
2202 goto error0;
2203 }
2204
eb9a297a
BF
2205 return 0;
2206
2207error0:
2208 return error;
2209}
2210
56b2de80
DC
2211STATIC int
2212xfs_imap_lookup(
2213 struct xfs_mount *mp,
2214 struct xfs_trans *tp,
2215 xfs_agnumber_t agno,
2216 xfs_agino_t agino,
2217 xfs_agblock_t agbno,
2218 xfs_agblock_t *chunk_agbno,
2219 xfs_agblock_t *offset_agbno,
2220 int flags)
2221{
2222 struct xfs_inobt_rec_incore rec;
2223 struct xfs_btree_cur *cur;
2224 struct xfs_buf *agbp;
2225 int error;
2226 int i;
2227
2228 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2229 if (error) {
a2ceac1f
DC
2230 xfs_alert(mp,
2231 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
2232 __func__, error, agno);
56b2de80
DC
2233 return error;
2234 }
2235
2236 /*
2237 * Lookup the inode record for the given agino. If the record cannot be
2238 * found, then it's an invalid inode number and we should abort. Once
2239 * we have a record, we need to ensure it contains the inode number
2240 * we are looking up.
2241 */
70eb7337 2242 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
56b2de80
DC
2243 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
2244 if (!error) {
2245 if (i)
2246 error = xfs_inobt_get_rec(cur, &rec, &i);
2247 if (!error && i == 0)
12b53197 2248 error = -EINVAL;
56b2de80
DC
2249 }
2250
2251 xfs_trans_brelse(tp, agbp);
660265b7 2252 xfs_btree_del_cursor(cur, error);
56b2de80
DC
2253 if (error)
2254 return error;
2255
2256 /* check that the returned record contains the required inode */
2257 if (rec.ir_startino > agino ||
ff105f75 2258 rec.ir_startino + mp->m_ialloc_inos <= agino)
12b53197 2259 return -EINVAL;
56b2de80
DC
2260
2261 /* for untrusted inodes check it is allocated first */
2262 if ((flags & XFS_IGET_UNTRUSTED) &&
2263 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
12b53197 2264 return -EINVAL;
56b2de80
DC
2265
2266 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
2267 *offset_agbno = agbno - *chunk_agbno;
2268 return 0;
2269}
2bd0ea18
NS
2270
2271/*
56b2de80 2272 * Return the location of the inode in imap, for mapping it into a buffer.
2bd0ea18 2273 */
2bd0ea18 2274int
56b2de80
DC
2275xfs_imap(
2276 xfs_mount_t *mp, /* file system mount structure */
2277 xfs_trans_t *tp, /* transaction pointer */
2bd0ea18 2278 xfs_ino_t ino, /* inode to locate */
56b2de80
DC
2279 struct xfs_imap *imap, /* location map structure */
2280 uint flags) /* flags for inode btree lookup */
2bd0ea18
NS
2281{
2282 xfs_agblock_t agbno; /* block number of inode in the alloc group */
2bd0ea18
NS
2283 xfs_agino_t agino; /* inode number within alloc group */
2284 xfs_agnumber_t agno; /* allocation group number */
2bd0ea18 2285 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
2bd0ea18 2286 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
2bd0ea18 2287 int error; /* error code */
dfc130f3 2288 int offset; /* index of inode in its buffer */
6bddecbc 2289 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
2bd0ea18
NS
2290
2291 ASSERT(ino != NULLFSINO);
56b2de80 2292
2bd0ea18
NS
2293 /*
2294 * Split up the inode number into its parts.
2295 */
2296 agno = XFS_INO_TO_AGNO(mp, ino);
2297 agino = XFS_INO_TO_AGINO(mp, ino);
2298 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2299 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
63518810
NS
2300 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2301#ifdef DEBUG
56b2de80
DC
2302 /*
2303 * Don't output diagnostic information for untrusted inodes
2304 * as they can be invalid without implying corruption.
2305 */
2306 if (flags & XFS_IGET_UNTRUSTED)
12b53197 2307 return -EINVAL;
5000d01d 2308 if (agno >= mp->m_sb.sb_agcount) {
a2ceac1f
DC
2309 xfs_alert(mp,
2310 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
2311 __func__, agno, mp->m_sb.sb_agcount);
63518810
NS
2312 }
2313 if (agbno >= mp->m_sb.sb_agblocks) {
a2ceac1f
DC
2314 xfs_alert(mp,
2315 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
2316 __func__, (unsigned long long)agbno,
2317 (unsigned long)mp->m_sb.sb_agblocks);
63518810
NS
2318 }
2319 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
a2ceac1f
DC
2320 xfs_alert(mp,
2321 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
2322 __func__, ino,
2323 XFS_AGINO_TO_INO(mp, agno, agino));
63518810 2324 }
5e656dbb 2325 xfs_stack_trace();
63518810 2326#endif /* DEBUG */
12b53197 2327 return -EINVAL;
63518810 2328 }
56b2de80 2329
56b2de80
DC
2330 /*
2331 * For bulkstat and handle lookups, we have an untrusted inode number
2332 * that we have to verify is valid. We cannot do this just by reading
2333 * the inode buffer as it may have been unlinked and removed leaving
2334 * inodes in stale state on disk. Hence we have to do a btree lookup
2335 * in all cases where an untrusted inode number is passed.
2336 */
2337 if (flags & XFS_IGET_UNTRUSTED) {
2338 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2339 &chunk_agbno, &offset_agbno, flags);
2340 if (error)
2341 return error;
2342 goto out_map;
2343 }
2344
2345 /*
2346 * If the inode cluster size is the same as the blocksize or
2347 * smaller we get to the buffer by simple arithmetics.
2348 */
42d896cf 2349 if (mp->m_blocks_per_cluster == 1) {
2bd0ea18
NS
2350 offset = XFS_INO_TO_OFFSET(mp, ino);
2351 ASSERT(offset < mp->m_sb.sb_inopblock);
56b2de80
DC
2352
2353 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
2354 imap->im_len = XFS_FSB_TO_BB(mp, 1);
187f8c22
DW
2355 imap->im_boffset = (unsigned short)(offset <<
2356 mp->m_sb.sb_inodelog);
2bd0ea18
NS
2357 return 0;
2358 }
56b2de80
DC
2359
2360 /*
2361 * If the inode chunks are aligned then use simple maths to
2362 * find the location. Otherwise we have to do a btree
2363 * lookup to find the location.
2364 */
2bd0ea18
NS
2365 if (mp->m_inoalign_mask) {
2366 offset_agbno = agbno & mp->m_inoalign_mask;
2367 chunk_agbno = agbno - offset_agbno;
2368 } else {
56b2de80
DC
2369 error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2370 &chunk_agbno, &offset_agbno, flags);
2bd0ea18
NS
2371 if (error)
2372 return error;
2bd0ea18 2373 }
56b2de80
DC
2374
2375out_map:
2bd0ea18
NS
2376 ASSERT(agbno >= chunk_agbno);
2377 cluster_agbno = chunk_agbno +
42d896cf
DW
2378 ((offset_agbno / mp->m_blocks_per_cluster) *
2379 mp->m_blocks_per_cluster);
2bd0ea18
NS
2380 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
2381 XFS_INO_TO_OFFSET(mp, ino);
56b2de80
DC
2382
2383 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
42d896cf 2384 imap->im_len = XFS_FSB_TO_BB(mp, mp->m_blocks_per_cluster);
919dbc78 2385 imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
56b2de80
DC
2386
2387 /*
2388 * If the inode number maps to a block outside the bounds
2389 * of the file system then return NULL rather than calling
2390 * read_buf and panicing when we get an error from the
2391 * driver.
2392 */
2393 if ((imap->im_blkno + imap->im_len) >
2394 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
a2ceac1f
DC
2395 xfs_alert(mp,
2396 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
2397 __func__, (unsigned long long) imap->im_blkno,
56b2de80
DC
2398 (unsigned long long) imap->im_len,
2399 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
12b53197 2400 return -EINVAL;
56b2de80 2401 }
2bd0ea18 2402 return 0;
2bd0ea18
NS
2403}
2404
2405/*
2406 * Compute and fill in value of m_in_maxlevels.
2407 */
2408void
2409xfs_ialloc_compute_maxlevels(
2410 xfs_mount_t *mp) /* file system mount structure */
2411{
730e2a19
DW
2412 uint inodes;
2413
2414 inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
1421de38 2415 mp->m_in_maxlevels = xfs_btree_compute_maxlevels(mp->m_inobt_mnr,
730e2a19 2416 inodes);
2bd0ea18
NS
2417}
2418
2419/*
ff105f75
DC
2420 * Log specified fields for the ag hdr (inode section). The growth of the agi
2421 * structure over time requires that we interpret the buffer as two logical
2422 * regions delineated by the end of the unlinked list. This is due to the size
2423 * of the hash table and its location in the middle of the agi.
2424 *
2425 * For example, a request to log a field before agi_unlinked and a field after
2426 * agi_unlinked could cause us to log the entire hash table and use an excessive
2427 * amount of log space. To avoid this behavior, log the region up through
2428 * agi_unlinked in one call and the region after agi_unlinked through the end of
2429 * the structure in another.
2bd0ea18
NS
2430 */
2431void
2432xfs_ialloc_log_agi(
2433 xfs_trans_t *tp, /* transaction pointer */
2434 xfs_buf_t *bp, /* allocation group header buffer */
2435 int fields) /* bitmask of fields to log */
2436{
2437 int first; /* first byte number */
2438 int last; /* last byte number */
2439 static const short offsets[] = { /* field starting offsets */
2440 /* keep in sync with bit definitions */
2441 offsetof(xfs_agi_t, agi_magicnum),
2442 offsetof(xfs_agi_t, agi_versionnum),
2443 offsetof(xfs_agi_t, agi_seqno),
2444 offsetof(xfs_agi_t, agi_length),
2445 offsetof(xfs_agi_t, agi_count),
2446 offsetof(xfs_agi_t, agi_root),
2447 offsetof(xfs_agi_t, agi_level),
2448 offsetof(xfs_agi_t, agi_freecount),
2449 offsetof(xfs_agi_t, agi_newino),
2450 offsetof(xfs_agi_t, agi_dirino),
2451 offsetof(xfs_agi_t, agi_unlinked),
c0a4c227
BF
2452 offsetof(xfs_agi_t, agi_free_root),
2453 offsetof(xfs_agi_t, agi_free_level),
2bd0ea18
NS
2454 sizeof(xfs_agi_t)
2455 };
2456#ifdef DEBUG
2457 xfs_agi_t *agi; /* allocation group header */
2458
2459 agi = XFS_BUF_TO_AGI(bp);
a2ceac1f 2460 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
2bd0ea18 2461#endif
ff105f75 2462
2bd0ea18 2463 /*
c0a4c227 2464 * Compute byte offsets for the first and last fields in the first
ff105f75
DC
2465 * region and log the agi buffer. This only logs up through
2466 * agi_unlinked.
2bd0ea18 2467 */
c0a4c227
BF
2468 if (fields & XFS_AGI_ALL_BITS_R1) {
2469 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2470 &first, &last);
2471 xfs_trans_log_buf(tp, bp, first, last);
2472 }
2473
2474 /*
ff105f75
DC
2475 * Mask off the bits in the first region and calculate the first and
2476 * last field offsets for any bits in the second region.
c0a4c227
BF
2477 */
2478 fields &= ~XFS_AGI_ALL_BITS_R1;
2479 if (fields) {
2480 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2481 &first, &last);
2482 xfs_trans_log_buf(tp, bp, first, last);
2483 }
2bd0ea18
NS
2484}
2485
bc01119d 2486static xfs_failaddr_t
a2ceac1f
DC
2487xfs_agi_verify(
2488 struct xfs_buf *bp)
2489{
2490 struct xfs_mount *mp = bp->b_target->bt_mount;
2491 struct xfs_agi *agi = XFS_BUF_TO_AGI(bp);
2d653064 2492 int i;
a2ceac1f 2493
a65d8d29
BF
2494 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2495 if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
bc01119d 2496 return __this_address;
a65d8d29
BF
2497 if (!xfs_log_check_lsn(mp,
2498 be64_to_cpu(XFS_BUF_TO_AGI(bp)->agi_lsn)))
bc01119d 2499 return __this_address;
a65d8d29
BF
2500 }
2501
a2ceac1f
DC
2502 /*
2503 * Validate the magic number of the agi block.
2504 */
dd5b876e 2505 if (agi->agi_magicnum != cpu_to_be32(XFS_AGI_MAGIC))
bc01119d 2506 return __this_address;
dd5b876e 2507 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
bc01119d 2508 return __this_address;
a2ceac1f 2509
00795aae
DW
2510 if (be32_to_cpu(agi->agi_level) < 1 ||
2511 be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
bc01119d 2512 return __this_address;
00795aae
DW
2513
2514 if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
2515 (be32_to_cpu(agi->agi_free_level) < 1 ||
2516 be32_to_cpu(agi->agi_free_level) > XFS_BTREE_MAXLEVELS))
bc01119d 2517 return __this_address;
00795aae 2518
a2ceac1f
DC
2519 /*
2520 * during growfs operations, the perag is not fully initialised,
2521 * so we can't use it for any useful checking. growfs ensures we can't
2522 * use it by using uncached buffers that don't have the perag attached
2523 * so we can detect and avoid this problem.
2524 */
dd5b876e 2525 if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
bc01119d 2526 return __this_address;
a2ceac1f 2527
2d653064 2528 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
e66d0a46 2529 if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO))
2d653064
DC
2530 continue;
2531 if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
2532 return __this_address;
2533 }
2534
bc01119d 2535 return NULL;
a2ceac1f
DC
2536}
2537
2538static void
2539xfs_agi_read_verify(
2540 struct xfs_buf *bp)
2541{
dd5b876e 2542 struct xfs_mount *mp = bp->b_target->bt_mount;
1e697959 2543 xfs_failaddr_t fa;
dd5b876e 2544
45922933
DC
2545 if (xfs_sb_version_hascrc(&mp->m_sb) &&
2546 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
1e697959
DW
2547 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2548 else {
2549 fa = xfs_agi_verify(bp);
2550 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2551 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2552 }
a2ceac1f
DC
2553}
2554
2555static void
2556xfs_agi_write_verify(
2557 struct xfs_buf *bp)
2558{
37d086ca
CM
2559 struct xfs_mount *mp = bp->b_target->bt_mount;
2560 struct xfs_buf_log_item *bip = bp->b_log_item;
1e697959 2561 xfs_failaddr_t fa;
dd5b876e 2562
1e697959
DW
2563 fa = xfs_agi_verify(bp);
2564 if (fa) {
2565 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
dd5b876e
DC
2566 return;
2567 }
2568
2569 if (!xfs_sb_version_hascrc(&mp->m_sb))
2570 return;
2571
2572 if (bip)
2573 XFS_BUF_TO_AGI(bp)->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
43b5aeed 2574 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
a2ceac1f
DC
2575}
2576
2577const struct xfs_buf_ops xfs_agi_buf_ops = {
a3fac935 2578 .name = "xfs_agi",
a2ceac1f
DC
2579 .verify_read = xfs_agi_read_verify,
2580 .verify_write = xfs_agi_write_verify,
95d9582b 2581 .verify_struct = xfs_agi_verify,
a2ceac1f
DC
2582};
2583
2bd0ea18
NS
2584/*
2585 * Read in the allocation group header (inode allocation section)
2586 */
2587int
56b2de80
DC
2588xfs_read_agi(
2589 struct xfs_mount *mp, /* file system mount structure */
2590 struct xfs_trans *tp, /* transaction pointer */
2591 xfs_agnumber_t agno, /* allocation group number */
2592 struct xfs_buf **bpp) /* allocation group hdr buf */
2bd0ea18 2593{
56b2de80 2594 int error;
2bd0ea18 2595
ff105f75 2596 trace_xfs_read_agi(mp, agno);
56b2de80 2597
ff105f75 2598 ASSERT(agno != NULLAGNUMBER);
56b2de80 2599 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
9440d84d 2600 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
a2ceac1f 2601 XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
9440d84d 2602 if (error)
2bd0ea18 2603 return error;
1b521475
ES
2604 if (tp)
2605 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF);
56b2de80 2606
a2ceac1f 2607 xfs_buf_set_ref(*bpp, XFS_AGI_REF);
56b2de80
DC
2608 return 0;
2609}
2610
2611int
2612xfs_ialloc_read_agi(
2613 struct xfs_mount *mp, /* file system mount structure */
2614 struct xfs_trans *tp, /* transaction pointer */
2615 xfs_agnumber_t agno, /* allocation group number */
2616 struct xfs_buf **bpp) /* allocation group hdr buf */
2617{
2618 struct xfs_agi *agi; /* allocation group header */
2619 struct xfs_perag *pag; /* per allocation group data */
2620 int error;
2621
ff105f75
DC
2622 trace_xfs_ialloc_read_agi(mp, agno);
2623
56b2de80
DC
2624 error = xfs_read_agi(mp, tp, agno, bpp);
2625 if (error)
2626 return error;
2627
2628 agi = XFS_BUF_TO_AGI(*bpp);
2629 pag = xfs_perag_get(mp, agno);
2bd0ea18 2630 if (!pag->pagi_init) {
6e3140c7 2631 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
cdded3d8 2632 pag->pagi_count = be32_to_cpu(agi->agi_count);
2bd0ea18 2633 pag->pagi_init = 1;
9440d84d 2634 }
9440d84d 2635
56b2de80
DC
2636 /*
2637 * It's possible for these to be out of sync if
2638 * we are in the middle of a forced shutdown.
2639 */
2640 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2641 XFS_FORCED_SHUTDOWN(mp));
2642 xfs_perag_put(pag);
2bd0ea18
NS
2643 return 0;
2644}
cdded3d8
DC
2645
2646/*
2647 * Read in the agi to initialise the per-ag data in the mount structure
2648 */
2649int
2650xfs_ialloc_pagi_init(
2651 xfs_mount_t *mp, /* file system mount structure */
2652 xfs_trans_t *tp, /* transaction pointer */
2653 xfs_agnumber_t agno) /* allocation group number */
2654{
2655 xfs_buf_t *bp = NULL;
2656 int error;
2657
5e656dbb
BN
2658 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2659 if (error)
cdded3d8
DC
2660 return error;
2661 if (bp)
2662 xfs_trans_brelse(tp, bp);
2663 return 0;
2664}
be3bf25c 2665
9deb7f65
DW
2666/* Is there an inode record covering a given range of inode numbers? */
2667int
2668xfs_ialloc_has_inode_record(
2669 struct xfs_btree_cur *cur,
2670 xfs_agino_t low,
2671 xfs_agino_t high,
2672 bool *exists)
2673{
2674 struct xfs_inobt_rec_incore irec;
2675 xfs_agino_t agino;
2676 uint16_t holemask;
2677 int has_record;
2678 int i;
2679 int error;
2680
2681 *exists = false;
2682 error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
2683 while (error == 0 && has_record) {
2684 error = xfs_inobt_get_rec(cur, &irec, &has_record);
2685 if (error || irec.ir_startino > high)
2686 break;
2687
2688 agino = irec.ir_startino;
2689 holemask = irec.ir_holemask;
2690 for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1,
2691 i++, agino += XFS_INODES_PER_HOLEMASK_BIT) {
2692 if (holemask & 1)
2693 continue;
2694 if (agino + XFS_INODES_PER_HOLEMASK_BIT > low &&
2695 agino <= high) {
2696 *exists = true;
2697 return 0;
2698 }
2699 }
2700
2701 error = xfs_btree_increment(cur, 0, &has_record);
2702 }
2703 return error;
2704}
2705
2706/* Is there an inode record covering a given extent? */
2707int
2708xfs_ialloc_has_inodes_at_extent(
2709 struct xfs_btree_cur *cur,
2710 xfs_agblock_t bno,
2711 xfs_extlen_t len,
2712 bool *exists)
2713{
2714 xfs_agino_t low;
2715 xfs_agino_t high;
2716
7516da71
DW
2717 low = XFS_AGB_TO_AGINO(cur->bc_mp, bno);
2718 high = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1;
9deb7f65
DW
2719
2720 return xfs_ialloc_has_inode_record(cur, low, high, exists);
2721}
2722
2723struct xfs_ialloc_count_inodes {
2724 xfs_agino_t count;
2725 xfs_agino_t freecount;
2726};
2727
2728/* Record inode counts across all inobt records. */
2729STATIC int
2730xfs_ialloc_count_inodes_rec(
2731 struct xfs_btree_cur *cur,
2732 union xfs_btree_rec *rec,
2733 void *priv)
2734{
2735 struct xfs_inobt_rec_incore irec;
2736 struct xfs_ialloc_count_inodes *ci = priv;
2737
2738 xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
2739 ci->count += irec.ir_count;
2740 ci->freecount += irec.ir_freecount;
2741
2742 return 0;
2743}
2744
2745/* Count allocated and free inodes under an inobt. */
2746int
2747xfs_ialloc_count_inodes(
2748 struct xfs_btree_cur *cur,
2749 xfs_agino_t *count,
2750 xfs_agino_t *freecount)
2751{
2752 struct xfs_ialloc_count_inodes ci = {0};
2753 int error;
2754
2755 ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
2756 error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
2757 if (error)
2758 return error;
2759
2760 *count = ci.count;
2761 *freecount = ci.freecount;
2762 return 0;
2763}