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