]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_itable.c
net: hns3: Fix for information of phydev lost problem when down/up
[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"
70a9883c 20#include "xfs_shared.h"
a4fbe6ab 21#include "xfs_format.h"
239880ef
DC
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
1da177e4 24#include "xfs_mount.h"
1da177e4 25#include "xfs_inode.h"
a4fbe6ab 26#include "xfs_btree.h"
1da177e4 27#include "xfs_ialloc.h"
a4fbe6ab 28#include "xfs_ialloc_btree.h"
1da177e4
LT
29#include "xfs_itable.h"
30#include "xfs_error.h"
f2d67614 31#include "xfs_trace.h"
33479e05 32#include "xfs_icache.h"
1da177e4 33
7dce11db
CH
34/*
35 * Return stat information for one inode.
36 * Return 0 if ok, else errno.
37 */
38int
39xfs_bulkstat_one_int(
40 struct xfs_mount *mp, /* mount point for filesystem */
41 xfs_ino_t ino, /* inode to get data for */
42 void __user *buffer, /* buffer to place output in */
43 int ubsize, /* size of buffer */
44 bulkstat_one_fmt_pf formatter, /* formatter, copy to user */
7dce11db
CH
45 int *ubused, /* bytes used by me */
46 int *stat) /* BULKSTAT_RV_... */
1da177e4 47{
7dce11db
CH
48 struct xfs_icdinode *dic; /* dinode core info pointer */
49 struct xfs_inode *ip; /* incore inode pointer */
3987848c 50 struct inode *inode;
7dce11db
CH
51 struct xfs_bstat *buf; /* return buffer */
52 int error = 0; /* error value */
53
54 *stat = BULKSTAT_RV_NOTHING;
55
56 if (!buffer || xfs_internal_inum(mp, ino))
2451337d 57 return -EINVAL;
7dce11db 58
f7ca3522 59 buf = kmem_zalloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
7dce11db 60 if (!buf)
2451337d 61 return -ENOMEM;
1da177e4 62
745b1f47 63 error = xfs_iget(mp, NULL, ino,
5132ba8f
DC
64 (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
65 XFS_ILOCK_SHARED, &ip);
8fe65776 66 if (error)
7dce11db 67 goto out_free;
1da177e4
LT
68
69 ASSERT(ip != NULL);
92bfc6e7 70 ASSERT(ip->i_imap.im_blkno != 0);
3987848c 71 inode = VFS_I(ip);
1da177e4
LT
72
73 dic = &ip->i_d;
74
75 /* xfs_iget returns the following without needing
76 * further change.
77 */
6743099c
AM
78 buf->bs_projid_lo = dic->di_projid_lo;
79 buf->bs_projid_hi = dic->di_projid_hi;
1da177e4 80 buf->bs_ino = ino;
1da177e4
LT
81 buf->bs_uid = dic->di_uid;
82 buf->bs_gid = dic->di_gid;
83 buf->bs_size = dic->di_size;
3987848c 84
54d7b5c1 85 buf->bs_nlink = inode->i_nlink;
3987848c
DC
86 buf->bs_atime.tv_sec = inode->i_atime.tv_sec;
87 buf->bs_atime.tv_nsec = inode->i_atime.tv_nsec;
88 buf->bs_mtime.tv_sec = inode->i_mtime.tv_sec;
89 buf->bs_mtime.tv_nsec = inode->i_mtime.tv_nsec;
90 buf->bs_ctime.tv_sec = inode->i_ctime.tv_sec;
91 buf->bs_ctime.tv_nsec = inode->i_ctime.tv_nsec;
9e9a2674 92 buf->bs_gen = inode->i_generation;
c19b3b05 93 buf->bs_mode = inode->i_mode;
3987848c 94
1da177e4
LT
95 buf->bs_xflags = xfs_ip2xflags(ip);
96 buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
97 buf->bs_extents = dic->di_nextents;
1da177e4
LT
98 memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
99 buf->bs_dmevmask = dic->di_dmevmask;
100 buf->bs_dmstate = dic->di_dmstate;
101 buf->bs_aextents = dic->di_anextents;
07000ee6 102 buf->bs_forkoff = XFS_IFORK_BOFF(ip);
1da177e4 103
f7ca3522
DW
104 if (dic->di_version == 3) {
105 if (dic->di_flags2 & XFS_DIFLAG2_COWEXTSIZE)
106 buf->bs_cowextsize = dic->di_cowextsize <<
107 mp->m_sb.sb_blocklog;
108 }
109
1da177e4
LT
110 switch (dic->di_format) {
111 case XFS_DINODE_FMT_DEV:
66f36464 112 buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
1da177e4
LT
113 buf->bs_blksize = BLKDEV_IOSIZE;
114 buf->bs_blocks = 0;
115 break;
116 case XFS_DINODE_FMT_LOCAL:
1da177e4
LT
117 buf->bs_rdev = 0;
118 buf->bs_blksize = mp->m_sb.sb_blocksize;
119 buf->bs_blocks = 0;
120 break;
121 case XFS_DINODE_FMT_EXTENTS:
122 case XFS_DINODE_FMT_BTREE:
123 buf->bs_rdev = 0;
124 buf->bs_blksize = mp->m_sb.sb_blocksize;
125 buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
126 break;
127 }
f2d67614
CH
128 xfs_iunlock(ip, XFS_ILOCK_SHARED);
129 IRELE(ip);
1da177e4 130
7dce11db 131 error = formatter(buffer, ubsize, ubused, buf);
7dce11db
CH
132 if (!error)
133 *stat = BULKSTAT_RV_DIDONE;
1da177e4 134
7dce11db
CH
135 out_free:
136 kmem_free(buf);
137 return error;
1da177e4
LT
138}
139
65fbaf24 140/* Return 0 on success or positive error */
faa63e95
MM
141STATIC int
142xfs_bulkstat_one_fmt(
143 void __user *ubuffer,
65fbaf24 144 int ubsize,
145 int *ubused,
faa63e95
MM
146 const xfs_bstat_t *buffer)
147{
65fbaf24 148 if (ubsize < sizeof(*buffer))
2451337d 149 return -ENOMEM;
faa63e95 150 if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
2451337d 151 return -EFAULT;
65fbaf24 152 if (ubused)
153 *ubused = sizeof(*buffer);
154 return 0;
faa63e95
MM
155}
156
2ee4fa5c 157int
158xfs_bulkstat_one(
159 xfs_mount_t *mp, /* mount point for filesystem */
160 xfs_ino_t ino, /* inode number to get data for */
161 void __user *buffer, /* buffer to place output in */
162 int ubsize, /* size of buffer */
2ee4fa5c 163 int *ubused, /* bytes used by me */
2ee4fa5c 164 int *stat) /* BULKSTAT_RV_... */
165{
166 return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
7b6259e7 167 xfs_bulkstat_one_fmt, ubused, stat);
8b56f083
NS
168}
169
4b8fdfec
JL
170/*
171 * Loop over all clusters in a chunk for a given incore inode allocation btree
172 * record. Do a readahead if there are any allocated inodes in that cluster.
173 */
174STATIC void
175xfs_bulkstat_ichunk_ra(
176 struct xfs_mount *mp,
177 xfs_agnumber_t agno,
178 struct xfs_inobt_rec_incore *irec)
179{
180 xfs_agblock_t agbno;
181 struct blk_plug plug;
182 int blks_per_cluster;
183 int inodes_per_cluster;
184 int i; /* inode chunk index */
185
186 agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
187 blks_per_cluster = xfs_icluster_size_fsb(mp);
188 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
189
190 blk_start_plug(&plug);
191 for (i = 0; i < XFS_INODES_PER_CHUNK;
192 i += inodes_per_cluster, agbno += blks_per_cluster) {
193 if (xfs_inobt_maskn(i, inodes_per_cluster) & ~irec->ir_free) {
194 xfs_btree_reada_bufs(mp, agno, agbno, blks_per_cluster,
195 &xfs_inode_buf_ops);
196 }
197 }
198 blk_finish_plug(&plug);
199}
200
f3d1e587
JL
201/*
202 * Lookup the inode chunk that the given inode lives in and then get the record
203 * if we found the chunk. If the inode was not the last in the chunk and there
204 * are some left allocated, update the data for the pointed-to record as well as
205 * return the count of grabbed inodes.
206 */
207STATIC int
208xfs_bulkstat_grab_ichunk(
209 struct xfs_btree_cur *cur, /* btree cursor */
210 xfs_agino_t agino, /* starting inode of chunk */
211 int *icount,/* return # of inodes grabbed */
212 struct xfs_inobt_rec_incore *irec) /* btree record */
213{
214 int idx; /* index into inode chunk */
215 int stat;
216 int error = 0;
217
218 /* Lookup the inode chunk that this inode lives in */
219 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &stat);
220 if (error)
221 return error;
222 if (!stat) {
223 *icount = 0;
224 return error;
225 }
226
227 /* Get the record, should always work */
228 error = xfs_inobt_get_rec(cur, irec, &stat);
229 if (error)
230 return error;
5fb5aeee 231 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, stat == 1);
f3d1e587
JL
232
233 /* Check if the record contains the inode in request */
febe3cbe
DC
234 if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino) {
235 *icount = 0;
236 return 0;
237 }
f3d1e587
JL
238
239 idx = agino - irec->ir_startino + 1;
240 if (idx < XFS_INODES_PER_CHUNK &&
241 (xfs_inobt_maskn(idx, XFS_INODES_PER_CHUNK - idx) & ~irec->ir_free)) {
242 int i;
243
244 /* We got a right chunk with some left inodes allocated at it.
245 * Grab the chunk record. Mark all the uninteresting inodes
246 * free -- because they're before our start point.
247 */
248 for (i = 0; i < idx; i++) {
249 if (XFS_INOBT_MASK(i) & ~irec->ir_free)
250 irec->ir_freecount++;
251 }
252
253 irec->ir_free |= xfs_inobt_maskn(0, idx);
12d0714d 254 *icount = irec->ir_count - irec->ir_freecount;
f3d1e587
JL
255 }
256
257 return 0;
258}
259
cd57e594
LM
260#define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
261
bf4a5af2
DC
262struct xfs_bulkstat_agichunk {
263 char __user **ac_ubuffer;/* pointer into user's buffer */
264 int ac_ubleft; /* bytes left in user's buffer */
265 int ac_ubelem; /* spaces used in user's buffer */
266};
267
1e773c49
JL
268/*
269 * Process inodes in chunk with a pointer to a formatter function
270 * that will iget the inode and fill in the appropriate structure.
271 */
bf4a5af2 272static int
1e773c49
JL
273xfs_bulkstat_ag_ichunk(
274 struct xfs_mount *mp,
275 xfs_agnumber_t agno,
276 struct xfs_inobt_rec_incore *irbp,
277 bulkstat_one_pf formatter,
278 size_t statstruct_size,
bf4a5af2 279 struct xfs_bulkstat_agichunk *acp,
00275899 280 xfs_agino_t *last_agino)
1e773c49 281{
1e773c49 282 char __user **ubufp = acp->ac_ubuffer;
2b831ac6 283 int chunkidx;
1e773c49 284 int error = 0;
00275899 285 xfs_agino_t agino = irbp->ir_startino;
1e773c49 286
2b831ac6
DC
287 for (chunkidx = 0; chunkidx < XFS_INODES_PER_CHUNK;
288 chunkidx++, agino++) {
289 int fmterror;
1e773c49 290 int ubused;
00275899
DC
291
292 /* inode won't fit in buffer, we are done */
293 if (acp->ac_ubleft < statstruct_size)
294 break;
1e773c49 295
1e773c49 296 /* Skip if this inode is free */
00275899 297 if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
1e773c49 298 continue;
1e773c49 299
1e773c49
JL
300 /* Get the inode and fill in a single buffer */
301 ubused = statstruct_size;
00275899
DC
302 error = formatter(mp, XFS_AGINO_TO_INO(mp, agno, agino),
303 *ubufp, acp->ac_ubleft, &ubused, &fmterror);
304
2b831ac6
DC
305 if (fmterror == BULKSTAT_RV_GIVEUP ||
306 (error && error != -ENOENT && error != -EINVAL)) {
307 acp->ac_ubleft = 0;
1e773c49
JL
308 ASSERT(error);
309 break;
310 }
2b831ac6
DC
311
312 /* be careful not to leak error if at end of chunk */
313 if (fmterror == BULKSTAT_RV_NOTHING || error) {
2b831ac6
DC
314 error = 0;
315 continue;
316 }
317
318 *ubufp += ubused;
319 acp->ac_ubleft -= ubused;
320 acp->ac_ubelem++;
2b831ac6 321 }
1e773c49 322
00275899
DC
323 /*
324 * Post-update *last_agino. At this point, agino will always point one
325 * inode past the last inode we processed successfully. Hence we
326 * substract that inode when setting the *last_agino cursor so that we
327 * return the correct cookie to userspace. On the next bulkstat call,
328 * the inode under the lastino cookie will be skipped as we have already
329 * processed it here.
330 */
331 *last_agino = agino - 1;
332
1e773c49
JL
333 return error;
334}
335
1da177e4
LT
336/*
337 * Return stat information in bulk (by-inode) for the filesystem.
338 */
339int /* error status */
340xfs_bulkstat(
341 xfs_mount_t *mp, /* mount point for filesystem */
342 xfs_ino_t *lastinop, /* last inode returned */
343 int *ubcountp, /* size of buffer/count returned */
344 bulkstat_one_pf formatter, /* func that'd fill a single buf */
1da177e4
LT
345 size_t statstruct_size, /* sizeof struct filling */
346 char __user *ubuffer, /* buffer with inode stats */
c41564b5 347 int *done) /* 1 if there are more stats to get */
1da177e4 348{
1da177e4 349 xfs_buf_t *agbp; /* agi header buffer */
1da177e4
LT
350 xfs_agino_t agino; /* inode # in allocation group */
351 xfs_agnumber_t agno; /* allocation group number */
1da177e4 352 xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
26275093 353 xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
1da177e4 354 int nirbuf; /* size of irbuf */
1da177e4 355 int ubcount; /* size of user's buffer */
bf4a5af2 356 struct xfs_bulkstat_agichunk ac;
6e57c542 357 int error = 0;
1da177e4
LT
358
359 /*
360 * Get the last inode value, see if there's nothing to do.
361 */
00275899
DC
362 agno = XFS_INO_TO_AGNO(mp, *lastinop);
363 agino = XFS_INO_TO_AGINO(mp, *lastinop);
1da177e4 364 if (agno >= mp->m_sb.sb_agcount ||
00275899 365 *lastinop != XFS_AGINO_TO_INO(mp, agno, agino)) {
1da177e4
LT
366 *done = 1;
367 *ubcountp = 0;
368 return 0;
369 }
296dfd7f 370
1da177e4 371 ubcount = *ubcountp; /* statstruct's */
bf4a5af2
DC
372 ac.ac_ubuffer = &ubuffer;
373 ac.ac_ubleft = ubcount * statstruct_size; /* bytes */;
374 ac.ac_ubelem = 0;
375
376 *ubcountp = 0;
1da177e4 377 *done = 0;
bf4a5af2 378
08b005f1 379 irbuf = kmem_zalloc_large(PAGE_SIZE * 4, KM_SLEEP);
bdfb0430 380 if (!irbuf)
2451337d 381 return -ENOMEM;
08b005f1 382 nirbuf = (PAGE_SIZE * 4) / sizeof(*irbuf);
bb3c7d29 383
1da177e4
LT
384 /*
385 * Loop over the allocation groups, starting from the last
386 * inode returned; 0 means start of the allocation group.
387 */
6e57c542
DC
388 while (agno < mp->m_sb.sb_agcount) {
389 struct xfs_inobt_rec_incore *irbp = irbuf;
390 struct xfs_inobt_rec_incore *irbufend = irbuf + nirbuf;
391 bool end_of_ag = false;
392 int icount = 0;
393 int stat;
394
1da177e4 395 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
d4c27348
JL
396 if (error)
397 break;
1da177e4
LT
398 /*
399 * Allocate and initialize a btree cursor for ialloc btree.
400 */
57bd3dbe
BF
401 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
402 XFS_BTNUM_INO);
1da177e4
LT
403 if (agino > 0) {
404 /*
f3d1e587
JL
405 * In the middle of an allocation group, we need to get
406 * the remainder of the chunk we're in.
1da177e4 407 */
f3d1e587
JL
408 struct xfs_inobt_rec_incore r;
409
410 error = xfs_bulkstat_grab_ichunk(cur, agino, &icount, &r);
411 if (error)
a6bbce54 412 goto del_cursor;
f3d1e587 413 if (icount) {
2e287a73 414 irbp->ir_startino = r.ir_startino;
12d0714d
BF
415 irbp->ir_holemask = r.ir_holemask;
416 irbp->ir_count = r.ir_count;
2e287a73
CH
417 irbp->ir_freecount = r.ir_freecount;
418 irbp->ir_free = r.ir_free;
1da177e4 419 irbp++;
1da177e4 420 }
f3d1e587 421 /* Increment to the next record */
afa947cb 422 error = xfs_btree_increment(cur, 0, &stat);
1da177e4 423 } else {
f3d1e587 424 /* Start of ag. Lookup the first inode chunk */
afa947cb 425 error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &stat);
1da177e4 426 }
afa947cb 427 if (error || stat == 0) {
6e57c542 428 end_of_ag = true;
a6bbce54 429 goto del_cursor;
afa947cb 430 }
d4c27348 431
1da177e4
LT
432 /*
433 * Loop through inode btree records in this ag,
434 * until we run out of inodes or space in the buffer.
435 */
436 while (irbp < irbufend && icount < ubcount) {
d4c27348 437 struct xfs_inobt_rec_incore r;
2e287a73 438
afa947cb
DC
439 error = xfs_inobt_get_rec(cur, &r, &stat);
440 if (error || stat == 0) {
6e57c542 441 end_of_ag = true;
a6bbce54 442 goto del_cursor;
1da177e4 443 }
2e287a73 444
1da177e4
LT
445 /*
446 * If this chunk has any allocated inodes, save it.
26275093 447 * Also start read-ahead now for this chunk.
1da177e4 448 */
12d0714d 449 if (r.ir_freecount < r.ir_count) {
4b8fdfec 450 xfs_bulkstat_ichunk_ra(mp, agno, &r);
2e287a73 451 irbp->ir_startino = r.ir_startino;
12d0714d
BF
452 irbp->ir_holemask = r.ir_holemask;
453 irbp->ir_count = r.ir_count;
2e287a73
CH
454 irbp->ir_freecount = r.ir_freecount;
455 irbp->ir_free = r.ir_free;
1da177e4 456 irbp++;
12d0714d 457 icount += r.ir_count - r.ir_freecount;
1da177e4 458 }
afa947cb
DC
459 error = xfs_btree_increment(cur, 0, &stat);
460 if (error || stat == 0) {
6e57c542 461 end_of_ag = true;
7a19dee1
JK
462 goto del_cursor;
463 }
cd57e594 464 cond_resched();
1da177e4 465 }
a6bbce54 466
1da177e4 467 /*
a6bbce54
DC
468 * Drop the btree buffers and the agi buffer as we can't hold any
469 * of the locks these represent when calling iget. If there is a
470 * pending error, then we are done.
1da177e4 471 */
a6bbce54 472del_cursor:
f307080a
BF
473 xfs_btree_del_cursor(cur, error ?
474 XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1da177e4 475 xfs_buf_relse(agbp);
a6bbce54
DC
476 if (error)
477 break;
1da177e4 478 /*
00275899
DC
479 * Now format all the good inodes into the user's buffer. The
480 * call to xfs_bulkstat_ag_ichunk() sets up the agino pointer
481 * for the next loop iteration.
1da177e4
LT
482 */
483 irbufend = irbp;
484 for (irbp = irbuf;
6e57c542 485 irbp < irbufend && ac.ac_ubleft >= statstruct_size;
bf4a5af2 486 irbp++) {
1e773c49 487 error = xfs_bulkstat_ag_ichunk(mp, agno, irbp,
bf4a5af2 488 formatter, statstruct_size, &ac,
00275899 489 &agino);
1e773c49 490 if (error)
febe3cbe 491 break;
1e773c49 492
cd57e594 493 cond_resched();
1da177e4 494 }
bf4a5af2 495
febe3cbe
DC
496 /*
497 * If we've run out of space or had a formatting error, we
498 * are now done
499 */
500 if (ac.ac_ubleft < statstruct_size || error)
1da177e4 501 break;
6e57c542
DC
502
503 if (end_of_ag) {
504 agno++;
505 agino = 0;
00275899 506 }
1da177e4
LT
507 }
508 /*
509 * Done, we're either out of filesystem or space to put the data.
510 */
fdd3ccee 511 kmem_free(irbuf);
bf4a5af2 512 *ubcountp = ac.ac_ubelem;
febe3cbe 513
cd57e594 514 /*
febe3cbe
DC
515 * We found some inodes, so clear the error status and return them.
516 * The lastino pointer will point directly at the inode that triggered
517 * any error that occurred, so on the next call the error will be
518 * triggered again and propagated to userspace as there will be no
519 * formatted inodes in the buffer.
cd57e594 520 */
bf4a5af2 521 if (ac.ac_ubelem)
febe3cbe
DC
522 error = 0;
523
00275899
DC
524 /*
525 * If we ran out of filesystem, lastino will point off the end of
526 * the filesystem so the next call will return immediately.
527 */
528 *lastinop = XFS_AGINO_TO_INO(mp, agno, agino);
529 if (agno >= mp->m_sb.sb_agcount)
1da177e4 530 *done = 1;
1da177e4 531
febe3cbe 532 return error;
1da177e4
LT
533}
534
faa63e95
MM
535int
536xfs_inumbers_fmt(
537 void __user *ubuffer, /* buffer to write to */
549fa006 538 const struct xfs_inogrp *buffer, /* buffer to read from */
faa63e95
MM
539 long count, /* # of elements to read */
540 long *written) /* # of bytes written */
541{
542 if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
543 return -EFAULT;
544 *written = count * sizeof(*buffer);
545 return 0;
546}
547
1da177e4
LT
548/*
549 * Return inode number table for the filesystem.
550 */
551int /* error status */
552xfs_inumbers(
549fa006
JL
553 struct xfs_mount *mp,/* mount point for filesystem */
554 xfs_ino_t *lastino,/* last inode returned */
555 int *count,/* size of buffer/count returned */
556 void __user *ubuffer,/* buffer with inode descriptions */
557 inumbers_fmt_pf formatter)
1da177e4 558{
549fa006
JL
559 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, *lastino);
560 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, *lastino);
561 struct xfs_btree_cur *cur = NULL;
c7cb51dc 562 struct xfs_buf *agbp = NULL;
549fa006
JL
563 struct xfs_inogrp *buffer;
564 int bcount;
565 int left = *count;
566 int bufidx = 0;
567 int error = 0;
568
1da177e4 569 *count = 0;
549fa006
JL
570 if (agno >= mp->m_sb.sb_agcount ||
571 *lastino != XFS_AGINO_TO_INO(mp, agno, agino))
572 return error;
573
e6a4b37f 574 bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
bf9216f9 575 buffer = kmem_zalloc(bcount * sizeof(*buffer), KM_SLEEP);
c7cb51dc 576 do {
549fa006
JL
577 struct xfs_inobt_rec_incore r;
578 int stat;
579
c7cb51dc 580 if (!agbp) {
1da177e4 581 error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
c7cb51dc
JL
582 if (error)
583 break;
584
57bd3dbe
BF
585 cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
586 XFS_BTNUM_INO);
21875505 587 error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
549fa006 588 &stat);
c7cb51dc
JL
589 if (error)
590 break;
591 if (!stat)
592 goto next_ag;
1da177e4 593 }
c7cb51dc 594
549fa006 595 error = xfs_inobt_get_rec(cur, &r, &stat);
c7cb51dc
JL
596 if (error)
597 break;
598 if (!stat)
599 goto next_ag;
600
2e287a73
CH
601 agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
602 buffer[bufidx].xi_startino =
603 XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
12d0714d 604 buffer[bufidx].xi_alloccount = r.ir_count - r.ir_freecount;
2e287a73 605 buffer[bufidx].xi_allocmask = ~r.ir_free;
c7cb51dc
JL
606 if (++bufidx == bcount) {
607 long written;
608
549fa006
JL
609 error = formatter(ubuffer, buffer, bufidx, &written);
610 if (error)
1da177e4 611 break;
faa63e95 612 ubuffer += written;
1da177e4
LT
613 *count += bufidx;
614 bufidx = 0;
615 }
c7cb51dc
JL
616 if (!--left)
617 break;
618
619 error = xfs_btree_increment(cur, 0, &stat);
620 if (error)
621 break;
622 if (stat)
623 continue;
624
625next_ag:
626 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
627 cur = NULL;
628 xfs_buf_relse(agbp);
629 agbp = NULL;
630 agino = 0;
a8b1ee8b
ES
631 agno++;
632 } while (agno < mp->m_sb.sb_agcount);
c7cb51dc 633
1da177e4
LT
634 if (!error) {
635 if (bufidx) {
c7cb51dc
JL
636 long written;
637
549fa006
JL
638 error = formatter(ubuffer, buffer, bufidx, &written);
639 if (!error)
1da177e4
LT
640 *count += bufidx;
641 }
642 *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
643 }
c7cb51dc 644
f0e2d93c 645 kmem_free(buffer);
1da177e4
LT
646 if (cur)
647 xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
648 XFS_BTREE_NOERROR));
649 if (agbp)
650 xfs_buf_relse(agbp);
c7cb51dc 651
1da177e4
LT
652 return error;
653}