xfs_itable.c 26 KB

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