blob: b1b801e4a28e41643d93b6c5438e01718c665bed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
37#include "xfs_ialloc.h"
38#include "xfs_itable.h"
39#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Eric Sandeend96f8f82009-07-02 00:09:33 -050042STATIC int
Vlad Apostolov6f1f2162006-09-28 11:06:15 +100043xfs_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 ||
Eric Sandeen62118702008-03-06 13:44:28 +110048 (xfs_sb_version_hasquota(&mp->m_sb) &&
Vlad Apostolov6f1f2162006-09-28 11:06:15 +100049 (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino)));
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052STATIC int
53xfs_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{
Christoph Hellwig347d1c02007-08-28 13:57:51 +100060 xfs_icdinode_t *dic; /* dinode core info pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 xfs_inode_t *ip; /* incore inode pointer */
Christoph Hellwigf9581b12009-10-06 20:29:26 +000062 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int error;
64
Nathan Scott745b1f472006-09-28 11:02:23 +100065 error = xfs_iget(mp, NULL, ino,
66 XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (error) {
68 *stat = BULKSTAT_RV_NOTHING;
69 return error;
70 }
71
72 ASSERT(ip != NULL);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +110073 ASSERT(ip->i_imap.im_blkno != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 dic = &ip->i_d;
Christoph Hellwigf9581b12009-10-06 20:29:26 +000076 inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* xfs_iget returns the following without needing
79 * further change.
80 */
81 buf->bs_nlink = dic->di_nlink;
82 buf->bs_projid = dic->di_projid;
83 buf->bs_ino = ino;
84 buf->bs_mode = dic->di_mode;
85 buf->bs_uid = dic->di_uid;
86 buf->bs_gid = dic->di_gid;
87 buf->bs_size = dic->di_size;
Christoph Hellwigf9581b12009-10-06 20:29:26 +000088
Christoph Hellwig8fab4512009-03-16 08:24:46 +010089 /*
Christoph Hellwigf9581b12009-10-06 20:29:26 +000090 * We need to read the timestamps from the Linux inode because
91 * the VFS keeps writing directly into the inode structure instead
92 * of telling us about the updates.
Christoph Hellwig8fab4512009-03-16 08:24:46 +010093 */
Christoph Hellwigf9581b12009-10-06 20:29:26 +000094 buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
95 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
96 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
97 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
98 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
99 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 buf->bs_xflags = xfs_ip2xflags(ip);
102 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
103 buf->bs_extents = dic->di_nextents;
104 buf->bs_gen = dic->di_gen;
105 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
106 buf->bs_dmevmask = dic->di_dmevmask;
107 buf->bs_dmstate = dic->di_dmstate;
108 buf->bs_aextents = dic->di_anextents;
Dave Chinner07000ee2010-03-05 04:41:14 +0000109 buf->bs_forkoff = XFS_IFORK_BOFF(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 switch (dic->di_format) {
112 case XFS_DINODE_FMT_DEV:
113 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
114 buf->bs_blksize = BLKDEV_IOSIZE;
115 buf->bs_blocks = 0;
116 break;
117 case XFS_DINODE_FMT_LOCAL:
118 case XFS_DINODE_FMT_UUID:
119 buf->bs_rdev = 0;
120 buf->bs_blksize = mp->m_sb.sb_blocksize;
121 buf->bs_blocks = 0;
122 break;
123 case XFS_DINODE_FMT_EXTENTS:
124 case XFS_DINODE_FMT_BTREE:
125 buf->bs_rdev = 0;
126 buf->bs_blksize = mp->m_sb.sb_blocksize;
127 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
128 break;
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 xfs_iput(ip, XFS_ILOCK_SHARED);
132 return error;
133}
134
David Chinner7b073392008-04-10 12:24:04 +1000135STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136xfs_bulkstat_one_dinode(
137 xfs_mount_t *mp, /* mount point for filesystem */
138 xfs_ino_t ino, /* inode number to get data for */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100139 xfs_dinode_t *dic, /* dinode inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 xfs_bstat_t *buf) /* return buffer */
141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /*
143 * The inode format changed when we moved the link count and
144 * made it 32 bits long. If this is an old format inode,
145 * convert it in memory to look like a new one. If it gets
146 * flushed to disk we will convert back before flushing or
147 * logging it. We zero out the new projid field and the old link
148 * count field. We'll handle clearing the pad field (the remains
149 * of the old uuid field) when we actually convert the inode to
150 * the new format. We don't change the version number so that we
151 * can distinguish this from a real new format inode.
152 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100153 if (dic->di_version == 1) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000154 buf->bs_nlink = be16_to_cpu(dic->di_onlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 buf->bs_projid = 0;
156 } else {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000157 buf->bs_nlink = be32_to_cpu(dic->di_nlink);
158 buf->bs_projid = be16_to_cpu(dic->di_projid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 buf->bs_ino = ino;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000162 buf->bs_mode = be16_to_cpu(dic->di_mode);
163 buf->bs_uid = be32_to_cpu(dic->di_uid);
164 buf->bs_gid = be32_to_cpu(dic->di_gid);
165 buf->bs_size = be64_to_cpu(dic->di_size);
166 buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec);
167 buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec);
168 buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec);
169 buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec);
170 buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec);
171 buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec);
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100172 buf->bs_xflags = xfs_dic2xflags(dic);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000173 buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog;
174 buf->bs_extents = be32_to_cpu(dic->di_nextents);
175 buf->bs_gen = be32_to_cpu(dic->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000177 buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask);
178 buf->bs_dmstate = be16_to_cpu(dic->di_dmstate);
179 buf->bs_aextents = be16_to_cpu(dic->di_anextents);
Dave Chinner07000ee2010-03-05 04:41:14 +0000180 buf->bs_forkoff = XFS_DFORK_BOFF(dic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000182 switch (dic->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 case XFS_DINODE_FMT_DEV:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100184 buf->bs_rdev = xfs_dinode_get_rdev(dic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 buf->bs_blksize = BLKDEV_IOSIZE;
186 buf->bs_blocks = 0;
187 break;
188 case XFS_DINODE_FMT_LOCAL:
189 case XFS_DINODE_FMT_UUID:
190 buf->bs_rdev = 0;
191 buf->bs_blksize = mp->m_sb.sb_blocksize;
192 buf->bs_blocks = 0;
193 break;
194 case XFS_DINODE_FMT_EXTENTS:
195 case XFS_DINODE_FMT_BTREE:
196 buf->bs_rdev = 0;
197 buf->bs_blksize = mp->m_sb.sb_blocksize;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000198 buf->bs_blocks = be64_to_cpu(dic->di_nblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600203/* Return 0 on success or positive error */
Michal Marekfaa63e92007-07-11 11:10:19 +1000204STATIC int
205xfs_bulkstat_one_fmt(
206 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600207 int ubsize,
208 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000209 const xfs_bstat_t *buffer)
210{
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600211 if (ubsize < sizeof(*buffer))
212 return XFS_ERROR(ENOMEM);
Michal Marekfaa63e92007-07-11 11:10:19 +1000213 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600214 return XFS_ERROR(EFAULT);
215 if (ubused)
216 *ubused = sizeof(*buffer);
217 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * Return stat information for one inode.
222 * Return 0 if ok, else errno.
223 */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600224int /* error status */
225xfs_bulkstat_one_int(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 xfs_mount_t *mp, /* mount point for filesystem */
227 xfs_ino_t ino, /* inode number to get data for */
228 void __user *buffer, /* buffer to place output in */
229 int ubsize, /* size of buffer */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600230 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 xfs_daddr_t bno, /* starting bno of inode cluster */
232 int *ubused, /* bytes used by me */
233 void *dibuff, /* on-disk inode buffer */
234 int *stat) /* BULKSTAT_RV_... */
235{
236 xfs_bstat_t *buf; /* return buffer */
237 int error = 0; /* error value */
238 xfs_dinode_t *dip; /* dinode inode pointer */
239
240 dip = (xfs_dinode_t *)dibuff;
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000241 *stat = BULKSTAT_RV_NOTHING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000243 if (!buffer || xfs_internal_inum(mp, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
247
248 if (dip == NULL) {
249 /* We're not being passed a pointer to a dinode. This happens
250 * if BULKSTAT_FG_IGET is selected. Do the iget.
251 */
252 error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
253 if (error)
254 goto out_free;
255 } else {
256 xfs_bulkstat_one_dinode(mp, ino, dip, buf);
257 }
258
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600259 error = formatter(buffer, ubsize, ubused, buf);
260 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 *stat = BULKSTAT_RV_DIDONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 out_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000266 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return error;
268}
269
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600270int
271xfs_bulkstat_one(
272 xfs_mount_t *mp, /* mount point for filesystem */
273 xfs_ino_t ino, /* inode number to get data for */
274 void __user *buffer, /* buffer to place output in */
275 int ubsize, /* size of buffer */
276 void *private_data, /* my private data */
277 xfs_daddr_t bno, /* starting bno of inode cluster */
278 int *ubused, /* bytes used by me */
279 void *dibuff, /* on-disk inode buffer */
280 int *stat) /* BULKSTAT_RV_... */
281{
282 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
283 xfs_bulkstat_one_fmt, bno,
284 ubused, dibuff, stat);
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
Nathan Scott8b56f082006-09-28 11:01:46 +1000288 * Test to see whether we can use the ondisk inode directly, based
289 * on the given bulkstat flags, filling in dipp accordingly.
290 * Returns zero if the inode is dodgey.
291 */
292STATIC int
293xfs_bulkstat_use_dinode(
294 xfs_mount_t *mp,
295 int flags,
296 xfs_buf_t *bp,
297 int clustidx,
298 xfs_dinode_t **dipp)
299{
300 xfs_dinode_t *dip;
301 unsigned int aformat;
302
303 *dipp = NULL;
304 if (!bp || (flags & BULKSTAT_FG_IGET))
305 return 1;
306 dip = (xfs_dinode_t *)
307 xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
Vlad Apostolov859d7182007-10-11 17:44:18 +1000308 /*
Vlad Apostolovc319b582007-11-23 16:27:51 +1100309 * Check the buffer containing the on-disk inode for di_mode == 0.
Vlad Apostolov859d7182007-10-11 17:44:18 +1000310 * This is to prevent xfs_bulkstat from picking up just reclaimed
311 * inodes that have their in-core state initialized but not flushed
312 * to disk yet. This is a temporary hack that would require a proper
313 * fix in the future.
314 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100315 if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
316 !XFS_DINODE_GOOD_VERSION(dip->di_version) ||
317 !dip->di_mode)
Nathan Scott8b56f082006-09-28 11:01:46 +1000318 return 0;
319 if (flags & BULKSTAT_FG_QUICK) {
320 *dipp = dip;
321 return 1;
322 }
323 /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100324 aformat = dip->di_aformat;
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100325 if ((XFS_DFORK_Q(dip) == 0) ||
Nathan Scott8b56f082006-09-28 11:01:46 +1000326 (aformat == XFS_DINODE_FMT_LOCAL) ||
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100327 (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_anextents)) {
Nathan Scott8b56f082006-09-28 11:01:46 +1000328 *dipp = dip;
329 return 1;
330 }
331 return 1;
332}
333
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100334#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
335
Nathan Scott8b56f082006-09-28 11:01:46 +1000336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Return stat information in bulk (by-inode) for the filesystem.
338 */
339int /* error status */
340xfs_bulkstat(
341 xfs_mount_t *mp, /* mount point for filesystem */
342 xfs_ino_t *lastinop, /* last inode returned */
343 int *ubcountp, /* size of buffer/count returned */
344 bulkstat_one_pf formatter, /* func that'd fill a single buf */
345 void *private_data,/* private data for formatter */
346 size_t statstruct_size, /* sizeof struct filling */
347 char __user *ubuffer, /* buffer with inode stats */
348 int flags, /* defined in xfs_itable.h */
Nathan Scottc41564b2006-03-29 08:55:14 +1000349 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 xfs_agblock_t agbno=0;/* allocation group block number */
352 xfs_buf_t *agbp; /* agi header buffer */
353 xfs_agi_t *agi; /* agi header data */
354 xfs_agino_t agino; /* inode # in allocation group */
355 xfs_agnumber_t agno; /* allocation group number */
356 xfs_daddr_t bno; /* inode cluster start daddr */
357 int chunkidx; /* current index into inode chunk */
358 int clustidx; /* current index into inode cluster */
359 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
360 int end_of_ag; /* set if we've seen the ag end */
361 int error; /* error code */
362 int fmterror;/* bulkstat formatter result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int i; /* loop index */
364 int icount; /* count of inodes good in irbuf */
Nathan Scott215101c2006-09-28 11:04:43 +1000365 size_t irbsize; /* size of irec buffer in bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 xfs_ino_t ino; /* inode number (filesystem) */
Nathan Scott262750932006-09-28 11:02:03 +1000367 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
368 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
369 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100370 xfs_ino_t lastino; /* last inode number returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int nbcluster; /* # of blocks in a cluster */
372 int nicluster; /* # of inodes in a cluster */
373 int nimask; /* mask for inode clusters */
374 int nirbuf; /* size of irbuf */
375 int rval; /* return value error code */
376 int tmp; /* result value from btree calls */
377 int ubcount; /* size of user's buffer */
378 int ubleft; /* bytes left in user's buffer */
379 char __user *ubufp; /* pointer into user's buffer */
380 int ubelem; /* spaces used in user's buffer */
381 int ubused; /* bytes used by formatter */
382 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
383 xfs_dinode_t *dip; /* ptr into bp for specific inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /*
386 * Get the last inode value, see if there's nothing to do.
387 */
388 ino = (xfs_ino_t)*lastinop;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100389 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 dip = NULL;
391 agno = XFS_INO_TO_AGNO(mp, ino);
392 agino = XFS_INO_TO_AGINO(mp, ino);
393 if (agno >= mp->m_sb.sb_agcount ||
394 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
395 *done = 1;
396 *ubcountp = 0;
397 return 0;
398 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100399 if (!ubcountp || *ubcountp <= 0) {
400 return EINVAL;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 ubcount = *ubcountp; /* statstruct's */
403 ubleft = ubcount * statstruct_size; /* bytes */
404 *ubcountp = ubelem = 0;
405 *done = 0;
406 fmterror = 0;
407 ubufp = ubuffer;
408 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
409 mp->m_sb.sb_inopblock :
410 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
411 nimask = ~(nicluster - 1);
412 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000413 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
414 if (!irbuf)
415 return ENOMEM;
416
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000417 nirbuf = irbsize / sizeof(*irbuf);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * Loop over the allocation groups, starting from the last
421 * inode returned; 0 means start of the allocation group.
422 */
423 rval = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100424 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
425 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (error) {
429 /*
430 * Skip this allocation group and go to the next one.
431 */
432 agno++;
433 agino = 0;
434 continue;
435 }
436 agi = XFS_BUF_TO_AGI(agbp);
437 /*
438 * Allocate and initialize a btree cursor for ialloc btree.
439 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100440 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 irbp = irbuf;
442 irbufend = irbuf + nirbuf;
443 end_of_ag = 0;
444 /*
445 * If we're returning in the middle of an allocation group,
446 * we need to get the remainder of the chunk we're in.
447 */
448 if (agino > 0) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300449 xfs_inobt_rec_incore_t r;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
452 * Lookup the inode chunk that this inode lives in.
453 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300454 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
455 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!error && /* no I/O error */
457 tmp && /* lookup succeeded */
458 /* got the record, should always work */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300459 !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 i == 1 &&
461 /* this is the right chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300462 agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* lastino was not last in chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300464 (chunkidx = agino - r.ir_startino + 1) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 XFS_INODES_PER_CHUNK &&
466 /* there are some left allocated */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600467 xfs_inobt_maskn(chunkidx,
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300468 XFS_INODES_PER_CHUNK - chunkidx) &
469 ~r.ir_free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * Grab the chunk record. Mark all the
472 * uninteresting inodes (because they're
473 * before our start point) free.
474 */
475 for (i = 0; i < chunkidx; i++) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300476 if (XFS_INOBT_MASK(i) & ~r.ir_free)
477 r.ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300479 r.ir_free |= xfs_inobt_maskn(0, chunkidx);
480 irbp->ir_startino = r.ir_startino;
481 irbp->ir_freecount = r.ir_freecount;
482 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300484 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
485 icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 } else {
487 /*
488 * If any of those tests failed, bump the
489 * inode number (just in case).
490 */
491 agino++;
492 icount = 0;
493 }
494 /*
495 * In any case, increment to the next record.
496 */
497 if (!error)
Christoph Hellwig637aa502008-10-30 16:55:45 +1100498 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 } else {
500 /*
501 * Start of ag. Lookup the first inode chunk.
502 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300503 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 icount = 0;
505 }
506 /*
507 * Loop through inode btree records in this ag,
508 * until we run out of inodes or space in the buffer.
509 */
510 while (irbp < irbufend && icount < ubcount) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300511 xfs_inobt_rec_incore_t r;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
514 * Loop as long as we're unable to read the
515 * inode btree.
516 */
517 while (error) {
518 agino += XFS_INODES_PER_CHUNK;
519 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100520 be32_to_cpu(agi->agi_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
Christoph Hellwig21875502009-08-31 20:58:21 -0300522 error = xfs_inobt_lookup(cur, agino,
523 XFS_LOOKUP_GE, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100524 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 /*
527 * If ran off the end of the ag either with an error,
528 * or the normal way, set end and stop collecting.
529 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300530 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 end_of_ag = 1;
532 break;
533 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300534
535 error = xfs_inobt_get_rec(cur, &r, &i);
536 if (error || i == 0) {
537 end_of_ag = 1;
538 break;
539 }
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
542 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000543 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300545 if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
Nathan Scott262750932006-09-28 11:02:03 +1000546 /*
547 * Loop over all clusters in the next chunk.
548 * Do a readahead if there are any allocated
549 * inodes in that cluster.
550 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300551 agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
552 for (chunkidx = 0;
Nathan Scott262750932006-09-28 11:02:03 +1000553 chunkidx < XFS_INODES_PER_CHUNK;
554 chunkidx += nicluster,
555 agbno += nbcluster) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300556 if (xfs_inobt_maskn(chunkidx, nicluster)
557 & ~r.ir_free)
Nathan Scott262750932006-09-28 11:02:03 +1000558 xfs_btree_reada_bufs(mp, agno,
559 agbno, nbcluster);
560 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300561 irbp->ir_startino = r.ir_startino;
562 irbp->ir_freecount = r.ir_freecount;
563 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300565 icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 /*
568 * Set agino to after this chunk and bump the cursor.
569 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300570 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100571 error = xfs_btree_increment(cur, 0, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100572 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 /*
575 * Drop the btree buffers and the agi buffer.
576 * We can't hold any of the locks these represent
577 * when calling iget.
578 */
579 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
580 xfs_buf_relse(agbp);
581 /*
582 * Now format all the good inodes into the user's buffer.
583 */
584 irbufend = irbp;
585 for (irbp = irbuf;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100586 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * Now process this chunk of inodes.
589 */
Nathan Scott262750932006-09-28 11:02:03 +1000590 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100591 XFS_BULKSTAT_UBLEFT(ubleft) &&
Nathan Scott262750932006-09-28 11:02:03 +1000592 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 chunkidx++, clustidx++, agino++) {
594 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
595 /*
596 * Recompute agbno if this is the
597 * first inode of the cluster.
598 *
599 * Careful with clustidx. There can be
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200600 * multiple clusters per chunk, a single
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * cluster per chunk or a cluster that has
602 * inodes represented from several different
603 * chunks (if blocksize is large).
604 *
605 * Because of this, the starting clustidx is
606 * initialized to zero in this loop but must
607 * later be reset after reading in the cluster
608 * buffer.
609 */
610 if ((chunkidx & (nicluster - 1)) == 0) {
611 agbno = XFS_AGINO_TO_AGBNO(mp,
Nathan Scott262750932006-09-28 11:02:03 +1000612 irbp->ir_startino) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ((chunkidx & nimask) >>
614 mp->m_sb.sb_inopblog);
615
Nathan Scott8b56f082006-09-28 11:01:46 +1000616 if (flags & (BULKSTAT_FG_QUICK |
617 BULKSTAT_FG_INLINE)) {
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100618 int offset;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 ino = XFS_AGINO_TO_INO(mp, agno,
621 agino);
622 bno = XFS_AGB_TO_DADDR(mp, agno,
623 agbno);
624
625 /*
626 * Get the inode cluster buffer
627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (bp)
629 xfs_buf_relse(bp);
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100630
631 error = xfs_inotobp(mp, NULL, ino, &dip,
632 &bp, &offset,
Christoph Hellwigb48d8d62008-11-28 14:23:41 +1100633 XFS_IGET_BULKSTAT);
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (!error)
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100636 clustidx = offset / mp->m_sb.sb_inodesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (XFS_TEST_ERROR(error != 0,
638 mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
639 XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
640 bp = NULL;
Nathan Scottb12dd342006-03-17 17:26:04 +1100641 ubleft = 0;
642 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 break;
644 }
645 }
646 }
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000647 ino = XFS_AGINO_TO_INO(mp, agno, agino);
648 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
650 * Skip if this inode is free.
651 */
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000652 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
653 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * Count used inodes as free so we can tell
658 * when the chunk is used up.
659 */
Nathan Scott262750932006-09-28 11:02:03 +1000660 irbp->ir_freecount++;
Nathan Scott8b56f082006-09-28 11:01:46 +1000661 if (!xfs_bulkstat_use_dinode(mp, flags, bp,
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000662 clustidx, &dip)) {
663 lastino = ino;
Nathan Scott8b56f082006-09-28 11:01:46 +1000664 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000665 }
Nathan Scott8b56f082006-09-28 11:01:46 +1000666 /*
667 * If we need to do an iget, cannot hold bp.
668 * Drop it, until starting the next cluster.
669 */
670 if ((flags & BULKSTAT_FG_INLINE) && !dip) {
671 if (bp)
672 xfs_buf_relse(bp);
673 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
676 /*
677 * Get the inode and fill in a single buffer.
678 * BULKSTAT_FG_QUICK uses dip to fill it in.
679 * BULKSTAT_FG_IGET uses igets.
Nathan Scott8b56f082006-09-28 11:01:46 +1000680 * BULKSTAT_FG_INLINE uses dip if we have an
681 * inline attr fork, else igets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
683 * This is also used to count inodes/blks, etc
684 * in xfs_qm_quotacheck.
685 */
686 ubused = statstruct_size;
687 error = formatter(mp, ino, ubufp,
688 ubleft, private_data,
689 bno, &ubused, dip, &fmterror);
690 if (fmterror == BULKSTAT_RV_NOTHING) {
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100691 if (error && error != ENOENT &&
692 error != EINVAL) {
Vlad Apostolove132f542006-09-28 11:04:31 +1000693 ubleft = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100694 rval = error;
695 break;
696 }
697 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 continue;
699 }
700 if (fmterror == BULKSTAT_RV_GIVEUP) {
701 ubleft = 0;
702 ASSERT(error);
703 rval = error;
704 break;
705 }
706 if (ubufp)
707 ubufp += ubused;
708 ubleft -= ubused;
709 ubelem++;
710 lastino = ino;
711 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100712
713 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
716 if (bp)
717 xfs_buf_relse(bp);
718
719 /*
720 * Set up for the next loop iteration.
721 */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100722 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (end_of_ag) {
724 agno++;
725 agino = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100726 } else
727 agino = XFS_INO_TO_AGINO(mp, lastino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 } else
729 break;
730 }
731 /*
732 * Done, we're either out of filesystem or space to put the data.
733 */
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000734 kmem_free_large(irbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 *ubcountp = ubelem;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100736 /*
737 * Found some inodes, return them now and return the error next time.
738 */
739 if (ubelem)
740 rval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (agno >= mp->m_sb.sb_agcount) {
742 /*
743 * If we ran out of filesystem, mark lastino as off
744 * the end of the filesystem, so the next call
745 * will return immediately.
746 */
747 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
748 *done = 1;
749 } else
750 *lastinop = (xfs_ino_t)lastino;
751
752 return rval;
753}
754
755/*
756 * Return stat information in bulk (by-inode) for the filesystem.
757 * Special case for non-sequential one inode bulkstat.
758 */
759int /* error status */
760xfs_bulkstat_single(
761 xfs_mount_t *mp, /* mount point for filesystem */
762 xfs_ino_t *lastinop, /* inode to return */
763 char __user *buffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000764 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 int count; /* count value for bulkstat call */
767 int error; /* return value */
768 xfs_ino_t ino; /* filesystem inode number */
769 int res; /* result from bs1 */
770
771 /*
772 * note that requesting valid inode numbers which are not allocated
773 * to inodes will most likely cause xfs_itobp to generate warning
774 * messages about bad magic numbers. This is ok. The fact that
775 * the inode isn't actually an inode is handled by the
776 * error check below. Done this way to make the usual case faster
777 * at the expense of the error case.
778 */
779
780 ino = (xfs_ino_t)*lastinop;
781 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
782 NULL, 0, NULL, NULL, &res);
783 if (error) {
784 /*
785 * Special case way failed, do it the "long" way
786 * to see if that works.
787 */
788 (*lastinop)--;
789 count = 1;
790 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
791 NULL, sizeof(xfs_bstat_t), buffer,
792 BULKSTAT_FG_IGET, done))
793 return error;
794 if (count == 0 || (xfs_ino_t)*lastinop != ino)
795 return error == EFSCORRUPTED ?
796 XFS_ERROR(EINVAL) : error;
797 else
798 return 0;
799 }
800 *done = 0;
801 return 0;
802}
803
Michal Marekfaa63e92007-07-11 11:10:19 +1000804int
805xfs_inumbers_fmt(
806 void __user *ubuffer, /* buffer to write to */
807 const xfs_inogrp_t *buffer, /* buffer to read from */
808 long count, /* # of elements to read */
809 long *written) /* # of bytes written */
810{
811 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
812 return -EFAULT;
813 *written = count * sizeof(*buffer);
814 return 0;
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*
818 * Return inode number table for the filesystem.
819 */
820int /* error status */
821xfs_inumbers(
822 xfs_mount_t *mp, /* mount point for filesystem */
823 xfs_ino_t *lastino, /* last inode returned */
824 int *count, /* size of buffer/count returned */
Michal Marekfaa63e92007-07-11 11:10:19 +1000825 void __user *ubuffer,/* buffer with inode descriptions */
826 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 xfs_buf_t *agbp;
829 xfs_agino_t agino;
830 xfs_agnumber_t agno;
831 int bcount;
832 xfs_inogrp_t *buffer;
833 int bufidx;
834 xfs_btree_cur_t *cur;
835 int error;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300836 xfs_inobt_rec_incore_t r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int i;
838 xfs_ino_t ino;
839 int left;
840 int tmp;
841
842 ino = (xfs_ino_t)*lastino;
843 agno = XFS_INO_TO_AGNO(mp, ino);
844 agino = XFS_INO_TO_AGINO(mp, ino);
845 left = *count;
846 *count = 0;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100847 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
849 error = bufidx = 0;
850 cur = NULL;
851 agbp = NULL;
852 while (left > 0 && agno < mp->m_sb.sb_agcount) {
853 if (agbp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (error) {
856 /*
857 * If we can't read the AGI of this ag,
858 * then just skip to the next one.
859 */
860 ASSERT(cur == NULL);
861 agbp = NULL;
862 agno++;
863 agino = 0;
864 continue;
865 }
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100866 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Christoph Hellwig21875502009-08-31 20:58:21 -0300867 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
868 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (error) {
870 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
871 cur = NULL;
872 xfs_buf_relse(agbp);
873 agbp = NULL;
874 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200875 * Move up the last inode in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * chunk. The lookup_ge will always get
877 * us the first inode in the next chunk.
878 */
879 agino += XFS_INODES_PER_CHUNK - 1;
880 continue;
881 }
882 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300883 error = xfs_inobt_get_rec(cur, &r, &i);
884 if (error || i == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 xfs_buf_relse(agbp);
886 agbp = NULL;
887 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
888 cur = NULL;
889 agno++;
890 agino = 0;
891 continue;
892 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300893 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
894 buffer[bufidx].xi_startino =
895 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
896 buffer[bufidx].xi_alloccount =
897 XFS_INODES_PER_CHUNK - r.ir_freecount;
898 buffer[bufidx].xi_allocmask = ~r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 bufidx++;
900 left--;
901 if (bufidx == bcount) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000902 long written;
903 if (formatter(ubuffer, buffer, bufidx, &written)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 error = XFS_ERROR(EFAULT);
905 break;
906 }
Michal Marekfaa63e92007-07-11 11:10:19 +1000907 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 *count += bufidx;
909 bufidx = 0;
910 }
911 if (left) {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100912 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (error) {
914 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
915 cur = NULL;
916 xfs_buf_relse(agbp);
917 agbp = NULL;
918 /*
919 * The agino value has already been bumped.
920 * Just try to skip up to it.
921 */
922 agino += XFS_INODES_PER_CHUNK;
923 continue;
924 }
925 }
926 }
927 if (!error) {
928 if (bufidx) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000929 long written;
930 if (formatter(ubuffer, buffer, bufidx, &written))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 error = XFS_ERROR(EFAULT);
932 else
933 *count += bufidx;
934 }
935 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
936 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000937 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (cur)
939 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
940 XFS_BTREE_NOERROR));
941 if (agbp)
942 xfs_buf_relse(agbp);
943 return error;
944}