xfs_itable.c 20 KB

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