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