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