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