blob: 200dc6fc8cc53e07502dcac2d1b884ffaa60a81a [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_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_ialloc.h"
34#include "xfs_itable.h"
35#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric Sandeend96f8f82009-07-02 00:09:33 -050038STATIC int
Vlad Apostolov6f1f2162006-09-28 11:06:15 +100039xfs_internal_inum(
40 xfs_mount_t *mp,
41 xfs_ino_t ino)
42{
43 return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
Eric Sandeen62118702008-03-06 13:44:28 +110044 (xfs_sb_version_hasquota(&mp->m_sb) &&
Vlad Apostolov6f1f2162006-09-28 11:06:15 +100045 (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino)));
46}
47
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100048/*
49 * Return stat information for one inode.
50 * Return 0 if ok, else errno.
51 */
52int
53xfs_bulkstat_one_int(
54 struct xfs_mount *mp, /* mount point for filesystem */
55 xfs_ino_t ino, /* inode to get data for */
56 void __user *buffer, /* buffer to place output in */
57 int ubsize, /* size of buffer */
58 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100059 int *ubused, /* bytes used by me */
60 int *stat) /* BULKSTAT_RV_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100062 struct xfs_icdinode *dic; /* dinode core info pointer */
63 struct xfs_inode *ip; /* incore inode pointer */
64 struct inode *inode;
65 struct xfs_bstat *buf; /* return buffer */
66 int error = 0; /* error value */
67
68 *stat = BULKSTAT_RV_NOTHING;
69
70 if (!buffer || xfs_internal_inum(mp, ino))
71 return XFS_ERROR(EINVAL);
72
73 buf = kmem_alloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
74 if (!buf)
75 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Nathan Scott745b1f472006-09-28 11:02:23 +100077 error = xfs_iget(mp, NULL, ino,
Dave Chinner7b6259e2010-06-24 11:35:17 +100078 XFS_IGET_UNTRUSTED, XFS_ILOCK_SHARED, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (error) {
80 *stat = BULKSTAT_RV_NOTHING;
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100081 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
84 ASSERT(ip != NULL);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +110085 ASSERT(ip->i_imap.im_blkno != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 dic = &ip->i_d;
Christoph Hellwigf9581b12009-10-06 20:29:26 +000088 inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 /* xfs_iget returns the following without needing
91 * further change.
92 */
93 buf->bs_nlink = dic->di_nlink;
94 buf->bs_projid = dic->di_projid;
95 buf->bs_ino = ino;
96 buf->bs_mode = dic->di_mode;
97 buf->bs_uid = dic->di_uid;
98 buf->bs_gid = dic->di_gid;
99 buf->bs_size = dic->di_size;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000100
Christoph Hellwig8fab4512009-03-16 08:24:46 +0100101 /*
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000102 * We need to read the timestamps from the Linux inode because
103 * the VFS keeps writing directly into the inode structure instead
104 * of telling us about the updates.
Christoph Hellwig8fab4512009-03-16 08:24:46 +0100105 */
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000106 buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
107 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
108 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
109 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
110 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
111 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 buf->bs_xflags = xfs_ip2xflags(ip);
114 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
115 buf->bs_extents = dic->di_nextents;
116 buf->bs_gen = dic->di_gen;
117 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
118 buf->bs_dmevmask = dic->di_dmevmask;
119 buf->bs_dmstate = dic->di_dmstate;
120 buf->bs_aextents = dic->di_anextents;
Dave Chinner07000ee2010-03-05 04:41:14 +0000121 buf->bs_forkoff = XFS_IFORK_BOFF(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 switch (dic->di_format) {
124 case XFS_DINODE_FMT_DEV:
125 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
126 buf->bs_blksize = BLKDEV_IOSIZE;
127 buf->bs_blocks = 0;
128 break;
129 case XFS_DINODE_FMT_LOCAL:
130 case XFS_DINODE_FMT_UUID:
131 buf->bs_rdev = 0;
132 buf->bs_blksize = mp->m_sb.sb_blocksize;
133 buf->bs_blocks = 0;
134 break;
135 case XFS_DINODE_FMT_EXTENTS:
136 case XFS_DINODE_FMT_BTREE:
137 buf->bs_rdev = 0;
138 buf->bs_blksize = mp->m_sb.sb_blocksize;
139 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
140 break;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 xfs_iput(ip, XFS_ILOCK_SHARED);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000143
144 error = formatter(buffer, ubsize, ubused, buf);
145
146 if (!error)
147 *stat = BULKSTAT_RV_DIDONE;
148
149 out_free:
150 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return error;
152}
153
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600154/* Return 0 on success or positive error */
Michal Marekfaa63e92007-07-11 11:10:19 +1000155STATIC int
156xfs_bulkstat_one_fmt(
157 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600158 int ubsize,
159 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000160 const xfs_bstat_t *buffer)
161{
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600162 if (ubsize < sizeof(*buffer))
163 return XFS_ERROR(ENOMEM);
Michal Marekfaa63e92007-07-11 11:10:19 +1000164 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600165 return XFS_ERROR(EFAULT);
166 if (ubused)
167 *ubused = sizeof(*buffer);
168 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000169}
170
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600171int
172xfs_bulkstat_one(
173 xfs_mount_t *mp, /* mount point for filesystem */
174 xfs_ino_t ino, /* inode number to get data for */
175 void __user *buffer, /* buffer to place output in */
176 int ubsize, /* size of buffer */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600177 int *ubused, /* bytes used by me */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600178 int *stat) /* BULKSTAT_RV_... */
179{
180 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
Dave Chinner7b6259e2010-06-24 11:35:17 +1000181 xfs_bulkstat_one_fmt, ubused, stat);
Nathan Scott8b56f082006-09-28 11:01:46 +1000182}
183
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100184#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
185
Nathan Scott8b56f082006-09-28 11:01:46 +1000186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Return stat information in bulk (by-inode) for the filesystem.
188 */
189int /* error status */
190xfs_bulkstat(
191 xfs_mount_t *mp, /* mount point for filesystem */
192 xfs_ino_t *lastinop, /* last inode returned */
193 int *ubcountp, /* size of buffer/count returned */
194 bulkstat_one_pf formatter, /* func that'd fill a single buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 size_t statstruct_size, /* sizeof struct filling */
196 char __user *ubuffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000197 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 xfs_agblock_t agbno=0;/* allocation group block number */
200 xfs_buf_t *agbp; /* agi header buffer */
201 xfs_agi_t *agi; /* agi header data */
202 xfs_agino_t agino; /* inode # in allocation group */
203 xfs_agnumber_t agno; /* allocation group number */
204 xfs_daddr_t bno; /* inode cluster start daddr */
205 int chunkidx; /* current index into inode chunk */
206 int clustidx; /* current index into inode cluster */
207 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
208 int end_of_ag; /* set if we've seen the ag end */
209 int error; /* error code */
210 int fmterror;/* bulkstat formatter result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int i; /* loop index */
212 int icount; /* count of inodes good in irbuf */
Nathan Scott215101c2006-09-28 11:04:43 +1000213 size_t irbsize; /* size of irec buffer in bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 xfs_ino_t ino; /* inode number (filesystem) */
Nathan Scott262750932006-09-28 11:02:03 +1000215 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
216 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
217 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100218 xfs_ino_t lastino; /* last inode number returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int nbcluster; /* # of blocks in a cluster */
220 int nicluster; /* # of inodes in a cluster */
221 int nimask; /* mask for inode clusters */
222 int nirbuf; /* size of irbuf */
223 int rval; /* return value error code */
224 int tmp; /* result value from btree calls */
225 int ubcount; /* size of user's buffer */
226 int ubleft; /* bytes left in user's buffer */
227 char __user *ubufp; /* pointer into user's buffer */
228 int ubelem; /* spaces used in user's buffer */
229 int ubused; /* bytes used by formatter */
230 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /*
233 * Get the last inode value, see if there's nothing to do.
234 */
235 ino = (xfs_ino_t)*lastinop;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100236 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 agno = XFS_INO_TO_AGNO(mp, ino);
238 agino = XFS_INO_TO_AGINO(mp, ino);
239 if (agno >= mp->m_sb.sb_agcount ||
240 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
241 *done = 1;
242 *ubcountp = 0;
243 return 0;
244 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100245 if (!ubcountp || *ubcountp <= 0) {
246 return EINVAL;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ubcount = *ubcountp; /* statstruct's */
249 ubleft = ubcount * statstruct_size; /* bytes */
250 *ubcountp = ubelem = 0;
251 *done = 0;
252 fmterror = 0;
253 ubufp = ubuffer;
254 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
255 mp->m_sb.sb_inopblock :
256 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
257 nimask = ~(nicluster - 1);
258 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000259 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
260 if (!irbuf)
261 return ENOMEM;
262
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000263 nirbuf = irbsize / sizeof(*irbuf);
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
266 * Loop over the allocation groups, starting from the last
267 * inode returned; 0 means start of the allocation group.
268 */
269 rval = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100270 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
271 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (error) {
275 /*
276 * Skip this allocation group and go to the next one.
277 */
278 agno++;
279 agino = 0;
280 continue;
281 }
282 agi = XFS_BUF_TO_AGI(agbp);
283 /*
284 * Allocate and initialize a btree cursor for ialloc btree.
285 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100286 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 irbp = irbuf;
288 irbufend = irbuf + nirbuf;
289 end_of_ag = 0;
290 /*
291 * If we're returning in the middle of an allocation group,
292 * we need to get the remainder of the chunk we're in.
293 */
294 if (agino > 0) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300295 xfs_inobt_rec_incore_t r;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /*
298 * Lookup the inode chunk that this inode lives in.
299 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300300 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
301 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!error && /* no I/O error */
303 tmp && /* lookup succeeded */
304 /* got the record, should always work */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300305 !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 i == 1 &&
307 /* this is the right chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300308 agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* lastino was not last in chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300310 (chunkidx = agino - r.ir_startino + 1) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 XFS_INODES_PER_CHUNK &&
312 /* there are some left allocated */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600313 xfs_inobt_maskn(chunkidx,
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300314 XFS_INODES_PER_CHUNK - chunkidx) &
315 ~r.ir_free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
317 * Grab the chunk record. Mark all the
318 * uninteresting inodes (because they're
319 * before our start point) free.
320 */
321 for (i = 0; i < chunkidx; i++) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300322 if (XFS_INOBT_MASK(i) & ~r.ir_free)
323 r.ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300325 r.ir_free |= xfs_inobt_maskn(0, chunkidx);
326 irbp->ir_startino = r.ir_startino;
327 irbp->ir_freecount = r.ir_freecount;
328 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300330 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
331 icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 } else {
333 /*
334 * If any of those tests failed, bump the
335 * inode number (just in case).
336 */
337 agino++;
338 icount = 0;
339 }
340 /*
341 * In any case, increment to the next record.
342 */
343 if (!error)
Christoph Hellwig637aa502008-10-30 16:55:45 +1100344 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else {
346 /*
347 * Start of ag. Lookup the first inode chunk.
348 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300349 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 icount = 0;
351 }
352 /*
353 * Loop through inode btree records in this ag,
354 * until we run out of inodes or space in the buffer.
355 */
356 while (irbp < irbufend && icount < ubcount) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300357 xfs_inobt_rec_incore_t r;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * Loop as long as we're unable to read the
361 * inode btree.
362 */
363 while (error) {
364 agino += XFS_INODES_PER_CHUNK;
365 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100366 be32_to_cpu(agi->agi_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
Christoph Hellwig21875502009-08-31 20:58:21 -0300368 error = xfs_inobt_lookup(cur, agino,
369 XFS_LOOKUP_GE, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100370 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 /*
373 * If ran off the end of the ag either with an error,
374 * or the normal way, set end and stop collecting.
375 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300376 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 end_of_ag = 1;
378 break;
379 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300380
381 error = xfs_inobt_get_rec(cur, &r, &i);
382 if (error || i == 0) {
383 end_of_ag = 1;
384 break;
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /*
388 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000389 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300391 if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
Nathan Scott262750932006-09-28 11:02:03 +1000392 /*
393 * Loop over all clusters in the next chunk.
394 * Do a readahead if there are any allocated
395 * inodes in that cluster.
396 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300397 agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
398 for (chunkidx = 0;
Nathan Scott262750932006-09-28 11:02:03 +1000399 chunkidx < XFS_INODES_PER_CHUNK;
400 chunkidx += nicluster,
401 agbno += nbcluster) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300402 if (xfs_inobt_maskn(chunkidx, nicluster)
403 & ~r.ir_free)
Nathan Scott262750932006-09-28 11:02:03 +1000404 xfs_btree_reada_bufs(mp, agno,
405 agbno, nbcluster);
406 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300407 irbp->ir_startino = r.ir_startino;
408 irbp->ir_freecount = r.ir_freecount;
409 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300411 icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 /*
414 * Set agino to after this chunk and bump the cursor.
415 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300416 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100417 error = xfs_btree_increment(cur, 0, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100418 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 /*
421 * Drop the btree buffers and the agi buffer.
422 * We can't hold any of the locks these represent
423 * when calling iget.
424 */
425 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
426 xfs_buf_relse(agbp);
427 /*
428 * Now format all the good inodes into the user's buffer.
429 */
430 irbufend = irbp;
431 for (irbp = irbuf;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100432 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * Now process this chunk of inodes.
435 */
Nathan Scott262750932006-09-28 11:02:03 +1000436 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100437 XFS_BULKSTAT_UBLEFT(ubleft) &&
Nathan Scott262750932006-09-28 11:02:03 +1000438 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 chunkidx++, clustidx++, agino++) {
440 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
441 /*
442 * Recompute agbno if this is the
443 * first inode of the cluster.
444 *
445 * Careful with clustidx. There can be
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200446 * multiple clusters per chunk, a single
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * cluster per chunk or a cluster that has
448 * inodes represented from several different
449 * chunks (if blocksize is large).
450 *
451 * Because of this, the starting clustidx is
452 * initialized to zero in this loop but must
453 * later be reset after reading in the cluster
454 * buffer.
455 */
456 if ((chunkidx & (nicluster - 1)) == 0) {
457 agbno = XFS_AGINO_TO_AGBNO(mp,
Nathan Scott262750932006-09-28 11:02:03 +1000458 irbp->ir_startino) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 ((chunkidx & nimask) >>
460 mp->m_sb.sb_inopblog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000462 ino = XFS_AGINO_TO_INO(mp, agno, agino);
463 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * Skip if this inode is free.
466 */
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000467 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
468 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * Count used inodes as free so we can tell
473 * when the chunk is used up.
474 */
Nathan Scott262750932006-09-28 11:02:03 +1000475 irbp->ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * Get the inode and fill in a single buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
480 ubused = statstruct_size;
Dave Chinner7b6259e2010-06-24 11:35:17 +1000481 error = formatter(mp, ino, ubufp, ubleft,
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000482 &ubused, &fmterror);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (fmterror == BULKSTAT_RV_NOTHING) {
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100484 if (error && error != ENOENT &&
485 error != EINVAL) {
Vlad Apostolove132f542006-09-28 11:04:31 +1000486 ubleft = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100487 rval = error;
488 break;
489 }
490 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 continue;
492 }
493 if (fmterror == BULKSTAT_RV_GIVEUP) {
494 ubleft = 0;
495 ASSERT(error);
496 rval = error;
497 break;
498 }
499 if (ubufp)
500 ubufp += ubused;
501 ubleft -= ubused;
502 ubelem++;
503 lastino = ino;
504 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100505
506 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 if (bp)
510 xfs_buf_relse(bp);
511
512 /*
513 * Set up for the next loop iteration.
514 */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100515 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (end_of_ag) {
517 agno++;
518 agino = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100519 } else
520 agino = XFS_INO_TO_AGINO(mp, lastino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 } else
522 break;
523 }
524 /*
525 * Done, we're either out of filesystem or space to put the data.
526 */
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000527 kmem_free_large(irbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *ubcountp = ubelem;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100529 /*
530 * Found some inodes, return them now and return the error next time.
531 */
532 if (ubelem)
533 rval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (agno >= mp->m_sb.sb_agcount) {
535 /*
536 * If we ran out of filesystem, mark lastino as off
537 * the end of the filesystem, so the next call
538 * will return immediately.
539 */
540 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
541 *done = 1;
542 } else
543 *lastinop = (xfs_ino_t)lastino;
544
545 return rval;
546}
547
548/*
549 * Return stat information in bulk (by-inode) for the filesystem.
550 * Special case for non-sequential one inode bulkstat.
551 */
552int /* error status */
553xfs_bulkstat_single(
554 xfs_mount_t *mp, /* mount point for filesystem */
555 xfs_ino_t *lastinop, /* inode to return */
556 char __user *buffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000557 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 int count; /* count value for bulkstat call */
560 int error; /* return value */
561 xfs_ino_t ino; /* filesystem inode number */
562 int res; /* result from bs1 */
563
564 /*
565 * note that requesting valid inode numbers which are not allocated
566 * to inodes will most likely cause xfs_itobp to generate warning
567 * messages about bad magic numbers. This is ok. The fact that
568 * the inode isn't actually an inode is handled by the
569 * error check below. Done this way to make the usual case faster
570 * at the expense of the error case.
571 */
572
573 ino = (xfs_ino_t)*lastinop;
Dave Chinner7b6259e2010-06-24 11:35:17 +1000574 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t), 0, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (error) {
576 /*
577 * Special case way failed, do it the "long" way
578 * to see if that works.
579 */
580 (*lastinop)--;
581 count = 1;
582 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000583 sizeof(xfs_bstat_t), buffer, done))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return error;
585 if (count == 0 || (xfs_ino_t)*lastinop != ino)
586 return error == EFSCORRUPTED ?
587 XFS_ERROR(EINVAL) : error;
588 else
589 return 0;
590 }
591 *done = 0;
592 return 0;
593}
594
Michal Marekfaa63e92007-07-11 11:10:19 +1000595int
596xfs_inumbers_fmt(
597 void __user *ubuffer, /* buffer to write to */
598 const xfs_inogrp_t *buffer, /* buffer to read from */
599 long count, /* # of elements to read */
600 long *written) /* # of bytes written */
601{
602 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
603 return -EFAULT;
604 *written = count * sizeof(*buffer);
605 return 0;
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * Return inode number table for the filesystem.
610 */
611int /* error status */
612xfs_inumbers(
613 xfs_mount_t *mp, /* mount point for filesystem */
614 xfs_ino_t *lastino, /* last inode returned */
615 int *count, /* size of buffer/count returned */
Michal Marekfaa63e92007-07-11 11:10:19 +1000616 void __user *ubuffer,/* buffer with inode descriptions */
617 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 xfs_buf_t *agbp;
620 xfs_agino_t agino;
621 xfs_agnumber_t agno;
622 int bcount;
623 xfs_inogrp_t *buffer;
624 int bufidx;
625 xfs_btree_cur_t *cur;
626 int error;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300627 xfs_inobt_rec_incore_t r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int i;
629 xfs_ino_t ino;
630 int left;
631 int tmp;
632
633 ino = (xfs_ino_t)*lastino;
634 agno = XFS_INO_TO_AGNO(mp, ino);
635 agino = XFS_INO_TO_AGINO(mp, ino);
636 left = *count;
637 *count = 0;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100638 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
640 error = bufidx = 0;
641 cur = NULL;
642 agbp = NULL;
643 while (left > 0 && agno < mp->m_sb.sb_agcount) {
644 if (agbp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (error) {
647 /*
648 * If we can't read the AGI of this ag,
649 * then just skip to the next one.
650 */
651 ASSERT(cur == NULL);
652 agbp = NULL;
653 agno++;
654 agino = 0;
655 continue;
656 }
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100657 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Christoph Hellwig21875502009-08-31 20:58:21 -0300658 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
659 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (error) {
661 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
662 cur = NULL;
663 xfs_buf_relse(agbp);
664 agbp = NULL;
665 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200666 * Move up the last inode in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * chunk. The lookup_ge will always get
668 * us the first inode in the next chunk.
669 */
670 agino += XFS_INODES_PER_CHUNK - 1;
671 continue;
672 }
673 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300674 error = xfs_inobt_get_rec(cur, &r, &i);
675 if (error || i == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 xfs_buf_relse(agbp);
677 agbp = NULL;
678 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
679 cur = NULL;
680 agno++;
681 agino = 0;
682 continue;
683 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300684 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
685 buffer[bufidx].xi_startino =
686 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
687 buffer[bufidx].xi_alloccount =
688 XFS_INODES_PER_CHUNK - r.ir_freecount;
689 buffer[bufidx].xi_allocmask = ~r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 bufidx++;
691 left--;
692 if (bufidx == bcount) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000693 long written;
694 if (formatter(ubuffer, buffer, bufidx, &written)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 error = XFS_ERROR(EFAULT);
696 break;
697 }
Michal Marekfaa63e92007-07-11 11:10:19 +1000698 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *count += bufidx;
700 bufidx = 0;
701 }
702 if (left) {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100703 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (error) {
705 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
706 cur = NULL;
707 xfs_buf_relse(agbp);
708 agbp = NULL;
709 /*
710 * The agino value has already been bumped.
711 * Just try to skip up to it.
712 */
713 agino += XFS_INODES_PER_CHUNK;
714 continue;
715 }
716 }
717 }
718 if (!error) {
719 if (bufidx) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000720 long written;
721 if (formatter(ubuffer, buffer, bufidx, &written))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 error = XFS_ERROR(EFAULT);
723 else
724 *count += bufidx;
725 }
726 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
727 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000728 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (cur)
730 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
731 XFS_BTREE_NOERROR));
732 if (agbp)
733 xfs_buf_relse(agbp);
734 return error;
735}