Btrfs: scrub: print path to corrupted files and trigger nodatasum fixup
v3 cover Btrfs: scrub: print path to corrupted files and trigger nodatasum fixup
This patch set introduces two new features for scrub. They share the backref iteration code which is the reason they made it into the same patch set.
The first feature adds printk statements in case scrub finds an error which list all affected files. You will need patch 1, 2 and 3 for that.
The second feature adds the trigger which enables us to correct i/o errors in case the affected extent does not have a checksum (nodatasum), eventually. You will need patch 1, 4, 5 and 6 for that.
I tried to apply all patches to the current cmason/for-linus branch and to Arne's current for-chris branch. They do apply with no errors (some offsets possible).
Please review.
Next I'm starting to make up my mind how to implement on-the-fly error correction correctly. This will enable us to rewrite good data whenever we encounter a bad copy. I have some preliminary patches already, the stress in the first sentence is on "correctly". The second feature mentioned in this patch series will then automatically use that code, too.
Changelog v1->v2:
- Various cleanup, sensible error codes as suggested by David Sterba
Changelog v2->v3:
- evaluation and iteration of shared refs
- support for in-tree refs (v2 iterated inline refs only)
- never call an interator function without releasing the path
- iterate_irefs now returns -ENOENT in case no refs are found
- some stupid bugs removed where release_path was called too early
- ioctls added to provide new functions to user mode
- bugfixes for cases where search_slot found the very end of a leaf
- bugfix: use right fs root for readpage instead of fs_root->fs_info
- based on current cmason/for-linus
A patch series to use the new ioctls from usermode will follow shortly. Please try it and report errors (or confirm there are none, of course).
-Jan
Jan Schmidt (8):
added helper functions to iterate backrefs
scrub: added unverified_errors
scrub: print paths of corrupted files
scrub: bugfix: mirror_num off by one
add mirror_num to extent_read_full_page
scrub: use int for mirror_num, not u64
scrub: add fixup code for errors on nodatasum files new ioctls to do logical->inode and inode->path resolving
fs/btrfs/Makefile | 3 +- fs/btrfs/backref.c | 748 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/backref.h | 62 +++++ fs/btrfs/disk-io.c | 2 +- fs/btrfs/extent_io.c | 6 +- fs/btrfs/extent_io.h | 3 +- fs/btrfs/inode.c | 2 +- fs/btrfs/ioctl.c | 134 +++++++++ fs/btrfs/ioctl.h | 29 ++ fs/btrfs/scrub.c | 412 +++++++++++++++++++++++++--- 10 files changed, 1362 insertions(+), 39 deletions(-) create mode 100644 fs/btrfs/backref.c create mode 100644 fs/btrfs/backref.h
Signature · 1 line
1.7.3.4
PATCH v3 1/8 added helper functions to iterate backrefs
These helper functions iterate back references and call a function for each backref. There is also a function to resolve an inode to a path in the file system.
--- fs/btrfs/Makefile | 3 +- fs/btrfs/backref.c | 748 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/backref.h | 62 +++++ fs/btrfs/ioctl.h | 10 + 4 files changed, 822 insertions(+), 1 deletions(-)
fs/btrfs/Makefile +2 -1
@@ -7,4 +7,5 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \ extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ export.o tree-log.o acl.o free-space-cache.o zlib.o lzo.o \ - compression.o delayed-ref.o relocation.o delayed-inode.o scrub.o + compression.o delayed-ref.o relocation.o delayed-inode.o backref.o \ + scrub.o
fs/btrfs/backref.c new file +748 -0
748 lines not on this page · raw message
fs/btrfs/backref.h new file +62 -0
62 lines not on this page · raw message
fs/btrfs/ioctl.h +10 -0
@@ -193,6 +193,16 @@ struct btrfs_ioctl_space_args {struct btrfs_ioctl_space_info spaces[0 ]; }; +struct btrfs_data_container { +__s32 size;/* out */ +__u32 elem_cnt;/* out */ +__u32 elem_missed;/* out */ +union { +char *str[0 ];/* out */ +__u64 val[0 ];/* out */ + }; +}; +#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args)#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
Signature · 1 line
1.7.3.4
PATCH v3 2/8 scrub: added unverified_errors
In reply to Jan SchmidtIn normal operation, scrub is reading data sequentially in large portions. In case of an i/o error, we try to find the corrupted area(s) by issuing page sized read requests. With this commit we increment the unverified_errors counter if all of the small size requests succeed.
Userland patches carrying such conspicous events to the administrator should already be around.
--- fs/btrfs/scrub.c | 37 ++++++++++++++++++++++++++----------- 1 files changed, 26 insertions(+), 11 deletions(-)
fs/btrfs/scrub.c +26 -11
@@ -201,18 +201,25 @@ nomem:* recheck_error gets called for every page in the bio, even though only * one may be bad */ -static void scrub_recheck_error (struct scrub_bio *sbio,int ix) +static int scrub_recheck_error (struct scrub_bio *sbio,int ix) { +struct scrub_dev *sdev = sbio->sdev; +u64 sector = (sbio->physical + ix *PAGE_SIZE ) >>9 ; +if (sbio->err) { -if (scrub_fixup_io (READ , sbio->sdev->dev->bdev, - (sbio->physical + ix *PAGE_SIZE ) >>9 , +if (scrub_fixup_io (READ , sbio->sdev->dev->bdev, sector, sbio->bio->bi_io_vec[ix].bv_page) ==0 ) {if (scrub_fixup_check (sbio, ix) ==0 ) -return ; +return 0 ; } } +spin_lock (&sdev->stat_lock); + ++sdev->stat.read_errors; +spin_unlock (&sdev->stat_lock); +scrub_fixup (sbio, ix); +return 1 ; }static int scrub_fixup_check (struct scrub_bio *sbio,int ix)
@@ -382,8 +389,14 @@ static void scrub_checksum(struct btrfs_work *work)int ret;if (sbio->err) { + ret =0 ;for (i =0 ; i < sbio->count; ++i) -scrub_recheck_error (sbio, i); + ret |=scrub_recheck_error (sbio, i); +if (!ret) { +spin_lock (&sdev->stat_lock); + ++sdev->stat.unverified_errors; +spin_unlock (&sdev->stat_lock); + } sbio->bio->bi_flags &= ~(BIO_POOL_MASK -1 ); sbio->bio->bi_flags |=1 <<BIO_UPTODATE ;
@@ -396,10 +409,6 @@ static void scrub_checksum(struct btrfs_work *work) bi->bv_offset =0 ; bi->bv_len =PAGE_SIZE ; } - -spin_lock (&sdev->stat_lock); - ++sdev->stat.read_errors; -spin_unlock (&sdev->stat_lock); goto out; }for (i =0 ; i < sbio->count; ++i) {
@@ -420,8 +429,14 @@ static void scrub_checksum(struct btrfs_work *work)WARN_ON (1 ); }kunmap_atomic (buffer,KM_USER0 ); -if (ret) -scrub_recheck_error (sbio, i); +if (ret) { + ret =scrub_recheck_error (sbio, i); +if (!ret) { +spin_lock (&sdev->stat_lock); + ++sdev->stat.unverified_errors; +spin_unlock (&sdev->stat_lock); + } + } } out:
Signature · 1 line
1.7.3.4
PATCH v3 3/8 scrub: print paths of corrupted files
In reply to Jan SchmidtWhile scrubbing, we may encounter various errors. Previously, a logical address was printed to the log only. Now, all paths belonging to that address are resolved and printed separately. That should work for hardlinks as well as reflinks.
--- fs/btrfs/scrub.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 163 insertions(+), 6 deletions(-)
fs/btrfs/scrub.c +163 -6
@@ -17,10 +17,12 @@*/ #include <linux/blkdev.h> +#include <linux/ratelimit.h> #include "ctree.h" #include "volumes.h" #include "disk-io.h" #include "ordered-data.h" +#include "backref.h" /* * This is only the first step towards a full-features scrub. It reads all
@@ -100,6 +102,19 @@ struct scrub_dev {spinlock_t stat_lock; }; +struct scrub_warning { +struct btrfs_path *path; +u64 extent_item_size; +char *scratch_buf; +char *msg_buf; +const char *errstr; +sector_t sector; +u64 logical; +struct btrfs_device *dev; +int msg_bufsize; +int scratch_bufsize; +}; +static void scrub_free_csums (struct scrub_dev *sdev) {while (!list_empty (&sdev->csum_list)) {
@@ -195,6 +210,143 @@ nomem:return ERR_PTR (-ENOMEM ); } +static int scrub_print_warning_inode (u64 inum,u64 offset,u64 root,void *ctx) +{ +u64 isize; +u32 nlink; +int ret; +int i; +struct extent_buffer *eb; +struct btrfs_inode_item *inode_item; +struct scrub_warning *swarn = ctx; +struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info; +struct inode_fs_paths *ipath =NULL ; +struct btrfs_root *local_root; +struct btrfs_key root_key; + + root_key.objectid = root; + root_key.type =BTRFS_ROOT_ITEM_KEY ; + root_key.offset = (u64)-1 ; + local_root =btrfs_read_fs_root_no_name (fs_info, &root_key); +if (IS_ERR (local_root)) { + ret =PTR_ERR (local_root); + goto err; + } + + ret =inode_item_info (inum,0 , local_root, swarn->path); +if (ret) { +btrfs_release_path (swarn->path); + goto err; + } + + eb = swarn->path->nodes[0 ]; + inode_item =btrfs_item_ptr (eb, swarn->path->slots[0 ], + struct btrfs_inode_item); + isize =btrfs_inode_size (eb, inode_item); + nlink =btrfs_inode_nlink (eb, inode_item); +btrfs_release_path (swarn->path); + + ipath =init_ipath (4096 , local_root, swarn->path); + ret =paths_from_inode (inum, ipath); + +if (ret <0 ) + goto err; + +/* +* we deliberately ignore the bit ipath might have been too small to +* hold all of the paths here +*/ +for (i =0 ; i < ipath->fspath->elem_cnt; ++i) +printk (KERN_WARNING "btrfs: %s at logical %llu on dev " +"%s, sector %llu, root %llu, inode %llu, offset %llu, " +"length %llu, links %u (path: %s)\n" , swarn->errstr, + swarn->logical, swarn->dev->name, + (unsigned long long )swarn->sector, root, inum, offset, +min (isize - offset, (u64 )PAGE_SIZE ), nlink, + ipath->fspath->str[i]); + +free_ipath (ipath); +return 0 ; + +err: +printk (KERN_WARNING "btrfs: %s at logical %llu on dev " +"%s, sector %llu, root %llu, inode %llu, offset %llu: path " +"resolving failed with ret=%d\n" , swarn->errstr, + swarn->logical, swarn->dev->name, + (unsigned long long )swarn->sector, root, inum, offset, ret); + +free_ipath (ipath); +return 0 ; +} + +static void scrub_print_warning (const char *errstr,struct scrub_bio *sbio, +int ix) +{ +struct btrfs_device *dev = sbio->sdev->dev; +struct btrfs_fs_info *fs_info = dev->dev_root->fs_info; +struct btrfs_path *path; +struct btrfs_key found_key; +struct extent_buffer *eb; +struct btrfs_extent_item *ei; +struct scrub_warning swarn; +u32 item_size; +int ret; +u64 ref_root; +u8 ref_level; +unsigned long ptr =0 ; +const int bufsize =4096 ; +u64 extent_offset; + + path =btrfs_alloc_path (); + + swarn.scratch_buf =kmalloc (bufsize,GFP_NOFS ); + swarn.msg_buf =kmalloc (bufsize,GFP_NOFS ); + swarn.sector = (sbio->physical + ix *PAGE_SIZE ) >>9 ; + swarn.logical = sbio->logical + ix *PAGE_SIZE ; + swarn.errstr = errstr; + swarn.dev = dev; + swarn.msg_bufsize = bufsize; + swarn.scratch_bufsize = bufsize; + +if (!path || !swarn.scratch_buf || !swarn.msg_buf) + goto out; + + ret =extent_from_logical (fs_info, swarn.logical, path, &found_key); +if (ret <0 ) + goto out; + + extent_offset = swarn.logical - found_key.objectid; + swarn.extent_item_size = found_key.offset; + + eb = path->nodes[0 ]; + ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); + item_size =btrfs_item_size_nr (eb, path->slots[0 ]); + +if (ret &BTRFS_EXTENT_FLAG_TREE_BLOCK ) { +do { + ret =tree_backref_for_extent (&ptr, eb, ei, item_size, + &ref_root, &ref_level); +printk (KERN_WARNING "%s at logical %llu on dev %s, " +"sector %llu: metadata %s (level %d) in tree " +"%llu\n" , errstr, swarn.logical, dev->name, + (unsigned long long )swarn.sector, + ref_level ?"node" :"leaf" , + ret <0 ?-1 : ref_level, + ret <0 ?-1 : ref_root); + }while (ret !=1 ); + }else { + swarn.path = path; +iterate_extent_inodes (fs_info, path, found_key.objectid, + extent_offset, + scrub_print_warning_inode, &swarn); + } + +out: +btrfs_free_path (path); +kfree (swarn.scratch_buf); +kfree (swarn.msg_buf); +} +/* * scrub_recheck_error gets called when either verification of the page * failed or the bio failed to read, e.g. with EIO. In the latter case,
@@ -205,6 +357,8 @@ static int scrub_recheck_error(struct scrub_bio *sbio, int ix) {struct scrub_dev *sdev = sbio->sdev;u64 sector = (sbio->physical + ix *PAGE_SIZE ) >>9 ; + static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, + DEFAULT_RATELIMIT_BURST);if (sbio->err) { if (scrub_fixup_io(READ, sbio->sdev->dev->bdev, sector,
@@ -212,6 +366,11 @@ static int scrub_recheck_error(struct scrub_bio *sbio, int ix)if (scrub_fixup_check (sbio, ix) ==0 )return 0 ; } +if (__ratelimit (&_rs)) +scrub_print_warning ("i/o error" , sbio, ix); + }else { +if (__ratelimit (&_rs)) +scrub_print_warning ("checksum error" , sbio, ix); }spin_lock (&sdev->stat_lock);
@@ -326,9 +485,8 @@ static void scrub_fixup(struct scrub_bio *sbio, int ix) ++sdev->stat.corrected_errors;spin_unlock (&sdev->stat_lock); -if (printk_ratelimit ()) -printk (KERN_ERR "btrfs: fixed up at %llu\n" , - (unsigned long long )logical); +printk_ratelimited (KERN_ERR "btrfs: fixed up error at logical %llu\n" , + (unsigned long long )logical);return ; uncorrectable:
@@ -337,9 +495,8 @@ uncorrectable: ++sdev->stat.uncorrectable_errors;spin_unlock (&sdev->stat_lock); -if (printk_ratelimit ()) -printk (KERN_ERR "btrfs: unable to fixup at %llu\n" , - (unsigned long long )logical); +printk_ratelimited (KERN_ERR "btrfs: unable to fixup (regular) error at " +"logical %llu\n" , (unsigned long long )logical); } static int scrub_fixup_io(int rw, struct block_device *bdev, sector_t sector,
Signature · 1 line
1.7.3.4
PATCH v3 4/8 scrub: bugfix: mirror_num off by one
In reply to Jan SchmidtFix the mirror_num determination in scrub_stripe. The rest of the scrub code did not use mirror_num for anything important and that error went unnoticed. The nodatasum fixup patch of this set depends on a correct mirror_num.
--- fs/btrfs/scrub.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
fs/btrfs/scrub.c +5 -5
@@ -930,21 +930,21 @@ static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,if (map->type &BTRFS_BLOCK_GROUP_RAID0 ) { offset = map->stripe_len * num; increment = map->stripe_len * map->num_stripes; - mirror_num =0 ; + mirror_num =1 ; }else if (map->type &BTRFS_BLOCK_GROUP_RAID10 ) {int factor = map->num_stripes / map->sub_stripes; offset = map->stripe_len * (num / map->sub_stripes); increment = map->stripe_len * factor; - mirror_num = num % map->sub_stripes; + mirror_num = num % map->sub_stripes +1 ; }else if (map->type &BTRFS_BLOCK_GROUP_RAID1 ) { increment = map->stripe_len; - mirror_num = num % map->num_stripes; + mirror_num = num % map->num_stripes +1 ; }else if (map->type &BTRFS_BLOCK_GROUP_DUP ) { increment = map->stripe_len; - mirror_num = num % map->num_stripes; + mirror_num = num % map->num_stripes +1 ; }else { increment = map->stripe_len; - mirror_num =0 ; + mirror_num =1 ; } path =btrfs_alloc_path ();
Signature · 1 line
1.7.3.4
PATCH v3 5/8 add mirror_num to extent_read_full_page
In reply to Jan SchmidtCurrently, extent_read_full_page always assumes we are trying to read mirror 0, which generally is the best we can do. To add flexibility, pass it as a parameter. This will be needed by scrub fixup code.
--- fs/btrfs/disk-io.c | 2 +- fs/btrfs/extent_io.c | 6 +++--- fs/btrfs/extent_io.h | 2 +- fs/btrfs/inode.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-)
fs/btrfs/disk-io.c +1 -1
@@ -874,7 +874,7 @@ static int btree_readpage(struct file *file, struct page *page) {struct extent_io_tree *tree; tree = &BTRFS_I (page->mapping->host)->io_tree; -return extent_read_full_page (tree, page, btree_get_extent); +return extent_read_full_page (tree, page, btree_get_extent,0 ); }static int btree_releasepage (struct page *page,gfp_t gfp_flags)
fs/btrfs/extent_io.c +3 -3
@@ -2111,16 +2111,16 @@ static int __extent_read_full_page(struct extent_io_tree *tree, }int extent_read_full_page (struct extent_io_tree *tree,struct page *page, -get_extent_t *get_extent) +get_extent_t *get_extent,int mirror_num) {struct bio *bio =NULL ;unsigned long bio_flags =0 ;int ret; - ret =__extent_read_full_page (tree, page, get_extent, &bio,0 , + ret =__extent_read_full_page (tree, page, get_extent, &bio, mirror_num, &bio_flags);if (bio) - ret =submit_one_bio (READ , bio,0 , bio_flags); + ret =submit_one_bio (READ , bio, mirror_num, bio_flags);return ret; }
fs/btrfs/extent_io.h +1 -1
@@ -177,7 +177,7 @@ int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,int try_lock_extent (struct extent_io_tree *tree,u64 start,u64 end,gfp_t mask);int extent_read_full_page (struct extent_io_tree *tree,struct page *page, -get_extent_t *get_extent); +get_extent_t *get_extent,int mirror_num);int __init extent_io_init (void );void extent_io_exit (void );
fs/btrfs/inode.c +1 -1
@@ -6248,7 +6248,7 @@ int btrfs_readpage(struct file *file, struct page *page) {struct extent_io_tree *tree; tree = &BTRFS_I (page->mapping->host)->io_tree; -return extent_read_full_page (tree, page, btrfs_get_extent); +return extent_read_full_page (tree, page, btrfs_get_extent,0 ); }static int btrfs_writepage (struct page *page,struct writeback_control *wbc)
Signature · 1 line
1.7.3.4
PATCH v3 6/8 scrub: use int for mirror_num, not u64
In reply to Jan Schmidtthe rest of the code uses int mirror_num, and so should scrub
--- fs/btrfs/scrub.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/scrub.c +4 -4
@@ -65,7 +65,7 @@ static void scrub_fixup(struct scrub_bio *sbio, int ix);struct scrub_page {u64 flags;/* extent flags */ u64 generation; -u64 mirror_num; +int mirror_num;int have_csum;u8 csum[BTRFS_CSUM_SIZE ]; };
@@ -776,7 +776,7 @@ nomem: }static int scrub_page (struct scrub_dev *sdev,u64 logical,u64 len, -u64 physical,u64 flags,u64 gen,u64 mirror_num, +u64 physical,u64 flags,u64 gen,int mirror_num,u8 *csum,int force) {struct scrub_bio *sbio;
@@ -873,7 +873,7 @@ static int scrub_find_csum(struct scrub_dev *sdev, u64 logical, u64 len,/* scrub extent tries to collect up to 64 kB for each bio */ static int scrub_extent (struct scrub_dev *sdev,u64 logical,u64 len, -u64 physical,u64 flags,u64 gen,u64 mirror_num) +u64 physical,u64 flags,u64 gen,int mirror_num) {int ret;u8 csum[BTRFS_CSUM_SIZE ];
@@ -919,7 +919,7 @@ static noinline_for_stack int scrub_stripe(struct scrub_dev *sdev,u64 physical;u64 logical;u64 generation; -u64 mirror_num; +int mirror_num;u64 increment = map->stripe_len;u64 offset;
Signature · 1 line
1.7.3.4
PATCH v3 7/8 scrub: add fixup code for errors on nodatasum files
In reply to Jan SchmidtThis removes a FIXME comment and introduces the first part of nodatasum
fixup: It gets the corresponding inode for a logical address and triggers a regular readpage for the corrupted sector.
Once we have on-the-fly error correction our error will be automatically corrected. The correction code is expected to clear the newly introduced EXTENT_DAMAGED flag, making scrub report that error as "corrected" instead of "uncorrectable" eventually.
--- fs/btrfs/extent_io.h | 1 + fs/btrfs/scrub.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 183 insertions(+), 6 deletions(-)
fs/btrfs/extent_io.h +1 -0
@@ -17,6 +17,7 @@#define EXTENT_NODATASUM (1 << 10)#define EXTENT_DO_ACCOUNTING (1 << 11)#define EXTENT_FIRST_DELALLOC (1 << 12) +#define EXTENT_DAMAGED (1 << 13)#define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)#define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | EXTENT_FIRST_DELALLOC)
fs/btrfs/scrub.c +182 -6
243 lines not on this page · raw message
Signature · 1 line
1.7.3.4
PATCH v3 8/8 new ioctls to do logical->inode and inode->path resolving
In reply to Jan Schmidtthese ioctls make use of the new functions initially added for scrub. they return all inodes belonging to a logical address (BTRFS_IOC_LOGICAL_INO) and all paths belonging to an inode (BTRFS_IOC_INO_PATHS).
--- fs/btrfs/ioctl.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ioctl.h | 19 ++++++++ 2 files changed, 153 insertions(+), 0 deletions(-)
fs/btrfs/ioctl.c +134 -0
@@ -51,6 +51,7 @@#include "volumes.h" #include "locking.h" #include "inode-map.h" +#include "backref.h" /* Mask out flags that are inappropriate for the given type of inode. */ static inline __u32 btrfs_mask_flags (umode_t mode,__u32 flags)
@@ -2836,6 +2837,135 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,return ret; } +static long btrfs_ioctl_ino_to_path (struct btrfs_root *root,void __user *arg) +{ +int ret =0 ; +int i; +unsigned long rel_ptr; +int size; +struct btrfs_ioctl_ino_path_args *ipa; +struct inode_fs_paths *ipath =NULL ; +struct btrfs_path *path; + + path =btrfs_alloc_path (); +if (!path) { + ret = -ENOMEM ; + goto out; + } + + ipa =memdup_user (arg,sizeof (*ipa)); +if (IS_ERR (ipa)) { + ret =PTR_ERR (ipa); + ipa =NULL ; + goto out; + } + + size =min (ipa->size,4096 ); + ipath =init_ipath (size, root, path); +if (IS_ERR (ipath)) { + ret =PTR_ERR (ipath); + ipath =NULL ; + goto out; + } + + ret =paths_from_inode (ipa->inum, ipath); +if (ret <0 ) + goto out; + +for (i =0 ; i < ipath->fspath->elem_cnt; ++i) { + rel_ptr = ipath->fspath->str[i] - (char *)ipath->fspath->str; + ipath->fspath->str[i] = (void *)rel_ptr; + } + + ret =copy_to_user (ipa->fspath, ipath->fspath, size); +if (ret) { + ret = -EFAULT ; + goto out; + } + +out: +btrfs_free_path (path); +free_ipath (ipath); +kfree (ipa); + +return ret; +} + +static int build_ino_list (u64 inum,u64 offset,u64 root,void *ctx) +{ +struct btrfs_data_container *inodes = ctx; + + inodes->size -=3 *sizeof (u64); +if (inodes->size >0 ) { + inodes->val[inodes->elem_cnt] = inum; + inodes->val[inodes->elem_cnt +1 ] = offset; + inodes->val[inodes->elem_cnt +2 ] = root; + inodes->elem_cnt +=3 ; + }else { + inodes->elem_missed +=3 ; + } + +return 0 ; +} + +static long btrfs_ioctl_logical_to_ino (struct btrfs_root *root, +void __user *arg) +{ +int ret =0 ; +int size; +u64 extent_offset; +struct btrfs_ioctl_logical_ino_args *loi; +struct btrfs_data_container *inodes =NULL ; +struct btrfs_path *path =NULL ; +struct btrfs_key key; + + loi =memdup_user (arg,sizeof (*loi)); +if (IS_ERR (loi)) { + ret =PTR_ERR (loi); + loi =NULL ; + goto out; + } + + path =btrfs_alloc_path (); +if (!path) { + ret = -ENOMEM ; + goto out; + } + + size =min (loi->size,4096 ); + inodes =init_data_container (size); +if (IS_ERR (inodes)) { + ret =PTR_ERR (inodes); + inodes =NULL ; + goto out; + } + + ret =extent_from_logical (root->fs_info, loi->logical, path, &key); + +if (ret &BTRFS_EXTENT_FLAG_TREE_BLOCK ) + ret = -ENOENT ; +if (ret <0 ) + goto out; + + extent_offset = loi->logical - key.objectid; + ret =iterate_extent_inodes (root->fs_info, path, key.objectid, + extent_offset, build_ino_list, inodes); + +if (ret <0 ) + goto out; + + ret =copy_to_user (loi->inodes, inodes, size); +if (ret) + ret = -EFAULT ; + +out: +btrfs_free_path (path); +kfree (inodes); +kfree (loi); + +return ret; +} +long btrfs_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {
@@ -2893,6 +3023,10 @@ long btrfs_ioctl(struct file *file, unsigned intreturn btrfs_ioctl_tree_search (file, argp);case BTRFS_IOC_INO_LOOKUP :return btrfs_ioctl_ino_lookup (file, argp); +case BTRFS_IOC_INO_PATHS : +return btrfs_ioctl_ino_to_path (root, argp); +case BTRFS_IOC_LOGICAL_INO : +return btrfs_ioctl_logical_to_ino (root, argp);case BTRFS_IOC_SPACE_INFO :return btrfs_ioctl_space_info (root, argp);case BTRFS_IOC_SYNC :
fs/btrfs/ioctl.h +19 -0
@@ -203,6 +203,20 @@ struct btrfs_data_container { }; }; +struct btrfs_ioctl_ino_path_args { +__u64 inum;/* in */ +__s32 size;/* in */ +__u64 reserved[4 ]; +struct btrfs_data_container *fspath;/* out */ +}; + +struct btrfs_ioctl_logical_ino_args { +__u64 logical;/* in */ +__s32 size;/* in */ +__u64 reserved[4 ]; +struct btrfs_data_container *inodes;/* out */ +}; +#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args)#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -258,4 +272,9 @@ struct btrfs_data_container { struct btrfs_ioctl_dev_info_args)#define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \ struct btrfs_ioctl_fs_info_args) +#define BTRFS_IOC_INO_PATHS _IOWR(BTRFS_IOCTL_MAGIC, 35, \ + struct btrfs_ioctl_ino_path_args) +#define BTRFS_IOC_LOGICAL_INO _IOWR(BTRFS_IOCTL_MAGIC, 36, \ + struct btrfs_ioctl_ino_path_args) +#endif
Signature · 1 line
1.7.3.4
Re: [PATCH v3 8/8] new ioctls to do logical->inode and inode->path resolving
Hi, Jan,
Quoted 3 lines · Jan Schmidt
these ioctls make use of the new functions initially added for scrub. they return all inodes belonging to a logical address (
BTRFS_IOC_LOGICAL_INO) and all paths belonging to an inode (BTRFS_IOC_INO_PATHS).
I've not read this patch in detail, so I may have missed something,
but why do we need new ioctls for these functions, when we have BTRFS_IOC_TREE_SEARCH, which will allow us to perform the same two operations using existing kernel-side infrastructure?
Hugo.
Quoted 200 lines
Signed-off-by Jan Schmidt <[email protected]>--- fs/btrfs/ioctl.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/ioctl.h | 19 ++++++++ 2 files changed, 153 insertions(+), 0 deletions(-)fs/btrfs/ioctl.c +134
-0@@ -51,6 +51,7 @@#include "volumes.h" #include "locking.h" #include "inode-map.h" +#include "backref.h" /* Mask out flags that are inappropriate for the given type of inode. */ static inline __u32 btrfs_mask_flags (umode_t mode,__u32 flags)@@ -2836,6 +2837,135 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,return ret; } +static long btrfs_ioctl_ino_to_path (struct btrfs_root *root,void __user *arg) +{ +int ret =0 ; +int i; +unsigned long rel_ptr; +int size; +struct btrfs_ioctl_ino_path_args *ipa; +struct inode_fs_paths *ipath =NULL ; +struct btrfs_path *path; + + path =btrfs_alloc_path (); +if (!path) { + ret = -ENOMEM ; + goto out; + } + + ipa =memdup_user (arg,sizeof (*ipa)); +if (IS_ERR (ipa)) { + ret =PTR_ERR (ipa); + ipa =NULL ; + goto out; + } + + size =min (ipa->size,4096 ); + ipath =init_ipath (size, root, path); +if (IS_ERR (ipath)) { + ret =PTR_ERR (ipath); + ipath =NULL ; + goto out; + } + + ret =paths_from_inode (ipa->inum, ipath); +if (ret <0 ) + goto out; + +for (i =0 ; i < ipath->fspath->elem_cnt; ++i) { + rel_ptr = ipath->fspath->str[i] - (char *)ipath->fspath->str; + ipath->fspath->str[i] = (void *)rel_ptr; + } + + ret =copy_to_user (ipa->fspath, ipath->fspath, size); +if (ret) { + ret = -EFAULT ; + goto out; + } + +out: +btrfs_free_path (path); +free_ipath (ipath); +kfree (ipa); + +return ret; +} + +static int build_ino_list (u64 inum,u64 offset,u64 root,void *ctx) +{ +struct btrfs_data_container *inodes = ctx; + + inodes->size -=3 *sizeof (u64); +if (inodes->size >0 ) { + inodes->val[inodes->elem_cnt] = inum; + inodes->val[inodes->elem_cnt +1 ] = offset; + inodes->val[inodes->elem_cnt +2 ] = root; + inodes->elem_cnt +=3 ; + }else { + inodes->elem_missed +=3 ; + } + +return 0 ; +} + +static long btrfs_ioctl_logical_to_ino (struct btrfs_root *root, +void __user *arg) +{ +int ret =0 ; +int size; +u64 extent_offset; +struct btrfs_ioctl_logical_ino_args *loi; +struct btrfs_data_container *inodes =NULL ; +struct btrfs_path *path =NULL ; +struct btrfs_key key; + + loi =memdup_user (arg,sizeof (*loi)); +if (IS_ERR (loi)) { + ret =PTR_ERR (loi); + loi =NULL ; + goto out; + } + + path =btrfs_alloc_path (); +if (!path) { + ret = -ENOMEM ; + goto out; + } + + size =min (loi->size,4096 ); + inodes =init_data_container (size); +if (IS_ERR (inodes)) { + ret =PTR_ERR (inodes); + inodes =NULL ; + goto out; + } + + ret =extent_from_logical (root->fs_info, loi->logical, path, &key); + +if (ret &BTRFS_EXTENT_FLAG_TREE_BLOCK ) + ret = -ENOENT ; +if (ret <0 ) + goto out; + + extent_offset = loi->logical - key.objectid; + ret =iterate_extent_inodes (root->fs_info, path, key.objectid, + extent_offset, build_ino_list, inodes); + +if (ret <0 ) + goto out; + + ret =copy_to_user (loi->inodes, inodes, size); +if (ret) + ret = -EFAULT ; + +out: +btrfs_free_path (path); +kfree (inodes); +kfree (loi); + +return ret; +} +long btrfs_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {@@ -2893,6 +3023,10 @@ long btrfs_ioctl(struct file *file, unsigned intreturn btrfs_ioctl_tree_search (file, argp);case BTRFS_IOC_INO_LOOKUP :return btrfs_ioctl_ino_lookup (file, argp); +case BTRFS_IOC_INO_PATHS : +return btrfs_ioctl_ino_to_path (root, argp); +case BTRFS_IOC_LOGICAL_INO : +return btrfs_ioctl_logical_to_ino (root, argp);case BTRFS_IOC_SPACE_INFO :return btrfs_ioctl_space_info (root, argp);case BTRFS_IOC_SYNC :fs/btrfs/ioctl.h +19
-0@@ -203,6 +203,20 @@ struct btrfs_data_container { }; }; +struct btrfs_ioctl_ino_path_args { +__u64 inum;/* in */ +__s32 size;/* in */ +__u64 reserved[4 ]; +struct btrfs_data_container *fspath;/* out */ +}; + +struct btrfs_ioctl_logical_ino_args { +__u64 logical;/* in */ +__s32 size;/* in */ +__u64 reserved[4 ]; +struct btrfs_data_container *inodes;/* out */ +}; +#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args)#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \@@ -258,4 +272,9 @@ struct btrfs_data_container { struct btrfs_ioctl_dev_info_args)#define BTRFS_IOC_FS_INFO _IOR(BTRFS_IOCTL_MAGIC, 31, \ struct btrfs_ioctl_fs_info_args) +#define BTRFS_IOC_INO_PATHS _IOWR(BTRFS_IOCTL_MAGIC, 35, \ + struct btrfs_ioctl_ino_path_args) +#define BTRFS_IOC_LOGICAL_INO _IOWR(BTRFS_IOCTL_MAGIC, 36, \ + struct btrfs_ioctl_ino_path_args) +#endif
Signature · 3 lines
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
--- This year, I'm giving up Lent. --- Re: [PATCH v3 8/8] new ioctls to do logical->inode and inode->path resolving
Hi,
Quoted 7 lines · Hugo Mills
Hi, Jan,
Quoted 3 lines · Jan Schmidt
these ioctls make use of the new functions initially added for scrub. they return all inodes belonging to a logical address (
BTRFS_IOC_LOGICAL_INO) and all paths belonging to an inode (BTRFS_IOC_INO_PATHS).
Quoted 6 lines
I've not read this patch in detail, so I may have missed something,
but why do we need new ioctls for these functions, when we haveBTRFS_IOC_TREE_SEARCH, which will allow us to perform the same two operations using existing kernel-side infrastructure?Hugo.
Note that those ioctls do a lot more than just one tree search. You are right, we could implement all this with (quite a few) BTRFS_IOC_TREE_SEARCH ioctls.
Especially resolving all file system paths for an inode needs really a lot of searches. I like to have logic requiring deep knowledge of the internals of btrfs trees in kernel, generally. Not to mention that this way we are safe to run this on a file system under load and still can get consistent results.
Last but not least, if we want to use this code for general error reporting to the kernel log (e.g. by scrub), we need all the resolving code in kernel anyway. So I'd like to provide that functionality to user space, too.
-Jan
Re: [PATCH v3 4/8] scrub: bugfix: mirror_num off by one
In reply to Jan SchmidtQuoted 2 lines · Jan Schmidt
Fix the
mirror_numdetermination inscrub_stripe. The rest of the scrub code did not usemirror_numfor anything important and that error went unnoticed.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This just turned out not to be the case. Please ignore this. I'll send a corrected version, soon.
The bug introduced with this series does nothing harmful by itself, but can prevent error correction.
-Jan