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