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