xfs_itable.c 26 KB

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