xfs_itable.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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_bulkstat_one_iget(
  43. xfs_mount_t *mp, /* mount point for filesystem */
  44. xfs_ino_t ino, /* inode number to get data for */
  45. xfs_daddr_t bno, /* starting bno of inode cluster */
  46. xfs_bstat_t *buf, /* return buffer */
  47. int *stat) /* BULKSTAT_RV_... */
  48. {
  49. xfs_dinode_core_t *dic; /* dinode core info pointer */
  50. xfs_inode_t *ip; /* incore inode pointer */
  51. bhv_vnode_t *vp;
  52. int error;
  53. error = xfs_iget(mp, NULL, ino,
  54. XFS_IGET_BULKSTAT, XFS_ILOCK_SHARED, &ip, bno);
  55. if (error) {
  56. *stat = BULKSTAT_RV_NOTHING;
  57. return error;
  58. }
  59. ASSERT(ip != NULL);
  60. ASSERT(ip->i_blkno != (xfs_daddr_t)0);
  61. if (ip->i_d.di_mode == 0) {
  62. *stat = BULKSTAT_RV_NOTHING;
  63. error = XFS_ERROR(ENOENT);
  64. goto out_iput;
  65. }
  66. vp = XFS_ITOV(ip);
  67. dic = &ip->i_d;
  68. /* xfs_iget returns the following without needing
  69. * further change.
  70. */
  71. buf->bs_nlink = dic->di_nlink;
  72. buf->bs_projid = dic->di_projid;
  73. buf->bs_ino = ino;
  74. buf->bs_mode = dic->di_mode;
  75. buf->bs_uid = dic->di_uid;
  76. buf->bs_gid = dic->di_gid;
  77. buf->bs_size = dic->di_size;
  78. vn_atime_to_bstime(vp, &buf->bs_atime);
  79. buf->bs_mtime.tv_sec = dic->di_mtime.t_sec;
  80. buf->bs_mtime.tv_nsec = dic->di_mtime.t_nsec;
  81. buf->bs_ctime.tv_sec = dic->di_ctime.t_sec;
  82. buf->bs_ctime.tv_nsec = dic->di_ctime.t_nsec;
  83. buf->bs_xflags = xfs_ip2xflags(ip);
  84. buf->bs_extsize = dic->di_extsize << mp->m_sb.sb_blocklog;
  85. buf->bs_extents = dic->di_nextents;
  86. buf->bs_gen = dic->di_gen;
  87. memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
  88. buf->bs_dmevmask = dic->di_dmevmask;
  89. buf->bs_dmstate = dic->di_dmstate;
  90. buf->bs_aextents = dic->di_anextents;
  91. switch (dic->di_format) {
  92. case XFS_DINODE_FMT_DEV:
  93. buf->bs_rdev = ip->i_df.if_u2.if_rdev;
  94. buf->bs_blksize = BLKDEV_IOSIZE;
  95. buf->bs_blocks = 0;
  96. break;
  97. case XFS_DINODE_FMT_LOCAL:
  98. case XFS_DINODE_FMT_UUID:
  99. buf->bs_rdev = 0;
  100. buf->bs_blksize = mp->m_sb.sb_blocksize;
  101. buf->bs_blocks = 0;
  102. break;
  103. case XFS_DINODE_FMT_EXTENTS:
  104. case XFS_DINODE_FMT_BTREE:
  105. buf->bs_rdev = 0;
  106. buf->bs_blksize = mp->m_sb.sb_blocksize;
  107. buf->bs_blocks = dic->di_nblocks + ip->i_delayed_blks;
  108. break;
  109. }
  110. out_iput:
  111. xfs_iput(ip, XFS_ILOCK_SHARED);
  112. return error;
  113. }
  114. STATIC int
  115. xfs_bulkstat_one_dinode(
  116. xfs_mount_t *mp, /* mount point for filesystem */
  117. xfs_ino_t ino, /* inode number to get data for */
  118. xfs_dinode_t *dip, /* dinode inode pointer */
  119. xfs_bstat_t *buf) /* return buffer */
  120. {
  121. xfs_dinode_core_t *dic; /* dinode core info pointer */
  122. dic = &dip->di_core;
  123. /*
  124. * The inode format changed when we moved the link count and
  125. * made it 32 bits long. If this is an old format inode,
  126. * convert it in memory to look like a new one. If it gets
  127. * flushed to disk we will convert back before flushing or
  128. * logging it. We zero out the new projid field and the old link
  129. * count field. We'll handle clearing the pad field (the remains
  130. * of the old uuid field) when we actually convert the inode to
  131. * the new format. We don't change the version number so that we
  132. * can distinguish this from a real new format inode.
  133. */
  134. if (INT_GET(dic->di_version, ARCH_CONVERT) == XFS_DINODE_VERSION_1) {
  135. buf->bs_nlink = INT_GET(dic->di_onlink, ARCH_CONVERT);
  136. buf->bs_projid = 0;
  137. } else {
  138. buf->bs_nlink = INT_GET(dic->di_nlink, ARCH_CONVERT);
  139. buf->bs_projid = INT_GET(dic->di_projid, ARCH_CONVERT);
  140. }
  141. buf->bs_ino = ino;
  142. buf->bs_mode = INT_GET(dic->di_mode, ARCH_CONVERT);
  143. buf->bs_uid = INT_GET(dic->di_uid, ARCH_CONVERT);
  144. buf->bs_gid = INT_GET(dic->di_gid, ARCH_CONVERT);
  145. buf->bs_size = INT_GET(dic->di_size, ARCH_CONVERT);
  146. buf->bs_atime.tv_sec = INT_GET(dic->di_atime.t_sec, ARCH_CONVERT);
  147. buf->bs_atime.tv_nsec = INT_GET(dic->di_atime.t_nsec, ARCH_CONVERT);
  148. buf->bs_mtime.tv_sec = INT_GET(dic->di_mtime.t_sec, ARCH_CONVERT);
  149. buf->bs_mtime.tv_nsec = INT_GET(dic->di_mtime.t_nsec, ARCH_CONVERT);
  150. buf->bs_ctime.tv_sec = INT_GET(dic->di_ctime.t_sec, ARCH_CONVERT);
  151. buf->bs_ctime.tv_nsec = INT_GET(dic->di_ctime.t_nsec, ARCH_CONVERT);
  152. buf->bs_xflags = xfs_dic2xflags(dic);
  153. buf->bs_extsize = INT_GET(dic->di_extsize, ARCH_CONVERT) << mp->m_sb.sb_blocklog;
  154. buf->bs_extents = INT_GET(dic->di_nextents, ARCH_CONVERT);
  155. buf->bs_gen = INT_GET(dic->di_gen, ARCH_CONVERT);
  156. memset(buf->bs_pad, 0, sizeof(buf->bs_pad));
  157. buf->bs_dmevmask = INT_GET(dic->di_dmevmask, ARCH_CONVERT);
  158. buf->bs_dmstate = INT_GET(dic->di_dmstate, ARCH_CONVERT);
  159. buf->bs_aextents = INT_GET(dic->di_anextents, ARCH_CONVERT);
  160. switch (INT_GET(dic->di_format, ARCH_CONVERT)) {
  161. case XFS_DINODE_FMT_DEV:
  162. buf->bs_rdev = INT_GET(dip->di_u.di_dev, ARCH_CONVERT);
  163. buf->bs_blksize = BLKDEV_IOSIZE;
  164. buf->bs_blocks = 0;
  165. break;
  166. case XFS_DINODE_FMT_LOCAL:
  167. case XFS_DINODE_FMT_UUID:
  168. buf->bs_rdev = 0;
  169. buf->bs_blksize = mp->m_sb.sb_blocksize;
  170. buf->bs_blocks = 0;
  171. break;
  172. case XFS_DINODE_FMT_EXTENTS:
  173. case XFS_DINODE_FMT_BTREE:
  174. buf->bs_rdev = 0;
  175. buf->bs_blksize = mp->m_sb.sb_blocksize;
  176. buf->bs_blocks = INT_GET(dic->di_nblocks, ARCH_CONVERT);
  177. break;
  178. }
  179. return 0;
  180. }
  181. /*
  182. * Return stat information for one inode.
  183. * Return 0 if ok, else errno.
  184. */
  185. int /* error status */
  186. xfs_bulkstat_one(
  187. xfs_mount_t *mp, /* mount point for filesystem */
  188. xfs_ino_t ino, /* inode number to get data for */
  189. void __user *buffer, /* buffer to place output in */
  190. int ubsize, /* size of buffer */
  191. void *private_data, /* my private data */
  192. xfs_daddr_t bno, /* starting bno of inode cluster */
  193. int *ubused, /* bytes used by me */
  194. void *dibuff, /* on-disk inode buffer */
  195. int *stat) /* BULKSTAT_RV_... */
  196. {
  197. xfs_bstat_t *buf; /* return buffer */
  198. int error = 0; /* error value */
  199. xfs_dinode_t *dip; /* dinode inode pointer */
  200. dip = (xfs_dinode_t *)dibuff;
  201. if (!buffer || ino == mp->m_sb.sb_rbmino || ino == mp->m_sb.sb_rsumino ||
  202. (XFS_SB_VERSION_HASQUOTA(&mp->m_sb) &&
  203. (ino == mp->m_sb.sb_uquotino || ino == mp->m_sb.sb_gquotino))) {
  204. *stat = BULKSTAT_RV_NOTHING;
  205. return XFS_ERROR(EINVAL);
  206. }
  207. if (ubsize < sizeof(*buf)) {
  208. *stat = BULKSTAT_RV_NOTHING;
  209. return XFS_ERROR(ENOMEM);
  210. }
  211. buf = kmem_alloc(sizeof(*buf), KM_SLEEP);
  212. if (dip == NULL) {
  213. /* We're not being passed a pointer to a dinode. This happens
  214. * if BULKSTAT_FG_IGET is selected. Do the iget.
  215. */
  216. error = xfs_bulkstat_one_iget(mp, ino, bno, buf, stat);
  217. if (error)
  218. goto out_free;
  219. } else {
  220. xfs_bulkstat_one_dinode(mp, ino, dip, buf);
  221. }
  222. if (copy_to_user(buffer, buf, sizeof(*buf))) {
  223. *stat = BULKSTAT_RV_NOTHING;
  224. error = EFAULT;
  225. goto out_free;
  226. }
  227. *stat = BULKSTAT_RV_DIDONE;
  228. if (ubused)
  229. *ubused = sizeof(*buf);
  230. out_free:
  231. kmem_free(buf, sizeof(*buf));
  232. return error;
  233. }
  234. /*
  235. * Test to see whether we can use the ondisk inode directly, based
  236. * on the given bulkstat flags, filling in dipp accordingly.
  237. * Returns zero if the inode is dodgey.
  238. */
  239. STATIC int
  240. xfs_bulkstat_use_dinode(
  241. xfs_mount_t *mp,
  242. int flags,
  243. xfs_buf_t *bp,
  244. int clustidx,
  245. xfs_dinode_t **dipp)
  246. {
  247. xfs_dinode_t *dip;
  248. unsigned int aformat;
  249. *dipp = NULL;
  250. if (!bp || (flags & BULKSTAT_FG_IGET))
  251. return 1;
  252. dip = (xfs_dinode_t *)
  253. xfs_buf_offset(bp, clustidx << mp->m_sb.sb_inodelog);
  254. if (INT_GET(dip->di_core.di_magic, ARCH_CONVERT) != XFS_DINODE_MAGIC ||
  255. !XFS_DINODE_GOOD_VERSION(
  256. INT_GET(dip->di_core.di_version, ARCH_CONVERT)))
  257. return 0;
  258. if (flags & BULKSTAT_FG_QUICK) {
  259. *dipp = dip;
  260. return 1;
  261. }
  262. /* BULKSTAT_FG_INLINE: if attr fork is local, or not there, use it */
  263. aformat = INT_GET(dip->di_core.di_aformat, ARCH_CONVERT);
  264. if ((XFS_CFORK_Q(&dip->di_core) == 0) ||
  265. (aformat == XFS_DINODE_FMT_LOCAL) ||
  266. (aformat == XFS_DINODE_FMT_EXTENTS && !dip->di_core.di_anextents)) {
  267. *dipp = dip;
  268. return 1;
  269. }
  270. return 1;
  271. }
  272. /*
  273. * Return stat information in bulk (by-inode) for the filesystem.
  274. */
  275. int /* error status */
  276. xfs_bulkstat(
  277. xfs_mount_t *mp, /* mount point for filesystem */
  278. xfs_ino_t *lastinop, /* last inode returned */
  279. int *ubcountp, /* size of buffer/count returned */
  280. bulkstat_one_pf formatter, /* func that'd fill a single buf */
  281. void *private_data,/* private data for formatter */
  282. size_t statstruct_size, /* sizeof struct filling */
  283. char __user *ubuffer, /* buffer with inode stats */
  284. int flags, /* defined in xfs_itable.h */
  285. int *done) /* 1 if there are more stats to get */
  286. {
  287. xfs_agblock_t agbno=0;/* allocation group block number */
  288. xfs_buf_t *agbp; /* agi header buffer */
  289. xfs_agi_t *agi; /* agi header data */
  290. xfs_agino_t agino; /* inode # in allocation group */
  291. xfs_agnumber_t agno; /* allocation group number */
  292. xfs_daddr_t bno; /* inode cluster start daddr */
  293. int chunkidx; /* current index into inode chunk */
  294. int clustidx; /* current index into inode cluster */
  295. xfs_btree_cur_t *cur; /* btree cursor for ialloc btree */
  296. int end_of_ag; /* set if we've seen the ag end */
  297. int error; /* error code */
  298. int fmterror;/* bulkstat formatter result */
  299. __int32_t gcnt; /* current btree rec's count */
  300. xfs_inofree_t gfree; /* current btree rec's free mask */
  301. xfs_agino_t gino; /* current btree rec's start inode */
  302. int i; /* loop index */
  303. int icount; /* count of inodes good in irbuf */
  304. int irbsize; /* size of irec buffer in bytes */
  305. unsigned int kmflags; /* flags for allocating irec buffer */
  306. xfs_ino_t ino; /* inode number (filesystem) */
  307. xfs_inobt_rec_incore_t *irbp; /* current irec buffer pointer */
  308. xfs_inobt_rec_incore_t *irbuf; /* start of irec buffer */
  309. xfs_inobt_rec_incore_t *irbufend; /* end of good irec buffer entries */
  310. xfs_ino_t lastino=0; /* last inode number returned */
  311. int nbcluster; /* # of blocks in a cluster */
  312. int nicluster; /* # of inodes in a cluster */
  313. int nimask; /* mask for inode clusters */
  314. int nirbuf; /* size of irbuf */
  315. int rval; /* return value error code */
  316. int tmp; /* result value from btree calls */
  317. int ubcount; /* size of user's buffer */
  318. int ubleft; /* bytes left in user's buffer */
  319. char __user *ubufp; /* pointer into user's buffer */
  320. int ubelem; /* spaces used in user's buffer */
  321. int ubused; /* bytes used by formatter */
  322. xfs_buf_t *bp; /* ptr to on-disk inode cluster buf */
  323. xfs_dinode_t *dip; /* ptr into bp for specific inode */
  324. xfs_inode_t *ip; /* ptr to in-core inode struct */
  325. /*
  326. * Get the last inode value, see if there's nothing to do.
  327. */
  328. ino = (xfs_ino_t)*lastinop;
  329. dip = NULL;
  330. agno = XFS_INO_TO_AGNO(mp, ino);
  331. agino = XFS_INO_TO_AGINO(mp, ino);
  332. if (agno >= mp->m_sb.sb_agcount ||
  333. ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
  334. *done = 1;
  335. *ubcountp = 0;
  336. return 0;
  337. }
  338. ubcount = *ubcountp; /* statstruct's */
  339. ubleft = ubcount * statstruct_size; /* bytes */
  340. *ubcountp = ubelem = 0;
  341. *done = 0;
  342. fmterror = 0;
  343. ubufp = ubuffer;
  344. nicluster = mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp) ?
  345. mp->m_sb.sb_inopblock :
  346. (XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog);
  347. nimask = ~(nicluster - 1);
  348. nbcluster = nicluster >> mp->m_sb.sb_inopblog;
  349. /*
  350. * Allocate a local buffer for inode cluster btree records.
  351. * This caps our maximum readahead window (so don't be stingy)
  352. * but we must handle the case where we can't get a contiguous
  353. * multi-page buffer, so we drop back toward pagesize; the end
  354. * case we ensure succeeds, via appropriate allocation flags.
  355. */
  356. irbsize = NBPP * 4;
  357. kmflags = KM_SLEEP | KM_MAYFAIL;
  358. while (!(irbuf = kmem_alloc(irbsize, kmflags))) {
  359. if ((irbsize >>= 1) <= NBPP)
  360. kmflags = KM_SLEEP;
  361. }
  362. nirbuf = irbsize / sizeof(*irbuf);
  363. /*
  364. * Loop over the allocation groups, starting from the last
  365. * inode returned; 0 means start of the allocation group.
  366. */
  367. rval = 0;
  368. while (ubleft >= statstruct_size && agno < mp->m_sb.sb_agcount) {
  369. bp = NULL;
  370. down_read(&mp->m_peraglock);
  371. error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
  372. up_read(&mp->m_peraglock);
  373. if (error) {
  374. /*
  375. * Skip this allocation group and go to the next one.
  376. */
  377. agno++;
  378. agino = 0;
  379. continue;
  380. }
  381. agi = XFS_BUF_TO_AGI(agbp);
  382. /*
  383. * Allocate and initialize a btree cursor for ialloc btree.
  384. */
  385. cur = xfs_btree_init_cursor(mp, NULL, agbp, agno, XFS_BTNUM_INO,
  386. (xfs_inode_t *)0, 0);
  387. irbp = irbuf;
  388. irbufend = irbuf + nirbuf;
  389. end_of_ag = 0;
  390. /*
  391. * If we're returning in the middle of an allocation group,
  392. * we need to get the remainder of the chunk we're in.
  393. */
  394. if (agino > 0) {
  395. /*
  396. * Lookup the inode chunk that this inode lives in.
  397. */
  398. error = xfs_inobt_lookup_le(cur, agino, 0, 0, &tmp);
  399. if (!error && /* no I/O error */
  400. tmp && /* lookup succeeded */
  401. /* got the record, should always work */
  402. !(error = xfs_inobt_get_rec(cur, &gino, &gcnt,
  403. &gfree, &i)) &&
  404. i == 1 &&
  405. /* this is the right chunk */
  406. agino < gino + XFS_INODES_PER_CHUNK &&
  407. /* lastino was not last in chunk */
  408. (chunkidx = agino - gino + 1) <
  409. XFS_INODES_PER_CHUNK &&
  410. /* there are some left allocated */
  411. XFS_INOBT_MASKN(chunkidx,
  412. XFS_INODES_PER_CHUNK - chunkidx) & ~gfree) {
  413. /*
  414. * Grab the chunk record. Mark all the
  415. * uninteresting inodes (because they're
  416. * before our start point) free.
  417. */
  418. for (i = 0; i < chunkidx; i++) {
  419. if (XFS_INOBT_MASK(i) & ~gfree)
  420. gcnt++;
  421. }
  422. gfree |= XFS_INOBT_MASKN(0, chunkidx);
  423. irbp->ir_startino = gino;
  424. irbp->ir_freecount = gcnt;
  425. irbp->ir_free = gfree;
  426. irbp++;
  427. agino = gino + XFS_INODES_PER_CHUNK;
  428. icount = XFS_INODES_PER_CHUNK - gcnt;
  429. } else {
  430. /*
  431. * If any of those tests failed, bump the
  432. * inode number (just in case).
  433. */
  434. agino++;
  435. icount = 0;
  436. }
  437. /*
  438. * In any case, increment to the next record.
  439. */
  440. if (!error)
  441. error = xfs_inobt_increment(cur, 0, &tmp);
  442. } else {
  443. /*
  444. * Start of ag. Lookup the first inode chunk.
  445. */
  446. error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &tmp);
  447. icount = 0;
  448. }
  449. /*
  450. * Loop through inode btree records in this ag,
  451. * until we run out of inodes or space in the buffer.
  452. */
  453. while (irbp < irbufend && icount < ubcount) {
  454. /*
  455. * Loop as long as we're unable to read the
  456. * inode btree.
  457. */
  458. while (error) {
  459. agino += XFS_INODES_PER_CHUNK;
  460. if (XFS_AGINO_TO_AGBNO(mp, agino) >=
  461. be32_to_cpu(agi->agi_length))
  462. break;
  463. error = xfs_inobt_lookup_ge(cur, agino, 0, 0,
  464. &tmp);
  465. }
  466. /*
  467. * If ran off the end of the ag either with an error,
  468. * or the normal way, set end and stop collecting.
  469. */
  470. if (error ||
  471. (error = xfs_inobt_get_rec(cur, &gino, &gcnt,
  472. &gfree, &i)) ||
  473. i == 0) {
  474. end_of_ag = 1;
  475. break;
  476. }
  477. /*
  478. * If this chunk has any allocated inodes, save it.
  479. * Also start read-ahead now for this chunk.
  480. */
  481. if (gcnt < XFS_INODES_PER_CHUNK) {
  482. /*
  483. * Loop over all clusters in the next chunk.
  484. * Do a readahead if there are any allocated
  485. * inodes in that cluster.
  486. */
  487. for (agbno = XFS_AGINO_TO_AGBNO(mp, gino),
  488. chunkidx = 0;
  489. chunkidx < XFS_INODES_PER_CHUNK;
  490. chunkidx += nicluster,
  491. agbno += nbcluster) {
  492. if (XFS_INOBT_MASKN(chunkidx,
  493. nicluster) & ~gfree)
  494. xfs_btree_reada_bufs(mp, agno,
  495. agbno, nbcluster);
  496. }
  497. irbp->ir_startino = gino;
  498. irbp->ir_freecount = gcnt;
  499. irbp->ir_free = gfree;
  500. irbp++;
  501. icount += XFS_INODES_PER_CHUNK - gcnt;
  502. }
  503. /*
  504. * Set agino to after this chunk and bump the cursor.
  505. */
  506. agino = gino + XFS_INODES_PER_CHUNK;
  507. error = xfs_inobt_increment(cur, 0, &tmp);
  508. }
  509. /*
  510. * Drop the btree buffers and the agi buffer.
  511. * We can't hold any of the locks these represent
  512. * when calling iget.
  513. */
  514. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  515. xfs_buf_relse(agbp);
  516. /*
  517. * Now format all the good inodes into the user's buffer.
  518. */
  519. irbufend = irbp;
  520. for (irbp = irbuf;
  521. irbp < irbufend && ubleft >= statstruct_size; irbp++) {
  522. /*
  523. * Now process this chunk of inodes.
  524. */
  525. for (agino = irbp->ir_startino, chunkidx = clustidx = 0;
  526. ubleft > 0 &&
  527. irbp->ir_freecount < XFS_INODES_PER_CHUNK;
  528. chunkidx++, clustidx++, agino++) {
  529. ASSERT(chunkidx < XFS_INODES_PER_CHUNK);
  530. /*
  531. * Recompute agbno if this is the
  532. * first inode of the cluster.
  533. *
  534. * Careful with clustidx. There can be
  535. * multple clusters per chunk, a single
  536. * cluster per chunk or a cluster that has
  537. * inodes represented from several different
  538. * chunks (if blocksize is large).
  539. *
  540. * Because of this, the starting clustidx is
  541. * initialized to zero in this loop but must
  542. * later be reset after reading in the cluster
  543. * buffer.
  544. */
  545. if ((chunkidx & (nicluster - 1)) == 0) {
  546. agbno = XFS_AGINO_TO_AGBNO(mp,
  547. irbp->ir_startino) +
  548. ((chunkidx & nimask) >>
  549. mp->m_sb.sb_inopblog);
  550. if (flags & (BULKSTAT_FG_QUICK |
  551. BULKSTAT_FG_INLINE)) {
  552. ino = XFS_AGINO_TO_INO(mp, agno,
  553. agino);
  554. bno = XFS_AGB_TO_DADDR(mp, agno,
  555. agbno);
  556. /*
  557. * Get the inode cluster buffer
  558. */
  559. ASSERT(xfs_inode_zone != NULL);
  560. ip = kmem_zone_zalloc(xfs_inode_zone,
  561. KM_SLEEP);
  562. ip->i_ino = ino;
  563. ip->i_mount = mp;
  564. if (bp)
  565. xfs_buf_relse(bp);
  566. error = xfs_itobp(mp, NULL, ip,
  567. &dip, &bp, bno,
  568. XFS_IMAP_BULKSTAT);
  569. if (!error)
  570. clustidx = ip->i_boffset / mp->m_sb.sb_inodesize;
  571. kmem_zone_free(xfs_inode_zone, ip);
  572. if (XFS_TEST_ERROR(error != 0,
  573. mp, XFS_ERRTAG_BULKSTAT_READ_CHUNK,
  574. XFS_RANDOM_BULKSTAT_READ_CHUNK)) {
  575. bp = NULL;
  576. ubleft = 0;
  577. rval = error;
  578. break;
  579. }
  580. }
  581. }
  582. /*
  583. * Skip if this inode is free.
  584. */
  585. if (XFS_INOBT_MASK(chunkidx) & irbp->ir_free)
  586. continue;
  587. /*
  588. * Count used inodes as free so we can tell
  589. * when the chunk is used up.
  590. */
  591. irbp->ir_freecount++;
  592. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  593. bno = XFS_AGB_TO_DADDR(mp, agno, agbno);
  594. if (!xfs_bulkstat_use_dinode(mp, flags, bp,
  595. clustidx, &dip))
  596. continue;
  597. /*
  598. * If we need to do an iget, cannot hold bp.
  599. * Drop it, until starting the next cluster.
  600. */
  601. if ((flags & BULKSTAT_FG_INLINE) && !dip) {
  602. if (bp)
  603. xfs_buf_relse(bp);
  604. bp = NULL;
  605. }
  606. /*
  607. * Get the inode and fill in a single buffer.
  608. * BULKSTAT_FG_QUICK uses dip to fill it in.
  609. * BULKSTAT_FG_IGET uses igets.
  610. * BULKSTAT_FG_INLINE uses dip if we have an
  611. * inline attr fork, else igets.
  612. * See: xfs_bulkstat_one & xfs_dm_bulkstat_one.
  613. * This is also used to count inodes/blks, etc
  614. * in xfs_qm_quotacheck.
  615. */
  616. ubused = statstruct_size;
  617. error = formatter(mp, ino, ubufp,
  618. ubleft, private_data,
  619. bno, &ubused, dip, &fmterror);
  620. if (fmterror == BULKSTAT_RV_NOTHING) {
  621. if (error == ENOMEM)
  622. ubleft = 0;
  623. continue;
  624. }
  625. if (fmterror == BULKSTAT_RV_GIVEUP) {
  626. ubleft = 0;
  627. ASSERT(error);
  628. rval = error;
  629. break;
  630. }
  631. if (ubufp)
  632. ubufp += ubused;
  633. ubleft -= ubused;
  634. ubelem++;
  635. lastino = ino;
  636. }
  637. }
  638. if (bp)
  639. xfs_buf_relse(bp);
  640. /*
  641. * Set up for the next loop iteration.
  642. */
  643. if (ubleft > 0) {
  644. if (end_of_ag) {
  645. agno++;
  646. agino = 0;
  647. } else
  648. agino = XFS_INO_TO_AGINO(mp, lastino);
  649. } else
  650. break;
  651. }
  652. /*
  653. * Done, we're either out of filesystem or space to put the data.
  654. */
  655. kmem_free(irbuf, irbsize);
  656. *ubcountp = ubelem;
  657. if (agno >= mp->m_sb.sb_agcount) {
  658. /*
  659. * If we ran out of filesystem, mark lastino as off
  660. * the end of the filesystem, so the next call
  661. * will return immediately.
  662. */
  663. *lastinop = (xfs_ino_t)XFS_AGINO_TO_INO(mp, agno, 0);
  664. *done = 1;
  665. } else
  666. *lastinop = (xfs_ino_t)lastino;
  667. return rval;
  668. }
  669. /*
  670. * Return stat information in bulk (by-inode) for the filesystem.
  671. * Special case for non-sequential one inode bulkstat.
  672. */
  673. int /* error status */
  674. xfs_bulkstat_single(
  675. xfs_mount_t *mp, /* mount point for filesystem */
  676. xfs_ino_t *lastinop, /* inode to return */
  677. char __user *buffer, /* buffer with inode stats */
  678. int *done) /* 1 if there are more stats to get */
  679. {
  680. int count; /* count value for bulkstat call */
  681. int error; /* return value */
  682. xfs_ino_t ino; /* filesystem inode number */
  683. int res; /* result from bs1 */
  684. /*
  685. * note that requesting valid inode numbers which are not allocated
  686. * to inodes will most likely cause xfs_itobp to generate warning
  687. * messages about bad magic numbers. This is ok. The fact that
  688. * the inode isn't actually an inode is handled by the
  689. * error check below. Done this way to make the usual case faster
  690. * at the expense of the error case.
  691. */
  692. ino = (xfs_ino_t)*lastinop;
  693. error = xfs_bulkstat_one(mp, ino, buffer, sizeof(xfs_bstat_t),
  694. NULL, 0, NULL, NULL, &res);
  695. if (error) {
  696. /*
  697. * Special case way failed, do it the "long" way
  698. * to see if that works.
  699. */
  700. (*lastinop)--;
  701. count = 1;
  702. if (xfs_bulkstat(mp, lastinop, &count, xfs_bulkstat_one,
  703. NULL, sizeof(xfs_bstat_t), buffer,
  704. BULKSTAT_FG_IGET, done))
  705. return error;
  706. if (count == 0 || (xfs_ino_t)*lastinop != ino)
  707. return error == EFSCORRUPTED ?
  708. XFS_ERROR(EINVAL) : error;
  709. else
  710. return 0;
  711. }
  712. *done = 0;
  713. return 0;
  714. }
  715. /*
  716. * Return inode number table for the filesystem.
  717. */
  718. int /* error status */
  719. xfs_inumbers(
  720. xfs_mount_t *mp, /* mount point for filesystem */
  721. xfs_ino_t *lastino, /* last inode returned */
  722. int *count, /* size of buffer/count returned */
  723. xfs_inogrp_t __user *ubuffer)/* buffer with inode descriptions */
  724. {
  725. xfs_buf_t *agbp;
  726. xfs_agino_t agino;
  727. xfs_agnumber_t agno;
  728. int bcount;
  729. xfs_inogrp_t *buffer;
  730. int bufidx;
  731. xfs_btree_cur_t *cur;
  732. int error;
  733. __int32_t gcnt;
  734. xfs_inofree_t gfree;
  735. xfs_agino_t gino;
  736. int i;
  737. xfs_ino_t ino;
  738. int left;
  739. int tmp;
  740. ino = (xfs_ino_t)*lastino;
  741. agno = XFS_INO_TO_AGNO(mp, ino);
  742. agino = XFS_INO_TO_AGINO(mp, ino);
  743. left = *count;
  744. *count = 0;
  745. bcount = MIN(left, (int)(NBPP / sizeof(*buffer)));
  746. buffer = kmem_alloc(bcount * sizeof(*buffer), KM_SLEEP);
  747. error = bufidx = 0;
  748. cur = NULL;
  749. agbp = NULL;
  750. while (left > 0 && agno < mp->m_sb.sb_agcount) {
  751. if (agbp == NULL) {
  752. down_read(&mp->m_peraglock);
  753. error = xfs_ialloc_read_agi(mp, NULL, agno, &agbp);
  754. up_read(&mp->m_peraglock);
  755. if (error) {
  756. /*
  757. * If we can't read the AGI of this ag,
  758. * then just skip to the next one.
  759. */
  760. ASSERT(cur == NULL);
  761. agbp = NULL;
  762. agno++;
  763. agino = 0;
  764. continue;
  765. }
  766. cur = xfs_btree_init_cursor(mp, NULL, agbp, agno,
  767. XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
  768. error = xfs_inobt_lookup_ge(cur, agino, 0, 0, &tmp);
  769. if (error) {
  770. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  771. cur = NULL;
  772. xfs_buf_relse(agbp);
  773. agbp = NULL;
  774. /*
  775. * Move up the the last inode in the current
  776. * chunk. The lookup_ge will always get
  777. * us the first inode in the next chunk.
  778. */
  779. agino += XFS_INODES_PER_CHUNK - 1;
  780. continue;
  781. }
  782. }
  783. if ((error = xfs_inobt_get_rec(cur, &gino, &gcnt, &gfree,
  784. &i)) ||
  785. i == 0) {
  786. xfs_buf_relse(agbp);
  787. agbp = NULL;
  788. xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
  789. cur = NULL;
  790. agno++;
  791. agino = 0;
  792. continue;
  793. }
  794. agino = gino + XFS_INODES_PER_CHUNK - 1;
  795. buffer[bufidx].xi_startino = XFS_AGINO_TO_INO(mp, agno, gino);
  796. buffer[bufidx].xi_alloccount = XFS_INODES_PER_CHUNK - gcnt;
  797. buffer[bufidx].xi_allocmask = ~gfree;
  798. bufidx++;
  799. left--;
  800. if (bufidx == bcount) {
  801. if (copy_to_user(ubuffer, buffer,
  802. bufidx * sizeof(*buffer))) {
  803. error = XFS_ERROR(EFAULT);
  804. break;
  805. }
  806. ubuffer += bufidx;
  807. *count += bufidx;
  808. bufidx = 0;
  809. }
  810. if (left) {
  811. error = xfs_inobt_increment(cur, 0, &tmp);
  812. if (error) {
  813. xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
  814. cur = NULL;
  815. xfs_buf_relse(agbp);
  816. agbp = NULL;
  817. /*
  818. * The agino value has already been bumped.
  819. * Just try to skip up to it.
  820. */
  821. agino += XFS_INODES_PER_CHUNK;
  822. continue;
  823. }
  824. }
  825. }
  826. if (!error) {
  827. if (bufidx) {
  828. if (copy_to_user(ubuffer, buffer,
  829. bufidx * sizeof(*buffer)))
  830. error = XFS_ERROR(EFAULT);
  831. else
  832. *count += bufidx;
  833. }
  834. *lastino = XFS_AGINO_TO_INO(mp, agno, agino);
  835. }
  836. kmem_free(buffer, bcount * sizeof(*buffer));
  837. if (cur)
  838. xfs_btree_del_cursor(cur, (error ? XFS_BTREE_ERROR :
  839. XFS_BTREE_NOERROR));
  840. if (agbp)
  841. xfs_buf_relse(agbp);
  842. return error;
  843. }