Skip to content

Commit c4d951d

Browse files
author
Kent Overstreet
committed
bcache: Fix sysfs splat on shutdown with flash only devs
Whoops. Signed-off-by: Kent Overstreet <kmo@daterainc.com>
1 parent 48a915a commit c4d951d

6 files changed

Lines changed: 30 additions & 33 deletions

File tree

‎drivers/md/bcache/bcache.h‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,10 @@ struct bcache_device {
266266

267267
struct gendisk *disk;
268268

269-
/* If nonzero, we're closing */
270-
atomic_t closing;
271-
272-
/* If nonzero, we're detaching/unregistering from cache set */
273-
atomic_t detaching;
274-
int flush_done;
269+
unsigned long flags;
270+
#define BCACHE_DEV_CLOSING 0
271+
#define BCACHE_DEV_DETACHING 1
272+
#define BCACHE_DEV_UNLINK_DONE 2
275273

276274
unsigned nr_stripes;
277275
unsigned stripe_size;

‎drivers/md/bcache/request.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
512512
struct task_struct *task = current;
513513
struct io *i;
514514

515-
if (atomic_read(&dc->disk.detaching) ||
515+
if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
516516
c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
517517
(bio->bi_rw & REQ_DISCARD))
518518
goto skip;

‎drivers/md/bcache/super.c‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ static void prio_read(struct cache *ca, uint64_t bucket)
621621
static int open_dev(struct block_device *b, fmode_t mode)
622622
{
623623
struct bcache_device *d = b->bd_disk->private_data;
624-
if (atomic_read(&d->closing))
624+
if (test_bit(BCACHE_DEV_CLOSING, &d->flags))
625625
return -ENXIO;
626626

627627
closure_get(&d->cl);
@@ -650,20 +650,24 @@ static const struct block_device_operations bcache_ops = {
650650

651651
void bcache_device_stop(struct bcache_device *d)
652652
{
653-
if (!atomic_xchg(&d->closing, 1))
653+
if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags))
654654
closure_queue(&d->cl);
655655
}
656656

657657
static void bcache_device_unlink(struct bcache_device *d)
658658
{
659-
unsigned i;
660-
struct cache *ca;
659+
lockdep_assert_held(&bch_register_lock);
661660

662-
sysfs_remove_link(&d->c->kobj, d->name);
663-
sysfs_remove_link(&d->kobj, "cache");
661+
if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) {
662+
unsigned i;
663+
struct cache *ca;
664664

665-
for_each_cache(ca, d->c, i)
666-
bd_unlink_disk_holder(ca->bdev, d->disk);
665+
sysfs_remove_link(&d->c->kobj, d->name);
666+
sysfs_remove_link(&d->kobj, "cache");
667+
668+
for_each_cache(ca, d->c, i)
669+
bd_unlink_disk_holder(ca->bdev, d->disk);
670+
}
667671
}
668672

669673
static void bcache_device_link(struct bcache_device *d, struct cache_set *c,
@@ -687,19 +691,16 @@ static void bcache_device_detach(struct bcache_device *d)
687691
{
688692
lockdep_assert_held(&bch_register_lock);
689693

690-
if (atomic_read(&d->detaching)) {
694+
if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) {
691695
struct uuid_entry *u = d->c->uuids + d->id;
692696

693697
SET_UUID_FLASH_ONLY(u, 0);
694698
memcpy(u->uuid, invalid_uuid, 16);
695699
u->invalidated = cpu_to_le32(get_seconds());
696700
bch_uuid_write(d->c);
697-
698-
atomic_set(&d->detaching, 0);
699701
}
700702

701-
if (!d->flush_done)
702-
bcache_device_unlink(d);
703+
bcache_device_unlink(d);
703704

704705
d->c->devices[d->id] = NULL;
705706
closure_put(&d->c->caching);
@@ -879,7 +880,7 @@ static void cached_dev_detach_finish(struct work_struct *w)
879880
struct closure cl;
880881
closure_init_stack(&cl);
881882

882-
BUG_ON(!atomic_read(&dc->disk.detaching));
883+
BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags));
883884
BUG_ON(atomic_read(&dc->count));
884885

885886
mutex_lock(&bch_register_lock);
@@ -893,6 +894,8 @@ static void cached_dev_detach_finish(struct work_struct *w)
893894
bcache_device_detach(&dc->disk);
894895
list_move(&dc->list, &uncached_devices);
895896

897+
clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags);
898+
896899
mutex_unlock(&bch_register_lock);
897900

898901
pr_info("Caching disabled for %s", bdevname(dc->bdev, buf));
@@ -905,10 +908,10 @@ void bch_cached_dev_detach(struct cached_dev *dc)
905908
{
906909
lockdep_assert_held(&bch_register_lock);
907910

908-
if (atomic_read(&dc->disk.closing))
911+
if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags))
909912
return;
910913

911-
if (atomic_xchg(&dc->disk.detaching, 1))
914+
if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
912915
return;
913916

914917
/*
@@ -1064,11 +1067,7 @@ static void cached_dev_flush(struct closure *cl)
10641067
struct bcache_device *d = &dc->disk;
10651068

10661069
mutex_lock(&bch_register_lock);
1067-
d->flush_done = 1;
1068-
1069-
if (d->c)
1070-
bcache_device_unlink(d);
1071-
1070+
bcache_device_unlink(d);
10721071
mutex_unlock(&bch_register_lock);
10731072

10741073
bch_cache_accounting_destroy(&dc->accounting);

‎drivers/md/bcache/sysfs.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ STORE(__bch_flash_dev)
370370
}
371371

372372
if (attr == &sysfs_unregister) {
373-
atomic_set(&d->detaching, 1);
373+
set_bit(BCACHE_DEV_DETACHING, &d->flags);
374374
bcache_device_stop(d);
375375
}
376376

‎drivers/md/bcache/writeback.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static unsigned writeback_delay(struct cached_dev *dc, unsigned sectors)
8989
{
9090
uint64_t ret;
9191

92-
if (atomic_read(&dc->disk.detaching) ||
92+
if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
9393
!dc->writeback_percent)
9494
return 0;
9595

@@ -404,7 +404,7 @@ static int bch_writeback_thread(void *arg)
404404
while (!kthread_should_stop()) {
405405
down_write(&dc->writeback_lock);
406406
if (!atomic_read(&dc->has_dirty) ||
407-
(!atomic_read(&dc->disk.detaching) &&
407+
(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) &&
408408
!dc->writeback_running)) {
409409
up_write(&dc->writeback_lock);
410410
set_current_state(TASK_INTERRUPTIBLE);
@@ -437,7 +437,7 @@ static int bch_writeback_thread(void *arg)
437437

438438
while (delay &&
439439
!kthread_should_stop() &&
440-
!atomic_read(&dc->disk.detaching))
440+
!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags))
441441
delay = schedule_timeout_interruptible(delay);
442442
}
443443
}

‎drivers/md/bcache/writeback.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static inline bool should_writeback(struct cached_dev *dc, struct bio *bio,
4545
unsigned in_use = dc->disk.c->gc_stats.in_use;
4646

4747
if (cache_mode != CACHE_MODE_WRITEBACK ||
48-
atomic_read(&dc->disk.detaching) ||
48+
test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) ||
4949
in_use > CUTOFF_WRITEBACK_SYNC)
5050
return false;
5151

0 commit comments

Comments
 (0)