xfs_itable.c 24 KB

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