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