[PATCH 5/5] bcache: set writeback_percent in a flexible range

2018-11-22 Thread Coly Li
Because CUTOFF_WRITEBACK is defined as 40, so before the changes of
dynamic cutoff writeback values, writeback_percent is limited to
[0, CUTOFF_WRITEBACK]. Any value larger than CUTOFF_WRITEBACK will
be fixed up to 40.

Now cutof writeback limit is a dynamic value bch_cutoff_writeback,
so the range of writeback_percent can be a more flexible range as
[0, bch_cutoff_writeback]. The flexibility is, it can be expended
to a larger or smaller range than [0, 40], depends on how value
bch_cutoff_writeback is specified.

The default value is still strongly recommended to most of users for
most of workloads. But for people who want to do research on bcache
writeback perforamnce tuning, they may have chance to specify more
flexible writeback_percent in range [0, 70].

Signed-off-by: Coly Li 
---
 drivers/md/bcache/sysfs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 27fbc2dd1734..36de4d52d60a 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -267,7 +267,8 @@ STORE(__cached_dev)
d_strtoul(writeback_running);
d_strtoul(writeback_delay);
 
-   sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
+   sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent,
+   0, bch_cutoff_writeback);
 
if (attr == _writeback_rate) {
ssize_t ret;
-- 
2.16.4



[PATCH 3/5] bcache: add MODULE_DESCRIPTION information

2018-11-22 Thread Coly Li
This patch moves MODULE_AUTHOR and MODULE_LICENSE to end of super.c, and
add MODULE_DESCRIPTION("Bcache: a Linux block layer cache").

This is preparation for adding module parameters.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/super.c | 7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 5b59d44656c0..61d3b63fa617 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -25,9 +25,6 @@
 #include 
 #include 
 
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Kent Overstreet ");
-
 static const char bcache_magic[] = {
0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
@@ -2469,3 +2466,7 @@ static int __init bcache_init(void)
 
 module_exit(bcache_exit);
 module_init(bcache_init);
+
+MODULE_DESCRIPTION("Bcache: a Linux block layer cache");
+MODULE_AUTHOR("Kent Overstreet ");
+MODULE_LICENSE("GPL");
-- 
2.16.4



[PATCH 4/5] bcache: make cutoff_writeback and cutoff_writeback_sync tunnable

2018-11-22 Thread Coly Li
Currently the cutoff writeback and cutoff writeback sync thresholds are
defined by CUTOFF_WRITEBACK (40) and CUTOFF_WRITEBACK_SYNC (70) as static
values. Most of time these they work fine, but when people want to do
research on bcache writeback mode performance tuning, there is no chance
to modify the soft and hard cutoff writeback values.

This patch introduces two module parameters bch_cutoff_writeback_sync and
bch_cutoff_writeback which permit people to tune the values when loading
bcache.ko. If they are not specified by module loading, current values
CUTOFF_WRITEBACK_SYNC and CUTOFF_WRITEBACK will be used as default and
nothing changes.

When people want to tune this two values,
- cutoff_writeback can be set in range [1, 70]
- cutoff_writeback_sync can be set in range [1, 90]
- cutoff_writeback always <= cutoff_writeback_sync

The default values are strongly recommended to most of users for most of
workloads. Anyway, if people wants to take their own risk to do research
on new writeback cutoff tuning for their own workload, now they can
make it.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/super.c | 40 
 drivers/md/bcache/sysfs.c |  7 +++
 drivers/md/bcache/writeback.h | 10 --
 3 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 61d3b63fa617..4dee119c3664 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -25,6 +25,9 @@
 #include 
 #include 
 
+unsigned int bch_cutoff_writeback;
+unsigned int bch_cutoff_writeback_sync;
+
 static const char bcache_magic[] = {
0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca,
0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81
@@ -2420,6 +2423,32 @@ static void bcache_exit(void)
mutex_destroy(_register_lock);
 }
 
+/* Check and fixup module parameters */
+static void check_module_parameters(void)
+{
+   if (bch_cutoff_writeback_sync == 0)
+   bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC;
+   else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) {
+   pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u",
+   bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX);
+   bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX;
+   }
+
+   if (bch_cutoff_writeback == 0)
+   bch_cutoff_writeback = CUTOFF_WRITEBACK;
+   else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) {
+   pr_warn("set bch_cutoff_writeback (%u) to max value %u",
+   bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX);
+   bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX;
+   }
+
+   if (bch_cutoff_writeback > bch_cutoff_writeback_sync) {
+   pr_warn("set bch_cutoff_writeback (%u) to %u",
+   bch_cutoff_writeback, bch_cutoff_writeback_sync);
+   bch_cutoff_writeback = bch_cutoff_writeback_sync;
+   }
+}
+
 static int __init bcache_init(void)
 {
static const struct attribute *files[] = {
@@ -2428,6 +2457,8 @@ static int __init bcache_init(void)
NULL
};
 
+   check_module_parameters();
+
mutex_init(_register_lock);
init_waitqueue_head(_wait);
register_reboot_notifier();
@@ -2464,9 +2495,18 @@ static int __init bcache_init(void)
return -ENOMEM;
 }
 
+/*
+ * Module hooks
+ */
 module_exit(bcache_exit);
 module_init(bcache_init);
 
+module_param(bch_cutoff_writeback, uint, 0);
+MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback");
+
+module_param(bch_cutoff_writeback_sync, uint, 0);
+MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff 
writeback");
+
 MODULE_DESCRIPTION("Bcache: a Linux block layer cache");
 MODULE_AUTHOR("Kent Overstreet ");
 MODULE_LICENSE("GPL");
diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index a38a74dffda4..27fbc2dd1734 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -88,6 +88,8 @@ read_attribute(writeback_keys_done);
 read_attribute(writeback_keys_failed);
 read_attribute(io_errors);
 read_attribute(congested);
+read_attribute(cutoff_writeback);
+read_attribute(cutoff_writeback_sync);
 rw_attribute(congested_read_threshold_us);
 rw_attribute(congested_write_threshold_us);
 
@@ -669,6 +671,9 @@ SHOW(__bch_cache_set)
sysfs_print(congested_write_threshold_us,
c->congested_write_threshold_us);
 
+   sysfs_print(cutoff_writeback, bch_cutoff_writeback);
+   sysfs_print(cutoff_writeback_sync, bch_cutoff_writeback_sync);
+
sysfs_print(active_journal_entries, fifo_used(>journal.pin));
sysfs_printf(verify,"%i", c->verify);
sysfs_printf(key_merging_disabled,  "%i", c->key

[PATCH 1/5] bcache: introduce force_wake_up_gc()

2018-11-22 Thread Coly Li
Garbage collection thread starts to work when c->sectors_to_gc is
negative value, otherwise nothing will happen even the gc thread
is woken up by wake_up_gc().

force_wake_up_gc() sets c->sectors_to_gc to -1 before calling
wake_up_gc(), then gc thread may have chance to run if no one else
sets c->sectors_to_gc to a positive value before gc_should_run().

This routine can be called where the gc thread is woken up and
required to run in force.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/btree.h | 18 ++
 drivers/md/bcache/sysfs.c | 17 ++---
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/drivers/md/bcache/btree.h b/drivers/md/bcache/btree.h
index a68d6c55783b..d1c72ef64edf 100644
--- a/drivers/md/bcache/btree.h
+++ b/drivers/md/bcache/btree.h
@@ -266,6 +266,24 @@ static inline void wake_up_gc(struct cache_set *c)
wake_up(>gc_wait);
 }
 
+static inline void force_wake_up_gc(struct cache_set *c)
+{
+   /*
+* Garbage collection thread only works when sectors_to_gc < 0,
+* calling wake_up_gc() won't start gc thread if sectors_to_gc is
+* not a nagetive value.
+* Therefore sectors_to_gc is set to -1 here, before waking up
+* gc thread by calling wake_up_gc(). Then gc_should_run() will
+* give a chance to permit gc thread to run. "Give a chance" means
+* before going into gc_should_run(), there is still possibility
+* that c->sectors_to_gc being set to other positive value. So
+* this routine won't 100% make sure gc thread will be woken up
+* to run.
+*/
+   atomic_set(>sectors_to_gc, -1);
+   wake_up_gc(c);
+}
+
 #define MAP_DONE   0
 #define MAP_CONTINUE   1
 
diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index d2e5c9892d4d..7351ee4940f3 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -725,21 +725,8 @@ STORE(__bch_cache_set)
bch_cache_accounting_clear(>accounting);
}
 
-   if (attr == _trigger_gc) {
-   /*
-* Garbage collection thread only works when sectors_to_gc < 0,
-* when users write to sysfs entry trigger_gc, most of time
-* they want to forcibly triger gargage collection. Here -1 is
-* set to c->sectors_to_gc, to make gc_should_run() give a
-* chance to permit gc thread to run. "give a chance" means
-* before going into gc_should_run(), there is still chance
-* that c->sectors_to_gc being set to other positive value. So
-* writing sysfs entry trigger_gc won't always make sure gc
-* thread takes effect.
-*/
-   atomic_set(>sectors_to_gc, -1);
-   wake_up_gc(c);
-   }
+   if (attr == _trigger_gc)
+   force_wake_up_gc(c);
 
if (attr == _prune_cache) {
struct shrink_control sc;
-- 
2.16.4



[PATCH 0/5] Writeback performance tuning options

2018-11-22 Thread Coly Li
I receive requirement to provide options to permit people to do research
on writeback performance tuning for their extreme heavy workloads. And
these options are required to be disabled by default to avoid changing
current code behavior.
 
This series adds several disabled-by-default options for writeback
performance tuning.

- Auto gc after writeback accomplished
  sysfs entry gc_after_writeback is added, if it is explicitly enabled,
  gc thread will be forced woken up when writeback accomplished and
  all cache data are clean. This behavior will shrink bcache B+ tree
  and discard clean SSD space, which might be helpful for following
  write request. The following patches cover this option,
  - bcache: introduce force_wake_up_gc()
  - bcache: option to automatically run gc thread after writeback
accomplished

- Tunnable cutoff writeback and cutoff writeback sync
  Currently cutoff writeback and cutoff wrireback sync are fixed value
  defined by macro. There is no way for people to set a larger value and
  test the resulting performance behavior. Now they can be specificed
  as dynamic module load time parameter (bch_cutoff_writeback, and
  bch_cutof_writeback_sync), and won't change after bcache module
  loaded. Now people can test and observe the behavior on their own
  cache device with larger cutoff writeback values. The following
  patches cover these 2 options,
  - bcache: add MODULE_DESCRIPTION information
  - bcache: make cutoff_writeback and cutoff_writeback_sync tunnable
  - bcache: set writeback_percent in a flexible range

- A more flexible writeback_percent range
  Currently writeback_percent is in range of [0, 40], because cutoff
  writeback value is defind as 40 by a macro. This patch permits the
  value to be specified in range [0, bch_cutoff_writeback], while the
  maximum value of bch_cutoff_writeback can be 70. Now people may have
  a more flexible writeback_percent parameter range to test and observe
  how bcache writeback code behaves on their own cache hardware. The
  following patch covers this change,
  - bcache: set writeback_percent in a flexible range

If anyone is also interested on writeback performance tuning with these
tunnable options, I do appreciate if you find a better performance
number with the non-default option values, and share them with us.

Thanks in advance.

Coly Li

---
Coly Li (5):
  bcache: introduce force_wake_up_gc()
  bcache: option to automatically run gc thread after writeback
accomplished
  bcache: add MODULE_DESCRIPTION information
  bcache: make cutoff_writeback and cutoff_writeback_sync tunnable
  bcache: set writeback_percent in a flexible range

 drivers/md/bcache/bcache.h| 14 ++
 drivers/md/bcache/btree.h | 18 +
 drivers/md/bcache/super.c | 45 +--
 drivers/md/bcache/sysfs.c | 36 +++---
 drivers/md/bcache/writeback.c | 27 ++
 drivers/md/bcache/writeback.h | 12 ++--
 6 files changed, 132 insertions(+), 20 deletions(-)

-- 
2.16.4



[PATCH 2/5] bcache: option to automatically run gc thread after writeback accomplished

2018-11-22 Thread Coly Li
The option gc_after_writeback is disabled by default, because garbage
collection will discard SSD data which drops cached data.

Echo 1 into /sys/fs/bcache//internal/gc_after_writeback will enable
this option, which wakes up gc thread when writeback accomplished and all
cached data is clean.

This option is helpful for people who cares writing performance more.
In heavy writing workload, all cached data can be clean only happens when
writeback thread cleans all cached data in I/O idle time. In such
situation a following gc running may help to shrink bcache B+ tree and
discard more clean data, which may be helpful for future writing requests.

If you are not sure whether this is helpful for your own workload, please
leave it as disabled by default.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/bcache.h| 14 ++
 drivers/md/bcache/sysfs.c |  9 +
 drivers/md/bcache/writeback.c | 27 +++
 drivers/md/bcache/writeback.h |  2 ++
 4 files changed, 52 insertions(+)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 96d2213f279e..fdf75352e16a 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -626,6 +626,20 @@ struct cache_set {
/* Where in the btree gc currently is */
struct bkey gc_done;
 
+   /*
+* For automatical garbage collection after writeback completed, this
+* varialbe is used as bit fields,
+* -  0001b (BCH_ENABLE_AUTO_GC): enable gc after writeback
+* -  0010b (BCH_DO_AUTO_GC): do gc after writeback
+* This is an optimization for following write request after writeback
+* finished, but read hit rate dropped due to clean data on cache is
+* discarded. Unless user explicitly sets it via sysfs, it won't be
+* enabled.
+*/
+#define BCH_ENABLE_AUTO_GC 1
+#define BCH_DO_AUTO_GC 2
+   uint8_t gc_after_writeback;
+
/*
 * The allocation code needs gc_mark in struct bucket to be correct, but
 * it's not while a gc is in progress. Protected by bucket_lock.
diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 7351ee4940f3..a38a74dffda4 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -128,6 +128,7 @@ rw_attribute(expensive_debug_checks);
 rw_attribute(cache_replacement_policy);
 rw_attribute(btree_shrinker_disabled);
 rw_attribute(copy_gc_enabled);
+rw_attribute(gc_after_writeback);
 rw_attribute(size);
 
 static ssize_t bch_snprint_string_list(char *buf,
@@ -676,6 +677,7 @@ SHOW(__bch_cache_set)
sysfs_printf(gc_always_rewrite, "%i", c->gc_always_rewrite);
sysfs_printf(btree_shrinker_disabled,   "%i", c->shrinker_disabled);
sysfs_printf(copy_gc_enabled,   "%i", c->copy_gc_enabled);
+   sysfs_printf(gc_after_writeback,"%i", c->gc_after_writeback);
sysfs_printf(io_disable,"%i",
 test_bit(CACHE_SET_IO_DISABLE, >flags));
 
@@ -776,6 +778,12 @@ STORE(__bch_cache_set)
sysfs_strtoul(gc_always_rewrite,c->gc_always_rewrite);
sysfs_strtoul(btree_shrinker_disabled,  c->shrinker_disabled);
sysfs_strtoul(copy_gc_enabled,  c->copy_gc_enabled);
+   /*
+* write gc_after_writeback here may overwrite an already set
+* BCH_DO_AUTO_GC, it doesn't matter because this flag will be
+* set in next chance.
+*/
+   sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
 
return size;
 }
@@ -856,6 +864,7 @@ static struct attribute *bch_cache_set_internal_files[] = {
_gc_always_rewrite,
_btree_shrinker_disabled,
_copy_gc_enabled,
+   _gc_after_writeback,
_io_disable,
NULL
 };
diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
index 08c3a9f9676c..74e3f5f3807b 100644
--- a/drivers/md/bcache/writeback.c
+++ b/drivers/md/bcache/writeback.c
@@ -17,6 +17,15 @@
 #include 
 #include 
 
+static void update_gc_after_writeback(struct cache_set *c)
+{
+   if (c->gc_after_writeback != (BCH_ENABLE_AUTO_GC) ||
+   c->gc_stats.in_use < BCH_AUTO_GC_DIRTY_THRESHOLD)
+   return;
+
+   c->gc_after_writeback |= BCH_DO_AUTO_GC;
+}
+
 /* Rate limiting */
 static uint64_t __calc_target_rate(struct cached_dev *dc)
 {
@@ -191,6 +200,7 @@ static void update_writeback_rate(struct work_struct *work)
if (!set_at_max_writeback_rate(c, dc)) {
down_read(>writeback_lock);
__update_writeback_rate(dc);
+   update_gc_after_writeback(c);
up_read(>writeback_lock);
}
}
@@ -689,6 +699,23 @@ static int bch_writeback_thread(void *arg)
 

Re: [PATCH 07/15] bcache: Populate writeback_rate_minimum attribute

2018-10-08 Thread Coly Li
On 2018/10/8 下午10:19, Jens Axboe wrote:
> On 10/8/18 6:41 AM, Coly Li wrote:
>> From: Ben Peddell 
>>
>> Forgot to include the maintainers with my first email.
>>
>> Somewhere between Michael Lyle's original
>> "bcache: PI controller for writeback rate V2" patch dated 07 Sep 2017
>> and 1d316e6 bcache: implement PI controller for writeback rate,
>> the mapping of the writeback_rate_minimum attribute was dropped.
>>
>> Re-add the missing sysfs writeback_rate_minimum attribute mapping to
>> "allow the user to specify a minimum rate at which dirty blocks are
>> retired."
>>
>> Fixes: 1d316e6 bcache: implement PI controller for writeback rate
> 
> Remember to use the correct format for the Fixes line.
> 

Hi Jens,

This is my fault, it slips from my eyes. I'd like to resend a fixed up
version to you.

BTW, now I only use checkpatches.pl to check patch format, do you use
extra scripts as format checking hooks ?

Thanks.

Coly Li


[PATCH 15/15] bcache: panic fix for making cache device

2018-10-08 Thread Coly Li
From: Dongbo Cao 

when the nbuckets of cache device is smaller than 1024, making cache
device will trigger BUG_ON in kernel, add a condition to avoid this.

Reported-by: nitroxis 
Signed-off-by: Dongbo Cao 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/super.c | 8 
 1 file changed, 8 insertions(+)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 2a362a7ba995..563f75686100 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2070,6 +2070,11 @@ static int cache_alloc(struct cache *ca)
 */
btree_buckets = ca->sb.njournal_buckets ?: 8;
free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
+   if (!free) {
+   ret = -EPERM;
+   err = "ca->sb.nbuckets is too small";
+   goto err_free;
+   }
 
if (!init_fifo(>free[RESERVE_BTREE], btree_buckets,
GFP_KERNEL)) {
@@ -2147,6 +2152,7 @@ static int cache_alloc(struct cache *ca)
 err_prio_alloc:
free_fifo(>free[RESERVE_BTREE]);
 err_btree_alloc:
+err_free:
module_put(THIS_MODULE);
if (err)
pr_notice("error %s: %s", ca->cache_dev_name, err);
@@ -2176,6 +2182,8 @@ static int register_cache(struct cache_sb *sb, struct 
page *sb_page,
blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
if (ret == -ENOMEM)
err = "cache_alloc(): -ENOMEM";
+   else if (ret == -EPERM)
+   err = "cache_alloc(): cache device is too small";
else
err = "cache_alloc(): unknown error";
goto err;
-- 
2.19.0



[PATCH 14/15] bcache: split combined if-condition code into separate ones

2018-10-08 Thread Coly Li
From: Dongbo Cao 

Split the combined '||' statements in if() check, to make the code easier
for debug.

Signed-off-by: Dongbo Cao 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/super.c | 90 +--
 1 file changed, 76 insertions(+), 14 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 719686ea5e27..2a362a7ba995 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2051,6 +2051,8 @@ static int cache_alloc(struct cache *ca)
size_t free;
size_t btree_buckets;
struct bucket *b;
+   int ret = -ENOMEM;
+   const char *err = NULL;
 
__module_get(THIS_MODULE);
kobject_init(>kobj, _cache_ktype);
@@ -2069,26 +2071,86 @@ static int cache_alloc(struct cache *ca)
btree_buckets = ca->sb.njournal_buckets ?: 8;
free = roundup_pow_of_two(ca->sb.nbuckets) >> 10;
 
-   if (!init_fifo(>free[RESERVE_BTREE], btree_buckets, GFP_KERNEL) ||
-   !init_fifo_exact(>free[RESERVE_PRIO], prio_buckets(ca), 
GFP_KERNEL) ||
-   !init_fifo(>free[RESERVE_MOVINGGC], free, GFP_KERNEL) ||
-   !init_fifo(>free[RESERVE_NONE], free, GFP_KERNEL) ||
-   !init_fifo(>free_inc,   free << 2, GFP_KERNEL) ||
-   !init_heap(>heap,   free << 3, GFP_KERNEL) ||
-   !(ca->buckets   = vzalloc(array_size(sizeof(struct bucket),
-ca->sb.nbuckets))) ||
-   !(ca->prio_buckets  = kzalloc(array3_size(sizeof(uint64_t),
- prio_buckets(ca), 2),
- GFP_KERNEL)) ||
-   !(ca->disk_buckets  = alloc_bucket_pages(GFP_KERNEL, ca)))
-   return -ENOMEM;
+   if (!init_fifo(>free[RESERVE_BTREE], btree_buckets,
+   GFP_KERNEL)) {
+   err = "ca->free[RESERVE_BTREE] alloc failed";
+   goto err_btree_alloc;
+   }
+
+   if (!init_fifo_exact(>free[RESERVE_PRIO], prio_buckets(ca),
+   GFP_KERNEL)) {
+   err = "ca->free[RESERVE_PRIO] alloc failed";
+   goto err_prio_alloc;
+   }
+
+   if (!init_fifo(>free[RESERVE_MOVINGGC], free, GFP_KERNEL)) {
+   err = "ca->free[RESERVE_MOVINGGC] alloc failed";
+   goto err_movinggc_alloc;
+   }
+
+   if (!init_fifo(>free[RESERVE_NONE], free, GFP_KERNEL)) {
+   err = "ca->free[RESERVE_NONE] alloc failed";
+   goto err_none_alloc;
+   }
+
+   if (!init_fifo(>free_inc, free << 2, GFP_KERNEL)) {
+   err = "ca->free_inc alloc failed";
+   goto err_free_inc_alloc;
+   }
+
+   if (!init_heap(>heap, free << 3, GFP_KERNEL)) {
+   err = "ca->heap alloc failed";
+   goto err_heap_alloc;
+   }
+
+   ca->buckets = vzalloc(array_size(sizeof(struct bucket),
+ ca->sb.nbuckets));
+   if (!ca->buckets) {
+   err = "ca->buckets alloc failed";
+   goto err_buckets_alloc;
+   }
+
+   ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t),
+  prio_buckets(ca), 2),
+  GFP_KERNEL);
+   if (!ca->prio_buckets) {
+   err = "ca->prio_buckets alloc failed";
+   goto err_prio_buckets_alloc;
+   }
+
+   ca->disk_buckets = alloc_bucket_pages(GFP_KERNEL, ca);
+   if (!ca->disk_buckets) {
+   err = "ca->disk_buckets alloc failed";
+   goto err_disk_buckets_alloc;
+   }
 
ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca);
 
