xfs_itable.c 25 KB

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