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