[XFS] dinode endianess annotations
[linux-2.6.git] / 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_icdinode_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 (dic->di_version == XFS_DINODE_VERSION_1) {
155                 buf->bs_nlink = be16_to_cpu(dic->di_onlink);
156                 buf->bs_projid = 0;
157         } else {
158                 buf->bs_nlink = be32_to_cpu(dic->di_nlink);
159                 buf->bs_projid = be16_to_cpu(dic->di_projid);
160         }
161
162         buf->bs_ino = ino;
163         buf->bs_mode = be16_to_cpu(dic->di_mode);
164         buf->bs_uid = be32_to_cpu(dic->di_uid);
165         buf->bs_gid = be32_to_cpu(dic->di_gid);
166         buf->bs_size = be64_to_cpu(dic->di_size);
167         buf->bs_atime.tv_sec = be32_to_cpu(dic->di_atime.t_sec);
168         buf->bs_atime.tv_nsec = be32_to_cpu(dic->di_atime.t_nsec);
169         buf->bs_mtime.tv_sec = be32_to_cpu(dic->di_mtime.t_sec);
170         buf->bs_mtime.tv_nsec = be32_to_cpu(dic->di_mtime.t_nsec);
171         buf->bs_ctime.tv_sec = be32_to_cpu(dic->di_ctime.t_sec);
172         buf->bs_ctime.tv_nsec = be32_to_cpu(dic->di_ctime.t_nsec);
173         buf->bs_xflags = xfs_dic2xflags(dic);
174         buf->bs_extsize = be32_to_cpu(dic->di_extsize) << mp->m_sb.sb_blocklog;
175         buf->bs_extents = be32_to_cpu(dic->di_nextents);
176         buf->bs_gen = be32_to_cpu(dic->di_gen);
177         memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
178         buf->bs_dmevmask = be32_to_cpu(dic->di_dmevmask);
179         buf->bs_dmstate = be16_to_cpu(dic->di_dmstate);
180         buf->bs_aextents = be16_to_cpu(dic->di_anextents);
181
182         switch (dic->di_format) {
183         case XFS_DINODE_FMT_DEV:
184                 buf->bs_rdev = be32_to_cpu(dip->di_u.di_dev);
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 = be64_to_cpu(dic->di_nblocks);
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 (be16_to_cpu(dip->di_core.di_magic) != XFS_DINODE_MAGIC ||
294             !XFS_DINODE_GOOD_VERSION(dip->di_core.di_version))
295                 return 0;
296         if (flags & BULKSTAT_FG_QUICK) {
297                 *dipp = dip;
298                 return 1;
299         }
300         /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
301         aformat = dip->di_core.di_aformat;
302         if ((XFS_CFORK_Q(&dip->di_core) == 0) ||
303             (aformat == XFS_DINODE_FMT_LOCAL) ||
304             (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_core.di_anextents)) {
305                 *dipp = dip;
306                 return 1;
307         }
308         return 1;
309 }
310
311 /*
312  * Return stat information in bulk (by-inode) for the filesystem.
313  */
314 int                                     /* error status */
315 xfs_bulkstat(
316         xfs_mount_t             *mp,    /* mount point for filesystem */
317         xfs_ino_t               *lastinop, /* last inode returned */
318         int                     *ubcountp, /* size of buffer/count returned */
319         bulkstat_one_pf         formatter, /* func that'd fill a single buf */
320         void                    *private_data,/* private data for formatter */
321         size_t                  statstruct_size, /* sizeof struct filling */
322         char                    __user *ubuffer, /* buffer with inode stats */
323         int                     flags,  /* defined in xfs_itable.h */
324         int                     *done)  /* 1 if there are more stats to get */
325 {
326         xfs_agblock_t           agbno=0;/* allocation group block number */
327         xfs_buf_t               *agbp;  /* agi header buffer */
328         xfs_agi_t               *agi;   /* agi header data */
329         xfs_agino_t             agino;  /* inode # in allocation group */
330         xfs_agnumber_t          agno;   /* allocation group number */
331         xfs_daddr_t             bno;    /* inode cluster start daddr */
332         int                     chunkidx; /* current index into inode chunk */
333         int                     clustidx; /* current index into inode cluster */
334         xfs_btree_cur_t         *cur;   /* btree cursor for ialloc btree */
335         int                     end_of_ag; /* set if we've seen the ag end */
336         int                     error;  /* error code */
337         int                     fmterror;/* bulkstat formatter result */
338         __int32_t               gcnt;   /* current btree rec's count */
339         xfs_inofree_t           gfree;  /* current btree rec's free mask */
340         xfs_agino_t             gino;   /* current btree rec's start inode */
341         int                     i;      /* loop index */
342         int                     icount; /* count of inodes good in irbuf */
343         size_t                  irbsize; /* size of irec buffer in bytes */
344         xfs_ino_t               ino;    /* inode number (filesystem) */
345         xfs_inobt_rec_incore_t  *irbp;  /* current irec buffer pointer */
346         xfs_inobt_rec_incore_t  *irbuf; /* start of irec buffer */
347         xfs_inobt_rec_incore_t  *irbufend; /* end of good irec buffer entries */
348         xfs_ino_t               lastino=0; /* last inode number returned */
349         int                     nbcluster; /* # of blocks in a cluster */
350         int                     nicluster; /* # of inodes in a cluster */
351         int                     nimask; /* mask for inode clusters */
352         int                     nirbuf; /* size of irbuf */
353         int                     rval;   /* return value error code */
354         int                     tmp;    /* result value from btree calls */
355         int                     ubcount; /* size of user's buffer */
356         int                     ubleft; /* bytes left in user's buffer */
357         char                    __user *ubufp;  /* pointer into user's buffer */
358         int                     ubelem; /* spaces used in user's buffer */
359         int                     ubused; /* bytes used by formatter */
360         xfs_buf_t               *bp;    /* ptr to on-disk inode cluster buf */
361         xfs_dinode_t            *dip;   /* ptr into bp for specific inode */
362         xfs_inode_t             *ip;    /* ptr to in-core inode struct */
363
364         /*
365          * Get the last inode value, see if there's nothing to do.
366          */
367         ino = (xfs_ino_t)*lastinop;
368         dip = NULL;
369         agno = XFS_INO_TO_AGNO(mp, ino);
370         agino = XFS_INO_TO_AGINO(mp, ino);
371         if (agno >= mp->m_sb.sb_agcount ||
372             ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
373                 *done = 1;
374                 *ubcountp = 0;
375                 return 0;
376         }
377         ubcount = *ubcountp; /* statstruct's */
378         ubleft = ubcount * statstruct_size; /* bytes */
379         *ubcountp = ubelem = 0;
380         *done = 0;
381         fmterror = 0;
382         ubufp = ubuffer;
383         nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
384                 mp->m_sb.sb_inopblock :
385                 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
386         nimask = ~(nicluster - 1);
387         nbcluster = nicluster >> mp->m_sb.sb_inopblog;
388         irbuf = kmem_zalloc_greedy(&irbsize, NBPC, NBPC * 4,
389                                    KM_SLEEP | KM_MAYFAIL | KM_LARGE);
390         nirbuf = irbsize / sizeof(*irbuf);
391
392         /*
393          * Loop over the allocation groups, starting from the last
394          * inode returned; 0 means start of the allocation group.
395          */
396         rval = 0;
397         while (ubleft >= statstruct_size && agno < mp->m_sb.sb_agcount) {
398                 bp = NULL;
399                 down_read(&mp->m_peraglock);
400                 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
401                 up_read(&mp->m_peraglock);
402                 if (error) {
403                         /*
404                          * Skip this allocation group and go to the next one.
405                          */
406                         agno++;
407                         agino = 0;
408                         continue;
409                 }
410                 agi = XFS_BUF_TO_AGI(agbp);
411                 /*
412                  * Allocate and initialize a btree cursor for ialloc btree.
413                  */
414                 cur = xfs_btree_init_cursor(mp, NULL, agbp, agno, XFS_BTNUM_INO,
415                                                 (xfs_inode_t *)0, 0);
416                 irbp = irbuf;
417                 irbufend = irbuf + nirbuf;
418                 end_of_ag = 0;
419                 /*
420                  * If we're returning in the middle of an allocation group,
421                  * we need to get the remainder of the chunk we're in.
422                  */
423                 if (agino > 0) {
424                         /*
425                          * Lookup the inode chunk that this inode lives in.
426                          */
427                         error = xfs_inobt_lookup_le(cur, agino, 0, 0, &tmp);
428                         if (!error &&   /* no I/O error */
429                             tmp &&      /* lookup succeeded */
430                                         /* got the record, should always work */
431                             !(error = xfs_inobt_get_rec(cur, &gino, &gcnt,
432                                     &gfree, &i)) &&
433                             i == 1 &&
434                                         /* this is the right chunk */
435                             agino < gino + XFS_INODES_PER_CHUNK &&
436                                         /* lastino was not last in chunk */
437                             (chunkidx = agino - gino + 1) <
438                                     XFS_INODES_PER_CHUNK &&
439                                         /* there are some left allocated */
440                             XFS_INOBT_MASKN(chunkidx,
441                                     XFS_INODES_PER_CHUNK - chunkidx) & ~gfree) {
442                                 /*
443                                  * Grab the chunk record.  Mark all the
444                                  * uninteresting inodes (because they're
445                                  * before our start point) free.
446                                  */
447                                 for (i = 0; i < chunkidx; i++) {
448                                         if (XFS_INOBT_MASK(i) & ~gfree)
449                                                 gcnt++;
450                                 }
451                                 gfree |= XFS_INOBT_MASKN(0, chunkidx);
452                                 irbp->ir_startino = gino;
453                                 irbp->ir_freecount = gcnt;
454                                 irbp->ir_free = gfree;
455                                 irbp++;
456                                 agino = gino + XFS_INODES_PER_CHUNK;
457                                 icount = XFS_INODES_PER_CHUNK - gcnt;
458                         } else {
459                                 /*
460                                  * If any of those tests failed, bump the
461                                  * inode number (just in case).
462                                  */
463                                 agino++;
464                                 icount = 0;
465                         }
466                         /*
467                          * In any case, increment to the next record.
468                          */
469                         if (!error)
470                                 error = xfs_inobt_increment(cur, 0, &tmp);
471                 } else {
472                         /*
473                          * Start of ag.  Lookup the first inode chunk.
474                          */
475                         error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &tmp);
476                         icount = 0;
477                 }
478                 /*
479                  * Loop through inode btree records in this ag,
480                  * until we run out of inodes or space in the buffer.
481                  */
482                 while (irbp < irbufend && icount < ubcount) {
483                         /*
484                          * Loop as long as we're unable to read the
485                          * inode btree.
486                          */
487                         while (error) {
488                                 agino += XFS_INODES_PER_CHUNK;
489                                 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
490                                                 be32_to_cpu(agi->agi_length))
491                                         break;
492                                 error = xfs_inobt_lookup_ge(cur, agino, 0, 0,
493                                                             &tmp);
494                         }
495                         /*
496                          * If ran off the end of the ag either with an error,
497                          * or the normal way, set end and stop collecting.
498                          */
499                         if (error ||
500                             (error = xfs_inobt_get_rec(cur, &gino, &gcnt,
501                                     &gfree, &i)) ||
502                             i == 0) {
503                                 end_of_ag = 1;
504                                 break;
505                         }
506                         /*
507                          * If this chunk has any allocated inodes, save it.
508                          * Also start read-ahead now for this chunk.
509                          */
510                         if (gcnt < XFS_INODES_PER_CHUNK) {
511                                 /*
512                                  * Loop over all clusters in the next chunk.
513                                  * Do a readahead if there are any allocated
514                                  * inodes in that cluster.
515                                  */
516                                 for (agbno = XFS_AGINO_TO_AGBNO(mp, gino),
517                                      chunkidx = 0;
518                                      chunkidx < XFS_INODES_PER_CHUNK;
519                                      chunkidx += nicluster,
520                                      agbno += nbcluster) {
521                                         if (XFS_INOBT_MASKN(chunkidx,
522                                                             nicluster) & ~gfree)
523                                                 xfs_btree_reada_bufs(mp, agno,
524                                                         agbno, nbcluster);
525                                 }
526                                 irbp->ir_startino = gino;
527                                 irbp->ir_freecount = gcnt;
528                                 irbp->ir_free = gfree;
529                                 irbp++;
530                                 icount += XFS_INODES_PER_CHUNK - gcnt;
531                         }
532                         /*
533                          * Set agino to after this chunk and bump the cursor.
534                          */
535                         agino = gino + XFS_INODES_PER_CHUNK;
536                         error = xfs_inobt_increment(cur, 0, &tmp);
537                 }
538                 /*
539                  * Drop the btree buffers and the agi buffer.
540                  * We can't hold any of the locks these represent
541                  * when calling iget.
542                  */
543                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
544                 xfs_buf_relse(agbp);
545                 /*
546                  * Now format all the good inodes into the user's buffer.
547                  */
548                 irbufend = irbp;
549                 for (irbp = irbuf;
550                      irbp < irbufend && ubleft >= statstruct_size; irbp++) {
551                         /*
552                          * Now process this chunk of inodes.
553                          */
554                         for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
555                              ubleft > 0 &&
556                                 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
557                              chunkidx++, clustidx++, agino++) {
558                                 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
559                                 /*
560                                  * Recompute agbno if this is the
561                                  * first inode of the cluster.
562                                  *
563                                  * Careful with clustidx.   There can be
564                                  * multple clusters per chunk, a single
565                                  * cluster per chunk or a cluster that has
566                                  * inodes represented from several different
567                                  * chunks (if blocksize is large).
568                                  *
569                                  * Because of this, the starting clustidx is
570                                  * initialized to zero in this loop but must
571                                  * later be reset after reading in the cluster
572                                  * buffer.
573                                  */
574                                 if ((chunkidx & (nicluster - 1)) == 0) {
575                                         agbno = XFS_AGINO_TO_AGBNO(mp,
576                                                         irbp->ir_startino) +
577                                                 ((chunkidx & nimask) >>
578                                                  mp->m_sb.sb_inopblog);
579
580                                         if (flags & (BULKSTAT_FG_QUICK |
581                                                      BULKSTAT_FG_INLINE)) {
582                                                 ino = XFS_AGINO_TO_INO(mp, agno,
583                                                                        agino);
584                                                 bno = XFS_AGB_TO_DADDR(mp, agno,
585                                                                        agbno);
586
587                                                 /*
588                                                  * Get the inode cluster buffer
589                                                  */
590                                                 ASSERT(xfs_inode_zone != NULL);
591                                                 ip = kmem_zone_zalloc(xfs_inode_zone,
592                                                                       KM_SLEEP);
593                                                 ip->i_ino = ino;
594                                                 ip->i_mount = mp;
595                                                 spin_lock_init(&ip->i_flags_lock);
596                                                 if (bp)
597                                                         xfs_buf_relse(bp);
598                                                 error = xfs_itobp(mp, NULL, ip,
599                                                                 &dip, &bp, bno,
600                                                                 XFS_IMAP_BULKSTAT);
601                                                 if (!error)
602                                                         clustidx = ip->i_boffset / mp->m_sb.sb_inodesize;
603                                                 kmem_zone_free(xfs_inode_zone, ip);
604                                                 if (XFS_TEST_ERROR(error != 0,
605                                                                    mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
606                                                                    XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
607                                                         bp = NULL;
608                                                         ubleft = 0;
609                                                         rval = error;
610                                                         break;
611                                                 }
612                                         }
613                                 }
614                                 /*
615                                  * Skip if this inode is free.
616                                  */
617                                 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
618                                         continue;
619                                 /*
620                                  * Count used inodes as free so we can tell
621                                  * when the chunk is used up.
622                                  */
623                                 irbp->ir_freecount++;
624                                 ino = XFS_AGINO_TO_INO(mp, agno, agino);
625                                 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
626                                 if (!xfs_bulkstat_use_dinode(mp, flags, bp,
627                                                              clustidx, &dip))
628                                         continue;
629                                 /*
630                                  * If we need to do an iget, cannot hold bp.
631                                  * Drop it, until starting the next cluster.
632                                  */
633                                 if ((flags & BULKSTAT_FG_INLINE) && !dip) {
634                                         if (bp)
635                                                 xfs_buf_relse(bp);
636                                         bp = NULL;
637                                 }
638
639                                 /*
640                                  * Get the inode and fill in a single buffer.
641                                  * BULKSTAT_FG_QUICK uses dip to fill it in.
642                                  * BULKSTAT_FG_IGET uses igets.
643                                  * BULKSTAT_FG_INLINE uses dip if we have an
644                                  * inline attr fork, else igets.
645                                  * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
646                                  * This is also used to count inodes/blks, etc
647                                  * in xfs_qm_quotacheck.
648                                  */
649                                 ubused = statstruct_size;
650                                 error = formatter(mp, ino, ubufp,
651                                                 ubleft, private_data,
652                                                 bno, &ubused, dip, &fmterror);
653                                 if (fmterror == BULKSTAT_RV_NOTHING) {
654                                         if (error == EFAULT) {
655                                                 ubleft = 0;
656                                                 rval = error;
657                                                 break;
658                                         }
659                                         else if (error == ENOMEM)
660                                                 ubleft = 0;
661                                         else
662                                                 lastino = ino;
663                                         continue;
664                                 }
665                                 if (fmterror == BULKSTAT_RV_GIVEUP) {
666                                         ubleft = 0;
667                                         ASSERT(error);
668                                         rval = error;
669                                         break;
670                                 }
671                                 if (ubufp)
672                                         ubufp += ubused;
673                                 ubleft -= ubused;
674                                 ubelem++;
675                                 lastino = ino;
676                         }
677                 }
678
679                 if (bp)
680                         xfs_buf_relse(bp);
681
682                 /*
683                  * Set up for the next loop iteration.
684                  */
685                 if (ubleft > 0) {
686                         if (end_of_ag) {
687                                 agno++;
688                                 agino = 0;
689                         } else
690                                 agino = XFS_INO_TO_AGINO(mp, lastino);
691                 } else
692                         break;
693         }
694         /*
695          * Done, we're either out of filesystem or space to put the data.
696          */
697         kmem_free(irbuf, irbsize);
698         *ubcountp = ubelem;
699         if (agno >= mp->m_sb.sb_agcount) {
700                 /*
701                  * If we ran out of filesystem, mark lastino as off
702                  * the end of the filesystem, so the next call
703                  * will return immediately.
704                  */
705                 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
706                 *done = 1;
707         } else
708                 *lastinop = (xfs_ino_t)lastino;
709
710         return rval;
711 }
712
713 /*
714  * Return stat information in bulk (by-inode) for the filesystem.
715  * Special case for non-sequential one inode bulkstat.
716  */
717 int                                     /* error status */
718 xfs_bulkstat_single(
719         xfs_mount_t             *mp,    /* mount point for filesystem */
720         xfs_ino_t               *lastinop, /* inode to return */
721         char                    __user *buffer, /* buffer with inode stats */
722         int                     *done)  /* 1 if there are more stats to get */
723 {
724         int                     count;  /* count value for bulkstat call */
725         int                     error;  /* return value */
726         xfs_ino_t               ino;    /* filesystem inode number */
727         int                     res;    /* result from bs1 */
728
729         /*
730          * note that requesting valid inode numbers which are not allocated
731          * to inodes will most likely cause xfs_itobp to generate warning
732          * messages about bad magic numbers. This is ok. The fact that
733          * the inode isn't actually an inode is handled by the
734          * error check below. Done this way to make the usual case faster
735          * at the expense of the error case.
736          */
737
738         ino = (xfs_ino_t)*lastinop;
739         error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
740                                  NULL, 0, NULL, NULL, &res);
741         if (error) {
742                 /*
743                  * Special case way failed, do it the "long" way
744                  * to see if that works.
745                  */
746                 (*lastinop)--;
747                 count = 1;
748                 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
749                                 NULL, sizeof(xfs_bstat_t), buffer,
750                                 BULKSTAT_FG_IGET, done))
751                         return error;
752                 if (count == 0 || (xfs_ino_t)*lastinop != ino)
753                         return error == EFSCORRUPTED ?
754                                 XFS_ERROR(EINVAL) : error;
755                 else
756                         return 0;
757         }
758         *done = 0;
759         return 0;
760 }
761
762 int
763 xfs_inumbers_fmt(
764         void                    __user *ubuffer, /* buffer to write to */
765         const xfs_inogrp_t      *buffer,        /* buffer to read from */
766         long                    count,          /* # of elements to read */
767         long                    *written)       /* # of bytes written */
768 {
769         if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
770                 return -EFAULT;
771         *written = count * sizeof(*buffer);
772         return 0;
773 }
774
775 /*
776  * Return inode number table for the filesystem.
777  */
778 int                                     /* error status */
779 xfs_inumbers(
780         xfs_mount_t     *mp,            /* mount point for filesystem */
781         xfs_ino_t       *lastino,       /* last inode returned */
782         int             *count,         /* size of buffer/count returned */
783         void            __user *ubuffer,/* buffer with inode descriptions */
784         inumbers_fmt_pf formatter)
785 {
786         xfs_buf_t       *agbp;
787         xfs_agino_t     agino;
788         xfs_agnumber_t  agno;
789         int             bcount;
790         xfs_inogrp_t    *buffer;
791         int             bufidx;
792         xfs_btree_cur_t *cur;
793         int             error;
794         __int32_t       gcnt;
795         xfs_inofree_t   gfree;
796         xfs_agino_t     gino;
797         int             i;
798         xfs_ino_t       ino;
799         int             left;
800         int             tmp;
801
802         ino = (xfs_ino_t)*lastino;
803         agno = XFS_INO_TO_AGNO(mp, ino);
804         agino = XFS_INO_TO_AGINO(mp, ino);
805         left = *count;
806         *count = 0;
807         bcount = MIN(left, (int)(NBPP / sizeof(*buffer)));
808         buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
809         error = bufidx = 0;
810         cur = NULL;
811         agbp = NULL;
812         while (left > 0 && agno < mp->m_sb.sb_agcount) {
813                 if (agbp == NULL) {
814                         down_read(&mp->m_peraglock);
815                         error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
816                         up_read(&mp->m_peraglock);
817                         if (error) {
818                                 /*
819                                  * If we can't read the AGI of this ag,
820                                  * then just skip to the next one.
821                                  */
822                                 ASSERT(cur == NULL);
823                                 agbp = NULL;
824                                 agno++;
825                                 agino = 0;
826                                 continue;
827                         }
828                         cur = xfs_btree_init_cursor(mp, NULL, agbp, agno,
829                                 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
830                         error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp);
831                         if (error) {
832                                 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
833                                 cur = NULL;
834                                 xfs_buf_relse(agbp);
835                                 agbp = NULL;
836                                 /*
837                                  * Move up the last inode in the current
838                                  * chunk.  The lookup_ge will always get
839                                  * us the first inode in the next chunk.
840                                  */
841                                 agino += XFS_INODES_PER_CHUNK - 1;
842                                 continue;
843                         }
844                 }
845                 if ((error = xfs_inobt_get_rec(cur, &gino, &gcnt, &gfree,
846                         &i)) ||
847                     i == 0) {
848                         xfs_buf_relse(agbp);
849                         agbp = NULL;
850                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
851                         cur = NULL;
852                         agno++;
853                         agino = 0;
854                         continue;
855                 }
856                 agino = gino + XFS_INODES_PER_CHUNK - 1;
857                 buffer[bufidx].xi_startino = XFS_AGINO_TO_INO(mp, agno, gino);
858                 buffer[bufidx].xi_alloccount = XFS_INODES_PER_CHUNK - gcnt;
859                 buffer[bufidx].xi_allocmask = ~gfree;
860                 bufidx++;
861                 left--;
862                 if (bufidx == bcount) {
863                         long written;
864                         if (formatter(ubuffer, buffer, bufidx, &written)) {
865                                 error = XFS_ERROR(EFAULT);
866                                 break;
867                         }
868                         ubuffer += written;
869                         *count += bufidx;
870                         bufidx = 0;
871                 }
872                 if (left) {
873                         error = xfs_inobt_increment(cur, 0, &tmp);
874                         if (error) {
875                                 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
876                                 cur = NULL;
877                                 xfs_buf_relse(agbp);
878                                 agbp = NULL;
879                                 /*
880                                  * The agino value has already been bumped.
881                                  * Just try to skip up to it.
882                                  */
883                                 agino += XFS_INODES_PER_CHUNK;
884                                 continue;
885                         }
886                 }
887         }
888         if (!error) {
889                 if (bufidx) {
890                         long written;
891                         if (formatter(ubuffer, buffer, bufidx, &written))
892                                 error = XFS_ERROR(EFAULT);
893                         else
894                                 *count += bufidx;
895                 }
896                 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
897         }
898         kmem_free(buffer, bcount * sizeof(*buffer));
899         if (cur)
900                 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
901                                            XFS_BTREE_NOERROR));
902         if (agbp)
903                 xfs_buf_relse(agbp);
904         return error;
905 }