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