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