xfs_itable.c 17 KB

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