xfs_itable.c 24 KB

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