blob: 940307a6a60b628a478e2970b0e1ca53d825fc57 [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;
109
110 switch (dic->di_format) {
111 case XFS_DINODE_FMT_DEV:
112 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
113 buf->bs_blksize = BLKDEV_IOSIZE;
114 buf->bs_blocks = 0;
115 break;
116 case XFS_DINODE_FMT_LOCAL:
117 case XFS_DINODE_FMT_UUID:
118 buf->bs_rdev = 0;
119 buf->bs_blksize = mp->m_sb.sb_blocksize;
120 buf->bs_blocks = 0;
121 break;
122 case XFS_DINODE_FMT_EXTENTS:
123 case XFS_DINODE_FMT_BTREE:
124 buf->bs_rdev = 0;
125 buf->bs_blksize = mp->m_sb.sb_blocksize;
126 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
127 break;
128 }
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 xfs_iput(ip, XFS_ILOCK_SHARED);
131 return error;
132}
133
David Chinner7b073392008-04-10 12:24:04 +1000134STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135xfs_bulkstat_one_dinode(
136 xfs_mount_t *mp, /* mount point for filesystem */
137 xfs_ino_t ino, /* inode number to get data for */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100138 xfs_dinode_t *dic, /* dinode inode pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 xfs_bstat_t *buf) /* return buffer */
140{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /*
142 * The inode format changed when we moved the link count and
143 * made it 32 bits long. If this is an old format inode,
144 * convert it in memory to look like a new one. If it gets
145 * flushed to disk we will convert back before flushing or
146 * logging it. We zero out the new projid field and the old link
147 * count field. We'll handle clearing the pad field (the remains
148 * of the old uuid field) when we actually convert the inode to
149 * the new format. We don't change the version number so that we
150 * can distinguish this from a real new format inode.
151 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100152 if (dic->di_version == 1) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000153 buf->bs_nlink = be16_to_cpu(dic->di_onlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 buf->bs_projid = 0;
155 } else {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000156 buf->bs_nlink = be32_to_cpu(dic->di_nlink);
157 buf->bs_projid = be16_to_cpu(dic->di_projid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
160 buf->bs_ino = ino;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000161 buf->bs_mode = be16_to_cpu(dic->di_mode);
162 buf->bs_uid = be32_to_cpu(dic->di_uid);
163 buf->bs_gid = be32_to_cpu(dic->di_gid);
164 buf->bs_size = be64_to_cpu(dic->di_size);
165 buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec);
166 buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec);
167 buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec);
168 buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec);
169 buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec);
170 buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec);
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100171 buf->bs_xflags = xfs_dic2xflags(dic);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000172 buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog;
173 buf->bs_extents = be32_to_cpu(dic->di_nextents);
174 buf->bs_gen = be32_to_cpu(dic->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000176 buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask);
177 buf->bs_dmstate = be16_to_cpu(dic->di_dmstate);
178 buf->bs_aextents = be16_to_cpu(dic->di_anextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000180 switch (dic->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 case XFS_DINODE_FMT_DEV:
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100182 buf->bs_rdev = xfs_dinode_get_rdev(dic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 buf->bs_blksize = BLKDEV_IOSIZE;
184 buf->bs_blocks = 0;
185 break;
186 case XFS_DINODE_FMT_LOCAL:
187 case XFS_DINODE_FMT_UUID:
188 buf->bs_rdev = 0;
189 buf->bs_blksize = mp->m_sb.sb_blocksize;
190 buf->bs_blocks = 0;
191 break;
192 case XFS_DINODE_FMT_EXTENTS:
193 case XFS_DINODE_FMT_BTREE:
194 buf->bs_rdev = 0;
195 buf->bs_blksize = mp->m_sb.sb_blocksize;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000196 buf->bs_blocks = be64_to_cpu(dic->di_nblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600201/* Return 0 on success or positive error */
Michal Marekfaa63e92007-07-11 11:10:19 +1000202STATIC int
203xfs_bulkstat_one_fmt(
204 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600205 int ubsize,
206 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000207 const xfs_bstat_t *buffer)
208{
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600209 if (ubsize < sizeof(*buffer))
210 return XFS_ERROR(ENOMEM);
Michal Marekfaa63e92007-07-11 11:10:19 +1000211 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600212 return XFS_ERROR(EFAULT);
213 if (ubused)
214 *ubused = sizeof(*buffer);
215 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * Return stat information for one inode.
220 * Return 0 if ok, else errno.
221 */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600222int /* error status */
223xfs_bulkstat_one_int(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 xfs_mount_t *mp, /* mount point for filesystem */
225 xfs_ino_t ino, /* inode number to get data for */
226 void __user *buffer, /* buffer to place output in */
227 int ubsize, /* size of buffer */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600228 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 xfs_daddr_t bno, /* starting bno of inode cluster */
230 int *ubused, /* bytes used by me */
231 void *dibuff, /* on-disk inode buffer */
232 int *stat) /* BULKSTAT_RV_... */
233{
234 xfs_bstat_t *buf; /* return buffer */
235 int error = 0; /* error value */
236 xfs_dinode_t *dip; /* dinode inode pointer */
237
238 dip = (xfs_dinode_t *)dibuff;
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000239 *stat = BULKSTAT_RV_NOTHING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000241 if (!buffer || xfs_internal_inum(mp, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
245
246 if (dip == NULL) {
247 /* We're not being passed a pointer to a dinode. This happens
248 * if BULKSTAT_FG_IGET is selected. Do the iget.
249 */
250 error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
251 if (error)
252 goto out_free;
253 } else {
254 xfs_bulkstat_one_dinode(mp, ino, dip, buf);
255 }
256
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600257 error = formatter(buffer, ubsize, ubused, buf);
258 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 *stat = BULKSTAT_RV_DIDONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 out_free:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000264 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return error;
266}
267
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600268int
269xfs_bulkstat_one(
270 xfs_mount_t *mp, /* mount point for filesystem */
271 xfs_ino_t ino, /* inode number to get data for */
272 void __user *buffer, /* buffer to place output in */
273 int ubsize, /* size of buffer */
274 void *private_data, /* my private data */
275 xfs_daddr_t bno, /* starting bno of inode cluster */
276 int *ubused, /* bytes used by me */
277 void *dibuff, /* on-disk inode buffer */
278 int *stat) /* BULKSTAT_RV_... */
279{
280 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
281 xfs_bulkstat_one_fmt, bno,
282 ubused, dibuff, stat);
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
Nathan Scott8b56f082006-09-28 11:01:46 +1000286 * Test to see whether we can use the ondisk inode directly, based
287 * on the given bulkstat flags, filling in dipp accordingly.
288 * Returns zero if the inode is dodgey.
289 */
290STATIC int
291xfs_bulkstat_use_dinode(
292 xfs_mount_t *mp,
293 int flags,
294 xfs_buf_t *bp,
295 int clustidx,
296 xfs_dinode_t **dipp)
297{
298 xfs_dinode_t *dip;
299 unsigned int aformat;
300
301 *dipp = NULL;
302 if (!bp || (flags & BULKSTAT_FG_IGET))
303 return 1;
304 dip = (xfs_dinode_t *)
305 xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
Vlad Apostolov859d7182007-10-11 17:44:18 +1000306 /*
Vlad Apostolovc319b582007-11-23 16:27:51 +1100307 * Check the buffer containing the on-disk inode for di_mode == 0.
Vlad Apostolov859d7182007-10-11 17:44:18 +1000308 * This is to prevent xfs_bulkstat from picking up just reclaimed
309 * inodes that have their in-core state initialized but not flushed
310 * to disk yet. This is a temporary hack that would require a proper
311 * fix in the future.
312 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100313 if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
314 !XFS_DINODE_GOOD_VERSION(dip->di_version) ||
315 !dip->di_mode)
Nathan Scott8b56f082006-09-28 11:01:46 +1000316 return 0;
317 if (flags & BULKSTAT_FG_QUICK) {
318 *dipp = dip;
319 return 1;
320 }
321 /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100322 aformat = dip->di_aformat;
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100323 if ((XFS_DFORK_Q(dip) == 0) ||
Nathan Scott8b56f082006-09-28 11:01:46 +1000324 (aformat == XFS_DINODE_FMT_LOCAL) ||
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100325 (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_anextents)) {
Nathan Scott8b56f082006-09-28 11:01:46 +1000326 *dipp = dip;
327 return 1;
328 }
329 return 1;
330}
331
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100332#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
333
Nathan Scott8b56f082006-09-28 11:01:46 +1000334/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * Return stat information in bulk (by-inode) for the filesystem.
336 */
337int /* error status */
338xfs_bulkstat(
339 xfs_mount_t *mp, /* mount point for filesystem */
340 xfs_ino_t *lastinop, /* last inode returned */
341 int *ubcountp, /* size of buffer/count returned */
342 bulkstat_one_pf formatter, /* func that'd fill a single buf */
343 void *private_data,/* private data for formatter */
344 size_t statstruct_size, /* sizeof struct filling */
345 char __user *ubuffer, /* buffer with inode stats */
346 int flags, /* defined in xfs_itable.h */
Nathan Scottc41564b2006-03-29 08:55:14 +1000347 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 xfs_agblock_t agbno=0;/* allocation group block number */
350 xfs_buf_t *agbp; /* agi header buffer */
351 xfs_agi_t *agi; /* agi header data */
352 xfs_agino_t agino; /* inode # in allocation group */
353 xfs_agnumber_t agno; /* allocation group number */
354 xfs_daddr_t bno; /* inode cluster start daddr */
355 int chunkidx; /* current index into inode chunk */
356 int clustidx; /* current index into inode cluster */
357 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
358 int end_of_ag; /* set if we've seen the ag end */
359 int error; /* error code */
360 int fmterror;/* bulkstat formatter result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int i; /* loop index */
362 int icount; /* count of inodes good in irbuf */
Nathan Scott215101c2006-09-28 11:04:43 +1000363 size_t irbsize; /* size of irec buffer in bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 xfs_ino_t ino; /* inode number (filesystem) */
Nathan Scott262750932006-09-28 11:02:03 +1000365 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
366 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
367 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100368 xfs_ino_t lastino; /* last inode number returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int nbcluster; /* # of blocks in a cluster */
370 int nicluster; /* # of inodes in a cluster */
371 int nimask; /* mask for inode clusters */
372 int nirbuf; /* size of irbuf */
373 int rval; /* return value error code */
374 int tmp; /* result value from btree calls */
375 int ubcount; /* size of user's buffer */
376 int ubleft; /* bytes left in user's buffer */
377 char __user *ubufp; /* pointer into user's buffer */
378 int ubelem; /* spaces used in user's buffer */
379 int ubused; /* bytes used by formatter */
380 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
381 xfs_dinode_t *dip; /* ptr into bp for specific inode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /*
384 * Get the last inode value, see if there's nothing to do.
385 */
386 ino = (xfs_ino_t)*lastinop;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100387 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 dip = NULL;
389 agno = XFS_INO_TO_AGNO(mp, ino);
390 agino = XFS_INO_TO_AGINO(mp, ino);
391 if (agno >= mp->m_sb.sb_agcount ||
392 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
393 *done = 1;
394 *ubcountp = 0;
395 return 0;
396 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100397 if (!ubcountp || *ubcountp <= 0) {
398 return EINVAL;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 ubcount = *ubcountp; /* statstruct's */
401 ubleft = ubcount * statstruct_size; /* bytes */
402 *ubcountp = ubelem = 0;
403 *done = 0;
404 fmterror = 0;
405 ubufp = ubuffer;
406 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
407 mp->m_sb.sb_inopblock :
408 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
409 nimask = ~(nicluster - 1);
410 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100411 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4,
Nathan Scott77e46352006-09-28 11:03:27 +1000412 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000413 nirbuf = irbsize / sizeof(*irbuf);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Loop over the allocation groups, starting from the last
417 * inode returned; 0 means start of the allocation group.
418 */
419 rval = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100420 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
421 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (error) {
425 /*
426 * Skip this allocation group and go to the next one.
427 */
428 agno++;
429 agino = 0;
430 continue;
431 }
432 agi = XFS_BUF_TO_AGI(agbp);
433 /*
434 * Allocate and initialize a btree cursor for ialloc btree.
435 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100436 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 irbp = irbuf;
438 irbufend = irbuf + nirbuf;
439 end_of_ag = 0;
440 /*
441 * If we're returning in the middle of an allocation group,
442 * we need to get the remainder of the chunk we're in.
443 */
444 if (agino > 0) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300445 xfs_inobt_rec_incore_t r;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * Lookup the inode chunk that this inode lives in.
449 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300450 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
451 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!error && /* no I/O error */
453 tmp && /* lookup succeeded */
454 /* got the record, should always work */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300455 !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 i == 1 &&
457 /* this is the right chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300458 agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* lastino was not last in chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300460 (chunkidx = agino - r.ir_startino + 1) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 XFS_INODES_PER_CHUNK &&
462 /* there are some left allocated */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600463 xfs_inobt_maskn(chunkidx,
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300464 XFS_INODES_PER_CHUNK - chunkidx) &
465 ~r.ir_free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
467 * Grab the chunk record. Mark all the
468 * uninteresting inodes (because they're
469 * before our start point) free.
470 */
471 for (i = 0; i < chunkidx; i++) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300472 if (XFS_INOBT_MASK(i) & ~r.ir_free)
473 r.ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300475 r.ir_free |= xfs_inobt_maskn(0, chunkidx);
476 irbp->ir_startino = r.ir_startino;
477 irbp->ir_freecount = r.ir_freecount;
478 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300480 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
481 icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } else {
483 /*
484 * If any of those tests failed, bump the
485 * inode number (just in case).
486 */
487 agino++;
488 icount = 0;
489 }
490 /*
491 * In any case, increment to the next record.
492 */
493 if (!error)
Christoph Hellwig637aa502008-10-30 16:55:45 +1100494 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else {
496 /*
497 * Start of ag. Lookup the first inode chunk.
498 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300499 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 icount = 0;
501 }
502 /*
503 * Loop through inode btree records in this ag,
504 * until we run out of inodes or space in the buffer.
505 */
506 while (irbp < irbufend && icount < ubcount) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300507 xfs_inobt_rec_incore_t r;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /*
510 * Loop as long as we're unable to read the
511 * inode btree.
512 */
513 while (error) {
514 agino += XFS_INODES_PER_CHUNK;
515 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100516 be32_to_cpu(agi->agi_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
Christoph Hellwig21875502009-08-31 20:58:21 -0300518 error = xfs_inobt_lookup(cur, agino,
519 XFS_LOOKUP_GE, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100520 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 /*
523 * If ran off the end of the ag either with an error,
524 * or the normal way, set end and stop collecting.
525 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300526 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 end_of_ag = 1;
528 break;
529 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300530
531 error = xfs_inobt_get_rec(cur, &r, &i);
532 if (error || i == 0) {
533 end_of_ag = 1;
534 break;
535 }
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000539 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300541 if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
Nathan Scott262750932006-09-28 11:02:03 +1000542 /*
543 * Loop over all clusters in the next chunk.
544 * Do a readahead if there are any allocated
545 * inodes in that cluster.
546 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300547 agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
548 for (chunkidx = 0;
Nathan Scott262750932006-09-28 11:02:03 +1000549 chunkidx < XFS_INODES_PER_CHUNK;
550 chunkidx += nicluster,
551 agbno += nbcluster) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300552 if (xfs_inobt_maskn(chunkidx, nicluster)
553 & ~r.ir_free)
Nathan Scott262750932006-09-28 11:02:03 +1000554 xfs_btree_reada_bufs(mp, agno,
555 agbno, nbcluster);
556 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300557 irbp->ir_startino = r.ir_startino;
558 irbp->ir_freecount = r.ir_freecount;
559 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300561 icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 /*
564 * Set agino to after this chunk and bump the cursor.
565 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300566 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100567 error = xfs_btree_increment(cur, 0, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100568 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 /*
571 * Drop the btree buffers and the agi buffer.
572 * We can't hold any of the locks these represent
573 * when calling iget.
574 */
575 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
576 xfs_buf_relse(agbp);
577 /*
578 * Now format all the good inodes into the user's buffer.
579 */
580 irbufend = irbp;
581 for (irbp = irbuf;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100582 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * Now process this chunk of inodes.
585 */
Nathan Scott262750932006-09-28 11:02:03 +1000586 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100587 XFS_BULKSTAT_UBLEFT(ubleft) &&
Nathan Scott262750932006-09-28 11:02:03 +1000588 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 chunkidx++, clustidx++, agino++) {
590 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
591 /*
592 * Recompute agbno if this is the
593 * first inode of the cluster.
594 *
595 * Careful with clustidx. There can be
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200596 * multiple clusters per chunk, a single
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * cluster per chunk or a cluster that has
598 * inodes represented from several different
599 * chunks (if blocksize is large).
600 *
601 * Because of this, the starting clustidx is
602 * initialized to zero in this loop but must
603 * later be reset after reading in the cluster
604 * buffer.
605 */
606 if ((chunkidx & (nicluster - 1)) == 0) {
607 agbno = XFS_AGINO_TO_AGBNO(mp,
Nathan Scott262750932006-09-28 11:02:03 +1000608 irbp->ir_startino) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 ((chunkidx & nimask) >>
610 mp->m_sb.sb_inopblog);
611
Nathan Scott8b56f082006-09-28 11:01:46 +1000612 if (flags & (BULKSTAT_FG_QUICK |
613 BULKSTAT_FG_INLINE)) {
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100614 int offset;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ino = XFS_AGINO_TO_INO(mp, agno,
617 agino);
618 bno = XFS_AGB_TO_DADDR(mp, agno,
619 agbno);
620
621 /*
622 * Get the inode cluster buffer
623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (bp)
625 xfs_buf_relse(bp);
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100626
627 error = xfs_inotobp(mp, NULL, ino, &dip,
628 &bp, &offset,
Christoph Hellwigb48d8d62008-11-28 14:23:41 +1100629 XFS_IGET_BULKSTAT);
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!error)
Christoph Hellwigc679eef2008-10-30 18:04:13 +1100632 clustidx = offset / mp->m_sb.sb_inodesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (XFS_TEST_ERROR(error != 0,
634 mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
635 XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
636 bp = NULL;
Nathan Scottb12dd342006-03-17 17:26:04 +1100637 ubleft = 0;
638 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 }
641 }
642 }
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000643 ino = XFS_AGINO_TO_INO(mp, agno, agino);
644 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
646 * Skip if this inode is free.
647 */
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000648 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
649 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * Count used inodes as free so we can tell
654 * when the chunk is used up.
655 */
Nathan Scott262750932006-09-28 11:02:03 +1000656 irbp->ir_freecount++;
Nathan Scott8b56f082006-09-28 11:01:46 +1000657 if (!xfs_bulkstat_use_dinode(mp, flags, bp,
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000658 clustidx, &dip)) {
659 lastino = ino;
Nathan Scott8b56f082006-09-28 11:01:46 +1000660 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000661 }
Nathan Scott8b56f082006-09-28 11:01:46 +1000662 /*
663 * If we need to do an iget, cannot hold bp.
664 * Drop it, until starting the next cluster.
665 */
666 if ((flags & BULKSTAT_FG_INLINE) && !dip) {
667 if (bp)
668 xfs_buf_relse(bp);
669 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 /*
673 * Get the inode and fill in a single buffer.
674 * BULKSTAT_FG_QUICK uses dip to fill it in.
675 * BULKSTAT_FG_IGET uses igets.
Nathan Scott8b56f082006-09-28 11:01:46 +1000676 * BULKSTAT_FG_INLINE uses dip if we have an
677 * inline attr fork, else igets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
679 * This is also used to count inodes/blks, etc
680 * in xfs_qm_quotacheck.
681 */
682 ubused = statstruct_size;
683 error = formatter(mp, ino, ubufp,
684 ubleft, private_data,
685 bno, &ubused, dip, &fmterror);
686 if (fmterror == BULKSTAT_RV_NOTHING) {
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100687 if (error && error != ENOENT &&
688 error != EINVAL) {
Vlad Apostolove132f542006-09-28 11:04:31 +1000689 ubleft = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100690 rval = error;
691 break;
692 }
693 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 continue;
695 }
696 if (fmterror == BULKSTAT_RV_GIVEUP) {
697 ubleft = 0;
698 ASSERT(error);
699 rval = error;
700 break;
701 }
702 if (ubufp)
703 ubufp += ubused;
704 ubleft -= ubused;
705 ubelem++;
706 lastino = ino;
707 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100708
709 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 if (bp)
713 xfs_buf_relse(bp);
714
715 /*
716 * Set up for the next loop iteration.
717 */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100718 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (end_of_ag) {
720 agno++;
721 agino = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100722 } else
723 agino = XFS_INO_TO_AGINO(mp, lastino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 } else
725 break;
726 }
727 /*
728 * Done, we're either out of filesystem or space to put the data.
729 */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000730 kmem_free(irbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *ubcountp = ubelem;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100732 /*
733 * Found some inodes, return them now and return the error next time.
734 */
735 if (ubelem)
736 rval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (agno >= mp->m_sb.sb_agcount) {
738 /*
739 * If we ran out of filesystem, mark lastino as off
740 * the end of the filesystem, so the next call
741 * will return immediately.
742 */
743 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
744 *done = 1;
745 } else
746 *lastinop = (xfs_ino_t)lastino;
747
748 return rval;
749}
750
751/*
752 * Return stat information in bulk (by-inode) for the filesystem.
753 * Special case for non-sequential one inode bulkstat.
754 */
755int /* error status */
756xfs_bulkstat_single(
757 xfs_mount_t *mp, /* mount point for filesystem */
758 xfs_ino_t *lastinop, /* inode to return */
759 char __user *buffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000760 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 int count; /* count value for bulkstat call */
763 int error; /* return value */
764 xfs_ino_t ino; /* filesystem inode number */
765 int res; /* result from bs1 */
766
767 /*
768 * note that requesting valid inode numbers which are not allocated
769 * to inodes will most likely cause xfs_itobp to generate warning
770 * messages about bad magic numbers. This is ok. The fact that
771 * the inode isn't actually an inode is handled by the
772 * error check below. Done this way to make the usual case faster
773 * at the expense of the error case.
774 */
775
776 ino = (xfs_ino_t)*lastinop;
777 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
778 NULL, 0, NULL, NULL, &res);
779 if (error) {
780 /*
781 * Special case way failed, do it the "long" way
782 * to see if that works.
783 */
784 (*lastinop)--;
785 count = 1;
786 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
787 NULL, sizeof(xfs_bstat_t), buffer,
788 BULKSTAT_FG_IGET, done))
789 return error;
790 if (count == 0 || (xfs_ino_t)*lastinop != ino)
791 return error == EFSCORRUPTED ?
792 XFS_ERROR(EINVAL) : error;
793 else
794 return 0;
795 }
796 *done = 0;
797 return 0;
798}
799
Michal Marekfaa63e92007-07-11 11:10:19 +1000800int
801xfs_inumbers_fmt(
802 void __user *ubuffer, /* buffer to write to */
803 const xfs_inogrp_t *buffer, /* buffer to read from */
804 long count, /* # of elements to read */
805 long *written) /* # of bytes written */
806{
807 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
808 return -EFAULT;
809 *written = count * sizeof(*buffer);
810 return 0;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/*
814 * Return inode number table for the filesystem.
815 */
816int /* error status */
817xfs_inumbers(
818 xfs_mount_t *mp, /* mount point for filesystem */
819 xfs_ino_t *lastino, /* last inode returned */
820 int *count, /* size of buffer/count returned */
Michal Marekfaa63e92007-07-11 11:10:19 +1000821 void __user *ubuffer,/* buffer with inode descriptions */
822 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 xfs_buf_t *agbp;
825 xfs_agino_t agino;
826 xfs_agnumber_t agno;
827 int bcount;
828 xfs_inogrp_t *buffer;
829 int bufidx;
830 xfs_btree_cur_t *cur;
831 int error;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300832 xfs_inobt_rec_incore_t r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int i;
834 xfs_ino_t ino;
835 int left;
836 int tmp;
837
838 ino = (xfs_ino_t)*lastino;
839 agno = XFS_INO_TO_AGNO(mp, ino);
840 agino = XFS_INO_TO_AGINO(mp, ino);
841 left = *count;
842 *count = 0;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100843 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
845 error = bufidx = 0;
846 cur = NULL;
847 agbp = NULL;
848 while (left > 0 && agno < mp->m_sb.sb_agcount) {
849 if (agbp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (error) {
852 /*
853 * If we can't read the AGI of this ag,
854 * then just skip to the next one.
855 */
856 ASSERT(cur == NULL);
857 agbp = NULL;
858 agno++;
859 agino = 0;
860 continue;
861 }
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100862 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Christoph Hellwig21875502009-08-31 20:58:21 -0300863 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
864 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (error) {
866 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
867 cur = NULL;
868 xfs_buf_relse(agbp);
869 agbp = NULL;
870 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200871 * Move up the last inode in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * chunk. The lookup_ge will always get
873 * us the first inode in the next chunk.
874 */
875 agino += XFS_INODES_PER_CHUNK - 1;
876 continue;
877 }
878 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300879 error = xfs_inobt_get_rec(cur, &r, &i);
880 if (error || i == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 xfs_buf_relse(agbp);
882 agbp = NULL;
883 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
884 cur = NULL;
885 agno++;
886 agino = 0;
887 continue;
888 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300889 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
890 buffer[bufidx].xi_startino =
891 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
892 buffer[bufidx].xi_alloccount =
893 XFS_INODES_PER_CHUNK - r.ir_freecount;
894 buffer[bufidx].xi_allocmask = ~r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 bufidx++;
896 left--;
897 if (bufidx == bcount) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000898 long written;
899 if (formatter(ubuffer, buffer, bufidx, &written)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 error = XFS_ERROR(EFAULT);
901 break;
902 }
Michal Marekfaa63e92007-07-11 11:10:19 +1000903 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 *count += bufidx;
905 bufidx = 0;
906 }
907 if (left) {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100908 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (error) {
910 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
911 cur = NULL;
912 xfs_buf_relse(agbp);
913 agbp = NULL;
914 /*
915 * The agino value has already been bumped.
916 * Just try to skip up to it.
917 */
918 agino += XFS_INODES_PER_CHUNK;
919 continue;
920 }
921 }
922 }
923 if (!error) {
924 if (bufidx) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000925 long written;
926 if (formatter(ubuffer, buffer, bufidx, &written))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 error = XFS_ERROR(EFAULT);
928 else
929 *count += bufidx;
930 }
931 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
932 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000933 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (cur)
935 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
936 XFS_BTREE_NOERROR));
937 if (agbp)
938 xfs_buf_relse(agbp);
939 return error;
940}