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