1 ext4: support GETFSMAP ioctls
3 From: "Darrick J. Wong" <darrick.wong@oracle.com>
5 Support the GETFSMAP ioctls so that we can use the xfs free space
6 management tools to probe ext4 as well. Note that this is a partial
7 implementation -- we only report fixed-location metadata and free space;
8 everything else is reported as "unknown".
10 Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
11 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
13 v2: directly track the user array and index to fix some type checking
14 issues and avoid corrupting user memory when fmh_count == 0.
16 fs/ext4/Makefile | 10 -
17 fs/ext4/fsmap.c | 722 +++++++++++++++++++++++++++++++++++++++++++
18 fs/ext4/fsmap.h | 69 ++++
19 fs/ext4/ioctl.c | 90 +++++
20 fs/ext4/mballoc.c | 49 +++
21 fs/ext4/mballoc.h | 17 +
23 include/trace/events/ext4.h | 74 ++++
24 8 files changed, 1027 insertions(+), 5 deletions(-)
25 create mode 100644 fs/ext4/fsmap.c
26 create mode 100644 fs/ext4/fsmap.h
28 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
29 index d511ffb..961ce09 100644
30 --- a/fs/ext4/Makefile
31 +++ b/fs/ext4/Makefile
34 obj-$(CONFIG_EXT4_FS) += ext4.o
36 -ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
37 - ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
38 - ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o \
39 - mmp.o indirect.o extents_status.o xattr.o xattr_user.o \
40 - xattr_trusted.o inline.o readpage.o sysfs.o
41 +ext4-y := balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \
42 + extents_status.o file.o fsmap.o fsync.o hash.o ialloc.o \
43 + indirect.o inline.o inode.o ioctl.o mballoc.o migrate.o \
44 + mmp.o move_extent.o namei.o page-io.o readpage.o resize.o \
45 + super.o symlink.o sysfs.o xattr.o xattr_trusted.o xattr_user.o
47 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
48 ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
49 diff --git a/fs/ext4/fsmap.c b/fs/ext4/fsmap.c
51 index 0000000..b194360
56 + * Copyright (C) 2017 Oracle. All Rights Reserved.
58 + * Author: Darrick J. Wong <darrick.wong@oracle.com>
60 + * This program is free software; you can redistribute it and/or
61 + * modify it under the terms of the GNU General Public License
62 + * as published by the Free Software Foundation; either version 2
63 + * of the License, or (at your option) any later version.
65 + * This program is distributed in the hope that it would be useful,
66 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
67 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68 + * GNU General Public License for more details.
70 + * You should have received a copy of the GNU General Public License
71 + * along with this program; if not, write the Free Software Foundation,
72 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
75 +#include <linux/fsmap.h>
78 +#include <linux/sort.h>
79 +#include <linux/list_sort.h>
80 +#include <trace/events/ext4.h>
82 +/* Convert an ext4_fsmap to an fsmap. */
83 +void ext4_fsmap_from_internal(struct super_block *sb, struct fsmap *dest,
84 + struct ext4_fsmap *src)
86 + dest->fmr_device = src->fmr_device;
87 + dest->fmr_flags = src->fmr_flags;
88 + dest->fmr_physical = src->fmr_physical << sb->s_blocksize_bits;
89 + dest->fmr_owner = src->fmr_owner;
90 + dest->fmr_offset = 0;
91 + dest->fmr_length = src->fmr_length << sb->s_blocksize_bits;
92 + dest->fmr_reserved[0] = 0;
93 + dest->fmr_reserved[1] = 0;
94 + dest->fmr_reserved[2] = 0;
97 +/* Convert an fsmap to an ext4_fsmap. */
98 +void ext4_fsmap_to_internal(struct super_block *sb, struct ext4_fsmap *dest,
101 + dest->fmr_device = src->fmr_device;
102 + dest->fmr_flags = src->fmr_flags;
103 + dest->fmr_physical = src->fmr_physical >> sb->s_blocksize_bits;
104 + dest->fmr_owner = src->fmr_owner;
105 + dest->fmr_length = src->fmr_length >> sb->s_blocksize_bits;
108 +/* getfsmap query state */
109 +struct ext4_getfsmap_info {
110 + struct ext4_fsmap_head *gfi_head;
111 + ext4_fsmap_format_t gfi_formatter; /* formatting fn */
112 + void *gfi_format_arg;/* format buffer */
113 + ext4_fsblk_t gfi_next_fsblk; /* next fsblock we expect */
114 + u32 gfi_dev; /* device id */
115 + ext4_group_t gfi_agno; /* bg number, if applicable */
116 + struct ext4_fsmap gfi_low; /* low rmap key */
117 + struct ext4_fsmap gfi_high; /* high rmap key */
118 + struct ext4_fsmap gfi_lastfree; /* free ext at end of last bg */
119 + struct list_head gfi_meta_list; /* fixed metadata list */
120 + bool gfi_last; /* last extent? */
123 +/* Associate a device with a getfsmap handler. */
124 +struct ext4_getfsmap_dev {
125 + int (*gfd_fn)(struct super_block *sb,
126 + struct ext4_fsmap *keys,
127 + struct ext4_getfsmap_info *info);
131 +/* Compare two getfsmap device handlers. */
132 +static int ext4_getfsmap_dev_compare(const void *p1, const void *p2)
134 + const struct ext4_getfsmap_dev *d1 = p1;
135 + const struct ext4_getfsmap_dev *d2 = p2;
137 + return d1->gfd_dev - d2->gfd_dev;
140 +/* Compare a record against our starting point */
141 +static bool ext4_getfsmap_rec_before_low_key(struct ext4_getfsmap_info *info,
142 + struct ext4_fsmap *rec)
144 + return rec->fmr_physical < info->gfi_low.fmr_physical;
148 + * Format a reverse mapping for getfsmap, having translated rm_startblock
149 + * into the appropriate daddr units.
151 +static int ext4_getfsmap_helper(struct super_block *sb,
152 + struct ext4_getfsmap_info *info,
153 + struct ext4_fsmap *rec)
155 + struct ext4_fsmap fmr;
156 + struct ext4_sb_info *sbi = EXT4_SB(sb);
157 + ext4_fsblk_t rec_fsblk = rec->fmr_physical;
162 + if (fatal_signal_pending(current))
166 + * Filter out records that start before our startpoint, if the
167 + * caller requested that.
169 + if (ext4_getfsmap_rec_before_low_key(info, rec)) {
170 + rec_fsblk += rec->fmr_length;
171 + if (info->gfi_next_fsblk < rec_fsblk)
172 + info->gfi_next_fsblk = rec_fsblk;
173 + return EXT4_QUERY_RANGE_CONTINUE;
176 + /* Are we just counting mappings? */
177 + if (info->gfi_head->fmh_count == 0) {
178 + if (rec_fsblk > info->gfi_next_fsblk)
179 + info->gfi_head->fmh_entries++;
181 + if (info->gfi_last)
182 + return EXT4_QUERY_RANGE_CONTINUE;
184 + info->gfi_head->fmh_entries++;
186 + rec_fsblk += rec->fmr_length;
187 + if (info->gfi_next_fsblk < rec_fsblk)
188 + info->gfi_next_fsblk = rec_fsblk;
189 + return EXT4_QUERY_RANGE_CONTINUE;
193 + * If the record starts past the last physical block we saw,
194 + * then we've found a gap. Report the gap as being owned by
195 + * whatever the caller specified is the missing owner.
197 + if (rec_fsblk > info->gfi_next_fsblk) {
198 + if (info->gfi_head->fmh_entries >= info->gfi_head->fmh_count)
199 + return EXT4_QUERY_RANGE_ABORT;
201 + ext4_get_group_no_and_offset(sb, info->gfi_next_fsblk,
203 + trace_ext4_fsmap_mapping(sb, info->gfi_dev, agno,
204 + EXT4_C2B(sbi, cno),
205 + rec_fsblk - info->gfi_next_fsblk,
206 + EXT4_FMR_OWN_UNKNOWN);
208 + fmr.fmr_device = info->gfi_dev;
209 + fmr.fmr_physical = info->gfi_next_fsblk;
210 + fmr.fmr_owner = EXT4_FMR_OWN_UNKNOWN;
211 + fmr.fmr_length = rec_fsblk - info->gfi_next_fsblk;
212 + fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
213 + error = info->gfi_formatter(&fmr, info->gfi_format_arg);
216 + info->gfi_head->fmh_entries++;
219 + if (info->gfi_last)
222 + /* Fill out the extent we found */
223 + if (info->gfi_head->fmh_entries >= info->gfi_head->fmh_count)
224 + return EXT4_QUERY_RANGE_ABORT;
226 + ext4_get_group_no_and_offset(sb, rec_fsblk, &agno, &cno);
227 + trace_ext4_fsmap_mapping(sb, info->gfi_dev, agno, EXT4_C2B(sbi, cno),
228 + rec->fmr_length, rec->fmr_owner);
230 + fmr.fmr_device = info->gfi_dev;
231 + fmr.fmr_physical = rec_fsblk;
232 + fmr.fmr_owner = rec->fmr_owner;
233 + fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
234 + fmr.fmr_length = rec->fmr_length;
235 + error = info->gfi_formatter(&fmr, info->gfi_format_arg);
238 + info->gfi_head->fmh_entries++;
241 + rec_fsblk += rec->fmr_length;
242 + if (info->gfi_next_fsblk < rec_fsblk)
243 + info->gfi_next_fsblk = rec_fsblk;
244 + return EXT4_QUERY_RANGE_CONTINUE;
247 +static inline ext4_fsblk_t ext4_fsmap_next_pblk(struct ext4_fsmap *fmr)
249 + return fmr->fmr_physical + fmr->fmr_length;
252 +/* Transform a blockgroup's free record into a fsmap */
253 +static int ext4_getfsmap_datadev_helper(struct super_block *sb,
254 + ext4_group_t agno, ext4_grpblk_t start,
255 + ext4_grpblk_t len, void *priv)
257 + struct ext4_fsmap irec;
258 + struct ext4_getfsmap_info *info = priv;
259 + struct ext4_fsmap *p;
260 + struct ext4_fsmap *tmp;
261 + struct ext4_sb_info *sbi = EXT4_SB(sb);
263 + ext4_fsblk_t fslen;
266 + fsb = (EXT4_C2B(sbi, start) + ext4_group_first_block_no(sb, agno));
267 + fslen = EXT4_C2B(sbi, len);
269 + /* If the retained free extent record is set... */
270 + if (info->gfi_lastfree.fmr_owner) {
271 + /* ...and abuts this one, lengthen it and return. */
272 + if (ext4_fsmap_next_pblk(&info->gfi_lastfree) == fsb) {
273 + info->gfi_lastfree.fmr_length += fslen;
278 + * There's a gap between the two free extents; emit the
279 + * retained extent prior to merging the meta_list.
281 + error = ext4_getfsmap_helper(sb, info, &info->gfi_lastfree);
284 + info->gfi_lastfree.fmr_owner = 0;
287 + /* Merge in any relevant extents from the meta_list */
288 + list_for_each_entry_safe(p, tmp, &info->gfi_meta_list, fmr_list) {
289 + if (p->fmr_physical + p->fmr_length <= info->gfi_next_fsblk) {
290 + list_del(&p->fmr_list);
292 + } else if (p->fmr_physical < fsb) {
293 + error = ext4_getfsmap_helper(sb, info, p);
297 + list_del(&p->fmr_list);
302 + irec.fmr_device = 0;
303 + irec.fmr_physical = fsb;
304 + irec.fmr_length = fslen;
305 + irec.fmr_owner = EXT4_FMR_OWN_FREE;
306 + irec.fmr_flags = 0;
308 + /* If this is a free extent at the end of a bg, buffer it. */
309 + if (ext4_fsmap_next_pblk(&irec) ==
310 + ext4_group_first_block_no(sb, agno + 1)) {
311 + info->gfi_lastfree = irec;
315 + /* Otherwise, emit it */
316 + return ext4_getfsmap_helper(sb, info, &irec);
319 +/* Execute a getfsmap query against the log device. */
320 +static int ext4_getfsmap_logdev(struct super_block *sb, struct ext4_fsmap *keys,
321 + struct ext4_getfsmap_info *info)
323 + journal_t *journal = EXT4_SB(sb)->s_journal;
324 + struct ext4_fsmap irec;
326 + /* Set up search keys */
327 + info->gfi_low = keys[0];
328 + info->gfi_low.fmr_length = 0;
330 + memset(&info->gfi_high, 0xFF, sizeof(info->gfi_high));
332 + trace_ext4_fsmap_low_key(sb, info->gfi_dev, 0,
333 + info->gfi_low.fmr_physical,
334 + info->gfi_low.fmr_length,
335 + info->gfi_low.fmr_owner);
337 + trace_ext4_fsmap_high_key(sb, info->gfi_dev, 0,
338 + info->gfi_high.fmr_physical,
339 + info->gfi_high.fmr_length,
340 + info->gfi_high.fmr_owner);
342 + if (keys[0].fmr_physical > 0)
345 + /* Fabricate an rmap entry for the external log device. */
346 + irec.fmr_physical = journal->j_blk_offset;
347 + irec.fmr_length = journal->j_maxlen;
348 + irec.fmr_owner = EXT4_FMR_OWN_LOG;
349 + irec.fmr_flags = 0;
351 + return ext4_getfsmap_helper(sb, info, &irec);
354 +/* Helper to fill out an ext4_fsmap. */
355 +static inline int ext4_getfsmap_fill(struct list_head *meta_list,
356 + ext4_fsblk_t fsb, ext4_fsblk_t len,
359 + struct ext4_fsmap *fsm;
361 + fsm = kmalloc(sizeof(*fsm), GFP_NOFS);
364 + fsm->fmr_device = 0;
365 + fsm->fmr_flags = 0;
366 + fsm->fmr_physical = fsb;
367 + fsm->fmr_owner = owner;
368 + fsm->fmr_length = len;
369 + list_add_tail(&fsm->fmr_list, meta_list);
375 + * This function returns the number of file system metadata blocks at
376 + * the beginning of a block group, including the reserved gdt blocks.
378 +static unsigned int ext4_getfsmap_find_sb(struct super_block *sb,
380 + struct list_head *meta_list)
382 + struct ext4_sb_info *sbi = EXT4_SB(sb);
383 + ext4_fsblk_t fsb = ext4_group_first_block_no(sb, agno);
385 + unsigned long first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
386 + unsigned long metagroup = agno / EXT4_DESC_PER_BLOCK(sb);
389 + /* Record the superblock. */
390 + if (ext4_bg_has_super(sb, agno)) {
391 + error = ext4_getfsmap_fill(meta_list, fsb, 1, EXT4_FMR_OWN_FS);
397 + /* Record the group descriptors. */
398 + len = ext4_bg_num_gdb(sb, agno);
401 + error = ext4_getfsmap_fill(meta_list, fsb, len,
407 + /* Reserved GDT blocks */
408 + if (!ext4_has_feature_meta_bg(sb) || metagroup < first_meta_bg) {
409 + len = le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
410 + error = ext4_getfsmap_fill(meta_list, fsb, len,
411 + EXT4_FMR_OWN_RESV_GDT);
419 +/* Compare two fsmap items. */
420 +static int ext4_getfsmap_compare(void *priv,
421 + struct list_head *a,
422 + struct list_head *b)
424 + struct ext4_fsmap *fa;
425 + struct ext4_fsmap *fb;
427 + fa = container_of(a, struct ext4_fsmap, fmr_list);
428 + fb = container_of(b, struct ext4_fsmap, fmr_list);
429 + if (fa->fmr_physical < fb->fmr_physical)
431 + else if (fa->fmr_physical > fb->fmr_physical)
436 +/* Merge adjacent extents of fixed metadata. */
437 +static void ext4_getfsmap_merge_fixed_metadata(struct list_head *meta_list)
439 + struct ext4_fsmap *p;
440 + struct ext4_fsmap *prev = NULL;
441 + struct ext4_fsmap *tmp;
443 + list_for_each_entry_safe(p, tmp, meta_list, fmr_list) {
449 + if (prev->fmr_owner == p->fmr_owner &&
450 + prev->fmr_physical + prev->fmr_length == p->fmr_physical) {
451 + prev->fmr_length += p->fmr_length;
452 + list_del(&p->fmr_list);
459 +/* Free a list of fixed metadata. */
460 +static void ext4_getfsmap_free_fixed_metadata(struct list_head *meta_list)
462 + struct ext4_fsmap *p;
463 + struct ext4_fsmap *tmp;
465 + list_for_each_entry_safe(p, tmp, meta_list, fmr_list) {
466 + list_del(&p->fmr_list);
471 +/* Find all the fixed metadata in the filesystem. */
472 +int ext4_getfsmap_find_fixed_metadata(struct super_block *sb,
473 + struct list_head *meta_list)
475 + struct ext4_group_desc *gdp;
479 + INIT_LIST_HEAD(meta_list);
481 + /* Collect everything. */
482 + for (agno = 0; agno < EXT4_SB(sb)->s_groups_count; agno++) {
483 + gdp = ext4_get_group_desc(sb, agno, NULL);
485 + error = -EFSCORRUPTED;
489 + /* Superblock & GDT */
490 + error = ext4_getfsmap_find_sb(sb, agno, meta_list);
495 + error = ext4_getfsmap_fill(meta_list,
496 + ext4_block_bitmap(sb, gdp), 1,
497 + EXT4_FMR_OWN_BLKBM);
502 + error = ext4_getfsmap_fill(meta_list,
503 + ext4_inode_bitmap(sb, gdp), 1,
504 + EXT4_FMR_OWN_INOBM);
509 + error = ext4_getfsmap_fill(meta_list,
510 + ext4_inode_table(sb, gdp),
511 + EXT4_SB(sb)->s_itb_per_group,
512 + EXT4_FMR_OWN_INODES);
517 + /* Sort the list */
518 + list_sort(NULL, meta_list, ext4_getfsmap_compare);
520 + /* Merge adjacent extents */
521 + ext4_getfsmap_merge_fixed_metadata(meta_list);
525 + ext4_getfsmap_free_fixed_metadata(meta_list);
529 +/* Execute a getfsmap query against the buddy bitmaps */
530 +static int ext4_getfsmap_datadev(struct super_block *sb,
531 + struct ext4_fsmap *keys,
532 + struct ext4_getfsmap_info *info)
534 + struct ext4_sb_info *sbi = EXT4_SB(sb);
535 + ext4_fsblk_t start_fsb;
536 + ext4_fsblk_t end_fsb;
538 + ext4_group_t start_ag;
539 + ext4_group_t end_ag;
540 + ext4_grpblk_t first_cluster;
541 + ext4_grpblk_t last_cluster;
544 + eofs = ext4_blocks_count(sbi->s_es);
545 + if (keys[0].fmr_physical >= eofs)
547 + if (keys[1].fmr_physical >= eofs)
548 + keys[1].fmr_physical = eofs - 1;
549 + start_fsb = keys[0].fmr_physical;
550 + end_fsb = keys[1].fmr_physical;
552 + /* Determine first and last group to examine based on start and end */
553 + ext4_get_group_no_and_offset(sb, start_fsb, &start_ag, &first_cluster);
554 + ext4_get_group_no_and_offset(sb, end_fsb, &end_ag, &last_cluster);
557 + * Convert the fsmap low/high keys to bg based keys. Initialize
558 + * low to the fsmap low key and max out the high key to the end
561 + info->gfi_low = keys[0];
562 + info->gfi_low.fmr_physical = EXT4_C2B(sbi, first_cluster);
563 + info->gfi_low.fmr_length = 0;
565 + memset(&info->gfi_high, 0xFF, sizeof(info->gfi_high));
567 + /* Assemble a list of all the fixed-location metadata. */
568 + error = ext4_getfsmap_find_fixed_metadata(sb, &info->gfi_meta_list);
572 + /* Query each bg */
573 + for (info->gfi_agno = start_ag;
574 + info->gfi_agno <= end_ag;
575 + info->gfi_agno++) {
577 + * Set the bg high key from the fsmap high key if this
578 + * is the last bg that we're querying.
580 + if (info->gfi_agno == end_ag) {
581 + info->gfi_high = keys[1];
582 + info->gfi_high.fmr_physical = EXT4_C2B(sbi,
584 + info->gfi_high.fmr_length = 0;
587 + trace_ext4_fsmap_low_key(sb, info->gfi_dev, info->gfi_agno,
588 + info->gfi_low.fmr_physical,
589 + info->gfi_low.fmr_length,
590 + info->gfi_low.fmr_owner);
592 + trace_ext4_fsmap_high_key(sb, info->gfi_dev, info->gfi_agno,
593 + info->gfi_high.fmr_physical,
594 + info->gfi_high.fmr_length,
595 + info->gfi_high.fmr_owner);
597 + error = ext4_mballoc_query_range(sb, info->gfi_agno,
598 + EXT4_B2C(sbi, info->gfi_low.fmr_physical),
599 + EXT4_B2C(sbi, info->gfi_high.fmr_physical),
600 + ext4_getfsmap_datadev_helper, info);
605 + * Set the bg low key to the start of the bg prior to
606 + * moving on to the next bg.
608 + if (info->gfi_agno == start_ag)
609 + memset(&info->gfi_low, 0, sizeof(info->gfi_low));
612 + /* Do we have a retained free extent? */
613 + if (info->gfi_lastfree.fmr_owner) {
614 + error = ext4_getfsmap_helper(sb, info, &info->gfi_lastfree);
619 + /* Report any gaps at the end of the bg */
620 + info->gfi_last = true;
621 + error = ext4_getfsmap_datadev_helper(sb, end_ag, last_cluster, 0, info);
626 + ext4_getfsmap_free_fixed_metadata(&info->gfi_meta_list);
630 +/* Do we recognize the device? */
631 +static bool ext4_getfsmap_is_valid_device(struct super_block *sb,
632 + struct ext4_fsmap *fm)
634 + if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
635 + fm->fmr_device == new_encode_dev(sb->s_bdev->bd_dev))
637 + if (EXT4_SB(sb)->journal_bdev &&
638 + fm->fmr_device == new_encode_dev(EXT4_SB(sb)->journal_bdev->bd_dev))
643 +/* Ensure that the low key is less than the high key. */
644 +static bool ext4_getfsmap_check_keys(struct ext4_fsmap *low_key,
645 + struct ext4_fsmap *high_key)
647 + if (low_key->fmr_device > high_key->fmr_device)
649 + if (low_key->fmr_device < high_key->fmr_device)
652 + if (low_key->fmr_physical > high_key->fmr_physical)
654 + if (low_key->fmr_physical < high_key->fmr_physical)
657 + if (low_key->fmr_owner > high_key->fmr_owner)
659 + if (low_key->fmr_owner < high_key->fmr_owner)
665 +#define EXT4_GETFSMAP_DEVS 2
667 + * Get filesystem's extents as described in head, and format for
668 + * output. Calls formatter to fill the user's buffer until all
669 + * extents are mapped, until the passed-in head->fmh_count slots have
670 + * been filled, or until the formatter short-circuits the loop, if it
671 + * is tracking filled-in extents on its own.
675 + * There are multiple levels of keys and counters at work here:
676 + * _fsmap_head.fmh_keys -- low and high fsmap keys passed in;
677 + * these reflect fs-wide block addrs.
678 + * dkeys -- fmh_keys used to query each device;
679 + * these are fmh_keys but w/ the low key
680 + * bumped up by fmr_length.
681 + * _getfsmap_info.gfi_next_fsblk-- next fs block we expect to see; this
682 + * is how we detect gaps in the fsmap
683 + * records and report them.
684 + * _getfsmap_info.gfi_low/high -- per-bg low/high keys computed from
685 + * dkeys; used to query the free space.
687 +int ext4_getfsmap(struct super_block *sb, struct ext4_fsmap_head *head,
688 + ext4_fsmap_format_t formatter, void *arg)
690 + struct ext4_fsmap dkeys[2]; /* per-dev keys */
691 + struct ext4_getfsmap_dev handlers[EXT4_GETFSMAP_DEVS];
692 + struct ext4_getfsmap_info info = {0};
696 + if (head->fmh_iflags & ~FMH_IF_VALID)
698 + if (!ext4_getfsmap_is_valid_device(sb, &head->fmh_keys[0]) ||
699 + !ext4_getfsmap_is_valid_device(sb, &head->fmh_keys[1]))
702 + head->fmh_entries = 0;
704 + /* Set up our device handlers. */
705 + memset(handlers, 0, sizeof(handlers));
706 + handlers[0].gfd_dev = new_encode_dev(sb->s_bdev->bd_dev);
707 + handlers[0].gfd_fn = ext4_getfsmap_datadev;
708 + if (EXT4_SB(sb)->journal_bdev) {
709 + handlers[1].gfd_dev = new_encode_dev(
710 + EXT4_SB(sb)->journal_bdev->bd_dev);
711 + handlers[1].gfd_fn = ext4_getfsmap_logdev;
714 + sort(handlers, EXT4_GETFSMAP_DEVS, sizeof(struct ext4_getfsmap_dev),
715 + ext4_getfsmap_dev_compare, NULL);
718 + * To continue where we left off, we allow userspace to use the
719 + * last mapping from a previous call as the low key of the next.
720 + * This is identified by a non-zero length in the low key. We
721 + * have to increment the low key in this scenario to ensure we
722 + * don't return the same mapping again, and instead return the
723 + * very next mapping.
725 + * Bump the physical offset as there can be no other mapping for
726 + * the same physical block range.
728 + dkeys[0] = head->fmh_keys[0];
729 + dkeys[0].fmr_physical += dkeys[0].fmr_length;
730 + dkeys[0].fmr_owner = 0;
731 + dkeys[0].fmr_length = 0;
732 + memset(&dkeys[1], 0xFF, sizeof(struct ext4_fsmap));
734 + if (!ext4_getfsmap_check_keys(dkeys, &head->fmh_keys[1]))
737 + info.gfi_next_fsblk = head->fmh_keys[0].fmr_physical +
738 + head->fmh_keys[0].fmr_length;
739 + info.gfi_formatter = formatter;
740 + info.gfi_format_arg = arg;
741 + info.gfi_head = head;
743 + /* For each device we support... */
744 + for (i = 0; i < EXT4_GETFSMAP_DEVS; i++) {
745 + /* Is this device within the range the user asked for? */
746 + if (!handlers[i].gfd_fn)
748 + if (head->fmh_keys[0].fmr_device > handlers[i].gfd_dev)
750 + if (head->fmh_keys[1].fmr_device < handlers[i].gfd_dev)
754 + * If this device number matches the high key, we have
755 + * to pass the high key to the handler to limit the
756 + * query results. If the device number exceeds the
757 + * low key, zero out the low key so that we get
758 + * everything from the beginning.
760 + if (handlers[i].gfd_dev == head->fmh_keys[1].fmr_device)
761 + dkeys[1] = head->fmh_keys[1];
762 + if (handlers[i].gfd_dev > head->fmh_keys[0].fmr_device)
763 + memset(&dkeys[0], 0, sizeof(struct ext4_fsmap));
765 + info.gfi_dev = handlers[i].gfd_dev;
766 + info.gfi_last = false;
767 + info.gfi_agno = -1;
768 + error = handlers[i].gfd_fn(sb, dkeys, &info);
771 + info.gfi_next_fsblk = 0;
774 + head->fmh_oflags = FMH_OF_DEV_T;
777 diff --git a/fs/ext4/fsmap.h b/fs/ext4/fsmap.h
779 index 0000000..9a2cd36
781 +++ b/fs/ext4/fsmap.h
784 + * Copyright (C) 2017 Oracle. All Rights Reserved.
786 + * Author: Darrick J. Wong <darrick.wong@oracle.com>
788 + * This program is free software; you can redistribute it and/or
789 + * modify it under the terms of the GNU General Public License
790 + * as published by the Free Software Foundation; either version 2
791 + * of the License, or (at your option) any later version.
793 + * This program is distributed in the hope that it would be useful,
794 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
795 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
796 + * GNU General Public License for more details.
798 + * You should have received a copy of the GNU General Public License
799 + * along with this program; if not, write the Free Software Foundation,
800 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
802 +#ifndef __EXT4_FSMAP_H__
803 +#define __EXT4_FSMAP_H__
807 +/* internal fsmap representation */
809 + struct list_head fmr_list;
810 + dev_t fmr_device; /* device id */
811 + uint32_t fmr_flags; /* mapping flags */
812 + uint64_t fmr_physical; /* device offset of segment */
813 + uint64_t fmr_owner; /* owner id */
814 + uint64_t fmr_length; /* length of segment, blocks */
817 +struct ext4_fsmap_head {
818 + uint32_t fmh_iflags; /* control flags */
819 + uint32_t fmh_oflags; /* output flags */
820 + unsigned int fmh_count; /* # of entries in array incl. input */
821 + unsigned int fmh_entries; /* # of entries filled in (output). */
823 + struct ext4_fsmap fmh_keys[2]; /* low and high keys */
826 +void ext4_fsmap_from_internal(struct super_block *sb, struct fsmap *dest,
827 + struct ext4_fsmap *src);
828 +void ext4_fsmap_to_internal(struct super_block *sb, struct ext4_fsmap *dest,
829 + struct fsmap *src);
831 +/* fsmap to userspace formatter - copy to user & advance pointer */
832 +typedef int (*ext4_fsmap_format_t)(struct ext4_fsmap *, void *);
834 +int ext4_getfsmap(struct super_block *sb, struct ext4_fsmap_head *head,
835 + ext4_fsmap_format_t formatter, void *arg);
837 +#define EXT4_QUERY_RANGE_ABORT 1
838 +#define EXT4_QUERY_RANGE_CONTINUE 0
840 +/* fmr_owner special values for FS_IOC_GETFSMAP; some share w/ XFS */
841 +#define EXT4_FMR_OWN_FREE FMR_OWN_FREE /* free space */
842 +#define EXT4_FMR_OWN_UNKNOWN FMR_OWN_UNKNOWN /* unknown owner */
843 +#define EXT4_FMR_OWN_FS FMR_OWNER('X', 1) /* static fs metadata */
844 +#define EXT4_FMR_OWN_LOG FMR_OWNER('X', 2) /* journalling log */
845 +#define EXT4_FMR_OWN_INODES FMR_OWNER('X', 5) /* inodes */
846 +#define EXT4_FMR_OWN_GDT FMR_OWNER('f', 1) /* group descriptors */
847 +#define EXT4_FMR_OWN_RESV_GDT FMR_OWNER('f', 2) /* reserved gdt blocks */
848 +#define EXT4_FMR_OWN_BLKBM FMR_OWNER('f', 3) /* inode bitmap */
849 +#define EXT4_FMR_OWN_INOBM FMR_OWNER('f', 4) /* block bitmap */
851 +#endif /* __EXT4_FSMAP_H__ */
852 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
853 index a4273dd..dfcb815 100644
854 --- a/fs/ext4/ioctl.c
855 +++ b/fs/ext4/ioctl.c
857 #include <linux/delay.h>
858 #include "ext4_jbd2.h"
860 +#include <linux/fsmap.h>
862 +#include <trace/events/ext4.h>
865 * Swap memory between @a and @b for @len bytes.
866 @@ -489,6 +492,90 @@ int ext4_shutdown(struct super_block *sb, unsigned long arg)
870 +struct getfsmap_info {
871 + struct super_block *gi_sb;
872 + struct fsmap_head __user *gi_data;
873 + unsigned int gi_idx;
874 + __u32 gi_last_flags;
877 +static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
879 + struct getfsmap_info *info = priv;
882 + trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
884 + info->gi_last_flags = xfm->fmr_flags;
885 + ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
886 + if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
887 + sizeof(struct fsmap)))
893 +static int ext4_ioc_getfsmap(struct super_block *sb,
894 + struct fsmap_head __user *arg)
896 + struct getfsmap_info info = {0};
897 + struct ext4_fsmap_head xhead = {0};
898 + struct fsmap_head head;
899 + bool aborted = false;
902 + if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
904 + if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
905 + memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
906 + sizeof(head.fmh_keys[0].fmr_reserved)) ||
907 + memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
908 + sizeof(head.fmh_keys[1].fmr_reserved)))
911 + * ext4 doesn't report file extents at all, so the only valid
912 + * file offsets are the magic ones (all zeroes or all ones).
914 + if (head.fmh_keys[0].fmr_offset ||
915 + (head.fmh_keys[1].fmr_offset != 0 &&
916 + head.fmh_keys[1].fmr_offset != -1ULL))
919 + xhead.fmh_iflags = head.fmh_iflags;
920 + xhead.fmh_count = head.fmh_count;
921 + ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
922 + ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
924 + trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
925 + trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
928 + info.gi_data = arg;
929 + error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
930 + if (error == EXT4_QUERY_RANGE_ABORT) {
936 + /* If we didn't abort, set the "last" flag in the last fmx */
937 + if (!aborted && info.gi_idx) {
938 + info.gi_last_flags |= FMR_OF_LAST;
939 + if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
940 + &info.gi_last_flags,
941 + sizeof(info.gi_last_flags)))
945 + /* copy back header */
946 + head.fmh_entries = xhead.fmh_entries;
947 + head.fmh_oflags = xhead.fmh_oflags;
948 + if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
954 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
956 struct inode *inode = file_inode(filp);
957 @@ -499,6 +586,8 @@ long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
958 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
961 + case FS_IOC_GETFSMAP:
962 + return ext4_ioc_getfsmap(sb, (void __user *)arg);
963 case EXT4_IOC_GETFLAGS:
964 ext4_get_inode_flags(ei);
965 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
966 @@ -1009,6 +1098,7 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
967 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
968 case EXT4_IOC_GET_ENCRYPTION_POLICY:
969 case EXT4_IOC_SHUTDOWN:
970 + case FS_IOC_GETFSMAP:
974 diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
975 index 354dc1a..9e0147a 100644
976 --- a/fs/ext4/mballoc.c
977 +++ b/fs/ext4/mballoc.c
978 @@ -5277,3 +5277,52 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
979 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
983 +/* Iterate all the free extents in the group. */
985 +ext4_mballoc_query_range(
986 + struct super_block *sb,
987 + ext4_group_t group,
988 + ext4_grpblk_t start,
990 + ext4_mballoc_query_range_fn formatter,
994 + ext4_grpblk_t next;
995 + struct ext4_buddy e4b;
998 + error = ext4_mb_load_buddy(sb, group, &e4b);
1001 + bitmap = e4b.bd_bitmap;
1003 + ext4_lock_group(sb, group);
1005 + start = (e4b.bd_info->bb_first_free > start) ?
1006 + e4b.bd_info->bb_first_free : start;
1007 + if (end >= EXT4_CLUSTERS_PER_GROUP(sb))
1008 + end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
1010 + while (start <= end) {
1011 + start = mb_find_next_zero_bit(bitmap, end + 1, start);
1014 + next = mb_find_next_bit(bitmap, end + 1, start);
1016 + ext4_unlock_group(sb, group);
1017 + error = formatter(sb, group, start, next - start, priv);
1020 + ext4_lock_group(sb, group);
1025 + ext4_unlock_group(sb, group);
1027 + ext4_mb_unload_buddy(&e4b);
1031 diff --git a/fs/ext4/mballoc.h b/fs/ext4/mballoc.h
1032 index 1aba469..2bed620 100644
1033 --- a/fs/ext4/mballoc.h
1034 +++ b/fs/ext4/mballoc.h
1035 @@ -199,4 +199,21 @@ static inline ext4_fsblk_t ext4_grp_offs_to_block(struct super_block *sb,
1036 return ext4_group_first_block_no(sb, fex->fe_group) +
1037 (fex->fe_start << EXT4_SB(sb)->s_cluster_bits);
1040 +typedef int (*ext4_mballoc_query_range_fn)(
1041 + struct super_block *sb,
1042 + ext4_group_t agno,
1043 + ext4_grpblk_t start,
1044 + ext4_grpblk_t len,
1048 +ext4_mballoc_query_range(
1049 + struct super_block *sb,
1050 + ext4_group_t agno,
1051 + ext4_grpblk_t start,
1052 + ext4_grpblk_t end,
1053 + ext4_mballoc_query_range_fn formatter,
1057 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
1058 index a9448db..353b03b 100644
1059 --- a/fs/ext4/super.c
1060 +++ b/fs/ext4/super.c
1064 #include "mballoc.h"
1067 #define CREATE_TRACE_POINTS
1068 #include <trace/events/ext4.h>
1069 diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
1070 index 09c71e9..dfae175 100644
1071 --- a/include/trace/events/ext4.h
1072 +++ b/include/trace/events/ext4.h
1073 @@ -15,6 +15,7 @@ struct ext4_inode_info;
1074 struct mpage_da_data;
1075 struct ext4_map_blocks;
1076 struct extent_status;
1079 #define EXT4_I(inode) (container_of(inode, struct ext4_inode_info, vfs_inode))
1081 @@ -2529,6 +2530,79 @@ TRACE_EVENT(ext4_es_shrink,
1082 __entry->scan_time, __entry->nr_skipped, __entry->retried)
1086 +DECLARE_EVENT_CLASS(ext4_fsmap_class,
1087 + TP_PROTO(struct super_block *sb, u32 keydev, u32 agno, u64 bno, u64 len,
1089 + TP_ARGS(sb, keydev, agno, bno, len, owner),
1091 + __field(dev_t, dev)
1092 + __field(dev_t, keydev)
1093 + __field(u32, agno)
1096 + __field(u64, owner)
1099 + __entry->dev = sb->s_bdev->bd_dev;
1100 + __entry->keydev = new_decode_dev(keydev);
1101 + __entry->agno = agno;
1102 + __entry->bno = bno;
1103 + __entry->len = len;
1104 + __entry->owner = owner;
1106 + TP_printk("dev %d:%d keydev %d:%d agno %u bno %llu len %llu owner %lld\n",
1107 + MAJOR(__entry->dev), MINOR(__entry->dev),
1108 + MAJOR(__entry->keydev), MINOR(__entry->keydev),
1114 +#define DEFINE_FSMAP_EVENT(name) \
1115 +DEFINE_EVENT(ext4_fsmap_class, name, \
1116 + TP_PROTO(struct super_block *sb, u32 keydev, u32 agno, u64 bno, u64 len, \
1118 + TP_ARGS(sb, keydev, agno, bno, len, owner))
1119 +DEFINE_FSMAP_EVENT(ext4_fsmap_low_key);
1120 +DEFINE_FSMAP_EVENT(ext4_fsmap_high_key);
1121 +DEFINE_FSMAP_EVENT(ext4_fsmap_mapping);
1123 +DECLARE_EVENT_CLASS(ext4_getfsmap_class,
1124 + TP_PROTO(struct super_block *sb, struct ext4_fsmap *fsmap),
1125 + TP_ARGS(sb, fsmap),
1127 + __field(dev_t, dev)
1128 + __field(dev_t, keydev)
1129 + __field(u64, block)
1131 + __field(u64, owner)
1132 + __field(u64, flags)
1135 + __entry->dev = sb->s_bdev->bd_dev;
1136 + __entry->keydev = new_decode_dev(fsmap->fmr_device);
1137 + __entry->block = fsmap->fmr_physical;
1138 + __entry->len = fsmap->fmr_length;
1139 + __entry->owner = fsmap->fmr_owner;
1140 + __entry->flags = fsmap->fmr_flags;
1142 + TP_printk("dev %d:%d keydev %d:%d block %llu len %llu owner %lld flags 0x%llx\n",
1143 + MAJOR(__entry->dev), MINOR(__entry->dev),
1144 + MAJOR(__entry->keydev), MINOR(__entry->keydev),
1150 +#define DEFINE_GETFSMAP_EVENT(name) \
1151 +DEFINE_EVENT(ext4_getfsmap_class, name, \
1152 + TP_PROTO(struct super_block *sb, struct ext4_fsmap *fsmap), \
1153 + TP_ARGS(sb, fsmap))
1154 +DEFINE_GETFSMAP_EVENT(ext4_getfsmap_low_key);
1155 +DEFINE_GETFSMAP_EVENT(ext4_getfsmap_high_key);
1156 +DEFINE_GETFSMAP_EVENT(ext4_getfsmap_mapping);
1158 #endif /* _TRACE_EXT4_H */
1160 /* This part must be outside protection */