for_each_bucket(b, ca)
atomic_set(>pin, 0);
-
return 0;
+
+err_disk_buckets_alloc:
+   kfree(ca->prio_buckets);
+err_prio_buckets_alloc:
+   vfree(ca->buckets);
+err_buckets_alloc:
+   free_heap(>heap);
+err_heap_alloc:
+   free_fifo(>free_inc);
+err_free_inc_alloc:
+   free_fifo(>free[RESERVE_NONE]);
+err_none_alloc:
+   free_fifo(>free[RESERVE_MOVINGGC]);
+err_movinggc_alloc:
+   free_fifo(>free[RESERVE_PRIO]);
+err_prio_alloc:
+   free_fifo(>free[RESERVE_BTREE]);
+err_btree_alloc:
+   module_put(THIS_MODULE);
+   if (err)
+   pr_notice("error %s: %s", ca->cache_dev_name, err);
+   return ret;
 }
 
 static int register_cache(struct cache_sb *sb, struct page *sb_page,
-- 
2.19.0



[PATCH 12/15] bcache: replace hard coded number with BUCKET_GC_GEN_MAX

2018-10-08 Thread Coly Li
In extents.c:bch_extent_bad(), number 96 is used as parameter to call
btree_bug_on(). The purpose is to check whether stale gen value exceeds
BUCKET_GC_GEN_MAX, so it is better to use macro BUCKET_GC_GEN_MAX to
make the code more understandable.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/extents.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/extents.c b/drivers/md/bcache/extents.c
index c809724e6571..956004366699 100644
--- a/drivers/md/bcache/extents.c
+++ b/drivers/md/bcache/extents.c
@@ -553,7 +553,7 @@ static bool bch_extent_bad(struct btree_keys *bk, const 
struct bkey *k)
for (i = 0; i < KEY_PTRS(k); i++) {
stale = ptr_stale(b->c, k, i);
 
-   btree_bug_on(stale > 96, b,
+   btree_bug_on(stale > BUCKET_GC_GEN_MAX, b,
 "key too stale: %i, need_gc %u",
 stale, b->c->need_gc);
 
-- 
2.19.0



[PATCH 13/15] bcache: use MAX_CACHES_PER_SET instead of magic number 8 in __bch_bucket_alloc_set

2018-10-08 Thread Coly Li
From: Shenghui Wang 

Current cache_set has MAX_CACHES_PER_SET caches most, and the macro
is used for
"
struct cache *cache_by_alloc[MAX_CACHES_PER_SET];
"
in the define of struct cache_set.

Use MAX_CACHES_PER_SET instead of magic number 8 in
__bch_bucket_alloc_set.

Signed-off-by: Shenghui Wang 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 7a28232d868b..5002838ea476 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -484,7 +484,7 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned 
int reserve,
int i;
 
lockdep_assert_held(>bucket_lock);
-   BUG_ON(!n || n > c->caches_loaded || n > 8);
+   BUG_ON(!n || n > c->caches_loaded || n > MAX_CACHES_PER_SET);
 
bkey_init(k);
 
-- 
2.19.0



[PATCH 09/15] bcache: recal cached_dev_sectors on detach

2018-10-08 Thread Coly Li
From: Shenghui Wang 

Recal cached_dev_sectors on cached_dev detached, as recal done on
cached_dev attached.

Update the cached_dev_sectors before bcache_device_detach called
as bcache_device_detach will set bcache_device->c to NULL.

Signed-off-by: Shenghui Wang 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/super.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 4989c7d4d4d0..dc392a485269 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1008,6 +1008,7 @@ static void cached_dev_detach_finish(struct work_struct 
*w)
bch_write_bdev_super(dc, );
closure_sync();
 
+   calc_cached_dev_sectors(dc->disk.c);
bcache_device_detach(>disk);
list_move(>list, _devices);
 
-- 
2.19.0



[PATCH 11/15] bcache: remove useless parameter of bch_debug_init()

2018-10-08 Thread Coly Li
From: Dongbo Cao 

Parameter "struct kobject *kobj" in bch_debug_init() is useless,
remove it in this patch.

Signed-off-by: Dongbo Cao 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/bcache.h | 2 +-
 drivers/md/bcache/debug.c  | 2 +-
 drivers/md/bcache/super.c  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 83504dd8100a..5cfa1ec1b7a1 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -1003,7 +1003,7 @@ void bch_open_buckets_free(struct cache_set *c);
 int bch_cache_allocator_start(struct cache *ca);
 
 void bch_debug_exit(void);
-void bch_debug_init(struct kobject *kobj);
+void bch_debug_init(void);
 void bch_request_exit(void);
 int bch_request_init(void);
 
diff --git a/drivers/md/bcache/debug.c b/drivers/md/bcache/debug.c
index 06da66b2488a..8f448b9c96a1 100644
--- a/drivers/md/bcache/debug.c
+++ b/drivers/md/bcache/debug.c
@@ -253,7 +253,7 @@ void bch_debug_exit(void)
debugfs_remove_recursive(bcache_debug);
 }
 
-void __init bch_debug_init(struct kobject *kobj)
+void __init bch_debug_init(void)
 {
/*
 * it is unnecessary to check return value of
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index dc392a485269..719686ea5e27 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2381,7 +2381,7 @@ static int __init bcache_init(void)
sysfs_create_files(bcache_kobj, files))
goto err;
 
-   bch_debug_init(bcache_kobj);
+   bch_debug_init();
closure_debug_init();
 
return 0;
-- 
2.19.0



[PATCH 10/15] bcache: remove unused bch_passthrough_cache

2018-10-08 Thread Coly Li
From: Shenghui Wang 

struct kmem_cache *bch_passthrough_cache is not used in
bcache code. Remove it.

Signed-off-by: Shenghui Wang 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/request.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/request.h b/drivers/md/bcache/request.h
index aa055cfeb099..721bf336ed1a 100644
--- a/drivers/md/bcache/request.h
+++ b/drivers/md/bcache/request.h
@@ -39,6 +39,6 @@ void bch_data_insert(struct closure *cl);
 void bch_cached_dev_request_init(struct cached_dev *dc);
 void bch_flash_dev_request_init(struct bcache_device *d);
 
-extern struct kmem_cache *bch_search_cache, *bch_passthrough_cache;
+extern struct kmem_cache *bch_search_cache;
 
 #endif /* _BCACHE_REQUEST_H_ */
-- 
2.19.0



[PATCH 06/15] bcache: correct dirty data statistics

2018-10-08 Thread Coly Li
From: Tang Junhui 

When bcache device is clean, dirty keys may still exist after
journal replay, so we need to count these dirty keys even
device in clean status, otherwise after writeback, the amount
of dirty data would be incorrect.

Signed-off-by: Tang Junhui 
Cc: sta...@vger.kernel.org
Signed-off-by: Coly Li 
---
 drivers/md/bcache/super.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index a99af19d2f91..4989c7d4d4d0 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1152,11 +1152,12 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct 
cache_set *c,
}
 
if (BDEV_STATE(>sb) == BDEV_STATE_DIRTY) {
-   bch_sectors_dirty_init(>disk);
atomic_set(>has_dirty, 1);
bch_writeback_queue(dc);
}
 
+   bch_sectors_dirty_init(>disk);
+
bch_cached_dev_run(dc);
bcache_device_link(>disk, c, "bdev");
atomic_inc(>attached_dev_nr);
-- 
2.19.0



[PATCH 07/15] bcache: Populate writeback_rate_minimum attribute

2018-10-08 Thread Coly Li
From: Ben Peddell 

Forgot to include the maintainers with my first email.

Somewhere between Michael Lyle's original
"bcache: PI controller for writeback rate V2" patch dated 07 Sep 2017
and 1d316e6 bcache: implement PI controller for writeback rate,
the mapping of the writeback_rate_minimum attribute was dropped.

Re-add the missing sysfs writeback_rate_minimum attribute mapping to
"allow the user to specify a minimum rate at which dirty blocks are
retired."

Fixes: 1d316e6 bcache: implement PI controller for writeback rate
Signed-off-by: Ben Peddell 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/sysfs.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 150cf4f4cf74..26f035a0c5b9 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -285,6 +285,7 @@ STORE(__cached_dev)
1, WRITEBACK_RATE_UPDATE_SECS_MAX);
d_strtoul(writeback_rate_i_term_inverse);
d_strtoul_nonzero(writeback_rate_p_term_inverse);
+   d_strtoul_nonzero(writeback_rate_minimum);
 
sysfs_strtoul_clamp(io_error_limit, dc->error_limit, 0, INT_MAX);
 
@@ -412,6 +413,7 @@ static struct attribute *bch_cached_dev_files[] = {
_writeback_rate_update_seconds,
_writeback_rate_i_term_inverse,
_writeback_rate_p_term_inverse,
+   _writeback_rate_minimum,
_writeback_rate_debug,
_errors,
_io_error_limit,
-- 
2.19.0



[PATCH 08/15] bcache: fix miss key refill->end in writeback

2018-10-08 Thread Coly Li
From: Tang Junhui 

refill->end record the last key of writeback, for example, at the first
time, keys (1,128K) to (1,1024K) are flush to the backend device, but
the end key (1,1024K) is not included, since the bellow code:
if (bkey_cmp(k, refill->end) >= 0) {
ret = MAP_DONE;
goto out;
}
And in the next time when we refill writeback keybuf again, we searched
key start from (1,1024K), and got a key bigger than it, so the key
(1,1024K) missed.
This patch modify the above code, and let the end key to be included to
the writeback key buffer.

Signed-off-by: Tang Junhui 
Cc: sta...@vger.kernel.org
Signed-off-by: Coly Li 
---
 drivers/md/bcache/btree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index e7d4817681f2..3f4211b5cd33 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -2434,7 +2434,7 @@ static int refill_keybuf_fn(struct btree_op *op, struct 
btree *b,
struct keybuf *buf = refill->buf;
int ret = MAP_CONTINUE;
 
-   if (bkey_cmp(k, refill->end) >= 0) {
+   if (bkey_cmp(k, refill->end) > 0) {
ret = MAP_DONE;
goto out;
}
-- 
2.19.0



[PATCH 00/15] bcache patches for 4.20

2018-10-08 Thread Coly Li
Hi Jens,

Here are patches for Linux 4.20. In this run we don't have big changes,
most of the patches for fixes or code cleanup.

Ben Peddell resends a patch which had no response for a while, I pick
it for this version. Our new developer Dongbo Cao contributes a bug fix
to avoid kernel panic for too small cache set size, with other code
cleanup changes. Shenghui and me have several code cleanup and minor
fixes, and Junhui again contributes helpful fixes for several real bugs.

Please pull them for 4.20 merge window. Thanks in advance.

Coly Li
---

Ben Peddell (1):
  bcache: Populate writeback_rate_minimum attribute

Coly Li (3):
  bcache: use REQ_PRIO to indicate bio for metadata
  bcache: fix typo in code comments of closure_return_with_destructor()
  bcache: replace hard coded number with BUCKET_GC_GEN_MAX

Dongbo Cao (3):
  bcache: remove useless parameter of bch_debug_init()
  bcache: split combined if-condition code into separate ones
  bcache: panic fix for making cache device

Shenghui Wang (4):
  bcache: account size of buckets used in uuid write to
ca->meta_sectors_written
  bcache: recal cached_dev_sectors on detach
  bcache: remove unused bch_passthrough_cache
  bcache: use MAX_CACHES_PER_SET instead of magic number 8 in
__bch_bucket_alloc_set

Tang Junhui (4):
  bcache: trace missed reading by cache_missed
  bcache: fix ioctl in flash device
  bcache: correct dirty data statistics
  bcache: fix miss key refill->end in writeback

 drivers/md/bcache/alloc.c   |   2 +-
 drivers/md/bcache/bcache.h  |   2 +-
 drivers/md/bcache/btree.c   |   2 +-
 drivers/md/bcache/closure.h |   3 +-
 drivers/md/bcache/debug.c   |   2 +-
 drivers/md/bcache/extents.c |   2 +-
 drivers/md/bcache/request.c |   9 ++-
 drivers/md/bcache/request.h |   2 +-
 drivers/md/bcache/super.c   | 113 +---
 drivers/md/bcache/sysfs.c   |   2 +
 10 files changed, 109 insertions(+), 30 deletions(-)

-- 
2.19.0



[PATCH 05/15] bcache: fix typo in code comments of closure_return_with_destructor()

2018-10-08 Thread Coly Li
The code comments of closure_return_with_destructor() in closure.h makrs
function name as closure_return(). This patch fixes this type with the
correct name - closure_return_with_destructor.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/closure.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/md/bcache/closure.h b/drivers/md/bcache/closure.h
index eca0d496b686..c88cdc4ae4ec 100644
--- a/drivers/md/bcache/closure.h
+++ b/drivers/md/bcache/closure.h
@@ -345,7 +345,8 @@ do {
\
 } while (0)
 
 /**
- * closure_return - finish execution of a closure, with destructor
+ * closure_return_with_destructor - finish execution of a closure,
+ * with destructor
  *
  * Works like closure_return(), except @destructor will be called when all
  * outstanding refs on @cl have been dropped; @destructor may be used to safely
-- 
2.19.0



[PATCH 04/15] bcache: fix ioctl in flash device

2018-10-08 Thread Coly Li
From: Tang Junhui 

When doing ioctl in flash device, it will call ioctl_dev() in super.c,
then we should not to get cached device since flash only device has
no backend device. This patch just move the jugement dc->io_disable
to cached_dev_ioctl() to make ioctl in flash device correctly.

Fixes: 0f0709e6bfc3c ("bcache: stop bcache device when backing device is 
offline")
Signed-off-by: Tang Junhui 
Cc: sta...@vger.kernel.org
Signed-off-by: Coly Li 
---
 drivers/md/bcache/request.c | 3 +++
 drivers/md/bcache/super.c   | 4 
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index ee15fb039fd0..3bf35914bb57 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -1218,6 +1218,9 @@ static int cached_dev_ioctl(struct bcache_device *d, 
fmode_t mode,
 {
struct cached_dev *dc = container_of(d, struct cached_dev, disk);
 
+   if (dc->io_disable)
+   return -EIO;
+
return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
 }
 
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 448e531e8c2d..a99af19d2f91 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -647,10 +647,6 @@ static int ioctl_dev(struct block_device *b, fmode_t mode,
 unsigned int cmd, unsigned long arg)
 {
struct bcache_device *d = b->bd_disk->private_data;
-   struct cached_dev *dc = container_of(d, struct cached_dev, disk);
-
-   if (dc->io_disable)
-   return -EIO;
 
return d->ioctl(d, mode, cmd, arg);
 }
-- 
2.19.0



[PATCH 02/15] bcache: trace missed reading by cache_missed

2018-10-08 Thread Coly Li
From: Tang Junhui 

Missed reading IOs are identified by s->cache_missed, not the
s->cache_miss, so in trace_bcache_read() using trace_bcache_read
to identify whether the IO is missed or not.

Signed-off-by: Tang Junhui 
Cc: sta...@vger.kernel.org
Signed-off-by: Coly Li 
---
 drivers/md/bcache/request.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 51be355a3309..4946d486f734 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -850,7 +850,7 @@ static void cached_dev_read_done_bh(struct closure *cl)
 
bch_mark_cache_accounting(s->iop.c, s->d,
  !s->cache_missed, s->iop.bypass);
-   trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
+   trace_bcache_read(s->orig_bio, !s->cache_missed, s->iop.bypass);
 
if (s->iop.status)
continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
-- 
2.19.0



[PATCH 03/15] bcache: use REQ_PRIO to indicate bio for metadata

2018-10-08 Thread Coly Li
In cached_dev_cache_miss() and check_should_bypass(), REQ_META is used
to check whether a bio is for metadata request. REQ_META is used for
blktrace, the correct REQ_ flag should be REQ_PRIO. This flag means the
bio should be prior to other bio, and frequently be used to indicate
metadata io in file system code.

This patch replaces REQ_META with correct flag REQ_PRIO.

CC Adam Manzanares because he explains to me what REQ_PRIO is for.

Signed-off-by: Coly Li 
Cc: Adam Manzanares 
---
 drivers/md/bcache/request.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 4946d486f734..ee15fb039fd0 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -395,7 +395,7 @@ static bool check_should_bypass(struct cached_dev *dc, 
struct bio *bio)
 * unless the read-ahead request is for metadata (eg, for gfs2).
 */
if (bio->bi_opf & (REQ_RAHEAD|REQ_BACKGROUND) &&
-   !(bio->bi_opf & REQ_META))
+   !(bio->bi_opf & REQ_PRIO))
goto skip;
 
if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) ||
@@ -877,7 +877,7 @@ static int cached_dev_cache_miss(struct btree *b, struct 
search *s,
}
 
if (!(bio->bi_opf & REQ_RAHEAD) &&
-   !(bio->bi_opf & REQ_META) &&
+   !(bio->bi_opf & REQ_PRIO) &&
s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
reada = min_t(sector_t, dc->readahead >> 9,
  get_capacity(bio->bi_disk) - bio_end_sector(bio));
-- 
2.19.0



[PATCH 01/15] bcache: account size of buckets used in uuid write to ca->meta_sectors_written

2018-10-08 Thread Coly Li
From: Shenghui Wang 

UUIDs are considered as metadata. __uuid_write should add the number
of buckets (in sectors) written to disk to ca->meta_sectors_written.
Currently only 1 bucket is used in uuid write.

Steps to test:
1) create a fresh backing device and a fresh cache device separately.
   The backing device didn't attach to any cache set.
2) cd /sys/block//bcache
   cat metadata_written  // record the output value
   cat bucket_size
3) attach the backing device to cache set
4) cat metadata_written
   The output value is almost the same as the value in step 2
   before the change.
   After the change, the value is bigger about 1 bucket size.

Signed-off-by: Shenghui Wang 
Reviewed-by: Tang Junhui 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/super.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 94c756c66bd7..448e531e8c2d 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -417,6 +417,7 @@ static int __uuid_write(struct cache_set *c)
 {
BKEY_PADDED(key) k;
struct closure cl;
+   struct cache *ca;
 
closure_init_stack();
lockdep_assert_held(_register_lock);
@@ -428,6 +429,10 @@ static int __uuid_write(struct cache_set *c)
uuid_io(c, REQ_OP_WRITE, 0, , );
closure_sync();
 
+   /* Only one bucket used for uuid write */
+   ca = PTR_CACHE(c, , 0);
+   atomic_long_add(ca->sb.bucket_size, >meta_sectors_written);
+
bkey_copy(>uuid_bucket, );
bkey_put(c, );
return 0;
-- 
2.19.0



Re: [PATCH] bcache: correct dirty data statistics

2018-09-30 Thread Coly Li



On 9/1/18 5:34 PM, Junhui Tang wrote:

 From fb4bdec3fce8016e813b4b8da6e705fb65d910f2 Mon Sep 17 00:00:00 2001
From: Tang Junhui 
Date: Sun, 2 Sep 2018 01:22:22 +0800
Subject: [PATCH] bcache: correct dirty data statistics

When bcache device is clean, dirty keys may still exist after
journal replay, so we need to count these dirty keys even
device in clean status, otherwise after writeback, the amount
of dirty data would be incorrect.

Signed-off-by: Tang Junhui 
---


Nice catch. Added to my for-next, and CC sta...@vger.kernel.org.

Thanks.

Coly Li


  drivers/md/bcache/super.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 55a3764..c5f0f65 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1139,11 +1139,12 @@ int bch_cached_dev_attach(struct cached_dev
*dc, struct cache_set *c,
   }

   if (BDEV_STATE(>sb) == BDEV_STATE_DIRTY) {
- bch_sectors_dirty_init(>disk);
   atomic_set(>has_dirty, 1);
   bch_writeback_queue(dc);
   }

+ bch_sectors_dirty_init(>disk);
+
   bch_cached_dev_run(dc);
   bcache_device_link(>disk, c, "bdev");
   atomic_inc(>attached_dev_nr);


Re: [PATCH] bcache: fix ioctl in flash device

2018-09-30 Thread Coly Li



On 9/7/18 8:01 PM, Junhui Tang wrote:

 From 799ad1d9443a28eb640c8dbc3abe8a224f3a1292 Mon Sep 17 00:00:00 2001
From: Tang Junhui 
Date: Sat, 8 Sep 2018 03:51:30 +0800
Subject: [PATCH] bcache: fix ioctl in flash device

When doing ioctl in flash device, it will call ioctl_dev() in super.c,
then we should not to get cached device since flash only device has
no backend device. This patch just move the jugement dc->io_disable
to cached_dev_ioctl() to make ioctl in flash device correctly.

Signed-off-by: Tang Junhui 


Nice catch, I will add a Fixes: 0f0709e6bfc3c line in the commit log.

Have it in my for-next. Thanks.


Coly Li


---
  drivers/md/bcache/request.c | 3 +++
  drivers/md/bcache/super.c   | 4 
  2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 7dbe8b6..bafc85e 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -1212,6 +1212,9 @@ static int cached_dev_ioctl(struct bcache_device
*d, fmode_t mode,
   unsigned int cmd, unsigned long arg)
  {
   struct cached_dev *dc = container_of(d, struct cached_dev, disk);
+
+ if (dc->io_disable)
+ return -EIO;
   return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
  }

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 55a3764..ced830e 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -636,10 +636,6 @@ static int ioctl_dev(struct block_device *b, fmode_t mode,
unsigned int cmd, unsigned long arg)
  {
   struct bcache_device *d = b->bd_disk->private_data;
- struct cached_dev *dc = container_of(d, struct cached_dev, disk);
-
- if (dc->io_disable)
- return -EIO;

   return d->ioctl(d, mode, cmd, arg);
  }


[PATCH 0/1] bcache fix for 4.19-rc6

2018-09-27 Thread Coly Li
Hi Jens,

Guoju Fang just posts a bug fix to solve a bcache journal deadlock.
This bug probably happens when system memory is in heavy usage,
the deadlock is observed occasionally for a long while.
If it is too late to go into Linux 4.19, I will submit to you in
4.20 merge window, but it will be cool to have it in this run.

Thanks.

Coly Li

guoju (1):
  bcache: add separate workqueue for journal_write to avoid deadlock

 drivers/md/bcache/bcache.h  | 1 +
 drivers/md/bcache/journal.c | 6 +++---
 drivers/md/bcache/super.c   | 8 
 3 files changed, 12 insertions(+), 3 deletions(-)

-- 
2.19.0



[PATCH 1/1] bcache: add separate workqueue for journal_write to avoid deadlock

2018-09-27 Thread Coly Li
From: guoju 

After write SSD completed, bcache schedules journal_write work to
system_wq, which is a public workqueue in system, without WQ_MEM_RECLAIM
flag. system_wq is also a bound wq, and there may be no idle kworker on
current processor. Creating a new kworker may unfortunately need to
reclaim memory first, by shrinking cache and slab used by vfs, which
depends on bcache device. That's a deadlock.

This patch create a new workqueue for journal_write with WQ_MEM_RECLAIM
flag. It's rescuer thread will work to avoid the deadlock.

Signed-off-by: guoju 
Cc: sta...@vger.kernel.org
Signed-off-by: Coly Li 
---
 drivers/md/bcache/bcache.h  | 1 +
 drivers/md/bcache/journal.c | 6 +++---
 drivers/md/bcache/super.c   | 8 
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 83504dd8100a..954dad29e6e8 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -965,6 +965,7 @@ void bch_prio_write(struct cache *ca);
 void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent);
 
 extern struct workqueue_struct *bcache_wq;
+extern struct workqueue_struct *bch_journal_wq;
 extern struct mutex bch_register_lock;
 extern struct list_head bch_cache_sets;
 
diff --git a/drivers/md/bcache/journal.c b/drivers/md/bcache/journal.c
index 6116bbf870d8..522c7426f3a0 100644
--- a/drivers/md/bcache/journal.c
+++ b/drivers/md/bcache/journal.c
@@ -485,7 +485,7 @@ static void do_journal_discard(struct cache *ca)
 
closure_get(>set->cl);
INIT_WORK(>discard_work, journal_discard_work);
-   schedule_work(>discard_work);
+   queue_work(bch_journal_wq, >discard_work);
}
 }
 
@@ -592,7 +592,7 @@ static void journal_write_done(struct closure *cl)
: >w[0];
 
__closure_wake_up(>wait);
-   continue_at_nobarrier(cl, journal_write, system_wq);
+   continue_at_nobarrier(cl, journal_write, bch_journal_wq);
 }
 
 static void journal_write_unlock(struct closure *cl)
@@ -627,7 +627,7 @@ static void journal_write_unlocked(struct closure *cl)
spin_unlock(>journal.lock);
 
btree_flush_write(c);
-   continue_at(cl, journal_write, system_wq);
+   continue_at(cl, journal_write, bch_journal_wq);
return;
}
 
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 94c756c66bd7..30ba9aeb5ee8 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -47,6 +47,7 @@ static int bcache_major;
 static DEFINE_IDA(bcache_device_idx);
 static wait_queue_head_t unregister_wait;
 struct workqueue_struct *bcache_wq;
+struct workqueue_struct *bch_journal_wq;
 
 #define BTREE_MAX_PAGES(256 * 1024 / PAGE_SIZE)
 /* limitation of partitions number on single bcache device */
@@ -2341,6 +2342,9 @@ static void bcache_exit(void)
kobject_put(bcache_kobj);
if (bcache_wq)
destroy_workqueue(bcache_wq);
+   if (bch_journal_wq)
+   destroy_workqueue(bch_journal_wq);
+
if (bcache_major)
unregister_blkdev(bcache_major, "bcache");
unregister_reboot_notifier();
@@ -2370,6 +2374,10 @@ static int __init bcache_init(void)
if (!bcache_wq)
goto err;
 
+   bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0);
+   if (!bch_journal_wq)
+   goto err;
+
bcache_kobj = kobject_create_and_add("bcache", fs_kobj);
if (!bcache_kobj)
goto err;
-- 
2.19.0



Re: [PATCH] bcache: use REQ_PRIO to indicate bio for metadata

2018-09-25 Thread Coly Li



On 9/26/18 12:32 AM, Adam Manzanares wrote:


On 9/25/18 9:12 AM, Coly Li wrote:

In cached_dev_cache_miss() and check_should_bypass(), REQ_META is used
to check whether a bio is for metadata request. REQ_META is used for
blktrace, the correct REQ_ flag should be REQ_PRIO. This flag means the
bio should be prior to other bio, and frequently be used to indicate
metadata io in file system code.

This patch replaces REQ_META with correct flag REQ_PRIO.

CC Adam Manzanares because he explains to me what REQ_PRIO is for.

Sorry for the confusion, I was talking about using the bi_ioprio field
to pass ioprio information down to devices that support prioritized
commands. I haven't used the REQ_PRIO flag in the work that I have done.


Hi Adam,

Aha, I misunderstood you. After 'git blame' I realize REQ_PRIO is 
invented by Christoph Hellwig,


    Add a new REQ_PRIO to let requests preempt others in the cfq I/O 
schedule,

    and lave REQ_META purely for marking requests as metadata in blktrace.

From log of commit 65299a3b788b ("block: separate priority boosting 
from REQ_META") it seems this patch still uses REQ_PRIO in a correct 
way. And, thank you for clarifying this :-)


Coly Li


Signed-off-by: Coly Li 
Cc: Adam Manzanares 
---
   drivers/md/bcache/request.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 51be355a3309..13d3355a90c0 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -395,7 +395,7 @@ static bool check_should_bypass(struct cached_dev *dc, 
struct bio *bio)
 * unless the read-ahead request is for metadata (eg, for gfs2).
 */
if (bio->bi_opf & (REQ_RAHEAD|REQ_BACKGROUND) &&
-   !(bio->bi_opf & REQ_META))
+   !(bio->bi_opf & REQ_PRIO))
goto skip;
   
   	if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) ||

@@ -877,7 +877,7 @@ static int cached_dev_cache_miss(struct btree *b, struct 
search *s,
}
   
   	if (!(bio->bi_opf & REQ_RAHEAD) &&

-   !(bio->bi_opf & REQ_META) &&
+   !(bio->bi_opf & REQ_PRIO) &&
s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
reada = min_t(sector_t, dc->readahead >> 9,
  get_capacity(bio->bi_disk) - bio_end_sector(bio));


[PATCH] bcache: use REQ_PRIO to indicate bio for metadata

2018-09-25 Thread Coly Li
In cached_dev_cache_miss() and check_should_bypass(), REQ_META is used
to check whether a bio is for metadata request. REQ_META is used for
blktrace, the correct REQ_ flag should be REQ_PRIO. This flag means the
bio should be prior to other bio, and frequently be used to indicate
metadata io in file system code.

This patch replaces REQ_META with correct flag REQ_PRIO.

CC Adam Manzanares because he explains to me what REQ_PRIO is for.

Signed-off-by: Coly Li 
Cc: Adam Manzanares 
---
 drivers/md/bcache/request.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 51be355a3309..13d3355a90c0 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -395,7 +395,7 @@ static bool check_should_bypass(struct cached_dev *dc, 
struct bio *bio)
 * unless the read-ahead request is for metadata (eg, for gfs2).
 */
if (bio->bi_opf & (REQ_RAHEAD|REQ_BACKGROUND) &&
-   !(bio->bi_opf & REQ_META))
+   !(bio->bi_opf & REQ_PRIO))
goto skip;
 
if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) ||
@@ -877,7 +877,7 @@ static int cached_dev_cache_miss(struct btree *b, struct 
search *s,
}
 
