xfs_itable.c 25 KB

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