xfs_fsmap.c 26 KB

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