]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_ialloc.c
xfsprogs: Release v6.15.0
[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"
b626fb59
DC
13#include "xfs_mount.h"
14#include "xfs_inode.h"
15#include "xfs_btree.h"
16#include "xfs_ialloc.h"
17#include "xfs_ialloc_btree.h"
18#include "xfs_alloc.h"
2cf10e4c 19#include "xfs_errortag.h"
b626fb59 20#include "xfs_bmap.h"
b626fb59
DC
21#include "xfs_trans.h"
22#include "xfs_trace.h"
85aec44f 23#include "xfs_rmap.h"
f93d2173 24#include "xfs_ag.h"
ad6fabbe 25#include "xfs_health.h"
2bd0ea18 26
b194c7d8 27/*
56b2de80 28 * Lookup a record by ino in the btree given by cur.
b194c7d8
BN
29 */
30int /* error */
56b2de80 31xfs_inobt_lookup(
b194c7d8
BN
32 struct xfs_btree_cur *cur, /* btree cursor */
33 xfs_agino_t ino, /* starting inode of chunk */
56b2de80 34 xfs_lookup_t dir, /* <=, >=, == */
b194c7d8
BN
35 int *stat) /* success/failure */
36{
37 cur->bc_rec.i.ir_startino = ino;
11640e30
BF
38 cur->bc_rec.i.ir_holemask = 0;
39 cur->bc_rec.i.ir_count = 0;
56b2de80
DC
40 cur->bc_rec.i.ir_freecount = 0;
41 cur->bc_rec.i.ir_free = 0;
42 return xfs_btree_lookup(cur, dir, stat);
b194c7d8
BN
43}
44
45/*
56b2de80 46 * Update the record referred to by cur to the value given.
b194c7d8
BN
47 * This either works (return 0) or gets an EFSCORRUPTED error.
48 */
49STATIC int /* error */
50xfs_inobt_update(
51 struct xfs_btree_cur *cur, /* btree cursor */
56b2de80 52 xfs_inobt_rec_incore_t *irec) /* btree record */
b194c7d8
BN
53{
54 union xfs_btree_rec rec;
55
56b2de80 56 rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
b16a427a 57 if (xfs_has_sparseinodes(cur->bc_mp)) {
11640e30
BF
58 rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
59 rec.inobt.ir_u.sp.ir_count = irec->ir_count;
60 rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
61 } else {
62 /* ir_holemask/ir_count not supported on-disk */
63 rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
64 }
56b2de80 65 rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
b194c7d8
BN
66 return xfs_btree_update(cur, &rec);
67}
68
b5f6747c
DW
69/* Convert on-disk btree record to incore inobt record. */
70void
71xfs_inobt_btrec_to_irec(
72 struct xfs_mount *mp,
e62318a3 73 const union xfs_btree_rec *rec,
b5f6747c 74 struct xfs_inobt_rec_incore *irec)
b194c7d8 75{
11640e30 76 irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
b16a427a 77 if (xfs_has_sparseinodes(mp)) {
11640e30
BF
78 irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
79 irec->ir_count = rec->inobt.ir_u.sp.ir_count;
80 irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
81 } else {
82 /*
83 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
84 * values for full inode chunks.
85 */
86 irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
87 irec->ir_count = XFS_INODES_PER_CHUNK;
88 irec->ir_freecount =
89 be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
b194c7d8 90 }
11640e30 91 irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
b5f6747c
DW
92}
93
5f8e3af0
DW
94/* Compute the freecount of an incore inode record. */
95uint8_t
96xfs_inobt_rec_freecount(
97 const struct xfs_inobt_rec_incore *irec)
98{
99 uint64_t realfree = irec->ir_free;
100
101 if (xfs_inobt_issparse(irec->ir_holemask))
102 realfree &= xfs_inobt_irec_to_allocmask(irec);
103 return hweight64(realfree);
104}
105
349aa687
DW
106/* Simple checks for inode records. */
107xfs_failaddr_t
108xfs_inobt_check_irec(
5f8e3af0 109 struct xfs_perag *pag,
349aa687
DW
110 const struct xfs_inobt_rec_incore *irec)
111{
71ba9fcc 112 /* Record has to be properly aligned within the AG. */
5f8e3af0 113 if (!xfs_verify_agino(pag, irec->ir_startino))
349aa687 114 return __this_address;
5f8e3af0 115 if (!xfs_verify_agino(pag,
71ba9fcc
DW
116 irec->ir_startino + XFS_INODES_PER_CHUNK - 1))
117 return __this_address;
349aa687
DW
118 if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
119 irec->ir_count > XFS_INODES_PER_CHUNK)
120 return __this_address;
121 if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
122 return __this_address;
123
5f8e3af0 124 if (xfs_inobt_rec_freecount(irec) != irec->ir_freecount)
349aa687
DW
125 return __this_address;
126
127 return NULL;
128}
129
e70bf9ba
DW
130static inline int
131xfs_inobt_complain_bad_rec(
132 struct xfs_btree_cur *cur,
133 xfs_failaddr_t fa,
134 const struct xfs_inobt_rec_incore *irec)
135{
136 struct xfs_mount *mp = cur->bc_mp;
137
138 xfs_warn(mp,
b85f26f6 139 "%sbt record corruption in AG %d detected at %pS!",
1fa5e300 140 cur->bc_ops->name, cur->bc_group->xg_gno, fa);
e70bf9ba
DW
141 xfs_warn(mp,
142"start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
143 irec->ir_startino, irec->ir_count, irec->ir_freecount,
144 irec->ir_free, irec->ir_holemask);
e30bad41 145 xfs_btree_mark_sick(cur);
e70bf9ba
DW
146 return -EFSCORRUPTED;
147}
148
b5f6747c
DW
149/*
150 * Get the data from the pointed-to record.
151 */
152int
153xfs_inobt_get_rec(
154 struct xfs_btree_cur *cur,
155 struct xfs_inobt_rec_incore *irec,
156 int *stat)
157{
ec291989 158 struct xfs_mount *mp = cur->bc_mp;
b5f6747c 159 union xfs_btree_rec *rec;
349aa687 160 xfs_failaddr_t fa;
b5f6747c
DW
161 int error;
162
163 error = xfs_btree_get_rec(cur, &rec, stat);
164 if (error || *stat == 0)
165 return error;
166
ec291989 167 xfs_inobt_btrec_to_irec(mp, rec, irec);
1fa5e300 168 fa = xfs_inobt_check_irec(to_perag(cur->bc_group), irec);
349aa687 169 if (fa)
e70bf9ba 170 return xfs_inobt_complain_bad_rec(cur, fa, irec);
11640e30
BF
171
172 return 0;
b194c7d8
BN
173}
174
3c699279
BF
175/*
176 * Insert a single inobt record. Cursor must already point to desired location.
177 */
aa465192 178int
3c699279
BF
179xfs_inobt_insert_rec(
180 struct xfs_btree_cur *cur,
4a492e72
DW
181 uint16_t holemask,
182 uint8_t count,
183 int32_t freecount,
3c699279
BF
184 xfs_inofree_t free,
185 int *stat)
186{
11640e30
BF
187 cur->bc_rec.i.ir_holemask = holemask;
188 cur->bc_rec.i.ir_count = count;
3c699279
BF
189 cur->bc_rec.i.ir_freecount = freecount;
190 cur->bc_rec.i.ir_free = free;
191 return xfs_btree_insert(cur, stat);
192}
193
194/*
195 * Insert records describing a newly allocated inode chunk into the inobt.
196 */
197STATIC int
198xfs_inobt_insert(
90e549b5 199 struct xfs_perag *pag,
3c699279
BF
200 struct xfs_trans *tp,
201 struct xfs_buf *agbp,
202 xfs_agino_t newino,
203 xfs_agino_t newlen,
53430ebb 204 bool is_finobt)
3c699279
BF
205{
206 struct xfs_btree_cur *cur;
3c699279
BF
207 xfs_agino_t thisino;
208 int i;
209 int error;
210
53430ebb 211 if (is_finobt)
7199f984
CH
212 cur = xfs_finobt_init_cursor(pag, tp, agbp);
213 else
214 cur = xfs_inobt_init_cursor(pag, tp, agbp);
3c699279
BF
215
216 for (thisino = newino;
217 thisino < newino + newlen;
218 thisino += XFS_INODES_PER_CHUNK) {
219 error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
220 if (error) {
221 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
222 return error;
223 }
224 ASSERT(i == 0);
225
11640e30
BF
226 error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
227 XFS_INODES_PER_CHUNK,
228 XFS_INODES_PER_CHUNK,
3c699279
BF
229 XFS_INOBT_ALL_FREE, &i);
230 if (error) {
231 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
232 return error;
233 }
234 ASSERT(i == 1);
235 }
236
237 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
238
239 return 0;
240}
241
56b2de80
DC
242/*
243 * Verify that the number of free inodes in the AGI is correct.
244 */
245#ifdef DEBUG
1dbf114f 246static int
56b2de80 247xfs_check_agi_freecount(
1dbf114f 248 struct xfs_btree_cur *cur)
56b2de80
DC
249{
250 if (cur->bc_nlevels == 1) {
251 xfs_inobt_rec_incore_t rec;
252 int freecount = 0;
253 int error;
254 int i;
255
256 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
257 if (error)
258 return error;
259
260 do {
261 error = xfs_inobt_get_rec(cur, &rec, &i);
262 if (error)
263 return error;
264
265 if (i) {
266 freecount += rec.ir_freecount;
267 error = xfs_btree_increment(cur, 0, &i);
268 if (error)
269 return error;
270 }
271 } while (i == 1);
272
1fa5e300
CH
273 if (!xfs_is_shutdown(cur->bc_mp)) {
274 ASSERT(freecount ==
275 to_perag(cur->bc_group)->pagi_freecount);
276 }
56b2de80
DC
277 }
278 return 0;
279}
280#else
1dbf114f 281#define xfs_check_agi_freecount(cur) 0
56b2de80
DC
282#endif
283
284/*
e9d35108
DC
285 * Initialise a new set of inodes. When called without a transaction context
286 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
287 * than logging them (which in a transaction context puts them into the AIL
288 * for writeback rather than the xfsbufd queue).
56b2de80 289 */
e9d35108 290int
56b2de80
DC
291xfs_ialloc_inode_init(
292 struct xfs_mount *mp,
293 struct xfs_trans *tp,
e9d35108 294 struct list_head *buffer_list,
fe8d48ac 295 int icount,
56b2de80
DC
296 xfs_agnumber_t agno,
297 xfs_agblock_t agbno,
298 xfs_agblock_t length,
299 unsigned int gen)
300{
301 struct xfs_buf *fbuf;
302 struct xfs_dinode *free;
42d896cf 303 int nbufs;
56b2de80
DC
304 int version;
305 int i, j;
306 xfs_daddr_t d;
41ce5f36 307 xfs_ino_t ino = 0;
7f15a547 308 int error;
56b2de80
DC
309
310 /*
ff105f75
DC
311 * Loop over the new block(s), filling in the inodes. For small block
312 * sizes, manipulate the inodes in buffers which are multiples of the
313 * blocks size.
56b2de80 314 */
e7fd2b6f 315 nbufs = length / M_IGEO(mp)->blocks_per_cluster;
56b2de80
DC
316
317 /*
e9d35108
DC
318 * Figure out what version number to use in the inodes we create. If
319 * the superblock version has caught up to the one that supports the new
320 * inode format, then use the new inode version. Otherwise use the old
321 * version so that old kernels will continue to be able to use the file
322 * system.
41ce5f36
DC
323 *
324 * For v3 inodes, we also need to write the inode number into the inode,
325 * so calculate the first inode number of the chunk here as
7516da71 326 * XFS_AGB_TO_AGINO() only works within a filesystem block, not
e9d35108
DC
327 * across multiple filesystem blocks (such as a cluster) and so cannot
328 * be used in the cluster buffer loop below.
329 *
330 * Further, because we are writing the inode directly into the buffer
331 * and calculating a CRC on the entire inode, we have ot log the entire
332 * inode so that the entire range the CRC covers is present in the log.
333 * That means for v3 inode we log the entire buffer rather than just the
334 * inode cores.
56b2de80 335 */
94541a16 336 if (xfs_has_v3inodes(mp)) {
41ce5f36 337 version = 3;
7516da71 338 ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno));
e9d35108
DC
339
340 /*
341 * log the initialisation that is about to take place as an
342 * logical operation. This means the transaction does not
343 * need to log the physical changes to the inode buffers as log
344 * recovery will know what initialisation is actually needed.
345 * Hence we only need to log the buffers as "ordered" buffers so
346 * they track in the AIL as if they were physically logged.
347 */
348 if (tp)
fe8d48ac 349 xfs_icreate_log(tp, agno, agbno, icount,
e9d35108 350 mp->m_sb.sb_inodesize, length, gen);
ff105f75 351 } else
56b2de80 352 version = 2;
56b2de80
DC
353
354 for (j = 0; j < nbufs; j++) {
355 /*
356 * Get the block.
357 */
42d896cf 358 d = XFS_AGB_TO_DADDR(mp, agno, agbno +
e7fd2b6f 359 (j * M_IGEO(mp)->blocks_per_cluster));
7f15a547
DW
360 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
361 mp->m_bsize * M_IGEO(mp)->blocks_per_cluster,
94ef61b5 362 0, &fbuf);
7f15a547
DW
363 if (error)
364 return error;
e9d35108
DC
365
366 /* Initialize the inode buffers and log them appropriately. */
a2ceac1f 367 fbuf->b_ops = &xfs_inode_buf_ops;
e9d35108 368 xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
e7fd2b6f 369 for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
56b2de80 370 int ioffset = i << mp->m_sb.sb_inodelog;
56b2de80
DC
371
372 free = xfs_make_iptr(mp, fbuf, i);
373 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
374 free->di_version = version;
375 free->di_gen = cpu_to_be32(gen);
376 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
41ce5f36
DC
377
378 if (version == 3) {
379 free->di_ino = cpu_to_be64(ino);
380 ino++;
9c4e12fb
ES
381 uuid_copy(&free->di_uuid,
382 &mp->m_sb.sb_meta_uuid);
41ce5f36 383 xfs_dinode_calc_crc(mp, free);
e9d35108
DC
384 } else if (tp) {
385 /* just log the inode core */
386 xfs_trans_log_buf(tp, fbuf, ioffset,
03d8044d 387 ioffset + XFS_DINODE_SIZE(mp) - 1);
41ce5f36 388 }
e9d35108 389 }
41ce5f36 390
e9d35108
DC
391 if (tp) {
392 /*
393 * Mark the buffer as an inode allocation buffer so it
394 * sticks in AIL at the point of this allocation
395 * transaction. This ensures the they are on disk before
396 * the tail of the log can be moved past this
397 * transaction (i.e. by preventing relogging from moving
398 * it forward in the log).
399 */
400 xfs_trans_inode_alloc_buf(tp, fbuf);
401 if (version == 3) {
402 /*
403 * Mark the buffer as ordered so that they are
404 * not physically logged in the transaction but
405 * still tracked in the AIL as part of the
406 * transaction and pin the log appropriately.
407 */
408 xfs_trans_ordered_buf(tp, fbuf);
e9d35108
DC
409 }
410 } else {
411 fbuf->b_flags |= XBF_DONE;
412 xfs_buf_delwri_queue(fbuf, buffer_list);
413 xfs_buf_relse(fbuf);
56b2de80 414 }
56b2de80 415 }
a2ceac1f 416 return 0;
56b2de80
DC
417}
418
6f4c54a4
BF
419/*
420 * Align startino and allocmask for a recently allocated sparse chunk such that
421 * they are fit for insertion (or merge) into the on-disk inode btrees.
422 *
423 * Background:
424 *
425 * When enabled, sparse inode support increases the inode alignment from cluster
426 * size to inode chunk size. This means that the minimum range between two
427 * non-adjacent inode records in the inobt is large enough for a full inode
428 * record. This allows for cluster sized, cluster aligned block allocation
429 * without need to worry about whether the resulting inode record overlaps with
430 * another record in the tree. Without this basic rule, we would have to deal
431 * with the consequences of overlap by potentially undoing recent allocations in
432 * the inode allocation codepath.
433 *
434 * Because of this alignment rule (which is enforced on mount), there are two
435 * inobt possibilities for newly allocated sparse chunks. One is that the
436 * aligned inode record for the chunk covers a range of inodes not already
437 * covered in the inobt (i.e., it is safe to insert a new sparse record). The
438 * other is that a record already exists at the aligned startino that considers
439 * the newly allocated range as sparse. In the latter case, record content is
440 * merged in hope that sparse inode chunks fill to full chunks over time.
441 */
442STATIC void
443xfs_align_sparse_ino(
444 struct xfs_mount *mp,
445 xfs_agino_t *startino,
446 uint16_t *allocmask)
447{
448 xfs_agblock_t agbno;
449 xfs_agblock_t mod;
450 int offset;
451
452 agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
453 mod = agbno % mp->m_sb.sb_inoalignmt;
454 if (!mod)
455 return;
456
457 /* calculate the inode offset and align startino */
7516da71 458 offset = XFS_AGB_TO_AGINO(mp, mod);
6f4c54a4
BF
459 *startino -= offset;
460
461 /*
462 * Since startino has been aligned down, left shift allocmask such that
463 * it continues to represent the same physical inodes relative to the
464 * new startino.
465 */
466 *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
467}
468
469/*
470 * Determine whether the source inode record can merge into the target. Both
471 * records must be sparse, the inode ranges must match and there must be no
472 * allocation overlap between the records.
473 */
474STATIC bool
475__xfs_inobt_can_merge(
476 struct xfs_inobt_rec_incore *trec, /* tgt record */
477 struct xfs_inobt_rec_incore *srec) /* src record */
478{
479 uint64_t talloc;
480 uint64_t salloc;
481
482 /* records must cover the same inode range */
483 if (trec->ir_startino != srec->ir_startino)
484 return false;
485
486 /* both records must be sparse */
487 if (!xfs_inobt_issparse(trec->ir_holemask) ||
488 !xfs_inobt_issparse(srec->ir_holemask))
489 return false;
490
491 /* both records must track some inodes */
492 if (!trec->ir_count || !srec->ir_count)
493 return false;
494
495 /* can't exceed capacity of a full record */
496 if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
497 return false;
498
499 /* verify there is no allocation overlap */
500 talloc = xfs_inobt_irec_to_allocmask(trec);
501 salloc = xfs_inobt_irec_to_allocmask(srec);
502 if (talloc & salloc)
503 return false;
504
505 return true;
506}
507
508/*
509 * Merge the source inode record into the target. The caller must call
510 * __xfs_inobt_can_merge() to ensure the merge is valid.
511 */
512STATIC void
513__xfs_inobt_rec_merge(
514 struct xfs_inobt_rec_incore *trec, /* target */
515 struct xfs_inobt_rec_incore *srec) /* src */
516{
517 ASSERT(trec->ir_startino == srec->ir_startino);
518
519 /* combine the counts */
520 trec->ir_count += srec->ir_count;
521 trec->ir_freecount += srec->ir_freecount;
522
523 /*
524 * Merge the holemask and free mask. For both fields, 0 bits refer to
525 * allocated inodes. We combine the allocated ranges with bitwise AND.
526 */
527 trec->ir_holemask &= srec->ir_holemask;
528 trec->ir_free &= srec->ir_free;
529}
530
531/*
9763e890
CH
532 * Insert a new sparse inode chunk into the associated inode allocation btree.
533 * The inode record for the sparse chunk is pre-aligned to a startino that
534 * should match any pre-existing sparse inode record in the tree. This allows
535 * sparse chunks to fill over time.
6f4c54a4 536 *
9763e890
CH
537 * If no preexisting record exists, the provided record is inserted.
538 * If there is a preexisting record, the provided record is merged with the
6f4c54a4 539 * existing record and updated in place. The merged record is returned in nrec.
6f4c54a4
BF
540 *
541 * It is considered corruption if a merge is requested and not possible. Given
542 * the sparse inode alignment constraints, this should never happen.
543 */
544STATIC int
545xfs_inobt_insert_sprec(
90e549b5 546 struct xfs_perag *pag,
6f4c54a4
BF
547 struct xfs_trans *tp,
548 struct xfs_buf *agbp,
9763e890 549 struct xfs_inobt_rec_incore *nrec) /* in/out: new/merged rec. */
6f4c54a4 550{
6a271c73 551 struct xfs_mount *mp = pag_mount(pag);
6f4c54a4 552 struct xfs_btree_cur *cur;
6f4c54a4
BF
553 int error;
554 int i;
555 struct xfs_inobt_rec_incore rec;
556
7199f984 557 cur = xfs_inobt_init_cursor(pag, tp, agbp);
6f4c54a4
BF
558
559 /* the new record is pre-aligned so we know where to look */
560 error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
561 if (error)
562 goto error;
563 /* if nothing there, insert a new record and return */
564 if (i == 0) {
565 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
566 nrec->ir_count, nrec->ir_freecount,
567 nrec->ir_free, &i);
568 if (error)
569 goto error;
fbb4fa7f 570 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 571 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
572 error = -EFSCORRUPTED;
573 goto error;
574 }
6f4c54a4
BF
575
576 goto out;
577 }
578
579 /*
9763e890 580 * A record exists at this startino. Merge the records.
6f4c54a4 581 */
9763e890
CH
582 error = xfs_inobt_get_rec(cur, &rec, &i);
583 if (error)
584 goto error;
585 if (XFS_IS_CORRUPT(mp, i != 1)) {
586 xfs_btree_mark_sick(cur);
587 error = -EFSCORRUPTED;
588 goto error;
589 }
590 if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) {
591 xfs_btree_mark_sick(cur);
592 error = -EFSCORRUPTED;
593 goto error;
594 }
6f4c54a4 595
9763e890
CH
596 /*
597 * This should never fail. If we have coexisting records that
598 * cannot merge, something is seriously wrong.
599 */
600 if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) {
601 xfs_btree_mark_sick(cur);
602 error = -EFSCORRUPTED;
603 goto error;
604 }
6f4c54a4 605
0601845b 606 trace_xfs_irec_merge_pre(pag, &rec, nrec);
6f4c54a4 607
9763e890
CH
608 /* merge to nrec to output the updated record */
609 __xfs_inobt_rec_merge(nrec, &rec);
6f4c54a4 610
0601845b 611 trace_xfs_irec_merge_post(pag, nrec);
6f4c54a4 612
9763e890
CH
613 error = xfs_inobt_rec_check_count(mp, nrec);
614 if (error)
615 goto error;
6f4c54a4
BF
616
617 error = xfs_inobt_update(cur, nrec);
618 if (error)
619 goto error;
620
621out:
622 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
623 return 0;
624error:
625 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
626 return error;
627}
628
9763e890
CH
629/*
630 * Insert a new sparse inode chunk into the free inode btree. The inode
631 * record for the sparse chunk is pre-aligned to a startino that should match
632 * any pre-existing sparse inode record in the tree. This allows sparse chunks
633 * to fill over time.
634 *
635 * The new record is always inserted, overwriting a pre-existing record if
636 * there is one.
637 */
638STATIC int
639xfs_finobt_insert_sprec(
640 struct xfs_perag *pag,
641 struct xfs_trans *tp,
642 struct xfs_buf *agbp,
643 struct xfs_inobt_rec_incore *nrec) /* in/out: new rec. */
644{
6a271c73 645 struct xfs_mount *mp = pag_mount(pag);
9763e890
CH
646 struct xfs_btree_cur *cur;
647 int error;
648 int i;
649
7199f984 650 cur = xfs_finobt_init_cursor(pag, tp, agbp);
9763e890
CH
651
652 /* the new record is pre-aligned so we know where to look */
653 error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
654 if (error)
655 goto error;
656 /* if nothing there, insert a new record and return */
657 if (i == 0) {
658 error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
659 nrec->ir_count, nrec->ir_freecount,
660 nrec->ir_free, &i);
661 if (error)
662 goto error;
663 if (XFS_IS_CORRUPT(mp, i != 1)) {
664 xfs_btree_mark_sick(cur);
665 error = -EFSCORRUPTED;
666 goto error;
667 }
668 } else {
669 error = xfs_inobt_update(cur, nrec);
670 if (error)
671 goto error;
672 }
673
674 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
675 return 0;
676error:
677 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
678 return error;
679}
680
681
2bd0ea18 682/*
771d0670
DC
683 * Allocate new inodes in the allocation group specified by agbp. Returns 0 if
684 * inodes were allocated in this AG; -EAGAIN if there was no space in this AG so
685 * the caller knows it can try another AG, a hard -ENOSPC when over the maximum
686 * inode count threshold, or the usual negative error code for other errors.
2bd0ea18 687 */
e7fd2b6f 688STATIC int
2bd0ea18 689xfs_ialloc_ag_alloc(
90e549b5 690 struct xfs_perag *pag,
e7fd2b6f 691 struct xfs_trans *tp,
90e549b5 692 struct xfs_buf *agbp)
2bd0ea18 693{
e7fd2b6f
DW
694 struct xfs_agi *agi;
695 struct xfs_alloc_arg args;
e7fd2b6f
DW
696 int error;
697 xfs_agino_t newino; /* new first inode's number */
698 xfs_agino_t newlen; /* new number of inodes */
699 int isaligned = 0; /* inode allocation at stripe */
700 /* unit boundary */
701 /* init. to full chunk */
6f4c54a4 702 struct xfs_inobt_rec_incore rec;
e7fd2b6f 703 struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp);
a426d0e1 704 uint16_t allocmask = (uint16_t) -1;
e7fd2b6f 705 int do_sparse = 0;
c9005f41 706
a2ceac1f 707 memset(&args, 0, sizeof(args));
2bd0ea18
NS
708 args.tp = tp;
709 args.mp = tp->t_mountp;
c9005f41 710 args.fsbno = NULLFSBLOCK;
007347e3 711 args.oinfo = XFS_RMAP_OINFO_INODES;
04215b9f 712 args.pag = pag;
2bd0ea18 713
0d995d03
BF
714#ifdef DEBUG
715 /* randomly do sparse inode allocations */
94541a16 716 if (xfs_has_sparseinodes(tp->t_mountp) &&
e7fd2b6f 717 igeo->ialloc_min_blks < igeo->ialloc_blks)
9a046f96 718 do_sparse = get_random_u32_below(2);
0d995d03
BF
719#endif
720
2bd0ea18
NS
721 /*
722 * Locking will ensure that we don't have two callers in here
723 * at one time.
724 */
e7fd2b6f
DW
725 newlen = igeo->ialloc_inos;
726 if (igeo->maxicount &&
cbf3beaa 727 percpu_counter_read_positive(&args.mp->m_icount) + newlen >
e7fd2b6f 728 igeo->maxicount)
12b53197 729 return -ENOSPC;
e7fd2b6f 730 args.minlen = args.maxlen = igeo->ialloc_blks;
2bd0ea18 731 /*
5e656dbb
BN
732 * First try to allocate inodes contiguous with the last-allocated
733 * chunk of inodes. If the filesystem is striped, this will fill
734 * an entire stripe unit with inodes.
3439d03a 735 */
c99cea5c 736 agi = agbp->b_addr;
5e656dbb
BN
737 newino = be32_to_cpu(agi->agi_newino);
738 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
e7fd2b6f 739 igeo->ialloc_blks;
c9005f41
BF
740 if (do_sparse)
741 goto sparse_alloc;
5e656dbb
BN
742 if (likely(newino != NULLAGINO &&
743 (args.agbno < be32_to_cpu(agi->agi_length)))) {
5e656dbb
BN
744 args.prod = 1;
745
746 /*
747 * We need to take into account alignment here to ensure that
748 * we don't modify the free list if we fail to have an exact
749 * block. If we don't have an exact match, and every oher
750 * attempt allocation attempt fails, we'll end up cancelling
751 * a dirty transaction and shutting down.
752 *
753 * For an exact allocation, alignment must be 1,
754 * however we need to take cluster alignment into account when
755 * fixing up the freelist. Use the minalignslop field to
756 * indicate that extra blocks might be required for alignment,
757 * but not to use them in the actual exact allocation.
758 */
759 args.alignment = 1;
e7fd2b6f 760 args.minalignslop = igeo->cluster_align - 1;
5e656dbb
BN
761
762 /* Allow space for the inode btree to split. */
d6e8deb1 763 args.minleft = igeo->inobt_maxlevels;
c4e10f2b 764 error = xfs_alloc_vextent_exact_bno(&args,
25e0d559 765 xfs_agbno_to_fsb(pag, args.agbno));
04215b9f 766 if (error)
5e656dbb 767 return error;
ff105f75
DC
768
769 /*
770 * This request might have dirtied the transaction if the AG can
771 * satisfy the request, but the exact block was not available.
772 * If the allocation did fail, subsequent requests will relax
773 * the exact agbno requirement and increase the alignment
774 * instead. It is critical that the total size of the request
775 * (len + alignment + slop) does not increase from this point
776 * on, so reset minalignslop to ensure it is not included in
777 * subsequent requests.
778 */
779 args.minalignslop = 0;
c9005f41 780 }
5e656dbb
BN
781
782 if (unlikely(args.fsbno == NULLFSBLOCK)) {
783 /*
784 * Set the alignment for the allocation.
785 * If stripe alignment is turned on then align at stripe unit
786 * boundary.
787 * If the cluster size is smaller than a filesystem block
788 * then we're doing I/O for inodes in filesystem block size
789 * pieces, so don't need alignment anyway.
790 */
791 isaligned = 0;
e7fd2b6f 792 if (igeo->ialloc_align) {
914e2a04 793 ASSERT(!xfs_has_noalign(args.mp));
5e656dbb
BN
794 args.alignment = args.mp->m_dalign;
795 isaligned = 1;
796 } else
e7fd2b6f 797 args.alignment = igeo->cluster_align;
5e656dbb
BN
798 /*
799 * Allocate a fixed-size extent of inodes.
800 */
5e656dbb
BN
801 args.prod = 1;
802 /*
803 * Allow space for the inode btree to split.
804 */
d6e8deb1 805 args.minleft = igeo->inobt_maxlevels;
15653695 806 error = xfs_alloc_vextent_near_bno(&args,
25e0d559
CH
807 xfs_agbno_to_fsb(pag,
808 be32_to_cpu(agi->agi_root)));
04215b9f 809 if (error)
5e656dbb
BN
810 return error;
811 }
2bd0ea18
NS
812
813 /*
814 * If stripe alignment is turned on, then try again with cluster
815 * alignment.
816 */
817 if (isaligned && args.fsbno == NULLFSBLOCK) {
e7fd2b6f 818 args.alignment = igeo->cluster_align;
15653695 819 error = xfs_alloc_vextent_near_bno(&args,
25e0d559
CH
820 xfs_agbno_to_fsb(pag,
821 be32_to_cpu(agi->agi_root)));
15653695 822 if (error)
dfc130f3 823 return error;
2bd0ea18 824 }
5000d01d 825
6f4c54a4
BF
826 /*
827 * Finally, try a sparse allocation if the filesystem supports it and
828 * the sparse allocation length is smaller than a full chunk.
829 */
94541a16 830 if (xfs_has_sparseinodes(args.mp) &&
e7fd2b6f 831 igeo->ialloc_min_blks < igeo->ialloc_blks &&
6f4c54a4 832 args.fsbno == NULLFSBLOCK) {
c9005f41 833sparse_alloc:
6f4c54a4
BF
834 args.alignment = args.mp->m_sb.sb_spino_align;
835 args.prod = 1;
836
e7fd2b6f 837 args.minlen = igeo->ialloc_min_blks;
6f4c54a4
BF
838 args.maxlen = args.minlen;
839
840 /*
841 * The inode record will be aligned to full chunk size. We must
842 * prevent sparse allocation from AG boundaries that result in
843 * invalid inode records, such as records that start at agbno 0
844 * or extend beyond the AG.
845 *
846 * Set min agbno to the first aligned, non-zero agbno and max to
847 * the last aligned agbno that is at least one full chunk from
848 * the end of the AG.
849 */
850 args.min_agbno = args.mp->m_sb.sb_inoalignmt;
12a11a2b
DC
851 args.max_agbno = round_down(xfs_ag_block_count(args.mp,
852 pag_agno(pag)),
6f4c54a4 853 args.mp->m_sb.sb_inoalignmt) -
e7fd2b6f 854 igeo->ialloc_blks;
6f4c54a4 855
15653695 856 error = xfs_alloc_vextent_near_bno(&args,
25e0d559
CH
857 xfs_agbno_to_fsb(pag,
858 be32_to_cpu(agi->agi_root)));
6f4c54a4
BF
859 if (error)
860 return error;
861
7516da71 862 newlen = XFS_AGB_TO_AGINO(args.mp, args.len);
0d995d03 863 ASSERT(newlen <= XFS_INODES_PER_CHUNK);
6f4c54a4
BF
864 allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
865 }
866
2cdc6e56 867 if (args.fsbno == NULLFSBLOCK)
771d0670 868 return -EAGAIN;
2cdc6e56 869
2bd0ea18 870 ASSERT(args.len == args.minlen);
a562a63b 871
5e656dbb 872 /*
56b2de80
DC
873 * Stamp and write the inode buffers.
874 *
5e656dbb
BN
875 * Seed the new inode cluster with a random generation number. This
876 * prevents short-term reuse of generation numbers if a chunk is
877 * freed and then immediately reallocated. We use random numbers
878 * rather than a linear progression to prevent the next generation
879 * number from being easily guessable.
880 */
6a271c73 881 error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, pag_agno(pag),
11d2f5af 882 args.agbno, args.len, get_random_u32());
56b2de80 883
a2ceac1f
DC
884 if (error)
885 return error;
56b2de80
DC
886 /*
887 * Convert the results.
888 */
7516da71 889 newino = XFS_AGB_TO_AGINO(args.mp, args.agbno);
6f4c54a4
BF
890
891 if (xfs_inobt_issparse(~allocmask)) {
892 /*
893 * We've allocated a sparse chunk. Align the startino and mask.
894 */
895 xfs_align_sparse_ino(args.mp, &newino, &allocmask);
896
897 rec.ir_startino = newino;
898 rec.ir_holemask = ~allocmask;
899 rec.ir_count = newlen;
900 rec.ir_freecount = newlen;
901 rec.ir_free = XFS_INOBT_ALL_FREE;
902
903 /*
904 * Insert the sparse record into the inobt and allow for a merge
905 * if necessary. If a merge does occur, rec is updated to the
906 * merged record.
907 */
9763e890 908 error = xfs_inobt_insert_sprec(pag, tp, agbp, &rec);
6f4c54a4
BF
909 if (error == -EFSCORRUPTED) {
910 xfs_alert(args.mp,
911 "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
ef7499e4 912 xfs_agino_to_ino(pag, rec.ir_startino),
6f4c54a4
BF
913 rec.ir_holemask, rec.ir_count);
914 xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
915 }
916 if (error)
917 return error;
918
919 /*
920 * We can't merge the part we've just allocated as for the inobt
921 * due to finobt semantics. The original record may or may not
922 * exist independent of whether physical inodes exist in this
923 * sparse chunk.
924 *
925 * We must update the finobt record based on the inobt record.
926 * rec contains the fully merged and up to date inobt record
927 * from the previous call. Set merge false to replace any
928 * existing record with this one.
929 */
94541a16 930 if (xfs_has_finobt(args.mp)) {
9763e890 931 error = xfs_finobt_insert_sprec(pag, tp, agbp, &rec);
6f4c54a4
BF
932 if (error)
933 return error;
934 }
935 } else {
936 /* full chunk - insert new records to both btrees */
53430ebb 937 error = xfs_inobt_insert(pag, tp, agbp, newino, newlen, false);
6f4c54a4
BF
938 if (error)
939 return error;
940
94541a16 941 if (xfs_has_finobt(args.mp)) {
90e549b5 942 error = xfs_inobt_insert(pag, tp, agbp, newino,
53430ebb 943 newlen, true);
6f4c54a4
BF
944 if (error)
945 return error;
946 }
947 }
948
949 /*
950 * Update AGI counts and newino.
951 */
5e656dbb
BN
952 be32_add_cpu(&agi->agi_count, newlen);
953 be32_add_cpu(&agi->agi_freecount, newlen);
56b2de80 954 pag->pagi_freecount += newlen;
a3204ee7 955 pag->pagi_count += newlen;
6e3140c7 956 agi->agi_newino = cpu_to_be32(newino);
56b2de80 957
2bd0ea18
NS
958 /*
959 * Log allocation group header fields
960 */
961 xfs_ialloc_log_agi(tp, agbp,
962 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
963 /*
964 * Modify/log superblock values for inode count and inode free count.
965 */
966 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
967 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
2bd0ea18
NS
968 return 0;
969}
970
56b2de80
DC
971/*
972 * Try to retrieve the next record to the left/right from the current one.
973 */
974STATIC int
975xfs_ialloc_next_rec(
976 struct xfs_btree_cur *cur,
977 xfs_inobt_rec_incore_t *rec,
978 int *done,
979 int left)
980{
981 int error;
982 int i;
983
984 if (left)
985 error = xfs_btree_decrement(cur, 0, &i);
986 else
987 error = xfs_btree_increment(cur, 0, &i);
988
989 if (error)
990 return error;
991 *done = !i;
992 if (i) {
993 error = xfs_inobt_get_rec(cur, rec, &i);
994 if (error)
995 return error;
4467a60a
DW
996 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
997 xfs_btree_mark_sick(cur);
fbb4fa7f 998 return -EFSCORRUPTED;
4467a60a 999 }
56b2de80
DC
1000 }
1001
1002 return 0;
1003}
1004
1005STATIC int
1006xfs_ialloc_get_rec(
1007 struct xfs_btree_cur *cur,
1008 xfs_agino_t agino,
1009 xfs_inobt_rec_incore_t *rec,
3439d03a 1010 int *done)
56b2de80
DC
1011{
1012 int error;
1013 int i;
1014
1015 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
1016 if (error)
1017 return error;
1018 *done = !i;
1019 if (i) {
1020 error = xfs_inobt_get_rec(cur, rec, &i);
1021 if (error)
1022 return error;
4467a60a
DW
1023 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1024 xfs_btree_mark_sick(cur);
fbb4fa7f 1025 return -EFSCORRUPTED;
4467a60a 1026 }
56b2de80
DC
1027 }
1028
1029 return 0;
1030}
1031
01792d3b 1032/*
fa58183c
BF
1033 * Return the offset of the first free inode in the record. If the inode chunk
1034 * is sparsely allocated, we convert the record holemask to inode granularity
1035 * and mask off the unallocated regions from the inode free mask.
01792d3b
BF
1036 */
1037STATIC int
1038xfs_inobt_first_free_inode(
1039 struct xfs_inobt_rec_incore *rec)
1040{
fa58183c
BF
1041 xfs_inofree_t realfree;
1042
1043 /* if there are no holes, return the first available offset */
1044 if (!xfs_inobt_issparse(rec->ir_holemask))
1045 return xfs_lowbit64(rec->ir_free);
1046
1047 realfree = xfs_inobt_irec_to_allocmask(rec);
1048 realfree &= rec->ir_free;
1049
1050 return xfs_lowbit64(realfree);
01792d3b
BF
1051}
1052
c9d6544c
DW
1053/*
1054 * If this AG has corrupt inodes, check if allocating this inode would fail
1055 * with corruption errors. Returns 0 if we're clear, or EAGAIN to try again
1056 * somewhere else.
1057 */
1058static int
1059xfs_dialloc_check_ino(
1060 struct xfs_perag *pag,
1061 struct xfs_trans *tp,
1062 xfs_ino_t ino)
1063{
1064 struct xfs_imap imap;
1065 struct xfs_buf *bp;
1066 int error;
1067
1068 error = xfs_imap(pag, tp, ino, &imap, 0);
1069 if (error)
1070 return -EAGAIN;
1071
6a271c73 1072 error = xfs_imap_to_bp(pag_mount(pag), tp, &imap, &bp);
c9d6544c
DW
1073 if (error)
1074 return -EAGAIN;
1075
1076 xfs_trans_brelse(tp, bp);
1077 return 0;
1078}
1079
5000d01d 1080/*
ff105f75 1081 * Allocate an inode using the inobt-only algorithm.
2bd0ea18 1082 */
a2ceac1f 1083STATIC int
ff105f75 1084xfs_dialloc_ag_inobt(
90e549b5 1085 struct xfs_perag *pag,
a2ceac1f
DC
1086 struct xfs_trans *tp,
1087 struct xfs_buf *agbp,
1088 xfs_ino_t parent,
1089 xfs_ino_t *inop)
2bd0ea18 1090{
a2ceac1f 1091 struct xfs_mount *mp = tp->t_mountp;
c99cea5c 1092 struct xfs_agi *agi = agbp->b_addr;
a2ceac1f
DC
1093 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1094 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
a2ceac1f
DC
1095 struct xfs_btree_cur *cur, *tcur;
1096 struct xfs_inobt_rec_incore rec, trec;
1097 xfs_ino_t ino;
1098 int error;
1099 int offset;
1100 int i, j;
d8c47890 1101 int searchdistance = 10;
2bd0ea18 1102
03dc2ef2
DC
1103 ASSERT(xfs_perag_initialised_agi(pag));
1104 ASSERT(xfs_perag_allows_inodes(pag));
a2ceac1f
DC
1105 ASSERT(pag->pagi_freecount > 0);
1106
56b2de80 1107 restart_pagno:
7199f984 1108 cur = xfs_inobt_init_cursor(pag, tp, agbp);
2bd0ea18
NS
1109 /*
1110 * If pagino is 0 (this is the root inode allocation) use newino.
1111 * This must work because we've just allocated some.
1112 */
1113 if (!pagino)
6e3140c7 1114 pagino = be32_to_cpu(agi->agi_newino);
2bd0ea18 1115
1dbf114f 1116 error = xfs_check_agi_freecount(cur);
56b2de80
DC
1117 if (error)
1118 goto error0;
2bd0ea18 1119
2bd0ea18 1120 /*
56b2de80 1121 * If in the same AG as the parent, try to get near the parent.
2bd0ea18 1122 */
6a271c73 1123 if (pagno == pag_agno(pag)) {
56b2de80
DC
1124 int doneleft; /* done, to the left */
1125 int doneright; /* done, to the right */
56b2de80
DC
1126
1127 error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
1128 if (error)
2bd0ea18 1129 goto error0;
fbb4fa7f 1130 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1131 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
1132 error = -EFSCORRUPTED;
1133 goto error0;
1134 }
56b2de80
DC
1135
1136 error = xfs_inobt_get_rec(cur, &rec, &j);
1137 if (error)
1138 goto error0;
fbb4fa7f 1139 if (XFS_IS_CORRUPT(mp, j != 1)) {
4467a60a 1140 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
1141 error = -EFSCORRUPTED;
1142 goto error0;
1143 }
56b2de80
DC
1144
1145 if (rec.ir_freecount > 0) {
2bd0ea18
NS
1146 /*
1147 * Found a free inode in the same chunk
56b2de80 1148 * as the parent, done.
2bd0ea18 1149 */
56b2de80 1150 goto alloc_inode;
2bd0ea18 1151 }
56b2de80
DC
1152
1153
1154 /*
1155 * In the same AG as parent, but parent's chunk is full.
1156 */
1157
1158 /* duplicate the cursor, search left & right simultaneously */
1159 error = xfs_btree_dup_cursor(cur, &tcur);
1160 if (error)
1161 goto error0;
1162
2bd0ea18 1163 /*
56b2de80 1164 * Skip to last blocks looked up if same parent inode.
2bd0ea18 1165 */
56b2de80
DC
1166 if (pagino != NULLAGINO &&
1167 pag->pagl_pagino == pagino &&
1168 pag->pagl_leftrec != NULLAGINO &&
1169 pag->pagl_rightrec != NULLAGINO) {
1170 error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
3439d03a 1171 &trec, &doneleft);
56b2de80
DC
1172 if (error)
1173 goto error1;
2bd0ea18 1174
56b2de80 1175 error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
3439d03a 1176 &rec, &doneright);
2bd0ea18 1177 if (error)
2bd0ea18 1178 goto error1;
56b2de80
DC
1179 } else {
1180 /* search left with tcur, back up 1 record */
1181 error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
1182 if (error)
2bd0ea18 1183 goto error1;
2bd0ea18 1184
56b2de80
DC
1185 /* search right with cur, go forward 1 record. */
1186 error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
1187 if (error)
1188 goto error1;
1189 }
1190
1191 /*
1192 * Loop until we find an inode chunk with a free inode.
1193 */
d8c47890 1194 while (--searchdistance > 0 && (!doneleft || !doneright)) {
56b2de80
DC
1195 int useleft; /* using left inode chunk this time */
1196
56b2de80
DC
1197 /* figure out the closer block if both are valid. */
1198 if (!doneleft && !doneright) {
1199 useleft = pagino -
1200 (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
1201 rec.ir_startino - pagino;
1202 } else {
1203 useleft = !doneleft;
1204 }
1205
1206 /* free inodes to the left? */
1207 if (useleft && trec.ir_freecount) {
56b2de80
DC
1208 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1209 cur = tcur;
1210
1211 pag->pagl_leftrec = trec.ir_startino;
1212 pag->pagl_rightrec = rec.ir_startino;
1213 pag->pagl_pagino = pagino;
ef54efb5 1214 rec = trec;
56b2de80
DC
1215 goto alloc_inode;
1216 }
1217
1218 /* free inodes to the right? */
1219 if (!useleft && rec.ir_freecount) {
1220 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1221
1222 pag->pagl_leftrec = trec.ir_startino;
1223 pag->pagl_rightrec = rec.ir_startino;
1224 pag->pagl_pagino = pagino;
1225 goto alloc_inode;
1226 }
1227
1228 /* get next record to check */
1229 if (useleft) {
1230 error = xfs_ialloc_next_rec(tcur, &trec,
1231 &doneleft, 1);
1232 } else {
1233 error = xfs_ialloc_next_rec(cur, &rec,
1234 &doneright, 0);
1235 }
1236 if (error)
1237 goto error1;
2bd0ea18 1238 }
56b2de80 1239
d8c47890
CM
1240 if (searchdistance <= 0) {
1241 /*
1242 * Not in range - save last search
1243 * location and allocate a new inode
1244 */
1245 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1246 pag->pagl_leftrec = trec.ir_startino;
1247 pag->pagl_rightrec = rec.ir_startino;
1248 pag->pagl_pagino = pagino;
1249
1250 } else {
1251 /*
1252 * We've reached the end of the btree. because
1253 * we are only searching a small chunk of the
1254 * btree each search, there is obviously free
1255 * inodes closer to the parent inode than we
1256 * are now. restart the search again.
1257 */
1258 pag->pagl_pagino = NULLAGINO;
1259 pag->pagl_leftrec = NULLAGINO;
1260 pag->pagl_rightrec = NULLAGINO;
1261 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1262 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1263 goto restart_pagno;
1264 }
2bd0ea18 1265 }
56b2de80 1266
2bd0ea18 1267 /*
56b2de80 1268 * In a different AG from the parent.
2bd0ea18
NS
1269 * See if the most recently allocated block has any free.
1270 */
a2ceac1f 1271 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
56b2de80
DC
1272 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1273 XFS_LOOKUP_EQ, &i);
1274 if (error)
2bd0ea18 1275 goto error0;
56b2de80
DC
1276
1277 if (i == 1) {
1278 error = xfs_inobt_get_rec(cur, &rec, &j);
2bd0ea18
NS
1279 if (error)
1280 goto error0;
56b2de80
DC
1281
1282 if (j == 1 && rec.ir_freecount > 0) {
1283 /*
1284 * The last chunk allocated in the group
1285 * still has a free inode.
1286 */
1287 goto alloc_inode;
2bd0ea18
NS
1288 }
1289 }
1290 }
56b2de80
DC
1291
1292 /*
1293 * None left in the last group, search the whole AG
1294 */
1295 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1296 if (error)
1297 goto error0;
fbb4fa7f 1298 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1299 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
1300 error = -EFSCORRUPTED;
1301 goto error0;
1302 }
56b2de80
DC
1303
1304 for (;;) {
1305 error = xfs_inobt_get_rec(cur, &rec, &i);
1306 if (error)
1307 goto error0;
fbb4fa7f 1308 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1309 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
1310 error = -EFSCORRUPTED;
1311 goto error0;
1312 }
56b2de80
DC
1313 if (rec.ir_freecount > 0)
1314 break;
1315 error = xfs_btree_increment(cur, 0, &i);
1316 if (error)
1317 goto error0;
fbb4fa7f 1318 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 1319 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
1320 error = -EFSCORRUPTED;
1321 goto error0;
1322 }
56b2de80
DC
1323 }
1324
1325alloc_inode:
01792d3b 1326 offset = xfs_inobt_first_free_inode(&rec);
2bd0ea18
NS
1327 ASSERT(offset >= 0);
1328 ASSERT(offset < XFS_INODES_PER_CHUNK);
1329 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1330 XFS_INODES_PER_CHUNK) == 0);
ef7499e4 1331 ino = xfs_agino_to_ino(pag, rec.ir_startino + offset);
c9d6544c
DW
1332
1333 if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) {
1334 error = xfs_dialloc_check_ino(pag, tp, ino);
1335 if (error)
1336 goto error0;
1337 }
1338
56b2de80 1339 rec.ir_free &= ~XFS_INOBT_MASK(offset);
2bd0ea18 1340 rec.ir_freecount--;
56b2de80
DC
1341 error = xfs_inobt_update(cur, &rec);
1342 if (error)
2bd0ea18 1343 goto error0;
5e656dbb 1344 be32_add_cpu(&agi->agi_freecount, -1);
2bd0ea18 1345 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
56b2de80
DC
1346 pag->pagi_freecount--;
1347
1dbf114f 1348 error = xfs_check_agi_freecount(cur);
56b2de80
DC
1349 if (error)
1350 goto error0;
2bd0ea18 1351
2bd0ea18
NS
1352 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1353 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1354 *inop = ino;
1355 return 0;
1356error1:
1357 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1358error0:
1359 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1360 return error;
1361}
1362
ff105f75
DC
1363/*
1364 * Use the free inode btree to allocate an inode based on distance from the
1365 * parent. Note that the provided cursor may be deleted and replaced.
1366 */
1367STATIC int
1368xfs_dialloc_ag_finobt_near(
1369 xfs_agino_t pagino,
1370 struct xfs_btree_cur **ocur,
1371 struct xfs_inobt_rec_incore *rec)
1372{
1373 struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
1374 struct xfs_btree_cur *rcur; /* right search cursor */
1375 struct xfs_inobt_rec_incore rrec;
1376 int error;
1377 int i, j;
1378
1379 error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1380 if (error)
1381 return error;
1382
1383 if (i == 1) {
1384 error = xfs_inobt_get_rec(lcur, rec, &i);
1385 if (error)
1386 return error;
4467a60a
DW
1387 if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1)) {
1388 xfs_btree_mark_sick(lcur);
fbb4fa7f 1389 return -EFSCORRUPTED;
4467a60a 1390 }
ff105f75
DC
1391
1392 /*
1393 * See if we've landed in the parent inode record. The finobt
1394 * only tracks chunks with at least one free inode, so record
1395 * existence is enough.
1396 */
1397 if (pagino >= rec->ir_startino &&
1398 pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1399 return 0;
1400 }
1401
1402 error = xfs_btree_dup_cursor(lcur, &rcur);
1403 if (error)
1404 return error;
1405
1406 error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1407 if (error)
1408 goto error_rcur;
1409 if (j == 1) {
1410 error = xfs_inobt_get_rec(rcur, &rrec, &j);
1411 if (error)
1412 goto error_rcur;
fbb4fa7f 1413 if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) {
4467a60a 1414 xfs_btree_mark_sick(lcur);
fbb4fa7f
DW
1415 error = -EFSCORRUPTED;
1416 goto error_rcur;
1417 }
ff105f75
DC
1418 }
1419
fbb4fa7f 1420 if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) {
4467a60a 1421 xfs_btree_mark_sick(lcur);
fbb4fa7f
DW
1422 error = -EFSCORRUPTED;
1423 goto error_rcur;
1424 }
ff105f75
DC
1425 if (i == 1 && j == 1) {
1426 /*
1427 * Both the left and right records are valid. Choose the closer
1428 * inode chunk to the target.
1429 */
1430 if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1431 (rrec.ir_startino - pagino)) {
1432 *rec = rrec;
1433 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1434 *ocur = rcur;
1435 } else {
1436 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1437 }
1438 } else if (j == 1) {
1439 /* only the right record is valid */
1440 *rec = rrec;
1441 xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1442 *ocur = rcur;
1443 } else if (i == 1) {
1444 /* only the left record is valid */
1445 xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1446 }
1447
1448 return 0;
1449
1450error_rcur:
1451 xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1452 return error;
1453}
1454
1455/*
1456 * Use the free inode btree to find a free inode based on a newino hint. If
1457 * the hint is NULL, find the first free inode in the AG.
1458 */
1459STATIC int
1460xfs_dialloc_ag_finobt_newino(
1461 struct xfs_agi *agi,
1462 struct xfs_btree_cur *cur,
1463 struct xfs_inobt_rec_incore *rec)
1464{
1465 int error;
1466 int i;
1467
1468 if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1469 error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1470 XFS_LOOKUP_EQ, &i);
1471 if (error)
1472 return error;
1473 if (i == 1) {
1474 error = xfs_inobt_get_rec(cur, rec, &i);
1475 if (error)
1476 return error;
4467a60a
DW
1477 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1478 xfs_btree_mark_sick(cur);
fbb4fa7f 1479 return -EFSCORRUPTED;
4467a60a 1480 }
ff105f75
DC
1481 return 0;
1482 }
1483 }
1484
1485 /*
1486 * Find the first inode available in the AG.
1487 */
1488 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1489 if (error)
1490 return error;
4467a60a
DW
1491 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1492 xfs_btree_mark_sick(cur);
fbb4fa7f 1493 return -EFSCORRUPTED;
4467a60a 1494 }
ff105f75
DC
1495
1496 error = xfs_inobt_get_rec(cur, rec, &i);
1497 if (error)
1498 return error;
4467a60a
DW
1499 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1500 xfs_btree_mark_sick(cur);
fbb4fa7f 1501 return -EFSCORRUPTED;
4467a60a 1502 }
ff105f75
DC
1503
1504 return 0;
1505}
1506
1507/*
1508 * Update the inobt based on a modification made to the finobt. Also ensure that
1509 * the records from both trees are equivalent post-modification.
1510 */
1511STATIC int
1512xfs_dialloc_ag_update_inobt(
1513 struct xfs_btree_cur *cur, /* inobt cursor */
1514 struct xfs_inobt_rec_incore *frec, /* finobt record */
1515 int offset) /* inode offset */
1516{
1517 struct xfs_inobt_rec_incore rec;
1518 int error;
1519 int i;
1520
1521 error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1522 if (error)
1523 return error;
4467a60a
DW
1524 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1525 xfs_btree_mark_sick(cur);
fbb4fa7f 1526 return -EFSCORRUPTED;
4467a60a 1527 }
ff105f75
DC
1528
1529 error = xfs_inobt_get_rec(cur, &rec, &i);
1530 if (error)
1531 return error;
4467a60a
DW
1532 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
1533 xfs_btree_mark_sick(cur);
fbb4fa7f 1534 return -EFSCORRUPTED;
4467a60a 1535 }
ff105f75
DC
1536 ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1537 XFS_INODES_PER_CHUNK) == 0);
1538
1539 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1540 rec.ir_freecount--;
1541
fbb4fa7f
DW
1542 if (XFS_IS_CORRUPT(cur->bc_mp,
1543 rec.ir_free != frec->ir_free ||
4467a60a
DW
1544 rec.ir_freecount != frec->ir_freecount)) {
1545 xfs_btree_mark_sick(cur);
fbb4fa7f 1546 return -EFSCORRUPTED;
4467a60a 1547 }
ff105f75 1548
5a35bf2c 1549 return xfs_inobt_update(cur, &rec);
ff105f75
DC
1550}
1551
1552/*
1553 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1554 * back to the inobt search algorithm.
1555 *
1556 * The caller selected an AG for us, and made sure that free inodes are
1557 * available.
1558 */
51b1e167 1559static int
88fc7306 1560xfs_dialloc_ag(
90e549b5 1561 struct xfs_perag *pag,
88fc7306
BF
1562 struct xfs_trans *tp,
1563 struct xfs_buf *agbp,
1564 xfs_ino_t parent,
1565 xfs_ino_t *inop)
1566{
1567 struct xfs_mount *mp = tp->t_mountp;
c99cea5c 1568 struct xfs_agi *agi = agbp->b_addr;
88fc7306
BF
1569 xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1570 xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
ff105f75
DC
1571 struct xfs_btree_cur *cur; /* finobt cursor */
1572 struct xfs_btree_cur *icur; /* inobt cursor */
88fc7306 1573 struct xfs_inobt_rec_incore rec;
88fc7306
BF
1574 xfs_ino_t ino;
1575 int error;
1576 int offset;
ff105f75 1577 int i;
88fc7306 1578
94541a16 1579 if (!xfs_has_finobt(mp))
90e549b5 1580 return xfs_dialloc_ag_inobt(pag, tp, agbp, parent, inop);
88fc7306 1581
88fc7306
BF
1582 /*
1583 * If pagino is 0 (this is the root inode allocation) use newino.
1584 * This must work because we've just allocated some.
1585 */
1586 if (!pagino)
1587 pagino = be32_to_cpu(agi->agi_newino);
1588
7199f984 1589 cur = xfs_finobt_init_cursor(pag, tp, agbp);
88fc7306 1590
1dbf114f 1591 error = xfs_check_agi_freecount(cur);
88fc7306
BF
1592 if (error)
1593 goto error_cur;
1594
ff105f75
DC
1595 /*
1596 * The search algorithm depends on whether we're in the same AG as the
1597 * parent. If so, find the closest available inode to the parent. If
1598 * not, consider the agi hint or find the first free inode in the AG.
1599 */
6a271c73 1600 if (pag_agno(pag) == pagno)
ff105f75
DC
1601 error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1602 else
1603 error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1604 if (error)
1605 goto error_cur;
88fc7306 1606
01792d3b 1607 offset = xfs_inobt_first_free_inode(&rec);
88fc7306
BF
1608 ASSERT(offset >= 0);
1609 ASSERT(offset < XFS_INODES_PER_CHUNK);
1610 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1611 XFS_INODES_PER_CHUNK) == 0);
ef7499e4 1612 ino = xfs_agino_to_ino(pag, rec.ir_startino + offset);
88fc7306 1613
c9d6544c
DW
1614 if (xfs_ag_has_sickness(pag, XFS_SICK_AG_INODES)) {
1615 error = xfs_dialloc_check_ino(pag, tp, ino);
1616 if (error)
1617 goto error_cur;
1618 }
1619
88fc7306
BF
1620 /*
1621 * Modify or remove the finobt record.
1622 */
1623 rec.ir_free &= ~XFS_INOBT_MASK(offset);
1624 rec.ir_freecount--;
ff105f75 1625 if (rec.ir_freecount)
88fc7306
BF
1626 error = xfs_inobt_update(cur, &rec);
1627 else
1628 error = xfs_btree_delete(cur, &i);
1629 if (error)
1630 goto error_cur;
1631
1632 /*
ff105f75
DC
1633 * The finobt has now been updated appropriately. We haven't updated the
1634 * agi and superblock yet, so we can create an inobt cursor and validate
1635 * the original freecount. If all is well, make the equivalent update to
1636 * the inobt using the finobt record and offset information.
88fc7306 1637 */
7199f984 1638 icur = xfs_inobt_init_cursor(pag, tp, agbp);
88fc7306 1639
1dbf114f 1640 error = xfs_check_agi_freecount(icur);
88fc7306 1641 if (error)
ff105f75 1642 goto error_icur;
88fc7306 1643
ff105f75 1644 error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
88fc7306 1645 if (error)
ff105f75 1646 goto error_icur;
88fc7306
BF
1647
1648 /*
ff105f75
DC
1649 * Both trees have now been updated. We must update the perag and
1650 * superblock before we can check the freecount for each btree.
88fc7306
BF
1651 */
1652 be32_add_cpu(&agi->agi_freecount, -1);
1653 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
a426d0e1 1654 pag->pagi_freecount--;
88fc7306
BF
1655
1656 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1657
1dbf114f 1658 error = xfs_check_agi_freecount(icur);
88fc7306 1659 if (error)
ff105f75 1660 goto error_icur;
1dbf114f 1661 error = xfs_check_agi_freecount(cur);
88fc7306 1662 if (error)
ff105f75 1663 goto error_icur;
88fc7306 1664
ff105f75 1665 xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
88fc7306 1666 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
88fc7306
BF
1667 *inop = ino;
1668 return 0;
1669
ff105f75
DC
1670error_icur:
1671 xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
88fc7306
BF
1672error_cur:
1673 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
88fc7306
BF
1674 return error;
1675}
1676
f7e5a7ae 1677static int
9543cb30
DC
1678xfs_dialloc_roll(
1679 struct xfs_trans **tpp,
1680 struct xfs_buf *agibp)
1681{
1682 struct xfs_trans *tp = *tpp;
1683 struct xfs_dquot_acct *dqinfo;
1684 int error;
1685
1686 /*
1687 * Hold to on to the agibp across the commit so no other allocation can
1688 * come in and take the free inodes we just allocated for our caller.
1689 */
1690 xfs_trans_bhold(tp, agibp);
1691
1692 /*
1693 * We want the quota changes to be associated with the next transaction,
1694 * NOT this one. So, detach the dqinfo from this and attach it to the
1695 * next transaction.
1696 */
1697 dqinfo = tp->t_dqinfo;
1698 tp->t_dqinfo = NULL;
1699
1700 error = xfs_trans_roll(&tp);
1701
1702 /* Re-attach the quota info that we detached from prev trx. */
1703 tp->t_dqinfo = dqinfo;
1704
771d0670
DC
1705 /*
1706 * Join the buffer even on commit error so that the buffer is released
1707 * when the caller cancels the transaction and doesn't have to handle
1708 * this error case specially.
1709 */
9543cb30 1710 xfs_trans_bjoin(tp, agibp);
771d0670
DC
1711 *tpp = tp;
1712 return error;
9543cb30
DC
1713}
1714
771d0670
DC
1715static bool
1716xfs_dialloc_good_ag(
771d0670 1717 struct xfs_perag *pag,
90e549b5 1718 struct xfs_trans *tp,
771d0670
DC
1719 umode_t mode,
1720 int flags,
1721 bool ok_alloc)
1722{
1723 struct xfs_mount *mp = tp->t_mountp;
1724 xfs_extlen_t ineed;
1725 xfs_extlen_t longest = 0;
1726 int needspace;
1727 int error;
1728
90e549b5
DC
1729 if (!pag)
1730 return false;
03dc2ef2 1731 if (!xfs_perag_allows_inodes(pag))
771d0670
DC
1732 return false;
1733
03dc2ef2 1734 if (!xfs_perag_initialised_agi(pag)) {
fd841d7c 1735 error = xfs_ialloc_read_agi(pag, tp, 0, NULL);
771d0670
DC
1736 if (error)
1737 return false;
1738 }
1739
1740 if (pag->pagi_freecount)
1741 return true;
1742 if (!ok_alloc)
1743 return false;
1744
03dc2ef2 1745 if (!xfs_perag_initialised_agf(pag)) {
f9084bd9 1746 error = xfs_alloc_read_agf(pag, tp, flags, NULL);
771d0670
DC
1747 if (error)
1748 return false;
1749 }
1750
1751 /*
1752 * Check that there is enough free space for the file plus a chunk of
1753 * inodes if we need to allocate some. If this is the first pass across
1754 * the AGs, take into account the potential space needed for alignment
1755 * of inode chunks when checking the longest contiguous free space in
1756 * the AG - this prevents us from getting ENOSPC because we have free
1757 * space larger than ialloc_blks but alignment constraints prevent us
1758 * from using it.
1759 *
1760 * If we can't find an AG with space for full alignment slack to be
1761 * taken into account, we must be near ENOSPC in all AGs. Hence we
1762 * don't include alignment for the second pass and so if we fail
1763 * allocation due to alignment issues then it is most likely a real
1764 * ENOSPC condition.
1765 *
1766 * XXX(dgc): this calculation is now bogus thanks to the per-ag
1767 * reservations that xfs_alloc_fix_freelist() now does via
1768 * xfs_alloc_space_available(). When the AG fills up, pagf_freeblks will
1769 * be more than large enough for the check below to succeed, but
1770 * xfs_alloc_space_available() will fail because of the non-zero
1771 * metadata reservation and hence we won't actually be able to allocate
1772 * more inodes in this AG. We do soooo much unnecessary work near ENOSPC
1773 * because of this.
1774 */
1775 ineed = M_IGEO(mp)->ialloc_min_blks;
1776 if (flags && ineed > 1)
1777 ineed += M_IGEO(mp)->cluster_align;
1778 longest = pag->pagf_longest;
1779 if (!longest)
1780 longest = pag->pagf_flcount > 0;
1781 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
1782
1783 if (pag->pagf_freeblks < needspace + ineed || longest < ineed)
1784 return false;
1785 return true;
1786}
1787
1788static int
1789xfs_dialloc_try_ag(
771d0670 1790 struct xfs_perag *pag,
90e549b5 1791 struct xfs_trans **tpp,
771d0670
DC
1792 xfs_ino_t parent,
1793 xfs_ino_t *new_ino,
1794 bool ok_alloc)
1795{
1796 struct xfs_buf *agbp;
1797 xfs_ino_t ino;
1798 int error;
1799
1800 /*
1801 * Then read in the AGI buffer and recheck with the AGI buffer
1802 * lock held.
1803 */
fd841d7c 1804 error = xfs_ialloc_read_agi(pag, *tpp, 0, &agbp);
771d0670
DC
1805 if (error)
1806 return error;
1807
1808 if (!pag->pagi_freecount) {
1809 if (!ok_alloc) {
1810 error = -EAGAIN;
1811 goto out_release;
1812 }
1813
90e549b5 1814 error = xfs_ialloc_ag_alloc(pag, *tpp, agbp);
771d0670
DC
1815 if (error < 0)
1816 goto out_release;
1817
1818 /*
1819 * We successfully allocated space for an inode cluster in this
1820 * AG. Roll the transaction so that we can allocate one of the
1821 * new inodes.
1822 */
1823 ASSERT(pag->pagi_freecount > 0);
1824 error = xfs_dialloc_roll(tpp, agbp);
1825 if (error)
1826 goto out_release;
1827 }
1828
1829 /* Allocate an inode in the found AG */
90e549b5 1830 error = xfs_dialloc_ag(pag, *tpp, agbp, parent, &ino);
771d0670
DC
1831 if (!error)
1832 *new_ino = ino;
1833 return error;
1834
1835out_release:
1836 xfs_trans_brelse(*tpp, agbp);
1837 return error;
1838}
1839
da7865a2
DW
1840/*
1841 * Pick an AG for the new inode.
1842 *
1843 * Directories, symlinks, and regular files frequently allocate at least one
1844 * block, so factor that potential expansion when we examine whether an AG has
1845 * enough space for file creation. Try to keep metadata files all in the same
1846 * AG.
1847 */
1848static inline xfs_agnumber_t
1849xfs_dialloc_pick_ag(
1850 struct xfs_mount *mp,
1851 struct xfs_inode *dp,
1852 umode_t mode)
1853{
1854 xfs_agnumber_t start_agno;
1855
1856 if (!dp)
1857 return 0;
1858 if (xfs_is_metadir_inode(dp)) {
1859 if (mp->m_sb.sb_logstart)
1860 return XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart);
1861 return 0;
1862 }
1863
1864 if (S_ISDIR(mode))
1865 return (atomic_inc_return(&mp->m_agirotor) - 1) % mp->m_maxagi;
1866
1867 start_agno = XFS_INO_TO_AGNO(mp, dp->i_ino);
1868 if (start_agno >= mp->m_maxagi)
1869 start_agno = 0;
1870
1871 return start_agno;
1872}
1873
a2ceac1f 1874/*
771d0670 1875 * Allocate an on-disk inode.
a2ceac1f 1876 *
8f1e9017 1877 * Mode is used to tell whether the new inode is a directory and hence where to
771d0670
DC
1878 * locate it. The on-disk inode that is allocated will be returned in @new_ino
1879 * on success, otherwise an error will be set to indicate the failure (e.g.
1880 * -ENOSPC).
a2ceac1f
DC
1881 */
1882int
51b1e167 1883xfs_dialloc(
f7e5a7ae 1884 struct xfs_trans **tpp,
bca9de39 1885 const struct xfs_icreate_args *args,
51b1e167 1886 xfs_ino_t *new_ino)
a2ceac1f 1887{
f7e5a7ae 1888 struct xfs_mount *mp = (*tpp)->t_mountp;
da7865a2
DW
1889 struct xfs_perag *pag;
1890 struct xfs_ino_geometry *igeo = M_IGEO(mp);
1891 xfs_ino_t ino = NULLFSINO;
bca9de39 1892 xfs_ino_t parent = args->pip ? args->pip->i_ino : 0;
a2ceac1f 1893 xfs_agnumber_t agno;
a2ceac1f 1894 xfs_agnumber_t start_agno;
da7865a2 1895 umode_t mode = args->mode & S_IFMT;
771d0670 1896 bool ok_alloc = true;
33f3aac8 1897 bool low_space = false;
d38ed6a9 1898 int flags;
da7865a2 1899 int error = 0;
8f1e9017 1900
da7865a2 1901 start_agno = xfs_dialloc_pick_ag(mp, args->pip, mode);
a2ceac1f
DC
1902
1903 /*
1904 * If we have already hit the ceiling of inode blocks then clear
771d0670 1905 * ok_alloc so we scan all available agi structures for a free
a2ceac1f 1906 * inode.
cbf3beaa
GW
1907 *
1908 * Read rough value of mp->m_icount by percpu_counter_read_positive,
1909 * which will sacrifice the preciseness but improve the performance.
a2ceac1f 1910 */
e7fd2b6f
DW
1911 if (igeo->maxicount &&
1912 percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos
1913 > igeo->maxicount) {
771d0670 1914 ok_alloc = false;
a2ceac1f
DC
1915 }
1916
33f3aac8
DC
1917 /*
1918 * If we are near to ENOSPC, we want to prefer allocation from AGs that
1919 * have free inodes in them rather than use up free space allocating new
1920 * inode chunks. Hence we turn off allocation for the first non-blocking
1921 * pass through the AGs if we are near ENOSPC to consume free inodes
1922 * that we can immediately allocate, but then we allow allocation on the
1923 * second pass if we fail to find an AG with free inodes in it.
1924 */
91643efd 1925 if (xfs_estimate_freecounter(mp, XC_FREE_BLOCKS) <
33f3aac8
DC
1926 mp->m_low_space[XFS_LOWSP_1_PCNT]) {
1927 ok_alloc = false;
1928 low_space = true;
1929 }
1930
a2ceac1f
DC
1931 /*
1932 * Loop until we find an allocation group that either has free inodes
1933 * or in which we can allocate some inodes. Iterate through the
1934 * allocation groups upward, wrapping at the end.
1935 */
d38ed6a9 1936 flags = XFS_ALLOC_FLAG_TRYLOCK;
5cb4ffc8
DC
1937retry:
1938 for_each_perag_wrap_at(mp, start_agno, mp->m_maxagi, agno, pag) {
90e549b5
DC
1939 if (xfs_dialloc_good_ag(pag, *tpp, mode, flags, ok_alloc)) {
1940 error = xfs_dialloc_try_ag(pag, tpp, parent,
771d0670
DC
1941 &ino, ok_alloc);
1942 if (error != -EAGAIN)
3fbe9fa3 1943 break;
5cb4ffc8 1944 error = 0;
a2ceac1f
DC
1945 }
1946
93adb06a 1947 if (xfs_is_shutdown(mp)) {
d38ed6a9 1948 error = -EFSCORRUPTED;
3fbe9fa3 1949 break;
d38ed6a9 1950 }
5cb4ffc8
DC
1951 }
1952 if (pag)
1953 xfs_perag_rele(pag);
1954 if (error)
1955 return error;
1956 if (ino == NULLFSINO) {
1957 if (flags) {
d38ed6a9 1958 flags = 0;
33f3aac8
DC
1959 if (low_space)
1960 ok_alloc = true;
5cb4ffc8 1961 goto retry;
d38ed6a9 1962 }
5cb4ffc8 1963 return -ENOSPC;
a2ceac1f 1964 }
02df7258
DW
1965
1966 /*
1967 * Protect against obviously corrupt allocation btree records. Later
1968 * xfs_iget checks will catch re-allocation of other active in-memory
1969 * and on-disk inodes. If we don't catch reallocating the parent inode
1970 * here we will deadlock in xfs_iget() so we have to do these checks
1971 * first.
1972 */
1973 if (ino == parent || !xfs_verify_dir_ino(mp, ino)) {
1974 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino);
1975 xfs_agno_mark_sick(mp, XFS_INO_TO_AGNO(mp, ino),
1976 XFS_SICK_AG_INOBT);
1977 return -EFSCORRUPTED;
1978 }
1979
5cb4ffc8
DC
1980 *new_ino = ino;
1981 return 0;
a2ceac1f
DC
1982}
1983
7338c4b8
BF
1984/*
1985 * Free the blocks of an inode chunk. We must consider that the inode chunk
1986 * might be sparse and only free the regions that are allocated as part of the
1987 * chunk.
1988 */
cd3e5d3c 1989static int
7338c4b8 1990xfs_difree_inode_chunk(
21375e5d 1991 struct xfs_trans *tp,
b3cdbd92 1992 struct xfs_perag *pag,
21375e5d 1993 struct xfs_inobt_rec_incore *rec)
7338c4b8 1994{
21375e5d
BF
1995 struct xfs_mount *mp = tp->t_mountp;
1996 xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp,
1997 rec->ir_startino);
1998 int startidx, endidx;
1999 int nextbit;
2000 xfs_agblock_t agbno;
2001 int contigblk;
7338c4b8
BF
2002 DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
2003
2004 if (!xfs_inobt_issparse(rec->ir_holemask)) {
2005 /* not sparse, calculate extent info directly */
25e0d559 2006 return xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, sagbno),
ef16737e 2007 M_IGEO(mp)->ialloc_blks, &XFS_RMAP_OINFO_INODES,
ad2fb6bc 2008 XFS_AG_RESV_NONE, 0);
7338c4b8
BF
2009 }
2010
2011 /* holemask is only 16-bits (fits in an unsigned long) */
2012 ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
2013 holemask[0] = rec->ir_holemask;
2014
2015 /*
2016 * Find contiguous ranges of zeroes (i.e., allocated regions) in the
2017 * holemask and convert the start/end index of each range to an extent.
2018 * We start with the start and end index both pointing at the first 0 in
2019 * the mask.
2020 */
2021 startidx = endidx = find_first_zero_bit(holemask,
2022 XFS_INOBT_HOLEMASK_BITS);
2023 nextbit = startidx + 1;
2024 while (startidx < XFS_INOBT_HOLEMASK_BITS) {
cd3e5d3c
DC
2025 int error;
2026
7338c4b8
BF
2027 nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
2028 nextbit);
2029 /*
2030 * If the next zero bit is contiguous, update the end index of
2031 * the current range and continue.
2032 */
2033 if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
2034 nextbit == endidx + 1) {
2035 endidx = nextbit;
2036 goto next;
2037 }
2038
2039 /*
2040 * nextbit is not contiguous with the current end index. Convert
2041 * the current start/end to an extent and add it to the free
2042 * list.
2043 */
2044 agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
2045 mp->m_sb.sb_inopblock;
2046 contigblk = ((endidx - startidx + 1) *
2047 XFS_INODES_PER_HOLEMASK_BIT) /
2048 mp->m_sb.sb_inopblock;
2049
2050 ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
2051 ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
25e0d559
CH
2052 error = xfs_free_extent_later(tp, xfs_agbno_to_fsb(pag, agbno),
2053 contigblk, &XFS_RMAP_OINFO_INODES,
2054 XFS_AG_RESV_NONE, 0);
cd3e5d3c
DC
2055 if (error)
2056 return error;
7338c4b8
BF
2057
2058 /* reset range to current bit and carry on... */
2059 startidx = endidx = nextbit;
2060
2061next:
2062 nextbit++;
2063 }
cd3e5d3c 2064 return 0;
7338c4b8
BF
2065}
2066
eb9a297a
BF
2067STATIC int
2068xfs_difree_inobt(
90e549b5 2069 struct xfs_perag *pag,
eb9a297a
BF
2070 struct xfs_trans *tp,
2071 struct xfs_buf *agbp,
2072 xfs_agino_t agino,
5a3b2e0a 2073 struct xfs_icluster *xic,
eb9a297a 2074 struct xfs_inobt_rec_incore *orec)
3439d03a 2075{
6a271c73 2076 struct xfs_mount *mp = pag_mount(pag);
c99cea5c 2077 struct xfs_agi *agi = agbp->b_addr;
eb9a297a
BF
2078 struct xfs_btree_cur *cur;
2079 struct xfs_inobt_rec_incore rec;
2080 int ilen;
2081 int error;
2082 int i;
2083 int off;
3439d03a 2084
3439d03a 2085 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
eb9a297a
BF
2086 ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
2087
3439d03a
DC
2088 /*
2089 * Initialize the cursor.
2090 */
7199f984 2091 cur = xfs_inobt_init_cursor(pag, tp, agbp);
3439d03a 2092
1dbf114f 2093 error = xfs_check_agi_freecount(cur);
3439d03a
DC
2094 if (error)
2095 goto error0;
2096
2097 /*
2098 * Look for the entry describing this inode.
2099 */
2100 if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
2101 xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
2102 __func__, error);
2103 goto error0;
2104 }
fbb4fa7f 2105 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2106 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2107 error = -EFSCORRUPTED;
2108 goto error0;
2109 }
3439d03a
DC
2110 error = xfs_inobt_get_rec(cur, &rec, &i);
2111 if (error) {
2112 xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
2113 __func__, error);
2114 goto error0;
2115 }
fbb4fa7f 2116 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2117 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2118 error = -EFSCORRUPTED;
2119 goto error0;
2120 }
3439d03a
DC
2121 /*
2122 * Get the offset in the inode chunk.
2123 */
2124 off = agino - rec.ir_startino;
2125 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
2126 ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
2127 /*
2128 * Mark the inode free & increment the count.
2129 */
2130 rec.ir_free |= XFS_INOBT_MASK(off);
2131 rec.ir_freecount++;
2132
2133 /*
f6580bcf
BF
2134 * When an inode chunk is free, it becomes eligible for removal. Don't
2135 * remove the chunk if the block size is large enough for multiple inode
2136 * chunks (that might not be free).
3439d03a 2137 */
914e2a04 2138 if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
f6580bcf 2139 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
180d3cd8 2140 xic->deleted = true;
ef7499e4 2141 xic->first_ino = xfs_agino_to_ino(pag, rec.ir_startino);
5a3b2e0a 2142 xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
3439d03a
DC
2143
2144 /*
2145 * Remove the inode cluster from the AGI B+Tree, adjust the
2146 * AGI and Superblock inode counts, and mark the disk space
2147 * to be freed when the transaction is committed.
2148 */
f6580bcf 2149 ilen = rec.ir_freecount;
3439d03a
DC
2150 be32_add_cpu(&agi->agi_count, -ilen);
2151 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
2152 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
3439d03a 2153 pag->pagi_freecount -= ilen - 1;
a3204ee7 2154 pag->pagi_count -= ilen;
3439d03a
DC
2155 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
2156 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
2157
2158 if ((error = xfs_btree_delete(cur, &i))) {
2159 xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
2160 __func__, error);
2161 goto error0;
2162 }
2163
b3cdbd92 2164 error = xfs_difree_inode_chunk(tp, pag, &rec);
cd3e5d3c
DC
2165 if (error)
2166 goto error0;
3439d03a 2167 } else {
180d3cd8 2168 xic->deleted = false;
3439d03a
DC
2169
2170 error = xfs_inobt_update(cur, &rec);
2171 if (error) {
2172 xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
2173 __func__, error);
2174 goto error0;
2175 }
2176
f8149110 2177 /*
3439d03a
DC
2178 * Change the inode free counts and log the ag/sb changes.
2179 */
2180 be32_add_cpu(&agi->agi_freecount, 1);
2181 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
a426d0e1 2182 pag->pagi_freecount++;
3439d03a
DC
2183 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
2184 }
2185
1dbf114f 2186 error = xfs_check_agi_freecount(cur);
3439d03a
DC
2187 if (error)
2188 goto error0;
2189
eb9a297a 2190 *orec = rec;
3439d03a
DC
2191 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2192 return 0;
2193
2194error0:
2195 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2196 return error;
2197}
2198
1bb93fd1
BF
2199/*
2200 * Free an inode in the free inode btree.
2201 */
2202STATIC int
2203xfs_difree_finobt(
90e549b5 2204 struct xfs_perag *pag,
1bb93fd1
BF
2205 struct xfs_trans *tp,
2206 struct xfs_buf *agbp,
2207 xfs_agino_t agino,
2208 struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
2209{
6a271c73 2210 struct xfs_mount *mp = pag_mount(pag);
1bb93fd1
BF
2211 struct xfs_btree_cur *cur;
2212 struct xfs_inobt_rec_incore rec;
2213 int offset = agino - ibtrec->ir_startino;
2214 int error;
2215 int i;
2216
7199f984 2217 cur = xfs_finobt_init_cursor(pag, tp, agbp);
1bb93fd1
BF
2218
2219 error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
2220 if (error)
2221 goto error;
2222 if (i == 0) {
2223 /*
2224 * If the record does not exist in the finobt, we must have just
2225 * freed an inode in a previously fully allocated chunk. If not,
2226 * something is out of sync.
2227 */
fbb4fa7f 2228 if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) {
4467a60a 2229 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2230 error = -EFSCORRUPTED;
2231 goto error;
2232 }
1bb93fd1 2233
11640e30
BF
2234 error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
2235 ibtrec->ir_count,
2236 ibtrec->ir_freecount,
1bb93fd1
BF
2237 ibtrec->ir_free, &i);
2238 if (error)
2239 goto error;
2240 ASSERT(i == 1);
2241
2242 goto out;
2243 }
2244
2245 /*
ff105f75
DC
2246 * Read and update the existing record. We could just copy the ibtrec
2247 * across here, but that would defeat the purpose of having redundant
2248 * metadata. By making the modifications independently, we can catch
2249 * corruptions that we wouldn't see if we just copied from one record
2250 * to another.
1bb93fd1
BF
2251 */
2252 error = xfs_inobt_get_rec(cur, &rec, &i);
2253 if (error)
2254 goto error;
fbb4fa7f 2255 if (XFS_IS_CORRUPT(mp, i != 1)) {
4467a60a 2256 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2257 error = -EFSCORRUPTED;
2258 goto error;
2259 }
1bb93fd1
BF
2260
2261 rec.ir_free |= XFS_INOBT_MASK(offset);
2262 rec.ir_freecount++;
2263
fbb4fa7f
DW
2264 if (XFS_IS_CORRUPT(mp,
2265 rec.ir_free != ibtrec->ir_free ||
2266 rec.ir_freecount != ibtrec->ir_freecount)) {
4467a60a 2267 xfs_btree_mark_sick(cur);
fbb4fa7f
DW
2268 error = -EFSCORRUPTED;
2269 goto error;
2270 }
1bb93fd1
BF
2271
2272 /*
2273 * The content of inobt records should always match between the inobt
2274 * and finobt. The lifecycle of records in the finobt is different from
2275 * the inobt in that the finobt only tracks records with at least one
ff105f75
DC
2276 * free inode. Hence, if all of the inodes are free and we aren't
2277 * keeping inode chunks permanently on disk, remove the record.
2278 * Otherwise, update the record with the new information.
f6580bcf
BF
2279 *
2280 * Note that we currently can't free chunks when the block size is large
2281 * enough for multiple chunks. Leave the finobt record to remain in sync
2282 * with the inobt.
1bb93fd1 2283 */
914e2a04
DC
2284 if (!xfs_has_ikeep(mp) && rec.ir_free == XFS_INOBT_ALL_FREE &&
2285 mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
1bb93fd1
BF
2286 error = xfs_btree_delete(cur, &i);
2287 if (error)
2288 goto error;
2289 ASSERT(i == 1);
2290 } else {
1bb93fd1
BF
2291 error = xfs_inobt_update(cur, &rec);
2292 if (error)
2293 goto error;
2294 }
2295
2296out:
1dbf114f 2297 error = xfs_check_agi_freecount(cur);
1bb93fd1
BF
2298 if (error)
2299 goto error;
2300
2301 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2302 return 0;
2303
2304error:
2305 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2306 return error;
2307}
2308
eb9a297a
BF
2309/*
2310 * Free disk inode. Carefully avoids touching the incore inode, all
2311 * manipulations incore are the caller's responsibility.
2312 * The on-disk inode is not changed by this operation, only the
2313 * btree (free inode mask) is changed.
2314 */
2315int
2316xfs_difree(
b8268cdd
DC
2317 struct xfs_trans *tp,
2318 struct xfs_perag *pag,
2319 xfs_ino_t inode,
2320 struct xfs_icluster *xic)
eb9a297a
BF
2321{
2322 /* REFERENCED */
2323 xfs_agblock_t agbno; /* block number containing inode */
2324 struct xfs_buf *agbp; /* buffer for allocation group header */
2325 xfs_agino_t agino; /* allocation group inode number */
eb9a297a 2326 int error; /* error return value */
a426d0e1 2327 struct xfs_mount *mp = tp->t_mountp;
eb9a297a 2328 struct xfs_inobt_rec_incore rec;/* btree record */
eb9a297a
BF
2329
2330 /*
2331 * Break up inode number into its components.
2332 */
6a271c73
CH
2333 if (pag_agno(pag) != XFS_INO_TO_AGNO(mp, inode)) {
2334 xfs_warn(mp, "%s: agno != pag_agno(pag) (%d != %d).",
2335 __func__, XFS_INO_TO_AGNO(mp, inode), pag_agno(pag));
eb9a297a 2336 ASSERT(0);
12b53197 2337 return -EINVAL;
eb9a297a
BF
2338 }
2339 agino = XFS_INO_TO_AGINO(mp, inode);
ef7499e4
CH
2340 if (inode != xfs_agino_to_ino(pag, agino)) {
2341 xfs_warn(mp, "%s: inode != xfs_agino_to_ino() (%llu != %llu).",
eb9a297a 2342 __func__, (unsigned long long)inode,
ef7499e4 2343 (unsigned long long)xfs_agino_to_ino(pag, agino));
eb9a297a 2344 ASSERT(0);
12b53197 2345 return -EINVAL;
eb9a297a
BF
2346 }
2347 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
12a11a2b
DC
2348 if (agbno >= xfs_ag_block_count(mp, pag_agno(pag))) {
2349 xfs_warn(mp, "%s: agbno >= xfs_ag_block_count (%d >= %d).",
2350 __func__, agbno, xfs_ag_block_count(mp, pag_agno(pag)));
eb9a297a 2351 ASSERT(0);
12b53197 2352 return -EINVAL;
eb9a297a
BF
2353 }
2354 /*
2355 * Get the allocation group header.
2356 */
fd841d7c 2357 error = xfs_ialloc_read_agi(pag, tp, 0, &agbp);
eb9a297a
BF
2358 if (error) {
2359 xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
2360 __func__, error);
2361 return error;
2362 }
2363
2364 /*
2365 * Fix up the inode allocation btree.
2366 */
90e549b5 2367 error = xfs_difree_inobt(pag, tp, agbp, agino, xic, &rec);
eb9a297a
BF
2368 if (error)
2369 goto error0;
2370
1bb93fd1
BF
2371 /*
2372 * Fix up the free inode btree.
2373 */
94541a16 2374 if (xfs_has_finobt(mp)) {
90e549b5 2375 error = xfs_difree_finobt(pag, tp, agbp, agino, &rec);
1bb93fd1
BF
2376 if (error)
2377 goto error0;
2378 }
2379
eb9a297a
BF
2380 return 0;
2381
2382error0:
2383 return error;
2384}
2385
56b2de80
DC
2386STATIC int
2387xfs_imap_lookup(
a426d0e1 2388 struct xfs_perag *pag,
e7722e28 2389 struct xfs_trans *tp,
56b2de80
DC
2390 xfs_agino_t agino,
2391 xfs_agblock_t agbno,
2392 xfs_agblock_t *chunk_agbno,
2393 xfs_agblock_t *offset_agbno,
2394 int flags)
2395{
6a271c73 2396 struct xfs_mount *mp = pag_mount(pag);
56b2de80
DC
2397 struct xfs_inobt_rec_incore rec;
2398 struct xfs_btree_cur *cur;
2399 struct xfs_buf *agbp;
2400 int error;
2401 int i;
2402
fd841d7c 2403 error = xfs_ialloc_read_agi(pag, tp, 0, &agbp);
56b2de80 2404 if (error) {
a2ceac1f
DC
2405 xfs_alert(mp,
2406 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
6a271c73 2407 __func__, error, pag_agno(pag));
56b2de80
DC
2408 return error;
2409 }
2410
2411 /*
2412 * Lookup the inode record for the given agino. If the record cannot be
2413 * found, then it's an invalid inode number and we should abort. Once
2414 * we have a record, we need to ensure it contains the inode number
2415 * we are looking up.
2416 */
7199f984 2417 cur = xfs_inobt_init_cursor(pag, tp, agbp);
56b2de80
DC
2418 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
2419 if (!error) {
2420 if (i)
2421 error = xfs_inobt_get_rec(cur, &rec, &i);
2422 if (!error && i == 0)
12b53197 2423 error = -EINVAL;
56b2de80
DC
2424 }
2425
2426 xfs_trans_brelse(tp, agbp);
660265b7 2427 xfs_btree_del_cursor(cur, error);
56b2de80
DC
2428 if (error)
2429 return error;
2430
2431 /* check that the returned record contains the required inode */
2432 if (rec.ir_startino > agino ||
e7fd2b6f 2433 rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino)
12b53197 2434 return -EINVAL;
56b2de80
DC
2435
2436 /* for untrusted inodes check it is allocated first */
2437 if ((flags & XFS_IGET_UNTRUSTED) &&
2438 (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
12b53197 2439 return -EINVAL;
56b2de80
DC
2440
2441 *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
2442 *offset_agbno = agbno - *chunk_agbno;
2443 return 0;
2444}
2bd0ea18
NS
2445
2446/*
56b2de80 2447 * Return the location of the inode in imap, for mapping it into a buffer.
2bd0ea18 2448 */
2bd0ea18 2449int
56b2de80 2450xfs_imap(
e7722e28
DC
2451 struct xfs_perag *pag,
2452 struct xfs_trans *tp,
a426d0e1
DC
2453 xfs_ino_t ino, /* inode to locate */
2454 struct xfs_imap *imap, /* location map structure */
2455 uint flags) /* flags for inode btree lookup */
2bd0ea18 2456{
6a271c73 2457 struct xfs_mount *mp = pag_mount(pag);
a426d0e1
DC
2458 xfs_agblock_t agbno; /* block number of inode in the alloc group */
2459 xfs_agino_t agino; /* inode number within alloc group */
2460 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
2461 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
2462 int error; /* error code */
2463 int offset; /* index of inode in its buffer */
2464 xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
2bd0ea18
NS
2465
2466 ASSERT(ino != NULLFSINO);
56b2de80 2467
2bd0ea18
NS
2468 /*
2469 * Split up the inode number into its parts.
2470 */
2bd0ea18
NS
2471 agino = XFS_INO_TO_AGINO(mp, ino);
2472 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
12a11a2b 2473 if (agbno >= xfs_ag_block_count(mp, pag_agno(pag)) ||
ef7499e4 2474 ino != xfs_agino_to_ino(pag, agino)) {
a426d0e1 2475 error = -EINVAL;
63518810 2476#ifdef DEBUG
56b2de80
DC
2477 /*
2478 * Don't output diagnostic information for untrusted inodes
2479 * as they can be invalid without implying corruption.
2480 */
2481 if (flags & XFS_IGET_UNTRUSTED)
e7722e28 2482 return error;
12a11a2b 2483 if (agbno >= xfs_ag_block_count(mp, pag_agno(pag))) {
a2ceac1f
DC
2484 xfs_alert(mp,
2485 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
2486 __func__, (unsigned long long)agbno,
12a11a2b
DC
2487 (unsigned long)xfs_ag_block_count(mp,
2488 pag_agno(pag)));
63518810 2489 }
ef7499e4 2490 if (ino != xfs_agino_to_ino(pag, agino)) {
a2ceac1f 2491 xfs_alert(mp,
ef7499e4 2492 "%s: ino (0x%llx) != xfs_agino_to_ino() (0x%llx)",
a2ceac1f 2493 __func__, ino,
ef7499e4 2494 xfs_agino_to_ino(pag, agino));
63518810 2495 }
5e656dbb 2496 xfs_stack_trace();
63518810 2497#endif /* DEBUG */
e7722e28 2498 return error;
63518810 2499 }
56b2de80 2500
56b2de80
DC
2501 /*
2502 * For bulkstat and handle lookups, we have an untrusted inode number
2503 * that we have to verify is valid. We cannot do this just by reading
2504 * the inode buffer as it may have been unlinked and removed leaving
2505 * inodes in stale state on disk. Hence we have to do a btree lookup
2506 * in all cases where an untrusted inode number is passed.
2507 */
2508 if (flags & XFS_IGET_UNTRUSTED) {
e7722e28 2509 error = xfs_imap_lookup(pag, tp, agino, agbno,
56b2de80
DC
2510 &chunk_agbno, &offset_agbno, flags);
2511 if (error)
e7722e28 2512 return error;
56b2de80
DC
2513 goto out_map;
2514 }
2515
2516 /*
2517 * If the inode cluster size is the same as the blocksize or
2518 * smaller we get to the buffer by simple arithmetics.
2519 */
e7fd2b6f 2520 if (M_IGEO(mp)->blocks_per_cluster == 1) {
2bd0ea18
NS
2521 offset = XFS_INO_TO_OFFSET(mp, ino);
2522 ASSERT(offset < mp->m_sb.sb_inopblock);
56b2de80 2523
25e0d559 2524 imap->im_blkno = xfs_agbno_to_daddr(pag, agbno);
56b2de80 2525 imap->im_len = XFS_FSB_TO_BB(mp, 1);
187f8c22
DW
2526 imap->im_boffset = (unsigned short)(offset <<
2527 mp->m_sb.sb_inodelog);
e7722e28 2528 return 0;
2bd0ea18 2529 }
56b2de80
DC
2530
2531 /*
2532 * If the inode chunks are aligned then use simple maths to
2533 * find the location. Otherwise we have to do a btree
2534 * lookup to find the location.
2535 */
e7fd2b6f
DW
2536 if (M_IGEO(mp)->inoalign_mask) {
2537 offset_agbno = agbno & M_IGEO(mp)->inoalign_mask;
2bd0ea18
NS
2538 chunk_agbno = agbno - offset_agbno;
2539 } else {
e7722e28 2540 error = xfs_imap_lookup(pag, tp, agino, agbno,
56b2de80 2541 &chunk_agbno, &offset_agbno, flags);
2bd0ea18 2542 if (error)
e7722e28 2543 return error;
2bd0ea18 2544 }
56b2de80
DC
2545
2546out_map:
2bd0ea18
NS
2547 ASSERT(agbno >= chunk_agbno);
2548 cluster_agbno = chunk_agbno +
e7fd2b6f
DW
2549 ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) *
2550 M_IGEO(mp)->blocks_per_cluster);
2bd0ea18
NS
2551 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
2552 XFS_INO_TO_OFFSET(mp, ino);
56b2de80 2553
25e0d559 2554 imap->im_blkno = xfs_agbno_to_daddr(pag, cluster_agbno);
e7fd2b6f 2555 imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
919dbc78 2556 imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
56b2de80
DC
2557
2558 /*
2559 * If the inode number maps to a block outside the bounds
2560 * of the file system then return NULL rather than calling
2561 * read_buf and panicing when we get an error from the
2562 * driver.
2563 */
2564 if ((imap->im_blkno + imap->im_len) >
2565 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
a2ceac1f
DC
2566 xfs_alert(mp,
2567 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
2568 __func__, (unsigned long long) imap->im_blkno,
56b2de80
DC
2569 (unsigned long long) imap->im_len,
2570 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
e7722e28 2571 return -EINVAL;
56b2de80 2572 }
e7722e28 2573 return 0;
2bd0ea18
NS
2574}
2575
2bd0ea18 2576/*
ff105f75
DC
2577 * Log specified fields for the ag hdr (inode section). The growth of the agi
2578 * structure over time requires that we interpret the buffer as two logical
2579 * regions delineated by the end of the unlinked list. This is due to the size
2580 * of the hash table and its location in the middle of the agi.
2581 *
2582 * For example, a request to log a field before agi_unlinked and a field after
2583 * agi_unlinked could cause us to log the entire hash table and use an excessive
2584 * amount of log space. To avoid this behavior, log the region up through
2585 * agi_unlinked in one call and the region after agi_unlinked through the end of
2586 * the structure in another.
2bd0ea18
NS
2587 */
2588void
2589xfs_ialloc_log_agi(
a8f712a6
DC
2590 struct xfs_trans *tp,
2591 struct xfs_buf *bp,
2592 uint32_t fields)
2bd0ea18
NS
2593{
2594 int first; /* first byte number */
2595 int last; /* last byte number */
2596 static const short offsets[] = { /* field starting offsets */
2597 /* keep in sync with bit definitions */
2598 offsetof(xfs_agi_t, agi_magicnum),
2599 offsetof(xfs_agi_t, agi_versionnum),
2600 offsetof(xfs_agi_t, agi_seqno),
2601 offsetof(xfs_agi_t, agi_length),
2602 offsetof(xfs_agi_t, agi_count),
2603 offsetof(xfs_agi_t, agi_root),
2604 offsetof(xfs_agi_t, agi_level),
2605 offsetof(xfs_agi_t, agi_freecount),
2606 offsetof(xfs_agi_t, agi_newino),
2607 offsetof(xfs_agi_t, agi_dirino),
2608 offsetof(xfs_agi_t, agi_unlinked),
c0a4c227
BF
2609 offsetof(xfs_agi_t, agi_free_root),
2610 offsetof(xfs_agi_t, agi_free_level),
177c81e2 2611 offsetof(xfs_agi_t, agi_iblocks),
2bd0ea18
NS
2612 sizeof(xfs_agi_t)
2613 };
2614#ifdef DEBUG
c99cea5c 2615 struct xfs_agi *agi = bp->b_addr;
2bd0ea18 2616
a2ceac1f 2617 ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
2bd0ea18 2618#endif
ff105f75 2619
2bd0ea18 2620 /*
c0a4c227 2621 * Compute byte offsets for the first and last fields in the first
ff105f75
DC
2622 * region and log the agi buffer. This only logs up through
2623 * agi_unlinked.
2bd0ea18 2624 */
c0a4c227
BF
2625 if (fields & XFS_AGI_ALL_BITS_R1) {
2626 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2627 &first, &last);
2628 xfs_trans_log_buf(tp, bp, first, last);
2629 }
2630
2631 /*
ff105f75
DC
2632 * Mask off the bits in the first region and calculate the first and
2633 * last field offsets for any bits in the second region.
c0a4c227
BF
2634 */
2635 fields &= ~XFS_AGI_ALL_BITS_R1;
2636 if (fields) {
2637 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2638 &first, &last);
2639 xfs_trans_log_buf(tp, bp, first, last);
2640 }
2bd0ea18
NS
2641}
2642
bc01119d 2643static xfs_failaddr_t
a2ceac1f 2644xfs_agi_verify(
98572f41 2645 struct xfs_buf *bp)
a2ceac1f 2646{
98572f41
DW
2647 struct xfs_mount *mp = bp->b_mount;
2648 struct xfs_agi *agi = bp->b_addr;
2649 xfs_failaddr_t fa;
2650 uint32_t agi_seqno = be32_to_cpu(agi->agi_seqno);
2651 uint32_t agi_length = be32_to_cpu(agi->agi_length);
2652 int i;
a2ceac1f 2653
b16a427a 2654 if (xfs_has_crc(mp)) {
a65d8d29 2655 if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
bc01119d 2656 return __this_address;
c99cea5c 2657 if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn)))
bc01119d 2658 return __this_address;
a65d8d29
BF
2659 }
2660
a2ceac1f
DC
2661 /*
2662 * Validate the magic number of the agi block.
2663 */
68dbe77f 2664 if (!xfs_verify_magic(bp, agi->agi_magicnum))
bc01119d 2665 return __this_address;
dd5b876e 2666 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
bc01119d 2667 return __this_address;
a2ceac1f 2668
98572f41
DW
2669 fa = xfs_validate_ag_length(bp, agi_seqno, agi_length);
2670 if (fa)
2671 return fa;
2672
00795aae 2673 if (be32_to_cpu(agi->agi_level) < 1 ||
55369096 2674 be32_to_cpu(agi->agi_level) > M_IGEO(mp)->inobt_maxlevels)
bc01119d 2675 return __this_address;
00795aae 2676
b16a427a 2677 if (xfs_has_finobt(mp) &&
00795aae 2678 (be32_to_cpu(agi->agi_free_level) < 1 ||
55369096 2679 be32_to_cpu(agi->agi_free_level) > M_IGEO(mp)->inobt_maxlevels))
bc01119d 2680 return __this_address;
00795aae 2681
2d653064 2682 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
e66d0a46 2683 if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO))
2d653064
DC
2684 continue;
2685 if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
2686 return __this_address;
2687 }
2688
bc01119d 2689 return NULL;
a2ceac1f
DC
2690}
2691
2692static void
2693xfs_agi_read_verify(
2694 struct xfs_buf *bp)
2695{
7861ef77 2696 struct xfs_mount *mp = bp->b_mount;
1e697959 2697 xfs_failaddr_t fa;
dd5b876e 2698
b16a427a 2699 if (xfs_has_crc(mp) &&
45922933 2700 !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
1e697959
DW
2701 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2702 else {
2703 fa = xfs_agi_verify(bp);
2704 if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2705 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2706 }
a2ceac1f
DC
2707}
2708
2709static void
2710xfs_agi_write_verify(
2711 struct xfs_buf *bp)
2712{
7861ef77 2713 struct xfs_mount *mp = bp->b_mount;
37d086ca 2714 struct xfs_buf_log_item *bip = bp->b_log_item;
c99cea5c 2715 struct xfs_agi *agi = bp->b_addr;
1e697959 2716 xfs_failaddr_t fa;
dd5b876e 2717
1e697959
DW
2718 fa = xfs_agi_verify(bp);
2719 if (fa) {
2720 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
dd5b876e
DC
2721 return;
2722 }
2723
b16a427a 2724 if (!xfs_has_crc(mp))
dd5b876e
DC
2725 return;
2726
2727 if (bip)
c99cea5c 2728 agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
43b5aeed 2729 xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
a2ceac1f
DC
2730}
2731
2732const struct xfs_buf_ops xfs_agi_buf_ops = {
a3fac935 2733 .name = "xfs_agi",
68dbe77f 2734 .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) },
a2ceac1f
DC
2735 .verify_read = xfs_agi_read_verify,
2736 .verify_write = xfs_agi_write_verify,
95d9582b 2737 .verify_struct = xfs_agi_verify,
a2ceac1f
DC
2738};
2739
2bd0ea18
NS
2740/*
2741 * Read in the allocation group header (inode allocation section)
2742 */
2743int
56b2de80 2744xfs_read_agi(
bc87af99
DC
2745 struct xfs_perag *pag,
2746 struct xfs_trans *tp,
fd841d7c 2747 xfs_buf_flags_t flags,
bc87af99 2748 struct xfs_buf **agibpp)
2bd0ea18 2749{
6a271c73 2750 struct xfs_mount *mp = pag_mount(pag);
56b2de80 2751 int error;
2bd0ea18 2752
ffae013e 2753 trace_xfs_read_agi(pag);
56b2de80
DC
2754
2755 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
6a271c73 2756 XFS_AG_DADDR(mp, pag_agno(pag), XFS_AGI_DADDR(mp)),
fd841d7c 2757 XFS_FSS_TO_BB(mp, 1), flags, agibpp, &xfs_agi_buf_ops);
ad6fabbe
DW
2758 if (xfs_metadata_is_sick(error))
2759 xfs_ag_mark_sick(pag, XFS_SICK_AG_AGI);
9440d84d 2760 if (error)
2bd0ea18 2761 return error;
1b521475 2762 if (tp)
bc87af99 2763 xfs_trans_buf_set_type(tp, *agibpp, XFS_BLFT_AGI_BUF);
56b2de80 2764
bc87af99 2765 xfs_buf_set_ref(*agibpp, XFS_AGI_REF);
56b2de80
DC
2766 return 0;
2767}
2768
37dc5890
DC
2769/*
2770 * Read in the agi and initialise the per-ag data. If the caller supplies a
2771 * @agibpp, return the locked AGI buffer to them, otherwise release it.
2772 */
56b2de80
DC
2773int
2774xfs_ialloc_read_agi(
4330a9e0
DC
2775 struct xfs_perag *pag,
2776 struct xfs_trans *tp,
fd841d7c 2777 int flags,
37dc5890 2778 struct xfs_buf **agibpp)
56b2de80 2779{
37dc5890 2780 struct xfs_buf *agibp;
4330a9e0 2781 struct xfs_agi *agi;
56b2de80
DC
2782 int error;
2783
ffae013e 2784 trace_xfs_ialloc_read_agi(pag);
ff105f75 2785
fd841d7c
DW
2786 error = xfs_read_agi(pag, tp,
2787 (flags & XFS_IALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
2788 &agibp);
56b2de80
DC
2789 if (error)
2790 return error;
2791
37dc5890 2792 agi = agibp->b_addr;
03dc2ef2 2793 if (!xfs_perag_initialised_agi(pag)) {
6e3140c7 2794 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
cdded3d8 2795 pag->pagi_count = be32_to_cpu(agi->agi_count);
03dc2ef2 2796 set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
9440d84d 2797 }
9440d84d 2798
56b2de80
DC
2799 /*
2800 * It's possible for these to be out of sync if
2801 * we are in the middle of a forced shutdown.
2802 */
2803 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
6a271c73 2804 xfs_is_shutdown(pag_mount(pag)));
37dc5890
DC
2805 if (agibpp)
2806 *agibpp = agibp;
2807 else
2808 xfs_trans_brelse(tp, agibp);
cdded3d8
DC
2809 return 0;
2810}
be3bf25c 2811
fc78c405
DW
2812/* How many inodes are backed by inode clusters ondisk? */
2813STATIC int
2814xfs_ialloc_count_ondisk(
2815 struct xfs_btree_cur *cur,
2816 xfs_agino_t low,
2817 xfs_agino_t high,
2818 unsigned int *allocated)
9deb7f65
DW
2819{
2820 struct xfs_inobt_rec_incore irec;
fc78c405
DW
2821 unsigned int ret = 0;
2822 int has_record;
2823 int error;
9deb7f65 2824
9deb7f65 2825 error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
fc78c405
DW
2826 if (error)
2827 return error;
2828
2829 while (has_record) {
2830 unsigned int i, hole_idx;
2831
9deb7f65 2832 error = xfs_inobt_get_rec(cur, &irec, &has_record);
fc78c405
DW
2833 if (error)
2834 return error;
2835 if (irec.ir_startino > high)
9deb7f65
DW
2836 break;
2837
fc78c405
DW
2838 for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
2839 if (irec.ir_startino + i < low)
9deb7f65 2840 continue;
fc78c405
DW
2841 if (irec.ir_startino + i > high)
2842 break;
2843
2844 hole_idx = i / XFS_INODES_PER_HOLEMASK_BIT;
2845 if (!(irec.ir_holemask & (1U << hole_idx)))
2846 ret++;
9deb7f65
DW
2847 }
2848
2849 error = xfs_btree_increment(cur, 0, &has_record);
fc78c405
DW
2850 if (error)
2851 return error;
9deb7f65 2852 }
fc78c405
DW
2853
2854 *allocated = ret;
2855 return 0;
9deb7f65
DW
2856}
2857
2858/* Is there an inode record covering a given extent? */
2859int
2860xfs_ialloc_has_inodes_at_extent(
2861 struct xfs_btree_cur *cur,
2862 xfs_agblock_t bno,
2863 xfs_extlen_t len,
fc78c405 2864 enum xbtree_recpacking *outcome)
9deb7f65 2865{
fc78c405
DW
2866 xfs_agino_t agino;
2867 xfs_agino_t last_agino;
2868 unsigned int allocated;
2869 int error;
9deb7f65 2870
fc78c405
DW
2871 agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno);
2872 last_agino = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1;
9deb7f65 2873
fc78c405
DW
2874 error = xfs_ialloc_count_ondisk(cur, agino, last_agino, &allocated);
2875 if (error)
2876 return error;
2877
2878 if (allocated == 0)
2879 *outcome = XBTREE_RECPACKING_EMPTY;
2880 else if (allocated == last_agino - agino + 1)
2881 *outcome = XBTREE_RECPACKING_FULL;
2882 else
2883 *outcome = XBTREE_RECPACKING_SPARSE;
2884 return 0;
9deb7f65
DW
2885}
2886
2887struct xfs_ialloc_count_inodes {
2888 xfs_agino_t count;
2889 xfs_agino_t freecount;
2890};
2891
2892/* Record inode counts across all inobt records. */
2893STATIC int
2894xfs_ialloc_count_inodes_rec(
2895 struct xfs_btree_cur *cur,
e62318a3 2896 const union xfs_btree_rec *rec,
9deb7f65
DW
2897 void *priv)
2898{
2899 struct xfs_inobt_rec_incore irec;
2900 struct xfs_ialloc_count_inodes *ci = priv;
e70bf9ba 2901 xfs_failaddr_t fa;
9deb7f65
DW
2902
2903 xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
1fa5e300 2904 fa = xfs_inobt_check_irec(to_perag(cur->bc_group), &irec);
e70bf9ba
DW
2905 if (fa)
2906 return xfs_inobt_complain_bad_rec(cur, fa, &irec);
349aa687 2907
9deb7f65
DW
2908 ci->count += irec.ir_count;
2909 ci->freecount += irec.ir_freecount;
2910
2911 return 0;
2912}
2913
2914/* Count allocated and free inodes under an inobt. */
2915int
2916xfs_ialloc_count_inodes(
2917 struct xfs_btree_cur *cur,
2918 xfs_agino_t *count,
2919 xfs_agino_t *freecount)
2920{
2921 struct xfs_ialloc_count_inodes ci = {0};
2922 int error;
2923
1c3eaac0 2924 ASSERT(xfs_btree_is_ino(cur->bc_ops));
9deb7f65
DW
2925 error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
2926 if (error)
2927 return error;
2928
2929 *count = ci.count;
2930 *freecount = ci.freecount;
2931 return 0;
2932}
3a05ab22
DW
2933
2934/*
2935 * Initialize inode-related geometry information.
2936 *
2937 * Compute the inode btree min and max levels and set maxicount.
2938 *
2939 * Set the inode cluster size. This may still be overridden by the file
2940 * system block size if it is larger than the chosen cluster size.
2941 *
2942 * For v5 filesystems, scale the cluster size with the inode size to keep a
2943 * constant ratio of inode per cluster buffer, but only if mkfs has set the
2944 * inode alignment value appropriately for larger cluster sizes.
2945 *
2946 * Then compute the inode cluster alignment information.
2947 */
2948void
2949xfs_ialloc_setup_geometry(
2950 struct xfs_mount *mp)
2951{
2952 struct xfs_sb *sbp = &mp->m_sb;
2953 struct xfs_ino_geometry *igeo = M_IGEO(mp);
2954 uint64_t icount;
2955 uint inodes;
2956
e7e3beb9 2957 igeo->new_diflags2 = 0;
94541a16 2958 if (xfs_has_bigtime(mp))
e7e3beb9 2959 igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME;
02a86c25
CB
2960 if (xfs_has_large_extent_counts(mp))
2961 igeo->new_diflags2 |= XFS_DIFLAG2_NREXT64;
e7e3beb9 2962
3a05ab22
DW
2963 /* Compute inode btree geometry. */
2964 igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
07037e85
DW
2965 igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, true);
2966 igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, false);
3a05ab22
DW
2967 igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2;
2968 igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2;
2969
2970 igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
2971 sbp->sb_inopblock);
2972 igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog;
2973
2974 if (sbp->sb_spino_align)
2975 igeo->ialloc_min_blks = sbp->sb_spino_align;
2976 else
2977 igeo->ialloc_min_blks = igeo->ialloc_blks;
2978
2979 /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */
2980 inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
2981 igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr,
2982 inodes);
441815c7 2983 ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk());
3a05ab22 2984
78173279
DW
2985 /*
2986 * Set the maximum inode count for this filesystem, being careful not
2987 * to use obviously garbage sb_inopblog/sb_inopblock values. Regular
2988 * users should never get here due to failing sb verification, but
2989 * certain users (xfs_db) need to be usable even with corrupt metadata.
2990 */
2991 if (sbp->sb_imax_pct && igeo->ialloc_blks) {
3a05ab22
DW
2992 /*
2993 * Make sure the maximum inode count is a multiple
2994 * of the units we allocate inodes in.
2995 */
2996 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
2997 do_div(icount, 100);
2998 do_div(icount, igeo->ialloc_blks);
2999 igeo->maxicount = XFS_FSB_TO_INO(mp,
3000 icount * igeo->ialloc_blks);
3001 } else {
3002 igeo->maxicount = 0;
3003 }
3004
f8780726
DW
3005 /*
3006 * Compute the desired size of an inode cluster buffer size, which
3007 * starts at 8K and (on v5 filesystems) scales up with larger inode
3008 * sizes.
3009 *
3010 * Preserve the desired inode cluster size because the sparse inodes
3011 * feature uses that desired size (not the actual size) to compute the
3012 * sparse inode alignment. The mount code validates this value, so we
3013 * cannot change the behavior.
3014 */
3015 igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE;
94541a16 3016 if (xfs_has_v3inodes(mp)) {
f8780726 3017 int new_size = igeo->inode_cluster_size_raw;
3a05ab22
DW
3018
3019 new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
3020 if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
f8780726 3021 igeo->inode_cluster_size_raw = new_size;
3a05ab22
DW
3022 }
3023
3024 /* Calculate inode cluster ratios. */
f8780726 3025 if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize)
3a05ab22 3026 igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp,
f8780726 3027 igeo->inode_cluster_size_raw);
3a05ab22
DW
3028 else
3029 igeo->blocks_per_cluster = 1;
f8780726 3030 igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster);
3a05ab22
DW
3031 igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster);
3032
3033 /* Calculate inode cluster alignment. */
94541a16 3034 if (xfs_has_align(mp) &&
3a05ab22
DW
3035 mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster)
3036 igeo->cluster_align = mp->m_sb.sb_inoalignmt;
3037 else
3038 igeo->cluster_align = 1;
3039 igeo->inoalign_mask = igeo->cluster_align - 1;
3040 igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align);
3041
3042 /*
3043 * If we are using stripe alignment, check whether
3044 * the stripe unit is a multiple of the inode alignment
3045 */
3046 if (mp->m_dalign && igeo->inoalign_mask &&
3047 !(mp->m_dalign & igeo->inoalign_mask))
3048 igeo->ialloc_align = mp->m_dalign;
3049 else
3050 igeo->ialloc_align = 0;
8a044052
PR
3051
3052 if (mp->m_sb.sb_blocksize > PAGE_SIZE)
3053 igeo->min_folio_order = mp->m_sb.sb_blocklog - PAGE_SHIFT;
3054 else
3055 igeo->min_folio_order = 0;
3a05ab22 3056}
1b0819ed
DW
3057
3058/* Compute the location of the root directory inode that is laid out by mkfs. */
3059xfs_ino_t
3060xfs_ialloc_calc_rootino(
3061 struct xfs_mount *mp,
3062 int sunit)
3063{
3064 struct xfs_ino_geometry *igeo = M_IGEO(mp);
3065 xfs_agblock_t first_bno;
3066
3067 /*
3068 * Pre-calculate the geometry of AG 0. We know what it looks like
3069 * because libxfs knows how to create allocation groups now.
3070 *
3071 * first_bno is the first block in which mkfs could possibly have
3072 * allocated the root directory inode, once we factor in the metadata
3073 * that mkfs formats before it. Namely, the four AG headers...
3074 */
3075 first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize);
3076
3077 /* ...the two free space btree roots... */
3078 first_bno += 2;
3079
3080 /* ...the inode btree root... */
3081 first_bno += 1;
3082
3083 /* ...the initial AGFL... */
3084 first_bno += xfs_alloc_min_freelist(mp, NULL);
3085
3086 /* ...the free inode btree root... */
94541a16 3087 if (xfs_has_finobt(mp))
1b0819ed
DW
3088 first_bno++;
3089
3090 /* ...the reverse mapping btree root... */
94541a16 3091 if (xfs_has_rmapbt(mp))
1b0819ed
DW
3092 first_bno++;
3093
3094 /* ...the reference count btree... */
94541a16 3095 if (xfs_has_reflink(mp))
1b0819ed
DW
3096 first_bno++;
3097
3098 /*
3099 * ...and the log, if it is allocated in the first allocation group.
3100 *
3101 * This can happen with filesystems that only have a single
3102 * allocation group, or very odd geometries created by old mkfs
3103 * versions on very small filesystems.
3104 */
54f6b9e5 3105 if (xfs_ag_contains_log(mp, 0))
1b0819ed
DW
3106 first_bno += mp->m_sb.sb_logblocks;
3107
3108 /*
3109 * Now round first_bno up to whatever allocation alignment is given
3110 * by the filesystem or was passed in.
3111 */
94541a16 3112 if (xfs_has_dalign(mp) && igeo->ialloc_align > 0)
1b0819ed 3113 first_bno = roundup(first_bno, sunit);
94541a16 3114 else if (xfs_has_align(mp) &&
1b0819ed
DW
3115 mp->m_sb.sb_inoalignmt > 1)
3116 first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt);
3117
3118 return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno));
3119}
23435e3c
DW
3120
3121/*
3122 * Ensure there are not sparse inode clusters that cross the new EOAG.
3123 *
3124 * This is a no-op for non-spinode filesystems since clusters are always fully
3125 * allocated and checking the bnobt suffices. However, a spinode filesystem
3126 * could have a record where the upper inodes are free blocks. If those blocks
3127 * were removed from the filesystem, the inode record would extend beyond EOAG,
3128 * which will be flagged as corruption.
3129 */
3130int
3131xfs_ialloc_check_shrink(
90e549b5 3132 struct xfs_perag *pag,
23435e3c 3133 struct xfs_trans *tp,
23435e3c
DW
3134 struct xfs_buf *agibp,
3135 xfs_agblock_t new_length)
3136{
3137 struct xfs_inobt_rec_incore rec;
3138 struct xfs_btree_cur *cur;
87a02c9e 3139 xfs_agino_t agino;
23435e3c
DW
3140 int has;
3141 int error;
3142
6a271c73 3143 if (!xfs_has_sparseinodes(pag_mount(pag)))
23435e3c
DW
3144 return 0;
3145
7199f984 3146 cur = xfs_inobt_init_cursor(pag, tp, agibp);
23435e3c
DW
3147
3148 /* Look up the inobt record that would correspond to the new EOFS. */
6a271c73 3149 agino = XFS_AGB_TO_AGINO(pag_mount(pag), new_length);
23435e3c
DW
3150 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has);
3151 if (error || !has)
3152 goto out;
3153
3154 error = xfs_inobt_get_rec(cur, &rec, &has);
3155 if (error)
3156 goto out;
3157
3158 if (!has) {
178287f4 3159 xfs_ag_mark_sick(pag, XFS_SICK_AG_INOBT);
23435e3c
DW
3160 error = -EFSCORRUPTED;
3161 goto out;
3162 }
3163
3164 /* If the record covers inodes that would be beyond EOFS, bail out. */
3165 if (rec.ir_startino + XFS_INODES_PER_CHUNK > agino) {
3166 error = -ENOSPC;
3167 goto out;
3168 }
3169out:
3170 xfs_btree_del_cursor(cur, error);
23435e3c
DW
3171 return error;
3172}