1 /*
2  * Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 
33 #include "xfs.h"
34 
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
37 #include "xfs_inum.h"
38 #include "xfs_log.h"
39 #include "xfs_trans.h"
40 #include "xfs_sb.h"
41 #include "xfs_ag.h"
42 #include "xfs_dir.h"
43 #include "xfs_dir2.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
56 #include "xfs_alloc.h"
57 #include "xfs_bit.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_bmap.h"
61 
62 /*
63  * Log specified fields for the inode given by bp and off.
64  */
65 STATIC void
xfs_ialloc_log_di(xfs_trans_t * tp,xfs_buf_t * bp,int off,int fields)66 xfs_ialloc_log_di(
67 	xfs_trans_t	*tp,		/* transaction pointer */
68 	xfs_buf_t	*bp,		/* inode buffer */
69 	int		off,		/* index of inode in buffer */
70 	int		fields)		/* bitmask of fields to log */
71 {
72 	int			first;		/* first byte number */
73 	int			ioffset;	/* off in bytes */
74 	int			last;		/* last byte number */
75 	xfs_mount_t		*mp;		/* mount point structure */
76 	static const short	offsets[] = {	/* field offsets */
77 						/* keep in sync with bits */
78 		offsetof(xfs_dinode_core_t, di_magic),
79 		offsetof(xfs_dinode_core_t, di_mode),
80 		offsetof(xfs_dinode_core_t, di_version),
81 		offsetof(xfs_dinode_core_t, di_format),
82 		offsetof(xfs_dinode_core_t, di_onlink),
83 		offsetof(xfs_dinode_core_t, di_uid),
84 		offsetof(xfs_dinode_core_t, di_gid),
85 		offsetof(xfs_dinode_core_t, di_nlink),
86 		offsetof(xfs_dinode_core_t, di_projid),
87 		offsetof(xfs_dinode_core_t, di_pad),
88 		offsetof(xfs_dinode_core_t, di_atime),
89 		offsetof(xfs_dinode_core_t, di_mtime),
90 		offsetof(xfs_dinode_core_t, di_ctime),
91 		offsetof(xfs_dinode_core_t, di_size),
92 		offsetof(xfs_dinode_core_t, di_nblocks),
93 		offsetof(xfs_dinode_core_t, di_extsize),
94 		offsetof(xfs_dinode_core_t, di_nextents),
95 		offsetof(xfs_dinode_core_t, di_anextents),
96 		offsetof(xfs_dinode_core_t, di_forkoff),
97 		offsetof(xfs_dinode_core_t, di_aformat),
98 		offsetof(xfs_dinode_core_t, di_dmevmask),
99 		offsetof(xfs_dinode_core_t, di_dmstate),
100 		offsetof(xfs_dinode_core_t, di_flags),
101 		offsetof(xfs_dinode_core_t, di_gen),
102 		offsetof(xfs_dinode_t, di_next_unlinked),
103 		offsetof(xfs_dinode_t, di_u),
104 		offsetof(xfs_dinode_t, di_a),
105 		sizeof(xfs_dinode_t)
106 	};
107 
108 
109 	ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
110 	ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
111 	mp = tp->t_mountp;
112 	/*
113 	 * Get the inode-relative first and last bytes for these fields
114 	 */
115 	xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
116 	/*
117 	 * Convert to buffer offsets and log it.
118 	 */
119 	ioffset = off << mp->m_sb.sb_inodelog;
120 	first += ioffset;
121 	last += ioffset;
122 	xfs_trans_log_buf(tp, bp, first, last);
123 }
124 
125 /*
126  * Allocation group level functions.
127  */
128 
129 /*
130  * Allocate new inodes in the allocation group specified by agbp.
131  * Return 0 for success, else error code.
132  */
133 STATIC int				/* error code or 0 */
xfs_ialloc_ag_alloc(xfs_trans_t * tp,xfs_buf_t * agbp,int * alloc)134 xfs_ialloc_ag_alloc(
135 	xfs_trans_t	*tp,		/* transaction pointer */
136 	xfs_buf_t	*agbp,		/* alloc group buffer */
137 	int		*alloc)
138 {
139 	xfs_agi_t	*agi;		/* allocation group header */
140 	xfs_alloc_arg_t	args;		/* allocation argument structure */
141 	int		blks_per_cluster;  /* fs blocks per inode cluster */
142 	xfs_btree_cur_t	*cur;		/* inode btree cursor */
143 	xfs_daddr_t	d;		/* disk addr of buffer */
144 	int		error;
145 	xfs_buf_t	*fbuf;		/* new free inodes' buffer */
146 	xfs_dinode_t	*free;		/* new free inode structure */
147 	int		i;		/* inode counter */
148 	int		j;		/* block counter */
149 	int		nbufs;		/* num bufs of new inodes */
150 	xfs_agino_t	newino;		/* new first inode's number */
151 	xfs_agino_t	newlen;		/* new number of inodes */
152 	int		ninodes;	/* num inodes per buf */
153 	xfs_agino_t	thisino;	/* current inode number, for loop */
154 	int		version;	/* inode version number to use */
155 	int		isaligned;	/* inode allocation at stripe unit */
156 					/* boundary */
157 	xfs_dinode_core_t dic;          /* a dinode_core to copy to new */
158 					/* inodes */
159 
160 	args.tp = tp;
161 	args.mp = tp->t_mountp;
162 
163 	/*
164 	 * Locking will ensure that we don't have two callers in here
165 	 * at one time.
166 	 */
167 	newlen = XFS_IALLOC_INODES(args.mp);
168 	if (args.mp->m_maxicount &&
169 	    args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
170 		return XFS_ERROR(ENOSPC);
171 	args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
172 	/*
173 	 * Set the alignment for the allocation.
174 	 * If stripe alignment is turned on then align at stripe unit
175 	 * boundary.
176 	 * If the cluster size is smaller than a filesystem block
177 	 * then we're doing I/O for inodes in filesystem block size pieces,
178 	 * so don't need alignment anyway.
179 	 */
180 	isaligned = 0;
181 	if (args.mp->m_sinoalign) {
182 		ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
183 		args.alignment = args.mp->m_dalign;
184 		isaligned = 1;
185 	} else if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
186 	    args.mp->m_sb.sb_inoalignmt >=
187 	    XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
188 		args.alignment = args.mp->m_sb.sb_inoalignmt;
189 	else
190 		args.alignment = 1;
191 	agi = XFS_BUF_TO_AGI(agbp);
192 	/*
193 	 * Need to figure out where to allocate the inode blocks.
194 	 * Ideally they should be spaced out through the a.g.
195 	 * For now, just allocate blocks up front.
196 	 */
197 	args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
198 	args.fsbno = XFS_AGB_TO_FSB(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
199 				    args.agbno);
200 	/*
201 	 * Allocate a fixed-size extent of inodes.
202 	 */
203 	args.type = XFS_ALLOCTYPE_NEAR_BNO;
204 	args.mod = args.total = args.wasdel = args.isfl = args.userdata =
205 		args.minalignslop = 0;
206 	args.prod = 1;
207 	/*
208 	 * Allow space for the inode btree to split.
209 	 */
210 	args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
211 	if ((error = xfs_alloc_vextent(&args)))
212 		return error;
213 
214 	/*
215 	 * If stripe alignment is turned on, then try again with cluster
216 	 * alignment.
217 	 */
218 	if (isaligned && args.fsbno == NULLFSBLOCK) {
219 		args.type = XFS_ALLOCTYPE_NEAR_BNO;
220 		args.agbno = INT_GET(agi->agi_root, ARCH_CONVERT);
221 		args.fsbno = XFS_AGB_TO_FSB(args.mp,
222 				INT_GET(agi->agi_seqno, ARCH_CONVERT), args.agbno);
223 		if (XFS_SB_VERSION_HASALIGN(&args.mp->m_sb) &&
224 			args.mp->m_sb.sb_inoalignmt >=
225 			XFS_B_TO_FSBT(args.mp, XFS_INODE_CLUSTER_SIZE(args.mp)))
226 				args.alignment = args.mp->m_sb.sb_inoalignmt;
227 		else
228 			args.alignment = 1;
229 		if ((error = xfs_alloc_vextent(&args)))
230 			return error;
231 	}
232 
233 	if (args.fsbno == NULLFSBLOCK) {
234 		*alloc = 0;
235 		return 0;
236 	}
237 	ASSERT(args.len == args.minlen);
238 	/*
239 	 * Convert the results.
240 	 */
241 	newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
242 	/*
243 	 * Loop over the new block(s), filling in the inodes.
244 	 * For small block sizes, manipulate the inodes in buffers
245 	 * which are multiples of the blocks size.
246 	 */
247 	if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
248 		blks_per_cluster = 1;
249 		nbufs = (int)args.len;
250 		ninodes = args.mp->m_sb.sb_inopblock;
251 	} else {
252 		blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
253 				   args.mp->m_sb.sb_blocksize;
254 		nbufs = (int)args.len / blks_per_cluster;
255 		ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
256 	}
257 	/*
258 	 * Figure out what version number to use in the inodes we create.
259 	 * If the superblock version has caught up to the one that supports
260 	 * the new inode format, then use the new inode version.  Otherwise
261 	 * use the old version so that old kernels will continue to be
262 	 * able to use the file system.
263 	 */
264 	if (XFS_SB_VERSION_HASNLINK(&args.mp->m_sb))
265 		version = XFS_DINODE_VERSION_2;
266 	else
267 		version = XFS_DINODE_VERSION_1;
268 
269 	memset(&dic, 0, sizeof(xfs_dinode_core_t));
270 	INT_SET(dic.di_magic, ARCH_CONVERT, XFS_DINODE_MAGIC);
271 	INT_SET(dic.di_version, ARCH_CONVERT, version);
272 
273 	for (j = 0; j < nbufs; j++) {
274 		/*
275 		 * Get the block.
276 		 */
277 		d = XFS_AGB_TO_DADDR(args.mp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
278 				     args.agbno + (j * blks_per_cluster));
279 		fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
280 					 args.mp->m_bsize * blks_per_cluster,
281 					 XFS_BUF_LOCK);
282 		ASSERT(fbuf);
283 		ASSERT(!XFS_BUF_GETERROR(fbuf));
284 		/*
285 		 * Loop over the inodes in this buffer.
286 		 */
287 
288 		for (i = 0; i < ninodes; i++) {
289 			free = XFS_MAKE_IPTR(args.mp, fbuf, i);
290 			memcpy(&(free->di_core), &dic, sizeof(xfs_dinode_core_t));
291 			INT_SET(free->di_next_unlinked, ARCH_CONVERT, NULLAGINO);
292 			xfs_ialloc_log_di(tp, fbuf, i,
293 				XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
294 		}
295 		xfs_trans_inode_alloc_buf(tp, fbuf);
296 	}
297 	INT_MOD(agi->agi_count, ARCH_CONVERT, newlen);
298 	INT_MOD(agi->agi_freecount, ARCH_CONVERT, newlen);
299 	down_read(&args.mp->m_peraglock);
300 	args.mp->m_perag[INT_GET(agi->agi_seqno, ARCH_CONVERT)].pagi_freecount += newlen;
301 	up_read(&args.mp->m_peraglock);
302 	INT_SET(agi->agi_newino, ARCH_CONVERT, newino);
303 	/*
304 	 * Insert records describing the new inode chunk into the btree.
305 	 */
306 	cur = xfs_btree_init_cursor(args.mp, tp, agbp,
307 			INT_GET(agi->agi_seqno, ARCH_CONVERT),
308 			XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
309 	for (thisino = newino;
310 	     thisino < newino + newlen;
311 	     thisino += XFS_INODES_PER_CHUNK) {
312 		if ((error = xfs_inobt_lookup_eq(cur, thisino,
313 				XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
314 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
315 			return error;
316 		}
317 		ASSERT(i == 0);
318 		if ((error = xfs_inobt_insert(cur, &i))) {
319 			xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
320 			return error;
321 		}
322 		ASSERT(i == 1);
323 	}
324 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
325 	/*
326 	 * Log allocation group header fields
327 	 */
328 	xfs_ialloc_log_agi(tp, agbp,
329 		XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
330 	/*
331 	 * Modify/log superblock values for inode count and inode free count.
332 	 */
333 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
334 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
335 	*alloc = 1;
336 	return 0;
337 }
338 
339 STATIC __inline xfs_agnumber_t
xfs_ialloc_next_ag(xfs_mount_t * mp)340 xfs_ialloc_next_ag(
341 	xfs_mount_t	*mp)
342 {
343 	xfs_agnumber_t	agno;
344 
345 	spin_lock(&mp->m_agirotor_lock);
346 	agno = mp->m_agirotor;
347 	if (++mp->m_agirotor == mp->m_maxagi)
348 		mp->m_agirotor = 0;
349 	spin_unlock(&mp->m_agirotor_lock);
350 
351 	return agno;
352 }
353 
354 /*
355  * Select an allocation group to look for a free inode in, based on the parent
356  * inode and then mode.  Return the allocation group buffer.
357  */
358 STATIC xfs_buf_t *			/* allocation group buffer */
xfs_ialloc_ag_select(xfs_trans_t * tp,xfs_ino_t parent,mode_t mode,int okalloc)359 xfs_ialloc_ag_select(
360 	xfs_trans_t	*tp,		/* transaction pointer */
361 	xfs_ino_t	parent,		/* parent directory inode number */
362 	mode_t		mode,		/* bits set to indicate file type */
363 	int		okalloc)	/* ok to allocate more space */
364 {
365 	xfs_buf_t	*agbp;		/* allocation group header buffer */
366 	xfs_agnumber_t	agcount;	/* number of ag's in the filesystem */
367 	xfs_agnumber_t	agno;		/* current ag number */
368 	int		flags;		/* alloc buffer locking flags */
369 	xfs_extlen_t	ineed;		/* blocks needed for inode allocation */
370 	xfs_extlen_t	longest = 0;	/* longest extent available */
371 	xfs_mount_t	*mp;		/* mount point structure */
372 	int		needspace;	/* file mode implies space allocated */
373 	xfs_perag_t	*pag;		/* per allocation group data */
374 	xfs_agnumber_t	pagno;		/* parent (starting) ag number */
375 
376 	/*
377 	 * Files of these types need at least one block if length > 0
378 	 * (and they won't fit in the inode, but that's hard to figure out).
379 	 */
380 	needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
381 	mp = tp->t_mountp;
382 	agcount = mp->m_maxagi;
383 	if (S_ISDIR(mode))
384 		pagno = xfs_ialloc_next_ag(mp);
385 	else {
386 		pagno = XFS_INO_TO_AGNO(mp, parent);
387 		if (pagno >= agcount)
388 			pagno = 0;
389 	}
390 	ASSERT(pagno < agcount);
391 	/*
392 	 * Loop through allocation groups, looking for one with a little
393 	 * free space in it.  Note we don't look for free inodes, exactly.
394 	 * Instead, we include whether there is a need to allocate inodes
395 	 * to mean that blocks must be allocated for them,
396 	 * if none are currently free.
397 	 */
398 	agno = pagno;
399 	flags = XFS_ALLOC_FLAG_TRYLOCK;
400 	down_read(&mp->m_peraglock);
401 	for (;;) {
402 		pag = &mp->m_perag[agno];
403 		if (!pag->pagi_init) {
404 			if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
405 				agbp = NULL;
406 				goto nextag;
407 			}
408 		} else
409 			agbp = NULL;
410 
411 		if (!pag->pagi_inodeok) {
412 			xfs_ialloc_next_ag(mp);
413 			goto unlock_nextag;
414 		}
415 
416 		/*
417 		 * Is there enough free space for the file plus a block
418 		 * of inodes (if we need to allocate some)?
419 		 */
420 		ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
421 		if (ineed && !pag->pagf_init) {
422 			if (agbp == NULL &&
423 			    xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
424 				agbp = NULL;
425 				goto nextag;
426 			}
427 			(void)xfs_alloc_pagf_init(mp, tp, agno, flags);
428 		}
429 		if (!ineed || pag->pagf_init) {
430 			if (ineed && !(longest = pag->pagf_longest))
431 				longest = pag->pagf_flcount > 0;
432 			if (!ineed ||
433 			    (pag->pagf_freeblks >= needspace + ineed &&
434 			     longest >= ineed &&
435 			     okalloc)) {
436 				if (agbp == NULL &&
437 				    xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
438 					agbp = NULL;
439 					goto nextag;
440 				}
441 				up_read(&mp->m_peraglock);
442 				return agbp;
443 			}
444 		}
445 unlock_nextag:
446 		if (agbp)
447 			xfs_trans_brelse(tp, agbp);
448 nextag:
449 		/*
450 		 * No point in iterating over the rest, if we're shutting
451 		 * down.
452 		 */
453 		if (XFS_FORCED_SHUTDOWN(mp)) {
454 			up_read(&mp->m_peraglock);
455 			return (xfs_buf_t *)0;
456 		}
457 		agno++;
458 		if (agno >= agcount)
459 			agno = 0;
460 		if (agno == pagno) {
461 			if (flags == 0) {
462 				up_read(&mp->m_peraglock);
463 				return (xfs_buf_t *)0;
464 			}
465 			flags = 0;
466 		}
467 	}
468 }
469 
470 /*
471  * Visible inode allocation functions.
472  */
473 
474 /*
475  * Allocate an inode on disk.
476  * Mode is used to tell whether the new inode will need space, and whether
477  * it is a directory.
478  *
479  * The arguments IO_agbp and alloc_done are defined to work within
480  * the constraint of one allocation per transaction.
481  * xfs_dialloc() is designed to be called twice if it has to do an
482  * allocation to make more free inodes.  On the first call,
483  * IO_agbp should be set to NULL. If an inode is available,
484  * i.e., xfs_dialloc() did not need to do an allocation, an inode
485  * number is returned.  In this case, IO_agbp would be set to the
486  * current ag_buf and alloc_done set to false.
487  * If an allocation needed to be done, xfs_dialloc would return
488  * the current ag_buf in IO_agbp and set alloc_done to true.
489  * The caller should then commit the current transaction, allocate a new
490  * transaction, and call xfs_dialloc() again, passing in the previous
491  * value of IO_agbp.  IO_agbp should be held across the transactions.
492  * Since the agbp is locked across the two calls, the second call is
493  * guaranteed to have a free inode available.
494  *
495  * Once we successfully pick an inode its number is returned and the
496  * on-disk data structures are updated.  The inode itself is not read
497  * in, since doing so would break ordering constraints with xfs_reclaim.
498  */
499 int
xfs_dialloc(xfs_trans_t * tp,xfs_ino_t parent,mode_t mode,int okalloc,xfs_buf_t ** IO_agbp,boolean_t * alloc_done,xfs_ino_t * inop)500 xfs_dialloc(
501 	xfs_trans_t	*tp,		/* transaction pointer */
502 	xfs_ino_t	parent,		/* parent inode (directory) */
503 	mode_t		mode,		/* mode bits for new inode */
504 	int		okalloc,	/* ok to allocate more space */
505 	xfs_buf_t	**IO_agbp,	/* in/out ag header's buffer */
506 	boolean_t	*alloc_done,	/* true if we needed to replenish
507 					   inode freelist */
508 	xfs_ino_t	*inop)		/* inode number allocated */
509 {
510 	xfs_agnumber_t	agcount;	/* number of allocation groups */
511 	xfs_buf_t	*agbp;		/* allocation group header's buffer */
512 	xfs_agnumber_t	agno;		/* allocation group number */
513 	xfs_agi_t	*agi;		/* allocation group header structure */
514 	xfs_btree_cur_t	*cur;		/* inode allocation btree cursor */
515 	int		error;		/* error return value */
516 	int		i;		/* result code */
517 	int		ialloced;	/* inode allocation status */
518 	int		noroom = 0;	/* no space for inode blk allocation */
519 	xfs_ino_t	ino;		/* fs-relative inode to be returned */
520 	/* REFERENCED */
521 	int		j;		/* result code */
522 	xfs_mount_t	*mp;		/* file system mount structure */
523 	int		offset;		/* index of inode in chunk */
524 	xfs_agino_t	pagino;		/* parent's a.g. relative inode # */
525 	xfs_agnumber_t	pagno;		/* parent's allocation group number */
526 	xfs_inobt_rec_t	rec;		/* inode allocation record */
527 	xfs_agnumber_t	tagno;		/* testing allocation group number */
528 	xfs_btree_cur_t	*tcur;		/* temp cursor */
529 	xfs_inobt_rec_t	trec;		/* temp inode allocation record */
530 
531 
532 	if (*IO_agbp == NULL) {
533 		/*
534 		 * We do not have an agbp, so select an initial allocation
535 		 * group for inode allocation.
536 		 */
537 		agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
538 		/*
539 		 * Couldn't find an allocation group satisfying the
540 		 * criteria, give up.
541 		 */
542 		if (!agbp) {
543 			*inop = NULLFSINO;
544 			return 0;
545 		}
546 		agi = XFS_BUF_TO_AGI(agbp);
547 		ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
548 	} else {
549 		/*
550 		 * Continue where we left off before.  In this case, we
551 		 * know that the allocation group has free inodes.
552 		 */
553 		agbp = *IO_agbp;
554 		agi = XFS_BUF_TO_AGI(agbp);
555 		ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
556 		ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
557 	}
558 	mp = tp->t_mountp;
559 	agcount = mp->m_sb.sb_agcount;
560 	agno = INT_GET(agi->agi_seqno, ARCH_CONVERT);
561 	tagno = agno;
562 	pagno = XFS_INO_TO_AGNO(mp, parent);
563 	pagino = XFS_INO_TO_AGINO(mp, parent);
564 
565 	/*
566 	 * If we have already hit the ceiling of inode blocks then clear
567 	 * okalloc so we scan all available agi structures for a free
568 	 * inode.
569 	 */
570 
571 	if (mp->m_maxicount &&
572 	    mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
573 		noroom = 1;
574 		okalloc = 0;
575 	}
576 
577 	/*
578 	 * Loop until we find an allocation group that either has free inodes
579 	 * or in which we can allocate some inodes.  Iterate through the
580 	 * allocation groups upward, wrapping at the end.
581 	 */
582 	*alloc_done = B_FALSE;
583 	while (INT_ISZERO(agi->agi_freecount, ARCH_CONVERT)) {
584 		/*
585 		 * Don't do anything if we're not supposed to allocate
586 		 * any blocks, just go on to the next ag.
587 		 */
588 		if (okalloc) {
589 			/*
590 			 * Try to allocate some new inodes in the allocation
591 			 * group.
592 			 */
593 			if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
594 				xfs_trans_brelse(tp, agbp);
595 				if (error == ENOSPC) {
596 					*inop = NULLFSINO;
597 					return 0;
598 				} else
599 					return error;
600 			}
601 			if (ialloced) {
602 				/*
603 				 * We successfully allocated some inodes, return
604 				 * the current context to the caller so that it
605 				 * can commit the current transaction and call
606 				 * us again where we left off.
607 				 */
608 				ASSERT(INT_GET(agi->agi_freecount, ARCH_CONVERT) > 0);
609 				*alloc_done = B_TRUE;
610 				*IO_agbp = agbp;
611 				*inop = NULLFSINO;
612 				return 0;
613 			}
614 		}
615 		/*
616 		 * If it failed, give up on this ag.
617 		 */
618 		xfs_trans_brelse(tp, agbp);
619 		/*
620 		 * Go on to the next ag: get its ag header.
621 		 */
622 nextag:
623 		if (++tagno == agcount)
624 			tagno = 0;
625 		if (tagno == agno) {
626 			*inop = NULLFSINO;
627 			return noroom ? ENOSPC : 0;
628 		}
629 		down_read(&mp->m_peraglock);
630 		if (mp->m_perag[tagno].pagi_inodeok == 0) {
631 			up_read(&mp->m_peraglock);
632 			goto nextag;
633 		}
634 		error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
635 		up_read(&mp->m_peraglock);
636 		if (error)
637 			goto nextag;
638 		agi = XFS_BUF_TO_AGI(agbp);
639 		ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
640 	}
641 	/*
642 	 * Here with an allocation group that has a free inode.
643 	 * Reset agno since we may have chosen a new ag in the
644 	 * loop above.
645 	 */
646 	agno = tagno;
647 	*IO_agbp = NULL;
648 	cur = xfs_btree_init_cursor(mp, tp, agbp, INT_GET(agi->agi_seqno, ARCH_CONVERT),
649 				    XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
650 	/*
651 	 * If pagino is 0 (this is the root inode allocation) use newino.
652 	 * This must work because we've just allocated some.
653 	 */
654 	if (!pagino)
655 		pagino = INT_GET(agi->agi_newino, ARCH_CONVERT);
656 #ifdef DEBUG
657 	if (cur->bc_nlevels == 1) {
658 		int	freecount = 0;
659 
660 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
661 			goto error0;
662 		XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
663 		do {
664 			if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
665 					&rec.ir_freecount, &rec.ir_free, &i, ARCH_NOCONVERT)))
666 				goto error0;
667 			XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
668 			freecount += rec.ir_freecount;
669 			if ((error = xfs_inobt_increment(cur, 0, &i)))
670 				goto error0;
671 		} while (i == 1);
672 
673 		ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
674 		       XFS_FORCED_SHUTDOWN(mp));
675 	}
676 #endif
677 	/*
678 	 * If in the same a.g. as the parent, try to get near the parent.
679 	 */
680 	if (pagno == agno) {
681 		if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
682 			goto error0;
683 		if (i != 0 &&
684 		    (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
685 			    &rec.ir_freecount, &rec.ir_free, &j, ARCH_NOCONVERT)) == 0 &&
686 		    j == 1 &&
687 		    rec.ir_freecount > 0) {
688 			/*
689 			 * Found a free inode in the same chunk
690 			 * as parent, done.
691 			 */
692 		}
693 		/*
694 		 * In the same a.g. as parent, but parent's chunk is full.
695 		 */
696 		else {
697 			int	doneleft;	/* done, to the left */
698 			int	doneright;	/* done, to the right */
699 
700 			if (error)
701 				goto error0;
702 			ASSERT(i == 1);
703 			ASSERT(j == 1);
704 			/*
705 			 * Duplicate the cursor, search left & right
706 			 * simultaneously.
707 			 */
708 			if ((error = xfs_btree_dup_cursor(cur, &tcur)))
709 				goto error0;
710 			/*
711 			 * Search left with tcur, back up 1 record.
712 			 */
713 			if ((error = xfs_inobt_decrement(tcur, 0, &i)))
714 				goto error1;
715 			doneleft = !i;
716 			if (!doneleft) {
717 				if ((error = xfs_inobt_get_rec(tcur,
718 						&trec.ir_startino,
719 						&trec.ir_freecount,
720 						&trec.ir_free, &i, ARCH_NOCONVERT)))
721 					goto error1;
722 				XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
723 			}
724 			/*
725 			 * Search right with cur, go forward 1 record.
726 			 */
727 			if ((error = xfs_inobt_increment(cur, 0, &i)))
728 				goto error1;
729 			doneright = !i;
730 			if (!doneright) {
731 				if ((error = xfs_inobt_get_rec(cur,
732 						&rec.ir_startino,
733 						&rec.ir_freecount,
734 						&rec.ir_free, &i, ARCH_NOCONVERT)))
735 					goto error1;
736 				XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
737 			}
738 			/*
739 			 * Loop until we find the closest inode chunk
740 			 * with a free one.
741 			 */
742 			while (!doneleft || !doneright) {
743 				int	useleft;  /* using left inode
744 						     chunk this time */
745 
746 				/*
747 				 * Figure out which block is closer,
748 				 * if both are valid.
749 				 */
750 				if (!doneleft && !doneright)
751 					useleft =
752 						pagino -
753 						(trec.ir_startino +
754 						 XFS_INODES_PER_CHUNK - 1) <
755 						 rec.ir_startino - pagino;
756 				else
757 					useleft = !doneleft;
758 				/*
759 				 * If checking the left, does it have
760 				 * free inodes?
761 				 */
762 				if (useleft && trec.ir_freecount) {
763 					/*
764 					 * Yes, set it up as the chunk to use.
765 					 */
766 					rec = trec;
767 					xfs_btree_del_cursor(cur,
768 						XFS_BTREE_NOERROR);
769 					cur = tcur;
770 					break;
771 				}
772 				/*
773 				 * If checking the right, does it have
774 				 * free inodes?
775 				 */
776 				if (!useleft && rec.ir_freecount) {
777 					/*
778 					 * Yes, it's already set up.
779 					 */
780 					xfs_btree_del_cursor(tcur,
781 						XFS_BTREE_NOERROR);
782 					break;
783 				}
784 				/*
785 				 * If used the left, get another one
786 				 * further left.
787 				 */
788 				if (useleft) {
789 					if ((error = xfs_inobt_decrement(tcur, 0,
790 							&i)))
791 						goto error1;
792 					doneleft = !i;
793 					if (!doneleft) {
794 						if ((error = xfs_inobt_get_rec(
795 							    tcur,
796 							    &trec.ir_startino,
797 							    &trec.ir_freecount,
798 							    &trec.ir_free, &i, ARCH_NOCONVERT)))
799 							goto error1;
800 						XFS_WANT_CORRUPTED_GOTO(i == 1,
801 							error1);
802 					}
803 				}
804 				/*
805 				 * If used the right, get another one
806 				 * further right.
807 				 */
808 				else {
809 					if ((error = xfs_inobt_increment(cur, 0,
810 							&i)))
811 						goto error1;
812 					doneright = !i;
813 					if (!doneright) {
814 						if ((error = xfs_inobt_get_rec(
815 							    cur,
816 							    &rec.ir_startino,
817 							    &rec.ir_freecount,
818 							    &rec.ir_free, &i, ARCH_NOCONVERT)))
819 							goto error1;
820 						XFS_WANT_CORRUPTED_GOTO(i == 1,
821 							error1);
822 					}
823 				}
824 			}
825 			ASSERT(!doneleft || !doneright);
826 		}
827 	}
828 	/*
829 	 * In a different a.g. from the parent.
830 	 * See if the most recently allocated block has any free.
831 	 */
832 	else if (INT_GET(agi->agi_newino, ARCH_CONVERT) != NULLAGINO) {
833 		if ((error = xfs_inobt_lookup_eq(cur,
834 				INT_GET(agi->agi_newino, ARCH_CONVERT), 0, 0, &i)))
835 			goto error0;
836 		if (i == 1 &&
837 		    (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
838 			    &rec.ir_freecount, &rec.ir_free, &j, ARCH_NOCONVERT)) == 0 &&
839 		    j == 1 &&
840 		    rec.ir_freecount > 0) {
841 			/*
842 			 * The last chunk allocated in the group still has
843 			 * a free inode.
844 			 */
845 		}
846 		/*
847 		 * None left in the last group, search the whole a.g.
848 		 */
849 		else {
850 			if (error)
851 				goto error0;
852 			if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
853 				goto error0;
854 			ASSERT(i == 1);
855 			for (;;) {
856 				if ((error = xfs_inobt_get_rec(cur,
857 						&rec.ir_startino,
858 						&rec.ir_freecount, &rec.ir_free,
859 						&i, ARCH_NOCONVERT)))
860 					goto error0;
861 				XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
862 				if (rec.ir_freecount > 0)
863 					break;
864 				if ((error = xfs_inobt_increment(cur, 0, &i)))
865 					goto error0;
866 				XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
867 			}
868 		}
869 	}
870 	offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
871 	ASSERT(offset >= 0);
872 	ASSERT(offset < XFS_INODES_PER_CHUNK);
873 	ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
874 				   XFS_INODES_PER_CHUNK) == 0);
875 	ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
876 	XFS_INOBT_CLR_FREE(&rec, offset, ARCH_NOCONVERT);
877 	rec.ir_freecount--;
878 	if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
879 			rec.ir_free)))
880 		goto error0;
881 	INT_MOD(agi->agi_freecount, ARCH_CONVERT, -1);
882 	xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
883 	down_read(&mp->m_peraglock);
884 	mp->m_perag[tagno].pagi_freecount--;
885 	up_read(&mp->m_peraglock);
886 #ifdef DEBUG
887 	if (cur->bc_nlevels == 1) {
888 		int	freecount = 0;
889 
890 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
891 			goto error0;
892 		do {
893 			if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
894 					&rec.ir_freecount, &rec.ir_free, &i, ARCH_NOCONVERT)))
895 				goto error0;
896 			XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
897 			freecount += rec.ir_freecount;
898 			if ((error = xfs_inobt_increment(cur, 0, &i)))
899 				goto error0;
900 		} while (i == 1);
901 		ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
902 		       XFS_FORCED_SHUTDOWN(mp));
903 	}
904 #endif
905 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
906 	xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
907 	*inop = ino;
908 	return 0;
909 error1:
910 	xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
911 error0:
912 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
913 	return error;
914 }
915 
916 /*
917  * Free disk inode.  Carefully avoids touching the incore inode, all
918  * manipulations incore are the caller's responsibility.
919  * The on-disk inode is not changed by this operation, only the
920  * btree (free inode mask) is changed.
921  */
922 int
xfs_difree(xfs_trans_t * tp,xfs_ino_t inode,xfs_bmap_free_t * flist,int * delete,xfs_ino_t * first_ino)923 xfs_difree(
924 	xfs_trans_t	*tp,		/* transaction pointer */
925 	xfs_ino_t	inode,		/* inode to be freed */
926 	xfs_bmap_free_t	*flist,		/* extents to free */
927 	int		*delete,	/* set if inode cluster was deleted */
928 	xfs_ino_t	*first_ino)	/* first inode in deleted cluster */
929 {
930 	/* REFERENCED */
931 	xfs_agblock_t	agbno;	/* block number containing inode */
932 	xfs_buf_t	*agbp;	/* buffer containing allocation group header */
933 	xfs_agino_t	agino;	/* inode number relative to allocation group */
934 	xfs_agnumber_t	agno;	/* allocation group number */
935 	xfs_agi_t	*agi;	/* allocation group header */
936 	xfs_btree_cur_t	*cur;	/* inode btree cursor */
937 	int		error;	/* error return value */
938 	int		i;	/* result code */
939 	int		ilen;	/* inodes in an inode cluster */
940 	xfs_mount_t	*mp;	/* mount structure for filesystem */
941 	int		off;	/* offset of inode in inode chunk */
942 	xfs_inobt_rec_t	rec;	/* btree record */
943 
944 	mp = tp->t_mountp;
945 
946 	/*
947 	 * Break up inode number into its components.
948 	 */
949 	agno = XFS_INO_TO_AGNO(mp, inode);
950 	if (agno >= mp->m_sb.sb_agcount)  {
951 		cmn_err(CE_WARN,
952 			"xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s.  Returning EINVAL.",
953 			agno, mp->m_sb.sb_agcount, mp->m_fsname);
954 		ASSERT(0);
955 		return XFS_ERROR(EINVAL);
956 	}
957 	agino = XFS_INO_TO_AGINO(mp, inode);
958 	if (inode != XFS_AGINO_TO_INO(mp, agno, agino))  {
959 		cmn_err(CE_WARN,
960 			"xfs_difree: inode != XFS_AGINO_TO_INO() (%d != %d) on %s.  Returning EINVAL.",
961 			inode, XFS_AGINO_TO_INO(mp, agno, agino), mp->m_fsname);
962 		ASSERT(0);
963 		return XFS_ERROR(EINVAL);
964 	}
965 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
966 	if (agbno >= mp->m_sb.sb_agblocks)  {
967 		cmn_err(CE_WARN,
968 			"xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s.  Returning EINVAL.",
969 			agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
970 		ASSERT(0);
971 		return XFS_ERROR(EINVAL);
972 	}
973 	/*
974 	 * Get the allocation group header.
975 	 */
976 	down_read(&mp->m_peraglock);
977 	error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
978 	up_read(&mp->m_peraglock);
979 	if (error) {
980 		cmn_err(CE_WARN,
981 			"xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s.  Returning error.",
982 			error, mp->m_fsname);
983 		return error;
984 	}
985 	agi = XFS_BUF_TO_AGI(agbp);
986 	ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
987 	ASSERT(agbno < INT_GET(agi->agi_length, ARCH_CONVERT));
988 	/*
989 	 * Initialize the cursor.
990 	 */
991 	cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
992 		(xfs_inode_t *)0, 0);
993 #ifdef DEBUG
994 	if (cur->bc_nlevels == 1) {
995 		int freecount = 0;
996 
997 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
998 			goto error0;
999 		do {
1000 			if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1001 					&rec.ir_freecount, &rec.ir_free, &i, ARCH_NOCONVERT)))
1002 				goto error0;
1003 			if (i) {
1004 				freecount += rec.ir_freecount;
1005 				if ((error = xfs_inobt_increment(cur, 0, &i)))
1006 					goto error0;
1007 			}
1008 		} while (i == 1);
1009 		ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1010 		       XFS_FORCED_SHUTDOWN(mp));
1011 	}
1012 #endif
1013 	/*
1014 	 * Look for the entry describing this inode.
1015 	 */
1016 	if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1017 		cmn_err(CE_WARN,
1018 			"xfs_difree: xfs_inobt_lookup_le returned()  an error %d on %s.  Returning error.",
1019 			error, mp->m_fsname);
1020 		goto error0;
1021 	}
1022 	XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1023 	if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1024 			&rec.ir_free, &i, ARCH_NOCONVERT))) {
1025 		cmn_err(CE_WARN,
1026 			"xfs_difree: xfs_inobt_get_rec()  returned an error %d on %s.  Returning error.",
1027 			error, mp->m_fsname);
1028 		goto error0;
1029 	}
1030 	XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1031 	/*
1032 	 * Get the offset in the inode chunk.
1033 	 */
1034 	off = agino - rec.ir_startino;
1035 	ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1036 	ASSERT(!XFS_INOBT_IS_FREE(&rec, off, ARCH_NOCONVERT));
1037 	/*
1038 	 * Mark the inode free & increment the count.
1039 	 */
1040 	XFS_INOBT_SET_FREE(&rec, off, ARCH_NOCONVERT);
1041 	rec.ir_freecount++;
1042 
1043 	/*
1044 	 * When an inode cluster is free, it becomes elgible for removal
1045 	 */
1046 	if ((mp->m_flags & XFS_MOUNT_IDELETE) &&
1047 	    (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1048 
1049 		*delete = 1;
1050 		*first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1051 
1052 		/*
1053 		 * Remove the inode cluster from the AGI B+Tree, adjust the
1054 		 * AGI and Superblock inode counts, and mark the disk space
1055 		 * to be freed when the transaction is committed.
1056 		 */
1057 		ilen = XFS_IALLOC_INODES(mp);
1058 		INT_MOD(agi->agi_count, ARCH_CONVERT, -ilen);
1059 		INT_MOD(agi->agi_freecount, ARCH_CONVERT, -(ilen - 1));
1060 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1061 		down_read(&mp->m_peraglock);
1062 		mp->m_perag[agno].pagi_freecount -= ilen - 1;
1063 		up_read(&mp->m_peraglock);
1064 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1065 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1066 
1067 		if ((error = xfs_inobt_delete(cur, &i))) {
1068 			cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1069 				error, mp->m_fsname);
1070 			goto error0;
1071 		}
1072 
1073 		xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1074 				agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1075 				XFS_IALLOC_BLOCKS(mp), flist, mp);
1076 	} else {
1077 		*delete = 0;
1078 
1079 		if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1080 			cmn_err(CE_WARN,
1081 				"xfs_difree: xfs_inobt_update()  returned an error %d on %s.  Returning error.",
1082 				error, mp->m_fsname);
1083 			goto error0;
1084 		}
1085 		/*
1086 		 * Change the inode free counts and log the ag/sb changes.
1087 		 */
1088 		INT_MOD(agi->agi_freecount, ARCH_CONVERT, 1);
1089 		xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1090 		down_read(&mp->m_peraglock);
1091 		mp->m_perag[agno].pagi_freecount++;
1092 		up_read(&mp->m_peraglock);
1093 		xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1094 	}
1095 
1096 #ifdef DEBUG
1097 	if (cur->bc_nlevels == 1) {
1098 		int freecount = 0;
1099 
1100 		if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1101 			goto error0;
1102 		do {
1103 			if ((error = xfs_inobt_get_rec(cur,
1104 					&rec.ir_startino,
1105 					&rec.ir_freecount,
1106 					&rec.ir_free, &i,
1107 					ARCH_NOCONVERT)))
1108 				goto error0;
1109 			if (i) {
1110 				freecount += rec.ir_freecount;
1111 				if ((error = xfs_inobt_increment(cur, 0, &i)))
1112 					goto error0;
1113 			}
1114 		} while (i == 1);
1115 		ASSERT(freecount == INT_GET(agi->agi_freecount, ARCH_CONVERT) ||
1116 		       XFS_FORCED_SHUTDOWN(mp));
1117 	}
1118 #endif
1119 	xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1120 	return 0;
1121 
1122 error0:
1123 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1124 	return error;
1125 }
1126 
1127 /*
1128  * Return the location of the inode in bno/off, for mapping it into a buffer.
1129  */
1130 /*ARGSUSED*/
1131 int
xfs_dilocate(xfs_mount_t * mp,xfs_trans_t * tp,xfs_ino_t ino,xfs_fsblock_t * bno,int * len,int * off,uint flags)1132 xfs_dilocate(
1133 	xfs_mount_t	*mp,	/* file system mount structure */
1134 	xfs_trans_t	*tp,	/* transaction pointer */
1135 	xfs_ino_t	ino,	/* inode to locate */
1136 	xfs_fsblock_t	*bno,	/* output: block containing inode */
1137 	int		*len,	/* output: num blocks in inode cluster */
1138 	int		*off,	/* output: index in block of inode */
1139 	uint		flags)	/* flags concerning inode lookup */
1140 {
1141 	xfs_agblock_t	agbno;	/* block number of inode in the alloc group */
1142 	xfs_buf_t	*agbp;	/* agi buffer */
1143 	xfs_agino_t	agino;	/* inode number within alloc group */
1144 	xfs_agnumber_t	agno;	/* allocation group number */
1145 	int		blks_per_cluster; /* num blocks per inode cluster */
1146 	xfs_agblock_t	chunk_agbno;	/* first block in inode chunk */
1147 	xfs_agino_t	chunk_agino;	/* first agino in inode chunk */
1148 	__int32_t	chunk_cnt;	/* count of free inodes in chunk */
1149 	xfs_inofree_t	chunk_free;	/* mask of free inodes in chunk */
1150 	xfs_agblock_t	cluster_agbno;	/* first block in inode cluster */
1151 	xfs_btree_cur_t	*cur;	/* inode btree cursor */
1152 	int		error;	/* error code */
1153 	int		i;	/* temp state */
1154 	int		offset;	/* index of inode in its buffer */
1155 	int		offset_agbno;	/* blks from chunk start to inode */
1156 
1157 	ASSERT(ino != NULLFSINO);
1158 	/*
1159 	 * Split up the inode number into its parts.
1160 	 */
1161 	agno = XFS_INO_TO_AGNO(mp, ino);
1162 	agino = XFS_INO_TO_AGINO(mp, ino);
1163 	agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1164 	if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1165 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1166 #ifdef DEBUG
1167 		if (agno >= mp->m_sb.sb_agcount) {
1168 			xfs_fs_cmn_err(CE_ALERT, mp,
1169 					"xfs_dilocate: agno (%d) >= "
1170 					"mp->m_sb.sb_agcount (%d)",
1171 					agno,  mp->m_sb.sb_agcount);
1172 		}
1173 		if (agbno >= mp->m_sb.sb_agblocks) {
1174 			xfs_fs_cmn_err(CE_ALERT, mp,
1175 					"xfs_dilocate: agbno (0x%llx) >= "
1176 					"mp->m_sb.sb_agblocks (0x%lx)",
1177 					(unsigned long long) agbno,
1178 					(unsigned long) mp->m_sb.sb_agblocks);
1179 		}
1180 		if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1181 			xfs_fs_cmn_err(CE_ALERT, mp,
1182 					"xfs_dilocate: ino (0x%llx) != "
1183 					"XFS_AGINO_TO_INO(mp, agno, agino) "
1184 					"(0x%llx)",
1185 					ino, XFS_AGINO_TO_INO(mp, agno, agino));
1186 		}
1187 #endif /* DEBUG */
1188 		return XFS_ERROR(EINVAL);
1189 	}
1190 	if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1191 	    !(flags & XFS_IMAP_LOOKUP)) {
1192 		offset = XFS_INO_TO_OFFSET(mp, ino);
1193 		ASSERT(offset < mp->m_sb.sb_inopblock);
1194 		*bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1195 		*off = offset;
1196 		*len = 1;
1197 		return 0;
1198 	}
1199 	blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1200 	if (*bno != NULLFSBLOCK) {
1201 		offset = XFS_INO_TO_OFFSET(mp, ino);
1202 		ASSERT(offset < mp->m_sb.sb_inopblock);
1203 		cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1204 		*off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1205 			offset;
1206 		*len = blks_per_cluster;
1207 		return 0;
1208 	}
1209 	if (mp->m_inoalign_mask) {
1210 		offset_agbno = agbno & mp->m_inoalign_mask;
1211 		chunk_agbno = agbno - offset_agbno;
1212 	} else {
1213 		down_read(&mp->m_peraglock);
1214 		error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1215 		up_read(&mp->m_peraglock);
1216 		if (error) {
1217 #ifdef DEBUG
1218 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1219 					"xfs_ialloc_read_agi() returned "
1220 					"error %d, agno %d",
1221 					error, agno);
1222 #endif /* DEBUG */
1223 			return error;
1224 		}
1225 		cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1226 			(xfs_inode_t *)0, 0);
1227 		if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1228 #ifdef DEBUG
1229 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1230 					"xfs_inobt_lookup_le() failed");
1231 #endif /* DEBUG */
1232 			goto error0;
1233 		}
1234 		if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1235 				&chunk_free, &i, ARCH_NOCONVERT))) {
1236 #ifdef DEBUG
1237 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1238 					"xfs_inobt_get_rec() failed");
1239 #endif /* DEBUG */
1240 			goto error0;
1241 		}
1242 		if (i == 0) {
1243 #ifdef DEBUG
1244 			xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1245 					"xfs_inobt_get_rec() failed");
1246 #endif /* DEBUG */
1247 			error = XFS_ERROR(EINVAL);
1248 		}
1249 		xfs_trans_brelse(tp, agbp);
1250 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1251 		if (error)
1252 			return error;
1253 		chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1254 		offset_agbno = agbno - chunk_agbno;
1255 	}
1256 	ASSERT(agbno >= chunk_agbno);
1257 	cluster_agbno = chunk_agbno +
1258 		((offset_agbno / blks_per_cluster) * blks_per_cluster);
1259 	offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1260 		XFS_INO_TO_OFFSET(mp, ino);
1261 	*bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1262 	*off = offset;
1263 	*len = blks_per_cluster;
1264 	return 0;
1265 error0:
1266 	xfs_trans_brelse(tp, agbp);
1267 	xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1268 	return error;
1269 }
1270 
1271 /*
1272  * Compute and fill in value of m_in_maxlevels.
1273  */
1274 void
xfs_ialloc_compute_maxlevels(xfs_mount_t * mp)1275 xfs_ialloc_compute_maxlevels(
1276 	xfs_mount_t	*mp)		/* file system mount structure */
1277 {
1278 	int		level;
1279 	uint		maxblocks;
1280 	uint		maxleafents;
1281 	int		minleafrecs;
1282 	int		minnoderecs;
1283 
1284 	maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1285 		XFS_INODES_PER_CHUNK_LOG;
1286 	minleafrecs = mp->m_alloc_mnr[0];
1287 	minnoderecs = mp->m_alloc_mnr[1];
1288 	maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1289 	for (level = 1; maxblocks > 1; level++)
1290 		maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1291 	mp->m_in_maxlevels = level;
1292 }
1293 
1294 /*
1295  * Log specified fields for the ag hdr (inode section)
1296  */
1297 void
xfs_ialloc_log_agi(xfs_trans_t * tp,xfs_buf_t * bp,int fields)1298 xfs_ialloc_log_agi(
1299 	xfs_trans_t	*tp,		/* transaction pointer */
1300 	xfs_buf_t	*bp,		/* allocation group header buffer */
1301 	int		fields)		/* bitmask of fields to log */
1302 {
1303 	int			first;		/* first byte number */
1304 	int			last;		/* last byte number */
1305 	static const short	offsets[] = {	/* field starting offsets */
1306 					/* keep in sync with bit definitions */
1307 		offsetof(xfs_agi_t, agi_magicnum),
1308 		offsetof(xfs_agi_t, agi_versionnum),
1309 		offsetof(xfs_agi_t, agi_seqno),
1310 		offsetof(xfs_agi_t, agi_length),
1311 		offsetof(xfs_agi_t, agi_count),
1312 		offsetof(xfs_agi_t, agi_root),
1313 		offsetof(xfs_agi_t, agi_level),
1314 		offsetof(xfs_agi_t, agi_freecount),
1315 		offsetof(xfs_agi_t, agi_newino),
1316 		offsetof(xfs_agi_t, agi_dirino),
1317 		offsetof(xfs_agi_t, agi_unlinked),
1318 		sizeof(xfs_agi_t)
1319 	};
1320 #ifdef DEBUG
1321 	xfs_agi_t		*agi;	/* allocation group header */
1322 
1323 	agi = XFS_BUF_TO_AGI(bp);
1324 	ASSERT(INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC);
1325 #endif
1326 	/*
1327 	 * Compute byte offsets for the first and last fields.
1328 	 */
1329 	xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1330 	/*
1331 	 * Log the allocation group inode header buffer.
1332 	 */
1333 	xfs_trans_log_buf(tp, bp, first, last);
1334 }
1335 
1336 /*
1337  * Read in the allocation group header (inode allocation section)
1338  */
1339 int
xfs_ialloc_read_agi(xfs_mount_t * mp,xfs_trans_t * tp,xfs_agnumber_t agno,xfs_buf_t ** bpp)1340 xfs_ialloc_read_agi(
1341 	xfs_mount_t	*mp,		/* file system mount structure */
1342 	xfs_trans_t	*tp,		/* transaction pointer */
1343 	xfs_agnumber_t	agno,		/* allocation group number */
1344 	xfs_buf_t	**bpp)		/* allocation group hdr buf */
1345 {
1346 	xfs_agi_t	*agi;		/* allocation group header */
1347 	int		agi_ok;		/* agi is consistent */
1348 	xfs_buf_t	*bp;		/* allocation group hdr buf */
1349 	xfs_perag_t	*pag;		/* per allocation group data */
1350 	int		error;
1351 
1352 	ASSERT(agno != NULLAGNUMBER);
1353 	error = xfs_trans_read_buf(
1354 			mp, tp, mp->m_ddev_targp,
1355 			XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1356 			XFS_FSS_TO_BB(mp, 1), 0, &bp);
1357 	if (error)
1358 		return error;
1359 	ASSERT(bp && !XFS_BUF_GETERROR(bp));
1360 
1361 	/*
1362 	 * Validate the magic number of the agi block.
1363 	 */
1364 	agi = XFS_BUF_TO_AGI(bp);
1365 	agi_ok =
1366 		INT_GET(agi->agi_magicnum, ARCH_CONVERT) == XFS_AGI_MAGIC &&
1367 		XFS_AGI_GOOD_VERSION(
1368 			INT_GET(agi->agi_versionnum, ARCH_CONVERT));
1369 	if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1370 			XFS_RANDOM_IALLOC_READ_AGI))) {
1371 		XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1372 				     mp, agi);
1373 		xfs_trans_brelse(tp, bp);
1374 		return XFS_ERROR(EFSCORRUPTED);
1375 	}
1376 	pag = &mp->m_perag[agno];
1377 	if (!pag->pagi_init) {
1378 		pag->pagi_freecount = INT_GET(agi->agi_freecount, ARCH_CONVERT);
1379 		pag->pagi_init = 1;
1380 	} else {
1381 		/*
1382 		 * It's possible for these to be out of sync if
1383 		 * we are in the middle of a forced shutdown.
1384 		 */
1385 		ASSERT(pag->pagi_freecount ==
1386 				INT_GET(agi->agi_freecount, ARCH_CONVERT)
1387 			|| XFS_FORCED_SHUTDOWN(mp));
1388 	}
1389 
1390 #ifdef DEBUG
1391 	{
1392 		int	i;
1393 
1394 		for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1395 			ASSERT(!INT_ISZERO(agi->agi_unlinked[i], ARCH_CONVERT));
1396 	}
1397 #endif
1398 
1399 	XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1400 	*bpp = bp;
1401 	return 0;
1402 }
1403