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