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