xfs: remove xfs_iput_new
[deliverable/linux.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 27#include "xfs_mount.h"
1da177e4 28#include "xfs_bmap_btree.h"
a844f451 29#include "xfs_alloc_btree.h"
1da177e4 30#include "xfs_ialloc_btree.h"
1da177e4
LT
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_ialloc.h"
34#include "xfs_itable.h"
35#include "xfs_error.h"
a844f451 36#include "xfs_btree.h"
1da177e4 37
d96f8f89 38STATIC int
6f1f2168
VA
39xfs_internal_inum(
40 xfs_mount_t *mp,
41 xfs_ino_t ino)
42{
43 return (ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
62118709 44 (xfs_sb_version_hasquota(&mp->m_sb) &&
6f1f2168
VA
45 (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino)));
46}
47
7dce11db
CH
48/*
49 * Return stat information for one inode.
50 * Return 0 if ok, else errno.
51 */
52int
53xfs_bulkstat_one_int(
54 struct xfs_mount *mp, /* mount point for filesystem */
55 xfs_ino_t ino, /* inode to get data for */
56 void __user *buffer, /* buffer to place output in */
57 int ubsize, /* size of buffer */
58 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
7dce11db
CH
59 int *ubused, /* bytes used by me */
60 int *stat) /* BULKSTAT_RV_... */
1da177e4 61{
7dce11db
CH
62 struct xfs_icdinode *dic; /* dinode core info pointer */
63 struct xfs_inode *ip; /* incore inode pointer */
64 struct inode *inode;
65 struct xfs_bstat *buf; /* return buffer */
66 int error = 0; /* error value */
67
68 *stat = BULKSTAT_RV_NOTHING;
69
70 if (!buffer || xfs_internal_inum(mp, ino))
71 return XFS_ERROR(EINVAL);
72
73 buf = kmem_alloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
74 if (!buf)
75 return XFS_ERROR(ENOMEM);
1da177e4 76
745b1f47 77 error = xfs_iget(mp, NULL, ino,
7b6259e7 78 XFS_IGET_UNTRUSTED, XFS_ILOCK_SHARED, &ip);
1da177e4
LT
79 if (error) {
80 *stat = BULKSTAT_RV_NOTHING;
7dce11db 81 goto out_free;
1da177e4
LT
82 }
83
84 ASSERT(ip != NULL);
92bfc6e7 85 ASSERT(ip->i_imap.im_blkno != 0);
1da177e4
LT
86
87 dic = &ip->i_d;
f9581b14 88 inode = VFS_I(ip);
1da177e4
LT
89
90 /* xfs_iget returns the following without needing
91 * further change.
92 */
93 buf->bs_nlink = dic->di_nlink;
94 buf->bs_projid = dic->di_projid;
95 buf->bs_ino = ino;
96 buf->bs_mode = dic->di_mode;
97 buf->bs_uid = dic->di_uid;
98 buf->bs_gid = dic->di_gid;
99 buf->bs_size = dic->di_size;
f9581b14 100
8fab451e 101 /*
f9581b14
CH
102 * We need to read the timestamps from the Linux inode because
103 * the VFS keeps writing directly into the inode structure instead
104 * of telling us about the updates.
8fab451e 105 */
f9581b14
CH
106 buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
107 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
108 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
109 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
110 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
111 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
112
1da177e4
LT
113 buf->bs_xflags = xfs_ip2xflags(ip);
114 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
115 buf->bs_extents = dic->di_nextents;
116 buf->bs_gen = dic->di_gen;
117 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
118 buf->bs_dmevmask = dic->di_dmevmask;
119 buf->bs_dmstate = dic->di_dmstate;
120 buf->bs_aextents = dic->di_anextents;
07000ee6 121 buf->bs_forkoff = XFS_IFORK_BOFF(ip);
1da177e4
LT
122
123 switch (dic->di_format) {
124 case XFS_DINODE_FMT_DEV:
125 buf->bs_rdev = ip->i_df.if_u2.if_rdev;
126 buf->bs_blksize = BLKDEV_IOSIZE;
127 buf->bs_blocks = 0;
128 break;
129 case XFS_DINODE_FMT_LOCAL:
130 case XFS_DINODE_FMT_UUID:
131 buf->bs_rdev = 0;
132 buf->bs_blksize = mp->m_sb.sb_blocksize;
133 buf->bs_blocks = 0;
134 break;
135 case XFS_DINODE_FMT_EXTENTS:
136 case XFS_DINODE_FMT_BTREE:
137 buf->bs_rdev = 0;
138 buf->bs_blksize = mp->m_sb.sb_blocksize;
139 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
140 break;
141 }
1da177e4 142 xfs_iput(ip, XFS_ILOCK_SHARED);
1da177e4 143
7dce11db 144 error = formatter(buffer, ubsize, ubused, buf);
1da177e4 145
7dce11db
CH
146 if (!error)
147 *stat = BULKSTAT_RV_DIDONE;
1da177e4 148
7dce11db
CH
149 out_free:
150 kmem_free(buf);
151 return error;
1da177e4
LT
152}
153
65fbaf24 154/* Return 0 on success or positive error */
faa63e95
MM
155STATIC int
156xfs_bulkstat_one_fmt(
157 void __user *ubuffer,
65fbaf24 158 int ubsize,
159 int *ubused,
faa63e95
MM
160 const xfs_bstat_t *buffer)
161{
65fbaf24 162 if (ubsize < sizeof(*buffer))
163 return XFS_ERROR(ENOMEM);
faa63e95 164 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
65fbaf24 165 return XFS_ERROR(EFAULT);
166 if (ubused)
167 *ubused = sizeof(*buffer);
168 return 0;
faa63e95
MM
169}
170
2ee4fa5c 171int
172xfs_bulkstat_one(
173 xfs_mount_t *mp, /* mount point for filesystem */
174 xfs_ino_t ino, /* inode number to get data for */
175 void __user *buffer, /* buffer to place output in */
176 int ubsize, /* size of buffer */
2ee4fa5c 177 int *ubused, /* bytes used by me */
2ee4fa5c 178 int *stat) /* BULKSTAT_RV_... */
179{
180 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
7b6259e7 181 xfs_bulkstat_one_fmt, ubused, stat);
8b56f083
NS
182}
183
cd57e594
LM
184#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
185
1da177e4
LT
186/*
187 * Return stat information in bulk (by-inode) for the filesystem.
188 */
189int /* error status */
190xfs_bulkstat(
191 xfs_mount_t *mp, /* mount point for filesystem */
192 xfs_ino_t *lastinop, /* last inode returned */
193 int *ubcountp, /* size of buffer/count returned */
194 bulkstat_one_pf formatter, /* func that'd fill a single buf */
1da177e4
LT
195 size_t statstruct_size, /* sizeof struct filling */
196 char __user *ubuffer, /* buffer with inode stats */
c41564b5 197 int *done) /* 1 if there are more stats to get */
1da177e4
LT
198{
199 xfs_agblock_t agbno=0;/* allocation group block number */
200 xfs_buf_t *agbp; /* agi header buffer */
201 xfs_agi_t *agi; /* agi header data */
202 xfs_agino_t agino; /* inode # in allocation group */
203 xfs_agnumber_t agno; /* allocation group number */
204 xfs_daddr_t bno; /* inode cluster start daddr */
205 int chunkidx; /* current index into inode chunk */
206 int clustidx; /* current index into inode cluster */
207 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
208 int end_of_ag; /* set if we've seen the ag end */
209 int error; /* error code */
210 int fmterror;/* bulkstat formatter result */
1da177e4
LT
211 int i; /* loop index */
212 int icount; /* count of inodes good in irbuf */
215101c3 213 size_t irbsize; /* size of irec buffer in bytes */
1da177e4 214 xfs_ino_t ino; /* inode number (filesystem) */
26275093
NS
215 xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
216 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
217 xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
cd57e594 218 xfs_ino_t lastino; /* last inode number returned */
1da177e4
LT
219 int nbcluster; /* # of blocks in a cluster */
220 int nicluster; /* # of inodes in a cluster */
221 int nimask; /* mask for inode clusters */
222 int nirbuf; /* size of irbuf */
223 int rval; /* return value error code */
224 int tmp; /* result value from btree calls */
225 int ubcount; /* size of user's buffer */
226 int ubleft; /* bytes left in user's buffer */
227 char __user *ubufp; /* pointer into user's buffer */
228 int ubelem; /* spaces used in user's buffer */
229 int ubused; /* bytes used by formatter */
230 xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
1da177e4
LT
231
232 /*
233 * Get the last inode value, see if there's nothing to do.
234 */
235 ino = (xfs_ino_t)*lastinop;
cd57e594 236 lastino = ino;
1da177e4
LT
237 agno = XFS_INO_TO_AGNO(mp, ino);
238 agino = XFS_INO_TO_AGINO(mp, ino);
239 if (agno >= mp->m_sb.sb_agcount ||
240 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
241 *done = 1;
242 *ubcountp = 0;
243 return 0;
244 }
cd57e594
LM
245 if (!ubcountp || *ubcountp <= 0) {
246 return EINVAL;
247 }
1da177e4
LT
248 ubcount = *ubcountp; /* statstruct's */
249 ubleft = ubcount * statstruct_size; /* bytes */
250 *ubcountp = ubelem = 0;
251 *done = 0;
252 fmterror = 0;
253 ubufp = ubuffer;
254 nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
255 mp->m_sb.sb_inopblock :
256 (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
257 nimask = ~(nicluster - 1);
258 nbcluster = nicluster >> mp->m_sb.sb_inopblog;
bdfb0430
CH
259 irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
260 if (!irbuf)
261 return ENOMEM;
262
bb3c7d29
NS
263 nirbuf = irbsize / sizeof(*irbuf);
264
1da177e4
LT
265 /*
266 * Loop over the allocation groups, starting from the last
267 * inode returned; 0 means start of the allocation group.
268 */
269 rval = 0;
cd57e594
LM
270 while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
271 cond_resched();
1da177e4 272 bp = NULL;
1da177e4 273 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
1da177e4
LT
274 if (error) {
275 /*
276 * Skip this allocation group and go to the next one.
277 */
278 agno++;
279 agino = 0;
280 continue;
281 }
282 agi = XFS_BUF_TO_AGI(agbp);
283 /*
284 * Allocate and initialize a btree cursor for ialloc btree.
285 */
561f7d17 286 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
1da177e4
LT
287 irbp = irbuf;
288 irbufend = irbuf + nirbuf;
289 end_of_ag = 0;
290 /*
291 * If we're returning in the middle of an allocation group,
292 * we need to get the remainder of the chunk we're in.
293 */
294 if (agino > 0) {
2e287a73
CH
295 xfs_inobt_rec_incore_t r;
296
1da177e4
LT
297 /*
298 * Lookup the inode chunk that this inode lives in.
299 */
21875505
CH
300 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
301 &tmp);
1da177e4
LT
302 if (!error && /* no I/O error */
303 tmp && /* lookup succeeded */
304 /* got the record, should always work */
2e287a73 305 !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
1da177e4
LT
306 i == 1 &&
307 /* this is the right chunk */
2e287a73 308 agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
1da177e4 309 /* lastino was not last in chunk */
2e287a73 310 (chunkidx = agino - r.ir_startino + 1) <
1da177e4
LT
311 XFS_INODES_PER_CHUNK &&
312 /* there are some left allocated */
9d87c319 313 xfs_inobt_maskn(chunkidx,
2e287a73
CH
314 XFS_INODES_PER_CHUNK - chunkidx) &
315 ~r.ir_free) {
1da177e4
LT
316 /*
317 * Grab the chunk record. Mark all the
318 * uninteresting inodes (because they're
319 * before our start point) free.
320 */
321 for (i = 0; i < chunkidx; i++) {
2e287a73
CH
322 if (XFS_INOBT_MASK(i) & ~r.ir_free)
323 r.ir_freecount++;
1da177e4 324 }
2e287a73
CH
325 r.ir_free |= xfs_inobt_maskn(0, chunkidx);
326 irbp->ir_startino = r.ir_startino;
327 irbp->ir_freecount = r.ir_freecount;
328 irbp->ir_free = r.ir_free;
1da177e4 329 irbp++;
2e287a73
CH
330 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
331 icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
1da177e4
LT
332 } else {
333 /*
334 * If any of those tests failed, bump the
335 * inode number (just in case).
336 */
337 agino++;
338 icount = 0;
339 }
340 /*
341 * In any case, increment to the next record.
342 */
343 if (!error)
637aa50f 344 error = xfs_btree_increment(cur, 0, &tmp);
1da177e4
LT
345 } else {
346 /*
347 * Start of ag. Lookup the first inode chunk.
348 */
21875505 349 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
1da177e4
LT
350 icount = 0;
351 }
352 /*
353 * Loop through inode btree records in this ag,
354 * until we run out of inodes or space in the buffer.
355 */
356 while (irbp < irbufend && icount < ubcount) {
2e287a73
CH
357 xfs_inobt_rec_incore_t r;
358
1da177e4
LT
359 /*
360 * Loop as long as we're unable to read the
361 * inode btree.
362 */
363 while (error) {
364 agino += XFS_INODES_PER_CHUNK;
365 if (XFS_AGINO_TO_AGBNO(mp, agino) >=
16259e7d 366 be32_to_cpu(agi->agi_length))
1da177e4 367 break;
21875505
CH
368 error = xfs_inobt_lookup(cur, agino,
369 XFS_LOOKUP_GE, &tmp);
cd57e594 370 cond_resched();
1da177e4
LT
371 }
372 /*
373 * If ran off the end of the ag either with an error,
374 * or the normal way, set end and stop collecting.
375 */
2e287a73
CH
376 if (error) {
377 end_of_ag = 1;
378 break;
379 }
380
381 error = xfs_inobt_get_rec(cur, &r, &i);
382 if (error || i == 0) {
1da177e4
LT
383 end_of_ag = 1;
384 break;
385 }
2e287a73 386
1da177e4
LT
387 /*
388 * If this chunk has any allocated inodes, save it.
26275093 389 * Also start read-ahead now for this chunk.
1da177e4 390 */
2e287a73 391 if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
26275093
NS
392 /*
393 * Loop over all clusters in the next chunk.
394 * Do a readahead if there are any allocated
395 * inodes in that cluster.
396 */
2e287a73
CH
397 agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
398 for (chunkidx = 0;
26275093
NS
399 chunkidx < XFS_INODES_PER_CHUNK;
400 chunkidx += nicluster,
401 agbno += nbcluster) {
2e287a73
CH
402 if (xfs_inobt_maskn(chunkidx, nicluster)
403 & ~r.ir_free)
26275093
NS
404 xfs_btree_reada_bufs(mp, agno,
405 agbno, nbcluster);
406 }
2e287a73
CH
407 irbp->ir_startino = r.ir_startino;
408 irbp->ir_freecount = r.ir_freecount;
409 irbp->ir_free = r.ir_free;
1da177e4 410 irbp++;
2e287a73 411 icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
1da177e4
LT
412 }
413 /*
414 * Set agino to after this chunk and bump the cursor.
415 */
2e287a73 416 agino = r.ir_startino + XFS_INODES_PER_CHUNK;
637aa50f 417 error = xfs_btree_increment(cur, 0, &tmp);
cd57e594 418 cond_resched();
1da177e4
LT
419 }
420 /*
421 * Drop the btree buffers and the agi buffer.
422 * We can't hold any of the locks these represent
423 * when calling iget.
424 */
425 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
426 xfs_buf_relse(agbp);
427 /*
428 * Now format all the good inodes into the user's buffer.
429 */
430 irbufend = irbp;
431 for (irbp = irbuf;
cd57e594 432 irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
1da177e4
LT
433 /*
434 * Now process this chunk of inodes.
435 */
26275093 436 for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
cd57e594 437 XFS_BULKSTAT_UBLEFT(ubleft) &&
26275093 438 irbp->ir_freecount < XFS_INODES_PER_CHUNK;
1da177e4
LT
439 chunkidx++, clustidx++, agino++) {
440 ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
441 /*
442 * Recompute agbno if this is the
443 * first inode of the cluster.
444 *
445 * Careful with clustidx. There can be
9da096fd 446 * multiple clusters per chunk, a single
1da177e4
LT
447 * cluster per chunk or a cluster that has
448 * inodes represented from several different
449 * chunks (if blocksize is large).
450 *
451 * Because of this, the starting clustidx is
452 * initialized to zero in this loop but must
453 * later be reset after reading in the cluster
454 * buffer.
455 */
456 if ((chunkidx & (nicluster - 1)) == 0) {
457 agbno = XFS_AGINO_TO_AGBNO(mp,
26275093 458 irbp->ir_startino) +
1da177e4
LT
459 ((chunkidx & nimask) >>
460 mp->m_sb.sb_inopblog);
1da177e4 461 }
c2cba57e
LM
462 ino = XFS_AGINO_TO_INO(mp, agno, agino);
463 bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1da177e4
LT
464 /*
465 * Skip if this inode is free.
466 */
c2cba57e
LM
467 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
468 lastino = ino;
1da177e4 469 continue;
c2cba57e 470 }
1da177e4
LT
471 /*
472 * Count used inodes as free so we can tell
473 * when the chunk is used up.
474 */
26275093 475 irbp->ir_freecount++;
1da177e4
LT
476
477 /*
478 * Get the inode and fill in a single buffer.
1da177e4
LT
479 */
480 ubused = statstruct_size;
7b6259e7 481 error = formatter(mp, ino, ubufp, ubleft,
7dce11db 482 &ubused, &fmterror);
1da177e4 483 if (fmterror == BULKSTAT_RV_NOTHING) {
cd57e594
LM
484 if (error && error != ENOENT &&
485 error != EINVAL) {
e132f54c 486 ubleft = 0;
cd57e594
LM
487 rval = error;
488 break;
489 }
490 lastino = ino;
1da177e4
LT
491 continue;
492 }
493 if (fmterror == BULKSTAT_RV_GIVEUP) {
494 ubleft = 0;
495 ASSERT(error);
496 rval = error;
497 break;
498 }
499 if (ubufp)
500 ubufp += ubused;
501 ubleft -= ubused;
502 ubelem++;
503 lastino = ino;
504 }
cd57e594
LM
505
506 cond_resched();
1da177e4
LT
507 }
508
509 if (bp)
510 xfs_buf_relse(bp);
511
512 /*
513 * Set up for the next loop iteration.
514 */
cd57e594 515 if (XFS_BULKSTAT_UBLEFT(ubleft)) {
1da177e4
LT
516 if (end_of_ag) {
517 agno++;
518 agino = 0;
cd57e594
LM
519 } else
520 agino = XFS_INO_TO_AGINO(mp, lastino);
1da177e4
LT
521 } else
522 break;
523 }
524 /*
525 * Done, we're either out of filesystem or space to put the data.
526 */
bdfb0430 527 kmem_free_large(irbuf);
1da177e4 528 *ubcountp = ubelem;
cd57e594
LM
529 /*
530 * Found some inodes, return them now and return the error next time.
531 */
532 if (ubelem)
533 rval = 0;
1da177e4
LT
534 if (agno >= mp->m_sb.sb_agcount) {
535 /*
536 * If we ran out of filesystem, mark lastino as off
537 * the end of the filesystem, so the next call
538 * will return immediately.
539 */
540 *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
541 *done = 1;
542 } else
543 *lastinop = (xfs_ino_t)lastino;
544
545 return rval;
546}
547
548/*
549 * Return stat information in bulk (by-inode) for the filesystem.
550 * Special case for non-sequential one inode bulkstat.
551 */
552int /* error status */
553xfs_bulkstat_single(
554 xfs_mount_t *mp, /* mount point for filesystem */
555 xfs_ino_t *lastinop, /* inode to return */
556 char __user *buffer, /* buffer with inode stats */
c41564b5 557 int *done) /* 1 if there are more stats to get */
1da177e4
LT
558{
559 int count; /* count value for bulkstat call */
560 int error; /* return value */
561 xfs_ino_t ino; /* filesystem inode number */
562 int res; /* result from bs1 */
563
564 /*
565 * note that requesting valid inode numbers which are not allocated
566 * to inodes will most likely cause xfs_itobp to generate warning
567 * messages about bad magic numbers. This is ok. The fact that
568 * the inode isn't actually an inode is handled by the
569 * error check below. Done this way to make the usual case faster
570 * at the expense of the error case.
571 */
572
573 ino = (xfs_ino_t)*lastinop;
7b6259e7 574 error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t), 0, &res);
1da177e4
LT
575 if (error) {
576 /*
577 * Special case way failed, do it the "long" way
578 * to see if that works.
579 */
580 (*lastinop)--;
581 count = 1;
582 if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
7dce11db 583 sizeof(xfs_bstat_t), buffer, done))
1da177e4
LT
584 return error;
585 if (count == 0 || (xfs_ino_t)*lastinop != ino)
586 return error == EFSCORRUPTED ?
587 XFS_ERROR(EINVAL) : error;
588 else
589 return 0;
590 }
591 *done = 0;
592 return 0;
593}
594
faa63e95
MM
595int
596xfs_inumbers_fmt(
597 void __user *ubuffer, /* buffer to write to */
598 const xfs_inogrp_t *buffer, /* buffer to read from */
599 long count, /* # of elements to read */
600 long *written) /* # of bytes written */
601{
602 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
603 return -EFAULT;
604 *written = count * sizeof(*buffer);
605 return 0;
606}
607
1da177e4
LT
608/*
609 * Return inode number table for the filesystem.
610 */
611int /* error status */
612xfs_inumbers(
613 xfs_mount_t *mp, /* mount point for filesystem */
614 xfs_ino_t *lastino, /* last inode returned */
615 int *count, /* size of buffer/count returned */
faa63e95
MM
616 void __user *ubuffer,/* buffer with inode descriptions */
617 inumbers_fmt_pf formatter)
1da177e4
LT
618{
619 xfs_buf_t *agbp;
620 xfs_agino_t agino;
621 xfs_agnumber_t agno;
622 int bcount;
623 xfs_inogrp_t *buffer;
624 int bufidx;
625 xfs_btree_cur_t *cur;
626 int error;
2e287a73 627 xfs_inobt_rec_incore_t r;
1da177e4
LT
628 int i;
629 xfs_ino_t ino;
630 int left;
631 int tmp;
632
633 ino = (xfs_ino_t)*lastino;
634 agno = XFS_INO_TO_AGNO(mp, ino);
635 agino = XFS_INO_TO_AGINO(mp, ino);
636 left = *count;
637 *count = 0;
e6a4b37f 638 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
1da177e4
LT
639 buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
640 error = bufidx = 0;
641 cur = NULL;
642 agbp = NULL;
643 while (left > 0 && agno < mp->m_sb.sb_agcount) {
644 if (agbp == NULL) {
1da177e4 645 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
1da177e4
LT
646 if (error) {
647 /*
648 * If we can't read the AGI of this ag,
649 * then just skip to the next one.
650 */
651 ASSERT(cur == NULL);
652 agbp = NULL;
653 agno++;
654 agino = 0;
655 continue;
656 }
561f7d17 657 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
21875505
CH
658 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
659 &tmp);
1da177e4
LT
660 if (error) {
661 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
662 cur = NULL;
663 xfs_buf_relse(agbp);
664 agbp = NULL;
665 /*
59c51591 666 * Move up the last inode in the current
1da177e4
LT
667 * chunk. The lookup_ge will always get
668 * us the first inode in the next chunk.
669 */
670 agino += XFS_INODES_PER_CHUNK - 1;
671 continue;
672 }
673 }
2e287a73
CH
674 error = xfs_inobt_get_rec(cur, &r, &i);
675 if (error || i == 0) {
1da177e4
LT
676 xfs_buf_relse(agbp);
677 agbp = NULL;
678 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
679 cur = NULL;
680 agno++;
681 agino = 0;
682 continue;
683 }
2e287a73
CH
684 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
685 buffer[bufidx].xi_startino =
686 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
687 buffer[bufidx].xi_alloccount =
688 XFS_INODES_PER_CHUNK - r.ir_freecount;
689 buffer[bufidx].xi_allocmask = ~r.ir_free;
1da177e4
LT
690 bufidx++;
691 left--;
692 if (bufidx == bcount) {
faa63e95
MM
693 long written;
694 if (formatter(ubuffer, buffer, bufidx, &written)) {
1da177e4
LT
695 error = XFS_ERROR(EFAULT);
696 break;
697 }
faa63e95 698 ubuffer += written;
1da177e4
LT
699 *count += bufidx;
700 bufidx = 0;
701 }
702 if (left) {
637aa50f 703 error = xfs_btree_increment(cur, 0, &tmp);
1da177e4
LT
704 if (error) {
705 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
706 cur = NULL;
707 xfs_buf_relse(agbp);
708 agbp = NULL;
709 /*
710 * The agino value has already been bumped.
711 * Just try to skip up to it.
712 */
713 agino += XFS_INODES_PER_CHUNK;
714 continue;
715 }
716 }
717 }
718 if (!error) {
719 if (bufidx) {
faa63e95
MM
720 long written;
721 if (formatter(ubuffer, buffer, bufidx, &written))
1da177e4
LT
722 error = XFS_ERROR(EFAULT);
723 else
724 *count += bufidx;
725 }
726 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
727 }
f0e2d93c 728 kmem_free(buffer);
1da177e4
LT
729 if (cur)
730 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
731 XFS_BTREE_NOERROR));
732 if (agbp)
733 xfs_buf_relse(agbp);
734 return error;
735}
This page took 0.461524 seconds and 5 git commands to generate.