xfs_itable.c 20 KB

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