xfs_itable.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 xfs_bstat *buf; /* return buffer */
  59. int error = 0; /* error value */
  60. *stat = BULKSTAT_RV_NOTHING;
  61. if (!buffer || xfs_internal_inum(mp, ino))
  62. return -EINVAL;
  63. buf = kmem_alloc(sizeof(*buf), KM_SLEEP | KM_MAYFAIL);
  64. if (!buf)
  65. return -ENOMEM;
  66. error = xfs_iget(mp, NULL, ino,
  67. (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
  68. XFS_ILOCK_SHARED, &ip);
  69. if (error)
  70. goto out_free;
  71. ASSERT(ip != NULL);
  72. ASSERT(ip->i_imap.im_blkno != 0);
  73. dic = &ip->i_d;
  74. /* xfs_iget returns the following without needing
  75. * further change.
  76. */
  77. buf->bs_nlink = dic->di_nlink;
  78. buf->bs_projid_lo = dic->di_projid_lo;
  79. buf->bs_projid_hi = dic->di_projid_hi;
  80. buf->bs_ino = ino;
  81. buf->bs_mode = dic->di_mode;
  82. buf->bs_uid = dic->di_uid;
  83. buf->bs_gid = dic->di_gid;
  84. buf->bs_size = dic->di_size;
  85. buf->bs_atime.tv_sec = dic->di_atime.t_sec;
  86. buf->bs_atime.tv_nsec = dic->di_atime.t_nsec;
  87. buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
  88. buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
  89. buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
  90. buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
  91. buf->bs_xflags = xfs_ip2xflags(ip);
  92. buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
  93. buf->bs_extents = dic->di_nextents;
  94. buf->bs_gen = dic->di_gen;
  95. memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
  96. buf->bs_dmevmask = dic->di_dmevmask;
  97. buf->bs_dmstate = dic->di_dmstate;
  98. buf->bs_aextents = dic->di_anextents;
  99. buf->bs_forkoff = XFS_IFORK_BOFF(ip);
  100. switch (dic->di_format) {
  101. case XFS_DINODE_FMT_DEV:
  102. buf->bs_rdev = ip->i_df.if_u2.if_rdev;
  103. buf->bs_blksize = BLKDEV_IOSIZE;
  104. buf->bs_blocks = 0;
  105. break;
  106. case XFS_DINODE_FMT_LOCAL:
  107. case XFS_DINODE_FMT_UUID:
  108. buf->bs_rdev = 0;
  109. buf->bs_blksize = mp->m_sb.sb_blocksize;
  110. buf->bs_blocks = 0;
  111. break;
  112. case XFS_DINODE_FMT_EXTENTS:
  113. case XFS_DINODE_FMT_BTREE:
  114. buf->bs_rdev = 0;
  115. buf->bs_blksize = mp->m_sb.sb_blocksize;
  116. buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
  117. break;
  118. }
  119. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  120. IRELE(ip);
  121. error = formatter(buffer, ubsize, ubused, buf);
  122. if (!error)
  123. *stat = BULKSTAT_RV_DIDONE;
  124. out_free:
  125. kmem_free(buf);
  126. return error;
  127. }
  128. /* Return 0 on success or positive error */
  129. STATIC int
  130. xfs_bulkstat_one_fmt(
  131. void __user *ubuffer,
  132. int ubsize,
  133. int *ubused,
  134. const xfs_bstat_t *buffer)
  135. {
  136. if (ubsize < sizeof(*buffer))
  137. return -ENOMEM;
  138. if (copy_to_user(ubuffer, buffer, sizeof(*buffer)))
  139. return -EFAULT;
  140. if (ubused)
  141. *ubused = sizeof(*buffer);
  142. return 0;
  143. }
  144. int
  145. xfs_bulkstat_one(
  146. xfs_mount_t *mp, /* mount point for filesystem */
  147. xfs_ino_t ino, /* inode number to get data for */
  148. void __user *buffer, /* buffer to place output in */
  149. int ubsize, /* size of buffer */
  150. int *ubused, /* bytes used by me */
  151. int *stat) /* BULKSTAT_RV_... */
  152. {
  153. return xfs_bulkstat_one_int(mp, ino, buffer, ubsize,
  154. xfs_bulkstat_one_fmt, ubused, stat);
  155. }
  156. /*
  157. * Loop over all clusters in a chunk for a given incore inode allocation btree
  158. * record. Do a readahead if there are any allocated inodes in that cluster.
  159. */
  160. STATIC void
  161. xfs_bulkstat_ichunk_ra(
  162. struct xfs_mount *mp,
  163. xfs_agnumber_t agno,
  164. struct xfs_inobt_rec_incore *irec)
  165. {
  166. xfs_agblock_t agbno;
  167. struct blk_plug plug;
  168. int blks_per_cluster;
  169. int inodes_per_cluster;
  170. int i; /* inode chunk index */
  171. agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
  172. blks_per_cluster = xfs_icluster_size_fsb(mp);
  173. inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
  174. blk_start_plug(&plug);
  175. for (i = 0; i < XFS_INODES_PER_CHUNK;
  176. i += inodes_per_cluster, agbno += blks_per_cluster) {
  177. if (xfs_inobt_maskn(i, inodes_per_cluster) & ~irec->ir_free) {
  178. xfs_btree_reada_bufs(mp, agno, agbno, blks_per_cluster,
  179. &xfs_inode_buf_ops);
  180. }
  181. }
  182. blk_finish_plug(&plug);
  183. }
  184. /*
  185. * Lookup the inode chunk that the given inode lives in and then get the record
  186. * if we found the chunk. If the inode was not the last in the chunk and there
  187. * are some left allocated, update the data for the pointed-to record as well as
  188. * return the count of grabbed inodes.
  189. */
  190. STATIC int
  191. xfs_bulkstat_grab_ichunk(
  192. struct xfs_btree_cur *cur, /* btree cursor */
  193. xfs_agino_t agino, /* starting inode of chunk */
  194. int *icount,/* return # of inodes grabbed */
  195. struct xfs_inobt_rec_incore *irec) /* btree record */
  196. {
  197. int idx; /* index into inode chunk */
  198. int stat;
  199. int error = 0;
  200. /* Lookup the inode chunk that this inode lives in */
  201. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &stat);
  202. if (error)
  203. return error;
  204. if (!stat) {
  205. *icount = 0;
  206. return error;
  207. }
  208. /* Get the record, should always work */
  209. error = xfs_inobt_get_rec(cur, irec, &stat);
  210. if (error)
  211. return error;
  212. XFS_WANT_CORRUPTED_RETURN(stat == 1);
  213. /* Check if the record contains the inode in request */
  214. if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino)
  215. return -EINVAL;
  216. idx = agino - irec->ir_startino + 1;
  217. if (idx < XFS_INODES_PER_CHUNK &&
  218. (xfs_inobt_maskn(idx, XFS_INODES_PER_CHUNK - idx) & ~irec->ir_free)) {
  219. int i;
  220. /* We got a right chunk with some left inodes allocated at it.
  221. * Grab the chunk record. Mark all the uninteresting inodes
  222. * free -- because they're before our start point.
  223. */
  224. for (i = 0; i < idx; i++) {
  225. if (XFS_INOBT_MASK(i) & ~irec->ir_free)
  226. irec->ir_freecount++;
  227. }
  228. irec->ir_free |= xfs_inobt_maskn(0, idx);
  229. *icount = XFS_INODES_PER_CHUNK - irec->ir_freecount;
  230. }
  231. return 0;
  232. }
  233. #define XFS_BULKSTAT_UBLEFT(ubleft) ((ubleft) >= statstruct_size)
  234. /*
  235. * Process inodes in chunk with a pointer to a formatter function
  236. * that will iget the inode and fill in the appropriate structure.
  237. */
  238. int
  239. xfs_bulkstat_ag_ichunk(
  240. struct xfs_mount *mp,
  241. xfs_agnumber_t agno,
  242. struct xfs_inobt_rec_incore *irbp,
  243. bulkstat_one_pf formatter,
  244. size_t statstruct_size,
  245. struct xfs_bulkstat_agichunk *acp)
  246. {
  247. xfs_ino_t lastino = acp->ac_lastino;
  248. char __user **ubufp = acp->ac_ubuffer;
  249. int ubleft = acp->ac_ubleft;
  250. int ubelem = acp->ac_ubelem;
  251. int chunkidx, clustidx;
  252. int error = 0;
  253. xfs_agino_t agino;
  254. for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
  255. XFS_BULKSTAT_UBLEFT(ubleft) &&
  256. irbp->ir_freecount < XFS_INODES_PER_CHUNK;
  257. chunkidx++, clustidx++, agino++) {
  258. int fmterror; /* bulkstat formatter result */
  259. int ubused;
  260. xfs_ino_t ino = XFS_AGINO_TO_INO(mp, agno, agino);
  261. ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
  262. /* Skip if this inode is free */
  263. if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
  264. lastino = ino;
  265. continue;
  266. }
  267. /*
  268. * Count used inodes as free so we can tell when the
  269. * chunk is used up.
  270. */
  271. irbp->ir_freecount++;
  272. /* Get the inode and fill in a single buffer */
  273. ubused = statstruct_size;
  274. error = formatter(mp, ino, *ubufp, ubleft, &ubused, &fmterror);
  275. if (fmterror == BULKSTAT_RV_NOTHING) {
  276. if (error && error != -ENOENT && error != -EINVAL) {
  277. ubleft = 0;
  278. break;
  279. }
  280. lastino = ino;
  281. continue;
  282. }
  283. if (fmterror == BULKSTAT_RV_GIVEUP) {
  284. ubleft = 0;
  285. ASSERT(error);
  286. break;
  287. }
  288. if (*ubufp)
  289. *ubufp += ubused;
  290. ubleft -= ubused;
  291. ubelem++;
  292. lastino = ino;
  293. }
  294. acp->ac_lastino = lastino;
  295. acp->ac_ubleft = ubleft;
  296. acp->ac_ubelem = ubelem;
  297. return error;
  298. }
  299. /*
  300. * Return stat information in bulk (by-inode) for the filesystem.
  301. */
  302. int /* error status */
  303. xfs_bulkstat(
  304. xfs_mount_t *mp, /* mount point for filesystem */
  305. xfs_ino_t *lastinop, /* last inode returned */
  306. int *ubcountp, /* size of buffer/count returned */
  307. bulkstat_one_pf formatter, /* func that'd fill a single buf */
  308. size_t statstruct_size, /* sizeof struct filling */
  309. char __user *ubuffer, /* buffer with inode stats */
  310. int *done) /* 1 if there are more stats to get */
  311. {
  312. xfs_buf_t *agbp; /* agi header buffer */
  313. xfs_agi_t *agi; /* agi header data */
  314. xfs_agino_t agino; /* inode # in allocation group */
  315. xfs_agnumber_t agno; /* allocation group number */
  316. xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
  317. int end_of_ag; /* set if we've seen the ag end */
  318. int error; /* error code */
  319. int fmterror;/* bulkstat formatter result */
  320. int i; /* loop index */
  321. int icount; /* count of inodes good in irbuf */
  322. size_t irbsize; /* size of irec buffer in bytes */
  323. xfs_ino_t ino; /* inode number (filesystem) */
  324. xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
  325. xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
  326. xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
  327. xfs_ino_t lastino; /* last inode number returned */
  328. int nirbuf; /* size of irbuf */
  329. int rval; /* return value error code */
  330. int tmp; /* result value from btree calls */
  331. int ubcount; /* size of user's buffer */
  332. int ubleft; /* bytes left in user's buffer */
  333. char __user *ubufp; /* pointer into user's buffer */
  334. int ubelem; /* spaces used in user's buffer */
  335. /*
  336. * Get the last inode value, see if there's nothing to do.
  337. */
  338. ino = (xfs_ino_t)*lastinop;
  339. lastino = ino;
  340. agno = XFS_INO_TO_AGNO(mp, ino);
  341. agino = XFS_INO_TO_AGINO(mp, ino);
  342. if (agno >= mp->m_sb.sb_agcount ||
  343. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  344. *done = 1;
  345. *ubcountp = 0;
  346. return 0;
  347. }
  348. ubcount = *ubcountp; /* statstruct's */
  349. ubleft = ubcount * statstruct_size; /* bytes */
  350. *ubcountp = ubelem = 0;
  351. *done = 0;
  352. fmterror = 0;
  353. ubufp = ubuffer;
  354. irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
  355. if (!irbuf)
  356. return -ENOMEM;
  357. nirbuf = irbsize / sizeof(*irbuf);
  358. /*
  359. * Loop over the allocation groups, starting from the last
  360. * inode returned; 0 means start of the allocation group.
  361. */
  362. rval = 0;
  363. while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
  364. cond_resched();
  365. error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
  366. if (error)
  367. break;
  368. agi = XFS_BUF_TO_AGI(agbp);
  369. /*
  370. * Allocate and initialize a btree cursor for ialloc btree.
  371. */
  372. cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
  373. XFS_BTNUM_INO);
  374. irbp = irbuf;
  375. irbufend = irbuf + nirbuf;
  376. end_of_ag = 0;
  377. icount = 0;
  378. if (agino > 0) {
  379. /*
  380. * In the middle of an allocation group, we need to get
  381. * the remainder of the chunk we're in.
  382. */
  383. struct xfs_inobt_rec_incore r;
  384. error = xfs_bulkstat_grab_ichunk(cur, agino, &icount, &r);
  385. if (error)
  386. break;
  387. if (icount) {
  388. irbp->ir_startino = r.ir_startino;
  389. irbp->ir_freecount = r.ir_freecount;
  390. irbp->ir_free = r.ir_free;
  391. irbp++;
  392. agino = r.ir_startino + XFS_INODES_PER_CHUNK;
  393. }
  394. /* Increment to the next record */
  395. error = xfs_btree_increment(cur, 0, &tmp);
  396. } else {
  397. /* Start of ag. Lookup the first inode chunk */
  398. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
  399. }
  400. if (error)
  401. break;
  402. /*
  403. * Loop through inode btree records in this ag,
  404. * until we run out of inodes or space in the buffer.
  405. */
  406. while (irbp < irbufend && icount < ubcount) {
  407. struct xfs_inobt_rec_incore r;
  408. error = xfs_inobt_get_rec(cur, &r, &i);
  409. if (error || i == 0) {
  410. end_of_ag = 1;
  411. break;
  412. }
  413. /*
  414. * If this chunk has any allocated inodes, save it.
  415. * Also start read-ahead now for this chunk.
  416. */
  417. if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
  418. xfs_bulkstat_ichunk_ra(mp, agno, &r);
  419. irbp->ir_startino = r.ir_startino;
  420. irbp->ir_freecount = r.ir_freecount;
  421. irbp->ir_free = r.ir_free;
  422. irbp++;
  423. icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
  424. }
  425. /*
  426. * Set agino to after this chunk and bump the cursor.
  427. */
  428. agino = r.ir_startino + XFS_INODES_PER_CHUNK;
  429. error = xfs_btree_increment(cur, 0, &tmp);
  430. cond_resched();
  431. }
  432. /*
  433. * Drop the btree buffers and the agi buffer.
  434. * We can't hold any of the locks these represent
  435. * when calling iget.
  436. */
  437. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  438. xfs_buf_relse(agbp);
  439. /*
  440. * Now format all the good inodes into the user's buffer.
  441. */
  442. irbufend = irbp;
  443. for (irbp = irbuf;
  444. irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
  445. struct xfs_bulkstat_agichunk ac;
  446. ac.ac_lastino = lastino;
  447. ac.ac_ubuffer = &ubuffer;
  448. ac.ac_ubleft = ubleft;
  449. ac.ac_ubelem = ubelem;
  450. error = xfs_bulkstat_ag_ichunk(mp, agno, irbp,
  451. formatter, statstruct_size, &ac);
  452. if (error)
  453. rval = error;
  454. lastino = ac.ac_lastino;
  455. ubleft = ac.ac_ubleft;
  456. ubelem = ac.ac_ubelem;
  457. cond_resched();
  458. }
  459. /*
  460. * Set up for the next loop iteration.
  461. */
  462. if (XFS_BULKSTAT_UBLEFT(ubleft)) {
  463. if (end_of_ag) {
  464. agno++;
  465. agino = 0;
  466. } else
  467. agino = XFS_INO_TO_AGINO(mp, lastino);
  468. } else
  469. break;
  470. }
  471. /*
  472. * Done, we're either out of filesystem or space to put the data.
  473. */
  474. kmem_free(irbuf);
  475. *ubcountp = ubelem;
  476. /*
  477. * Found some inodes, return them now and return the error next time.
  478. */
  479. if (ubelem)
  480. rval = 0;
  481. if (agno >= mp->m_sb.sb_agcount) {
  482. /*
  483. * If we ran out of filesystem, mark lastino as off
  484. * the end of the filesystem, so the next call
  485. * will return immediately.
  486. */
  487. *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
  488. *done = 1;
  489. } else
  490. *lastinop = (xfs_ino_t)lastino;
  491. return rval;
  492. }
  493. int
  494. xfs_inumbers_fmt(
  495. void __user *ubuffer, /* buffer to write to */
  496. const struct xfs_inogrp *buffer, /* buffer to read from */
  497. long count, /* # of elements to read */
  498. long *written) /* # of bytes written */
  499. {
  500. if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
  501. return -EFAULT;
  502. *written = count * sizeof(*buffer);
  503. return 0;
  504. }
  505. /*
  506. * Return inode number table for the filesystem.
  507. */
  508. int /* error status */
  509. xfs_inumbers(
  510. struct xfs_mount *mp,/* mount point for filesystem */
  511. xfs_ino_t *lastino,/* last inode returned */
  512. int *count,/* size of buffer/count returned */
  513. void __user *ubuffer,/* buffer with inode descriptions */
  514. inumbers_fmt_pf formatter)
  515. {
  516. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, *lastino);
  517. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, *lastino);
  518. struct xfs_btree_cur *cur = NULL;
  519. struct xfs_buf *agbp = NULL;
  520. struct xfs_inogrp *buffer;
  521. int bcount;
  522. int left = *count;
  523. int bufidx = 0;
  524. int error = 0;
  525. *count = 0;
  526. if (agno >= mp->m_sb.sb_agcount ||
  527. *lastino != XFS_AGINO_TO_INO(mp, agno, agino))
  528. return error;
  529. bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
  530. buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
  531. do {
  532. struct xfs_inobt_rec_incore r;
  533. int stat;
  534. if (!agbp) {
  535. error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
  536. if (error)
  537. break;
  538. cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno,
  539. XFS_BTNUM_INO);
  540. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
  541. &stat);
  542. if (error)
  543. break;
  544. if (!stat)
  545. goto next_ag;
  546. }
  547. error = xfs_inobt_get_rec(cur, &r, &stat);
  548. if (error)
  549. break;
  550. if (!stat)
  551. goto next_ag;
  552. agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
  553. buffer[bufidx].xi_startino =
  554. XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
  555. buffer[bufidx].xi_alloccount =
  556. XFS_INODES_PER_CHUNK - r.ir_freecount;
  557. buffer[bufidx].xi_allocmask = ~r.ir_free;
  558. if (++bufidx == bcount) {
  559. long written;
  560. error = formatter(ubuffer, buffer, bufidx, &written);
  561. if (error)
  562. break;
  563. ubuffer += written;
  564. *count += bufidx;
  565. bufidx = 0;
  566. }
  567. if (!--left)
  568. break;
  569. error = xfs_btree_increment(cur, 0, &stat);
  570. if (error)
  571. break;
  572. if (stat)
  573. continue;
  574. next_ag:
  575. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  576. cur = NULL;
  577. xfs_buf_relse(agbp);
  578. agbp = NULL;
  579. agino = 0;
  580. agno++;
  581. } while (agno < mp->m_sb.sb_agcount);
  582. if (!error) {
  583. if (bufidx) {
  584. long written;
  585. error = formatter(ubuffer, buffer, bufidx, &written);
  586. if (!error)
  587. *count += bufidx;
  588. }
  589. *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
  590. }
  591. kmem_free(buffer);
  592. if (cur)
  593. xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
  594. XFS_BTREE_NOERROR));
  595. if (agbp)
  596. xfs_buf_relse(agbp);
  597. return error;
  598. }