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