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