xfs_itable.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_btree.h"
  15. #include "xfs_ialloc.h"
  16. #include "xfs_ialloc_btree.h"
  17. #include "xfs_iwalk.h"
  18. #include "xfs_itable.h"
  19. #include "xfs_error.h"
  20. #include "xfs_icache.h"
  21. #include "xfs_health.h"
  22. #include "xfs_trans.h"
  23. /*
  24. * Bulk Stat
  25. * =========
  26. *
  27. * Use the inode walking functions to fill out struct xfs_bulkstat for every
  28. * allocated inode, then pass the stat information to some externally provided
  29. * iteration function.
  30. */
  31. struct xfs_bstat_chunk {
  32. bulkstat_one_fmt_pf formatter;
  33. struct xfs_ibulk *breq;
  34. struct xfs_bulkstat *buf;
  35. };
  36. /*
  37. * Fill out the bulkstat info for a single inode and report it somewhere.
  38. *
  39. * bc->breq->lastino is effectively the inode cursor as we walk through the
  40. * filesystem. Therefore, we update it any time we need to move the cursor
  41. * forward, regardless of whether or not we're sending any bstat information
  42. * back to userspace. If the inode is internal metadata or, has been freed
  43. * out from under us, we just simply keep going.
  44. *
  45. * However, if any other type of error happens we want to stop right where we
  46. * are so that userspace will call back with exact number of the bad inode and
  47. * we can send back an error code.
  48. *
  49. * Note that if the formatter tells us there's no space left in the buffer we
  50. * move the cursor forward and abort the walk.
  51. */
  52. STATIC int
  53. xfs_bulkstat_one_int(
  54. struct xfs_mount *mp,
  55. struct mnt_idmap *idmap,
  56. struct xfs_trans *tp,
  57. xfs_ino_t ino,
  58. struct xfs_bstat_chunk *bc)
  59. {
  60. struct user_namespace *sb_userns = mp->m_super->s_user_ns;
  61. struct xfs_inode *ip; /* incore inode pointer */
  62. struct inode *inode;
  63. struct xfs_bulkstat *buf = bc->buf;
  64. xfs_extnum_t nextents;
  65. int error = -EINVAL;
  66. vfsuid_t vfsuid;
  67. vfsgid_t vfsgid;
  68. if (xfs_internal_inum(mp, ino))
  69. goto out_advance;
  70. error = xfs_iget(mp, tp, ino,
  71. (XFS_IGET_DONTCACHE | XFS_IGET_UNTRUSTED),
  72. XFS_ILOCK_SHARED, &ip);
  73. if (error == -ENOENT || error == -EINVAL)
  74. goto out_advance;
  75. if (error)
  76. goto out;
  77. /* Reload the incore unlinked list to avoid failure in inodegc. */
  78. if (xfs_inode_unlinked_incomplete(ip)) {
  79. error = xfs_inode_reload_unlinked_bucket(tp, ip);
  80. if (error) {
  81. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  82. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  83. xfs_irele(ip);
  84. return error;
  85. }
  86. }
  87. ASSERT(ip != NULL);
  88. ASSERT(ip->i_imap.im_blkno != 0);
  89. inode = VFS_I(ip);
  90. vfsuid = i_uid_into_vfsuid(idmap, inode);
  91. vfsgid = i_gid_into_vfsgid(idmap, inode);
  92. /* If this is a private inode, don't leak its details to userspace. */
  93. if (IS_PRIVATE(inode)) {
  94. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  95. xfs_irele(ip);
  96. error = -EINVAL;
  97. goto out_advance;
  98. }
  99. /* xfs_iget returns the following without needing
  100. * further change.
  101. */
  102. buf->bs_projectid = ip->i_projid;
  103. buf->bs_ino = ino;
  104. buf->bs_uid = from_kuid(sb_userns, vfsuid_into_kuid(vfsuid));
  105. buf->bs_gid = from_kgid(sb_userns, vfsgid_into_kgid(vfsgid));
  106. buf->bs_size = ip->i_disk_size;
  107. buf->bs_nlink = inode->i_nlink;
  108. buf->bs_atime = inode_get_atime_sec(inode);
  109. buf->bs_atime_nsec = inode_get_atime_nsec(inode);
  110. buf->bs_mtime = inode_get_mtime_sec(inode);
  111. buf->bs_mtime_nsec = inode_get_mtime_nsec(inode);
  112. buf->bs_ctime = inode_get_ctime_sec(inode);
  113. buf->bs_ctime_nsec = inode_get_ctime_nsec(inode);
  114. buf->bs_gen = inode->i_generation;
  115. buf->bs_mode = inode->i_mode;
  116. buf->bs_xflags = xfs_ip2xflags(ip);
  117. buf->bs_extsize_blks = ip->i_extsize;
  118. nextents = xfs_ifork_nextents(&ip->i_df);
  119. if (!(bc->breq->flags & XFS_IBULK_NREXT64))
  120. buf->bs_extents = min(nextents, XFS_MAX_EXTCNT_DATA_FORK_SMALL);
  121. else
  122. buf->bs_extents64 = nextents;
  123. xfs_bulkstat_health(ip, buf);
  124. buf->bs_aextents = xfs_ifork_nextents(&ip->i_af);
  125. buf->bs_forkoff = xfs_inode_fork_boff(ip);
  126. buf->bs_version = XFS_BULKSTAT_VERSION_V5;
  127. if (xfs_has_v3inodes(mp)) {
  128. buf->bs_btime = ip->i_crtime.tv_sec;
  129. buf->bs_btime_nsec = ip->i_crtime.tv_nsec;
  130. if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
  131. buf->bs_cowextsize_blks = ip->i_cowextsize;
  132. }
  133. switch (ip->i_df.if_format) {
  134. case XFS_DINODE_FMT_DEV:
  135. buf->bs_rdev = sysv_encode_dev(inode->i_rdev);
  136. buf->bs_blksize = BLKDEV_IOSIZE;
  137. buf->bs_blocks = 0;
  138. break;
  139. case XFS_DINODE_FMT_LOCAL:
  140. buf->bs_rdev = 0;
  141. buf->bs_blksize = mp->m_sb.sb_blocksize;
  142. buf->bs_blocks = 0;
  143. break;
  144. case XFS_DINODE_FMT_EXTENTS:
  145. case XFS_DINODE_FMT_BTREE:
  146. buf->bs_rdev = 0;
  147. buf->bs_blksize = mp->m_sb.sb_blocksize;
  148. buf->bs_blocks = ip->i_nblocks + ip->i_delayed_blks;
  149. break;
  150. }
  151. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  152. xfs_irele(ip);
  153. error = bc->formatter(bc->breq, buf);
  154. if (error == -ECANCELED)
  155. goto out_advance;
  156. if (error)
  157. goto out;
  158. out_advance:
  159. /*
  160. * Advance the cursor to the inode that comes after the one we just
  161. * looked at. We want the caller to move along if the bulkstat
  162. * information was copied successfully; if we tried to grab the inode
  163. * but it's no longer allocated; or if it's internal metadata.
  164. */
  165. bc->breq->startino = ino + 1;
  166. out:
  167. return error;
  168. }
  169. /* Bulkstat a single inode. */
  170. int
  171. xfs_bulkstat_one(
  172. struct xfs_ibulk *breq,
  173. bulkstat_one_fmt_pf formatter)
  174. {
  175. struct xfs_bstat_chunk bc = {
  176. .formatter = formatter,
  177. .breq = breq,
  178. };
  179. struct xfs_trans *tp;
  180. int error;
  181. if (breq->idmap != &nop_mnt_idmap) {
  182. xfs_warn_ratelimited(breq->mp,
  183. "bulkstat not supported inside of idmapped mounts.");
  184. return -EINVAL;
  185. }
  186. ASSERT(breq->icount == 1);
  187. bc.buf = kzalloc(sizeof(struct xfs_bulkstat),
  188. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  189. if (!bc.buf)
  190. return -ENOMEM;
  191. /*
  192. * Grab an empty transaction so that we can use its recursive buffer
  193. * locking abilities to detect cycles in the inobt without deadlocking.
  194. */
  195. error = xfs_trans_alloc_empty(breq->mp, &tp);
  196. if (error)
  197. goto out;
  198. error = xfs_bulkstat_one_int(breq->mp, breq->idmap, tp,
  199. breq->startino, &bc);
  200. xfs_trans_cancel(tp);
  201. out:
  202. kfree(bc.buf);
  203. /*
  204. * If we reported one inode to userspace then we abort because we hit
  205. * the end of the buffer. Don't leak that back to userspace.
  206. */
  207. if (error == -ECANCELED)
  208. error = 0;
  209. return error;
  210. }
  211. static int
  212. xfs_bulkstat_iwalk(
  213. struct xfs_mount *mp,
  214. struct xfs_trans *tp,
  215. xfs_ino_t ino,
  216. void *data)
  217. {
  218. struct xfs_bstat_chunk *bc = data;
  219. int error;
  220. error = xfs_bulkstat_one_int(mp, bc->breq->idmap, tp, ino, data);
  221. /* bulkstat just skips over missing inodes */
  222. if (error == -ENOENT || error == -EINVAL)
  223. return 0;
  224. return error;
  225. }
  226. /*
  227. * Check the incoming lastino parameter.
  228. *
  229. * We allow any inode value that could map to physical space inside the
  230. * filesystem because if there are no inodes there, bulkstat moves on to the
  231. * next chunk. In other words, the magic agino value of zero takes us to the
  232. * first chunk in the AG, and an agino value past the end of the AG takes us to
  233. * the first chunk in the next AG.
  234. *
  235. * Therefore we can end early if the requested inode is beyond the end of the
  236. * filesystem or doesn't map properly.
  237. */
  238. static inline bool
  239. xfs_bulkstat_already_done(
  240. struct xfs_mount *mp,
  241. xfs_ino_t startino)
  242. {
  243. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
  244. xfs_agino_t agino = XFS_INO_TO_AGINO(mp, startino);
  245. return agno >= mp->m_sb.sb_agcount ||
  246. startino != XFS_AGINO_TO_INO(mp, agno, agino);
  247. }
  248. /* Return stat information in bulk (by-inode) for the filesystem. */
  249. int
  250. xfs_bulkstat(
  251. struct xfs_ibulk *breq,
  252. bulkstat_one_fmt_pf formatter)
  253. {
  254. struct xfs_bstat_chunk bc = {
  255. .formatter = formatter,
  256. .breq = breq,
  257. };
  258. struct xfs_trans *tp;
  259. unsigned int iwalk_flags = 0;
  260. int error;
  261. if (breq->idmap != &nop_mnt_idmap) {
  262. xfs_warn_ratelimited(breq->mp,
  263. "bulkstat not supported inside of idmapped mounts.");
  264. return -EINVAL;
  265. }
  266. if (xfs_bulkstat_already_done(breq->mp, breq->startino))
  267. return 0;
  268. bc.buf = kzalloc(sizeof(struct xfs_bulkstat),
  269. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  270. if (!bc.buf)
  271. return -ENOMEM;
  272. /*
  273. * Grab an empty transaction so that we can use its recursive buffer
  274. * locking abilities to detect cycles in the inobt without deadlocking.
  275. */
  276. error = xfs_trans_alloc_empty(breq->mp, &tp);
  277. if (error)
  278. goto out;
  279. if (breq->flags & XFS_IBULK_SAME_AG)
  280. iwalk_flags |= XFS_IWALK_SAME_AG;
  281. error = xfs_iwalk(breq->mp, tp, breq->startino, iwalk_flags,
  282. xfs_bulkstat_iwalk, breq->icount, &bc);
  283. xfs_trans_cancel(tp);
  284. out:
  285. kfree(bc.buf);
  286. /*
  287. * We found some inodes, so clear the error status and return them.
  288. * The lastino pointer will point directly at the inode that triggered
  289. * any error that occurred, so on the next call the error will be
  290. * triggered again and propagated to userspace as there will be no
  291. * formatted inodes in the buffer.
  292. */
  293. if (breq->ocount > 0)
  294. error = 0;
  295. return error;
  296. }
  297. /* Convert bulkstat (v5) to bstat (v1). */
  298. void
  299. xfs_bulkstat_to_bstat(
  300. struct xfs_mount *mp,
  301. struct xfs_bstat *bs1,
  302. const struct xfs_bulkstat *bstat)
  303. {
  304. /* memset is needed here because of padding holes in the structure. */
  305. memset(bs1, 0, sizeof(struct xfs_bstat));
  306. bs1->bs_ino = bstat->bs_ino;
  307. bs1->bs_mode = bstat->bs_mode;
  308. bs1->bs_nlink = bstat->bs_nlink;
  309. bs1->bs_uid = bstat->bs_uid;
  310. bs1->bs_gid = bstat->bs_gid;
  311. bs1->bs_rdev = bstat->bs_rdev;
  312. bs1->bs_blksize = bstat->bs_blksize;
  313. bs1->bs_size = bstat->bs_size;
  314. bs1->bs_atime.tv_sec = bstat->bs_atime;
  315. bs1->bs_mtime.tv_sec = bstat->bs_mtime;
  316. bs1->bs_ctime.tv_sec = bstat->bs_ctime;
  317. bs1->bs_atime.tv_nsec = bstat->bs_atime_nsec;
  318. bs1->bs_mtime.tv_nsec = bstat->bs_mtime_nsec;
  319. bs1->bs_ctime.tv_nsec = bstat->bs_ctime_nsec;
  320. bs1->bs_blocks = bstat->bs_blocks;
  321. bs1->bs_xflags = bstat->bs_xflags;
  322. bs1->bs_extsize = XFS_FSB_TO_B(mp, bstat->bs_extsize_blks);
  323. bs1->bs_extents = bstat->bs_extents;
  324. bs1->bs_gen = bstat->bs_gen;
  325. bs1->bs_projid_lo = bstat->bs_projectid & 0xFFFF;
  326. bs1->bs_forkoff = bstat->bs_forkoff;
  327. bs1->bs_projid_hi = bstat->bs_projectid >> 16;
  328. bs1->bs_sick = bstat->bs_sick;
  329. bs1->bs_checked = bstat->bs_checked;
  330. bs1->bs_cowextsize = XFS_FSB_TO_B(mp, bstat->bs_cowextsize_blks);
  331. bs1->bs_dmevmask = 0;
  332. bs1->bs_dmstate = 0;
  333. bs1->bs_aextents = bstat->bs_aextents;
  334. }
  335. struct xfs_inumbers_chunk {
  336. inumbers_fmt_pf formatter;
  337. struct xfs_ibulk *breq;
  338. };
  339. /*
  340. * INUMBERS
  341. * ========
  342. * This is how we export inode btree records to userspace, so that XFS tools
  343. * can figure out where inodes are allocated.
  344. */
  345. /*
  346. * Format the inode group structure and report it somewhere.
  347. *
  348. * Similar to xfs_bulkstat_one_int, lastino is the inode cursor as we walk
  349. * through the filesystem so we move it forward unless there was a runtime
  350. * error. If the formatter tells us the buffer is now full we also move the
  351. * cursor forward and abort the walk.
  352. */
  353. STATIC int
  354. xfs_inumbers_walk(
  355. struct xfs_mount *mp,
  356. struct xfs_trans *tp,
  357. xfs_agnumber_t agno,
  358. const struct xfs_inobt_rec_incore *irec,
  359. void *data)
  360. {
  361. struct xfs_inumbers inogrp = {
  362. .xi_startino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino),
  363. .xi_alloccount = irec->ir_count - irec->ir_freecount,
  364. .xi_allocmask = ~irec->ir_free,
  365. .xi_version = XFS_INUMBERS_VERSION_V5,
  366. };
  367. struct xfs_inumbers_chunk *ic = data;
  368. int error;
  369. error = ic->formatter(ic->breq, &inogrp);
  370. if (error && error != -ECANCELED)
  371. return error;
  372. ic->breq->startino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino) +
  373. XFS_INODES_PER_CHUNK;
  374. return error;
  375. }
  376. /*
  377. * Return inode number table for the filesystem.
  378. */
  379. int
  380. xfs_inumbers(
  381. struct xfs_ibulk *breq,
  382. inumbers_fmt_pf formatter)
  383. {
  384. struct xfs_inumbers_chunk ic = {
  385. .formatter = formatter,
  386. .breq = breq,
  387. };
  388. struct xfs_trans *tp;
  389. unsigned int iwalk_flags = 0;
  390. int error = 0;
  391. if (xfs_bulkstat_already_done(breq->mp, breq->startino))
  392. return 0;
  393. if (breq->flags & XFS_IBULK_SAME_AG)
  394. iwalk_flags |= XFS_IWALK_SAME_AG;
  395. /*
  396. * Grab an empty transaction so that we can use its recursive buffer
  397. * locking abilities to detect cycles in the inobt without deadlocking.
  398. */
  399. error = xfs_trans_alloc_empty(breq->mp, &tp);
  400. if (error)
  401. goto out;
  402. error = xfs_inobt_walk(breq->mp, tp, breq->startino, iwalk_flags,
  403. xfs_inumbers_walk, breq->icount, &ic);
  404. xfs_trans_cancel(tp);
  405. out:
  406. /*
  407. * We found some inode groups, so clear the error status and return
  408. * them. The lastino pointer will point directly at the inode that
  409. * triggered any error that occurred, so on the next call the error
  410. * will be triggered again and propagated to userspace as there will be
  411. * no formatted inode groups in the buffer.
  412. */
  413. if (breq->ocount > 0)
  414. error = 0;
  415. return error;
  416. }
  417. /* Convert an inumbers (v5) struct to a inogrp (v1) struct. */
  418. void
  419. xfs_inumbers_to_inogrp(
  420. struct xfs_inogrp *ig1,
  421. const struct xfs_inumbers *ig)
  422. {
  423. /* memset is needed here because of padding holes in the structure. */
  424. memset(ig1, 0, sizeof(struct xfs_inogrp));
  425. ig1->xi_startino = ig->xi_startino;
  426. ig1->xi_alloccount = ig->xi_alloccount;
  427. ig1->xi_allocmask = ig->xi_allocmask;
  428. }