xfs_fsmap.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  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_sb.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_defer.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_error.h"
  18. #include "xfs_btree.h"
  19. #include "xfs_rmap_btree.h"
  20. #include "xfs_trace.h"
  21. #include "xfs_log.h"
  22. #include "xfs_rmap.h"
  23. #include "xfs_alloc.h"
  24. #include "xfs_bit.h"
  25. #include <linux/fsmap.h>
  26. #include "xfs_fsmap.h"
  27. #include "xfs_refcount.h"
  28. #include "xfs_refcount_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_rtalloc.h"
  31. /* Convert an xfs_fsmap to an fsmap. */
  32. void
  33. xfs_fsmap_from_internal(
  34. struct fsmap *dest,
  35. struct xfs_fsmap *src)
  36. {
  37. dest->fmr_device = src->fmr_device;
  38. dest->fmr_flags = src->fmr_flags;
  39. dest->fmr_physical = BBTOB(src->fmr_physical);
  40. dest->fmr_owner = src->fmr_owner;
  41. dest->fmr_offset = BBTOB(src->fmr_offset);
  42. dest->fmr_length = BBTOB(src->fmr_length);
  43. dest->fmr_reserved[0] = 0;
  44. dest->fmr_reserved[1] = 0;
  45. dest->fmr_reserved[2] = 0;
  46. }
  47. /* Convert an fsmap to an xfs_fsmap. */
  48. void
  49. xfs_fsmap_to_internal(
  50. struct xfs_fsmap *dest,
  51. struct fsmap *src)
  52. {
  53. dest->fmr_device = src->fmr_device;
  54. dest->fmr_flags = src->fmr_flags;
  55. dest->fmr_physical = BTOBBT(src->fmr_physical);
  56. dest->fmr_owner = src->fmr_owner;
  57. dest->fmr_offset = BTOBBT(src->fmr_offset);
  58. dest->fmr_length = BTOBBT(src->fmr_length);
  59. }
  60. /* Convert an fsmap owner into an rmapbt owner. */
  61. static int
  62. xfs_fsmap_owner_to_rmap(
  63. struct xfs_rmap_irec *dest,
  64. struct xfs_fsmap *src)
  65. {
  66. if (!(src->fmr_flags & FMR_OF_SPECIAL_OWNER)) {
  67. dest->rm_owner = src->fmr_owner;
  68. return 0;
  69. }
  70. switch (src->fmr_owner) {
  71. case 0: /* "lowest owner id possible" */
  72. case -1ULL: /* "highest owner id possible" */
  73. dest->rm_owner = 0;
  74. break;
  75. case XFS_FMR_OWN_FREE:
  76. dest->rm_owner = XFS_RMAP_OWN_NULL;
  77. break;
  78. case XFS_FMR_OWN_UNKNOWN:
  79. dest->rm_owner = XFS_RMAP_OWN_UNKNOWN;
  80. break;
  81. case XFS_FMR_OWN_FS:
  82. dest->rm_owner = XFS_RMAP_OWN_FS;
  83. break;
  84. case XFS_FMR_OWN_LOG:
  85. dest->rm_owner = XFS_RMAP_OWN_LOG;
  86. break;
  87. case XFS_FMR_OWN_AG:
  88. dest->rm_owner = XFS_RMAP_OWN_AG;
  89. break;
  90. case XFS_FMR_OWN_INOBT:
  91. dest->rm_owner = XFS_RMAP_OWN_INOBT;
  92. break;
  93. case XFS_FMR_OWN_INODES:
  94. dest->rm_owner = XFS_RMAP_OWN_INODES;
  95. break;
  96. case XFS_FMR_OWN_REFC:
  97. dest->rm_owner = XFS_RMAP_OWN_REFC;
  98. break;
  99. case XFS_FMR_OWN_COW:
  100. dest->rm_owner = XFS_RMAP_OWN_COW;
  101. break;
  102. case XFS_FMR_OWN_DEFECTIVE: /* not implemented */
  103. /* fall through */
  104. default:
  105. return -EINVAL;
  106. }
  107. return 0;
  108. }
  109. /* Convert an rmapbt owner into an fsmap owner. */
  110. static int
  111. xfs_fsmap_owner_from_rmap(
  112. struct xfs_fsmap *dest,
  113. struct xfs_rmap_irec *src)
  114. {
  115. dest->fmr_flags = 0;
  116. if (!XFS_RMAP_NON_INODE_OWNER(src->rm_owner)) {
  117. dest->fmr_owner = src->rm_owner;
  118. return 0;
  119. }
  120. dest->fmr_flags |= FMR_OF_SPECIAL_OWNER;
  121. switch (src->rm_owner) {
  122. case XFS_RMAP_OWN_FS:
  123. dest->fmr_owner = XFS_FMR_OWN_FS;
  124. break;
  125. case XFS_RMAP_OWN_LOG:
  126. dest->fmr_owner = XFS_FMR_OWN_LOG;
  127. break;
  128. case XFS_RMAP_OWN_AG:
  129. dest->fmr_owner = XFS_FMR_OWN_AG;
  130. break;
  131. case XFS_RMAP_OWN_INOBT:
  132. dest->fmr_owner = XFS_FMR_OWN_INOBT;
  133. break;
  134. case XFS_RMAP_OWN_INODES:
  135. dest->fmr_owner = XFS_FMR_OWN_INODES;
  136. break;
  137. case XFS_RMAP_OWN_REFC:
  138. dest->fmr_owner = XFS_FMR_OWN_REFC;
  139. break;
  140. case XFS_RMAP_OWN_COW:
  141. dest->fmr_owner = XFS_FMR_OWN_COW;
  142. break;
  143. case XFS_RMAP_OWN_NULL: /* "free" */
  144. dest->fmr_owner = XFS_FMR_OWN_FREE;
  145. break;
  146. default:
  147. return -EFSCORRUPTED;
  148. }
  149. return 0;
  150. }
  151. /* getfsmap query state */
  152. struct xfs_getfsmap_info {
  153. struct xfs_fsmap_head *head;
  154. xfs_fsmap_format_t formatter; /* formatting fn */
  155. void *format_arg; /* format buffer */
  156. struct xfs_buf *agf_bp; /* AGF, for refcount queries */
  157. xfs_daddr_t next_daddr; /* next daddr we expect */
  158. u64 missing_owner; /* owner of holes */
  159. u32 dev; /* device id */
  160. xfs_agnumber_t agno; /* AG number, if applicable */
  161. struct xfs_rmap_irec low; /* low rmap key */
  162. struct xfs_rmap_irec high; /* high rmap key */
  163. bool last; /* last extent? */
  164. };
  165. /* Associate a device with a getfsmap handler. */
  166. struct xfs_getfsmap_dev {
  167. u32 dev;
  168. int (*fn)(struct xfs_trans *tp,
  169. struct xfs_fsmap *keys,
  170. struct xfs_getfsmap_info *info);
  171. };
  172. /* Compare two getfsmap device handlers. */
  173. static int
  174. xfs_getfsmap_dev_compare(
  175. const void *p1,
  176. const void *p2)
  177. {
  178. const struct xfs_getfsmap_dev *d1 = p1;
  179. const struct xfs_getfsmap_dev *d2 = p2;
  180. return d1->dev - d2->dev;
  181. }
  182. /* Decide if this mapping is shared. */
  183. STATIC int
  184. xfs_getfsmap_is_shared(
  185. struct xfs_trans *tp,
  186. struct xfs_getfsmap_info *info,
  187. struct xfs_rmap_irec *rec,
  188. bool *stat)
  189. {
  190. struct xfs_mount *mp = tp->t_mountp;
  191. struct xfs_btree_cur *cur;
  192. xfs_agblock_t fbno;
  193. xfs_extlen_t flen;
  194. int error;
  195. *stat = false;
  196. if (!xfs_sb_version_hasreflink(&mp->m_sb))
  197. return 0;
  198. /* rt files will have agno set to NULLAGNUMBER */
  199. if (info->agno == NULLAGNUMBER)
  200. return 0;
  201. /* Are there any shared blocks here? */
  202. flen = 0;
  203. cur = xfs_refcountbt_init_cursor(mp, tp, info->agf_bp,
  204. info->agno, NULL);
  205. error = xfs_refcount_find_shared(cur, rec->rm_startblock,
  206. rec->rm_blockcount, &fbno, &flen, false);
  207. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  208. if (error)
  209. return error;
  210. *stat = flen > 0;
  211. return 0;
  212. }
  213. /*
  214. * Format a reverse mapping for getfsmap, having translated rm_startblock
  215. * into the appropriate daddr units.
  216. */
  217. STATIC int
  218. xfs_getfsmap_helper(
  219. struct xfs_trans *tp,
  220. struct xfs_getfsmap_info *info,
  221. struct xfs_rmap_irec *rec,
  222. xfs_daddr_t rec_daddr)
  223. {
  224. struct xfs_fsmap fmr;
  225. struct xfs_mount *mp = tp->t_mountp;
  226. bool shared;
  227. int error;
  228. if (fatal_signal_pending(current))
  229. return -EINTR;
  230. /*
  231. * Filter out records that start before our startpoint, if the
  232. * caller requested that.
  233. */
  234. if (xfs_rmap_compare(rec, &info->low) < 0) {
  235. rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
  236. if (info->next_daddr < rec_daddr)
  237. info->next_daddr = rec_daddr;
  238. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  239. }
  240. /* Are we just counting mappings? */
  241. if (info->head->fmh_count == 0) {
  242. if (rec_daddr > info->next_daddr)
  243. info->head->fmh_entries++;
  244. if (info->last)
  245. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  246. info->head->fmh_entries++;
  247. rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
  248. if (info->next_daddr < rec_daddr)
  249. info->next_daddr = rec_daddr;
  250. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  251. }
  252. /*
  253. * If the record starts past the last physical block we saw,
  254. * then we've found a gap. Report the gap as being owned by
  255. * whatever the caller specified is the missing owner.
  256. */
  257. if (rec_daddr > info->next_daddr) {
  258. if (info->head->fmh_entries >= info->head->fmh_count)
  259. return XFS_BTREE_QUERY_RANGE_ABORT;
  260. fmr.fmr_device = info->dev;
  261. fmr.fmr_physical = info->next_daddr;
  262. fmr.fmr_owner = info->missing_owner;
  263. fmr.fmr_offset = 0;
  264. fmr.fmr_length = rec_daddr - info->next_daddr;
  265. fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
  266. error = info->formatter(&fmr, info->format_arg);
  267. if (error)
  268. return error;
  269. info->head->fmh_entries++;
  270. }
  271. if (info->last)
  272. goto out;
  273. /* Fill out the extent we found */
  274. if (info->head->fmh_entries >= info->head->fmh_count)
  275. return XFS_BTREE_QUERY_RANGE_ABORT;
  276. trace_xfs_fsmap_mapping(mp, info->dev, info->agno, rec);
  277. fmr.fmr_device = info->dev;
  278. fmr.fmr_physical = rec_daddr;
  279. error = xfs_fsmap_owner_from_rmap(&fmr, rec);
  280. if (error)
  281. return error;
  282. fmr.fmr_offset = XFS_FSB_TO_BB(mp, rec->rm_offset);
  283. fmr.fmr_length = XFS_FSB_TO_BB(mp, rec->rm_blockcount);
  284. if (rec->rm_flags & XFS_RMAP_UNWRITTEN)
  285. fmr.fmr_flags |= FMR_OF_PREALLOC;
  286. if (rec->rm_flags & XFS_RMAP_ATTR_FORK)
  287. fmr.fmr_flags |= FMR_OF_ATTR_FORK;
  288. if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
  289. fmr.fmr_flags |= FMR_OF_EXTENT_MAP;
  290. if (fmr.fmr_flags == 0) {
  291. error = xfs_getfsmap_is_shared(tp, info, rec, &shared);
  292. if (error)
  293. return error;
  294. if (shared)
  295. fmr.fmr_flags |= FMR_OF_SHARED;
  296. }
  297. error = info->formatter(&fmr, info->format_arg);
  298. if (error)
  299. return error;
  300. info->head->fmh_entries++;
  301. out:
  302. rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
  303. if (info->next_daddr < rec_daddr)
  304. info->next_daddr = rec_daddr;
  305. return XFS_BTREE_QUERY_RANGE_CONTINUE;
  306. }
  307. /* Transform a rmapbt irec into a fsmap */
  308. STATIC int
  309. xfs_getfsmap_datadev_helper(
  310. struct xfs_btree_cur *cur,
  311. struct xfs_rmap_irec *rec,
  312. void *priv)
  313. {
  314. struct xfs_mount *mp = cur->bc_mp;
  315. struct xfs_getfsmap_info *info = priv;
  316. xfs_fsblock_t fsb;
  317. xfs_daddr_t rec_daddr;
  318. fsb = XFS_AGB_TO_FSB(mp, cur->bc_private.a.agno, rec->rm_startblock);
  319. rec_daddr = XFS_FSB_TO_DADDR(mp, fsb);
  320. return xfs_getfsmap_helper(cur->bc_tp, info, rec, rec_daddr);
  321. }
  322. /* Transform a bnobt irec into a fsmap */
  323. STATIC int
  324. xfs_getfsmap_datadev_bnobt_helper(
  325. struct xfs_btree_cur *cur,
  326. struct xfs_alloc_rec_incore *rec,
  327. void *priv)
  328. {
  329. struct xfs_mount *mp = cur->bc_mp;
  330. struct xfs_getfsmap_info *info = priv;
  331. struct xfs_rmap_irec irec;
  332. xfs_daddr_t rec_daddr;
  333. rec_daddr = XFS_AGB_TO_DADDR(mp, cur->bc_private.a.agno,
  334. rec->ar_startblock);
  335. irec.rm_startblock = rec->ar_startblock;
  336. irec.rm_blockcount = rec->ar_blockcount;
  337. irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
  338. irec.rm_offset = 0;
  339. irec.rm_flags = 0;
  340. return xfs_getfsmap_helper(cur->bc_tp, info, &irec, rec_daddr);
  341. }
  342. /* Set rmap flags based on the getfsmap flags */
  343. static void
  344. xfs_getfsmap_set_irec_flags(
  345. struct xfs_rmap_irec *irec,
  346. struct xfs_fsmap *fmr)
  347. {
  348. irec->rm_flags = 0;
  349. if (fmr->fmr_flags & FMR_OF_ATTR_FORK)
  350. irec->rm_flags |= XFS_RMAP_ATTR_FORK;
  351. if (fmr->fmr_flags & FMR_OF_EXTENT_MAP)
  352. irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
  353. if (fmr->fmr_flags & FMR_OF_PREALLOC)
  354. irec->rm_flags |= XFS_RMAP_UNWRITTEN;
  355. }
  356. /* Execute a getfsmap query against the log device. */
  357. STATIC int
  358. xfs_getfsmap_logdev(
  359. struct xfs_trans *tp,
  360. struct xfs_fsmap *keys,
  361. struct xfs_getfsmap_info *info)
  362. {
  363. struct xfs_mount *mp = tp->t_mountp;
  364. struct xfs_rmap_irec rmap;
  365. int error;
  366. /* Set up search keys */
  367. info->low.rm_startblock = XFS_BB_TO_FSBT(mp, keys[0].fmr_physical);
  368. info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
  369. error = xfs_fsmap_owner_to_rmap(&info->low, keys);
  370. if (error)
  371. return error;
  372. info->low.rm_blockcount = 0;
  373. xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
  374. error = xfs_fsmap_owner_to_rmap(&info->high, keys + 1);
  375. if (error)
  376. return error;
  377. info->high.rm_startblock = -1U;
  378. info->high.rm_owner = ULLONG_MAX;
  379. info->high.rm_offset = ULLONG_MAX;
  380. info->high.rm_blockcount = 0;
  381. info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
  382. info->missing_owner = XFS_FMR_OWN_FREE;
  383. trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
  384. trace_xfs_fsmap_high_key(mp, info->dev, info->agno, &info->high);
  385. if (keys[0].fmr_physical > 0)
  386. return 0;
  387. /* Fabricate an rmap entry for the external log device. */
  388. rmap.rm_startblock = 0;
  389. rmap.rm_blockcount = mp->m_sb.sb_logblocks;
  390. rmap.rm_owner = XFS_RMAP_OWN_LOG;
  391. rmap.rm_offset = 0;
  392. rmap.rm_flags = 0;
  393. return xfs_getfsmap_helper(tp, info, &rmap, 0);
  394. }
  395. #ifdef CONFIG_XFS_RT
  396. /* Transform a rtbitmap "record" into a fsmap */
  397. STATIC int
  398. xfs_getfsmap_rtdev_rtbitmap_helper(
  399. struct xfs_trans *tp,
  400. struct xfs_rtalloc_rec *rec,
  401. void *priv)
  402. {
  403. struct xfs_mount *mp = tp->t_mountp;
  404. struct xfs_getfsmap_info *info = priv;
  405. struct xfs_rmap_irec irec;
  406. xfs_daddr_t rec_daddr;
  407. irec.rm_startblock = rec->ar_startext * mp->m_sb.sb_rextsize;
  408. rec_daddr = XFS_FSB_TO_BB(mp, irec.rm_startblock);
  409. irec.rm_blockcount = rec->ar_extcount * mp->m_sb.sb_rextsize;
  410. irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
  411. irec.rm_offset = 0;
  412. irec.rm_flags = 0;
  413. return xfs_getfsmap_helper(tp, info, &irec, rec_daddr);
  414. }
  415. /* Execute a getfsmap query against the realtime device. */
  416. STATIC int
  417. __xfs_getfsmap_rtdev(
  418. struct xfs_trans *tp,
  419. struct xfs_fsmap *keys,
  420. int (*query_fn)(struct xfs_trans *,
  421. struct xfs_getfsmap_info *),
  422. struct xfs_getfsmap_info *info)
  423. {
  424. struct xfs_mount *mp = tp->t_mountp;
  425. xfs_fsblock_t start_fsb;
  426. xfs_fsblock_t end_fsb;
  427. xfs_daddr_t eofs;
  428. int error = 0;
  429. eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  430. if (keys[0].fmr_physical >= eofs)
  431. return 0;
  432. if (keys[1].fmr_physical >= eofs)
  433. keys[1].fmr_physical = eofs - 1;
  434. start_fsb = XFS_BB_TO_FSBT(mp, keys[0].fmr_physical);
  435. end_fsb = XFS_BB_TO_FSB(mp, keys[1].fmr_physical);
  436. /* Set up search keys */
  437. info->low.rm_startblock = start_fsb;
  438. error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
  439. if (error)
  440. return error;
  441. info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
  442. info->low.rm_blockcount = 0;
  443. xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
  444. info->high.rm_startblock = end_fsb;
  445. error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
  446. if (error)
  447. return error;
  448. info->high.rm_offset = XFS_BB_TO_FSBT(mp, keys[1].fmr_offset);
  449. info->high.rm_blockcount = 0;
  450. xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
  451. trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
  452. trace_xfs_fsmap_high_key(mp, info->dev, info->agno, &info->high);
  453. return query_fn(tp, info);
  454. }
  455. /* Actually query the realtime bitmap. */
  456. STATIC int
  457. xfs_getfsmap_rtdev_rtbitmap_query(
  458. struct xfs_trans *tp,
  459. struct xfs_getfsmap_info *info)
  460. {
  461. struct xfs_rtalloc_rec alow = { 0 };
  462. struct xfs_rtalloc_rec ahigh = { 0 };
  463. int error;
  464. xfs_ilock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
  465. alow.ar_startext = info->low.rm_startblock;
  466. ahigh.ar_startext = info->high.rm_startblock;
  467. do_div(alow.ar_startext, tp->t_mountp->m_sb.sb_rextsize);
  468. if (do_div(ahigh.ar_startext, tp->t_mountp->m_sb.sb_rextsize))
  469. ahigh.ar_startext++;
  470. error = xfs_rtalloc_query_range(tp, &alow, &ahigh,
  471. xfs_getfsmap_rtdev_rtbitmap_helper, info);
  472. if (error)
  473. goto err;
  474. /* Report any gaps at the end of the rtbitmap */
  475. info->last = true;
  476. error = xfs_getfsmap_rtdev_rtbitmap_helper(tp, &ahigh, info);
  477. if (error)
  478. goto err;
  479. err:
  480. xfs_iunlock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
  481. return error;
  482. }
  483. /* Execute a getfsmap query against the realtime device rtbitmap. */
  484. STATIC int
  485. xfs_getfsmap_rtdev_rtbitmap(
  486. struct xfs_trans *tp,
  487. struct xfs_fsmap *keys,
  488. struct xfs_getfsmap_info *info)
  489. {
  490. info->missing_owner = XFS_FMR_OWN_UNKNOWN;
  491. return __xfs_getfsmap_rtdev(tp, keys, xfs_getfsmap_rtdev_rtbitmap_query,
  492. info);
  493. }
  494. #endif /* CONFIG_XFS_RT */
  495. /* Execute a getfsmap query against the regular data device. */
  496. STATIC int
  497. __xfs_getfsmap_datadev(
  498. struct xfs_trans *tp,
  499. struct xfs_fsmap *keys,
  500. struct xfs_getfsmap_info *info,
  501. int (*query_fn)(struct xfs_trans *,
  502. struct xfs_getfsmap_info *,
  503. struct xfs_btree_cur **,
  504. void *),
  505. void *priv)
  506. {
  507. struct xfs_mount *mp = tp->t_mountp;
  508. struct xfs_btree_cur *bt_cur = NULL;
  509. xfs_fsblock_t start_fsb;
  510. xfs_fsblock_t end_fsb;
  511. xfs_agnumber_t start_ag;
  512. xfs_agnumber_t end_ag;
  513. xfs_daddr_t eofs;
  514. int error = 0;
  515. eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  516. if (keys[0].fmr_physical >= eofs)
  517. return 0;
  518. if (keys[1].fmr_physical >= eofs)
  519. keys[1].fmr_physical = eofs - 1;
  520. start_fsb = XFS_DADDR_TO_FSB(mp, keys[0].fmr_physical);
  521. end_fsb = XFS_DADDR_TO_FSB(mp, keys[1].fmr_physical);
  522. /*
  523. * Convert the fsmap low/high keys to AG based keys. Initialize
  524. * low to the fsmap low key and max out the high key to the end
  525. * of the AG.
  526. */
  527. info->low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
  528. info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
  529. error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
  530. if (error)
  531. return error;
  532. info->low.rm_blockcount = 0;
  533. xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
  534. info->high.rm_startblock = -1U;
  535. info->high.rm_owner = ULLONG_MAX;
  536. info->high.rm_offset = ULLONG_MAX;
  537. info->high.rm_blockcount = 0;
  538. info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
  539. start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
  540. end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
  541. /* Query each AG */
  542. for (info->agno = start_ag; info->agno <= end_ag; info->agno++) {
  543. /*
  544. * Set the AG high key from the fsmap high key if this
  545. * is the last AG that we're querying.
  546. */
  547. if (info->agno == end_ag) {
  548. info->high.rm_startblock = XFS_FSB_TO_AGBNO(mp,
  549. end_fsb);
  550. info->high.rm_offset = XFS_BB_TO_FSBT(mp,
  551. keys[1].fmr_offset);
  552. error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
  553. if (error)
  554. goto err;
  555. xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
  556. }
  557. if (bt_cur) {
  558. xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
  559. bt_cur = NULL;
  560. xfs_trans_brelse(tp, info->agf_bp);
  561. info->agf_bp = NULL;
  562. }
  563. error = xfs_alloc_read_agf(mp, tp, info->agno, 0,
  564. &info->agf_bp);
  565. if (error)
  566. goto err;
  567. trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
  568. trace_xfs_fsmap_high_key(mp, info->dev, info->agno,
  569. &info->high);
  570. error = query_fn(tp, info, &bt_cur, priv);
  571. if (error)
  572. goto err;
  573. /*
  574. * Set the AG low key to the start of the AG prior to
  575. * moving on to the next AG.
  576. */
  577. if (info->agno == start_ag) {
  578. info->low.rm_startblock = 0;
  579. info->low.rm_owner = 0;
  580. info->low.rm_offset = 0;
  581. info->low.rm_flags = 0;
  582. }
  583. }
  584. /* Report any gap at the end of the AG */
  585. info->last = true;
  586. error = query_fn(tp, info, &bt_cur, priv);
  587. if (error)
  588. goto err;
  589. err:
  590. if (bt_cur)
  591. xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
  592. XFS_BTREE_NOERROR);
  593. if (info->agf_bp) {
  594. xfs_trans_brelse(tp, info->agf_bp);
  595. info->agf_bp = NULL;
  596. }
  597. return error;
  598. }
  599. /* Actually query the rmap btree. */
  600. STATIC int
  601. xfs_getfsmap_datadev_rmapbt_query(
  602. struct xfs_trans *tp,
  603. struct xfs_getfsmap_info *info,
  604. struct xfs_btree_cur **curpp,
  605. void *priv)
  606. {
  607. /* Report any gap at the end of the last AG. */
  608. if (info->last)
  609. return xfs_getfsmap_datadev_helper(*curpp, &info->high, info);
  610. /* Allocate cursor for this AG and query_range it. */
  611. *curpp = xfs_rmapbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
  612. info->agno);
  613. return xfs_rmap_query_range(*curpp, &info->low, &info->high,
  614. xfs_getfsmap_datadev_helper, info);
  615. }
  616. /* Execute a getfsmap query against the regular data device rmapbt. */
  617. STATIC int
  618. xfs_getfsmap_datadev_rmapbt(
  619. struct xfs_trans *tp,
  620. struct xfs_fsmap *keys,
  621. struct xfs_getfsmap_info *info)
  622. {
  623. info->missing_owner = XFS_FMR_OWN_FREE;
  624. return __xfs_getfsmap_datadev(tp, keys, info,
  625. xfs_getfsmap_datadev_rmapbt_query, NULL);
  626. }
  627. /* Actually query the bno btree. */
  628. STATIC int
  629. xfs_getfsmap_datadev_bnobt_query(
  630. struct xfs_trans *tp,
  631. struct xfs_getfsmap_info *info,
  632. struct xfs_btree_cur **curpp,
  633. void *priv)
  634. {
  635. struct xfs_alloc_rec_incore *key = priv;
  636. /* Report any gap at the end of the last AG. */
  637. if (info->last)
  638. return xfs_getfsmap_datadev_bnobt_helper(*curpp, &key[1], info);
  639. /* Allocate cursor for this AG and query_range it. */
  640. *curpp = xfs_allocbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
  641. info->agno, XFS_BTNUM_BNO);
  642. key->ar_startblock = info->low.rm_startblock;
  643. key[1].ar_startblock = info->high.rm_startblock;
  644. return xfs_alloc_query_range(*curpp, key, &key[1],
  645. xfs_getfsmap_datadev_bnobt_helper, info);
  646. }
  647. /* Execute a getfsmap query against the regular data device's bnobt. */
  648. STATIC int
  649. xfs_getfsmap_datadev_bnobt(
  650. struct xfs_trans *tp,
  651. struct xfs_fsmap *keys,
  652. struct xfs_getfsmap_info *info)
  653. {
  654. struct xfs_alloc_rec_incore akeys[2];
  655. info->missing_owner = XFS_FMR_OWN_UNKNOWN;
  656. return __xfs_getfsmap_datadev(tp, keys, info,
  657. xfs_getfsmap_datadev_bnobt_query, &akeys[0]);
  658. }
  659. /* Do we recognize the device? */
  660. STATIC bool
  661. xfs_getfsmap_is_valid_device(
  662. struct xfs_mount *mp,
  663. struct xfs_fsmap *fm)
  664. {
  665. if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
  666. fm->fmr_device == new_encode_dev(mp->m_ddev_targp->bt_dev))
  667. return true;
  668. if (mp->m_logdev_targp &&
  669. fm->fmr_device == new_encode_dev(mp->m_logdev_targp->bt_dev))
  670. return true;
  671. if (mp->m_rtdev_targp &&
  672. fm->fmr_device == new_encode_dev(mp->m_rtdev_targp->bt_dev))
  673. return true;
  674. return false;
  675. }
  676. /* Ensure that the low key is less than the high key. */
  677. STATIC bool
  678. xfs_getfsmap_check_keys(
  679. struct xfs_fsmap *low_key,
  680. struct xfs_fsmap *high_key)
  681. {
  682. if (low_key->fmr_device > high_key->fmr_device)
  683. return false;
  684. if (low_key->fmr_device < high_key->fmr_device)
  685. return true;
  686. if (low_key->fmr_physical > high_key->fmr_physical)
  687. return false;
  688. if (low_key->fmr_physical < high_key->fmr_physical)
  689. return true;
  690. if (low_key->fmr_owner > high_key->fmr_owner)
  691. return false;
  692. if (low_key->fmr_owner < high_key->fmr_owner)
  693. return true;
  694. if (low_key->fmr_offset > high_key->fmr_offset)
  695. return false;
  696. if (low_key->fmr_offset < high_key->fmr_offset)
  697. return true;
  698. return false;
  699. }
  700. /*
  701. * There are only two devices if we didn't configure RT devices at build time.
  702. */
  703. #ifdef CONFIG_XFS_RT
  704. #define XFS_GETFSMAP_DEVS 3
  705. #else
  706. #define XFS_GETFSMAP_DEVS 2
  707. #endif /* CONFIG_XFS_RT */
  708. /*
  709. * Get filesystem's extents as described in head, and format for
  710. * output. Calls formatter to fill the user's buffer until all
  711. * extents are mapped, until the passed-in head->fmh_count slots have
  712. * been filled, or until the formatter short-circuits the loop, if it
  713. * is tracking filled-in extents on its own.
  714. *
  715. * Key to Confusion
  716. * ----------------
  717. * There are multiple levels of keys and counters at work here:
  718. * xfs_fsmap_head.fmh_keys -- low and high fsmap keys passed in;
  719. * these reflect fs-wide sector addrs.
  720. * dkeys -- fmh_keys used to query each device;
  721. * these are fmh_keys but w/ the low key
  722. * bumped up by fmr_length.
  723. * xfs_getfsmap_info.next_daddr -- next disk addr we expect to see; this
  724. * is how we detect gaps in the fsmap
  725. records and report them.
  726. * xfs_getfsmap_info.low/high -- per-AG low/high keys computed from
  727. * dkeys; used to query the metadata.
  728. */
  729. int
  730. xfs_getfsmap(
  731. struct xfs_mount *mp,
  732. struct xfs_fsmap_head *head,
  733. xfs_fsmap_format_t formatter,
  734. void *arg)
  735. {
  736. struct xfs_trans *tp = NULL;
  737. struct xfs_fsmap dkeys[2]; /* per-dev keys */
  738. struct xfs_getfsmap_dev handlers[XFS_GETFSMAP_DEVS];
  739. struct xfs_getfsmap_info info = { NULL };
  740. bool use_rmap;
  741. int i;
  742. int error = 0;
  743. if (head->fmh_iflags & ~FMH_IF_VALID)
  744. return -EINVAL;
  745. if (!xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[0]) ||
  746. !xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[1]))
  747. return -EINVAL;
  748. use_rmap = capable(CAP_SYS_ADMIN) &&
  749. xfs_sb_version_hasrmapbt(&mp->m_sb);
  750. head->fmh_entries = 0;
  751. /* Set up our device handlers. */
  752. memset(handlers, 0, sizeof(handlers));
  753. handlers[0].dev = new_encode_dev(mp->m_ddev_targp->bt_dev);
  754. if (use_rmap)
  755. handlers[0].fn = xfs_getfsmap_datadev_rmapbt;
  756. else
  757. handlers[0].fn = xfs_getfsmap_datadev_bnobt;
  758. if (mp->m_logdev_targp != mp->m_ddev_targp) {
  759. handlers[1].dev = new_encode_dev(mp->m_logdev_targp->bt_dev);
  760. handlers[1].fn = xfs_getfsmap_logdev;
  761. }
  762. #ifdef CONFIG_XFS_RT
  763. if (mp->m_rtdev_targp) {
  764. handlers[2].dev = new_encode_dev(mp->m_rtdev_targp->bt_dev);
  765. handlers[2].fn = xfs_getfsmap_rtdev_rtbitmap;
  766. }
  767. #endif /* CONFIG_XFS_RT */
  768. xfs_sort(handlers, XFS_GETFSMAP_DEVS, sizeof(struct xfs_getfsmap_dev),
  769. xfs_getfsmap_dev_compare);
  770. /*
  771. * To continue where we left off, we allow userspace to use the
  772. * last mapping from a previous call as the low key of the next.
  773. * This is identified by a non-zero length in the low key. We
  774. * have to increment the low key in this scenario to ensure we
  775. * don't return the same mapping again, and instead return the
  776. * very next mapping.
  777. *
  778. * If the low key mapping refers to file data, the same physical
  779. * blocks could be mapped to several other files/offsets.
  780. * According to rmapbt record ordering, the minimal next
  781. * possible record for the block range is the next starting
  782. * offset in the same inode. Therefore, bump the file offset to
  783. * continue the search appropriately. For all other low key
  784. * mapping types (attr blocks, metadata), bump the physical
  785. * offset as there can be no other mapping for the same physical
  786. * block range.
  787. */
  788. dkeys[0] = head->fmh_keys[0];
  789. if (dkeys[0].fmr_flags & (FMR_OF_SPECIAL_OWNER | FMR_OF_EXTENT_MAP)) {
  790. dkeys[0].fmr_physical += dkeys[0].fmr_length;
  791. dkeys[0].fmr_owner = 0;
  792. if (dkeys[0].fmr_offset)
  793. return -EINVAL;
  794. } else
  795. dkeys[0].fmr_offset += dkeys[0].fmr_length;
  796. dkeys[0].fmr_length = 0;
  797. memset(&dkeys[1], 0xFF, sizeof(struct xfs_fsmap));
  798. if (!xfs_getfsmap_check_keys(dkeys, &head->fmh_keys[1]))
  799. return -EINVAL;
  800. info.next_daddr = head->fmh_keys[0].fmr_physical +
  801. head->fmh_keys[0].fmr_length;
  802. info.formatter = formatter;
  803. info.format_arg = arg;
  804. info.head = head;
  805. /* For each device we support... */
  806. for (i = 0; i < XFS_GETFSMAP_DEVS; i++) {
  807. /* Is this device within the range the user asked for? */
  808. if (!handlers[i].fn)
  809. continue;
  810. if (head->fmh_keys[0].fmr_device > handlers[i].dev)
  811. continue;
  812. if (head->fmh_keys[1].fmr_device < handlers[i].dev)
  813. break;
  814. /*
  815. * If this device number matches the high key, we have
  816. * to pass the high key to the handler to limit the
  817. * query results. If the device number exceeds the
  818. * low key, zero out the low key so that we get
  819. * everything from the beginning.
  820. */
  821. if (handlers[i].dev == head->fmh_keys[1].fmr_device)
  822. dkeys[1] = head->fmh_keys[1];
  823. if (handlers[i].dev > head->fmh_keys[0].fmr_device)
  824. memset(&dkeys[0], 0, sizeof(struct xfs_fsmap));
  825. error = xfs_trans_alloc_empty(mp, &tp);
  826. if (error)
  827. break;
  828. info.dev = handlers[i].dev;
  829. info.last = false;
  830. info.agno = NULLAGNUMBER;
  831. error = handlers[i].fn(tp, dkeys, &info);
  832. if (error)
  833. break;
  834. xfs_trans_cancel(tp);
  835. tp = NULL;
  836. info.next_daddr = 0;
  837. }
  838. if (tp)
  839. xfs_trans_cancel(tp);
  840. head->fmh_oflags = FMH_OF_DEV_T;
  841. return error;
  842. }