xfs_fsmap.c 26 KB

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