if (!(bio->bi_opf & REQ_RAHEAD) &&
-   !(bio->bi_opf & REQ_META) &&
+   !(bio->bi_opf & REQ_PRIO) &&
s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
reada = min_t(sector_t, dc->readahead >> 9,
  get_capacity(bio->bi_disk) - bio_end_sector(bio));
-- 
2.19.0



Re: [PATCH v2] bcache: release dc->writeback_lock properly in bch_writeback_thread()

2018-08-22 Thread Coly Li
On 2018/8/23 2:02 AM, Coly Li wrote:
> From: Shan Hai 
> 
> The writeback thread would exit with a lock held when the cache device is
> detached via sysfs interface, fix it by releasing the held lock before exiting
> the while-loop.
> 
> Fixes: fadd94e05c02 (bcache: quit dc->writeback_thread when 
> BCACHE_DEV_DETACHING is set)
> Signed-off-by: Shan Hai 
> Signed-off-by: Coly Li 
> Tested-by: Shenghui Wang 
> Cc: sta...@vger.kernel.org #4.17+
> ---
> Changelog:
> v2: add Fixes tag by Coly Li.
> v1: initial patch from Shan Hai.

Hi Jens,

Please pick this patch for your next pull request. The change from Shan
Hai fixes a real issue which is reported and lately verified by Shenghui
Wang.

Thanks in advance.

Coly Li

> 
>  drivers/md/bcache/writeback.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
> index 6be05bd7ca67..08c3a9f9676c 100644
> --- a/drivers/md/bcache/writeback.c
> +++ b/drivers/md/bcache/writeback.c
> @@ -685,8 +685,10 @@ static int bch_writeback_thread(void *arg)
>* data on cache. BCACHE_DEV_DETACHING flag is set in
>* bch_cached_dev_detach().
>*/
> - if (test_bit(BCACHE_DEV_DETACHING, >disk.flags))
> + if (test_bit(BCACHE_DEV_DETACHING, >disk.flags)) {
> + up_write(>writeback_lock);
>   break;
> + }
>   }
>  
>   up_write(>writeback_lock);
> 



[PATCH v2] bcache: release dc->writeback_lock properly in bch_writeback_thread()

2018-08-22 Thread Coly Li
From: Shan Hai 

The writeback thread would exit with a lock held when the cache device is
detached via sysfs interface, fix it by releasing the held lock before exiting
the while-loop.

Fixes: fadd94e05c02 (bcache: quit dc->writeback_thread when 
BCACHE_DEV_DETACHING is set)
Signed-off-by: Shan Hai 
Signed-off-by: Coly Li 
Tested-by: Shenghui Wang 
Cc: sta...@vger.kernel.org #4.17+
---
Changelog:
v2: add Fixes tag by Coly Li.
v1: initial patch from Shan Hai.

 drivers/md/bcache/writeback.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
index 6be05bd7ca67..08c3a9f9676c 100644
--- a/drivers/md/bcache/writeback.c
+++ b/drivers/md/bcache/writeback.c
@@ -685,8 +685,10 @@ static int bch_writeback_thread(void *arg)
 * data on cache. BCACHE_DEV_DETACHING flag is set in
 * bch_cached_dev_detach().
 */
-   if (test_bit(BCACHE_DEV_DETACHING, >disk.flags))
+   if (test_bit(BCACHE_DEV_DETACHING, >disk.flags)) {
+   up_write(>writeback_lock);
break;
+   }
}
 
up_write(>writeback_lock);
-- 
2.18.0



[PATCH] bcache: release dc->writeback_lock properly in bch_writeback_thread()

2018-08-22 Thread Coly Li
From: Shan Hai 

The writeback thread would exit with a lock held when the cache device is
detached via sysfs interface, fix it by releasing the held lock before exiting
the while-loop.

Signed-off-by: Shan Hai 
Signed-off-by: Coly Li 
Tested-by: Shenghui Wang 
Cc: sta...@vger.kernel.org #4.17+
---
 drivers/md/bcache/writeback.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
index 6be05bd7ca67..08c3a9f9676c 100644
--- a/drivers/md/bcache/writeback.c
+++ b/drivers/md/bcache/writeback.c
@@ -685,8 +685,10 @@ static int bch_writeback_thread(void *arg)
 * data on cache. BCACHE_DEV_DETACHING flag is set in
 * bch_cached_dev_detach().
 */
-   if (test_bit(BCACHE_DEV_DETACHING, >disk.flags))
+   if (test_bit(BCACHE_DEV_DETACHING, >disk.flags)) {
+   up_write(>writeback_lock);
break;
+   }
}
 
up_write(>writeback_lock);
-- 
2.18.0



Re: [PATCH 1/1] bcache: release the lock before stopping the writeback thread

2018-08-22 Thread Coly Li
Hi Shan,

On 2018/8/22 3:00 PM, Shan Hai wrote:
> The writeback thread would exit with a lock held when the cache device is
> detached via sysfs interface, fix it by releasing the held lock before exiting
> the thread.

I will change "the thread" to "the while-loop" when I apply this patch.

> 
> Signed-off-by: Shan Hai 

Nice catch!

This one should go to stable trees since 4.17 as well. I will CC stable
maintainers when I submit this one for 4.19.

Thank you for the fix.

Coly Li

> ---
>  drivers/md/bcache/writeback.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
> index 481d4cf..adc583f 100644
> --- a/drivers/md/bcache/writeback.c
> +++ b/drivers/md/bcache/writeback.c
> @@ -679,8 +679,10 @@ static int bch_writeback_thread(void *arg)
>* data on cache. BCACHE_DEV_DETACHING flag is set in
>* bch_cached_dev_detach().
>*/
> - if (test_bit(BCACHE_DEV_DETACHING, >disk.flags))
> + if (test_bit(BCACHE_DEV_DETACHING, >disk.flags)) {
> + up_write(>writeback_lock);
>   break;
> + }
>   }
>  
>   up_write(>writeback_lock);
> 



Re: [PATCH] bcache: fix 0day error of setting writeback_rate by sysfs interface

