xfs_itable.c 19 KB

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