fsmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Oracle. All Rights Reserved.
  4. *
  5. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  6. */
  7. #include "ext4.h"
  8. #include <linux/fsmap.h>
  9. #include "fsmap.h"
  10. #include "mballoc.h"
  11. #include <linux/sort.h>
  12. #include <linux/list_sort.h>
  13. #include <trace/events/ext4.h>
  14. /* Convert an ext4_fsmap to an fsmap. */
  15. void ext4_fsmap_from_internal(struct super_block *sb, struct fsmap *dest,
  16. struct ext4_fsmap *src)
  17. {
  18. dest->fmr_device = src->fmr_device;
  19. dest->fmr_flags = src->fmr_flags;
  20. dest->fmr_physical = src->fmr_physical << sb->s_blocksize_bits;
  21. dest->fmr_owner = src->fmr_owner;
  22. dest->fmr_offset = 0;
  23. dest->fmr_length = src->fmr_length << sb->s_blocksize_bits;
  24. dest->fmr_reserved[0] = 0;
  25. dest->fmr_reserved[1] = 0;
  26. dest->fmr_reserved[2] = 0;
  27. }
  28. /* Convert an fsmap to an ext4_fsmap. */
  29. void ext4_fsmap_to_internal(struct super_block *sb, struct ext4_fsmap *dest,
  30. struct fsmap *src)
  31. {
  32. dest->fmr_device = src->fmr_device;
  33. dest->fmr_flags = src->fmr_flags;
  34. dest->fmr_physical = src->fmr_physical >> sb->s_blocksize_bits;
  35. dest->fmr_owner = src->fmr_owner;
  36. dest->fmr_length = src->fmr_length >> sb->s_blocksize_bits;
  37. }
  38. /* getfsmap query state */
  39. struct ext4_getfsmap_info {
  40. struct ext4_fsmap_head *gfi_head;
  41. ext4_fsmap_format_t gfi_formatter; /* formatting fn */
  42. void *gfi_format_arg;/* format buffer */
  43. ext4_fsblk_t gfi_next_fsblk; /* next fsblock we expect */
  44. u32 gfi_dev; /* device id */
  45. ext4_group_t gfi_agno; /* bg number, if applicable */
  46. struct ext4_fsmap gfi_low; /* low rmap key */
  47. struct ext4_fsmap gfi_high; /* high rmap key */
  48. struct ext4_fsmap gfi_lastfree; /* free ext at end of last bg */
  49. struct list_head gfi_meta_list; /* fixed metadata list */
  50. bool gfi_last; /* last extent? */
  51. };
  52. /* Associate a device with a getfsmap handler. */
  53. struct ext4_getfsmap_dev {
  54. int (*gfd_fn)(struct super_block *sb,
  55. struct ext4_fsmap *keys,
  56. struct ext4_getfsmap_info *info);
  57. u32 gfd_dev;
  58. };
  59. /* Compare two getfsmap device handlers. */
  60. static int ext4_getfsmap_dev_compare(const void *p1, const void *p2)
  61. {
  62. const struct ext4_getfsmap_dev *d1 = p1;
  63. const struct ext4_getfsmap_dev *d2 = p2;
  64. return d1->gfd_dev - d2->gfd_dev;
  65. }
  66. /* Compare a record against our starting point */
  67. static bool ext4_getfsmap_rec_before_low_key(struct ext4_getfsmap_info *info,
  68. struct ext4_fsmap *rec)
  69. {
  70. return rec->fmr_physical < info->gfi_low.fmr_physical;
  71. }
  72. /*
  73. * Format a reverse mapping for getfsmap, having translated rm_startblock
  74. * into the appropriate daddr units.
  75. */
  76. static int ext4_getfsmap_helper(struct super_block *sb,
  77. struct ext4_getfsmap_info *info,
  78. struct ext4_fsmap *rec)
  79. {
  80. struct ext4_fsmap fmr;
  81. struct ext4_sb_info *sbi = EXT4_SB(sb);
  82. ext4_fsblk_t rec_fsblk = rec->fmr_physical;
  83. ext4_group_t agno;
  84. ext4_grpblk_t cno;
  85. int error;
  86. if (fatal_signal_pending(current))
  87. return -EINTR;
  88. /*
  89. * Filter out records that start before our startpoint, if the
  90. * caller requested that.
  91. */
  92. if (ext4_getfsmap_rec_before_low_key(info, rec)) {
  93. rec_fsblk += rec->fmr_length;
  94. if (info->gfi_next_fsblk < rec_fsblk)
  95. info->gfi_next_fsblk = rec_fsblk;
  96. return EXT4_QUERY_RANGE_CONTINUE;
  97. }
  98. /* Are we just counting mappings? */
  99. if (info->gfi_head->fmh_count == 0) {
  100. if (rec_fsblk > info->gfi_next_fsblk)
  101. info->gfi_head->fmh_entries++;
  102. if (info->gfi_last)
  103. return EXT4_QUERY_RANGE_CONTINUE;
  104. info->gfi_head->fmh_entries++;
  105. rec_fsblk += rec->fmr_length;
  106. if (info->gfi_next_fsblk < rec_fsblk)
  107. info->gfi_next_fsblk = rec_fsblk;
  108. return EXT4_QUERY_RANGE_CONTINUE;
  109. }
  110. /*
  111. * If the record starts past the last physical block we saw,
  112. * then we've found a gap. Report the gap as being owned by
  113. * whatever the caller specified is the missing owner.
  114. */
  115. if (rec_fsblk > info->gfi_next_fsblk) {
  116. if (info->gfi_head->fmh_entries >= info->gfi_head->fmh_count)
  117. return EXT4_QUERY_RANGE_ABORT;
  118. ext4_get_group_no_and_offset(sb, info->gfi_next_fsblk,
  119. &agno, &cno);
  120. trace_ext4_fsmap_mapping(sb, info->gfi_dev, agno,
  121. EXT4_C2B(sbi, cno),
  122. rec_fsblk - info->gfi_next_fsblk,
  123. EXT4_FMR_OWN_UNKNOWN);
  124. fmr.fmr_device = info->gfi_dev;
  125. fmr.fmr_physical = info->gfi_next_fsblk;
  126. fmr.fmr_owner = EXT4_FMR_OWN_UNKNOWN;
  127. fmr.fmr_length = rec_fsblk - info->gfi_next_fsblk;
  128. fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
  129. error = info->gfi_formatter(&fmr, info->gfi_format_arg);
  130. if (error)
  131. return error;
  132. info->gfi_head->fmh_entries++;
  133. }
  134. if (info->gfi_last)
  135. goto out;
  136. /* Fill out the extent we found */
  137. if (info->gfi_head->fmh_entries >= info->gfi_head->fmh_count)
  138. return EXT4_QUERY_RANGE_ABORT;
  139. ext4_get_group_no_and_offset(sb, rec_fsblk, &agno, &cno);
  140. trace_ext4_fsmap_mapping(sb, info->gfi_dev, agno, EXT4_C2B(sbi, cno),
  141. rec->fmr_length, rec->fmr_owner);
  142. fmr.fmr_device = info->gfi_dev;
  143. fmr.fmr_physical = rec_fsblk;
  144. fmr.fmr_owner = rec->fmr_owner;
  145. fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
  146. fmr.fmr_length = rec->fmr_length;
  147. error = info->gfi_formatter(&fmr, info->gfi_format_arg);
  148. if (error)
  149. return error;
  150. info->gfi_head->fmh_entries++;
  151. out:
  152. rec_fsblk += rec->fmr_length;
  153. if (info->gfi_next_fsblk < rec_fsblk)
  154. info->gfi_next_fsblk = rec_fsblk;
  155. return EXT4_QUERY_RANGE_CONTINUE;
  156. }
  157. static inline ext4_fsblk_t ext4_fsmap_next_pblk(struct ext4_fsmap *fmr)
  158. {
  159. return fmr->fmr_physical + fmr->fmr_length;
  160. }
  161. /* Transform a blockgroup's free record into a fsmap */
  162. static int ext4_getfsmap_datadev_helper(struct super_block *sb,
  163. ext4_group_t agno, ext4_grpblk_t start,
  164. ext4_grpblk_t len, void *priv)
  165. {
  166. struct ext4_fsmap irec;
  167. struct ext4_getfsmap_info *info = priv;
  168. struct ext4_fsmap *p;
  169. struct ext4_fsmap *tmp;
  170. struct ext4_sb_info *sbi = EXT4_SB(sb);
  171. ext4_fsblk_t fsb;
  172. ext4_fsblk_t fslen;
  173. int error;
  174. fsb = (EXT4_C2B(sbi, start) + ext4_group_first_block_no(sb, agno));
  175. fslen = EXT4_C2B(sbi, len);
  176. /* If the retained free extent record is set... */
  177. if (info->gfi_lastfree.fmr_owner) {
  178. /* ...and abuts this one, lengthen it and return. */
  179. if (ext4_fsmap_next_pblk(&info->gfi_lastfree) == fsb) {
  180. info->gfi_lastfree.fmr_length += fslen;
  181. return 0;
  182. }
  183. /*
  184. * There's a gap between the two free extents; emit the
  185. * retained extent prior to merging the meta_list.
  186. */
  187. error = ext4_getfsmap_helper(sb, info, &info->gfi_lastfree);
  188. if (error)
  189. return error;
  190. info->gfi_lastfree.fmr_owner = 0;
  191. }
  192. /* Merge in any relevant extents from the meta_list */
  193. list_for_each_entry_safe(p, tmp, &info->gfi_meta_list, fmr_list) {
  194. if (p->fmr_physical + p->fmr_length <= info->gfi_next_fsblk) {
  195. list_del(&p->fmr_list);
  196. kfree(p);
  197. } else if (p->fmr_physical < fsb) {
  198. error = ext4_getfsmap_helper(sb, info, p);
  199. if (error)
  200. return error;
  201. list_del(&p->fmr_list);
  202. kfree(p);
  203. }
  204. }
  205. irec.fmr_device = 0;
  206. irec.fmr_physical = fsb;
  207. irec.fmr_length = fslen;
  208. irec.fmr_owner = EXT4_FMR_OWN_FREE;
  209. irec.fmr_flags = 0;
  210. /* If this is a free extent at the end of a bg, buffer it. */
  211. if (ext4_fsmap_next_pblk(&irec) ==
  212. ext4_group_first_block_no(sb, agno + 1)) {
  213. info->gfi_lastfree = irec;
  214. return 0;
  215. }
  216. /* Otherwise, emit it */
  217. return ext4_getfsmap_helper(sb, info, &irec);
  218. }
  219. /* Execute a getfsmap query against the log device. */
  220. static int ext4_getfsmap_logdev(struct super_block *sb, struct ext4_fsmap *keys,
  221. struct ext4_getfsmap_info *info)
  222. {
  223. journal_t *journal = EXT4_SB(sb)->s_journal;
  224. struct ext4_fsmap irec;
  225. /* Set up search keys */
  226. info->gfi_low = keys[0];
  227. info->gfi_low.fmr_length = 0;
  228. memset(&info->gfi_high, 0xFF, sizeof(info->gfi_high));
  229. trace_ext4_fsmap_low_key(sb, info->gfi_dev, 0,
  230. info->gfi_low.fmr_physical,
  231. info->gfi_low.fmr_length,
  232. info->gfi_low.fmr_owner);
  233. trace_ext4_fsmap_high_key(sb, info->gfi_dev, 0,
  234. info->gfi_high.fmr_physical,
  235. info->gfi_high.fmr_length,
  236. info->gfi_high.fmr_owner);
  237. if (keys[0].fmr_physical > 0)
  238. return 0;
  239. /* Fabricate an rmap entry for the external log device. */
  240. irec.fmr_physical = journal->j_blk_offset;
  241. irec.fmr_length = journal->j_maxlen;
  242. irec.fmr_owner = EXT4_FMR_OWN_LOG;
  243. irec.fmr_flags = 0;
  244. return ext4_getfsmap_helper(sb, info, &irec);
  245. }
  246. /* Helper to fill out an ext4_fsmap. */
  247. static inline int ext4_getfsmap_fill(struct list_head *meta_list,
  248. ext4_fsblk_t fsb, ext4_fsblk_t len,
  249. uint64_t owner)
  250. {
  251. struct ext4_fsmap *fsm;
  252. fsm = kmalloc(sizeof(*fsm), GFP_NOFS);
  253. if (!fsm)
  254. return -ENOMEM;
  255. fsm->fmr_device = 0;
  256. fsm->fmr_flags = 0;
  257. fsm->fmr_physical = fsb;
  258. fsm->fmr_owner = owner;
  259. fsm->fmr_length = len;
  260. list_add_tail(&fsm->fmr_list, meta_list);
  261. return 0;
  262. }
  263. /*
  264. * This function returns the number of file system metadata blocks at
  265. * the beginning of a block group, including the reserved gdt blocks.
  266. */
  267. static unsigned int ext4_getfsmap_find_sb(struct super_block *sb,
  268. ext4_group_t agno,
  269. struct list_head *meta_list)
  270. {
  271. struct ext4_sb_info *sbi = EXT4_SB(sb);
  272. ext4_fsblk_t fsb = ext4_group_first_block_no(sb, agno);
  273. ext4_fsblk_t len;
  274. unsigned long first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  275. unsigned long metagroup = agno / EXT4_DESC_PER_BLOCK(sb);
  276. int error;
  277. /* Record the superblock. */
  278. if (ext4_bg_has_super(sb, agno)) {
  279. error = ext4_getfsmap_fill(meta_list, fsb, 1, EXT4_FMR_OWN_FS);
  280. if (error)
  281. return error;
  282. fsb++;
  283. }
  284. /* Record the group descriptors. */
  285. len = ext4_bg_num_gdb(sb, agno);
  286. if (!len)
  287. return 0;
  288. error = ext4_getfsmap_fill(meta_list, fsb, len,
  289. EXT4_FMR_OWN_GDT);
  290. if (error)
  291. return error;
  292. fsb += len;
  293. /* Reserved GDT blocks */
  294. if (!ext4_has_feature_meta_bg(sb) || metagroup < first_meta_bg) {
  295. len = le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
  296. error = ext4_getfsmap_fill(meta_list, fsb, len,
  297. EXT4_FMR_OWN_RESV_GDT);
  298. if (error)
  299. return error;
  300. }
  301. return 0;
  302. }
  303. /* Compare two fsmap items. */
  304. static int ext4_getfsmap_compare(void *priv,
  305. struct list_head *a,
  306. struct list_head *b)
  307. {
  308. struct ext4_fsmap *fa;
  309. struct ext4_fsmap *fb;
  310. fa = container_of(a, struct ext4_fsmap, fmr_list);
  311. fb = container_of(b, struct ext4_fsmap, fmr_list);
  312. if (fa->fmr_physical < fb->fmr_physical)
  313. return -1;
  314. else if (fa->fmr_physical > fb->fmr_physical)
  315. return 1;
  316. return 0;
  317. }
  318. /* Merge adjacent extents of fixed metadata. */
  319. static void ext4_getfsmap_merge_fixed_metadata(struct list_head *meta_list)
  320. {
  321. struct ext4_fsmap *p;
  322. struct ext4_fsmap *prev = NULL;
  323. struct ext4_fsmap *tmp;
  324. list_for_each_entry_safe(p, tmp, meta_list, fmr_list) {
  325. if (!prev) {
  326. prev = p;
  327. continue;
  328. }
  329. if (prev->fmr_owner == p->fmr_owner &&
  330. prev->fmr_physical + prev->fmr_length == p->fmr_physical) {
  331. prev->fmr_length += p->fmr_length;
  332. list_del(&p->fmr_list);
  333. kfree(p);
  334. } else
  335. prev = p;
  336. }
  337. }
  338. /* Free a list of fixed metadata. */
  339. static void ext4_getfsmap_free_fixed_metadata(struct list_head *meta_list)
  340. {
  341. struct ext4_fsmap *p;
  342. struct ext4_fsmap *tmp;
  343. list_for_each_entry_safe(p, tmp, meta_list, fmr_list) {
  344. list_del(&p->fmr_list);
  345. kfree(p);
  346. }
  347. }
  348. /* Find all the fixed metadata in the filesystem. */
  349. static int ext4_getfsmap_find_fixed_metadata(struct super_block *sb,
  350. struct list_head *meta_list)
  351. {
  352. struct ext4_group_desc *gdp;
  353. ext4_group_t agno;
  354. int error;
  355. INIT_LIST_HEAD(meta_list);
  356. /* Collect everything. */
  357. for (agno = 0; agno < EXT4_SB(sb)->s_groups_count; agno++) {
  358. gdp = ext4_get_group_desc(sb, agno, NULL);
  359. if (!gdp) {
  360. error = -EFSCORRUPTED;
  361. goto err;
  362. }
  363. /* Superblock & GDT */
  364. error = ext4_getfsmap_find_sb(sb, agno, meta_list);
  365. if (error)
  366. goto err;
  367. /* Block bitmap */
  368. error = ext4_getfsmap_fill(meta_list,
  369. ext4_block_bitmap(sb, gdp), 1,
  370. EXT4_FMR_OWN_BLKBM);
  371. if (error)
  372. goto err;
  373. /* Inode bitmap */
  374. error = ext4_getfsmap_fill(meta_list,
  375. ext4_inode_bitmap(sb, gdp), 1,
  376. EXT4_FMR_OWN_INOBM);
  377. if (error)
  378. goto err;
  379. /* Inodes */
  380. error = ext4_getfsmap_fill(meta_list,
  381. ext4_inode_table(sb, gdp),
  382. EXT4_SB(sb)->s_itb_per_group,
  383. EXT4_FMR_OWN_INODES);
  384. if (error)
  385. goto err;
  386. }
  387. /* Sort the list */
  388. list_sort(NULL, meta_list, ext4_getfsmap_compare);
  389. /* Merge adjacent extents */
  390. ext4_getfsmap_merge_fixed_metadata(meta_list);
  391. return 0;
  392. err:
  393. ext4_getfsmap_free_fixed_metadata(meta_list);
  394. return error;
  395. }
  396. /* Execute a getfsmap query against the buddy bitmaps */
  397. static int ext4_getfsmap_datadev(struct super_block *sb,
  398. struct ext4_fsmap *keys,
  399. struct ext4_getfsmap_info *info)
  400. {
  401. struct ext4_sb_info *sbi = EXT4_SB(sb);
  402. ext4_fsblk_t start_fsb;
  403. ext4_fsblk_t end_fsb;
  404. ext4_fsblk_t bofs;
  405. ext4_fsblk_t eofs;
  406. ext4_group_t start_ag;
  407. ext4_group_t end_ag;
  408. ext4_grpblk_t first_cluster;
  409. ext4_grpblk_t last_cluster;
  410. int error = 0;
  411. bofs = le32_to_cpu(sbi->s_es->s_first_data_block);
  412. eofs = ext4_blocks_count(sbi->s_es);
  413. if (keys[0].fmr_physical >= eofs)
  414. return 0;
  415. else if (keys[0].fmr_physical < bofs)
  416. keys[0].fmr_physical = bofs;
  417. if (keys[1].fmr_physical >= eofs)
  418. keys[1].fmr_physical = eofs - 1;
  419. start_fsb = keys[0].fmr_physical;
  420. end_fsb = keys[1].fmr_physical;
  421. /* Determine first and last group to examine based on start and end */
  422. ext4_get_group_no_and_offset(sb, start_fsb, &start_ag, &first_cluster);
  423. ext4_get_group_no_and_offset(sb, end_fsb, &end_ag, &last_cluster);
  424. /*
  425. * Convert the fsmap low/high keys to bg based keys. Initialize
  426. * low to the fsmap low key and max out the high key to the end
  427. * of the bg.
  428. */
  429. info->gfi_low = keys[0];
  430. info->gfi_low.fmr_physical = EXT4_C2B(sbi, first_cluster);
  431. info->gfi_low.fmr_length = 0;
  432. memset(&info->gfi_high, 0xFF, sizeof(info->gfi_high));
  433. /* Assemble a list of all the fixed-location metadata. */
  434. error = ext4_getfsmap_find_fixed_metadata(sb, &info->gfi_meta_list);
  435. if (error)
  436. goto err;
  437. /* Query each bg */
  438. for (info->gfi_agno = start_ag;
  439. info->gfi_agno <= end_ag;
  440. info->gfi_agno++) {
  441. /*
  442. * Set the bg high key from the fsmap high key if this
  443. * is the last bg that we're querying.
  444. */
  445. if (info->gfi_agno == end_ag) {
  446. info->gfi_high = keys[1];
  447. info->gfi_high.fmr_physical = EXT4_C2B(sbi,
  448. last_cluster);
  449. info->gfi_high.fmr_length = 0;
  450. }
  451. trace_ext4_fsmap_low_key(sb, info->gfi_dev, info->gfi_agno,
  452. info->gfi_low.fmr_physical,
  453. info->gfi_low.fmr_length,
  454. info->gfi_low.fmr_owner);
  455. trace_ext4_fsmap_high_key(sb, info->gfi_dev, info->gfi_agno,
  456. info->gfi_high.fmr_physical,
  457. info->gfi_high.fmr_length,
  458. info->gfi_high.fmr_owner);
  459. error = ext4_mballoc_query_range(sb, info->gfi_agno,
  460. EXT4_B2C(sbi, info->gfi_low.fmr_physical),
  461. EXT4_B2C(sbi, info->gfi_high.fmr_physical),
  462. ext4_getfsmap_datadev_helper, info);
  463. if (error)
  464. goto err;
  465. /*
  466. * Set the bg low key to the start of the bg prior to
  467. * moving on to the next bg.
  468. */
  469. if (info->gfi_agno == start_ag)
  470. memset(&info->gfi_low, 0, sizeof(info->gfi_low));
  471. }
  472. /* Do we have a retained free extent? */
  473. if (info->gfi_lastfree.fmr_owner) {
  474. error = ext4_getfsmap_helper(sb, info, &info->gfi_lastfree);
  475. if (error)
  476. goto err;
  477. }
  478. /* Report any gaps at the end of the bg */
  479. info->gfi_last = true;
  480. error = ext4_getfsmap_datadev_helper(sb, end_ag, last_cluster, 0, info);
  481. if (error)
  482. goto err;
  483. err:
  484. ext4_getfsmap_free_fixed_metadata(&info->gfi_meta_list);
  485. return error;
  486. }
  487. /* Do we recognize the device? */
  488. static bool ext4_getfsmap_is_valid_device(struct super_block *sb,
  489. struct ext4_fsmap *fm)
  490. {
  491. if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
  492. fm->fmr_device == new_encode_dev(sb->s_bdev->bd_dev))
  493. return true;
  494. if (EXT4_SB(sb)->journal_bdev &&
  495. fm->fmr_device == new_encode_dev(EXT4_SB(sb)->journal_bdev->bd_dev))
  496. return true;
  497. return false;
  498. }
  499. /* Ensure that the low key is less than the high key. */
  500. static bool ext4_getfsmap_check_keys(struct ext4_fsmap *low_key,
  501. struct ext4_fsmap *high_key)
  502. {
  503. if (low_key->fmr_device > high_key->fmr_device)
  504. return false;
  505. if (low_key->fmr_device < high_key->fmr_device)
  506. return true;
  507. if (low_key->fmr_physical > high_key->fmr_physical)
  508. return false;
  509. if (low_key->fmr_physical < high_key->fmr_physical)
  510. return true;
  511. if (low_key->fmr_owner > high_key->fmr_owner)
  512. return false;
  513. if (low_key->fmr_owner < high_key->fmr_owner)
  514. return true;
  515. return false;
  516. }
  517. #define EXT4_GETFSMAP_DEVS 2
  518. /*
  519. * Get filesystem's extents as described in head, and format for
  520. * output. Calls formatter to fill the user's buffer until all
  521. * extents are mapped, until the passed-in head->fmh_count slots have
  522. * been filled, or until the formatter short-circuits the loop, if it
  523. * is tracking filled-in extents on its own.
  524. *
  525. * Key to Confusion
  526. * ----------------
  527. * There are multiple levels of keys and counters at work here:
  528. * _fsmap_head.fmh_keys -- low and high fsmap keys passed in;
  529. * these reflect fs-wide block addrs.
  530. * dkeys -- fmh_keys used to query each device;
  531. * these are fmh_keys but w/ the low key
  532. * bumped up by fmr_length.
  533. * _getfsmap_info.gfi_next_fsblk-- next fs block we expect to see; this
  534. * is how we detect gaps in the fsmap
  535. * records and report them.
  536. * _getfsmap_info.gfi_low/high -- per-bg low/high keys computed from
  537. * dkeys; used to query the free space.
  538. */
  539. int ext4_getfsmap(struct super_block *sb, struct ext4_fsmap_head *head,
  540. ext4_fsmap_format_t formatter, void *arg)
  541. {
  542. struct ext4_fsmap dkeys[2]; /* per-dev keys */
  543. struct ext4_getfsmap_dev handlers[EXT4_GETFSMAP_DEVS];
  544. struct ext4_getfsmap_info info = {0};
  545. int i;
  546. int error = 0;
  547. if (head->fmh_iflags & ~FMH_IF_VALID)
  548. return -EINVAL;
  549. if (!ext4_getfsmap_is_valid_device(sb, &head->fmh_keys[0]) ||
  550. !ext4_getfsmap_is_valid_device(sb, &head->fmh_keys[1]))
  551. return -EINVAL;
  552. head->fmh_entries = 0;
  553. /* Set up our device handlers. */
  554. memset(handlers, 0, sizeof(handlers));
  555. handlers[0].gfd_dev = new_encode_dev(sb->s_bdev->bd_dev);
  556. handlers[0].gfd_fn = ext4_getfsmap_datadev;
  557. if (EXT4_SB(sb)->journal_bdev) {
  558. handlers[1].gfd_dev = new_encode_dev(
  559. EXT4_SB(sb)->journal_bdev->bd_dev);
  560. handlers[1].gfd_fn = ext4_getfsmap_logdev;
  561. }
  562. sort(handlers, EXT4_GETFSMAP_DEVS, sizeof(struct ext4_getfsmap_dev),
  563. ext4_getfsmap_dev_compare, NULL);
  564. /*
  565. * To continue where we left off, we allow userspace to use the
  566. * last mapping from a previous call as the low key of the next.
  567. * This is identified by a non-zero length in the low key. We
  568. * have to increment the low key in this scenario to ensure we
  569. * don't return the same mapping again, and instead return the
  570. * very next mapping.
  571. *
  572. * Bump the physical offset as there can be no other mapping for
  573. * the same physical block range.
  574. */
  575. dkeys[0] = head->fmh_keys[0];
  576. dkeys[0].fmr_physical += dkeys[0].fmr_length;
  577. dkeys[0].fmr_owner = 0;
  578. dkeys[0].fmr_length = 0;
  579. memset(&dkeys[1], 0xFF, sizeof(struct ext4_fsmap));
  580. if (!ext4_getfsmap_check_keys(dkeys, &head->fmh_keys[1]))
  581. return -EINVAL;
  582. info.gfi_next_fsblk = head->fmh_keys[0].fmr_physical +
  583. head->fmh_keys[0].fmr_length;
  584. info.gfi_formatter = formatter;
  585. info.gfi_format_arg = arg;
  586. info.gfi_head = head;
  587. /* For each device we support... */
  588. for (i = 0; i < EXT4_GETFSMAP_DEVS; i++) {
  589. /* Is this device within the range the user asked for? */
  590. if (!handlers[i].gfd_fn)
  591. continue;
  592. if (head->fmh_keys[0].fmr_device > handlers[i].gfd_dev)
  593. continue;
  594. if (head->fmh_keys[1].fmr_device < handlers[i].gfd_dev)
  595. break;
  596. /*
  597. * If this device number matches the high key, we have
  598. * to pass the high key to the handler to limit the
  599. * query results. If the device number exceeds the
  600. * low key, zero out the low key so that we get
  601. * everything from the beginning.
  602. */
  603. if (handlers[i].gfd_dev == head->fmh_keys[1].fmr_device)
  604. dkeys[1] = head->fmh_keys[1];
  605. if (handlers[i].gfd_dev > head->fmh_keys[0].fmr_device)
  606. memset(&dkeys[0], 0, sizeof(struct ext4_fsmap));
  607. info.gfi_dev = handlers[i].gfd_dev;
  608. info.gfi_last = false;
  609. info.gfi_agno = -1;
  610. error = handlers[i].gfd_fn(sb, dkeys, &info);
  611. if (error)
  612. break;
  613. info.gfi_next_fsblk = 0;
  614. }
  615. head->fmh_oflags = FMH_OF_DEV_T;
  616. return error;
  617. }