xfs: improve xfs_inobt_get_rec prototype
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xfs / xfs_itable.c
blob3ec13e8a8c54006467ccd96a03718be67b7f459f
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * 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.
14 * 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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_itable.h"
39 #include "xfs_error.h"
40 #include "xfs_btree.h"
42 STATIC int
43 xfs_internal_inum(
44 xfs_mount_t *mp,
45 xfs_ino_t ino)
47 return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
48 (xfs_sb_version_hasquota(&mp->m_sb) &&
49 (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino)));
52 STATIC int
53 xfs_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_... */
60 xfs_icdinode_t *dic; /* dinode core info pointer */
61 xfs_inode_t *ip; /* incore inode pointer */
62 int error;
64 error = xfs_iget(mp, NULL, ino,
65 XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno);
66 if (error) {
67 *stat = BULKSTAT_RV_NOTHING;
68 return error;
71 ASSERT(ip != NULL);
72 ASSERT(ip->i_imap.im_blkno != 0);
74 dic = &ip->i_d;
76 /* xfs_iget returns the following without needing
77 * further change.
79 buf->bs_nlink = dic->di_nlink;
80 buf->bs_projid = dic->di_projid;
81 buf->bs_ino = ino;
82 buf->bs_mode = dic->di_mode;
83 buf->bs_uid = dic->di_uid;
84 buf->bs_gid = dic->di_gid;
85 buf->bs_size = dic->di_size;
87 * We are reading the atime from the Linux inode because the
88 * dinode might not be uptodate.
90 buf->bs_atime.tv_sec = VFS_I(ip)->i_atime.tv_sec;
91 buf->bs_atime.tv_nsec = VFS_I(ip)->i_atime.tv_nsec;
92 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;
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;
125 xfs_iput(ip, XFS_ILOCK_SHARED);
126 return error;
129 STATIC void
130 xfs_bulkstat_one_dinode(
131 xfs_mount_t *mp, /* mount point for filesystem */
132 xfs_ino_t ino, /* inode number to get data for */
133 xfs_dinode_t *dic, /* dinode inode pointer */
134 xfs_bstat_t *buf) /* return buffer */
137 * The inode format changed when we moved the link count and
138 * made it 32 bits long. If this is an old format inode,
139 * convert it in memory to look like a new one. If it gets
140 * flushed to disk we will convert back before flushing or
141 * logging it. We zero out the new projid field and the old link
142 * count field. We'll handle clearing the pad field (the remains
143 * of the old uuid field) when we actually convert the inode to
144 * the new format. We don't change the version number so that we
145 * can distinguish this from a real new format inode.
147 if (dic->di_version == 1) {
148 buf->bs_nlink = be16_to_cpu(dic->di_onlink);
149 buf->bs_projid = 0;
150 } else {
151 buf->bs_nlink = be32_to_cpu(dic->di_nlink);
152 buf->bs_projid = be16_to_cpu(dic->di_projid);
155 buf->bs_ino = ino;
156 buf->bs_mode = be16_to_cpu(dic->di_mode);
157 buf->bs_uid = be32_to_cpu(dic->di_uid);
158 buf->bs_gid = be32_to_cpu(dic->di_gid);
159 buf->bs_size = be64_to_cpu(dic->di_size);
160 buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec);
161 buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec);
162 buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec);
163 buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec);
164 buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec);
165 buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec);
166 buf->bs_xflags = xfs_dic2xflags(dic);
167 buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog;
168 buf->bs_extents = be32_to_cpu(dic->di_nextents);
169 buf->bs_gen = be32_to_cpu(dic->di_gen);
170 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
171 buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask);
172 buf->bs_dmstate = be16_to_cpu(dic->di_dmstate);
173 buf->bs_aextents = be16_to_cpu(dic->di_anextents);
175 switch (dic->di_format) {
176 case XFS_DINODE_FMT_DEV:
177 buf->bs_rdev = xfs_dinode_get_rdev(dic);
178 buf->bs_blksize = BLKDEV_IOSIZE;
179 buf->bs_blocks = 0;
180 break;
181 case XFS_DINODE_FMT_LOCAL:
182 case XFS_DINODE_FMT_UUID:
183 buf->bs_rdev = 0;
184 buf->bs_blksize = mp->m_sb.sb_blocksize;
185 buf->bs_blocks = 0;
186 break;
187 case XFS_DINODE_FMT_EXTENTS:
188 case XFS_DINODE_FMT_BTREE:
189 buf->bs_rdev = 0;
190 buf->bs_blksize = mp->m_sb.sb_blocksize;
191 buf->bs_blocks = be64_to_cpu(dic->di_nblocks);
192 break;
196 /* Return 0 on success or positive error */
197 STATIC int
198 xfs_bulkstat_one_fmt(
199 void __user *ubuffer,
200 int ubsize,
201 int *ubused,
202 const xfs_bstat_t *buffer)
204 if (ubsize < sizeof(*buffer))
205 return XFS_ERROR(ENOMEM);
206 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
207 return XFS_ERROR(EFAULT);
208 if (ubused)
209 *ubused = sizeof(*buffer);
210 return 0;
214 * Return stat information for one inode.
215 * Return 0 if ok, else errno.
217 int /* error status */
218 xfs_bulkstat_one_int(
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 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
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_... */
229 xfs_bstat_t *buf; /* return buffer */
230 int error = 0; /* error value */
231 xfs_dinode_t *dip; /* dinode inode pointer */
233 dip = (xfs_dinode_t *)dibuff;
234 *stat = BULKSTAT_RV_NOTHING;
236 if (!buffer || xfs_internal_inum(mp, ino))
237 return XFS_ERROR(EINVAL);
239 buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
241 if (dip == NULL) {
242 /* We're not being passed a pointer to a dinode. This happens
243 * if BULKSTAT_FG_IGET is selected. Do the iget.
245 error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
246 if (error)
247 goto out_free;
248 } else {
249 xfs_bulkstat_one_dinode(mp, ino, dip, buf);
252 error = formatter(buffer, ubsize, ubused, buf);
253 if (error)
254 goto out_free;
256 *stat = BULKSTAT_RV_DIDONE;
258 out_free:
259 kmem_free(buf);
260 return error;
264 xfs_bulkstat_one(
265 xfs_mount_t *mp, /* mount point for filesystem */
266 xfs_ino_t ino, /* inode number to get data for */
267 void __user *buffer, /* buffer to place output in */
268 int ubsize, /* size of buffer */
269 void *private_data, /* my private data */
270 xfs_daddr_t bno, /* starting bno of inode cluster */
271 int *ubused, /* bytes used by me */
272 void *dibuff, /* on-disk inode buffer */
273 int *stat) /* BULKSTAT_RV_... */
275 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
276 xfs_bulkstat_one_fmt, bno,
277 ubused, dibuff, stat);
281 * Test to see whether we can use the ondisk inode directly, based
282 * on the given bulkstat flags, filling in dipp accordingly.
283 * Returns zero if the inode is dodgey.
285 STATIC int
286 xfs_bulkstat_use_dinode(
287 xfs_mount_t *mp,
288 int flags,
289 xfs_buf_t *bp,
290 int clustidx,
291 xfs_dinode_t **dipp)
293 xfs_dinode_t *dip;
294 unsigned int aformat;
296 *dipp = NULL;
297 if (!bp || (flags & BULKSTAT_FG_IGET))
298 return 1;
299 dip = (xfs_dinode_t *)
300 xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
302 * Check the buffer containing the on-disk inode for di_mode == 0.
303 * This is to prevent xfs_bulkstat from picking up just reclaimed
304 * inodes that have their in-core state initialized but not flushed
305 * to disk yet. This is a temporary hack that would require a proper
306 * fix in the future.
308 if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
309 !XFS_DINODE_GOOD_VERSION(dip->di_version) ||
310 !dip->di_mode)
311 return 0;
312 if (flags & BULKSTAT_FG_QUICK) {
313 *dipp = dip;
314 return 1;
316 /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
317 aformat = dip->di_aformat;
318 if ((XFS_DFORK_Q(dip) == 0) ||
319 (aformat == XFS_DINODE_FMT_LOCAL) ||
320 (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_anextents)) {
321 *dipp = dip;
322 return 1;
324 return 1;
327 #define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
330 * Return stat information in bulk (by-inode) for the filesystem.
332 int /* error status */
333 xfs_bulkstat(
334 xfs_mount_t *mp, /* mount point for filesystem */
335 xfs_ino_t *lastinop, /* last inode returned */
336 int *ubcountp, /* size of buffer/count returned */
337 bulkstat_one_pf formatter, /* func that'd fill a single buf */
338 void *private_data,/* private data for formatter */
339 size_t statstruct_size, /* sizeof struct filling */
340 char __user *ubuffer, /* buffer with inode stats */
341 int flags, /* defined in xfs_itable.h */
342 int *done) /* 1 if there are more stats to get */
344 xfs_agblock_t agbno=0;/* allocation group block number */
345 xfs_buf_t *agbp; /* agi header buffer */
346 xfs_agi_t *agi; /* agi header data */
347 xfs_agino_t agino; /* inode # in allocation group */
348 xfs_agnumber_t agno; /* allocation group number */
349 xfs_daddr_t bno; /* inode cluster start daddr */
350 int chunkidx; /* current index into inode chunk */
351 int clustidx; /* current index into inode cluster */
352 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
353 int end_of_ag; /* set if we've seen the ag end */
354 int error; /* error code */
355 int fmterror;/* bulkstat formatter result */
356 int i; /* loop index */
357 int icount; /* count of inodes good in irbuf */
358 size_t irbsize; /* size of irec buffer in bytes */
359 xfs_ino_t ino; /* inode number (filesystem) */
360 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
361 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
362 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
363 xfs_ino_t lastino; /* last inode number returned */
364 int nbcluster; /* # of blocks in a cluster */
365 int nicluster; /* # of inodes in a cluster */
366 int nimask; /* mask for inode clusters */
367 int nirbuf; /* size of irbuf */
368 int rval; /* return value error code */
369 int tmp; /* result value from btree calls */
370 int ubcount; /* size of user's buffer */
371 int ubleft; /* bytes left in user's buffer */
372 char __user *ubufp; /* pointer into user's buffer */
373 int ubelem; /* spaces used in user's buffer */
374 int ubused; /* bytes used by formatter */
375 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
376 xfs_dinode_t *dip; /* ptr into bp for specific inode */
379 * Get the last inode value, see if there's nothing to do.
381 ino = (xfs_ino_t)*lastinop;
382 lastino = ino;
383 dip = NULL;
384 agno = XFS_INO_TO_AGNO(mp, ino);
385 agino = XFS_INO_TO_AGINO(mp, ino);
386 if (agno >= mp->m_sb.sb_agcount ||
387 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
388 *done = 1;
389 *ubcountp = 0;
390 return 0;
392 if (!ubcountp || *ubcountp <= 0) {
393 return EINVAL;
395 ubcount = *ubcountp; /* statstruct's */
396 ubleft = ubcount * statstruct_size; /* bytes */
397 *ubcountp = ubelem = 0;
398 *done = 0;
399 fmterror = 0;
400 ubufp = ubuffer;
401 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
402 mp->m_sb.sb_inopblock :
403 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
404 nimask = ~(nicluster - 1);
405 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
406 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4,
407 KM_SLEEP | KM_MAYFAIL | KM_LARGE);
408 nirbuf = irbsize / sizeof(*irbuf);
411 * Loop over the allocation groups, starting from the last
412 * inode returned; 0 means start of the allocation group.
414 rval = 0;
415 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
416 cond_resched();
417 bp = NULL;
418 down_read(&mp->m_peraglock);
419 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
420 up_read(&mp->m_peraglock);
421 if (error) {
423 * Skip this allocation group and go to the next one.
425 agno++;
426 agino = 0;
427 continue;
429 agi = XFS_BUF_TO_AGI(agbp);
431 * Allocate and initialize a btree cursor for ialloc btree.
433 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
434 irbp = irbuf;
435 irbufend = irbuf + nirbuf;
436 end_of_ag = 0;
438 * If we're returning in the middle of an allocation group,
439 * we need to get the remainder of the chunk we're in.
441 if (agino > 0) {
442 xfs_inobt_rec_incore_t r;
445 * Lookup the inode chunk that this inode lives in.
447 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &tmp);
448 if (!error && /* no I/O error */
449 tmp && /* lookup succeeded */
450 /* got the record, should always work */
451 !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
452 i == 1 &&
453 /* this is the right chunk */
454 agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
455 /* lastino was not last in chunk */
456 (chunkidx = agino - r.ir_startino + 1) <
457 XFS_INODES_PER_CHUNK &&
458 /* there are some left allocated */
459 xfs_inobt_maskn(chunkidx,
460 XFS_INODES_PER_CHUNK - chunkidx) &
461 ~r.ir_free) {
463 * Grab the chunk record. Mark all the
464 * uninteresting inodes (because they're
465 * before our start point) free.
467 for (i = 0; i < chunkidx; i++) {
468 if (XFS_INOBT_MASK(i) & ~r.ir_free)
469 r.ir_freecount++;
471 r.ir_free |= xfs_inobt_maskn(0, chunkidx);
472 irbp->ir_startino = r.ir_startino;
473 irbp->ir_freecount = r.ir_freecount;
474 irbp->ir_free = r.ir_free;
475 irbp++;
476 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
477 icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
478 } else {
480 * If any of those tests failed, bump the
481 * inode number (just in case).
483 agino++;
484 icount = 0;
487 * In any case, increment to the next record.
489 if (!error)
490 error = xfs_btree_increment(cur, 0, &tmp);
491 } else {
493 * Start of ag. Lookup the first inode chunk.
495 error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &tmp);
496 icount = 0;
499 * Loop through inode btree records in this ag,
500 * until we run out of inodes or space in the buffer.
502 while (irbp < irbufend && icount < ubcount) {
503 xfs_inobt_rec_incore_t r;
506 * Loop as long as we're unable to read the
507 * inode btree.
509 while (error) {
510 agino += XFS_INODES_PER_CHUNK;
511 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
512 be32_to_cpu(agi->agi_length))
513 break;
514 error = xfs_inobt_lookup_ge(cur, agino, 0, 0,
515 &tmp);
516 cond_resched();
519 * If ran off the end of the ag either with an error,
520 * or the normal way, set end and stop collecting.
522 if (error) {
523 end_of_ag = 1;
524 break;
527 error = xfs_inobt_get_rec(cur, &r, &i);
528 if (error || i == 0) {
529 end_of_ag = 1;
530 break;
534 * If this chunk has any allocated inodes, save it.
535 * Also start read-ahead now for this chunk.
537 if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
539 * Loop over all clusters in the next chunk.
540 * Do a readahead if there are any allocated
541 * inodes in that cluster.
543 agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
544 for (chunkidx = 0;
545 chunkidx < XFS_INODES_PER_CHUNK;
546 chunkidx += nicluster,
547 agbno += nbcluster) {
548 if (xfs_inobt_maskn(chunkidx, nicluster)
549 & ~r.ir_free)
550 xfs_btree_reada_bufs(mp, agno,
551 agbno, nbcluster);
553 irbp->ir_startino = r.ir_startino;
554 irbp->ir_freecount = r.ir_freecount;
555 irbp->ir_free = r.ir_free;
556 irbp++;
557 icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
560 * Set agino to after this chunk and bump the cursor.
562 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
563 error = xfs_btree_increment(cur, 0, &tmp);
564 cond_resched();
567 * Drop the btree buffers and the agi buffer.
568 * We can't hold any of the locks these represent
569 * when calling iget.
571 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
572 xfs_buf_relse(agbp);
574 * Now format all the good inodes into the user's buffer.
576 irbufend = irbp;
577 for (irbp = irbuf;
578 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
580 * Now process this chunk of inodes.
582 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
583 XFS_BULKSTAT_UBLEFT(ubleft) &&
584 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
585 chunkidx++, clustidx++, agino++) {
586 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
588 * Recompute agbno if this is the
589 * first inode of the cluster.
591 * Careful with clustidx. There can be
592 * multiple clusters per chunk, a single
593 * cluster per chunk or a cluster that has
594 * inodes represented from several different
595 * chunks (if blocksize is large).
597 * Because of this, the starting clustidx is
598 * initialized to zero in this loop but must
599 * later be reset after reading in the cluster
600 * buffer.
602 if ((chunkidx & (nicluster - 1)) == 0) {
603 agbno = XFS_AGINO_TO_AGBNO(mp,
604 irbp->ir_startino) +
605 ((chunkidx & nimask) >>
606 mp->m_sb.sb_inopblog);
608 if (flags & (BULKSTAT_FG_QUICK |
609 BULKSTAT_FG_INLINE)) {
610 int offset;
612 ino = XFS_AGINO_TO_INO(mp, agno,
613 agino);
614 bno = XFS_AGB_TO_DADDR(mp, agno,
615 agbno);
618 * Get the inode cluster buffer
620 if (bp)
621 xfs_buf_relse(bp);
623 error = xfs_inotobp(mp, NULL, ino, &dip,
624 &bp, &offset,
625 XFS_IGET_BULKSTAT);
627 if (!error)
628 clustidx = offset / mp->m_sb.sb_inodesize;
629 if (XFS_TEST_ERROR(error != 0,
630 mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
631 XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
632 bp = NULL;
633 ubleft = 0;
634 rval = error;
635 break;
639 ino = XFS_AGINO_TO_INO(mp, agno, agino);
640 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
642 * Skip if this inode is free.
644 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
645 lastino = ino;
646 continue;
649 * Count used inodes as free so we can tell
650 * when the chunk is used up.
652 irbp->ir_freecount++;
653 if (!xfs_bulkstat_use_dinode(mp, flags, bp,
654 clustidx, &dip)) {
655 lastino = ino;
656 continue;
659 * If we need to do an iget, cannot hold bp.
660 * Drop it, until starting the next cluster.
662 if ((flags & BULKSTAT_FG_INLINE) && !dip) {
663 if (bp)
664 xfs_buf_relse(bp);
665 bp = NULL;
669 * Get the inode and fill in a single buffer.
670 * BULKSTAT_FG_QUICK uses dip to fill it in.
671 * BULKSTAT_FG_IGET uses igets.
672 * BULKSTAT_FG_INLINE uses dip if we have an
673 * inline attr fork, else igets.
674 * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
675 * This is also used to count inodes/blks, etc
676 * in xfs_qm_quotacheck.
678 ubused = statstruct_size;
679 error = formatter(mp, ino, ubufp,
680 ubleft, private_data,
681 bno, &ubused, dip, &fmterror);
682 if (fmterror == BULKSTAT_RV_NOTHING) {
683 if (error && error != ENOENT &&
684 error != EINVAL) {
685 ubleft = 0;
686 rval = error;
687 break;
689 lastino = ino;
690 continue;
692 if (fmterror == BULKSTAT_RV_GIVEUP) {
693 ubleft = 0;
694 ASSERT(error);
695 rval = error;
696 break;
698 if (ubufp)
699 ubufp += ubused;
700 ubleft -= ubused;
701 ubelem++;
702 lastino = ino;
705 cond_resched();
708 if (bp)
709 xfs_buf_relse(bp);
712 * Set up for the next loop iteration.
714 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
715 if (end_of_ag) {
716 agno++;
717 agino = 0;
718 } else
719 agino = XFS_INO_TO_AGINO(mp, lastino);
720 } else
721 break;
724 * Done, we're either out of filesystem or space to put the data.
726 kmem_free(irbuf);
727 *ubcountp = ubelem;
729 * Found some inodes, return them now and return the error next time.
731 if (ubelem)
732 rval = 0;
733 if (agno >= mp->m_sb.sb_agcount) {
735 * If we ran out of filesystem, mark lastino as off
736 * the end of the filesystem, so the next call
737 * will return immediately.
739 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
740 *done = 1;
741 } else
742 *lastinop = (xfs_ino_t)lastino;
744 return rval;
748 * Return stat information in bulk (by-inode) for the filesystem.
749 * Special case for non-sequential one inode bulkstat.
751 int /* error status */
752 xfs_bulkstat_single(
753 xfs_mount_t *mp, /* mount point for filesystem */
754 xfs_ino_t *lastinop, /* inode to return */
755 char __user *buffer, /* buffer with inode stats */
756 int *done) /* 1 if there are more stats to get */
758 int count; /* count value for bulkstat call */
759 int error; /* return value */
760 xfs_ino_t ino; /* filesystem inode number */
761 int res; /* result from bs1 */
764 * note that requesting valid inode numbers which are not allocated
765 * to inodes will most likely cause xfs_itobp to generate warning
766 * messages about bad magic numbers. This is ok. The fact that
767 * the inode isn't actually an inode is handled by the
768 * error check below. Done this way to make the usual case faster
769 * at the expense of the error case.
772 ino = (xfs_ino_t)*lastinop;
773 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
774 NULL, 0, NULL, NULL, &res);
775 if (error) {
777 * Special case way failed, do it the "long" way
778 * to see if that works.
780 (*lastinop)--;
781 count = 1;
782 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
783 NULL, sizeof(xfs_bstat_t), buffer,
784 BULKSTAT_FG_IGET, done))
785 return error;
786 if (count == 0 || (xfs_ino_t)*lastinop != ino)
787 return error == EFSCORRUPTED ?
788 XFS_ERROR(EINVAL) : error;
789 else
790 return 0;
792 *done = 0;
793 return 0;
797 xfs_inumbers_fmt(
798 void __user *ubuffer, /* buffer to write to */
799 const xfs_inogrp_t *buffer, /* buffer to read from */
800 long count, /* # of elements to read */
801 long *written) /* # of bytes written */
803 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
804 return -EFAULT;
805 *written = count * sizeof(*buffer);
806 return 0;
810 * Return inode number table for the filesystem.
812 int /* error status */
813 xfs_inumbers(
814 xfs_mount_t *mp, /* mount point for filesystem */
815 xfs_ino_t *lastino, /* last inode returned */
816 int *count, /* size of buffer/count returned */
817 void __user *ubuffer,/* buffer with inode descriptions */
818 inumbers_fmt_pf formatter)
820 xfs_buf_t *agbp;
821 xfs_agino_t agino;
822 xfs_agnumber_t agno;
823 int bcount;
824 xfs_inogrp_t *buffer;
825 int bufidx;
826 xfs_btree_cur_t *cur;
827 int error;
828 xfs_inobt_rec_incore_t r;
829 int i;
830 xfs_ino_t ino;
831 int left;
832 int tmp;
834 ino = (xfs_ino_t)*lastino;
835 agno = XFS_INO_TO_AGNO(mp, ino);
836 agino = XFS_INO_TO_AGINO(mp, ino);
837 left = *count;
838 *count = 0;
839 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
840 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
841 error = bufidx = 0;
842 cur = NULL;
843 agbp = NULL;
844 while (left > 0 && agno < mp->m_sb.sb_agcount) {
845 if (agbp == NULL) {
846 down_read(&mp->m_peraglock);
847 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
848 up_read(&mp->m_peraglock);
849 if (error) {
851 * If we can't read the AGI of this ag,
852 * then just skip to the next one.
854 ASSERT(cur == NULL);
855 agbp = NULL;
856 agno++;
857 agino = 0;
858 continue;
860 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
861 error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp);
862 if (error) {
863 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
864 cur = NULL;
865 xfs_buf_relse(agbp);
866 agbp = NULL;
868 * Move up the last inode in the current
869 * chunk. The lookup_ge will always get
870 * us the first inode in the next chunk.
872 agino += XFS_INODES_PER_CHUNK - 1;
873 continue;
876 error = xfs_inobt_get_rec(cur, &r, &i);
877 if (error || i == 0) {
878 xfs_buf_relse(agbp);
879 agbp = NULL;
880 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
881 cur = NULL;
882 agno++;
883 agino = 0;
884 continue;
886 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
887 buffer[bufidx].xi_startino =
888 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
889 buffer[bufidx].xi_alloccount =
890 XFS_INODES_PER_CHUNK - r.ir_freecount;
891 buffer[bufidx].xi_allocmask = ~r.ir_free;
892 bufidx++;
893 left--;
894 if (bufidx == bcount) {
895 long written;
896 if (formatter(ubuffer, buffer, bufidx, &written)) {
897 error = XFS_ERROR(EFAULT);
898 break;
900 ubuffer += written;
901 *count += bufidx;
902 bufidx = 0;
904 if (left) {
905 error = xfs_btree_increment(cur, 0, &tmp);
906 if (error) {
907 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
908 cur = NULL;
909 xfs_buf_relse(agbp);
910 agbp = NULL;
912 * The agino value has already been bumped.
913 * Just try to skip up to it.
915 agino += XFS_INODES_PER_CHUNK;
916 continue;
920 if (!error) {
921 if (bufidx) {
922 long written;
923 if (formatter(ubuffer, buffer, bufidx, &written))
924 error = XFS_ERROR(EFAULT);
925 else
926 *count += bufidx;
928 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
930 kmem_free(buffer);
931 if (cur)
932 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
933 XFS_BTREE_NOERROR));
934 if (agbp)
935 xfs_buf_relse(agbp);
936 return error;