2018-08-12 Thread Coly Li
On 2018/8/12 11:10 PM, Jens Axboe wrote:
> On 8/10/18 10:49 PM, Coly Li wrote:
>> On 2018/8/11 2:19 AM, Jens Axboe wrote:
>>> On 8/10/18 9:45 AM, Coly Li wrote:
>>>> Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request
>>>> is idle") changes struct bch_ratelimit member rate from uint32_t to
>>>> atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c
>>>> to set new writeback rate, after the input is converted from memory
>>>> buf to long int by sysfs_strtoul_clamp().
>>>>
>>>> The above change has a problem because there is an implicit return
>>>> inside sysfs_strtoul_clamp() so the following atomic_long_set()
>>>> won't be called. This error is detected by 0day system with following
>>>> snipped smatch warnings:
>>>>
>>>> drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized
>>>> symbol 'v'.
>>>> 270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
>>>>  ^^^
>>>> @271 atomic_long_set(>writeback_rate.rate, v);
>>>>
>>>> This patch fixes the above error by using strtoul_safe_clamp() to
>>>> convert the input buffer into a long int type result.
>>>>
>>>> Fixes: Commit ea8c5356d390 ("bcache: set max writeback rate when I/O 
>>>> request is idle")
>>>> Signed-off-by: Coly Li 
>>>> Cc: sta...@vger.kernel.org #4.16+
>>>> Cc: Kai Krakow 
>>>> Cc: Stefan Priebe 
>>>
>>
>> Hi Jens,
>>
>>> You don't need to CC stable for this, the commit isn't in a released kernel
>>> yet. In fact, it's not even in Linus's tree, it's just queued up for 4.19.
>>>
>>
>> What I thought was, Commit ea8c5356d390 ("bcache: set max writeback rate
>> when I/O request is idle") is a stable fix, so this patch should go into
>> stable as well. Otherwise the stable kernel cannot manually set
>> writeback_rate via sysfs interface.
> 
> It's not necessary when new patch is correctly marked as fixing one that
> is going to stable.
> 

Copied. Thanks for the hint.

Coly Li


Re: [PATCH v3 00/17] bcache for 4.19, 3rd wave

2018-08-11 Thread Coly Li
On 2018/8/12 5:42 AM, Jens Axboe wrote:
> On 8/10/18 11:19 PM, Coly Li wrote:
>> Hi Jens,
>>
>> This series contains several minor fixes and code cleanup reported by
>> scripts/checkpatch.pl. After this series, there are still a few warning
>> from checkpatch.pl, but I think they are necessary to be the way they
>> are and don't change them. For example, error message exceeds 80 chars
>> line limit.
> 
> It's a little late to apply these at this point, since 4.18 will be
> released tomorrow. I can queue them up for later in the merge
> window, for a post pull.
> 

Hi Jens

Sure, thank you.

Coly Li


Re: [PATCH v3 00/17] bcache for 4.19, 3rd wave

2018-08-10 Thread Coly Li
On 2018/8/11 1:19 PM, Coly Li wrote:
> Hi Jens,
> 
> This series contains several minor fixes and code cleanup reported by
> scripts/checkpatch.pl. After this series, there are still a few warning
> from checkpatch.pl, but I think they are necessary to be the way they
> are and don't change them. For example, error message exceeds 80 chars
> line limit.
> 
> Thanks in advance for any comment or review.
> 

BTW, all these patches are reviewed by Shenghui Wang, and pass my
testing procedure. It is good to pick them for 4.19 for now.

Thanks.

Coly Li



[PATCH v3 08/17] bcache: replace '%pF' by '%pS' in seq_printf()

2018-08-10 Thread Coly Li
'%pF' and '%pf' are deprecated vsprintf pointer extensions, this patch
replace them by '%pS', which is suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/closure.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/closure.c b/drivers/md/bcache/closure.c
index 8570fc426e31..034067e0e9ce 100644
--- a/drivers/md/bcache/closure.c
+++ b/drivers/md/bcache/closure.c
@@ -168,7 +168,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
list_for_each_entry(cl, _list, all) {
int r = atomic_read(>remaining);
 
-   seq_printf(f, "%p: %pF -> %pf p %p r %i ",
+   seq_printf(f, "%p: %pS -> %pS p %p r %i ",
   cl, (void *) cl->ip, cl->fn, cl->parent,
   r & CLOSURE_REMAINING_MASK);
 
@@ -178,7 +178,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
   r & CLOSURE_RUNNING  ? "R" : "");
 
if (r & CLOSURE_WAITING)
-   seq_printf(f, " W %pF\n",
+   seq_printf(f, " W %pS\n",
   (void *) cl->waiting_on);
 
seq_printf(f, "\n");
-- 
2.18.0



[PATCH v3 09/17] bcache: fix typo 'succesfully' to 'successfully'

2018-08-10 Thread Coly Li
This patch fixes typo 'succesfully' to correct 'successfully', which is
suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/io.c  | 2 +-
 drivers/md/bcache/request.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/io.c b/drivers/md/bcache/io.c
index cfc56add799a..c25097968319 100644
--- a/drivers/md/bcache/io.c
+++ b/drivers/md/bcache/io.c
@@ -86,7 +86,7 @@ void bch_count_io_errors(struct cache *ca,
 
/*
 * First we subtract refresh from count; each time we
-* succesfully do so, we rescale the errors once:
+* successfully do so, we rescale the errors once:
 */
 
count = atomic_cmpxchg(>io_count, old, new);
diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 858dd3da9dc5..55264e71369d 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -154,7 +154,7 @@ static void bch_data_insert_error(struct closure *cl)
 
/*
 * Our data write just errored, which means we've got a bunch of keys to
-* insert that point to data that wasn't succesfully written.
+* insert that point to data that wasn't successfully written.
 *
 * We don't have to insert those keys but we still have to invalidate
 * that region of the cache - so, if we just strip off all the pointers
-- 
2.18.0



[PATCH v3 12/17] bcache: fix code comments style

2018-08-10 Thread Coly Li
This patch fixes 3 style issues warned by checkpatch.pl,
- Comment lines are not aligned
- Comments use "/*" on subsequent lines
- Comment lines use a trailing "*/"

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bset.c  |  9 ++---
 drivers/md/bcache/super.c | 22 +-
 drivers/md/bcache/writeback.c |  3 ++-
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index b6a3f9d291a9..8f07fa6e1739 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -402,7 +402,8 @@ static unsigned int inorder_prev(unsigned int j, unsigned 
int size)
return j;
 }
 
-/* I have no idea why this code works... and I'm the one who wrote it
+/*
+ * I have no idea why this code works... and I'm the one who wrote it
  *
  * However, I do know what it does:
  * Given a binary tree constructed in an array (i.e. how you normally implement
@@ -795,7 +796,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
if (!t->size)
return;
 
-   /* k is the key we just inserted; we need to find the entry in the
+   /*
+* k is the key we just inserted; we need to find the entry in the
 * lookup table for the first key that is strictly greater than k:
 * it's either k's cacheline or the next one
 */
@@ -803,7 +805,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
   table_to_bkey(t, j) <= k)
j++;
 
-   /* Adjust all the lookup table entries, and find a new key for any that
+   /*
+* Adjust all the lookup table entries, and find a new key for any that
 * have gotten too big
 */
for (; j < t->size; j++) {
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 2a7be104557e..01fc3c015a58 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -465,8 +465,8 @@ static struct uuid_entry *uuid_find_empty(struct cache_set 
*c)
  * Bucket priorities/gens:
  *
  * For each bucket, we store on disk its
-   * 8 bit gen
-   * 16 bit priority
+ *   8 bit gen
+ *  16 bit priority
  *
  * See alloc.c for an explanation of the gen. The priority is used to implement
  * lru (and in the future other) cache replacement policies; for most purposes
@@ -934,8 +934,10 @@ void bch_cached_dev_run(struct cached_dev *dc)
 
add_disk(d->disk);
bd_link_disk_holder(dc->bdev, dc->disk.disk);
-   /* won't show up in the uevent file, use udevadm monitor -e instead
-* only class / kset properties are persistent */
+   /*
+* won't show up in the uevent file, use udevadm monitor -e instead
+* only class / kset properties are persistent
+*/
kobject_uevent_env(_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
kfree(env[1]);
kfree(env[2]);
@@ -1104,8 +1106,9 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct 
cache_set *c,
}
}
 
-   /* Deadlocks since we're called via sysfs...
-   sysfs_remove_file(>kobj, _attach);
+   /*
+* Deadlocks since we're called via sysfs...
+* sysfs_remove_file(>kobj, _attach);
 */
 
if (bch_is_zero(u->uuid, 16)) {
@@ -1468,9 +1471,10 @@ bool bch_cache_set_error(struct cache_set *c, const char 
*fmt, ...)
if (test_and_set_bit(CACHE_SET_IO_DISABLE, >flags))
pr_info("CACHE_SET_IO_DISABLE already set");
 
-   /* XXX: we can be called from atomic context
-   acquire_console_sem();
-   */
+   /*
+* XXX: we can be called from atomic context
+* acquire_console_sem();
+*/
 
pr_err("bcache: error on %pU: ", c->sb.set_uuid);
 
diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
index e40bf0e403e7..6be05bd7ca67 100644
--- a/drivers/md/bcache/writeback.c
+++ b/drivers/md/bcache/writeback.c
@@ -468,7 +468,8 @@ static void read_dirty(struct cached_dev *dc)
 
down(>in_flight);
 
-   /* We've acquired a semaphore for the maximum
+   /*
+* We've acquired a semaphore for the maximum
 * simultaneous number of writebacks; from here
 * everything happens asynchronously.
 */
-- 
2.18.0



[PATCH v3 16/17] bcache: remove unnecessary space before ioctl function pointer arguments

2018-08-10 Thread Coly Li
This is warned by checkpatch.pl, this patch removes the extra space.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bcache.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 66e6d5639b38..83504dd8100a 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -275,8 +275,8 @@ struct bcache_device {
 
int (*cache_miss)(struct btree *b, struct search *s,
  struct bio *bio, unsigned int sectors);
-   int (*ioctl) (struct bcache_device *d, fmode_t mode,
- unsigned int cmd, unsigned long arg);
+   int (*ioctl)(struct bcache_device *d, fmode_t mode,
+unsigned int cmd, unsigned long arg);
 };
 
 struct io {
-- 
2.18.0



[PATCH v3 14/17] bcache: move open brace at end of function definitions to next line

2018-08-10 Thread Coly Li
This is not a preferred style to place open brace '{' at the end of
function definition, checkpatch.pl reports error for such coding
style. This patch moves them into the start of the next new line.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/super.c | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 01fc3c015a58..e3ecf08a10fc 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2152,7 +2152,8 @@ static ssize_t register_bcache(struct kobject *k, struct 
kobj_attribute *attr,
 kobj_attribute_write(register, register_bcache);
 kobj_attribute_write(register_quiet,   register_bcache);
 
-static bool bch_is_open_backing(struct block_device *bdev) {
+static bool bch_is_open_backing(struct block_device *bdev)
+{
struct cache_set *c, *tc;
struct cached_dev *dc, *t;
 
@@ -2166,7 +2167,8 @@ static bool bch_is_open_backing(struct block_device 
*bdev) {
return false;
 }
 
-static bool bch_is_open_cache(struct block_device *bdev) {
+static bool bch_is_open_cache(struct block_device *bdev)
+{
struct cache_set *c, *tc;
struct cache *ca;
unsigned int i;
@@ -2178,7 +2180,8 @@ static bool bch_is_open_cache(struct block_device *bdev) {
return false;
 }
 
-static bool bch_is_open(struct block_device *bdev) {
+static bool bch_is_open(struct block_device *bdev)
+{
return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
 }
 
-- 
2.18.0



[PATCH v3 11/17] bcache: do not check NULL pointer before calling kmem_cache_destroy

2018-08-10 Thread Coly Li
kmem_cache_destroy() is safe for NULL pointer as input, the NULL pointer
checking is unncessary. This patch just removes the NULL pointer checking
to make code simpler.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/request.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 55264e71369d..51be355a3309 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -1367,8 +1367,7 @@ void bch_flash_dev_request_init(struct bcache_device *d)
 
 void bch_request_exit(void)
 {
-   if (bch_search_cache)
-   kmem_cache_destroy(bch_search_cache);
+   kmem_cache_destroy(bch_search_cache);
 }
 
 int __init bch_request_init(void)
-- 
2.18.0



[PATCH v3 17/17] bcache: add the missing comments for smp_mb()/smp_wmb()

2018-08-10 Thread Coly Li
Checkpatch.pl warns there are 2 locations of smp_mb() and smp_wmb()
without code comment. This patch adds the missing code comments for
these memory barrier calls.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/closure.h | 4 +++-
 drivers/md/bcache/super.c   | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/closure.h b/drivers/md/bcache/closure.h
index 7f3594c0be14..eca0d496b686 100644
--- a/drivers/md/bcache/closure.h
+++ b/drivers/md/bcache/closure.h
@@ -289,10 +289,12 @@ static inline void closure_init_stack(struct closure *cl)
 }
 
 /**
- * closure_wake_up - wake up all closures on a wait list.
+ * closure_wake_up - wake up all closures on a wait list,
+ *  with memory barrier
  */
 static inline void closure_wake_up(struct closure_waitlist *list)
 {
+   /* Memory barrier for the wait list */
smp_mb();
__closure_wake_up(list);
 }
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index e637d74e2908..94c756c66bd7 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1136,11 +1136,11 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct 
cache_set *c,
list_move(>list, >cached_devs);
calc_cached_dev_sectors(c);
 
-   smp_wmb();
/*
 * dc->c must be set before dc->count != 0 - paired with the mb in
 * cached_dev_get()
 */
+   smp_wmb();
refcount_set(>count, 1);
 
/* Block writeback thread, but spawn it */
-- 
2.18.0



[PATCH v3 13/17] bcache: add static const prefix to char * array declarations

2018-08-10 Thread Coly Li
This patch declares char * array with const prefix in sysfs.c,
which is suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 2ed503ab9e5c..543b06408321 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -150,7 +150,7 @@ SHOW(__bch_cached_dev)
 {
struct cached_dev *dc = container_of(kobj, struct cached_dev,
 disk.kobj);
-   const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
+   char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
int wb = dc->writeback_running;
 
 #define var(stat)  (dc->stat)
-- 
2.18.0



[PATCH v3 10/17] bcache: prefer 'help' in Kconfig

2018-08-10 Thread Coly Li
Current bcache Kconfig uses '---help---' as header of help information,
for now 'help' is prefered. This patch fixes this style by replacing
'---help---' by 'help' in bcache Kconfig file.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/Kconfig | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/Kconfig b/drivers/md/bcache/Kconfig
index 17bf109c58e9..817b9fba50db 100644
--- a/drivers/md/bcache/Kconfig
+++ b/drivers/md/bcache/Kconfig
@@ -1,7 +1,7 @@
 
 config BCACHE
tristate "Block device as cache"
-   ---help---
+   help
Allows a block device to be used as cache for other devices; uses
a btree for indexing and the layout is optimized for SSDs.
 
@@ -10,7 +10,7 @@ config BCACHE
 config BCACHE_DEBUG
bool "Bcache debugging"
depends on BCACHE
-   ---help---
+   help
Don't select this option unless you're a developer
 
Enables extra debugging tools, allows expensive runtime checks to be
@@ -20,7 +20,7 @@ config BCACHE_CLOSURES_DEBUG
bool "Debug closures"
depends on BCACHE
select DEBUG_FS
-   ---help---
+   help
Keeps all active closures in a linked list and provides a debugfs
interface to list them, which makes it possible to see asynchronous
operations that get stuck.
-- 
2.18.0



[PATCH v3 15/17] bcache: add missing SPDX header

2018-08-10 Thread Coly Li
The SPDX header is missing fro closure.c, super.c and util.c, this
patch adds SPDX header for GPL-2.0 into these files.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/closure.c | 1 +
 drivers/md/bcache/super.c   | 1 +
 drivers/md/bcache/util.c| 1 +
 3 files changed, 3 insertions(+)

diff --git a/drivers/md/bcache/closure.c b/drivers/md/bcache/closure.c
index 034067e0e9ce..73f5319295bc 100644
--- a/drivers/md/bcache/closure.c
+++ b/drivers/md/bcache/closure.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Asynchronous refcounty things
  *
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index e3ecf08a10fc..e637d74e2908 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * bcache setup/teardown code, and some metadata io - read a superblock and
  * figure out what to do with it.
diff --git a/drivers/md/bcache/util.c b/drivers/md/bcache/util.c
index 18016e7bb32c..e873b0f7a82a 100644
--- a/drivers/md/bcache/util.c
+++ b/drivers/md/bcache/util.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * random utiility code, for bcache but in theory not specific to bcache
  *
-- 
2.18.0



[PATCH v3 07/17] bcache: fix indent by replacing blank by tabs

2018-08-10 Thread Coly Li
bch_btree_insert_check_key() has unaligned indent, or indent by blank
characters. This patch makes the indent aligned and replace blank by
tabs.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/btree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index c2e4174a2e03..e7d4817681f2 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -2231,10 +2231,10 @@ int bch_btree_insert_check_key(struct btree *b, struct 
btree_op *op,
rw_lock(true, b, b->level);
 
if (b->key.ptr[0] != btree_ptr ||
-   b->seq != seq + 1) {
+   b->seq != seq + 1) {
op->lock = b->level;
goto out;
-   }
+   }
}
 
SET_KEY_PTRS(check_key, 1);
-- 
2.18.0



[PATCH v3 00/17] bcache for 4.19, 3rd wave

2018-08-10 Thread Coly Li
Hi Jens,

This series contains several minor fixes and code cleanup reported by
scripts/checkpatch.pl. After this series, there are still a few warning
from checkpatch.pl, but I think they are necessary to be the way they
are and don't change them. For example, error message exceeds 80 chars
line limit.

Thanks in advance for any comment or review.

Changelog:
v3: Fix coments from Shenghui Wang.
v2: Fix comments from Shenghui Wang.
v1: Initial version

Coly Li 
---
Coly Li (17):
  bcache: style fix to replace 'unsigned' by 'unsigned int'
  bcache: style fix to add a blank line after declarations
  bcache: add identifier names to arguments of function definitions
  bcache: style fixes for lines over 80 characters
  bcache: replace Symbolic permissions by octal permission numbers
  bcache: replace printk() by pr_*() routines
  bcache: fix indent by replacing blank by tabs
  bcache: replace '%pF' by '%pS' in seq_printf()
  bcache: fix typo 'succesfully' to 'successfully'
  bcache: prefer 'help' in Kconfig
  bcache: do not check NULL pointer before calling kmem_cache_destroy
  bcache: fix code comments style
  bcache: add static const prefix to char * array declarations
  bcache: move open brace at end of function definitions to next line
  bcache: add missing SPDX header
  bcache: remove unnecessary space before ioctl function pointer
arguments
  bcache: add the missing comments for smp_mb()/smp_wmb()

 drivers/md/bcache/Kconfig |   6 +-
 drivers/md/bcache/alloc.c |  39 ---
 drivers/md/bcache/bcache.h| 210 +-
 drivers/md/bcache/bset.c  | 142 +--
 drivers/md/bcache/bset.h  | 146 ---
 drivers/md/bcache/btree.c |  72 +++-
 drivers/md/bcache/btree.h |  86 +++---
 drivers/md/bcache/closure.c   |   6 +-
 drivers/md/bcache/closure.h   |   6 +-
 drivers/md/bcache/debug.c |  23 ++--
 drivers/md/bcache/debug.h |   6 +-
 drivers/md/bcache/extents.c   |  37 +++---
 drivers/md/bcache/extents.h   |   6 +-
 drivers/md/bcache/io.c|  24 ++--
 drivers/md/bcache/journal.c   |  27 +++--
 drivers/md/bcache/journal.h   |  28 ++---
 drivers/md/bcache/movinggc.c  |  14 ++-
 drivers/md/bcache/request.c   |  61 +-
 drivers/md/bcache/request.h   |  18 +--
 drivers/md/bcache/stats.c |  15 ++-
 drivers/md/bcache/stats.h |  15 ++-
 drivers/md/bcache/super.c | 107 ++---
 drivers/md/bcache/sysfs.c |  36 +++---
 drivers/md/bcache/sysfs.h |   6 +-
 drivers/md/bcache/util.c  |   2 +
 drivers/md/bcache/util.h  |  24 ++--
 drivers/md/bcache/writeback.c |  30 +++--
 drivers/md/bcache/writeback.h |  19 +--
 include/uapi/linux/bcache.h   |   8 +-
 29 files changed, 679 insertions(+), 540 deletions(-)

-- 
2.18.0



[PATCH v3 06/17] bcache: replace printk() by pr_*() routines

2018-08-10 Thread Coly Li
There are still many places in bcache use printk to display kernel
message, which are suggested to be preplaced by pr_*() routines like
pr_err(), pr_info(), or pr_notice().

This patch replaces all printk() with a proper pr_*() routine for
bcache code.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bset.c|  8 
 drivers/md/bcache/debug.c   | 10 +-
 drivers/md/bcache/extents.c |  8 
 drivers/md/bcache/super.c   |  4 ++--
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index 19b4febe5b45..b6a3f9d291a9 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -25,18 +25,18 @@ void bch_dump_bset(struct btree_keys *b, struct bset *i, 
unsigned int set)
for (k = i->start; k < bset_bkey_last(i); k = next) {
next = bkey_next(k);
 
-   printk(KERN_ERR "block %u key %u/%u: ", set,
+   pr_err("block %u key %u/%u: ", set,
   (unsigned int) ((u64 *) k - i->d), i->keys);
 
if (b->ops->key_dump)
b->ops->key_dump(b, k);
else
-   printk("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
+   pr_err("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
 
if (next < bset_bkey_last(i) &&
bkey_cmp(k, b->ops->is_extents ?
 _KEY(next) : next) > 0)
-   printk(KERN_ERR "Key skipped backwards\n");
+   pr_err("Key skipped backwards\n");
}
 }
 
@@ -482,7 +482,7 @@ void inorder_test(void)
unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
 
if (!(size % 4096))
-   printk(KERN_NOTICE "loop %u, %llu per us\n", size,
+   pr_notice("loop %u, %llu per us\n", size,
   done / ktime_us_delta(ktime_get(), start));
 
while (1) {
diff --git a/drivers/md/bcache/debug.c b/drivers/md/bcache/debug.c
index a8f70c916fdb..06da66b2488a 100644
--- a/drivers/md/bcache/debug.c
+++ b/drivers/md/bcache/debug.c
@@ -74,28 +74,28 @@ void bch_btree_verify(struct btree *b)
 
console_lock();
 
-   printk(KERN_ERR "*** in memory:\n");
+   pr_err("*** in memory:\n");
bch_dump_bset(>keys, inmemory, 0);
 
-   printk(KERN_ERR "*** read back in:\n");
+   pr_err("*** read back in:\n");
bch_dump_bset(>keys, sorted, 0);
 
for_each_written_bset(b, ondisk, i) {
unsigned int block = ((void *) i - (void *) ondisk) /
block_bytes(b->c);
 
-   printk(KERN_ERR "*** on disk block %u:\n", block);
+   pr_err("*** on disk block %u:\n", block);
bch_dump_bset(>keys, i, block);
}
 
-   printk(KERN_ERR "*** block %zu not written\n",
+   pr_err("*** block %zu not written\n",
   ((void *) i - (void *) ondisk) / block_bytes(b->c));
 
for (j = 0; j < inmemory->keys; j++)
if (inmemory->d[j] != sorted->d[j])
break;
 
-   printk(KERN_ERR "b->written %u\n", b->written);
+   pr_err("b->written %u\n", b->written);
 
console_unlock();
panic("verify failed at %u\n", j);
diff --git a/drivers/md/bcache/extents.c b/drivers/md/bcache/extents.c
index cb3b2c613ed6..c809724e6571 100644
--- a/drivers/md/bcache/extents.c
+++ b/drivers/md/bcache/extents.c
@@ -130,18 +130,18 @@ static void bch_bkey_dump(struct btree_keys *keys, const 
struct bkey *k)
char buf[80];
 
bch_extent_to_text(buf, sizeof(buf), k);
-   printk(" %s", buf);
+   pr_err(" %s", buf);
 
for (j = 0; j < KEY_PTRS(k); j++) {
size_t n = PTR_BUCKET_NR(b->c, k, j);
 
-   printk(" bucket %zu", n);
+   pr_err(" bucket %zu", n);
if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
-   printk(" prio %i",
+   pr_err(" prio %i",
   PTR_BUCKET(b->c, k, j)->prio);
}
 
-   printk(" %s\n", bch_ptr_status(b->c, k));
+   pr_err(" %s\n", bch_ptr_status(b->c, k));
 }
 
 /* Btree ptrs */
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 296fc8c31c6c.

[PATCH v3 03/17] bcache: add identifier names to arguments of function definitions

2018-08-10 Thread Coly Li
There are many function definitions do not have identifier argument names,
scripts/checkpatch.pl complains warnings like this,

 WARNING: function definition argument 'struct bcache_device *' should
  also have an identifier name
  #16735: FILE: writeback.h:120:
  +void bch_sectors_dirty_init(struct bcache_device *);

This patch adds identifier argument names to all bcache function
definitions to fix such warnings.

Signed-off-by: Coly Li 
Reviewed: Shenghui Wang 
---
 drivers/md/bcache/bcache.h| 112 +++---
 drivers/md/bcache/bset.h  | 126 +++---
 drivers/md/bcache/btree.c |   6 +-
 drivers/md/bcache/btree.h |  80 ++---
 drivers/md/bcache/debug.h |   6 +-
 drivers/md/bcache/extents.h   |   6 +-
 drivers/md/bcache/journal.c   |   2 +-
 drivers/md/bcache/journal.h   |  20 +++---
 drivers/md/bcache/request.c   |   2 +-
 drivers/md/bcache/request.h   |   2 +-
 drivers/md/bcache/stats.h |  13 ++--
 drivers/md/bcache/super.c |   4 +-
 drivers/md/bcache/util.h  |  12 ++--
 drivers/md/bcache/writeback.h |   9 +--
 14 files changed, 215 insertions(+), 185 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index fd74dd075951..0bd505c61943 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -273,9 +273,10 @@ struct bcache_device {
 
unsigned intdata_csum:1;
 
-   int (*cache_miss)(struct btree *, struct search *,
- struct bio *, unsigned int);
-   int (*ioctl) (struct bcache_device *, fmode_t, unsigned int, unsigned 
long);
+   int (*cache_miss)(struct btree *b, struct search *s,
+ struct bio *bio, unsigned int sectors);
+   int (*ioctl) (struct bcache_device *d, fmode_t mode,
+ unsigned int cmd, unsigned long arg);
 };
 
 struct io {
@@ -925,41 +926,43 @@ static inline void wait_for_kthread_stop(void)
 /* Forward declarations */
 
 void bch_count_backing_io_errors(struct cached_dev *dc, struct bio *bio);
-void bch_count_io_errors(struct cache *, blk_status_t, int, const char *);
-void bch_bbio_count_io_errors(struct cache_set *, struct bio *,
- blk_status_t, const char *);
-void bch_bbio_endio(struct cache_set *, struct bio *, blk_status_t,
-   const char *);
-void bch_bbio_free(struct bio *, struct cache_set *);
-struct bio *bch_bbio_alloc(struct cache_set *);
-
-void __bch_submit_bbio(struct bio *, struct cache_set *);
-void bch_submit_bbio(struct bio *, struct cache_set *,
-struct bkey *, unsigned int);
-
-uint8_t bch_inc_gen(struct cache *, struct bucket *);
-void bch_rescale_priorities(struct cache_set *, int);
-
-bool bch_can_invalidate_bucket(struct cache *, struct bucket *);
-void __bch_invalidate_one_bucket(struct cache *, struct bucket *);
-
-void __bch_bucket_free(struct cache *, struct bucket *);
-void bch_bucket_free(struct cache_set *, struct bkey *);
-
-long bch_bucket_alloc(struct cache *, unsigned int, bool);
-int __bch_bucket_alloc_set(struct cache_set *, unsigned int,
-  struct bkey *, int, bool);
-int bch_bucket_alloc_set(struct cache_set *, unsigned int,
-struct bkey *, int, bool);
-bool bch_alloc_sectors(struct cache_set *, struct bkey *, unsigned int,
-  unsigned int, unsigned int, bool);
+void bch_count_io_errors(struct cache *ca, blk_status_t error,
+int is_read, const char *m);
+void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
+ blk_status_t error, const char *m);
+void bch_bbio_endio(struct cache_set *c, struct bio *bio,
+   blk_status_t error, const char *m);
+void bch_bbio_free(struct bio *bio, struct cache_set *c);
+struct bio *bch_bbio_alloc(struct cache_set *c);
+
+void __bch_submit_bbio(struct bio *bio, struct cache_set *c);
+void bch_submit_bbio(struct bio *bio, struct cache_set *c,
+struct bkey *k, unsigned int ptr);
+
+uint8_t bch_inc_gen(struct cache *ca, struct bucket *b);
+void bch_rescale_priorities(struct cache_set *c, int sectors);
+
+bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b);
+void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b);
+
+void __bch_bucket_free(struct cache *ca, struct bucket *b);
+void bch_bucket_free(struct cache_set *c, struct bkey *k);
+
+long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait);
+int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
+  struct bkey *k, int n, bool wait);
+int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
+struct bkey *k, int n, bool wait);
+bool bch_alloc_sectors(struct cache_set *c, struct bkey *k,
+  unsigned int sectors, unsigned int write_point,
+  unsigned int

[PATCH v3 01/17] bcache: style fix to replace 'unsigned' by 'unsigned int'

2018-08-10 Thread Coly Li
This patch fixes warning reported by checkpatch.pl by replacing 'unsigned'
with 'unsigned int'.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/alloc.c |  36 ++-
 drivers/md/bcache/bcache.h| 107 +++
 drivers/md/bcache/bset.c  | 114 ++
 drivers/md/bcache/bset.h  |  34 +-
 drivers/md/bcache/btree.c |  50 +++
 drivers/md/bcache/btree.h |   4 +-
 drivers/md/bcache/closure.h   |   2 +-
 drivers/md/bcache/debug.c |   6 +-
 drivers/md/bcache/extents.c   |  22 +++
 drivers/md/bcache/io.c|  18 +++---
 drivers/md/bcache/journal.c   |  20 +++---
 drivers/md/bcache/journal.h   |   8 +--
 drivers/md/bcache/movinggc.c  |  12 ++--
 drivers/md/bcache/request.c   |  42 ++---
 drivers/md/bcache/request.h   |  18 +++---
 drivers/md/bcache/stats.c |  12 ++--
 drivers/md/bcache/stats.h |   2 +-
 drivers/md/bcache/super.c |  34 +-
 drivers/md/bcache/sysfs.c |  18 +++---
 drivers/md/bcache/util.h  |   9 +--
 drivers/md/bcache/writeback.c |  19 +++---
 drivers/md/bcache/writeback.h |  12 ++--
 include/uapi/linux/bcache.h   |   6 +-
 23 files changed, 309 insertions(+), 296 deletions(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 7fa2631b422c..89f663d22551 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -87,8 +87,8 @@ void bch_rescale_priorities(struct cache_set *c, int sectors)
 {
struct cache *ca;
struct bucket *b;
-   unsigned next = c->nbuckets * c->sb.bucket_size / 1024;
-   unsigned i;
+   unsigned int next = c->nbuckets * c->sb.bucket_size / 1024;
+   unsigned int i;
int r;
 
atomic_sub(sectors, >rescale);
@@ -169,7 +169,7 @@ static void bch_invalidate_one_bucket(struct cache *ca, 
struct bucket *b)
 
 #define bucket_prio(b) \
 ({ \
-   unsigned min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
+   unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
\
(b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b);  \
 })
@@ -301,7 +301,7 @@ do {
\
 
 static int bch_allocator_push(struct cache *ca, long bucket)
 {
-   unsigned i;
+   unsigned int i;
 
/* Prios/gens are actually the most important reserve */
if (fifo_push(>free[RESERVE_PRIO], bucket))
@@ -385,7 +385,7 @@ static int bch_allocator_thread(void *arg)
 
 /* Allocation */
 
-long bch_bucket_alloc(struct cache *ca, unsigned reserve, bool wait)
+long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait)
 {
DEFINE_WAIT(w);
struct bucket *b;
@@ -421,7 +421,7 @@ long bch_bucket_alloc(struct cache *ca, unsigned reserve, 
bool wait)
if (expensive_debug_checks(ca->set)) {
size_t iter;
long i;
-   unsigned j;
+   unsigned int j;
 
for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
@@ -470,14 +470,14 @@ void __bch_bucket_free(struct cache *ca, struct bucket *b)
 
 void bch_bucket_free(struct cache_set *c, struct bkey *k)
 {
-   unsigned i;
+   unsigned int i;
 
for (i = 0; i < KEY_PTRS(k); i++)
__bch_bucket_free(PTR_CACHE(c, k, i),
  PTR_BUCKET(c, k, i));
 }
 
-int __bch_bucket_alloc_set(struct cache_set *c, unsigned reserve,
+int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
   struct bkey *k, int n, bool wait)
 {
int i;
@@ -510,7 +510,7 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned 
reserve,
return -1;
 }
 
-int bch_bucket_alloc_set(struct cache_set *c, unsigned reserve,
+int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 struct bkey *k, int n, bool wait)
 {
int ret;
@@ -524,8 +524,8 @@ int bch_bucket_alloc_set(struct cache_set *c, unsigned 
reserve,
 
 struct open_bucket {
struct list_headlist;
-   unsignedlast_write_point;
-   unsignedsectors_free;
+   unsigned intlast_write_point;
+   unsigned intsectors_free;
BKEY_PADDED(key);
 };
 
@@ -556,7 +556,7 @@ struct open_bucket {
  */
 static struct open_bucket *pick_data_bucket(struct cache_set *c,
const struct bkey *search,
-   unsigned write_point,
+   unsigned int write_point,

[PATCH v3 04/17] bcache: style fixes for lines over 80 characters

2018-08-10 Thread Coly Li
This patch fixes the lines over 80 characters into more lines, to minimize
warnings by checkpatch.pl. There are still some lines exceed 80 characters,
but it is better to be a single line and I don't change them.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bcache.h|  4 ++--
 drivers/md/bcache/bset.c  | 10 +++---
 drivers/md/bcache/bset.h  |  6 --
 drivers/md/bcache/btree.c |  5 -
 drivers/md/bcache/btree.h |  6 --
 drivers/md/bcache/debug.c |  3 ++-
 drivers/md/bcache/extents.c   |  4 +++-
 drivers/md/bcache/journal.c   |  3 ++-
 drivers/md/bcache/request.c   |  7 +--
 drivers/md/bcache/super.c | 18 --
 drivers/md/bcache/sysfs.c | 11 +++
 drivers/md/bcache/util.h  |  3 ++-
 drivers/md/bcache/writeback.c |  7 +--
 13 files changed, 59 insertions(+), 28 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 0bd505c61943..031a75a25d3e 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -614,8 +614,8 @@ struct cache_set {
uint16_tmin_prio;
 
/*
-* max(gen - last_gc) for all buckets. When it gets too big we have to 
gc
-* to keep gens from wrapping around.
+* max(gen - last_gc) for all buckets. When it gets too big we have to
+* gc to keep gens from wrapping around.
 */
uint8_t need_gc;
struct gc_stat  gc_stats;
diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index 6fd5623b2e63..19b4febe5b45 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -311,7 +311,9 @@ void bch_btree_keys_free(struct btree_keys *b)
 }
 EXPORT_SYMBOL(bch_btree_keys_free);
 
-int bch_btree_keys_alloc(struct btree_keys *b, unsigned int page_order, gfp_t 
gfp)
+int bch_btree_keys_alloc(struct btree_keys *b,
+unsigned int page_order,
+gfp_t gfp)
 {
struct bset_tree *t = b->set;
 
@@ -475,7 +477,8 @@ void inorder_test(void)
for (unsigned int size = 2;
 size < 65536000;
 size++) {
-   unsigned int extra = (size - rounddown_pow_of_two(size - 1)) << 
1;
+   unsigned int extra =
+   (size - rounddown_pow_of_two(size - 1)) << 1;
unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
 
if (!(size % 4096))
@@ -825,7 +828,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
 k != bset_bkey_last(t->data);
 k = bkey_next(k))
if (t->size == bkey_to_cacheline(t, k)) {
-   t->prev[t->size] = bkey_to_cacheline_offset(t, t->size, 
k);
+   t->prev[t->size] =
+   bkey_to_cacheline_offset(t, t->size, k);
t->size++;
}
 }
diff --git a/drivers/md/bcache/bset.h b/drivers/md/bcache/bset.h
index f5bf333aa40d..bac76aabca6d 100644
--- a/drivers/md/bcache/bset.h
+++ b/drivers/md/bcache/bset.h
@@ -246,12 +246,14 @@ static inline bool bkey_written(struct btree_keys *b, 
struct bkey *k)
return !b->last_set_unwritten || k < b->set[b->nsets].data->start;
 }
 
-static inline unsigned int bset_byte_offset(struct btree_keys *b, struct bset 
*i)
+static inline unsigned int bset_byte_offset(struct btree_keys *b,
+   struct bset *i)
 {
return ((size_t) i) - ((size_t) b->set->data);
 }
 
-static inline unsigned int bset_sector_offset(struct btree_keys *b, struct 
bset *i)
+static inline unsigned int bset_sector_offset(struct btree_keys *b,
+ struct bset *i)
 {
return bset_byte_offset(b, i) >> 9;
 }
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index a797ef359a21..c2e4174a2e03 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -436,7 +436,10 @@ static void do_btree_node_write(struct btree *b)
 
continue_at(cl, btree_node_write_done, NULL);
} else {
-   /* No problem for multipage bvec since the bio is just 
allocated */
+   /*
+* No problem for multipage bvec since the bio is
+* just allocated
+*/
b->bio->bi_vcnt = 0;
bch_bio_map(b->bio, i);
 
diff --git a/drivers/md/bcache/btree.h b/drivers/md/bcache/btree.h
index f13b71a0613a..a68d6c55783b 100644
--- a/drivers/md/bcache/btree.h
+++ b/drivers/md/bcache/btree.h
@@ -306,7 +306,9 @@ bool bch_keybuf_check_overlapping(struct keybuf *buf, 
struct bkey *start,
  struct bkey *end);
 void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w);
 struct keybuf_key *bch_keybuf_next(struct keybuf *buf);
-s

[PATCH v3 02/17] bcache: style fix to add a blank line after declarations

2018-08-10 Thread Coly Li
Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/alloc.c |  3 +++
 drivers/md/bcache/bcache.h|  1 +
 drivers/md/bcache/bset.c  |  5 -
 drivers/md/bcache/btree.c |  7 +++
 drivers/md/bcache/closure.c   |  1 +
 drivers/md/bcache/debug.c |  4 ++--
 drivers/md/bcache/extents.c   |  5 -
 drivers/md/bcache/io.c|  4 +++-
 drivers/md/bcache/journal.c   |  2 ++
 drivers/md/bcache/movinggc.c  |  2 ++
 drivers/md/bcache/request.c   |  5 -
 drivers/md/bcache/stats.c |  3 +++
 drivers/md/bcache/super.c | 13 -
 drivers/md/bcache/sysfs.c |  5 +
 drivers/md/bcache/util.c  |  1 +
 drivers/md/bcache/writeback.c |  1 +
 include/uapi/linux/bcache.h   |  2 ++
 17 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 89f663d22551..7a28232d868b 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -244,6 +244,7 @@ static void invalidate_buckets_random(struct cache *ca)
 
while (!fifo_full(>free_inc)) {
size_t n;
+
get_random_bytes(, sizeof(n));
 
n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
@@ -514,6 +515,7 @@ int bch_bucket_alloc_set(struct cache_set *c, unsigned int 
reserve,
 struct bkey *k, int n, bool wait)
 {
int ret;
+
mutex_lock(>bucket_lock);
ret = __bch_bucket_alloc_set(c, reserve, k, n, wait);
mutex_unlock(>bucket_lock);
@@ -706,6 +708,7 @@ int bch_open_buckets_alloc(struct cache_set *c)
 
for (i = 0; i < MAX_OPEN_BUCKETS; i++) {
struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL);
+
if (!b)
return -ENOMEM;
 
diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 1ebd2d9d90d5..fd74dd075951 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -783,6 +783,7 @@ static inline struct bucket *PTR_BUCKET(struct cache_set *c,
 static inline uint8_t gen_after(uint8_t a, uint8_t b)
 {
uint8_t r = a - b;
+
return r > 128U ? 0 : r;
 }
 
diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index dfda7e9efc3e..6fd5623b2e63 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -585,6 +585,7 @@ static inline unsigned int bfloat_mantissa(const struct 
bkey *k,
   struct bkey_float *f)
 {
const uint64_t *p = >low - (f->exponent >> 6);
+
return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
 }
 
@@ -964,6 +965,7 @@ static struct bset_search_iter bset_search_tree(struct 
bset_tree *t,
 * but a branch instruction is avoided.
 */
unsigned int p = n << 4;
+
p &= ((int) (p - t->size)) >> 31;
 
prefetch(>tree[p]);
@@ -1114,6 +1116,7 @@ static struct bkey *__bch_btree_iter_init(struct 
btree_keys *b,
  struct bset_tree *start)
 {
struct bkey *ret = NULL;
+
iter->size = ARRAY_SIZE(iter->data);
iter->used = 0;
 
@@ -1329,8 +1332,8 @@ void bch_btree_sort_into(struct btree_keys *b, struct 
btree_keys *new,
 struct bset_sort_state *state)
 {
uint64_t start_time = local_clock();
-
struct btree_iter iter;
+
bch_btree_iter_init(b, , NULL);
 
btree_mergesort(b, new->set->data, , false, true);
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 96c39a8db895..4003f92f4d2c 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -287,6 +287,7 @@ void bch_btree_node_read_done(struct btree *b)
 static void btree_node_read_endio(struct bio *bio)
 {
struct closure *cl = bio->bi_private;
+
closure_put(cl);
 }
 
@@ -604,6 +605,7 @@ static struct btree *mca_bucket_alloc(struct cache_set *c,
  struct bkey *k, gfp_t gfp)
 {
struct btree *b = kzalloc(sizeof(struct btree), gfp);
+
if (!b)
return NULL;
 
@@ -746,6 +748,7 @@ void bch_btree_cache_free(struct cache_set *c)
 {
struct btree *b;
struct closure cl;
+
closure_init_stack();
 
if (c->shrink.list.next)
@@ -1124,6 +1127,7 @@ static struct btree *btree_node_alloc_replacement(struct 
btree *b,
  struct btree_op *op)
 {
struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent);
+
if (!IS_ERR_OR_NULL(n)) {
mutex_lock(>write_lock);
bch_btree_sort_into(>keys, >keys, >c->sort);
@@ -2488,6 +2492,7 @@ void bch_refill_keybuf(struct cache_set *c, struct keybuf 
*buf,
 
if (!RB_EMPTY_ROOT(>keys)) {
  

[PATCH v3 05/17] bcache: replace Symbolic permissions by octal permission numbers

2018-08-10 Thread Coly Li
Symbolic permission names are used in bcache, for now octal permission
numbers are encouraged to use for readability. This patch replaces
all symbolic permissions by octal permission numbers.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bcache.h | 4 ++--
 drivers/md/bcache/sysfs.h  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 031a75a25d3e..66e6d5639b38 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -881,11 +881,11 @@ static inline uint8_t bucket_gc_gen(struct bucket *b)
 #define BUCKET_GC_GEN_MAX  96U
 
 #define kobj_attribute_write(n, fn)\
-   static struct kobj_attribute ksysfs_##n = __ATTR(n, S_IWUSR, NULL, fn)
+   static struct kobj_attribute ksysfs_##n = __ATTR(n, 0200, NULL, fn)
 
 #define kobj_attribute_rw(n, show, store)  \
static struct kobj_attribute ksysfs_##n =   \
-   __ATTR(n, S_IWUSR|S_IRUSR, show, store)
+   __ATTR(n, 0600, show, store)
 
 static inline void wake_up_allocators(struct cache_set *c)
 {
diff --git a/drivers/md/bcache/sysfs.h b/drivers/md/bcache/sysfs.h
index b54fe9602529..3fe82425859c 100644
--- a/drivers/md/bcache/sysfs.h
+++ b/drivers/md/bcache/sysfs.h
@@ -44,9 +44,9 @@ STORE(fn) 
\
static struct attribute sysfs_##_name = \
{ .name = #_name, .mode = _mode }
 
-#define write_attribute(n) __sysfs_attribute(n, S_IWUSR)
-#define read_attribute(n)  __sysfs_attribute(n, S_IRUGO)
-#define rw_attribute(n)__sysfs_attribute(n, S_IRUGO|S_IWUSR)
+#define write_attribute(n) __sysfs_attribute(n, 0200)
+#define read_attribute(n)  __sysfs_attribute(n, 0444)
+#define rw_attribute(n)__sysfs_attribute(n, 0644)
 
 #define sysfs_printf(file, fmt, ...)   \
 do {   \
-- 
2.18.0



Re: [PATCH] bcache: fix 0day error of setting writeback_rate by sysfs interface

2018-08-10 Thread Coly Li
On 2018/8/11 2:19 AM, Jens Axboe wrote:
> On 8/10/18 9:45 AM, Coly Li wrote:
>> Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request
>> is idle") changes struct bch_ratelimit member rate from uint32_t to
>> atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c
>> to set new writeback rate, after the input is converted from memory
>> buf to long int by sysfs_strtoul_clamp().
>>
>> The above change has a problem because there is an implicit return
>> inside sysfs_strtoul_clamp() so the following atomic_long_set()
>> won't be called. This error is detected by 0day system with following
>> snipped smatch warnings:
>>
>> drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized
>> symbol 'v'.
>> 270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
>>  ^^^
>> @271 atomic_long_set(>writeback_rate.rate, v);
>>
>> This patch fixes the above error by using strtoul_safe_clamp() to
>> convert the input buffer into a long int type result.
>>
>> Fixes: Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request 
>> is idle")
>> Signed-off-by: Coly Li 
>> Cc: sta...@vger.kernel.org #4.16+
>> Cc: Kai Krakow 
>> Cc: Stefan Priebe 
> 

Hi Jens,

> You don't need to CC stable for this, the commit isn't in a released kernel
> yet. In fact, it's not even in Linus's tree, it's just queued up for 4.19.
> 

What I thought was, Commit ea8c5356d390 ("bcache: set max writeback rate
when I/O request is idle") is a stable fix, so this patch should go into
stable as well. Otherwise the stable kernel cannot manually set
writeback_rate via sysfs interface.

> Also, the fixes line should be:
> 
> Fixes: ea8c5356d390 ("bcache: set max writeback rate when I/O request is 
> idle")
> 
> (no Commit). I missed that on some of your earlier commits.

Copied, keep in mind for next time.

> 
> Applied with those edits, and some wording changes.
> 

Thanks for your help !

Coly Li


Re: [PATCH] bcache: fix 0day error of setting writeback_rate by sysfs interface

2018-08-10 Thread Coly Li
On 2018/8/11 2:13 AM, Stefan Priebe - Profihost AG wrote:
> Thanks for cc. How is this exploitable? I mean only root can write to
> sysfs? Or do you mean by allowing a user via sudo to write to that entry?

Hi Stefan,

This is not a security 0day bug, this is an error reported by Linux
kernel 0day test service
(https://01.org/zh/lkp/documentation/0-day-test-service). My development
tree is registered and monitored by 0day testing service, so if there is
any static code error or boot failure, I can be noticed in very early stage.

The bug in previous patch is, writeback_rate cannot be set by sysfs
interface, because sysfs_strtoul_clamp() directly returns. This patch
fixes this and allows writeback_rate can be manually set again.

Coly Li

> 
> Am 10.08.2018 um 17:45 schrieb Coly Li:
>> Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request
>> is idle") changes struct bch_ratelimit member rate from uint32_t to
>> atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c
>> to set new writeback rate, after the input is converted from memory
>> buf to long int by sysfs_strtoul_clamp().
>>
>> The above change has a problem because there is an implicit return
>> inside sysfs_strtoul_clamp() so the following atomic_long_set()
>> won't be called. This error is detected by 0day system with following
>> snipped smatch warnings:
>>
>> drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized
>> symbol 'v'.
>> 270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
>>  ^^^
>> @271 atomic_long_set(>writeback_rate.rate, v);
>>
>> This patch fixes the above error by using strtoul_safe_clamp() to
>> convert the input buffer into a long int type result.
>>
>> Fixes: Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request 
>> is idle")
>> Signed-off-by: Coly Li 
>> Cc: sta...@vger.kernel.org #4.16+
>> Cc: Kai Krakow 
>> Cc: Stefan Priebe 
>> ---
>>  drivers/md/bcache/sysfs.c | 13 ++---
>>  1 file changed, 10 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
>> index 543b06408321..150cf4f4cf74 100644
>> --- a/drivers/md/bcache/sysfs.c
>> +++ b/drivers/md/bcache/sysfs.c
>> @@ -267,10 +267,17 @@ STORE(__cached_dev)
>>  sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
>>  
>>  if (attr == _writeback_rate) {
>> -int v;
>> +ssize_t ret;
>> +long int v = atomic_long_read(>writeback_rate.rate);
>> +
>> +ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
>>  
>> -sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
>> -atomic_long_set(>writeback_rate.rate, v);
>> +if (!ret) {
>> +atomic_long_set(>writeback_rate.rate, v);
>> +ret = size;
>> +}
>> +
>> +return ret;
>>  }
>>  
>>  sysfs_strtoul_clamp(writeback_rate_update_seconds,
>>



[PATCH v3 17/17] bcache: add the missing comments for smp_mb()/smp_wmb()

2018-08-10 Thread Coly Li
Checkpatch.pl warns there are 2 locations of smp_mb() and smp_wmb()
without code comment. This patch adds the missing code comments for
these memory barrier calls.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/closure.h | 4 +++-
 drivers/md/bcache/super.c   | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/closure.h b/drivers/md/bcache/closure.h
index 7f3594c0be14..eca0d496b686 100644
--- a/drivers/md/bcache/closure.h
+++ b/drivers/md/bcache/closure.h
@@ -289,10 +289,12 @@ static inline void closure_init_stack(struct closure *cl)
 }
 
 /**
- * closure_wake_up - wake up all closures on a wait list.
+ * closure_wake_up - wake up all closures on a wait list,
+ *  with memory barrier
  */
 static inline void closure_wake_up(struct closure_waitlist *list)
 {
+   /* Memory barrier for the wait list */
smp_mb();
__closure_wake_up(list);
 }
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index e637d74e2908..94c756c66bd7 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1136,11 +1136,11 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct 
cache_set *c,
list_move(>list, >cached_devs);
calc_cached_dev_sectors(c);
 
-   smp_wmb();
/*
 * dc->c must be set before dc->count != 0 - paired with the mb in
 * cached_dev_get()
 */
+   smp_wmb();
refcount_set(>count, 1);
 
/* Block writeback thread, but spawn it */
-- 
2.18.0



[PATCH v3 14/17] bcache: move open brace at end of function definitions to next line

2018-08-10 Thread Coly Li
This is not a preferred style to place open brace '{' at the end of
function definition, checkpatch.pl reports error for such coding
style. This patch moves them into the start of the next new line.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/super.c | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 01fc3c015a58..e3ecf08a10fc 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2152,7 +2152,8 @@ static ssize_t register_bcache(struct kobject *k, struct 
kobj_attribute *attr,
 kobj_attribute_write(register, register_bcache);
 kobj_attribute_write(register_quiet,   register_bcache);
 
-static bool bch_is_open_backing(struct block_device *bdev) {
+static bool bch_is_open_backing(struct block_device *bdev)
+{
struct cache_set *c, *tc;
struct cached_dev *dc, *t;
 
@@ -2166,7 +2167,8 @@ static bool bch_is_open_backing(struct block_device 
*bdev) {
return false;
 }
 
-static bool bch_is_open_cache(struct block_device *bdev) {
+static bool bch_is_open_cache(struct block_device *bdev)
+{
struct cache_set *c, *tc;
struct cache *ca;
unsigned int i;
@@ -2178,7 +2180,8 @@ static bool bch_is_open_cache(struct block_device *bdev) {
return false;
 }
 
-static bool bch_is_open(struct block_device *bdev) {
+static bool bch_is_open(struct block_device *bdev)
+{
return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
 }
 
-- 
2.18.0



[PATCH v3 13/17] bcache: add static const prefix to char * array declarations

2018-08-10 Thread Coly Li
This patch declares char * array with const prefix in sysfs.c,
which is suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 2ed503ab9e5c..543b06408321 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -150,7 +150,7 @@ SHOW(__bch_cached_dev)
 {
struct cached_dev *dc = container_of(kobj, struct cached_dev,
 disk.kobj);
-   const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
+   char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
int wb = dc->writeback_running;
 
 #define var(stat)  (dc->stat)
-- 
2.18.0



[PATCH v3 15/17] bcache: add missing SPDX header

2018-08-10 Thread Coly Li
The SPDX header is missing fro closure.c, super.c and util.c, this
patch adds SPDX header for GPL-2.0 into these files.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/closure.c | 1 +
 drivers/md/bcache/super.c   | 1 +
 drivers/md/bcache/util.c| 1 +
 3 files changed, 3 insertions(+)

diff --git a/drivers/md/bcache/closure.c b/drivers/md/bcache/closure.c
index 034067e0e9ce..73f5319295bc 100644
--- a/drivers/md/bcache/closure.c
+++ b/drivers/md/bcache/closure.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Asynchronous refcounty things
  *
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index e3ecf08a10fc..e637d74e2908 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * bcache setup/teardown code, and some metadata io - read a superblock and
  * figure out what to do with it.
diff --git a/drivers/md/bcache/util.c b/drivers/md/bcache/util.c
index 18016e7bb32c..e873b0f7a82a 100644
--- a/drivers/md/bcache/util.c
+++ b/drivers/md/bcache/util.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * random utiility code, for bcache but in theory not specific to bcache
  *
-- 
2.18.0



[PATCH v3 16/17] bcache: remove unnecessary space before ioctl function pointer arguments

2018-08-10 Thread Coly Li
This is warned by checkpatch.pl, this patch removes the extra space.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bcache.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 66e6d5639b38..83504dd8100a 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -275,8 +275,8 @@ struct bcache_device {
 
int (*cache_miss)(struct btree *b, struct search *s,
  struct bio *bio, unsigned int sectors);
-   int (*ioctl) (struct bcache_device *d, fmode_t mode,
- unsigned int cmd, unsigned long arg);
+   int (*ioctl)(struct bcache_device *d, fmode_t mode,
+unsigned int cmd, unsigned long arg);
 };
 
 struct io {
-- 
2.18.0



[PATCH v3 11/17] bcache: do not check NULL pointer before calling kmem_cache_destroy

2018-08-10 Thread Coly Li
kmem_cache_destroy() is safe for NULL pointer as input, the NULL pointer
checking is unncessary. This patch just removes the NULL pointer checking
to make code simpler.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/request.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 55264e71369d..51be355a3309 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -1367,8 +1367,7 @@ void bch_flash_dev_request_init(struct bcache_device *d)
 
 void bch_request_exit(void)
 {
-   if (bch_search_cache)
-   kmem_cache_destroy(bch_search_cache);
+   kmem_cache_destroy(bch_search_cache);
 }
 
 int __init bch_request_init(void)
-- 
2.18.0



[PATCH v3 12/17] bcache: fix code comments style

2018-08-10 Thread Coly Li
This patch fixes 3 style issues warned by checkpatch.pl,
- Comment lines are not aligned
- Comments use "/*" on subsequent lines
- Comment lines use a trailing "*/"

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bset.c  |  9 ++---
 drivers/md/bcache/super.c | 22 +-
 drivers/md/bcache/writeback.c |  3 ++-
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index b6a3f9d291a9..8f07fa6e1739 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -402,7 +402,8 @@ static unsigned int inorder_prev(unsigned int j, unsigned 
int size)
return j;
 }
 
-/* I have no idea why this code works... and I'm the one who wrote it
+/*
+ * I have no idea why this code works... and I'm the one who wrote it
  *
  * However, I do know what it does:
  * Given a binary tree constructed in an array (i.e. how you normally implement
@@ -795,7 +796,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
if (!t->size)
return;
 
-   /* k is the key we just inserted; we need to find the entry in the
+   /*
+* k is the key we just inserted; we need to find the entry in the
 * lookup table for the first key that is strictly greater than k:
 * it's either k's cacheline or the next one
 */
@@ -803,7 +805,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
   table_to_bkey(t, j) <= k)
j++;
 
-   /* Adjust all the lookup table entries, and find a new key for any that
+   /*
+* Adjust all the lookup table entries, and find a new key for any that
 * have gotten too big
 */
for (; j < t->size; j++) {
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 2a7be104557e..01fc3c015a58 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -465,8 +465,8 @@ static struct uuid_entry *uuid_find_empty(struct cache_set 
*c)
  * Bucket priorities/gens:
  *
  * For each bucket, we store on disk its
-   * 8 bit gen
-   * 16 bit priority
+ *   8 bit gen
+ *  16 bit priority
  *
  * See alloc.c for an explanation of the gen. The priority is used to implement
  * lru (and in the future other) cache replacement policies; for most purposes
@@ -934,8 +934,10 @@ void bch_cached_dev_run(struct cached_dev *dc)
 
add_disk(d->disk);
bd_link_disk_holder(dc->bdev, dc->disk.disk);
-   /* won't show up in the uevent file, use udevadm monitor -e instead
-* only class / kset properties are persistent */
+   /*
+* won't show up in the uevent file, use udevadm monitor -e instead
+* only class / kset properties are persistent
+*/
kobject_uevent_env(_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
kfree(env[1]);
kfree(env[2]);
@@ -1104,8 +1106,9 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct 
cache_set *c,
}
}
 
-   /* Deadlocks since we're called via sysfs...
-   sysfs_remove_file(>kobj, _attach);
+   /*
+* Deadlocks since we're called via sysfs...
+* sysfs_remove_file(>kobj, _attach);
 */
 
if (bch_is_zero(u->uuid, 16)) {
@@ -1468,9 +1471,10 @@ bool bch_cache_set_error(struct cache_set *c, const char 
*fmt, ...)
if (test_and_set_bit(CACHE_SET_IO_DISABLE, >flags))
pr_info("CACHE_SET_IO_DISABLE already set");
 
-   /* XXX: we can be called from atomic context
-   acquire_console_sem();
-   */
+   /*
+* XXX: we can be called from atomic context
+* acquire_console_sem();
+*/
 
pr_err("bcache: error on %pU: ", c->sb.set_uuid);
 
diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
index e40bf0e403e7..6be05bd7ca67 100644
--- a/drivers/md/bcache/writeback.c
+++ b/drivers/md/bcache/writeback.c
@@ -468,7 +468,8 @@ static void read_dirty(struct cached_dev *dc)
 
down(>in_flight);
 
-   /* We've acquired a semaphore for the maximum
+   /*
+* We've acquired a semaphore for the maximum
 * simultaneous number of writebacks; from here
 * everything happens asynchronously.
 */
-- 
2.18.0



[PATCH v3 10/17] bcache: prefer 'help' in Kconfig

2018-08-10 Thread Coly Li
Current bcache Kconfig uses '---help---' as header of help information,
for now 'help' is prefered. This patch fixes this style by replacing
'---help---' by 'help' in bcache Kconfig file.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/Kconfig | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/Kconfig b/drivers/md/bcache/Kconfig
index 17bf109c58e9..817b9fba50db 100644
--- a/drivers/md/bcache/Kconfig
+++ b/drivers/md/bcache/Kconfig
@@ -1,7 +1,7 @@
 
 config BCACHE
tristate "Block device as cache"
-   ---help---
+   help
Allows a block device to be used as cache for other devices; uses
a btree for indexing and the layout is optimized for SSDs.
 
@@ -10,7 +10,7 @@ config BCACHE
 config BCACHE_DEBUG
bool "Bcache debugging"
depends on BCACHE
-   ---help---
+   help
Don't select this option unless you're a developer
 
Enables extra debugging tools, allows expensive runtime checks to be
@@ -20,7 +20,7 @@ config BCACHE_CLOSURES_DEBUG
bool "Debug closures"
depends on BCACHE
select DEBUG_FS
-   ---help---
+   help
Keeps all active closures in a linked list and provides a debugfs
interface to list them, which makes it possible to see asynchronous
operations that get stuck.
-- 
2.18.0



[PATCH v3 09/17] bcache: fix typo 'succesfully' to 'successfully'

2018-08-10 Thread Coly Li
This patch fixes typo 'succesfully' to correct 'successfully', which is
suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/io.c  | 2 +-
 drivers/md/bcache/request.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/io.c b/drivers/md/bcache/io.c
index cfc56add799a..c25097968319 100644
--- a/drivers/md/bcache/io.c
+++ b/drivers/md/bcache/io.c
@@ -86,7 +86,7 @@ void bch_count_io_errors(struct cache *ca,
 
/*
 * First we subtract refresh from count; each time we
-* succesfully do so, we rescale the errors once:
+* successfully do so, we rescale the errors once:
 */
 
count = atomic_cmpxchg(>io_count, old, new);
diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 858dd3da9dc5..55264e71369d 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -154,7 +154,7 @@ static void bch_data_insert_error(struct closure *cl)
 
/*
 * Our data write just errored, which means we've got a bunch of keys to
-* insert that point to data that wasn't succesfully written.
+* insert that point to data that wasn't successfully written.
 *
 * We don't have to insert those keys but we still have to invalidate
 * that region of the cache - so, if we just strip off all the pointers
-- 
2.18.0



[PATCH v3 07/17] bcache: fix indent by replacing blank by tabs

2018-08-10 Thread Coly Li
bch_btree_insert_check_key() has unaligned indent, or indent by blank
characters. This patch makes the indent aligned and replace blank by
tabs.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/btree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index c2e4174a2e03..e7d4817681f2 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -2231,10 +2231,10 @@ int bch_btree_insert_check_key(struct btree *b, struct 
btree_op *op,
rw_lock(true, b, b->level);
 
if (b->key.ptr[0] != btree_ptr ||
-   b->seq != seq + 1) {
+   b->seq != seq + 1) {
op->lock = b->level;
goto out;
-   }
+   }
}
 
SET_KEY_PTRS(check_key, 1);
-- 
2.18.0



[PATCH v3 08/17] bcache: replace '%pF' by '%pS' in seq_printf()

2018-08-10 Thread Coly Li
'%pF' and '%pf' are deprecated vsprintf pointer extensions, this patch
replace them by '%pS', which is suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/closure.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/closure.c b/drivers/md/bcache/closure.c
index 8570fc426e31..034067e0e9ce 100644
--- a/drivers/md/bcache/closure.c
+++ b/drivers/md/bcache/closure.c
@@ -168,7 +168,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
list_for_each_entry(cl, _list, all) {
int r = atomic_read(>remaining);
 
-   seq_printf(f, "%p: %pF -> %pf p %p r %i ",
+   seq_printf(f, "%p: %pS -> %pS p %p r %i ",
   cl, (void *) cl->ip, cl->fn, cl->parent,
   r & CLOSURE_REMAINING_MASK);
 
@@ -178,7 +178,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
   r & CLOSURE_RUNNING  ? "R" : "");
 
if (r & CLOSURE_WAITING)
-   seq_printf(f, " W %pF\n",
+   seq_printf(f, " W %pS\n",
   (void *) cl->waiting_on);
 
seq_printf(f, "\n");
-- 
2.18.0



[PATCH v3 01/17] bcache: style fix to replace 'unsigned' by 'unsigned int'

2018-08-10 Thread Coly Li
This patch fixes warning reported by checkpatch.pl by replacing 'unsigned'
with 'unsigned int'.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/alloc.c |  36 ++-
 drivers/md/bcache/bcache.h| 107 +++
 drivers/md/bcache/bset.c  | 114 ++
 drivers/md/bcache/bset.h  |  34 +-
 drivers/md/bcache/btree.c |  50 +++
 drivers/md/bcache/btree.h |   4 +-
 drivers/md/bcache/closure.h   |   2 +-
 drivers/md/bcache/debug.c |   6 +-
 drivers/md/bcache/extents.c   |  22 +++
 drivers/md/bcache/io.c|  18 +++---
 drivers/md/bcache/journal.c   |  20 +++---
 drivers/md/bcache/journal.h   |   8 +--
 drivers/md/bcache/movinggc.c  |  12 ++--
 drivers/md/bcache/request.c   |  42 ++---
 drivers/md/bcache/request.h   |  18 +++---
 drivers/md/bcache/stats.c |  12 ++--
 drivers/md/bcache/stats.h |   2 +-
 drivers/md/bcache/super.c |  34 +-
 drivers/md/bcache/sysfs.c |  18 +++---
 drivers/md/bcache/util.h  |   9 +--
 drivers/md/bcache/writeback.c |  19 +++---
 drivers/md/bcache/writeback.h |  12 ++--
 include/uapi/linux/bcache.h   |   6 +-
 23 files changed, 309 insertions(+), 296 deletions(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 7fa2631b422c..89f663d22551 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -87,8 +87,8 @@ void bch_rescale_priorities(struct cache_set *c, int sectors)
 {
struct cache *ca;
struct bucket *b;
-   unsigned next = c->nbuckets * c->sb.bucket_size / 1024;
-   unsigned i;
+   unsigned int next = c->nbuckets * c->sb.bucket_size / 1024;
+   unsigned int i;
int r;
 
atomic_sub(sectors, >rescale);
@@ -169,7 +169,7 @@ static void bch_invalidate_one_bucket(struct cache *ca, 
struct bucket *b)
 
 #define bucket_prio(b) \
 ({ \
-   unsigned min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
+   unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
\
(b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b);  \
 })
@@ -301,7 +301,7 @@ do {
\
 
 static int bch_allocator_push(struct cache *ca, long bucket)
 {
-   unsigned i;
+   unsigned int i;
 
/* Prios/gens are actually the most important reserve */
if (fifo_push(>free[RESERVE_PRIO], bucket))
@@ -385,7 +385,7 @@ static int bch_allocator_thread(void *arg)
 
 /* Allocation */
 
-long bch_bucket_alloc(struct cache *ca, unsigned reserve, bool wait)
+long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait)
 {
DEFINE_WAIT(w);
struct bucket *b;
@@ -421,7 +421,7 @@ long bch_bucket_alloc(struct cache *ca, unsigned reserve, 
bool wait)
if (expensive_debug_checks(ca->set)) {
size_t iter;
long i;
-   unsigned j;
+   unsigned int j;
 
for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
@@ -470,14 +470,14 @@ void __bch_bucket_free(struct cache *ca, struct bucket *b)
 
 void bch_bucket_free(struct cache_set *c, struct bkey *k)
 {
-   unsigned i;
+   unsigned int i;
 
for (i = 0; i < KEY_PTRS(k); i++)
__bch_bucket_free(PTR_CACHE(c, k, i),
  PTR_BUCKET(c, k, i));
 }
 
-int __bch_bucket_alloc_set(struct cache_set *c, unsigned reserve,
+int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
   struct bkey *k, int n, bool wait)
 {
int i;
@@ -510,7 +510,7 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned 
reserve,
return -1;
 }
 
-int bch_bucket_alloc_set(struct cache_set *c, unsigned reserve,
+int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 struct bkey *k, int n, bool wait)
 {
int ret;
@@ -524,8 +524,8 @@ int bch_bucket_alloc_set(struct cache_set *c, unsigned 
reserve,
 
 struct open_bucket {
struct list_headlist;
-   unsignedlast_write_point;
-   unsignedsectors_free;
+   unsigned intlast_write_point;
+   unsigned intsectors_free;
BKEY_PADDED(key);
 };
 
@@ -556,7 +556,7 @@ struct open_bucket {
  */
 static struct open_bucket *pick_data_bucket(struct cache_set *c,
const struct bkey *search,
-   unsigned write_point,
+   unsigned int write_point,

[PATCH v3 02/17] bcache: style fix to add a blank line after declarations

2018-08-10 Thread Coly Li
Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/alloc.c |  3 +++
 drivers/md/bcache/bcache.h|  1 +
 drivers/md/bcache/bset.c  |  5 -
 drivers/md/bcache/btree.c |  7 +++
 drivers/md/bcache/closure.c   |  1 +
 drivers/md/bcache/debug.c |  4 ++--
 drivers/md/bcache/extents.c   |  5 -
 drivers/md/bcache/io.c|  4 +++-
 drivers/md/bcache/journal.c   |  2 ++
 drivers/md/bcache/movinggc.c  |  2 ++
 drivers/md/bcache/request.c   |  5 -
 drivers/md/bcache/stats.c |  3 +++
 drivers/md/bcache/super.c | 13 -
 drivers/md/bcache/sysfs.c |  5 +
 drivers/md/bcache/util.c  |  1 +
 drivers/md/bcache/writeback.c |  1 +
 include/uapi/linux/bcache.h   |  2 ++
 17 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 89f663d22551..7a28232d868b 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -244,6 +244,7 @@ static void invalidate_buckets_random(struct cache *ca)
 
while (!fifo_full(>free_inc)) {
size_t n;
+
get_random_bytes(, sizeof(n));
 
n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
@@ -514,6 +515,7 @@ int bch_bucket_alloc_set(struct cache_set *c, unsigned int 
reserve,
 struct bkey *k, int n, bool wait)
 {
int ret;
+
mutex_lock(>bucket_lock);
ret = __bch_bucket_alloc_set(c, reserve, k, n, wait);
mutex_unlock(>bucket_lock);
@@ -706,6 +708,7 @@ int bch_open_buckets_alloc(struct cache_set *c)
 
for (i = 0; i < MAX_OPEN_BUCKETS; i++) {
struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL);
+
if (!b)
return -ENOMEM;
 
diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 1ebd2d9d90d5..fd74dd075951 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -783,6 +783,7 @@ static inline struct bucket *PTR_BUCKET(struct cache_set *c,
 static inline uint8_t gen_after(uint8_t a, uint8_t b)
 {
uint8_t r = a - b;
+
return r > 128U ? 0 : r;
 }
 
diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index dfda7e9efc3e..6fd5623b2e63 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -585,6 +585,7 @@ static inline unsigned int bfloat_mantissa(const struct 
bkey *k,
   struct bkey_float *f)
 {
const uint64_t *p = >low - (f->exponent >> 6);
+
return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
 }
 
@@ -964,6 +965,7 @@ static struct bset_search_iter bset_search_tree(struct 
bset_tree *t,
 * but a branch instruction is avoided.
 */
unsigned int p = n << 4;
+
p &= ((int) (p - t->size)) >> 31;
 
prefetch(>tree[p]);
@@ -1114,6 +1116,7 @@ static struct bkey *__bch_btree_iter_init(struct 
btree_keys *b,
  struct bset_tree *start)
 {
struct bkey *ret = NULL;
+
iter->size = ARRAY_SIZE(iter->data);
iter->used = 0;
 
@@ -1329,8 +1332,8 @@ void bch_btree_sort_into(struct btree_keys *b, struct 
btree_keys *new,
 struct bset_sort_state *state)
 {
uint64_t start_time = local_clock();
-
struct btree_iter iter;
+
bch_btree_iter_init(b, , NULL);
 
btree_mergesort(b, new->set->data, , false, true);
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 96c39a8db895..4003f92f4d2c 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -287,6 +287,7 @@ void bch_btree_node_read_done(struct btree *b)
 static void btree_node_read_endio(struct bio *bio)
 {
struct closure *cl = bio->bi_private;
+
closure_put(cl);
 }
 
@@ -604,6 +605,7 @@ static struct btree *mca_bucket_alloc(struct cache_set *c,
  struct bkey *k, gfp_t gfp)
 {
struct btree *b = kzalloc(sizeof(struct btree), gfp);
+
if (!b)
return NULL;
 
@@ -746,6 +748,7 @@ void bch_btree_cache_free(struct cache_set *c)
 {
struct btree *b;
struct closure cl;
+
closure_init_stack();
 
if (c->shrink.list.next)
@@ -1124,6 +1127,7 @@ static struct btree *btree_node_alloc_replacement(struct 
btree *b,
  struct btree_op *op)
 {
struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent);
+
if (!IS_ERR_OR_NULL(n)) {
mutex_lock(>write_lock);
bch_btree_sort_into(>keys, >keys, >c->sort);
@@ -2488,6 +2492,7 @@ void bch_refill_keybuf(struct cache_set *c, struct keybuf 
*buf,
 
if (!RB_EMPTY_ROOT(>keys)) {
  

[PATCH v3 06/17] bcache: replace printk() by pr_*() routines

2018-08-10 Thread Coly Li
There are still many places in bcache use printk to display kernel
message, which are suggested to be preplaced by pr_*() routines like
pr_err(), pr_info(), or pr_notice().

This patch replaces all printk() with a proper pr_*() routine for
bcache code.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bset.c|  8 
 drivers/md/bcache/debug.c   | 10 +-
 drivers/md/bcache/extents.c |  8 
 drivers/md/bcache/super.c   |  4 ++--
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index 19b4febe5b45..b6a3f9d291a9 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -25,18 +25,18 @@ void bch_dump_bset(struct btree_keys *b, struct bset *i, 
unsigned int set)
for (k = i->start; k < bset_bkey_last(i); k = next) {
next = bkey_next(k);
 
-   printk(KERN_ERR "block %u key %u/%u: ", set,
+   pr_err("block %u key %u/%u: ", set,
   (unsigned int) ((u64 *) k - i->d), i->keys);
 
if (b->ops->key_dump)
b->ops->key_dump(b, k);
else
-   printk("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
+   pr_err("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
 
if (next < bset_bkey_last(i) &&
bkey_cmp(k, b->ops->is_extents ?
 _KEY(next) : next) > 0)
-   printk(KERN_ERR "Key skipped backwards\n");
+   pr_err("Key skipped backwards\n");
}
 }
 
@@ -482,7 +482,7 @@ void inorder_test(void)
unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
 
if (!(size % 4096))
-   printk(KERN_NOTICE "loop %u, %llu per us\n", size,
+   pr_notice("loop %u, %llu per us\n", size,
   done / ktime_us_delta(ktime_get(), start));
 
while (1) {
diff --git a/drivers/md/bcache/debug.c b/drivers/md/bcache/debug.c
index a8f70c916fdb..06da66b2488a 100644
--- a/drivers/md/bcache/debug.c
+++ b/drivers/md/bcache/debug.c
@@ -74,28 +74,28 @@ void bch_btree_verify(struct btree *b)
 
console_lock();
 
-   printk(KERN_ERR "*** in memory:\n");
+   pr_err("*** in memory:\n");
bch_dump_bset(>keys, inmemory, 0);
 
-   printk(KERN_ERR "*** read back in:\n");
+   pr_err("*** read back in:\n");
bch_dump_bset(>keys, sorted, 0);
 
for_each_written_bset(b, ondisk, i) {
unsigned int block = ((void *) i - (void *) ondisk) /
block_bytes(b->c);
 
-   printk(KERN_ERR "*** on disk block %u:\n", block);
+   pr_err("*** on disk block %u:\n", block);
bch_dump_bset(>keys, i, block);
}
 
-   printk(KERN_ERR "*** block %zu not written\n",
+   pr_err("*** block %zu not written\n",
   ((void *) i - (void *) ondisk) / block_bytes(b->c));
 
for (j = 0; j < inmemory->keys; j++)
if (inmemory->d[j] != sorted->d[j])
break;
 
-   printk(KERN_ERR "b->written %u\n", b->written);
+   pr_err("b->written %u\n", b->written);
 
console_unlock();
panic("verify failed at %u\n", j);
diff --git a/drivers/md/bcache/extents.c b/drivers/md/bcache/extents.c
index cb3b2c613ed6..c809724e6571 100644
--- a/drivers/md/bcache/extents.c
+++ b/drivers/md/bcache/extents.c
@@ -130,18 +130,18 @@ static void bch_bkey_dump(struct btree_keys *keys, const 
struct bkey *k)
char buf[80];
 
bch_extent_to_text(buf, sizeof(buf), k);
-   printk(" %s", buf);
+   pr_err(" %s", buf);
 
for (j = 0; j < KEY_PTRS(k); j++) {
size_t n = PTR_BUCKET_NR(b->c, k, j);
 
-   printk(" bucket %zu", n);
+   pr_err(" bucket %zu", n);
if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
-   printk(" prio %i",
+   pr_err(" prio %i",
   PTR_BUCKET(b->c, k, j)->prio);
}
 
-   printk(" %s\n", bch_ptr_status(b->c, k));
+   pr_err(" %s\n", bch_ptr_status(b->c, k));
 }
 
 /* Btree ptrs */
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 296fc8c31c6c.

[PATCH v3 04/17] bcache: style fixes for lines over 80 characters

2018-08-10 Thread Coly Li
This patch fixes the lines over 80 characters into more lines, to minimize
warnings by checkpatch.pl. There are still some lines exceed 80 characters,
but it is better to be a single line and I don't change them.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bcache.h|  4 ++--
 drivers/md/bcache/bset.c  | 10 +++---
 drivers/md/bcache/bset.h  |  6 --
 drivers/md/bcache/btree.c |  5 -
 drivers/md/bcache/btree.h |  6 --
 drivers/md/bcache/debug.c |  3 ++-
 drivers/md/bcache/extents.c   |  4 +++-
 drivers/md/bcache/journal.c   |  3 ++-
 drivers/md/bcache/request.c   |  7 +--
 drivers/md/bcache/super.c | 18 --
 drivers/md/bcache/sysfs.c | 11 +++
 drivers/md/bcache/util.h  |  3 ++-
 drivers/md/bcache/writeback.c |  7 +--
 13 files changed, 59 insertions(+), 28 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 0bd505c61943..031a75a25d3e 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -614,8 +614,8 @@ struct cache_set {
uint16_tmin_prio;
 
/*
-* max(gen - last_gc) for all buckets. When it gets too big we have to 
gc
-* to keep gens from wrapping around.
+* max(gen - last_gc) for all buckets. When it gets too big we have to
+* gc to keep gens from wrapping around.
 */
uint8_t need_gc;
struct gc_stat  gc_stats;
diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index 6fd5623b2e63..19b4febe5b45 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -311,7 +311,9 @@ void bch_btree_keys_free(struct btree_keys *b)
 }
 EXPORT_SYMBOL(bch_btree_keys_free);
 
-int bch_btree_keys_alloc(struct btree_keys *b, unsigned int page_order, gfp_t 
gfp)
+int bch_btree_keys_alloc(struct btree_keys *b,
+unsigned int page_order,
+gfp_t gfp)
 {
struct bset_tree *t = b->set;
 
@@ -475,7 +477,8 @@ void inorder_test(void)
for (unsigned int size = 2;
 size < 65536000;
 size++) {
-   unsigned int extra = (size - rounddown_pow_of_two(size - 1)) << 
1;
+   unsigned int extra =
+   (size - rounddown_pow_of_two(size - 1)) << 1;
unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
 
if (!(size % 4096))
@@ -825,7 +828,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
 k != bset_bkey_last(t->data);
 k = bkey_next(k))
if (t->size == bkey_to_cacheline(t, k)) {
-   t->prev[t->size] = bkey_to_cacheline_offset(t, t->size, 
k);
+   t->prev[t->size] =
+   bkey_to_cacheline_offset(t, t->size, k);
t->size++;
}
 }
diff --git a/drivers/md/bcache/bset.h b/drivers/md/bcache/bset.h
index f5bf333aa40d..bac76aabca6d 100644
--- a/drivers/md/bcache/bset.h
+++ b/drivers/md/bcache/bset.h
@@ -246,12 +246,14 @@ static inline bool bkey_written(struct btree_keys *b, 
struct bkey *k)
return !b->last_set_unwritten || k < b->set[b->nsets].data->start;
 }
 
-static inline unsigned int bset_byte_offset(struct btree_keys *b, struct bset 
*i)
+static inline unsigned int bset_byte_offset(struct btree_keys *b,
+   struct bset *i)
 {
return ((size_t) i) - ((size_t) b->set->data);
 }
 
-static inline unsigned int bset_sector_offset(struct btree_keys *b, struct 
bset *i)
+static inline unsigned int bset_sector_offset(struct btree_keys *b,
+ struct bset *i)
 {
return bset_byte_offset(b, i) >> 9;
 }
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index a797ef359a21..c2e4174a2e03 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -436,7 +436,10 @@ static void do_btree_node_write(struct btree *b)
 
continue_at(cl, btree_node_write_done, NULL);
} else {
-   /* No problem for multipage bvec since the bio is just 
allocated */
+   /*
+* No problem for multipage bvec since the bio is
+* just allocated
+*/
b->bio->bi_vcnt = 0;
bch_bio_map(b->bio, i);
 
diff --git a/drivers/md/bcache/btree.h b/drivers/md/bcache/btree.h
index f13b71a0613a..a68d6c55783b 100644
--- a/drivers/md/bcache/btree.h
+++ b/drivers/md/bcache/btree.h
@@ -306,7 +306,9 @@ bool bch_keybuf_check_overlapping(struct keybuf *buf, 
struct bkey *start,
  struct bkey *end);
 void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w);
 struct keybuf_key *bch_keybuf_next(struct keybuf *buf);
-s

[PATCH v3 03/17] bcache: add identifier names to arguments of function definitions

2018-08-10 Thread Coly Li
There are many function definitions do not have identifier argument names,
scripts/checkpatch.pl complains warnings like this,

 WARNING: function definition argument 'struct bcache_device *' should
  also have an identifier name
  #16735: FILE: writeback.h:120:
  +void bch_sectors_dirty_init(struct bcache_device *);

This patch adds identifier argument names to all bcache function
definitions to fix such warnings.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/bcache.h| 112 +++---
 drivers/md/bcache/bset.h  | 126 +++---
 drivers/md/bcache/btree.c |   6 +-
 drivers/md/bcache/btree.h |  80 ++---
 drivers/md/bcache/debug.h |   6 +-
 drivers/md/bcache/extents.h   |   6 +-
 drivers/md/bcache/journal.c   |   2 +-
 drivers/md/bcache/journal.h   |  20 +++---
 drivers/md/bcache/request.c   |   2 +-
 drivers/md/bcache/request.h   |   2 +-
 drivers/md/bcache/stats.h |  13 ++--
 drivers/md/bcache/super.c |   4 +-
 drivers/md/bcache/util.h  |  12 ++--
 drivers/md/bcache/writeback.h |   9 +--
 14 files changed, 215 insertions(+), 185 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index fd74dd075951..0bd505c61943 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -273,9 +273,10 @@ struct bcache_device {
 
unsigned intdata_csum:1;
 
-   int (*cache_miss)(struct btree *, struct search *,
- struct bio *, unsigned int);
-   int (*ioctl) (struct bcache_device *, fmode_t, unsigned int, unsigned 
long);
+   int (*cache_miss)(struct btree *b, struct search *s,
+ struct bio *bio, unsigned int sectors);
+   int (*ioctl) (struct bcache_device *d, fmode_t mode,
+ unsigned int cmd, unsigned long arg);
 };
 
 struct io {
@@ -925,41 +926,43 @@ static inline void wait_for_kthread_stop(void)
 /* Forward declarations */
 
 void bch_count_backing_io_errors(struct cached_dev *dc, struct bio *bio);
-void bch_count_io_errors(struct cache *, blk_status_t, int, const char *);
-void bch_bbio_count_io_errors(struct cache_set *, struct bio *,
- blk_status_t, const char *);
-void bch_bbio_endio(struct cache_set *, struct bio *, blk_status_t,
-   const char *);
-void bch_bbio_free(struct bio *, struct cache_set *);
-struct bio *bch_bbio_alloc(struct cache_set *);
-
-void __bch_submit_bbio(struct bio *, struct cache_set *);
-void bch_submit_bbio(struct bio *, struct cache_set *,
-struct bkey *, unsigned int);
-
-uint8_t bch_inc_gen(struct cache *, struct bucket *);
-void bch_rescale_priorities(struct cache_set *, int);
-
-bool bch_can_invalidate_bucket(struct cache *, struct bucket *);
-void __bch_invalidate_one_bucket(struct cache *, struct bucket *);
-
-void __bch_bucket_free(struct cache *, struct bucket *);
-void bch_bucket_free(struct cache_set *, struct bkey *);
-
-long bch_bucket_alloc(struct cache *, unsigned int, bool);
-int __bch_bucket_alloc_set(struct cache_set *, unsigned int,
-  struct bkey *, int, bool);
-int bch_bucket_alloc_set(struct cache_set *, unsigned int,
-struct bkey *, int, bool);
-bool bch_alloc_sectors(struct cache_set *, struct bkey *, unsigned int,
-  unsigned int, unsigned int, bool);
+void bch_count_io_errors(struct cache *ca, blk_status_t error,
+int is_read, const char *m);
+void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
+ blk_status_t error, const char *m);
+void bch_bbio_endio(struct cache_set *c, struct bio *bio,
+   blk_status_t error, const char *m);
+void bch_bbio_free(struct bio *bio, struct cache_set *c);
+struct bio *bch_bbio_alloc(struct cache_set *c);
+
+void __bch_submit_bbio(struct bio *bio, struct cache_set *c);
+void bch_submit_bbio(struct bio *bio, struct cache_set *c,
+struct bkey *k, unsigned int ptr);
+
+uint8_t bch_inc_gen(struct cache *ca, struct bucket *b);
+void bch_rescale_priorities(struct cache_set *c, int sectors);
+
+bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b);
+void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b);
+
+void __bch_bucket_free(struct cache *ca, struct bucket *b);
+void bch_bucket_free(struct cache_set *c, struct bkey *k);
+
+long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait);
+int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
+  struct bkey *k, int n, bool wait);
+int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
+struct bkey *k, int n, bool wait);
+bool bch_alloc_sectors(struct cache_set *c, struct bkey *k,
+  unsigned int sectors, unsigned int write_point,
+  unsigned int

[PATCH v3 05/17] bcache: replace Symbolic permissions by octal permission numbers

2018-08-10 Thread Coly Li
Symbolic permission names are used in bcache, for now octal permission
numbers are encouraged to use for readability. This patch replaces
all symbolic permissions by octal permission numbers.

Signed-off-by: Coly Li 
Reviewed-by: Shenghui Wang 
---
 drivers/md/bcache/bcache.h | 4 ++--
 drivers/md/bcache/sysfs.h  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 031a75a25d3e..66e6d5639b38 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -881,11 +881,11 @@ static inline uint8_t bucket_gc_gen(struct bucket *b)
 #define BUCKET_GC_GEN_MAX  96U
 
 #define kobj_attribute_write(n, fn)\
-   static struct kobj_attribute ksysfs_##n = __ATTR(n, S_IWUSR, NULL, fn)
+   static struct kobj_attribute ksysfs_##n = __ATTR(n, 0200, NULL, fn)
 
 #define kobj_attribute_rw(n, show, store)  \
static struct kobj_attribute ksysfs_##n =   \
-   __ATTR(n, S_IWUSR|S_IRUSR, show, store)
+   __ATTR(n, 0600, show, store)
 
 static inline void wake_up_allocators(struct cache_set *c)
 {
diff --git a/drivers/md/bcache/sysfs.h b/drivers/md/bcache/sysfs.h
index b54fe9602529..3fe82425859c 100644
--- a/drivers/md/bcache/sysfs.h
+++ b/drivers/md/bcache/sysfs.h
@@ -44,9 +44,9 @@ STORE(fn) 
\
static struct attribute sysfs_##_name = \
{ .name = #_name, .mode = _mode }
 
-#define write_attribute(n) __sysfs_attribute(n, S_IWUSR)
-#define read_attribute(n)  __sysfs_attribute(n, S_IRUGO)
-#define rw_attribute(n)__sysfs_attribute(n, S_IRUGO|S_IWUSR)
+#define write_attribute(n) __sysfs_attribute(n, 0200)
+#define read_attribute(n)  __sysfs_attribute(n, 0444)
+#define rw_attribute(n)__sysfs_attribute(n, 0644)
 
 #define sysfs_printf(file, fmt, ...)   \
 do {   \
-- 
2.18.0



[PATCH v3 00/17] fixes reported by checkpatch.pl

2018-08-10 Thread Coly Li
This series contains several minor fixes and code cleanup reported by
scripts/checkpatch.pl. After this series, there are still a few warning
from checkpatch.pl, but I think they are necessary to be the way they
are and don't change them. For example, error message exceeds 80 chars
line limit.

Thanks in advance for any comment or review.

Changelog:
v3: Fix coments from Shenghui Wang.
v2: Fix comments from Shenghui Wang.
v1: Initial version

Coly Li 
---
Coly Li (17):
  bcache: style fix to replace 'unsigned' by 'unsigned int'
  bcache: style fix to add a blank line after declarations
  bcache: add identifier names to arguments of function definitions
  bcache: style fixes for lines over 80 characters
  bcache: replace Symbolic permissions by octal permission numbers
  bcache: replace printk() by pr_*() routines
  bcache: fix indent by replacing blank by tabs
  bcache: replace '%pF' by '%pS' in seq_printf()
  bcache: fix typo 'succesfully' to 'successfully'
  bcache: prefer 'help' in Kconfig
  bcache: do not check NULL pointer before calling kmem_cache_destroy
  bcache: fix code comments style
  bcache: add static const prefix to char * array declarations
  bcache: move open brace at end of function definitions to next line
  bcache: add missing SPDX header
  bcache: remove unnecessary space before ioctl function pointer
arguments
  bcache: add the missing comments for smp_mb()/smp_wmb()

 drivers/md/bcache/Kconfig |   6 +-
 drivers/md/bcache/alloc.c |  39 ---
 drivers/md/bcache/bcache.h| 210 +-
 drivers/md/bcache/bset.c  | 142 +--
 drivers/md/bcache/bset.h  | 146 ---
 drivers/md/bcache/btree.c |  72 +++-
 drivers/md/bcache/btree.h |  86 +++---
 drivers/md/bcache/closure.c   |   6 +-
 drivers/md/bcache/closure.h   |   6 +-
 drivers/md/bcache/debug.c |  23 ++--
 drivers/md/bcache/debug.h |   6 +-
 drivers/md/bcache/extents.c   |  37 +++---
 drivers/md/bcache/extents.h   |   6 +-
 drivers/md/bcache/io.c|  24 ++--
 drivers/md/bcache/journal.c   |  27 +++--
 drivers/md/bcache/journal.h   |  28 ++---
 drivers/md/bcache/movinggc.c  |  14 ++-
 drivers/md/bcache/request.c   |  61 +-
 drivers/md/bcache/request.h   |  18 +--
 drivers/md/bcache/stats.c |  15 ++-
 drivers/md/bcache/stats.h |  15 ++-
 drivers/md/bcache/super.c | 107 ++---
 drivers/md/bcache/sysfs.c |  36 +++---
 drivers/md/bcache/sysfs.h |   6 +-
 drivers/md/bcache/util.c  |   2 +
 drivers/md/bcache/util.h  |  24 ++--
 drivers/md/bcache/writeback.c |  30 +++--
 drivers/md/bcache/writeback.h |  19 +--
 include/uapi/linux/bcache.h   |   8 +-
 29 files changed, 679 insertions(+), 540 deletions(-)

-- 
2.18.0



[PATCH] bcache: fix 0day error of setting writeback_rate by sysfs interface

2018-08-10 Thread Coly Li
Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request
is idle") changes struct bch_ratelimit member rate from uint32_t to
atomic_long_t and uses atomic_long_set() in drivers/md/bcache/sysfs.c
to set new writeback rate, after the input is converted from memory
buf to long int by sysfs_strtoul_clamp().

The above change has a problem because there is an implicit return
inside sysfs_strtoul_clamp() so the following atomic_long_set()
won't be called. This error is detected by 0day system with following
snipped smatch warnings:

drivers/md/bcache/sysfs.c:271 __cached_dev_store() error: uninitialized
symbol 'v'.
270  sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
 ^^^
@271 atomic_long_set(>writeback_rate.rate, v);

This patch fixes the above error by using strtoul_safe_clamp() to
convert the input buffer into a long int type result.

Fixes: Commit ea8c5356d390 ("bcache: set max writeback rate when I/O request is 
idle")
Signed-off-by: Coly Li 
Cc: sta...@vger.kernel.org #4.16+
Cc: Kai Krakow 
Cc: Stefan Priebe 
---
 drivers/md/bcache/sysfs.c | 13 ++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 543b06408321..150cf4f4cf74 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -267,10 +267,17 @@ STORE(__cached_dev)
sysfs_strtoul_clamp(writeback_percent, dc->writeback_percent, 0, 40);
 
if (attr == _writeback_rate) {
-   int v;
+   ssize_t ret;
+   long int v = atomic_long_read(>writeback_rate.rate);
+
+   ret = strtoul_safe_clamp(buf, v, 1, INT_MAX);
 
-   sysfs_strtoul_clamp(writeback_rate, v, 1, INT_MAX);
-   atomic_long_set(>writeback_rate.rate, v);
+   if (!ret) {
+   atomic_long_set(>writeback_rate.rate, v);
+   ret = size;
+   }
+
+   return ret;
}
 
sysfs_strtoul_clamp(writeback_rate_update_seconds,
-- 
2.18.0



Re: [PATCH v2 03/17] bcache: add identifier names to arguments of function definitions

2018-08-10 Thread Coly Li
On 2018/8/10 4:26 PM, shenghui wrote:
> 
> 
> On 08/10/2018 03:23 PM, Coly Li wrote:
>> There are many function definitions do not have identifier argument names,
>> scripts/checkpatch.pl complains warnings like this,
>>
>>  WARNING: function definition argument 'struct bcache_device *' should
>>   also have an identifier name
>>   #16735: FILE: writeback.h:120:
>>   +void bch_sectors_dirty_init(struct bcache_device *);
>>
>> This patch adds identifier argument names to all bcache function
>> definitions to fix such warnings.
>>
>> Signed-off-by: Coly Li 
>> Cc: Shenghui Wang 
>> ---
>>  drivers/md/bcache/bcache.h| 112 +++---
>>  drivers/md/bcache/bset.h  | 126 +++---
>>  drivers/md/bcache/btree.c |   6 +-
>>  drivers/md/bcache/btree.h |  80 ++---
>>  drivers/md/bcache/debug.h |   6 +-
>>  drivers/md/bcache/extents.h   |   6 +-
>>  drivers/md/bcache/journal.h   |  20 +++---
> 
> In journal.c, there is:
> 585 static void journal_write(struct closure *);
> 
> In super.c, there is:
> 2150 static ssize_t register_bcache(struct kobject *, struct kobj_attribute *,
> 2151const char *, size_t);
> 
> 

Nice catch, it will be fixed in v3 series.

Thanks.

Coly Li


[PATCH v2 17/17] bcache: add the missing comments for smp_mb()/smp_wmb()

2018-08-10 Thread Coly Li
Checkpatch.pl warns there are 2 locations of smp_mb() and smp_wmb()
without code comment. This patch adds the missing code comments for
these memory barrier calls.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/closure.h | 4 +++-
 drivers/md/bcache/super.c   | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/closure.h b/drivers/md/bcache/closure.h
index 7f3594c0be14..eca0d496b686 100644
--- a/drivers/md/bcache/closure.h
+++ b/drivers/md/bcache/closure.h
@@ -289,10 +289,12 @@ static inline void closure_init_stack(struct closure *cl)
 }
 
 /**
- * closure_wake_up - wake up all closures on a wait list.
+ * closure_wake_up - wake up all closures on a wait list,
+ *  with memory barrier
  */
 static inline void closure_wake_up(struct closure_waitlist *list)
 {
+   /* Memory barrier for the wait list */
smp_mb();
__closure_wake_up(list);
 }
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index d09c5fe4f758..0dbd1bcb0d38 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1136,11 +1136,11 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct 
cache_set *c,
list_move(>list, >cached_devs);
calc_cached_dev_sectors(c);
 
-   smp_wmb();
/*
 * dc->c must be set before dc->count != 0 - paired with the mb in
 * cached_dev_get()
 */
+   smp_wmb();
refcount_set(>count, 1);
 
/* Block writeback thread, but spawn it */
-- 
2.18.0



[PATCH v2 14/17] bcache: move open brace at end of function definitions to next line

2018-08-10 Thread Coly Li
This is not a preferred style to place open brace '{' at the end of
function definition, checkpatch.pl reports error for such coding
style. This patch moves them into the start of the next new line.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/super.c | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 1b5ace425261..18fa72e88342 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2152,7 +2152,8 @@ static ssize_t register_bcache(struct kobject *, struct 
kobj_attribute *,
 kobj_attribute_write(register, register_bcache);
 kobj_attribute_write(register_quiet,   register_bcache);
 
-static bool bch_is_open_backing(struct block_device *bdev) {
+static bool bch_is_open_backing(struct block_device *bdev)
+{
struct cache_set *c, *tc;
struct cached_dev *dc, *t;
 
@@ -2166,7 +2167,8 @@ static bool bch_is_open_backing(struct block_device 
*bdev) {
return false;
 }
 
-static bool bch_is_open_cache(struct block_device *bdev) {
+static bool bch_is_open_cache(struct block_device *bdev)
+{
struct cache_set *c, *tc;
struct cache *ca;
unsigned int i;
@@ -2178,7 +2180,8 @@ static bool bch_is_open_cache(struct block_device *bdev) {
return false;
 }
 
-static bool bch_is_open(struct block_device *bdev) {
+static bool bch_is_open(struct block_device *bdev)
+{
return bch_is_open_cache(bdev) || bch_is_open_backing(bdev);
 }
 
-- 
2.18.0



[PATCH v2 15/17] bcache: add missing SPDX header

2018-08-10 Thread Coly Li
The SPDX header is missing fro closure.c, super.c and util.c, this
patch adds SPDX header for GPL-2.0 into these files.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/closure.c | 1 +
 drivers/md/bcache/super.c   | 1 +
 drivers/md/bcache/util.c| 1 +
 3 files changed, 3 insertions(+)

diff --git a/drivers/md/bcache/closure.c b/drivers/md/bcache/closure.c
index 034067e0e9ce..73f5319295bc 100644
--- a/drivers/md/bcache/closure.c
+++ b/drivers/md/bcache/closure.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Asynchronous refcounty things
  *
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 18fa72e88342..d09c5fe4f758 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * bcache setup/teardown code, and some metadata io - read a superblock and
  * figure out what to do with it.
diff --git a/drivers/md/bcache/util.c b/drivers/md/bcache/util.c
index 18016e7bb32c..e873b0f7a82a 100644
--- a/drivers/md/bcache/util.c
+++ b/drivers/md/bcache/util.c
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * random utiility code, for bcache but in theory not specific to bcache
  *
-- 
2.18.0



[PATCH v2 16/17] bcache: remove unnecessary space before ioctl function pointer arguments

2018-08-10 Thread Coly Li
This is warned by checkpatch.pl, this patch removes the extra space.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/bcache.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 66e6d5639b38..83504dd8100a 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -275,8 +275,8 @@ struct bcache_device {
 
int (*cache_miss)(struct btree *b, struct search *s,
  struct bio *bio, unsigned int sectors);
-   int (*ioctl) (struct bcache_device *d, fmode_t mode,
- unsigned int cmd, unsigned long arg);
+   int (*ioctl)(struct bcache_device *d, fmode_t mode,
+unsigned int cmd, unsigned long arg);
 };
 
 struct io {
-- 
2.18.0



[PATCH v2 11/17] bcache: do not check NULL pointer before calling kmem_cache_destroy

2018-08-10 Thread Coly Li
kmem_cache_destroy() is safe for NULL pointer as input, the NULL pointer
checking is unncessary. This patch just removes the NULL pointer checking
to make code simpler.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/request.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 55264e71369d..51be355a3309 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -1367,8 +1367,7 @@ void bch_flash_dev_request_init(struct bcache_device *d)
 
 void bch_request_exit(void)
 {
-   if (bch_search_cache)
-   kmem_cache_destroy(bch_search_cache);
+   kmem_cache_destroy(bch_search_cache);
 }
 
 int __init bch_request_init(void)
-- 
2.18.0



[PATCH v2 10/17] bcache: prefer 'help' in Kconfig

2018-08-10 Thread Coly Li
Current bcache Kconfig uses '---help---' as header of help information,
for now 'help' is prefered. This patch fixes this style by replacing
'---help---' by 'help' in bcache Kconfig file.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/Kconfig | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/md/bcache/Kconfig b/drivers/md/bcache/Kconfig
index 17bf109c58e9..817b9fba50db 100644
--- a/drivers/md/bcache/Kconfig
+++ b/drivers/md/bcache/Kconfig
@@ -1,7 +1,7 @@
 
 config BCACHE
tristate "Block device as cache"
-   ---help---
+   help
Allows a block device to be used as cache for other devices; uses
a btree for indexing and the layout is optimized for SSDs.
 
@@ -10,7 +10,7 @@ config BCACHE
 config BCACHE_DEBUG
bool "Bcache debugging"
depends on BCACHE
-   ---help---
+   help
Don't select this option unless you're a developer
 
Enables extra debugging tools, allows expensive runtime checks to be
@@ -20,7 +20,7 @@ config BCACHE_CLOSURES_DEBUG
bool "Debug closures"
depends on BCACHE
select DEBUG_FS
-   ---help---
+   help
Keeps all active closures in a linked list and provides a debugfs
interface to list them, which makes it possible to see asynchronous
operations that get stuck.
-- 
2.18.0



[PATCH v2 08/17] bcache: replace '%pF' by '%pS' in seq_printf()

2018-08-10 Thread Coly Li
'%pF' and '%pf' are deprecated vsprintf pointer extensions, this patch
replace them by '%pS', which is suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/closure.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/closure.c b/drivers/md/bcache/closure.c
index 8570fc426e31..034067e0e9ce 100644
--- a/drivers/md/bcache/closure.c
+++ b/drivers/md/bcache/closure.c
@@ -168,7 +168,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
list_for_each_entry(cl, _list, all) {
int r = atomic_read(>remaining);
 
-   seq_printf(f, "%p: %pF -> %pf p %p r %i ",
+   seq_printf(f, "%p: %pS -> %pS p %p r %i ",
   cl, (void *) cl->ip, cl->fn, cl->parent,
   r & CLOSURE_REMAINING_MASK);
 
@@ -178,7 +178,7 @@ static int debug_seq_show(struct seq_file *f, void *data)
   r & CLOSURE_RUNNING  ? "R" : "");
 
if (r & CLOSURE_WAITING)
-   seq_printf(f, " W %pF\n",
+   seq_printf(f, " W %pS\n",
   (void *) cl->waiting_on);
 
seq_printf(f, "\n");
-- 
2.18.0



[PATCH v2 05/17] bcache: replace Symbolic permissions by octal permission numbers

2018-08-10 Thread Coly Li
Symbolic permission names are used in bcache, for now octal permission
numbers are encouraged to use for readability. This patch replaces
all symbolic permissions by octal permission numbers.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/bcache.h | 4 ++--
 drivers/md/bcache/sysfs.h  | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 031a75a25d3e..66e6d5639b38 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -881,11 +881,11 @@ static inline uint8_t bucket_gc_gen(struct bucket *b)
 #define BUCKET_GC_GEN_MAX  96U
 
 #define kobj_attribute_write(n, fn)\
-   static struct kobj_attribute ksysfs_##n = __ATTR(n, S_IWUSR, NULL, fn)
+   static struct kobj_attribute ksysfs_##n = __ATTR(n, 0200, NULL, fn)
 
 #define kobj_attribute_rw(n, show, store)  \
static struct kobj_attribute ksysfs_##n =   \
-   __ATTR(n, S_IWUSR|S_IRUSR, show, store)
+   __ATTR(n, 0600, show, store)
 
 static inline void wake_up_allocators(struct cache_set *c)
 {
diff --git a/drivers/md/bcache/sysfs.h b/drivers/md/bcache/sysfs.h
index b54fe9602529..3fe82425859c 100644
--- a/drivers/md/bcache/sysfs.h
+++ b/drivers/md/bcache/sysfs.h
@@ -44,9 +44,9 @@ STORE(fn) 
\
static struct attribute sysfs_##_name = \
{ .name = #_name, .mode = _mode }
 
-#define write_attribute(n) __sysfs_attribute(n, S_IWUSR)
-#define read_attribute(n)  __sysfs_attribute(n, S_IRUGO)
-#define rw_attribute(n)__sysfs_attribute(n, S_IRUGO|S_IWUSR)
+#define write_attribute(n) __sysfs_attribute(n, 0200)
+#define read_attribute(n)  __sysfs_attribute(n, 0444)
+#define rw_attribute(n)__sysfs_attribute(n, 0644)
 
 #define sysfs_printf(file, fmt, ...)   \
 do {   \
-- 
2.18.0



[PATCH v2 09/17] bcache: fix typo 'succesfully' to 'successfully'

2018-08-10 Thread Coly Li
This patch fixes typo 'succesfully' to correct 'successfully', which is
suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/io.c  | 2 +-
 drivers/md/bcache/request.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/io.c b/drivers/md/bcache/io.c
index cfc56add799a..c25097968319 100644
--- a/drivers/md/bcache/io.c
+++ b/drivers/md/bcache/io.c
@@ -86,7 +86,7 @@ void bch_count_io_errors(struct cache *ca,
 
/*
 * First we subtract refresh from count; each time we
-* succesfully do so, we rescale the errors once:
+* successfully do so, we rescale the errors once:
 */
 
count = atomic_cmpxchg(>io_count, old, new);
diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 858dd3da9dc5..55264e71369d 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -154,7 +154,7 @@ static void bch_data_insert_error(struct closure *cl)
 
/*
 * Our data write just errored, which means we've got a bunch of keys to
-* insert that point to data that wasn't succesfully written.
+* insert that point to data that wasn't successfully written.
 *
 * We don't have to insert those keys but we still have to invalidate
 * that region of the cache - so, if we just strip off all the pointers
-- 
2.18.0



[PATCH v2 13/17] bcache: add static const prefix to char * array declarations

2018-08-10 Thread Coly Li
This patch declares char * array with const prefix in sysfs.c,
which is suggested by checkpatch.pl.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 2ed503ab9e5c..543b06408321 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -150,7 +150,7 @@ SHOW(__bch_cached_dev)
 {
struct cached_dev *dc = container_of(kobj, struct cached_dev,
 disk.kobj);
-   const char *states[] = { "no cache", "clean", "dirty", "inconsistent" };
+   char const *states[] = { "no cache", "clean", "dirty", "inconsistent" };
int wb = dc->writeback_running;
 
 #define var(stat)  (dc->stat)
-- 
2.18.0



[PATCH v2 12/17] bcache: fix code comments style

2018-08-10 Thread Coly Li
This patch fixes 3 style issues warned by checkpatch.pl,
- Comment lines are not aligned
- Comments use "/*" on subsequent lines
- Comment lines use a trailing "*/"

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/bset.c  |  9 ++---
 drivers/md/bcache/super.c | 22 +-
 drivers/md/bcache/writeback.c |  3 ++-
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index b6a3f9d291a9..8f07fa6e1739 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -402,7 +402,8 @@ static unsigned int inorder_prev(unsigned int j, unsigned 
int size)
return j;
 }
 
-/* I have no idea why this code works... and I'm the one who wrote it
+/*
+ * I have no idea why this code works... and I'm the one who wrote it
  *
  * However, I do know what it does:
  * Given a binary tree constructed in an array (i.e. how you normally implement
@@ -795,7 +796,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
if (!t->size)
return;
 
-   /* k is the key we just inserted; we need to find the entry in the
+   /*
+* k is the key we just inserted; we need to find the entry in the
 * lookup table for the first key that is strictly greater than k:
 * it's either k's cacheline or the next one
 */
@@ -803,7 +805,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
   table_to_bkey(t, j) <= k)
j++;
 
-   /* Adjust all the lookup table entries, and find a new key for any that
+   /*
+* Adjust all the lookup table entries, and find a new key for any that
 * have gotten too big
 */
for (; j < t->size; j++) {
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 951776e8cd8b..1b5ace425261 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -465,8 +465,8 @@ static struct uuid_entry *uuid_find_empty(struct cache_set 
*c)
  * Bucket priorities/gens:
  *
  * For each bucket, we store on disk its
-   * 8 bit gen
-   * 16 bit priority
+ *   8 bit gen
+ *  16 bit priority
  *
  * See alloc.c for an explanation of the gen. The priority is used to implement
  * lru (and in the future other) cache replacement policies; for most purposes
@@ -934,8 +934,10 @@ void bch_cached_dev_run(struct cached_dev *dc)
 
add_disk(d->disk);
bd_link_disk_holder(dc->bdev, dc->disk.disk);
-   /* won't show up in the uevent file, use udevadm monitor -e instead
-* only class / kset properties are persistent */
+   /*
+* won't show up in the uevent file, use udevadm monitor -e instead
+* only class / kset properties are persistent
+*/
kobject_uevent_env(_to_dev(d->disk)->kobj, KOBJ_CHANGE, env);
kfree(env[1]);
kfree(env[2]);
@@ -1104,8 +1106,9 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct 
cache_set *c,
}
}
 
-   /* Deadlocks since we're called via sysfs...
-   sysfs_remove_file(>kobj, _attach);
+   /*
+* Deadlocks since we're called via sysfs...
+* sysfs_remove_file(>kobj, _attach);
 */
 
if (bch_is_zero(u->uuid, 16)) {
@@ -1468,9 +1471,10 @@ bool bch_cache_set_error(struct cache_set *c, const char 
*fmt, ...)
if (test_and_set_bit(CACHE_SET_IO_DISABLE, >flags))
pr_info("CACHE_SET_IO_DISABLE already set");
 
-   /* XXX: we can be called from atomic context
-   acquire_console_sem();
-   */
+   /*
+* XXX: we can be called from atomic context
+* acquire_console_sem();
+*/
 
pr_err("bcache: error on %pU: ", c->sb.set_uuid);
 
diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
index e40bf0e403e7..6be05bd7ca67 100644
--- a/drivers/md/bcache/writeback.c
+++ b/drivers/md/bcache/writeback.c
@@ -468,7 +468,8 @@ static void read_dirty(struct cached_dev *dc)
 
down(>in_flight);
 
-   /* We've acquired a semaphore for the maximum
+   /*
+* We've acquired a semaphore for the maximum
 * simultaneous number of writebacks; from here
 * everything happens asynchronously.
 */
-- 
2.18.0



[PATCH v2 07/17] bcache: fix indent by replacing blank by tabs

2018-08-10 Thread Coly Li
bch_btree_insert_check_key() has unaligned indent, or indent by blank
characters. This patch makes the indent aligned and replace blank by
tabs.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/btree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index c2e4174a2e03..e7d4817681f2 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -2231,10 +2231,10 @@ int bch_btree_insert_check_key(struct btree *b, struct 
btree_op *op,
rw_lock(true, b, b->level);
 
if (b->key.ptr[0] != btree_ptr ||
-   b->seq != seq + 1) {
+   b->seq != seq + 1) {
op->lock = b->level;
goto out;
-   }
+   }
}
 
SET_KEY_PTRS(check_key, 1);
-- 
2.18.0



[PATCH v2 06/17] bcache: replace printk() by pr_*() routines

2018-08-10 Thread Coly Li
There are still many places in bcache use printk to display kernel
message, which are suggested to be preplaced by pr_*() routines like
pr_err(), pr_info(), or pr_notice().

This patch replaces all printk() with a proper pr_*() routine for
bcache code.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/bset.c|  8 
 drivers/md/bcache/debug.c   | 10 +-
 drivers/md/bcache/extents.c |  8 
 drivers/md/bcache/super.c   |  4 ++--
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index 19b4febe5b45..b6a3f9d291a9 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -25,18 +25,18 @@ void bch_dump_bset(struct btree_keys *b, struct bset *i, 
unsigned int set)
for (k = i->start; k < bset_bkey_last(i); k = next) {
next = bkey_next(k);
 
-   printk(KERN_ERR "block %u key %u/%u: ", set,
+   pr_err("block %u key %u/%u: ", set,
   (unsigned int) ((u64 *) k - i->d), i->keys);
 
if (b->ops->key_dump)
b->ops->key_dump(b, k);
else
-   printk("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
+   pr_err("%llu:%llu\n", KEY_INODE(k), KEY_OFFSET(k));
 
if (next < bset_bkey_last(i) &&
bkey_cmp(k, b->ops->is_extents ?
 _KEY(next) : next) > 0)
-   printk(KERN_ERR "Key skipped backwards\n");
+   pr_err("Key skipped backwards\n");
}
 }
 
@@ -482,7 +482,7 @@ void inorder_test(void)
unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
 
if (!(size % 4096))
-   printk(KERN_NOTICE "loop %u, %llu per us\n", size,
+   pr_notice("loop %u, %llu per us\n", size,
   done / ktime_us_delta(ktime_get(), start));
 
while (1) {
diff --git a/drivers/md/bcache/debug.c b/drivers/md/bcache/debug.c
index a8f70c916fdb..06da66b2488a 100644
--- a/drivers/md/bcache/debug.c
+++ b/drivers/md/bcache/debug.c
@@ -74,28 +74,28 @@ void bch_btree_verify(struct btree *b)
 
console_lock();
 
-   printk(KERN_ERR "*** in memory:\n");
+   pr_err("*** in memory:\n");
bch_dump_bset(>keys, inmemory, 0);
 
-   printk(KERN_ERR "*** read back in:\n");
+   pr_err("*** read back in:\n");
bch_dump_bset(>keys, sorted, 0);
 
for_each_written_bset(b, ondisk, i) {
unsigned int block = ((void *) i - (void *) ondisk) /
block_bytes(b->c);
 
-   printk(KERN_ERR "*** on disk block %u:\n", block);
+   pr_err("*** on disk block %u:\n", block);
bch_dump_bset(>keys, i, block);
}
 
-   printk(KERN_ERR "*** block %zu not written\n",
+   pr_err("*** block %zu not written\n",
   ((void *) i - (void *) ondisk) / block_bytes(b->c));
 
for (j = 0; j < inmemory->keys; j++)
if (inmemory->d[j] != sorted->d[j])
break;
 
-   printk(KERN_ERR "b->written %u\n", b->written);
+   pr_err("b->written %u\n", b->written);
 
console_unlock();
panic("verify failed at %u\n", j);
diff --git a/drivers/md/bcache/extents.c b/drivers/md/bcache/extents.c
index cb3b2c613ed6..c809724e6571 100644
--- a/drivers/md/bcache/extents.c
+++ b/drivers/md/bcache/extents.c
@@ -130,18 +130,18 @@ static void bch_bkey_dump(struct btree_keys *keys, const 
struct bkey *k)
char buf[80];
 
bch_extent_to_text(buf, sizeof(buf), k);
-   printk(" %s", buf);
+   pr_err(" %s", buf);
 
for (j = 0; j < KEY_PTRS(k); j++) {
size_t n = PTR_BUCKET_NR(b->c, k, j);
 
-   printk(" bucket %zu", n);
+   pr_err(" bucket %zu", n);
if (n >= b->c->sb.first_bucket && n < b->c->sb.nbuckets)
-   printk(" prio %i",
+   pr_err(" prio %i",
   PTR_BUCKET(b->c, k, j)->prio);
}
 
-   printk(" %s\n", bch_ptr_status(b->c, k));
+   pr_err(" %s\n", bch_ptr_status(b->c, k));
 }
 
 /* Btree ptrs */
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index fb4d885e4093..95177

[PATCH v2 03/17] bcache: add identifier names to arguments of function definitions

2018-08-10 Thread Coly Li
There are many function definitions do not have identifier argument names,
scripts/checkpatch.pl complains warnings like this,

 WARNING: function definition argument 'struct bcache_device *' should
  also have an identifier name
  #16735: FILE: writeback.h:120:
  +void bch_sectors_dirty_init(struct bcache_device *);

This patch adds identifier argument names to all bcache function
definitions to fix such warnings.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/bcache.h| 112 +++---
 drivers/md/bcache/bset.h  | 126 +++---
 drivers/md/bcache/btree.c |   6 +-
 drivers/md/bcache/btree.h |  80 ++---
 drivers/md/bcache/debug.h |   6 +-
 drivers/md/bcache/extents.h   |   6 +-
 drivers/md/bcache/journal.h   |  20 +++---
 drivers/md/bcache/request.c   |   2 +-
 drivers/md/bcache/request.h   |   2 +-
 drivers/md/bcache/stats.h |  13 ++--
 drivers/md/bcache/util.h  |  12 ++--
 drivers/md/bcache/writeback.h |   9 +--
 12 files changed, 212 insertions(+), 182 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index fd74dd075951..0bd505c61943 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -273,9 +273,10 @@ struct bcache_device {
 
unsigned intdata_csum:1;
 
-   int (*cache_miss)(struct btree *, struct search *,
- struct bio *, unsigned int);
-   int (*ioctl) (struct bcache_device *, fmode_t, unsigned int, unsigned 
long);
+   int (*cache_miss)(struct btree *b, struct search *s,
+ struct bio *bio, unsigned int sectors);
+   int (*ioctl) (struct bcache_device *d, fmode_t mode,
+ unsigned int cmd, unsigned long arg);
 };
 
 struct io {
@@ -925,41 +926,43 @@ static inline void wait_for_kthread_stop(void)
 /* Forward declarations */
 
 void bch_count_backing_io_errors(struct cached_dev *dc, struct bio *bio);
-void bch_count_io_errors(struct cache *, blk_status_t, int, const char *);
-void bch_bbio_count_io_errors(struct cache_set *, struct bio *,
- blk_status_t, const char *);
-void bch_bbio_endio(struct cache_set *, struct bio *, blk_status_t,
-   const char *);
-void bch_bbio_free(struct bio *, struct cache_set *);
-struct bio *bch_bbio_alloc(struct cache_set *);
-
-void __bch_submit_bbio(struct bio *, struct cache_set *);
-void bch_submit_bbio(struct bio *, struct cache_set *,
-struct bkey *, unsigned int);
-
-uint8_t bch_inc_gen(struct cache *, struct bucket *);
-void bch_rescale_priorities(struct cache_set *, int);
-
-bool bch_can_invalidate_bucket(struct cache *, struct bucket *);
-void __bch_invalidate_one_bucket(struct cache *, struct bucket *);
-
-void __bch_bucket_free(struct cache *, struct bucket *);
-void bch_bucket_free(struct cache_set *, struct bkey *);
-
-long bch_bucket_alloc(struct cache *, unsigned int, bool);
-int __bch_bucket_alloc_set(struct cache_set *, unsigned int,
-  struct bkey *, int, bool);
-int bch_bucket_alloc_set(struct cache_set *, unsigned int,
-struct bkey *, int, bool);
-bool bch_alloc_sectors(struct cache_set *, struct bkey *, unsigned int,
-  unsigned int, unsigned int, bool);
+void bch_count_io_errors(struct cache *ca, blk_status_t error,
+int is_read, const char *m);
+void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
+ blk_status_t error, const char *m);
+void bch_bbio_endio(struct cache_set *c, struct bio *bio,
+   blk_status_t error, const char *m);
+void bch_bbio_free(struct bio *bio, struct cache_set *c);
+struct bio *bch_bbio_alloc(struct cache_set *c);
+
+void __bch_submit_bbio(struct bio *bio, struct cache_set *c);
+void bch_submit_bbio(struct bio *bio, struct cache_set *c,
+struct bkey *k, unsigned int ptr);
+
+uint8_t bch_inc_gen(struct cache *ca, struct bucket *b);
+void bch_rescale_priorities(struct cache_set *c, int sectors);
+
+bool bch_can_invalidate_bucket(struct cache *ca, struct bucket *b);
+void __bch_invalidate_one_bucket(struct cache *ca, struct bucket *b);
+
+void __bch_bucket_free(struct cache *ca, struct bucket *b);
+void bch_bucket_free(struct cache_set *c, struct bkey *k);
+
+long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait);
+int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
+  struct bkey *k, int n, bool wait);
+int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
+struct bkey *k, int n, bool wait);
+bool bch_alloc_sectors(struct cache_set *c, struct bkey *k,
+  unsigned int sectors, unsigned int write_point,
+  unsigned int write_prio, bool wait);
 bool bch_cached_dev_error(struct cached_dev *dc

[PATCH v2 04/17] bcache: style fixes for lines over 80 characters

2018-08-10 Thread Coly Li
This patch fixes the lines over 80 characters into more lines, to minimize
warnings by checkpatch.pl. There are still some lines exceed 80 characters,
but it is better to be a single line and I don't change them.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/bcache.h|  4 ++--
 drivers/md/bcache/bset.c  | 10 +++---
 drivers/md/bcache/bset.h  |  6 --
 drivers/md/bcache/btree.c |  5 -
 drivers/md/bcache/btree.h |  6 --
 drivers/md/bcache/debug.c |  3 ++-
 drivers/md/bcache/extents.c   |  4 +++-
 drivers/md/bcache/journal.c   |  3 ++-
 drivers/md/bcache/request.c   |  7 +--
 drivers/md/bcache/super.c | 18 --
 drivers/md/bcache/sysfs.c | 11 +++
 drivers/md/bcache/util.h  |  3 ++-
 drivers/md/bcache/writeback.c |  7 +--
 13 files changed, 59 insertions(+), 28 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 0bd505c61943..031a75a25d3e 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -614,8 +614,8 @@ struct cache_set {
uint16_tmin_prio;
 
/*
-* max(gen - last_gc) for all buckets. When it gets too big we have to 
gc
-* to keep gens from wrapping around.
+* max(gen - last_gc) for all buckets. When it gets too big we have to
+* gc to keep gens from wrapping around.
 */
uint8_t need_gc;
struct gc_stat  gc_stats;
diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index 6fd5623b2e63..19b4febe5b45 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -311,7 +311,9 @@ void bch_btree_keys_free(struct btree_keys *b)
 }
 EXPORT_SYMBOL(bch_btree_keys_free);
 
-int bch_btree_keys_alloc(struct btree_keys *b, unsigned int page_order, gfp_t 
gfp)
+int bch_btree_keys_alloc(struct btree_keys *b,
+unsigned int page_order,
+gfp_t gfp)
 {
struct bset_tree *t = b->set;
 
@@ -475,7 +477,8 @@ void inorder_test(void)
for (unsigned int size = 2;
 size < 65536000;
 size++) {
-   unsigned int extra = (size - rounddown_pow_of_two(size - 1)) << 
1;
+   unsigned int extra =
+   (size - rounddown_pow_of_two(size - 1)) << 1;
unsigned int i = 1, j = rounddown_pow_of_two(size - 1);
 
if (!(size % 4096))
@@ -825,7 +828,8 @@ static void bch_bset_fix_lookup_table(struct btree_keys *b,
 k != bset_bkey_last(t->data);
 k = bkey_next(k))
if (t->size == bkey_to_cacheline(t, k)) {
-   t->prev[t->size] = bkey_to_cacheline_offset(t, t->size, 
k);
+   t->prev[t->size] =
+   bkey_to_cacheline_offset(t, t->size, k);
t->size++;
}
 }
diff --git a/drivers/md/bcache/bset.h b/drivers/md/bcache/bset.h
index f5bf333aa40d..bac76aabca6d 100644
--- a/drivers/md/bcache/bset.h
+++ b/drivers/md/bcache/bset.h
@@ -246,12 +246,14 @@ static inline bool bkey_written(struct btree_keys *b, 
struct bkey *k)
return !b->last_set_unwritten || k < b->set[b->nsets].data->start;
 }
 
-static inline unsigned int bset_byte_offset(struct btree_keys *b, struct bset 
*i)
+static inline unsigned int bset_byte_offset(struct btree_keys *b,
+   struct bset *i)
 {
return ((size_t) i) - ((size_t) b->set->data);
 }
 
-static inline unsigned int bset_sector_offset(struct btree_keys *b, struct 
bset *i)
+static inline unsigned int bset_sector_offset(struct btree_keys *b,
+ struct bset *i)
 {
return bset_byte_offset(b, i) >> 9;
 }
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index a797ef359a21..c2e4174a2e03 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -436,7 +436,10 @@ static void do_btree_node_write(struct btree *b)
 
continue_at(cl, btree_node_write_done, NULL);
} else {
-   /* No problem for multipage bvec since the bio is just 
allocated */
+   /*
+* No problem for multipage bvec since the bio is
+* just allocated
+*/
b->bio->bi_vcnt = 0;
bch_bio_map(b->bio, i);
 
diff --git a/drivers/md/bcache/btree.h b/drivers/md/bcache/btree.h
index f13b71a0613a..a68d6c55783b 100644
--- a/drivers/md/bcache/btree.h
+++ b/drivers/md/bcache/btree.h
@@ -306,7 +306,9 @@ bool bch_keybuf_check_overlapping(struct keybuf *buf, 
struct bkey *start,
  struct bkey *end);
 void bch_keybuf_del(struct keybuf *buf, struct keybuf_key *w);
 struct keybuf_key *bch_keybuf_next(struct keybuf *buf);
-s

[PATCH v2 02/17] bcache: style fix to add a blank line after declarations

2018-08-10 Thread Coly Li
Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/alloc.c |  3 +++
 drivers/md/bcache/bcache.h|  1 +
 drivers/md/bcache/bset.c  |  5 -
 drivers/md/bcache/btree.c |  7 +++
 drivers/md/bcache/closure.c   |  1 +
 drivers/md/bcache/debug.c |  4 ++--
 drivers/md/bcache/extents.c   |  5 -
 drivers/md/bcache/io.c|  4 +++-
 drivers/md/bcache/journal.c   |  2 ++
 drivers/md/bcache/movinggc.c  |  2 ++
 drivers/md/bcache/request.c   |  5 -
 drivers/md/bcache/stats.c |  3 +++
 drivers/md/bcache/super.c | 13 -
 drivers/md/bcache/sysfs.c |  5 +
 drivers/md/bcache/util.c  |  1 +
 drivers/md/bcache/writeback.c |  1 +
 include/uapi/linux/bcache.h   |  2 ++
 17 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 89f663d22551..7a28232d868b 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -244,6 +244,7 @@ static void invalidate_buckets_random(struct cache *ca)
 
while (!fifo_full(>free_inc)) {
size_t n;
+
get_random_bytes(, sizeof(n));
 
n %= (size_t) (ca->sb.nbuckets - ca->sb.first_bucket);
@@ -514,6 +515,7 @@ int bch_bucket_alloc_set(struct cache_set *c, unsigned int 
reserve,
 struct bkey *k, int n, bool wait)
 {
int ret;
+
mutex_lock(>bucket_lock);
ret = __bch_bucket_alloc_set(c, reserve, k, n, wait);
mutex_unlock(>bucket_lock);
@@ -706,6 +708,7 @@ int bch_open_buckets_alloc(struct cache_set *c)
 
for (i = 0; i < MAX_OPEN_BUCKETS; i++) {
struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL);
+
if (!b)
return -ENOMEM;
 
diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 1ebd2d9d90d5..fd74dd075951 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -783,6 +783,7 @@ static inline struct bucket *PTR_BUCKET(struct cache_set *c,
 static inline uint8_t gen_after(uint8_t a, uint8_t b)
 {
uint8_t r = a - b;
+
return r > 128U ? 0 : r;
 }
 
diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index dfda7e9efc3e..6fd5623b2e63 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -585,6 +585,7 @@ static inline unsigned int bfloat_mantissa(const struct 
bkey *k,
   struct bkey_float *f)
 {
const uint64_t *p = >low - (f->exponent >> 6);
+
return shrd128(p[-1], p[0], f->exponent & 63) & BKEY_MANTISSA_MASK;
 }
 
@@ -964,6 +965,7 @@ static struct bset_search_iter bset_search_tree(struct 
bset_tree *t,
 * but a branch instruction is avoided.
 */
unsigned int p = n << 4;
+
p &= ((int) (p - t->size)) >> 31;
 
prefetch(>tree[p]);
@@ -1114,6 +1116,7 @@ static struct bkey *__bch_btree_iter_init(struct 
btree_keys *b,
  struct bset_tree *start)
 {
struct bkey *ret = NULL;
+
iter->size = ARRAY_SIZE(iter->data);
iter->used = 0;
 
@@ -1329,8 +1332,8 @@ void bch_btree_sort_into(struct btree_keys *b, struct 
btree_keys *new,
 struct bset_sort_state *state)
 {
uint64_t start_time = local_clock();
-
struct btree_iter iter;
+
bch_btree_iter_init(b, , NULL);
 
btree_mergesort(b, new->set->data, , false, true);
diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c
index 96c39a8db895..4003f92f4d2c 100644
--- a/drivers/md/bcache/btree.c
+++ b/drivers/md/bcache/btree.c
@@ -287,6 +287,7 @@ void bch_btree_node_read_done(struct btree *b)
 static void btree_node_read_endio(struct bio *bio)
 {
struct closure *cl = bio->bi_private;
+
closure_put(cl);
 }
 
@@ -604,6 +605,7 @@ static struct btree *mca_bucket_alloc(struct cache_set *c,
  struct bkey *k, gfp_t gfp)
 {
struct btree *b = kzalloc(sizeof(struct btree), gfp);
+
if (!b)
return NULL;
 
@@ -746,6 +748,7 @@ void bch_btree_cache_free(struct cache_set *c)
 {
struct btree *b;
struct closure cl;
+
closure_init_stack();
 
if (c->shrink.list.next)
@@ -1124,6 +1127,7 @@ static struct btree *btree_node_alloc_replacement(struct 
btree *b,
  struct btree_op *op)
 {
struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent);
+
if (!IS_ERR_OR_NULL(n)) {
mutex_lock(>write_lock);
bch_btree_sort_into(>keys, >keys, >c->sort);
@@ -2488,6 +2492,7 @@ void bch_refill_keybuf(struct cache_set *c, struct keybuf 
*buf,
 
if (!RB_EMPTY_ROOT(>keys)) {
struct keybuf_key *w

[PATCH v2 01/17] bcache: style fix to replace 'unsigned' by 'unsigned int'

2018-08-10 Thread Coly Li
This patch fixes warning reported by checkpatch.pl by replacing 'unsigned'
with 'unsigned int'.

Signed-off-by: Coly Li 
Cc: Shenghui Wang 
---
 drivers/md/bcache/alloc.c |  36 ++-
 drivers/md/bcache/bcache.h| 107 +++
 drivers/md/bcache/bset.c  | 114 ++
 drivers/md/bcache/bset.h  |  34 +-
 drivers/md/bcache/btree.c |  50 +++
 drivers/md/bcache/btree.h |   4 +-
 drivers/md/bcache/closure.h   |   2 +-
 drivers/md/bcache/debug.c |   6 +-
 drivers/md/bcache/extents.c   |  22 +++
 drivers/md/bcache/io.c|  18 +++---
 drivers/md/bcache/journal.c   |  20 +++---
 drivers/md/bcache/journal.h   |   8 +--
 drivers/md/bcache/movinggc.c  |  12 ++--
 drivers/md/bcache/request.c   |  42 ++---
 drivers/md/bcache/request.h   |  18 +++---
 drivers/md/bcache/stats.c |  12 ++--
 drivers/md/bcache/stats.h |   2 +-
 drivers/md/bcache/super.c |  34 +-
 drivers/md/bcache/sysfs.c |  18 +++---
 drivers/md/bcache/util.h  |   9 +--
 drivers/md/bcache/writeback.c |  19 +++---
 drivers/md/bcache/writeback.h |  12 ++--
 include/uapi/linux/bcache.h   |   6 +-
 23 files changed, 309 insertions(+), 296 deletions(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 7fa2631b422c..89f663d22551 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -87,8 +87,8 @@ void bch_rescale_priorities(struct cache_set *c, int sectors)
 {
struct cache *ca;
struct bucket *b;
-   unsigned next = c->nbuckets * c->sb.bucket_size / 1024;
-   unsigned i;
+   unsigned int next = c->nbuckets * c->sb.bucket_size / 1024;
+   unsigned int i;
int r;
 
atomic_sub(sectors, >rescale);
@@ -169,7 +169,7 @@ static void bch_invalidate_one_bucket(struct cache *ca, 
struct bucket *b)
 
 #define bucket_prio(b) \
 ({ \
-   unsigned min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
+   unsigned int min_prio = (INITIAL_PRIO - ca->set->min_prio) / 8; \
\
(b->prio - ca->set->min_prio + min_prio) * GC_SECTORS_USED(b);  \
 })
@@ -301,7 +301,7 @@ do {
\
 
 static int bch_allocator_push(struct cache *ca, long bucket)
 {
-   unsigned i;
+   unsigned int i;
 
/* Prios/gens are actually the most important reserve */
if (fifo_push(>free[RESERVE_PRIO], bucket))
@@ -385,7 +385,7 @@ static int bch_allocator_thread(void *arg)
 
 /* Allocation */
 
-long bch_bucket_alloc(struct cache *ca, unsigned reserve, bool wait)
+long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait)
 {
DEFINE_WAIT(w);
struct bucket *b;
@@ -421,7 +421,7 @@ long bch_bucket_alloc(struct cache *ca, unsigned reserve, 
bool wait)
if (expensive_debug_checks(ca->set)) {
size_t iter;
long i;
-   unsigned j;
+   unsigned int j;
 
for (iter = 0; iter < prio_buckets(ca) * 2; iter++)
BUG_ON(ca->prio_buckets[iter] == (uint64_t) r);
@@ -470,14 +470,14 @@ void __bch_bucket_free(struct cache *ca, struct bucket *b)
 
 void bch_bucket_free(struct cache_set *c, struct bkey *k)
 {
-   unsigned i;
+   unsigned int i;
 
for (i = 0; i < KEY_PTRS(k); i++)
__bch_bucket_free(PTR_CACHE(c, k, i),
  PTR_BUCKET(c, k, i));
 }
 
-int __bch_bucket_alloc_set(struct cache_set *c, unsigned reserve,
+int __bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
   struct bkey *k, int n, bool wait)
 {
int i;
@@ -510,7 +510,7 @@ int __bch_bucket_alloc_set(struct cache_set *c, unsigned 
reserve,
return -1;
 }
 
-int bch_bucket_alloc_set(struct cache_set *c, unsigned reserve,
+int bch_bucket_alloc_set(struct cache_set *c, unsigned int reserve,
 struct bkey *k, int n, bool wait)
 {
int ret;
@@ -524,8 +524,8 @@ int bch_bucket_alloc_set(struct cache_set *c, unsigned 
reserve,
 
 struct open_bucket {
struct list_headlist;
-   unsignedlast_write_point;
-   unsignedsectors_free;
+   unsigned intlast_write_point;
+   unsigned intsectors_free;
BKEY_PADDED(key);
 };
 
@@ -556,7 +556,7 @@ struct open_bucket {
  */
 static struct open_bucket *pick_data_bucket(struct cache_set *c,
const struct bkey *search,
-   unsigned write_point,
+   unsigned int write_point,
   

[PATCH v2 00/17] fixes reported by checkpatch.pl

2018-08-10 Thread Coly Li
This series contains several minor fixes and code cleanup reported by
scripts/checkpatch.pl. After this series, there are still a few warning
from checkpatch.pl, but I think they are necessary to be the way they
are and don't change them. For example, error message exceeds 80 chars
line limit.

Thanks in advance for any comment or review.

Changelog:
v2: fix comments from Shenghui Wang.
v1: initial version

Coly Li
---
Coly Li (17):
  bcache: style fix to replace 'unsigned' by 'unsigned int'
  bcache: style fix to add a blank line after declarations
  bcache: add identifier names to arguments of function definitions
  bcache: style fixes for lines over 80 characters
  bcache: replace Symbolic permissions by octal permission numbers
  bcache: replace printk() by pr_*() routines
  bcache: fix indent by replacing blank by tabs
  bcache: replace '%pF' by '%pS' in seq_printf()
  bcache: fix typo 'succesfully' to 'successfully'
  bcache: prefer 'help' in Kconfig
  bcache: do not check NULL pointer before calling kmem_cache_destroy
  bcache: fix code comments style
  bcache: add static const prefix to char * array declarations
  bcache: move open brace at end of function definitions to next line
  bcache: add missing SPDX header
  bcache: remove unnecessary space before ioctl function pointer
arguments
  bcache: add the missing comments for smp_mb()/smp_wmb()

 drivers/md/bcache/Kconfig |   6 +-
 drivers/md/bcache/alloc.c |  39 ---
 drivers/md/bcache/bcache.h| 210 +-
 drivers/md/bcache/bset.c  | 142 +--
 drivers/md/bcache/bset.h  | 146 ---
 drivers/md/bcache/btree.c |  72 +++-
 drivers/md/bcache/btree.h |  86 +++---
 drivers/md/bcache/closure.c   |   6 +-
 drivers/md/bcache/closure.h   |   6 +-
 drivers/md/bcache/debug.c |  23 ++--
 drivers/md/bcache/debug.h |   6 +-
 drivers/md/bcache/extents.c   |  37 +++---
 drivers/md/bcache/extents.h   |   6 +-
 drivers/md/bcache/io.c|  24 ++--
 drivers/md/bcache/journal.c   |  25 ++--
 drivers/md/bcache/journal.h   |  28 ++---
 drivers/md/bcache/movinggc.c  |  14 ++-
 drivers/md/bcache/request.c   |  61 +-
 drivers/md/bcache/request.h   |  18 +--
 drivers/md/bcache/stats.c |  15 ++-
 drivers/md/bcache/stats.h |  15 ++-
 drivers/md/bcache/super.c | 103 ++---
 drivers/md/bcache/sysfs.c |  36 +++---
 drivers/md/bcache/sysfs.h |   6 +-
 drivers/md/bcache/util.c  |   2 +
 drivers/md/bcache/util.h  |  24 ++--
 drivers/md/bcache/writeback.c |  30 +++--
 drivers/md/bcache/writeback.h |  19 +--
 include/uapi/linux/bcache.h   |   8 +-
 29 files changed, 676 insertions(+), 537 deletions(-)

-- 
2.18.0



Re: [PATCH 00/10] bcache patches for 4.19, 2nd wave

2018-08-09 Thread Coly Li
On 2018/8/9 10:21 PM, Jens Axboe wrote:
> On 8/9/18 1:48 AM, Coly Li wrote:
>> Hi Jens, 
>>
>> Here are 2nd wave bcache patches for 4.19.
>>
>> The patches from me were either verified by other people or posted
>> for quite long time. Except for the debugfs_create_dir() fix and
>> "set max writeback rate" fix, rested patches are simple or trivial
>> IMHO.
>>
>> Our new bcache developer Shenghui Wang contributes two patches in
>> this run. The first one fixes a misleading error message, and the
>> second one is a code style clean up.
>>
>> Thanks in advance for picking them.
> 
> Applied, fixed up a typo in your patch #4.
> 
Oh, thanks!

Coly Li


[PATCH 10/10] bcache: trivial - remove tailing backslash in macro BTREE_FLAG

2018-08-09 Thread Coly Li
From: Shenghui Wang 

Remove the tailing backslash in macro BTREE_FLAG in btree.h

Signed-off-by: Shenghui Wang 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/btree.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/btree.h b/drivers/md/bcache/btree.h
index d211e2c25b6b..68e9d926134d 100644
--- a/drivers/md/bcache/btree.h
+++ b/drivers/md/bcache/btree.h
@@ -152,7 +152,7 @@ static inline bool btree_node_ ## flag(struct btree *b) 
\
 {  return test_bit(BTREE_NODE_ ## flag, >flags); }  \
\
 static inline void set_btree_node_ ## flag(struct btree *b)\
-{  set_bit(BTREE_NODE_ ## flag, >flags); }  \
+{  set_bit(BTREE_NODE_ ## flag, >flags); }
 
 enum btree_flags {
BTREE_NODE_io_error,
-- 
2.18.0



[PATCH 09/10] bcache: make the pr_err statement used for ENOENT only in sysfs_attatch section

2018-08-09 Thread Coly Li
From: Shenghui Wang 

The pr_err statement in the code for sysfs_attatch section would run
for various error codes, which maybe confusing.

E.g,

Run the command twice:
   echo 796b5c05-b03c-4bc7-9cbd-a8df5e8be891 > \
/sys/block/bcache0/bcache/attach
   [the backing dev got attached on the first run]
   echo 796b5c05-b03c-4bc7-9cbd-a8df5e8be891 > \
/sys/block/bcache0/bcache/attach

In dmesg, after the command run twice, we can get:
bcache: bch_cached_dev_attach() Can't attach sda6: already attached
bcache: __cached_dev_store() Can't attach 796b5c05-b03c-4bc7-9cbd-\
a8df5e8be891
   : cache set not found
The first statement in the message was right, but the second was
confusing.

bch_cached_dev_attach has various pr_ statements for various error
codes, except ENOENT.

After the change, rerun above command twice:
echo 796b5c05-b03c-4bc7-9cbd-a8df5e8be891 > \
/sys/block/bcache0/bcache/attach
echo 796b5c05-b03c-4bc7-9cbd-a8df5e8be891 > \
/sys/block/bcache0/bcache/attach

In dmesg we only got:
bcache: bch_cached_dev_attach() Can't attach sda6: already attached
No confusing "cache set not found" message anymore.

And for some not exist SET-UUID:
echo 796b5c05-b03c-4bc7-9cbd-a8df5e8be898 > \
/sys/block/bcache0/bcache/attach
In dmesg we can get:
bcache: __cached_dev_store() Can't attach 796b5c05-b03c-4bc7-9cbd-\
a8df5e8be898
   : cache set not found

Signed-off-by: Shenghui Wang 
Signed-off-by: Coly Li 
---
 drivers/md/bcache/sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 6e88142514fb..22f8565d2bf1 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -351,8 +351,8 @@ STORE(__cached_dev)
if (!v)
return size;
}
-
-   pr_err("Can't attach %s: cache set not found", buf);
+   if (v == -ENOENT)
+   pr_err("Can't attach %s: cache set not found", buf);
return v;
}
 
-- 
2.18.0



[PATCH 08/10] bcache: set max writeback rate when I/O request is idle

2018-08-09 Thread Coly Li
Commit b1092c9af9ed ("bcache: allow quick writeback when backing idle")
allows the writeback rate to be faster if there is no I/O request on a
bcache device. It works well if there is only one bcache device attached
to the cache set. If there are many bcache devices attached to a cache
set, it may introduce performance regression because multiple faster
writeback threads of the idle bcache devices will compete the btree level
locks with the bcache device who have I/O requests coming.

This patch fixes the above issue by only permitting fast writebac when
all bcache devices attached on the cache set are idle. And if one of the
bcache devices has new I/O request coming, minimized all writeback
throughput immediately and let PI controller __update_writeback_rate()
to decide the upcoming writeback rate for each bcache device.

Also when all bcache devices are idle, limited wrieback rate to a small
number is wast of thoughput, especially when backing devices are slower
non-rotation devices (e.g. SATA SSD). This patch sets a max writeback
rate for each backing device if the whole cache set is idle. A faster
writeback rate in idle time means new I/Os may have more available space
for dirty data, and people may observe a better write performance then.

Please note bcache may change its cache mode in run time, and this patch
still works if the cache mode is switched from writeback mode and there
is still dirty data on cache.

Fixes: Commit b1092c9af9ed ("bcache: allow quick writeback when backing idle")
Cc: sta...@vger.kernel.org #4.16+
Signed-off-by: Coly Li 
Tested-by: Kai Krakow 
Tested-by: Stefan Priebe 
Cc: Michael Lyle 
---
 drivers/md/bcache/bcache.h| 10 ++--
 drivers/md/bcache/request.c   | 59 ++-
 drivers/md/bcache/super.c |  4 ++
 drivers/md/bcache/sysfs.c | 15 --
 drivers/md/bcache/util.c  |  2 +-
 drivers/md/bcache/util.h  |  2 +-
 drivers/md/bcache/writeback.c | 91 +++
 7 files changed, 138 insertions(+), 45 deletions(-)

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index b393b3fd06b6..05f82ff6f016 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -328,13 +328,6 @@ struct cached_dev {
 */
atomic_thas_dirty;
 
-   /*
-* Set to zero by things that touch the backing volume-- except
-* writeback.  Incremented by writeback.  Used to determine when to
-* accelerate idle writeback.
-*/
-   atomic_tbacking_idle;
-
struct bch_ratelimitwriteback_rate;
struct delayed_work writeback_rate_update;
 
@@ -515,6 +508,8 @@ struct cache_set {
struct cache_accounting accounting;
 
unsigned long   flags;
+   atomic_tidle_counter;
+   atomic_tat_max_writeback_rate;
 
struct cache_sb sb;
 
@@ -524,6 +519,7 @@ struct cache_set {
 
struct bcache_device**devices;
unsigneddevices_max_used;
+   atomic_tattached_dev_nr;
struct list_headcached_devs;
uint64_tcached_dev_sectors;
atomic_long_t   flash_dev_dirty_sectors;
diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 914d501ad1e0..7dbe8b6316a0 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -1103,6 +1103,44 @@ static void detached_dev_do_request(struct bcache_device 
*d, struct bio *bio)
generic_make_request(bio);
 }
 
+static void quit_max_writeback_rate(struct cache_set *c,
+   struct cached_dev *this_dc)
+{
+   int i;
+   struct bcache_device *d;
+   struct cached_dev *dc;
+
+   /*
+* mutex bch_register_lock may compete with other parallel requesters,
+* or attach/detach operations on other backing device. Waiting to
+* the mutex lock may increase I/O request latency for seconds or more.
+* To avoid such situation, if mutext_trylock() failed, only writeback
+* rate of current cached device is set to 1, and __update_write_back()
+* will decide writeback rate of other cached devices (remember now
+* c->idle_counter is 0 already).
+*/
+   if (mutex_trylock(_register_lock)) {
+   for (i = 0; i < c->devices_max_used; i++) {
+   if (!c->devices[i])
+   continue;
+
+   if (UUID_FLASH_ONLY(>uuids[i]))
+   continue;
+
+   d = c->devices[i];
+   dc = container_of(d, struct cached_dev, disk);
+   /*
+* set writeback rate to default minimum value,
+* then let update_writeback_rate() to decide the
+ 

[PATCH 07/10] bcache: add code comments for bset.c

2018-08-09 Thread Coly Li
This patch tries to add code comments in bset.c, to make some
tricky code and designment to be more comprehensible. Most information
of this patch comes from the discussion between Kent and I, he
offers very informative details. If there is any mistake
of the idea behind the code, no doubt that's from me misrepresentation.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/bset.c | 63 
 1 file changed, 63 insertions(+)

diff --git a/drivers/md/bcache/bset.c b/drivers/md/bcache/bset.c
index f3403b45bc28..596c93b44e9b 100644
--- a/drivers/md/bcache/bset.c
+++ b/drivers/md/bcache/bset.c
@@ -366,6 +366,10 @@ EXPORT_SYMBOL(bch_btree_keys_init);
 
 /* Binary tree stuff for auxiliary search trees */
 
+/*
+ * return array index next to j when does in-order traverse
+ * of a binary tree which is stored in a linear array
+ */
 static unsigned inorder_next(unsigned j, unsigned size)
 {
if (j * 2 + 1 < size) {
@@ -379,6 +383,10 @@ static unsigned inorder_next(unsigned j, unsigned size)
return j;
 }
 
+/*
+ * return array index previous to j when does in-order traverse
+ * of a binary tree which is stored in a linear array
+ */
 static unsigned inorder_prev(unsigned j, unsigned size)
 {
if (j * 2 < size) {
@@ -421,6 +429,10 @@ static unsigned __to_inorder(unsigned j, unsigned size, 
unsigned extra)
return j;
 }
 
+/*
+ * Return the cacheline index in bset_tree->data, where j is index
+ * from a linear array which stores the auxiliar binary tree
+ */
 static unsigned to_inorder(unsigned j, struct bset_tree *t)
 {
return __to_inorder(j, t->size, t->extra);
@@ -441,6 +453,10 @@ static unsigned __inorder_to_tree(unsigned j, unsigned 
size, unsigned extra)
return j;
 }
 
+/*
+ * Return an index from a linear array which stores the auxiliar binary
+ * tree, j is the cacheline index of t->data.
+ */
 static unsigned inorder_to_tree(unsigned j, struct bset_tree *t)
 {
return __inorder_to_tree(j, t->size, t->extra);
@@ -546,6 +562,20 @@ static inline uint64_t shrd128(uint64_t high, uint64_t 
low, uint8_t shift)
return low;
 }
 
+/*
+ * Calculate mantissa value for struct bkey_float.
+ * If most significant bit of f->exponent is not set, then
+ *  - f->exponent >> 6 is 0
+ *  - p[0] points to bkey->low
+ *  - p[-1] borrows bits from KEY_INODE() of bkey->high
+ * if most isgnificant bits of f->exponent is set, then
+ *  - f->exponent >> 6 is 1
+ *  - p[0] points to bits from KEY_INODE() of bkey->high
+ *  - p[-1] points to other bits from KEY_INODE() of
+ *bkey->high too.
+ * See make_bfloat() to check when most significant bit of f->exponent
+ * is set or not.
+ */
 static inline unsigned bfloat_mantissa(const struct bkey *k,
   struct bkey_float *f)
 {
@@ -570,6 +600,16 @@ static void make_bfloat(struct bset_tree *t, unsigned j)
BUG_ON(m < l || m > r);
BUG_ON(bkey_next(p) != m);
 
+   /*
+* If l and r have different KEY_INODE values (different backing
+* device), f->exponent records how many least significant bits
+* are different in KEY_INODE values and sets most significant
+* bits to 1 (by +64).
+* If l and r have same KEY_INODE value, f->exponent records
+* how many different bits in least significant bits of bkey->low.
+* See bfloat_mantiss() how the most significant bit of
+* f->exponent is used to calculate bfloat mantissa value.
+*/
if (KEY_INODE(l) != KEY_INODE(r))
f->exponent = fls64(KEY_INODE(r) ^ KEY_INODE(l)) + 64;
else
@@ -633,6 +673,15 @@ void bch_bset_init_next(struct btree_keys *b, struct bset 
*i, uint64_t magic)
 }
 EXPORT_SYMBOL(bch_bset_init_next);
 
+/*
+ * Build auxiliary binary tree 'struct bset_tree *t', this tree is used to
+ * accelerate bkey search in a btree node (pointed by bset_tree->data in
+ * memory). After search in the auxiliar tree by calling bset_search_tree(),
+ * a struct bset_search_iter is returned which indicates range [l, r] from
+ * bset_tree->data where the searching bkey might be inside. Then a followed
+ * linear comparison does the exact search, see __bch_bset_search() for how
+ * the auxiliary tree is used.
+ */
 void bch_bset_build_written_tree(struct btree_keys *b)
 {
struct bset_tree *t = bset_tree_last(b);
@@ -898,6 +947,17 @@ static struct bset_search_iter bset_search_tree(struct 
bset_tree *t,
unsigned inorder, j, n = 1;
 
do {
+   /*
+* A bit trick here.
+* If p < t->size, (int)(p - t->size) is a minus value and
+* the most significant bit is set, right shifting 31 bits
+* gets 1. If p >= t->size, the most significant bit is
+* not set, right shifting 31 bits gets 0.
+* So th

[PATCH 06/10] bcache: fix mistaken comments in request.c

2018-08-09 Thread Coly Li
This patch updates code comment in bch_keylist_realloc() by fixing
incorrected function names, to make the code to be more comprehennsible.

Signed-off-by: Coly Li 
---
 drivers/md/bcache/request.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/md/bcache/request.c b/drivers/md/bcache/request.c
index 43af905920f5..914d501ad1e0 100644
--- a/drivers/md/bcache/request.c
+++ b/drivers/md/bcache/request.c
@@ -107,7 +107,7 @@ static int bch_keylist_realloc(struct keylist *l, unsigned 
u64s,
/*
 * The journalling code doesn't handle the case where the keys to insert
 * is bigger than an empty write: If we just return -ENOMEM here,
-* bio_insert() and bio_invalidate() will insert the keys created so far
+* bch_data_insert_keys() will insert the keys created so far
 * and finish the rest when the keylist is empty.
 */
if (newsize * sizeof(uint64_t) > block_bytes(c) - sizeof(struct jset))
-- 
2.18.0



  1   2   3   4   5   6   >