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