xfs_itable.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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_types.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_alloc_btree.h"
  29. #include "xfs_ialloc_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_ialloc.h"
  33. #include "xfs_itable.h"
  34. #include "xfs_error.h"
  35. #include "xfs_btree.h"
  36. #include "xfs_trace.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. (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino)));
  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 nbcluster; /* # of blocks in a cluster */
  196. int nicluster; /* # of inodes in a cluster */
  197. int nimask; /* mask for inode clusters */
  198. int nirbuf; /* size of irbuf */
  199. int rval; /* return value error code */
  200. int tmp; /* result value from btree calls */
  201. int ubcount; /* size of user's buffer */
  202. int ubleft; /* bytes left in user's buffer */
  203. char __user *ubufp; /* pointer into user's buffer */
  204. int ubelem; /* spaces used in user's buffer */
  205. int ubused; /* bytes used by formatter */
  206. xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
  207. /*
  208. * Get the last inode value, see if there's nothing to do.
  209. */
  210. ino = (xfs_ino_t)*lastinop;
  211. lastino = ino;
  212. agno = XFS_INO_TO_AGNO(mp, ino);
  213. agino = XFS_INO_TO_AGINO(mp, ino);
  214. if (agno >= mp->m_sb.sb_agcount ||
  215. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  216. *done = 1;
  217. *ubcountp = 0;
  218. return 0;
  219. }
  220. if (!ubcountp || *ubcountp <= 0) {
  221. return EINVAL;
  222. }
  223. ubcount = *ubcountp; /* statstruct's */
  224. ubleft = ubcount * statstruct_size; /* bytes */
  225. *ubcountp = ubelem = 0;
  226. *done = 0;
  227. fmterror = 0;
  228. ubufp = ubuffer;
  229. nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
  230. mp->m_sb.sb_inopblock :
  231. (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
  232. nimask = ~(nicluster - 1);
  233. nbcluster = nicluster >> mp->m_sb.sb_inopblog;
  234. irbuf = kmem_zalloc_greedy(&irbsize, PAGE_SIZE, PAGE_SIZE * 4);
  235. if (!irbuf)
  236. return ENOMEM;
  237. nirbuf = irbsize / sizeof(*irbuf);
  238. /*
  239. * Loop over the allocation groups, starting from the last
  240. * inode returned; 0 means start of the allocation group.
  241. */
  242. rval = 0;
  243. while (XFS_BULKSTAT_UBLEFT(ubleft) && agno < mp->m_sb.sb_agcount) {
  244. cond_resched();
  245. bp = NULL;
  246. error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
  247. if (error) {
  248. /*
  249. * Skip this allocation group and go to the next one.
  250. */
  251. agno++;
  252. agino = 0;
  253. continue;
  254. }
  255. agi = XFS_BUF_TO_AGI(agbp);
  256. /*
  257. * Allocate and initialize a btree cursor for ialloc btree.
  258. */
  259. cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
  260. irbp = irbuf;
  261. irbufend = irbuf + nirbuf;
  262. end_of_ag = 0;
  263. /*
  264. * If we're returning in the middle of an allocation group,
  265. * we need to get the remainder of the chunk we're in.
  266. */
  267. if (agino > 0) {
  268. xfs_inobt_rec_incore_t r;
  269. /*
  270. * Lookup the inode chunk that this inode lives in.
  271. */
  272. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE,
  273. &tmp);
  274. if (!error && /* no I/O error */
  275. tmp && /* lookup succeeded */
  276. /* got the record, should always work */
  277. !(error = xfs_inobt_get_rec(cur, &r, &i)) &&
  278. i == 1 &&
  279. /* this is the right chunk */
  280. agino < r.ir_startino + XFS_INODES_PER_CHUNK &&
  281. /* lastino was not last in chunk */
  282. (chunkidx = agino - r.ir_startino + 1) <
  283. XFS_INODES_PER_CHUNK &&
  284. /* there are some left allocated */
  285. xfs_inobt_maskn(chunkidx,
  286. XFS_INODES_PER_CHUNK - chunkidx) &
  287. ~r.ir_free) {
  288. /*
  289. * Grab the chunk record. Mark all the
  290. * uninteresting inodes (because they're
  291. * before our start point) free.
  292. */
  293. for (i = 0; i < chunkidx; i++) {
  294. if (XFS_INOBT_MASK(i) & ~r.ir_free)
  295. r.ir_freecount++;
  296. }
  297. r.ir_free |= xfs_inobt_maskn(0, chunkidx);
  298. irbp->ir_startino = r.ir_startino;
  299. irbp->ir_freecount = r.ir_freecount;
  300. irbp->ir_free = r.ir_free;
  301. irbp++;
  302. agino = r.ir_startino + XFS_INODES_PER_CHUNK;
  303. icount = XFS_INODES_PER_CHUNK - r.ir_freecount;
  304. } else {
  305. /*
  306. * If any of those tests failed, bump the
  307. * inode number (just in case).
  308. */
  309. agino++;
  310. icount = 0;
  311. }
  312. /*
  313. * In any case, increment to the next record.
  314. */
  315. if (!error)
  316. error = xfs_btree_increment(cur, 0, &tmp);
  317. } else {
  318. /*
  319. * Start of ag. Lookup the first inode chunk.
  320. */
  321. error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &tmp);
  322. icount = 0;
  323. }
  324. /*
  325. * Loop through inode btree records in this ag,
  326. * until we run out of inodes or space in the buffer.
  327. */
  328. while (irbp < irbufend && icount < ubcount) {
  329. xfs_inobt_rec_incore_t r;
  330. /*
  331. * Loop as long as we're unable to read the
  332. * inode btree.
  333. */
  334. while (error) {
  335. agino += XFS_INODES_PER_CHUNK;
  336. if (XFS_AGINO_TO_AGBNO(mp, agino) >=
  337. be32_to_cpu(agi->agi_length))
  338. break;
  339. error = xfs_inobt_lookup(cur, agino,
  340. XFS_LOOKUP_GE, &tmp);
  341. cond_resched();
  342. }
  343. /*
  344. * If ran off the end of the ag either with an error,
  345. * or the normal way, set end and stop collecting.
  346. */
  347. if (error) {
  348. end_of_ag = 1;
  349. break;
  350. }
  351. error = xfs_inobt_get_rec(cur, &r, &i);
  352. if (error || i == 0) {
  353. end_of_ag = 1;
  354. break;
  355. }
  356. /*
  357. * If this chunk has any allocated inodes, save it.
  358. * Also start read-ahead now for this chunk.
  359. */
  360. if (r.ir_freecount < XFS_INODES_PER_CHUNK) {
  361. /*
  362. * Loop over all clusters in the next chunk.
  363. * Do a readahead if there are any allocated
  364. * inodes in that cluster.
  365. */
  366. agbno = XFS_AGINO_TO_AGBNO(mp, r.ir_startino);
  367. for (chunkidx = 0;
  368. chunkidx < XFS_INODES_PER_CHUNK;
  369. chunkidx += nicluster,
  370. agbno += nbcluster) {
  371. if (xfs_inobt_maskn(chunkidx, nicluster)
  372. & ~r.ir_free)
  373. xfs_btree_reada_bufs(mp, agno,
  374. agbno, nbcluster);
  375. }
  376. irbp->ir_startino = r.ir_startino;
  377. irbp->ir_freecount = r.ir_freecount;
  378. irbp->ir_free = r.ir_free;
  379. irbp++;
  380. icount += XFS_INODES_PER_CHUNK - r.ir_freecount;
  381. }
  382. /*
  383. * Set agino to after this chunk and bump the cursor.
  384. */
  385. agino = r.ir_startino + XFS_INODES_PER_CHUNK;
  386. error = xfs_btree_increment(cur, 0, &tmp);
  387. cond_resched();
  388. }
  389. /*
  390. * Drop the btree buffers and the agi buffer.
  391. * We can't hold any of the locks these represent
  392. * when calling iget.
  393. */
  394. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  395. xfs_buf_relse(agbp);
  396. /*
  397. * Now format all the good inodes into the user's buffer.
  398. */
  399. irbufend = irbp;
  400. for (irbp = irbuf;
  401. irbp < irbufend && XFS_BULKSTAT_UBLEFT(ubleft); irbp++) {
  402. /*
  403. * Now process this chunk of inodes.
  404. */
  405. for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
  406. XFS_BULKSTAT_UBLEFT(ubleft) &&
  407. irbp->ir_freecount < XFS_INODES_PER_CHUNK;
  408. chunkidx++, clustidx++, agino++) {
  409. ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
  410. /*
  411. * Recompute agbno if this is the
  412. * first inode of the cluster.
  413. *
  414. * Careful with clustidx. There can be
  415. * multiple clusters per chunk, a single
  416. * cluster per chunk or a cluster that has
  417. * inodes represented from several different
  418. * chunks (if blocksize is large).
  419. *
  420. * Because of this, the starting clustidx is
  421. * initialized to zero in this loop but must
  422. * later be reset after reading in the cluster
  423. * buffer.
  424. */
  425. if ((chunkidx & (nicluster - 1)) == 0) {
  426. agbno = XFS_AGINO_TO_AGBNO(mp,
  427. irbp->ir_startino) +
  428. ((chunkidx & nimask) >>
  429. mp->m_sb.sb_inopblog);
  430. }
  431. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  432. /*
  433. * Skip if this inode is free.
  434. */
  435. if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free) {
  436. lastino = ino;
  437. continue;
  438. }
  439. /*
  440. * Count used inodes as free so we can tell
  441. * when the chunk is used up.
  442. */
  443. irbp->ir_freecount++;
  444. /*
  445. * Get the inode and fill in a single buffer.
  446. */
  447. ubused = statstruct_size;
  448. error = formatter(mp, ino, ubufp, ubleft,
  449. &ubused, &fmterror);
  450. if (fmterror == BULKSTAT_RV_NOTHING) {
  451. if (error && error != ENOENT &&
  452. error != EINVAL) {
  453. ubleft = 0;
  454. rval = error;
  455. break;
  456. }
  457. lastino = ino;
  458. continue;
  459. }
  460. if (fmterror == BULKSTAT_RV_GIVEUP) {
  461. ubleft = 0;
  462. ASSERT(error);
  463. rval = error;
  464. break;
  465. }
  466. if (ubufp)
  467. ubufp += ubused;
  468. ubleft -= ubused;
  469. ubelem++;
  470. lastino = ino;
  471. }
  472. cond_resched();
  473. }
  474. if (bp)
  475. xfs_buf_relse(bp);
  476. /*
  477. * Set up for the next loop iteration.
  478. */
  479. if (XFS_BULKSTAT_UBLEFT(ubleft)) {
  480. if (end_of_ag) {
  481. agno++;
  482. agino = 0;
  483. } else
  484. agino = XFS_INO_TO_AGINO(mp, lastino);
  485. } else
  486. break;
  487. }
  488. /*
  489. * Done, we're either out of filesystem or space to put the data.
  490. */
  491. kmem_free_large(irbuf);
  492. *ubcountp = ubelem;
  493. /*
  494. * Found some inodes, return them now and return the error next time.
  495. */
  496. if (ubelem)
  497. rval = 0;
  498. if (agno >= mp->m_sb.sb_agcount) {
  499. /*
  500. * If we ran out of filesystem, mark lastino as off
  501. * the end of the filesystem, so the next call
  502. * will return immediately.
  503. */
  504. *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
  505. *done = 1;
  506. } else
  507. *lastinop = (xfs_ino_t)lastino;
  508. return rval;
  509. }
  510. /*
  511. * Return stat information in bulk (by-inode) for the filesystem.
  512. * Special case for non-sequential one inode bulkstat.
  513. */
  514. int /* error status */
  515. xfs_bulkstat_single(
  516. xfs_mount_t *mp, /* mount point for filesystem */
  517. xfs_ino_t *lastinop, /* inode to return */
  518. char __user *buffer, /* buffer with inode stats */
  519. int *done) /* 1 if there are more stats to get */
  520. {
  521. int count; /* count value for bulkstat call */
  522. int error; /* return value */
  523. xfs_ino_t ino; /* filesystem inode number */
  524. int res; /* result from bs1 */
  525. /*
  526. * note that requesting valid inode numbers which are not allocated
  527. * to inodes will most likely cause xfs_imap_to_bp to generate warning
  528. * messages about bad magic numbers. This is ok. The fact that
  529. * the inode isn't actually an inode is handled by the
  530. * error check below. Done this way to make the usual case faster
  531. * at the expense of the error case.
  532. */
  533. ino = (xfs_ino_t)*lastinop;
  534. error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t), 0, &res);
  535. if (error) {
  536. /*
  537. * Special case way failed, do it the "long" way
  538. * to see if that works.
  539. */
  540. (*lastinop)--;
  541. count = 1;
  542. if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
  543. sizeof(xfs_bstat_t), buffer, done))
  544. return error;
  545. if (count == 0 || (xfs_ino_t)*lastinop != ino)
  546. return error == EFSCORRUPTED ?
  547. XFS_ERROR(EINVAL) : error;
  548. else
  549. return 0;
  550. }
  551. *done = 0;
  552. return 0;
  553. }
  554. int
  555. xfs_inumbers_fmt(
  556. void __user *ubuffer, /* buffer to write to */
  557. const xfs_inogrp_t *buffer, /* buffer to read from */
  558. long count, /* # of elements to read */
  559. long *written) /* # of bytes written */
  560. {
  561. if (copy_to_user(ubuffer, buffer, count * sizeof(*buffer)))
  562. return -EFAULT;
  563. *written = count * sizeof(*buffer);
  564. return 0;
  565. }
  566. /*
  567. * Return inode number table for the filesystem.
  568. */
  569. int /* error status */
  570. xfs_inumbers(
  571. xfs_mount_t *mp, /* mount point for filesystem */
  572. xfs_ino_t *lastino, /* last inode returned */
  573. int *count, /* size of buffer/count returned */
  574. void __user *ubuffer,/* buffer with inode descriptions */
  575. inumbers_fmt_pf formatter)
  576. {
  577. xfs_buf_t *agbp;
  578. xfs_agino_t agino;
  579. xfs_agnumber_t agno;
  580. int bcount;
  581. xfs_inogrp_t *buffer;
  582. int bufidx;
  583. xfs_btree_cur_t *cur;
  584. int error;
  585. xfs_inobt_rec_incore_t r;
  586. int i;
  587. xfs_ino_t ino;
  588. int left;
  589. int tmp;
  590. ino = (xfs_ino_t)*lastino;
  591. agno = XFS_INO_TO_AGNO(mp, ino);
  592. agino = XFS_INO_TO_AGINO(mp, ino);
  593. left = *count;
  594. *count = 0;
  595. bcount = MIN(left, (int)(PAGE_SIZE / sizeof(*buffer)));
  596. buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
  597. error = bufidx = 0;
  598. cur = NULL;
  599. agbp = NULL;
  600. while (left > 0 && agno < mp->m_sb.sb_agcount) {
  601. if (agbp == NULL) {
  602. error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
  603. if (error) {
  604. /*
  605. * If we can't read the AGI of this ag,
  606. * then just skip to the next one.
  607. */
  608. ASSERT(cur == NULL);
  609. agbp = NULL;
  610. agno++;
  611. agino = 0;
  612. continue;
  613. }
  614. cur = xfs_inobt_init_cursor(mp, NULL, agbp, agno);
  615. error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_GE,
  616. &tmp);
  617. if (error) {
  618. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  619. cur = NULL;
  620. xfs_buf_relse(agbp);
  621. agbp = NULL;
  622. /*
  623. * Move up the last inode in the current
  624. * chunk. The lookup_ge will always get
  625. * us the first inode in the next chunk.
  626. */
  627. agino += XFS_INODES_PER_CHUNK - 1;
  628. continue;
  629. }
  630. }
  631. error = xfs_inobt_get_rec(cur, &r, &i);
  632. if (error || i == 0) {
  633. xfs_buf_relse(agbp);
  634. agbp = NULL;
  635. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  636. cur = NULL;
  637. agno++;
  638. agino = 0;
  639. continue;
  640. }
  641. agino = r.ir_startino + XFS_INODES_PER_CHUNK - 1;
  642. buffer[bufidx].xi_startino =
  643. XFS_AGINO_TO_INO(mp, agno, r.ir_startino);
  644. buffer[bufidx].xi_alloccount =
  645. XFS_INODES_PER_CHUNK - r.ir_freecount;
  646. buffer[bufidx].xi_allocmask = ~r.ir_free;
  647. bufidx++;
  648. left--;
  649. if (bufidx == bcount) {
  650. long written;
  651. if (formatter(ubuffer, buffer, bufidx, &written)) {
  652. error = XFS_ERROR(EFAULT);
  653. break;
  654. }
  655. ubuffer += written;
  656. *count += bufidx;
  657. bufidx = 0;
  658. }
  659. if (left) {
  660. error = xfs_btree_increment(cur, 0, &tmp);
  661. if (error) {
  662. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  663. cur = NULL;
  664. xfs_buf_relse(agbp);
  665. agbp = NULL;
  666. /*
  667. * The agino value has already been bumped.
  668. * Just try to skip up to it.
  669. */
  670. agino += XFS_INODES_PER_CHUNK;
  671. continue;
  672. }
  673. }
  674. }
  675. if (!error) {
  676. if (bufidx) {
  677. long written;
  678. if (formatter(ubuffer, buffer, bufidx, &written))
  679. error = XFS_ERROR(EFAULT);
  680. else
  681. *count += bufidx;
  682. }
  683. *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
  684. }
  685. kmem_free(buffer);
  686. if (cur)
  687. xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
  688. XFS_BTREE_NOERROR));
  689. if (agbp)
  690. xfs_buf_relse(agbp);
  691. return error;
  692. }