xfs_itable.c 25 KB

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