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