xfs_itable.c 20 KB

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