@@ -621,7 +621,7 @@ static void prio_read(struct cache *ca, uint64_t bucket)
621621static 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
651651void 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
657657static 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
669673static 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 );
0 commit comments