1010#include "btree.h"
1111#include "debug.h"
1212#include "request.h"
13+ #include "writeback.h"
1314
1415#include <linux/blkdev.h>
1516#include <linux/buffer_head.h>
@@ -744,13 +745,35 @@ static void bcache_device_free(struct bcache_device *d)
744745 mempool_destroy (d -> unaligned_bvec );
745746 if (d -> bio_split )
746747 bioset_free (d -> bio_split );
748+ if (is_vmalloc_addr (d -> stripe_sectors_dirty ))
749+ vfree (d -> stripe_sectors_dirty );
750+ else
751+ kfree (d -> stripe_sectors_dirty );
747752
748753 closure_debug_destroy (& d -> cl );
749754}
750755
751- static int bcache_device_init (struct bcache_device * d , unsigned block_size )
756+ static int bcache_device_init (struct bcache_device * d , unsigned block_size ,
757+ sector_t sectors )
752758{
753759 struct request_queue * q ;
760+ size_t n ;
761+
762+ if (!d -> stripe_size_bits )
763+ d -> stripe_size_bits = 31 ;
764+
765+ d -> nr_stripes = round_up (sectors , 1 << d -> stripe_size_bits ) >>
766+ d -> stripe_size_bits ;
767+
768+ if (!d -> nr_stripes || d -> nr_stripes > SIZE_MAX / sizeof (atomic_t ))
769+ return - ENOMEM ;
770+
771+ n = d -> nr_stripes * sizeof (atomic_t );
772+ d -> stripe_sectors_dirty = n < PAGE_SIZE << 6
773+ ? kzalloc (n , GFP_KERNEL )
774+ : vzalloc (n );
775+ if (!d -> stripe_sectors_dirty )
776+ return - ENOMEM ;
754777
755778 if (!(d -> bio_split = bioset_create (4 , offsetof(struct bbio , bio ))) ||
756779 !(d -> unaligned_bvec = mempool_create_kmalloc_pool (1 ,
@@ -760,6 +783,7 @@ static int bcache_device_init(struct bcache_device *d, unsigned block_size)
760783 !(q = blk_alloc_queue (GFP_KERNEL )))
761784 return - ENOMEM ;
762785
786+ set_capacity (d -> disk , sectors );
763787 snprintf (d -> disk -> disk_name , DISK_NAME_LEN , "bcache%i" , bcache_minor );
764788
765789 d -> disk -> major = bcache_major ;
@@ -1047,7 +1071,8 @@ static int cached_dev_init(struct cached_dev *dc, unsigned block_size)
10471071 hlist_add_head (& io -> hash , dc -> io_hash + RECENT_IO );
10481072 }
10491073
1050- ret = bcache_device_init (& dc -> disk , block_size );
1074+ ret = bcache_device_init (& dc -> disk , block_size ,
1075+ dc -> bdev -> bd_part -> nr_sects - dc -> sb .data_offset );
10511076 if (ret )
10521077 return ret ;
10531078
@@ -1146,11 +1171,10 @@ static int flash_dev_run(struct cache_set *c, struct uuid_entry *u)
11461171
11471172 kobject_init (& d -> kobj , & bch_flash_dev_ktype );
11481173
1149- if (bcache_device_init (d , block_bytes (c )))
1174+ if (bcache_device_init (d , block_bytes (c ), u -> sectors ))
11501175 goto err ;
11511176
11521177 bcache_device_attach (d , c , u - c -> uuids );
1153- set_capacity (d -> disk , u -> sectors );
11541178 bch_flash_dev_request_init (d );
11551179 add_disk (d -> disk );
11561180
0 commit comments