xfs_itable.c 26 KB

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