xfs_itable.c 24 KB

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