xfs_itable.c 12 KB

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