LCOV - code coverage report
Current view: top level - fs/ext4 - fsmap.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 0 341 0.0 %
Date: 2021-04-22 12:43:58 Functions: 0 16 0.0 %

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

Generated by: LCOV version 1.14