xfs_itable.c 13 KB

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