xfs_itable.c 24 KB

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