xfs_itable.c 25 KB

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