xref: /linux/fs/xfs/xfs_itable.c (revision b8bb76713ec50df2f11efee386e16f93d51e1076)
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_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_itable.h"
39 #include "xfs_error.h"
40 #include "xfs_btree.h"
41 
42 int
43 xfs_internal_inum(
44 	xfs_mount_t	*mp,
45 	xfs_ino_t	ino)
46 {
47 	return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
48 		(xfs_sb_version_hasquota(&mp->m_sb) &&
49 		 (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino)));
50 }
51 
52 STATIC int
53 xfs_bulkstat_one_iget(
54 	xfs_mount_t	*mp,		/* mount point for filesystem */
55 	xfs_ino_t	ino,		/* inode number to get data for */
56 	xfs_daddr_t	bno,		/* starting bno of inode cluster */
57 	xfs_bstat_t	*buf,		/* return buffer */
58 	int		*stat)		/* BULKSTAT_RV_... */
59 {
60 	xfs_icdinode_t	*dic;	/* dinode core info pointer */
61 	xfs_inode_t	*ip;		/* incore inode pointer */
62 	int		error;
63 
64 	error = xfs_iget(mp, NULL, ino,
65 			 XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno);
66 	if (error) {
67 		*stat = BULKSTAT_RV_NOTHING;
68 		return error;
69 	}
70 
71 	ASSERT(ip != NULL);
72 	ASSERT(ip->i_imap.im_blkno != 0);
73 
74 	dic = &ip->i_d;
75 
76 	/* xfs_iget returns the following without needing
77 	 * further change.
78 	 */
79 	buf->bs_nlink = dic->di_nlink;
80 	buf->bs_projid = dic->di_projid;
81 	buf->bs_ino = ino;
82 	buf->bs_mode = dic->di_mode;
83 	buf->bs_uid = dic->di_uid;
84 	buf->bs_gid = dic->di_gid;
85 	buf->bs_size = dic->di_size;
86 	vn_atime_to_bstime(VFS_I(ip), &buf->bs_atime);
87 	buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
88 	buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
89 	buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
90 	buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
91 	buf->bs_xflags = xfs_ip2xflags(ip);
92 	buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
93 	buf->bs_extents = dic->di_nextents;
94 	buf->bs_gen = dic->di_gen;
95 	memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
96 	buf->bs_dmevmask = dic->di_dmevmask;
97 	buf->bs_dmstate = dic->di_dmstate;
98 	buf->bs_aextents = dic->di_anextents;
99 
100 	switch (dic->di_format) {
101 	case XFS_DINODE_FMT_DEV:
102 		buf->bs_rdev = ip->i_df.if_u2.if_rdev;
103 		buf->bs_blksize = BLKDEV_IOSIZE;
104 		buf->bs_blocks = 0;
105 		break;
106 	case XFS_DINODE_FMT_LOCAL:
107 	case XFS_DINODE_FMT_UUID:
108 		buf->bs_rdev = 0;
109 		buf->bs_blksize = mp->m_sb.sb_blocksize;
110 		buf->bs_blocks = 0;
111 		break;
112 	case XFS_DINODE_FMT_EXTENTS:
113 	case XFS_DINODE_FMT_BTREE:
114 		buf->bs_rdev = 0;
115 		buf->bs_blksize = mp->m_sb.sb_blocksize;
116 		buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
117 		break;
118 	}
119 
120 	xfs_iput(ip, XFS_ILOCK_SHARED);
121 	return error;
122 }
123 
124 STATIC void
125 xfs_bulkstat_one_dinode(
126 	xfs_mount_t	*mp,		/* mount point for filesystem */
127 	xfs_ino_t	ino,		/* inode number to get data for */
128 	xfs_dinode_t	*dic,		/* dinode inode pointer */
129 	xfs_bstat_t	*buf)		/* return buffer */
130 {
131 	/*
132 	 * The inode format changed when we moved the link count and
133 	 * made it 32 bits long.  If this is an old format inode,
134 	 * convert it in memory to look like a new one.  If it gets
135 	 * flushed to disk we will convert back before flushing or
136 	 * logging it.  We zero out the new projid field and the old link
137 	 * count field.  We'll handle clearing the pad field (the remains
138 	 * of the old uuid field) when we actually convert the inode to
139 	 * the new format. We don't change the version number so that we
140 	 * can distinguish this from a real new format inode.
141 	 */
142 	if (dic->di_version == 1) {
143 		buf->bs_nlink = be16_to_cpu(dic->di_onlink);
144 		buf->bs_projid = 0;
145 	} else {
146 		buf->bs_nlink = be32_to_cpu(dic->di_nlink);
147 		buf->bs_projid = be16_to_cpu(dic->di_projid);
148 	}
149 
150 	buf->bs_ino = ino;
151 	buf->bs_mode = be16_to_cpu(dic->di_mode);
152 	buf->bs_uid = be32_to_cpu(dic->di_uid);
153 	buf->bs_gid = be32_to_cpu(dic->di_gid);
154 	buf->bs_size = be64_to_cpu(dic->di_size);
155 	buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec);
156 	buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec);
157 	buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec);
158 	buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec);
159 	buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec);
160 	buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec);
161 	buf->bs_xflags = xfs_dic2xflags(dic);
162 	buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog;
163 	buf->bs_extents = be32_to_cpu(dic->di_nextents);
164 	buf->bs_gen = be32_to_cpu(dic->di_gen);
165 	memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
166 	buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask);
167 	buf->bs_dmstate = be16_to_cpu(dic->di_dmstate);
168 	buf->bs_aextents = be16_to_cpu(dic->di_anextents);
169 
170 	switch (dic->di_format) {
171 	case XFS_DINODE_FMT_DEV:
172 		buf->bs_rdev = xfs_dinode_get_rdev(dic);
173 		buf->bs_blksize = BLKDEV_IOSIZE;
174 		buf->bs_blocks = 0;
175 		break;
176 	case XFS_DINODE_FMT_LOCAL:
177 	case XFS_DINODE_FMT_UUID:
178 		buf->bs_rdev = 0;
179 		buf->bs_blksize = mp->m_sb.sb_blocksize;
180 		buf->bs_blocks = 0;
181 		break;
182 	case XFS_DINODE_FMT_EXTENTS:
183 	case XFS_DINODE_FMT_BTREE:
184 		buf->bs_rdev = 0;
185 		buf->bs_blksize = mp->m_sb.sb_blocksize;
186 		buf->bs_blocks = be64_to_cpu(dic->di_nblocks);
187 		break;
188 	}
189 }
190 
191 /* Return 0 on success or positive error */
192 STATIC int
193 xfs_bulkstat_one_fmt(
194 	void			__user *ubuffer,
195 	int			ubsize,
196 	int			*ubused,
197 	const xfs_bstat_t	*buffer)
198 {
199 	if (ubsize < sizeof(*buffer))
200 		return XFS_ERROR(ENOMEM);
201 	if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
202 		return XFS_ERROR(EFAULT);
203 	if (ubused)
204 		*ubused = sizeof(*buffer);
205 	return 0;
206 }
207 
208 /*
209  * Return stat information for one inode.
210  * Return 0 if ok, else errno.
211  */
212 int		   	    		/* error status */
213 xfs_bulkstat_one_int(
214 	xfs_mount_t	*mp,		/* mount point for filesystem */
215 	xfs_ino_t	ino,		/* inode number to get data for */
216 	void		__user *buffer,	/* buffer to place output in */
217 	int		ubsize,		/* size of buffer */
218 	bulkstat_one_fmt_pf formatter,	/* formatter, copy to user */
219 	xfs_daddr_t	bno,		/* starting bno of inode cluster */
220 	int		*ubused,	/* bytes used by me */
221 	void		*dibuff,	/* on-disk inode buffer */
222 	int		*stat)		/* BULKSTAT_RV_... */
223 {
224 	xfs_bstat_t	*buf;		/* return buffer */
225 	int		error = 0;	/* error value */
226 	xfs_dinode_t	*dip;		/* dinode inode pointer */
227 
228 	dip = (xfs_dinode_t *)dibuff;
229 	*stat = BULKSTAT_RV_NOTHING;
230 
231 	if (!buffer || xfs_internal_inum(mp, ino))
232 		return XFS_ERROR(EINVAL);
233 
234 	buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
235 
236 	if (dip == NULL) {
237 		/* We're not being passed a pointer to a dinode.  This happens
238 		 * if BULKSTAT_FG_IGET is selected.  Do the iget.
239 		 */
240 		error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
241 		if (error)
242 			goto out_free;
243 	} else {
244 		xfs_bulkstat_one_dinode(mp, ino, dip, buf);
245 	}
246 
247 	error = formatter(buffer, ubsize, ubused, buf);
248 	if (error)
249 		goto out_free;
250 
251 	*stat = BULKSTAT_RV_DIDONE;
252 
253  out_free:
254 	kmem_free(buf);
255 	return error;
256 }
257 
258 int
259 xfs_bulkstat_one(
260 	xfs_mount_t	*mp,		/* mount point for filesystem */
261 	xfs_ino_t	ino,		/* inode number to get data for */
262 	void		__user *buffer,	/* buffer to place output in */
263 	int		ubsize,		/* size of buffer */
264 	void		*private_data,	/* my private data */
265 	xfs_daddr_t	bno,		/* starting bno of inode cluster */
266 	int		*ubused,	/* bytes used by me */
267 	void		*dibuff,	/* on-disk inode buffer */
268 	int		*stat)		/* BULKSTAT_RV_... */
269 {
270 	return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
271 				    xfs_bulkstat_one_fmt, bno,
272 				    ubused, dibuff, stat);
273 }
274 
275 /*
276  * Test to see whether we can use the ondisk inode directly, based
277  * on the given bulkstat flags, filling in dipp accordingly.
278  * Returns zero if the inode is dodgey.
279  */
280 STATIC int
281 xfs_bulkstat_use_dinode(
282 	xfs_mount_t	*mp,
283 	int		flags,
284 	xfs_buf_t	*bp,
285 	int		clustidx,
286 	xfs_dinode_t	**dipp)
287 {
288 	xfs_dinode_t	*dip;
289 	unsigned int	aformat;
290 
291 	*dipp = NULL;
292 	if (!bp || (flags & BULKSTAT_FG_IGET))
293 		return 1;
294 	dip = (xfs_dinode_t *)
295 			xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
296 	/*
297 	 * Check the buffer containing the on-disk inode for di_mode == 0.
298 	 * This is to prevent xfs_bulkstat from picking up just reclaimed
299 	 * inodes that have their in-core state initialized but not flushed
300 	 * to disk yet. This is a temporary hack that would require a proper
301 	 * fix in the future.
302 	 */
303 	if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
304 	    !XFS_DINODE_GOOD_VERSION(dip->di_version) ||
305 	    !dip->di_mode)
306 		return 0;
307 	if (flags & BULKSTAT_FG_QUICK) {
308 		*dipp = dip;
309 		return 1;
310 	}
311 	/* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
312 	aformat = dip->di_aformat;
313 	if ((XFS_DFORK_Q(dip) == 0) ||
314 	    (aformat == XFS_DINODE_FMT_LOCAL) ||
315 	    (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_anextents)) {
316 		*dipp = dip;
317 		return 1;
318 	}
319 	return 1;
320 }
321 
322 #define XFS_BULKSTAT_UBLEFT(ubleft)	((ubleft) >= statstruct_size)
323 
324 /*
325  * Return stat information in bulk (by-inode) for the filesystem.
326  */
327 int					/* error status */
328 xfs_bulkstat(
329 	xfs_mount_t		*mp,	/* mount point for filesystem */
330 	xfs_ino_t		*lastinop, /* last inode returned */
331 	int			*ubcountp, /* size of buffer/count returned */
332 	bulkstat_one_pf		formatter, /* func that'd fill a single buf */
333 	void			*private_data,/* private data for formatter */
334 	size_t			statstruct_size, /* sizeof struct filling */
335 	char			__user *ubuffer, /* buffer with inode stats */
336 	int			flags,	/* defined in xfs_itable.h */
337 	int			*done)	/* 1 if there are more stats to get */
338 {
339 	xfs_agblock_t		agbno=0;/* allocation group block number */
340 	xfs_buf_t		*agbp;	/* agi header buffer */
341 	xfs_agi_t		*agi;	/* agi header data */
342 	xfs_agino_t		agino;	/* inode # in allocation group */
343 	xfs_agnumber_t		agno;	/* allocation group number */
344 	xfs_daddr_t		bno;	/* inode cluster start daddr */
345 	int			chunkidx; /* current index into inode chunk */
346 	int			clustidx; /* current index into inode cluster */
347 	xfs_btree_cur_t		*cur;	/* btree cursor for ialloc btree */
348 	int			end_of_ag; /* set if we've seen the ag end */
349 	int			error;	/* error code */
350 	int                     fmterror;/* bulkstat formatter result */
351 	__int32_t		gcnt;	/* current btree rec's count */
352 	xfs_inofree_t		gfree;	/* current btree rec's free mask */
353 	xfs_agino_t		gino;	/* current btree rec's start inode */
354 	int			i;	/* loop index */
355 	int			icount;	/* count of inodes good in irbuf */
356 	size_t			irbsize; /* size of irec buffer in bytes */
357 	xfs_ino_t		ino;	/* inode number (filesystem) */
358 	xfs_inobt_rec_incore_t	*irbp;	/* current irec buffer pointer */
359 	xfs_inobt_rec_incore_t	*irbuf;	/* start of irec buffer */
360 	xfs_inobt_rec_incore_t	*irbufend; /* end of good irec buffer entries */
361 	xfs_ino_t		lastino; /* last inode number returned */
362 	int			nbcluster; /* # of blocks in a cluster */
363 	int			nicluster; /* # of inodes in a cluster */
364 	int			nimask;	/* mask for inode clusters */
365 	int			nirbuf;	/* size of irbuf */
366 	int			rval;	/* return value error code */
367 	int			tmp;	/* result value from btree calls */
368 	int			ubcount; /* size of user's buffer */
369 	int			ubleft;	/* bytes left in user's buffer */
370 	char			__user *ubufp;	/* pointer into user's buffer */
371 	int			ubelem;	/* spaces used in user's buffer */
372 	int			ubused;	/* bytes used by formatter */
373 	xfs_buf_t		*bp;	/* ptr to on-disk inode cluster buf */
374 	xfs_dinode_t		*dip;	/* ptr into bp for specific inode */
375 
376 	/*
377 	 * Get the last inode value, see if there's nothing to do.
378 	 */
379 	ino = (xfs_ino_t)*lastinop;
380 	lastino = ino;
381 	dip = NULL;
382 	agno = XFS_INO_TO_AGNO(mp, ino);
383 	agino = XFS_INO_TO_AGINO(mp, ino);
384 	if (agno >= mp->m_sb.sb_agcount ||
385 	    ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
386 		*done = 1;
387 		*ubcountp = 0;
388 		return 0;
389 	}
390 	if (!ubcountp || *ubcountp <= 0) {
391 		return EINVAL;
392 	}
393 	ubcount = *ubcountp; /* statstruct's */
394 	ubleft = ubcount * statstruct_size; /* bytes */
395 	*ubcountp = ubelem = 0;
396 	*done = 0;
397 	fmterror = 0;
398 	ubufp = ubuffer;
399 	nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
400 		mp->m_sb.sb_inopblock :
401 		(XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
402 	nimask = ~(nicluster - 1);
403 	nbcluster = nicluster >> mp->m_sb.sb_inopblog;
404 	irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4,
405 				   KM_SLEEP | KM_MAYFAIL | KM_LARGE);
406 	nirbuf = irbsize / sizeof(*irbuf);
407 
408 	/*
409 	 * Loop over the allocation groups, starting from the last
410 	 * inode returned; 0 means start of the allocation group.
411 	 */
412 	rval = 0;
413 	while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
414 		cond_resched();
415 		bp = NULL;
416 		down_read(&mp->m_peraglock);
417 		error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
418 		up_read(&mp->m_peraglock);
419 		if (error) {
420 			/*
421 			 * Skip this allocation group and go to the next one.
422 			 */
423 			agno++;
424 			agino = 0;
425 			continue;
426 		}
427 		agi = XFS_BUF_TO_AGI(agbp);
428 		/*
429 		 * Allocate and initialize a btree cursor for ialloc btree.
430 		 */
431 		cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
432 		irbp = irbuf;
433 		irbufend = irbuf + nirbuf;
434 		end_of_ag = 0;
435 		/*
436 		 * If we're returning in the middle of an allocation group,
437 		 * we need to get the remainder of the chunk we're in.
438 		 */
439 		if (agino > 0) {
440 			/*
441 			 * Lookup the inode chunk that this inode lives in.
442 			 */
443 			error = xfs_inobt_lookup_le(cur, agino, 0, 0, &tmp);
444 			if (!error &&	/* no I/O error */
445 			    tmp &&	/* lookup succeeded */
446 					/* got the record, should always work */
447 			    !(error = xfs_inobt_get_rec(cur, &gino, &gcnt,
448 				    &gfree, &i)) &&
449 			    i == 1 &&
450 					/* this is the right chunk */
451 			    agino < gino + XFS_INODES_PER_CHUNK &&
452 					/* lastino was not last in chunk */
453 			    (chunkidx = agino - gino + 1) <
454 				    XFS_INODES_PER_CHUNK &&
455 					/* there are some left allocated */
456 			    xfs_inobt_maskn(chunkidx,
457 				    XFS_INODES_PER_CHUNK - chunkidx) & ~gfree) {
458 				/*
459 				 * Grab the chunk record.  Mark all the
460 				 * uninteresting inodes (because they're
461 				 * before our start point) free.
462 				 */
463 				for (i = 0; i < chunkidx; i++) {
464 					if (XFS_INOBT_MASK(i) & ~gfree)
465 						gcnt++;
466 				}
467 				gfree |= xfs_inobt_maskn(0, chunkidx);
468 				irbp->ir_startino = gino;
469 				irbp->ir_freecount = gcnt;
470 				irbp->ir_free = gfree;
471 				irbp++;
472 				agino = gino + XFS_INODES_PER_CHUNK;
473 				icount = XFS_INODES_PER_CHUNK - gcnt;
474 			} else {
475 				/*
476 				 * If any of those tests failed, bump the
477 				 * inode number (just in case).
478 				 */
479 				agino++;
480 				icount = 0;
481 			}
482 			/*
483 			 * In any case, increment to the next record.
484 			 */
485 			if (!error)
486 				error = xfs_btree_increment(cur, 0, &tmp);
487 		} else {
488 			/*
489 			 * Start of ag.  Lookup the first inode chunk.
490 			 */
491 			error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &tmp);
492 			icount = 0;
493 		}
494 		/*
495 		 * Loop through inode btree records in this ag,
496 		 * until we run out of inodes or space in the buffer.
497 		 */
498 		while (irbp < irbufend && icount < ubcount) {
499 			/*
500 			 * Loop as long as we're unable to read the
501 			 * inode btree.
502 			 */
503 			while (error) {
504 				agino += XFS_INODES_PER_CHUNK;
505 				if (XFS_AGINO_TO_AGBNO(mp, agino) >=
506 						be32_to_cpu(agi->agi_length))
507 					break;
508 				error = xfs_inobt_lookup_ge(cur, agino, 0, 0,
509 							    &tmp);
510 				cond_resched();
511 			}
512 			/*
513 			 * If ran off the end of the ag either with an error,
514 			 * or the normal way, set end and stop collecting.
515 			 */
516 			if (error ||
517 			    (error = xfs_inobt_get_rec(cur, &gino, &gcnt,
518 				    &gfree, &i)) ||
519 			    i == 0) {
520 				end_of_ag = 1;
521 				break;
522 			}
523 			/*
524 			 * If this chunk has any allocated inodes, save it.
525 			 * Also start read-ahead now for this chunk.
526 			 */
527 			if (gcnt < XFS_INODES_PER_CHUNK) {
528 				/*
529 				 * Loop over all clusters in the next chunk.
530 				 * Do a readahead if there are any allocated
531 				 * inodes in that cluster.
532 				 */
533 				for (agbno = XFS_AGINO_TO_AGBNO(mp, gino),
534 				     chunkidx = 0;
535 				     chunkidx < XFS_INODES_PER_CHUNK;
536 				     chunkidx += nicluster,
537 				     agbno += nbcluster) {
538 					if (xfs_inobt_maskn(chunkidx,
539 							    nicluster) & ~gfree)
540 						xfs_btree_reada_bufs(mp, agno,
541 							agbno, nbcluster);
542 				}
543 				irbp->ir_startino = gino;
544 				irbp->ir_freecount = gcnt;
545 				irbp->ir_free = gfree;
546 				irbp++;
547 				icount += XFS_INODES_PER_CHUNK - gcnt;
548 			}
549 			/*
550 			 * Set agino to after this chunk and bump the cursor.
551 			 */
552 			agino = gino + XFS_INODES_PER_CHUNK;
553 			error = xfs_btree_increment(cur, 0, &tmp);
554 			cond_resched();
555 		}
556 		/*
557 		 * Drop the btree buffers and the agi buffer.
558 		 * We can't hold any of the locks these represent
559 		 * when calling iget.
560 		 */
561 		xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
562 		xfs_buf_relse(agbp);
563 		/*
564 		 * Now format all the good inodes into the user's buffer.
565 		 */
566 		irbufend = irbp;
567 		for (irbp = irbuf;
568 		     irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
569 			/*
570 			 * Now process this chunk of inodes.
571 			 */
572 			for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
573 			     XFS_BULKSTAT_UBLEFT(ubleft) &&
574 				irbp->ir_freecount < XFS_INODES_PER_CHUNK;
575 			     chunkidx++, clustidx++, agino++) {
576 				ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
577 				/*
578 				 * Recompute agbno if this is the
579 				 * first inode of the cluster.
580 				 *
581 				 * Careful with clustidx.   There can be
582 				 * multple clusters per chunk, a single
583 				 * cluster per chunk or a cluster that has
584 				 * inodes represented from several different
585 				 * chunks (if blocksize is large).
586 				 *
587 				 * Because of this, the starting clustidx is
588 				 * initialized to zero in this loop but must
589 				 * later be reset after reading in the cluster
590 				 * buffer.
591 				 */
592 				if ((chunkidx & (nicluster - 1)) == 0) {
593 					agbno = XFS_AGINO_TO_AGBNO(mp,
594 							irbp->ir_startino) +
595 						((chunkidx & nimask) >>
596 						 mp->m_sb.sb_inopblog);
597 
598 					if (flags & (BULKSTAT_FG_QUICK |
599 						     BULKSTAT_FG_INLINE)) {
600 						int offset;
601 
602 						ino = XFS_AGINO_TO_INO(mp, agno,
603 								       agino);
604 						bno = XFS_AGB_TO_DADDR(mp, agno,
605 								       agbno);
606 
607 						/*
608 						 * Get the inode cluster buffer
609 						 */
610 						if (bp)
611 							xfs_buf_relse(bp);
612 
613 						error = xfs_inotobp(mp, NULL, ino, &dip,
614 								    &bp, &offset,
615 								    XFS_IGET_BULKSTAT);
616 
617 						if (!error)
618 							clustidx = offset / mp->m_sb.sb_inodesize;
619 						if (XFS_TEST_ERROR(error != 0,
620 								   mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
621 								   XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
622 							bp = NULL;
623 							ubleft = 0;
624 							rval = error;
625 							break;
626 						}
627 					}
628 				}
629 				ino = XFS_AGINO_TO_INO(mp, agno, agino);
630 				bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
631 				/*
632 				 * Skip if this inode is free.
633 				 */
634 				if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
635 					lastino = ino;
636 					continue;
637 				}
638 				/*
639 				 * Count used inodes as free so we can tell
640 				 * when the chunk is used up.
641 				 */
642 				irbp->ir_freecount++;
643 				if (!xfs_bulkstat_use_dinode(mp, flags, bp,
644 							     clustidx, &dip)) {
645 					lastino = ino;
646 					continue;
647 				}
648 				/*
649 				 * If we need to do an iget, cannot hold bp.
650 				 * Drop it, until starting the next cluster.
651 				 */
652 				if ((flags & BULKSTAT_FG_INLINE) && !dip) {
653 					if (bp)
654 						xfs_buf_relse(bp);
655 					bp = NULL;
656 				}
657 
658 				/*
659 				 * Get the inode and fill in a single buffer.
660 				 * BULKSTAT_FG_QUICK uses dip to fill it in.
661 				 * BULKSTAT_FG_IGET uses igets.
662 				 * BULKSTAT_FG_INLINE uses dip if we have an
663 				 * inline attr fork, else igets.
664 				 * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
665 				 * This is also used to count inodes/blks, etc
666 				 * in xfs_qm_quotacheck.
667 				 */
668 				ubused = statstruct_size;
669 				error = formatter(mp, ino, ubufp,
670 						ubleft, private_data,
671 						bno, &ubused, dip, &fmterror);
672 				if (fmterror == BULKSTAT_RV_NOTHING) {
673 					if (error && error != ENOENT &&
674 						error != EINVAL) {
675 						ubleft = 0;
676 						rval = error;
677 						break;
678 					}
679 					lastino = ino;
680 					continue;
681 				}
682 				if (fmterror == BULKSTAT_RV_GIVEUP) {
683 					ubleft = 0;
684 					ASSERT(error);
685 					rval = error;
686 					break;
687 				}
688 				if (ubufp)
689 					ubufp += ubused;
690 				ubleft -= ubused;
691 				ubelem++;
692 				lastino = ino;
693 			}
694 
695 			cond_resched();
696 		}
697 
698 		if (bp)
699 			xfs_buf_relse(bp);
700 
701 		/*
702 		 * Set up for the next loop iteration.
703 		 */
704 		if (XFS_BULKSTAT_UBLEFT(ubleft)) {
705 			if (end_of_ag) {
706 				agno++;
707 				agino = 0;
708 			} else
709 				agino = XFS_INO_TO_AGINO(mp, lastino);
710 		} else
711 			break;
712 	}
713 	/*
714 	 * Done, we're either out of filesystem or space to put the data.
715 	 */
716 	kmem_free(irbuf);
717 	*ubcountp = ubelem;
718 	/*
719 	 * Found some inodes, return them now and return the error next time.
720 	 */
721 	if (ubelem)
722 		rval = 0;
723 	if (agno >= mp->m_sb.sb_agcount) {
724 		/*
725 		 * If we ran out of filesystem, mark lastino as off
726 		 * the end of the filesystem, so the next call
727 		 * will return immediately.
728 		 */
729 		*lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
730 		*done = 1;
731 	} else
732 		*lastinop = (xfs_ino_t)lastino;
733 
734 	return rval;
735 }
736 
737 /*
738  * Return stat information in bulk (by-inode) for the filesystem.
739  * Special case for non-sequential one inode bulkstat.
740  */
741 int					/* error status */
742 xfs_bulkstat_single(
743 	xfs_mount_t		*mp,	/* mount point for filesystem */
744 	xfs_ino_t		*lastinop, /* inode to return */
745 	char			__user *buffer, /* buffer with inode stats */
746 	int			*done)	/* 1 if there are more stats to get */
747 {
748 	int			count;	/* count value for bulkstat call */
749 	int			error;	/* return value */
750 	xfs_ino_t		ino;	/* filesystem inode number */
751 	int			res;	/* result from bs1 */
752 
753 	/*
754 	 * note that requesting valid inode numbers which are not allocated
755 	 * to inodes will most likely cause xfs_itobp to generate warning
756 	 * messages about bad magic numbers. This is ok. The fact that
757 	 * the inode isn't actually an inode is handled by the
758 	 * error check below. Done this way to make the usual case faster
759 	 * at the expense of the error case.
760 	 */
761 
762 	ino = (xfs_ino_t)*lastinop;
763 	error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
764 				 NULL, 0, NULL, NULL, &res);
765 	if (error) {
766 		/*
767 		 * Special case way failed, do it the "long" way
768 		 * to see if that works.
769 		 */
770 		(*lastinop)--;
771 		count = 1;
772 		if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
773 				NULL, sizeof(xfs_bstat_t), buffer,
774 				BULKSTAT_FG_IGET, done))
775 			return error;
776 		if (count == 0 || (xfs_ino_t)*lastinop != ino)
777 			return error == EFSCORRUPTED ?
778 				XFS_ERROR(EINVAL) : error;
779 		else
780 			return 0;
781 	}
782 	*done = 0;
783 	return 0;
784 }
785 
786 int
787 xfs_inumbers_fmt(
788 	void			__user *ubuffer, /* buffer to write to */
789 	const xfs_inogrp_t	*buffer,	/* buffer to read from */
790 	long			count,		/* # of elements to read */
791 	long			*written)	/* # of bytes written */
792 {
793 	if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
794 		return -EFAULT;
795 	*written = count * sizeof(*buffer);
796 	return 0;
797 }
798 
799 /*
800  * Return inode number table for the filesystem.
801  */
802 int					/* error status */
803 xfs_inumbers(
804 	xfs_mount_t	*mp,		/* mount point for filesystem */
805 	xfs_ino_t	*lastino,	/* last inode returned */
806 	int		*count,		/* size of buffer/count returned */
807 	void		__user *ubuffer,/* buffer with inode descriptions */
808 	inumbers_fmt_pf	formatter)
809 {
810 	xfs_buf_t	*agbp;
811 	xfs_agino_t	agino;
812 	xfs_agnumber_t	agno;
813 	int		bcount;
814 	xfs_inogrp_t	*buffer;
815 	int		bufidx;
816 	xfs_btree_cur_t	*cur;
817 	int		error;
818 	__int32_t	gcnt;
819 	xfs_inofree_t	gfree;
820 	xfs_agino_t	gino;
821 	int		i;
822 	xfs_ino_t	ino;
823 	int		left;
824 	int		tmp;
825 
826 	ino = (xfs_ino_t)*lastino;
827 	agno = XFS_INO_TO_AGNO(mp, ino);
828 	agino = XFS_INO_TO_AGINO(mp, ino);
829 	left = *count;
830 	*count = 0;
831 	bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
832 	buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
833 	error = bufidx = 0;
834 	cur = NULL;
835 	agbp = NULL;
836 	while (left > 0 && agno < mp->m_sb.sb_agcount) {
837 		if (agbp == NULL) {
838 			down_read(&mp->m_peraglock);
839 			error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
840 			up_read(&mp->m_peraglock);
841 			if (error) {
842 				/*
843 				 * If we can't read the AGI of this ag,
844 				 * then just skip to the next one.
845 				 */
846 				ASSERT(cur == NULL);
847 				agbp = NULL;
848 				agno++;
849 				agino = 0;
850 				continue;
851 			}
852 			cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
853 			error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp);
854 			if (error) {
855 				xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
856 				cur = NULL;
857 				xfs_buf_relse(agbp);
858 				agbp = NULL;
859 				/*
860 				 * Move up the last inode in the current
861 				 * chunk.  The lookup_ge will always get
862 				 * us the first inode in the next chunk.
863 				 */
864 				agino += XFS_INODES_PER_CHUNK - 1;
865 				continue;
866 			}
867 		}
868 		if ((error = xfs_inobt_get_rec(cur, &gino, &gcnt, &gfree,
869 			&i)) ||
870 		    i == 0) {
871 			xfs_buf_relse(agbp);
872 			agbp = NULL;
873 			xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
874 			cur = NULL;
875 			agno++;
876 			agino = 0;
877 			continue;
878 		}
879 		agino = gino + XFS_INODES_PER_CHUNK - 1;
880 		buffer[bufidx].xi_startino = XFS_AGINO_TO_INO(mp, agno, gino);
881 		buffer[bufidx].xi_alloccount = XFS_INODES_PER_CHUNK - gcnt;
882 		buffer[bufidx].xi_allocmask = ~gfree;
883 		bufidx++;
884 		left--;
885 		if (bufidx == bcount) {
886 			long written;
887 			if (formatter(ubuffer, buffer, bufidx, &written)) {
888 				error = XFS_ERROR(EFAULT);
889 				break;
890 			}
891 			ubuffer += written;
892 			*count += bufidx;
893 			bufidx = 0;
894 		}
895 		if (left) {
896 			error = xfs_btree_increment(cur, 0, &tmp);
897 			if (error) {
898 				xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
899 				cur = NULL;
900 				xfs_buf_relse(agbp);
901 				agbp = NULL;
902 				/*
903 				 * The agino value has already been bumped.
904 				 * Just try to skip up to it.
905 				 */
906 				agino += XFS_INODES_PER_CHUNK;
907 				continue;
908 			}
909 		}
910 	}
911 	if (!error) {
912 		if (bufidx) {
913 			long written;
914 			if (formatter(ubuffer, buffer, bufidx, &written))
915 				error = XFS_ERROR(EFAULT);
916 			else
917 				*count += bufidx;
918 		}
919 		*lastino = XFS_AGINO_TO_INO(mp, agno, agino);
920 	}
921 	kmem_free(buffer);
922 	if (cur)
923 		xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
924 					   XFS_BTREE_NOERROR));
925 	if (agbp)
926 		xfs_buf_relse(agbp);
927 	return error;
928 }
929