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