xfs_itable.c 25 KB

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