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