xfs_itable.c 26 KB

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