blob: 0fbbd7b9c696f5be8956f63a268cd7336ca50e0c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070042STATIC int
43xfs_bulkstat_one_iget(
44 xfs_mount_t *mp, /* mount point for filesystem */
45 xfs_ino_t ino, /* inode number to get data for */
46 xfs_daddr_t bno, /* starting bno of inode cluster */
47 xfs_bstat_t *buf, /* return buffer */
48 int *stat) /* BULKSTAT_RV_... */
49{
50 xfs_dinode_core_t *dic; /* dinode core info pointer */
51 xfs_inode_t *ip; /* incore inode pointer */
Nathan Scott67fcaa72006-06-09 17:00:52 +100052 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int error;
54
Nathan Scott745b1f472006-09-28 11:02:23 +100055 error = xfs_iget(mp, NULL, ino,
56 XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (error) {
58 *stat = BULKSTAT_RV_NOTHING;
59 return error;
60 }
61
62 ASSERT(ip != NULL);
63 ASSERT(ip->i_blkno != (xfs_daddr_t)0);
64 if (ip->i_d.di_mode == 0) {
65 *stat = BULKSTAT_RV_NOTHING;
66 error = XFS_ERROR(ENOENT);
67 goto out_iput;
68 }
69
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110070 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 dic = &ip->i_d;
72
73 /* xfs_iget returns the following without needing
74 * further change.
75 */
76 buf->bs_nlink = dic->di_nlink;
77 buf->bs_projid = dic->di_projid;
78 buf->bs_ino = ino;
79 buf->bs_mode = dic->di_mode;
80 buf->bs_uid = dic->di_uid;
81 buf->bs_gid = dic->di_gid;
82 buf->bs_size = dic->di_size;
Nathan Scottca5ccbf2006-01-11 21:03:04 +110083 vn_atime_to_bstime(vp, &buf->bs_atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
85 buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
86 buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
87 buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
88 buf->bs_xflags = xfs_ip2xflags(ip);
89 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
90 buf->bs_extents = dic->di_nextents;
91 buf->bs_gen = dic->di_gen;
92 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
93 buf->bs_dmevmask = dic->di_dmevmask;
94 buf->bs_dmstate = dic->di_dmstate;
95 buf->bs_aextents = dic->di_anextents;
96
97 switch (dic->di_format) {
98 case XFS_DINODE_FMT_DEV:
99 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
100 buf->bs_blksize = BLKDEV_IOSIZE;
101 buf->bs_blocks = 0;
102 break;
103 case XFS_DINODE_FMT_LOCAL:
104 case XFS_DINODE_FMT_UUID:
105 buf->bs_rdev = 0;
106 buf->bs_blksize = mp->m_sb.sb_blocksize;
107 buf->bs_blocks = 0;
108 break;
109 case XFS_DINODE_FMT_EXTENTS:
110 case XFS_DINODE_FMT_BTREE:
111 buf->bs_rdev = 0;
112 buf->bs_blksize = mp->m_sb.sb_blocksize;
113 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
114 break;
115 }
116
117 out_iput:
118 xfs_iput(ip, XFS_ILOCK_SHARED);
119 return error;
120}
121
122STATIC int
123xfs_bulkstat_one_dinode(
124 xfs_mount_t *mp, /* mount point for filesystem */
125 xfs_ino_t ino, /* inode number to get data for */
126 xfs_dinode_t *dip, /* dinode inode pointer */
127 xfs_bstat_t *buf) /* return buffer */
128{
129 xfs_dinode_core_t *dic; /* dinode core info pointer */
130
131 dic = &dip->di_core;
132
133 /*
134 * The inode format changed when we moved the link count and
135 * made it 32 bits long. If this is an old format inode,
136 * convert it in memory to look like a new one. If it gets
137 * flushed to disk we will convert back before flushing or
138 * logging it. We zero out the new projid field and the old link
139 * count field. We'll handle clearing the pad field (the remains
140 * of the old uuid field) when we actually convert the inode to
141 * the new format. We don't change the version number so that we
142 * can distinguish this from a real new format inode.
143 */
144 if (INT_GET(dic->di_version, ARCH_CONVERT) == XFS_DINODE_VERSION_1) {
145 buf->bs_nlink = INT_GET(dic->di_onlink, ARCH_CONVERT);
146 buf->bs_projid = 0;
147 } else {
148 buf->bs_nlink = INT_GET(dic->di_nlink, ARCH_CONVERT);
149 buf->bs_projid = INT_GET(dic->di_projid, ARCH_CONVERT);
150 }
151
152 buf->bs_ino = ino;
153 buf->bs_mode = INT_GET(dic->di_mode, ARCH_CONVERT);
154 buf->bs_uid = INT_GET(dic->di_uid, ARCH_CONVERT);
155 buf->bs_gid = INT_GET(dic->di_gid, ARCH_CONVERT);
156 buf->bs_size = INT_GET(dic->di_size, ARCH_CONVERT);
157 buf->bs_atime.tv_sec = INT_GET(dic->di_atime.t_sec, ARCH_CONVERT);
158 buf->bs_atime.tv_nsec = INT_GET(dic->di_atime.t_nsec, ARCH_CONVERT);
159 buf->bs_mtime.tv_sec = INT_GET(dic->di_mtime.t_sec, ARCH_CONVERT);
160 buf->bs_mtime.tv_nsec = INT_GET(dic->di_mtime.t_nsec, ARCH_CONVERT);
161 buf->bs_ctime.tv_sec = INT_GET(dic->di_ctime.t_sec, ARCH_CONVERT);
162 buf->bs_ctime.tv_nsec = INT_GET(dic->di_ctime.t_nsec, ARCH_CONVERT);
163 buf->bs_xflags = xfs_dic2xflags(dic);
164 buf->bs_extsize = INT_GET(dic->di_extsize, ARCH_CONVERT) << mp->m_sb.sb_blocklog;
165 buf->bs_extents = INT_GET(dic->di_nextents, ARCH_CONVERT);
166 buf->bs_gen = INT_GET(dic->di_gen, ARCH_CONVERT);
167 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
168 buf->bs_dmevmask = INT_GET(dic->di_dmevmask, ARCH_CONVERT);
169 buf->bs_dmstate = INT_GET(dic->di_dmstate, ARCH_CONVERT);
170 buf->bs_aextents = INT_GET(dic->di_anextents, ARCH_CONVERT);
171
172 switch (INT_GET(dic->di_format, ARCH_CONVERT)) {
173 case XFS_DINODE_FMT_DEV:
174 buf->bs_rdev = INT_GET(dip->di_u.di_dev, ARCH_CONVERT);
175 buf->bs_blksize = BLKDEV_IOSIZE;
176 buf->bs_blocks = 0;
177 break;
178 case XFS_DINODE_FMT_LOCAL:
179 case XFS_DINODE_FMT_UUID:
180 buf->bs_rdev = 0;
181 buf->bs_blksize = mp->m_sb.sb_blocksize;
182 buf->bs_blocks = 0;
183 break;
184 case XFS_DINODE_FMT_EXTENTS:
185 case XFS_DINODE_FMT_BTREE:
186 buf->bs_rdev = 0;
187 buf->bs_blksize = mp->m_sb.sb_blocksize;
188 buf->bs_blocks = INT_GET(dic->di_nblocks, ARCH_CONVERT);
189 break;
190 }
191
192 return 0;
193}
194
195/*
196 * Return stat information for one inode.
197 * Return 0 if ok, else errno.
198 */
199int /* error status */
200xfs_bulkstat_one(
201 xfs_mount_t *mp, /* mount point for filesystem */
202 xfs_ino_t ino, /* inode number to get data for */
203 void __user *buffer, /* buffer to place output in */
204 int ubsize, /* size of buffer */
205 void *private_data, /* my private data */
206 xfs_daddr_t bno, /* starting bno of inode cluster */
207 int *ubused, /* bytes used by me */
208 void *dibuff, /* on-disk inode buffer */
209 int *stat) /* BULKSTAT_RV_... */
210{
211 xfs_bstat_t *buf; /* return buffer */
212 int error = 0; /* error value */
213 xfs_dinode_t *dip; /* dinode inode pointer */
214
215 dip = (xfs_dinode_t *)dibuff;
216
217 if (!buffer || ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
218 (XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
219 (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino))) {
220 *stat = BULKSTAT_RV_NOTHING;
221 return XFS_ERROR(EINVAL);
222 }
223 if (ubsize < sizeof(*buf)) {
224 *stat = BULKSTAT_RV_NOTHING;
225 return XFS_ERROR(ENOMEM);
226 }
227
228 buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
229
230 if (dip == NULL) {
231 /* We're not being passed a pointer to a dinode. This happens
232 * if BULKSTAT_FG_IGET is selected. Do the iget.
233 */
234 error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
235 if (error)
236 goto out_free;
237 } else {
238 xfs_bulkstat_one_dinode(mp, ino, dip, buf);
239 }
240
241 if (copy_to_user(buffer, buf, sizeof(*buf))) {
242 *stat = BULKSTAT_RV_NOTHING;
243 error = EFAULT;
244 goto out_free;
245 }
246
247 *stat = BULKSTAT_RV_DIDONE;
248 if (ubused)
249 *ubused = sizeof(*buf);
250
251 out_free:
252 kmem_free(buf, sizeof(*buf));
253 return error;
254}
255
256/*
Nathan Scott8b56f082006-09-28 11:01:46 +1000257 * Test to see whether we can use the ondisk inode directly, based
258 * on the given bulkstat flags, filling in dipp accordingly.
259 * Returns zero if the inode is dodgey.
260 */
261STATIC int
262xfs_bulkstat_use_dinode(
263 xfs_mount_t *mp,
264 int flags,
265 xfs_buf_t *bp,
266 int clustidx,
267 xfs_dinode_t **dipp)
268{
269 xfs_dinode_t *dip;
270 unsigned int aformat;
271
272 *dipp = NULL;
273 if (!bp || (flags & BULKSTAT_FG_IGET))
274 return 1;
275 dip = (xfs_dinode_t *)
276 xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
277 if (INT_GET(dip->di_core.di_magic, ARCH_CONVERT) != XFS_DINODE_MAGIC ||
278 !XFS_DINODE_GOOD_VERSION(
279 INT_GET(dip->di_core.di_version, ARCH_CONVERT)))
280 return 0;
281 if (flags & BULKSTAT_FG_QUICK) {
282 *dipp = dip;
283 return 1;
284 }
285 /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
286 aformat = INT_GET(dip->di_core.di_aformat, ARCH_CONVERT);
287 if ((XFS_CFORK_Q(&dip->di_core) == 0) ||
288 (aformat == XFS_DINODE_FMT_LOCAL) ||
289 (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_core.di_anextents)) {
290 *dipp = dip;
291 return 1;
292 }
293 return 1;
294}
295
296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Return stat information in bulk (by-inode) for the filesystem.
298 */
299int /* error status */
300xfs_bulkstat(
301 xfs_mount_t *mp, /* mount point for filesystem */
302 xfs_ino_t *lastinop, /* last inode returned */
303 int *ubcountp, /* size of buffer/count returned */
304 bulkstat_one_pf formatter, /* func that'd fill a single buf */
305 void *private_data,/* private data for formatter */
306 size_t statstruct_size, /* sizeof struct filling */
307 char __user *ubuffer, /* buffer with inode stats */
308 int flags, /* defined in xfs_itable.h */
Nathan Scottc41564b2006-03-29 08:55:14 +1000309 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 xfs_agblock_t agbno=0;/* allocation group block number */
312 xfs_buf_t *agbp; /* agi header buffer */
313 xfs_agi_t *agi; /* agi header data */
314 xfs_agino_t agino; /* inode # in allocation group */
315 xfs_agnumber_t agno; /* allocation group number */
316 xfs_daddr_t bno; /* inode cluster start daddr */
317 int chunkidx; /* current index into inode chunk */
318 int clustidx; /* current index into inode cluster */
319 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
320 int end_of_ag; /* set if we've seen the ag end */
321 int error; /* error code */
322 int fmterror;/* bulkstat formatter result */
323 __int32_t gcnt; /* current btree rec's count */
324 xfs_inofree_t gfree; /* current btree rec's free mask */
325 xfs_agino_t gino; /* current btree rec's start inode */
326 int i; /* loop index */
327 int icount; /* count of inodes good in irbuf */
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000328 int irbsize; /* size of irec buffer in bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 xfs_ino_t ino; /* inode number (filesystem) */
Nathan Scott262750932006-09-28 11:02:03 +1000330 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
331 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
332 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 xfs_ino_t lastino=0; /* last inode number returned */
334 int nbcluster; /* # of blocks in a cluster */
335 int nicluster; /* # of inodes in a cluster */
336 int nimask; /* mask for inode clusters */
337 int nirbuf; /* size of irbuf */
338 int rval; /* return value error code */
339 int tmp; /* result value from btree calls */
340 int ubcount; /* size of user's buffer */
341 int ubleft; /* bytes left in user's buffer */
342 char __user *ubufp; /* pointer into user's buffer */
343 int ubelem; /* spaces used in user's buffer */
344 int ubused; /* bytes used by formatter */
345 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
346 xfs_dinode_t *dip; /* ptr into bp for specific inode */
347 xfs_inode_t *ip; /* ptr to in-core inode struct */
348
349 /*
350 * Get the last inode value, see if there's nothing to do.
351 */
352 ino = (xfs_ino_t)*lastinop;
353 dip = NULL;
354 agno = XFS_INO_TO_AGNO(mp, ino);
355 agino = XFS_INO_TO_AGINO(mp, ino);
356 if (agno >= mp->m_sb.sb_agcount ||
357 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
358 *done = 1;
359 *ubcountp = 0;
360 return 0;
361 }
362 ubcount = *ubcountp; /* statstruct's */
363 ubleft = ubcount * statstruct_size; /* bytes */
364 *ubcountp = ubelem = 0;
365 *done = 0;
366 fmterror = 0;
367 ubufp = ubuffer;
368 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
369 mp->m_sb.sb_inopblock :
370 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
371 nimask = ~(nicluster - 1);
372 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
Nathan Scott77e46352006-09-28 11:03:27 +1000373 irbuf = kmem_zalloc_greedy(&irbsize, NBPC, NBPC * 4,
374 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000375 nirbuf = irbsize / sizeof(*irbuf);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Loop over the allocation groups, starting from the last
379 * inode returned; 0 means start of the allocation group.
380 */
381 rval = 0;
382 while (ubleft >= statstruct_size && agno < mp->m_sb.sb_agcount) {
383 bp = NULL;
384 down_read(&mp->m_peraglock);
385 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
386 up_read(&mp->m_peraglock);
387 if (error) {
388 /*
389 * Skip this allocation group and go to the next one.
390 */
391 agno++;
392 agino = 0;
393 continue;
394 }
395 agi = XFS_BUF_TO_AGI(agbp);
396 /*
397 * Allocate and initialize a btree cursor for ialloc btree.
398 */
399 cur = xfs_btree_init_cursor(mp, NULL, agbp, agno, XFS_BTNUM_INO,
Nathan Scott262750932006-09-28 11:02:03 +1000400 (xfs_inode_t *)0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 irbp = irbuf;
402 irbufend = irbuf + nirbuf;
403 end_of_ag = 0;
404 /*
405 * If we're returning in the middle of an allocation group,
406 * we need to get the remainder of the chunk we're in.
407 */
408 if (agino > 0) {
409 /*
410 * Lookup the inode chunk that this inode lives in.
411 */
412 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &tmp);
413 if (!error && /* no I/O error */
414 tmp && /* lookup succeeded */
415 /* got the record, should always work */
416 !(error = xfs_inobt_get_rec(cur, &gino, &gcnt,
417 &gfree, &i)) &&
418 i == 1 &&
419 /* this is the right chunk */
420 agino < gino + XFS_INODES_PER_CHUNK &&
421 /* lastino was not last in chunk */
422 (chunkidx = agino - gino + 1) <
423 XFS_INODES_PER_CHUNK &&
424 /* there are some left allocated */
425 XFS_INOBT_MASKN(chunkidx,
426 XFS_INODES_PER_CHUNK - chunkidx) & ~gfree) {
427 /*
428 * Grab the chunk record. Mark all the
429 * uninteresting inodes (because they're
430 * before our start point) free.
431 */
432 for (i = 0; i < chunkidx; i++) {
433 if (XFS_INOBT_MASK(i) & ~gfree)
434 gcnt++;
435 }
436 gfree |= XFS_INOBT_MASKN(0, chunkidx);
Nathan Scott262750932006-09-28 11:02:03 +1000437 irbp->ir_startino = gino;
438 irbp->ir_freecount = gcnt;
439 irbp->ir_free = gfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 irbp++;
441 agino = gino + XFS_INODES_PER_CHUNK;
442 icount = XFS_INODES_PER_CHUNK - gcnt;
443 } else {
444 /*
445 * If any of those tests failed, bump the
446 * inode number (just in case).
447 */
448 agino++;
449 icount = 0;
450 }
451 /*
452 * In any case, increment to the next record.
453 */
454 if (!error)
455 error = xfs_inobt_increment(cur, 0, &tmp);
456 } else {
457 /*
458 * Start of ag. Lookup the first inode chunk.
459 */
460 error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &tmp);
461 icount = 0;
462 }
463 /*
464 * Loop through inode btree records in this ag,
465 * until we run out of inodes or space in the buffer.
466 */
467 while (irbp < irbufend && icount < ubcount) {
468 /*
469 * Loop as long as we're unable to read the
470 * inode btree.
471 */
472 while (error) {
473 agino += XFS_INODES_PER_CHUNK;
474 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
Christoph Hellwig16259e72005-11-02 15:11:25 +1100475 be32_to_cpu(agi->agi_length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 error = xfs_inobt_lookup_ge(cur, agino, 0, 0,
478 &tmp);
479 }
480 /*
481 * If ran off the end of the ag either with an error,
482 * or the normal way, set end and stop collecting.
483 */
484 if (error ||
485 (error = xfs_inobt_get_rec(cur, &gino, &gcnt,
486 &gfree, &i)) ||
487 i == 0) {
488 end_of_ag = 1;
489 break;
490 }
491 /*
492 * If this chunk has any allocated inodes, save it.
Nathan Scott262750932006-09-28 11:02:03 +1000493 * Also start read-ahead now for this chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
495 if (gcnt < XFS_INODES_PER_CHUNK) {
Nathan Scott262750932006-09-28 11:02:03 +1000496 /*
497 * Loop over all clusters in the next chunk.
498 * Do a readahead if there are any allocated
499 * inodes in that cluster.
500 */
501 for (agbno = XFS_AGINO_TO_AGBNO(mp, gino),
502 chunkidx = 0;
503 chunkidx < XFS_INODES_PER_CHUNK;
504 chunkidx += nicluster,
505 agbno += nbcluster) {
506 if (XFS_INOBT_MASKN(chunkidx,
507 nicluster) & ~gfree)
508 xfs_btree_reada_bufs(mp, agno,
509 agbno, nbcluster);
510 }
511 irbp->ir_startino = gino;
512 irbp->ir_freecount = gcnt;
513 irbp->ir_free = gfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 irbp++;
515 icount += XFS_INODES_PER_CHUNK - gcnt;
516 }
517 /*
518 * Set agino to after this chunk and bump the cursor.
519 */
520 agino = gino + XFS_INODES_PER_CHUNK;
521 error = xfs_inobt_increment(cur, 0, &tmp);
522 }
523 /*
524 * Drop the btree buffers and the agi buffer.
525 * We can't hold any of the locks these represent
526 * when calling iget.
527 */
528 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
529 xfs_buf_relse(agbp);
530 /*
531 * Now format all the good inodes into the user's buffer.
532 */
533 irbufend = irbp;
534 for (irbp = irbuf;
535 irbp < irbufend && ubleft >= statstruct_size; irbp++) {
536 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Now process this chunk of inodes.
538 */
Nathan Scott262750932006-09-28 11:02:03 +1000539 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ubleft > 0 &&
Nathan Scott262750932006-09-28 11:02:03 +1000541 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 chunkidx++, clustidx++, agino++) {
543 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
544 /*
545 * Recompute agbno if this is the
546 * first inode of the cluster.
547 *
548 * Careful with clustidx. There can be
549 * multple clusters per chunk, a single
550 * cluster per chunk or a cluster that has
551 * inodes represented from several different
552 * chunks (if blocksize is large).
553 *
554 * Because of this, the starting clustidx is
555 * initialized to zero in this loop but must
556 * later be reset after reading in the cluster
557 * buffer.
558 */
559 if ((chunkidx & (nicluster - 1)) == 0) {
560 agbno = XFS_AGINO_TO_AGBNO(mp,
Nathan Scott262750932006-09-28 11:02:03 +1000561 irbp->ir_startino) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ((chunkidx & nimask) >>
563 mp->m_sb.sb_inopblog);
564
Nathan Scott8b56f082006-09-28 11:01:46 +1000565 if (flags & (BULKSTAT_FG_QUICK |
566 BULKSTAT_FG_INLINE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ino = XFS_AGINO_TO_INO(mp, agno,
568 agino);
569 bno = XFS_AGB_TO_DADDR(mp, agno,
570 agbno);
571
572 /*
573 * Get the inode cluster buffer
574 */
575 ASSERT(xfs_inode_zone != NULL);
576 ip = kmem_zone_zalloc(xfs_inode_zone,
577 KM_SLEEP);
578 ip->i_ino = ino;
579 ip->i_mount = mp;
580 if (bp)
581 xfs_buf_relse(bp);
582 error = xfs_itobp(mp, NULL, ip,
Nathan Scottb12dd342006-03-17 17:26:04 +1100583 &dip, &bp, bno,
584 XFS_IMAP_BULKSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!error)
586 clustidx = ip->i_boffset / mp->m_sb.sb_inodesize;
587 kmem_zone_free(xfs_inode_zone, ip);
588 if (XFS_TEST_ERROR(error != 0,
589 mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
590 XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
591 bp = NULL;
Nathan Scottb12dd342006-03-17 17:26:04 +1100592 ubleft = 0;
593 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595 }
596 }
597 }
598 /*
599 * Skip if this inode is free.
600 */
Nathan Scott262750932006-09-28 11:02:03 +1000601 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 continue;
603 /*
604 * Count used inodes as free so we can tell
605 * when the chunk is used up.
606 */
Nathan Scott262750932006-09-28 11:02:03 +1000607 irbp->ir_freecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 ino = XFS_AGINO_TO_INO(mp, agno, agino);
609 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
Nathan Scott8b56f082006-09-28 11:01:46 +1000610 if (!xfs_bulkstat_use_dinode(mp, flags, bp,
611 clustidx, &dip))
612 continue;
613 /*
614 * If we need to do an iget, cannot hold bp.
615 * Drop it, until starting the next cluster.
616 */
617 if ((flags & BULKSTAT_FG_INLINE) && !dip) {
618 if (bp)
619 xfs_buf_relse(bp);
620 bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 /*
624 * Get the inode and fill in a single buffer.
625 * BULKSTAT_FG_QUICK uses dip to fill it in.
626 * BULKSTAT_FG_IGET uses igets.
Nathan Scott8b56f082006-09-28 11:01:46 +1000627 * BULKSTAT_FG_INLINE uses dip if we have an
628 * inline attr fork, else igets.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
630 * This is also used to count inodes/blks, etc
631 * in xfs_qm_quotacheck.
632 */
633 ubused = statstruct_size;
634 error = formatter(mp, ino, ubufp,
635 ubleft, private_data,
636 bno, &ubused, dip, &fmterror);
637 if (fmterror == BULKSTAT_RV_NOTHING) {
638 if (error == ENOMEM)
639 ubleft = 0;
640 continue;
641 }
642 if (fmterror == BULKSTAT_RV_GIVEUP) {
643 ubleft = 0;
644 ASSERT(error);
645 rval = error;
646 break;
647 }
648 if (ubufp)
649 ubufp += ubused;
650 ubleft -= ubused;
651 ubelem++;
652 lastino = ino;
653 }
654 }
655
656 if (bp)
657 xfs_buf_relse(bp);
658
659 /*
660 * Set up for the next loop iteration.
661 */
662 if (ubleft > 0) {
663 if (end_of_ag) {
664 agno++;
665 agino = 0;
666 } else
667 agino = XFS_INO_TO_AGINO(mp, lastino);
668 } else
669 break;
670 }
671 /*
672 * Done, we're either out of filesystem or space to put the data.
673 */
Nathan Scottbb3c7d22006-09-28 11:02:09 +1000674 kmem_free(irbuf, irbsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *ubcountp = ubelem;
676 if (agno >= mp->m_sb.sb_agcount) {
677 /*
678 * If we ran out of filesystem, mark lastino as off
679 * the end of the filesystem, so the next call
680 * will return immediately.
681 */
682 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
683 *done = 1;
684 } else
685 *lastinop = (xfs_ino_t)lastino;
686
687 return rval;
688}
689
690/*
691 * Return stat information in bulk (by-inode) for the filesystem.
692 * Special case for non-sequential one inode bulkstat.
693 */
694int /* error status */
695xfs_bulkstat_single(
696 xfs_mount_t *mp, /* mount point for filesystem */
697 xfs_ino_t *lastinop, /* inode to return */
698 char __user *buffer, /* buffer with inode stats */
Nathan Scottc41564b2006-03-29 08:55:14 +1000699 int *done) /* 1 if there are more stats to get */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 int count; /* count value for bulkstat call */
702 int error; /* return value */
703 xfs_ino_t ino; /* filesystem inode number */
704 int res; /* result from bs1 */
705
706 /*
707 * note that requesting valid inode numbers which are not allocated
708 * to inodes will most likely cause xfs_itobp to generate warning
709 * messages about bad magic numbers. This is ok. The fact that
710 * the inode isn't actually an inode is handled by the
711 * error check below. Done this way to make the usual case faster
712 * at the expense of the error case.
713 */
714
715 ino = (xfs_ino_t)*lastinop;
716 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
717 NULL, 0, NULL, NULL, &res);
718 if (error) {
719 /*
720 * Special case way failed, do it the "long" way
721 * to see if that works.
722 */
723 (*lastinop)--;
724 count = 1;
725 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
726 NULL, sizeof(xfs_bstat_t), buffer,
727 BULKSTAT_FG_IGET, done))
728 return error;
729 if (count == 0 || (xfs_ino_t)*lastinop != ino)
730 return error == EFSCORRUPTED ?
731 XFS_ERROR(EINVAL) : error;
732 else
733 return 0;
734 }
735 *done = 0;
736 return 0;
737}
738
739/*
740 * Return inode number table for the filesystem.
741 */
742int /* error status */
743xfs_inumbers(
744 xfs_mount_t *mp, /* mount point for filesystem */
745 xfs_ino_t *lastino, /* last inode returned */
746 int *count, /* size of buffer/count returned */
747 xfs_inogrp_t __user *ubuffer)/* buffer with inode descriptions */
748{
749 xfs_buf_t *agbp;
750 xfs_agino_t agino;
751 xfs_agnumber_t agno;
752 int bcount;
753 xfs_inogrp_t *buffer;
754 int bufidx;
755 xfs_btree_cur_t *cur;
756 int error;
757 __int32_t gcnt;
758 xfs_inofree_t gfree;
759 xfs_agino_t gino;
760 int i;
761 xfs_ino_t ino;
762 int left;
763 int tmp;
764
765 ino = (xfs_ino_t)*lastino;
766 agno = XFS_INO_TO_AGNO(mp, ino);
767 agino = XFS_INO_TO_AGINO(mp, ino);
768 left = *count;
769 *count = 0;
770 bcount = MIN(left, (int)(NBPP / sizeof(*buffer)));
771 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
772 error = bufidx = 0;
773 cur = NULL;
774 agbp = NULL;
775 while (left > 0 && agno < mp->m_sb.sb_agcount) {
776 if (agbp == NULL) {
777 down_read(&mp->m_peraglock);
778 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
779 up_read(&mp->m_peraglock);
780 if (error) {
781 /*
782 * If we can't read the AGI of this ag,
783 * then just skip to the next one.
784 */
785 ASSERT(cur == NULL);
786 agbp = NULL;
787 agno++;
788 agino = 0;
789 continue;
790 }
791 cur = xfs_btree_init_cursor(mp, NULL, agbp, agno,
792 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
793 error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp);
794 if (error) {
795 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
796 cur = NULL;
797 xfs_buf_relse(agbp);
798 agbp = NULL;
799 /*
800 * Move up the the last inode in the current
801 * chunk. The lookup_ge will always get
802 * us the first inode in the next chunk.
803 */
804 agino += XFS_INODES_PER_CHUNK - 1;
805 continue;
806 }
807 }
808 if ((error = xfs_inobt_get_rec(cur, &gino, &gcnt, &gfree,
809 &i)) ||
810 i == 0) {
811 xfs_buf_relse(agbp);
812 agbp = NULL;
813 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
814 cur = NULL;
815 agno++;
816 agino = 0;
817 continue;
818 }
819 agino = gino + XFS_INODES_PER_CHUNK - 1;
820 buffer[bufidx].xi_startino = XFS_AGINO_TO_INO(mp, agno, gino);
821 buffer[bufidx].xi_alloccount = XFS_INODES_PER_CHUNK - gcnt;
822 buffer[bufidx].xi_allocmask = ~gfree;
823 bufidx++;
824 left--;
825 if (bufidx == bcount) {
826 if (copy_to_user(ubuffer, buffer,
827 bufidx * sizeof(*buffer))) {
828 error = XFS_ERROR(EFAULT);
829 break;
830 }
831 ubuffer += bufidx;
832 *count += bufidx;
833 bufidx = 0;
834 }
835 if (left) {
836 error = xfs_inobt_increment(cur, 0, &tmp);
837 if (error) {
838 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
839 cur = NULL;
840 xfs_buf_relse(agbp);
841 agbp = NULL;
842 /*
843 * The agino value has already been bumped.
844 * Just try to skip up to it.
845 */
846 agino += XFS_INODES_PER_CHUNK;
847 continue;
848 }
849 }
850 }
851 if (!error) {
852 if (bufidx) {
853 if (copy_to_user(ubuffer, buffer,
854 bufidx * sizeof(*buffer)))
855 error = XFS_ERROR(EFAULT);
856 else
857 *count += bufidx;
858 }
859 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
860 }
861 kmem_free(buffer, bcount * sizeof(*buffer));
862 if (cur)
863 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
864 XFS_BTREE_NOERROR));
865 if (agbp)
866 xfs_buf_relse(agbp);
867 return error;
868}