fsmap.c 20 KB

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