blob: 2b86f86105125c3869a641b68bc73b984ce69e79 [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
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100052/*
53 * Return stat information for one inode.
54 * Return 0 if ok, else errno.
55 */
56int
57xfs_bulkstat_one_int(
58 struct xfs_mount *mp, /* mount point for filesystem */
59 xfs_ino_t ino, /* inode to get data for */
60 void __user *buffer, /* buffer to place output in */
61 int ubsize, /* size of buffer */
62 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100063 int *ubused, /* bytes used by me */
64 int *stat) /* BULKSTAT_RV_... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100066 struct xfs_icdinode *dic; /* dinode core info pointer */
67 struct xfs_inode *ip; /* incore inode pointer */
68 struct inode *inode;
69 struct xfs_bstat *buf; /* return buffer */
70 int error = 0; /* error value */
71
72 *stat = BULKSTAT_RV_NOTHING;
73
74 if (!buffer || xfs_internal_inum(mp, ino))
75 return XFS_ERROR(EINVAL);
76
77 buf = kmem_alloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
78 if (!buf)
79 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Nathan Scott745b1f472006-09-28 11:02:23 +100081 error = xfs_iget(mp, NULL, ino,
Dave Chinner7b6259e2010-06-24 11:35:17 +100082 XFS_IGET_UNTRUSTED, XFS_ILOCK_SHARED, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (error) {
84 *stat = BULKSTAT_RV_NOTHING;
Christoph Hellwig7dce11d2010-06-23 18:11:11 +100085 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
88 ASSERT(ip != NULL);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +110089 ASSERT(ip->i_imap.im_blkno != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 dic = &ip->i_d;
Christoph Hellwigf9581b12009-10-06 20:29:26 +000092 inode = VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* xfs_iget returns the following without needing
95 * further change.
96 */
97 buf->bs_nlink = dic->di_nlink;
98 buf->bs_projid = dic->di_projid;
99 buf->bs_ino = ino;
100 buf->bs_mode = dic->di_mode;
101 buf->bs_uid = dic->di_uid;
102 buf->bs_gid = dic->di_gid;
103 buf->bs_size = dic->di_size;
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000104
Christoph Hellwig8fab4512009-03-16 08:24:46 +0100105 /*
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000106 * We need to read the timestamps from the Linux inode because
107 * the VFS keeps writing directly into the inode structure instead
108 * of telling us about the updates.
Christoph Hellwig8fab4512009-03-16 08:24:46 +0100109 */
Christoph Hellwigf9581b12009-10-06 20:29:26 +0000110 buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
111 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
112 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
113 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
114 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
115 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 buf->bs_xflags = xfs_ip2xflags(ip);
118 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
119 buf->bs_extents = dic->di_nextents;
120 buf->bs_gen = dic->di_gen;
121 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
122 buf->bs_dmevmask = dic->di_dmevmask;
123 buf->bs_dmstate = dic->di_dmstate;
124 buf->bs_aextents = dic->di_anextents;
Dave Chinner07000ee2010-03-05 04:41:14 +0000125 buf->bs_forkoff = XFS_IFORK_BOFF(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 switch (dic->di_format) {
128 case XFS_DINODE_FMT_DEV:
129 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
130 buf->bs_blksize = BLKDEV_IOSIZE;
131 buf->bs_blocks = 0;
132 break;
133 case XFS_DINODE_FMT_LOCAL:
134 case XFS_DINODE_FMT_UUID:
135 buf->bs_rdev = 0;
136 buf->bs_blksize = mp->m_sb.sb_blocksize;
137 buf->bs_blocks = 0;
138 break;
139 case XFS_DINODE_FMT_EXTENTS:
140 case XFS_DINODE_FMT_BTREE:
141 buf->bs_rdev = 0;
142 buf->bs_blksize = mp->m_sb.sb_blocksize;
143 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
144 break;
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 xfs_iput(ip, XFS_ILOCK_SHARED);
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000147
148 error = formatter(buffer, ubsize, ubused, buf);
149
150 if (!error)
151 *stat = BULKSTAT_RV_DIDONE;
152
153 out_free:
154 kmem_free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return error;
156}
157
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600158/* Return 0 on success or positive error */
Michal Marekfaa63e92007-07-11 11:10:19 +1000159STATIC int
160xfs_bulkstat_one_fmt(
161 void __user *ubuffer,
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600162 int ubsize,
163 int *ubused,
Michal Marekfaa63e92007-07-11 11:10:19 +1000164 const xfs_bstat_t *buffer)
165{
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600166 if (ubsize < sizeof(*buffer))
167 return XFS_ERROR(ENOMEM);
Michal Marekfaa63e92007-07-11 11:10:19 +1000168 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
sandeen@sandeen.net65fbaf22008-11-25 21:20:12 -0600169 return XFS_ERROR(EFAULT);
170 if (ubused)
171 *ubused = sizeof(*buffer);
172 return 0;
Michal Marekfaa63e92007-07-11 11:10:19 +1000173}
174
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600175int
176xfs_bulkstat_one(
177 xfs_mount_t *mp, /* mount point for filesystem */
178 xfs_ino_t ino, /* inode number to get data for */
179 void __user *buffer, /* buffer to place output in */
180 int ubsize, /* size of buffer */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600181 int *ubused, /* bytes used by me */
sandeen@sandeen.net2ee4fa52008-11-25 21:20:11 -0600182 int *stat) /* BULKSTAT_RV_... */
183{
184 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
Dave Chinner7b6259e2010-06-24 11:35:17 +1000185 xfs_bulkstat_one_fmt, ubused, stat);
Nathan Scott8b56f082006-09-28 11:01:46 +1000186}
187
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100188#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
189
Nathan Scott8b56f082006-09-28 11:01:46 +1000190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Return stat information in bulk (by-inode) for the filesystem.
192 */
193int /* error status */
194xfs_bulkstat(
195 xfs_mount_t *mp, /* mount point for filesystem */
196 xfs_ino_t *lastinop, /* last inode returned */
197 int *ubcountp, /* size of buffer/count returned */
198 bulkstat_one_pf formatter, /* func that'd fill a single buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 size_t statstruct_size, /* sizeof struct filling */
200 char __user *ubuffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000201 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 xfs_agblock_t agbno=0;/* allocation group block number */
204 xfs_buf_t *agbp; /* agi header buffer */
205 xfs_agi_t *agi; /* agi header data */
206 xfs_agino_t agino; /* inode # in allocation group */
207 xfs_agnumber_t agno; /* allocation group number */
208 xfs_daddr_t bno; /* inode cluster start daddr */
209 int chunkidx; /* current index into inode chunk */
210 int clustidx; /* current index into inode cluster */
211 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
212 int end_of_ag; /* set if we've seen the ag end */
213 int error; /* error code */
214 int fmterror;/* bulkstat formatter result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int i; /* loop index */
216 int icount; /* count of inodes good in irbuf */
Nathan Scott215101c2006-09-28 11:04:43 +1000217 size_t irbsize; /* size of irec buffer in bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 xfs_ino_t ino; /* inode number (filesystem) */
Nathan Scott262750932006-09-28 11:02:03 +1000219 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
220 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
221 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100222 xfs_ino_t lastino; /* last inode number returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int nbcluster; /* # of blocks in a cluster */
224 int nicluster; /* # of inodes in a cluster */
225 int nimask; /* mask for inode clusters */
226 int nirbuf; /* size of irbuf */
227 int rval; /* return value error code */
228 int tmp; /* result value from btree calls */
229 int ubcount; /* size of user's buffer */
230 int ubleft; /* bytes left in user's buffer */
231 char __user *ubufp; /* pointer into user's buffer */
232 int ubelem; /* spaces used in user's buffer */
233 int ubused; /* bytes used by formatter */
234 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * Get the last inode value, see if there's nothing to do.
238 */
239 ino = (xfs_ino_t)*lastinop;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100240 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 agno = XFS_INO_TO_AGNO(mp, ino);
242 agino = XFS_INO_TO_AGINO(mp, ino);
243 if (agno >= mp->m_sb.sb_agcount ||
244 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
245 *done = 1;
246 *ubcountp = 0;
247 return 0;
248 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100249 if (!ubcountp || *ubcountp <= 0) {
250 return EINVAL;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ubcount = *ubcountp; /* statstruct's */
253 ubleft = ubcount * statstruct_size; /* bytes */
254 *ubcountp = ubelem = 0;
255 *done = 0;
256 fmterror = 0;
257 ubufp = ubuffer;
258 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
259 mp->m_sb.sb_inopblock :
260 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
261 nimask = ~(nicluster - 1);
262 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000263 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
264 if (!irbuf)
265 return ENOMEM;
266
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000267 nirbuf = irbsize / sizeof(*irbuf);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /*
270 * Loop over the allocation groups, starting from the last
271 * inode returned; 0 means start of the allocation group.
272 */
273 rval = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100274 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
275 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (error) {
279 /*
280 * Skip this allocation group and go to the next one.
281 */
282 agno++;
283 agino = 0;
284 continue;
285 }
286 agi = XFS_BUF_TO_AGI(agbp);
287 /*
288 * Allocate and initialize a btree cursor for ialloc btree.
289 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100290 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 irbp = irbuf;
292 irbufend = irbuf + nirbuf;
293 end_of_ag = 0;
294 /*
295 * If we're returning in the middle of an allocation group,
296 * we need to get the remainder of the chunk we're in.
297 */
298 if (agino > 0) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300299 xfs_inobt_rec_incore_t r;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /*
302 * Lookup the inode chunk that this inode lives in.
303 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300304 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
305 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!error && /* no I/O error */
307 tmp && /* lookup succeeded */
308 /* got the record, should always work */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300309 !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 i == 1 &&
311 /* this is the right chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300312 agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* lastino was not last in chunk */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300314 (chunkidx = agino - r.ir_startino + 1) <
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 XFS_INODES_PER_CHUNK &&
316 /* there are some left allocated */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600317 xfs_inobt_maskn(chunkidx,
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300318 XFS_INODES_PER_CHUNK - chunkidx) &
319 ~r.ir_free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
321 * Grab the chunk record. Mark all the
322 * uninteresting inodes (because they're
323 * before our start point) free.
324 */
325 for (i = 0; i < chunkidx; i++) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300326 if (XFS_INOBT_MASK(i) & ~r.ir_free)
327 r.ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300329 r.ir_free |= xfs_inobt_maskn(0, chunkidx);
330 irbp->ir_startino = r.ir_startino;
331 irbp->ir_freecount = r.ir_freecount;
332 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300334 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
335 icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else {
337 /*
338 * If any of those tests failed, bump the
339 * inode number (just in case).
340 */
341 agino++;
342 icount = 0;
343 }
344 /*
345 * In any case, increment to the next record.
346 */
347 if (!error)
Christoph Hellwig637aa502008-10-30 16:55:45 +1100348 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 } else {
350 /*
351 * Start of ag. Lookup the first inode chunk.
352 */
Christoph Hellwig21875502009-08-31 20:58:21 -0300353 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 icount = 0;
355 }
356 /*
357 * Loop through inode btree records in this ag,
358 * until we run out of inodes or space in the buffer.
359 */
360 while (irbp < irbufend && icount < ubcount) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300361 xfs_inobt_rec_incore_t r;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /*
364 * Loop as long as we're unable to read the
365 * inode btree.
366 */
367 while (error) {
368 agino += XFS_INODES_PER_CHUNK;
369 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100370 be32_to_cpu(agi->agi_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 break;
Christoph Hellwig21875502009-08-31 20:58:21 -0300372 error = xfs_inobt_lookup(cur, agino,
373 XFS_LOOKUP_GE, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100374 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 /*
377 * If ran off the end of the ag either with an error,
378 * or the normal way, set end and stop collecting.
379 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300380 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 end_of_ag = 1;
382 break;
383 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300384
385 error = xfs_inobt_get_rec(cur, &r, &i);
386 if (error || i == 0) {
387 end_of_ag = 1;
388 break;
389 }
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
392 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000393 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300395 if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
Nathan Scott262750932006-09-28 11:02:03 +1000396 /*
397 * Loop over all clusters in the next chunk.
398 * Do a readahead if there are any allocated
399 * inodes in that cluster.
400 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300401 agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
402 for (chunkidx = 0;
Nathan Scott262750932006-09-28 11:02:03 +1000403 chunkidx < XFS_INODES_PER_CHUNK;
404 chunkidx += nicluster,
405 agbno += nbcluster) {
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300406 if (xfs_inobt_maskn(chunkidx, nicluster)
407 & ~r.ir_free)
Nathan Scott262750932006-09-28 11:02:03 +1000408 xfs_btree_reada_bufs(mp, agno,
409 agbno, nbcluster);
410 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300411 irbp->ir_startino = r.ir_startino;
412 irbp->ir_freecount = r.ir_freecount;
413 irbp->ir_free = r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 irbp++;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300415 icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 /*
418 * Set agino to after this chunk and bump the cursor.
419 */
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300420 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
Christoph Hellwig637aa502008-10-30 16:55:45 +1100421 error = xfs_btree_increment(cur, 0, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100422 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 /*
425 * Drop the btree buffers and the agi buffer.
426 * We can't hold any of the locks these represent
427 * when calling iget.
428 */
429 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
430 xfs_buf_relse(agbp);
431 /*
432 * Now format all the good inodes into the user's buffer.
433 */
434 irbufend = irbp;
435 for (irbp = irbuf;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100436 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * Now process this chunk of inodes.
439 */
Nathan Scott262750932006-09-28 11:02:03 +1000440 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100441 XFS_BULKSTAT_UBLEFT(ubleft) &&
Nathan Scott262750932006-09-28 11:02:03 +1000442 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 chunkidx++, clustidx++, agino++) {
444 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
445 /*
446 * Recompute agbno if this is the
447 * first inode of the cluster.
448 *
449 * Careful with clustidx. There can be
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200450 * multiple clusters per chunk, a single
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * cluster per chunk or a cluster that has
452 * inodes represented from several different
453 * chunks (if blocksize is large).
454 *
455 * Because of this, the starting clustidx is
456 * initialized to zero in this loop but must
457 * later be reset after reading in the cluster
458 * buffer.
459 */
460 if ((chunkidx & (nicluster - 1)) == 0) {
461 agbno = XFS_AGINO_TO_AGBNO(mp,
Nathan Scott262750932006-09-28 11:02:03 +1000462 irbp->ir_startino) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ((chunkidx & nimask) >>
464 mp->m_sb.sb_inopblog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000466 ino = XFS_AGINO_TO_INO(mp, agno, agino);
467 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Skip if this inode is free.
470 */
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000471 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
472 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * Count used inodes as free so we can tell
477 * when the chunk is used up.
478 */
Nathan Scott262750932006-09-28 11:02:03 +1000479 irbp->ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /*
482 * Get the inode and fill in a single buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
484 ubused = statstruct_size;
Dave Chinner7b6259e2010-06-24 11:35:17 +1000485 error = formatter(mp, ino, ubufp, ubleft,
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000486 &ubused, &fmterror);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (fmterror == BULKSTAT_RV_NOTHING) {
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100488 if (error && error != ENOENT &&
489 error != EINVAL) {
Vlad Apostolove132f542006-09-28 11:04:31 +1000490 ubleft = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100491 rval = error;
492 break;
493 }
494 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 continue;
496 }
497 if (fmterror == BULKSTAT_RV_GIVEUP) {
498 ubleft = 0;
499 ASSERT(error);
500 rval = error;
501 break;
502 }
503 if (ubufp)
504 ubufp += ubused;
505 ubleft -= ubused;
506 ubelem++;
507 lastino = ino;
508 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100509
510 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
513 if (bp)
514 xfs_buf_relse(bp);
515
516 /*
517 * Set up for the next loop iteration.
518 */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100519 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (end_of_ag) {
521 agno++;
522 agino = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100523 } else
524 agino = XFS_INO_TO_AGINO(mp, lastino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else
526 break;
527 }
528 /*
529 * Done, we're either out of filesystem or space to put the data.
530 */
Christoph Hellwigbdfb0432010-01-20 21:55:30 +0000531 kmem_free_large(irbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *ubcountp = ubelem;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100533 /*
534 * Found some inodes, return them now and return the error next time.
535 */
536 if (ubelem)
537 rval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (agno >= mp->m_sb.sb_agcount) {
539 /*
540 * If we ran out of filesystem, mark lastino as off
541 * the end of the filesystem, so the next call
542 * will return immediately.
543 */
544 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
545 *done = 1;
546 } else
547 *lastinop = (xfs_ino_t)lastino;
548
549 return rval;
550}
551
552/*
553 * Return stat information in bulk (by-inode) for the filesystem.
554 * Special case for non-sequential one inode bulkstat.
555 */
556int /* error status */
557xfs_bulkstat_single(
558 xfs_mount_t *mp, /* mount point for filesystem */
559 xfs_ino_t *lastinop, /* inode to return */
560 char __user *buffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000561 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 int count; /* count value for bulkstat call */
564 int error; /* return value */
565 xfs_ino_t ino; /* filesystem inode number */
566 int res; /* result from bs1 */
567
568 /*
569 * note that requesting valid inode numbers which are not allocated
570 * to inodes will most likely cause xfs_itobp to generate warning
571 * messages about bad magic numbers. This is ok. The fact that
572 * the inode isn't actually an inode is handled by the
573 * error check below. Done this way to make the usual case faster
574 * at the expense of the error case.
575 */
576
577 ino = (xfs_ino_t)*lastinop;
Dave Chinner7b6259e2010-06-24 11:35:17 +1000578 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t), 0, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (error) {
580 /*
581 * Special case way failed, do it the "long" way
582 * to see if that works.
583 */
584 (*lastinop)--;
585 count = 1;
586 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
Christoph Hellwig7dce11d2010-06-23 18:11:11 +1000587 sizeof(xfs_bstat_t), buffer, done))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return error;
589 if (count == 0 || (xfs_ino_t)*lastinop != ino)
590 return error == EFSCORRUPTED ?
591 XFS_ERROR(EINVAL) : error;
592 else
593 return 0;
594 }
595 *done = 0;
596 return 0;
597}
598
Michal Marekfaa63e92007-07-11 11:10:19 +1000599int
600xfs_inumbers_fmt(
601 void __user *ubuffer, /* buffer to write to */
602 const xfs_inogrp_t *buffer, /* buffer to read from */
603 long count, /* # of elements to read */
604 long *written) /* # of bytes written */
605{
606 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
607 return -EFAULT;
608 *written = count * sizeof(*buffer);
609 return 0;
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * Return inode number table for the filesystem.
614 */
615int /* error status */
616xfs_inumbers(
617 xfs_mount_t *mp, /* mount point for filesystem */
618 xfs_ino_t *lastino, /* last inode returned */
619 int *count, /* size of buffer/count returned */
Michal Marekfaa63e92007-07-11 11:10:19 +1000620 void __user *ubuffer,/* buffer with inode descriptions */
621 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 xfs_buf_t *agbp;
624 xfs_agino_t agino;
625 xfs_agnumber_t agno;
626 int bcount;
627 xfs_inogrp_t *buffer;
628 int bufidx;
629 xfs_btree_cur_t *cur;
630 int error;
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300631 xfs_inobt_rec_incore_t r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int i;
633 xfs_ino_t ino;
634 int left;
635 int tmp;
636
637 ino = (xfs_ino_t)*lastino;
638 agno = XFS_INO_TO_AGNO(mp, ino);
639 agino = XFS_INO_TO_AGINO(mp, ino);
640 left = *count;
641 *count = 0;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100642 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
644 error = bufidx = 0;
645 cur = NULL;
646 agbp = NULL;
647 while (left > 0 && agno < mp->m_sb.sb_agcount) {
648 if (agbp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (error) {
651 /*
652 * If we can't read the AGI of this ag,
653 * then just skip to the next one.
654 */
655 ASSERT(cur == NULL);
656 agbp = NULL;
657 agno++;
658 agino = 0;
659 continue;
660 }
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100661 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
Christoph Hellwig21875502009-08-31 20:58:21 -0300662 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
663 &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (error) {
665 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
666 cur = NULL;
667 xfs_buf_relse(agbp);
668 agbp = NULL;
669 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200670 * Move up the last inode in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * chunk. The lookup_ge will always get
672 * us the first inode in the next chunk.
673 */
674 agino += XFS_INODES_PER_CHUNK - 1;
675 continue;
676 }
677 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300678 error = xfs_inobt_get_rec(cur, &r, &i);
679 if (error || i == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xfs_buf_relse(agbp);
681 agbp = NULL;
682 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
683 cur = NULL;
684 agno++;
685 agino = 0;
686 continue;
687 }
Christoph Hellwig2e287a72009-08-31 20:56:58 -0300688 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
689 buffer[bufidx].xi_startino =
690 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
691 buffer[bufidx].xi_alloccount =
692 XFS_INODES_PER_CHUNK - r.ir_freecount;
693 buffer[bufidx].xi_allocmask = ~r.ir_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 bufidx++;
695 left--;
696 if (bufidx == bcount) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000697 long written;
698 if (formatter(ubuffer, buffer, bufidx, &written)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 error = XFS_ERROR(EFAULT);
700 break;
701 }
Michal Marekfaa63e92007-07-11 11:10:19 +1000702 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 *count += bufidx;
704 bufidx = 0;
705 }
706 if (left) {
Christoph Hellwig637aa502008-10-30 16:55:45 +1100707 error = xfs_btree_increment(cur, 0, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (error) {
709 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
710 cur = NULL;
711 xfs_buf_relse(agbp);
712 agbp = NULL;
713 /*
714 * The agino value has already been bumped.
715 * Just try to skip up to it.
716 */
717 agino += XFS_INODES_PER_CHUNK;
718 continue;
719 }
720 }
721 }
722 if (!error) {
723 if (bufidx) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000724 long written;
725 if (formatter(ubuffer, buffer, bufidx, &written))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 error = XFS_ERROR(EFAULT);
727 else
728 *count += bufidx;
729 }
730 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
731 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000732 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (cur)
734 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
735 XFS_BTREE_NOERROR));
736 if (agbp)
737 xfs_buf_relse(agbp);
738 return error;
739}