xfs_itable.c 26 KB

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