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