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