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