blob: eb85bdedad0c7505aa90bdeb09c97403b3979631 [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
Vlad Apostolov6f1f2162006-09-28 11:06:15 +100042int
43xfs_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 */
Nathan Scott67fcaa72006-06-09 17:00:52 +100062 bhv_vnode_t *vp;
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);
73 ASSERT(ip->i_blkno != (xfs_daddr_t)0);
74 if (ip->i_d.di_mode == 0) {
75 *stat = BULKSTAT_RV_NOTHING;
76 error = XFS_ERROR(ENOENT);
77 goto out_iput;
78 }
79
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110080 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 dic = &ip->i_d;
82
83 /* xfs_iget returns the following without needing
84 * further change.
85 */
86 buf->bs_nlink = dic->di_nlink;
87 buf->bs_projid = dic->di_projid;
88 buf->bs_ino = ino;
89 buf->bs_mode = dic->di_mode;
90 buf->bs_uid = dic->di_uid;
91 buf->bs_gid = dic->di_gid;
92 buf->bs_size = dic->di_size;
Nathan Scottca5ccbf2006-01-11 21:03:04 +110093 vn_atime_to_bstime(vp, &buf->bs_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
95 buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
96 buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
97 buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
98 buf->bs_xflags = xfs_ip2xflags(ip);
99 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
100 buf->bs_extents = dic->di_nextents;
101 buf->bs_gen = dic->di_gen;
102 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
103 buf->bs_dmevmask = dic->di_dmevmask;
104 buf->bs_dmstate = dic->di_dmstate;
105 buf->bs_aextents = dic->di_anextents;
106
107 switch (dic->di_format) {
108 case XFS_DINODE_FMT_DEV:
109 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
110 buf->bs_blksize = BLKDEV_IOSIZE;
111 buf->bs_blocks = 0;
112 break;
113 case XFS_DINODE_FMT_LOCAL:
114 case XFS_DINODE_FMT_UUID:
115 buf->bs_rdev = 0;
116 buf->bs_blksize = mp->m_sb.sb_blocksize;
117 buf->bs_blocks = 0;
118 break;
119 case XFS_DINODE_FMT_EXTENTS:
120 case XFS_DINODE_FMT_BTREE:
121 buf->bs_rdev = 0;
122 buf->bs_blksize = mp->m_sb.sb_blocksize;
123 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
124 break;
125 }
126
127 out_iput:
128 xfs_iput(ip, XFS_ILOCK_SHARED);
129 return error;
130}
131
David Chinner7b073392008-04-10 12:24:04 +1000132STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133xfs_bulkstat_one_dinode(
134 xfs_mount_t *mp, /* mount point for filesystem */
135 xfs_ino_t ino, /* inode number to get data for */
136 xfs_dinode_t *dip, /* dinode inode pointer */
137 xfs_bstat_t *buf) /* return buffer */
138{
139 xfs_dinode_core_t *dic; /* dinode core info pointer */
140
141 dic = &dip->di_core;
142
143 /*
144 * The inode format changed when we moved the link count and
145 * made it 32 bits long. If this is an old format inode,
146 * convert it in memory to look like a new one. If it gets
147 * flushed to disk we will convert back before flushing or
148 * logging it. We zero out the new projid field and the old link
149 * count field. We'll handle clearing the pad field (the remains
150 * of the old uuid field) when we actually convert the inode to
151 * the new format. We don't change the version number so that we
152 * can distinguish this from a real new format inode.
153 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000154 if (dic->di_version == XFS_DINODE_VERSION_1) {
155 buf->bs_nlink = be16_to_cpu(dic->di_onlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 buf->bs_projid = 0;
157 } else {
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000158 buf->bs_nlink = be32_to_cpu(dic->di_nlink);
159 buf->bs_projid = be16_to_cpu(dic->di_projid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 buf->bs_ino = ino;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000163 buf->bs_mode = be16_to_cpu(dic->di_mode);
164 buf->bs_uid = be32_to_cpu(dic->di_uid);
165 buf->bs_gid = be32_to_cpu(dic->di_gid);
166 buf->bs_size = be64_to_cpu(dic->di_size);
167 buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec);
168 buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec);
169 buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec);
170 buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec);
171 buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec);
172 buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec);
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100173 buf->bs_xflags = xfs_dic2xflags(dip);
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000174 buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog;
175 buf->bs_extents = be32_to_cpu(dic->di_nextents);
176 buf->bs_gen = be32_to_cpu(dic->di_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000178 buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask);
179 buf->bs_dmstate = be16_to_cpu(dic->di_dmstate);
180 buf->bs_aextents = be16_to_cpu(dic->di_anextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000182 switch (dic->di_format) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 case XFS_DINODE_FMT_DEV:
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000184 buf->bs_rdev = be32_to_cpu(dip->di_u.di_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 buf->bs_blksize = BLKDEV_IOSIZE;
186 buf->bs_blocks = 0;
187 break;
188 case XFS_DINODE_FMT_LOCAL:
189 case XFS_DINODE_FMT_UUID:
190 buf->bs_rdev = 0;
191 buf->bs_blksize = mp->m_sb.sb_blocksize;
192 buf->bs_blocks = 0;
193 break;
194 case XFS_DINODE_FMT_EXTENTS:
195 case XFS_DINODE_FMT_BTREE:
196 buf->bs_rdev = 0;
197 buf->bs_blksize = mp->m_sb.sb_blocksize;
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000198 buf->bs_blocks = be64_to_cpu(dic->di_nblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Michal Marekfaa63e92007-07-11 11:10:19 +1000203STATIC int
204xfs_bulkstat_one_fmt(
205 void __user *ubuffer,
206 const xfs_bstat_t *buffer)
207{
208 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
209 return -EFAULT;
210 return sizeof(*buffer);
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * Return stat information for one inode.
215 * Return 0 if ok, else errno.
216 */
217int /* error status */
218xfs_bulkstat_one(
219 xfs_mount_t *mp, /* mount point for filesystem */
220 xfs_ino_t ino, /* inode number to get data for */
221 void __user *buffer, /* buffer to place output in */
222 int ubsize, /* size of buffer */
223 void *private_data, /* my private data */
224 xfs_daddr_t bno, /* starting bno of inode cluster */
225 int *ubused, /* bytes used by me */
226 void *dibuff, /* on-disk inode buffer */
227 int *stat) /* BULKSTAT_RV_... */
228{
229 xfs_bstat_t *buf; /* return buffer */
230 int error = 0; /* error value */
231 xfs_dinode_t *dip; /* dinode inode pointer */
Michal Marekfaa63e92007-07-11 11:10:19 +1000232 bulkstat_one_fmt_pf formatter = private_data ? : xfs_bulkstat_one_fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 dip = (xfs_dinode_t *)dibuff;
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000235 *stat = BULKSTAT_RV_NOTHING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000237 if (!buffer || xfs_internal_inum(mp, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return XFS_ERROR(EINVAL);
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000239 if (ubsize < sizeof(*buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return XFS_ERROR(ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
243
244 if (dip == NULL) {
245 /* We're not being passed a pointer to a dinode. This happens
246 * if BULKSTAT_FG_IGET is selected. Do the iget.
247 */
248 error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
249 if (error)
250 goto out_free;
251 } else {
252 xfs_bulkstat_one_dinode(mp, ino, dip, buf);
253 }
254
Michal Marekfaa63e92007-07-11 11:10:19 +1000255 error = formatter(buffer, buf);
256 if (error < 0) {
Vlad Apostolov6f1f2162006-09-28 11:06:15 +1000257 error = EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto out_free;
259 }
260
261 *stat = BULKSTAT_RV_DIDONE;
262 if (ubused)
Michal Marekfaa63e92007-07-11 11:10:19 +1000263 *ubused = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 out_free:
266 kmem_free(buf, sizeof(*buf));
267 return error;
268}
269
270/*
Nathan Scott8b56f082006-09-28 11:01:46 +1000271 * Test to see whether we can use the ondisk inode directly, based
272 * on the given bulkstat flags, filling in dipp accordingly.
273 * Returns zero if the inode is dodgey.
274 */
275STATIC int
276xfs_bulkstat_use_dinode(
277 xfs_mount_t *mp,
278 int flags,
279 xfs_buf_t *bp,
280 int clustidx,
281 xfs_dinode_t **dipp)
282{
283 xfs_dinode_t *dip;
284 unsigned int aformat;
285
286 *dipp = NULL;
287 if (!bp || (flags & BULKSTAT_FG_IGET))
288 return 1;
289 dip = (xfs_dinode_t *)
290 xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
Vlad Apostolov859d7182007-10-11 17:44:18 +1000291 /*
Vlad Apostolovc319b582007-11-23 16:27:51 +1100292 * Check the buffer containing the on-disk inode for di_mode == 0.
Vlad Apostolov859d7182007-10-11 17:44:18 +1000293 * This is to prevent xfs_bulkstat from picking up just reclaimed
294 * inodes that have their in-core state initialized but not flushed
295 * to disk yet. This is a temporary hack that would require a proper
296 * fix in the future.
297 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000298 if (be16_to_cpu(dip->di_core.di_magic) != XFS_DINODE_MAGIC ||
Vlad Apostolov859d7182007-10-11 17:44:18 +1000299 !XFS_DINODE_GOOD_VERSION(dip->di_core.di_version) ||
Vlad Apostolovc319b582007-11-23 16:27:51 +1100300 !dip->di_core.di_mode)
Nathan Scott8b56f082006-09-28 11:01:46 +1000301 return 0;
302 if (flags & BULKSTAT_FG_QUICK) {
303 *dipp = dip;
304 return 1;
305 }
306 /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000307 aformat = dip->di_core.di_aformat;
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100308 if ((XFS_DFORK_Q(dip) == 0) ||
Nathan Scott8b56f082006-09-28 11:01:46 +1000309 (aformat == XFS_DINODE_FMT_LOCAL) ||
310 (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_core.di_anextents)) {
311 *dipp = dip;
312 return 1;
313 }
314 return 1;
315}
316
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100317#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
318
Nathan Scott8b56f082006-09-28 11:01:46 +1000319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * Return stat information in bulk (by-inode) for the filesystem.
321 */
322int /* error status */
323xfs_bulkstat(
324 xfs_mount_t *mp, /* mount point for filesystem */
325 xfs_ino_t *lastinop, /* last inode returned */
326 int *ubcountp, /* size of buffer/count returned */
327 bulkstat_one_pf formatter, /* func that'd fill a single buf */
328 void *private_data,/* private data for formatter */
329 size_t statstruct_size, /* sizeof struct filling */
330 char __user *ubuffer, /* buffer with inode stats */
331 int flags, /* defined in xfs_itable.h */
Nathan Scottc41564b2006-03-29 08:55:14 +1000332 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 xfs_agblock_t agbno=0;/* allocation group block number */
335 xfs_buf_t *agbp; /* agi header buffer */
336 xfs_agi_t *agi; /* agi header data */
337 xfs_agino_t agino; /* inode # in allocation group */
338 xfs_agnumber_t agno; /* allocation group number */
339 xfs_daddr_t bno; /* inode cluster start daddr */
340 int chunkidx; /* current index into inode chunk */
341 int clustidx; /* current index into inode cluster */
342 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
343 int end_of_ag; /* set if we've seen the ag end */
344 int error; /* error code */
345 int fmterror;/* bulkstat formatter result */
346 __int32_t gcnt; /* current btree rec's count */
347 xfs_inofree_t gfree; /* current btree rec's free mask */
348 xfs_agino_t gino; /* current btree rec's start inode */
349 int i; /* loop index */
350 int icount; /* count of inodes good in irbuf */
Nathan Scott215101c2006-09-28 11:04:43 +1000351 size_t irbsize; /* size of irec buffer in bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xfs_ino_t ino; /* inode number (filesystem) */
Nathan Scott262750932006-09-28 11:02:03 +1000353 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
354 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
355 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100356 xfs_ino_t lastino; /* last inode number returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int nbcluster; /* # of blocks in a cluster */
358 int nicluster; /* # of inodes in a cluster */
359 int nimask; /* mask for inode clusters */
360 int nirbuf; /* size of irbuf */
361 int rval; /* return value error code */
362 int tmp; /* result value from btree calls */
363 int ubcount; /* size of user's buffer */
364 int ubleft; /* bytes left in user's buffer */
365 char __user *ubufp; /* pointer into user's buffer */
366 int ubelem; /* spaces used in user's buffer */
367 int ubused; /* bytes used by formatter */
368 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
369 xfs_dinode_t *dip; /* ptr into bp for specific inode */
370 xfs_inode_t *ip; /* ptr to in-core inode struct */
371
372 /*
373 * Get the last inode value, see if there's nothing to do.
374 */
375 ino = (xfs_ino_t)*lastinop;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100376 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 dip = NULL;
378 agno = XFS_INO_TO_AGNO(mp, ino);
379 agino = XFS_INO_TO_AGINO(mp, ino);
380 if (agno >= mp->m_sb.sb_agcount ||
381 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
382 *done = 1;
383 *ubcountp = 0;
384 return 0;
385 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100386 if (!ubcountp || *ubcountp <= 0) {
387 return EINVAL;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 ubcount = *ubcountp; /* statstruct's */
390 ubleft = ubcount * statstruct_size; /* bytes */
391 *ubcountp = ubelem = 0;
392 *done = 0;
393 fmterror = 0;
394 ubufp = ubuffer;
395 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
396 mp->m_sb.sb_inopblock :
397 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
398 nimask = ~(nicluster - 1);
399 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100400 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4,
Nathan Scott77e46352006-09-28 11:03:27 +1000401 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000402 nirbuf = irbsize / sizeof(*irbuf);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /*
405 * Loop over the allocation groups, starting from the last
406 * inode returned; 0 means start of the allocation group.
407 */
408 rval = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100409 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
410 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 bp = NULL;
412 down_read(&mp->m_peraglock);
413 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
414 up_read(&mp->m_peraglock);
415 if (error) {
416 /*
417 * Skip this allocation group and go to the next one.
418 */
419 agno++;
420 agino = 0;
421 continue;
422 }
423 agi = XFS_BUF_TO_AGI(agbp);
424 /*
425 * Allocate and initialize a btree cursor for ialloc btree.
426 */
427 cur = xfs_btree_init_cursor(mp, NULL, agbp, agno, XFS_BTNUM_INO,
Nathan Scott262750932006-09-28 11:02:03 +1000428 (xfs_inode_t *)0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 irbp = irbuf;
430 irbufend = irbuf + nirbuf;
431 end_of_ag = 0;
432 /*
433 * If we're returning in the middle of an allocation group,
434 * we need to get the remainder of the chunk we're in.
435 */
436 if (agino > 0) {
437 /*
438 * Lookup the inode chunk that this inode lives in.
439 */
440 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &tmp);
441 if (!error && /* no I/O error */
442 tmp && /* lookup succeeded */
443 /* got the record, should always work */
444 !(error = xfs_inobt_get_rec(cur, &gino, &gcnt,
445 &gfree, &i)) &&
446 i == 1 &&
447 /* this is the right chunk */
448 agino < gino + XFS_INODES_PER_CHUNK &&
449 /* lastino was not last in chunk */
450 (chunkidx = agino - gino + 1) <
451 XFS_INODES_PER_CHUNK &&
452 /* there are some left allocated */
453 XFS_INOBT_MASKN(chunkidx,
454 XFS_INODES_PER_CHUNK - chunkidx) & ~gfree) {
455 /*
456 * Grab the chunk record. Mark all the
457 * uninteresting inodes (because they're
458 * before our start point) free.
459 */
460 for (i = 0; i < chunkidx; i++) {
461 if (XFS_INOBT_MASK(i) & ~gfree)
462 gcnt++;
463 }
464 gfree |= XFS_INOBT_MASKN(0, chunkidx);
Nathan Scott262750932006-09-28 11:02:03 +1000465 irbp->ir_startino = gino;
466 irbp->ir_freecount = gcnt;
467 irbp->ir_free = gfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 irbp++;
469 agino = gino + XFS_INODES_PER_CHUNK;
470 icount = XFS_INODES_PER_CHUNK - gcnt;
471 } else {
472 /*
473 * If any of those tests failed, bump the
474 * inode number (just in case).
475 */
476 agino++;
477 icount = 0;
478 }
479 /*
480 * In any case, increment to the next record.
481 */
482 if (!error)
483 error = xfs_inobt_increment(cur, 0, &tmp);
484 } else {
485 /*
486 * Start of ag. Lookup the first inode chunk.
487 */
488 error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &tmp);
489 icount = 0;
490 }
491 /*
492 * Loop through inode btree records in this ag,
493 * until we run out of inodes or space in the buffer.
494 */
495 while (irbp < irbufend && icount < ubcount) {
496 /*
497 * Loop as long as we're unable to read the
498 * inode btree.
499 */
500 while (error) {
501 agino += XFS_INODES_PER_CHUNK;
502 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100503 be32_to_cpu(agi->agi_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 error = xfs_inobt_lookup_ge(cur, agino, 0, 0,
506 &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100507 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 /*
510 * If ran off the end of the ag either with an error,
511 * or the normal way, set end and stop collecting.
512 */
513 if (error ||
514 (error = xfs_inobt_get_rec(cur, &gino, &gcnt,
515 &gfree, &i)) ||
516 i == 0) {
517 end_of_ag = 1;
518 break;
519 }
520 /*
521 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000522 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
524 if (gcnt < XFS_INODES_PER_CHUNK) {
Nathan Scott262750932006-09-28 11:02:03 +1000525 /*
526 * Loop over all clusters in the next chunk.
527 * Do a readahead if there are any allocated
528 * inodes in that cluster.
529 */
530 for (agbno = XFS_AGINO_TO_AGBNO(mp, gino),
531 chunkidx = 0;
532 chunkidx < XFS_INODES_PER_CHUNK;
533 chunkidx += nicluster,
534 agbno += nbcluster) {
535 if (XFS_INOBT_MASKN(chunkidx,
536 nicluster) & ~gfree)
537 xfs_btree_reada_bufs(mp, agno,
538 agbno, nbcluster);
539 }
540 irbp->ir_startino = gino;
541 irbp->ir_freecount = gcnt;
542 irbp->ir_free = gfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 irbp++;
544 icount += XFS_INODES_PER_CHUNK - gcnt;
545 }
546 /*
547 * Set agino to after this chunk and bump the cursor.
548 */
549 agino = gino + XFS_INODES_PER_CHUNK;
550 error = xfs_inobt_increment(cur, 0, &tmp);
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100551 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 /*
554 * Drop the btree buffers and the agi buffer.
555 * We can't hold any of the locks these represent
556 * when calling iget.
557 */
558 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
559 xfs_buf_relse(agbp);
560 /*
561 * Now format all the good inodes into the user's buffer.
562 */
563 irbufend = irbp;
564 for (irbp = irbuf;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100565 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * Now process this chunk of inodes.
568 */
Nathan Scott262750932006-09-28 11:02:03 +1000569 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100570 XFS_BULKSTAT_UBLEFT(ubleft) &&
Nathan Scott262750932006-09-28 11:02:03 +1000571 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 chunkidx++, clustidx++, agino++) {
573 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
574 /*
575 * Recompute agbno if this is the
576 * first inode of the cluster.
577 *
578 * Careful with clustidx. There can be
579 * multple clusters per chunk, a single
580 * cluster per chunk or a cluster that has
581 * inodes represented from several different
582 * chunks (if blocksize is large).
583 *
584 * Because of this, the starting clustidx is
585 * initialized to zero in this loop but must
586 * later be reset after reading in the cluster
587 * buffer.
588 */
589 if ((chunkidx & (nicluster - 1)) == 0) {
590 agbno = XFS_AGINO_TO_AGBNO(mp,
Nathan Scott262750932006-09-28 11:02:03 +1000591 irbp->ir_startino) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ((chunkidx & nimask) >>
593 mp->m_sb.sb_inopblog);
594
Nathan Scott8b56f082006-09-28 11:01:46 +1000595 if (flags & (BULKSTAT_FG_QUICK |
596 BULKSTAT_FG_INLINE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 ino = XFS_AGINO_TO_INO(mp, agno,
598 agino);
599 bno = XFS_AGB_TO_DADDR(mp, agno,
600 agbno);
601
602 /*
603 * Get the inode cluster buffer
604 */
605 ASSERT(xfs_inode_zone != NULL);
606 ip = kmem_zone_zalloc(xfs_inode_zone,
607 KM_SLEEP);
608 ip->i_ino = ino;
609 ip->i_mount = mp;
David Chinnerf273ab82006-09-28 11:06:03 +1000610 spin_lock_init(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (bp)
612 xfs_buf_relse(bp);
613 error = xfs_itobp(mp, NULL, ip,
Nathan Scottb12dd342006-03-17 17:26:04 +1100614 &dip, &bp, bno,
David Chinnera3f74ff2008-03-06 13:43:42 +1100615 XFS_IMAP_BULKSTAT,
616 XFS_BUF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!error)
618 clustidx = ip->i_boffset / mp->m_sb.sb_inodesize;
619 kmem_zone_free(xfs_inode_zone, ip);
620 if (XFS_TEST_ERROR(error != 0,
621 mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
622 XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
623 bp = NULL;
Nathan Scottb12dd342006-03-17 17:26:04 +1100624 ubleft = 0;
625 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
627 }
628 }
629 }
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000630 ino = XFS_AGINO_TO_INO(mp, agno, agino);
631 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Skip if this inode is free.
634 */
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000635 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
636 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * Count used inodes as free so we can tell
641 * when the chunk is used up.
642 */
Nathan Scott262750932006-09-28 11:02:03 +1000643 irbp->ir_freecount++;
Nathan Scott8b56f082006-09-28 11:01:46 +1000644 if (!xfs_bulkstat_use_dinode(mp, flags, bp,
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000645 clustidx, &dip)) {
646 lastino = ino;
Nathan Scott8b56f082006-09-28 11:01:46 +1000647 continue;
Lachlan McIlroyc2cba572007-10-12 11:12:20 +1000648 }
Nathan Scott8b56f082006-09-28 11:01:46 +1000649 /*
650 * If we need to do an iget, cannot hold bp.
651 * Drop it, until starting the next cluster.
652 */
653 if ((flags & BULKSTAT_FG_INLINE) && !dip) {
654 if (bp)
655 xfs_buf_relse(bp);
656 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
659 /*
660 * Get the inode and fill in a single buffer.
661 * BULKSTAT_FG_QUICK uses dip to fill it in.
662 * BULKSTAT_FG_IGET uses igets.
Nathan Scott8b56f082006-09-28 11:01:46 +1000663 * BULKSTAT_FG_INLINE uses dip if we have an
664 * inline attr fork, else igets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
666 * This is also used to count inodes/blks, etc
667 * in xfs_qm_quotacheck.
668 */
669 ubused = statstruct_size;
670 error = formatter(mp, ino, ubufp,
671 ubleft, private_data,
672 bno, &ubused, dip, &fmterror);
673 if (fmterror == BULKSTAT_RV_NOTHING) {
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100674 if (error && error != ENOENT &&
675 error != EINVAL) {
Vlad Apostolove132f542006-09-28 11:04:31 +1000676 ubleft = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100677 rval = error;
678 break;
679 }
680 lastino = ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 continue;
682 }
683 if (fmterror == BULKSTAT_RV_GIVEUP) {
684 ubleft = 0;
685 ASSERT(error);
686 rval = error;
687 break;
688 }
689 if (ubufp)
690 ubufp += ubused;
691 ubleft -= ubused;
692 ubelem++;
693 lastino = ino;
694 }
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100695
696 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 if (bp)
700 xfs_buf_relse(bp);
701
702 /*
703 * Set up for the next loop iteration.
704 */
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100705 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (end_of_ag) {
707 agno++;
708 agino = 0;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100709 } else
710 agino = XFS_INO_TO_AGINO(mp, lastino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } else
712 break;
713 }
714 /*
715 * Done, we're either out of filesystem or space to put the data.
716 */
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000717 kmem_free(irbuf, irbsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *ubcountp = ubelem;
Lachlan McIlroycd57e592007-11-23 16:30:32 +1100719 /*
720 * Found some inodes, return them now and return the error next time.
721 */
722 if (ubelem)
723 rval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (agno >= mp->m_sb.sb_agcount) {
725 /*
726 * If we ran out of filesystem, mark lastino as off
727 * the end of the filesystem, so the next call
728 * will return immediately.
729 */
730 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
731 *done = 1;
732 } else
733 *lastinop = (xfs_ino_t)lastino;
734
735 return rval;
736}
737
738/*
739 * Return stat information in bulk (by-inode) for the filesystem.
740 * Special case for non-sequential one inode bulkstat.
741 */
742int /* error status */
743xfs_bulkstat_single(
744 xfs_mount_t *mp, /* mount point for filesystem */
745 xfs_ino_t *lastinop, /* inode to return */
746 char __user *buffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000747 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 int count; /* count value for bulkstat call */
750 int error; /* return value */
751 xfs_ino_t ino; /* filesystem inode number */
752 int res; /* result from bs1 */
753
754 /*
755 * note that requesting valid inode numbers which are not allocated
756 * to inodes will most likely cause xfs_itobp to generate warning
757 * messages about bad magic numbers. This is ok. The fact that
758 * the inode isn't actually an inode is handled by the
759 * error check below. Done this way to make the usual case faster
760 * at the expense of the error case.
761 */
762
763 ino = (xfs_ino_t)*lastinop;
764 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
765 NULL, 0, NULL, NULL, &res);
766 if (error) {
767 /*
768 * Special case way failed, do it the "long" way
769 * to see if that works.
770 */
771 (*lastinop)--;
772 count = 1;
773 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
774 NULL, sizeof(xfs_bstat_t), buffer,
775 BULKSTAT_FG_IGET, done))
776 return error;
777 if (count == 0 || (xfs_ino_t)*lastinop != ino)
778 return error == EFSCORRUPTED ?
779 XFS_ERROR(EINVAL) : error;
780 else
781 return 0;
782 }
783 *done = 0;
784 return 0;
785}
786
Michal Marekfaa63e92007-07-11 11:10:19 +1000787int
788xfs_inumbers_fmt(
789 void __user *ubuffer, /* buffer to write to */
790 const xfs_inogrp_t *buffer, /* buffer to read from */
791 long count, /* # of elements to read */
792 long *written) /* # of bytes written */
793{
794 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
795 return -EFAULT;
796 *written = count * sizeof(*buffer);
797 return 0;
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/*
801 * Return inode number table for the filesystem.
802 */
803int /* error status */
804xfs_inumbers(
805 xfs_mount_t *mp, /* mount point for filesystem */
806 xfs_ino_t *lastino, /* last inode returned */
807 int *count, /* size of buffer/count returned */
Michal Marekfaa63e92007-07-11 11:10:19 +1000808 void __user *ubuffer,/* buffer with inode descriptions */
809 inumbers_fmt_pf formatter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 xfs_buf_t *agbp;
812 xfs_agino_t agino;
813 xfs_agnumber_t agno;
814 int bcount;
815 xfs_inogrp_t *buffer;
816 int bufidx;
817 xfs_btree_cur_t *cur;
818 int error;
819 __int32_t gcnt;
820 xfs_inofree_t gfree;
821 xfs_agino_t gino;
822 int i;
823 xfs_ino_t ino;
824 int left;
825 int tmp;
826
827 ino = (xfs_ino_t)*lastino;
828 agno = XFS_INO_TO_AGNO(mp, ino);
829 agino = XFS_INO_TO_AGINO(mp, ino);
830 left = *count;
831 *count = 0;
Tim Shimmine6a4b372007-11-23 16:30:42 +1100832 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
834 error = bufidx = 0;
835 cur = NULL;
836 agbp = NULL;
837 while (left > 0 && agno < mp->m_sb.sb_agcount) {
838 if (agbp == NULL) {
839 down_read(&mp->m_peraglock);
840 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
841 up_read(&mp->m_peraglock);
842 if (error) {
843 /*
844 * If we can't read the AGI of this ag,
845 * then just skip to the next one.
846 */
847 ASSERT(cur == NULL);
848 agbp = NULL;
849 agno++;
850 agino = 0;
851 continue;
852 }
853 cur = xfs_btree_init_cursor(mp, NULL, agbp, agno,
854 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
855 error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp);
856 if (error) {
857 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
858 cur = NULL;
859 xfs_buf_relse(agbp);
860 agbp = NULL;
861 /*
Michael Opdenacker59c51592007-05-09 08:57:56 +0200862 * Move up the last inode in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * chunk. The lookup_ge will always get
864 * us the first inode in the next chunk.
865 */
866 agino += XFS_INODES_PER_CHUNK - 1;
867 continue;
868 }
869 }
870 if ((error = xfs_inobt_get_rec(cur, &gino, &gcnt, &gfree,
871 &i)) ||
872 i == 0) {
873 xfs_buf_relse(agbp);
874 agbp = NULL;
875 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
876 cur = NULL;
877 agno++;
878 agino = 0;
879 continue;
880 }
881 agino = gino + XFS_INODES_PER_CHUNK - 1;
882 buffer[bufidx].xi_startino = XFS_AGINO_TO_INO(mp, agno, gino);
883 buffer[bufidx].xi_alloccount = XFS_INODES_PER_CHUNK - gcnt;
884 buffer[bufidx].xi_allocmask = ~gfree;
885 bufidx++;
886 left--;
887 if (bufidx == bcount) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000888 long written;
889 if (formatter(ubuffer, buffer, bufidx, &written)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 error = XFS_ERROR(EFAULT);
891 break;
892 }
Michal Marekfaa63e92007-07-11 11:10:19 +1000893 ubuffer += written;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *count += bufidx;
895 bufidx = 0;
896 }
897 if (left) {
898 error = xfs_inobt_increment(cur, 0, &tmp);
899 if (error) {
900 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
901 cur = NULL;
902 xfs_buf_relse(agbp);
903 agbp = NULL;
904 /*
905 * The agino value has already been bumped.
906 * Just try to skip up to it.
907 */
908 agino += XFS_INODES_PER_CHUNK;
909 continue;
910 }
911 }
912 }
913 if (!error) {
914 if (bufidx) {
Michal Marekfaa63e92007-07-11 11:10:19 +1000915 long written;
916 if (formatter(ubuffer, buffer, bufidx, &written))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 error = XFS_ERROR(EFAULT);
918 else
919 *count += bufidx;
920 }
921 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
922 }
923 kmem_free(buffer, bcount * sizeof(*buffer));
924 if (cur)
925 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
926 XFS_BTREE_NOERROR));
927 if (agbp)
928 xfs_buf_relse(agbp);
929 return error;
930}