Btrfs: add IO error device stats
v4 cover Btrfs: add IO error device stats
Changes v1-v2:
- Remove restriction that BTRFS_IOC_GET_DEVICE_STATS is a privileged operation
- Cast u64 to unsigned long long for printf()
Changes v2-v3:
- Rebased on Chris' current master
Changes v3-v4:
- Add padding at end of ioctl structure
The goal is to detect when drives start to get an increased error rate, when drives should be replaced soon. Therefore statistic counters are added that count IO errors (read, write and flush). Additionally, the software detected errors like checksum errors and corrupted blocks are counted.
An ioctl interface is added to get the device statistic counters. A second ioctl is added to atomically get and reset these counters.
The device statistics are written into the device tree with each transaction commit. Only modified statistics are written. When a filesystem is mounted, the device statistics for each involved device are read from the device tree and used to initialize the counters.
A patch for the btrfs-progs world will also be sent.
Stefan Behrens (3):
Btrfs: add device counters for detected IO and checksum errors
Btrfs: add ioctl to get and reset the device stats
Btrfs: read device stats on mount, write modified ones during commit
fs/btrfs/ctree.h | 51 ++++++++ fs/btrfs/disk-io.c | 25 +++- fs/btrfs/extent_io.c | 27 +++- fs/btrfs/ioctl.c | 26 ++++ fs/btrfs/ioctl.h | 28 ++++ fs/btrfs/print-tree.c | 3 + fs/btrfs/scrub.c | 72 ++++++++--- fs/btrfs/transaction.c | 4 + fs/btrfs/volumes.c | 335 +++++++++++++++++++++++++++++++++++++++++++++++- fs/btrfs/volumes.h | 43 +++++++ 10 files changed, 589 insertions(+), 25 deletions(-)
Signature · 1 line
1.7.10.2
PATCH v4 1/3 Btrfs: add device counters for detected IO and checksum errors
The goal is to detect when drives start to get an increased error rate, when drives should be replaced soon. Therefore statistic counters are added that count IO errors (read, write and flush). Additionally, the software detected errors like checksum errors and corrupted blocks are counted.
--- fs/btrfs/disk-io.c | 18 ++++++++++--- fs/btrfs/extent_io.c | 27 +++++++++++++++++-- fs/btrfs/scrub.c | 72 +++++++++++++++++++++++++++++++++++++++----------- fs/btrfs/volumes.c | 61 +++++++++++++++++++++++++++++++++++++++--- fs/btrfs/volumes.h | 21 +++++++++++++++ 5 files changed, 174 insertions(+), 25 deletions(-)
fs/btrfs/disk-io.c +14 -4
@@ -2556,18 +2556,21 @@ recovery_tree_root:static void btrfs_end_buffer_write_sync (struct buffer_head *bh,int uptodate) { -char b[BDEVNAME_SIZE ]; -if (uptodate) {set_buffer_uptodate (bh); }else { +struct btrfs_device *device = (struct btrfs_device *) + bh->b_private; +printk_ratelimited (KERN_WARNING "lost page write due to " -"I/O error on %s\n" , -bdevname (bh->b_bdev, b)); +"I/O error on %s\n" , device->name);/* note, we dont' set_buffer_write_io_error because we have * our own ways of dealing with the IO errors */ clear_buffer_uptodate (bh); +btrfs_device_stat_inc (&device->cnt_write_io_errs); + device->device_stats_dirty =1 ; +btrfs_device_stat_print_on_error (device); }unlock_buffer (bh);put_bh (bh);
@@ -2682,6 +2685,7 @@ static int write_dev_supers(struct btrfs_device *device,set_buffer_uptodate (bh);lock_buffer (bh); bh->b_end_io = btrfs_end_buffer_write_sync; + bh->b_private = device; }/*
@@ -2740,6 +2744,12 @@ static int write_dev_flush(struct btrfs_device *device, int wait) }if (!bio_flagged (bio,BIO_UPTODATE )) { ret = -EIO ; +if (!bio_flagged (bio,BIO_EOPNOTSUPP )) { +btrfs_device_stat_inc ( + &device->cnt_flush_io_errs); + device->device_stats_dirty =1 ; +btrfs_device_stat_print_on_error (device); + } }/* drop the reference from the wait == 0 run */
fs/btrfs/extent_io.c +25 -2
@@ -1923,6 +1923,9 @@ int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,if (!test_bit (BIO_UPTODATE , &bio->bi_flags)) {/* try to remap that extent elsewhere? */ bio_put (bio); +btrfs_device_stat_inc (&dev->cnt_write_io_errs); + dev->device_stats_dirty =1 ; +btrfs_device_stat_print_on_error (dev);return -EIO ; }
@@ -2347,10 +2350,30 @@ static void end_bio_extent_readpage(struct bio *bio, int err)if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { ret = tree->ops->readpage_end_io_hook (page, start, end, state, mirror); -if (ret) +if (ret) { +/* no IO indicated but software detected errors +* in the block, either checksum errors or +* issues with the contents */ +int failed_mirror = (int )(uintptr_t ) + bio->bi_bdev; +struct btrfs_root *root = +BTRFS_I (page->mapping->host)->root; +struct btrfs_device *device; + uptodate =0 ; -else + device =btrfs_find_device_for_logical ( + root, start, + (int )failed_mirror); +if (device) { +btrfs_device_stat_inc ( + &device->cnt_corruption_errs); + device->device_stats_dirty =1 ; +btrfs_device_stat_print_on_error ( + device); + } + }else {clean_io_failure (start, page); + } }if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
fs/btrfs/scrub.c +56 -16
196 lines not on this page · raw message
fs/btrfs/volumes.c +58 -3
91 lines not on this page · raw message
fs/btrfs/volumes.h +21 -0
@@ -106,6 +106,14 @@ struct btrfs_device {struct completion flush_wait;int nobarriers; +/* disk I/O failure stats. For detailed description refer to +* struct btrfs_device_stats_item in ctree.h */ +int device_stats_dirty;/* counters need to be written to disk */ +atomic_t cnt_write_io_errs; +atomic_t cnt_read_io_errs; +atomic_t cnt_flush_io_errs; +atomic_t cnt_corruption_errs; +atomic_t cnt_generation_errs; };struct btrfs_fs_devices {
@@ -281,4 +289,17 @@ int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);int btrfs_chunk_readonly (struct btrfs_root *root,u64 chunk_offset);int find_free_dev_extent (struct btrfs_device *device,u64 num_bytes,u64 *start,u64 *max_avail); +struct btrfs_device *btrfs_find_device_for_logical (struct btrfs_root *root, +u64 logical,int mirror_num); +void btrfs_device_stat_print_on_error (struct btrfs_device *device); + +static inline void btrfs_device_stat_inc (atomic_t *cnt) +{ +atomic_inc (cnt); +} + +static inline int btrfs_device_stat_read (atomic_t *cnt) +{ +return atomic_read (cnt); +}#endif
Signature · 1 line
1.7.10.2
Re: [PATCH v4 1/3] Btrfs: add device counters for detected IO and checksum errors
fs/btrfs/disk-io.c recovery_tree_root: · line 2573
Quoted 5 lines · Stefan Behrens
fs/btrfs/disk-io.c +3
-0*/ clear_buffer_uptodate (bh); +btrfs_device_stat_inc (&device->cnt_write_io_errs); + device->device_stats_dirty =1 ; +btrfs_device_stat_print_on_error (device);
this sequence repeats several times (with a slight change), IMHO it asks for a helper.
void stat_inc(... *device, atomic_inc *cnt) {
atomic_inc(cntr);
device->device_stats_dirty = 1;
btrfs_device_stat_print_on_error(device);
}usage:
stat_inc(device, &device->cnt_write_io_errs);
I think it's unavoidable to leave out the 'device' parameter without a macro trickery, but as there are only 2 params, it don't it's needed.
fs/btrfs/volumes.c int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, · line 4008
Quoted 5 lines
static void
btrfs_end_bio(struct bio *bio, int err){fs/btrfs/volumes.c +2
-1-struct btrfs_bio *bbio = bio->bi_private; +struct btrfs_bio *bbio = (struct btrfs_bio *) + (((uintptr_t )bio->bi_private) & ~((uintptr_t )3 ));
I suggest do use a helper for all the "var & 3" places, and comment why is it so and what are the limitations. I can see from the code below that it encodes number of devices and fails for > 3.
fs/btrfs/volumes.c int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, · line 4014
Quoted 7 lines
fs/btrfs/volumes.c +3
-1int is_orig_bio =0 ; -if (err) +if (err) {atomic_inc (&bbio->error); +if (err == -EIO || err == -EREMOTEIO ) { +unsigned int dev_nr = ((uintptr_t )bio->bi_private) &3 ;
eg.
unsigned int dev_nr = bio_private_to_nr_dev(bio);
fs/btrfs/volumes.c int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, · line 4174
Quoted 11 lines
fs/btrfs/volumes.c +3
-1@@ -4148,7 +4166,9 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, }else { bio = first_bio; } - bio->bi_private = bbio; +BUG_ON ((((uintptr_t )bbio) &3 ) !=0 ); +BUG_ON (dev_nr >3 ); + bio->bi_private = (void *)(((uintptr_t )bbio) | dev_nr); bio->bi_end_io = btrfs_end_bio; bio->bi_sector = bbio->stripes[dev_nr].physical >>9 ; dev = bbio->stripes[dev_nr].dev;
otherwise ok,
david
PATCH v4 2/3 Btrfs: add ioctl to get and reset the device stats
In reply to Stefan BehrensAn ioctl interface is added to get the device statistic counters. A second ioctl is added to atomically get and reset these counters.
--- fs/btrfs/ioctl.c | 26 ++++++++++++++++++++ fs/btrfs/ioctl.h | 28 +++++++++++++++++++++ fs/btrfs/volumes.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 13 ++++++++++ 4 files changed, 136 insertions(+)
fs/btrfs/ioctl.c +26 -0
@@ -3042,6 +3042,28 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,return ret; } +static long btrfs_ioctl_get_device_stats (struct btrfs_root *root, +void __user *arg,int reset_after_read) +{ +struct btrfs_ioctl_get_device_stats *sa; +int ret; + +if (reset_after_read && !capable (CAP_SYS_ADMIN )) +return -EPERM ; + + sa =memdup_user (arg,sizeof (*sa)); +if (IS_ERR (sa)) +return PTR_ERR (sa); + + ret =btrfs_get_device_stats (root, sa, reset_after_read); + +if (copy_to_user (arg, sa,sizeof (*sa))) + ret = -EFAULT ; + +kfree (sa); +return ret; +} +static long btrfs_ioctl_ino_to_path (struct btrfs_root *root,void __user *arg) {int ret =0 ;
@@ -3424,6 +3446,10 @@ long btrfs_ioctl(struct file *file, unsigned intreturn btrfs_ioctl_balance_ctl (root, arg);case BTRFS_IOC_BALANCE_PROGRESS :return btrfs_ioctl_balance_progress (root, argp); +case BTRFS_IOC_GET_DEVICE_STATS : +return btrfs_ioctl_get_device_stats (root, argp,0 ); +case BTRFS_IOC_GET_AND_RESET_DEVICE_STATS : +return btrfs_ioctl_get_device_stats (root, argp,1 ); }return -ENOTTY ;
fs/btrfs/ioctl.h +28 -0
@@ -266,6 +266,30 @@ struct btrfs_ioctl_logical_ino_args {__u64 inodes; }; +#define BTRFS_IOCTL_GET_DEVICE_STATS_MAX_NR_ITEMS 5 +struct btrfs_ioctl_get_device_stats { +__u64 devid;/* in */ +__u64 nr_items;/* in/out */ + +/* out values: */ + +/* disk I/O failure stats */ +__u64 cnt_write_io_errs;/* EIO or EREMOTEIO from lower layers */ +__u64 cnt_read_io_errs;/* EIO or EREMOTEIO from lower layers */ +__u64 cnt_flush_io_errs;/* EIO or EREMOTEIO from lower layers */ + +/* stats for indirect indications for I/O failures */ +__u64 cnt_corruption_errs;/* checksum error, bytenr error or +* contents is illegal: this is an +* indication that the block was damaged +* during read or write, or written to +* wrong location or read from wrong +* location */ +__u64 cnt_generation_errs;/* an indication that blocks have not +* been written */ +__u64 unused[121 ];/* pad to 1k */ +}; +#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args)#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -330,5 +354,9 @@ struct btrfs_ioctl_logical_ino_args { struct btrfs_ioctl_ino_path_args)#define BTRFS_IOC_LOGICAL_INO _IOWR(BTRFS_IOCTL_MAGIC, 36, \ struct btrfs_ioctl_ino_path_args) +#define BTRFS_IOC_GET_DEVICE_STATS _IOWR(BTRFS_IOCTL_MAGIC, 52, \ + struct btrfs_ioctl_get_device_stats) +#define BTRFS_IOC_GET_AND_RESET_DEVICE_STATS _IOWR(BTRFS_IOCTL_MAGIC, 53, \ + struct btrfs_ioctl_get_device_stats)#endif
fs/btrfs/volumes.c +69 -0
72 lines not on this page · raw message
fs/btrfs/volumes.h +13 -0
@@ -292,6 +292,9 @@ int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,struct btrfs_device *btrfs_find_device_for_logical (struct btrfs_root *root,u64 logical,int mirror_num);void btrfs_device_stat_print_on_error (struct btrfs_device *device); +int btrfs_get_device_stats (struct btrfs_root *root, +struct btrfs_ioctl_get_device_stats *stats, +int reset_after_read);static inline void btrfs_device_stat_inc (atomic_t *cnt) {
@@ -302,4 +305,14 @@ static inline int btrfs_device_stat_read(atomic_t *cnt) {return atomic_read (cnt); } + +static inline int btrfs_device_stat_read_and_reset (atomic_t *cnt) +{ +return atomic_xchg (cnt,0 ); +} + +static inline void btrfs_device_stat_reset (atomic_t *cnt) +{ +atomic_set (cnt,0 ); +}#endif
Signature · 1 line
1.7.10.2
Re: [PATCH v4 2/3] Btrfs: add ioctl to get and reset the device stats
Quoted 31 lines · Stefan Behrens
An ioctl interface is added to get the device statistic counters. A second ioctl is added to atomically get and reset these counters.
Signed-off-by Stefan Behrens <[email protected]>--- fs/btrfs/ioctl.c | 26 ++++++++++++++++++++ fs/btrfs/ioctl.h | 28 +++++++++++++++++++++ fs/btrfs/volumes.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 13 ++++++++++ 4 files changed, 136 insertions(+)fs/btrfs/ioctl.c +4
-0@@ -3424,6 +3446,10 @@ long btrfs_ioctl(struct file *file, unsigned intreturn btrfs_ioctl_balance_ctl (root, arg);case BTRFS_IOC_BALANCE_PROGRESS :return btrfs_ioctl_balance_progress (root, argp); +case BTRFS_IOC_GET_DEVICE_STATS : +return btrfs_ioctl_get_device_stats (root, argp,0 ); +case BTRFS_IOC_GET_AND_RESET_DEVICE_STATS : +return btrfs_ioctl_get_device_stats (root, argp,1 ); }return -ENOTTY ;fs/btrfs/ioctl.h +0
-0
fs/btrfs/ioctl.h struct btrfs_ioctl_logical_ino_args { · line 269
Quoted 4 lines
fs/btrfs/ioctl.h +1
-0@@ -266,6 +266,30 @@ struct btrfs_ioctl_logical_ino_args {__u64 inodes; }; +#define BTRFS_IOCTL_GET_DEVICE_STATS_MAX_NR_ITEMS 5
The check at the end of btrfs_get_device_stats() should use this number:
Quoted 18 lines
fs/btrfs/ioctl.h +16
-0+if (stats->nr_items >5 ) + stats->nr_items =5 ; +return 0 ; +} +struct btrfs_ioctl_get_device_stats { +__u64 devid;/* in */ +__u64 nr_items;/* in/out */ + +/* out values: */ + +/* disk I/O failure stats */ +__u64 cnt_write_io_errs;/* EIO or EREMOTEIO from lower layers */ +__u64 cnt_read_io_errs;/* EIO or EREMOTEIO from lower layers */ +__u64 cnt_flush_io_errs;/* EIO or EREMOTEIO from lower layers */ + +/* stats for indirect indications for I/O failures */
fs/btrfs/ioctl.h struct btrfs_ioctl_logical_ino_args { · line 291
Quoted 10 lines
fs/btrfs/ioctl.h +10
-0+__u64 cnt_corruption_errs;/* checksum error, bytenr error or +* contents is illegal: this is an +* indication that the block was damaged +* during read or write, or written to +* wrong location or read from wrong +* location */ +__u64 cnt_generation_errs;/* an indication that blocks have not +* been written */ +__u64 unused[121 ];/* pad to 1k */ +};
Padding has landed, and thanks for the explanation in the V3, I see how the compatibility works.
fs/btrfs/ioctl.h struct btrfs_ioctl_logical_ino_args { · line 360
Quoted 12 lines
fs/btrfs/ioctl.h +5
-0+#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ struct btrfs_ioctl_vol_args)#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \@@ -330,5 +354,9 @@ struct btrfs_ioctl_logical_ino_args { struct btrfs_ioctl_ino_path_args)#define BTRFS_IOC_LOGICAL_INO _IOWR(BTRFS_IOCTL_MAGIC, 36, \ struct btrfs_ioctl_ino_path_args) +#define BTRFS_IOC_GET_DEVICE_STATS _IOWR(BTRFS_IOCTL_MAGIC, 52, \ + struct btrfs_ioctl_get_device_stats) +#define BTRFS_IOC_GET_AND_RESET_DEVICE_STATS _IOWR(BTRFS_IOCTL_MAGIC, 53, \ + struct btrfs_ioctl_get_device_stats)
I think that 'reset device' should go into the structure, besides the ioctl number there is no difference.
Then the permission check will look like:
fs/btrfs/ioctl.h static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, · line 3046
Quoted 6 lines
fs/btrfs/ioctl.h +2
-0@@ -3042,6 +3042,28 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,return ret; } +static long btrfs_ioctl_get_device_stats (struct btrfs_root *root, +void __user *arg,int reset_after_read)
void __user *arg)
fs/btrfs/ioctl.h static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, · line 3055
Quoted 9 lines
fs/btrfs/ioctl.h +9
-0+{ +struct btrfs_ioctl_get_device_stats *sa; +int ret; + + sa =memdup_user (arg,sizeof (*sa)); +if (IS_ERR (sa)) +return PTR_ERR (sa); + +if (reset_after_read && !capable (CAP_SYS_ADMIN ))
if (sa->reset_after_read && !capable(CAP_SYS_ADMIN)) {
kfree(sa);fs/btrfs/ioctl.h static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, · line 3056
Quoted 1 line
fs/btrfs/ioctl.h +1
-0+return -EPERM ;
}
fs/btrfs/ioctl.h static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, · line 3065
Quoted 9 lines
fs/btrfs/ioctl.h +9
-0+ + ret =btrfs_get_device_stats (root, sa, reset_after_read); + +if (copy_to_user (arg, sa,sizeof (*sa))) + ret = -EFAULT ; + +kfree (sa); +return ret; +}
So the EINVAL could come earlier than EPERM, but I don't think it's a problem.
david
PATCH v4 3/3 Btrfs: read device stats on mount, write modified ones during commit
In reply to Stefan BehrensThe device statistics are written into the device tree with each transaction commit. Only modified statistics are written. When a filesystem is mounted, the device statistics for each involved device are read from the device tree and used to initialize the counters.
--- fs/btrfs/ctree.h | 51 ++++++++++++ fs/btrfs/disk-io.c | 7 ++ fs/btrfs/print-tree.c | 3 + fs/btrfs/transaction.c | 4 + fs/btrfs/volumes.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 9 +++ 6 files changed, 279 insertions(+)
fs/btrfs/ctree.h +51 -0
@@ -823,6 +823,26 @@ struct btrfs_csum_item {u8 csum; } __attribute__ ((__packed__)); +struct btrfs_device_stats_item { +/* +* grow this item struct at the end for future enhancements and keep +* the existing values unchanged +*/ +__le64 cnt_write_io_errs;/* EIO or EREMOTEIO from lower layers */ +__le64 cnt_read_io_errs;/* EIO or EREMOTEIO from lower layers */ +__le64 cnt_flush_io_errs;/* EIO or EREMOTEIO from lower layers */ + +/* stats for indirect indications for I/O failures */ +__le64 cnt_corruption_errs;/* checksum error, bytenr error or +* contents is illegal: this is an +* indication that the block was damaged +* during read or write, or written to +* wrong location or read from wrong +* location */ +__le64 cnt_generation_errs;/* an indication that blocks have not +* been written */ +} __attribute__ ((__packed__ )); +/* different types of block groups (and chunks) */ #define BTRFS_BLOCK_GROUP_DATA (1ULL << 0)#define BTRFS_BLOCK_GROUP_SYSTEM (1ULL << 1)
@@ -1508,6 +1528,12 @@ struct btrfs_ioctl_defrag_range_args {#define BTRFS_BALANCE_ITEM_KEY 248/* +* Persistantly stores the io stats in the device tree. +* One key for all stats, (0, BTRFS_DEVICE_STATS_KEY, devid). +*/ +#define BTRFS_DEVICE_STATS_KEY 249 + +/* * string items are for debugging. They just store a short string of * data in the FS */
@@ -2415,6 +2441,31 @@ static inline u32 btrfs_file_extent_inline_item_len(struct extent_buffer *eb,return btrfs_item_size (eb, e) - offset; } +/* btrfs_device_stats_item */ +BTRFS_SETGET_FUNCS (device_stats_cnt_write_io_errs, + struct btrfs_device_stats_item, cnt_write_io_errs, 64); +BTRFS_SETGET_FUNCS (device_stats_cnt_read_io_errs, + struct btrfs_device_stats_item, cnt_read_io_errs, 64); +BTRFS_SETGET_FUNCS (device_stats_cnt_flush_io_errs, + struct btrfs_device_stats_item, cnt_flush_io_errs, 64); +BTRFS_SETGET_FUNCS (device_stats_cnt_corruption_errs, + struct btrfs_device_stats_item, cnt_corruption_errs, 64); +BTRFS_SETGET_FUNCS (device_stats_cnt_generation_errs, + struct btrfs_device_stats_item, cnt_generation_errs, 64); + +BTRFS_SETGET_STACK_FUNCS (stack_device_stats_cnt_write_io_errs, + struct btrfs_device_stats_item, cnt_write_io_errs, 64); +BTRFS_SETGET_STACK_FUNCS (stack_device_stats_cnt_read_io_errs, + struct btrfs_device_stats_item, cnt_read_io_errs, 64); +BTRFS_SETGET_STACK_FUNCS (stack_device_stats_cnt_flush_io_errs, + struct btrfs_device_stats_item, cnt_flush_io_errs, 64); +BTRFS_SETGET_STACK_FUNCS (stack_device_stats_cnt_corruption_errs, + struct btrfs_device_stats_item, cnt_corruption_errs, +64 ); +BTRFS_SETGET_STACK_FUNCS (stack_device_stats_cnt_generation_errs, + struct btrfs_device_stats_item, cnt_generation_errs, +64 ); +static inline struct btrfs_fs_info *btrfs_sb (struct super_block *sb) {return sb->s_fs_info;
fs/btrfs/disk-io.c +7 -0
@@ -2353,6 +2353,13 @@ retry_root_backup: fs_info->generation = generation; fs_info->last_trans_committed = generation; + ret =btrfs_init_device_stats (fs_info); +if (ret) { +printk (KERN_ERR "btrfs: failed to init device_stats: %d\n" , + ret); + goto fail_block_groups; + } + ret =btrfs_init_space_info (fs_info);if (ret) {printk (KERN_ERR "Failed to initial space info: %d\n" , ret);
fs/btrfs/print-tree.c +3 -0
@@ -294,6 +294,9 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)btrfs_dev_extent_chunk_offset (l, dev_extent), (unsigned long long ) btrfs_dev_extent_length(l, dev_extent)); +case BTRFS_DEVICE_STATS_KEY : +printk (KERN_INFO "\t\tdevice stats\n" ); +break ; }; } }
fs/btrfs/transaction.c +4 -0
@@ -28,6 +28,7 @@#include "locking.h" #include "tree-log.h" #include "inode-map.h" +#include "volumes.h" #define BTRFS_ROOT_TRANS_TAG 0
@@ -758,6 +759,9 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,if (ret)return ret; + ret =btrfs_run_device_stats (trans, root->fs_info); +BUG_ON (ret); +while (!list_empty (&fs_info->dirty_cowonly_roots)) { next = fs_info->dirty_cowonly_roots.next;list_del_init (next);
fs/btrfs/volumes.c +205 -0
237 lines not on this page · raw message
fs/btrfs/volumes.h +9 -0
@@ -108,6 +108,7 @@ struct btrfs_device {/* disk I/O failure stats. For detailed description refer to * struct btrfs_device_stats_item in ctree.h */ +int device_stats_valid;int device_stats_dirty;/* counters need to be written to disk */ atomic_t cnt_write_io_errs;atomic_t cnt_read_io_errs;
@@ -291,6 +292,9 @@ int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, u64 *start, u64 *max_avail);struct btrfs_device *btrfs_find_device_for_logical (struct btrfs_root *root,u64 logical,int mirror_num); +int btrfs_init_device_stats (struct btrfs_fs_info *fs_info); +int btrfs_run_device_stats (struct btrfs_trans_handle *trans, +struct btrfs_fs_info *fs_info);void btrfs_device_stat_print_on_error (struct btrfs_device *device); int btrfs_get_device_stats(struct btrfs_root *root, struct btrfs_ioctl_get_device_stats *stats,
@@ -315,4 +319,9 @@ static inline void btrfs_device_stat_reset(atomic_t *cnt) {atomic_set (cnt,0 ); } + +static inline void btrfs_device_stat_set (atomic_t *cnt,unsigned long val) +{ +atomic_set (cnt, val); +}#endif
Signature · 1 line
1.7.10.2
Re: [PATCH v4 3/3] Btrfs: read device stats on mount, write modified ones during commit
Quoted 16 lines · Stefan Behrens
The device statistics are written into the device tree with each transaction commit. Only modified statistics are written. When a filesystem is mounted, the device statistics for each involved device are read from the device tree and used to initialize the counters.
Signed-off-by Stefan Behrens <[email protected]>--- fs/btrfs/ctree.h | 51 ++++++++++++ fs/btrfs/disk-io.c | 7 ++ fs/btrfs/print-tree.c | 3 + fs/btrfs/transaction.c | 4 + fs/btrfs/volumes.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 9 +++ 6 files changed, 279 insertions(+)
[...]
Quoted 7 lines
fs/btrfs/volumes.c +7
-0+static int update_device_stat_item (struct btrfs_trans_handle *trans, +struct btrfs_root *dev_root, +struct btrfs_device *device) +{ +struct btrfs_path *path; +struct btrfs_key key; +struct extent_buffer *eb;
fs/btrfs/volumes.c error: · line 4740
Quoted 10 lines
fs/btrfs/volumes.c +10
-0+struct btrfs_device_stats_item *ptr; +int ret; + + key.objectid =0 ; + key.type =BTRFS_DEVICE_STATS_KEY ; + key.offset = device->devid; + + path =btrfs_alloc_path (); +BUG_ON (!path); + ret =btrfs_search_slot (trans, dev_root, &key, path,0 ,1 );
Since we may delete this item, I prefer cow: -1,
btrfs_search_slot(trans, dev_root, &key, path, 0, -1);
thanks,
liubo
Quoted 144 lines
fs/btrfs/volumes.c +93
-0+if (ret <0 ) { +printk (KERN_WARNING "btrfs: error %d while searching for device_stats item for device %s!\n" , + ret, device->name); + goto out; + } + +if (ret ==0 && +btrfs_item_size_nr (path->nodes[0 ], path->slots[0 ]) <sizeof (*ptr)) { +/* need to delete old one and insert a new one */ + ret =btrfs_del_item (trans, dev_root, path); +if (ret !=0 ) { +printk (KERN_WARNING "btrfs: delete too small device_stats item for device %s failed %d!\n" , + device->name, ret); + goto out; + } + ret =1 ; + } + +if (ret ==1 ) { +/* need to insert a new item */ +btrfs_release_path (path); + ret =btrfs_insert_empty_item (trans, dev_root, path, + &key,sizeof (*ptr)); +if (ret <0 ) { +printk (KERN_WARNING "btrfs: insert device_stats item for device %s failed %d!\n" , + device->name, ret); + goto out; + } + } + + eb = path->nodes[0 ]; + ptr =btrfs_item_ptr (eb, path->slots[0 ], + struct btrfs_device_stats_item); +btrfs_set_device_stats_cnt_write_io_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_write_io_errs)); +btrfs_set_device_stats_cnt_read_io_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_read_io_errs)); +btrfs_set_device_stats_cnt_flush_io_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_flush_io_errs)); +btrfs_set_device_stats_cnt_corruption_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_corruption_errs)); +btrfs_set_device_stats_cnt_generation_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_generation_errs)); +btrfs_mark_buffer_dirty (eb); + +out: +btrfs_free_path (path); +return ret; +} + +/* +* called from commit_transaction. Writes all changed device stats to disk. +*/ +int btrfs_run_device_stats (struct btrfs_trans_handle *trans, +struct btrfs_fs_info *fs_info) +{ +struct btrfs_root *dev_root = fs_info->dev_root; +struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; +struct btrfs_device *device; +int ret =0 ; + +mutex_lock (&fs_devices->device_list_mutex); +list_for_each_entry (device, &fs_devices->devices, dev_list) { +if (!device->device_stats_valid || !device->device_stats_dirty) +continue ; + + ret =update_device_stat_item (trans, dev_root, device); +if (!ret) + device->device_stats_dirty =0 ; + } +mutex_unlock (&fs_devices->device_list_mutex); + +return ret; +} +void btrfs_device_stat_print_on_error (struct btrfs_device *device) { +if (!device->device_stats_valid) +return ; printk_ratelimited(KERN_ERR "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n", device->name,@@ -4639,6 +4828,18 @@ void btrfs_device_stat_print_on_error(struct btrfs_device *device) &device->cnt_generation_errs)); } +static void btrfs_device_stat_print_on_load (struct btrfs_device *device) +{ +printk (KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u," +" corrupt %u, gen %u\n" , + device->name, +btrfs_device_stat_read (&device->cnt_write_io_errs), +btrfs_device_stat_read (&device->cnt_read_io_errs), +btrfs_device_stat_read (&device->cnt_flush_io_errs), +btrfs_device_stat_read (&device->cnt_corruption_errs), +btrfs_device_stat_read (&device->cnt_generation_errs)); +} +int btrfs_get_device_stats (struct btrfs_root *root,struct btrfs_ioctl_get_device_stats *stats,int reset_after_read)@@ -4654,6 +4855,10 @@ int btrfs_get_device_stats(struct btrfs_root *root,printk (KERN_WARNING "btrfs: get device_stats failed, device not found\n" );return -ENODEV ; + }else if (!dev->device_stats_valid) { +printk (KERN_WARNING +"btrfs: get device_stats failed, not yet valid\n" ); +return -ENODEV ; }else if (reset_after_read) {if (stats->nr_items >=1 ) stats->cnt_write_io_errs =fs/btrfs/volumes.h +9
-0@@ -108,6 +108,7 @@ struct btrfs_device {/* disk I/O failure stats. For detailed description refer to * struct btrfs_device_stats_item in ctree.h */ +int device_stats_valid;int device_stats_dirty;/* counters need to be written to disk */ atomic_t cnt_write_io_errs;atomic_t cnt_read_io_errs;@@ -291,6 +292,9 @@ int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, u64 *start, u64 *max_avail);struct btrfs_device *btrfs_find_device_for_logical (struct btrfs_root *root,u64 logical,int mirror_num); +int btrfs_init_device_stats (struct btrfs_fs_info *fs_info); +int btrfs_run_device_stats (struct btrfs_trans_handle *trans, +struct btrfs_fs_info *fs_info);void btrfs_device_stat_print_on_error (struct btrfs_device *device); int btrfs_get_device_stats(struct btrfs_root *root, struct btrfs_ioctl_get_device_stats *stats,@@ -315,4 +319,9 @@ static inline void btrfs_device_stat_reset(atomic_t *cnt) {atomic_set (cnt,0 ); } + +static inline void btrfs_device_stat_set (atomic_t *cnt,unsigned long val) +{ +atomic_set (cnt, val); +}#endif
Re: [PATCH v4 3/3] Btrfs: read device stats on mount, write modified ones during commit
Quoted 40 lines · Liu Bo
Quoted 16 lines · Stefan Behrens
The device statistics are written into the device tree with each transaction commit. Only modified statistics are written. When a filesystem is mounted, the device statistics for each involved device are read from the device tree and used to initialize the counters.
Signed-off-by Stefan Behrens <[email protected]>--- fs/btrfs/ctree.h | 51 ++++++++++++ fs/btrfs/disk-io.c | 7 ++ fs/btrfs/print-tree.c | 3 + fs/btrfs/transaction.c | 4 + fs/btrfs/volumes.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 9 +++ 6 files changed, 279 insertions(+)[...]
Quoted 7 lines
fs/btrfs/volumes.c +7
-0+static int update_device_stat_item (struct btrfs_trans_handle *trans, +struct btrfs_root *dev_root, +struct btrfs_device *device) +{ +struct btrfs_path *path; +struct btrfs_key key; +struct extent_buffer *eb;fs/btrfs/volumes.c error: · line 4740
Quoted 10 lines
fs/btrfs/volumes.c +10
-0+struct btrfs_device_stats_item *ptr; +int ret; + + key.objectid =0 ; + key.type =BTRFS_DEVICE_STATS_KEY ; + key.offset = device->devid; + + path =btrfs_alloc_path (); +BUG_ON (!path); + ret =btrfs_search_slot (trans, dev_root, &key, path,0 ,1 );Since we may delete this item, I prefer cow: -1,
Quoted 4 lines
btrfs_search_slot(trans, dev_root, &key, path, 0, -1);thanks,
liubo
I am sure you mean to set ins_len (the 5th parameter) to -1 and you are right. Thanks for this finding! I will send a v5.
Quoted 49 lines
Quoted 49 lines
fs/btrfs/volumes.c +49
-0+if (ret <0 ) { +printk (KERN_WARNING "btrfs: error %d while searching for device_stats item for device %s!\n" , + ret, device->name); + goto out; + } + +if (ret ==0 && +btrfs_item_size_nr (path->nodes[0 ], path->slots[0 ]) <sizeof (*ptr)) { +/* need to delete old one and insert a new one */ + ret =btrfs_del_item (trans, dev_root, path); +if (ret !=0 ) { +printk (KERN_WARNING "btrfs: delete too small device_stats item for device %s failed %d!\n" , + device->name, ret); + goto out; + } + ret =1 ; + } + +if (ret ==1 ) { +/* need to insert a new item */ +btrfs_release_path (path); + ret =btrfs_insert_empty_item (trans, dev_root, path, + &key,sizeof (*ptr)); +if (ret <0 ) { +printk (KERN_WARNING "btrfs: insert device_stats item for device %s failed %d!\n" , + device->name, ret); + goto out; + } + } + + eb = path->nodes[0 ]; + ptr =btrfs_item_ptr (eb, path->slots[0 ], + struct btrfs_device_stats_item); +btrfs_set_device_stats_cnt_write_io_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_write_io_errs)); +btrfs_set_device_stats_cnt_read_io_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_read_io_errs)); +btrfs_set_device_stats_cnt_flush_io_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_flush_io_errs)); +btrfs_set_device_stats_cnt_corruption_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_corruption_errs)); +btrfs_set_device_stats_cnt_generation_errs (eb, ptr, +btrfs_device_stat_read (&device->cnt_generation_errs)); +btrfs_mark_buffer_dirty (eb); + +out: +btrfs_free_path (path); +return ret; +}
[...]