xfs_itable.c 17 KB

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