xfs_itable.c 20 KB

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