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