blob: b68f9107e26cce1e3eb7f4b08f272d493a9d8b06 [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 */
62 int error;
63
Nathan Scott745b1f472006-09-28 11:02:23 +100064 error = xfs_iget(mp, NULL, ino,
65 XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (error) {
67 *stat = BULKSTAT_RV_NOTHING;
68 return error;
69 }
70
71 ASSERT(ip != NULL);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +110072 ASSERT(ip->i_imap.im_blkno != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
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;
Christoph Hellwig8fab4512009-03-16 08:24:46 +010086 /*
87 * We are reading the atime from the Linux inode because the
88 * dinode might not be uptodate.
89 */
90 buf->bs_atime.tv_sec = VFS_I(ip)->i_atime.tv_sec;
91 buf->bs_atime.tv_nsec = VFS_I(ip)->i_atime.tv_nsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
93 buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
94 buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
95 buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
96 buf->bs_xflags = xfs_ip2xflags(ip);
97 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
98 buf->bs_extents = dic->di_nextents;
99 buf->bs_gen = dic->di_gen;
100 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
101 buf->bs_dmevmask = dic->di_dmevmask;
102 buf->bs_dmstate = dic->di_dmstate;
103 buf->bs_aextents = dic->di_anextents;
104
105 switch (dic->di_format) {
106 case XFS_DINODE_FMT_DEV:
107 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
108 buf->bs_blksize = BLKDEV_IOSIZE;
109 buf->bs_blocks = 0;
110 break;
111 case XFS_DINODE_FMT_LOCAL:
112 case XFS_DINODE_FMT_UUID:
113 buf->bs_rdev = 0;
114 buf->bs_blksize = mp->m_sb.sb_blocksize;
115 buf->bs_blocks = 0;
116 break;
117 case XFS_DINODE_FMT_EXTENTS:
118 case XFS_DINODE_FMT_BTREE:
119 buf->bs_rdev = 0;
120 buf->bs_blksize = mp->m_sb.sb_blocksize;
121 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
122 break;
123 }
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 xfs_iput(ip, XFS_ILOCK_SHARED);
126 return error;
127}
128
David Chinner7b073392008-04-10 12:24:04 +1000129STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130xfs_bulkstat_one_dinode(
131 xfs_mount_t *mp, /* mount point for filesystem */
132 xfs_ino_t ino, /* inode number to get data for */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100133 xfs_dinode_t *dic, /* dinode inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 xfs_bstat_t *buf) /* return buffer */
135{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
137 * The inode format changed when we moved the link count and
138 * made it 32 bits long. If this is an old format inode,
139 * convert it in memory to look like a new one. If it gets
140 * flushed to disk we will convert back before flushing or
141 * logging it. We zero out the new projid field and the old link
142 * count field. We'll handle clearing the pad field (the remains
143 * of the old uuid field) when we actually convert the inode to
144 * the new format. We don't change the version number so that we
145 * can distinguish this from a real new format inode.
146 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100147 if (dic->di_version == 1) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000148 buf->bs_nlink = be16_to_cpu(dic->di_onlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 buf->bs_projid = 0;
150 } else {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000151 buf->bs_nlink = be32_to_cpu(dic->di_nlink);
152 buf->bs_projid = be16_to_cpu(dic->di_projid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 buf->bs_ino = ino;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000156 buf->bs_mode = be16_to_cpu(dic->di_mode);
157 buf->bs_uid = be32_to_cpu(dic->di_uid);
158 buf->bs_gid = be32_to_cpu(dic->di_gid);
159 buf->bs_size = be64_to_cpu(dic->di_size);
160 buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec);
161 buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec);
162 buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec);
163 buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec);
164 buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec);
165 buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec);
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100166 buf->bs_xflags = xfs_dic2xflags(dic);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000167 buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog;
168 buf->bs_extents = be32_to_cpu(dic->di_nextents);
169 buf->bs_gen = be32_to_cpu(dic->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000171 buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask);
172 buf->bs_dmstate = be16_to_cpu(dic->di_dmstate);
173 buf->bs_aextents = be16_to_cpu(dic->di_anextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000175 switch (dic->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 case XFS_DINODE_FMT_DEV:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100177 buf->bs_rdev = xfs_dinode_get_rdev(dic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 buf->bs_blksize = BLKDEV_IOSIZE;
179 buf->bs_blocks = 0;
180 break;
181 case XFS_DINODE_FMT_LOCAL:
182 case XFS_DINODE_FMT_UUID:
183 buf->bs_rdev = 0;
184 buf->bs_blksize = mp->m_sb.sb_blocksize;
185 buf->bs_blocks = 0;
186 break;
187 case XFS_DINODE_FMT_EXTENTS:
188 case XFS_DINODE_FMT_BTREE:
189 buf->bs_rdev = 0;
190 buf->bs_blksize = mp->m_sb.sb_blocksize;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000191 buf->bs_blocks = be64_to_cpu(dic->di_nblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600196/* Return 0 on success or positive error */
Michal Marekfaa63e92007-07-11 11:10:19 +1000197STATIC int
198xfs_bulkstat_one_fmt(
199 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600200 int ubsize,
201 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000202 const xfs_bstat_t *buffer)
203{
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600204 if (ubsize < sizeof(*buffer))
205 return XFS_ERROR(ENOMEM);
Michal Marekfaa63e92007-07-11 11:10:19 +1000206 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600207 return XFS_ERROR(EFAULT);
208 if (ubused)
209 *ubused = sizeof(*buffer);
210 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * Return stat information for one inode.
215 * Return 0 if ok, else errno.
216 */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600217int /* error status */
218xfs_bulkstat_one_int(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 xfs_mount_t *mp, /* mount point for filesystem */
220 xfs_ino_t ino, /* inode number to get data for */
221 void __user *buffer, /* buffer to place output in */
222 int ubsize, /* size of buffer */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600223 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 xfs_daddr_t bno, /* starting bno of inode cluster */
225 int *ubused, /* bytes used by me */
226 void *dibuff, /* on-disk inode buffer */
227 int *stat) /* BULKSTAT_RV_... */
228{
229 xfs_bstat_t *buf; /* return buffer */
230 int error = 0; /* error value */
231 xfs_dinode_t *dip; /* dinode inode pointer */
232
233 dip = (xfs_dinode_t *)dibuff;
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000234 *stat = BULKSTAT_RV_NOTHING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000236 if (!buffer || xfs_internal_inum(mp, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
240
241 if (dip == NULL) {
242 /* We're not being passed a pointer to a dinode. This happens
243 * if BULKSTAT_FG_IGET is selected. Do the iget.
244 */
245 error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
246 if (error)
247 goto out_free;
248 } else {
249 xfs_bulkstat_one_dinode(mp, ino, dip, buf);
250 }
251
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600252 error = formatter(buffer, ubsize, ubused, buf);
253 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 *stat = BULKSTAT_RV_DIDONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 out_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000259 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return error;
261}
262
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600263int
264xfs_bulkstat_one(
265 xfs_mount_t *mp, /* mount point for filesystem */
266 xfs_ino_t ino, /* inode number to get data for */
267 void __user *buffer, /* buffer to place output in */
268 int ubsize, /* size of buffer */
269 void *private_data, /* my private data */
270 xfs_daddr_t bno, /* starting bno of inode cluster */
271 int *ubused, /* bytes used by me */
272 void *dibuff, /* on-disk inode buffer */
273 int *stat) /* BULKSTAT_RV_... */
274{
275 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
276 xfs_bulkstat_one_fmt, bno,
277 ubused, dibuff, stat);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
Nathan Scott8b56f082006-09-28 11:01:46 +1000281 * Test to see whether we can use the ondisk inode directly, based
282 * on the given bulkstat flags, filling in dipp accordingly.
283 * Returns zero if the inode is dodgey.
284 */
285STATIC int
286xfs_bulkstat_use_dinode(
287 xfs_mount_t *mp,
288 int flags,
289 xfs_buf_t *bp,
290 int clustidx,
291 xfs_dinode_t **dipp)
292{
293 xfs_dinode_t *dip;
294 unsigned int aformat;
295
296 *dipp = NULL;
297 if (!bp || (flags & BULKSTAT_FG_IGET))
298 return 1;
299 dip = (xfs_dinode_t *)
300 xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
Vlad Apostolov859d7182007-10-11 17:44:18 +1000301 /*
Vlad Apostolovc319b582007-11-23 16:27:51 +1100302 * Check the buffer containing the on-disk inode for di_mode == 0.
Vlad Apostolov859d7182007-10-11 17:44:18 +1000303 * This is to prevent xfs_bulkstat from picking up just reclaimed
304 * inodes that have their in-core state initialized but not flushed
305 * to disk yet. This is a temporary hack that would require a proper
306 * fix in the future.
307 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100308 if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
309 !XFS_DINODE_GOOD_VERSION(dip->di_version) ||
310 !dip->di_mode)
Nathan Scott8b56f082006-09-28 11:01:46 +1000311 return 0;
312 if (flags & BULKSTAT_FG_QUICK) {
313 *dipp = dip;
314 return 1;
315 }
316 /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100317 aformat = dip->di_aformat;
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100318 if ((XFS_DFORK_Q(dip) == 0) ||
Nathan Scott8b56f082006-09-28 11:01:46 +1000319 (aformat == XFS_DINODE_FMT_LOCAL) ||
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100320 (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_anextents)) {
Nathan Scott8b56f082006-09-28 11:01:46 +1000321 *dipp = dip;
322 return 1;
323 }
324 return 1;
325}
326
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100327#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
328
Nathan Scott8b56f082006-09-28 11:01:46 +1000329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * Return stat information in bulk (by-inode) for the filesystem.
331 */
332int /* error status */
333xfs_bulkstat(
334 xfs_mount_t *mp, /* mount point for filesystem */
335 xfs_ino_t *lastinop, /* last inode returned */
336 int *ubcountp, /* size of buffer/count returned */
337 bulkstat_one_pf formatter, /* func that'd fill a single buf */
338 void *private_data,/* private data for formatter */
339 size_t statstruct_size, /* sizeof struct filling */
340 char __user *ubuffer, /* buffer with inode stats */
341 int flags, /* defined in xfs_itable.h */
Nathan Scottc41564b2006-03-29 08:55:14 +1000342 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 xfs_agblock_t agbno=0;/* allocation group block number */
345 xfs_buf_t *agbp; /* agi header buffer */
346 xfs_agi_t *agi; /* agi header data */
347 xfs_agino_t agino; /* inode # in allocation group */
348 xfs_agnumber_t agno; /* allocation group number */
349 xfs_daddr_t bno; /* inode cluster start daddr */
350 int chunkidx; /* current index into inode chunk */
351 int clustidx; /* current index into inode cluster */
352 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
353 int end_of_ag; /* set if we've seen the ag end */
354 int error; /* error code */
355 int fmterror;/* bulkstat formatter result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int i; /* loop index */
357 int icount; /* count of inodes good in irbuf */
Nathan Scott215101c2006-09-28 11:04:43 +1000358 size_t irbsize; /* size of irec buffer in bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 xfs_ino_t ino; /* inode number (filesystem) */
Nathan Scott262750932006-09-28 11:02:03 +1000360 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
361 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
362 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100363 xfs_ino_t lastino; /* last inode number returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int nbcluster; /* # of blocks in a cluster */
365 int nicluster; /* # of inodes in a cluster */
366 int nimask; /* mask for inode clusters */
367 int nirbuf; /* size of irbuf */
368 int rval; /* return value error code */
369 int tmp; /* result value from btree calls */
370 int ubcount; /* size of user's buffer */
371 int ubleft; /* bytes left in user's buffer */
372 char __user *ubufp; /* pointer into user's buffer */
373 int ubelem; /* spaces used in user's buffer */
374 int ubused; /* bytes used by formatter */
375 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
376 xfs_dinode_t *dip; /* ptr into bp for specific inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /*
379 * Get the last inode value, see if there's nothing to do.
380 */
381 ino = (xfs_ino_t)*lastinop;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100382 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dip = NULL;
384 agno = XFS_INO_TO_AGNO(mp, ino);
385 agino = XFS_INO_TO_AGINO(mp, ino);
386 if (agno >= mp->m_sb.sb_agcount ||
387 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
388 *done = 1;
389 *ubcountp = 0;
390 return 0;
391 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100392 if (!ubcountp || *ubcountp <= 0) {
393 return EINVAL;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ubcount = *ubcountp; /* statstruct's */
396 ubleft = ubcount * statstruct_size; /* bytes */
397 *ubcountp = ubelem = 0;
398 *done = 0;
399 fmterror = 0;
400 ubufp = ubuffer;
401 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
402 mp->m_sb.sb_inopblock :
403 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
404 nimask = ~(nicluster - 1);
405 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100406 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4,
Nathan Scott77e46352006-09-28 11:03:27 +1000407 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000408 nirbuf = irbsize / sizeof(*irbuf);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Loop over the allocation groups, starting from the last
412 * inode returned; 0 means start of the allocation group.
413 */
414 rval = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100415 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
416 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 bp = NULL;
418 down_read(&mp->m_peraglock);
419 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
420 up_read(&mp->m_peraglock);
421 if (error) {
422 /*
423 * Skip this allocation group and go to the next one.
424 */
425 agno++;
426 agino = 0;
427 continue;
428 }
429 agi = XFS_BUF_TO_AGI(agbp);
430 /*
431 * Allocate and initialize a btree cursor for ialloc btree.
432 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100433 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 irbp = irbuf;
435 irbufend = irbuf + nirbuf;
436 end_of_ag = 0;
437 /*
438 * If we're returning in the middle of an allocation group,
439 * we need to get the remainder of the chunk we're in.
440 */
441 if (agino > 0) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300442 xfs_inobt_rec_incore_t r;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
445 * Lookup the inode chunk that this inode lives in.
446 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300447 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
448 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!error && /* no I/O error */
450 tmp && /* lookup succeeded */
451 /* got the record, should always work */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300452 !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 i == 1 &&
454 /* this is the right chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300455 agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* lastino was not last in chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300457 (chunkidx = agino - r.ir_startino + 1) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 XFS_INODES_PER_CHUNK &&
459 /* there are some left allocated */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600460 xfs_inobt_maskn(chunkidx,
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300461 XFS_INODES_PER_CHUNK - chunkidx) &
462 ~r.ir_free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Grab the chunk record. Mark all the
465 * uninteresting inodes (because they're
466 * before our start point) free.
467 */
468 for (i = 0; i < chunkidx; i++) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300469 if (XFS_INOBT_MASK(i) & ~r.ir_free)
470 r.ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300472 r.ir_free |= xfs_inobt_maskn(0, chunkidx);
473 irbp->ir_startino = r.ir_startino;
474 irbp->ir_freecount = r.ir_freecount;
475 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300477 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
478 icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else {
480 /*
481 * If any of those tests failed, bump the
482 * inode number (just in case).
483 */
484 agino++;
485 icount = 0;
486 }
487 /*
488 * In any case, increment to the next record.
489 */
490 if (!error)
Christoph Hellwig637aa502008-10-30 16:55:45 +1100491 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
493 /*
494 * Start of ag. Lookup the first inode chunk.
495 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300496 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 icount = 0;
498 }
499 /*
500 * Loop through inode btree records in this ag,
501 * until we run out of inodes or space in the buffer.
502 */
503 while (irbp < irbufend && icount < ubcount) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300504 xfs_inobt_rec_incore_t r;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * Loop as long as we're unable to read the
508 * inode btree.
509 */
510 while (error) {
511 agino += XFS_INODES_PER_CHUNK;
512 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100513 be32_to_cpu(agi->agi_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
Christoph Hellwig21875502009-08-31 20:58:21 -0300515 error = xfs_inobt_lookup(cur, agino,
516 XFS_LOOKUP_GE, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100517 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 /*
520 * If ran off the end of the ag either with an error,
521 * or the normal way, set end and stop collecting.
522 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300523 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 end_of_ag = 1;
525 break;
526 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300527
528 error = xfs_inobt_get_rec(cur, &r, &i);
529 if (error || i == 0) {
530 end_of_ag = 1;
531 break;
532 }
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
535 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000536 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300538 if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
Nathan Scott262750932006-09-28 11:02:03 +1000539 /*
540 * Loop over all clusters in the next chunk.
541 * Do a readahead if there are any allocated
542 * inodes in that cluster.
543 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300544 agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
545 for (chunkidx = 0;
Nathan Scott262750932006-09-28 11:02:03 +1000546 chunkidx < XFS_INODES_PER_CHUNK;
547 chunkidx += nicluster,
548 agbno += nbcluster) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300549 if (xfs_inobt_maskn(chunkidx, nicluster)
550 & ~r.ir_free)
Nathan Scott262750932006-09-28 11:02:03 +1000551 xfs_btree_reada_bufs(mp, agno,
552 agbno, nbcluster);
553 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300554 irbp->ir_startino = r.ir_startino;
555 irbp->ir_freecount = r.ir_freecount;
556 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300558 icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 /*
561 * Set agino to after this chunk and bump the cursor.
562 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300563 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100564 error = xfs_btree_increment(cur, 0, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100565 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 /*
568 * Drop the btree buffers and the agi buffer.
569 * We can't hold any of the locks these represent
570 * when calling iget.
571 */
572 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
573 xfs_buf_relse(agbp);
574 /*
575 * Now format all the good inodes into the user's buffer.
576 */
577 irbufend = irbp;
578 for (irbp = irbuf;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100579 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * Now process this chunk of inodes.
582 */
Nathan Scott262750932006-09-28 11:02:03 +1000583 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100584 XFS_BULKSTAT_UBLEFT(ubleft) &&
Nathan Scott262750932006-09-28 11:02:03 +1000585 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 chunkidx++, clustidx++, agino++) {
587 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
588 /*
589 * Recompute agbno if this is the
590 * first inode of the cluster.
591 *
592 * Careful with clustidx. There can be
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200593 * multiple clusters per chunk, a single
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * cluster per chunk or a cluster that has
595 * inodes represented from several different
596 * chunks (if blocksize is large).
597 *
598 * Because of this, the starting clustidx is
599 * initialized to zero in this loop but must
600 * later be reset after reading in the cluster
601 * buffer.
602 */
603 if ((chunkidx & (nicluster - 1)) == 0) {
604 agbno = XFS_AGINO_TO_AGBNO(mp,
Nathan Scott262750932006-09-28 11:02:03 +1000605 irbp->ir_startino) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ((chunkidx & nimask) >>
607 mp->m_sb.sb_inopblog);
608
Nathan Scott8b56f082006-09-28 11:01:46 +1000609 if (flags & (BULKSTAT_FG_QUICK |
610 BULKSTAT_FG_INLINE)) {
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100611 int offset;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ino = XFS_AGINO_TO_INO(mp, agno,
614 agino);
615 bno = XFS_AGB_TO_DADDR(mp, agno,
616 agbno);
617
618 /*
619 * Get the inode cluster buffer
620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (bp)
622 xfs_buf_relse(bp);
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100623
624 error = xfs_inotobp(mp, NULL, ino, &dip,
625 &bp, &offset,
Christoph Hellwigb48d8d62008-11-28 14:23:41 +1100626 XFS_IGET_BULKSTAT);
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!error)
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100629 clustidx = offset / mp->m_sb.sb_inodesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (XFS_TEST_ERROR(error != 0,
631 mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
632 XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
633 bp = NULL;
Nathan Scottb12dd342006-03-17 17:26:04 +1100634 ubleft = 0;
635 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637 }
638 }
639 }
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000640 ino = XFS_AGINO_TO_INO(mp, agno, agino);
641 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /*
643 * Skip if this inode is free.
644 */
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000645 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
646 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
650 * Count used inodes as free so we can tell
651 * when the chunk is used up.
652 */
Nathan Scott262750932006-09-28 11:02:03 +1000653 irbp->ir_freecount++;
Nathan Scott8b56f082006-09-28 11:01:46 +1000654 if (!xfs_bulkstat_use_dinode(mp, flags, bp,
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000655 clustidx, &dip)) {
656 lastino = ino;
Nathan Scott8b56f082006-09-28 11:01:46 +1000657 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000658 }
Nathan Scott8b56f082006-09-28 11:01:46 +1000659 /*
660 * If we need to do an iget, cannot hold bp.
661 * Drop it, until starting the next cluster.
662 */
663 if ((flags & BULKSTAT_FG_INLINE) && !dip) {
664 if (bp)
665 xfs_buf_relse(bp);
666 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
669 /*
670 * Get the inode and fill in a single buffer.
671 * BULKSTAT_FG_QUICK uses dip to fill it in.
672 * BULKSTAT_FG_IGET uses igets.
Nathan Scott8b56f082006-09-28 11:01:46 +1000673 * BULKSTAT_FG_INLINE uses dip if we have an
674 * inline attr fork, else igets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
676 * This is also used to count inodes/blks, etc
677 * in xfs_qm_quotacheck.
678 */
679 ubused = statstruct_size;
680 error = formatter(mp, ino, ubufp,
681 ubleft, private_data,
682 bno, &ubused, dip, &fmterror);
683 if (fmterror == BULKSTAT_RV_NOTHING) {
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100684 if (error && error != ENOENT &&
685 error != EINVAL) {
Vlad Apostolove132f542006-09-28 11:04:31 +1000686 ubleft = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100687 rval = error;
688 break;
689 }
690 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 continue;
692 }
693 if (fmterror == BULKSTAT_RV_GIVEUP) {
694 ubleft = 0;
695 ASSERT(error);
696 rval = error;
697 break;
698 }
699 if (ubufp)
700 ubufp += ubused;
701 ubleft -= ubused;
702 ubelem++;
703 lastino = ino;
704 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100705
706 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
709 if (bp)
710 xfs_buf_relse(bp);
711
712 /*
713 * Set up for the next loop iteration.
714 */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100715 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (end_of_ag) {
717 agno++;
718 agino = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100719 } else
720 agino = XFS_INO_TO_AGINO(mp, lastino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 } else
722 break;
723 }
724 /*
725 * Done, we're either out of filesystem or space to put the data.
726 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000727 kmem_free(irbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *ubcountp = ubelem;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100729 /*
730 * Found some inodes, return them now and return the error next time.
731 */
732 if (ubelem)
733 rval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (agno >= mp->m_sb.sb_agcount) {
735 /*
736 * If we ran out of filesystem, mark lastino as off
737 * the end of the filesystem, so the next call
738 * will return immediately.
739 */
740 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
741 *done = 1;
742 } else
743 *lastinop = (xfs_ino_t)lastino;
744
745 return rval;
746}
747
748/*
749 * Return stat information in bulk (by-inode) for the filesystem.
750 * Special case for non-sequential one inode bulkstat.
751 */
752int /* error status */
753xfs_bulkstat_single(
754 xfs_mount_t *mp, /* mount point for filesystem */
755 xfs_ino_t *lastinop, /* inode to return */
756 char __user *buffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000757 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 int count; /* count value for bulkstat call */
760 int error; /* return value */
761 xfs_ino_t ino; /* filesystem inode number */
762 int res; /* result from bs1 */
763
764 /*
765 * note that requesting valid inode numbers which are not allocated
766 * to inodes will most likely cause xfs_itobp to generate warning
767 * messages about bad magic numbers. This is ok. The fact that
768 * the inode isn't actually an inode is handled by the
769 * error check below. Done this way to make the usual case faster
770 * at the expense of the error case.
771 */
772
773 ino = (xfs_ino_t)*lastinop;
774 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
775 NULL, 0, NULL, NULL, &res);
776 if (error) {
777 /*
778 * Special case way failed, do it the "long" way
779 * to see if that works.
780 */
781 (*lastinop)--;
782 count = 1;
783 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
784 NULL, sizeof(xfs_bstat_t), buffer,
785 BULKSTAT_FG_IGET, done))
786 return error;
787 if (count == 0 || (xfs_ino_t)*lastinop != ino)
788 return error == EFSCORRUPTED ?
789 XFS_ERROR(EINVAL) : error;
790 else
791 return 0;
792 }
793 *done = 0;
794 return 0;
795}
796
Michal Marekfaa63e92007-07-11 11:10:19 +1000797int
798xfs_inumbers_fmt(
799 void __user *ubuffer, /* buffer to write to */
800 const xfs_inogrp_t *buffer, /* buffer to read from */
801 long count, /* # of elements to read */
802 long *written) /* # of bytes written */
803{
804 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
805 return -EFAULT;
806 *written = count * sizeof(*buffer);
807 return 0;
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810/*
811 * Return inode number table for the filesystem.
812 */
813int /* error status */
814xfs_inumbers(
815 xfs_mount_t *mp, /* mount point for filesystem */
816 xfs_ino_t *lastino, /* last inode returned */
817 int *count, /* size of buffer/count returned */
Michal Marekfaa63e92007-07-11 11:10:19 +1000818 void __user *ubuffer,/* buffer with inode descriptions */
819 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 xfs_buf_t *agbp;
822 xfs_agino_t agino;
823 xfs_agnumber_t agno;
824 int bcount;
825 xfs_inogrp_t *buffer;
826 int bufidx;
827 xfs_btree_cur_t *cur;
828 int error;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300829 xfs_inobt_rec_incore_t r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int i;
831 xfs_ino_t ino;
832 int left;
833 int tmp;
834
835 ino = (xfs_ino_t)*lastino;
836 agno = XFS_INO_TO_AGNO(mp, ino);
837 agino = XFS_INO_TO_AGINO(mp, ino);
838 left = *count;
839 *count = 0;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100840 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
842 error = bufidx = 0;
843 cur = NULL;
844 agbp = NULL;
845 while (left > 0 && agno < mp->m_sb.sb_agcount) {
846 if (agbp == NULL) {
847 down_read(&mp->m_peraglock);
848 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
849 up_read(&mp->m_peraglock);
850 if (error) {
851 /*
852 * If we can't read the AGI of this ag,
853 * then just skip to the next one.
854 */
855 ASSERT(cur == NULL);
856 agbp = NULL;
857 agno++;
858 agino = 0;
859 continue;
860 }
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100861 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Christoph Hellwig21875502009-08-31 20:58:21 -0300862 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
863 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (error) {
865 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
866 cur = NULL;
867 xfs_buf_relse(agbp);
868 agbp = NULL;
869 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200870 * Move up the last inode in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 * chunk. The lookup_ge will always get
872 * us the first inode in the next chunk.
873 */
874 agino += XFS_INODES_PER_CHUNK - 1;
875 continue;
876 }
877 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300878 error = xfs_inobt_get_rec(cur, &r, &i);
879 if (error || i == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 xfs_buf_relse(agbp);
881 agbp = NULL;
882 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
883 cur = NULL;
884 agno++;
885 agino = 0;
886 continue;
887 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300888 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
889 buffer[bufidx].xi_startino =
890 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
891 buffer[bufidx].xi_alloccount =
892 XFS_INODES_PER_CHUNK - r.ir_freecount;
893 buffer[bufidx].xi_allocmask = ~r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 bufidx++;
895 left--;
896 if (bufidx == bcount) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000897 long written;
898 if (formatter(ubuffer, buffer, bufidx, &written)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 error = XFS_ERROR(EFAULT);
900 break;
901 }
Michal Marekfaa63e92007-07-11 11:10:19 +1000902 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 *count += bufidx;
904 bufidx = 0;
905 }
906 if (left) {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100907 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (error) {
909 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
910 cur = NULL;
911 xfs_buf_relse(agbp);
912 agbp = NULL;
913 /*
914 * The agino value has already been bumped.
915 * Just try to skip up to it.
916 */
917 agino += XFS_INODES_PER_CHUNK;
918 continue;
919 }
920 }
921 }
922 if (!error) {
923 if (bufidx) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000924 long written;
925 if (formatter(ubuffer, buffer, bufidx, &written))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 error = XFS_ERROR(EFAULT);
927 else
928 *count += bufidx;
929 }
930 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
931 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000932 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (cur)
934 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
935 XFS_BTREE_NOERROR));
936 if (agbp)
937 xfs_buf_relse(agbp);
938 return error;
939}