Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.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_dir.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_itable.h"
41 #include "xfs_error.h"
42 #include "xfs_btree.h"
43
44 #ifndef HAVE_USERACC
45 #define useracc(ubuffer, size, flags, foo) (0)
46 #define unuseracc(ubuffer, size, flags)
47 #endif
48
49 STATIC int
50 xfs_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 */
59         vnode_t         *vp;
60         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
76         vp = XFS_ITOV(ip);
77         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_xid = dic->di_xid;
89         buf->bs_size = dic->di_size;
90         vn_atime_to_bstime(vp, &buf->bs_atime);
91         buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
92         buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
93         buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
94         buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
95         buf->bs_xflags = xfs_ip2xflags(ip);
96         buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
97         buf->bs_extents = dic->di_nextents;
98         buf->bs_gen = dic->di_gen;
99         memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
100         buf->bs_dmevmask = dic->di_dmevmask;
101         buf->bs_dmstate = dic->di_dmstate;
102         buf->bs_aextents = dic->di_anextents;
103
104         switch (dic->di_format) {
105         case XFS_DINODE_FMT_DEV:
106                 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
107                 buf->bs_blksize = BLKDEV_IOSIZE;
108                 buf->bs_blocks = 0;
109                 break;
110         case XFS_DINODE_FMT_LOCAL:
111         case XFS_DINODE_FMT_UUID:
112                 buf->bs_rdev = 0;
113                 buf->bs_blksize = mp->m_sb.sb_blocksize;
114                 buf->bs_blocks = 0;
115                 break;
116         case XFS_DINODE_FMT_EXTENTS:
117         case XFS_DINODE_FMT_BTREE:
118                 buf->bs_rdev = 0;
119                 buf->bs_blksize = mp->m_sb.sb_blocksize;
120                 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
121                 break;
122         }
123
124  out_iput:
125         xfs_iput(ip, XFS_ILOCK_SHARED);
126         return error;
127 }
128
129 STATIC int
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    *dip,           /* dinode inode pointer */
134         xfs_bstat_t     *buf)           /* return buffer */
135 {
136         xfs_dinode_core_t *dic;         /* dinode core info pointer */
137
138         dic = &dip->di_core;
139
140         /*
141          * The inode format changed when we moved the link count and
142          * made it 32 bits long.  If this is an old format inode,
143          * convert it in memory to look like a new one.  If it gets
144          * flushed to disk we will convert back before flushing or
145          * logging it.  We zero out the new projid field and the old link
146          * count field.  We'll handle clearing the pad field (the remains
147          * of the old uuid field) when we actually convert the inode to
148          * the new format. We don't change the version number so that we
149          * can distinguish this from a real new format inode.
150          */
151         if (INT_GET(dic->di_version, ARCH_CONVERT) == XFS_DINODE_VERSION_1) {
152                 buf->bs_nlink = INT_GET(dic->di_onlink, ARCH_CONVERT);
153                 buf->bs_projid = 0;
154         } else {
155                 buf->bs_nlink = INT_GET(dic->di_nlink, ARCH_CONVERT);
156                 buf->bs_projid = INT_GET(dic->di_projid, ARCH_CONVERT);
157         }
158
159         buf->bs_ino = ino;
160         buf->bs_mode = INT_GET(dic->di_mode, ARCH_CONVERT);
161         buf->bs_uid = INT_GET(dic->di_uid, ARCH_CONVERT);
162         buf->bs_gid = INT_GET(dic->di_gid, ARCH_CONVERT);
163         buf->bs_xid = INT_GET(dic->di_xid, 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  */
207 int                             /* error status */
208 xfs_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  */
267 int                                     /* error status */
268 xfs_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 are 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) >=
454                                                 be32_to_cpu(agi->agi_length))
455                                         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                                                                 XFS_IMAP_BULKSTAT);
569                                                 if (!error)
570                                                         clustidx = ip->i_boffset / mp->m_sb.sb_inodesize;
571                                                 kmem_zone_free(xfs_inode_zone, ip);
572                                                 if (XFS_TEST_ERROR(error != 0,
573                                                                    mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
574                                                                    XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
575                                                         bp = NULL;
576                                                         ubleft = 0;
577                                                         rval = error;
578                                                         break;
579                                                 }
580                                         }
581                                 }
582                                 /*
583                                  * Skip if this inode is free.
584                                  */
585                                 if (XFS_INOBT_MASK(chunkidx) & INT_GET(irbp->ir_free, ARCH_CONVERT))
586                                         continue;
587                                 /*
588                                  * Count used inodes as free so we can tell
589                                  * when the chunk is used up.
590                                  */
591                                 INT_MOD(irbp->ir_freecount, ARCH_CONVERT, +1);
592                                 ino = XFS_AGINO_TO_INO(mp, agno, agino);
593                                 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
594                                 if (flags & BULKSTAT_FG_QUICK) {
595                                         dip = (xfs_dinode_t *)xfs_buf_offset(bp,
596                                               (clustidx << mp->m_sb.sb_inodelog));
597
598                                         if (INT_GET(dip->di_core.di_magic, ARCH_CONVERT)
599                                                     != XFS_DINODE_MAGIC
600                                             || !XFS_DINODE_GOOD_VERSION(
601                                                     INT_GET(dip->di_core.di_version, ARCH_CONVERT)))
602                                                 continue;
603                                 }
604
605                                 /*
606                                  * Get the inode and fill in a single buffer.
607                                  * BULKSTAT_FG_QUICK uses dip to fill it in.
608                                  * BULKSTAT_FG_IGET uses igets.
609                                  * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
610                                  * This is also used to count inodes/blks, etc
611                                  * in xfs_qm_quotacheck.
612                                  */
613                                 ubused = statstruct_size;
614                                 error = formatter(mp, ino, ubufp,
615                                                 ubleft, private_data,
616                                                 bno, &ubused, dip, &fmterror);
617                                 if (fmterror == BULKSTAT_RV_NOTHING) {
618                                         if (error == ENOMEM)
619                                                 ubleft = 0;
620                                         continue;
621                                 }
622                                 if (fmterror == BULKSTAT_RV_GIVEUP) {
623                                         ubleft = 0;
624                                         ASSERT(error);
625                                         rval = error;
626                                         break;
627                                 }
628                                 if (ubufp)
629                                         ubufp += ubused;
630                                 ubleft -= ubused;
631                                 ubelem++;
632                                 lastino = ino;
633                         }
634                 }
635
636                 if (bp)
637                         xfs_buf_relse(bp);
638
639                 /*
640                  * Set up for the next loop iteration.
641                  */
642                 if (ubleft > 0) {
643                         if (end_of_ag) {
644                                 agno++;
645                                 agino = 0;
646                         } else
647                                 agino = XFS_INO_TO_AGINO(mp, lastino);
648                 } else
649                         break;
650         }
651         /*
652          * Done, we're either out of filesystem or space to put the data.
653          */
654         kmem_free(irbuf, NBPC);
655         if (ubuffer)
656                 unuseracc(ubuffer, ubcount * statstruct_size, (B_READ|B_PHYS));
657         *ubcountp = ubelem;
658         if (agno >= mp->m_sb.sb_agcount) {
659                 /*
660                  * If we ran out of filesystem, mark lastino as off
661                  * the end of the filesystem, so the next call
662                  * will return immediately.
663                  */
664                 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
665                 *done = 1;
666         } else
667                 *lastinop = (xfs_ino_t)lastino;
668
669         return rval;
670 }
671
672 /*
673  * Return stat information in bulk (by-inode) for the filesystem.
674  * Special case for non-sequential one inode bulkstat.
675  */
676 int                                     /* error status */
677 xfs_bulkstat_single(
678         xfs_mount_t             *mp,    /* mount point for filesystem */
679         xfs_ino_t               *lastinop, /* inode to return */
680         char                    __user *buffer, /* buffer with inode stats */
681         int                     *done)  /* 1 if there are more stats to get */
682 {
683         int                     count;  /* count value for bulkstat call */
684         int                     error;  /* return value */
685         xfs_ino_t               ino;    /* filesystem inode number */
686         int                     res;    /* result from bs1 */
687
688         /*
689          * note that requesting valid inode numbers which are not allocated
690          * to inodes will most likely cause xfs_itobp to generate warning
691          * messages about bad magic numbers. This is ok. The fact that
692          * the inode isn't actually an inode is handled by the
693          * error check below. Done this way to make the usual case faster
694          * at the expense of the error case.
695          */
696
697         ino = (xfs_ino_t)*lastinop;
698         error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
699                                  NULL, 0, NULL, NULL, &res);
700         if (error) {
701                 /*
702                  * Special case way failed, do it the "long" way
703                  * to see if that works.
704                  */
705                 (*lastinop)--;
706                 count = 1;
707                 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
708                                 NULL, sizeof(xfs_bstat_t), buffer,
709                                 BULKSTAT_FG_IGET, done))
710                         return error;
711                 if (count == 0 || (xfs_ino_t)*lastinop != ino)
712                         return error == EFSCORRUPTED ?
713                                 XFS_ERROR(EINVAL) : error;
714                 else
715                         return 0;
716         }
717         *done = 0;
718         return 0;
719 }
720
721 /*
722  * Return inode number table for the filesystem.
723  */
724 int                                     /* error status */
725 xfs_inumbers(
726         xfs_mount_t     *mp,            /* mount point for filesystem */
727         xfs_ino_t       *lastino,       /* last inode returned */
728         int             *count,         /* size of buffer/count returned */
729         xfs_inogrp_t    __user *ubuffer)/* buffer with inode descriptions */
730 {
731         xfs_buf_t       *agbp;
732         xfs_agino_t     agino;
733         xfs_agnumber_t  agno;
734         int             bcount;
735         xfs_inogrp_t    *buffer;
736         int             bufidx;
737         xfs_btree_cur_t *cur;
738         int             error;
739         __int32_t       gcnt;
740         xfs_inofree_t   gfree;
741         xfs_agino_t     gino;
742         int             i;
743         xfs_ino_t       ino;
744         int             left;
745         int             tmp;
746
747         ino = (xfs_ino_t)*lastino;
748         agno = XFS_INO_TO_AGNO(mp, ino);
749         agino = XFS_INO_TO_AGINO(mp, ino);
750         left = *count;
751         *count = 0;
752         bcount = MIN(left, (int)(NBPP / sizeof(*buffer)));
753         buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
754         error = bufidx = 0;
755         cur = NULL;
756         agbp = NULL;
757         while (left > 0 && agno < mp->m_sb.sb_agcount) {
758                 if (agbp == NULL) {
759                         down_read(&mp->m_peraglock);
760                         error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
761                         up_read(&mp->m_peraglock);
762                         if (error) {
763                                 /*
764                                  * If we can't read the AGI of this ag,
765                                  * then just skip to the next one.
766                                  */
767                                 ASSERT(cur == NULL);
768                                 agbp = NULL;
769                                 agno++;
770                                 agino = 0;
771                                 continue;
772                         }
773                         cur = xfs_btree_init_cursor(mp, NULL, agbp, agno,
774                                 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
775                         error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp);
776                         if (error) {
777                                 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
778                                 cur = NULL;
779                                 xfs_buf_relse(agbp);
780                                 agbp = NULL;
781                                 /*
782                                  * Move up the the last inode in the current
783                                  * chunk.  The lookup_ge will always get
784                                  * us the first inode in the next chunk.
785                                  */
786                                 agino += XFS_INODES_PER_CHUNK - 1;
787                                 continue;
788                         }
789                 }
790                 if ((error = xfs_inobt_get_rec(cur, &gino, &gcnt, &gfree,
791                         &i)) ||
792                     i == 0) {
793                         xfs_buf_relse(agbp);
794                         agbp = NULL;
795                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
796                         cur = NULL;
797                         agno++;
798                         agino = 0;
799                         continue;
800                 }
801                 agino = gino + XFS_INODES_PER_CHUNK - 1;
802                 buffer[bufidx].xi_startino = XFS_AGINO_TO_INO(mp, agno, gino);
803                 buffer[bufidx].xi_alloccount = XFS_INODES_PER_CHUNK - gcnt;
804                 buffer[bufidx].xi_allocmask = ~gfree;
805                 bufidx++;
806                 left--;
807                 if (bufidx == bcount) {
808                         if (copy_to_user(ubuffer, buffer,
809                                         bufidx * sizeof(*buffer))) {
810                                 error = XFS_ERROR(EFAULT);
811                                 break;
812                         }
813                         ubuffer += bufidx;
814                         *count += bufidx;
815                         bufidx = 0;
816                 }
817                 if (left) {
818                         error = xfs_inobt_increment(cur, 0, &tmp);
819                         if (error) {
820                                 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
821                                 cur = NULL;
822                                 xfs_buf_relse(agbp);
823                                 agbp = NULL;
824                                 /*
825                                  * The agino value has already been bumped.
826                                  * Just try to skip up to it.
827                                  */
828                                 agino += XFS_INODES_PER_CHUNK;
829                                 continue;
830                         }
831                 }
832         }
833         if (!error) {
834                 if (bufidx) {
835                         if (copy_to_user(ubuffer, buffer,
836                                         bufidx * sizeof(*buffer)))
837                                 error = XFS_ERROR(EFAULT);
838                         else
839                                 *count += bufidx;
840                 }
841                 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
842         }
843         kmem_free(buffer, bcount * sizeof(*buffer));
844         if (cur)
845                 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
846                                            XFS_BTREE_NOERROR));
847         if (agbp)
848                 xfs_buf_relse(agbp);
849         return error;
850 }