187418ef9SColy Li // SPDX-License-Identifier: GPL-2.0 2cafe5635SKent Overstreet /* 3cafe5635SKent Overstreet * bcache setup/teardown code, and some metadata io - read a superblock and 4cafe5635SKent Overstreet * figure out what to do with it. 5cafe5635SKent Overstreet * 6cafe5635SKent Overstreet * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 7cafe5635SKent Overstreet * Copyright 2012 Google, Inc. 8cafe5635SKent Overstreet */ 9cafe5635SKent Overstreet 10cafe5635SKent Overstreet #include "bcache.h" 11cafe5635SKent Overstreet #include "btree.h" 12cafe5635SKent Overstreet #include "debug.h" 1365d45231SKent Overstreet #include "extents.h" 14cafe5635SKent Overstreet #include "request.h" 15279afbadSKent Overstreet #include "writeback.h" 16d721a43fSColy Li #include "features.h" 17cafe5635SKent Overstreet 18c37511b8SKent Overstreet #include <linux/blkdev.h> 194ee60ec1SMatthew Wilcox (Oracle) #include <linux/pagemap.h> 20cafe5635SKent Overstreet #include <linux/debugfs.h> 21cafe5635SKent Overstreet #include <linux/genhd.h> 2228935ab5SKent Overstreet #include <linux/idr.h> 2379826c35SKent Overstreet #include <linux/kthread.h> 24ee4a36f4SColy Li #include <linux/workqueue.h> 25cafe5635SKent Overstreet #include <linux/module.h> 26cafe5635SKent Overstreet #include <linux/random.h> 27cafe5635SKent Overstreet #include <linux/reboot.h> 28cafe5635SKent Overstreet #include <linux/sysfs.h> 29cafe5635SKent Overstreet 309aaf5165SColy Li unsigned int bch_cutoff_writeback; 319aaf5165SColy Li unsigned int bch_cutoff_writeback_sync; 329aaf5165SColy Li 33cafe5635SKent Overstreet static const char bcache_magic[] = { 34cafe5635SKent Overstreet 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, 35cafe5635SKent Overstreet 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 36cafe5635SKent Overstreet }; 37cafe5635SKent Overstreet 38cafe5635SKent Overstreet static const char invalid_uuid[] = { 39cafe5635SKent Overstreet 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78, 40cafe5635SKent Overstreet 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99 41cafe5635SKent Overstreet }; 42cafe5635SKent Overstreet 43cafe5635SKent Overstreet static struct kobject *bcache_kobj; 44cafe5635SKent Overstreet struct mutex bch_register_lock; 45a59ff6ccSColy Li bool bcache_is_reboot; 46cafe5635SKent Overstreet LIST_HEAD(bch_cache_sets); 47cafe5635SKent Overstreet static LIST_HEAD(uncached_devices); 48cafe5635SKent Overstreet 4928935ab5SKent Overstreet static int bcache_major; 501dbe32adSColy Li static DEFINE_IDA(bcache_device_idx); 51cafe5635SKent Overstreet static wait_queue_head_t unregister_wait; 52cafe5635SKent Overstreet struct workqueue_struct *bcache_wq; 53afe78ab4SKai Krakow struct workqueue_struct *bch_flush_wq; 540f843e65SGuoju Fang struct workqueue_struct *bch_journal_wq; 55cafe5635SKent Overstreet 56a59ff6ccSColy Li 57cafe5635SKent Overstreet #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE) 581dbe32adSColy Li /* limitation of partitions number on single bcache device */ 591dbe32adSColy Li #define BCACHE_MINORS 128 601dbe32adSColy Li /* limitation of bcache devices number on single system */ 611dbe32adSColy Li #define BCACHE_DEVICE_IDX_MAX ((1U << MINORBITS)/BCACHE_MINORS) 62cafe5635SKent Overstreet 63cafe5635SKent Overstreet /* Superblock */ 64cafe5635SKent Overstreet 65ffa47032SColy Li static unsigned int get_bucket_size(struct cache_sb *sb, struct cache_sb_disk *s) 66ffa47032SColy Li { 67ffa47032SColy Li unsigned int bucket_size = le16_to_cpu(s->bucket_size); 68ffa47032SColy Li 69b16671e8SColy Li if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) { 70b16671e8SColy Li if (bch_has_feature_large_bucket(sb)) { 71b16671e8SColy Li unsigned int max, order; 72b16671e8SColy Li 73b16671e8SColy Li max = sizeof(unsigned int) * BITS_PER_BYTE - 1; 74b16671e8SColy Li order = le16_to_cpu(s->bucket_size); 75b16671e8SColy Li /* 76b16671e8SColy Li * bcache tool will make sure the overflow won't 77b16671e8SColy Li * happen, an error message here is enough. 78b16671e8SColy Li */ 79b16671e8SColy Li if (order > max) 80b16671e8SColy Li pr_err("Bucket size (1 << %u) overflows\n", 81b16671e8SColy Li order); 82b16671e8SColy Li bucket_size = 1 << order; 83b16671e8SColy Li } else if (bch_has_feature_obso_large_bucket(sb)) { 84b16671e8SColy Li bucket_size += 85b16671e8SColy Li le16_to_cpu(s->obso_bucket_size_hi) << 16; 86b16671e8SColy Li } 87b16671e8SColy Li } 88ffa47032SColy Li 89ffa47032SColy Li return bucket_size; 90ffa47032SColy Li } 91ffa47032SColy Li 925b21403cSColy Li static const char *read_super_common(struct cache_sb *sb, struct block_device *bdev, 935b21403cSColy Li struct cache_sb_disk *s) 94cafe5635SKent Overstreet { 95cafe5635SKent Overstreet const char *err; 966f10f7d1SColy Li unsigned int i; 97cafe5635SKent Overstreet 98cafe5635SKent Overstreet sb->first_bucket= le16_to_cpu(s->first_bucket); 995b21403cSColy Li sb->nbuckets = le64_to_cpu(s->nbuckets); 100ffa47032SColy Li sb->bucket_size = get_bucket_size(sb, s); 101cafe5635SKent Overstreet 1025b21403cSColy Li sb->nr_in_set = le16_to_cpu(s->nr_in_set); 1035b21403cSColy Li sb->nr_this_dev = le16_to_cpu(s->nr_this_dev); 104cafe5635SKent Overstreet 105cafe5635SKent Overstreet err = "Too many journal buckets"; 106cafe5635SKent Overstreet if (sb->keys > SB_JOURNAL_BUCKETS) 107cafe5635SKent Overstreet goto err; 108cafe5635SKent Overstreet 109cafe5635SKent Overstreet err = "Too many buckets"; 110cafe5635SKent Overstreet if (sb->nbuckets > LONG_MAX) 111cafe5635SKent Overstreet goto err; 112cafe5635SKent Overstreet 113cafe5635SKent Overstreet err = "Not enough buckets"; 114cafe5635SKent Overstreet if (sb->nbuckets < 1 << 7) 115cafe5635SKent Overstreet goto err; 116cafe5635SKent Overstreet 117c557a5f7SColy Li err = "Bad block size (not power of 2)"; 118c557a5f7SColy Li if (!is_power_of_2(sb->block_size)) 119c557a5f7SColy Li goto err; 120c557a5f7SColy Li 121c557a5f7SColy Li err = "Bad block size (larger than page size)"; 122c557a5f7SColy Li if (sb->block_size > PAGE_SECTORS) 123c557a5f7SColy Li goto err; 124c557a5f7SColy Li 125c557a5f7SColy Li err = "Bad bucket size (not power of 2)"; 126c557a5f7SColy Li if (!is_power_of_2(sb->bucket_size)) 127c557a5f7SColy Li goto err; 128c557a5f7SColy Li 129c557a5f7SColy Li err = "Bad bucket size (smaller than page size)"; 130c557a5f7SColy Li if (sb->bucket_size < PAGE_SECTORS) 1312903381fSKent Overstreet goto err; 1322903381fSKent Overstreet 133cafe5635SKent Overstreet err = "Invalid superblock: device too small"; 134b0d30981SColy Li if (get_capacity(bdev->bd_disk) < 135b0d30981SColy Li sb->bucket_size * sb->nbuckets) 136cafe5635SKent Overstreet goto err; 137cafe5635SKent Overstreet 138cafe5635SKent Overstreet err = "Bad UUID"; 139169ef1cfSKent Overstreet if (bch_is_zero(sb->set_uuid, 16)) 140cafe5635SKent Overstreet goto err; 141cafe5635SKent Overstreet 142cafe5635SKent Overstreet err = "Bad cache device number in set"; 143cafe5635SKent Overstreet if (!sb->nr_in_set || 144cafe5635SKent Overstreet sb->nr_in_set <= sb->nr_this_dev || 145cafe5635SKent Overstreet sb->nr_in_set > MAX_CACHES_PER_SET) 146cafe5635SKent Overstreet goto err; 147cafe5635SKent Overstreet 148cafe5635SKent Overstreet err = "Journal buckets not sequential"; 149cafe5635SKent Overstreet for (i = 0; i < sb->keys; i++) 150cafe5635SKent Overstreet if (sb->d[i] != sb->first_bucket + i) 151cafe5635SKent Overstreet goto err; 152cafe5635SKent Overstreet 153cafe5635SKent Overstreet err = "Too many journal buckets"; 154cafe5635SKent Overstreet if (sb->first_bucket + sb->keys > sb->nbuckets) 155cafe5635SKent Overstreet goto err; 156cafe5635SKent Overstreet 157cafe5635SKent Overstreet err = "Invalid superblock: first bucket comes before end of super"; 158cafe5635SKent Overstreet if (sb->first_bucket * sb->bucket_size < 16) 159cafe5635SKent Overstreet goto err; 1602903381fSKent Overstreet 1615b21403cSColy Li err = NULL; 1625b21403cSColy Li err: 1635b21403cSColy Li return err; 1645b21403cSColy Li } 1655b21403cSColy Li 1665b21403cSColy Li 167cafe5635SKent Overstreet static const char *read_super(struct cache_sb *sb, struct block_device *bdev, 168cafe5635SKent Overstreet struct cache_sb_disk **res) 169cafe5635SKent Overstreet { 170cafe5635SKent Overstreet const char *err; 171cafe5635SKent Overstreet struct cache_sb_disk *s; 172cafe5635SKent Overstreet struct page *page; 173cafe5635SKent Overstreet unsigned int i; 174cafe5635SKent Overstreet 175cafe5635SKent Overstreet page = read_cache_page_gfp(bdev->bd_inode->i_mapping, 176cafe5635SKent Overstreet SB_OFFSET >> PAGE_SHIFT, GFP_KERNEL); 177cafe5635SKent Overstreet if (IS_ERR(page)) 178cafe5635SKent Overstreet return "IO error"; 179cafe5635SKent Overstreet s = page_address(page) + offset_in_page(SB_OFFSET); 180cafe5635SKent Overstreet 181cafe5635SKent Overstreet sb->offset = le64_to_cpu(s->offset); 182cafe5635SKent Overstreet sb->version = le64_to_cpu(s->version); 183cafe5635SKent Overstreet 184cafe5635SKent Overstreet memcpy(sb->magic, s->magic, 16); 185cafe5635SKent Overstreet memcpy(sb->uuid, s->uuid, 16); 186cafe5635SKent Overstreet memcpy(sb->set_uuid, s->set_uuid, 16); 187cafe5635SKent Overstreet memcpy(sb->label, s->label, SB_LABEL_SIZE); 188cafe5635SKent Overstreet 189cafe5635SKent Overstreet sb->flags = le64_to_cpu(s->flags); 190cafe5635SKent Overstreet sb->seq = le64_to_cpu(s->seq); 191cafe5635SKent Overstreet sb->last_mount = le32_to_cpu(s->last_mount); 192cafe5635SKent Overstreet sb->keys = le16_to_cpu(s->keys); 193cafe5635SKent Overstreet 194cafe5635SKent Overstreet for (i = 0; i < SB_JOURNAL_BUCKETS; i++) 195cafe5635SKent Overstreet sb->d[i] = le64_to_cpu(s->d[i]); 196cafe5635SKent Overstreet 197cafe5635SKent Overstreet pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u\n", 198cafe5635SKent Overstreet sb->version, sb->flags, sb->seq, sb->keys); 199cafe5635SKent Overstreet 200cafe5635SKent Overstreet err = "Not a bcache superblock (bad offset)"; 201cafe5635SKent Overstreet if (sb->offset != SB_SECTOR) 202cafe5635SKent Overstreet goto err; 203cafe5635SKent Overstreet 204cafe5635SKent Overstreet err = "Not a bcache superblock (bad magic)"; 205cafe5635SKent Overstreet if (memcmp(sb->magic, bcache_magic, 16)) 206cafe5635SKent Overstreet goto err; 207cafe5635SKent Overstreet 208cafe5635SKent Overstreet err = "Bad checksum"; 209cafe5635SKent Overstreet if (s->csum != csum_set(s)) 210cafe5635SKent Overstreet goto err; 211cafe5635SKent Overstreet 212cafe5635SKent Overstreet err = "Bad UUID"; 213cafe5635SKent Overstreet if (bch_is_zero(sb->uuid, 16)) 214cafe5635SKent Overstreet goto err; 215cafe5635SKent Overstreet 216cafe5635SKent Overstreet sb->block_size = le16_to_cpu(s->block_size); 217cafe5635SKent Overstreet 218cafe5635SKent Overstreet err = "Superblock block size smaller than device block size"; 219cafe5635SKent Overstreet if (sb->block_size << 9 < bdev_logical_block_size(bdev)) 220cafe5635SKent Overstreet goto err; 221cafe5635SKent Overstreet 222cafe5635SKent Overstreet switch (sb->version) { 223cafe5635SKent Overstreet case BCACHE_SB_VERSION_BDEV: 224cafe5635SKent Overstreet sb->data_offset = BDEV_DATA_START_DEFAULT; 225cafe5635SKent Overstreet break; 226cafe5635SKent Overstreet case BCACHE_SB_VERSION_BDEV_WITH_OFFSET: 227d721a43fSColy Li case BCACHE_SB_VERSION_BDEV_WITH_FEATURES: 228cafe5635SKent Overstreet sb->data_offset = le64_to_cpu(s->data_offset); 229cafe5635SKent Overstreet 230cafe5635SKent Overstreet err = "Bad data offset"; 231cafe5635SKent Overstreet if (sb->data_offset < BDEV_DATA_START_DEFAULT) 232cafe5635SKent Overstreet goto err; 233cafe5635SKent Overstreet 234cafe5635SKent Overstreet break; 235cafe5635SKent Overstreet case BCACHE_SB_VERSION_CDEV: 236cafe5635SKent Overstreet case BCACHE_SB_VERSION_CDEV_WITH_UUID: 2375b21403cSColy Li err = read_super_common(sb, bdev, s); 2385b21403cSColy Li if (err) 239cafe5635SKent Overstreet goto err; 2402903381fSKent Overstreet break; 241d721a43fSColy Li case BCACHE_SB_VERSION_CDEV_WITH_FEATURES: 242ffa47032SColy Li /* 243ffa47032SColy Li * Feature bits are needed in read_super_common(), 244ffa47032SColy Li * convert them firstly. 245ffa47032SColy Li */ 246d721a43fSColy Li sb->feature_compat = le64_to_cpu(s->feature_compat); 247d721a43fSColy Li sb->feature_incompat = le64_to_cpu(s->feature_incompat); 248d721a43fSColy Li sb->feature_ro_compat = le64_to_cpu(s->feature_ro_compat); 2491dfc0686SColy Li 2501dfc0686SColy Li /* Check incompatible features */ 2511dfc0686SColy Li err = "Unsupported compatible feature found"; 2521dfc0686SColy Li if (bch_has_unknown_compat_features(sb)) 2531dfc0686SColy Li goto err; 2541dfc0686SColy Li 2551dfc0686SColy Li err = "Unsupported read-only compatible feature found"; 2561dfc0686SColy Li if (bch_has_unknown_ro_compat_features(sb)) 2571dfc0686SColy Li goto err; 2581dfc0686SColy Li 2591dfc0686SColy Li err = "Unsupported incompatible feature found"; 2601dfc0686SColy Li if (bch_has_unknown_incompat_features(sb)) 2611dfc0686SColy Li goto err; 2621dfc0686SColy Li 263ffa47032SColy Li err = read_super_common(sb, bdev, s); 264ffa47032SColy Li if (err) 265ffa47032SColy Li goto err; 2662903381fSKent Overstreet break; 2672903381fSKent Overstreet default: 2682903381fSKent Overstreet err = "Unsupported superblock version"; 2692903381fSKent Overstreet goto err; 2702903381fSKent Overstreet } 2712903381fSKent Overstreet 27275cbb3f1SArnd Bergmann sb->last_mount = (u32)ktime_get_real_seconds(); 273cfa0c56dSChristoph Hellwig *res = s; 2746321bef0SChristoph Hellwig return NULL; 275cafe5635SKent Overstreet err: 2766321bef0SChristoph Hellwig put_page(page); 277cafe5635SKent Overstreet return err; 278cafe5635SKent Overstreet } 279cafe5635SKent Overstreet 2804246a0b6SChristoph Hellwig static void write_bdev_super_endio(struct bio *bio) 281cafe5635SKent Overstreet { 282cafe5635SKent Overstreet struct cached_dev *dc = bio->bi_private; 28308ec1e62SColy Li 28408ec1e62SColy Li if (bio->bi_status) 28508ec1e62SColy Li bch_count_backing_io_errors(dc, bio); 286cafe5635SKent Overstreet 287cb7a583eSKent Overstreet closure_put(&dc->sb_write); 288cafe5635SKent Overstreet } 289cafe5635SKent Overstreet 290475389aeSChristoph Hellwig static void __write_super(struct cache_sb *sb, struct cache_sb_disk *out, 291475389aeSChristoph Hellwig struct bio *bio) 292cafe5635SKent Overstreet { 2936f10f7d1SColy Li unsigned int i; 294cafe5635SKent Overstreet 295475389aeSChristoph Hellwig bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META; 2964f024f37SKent Overstreet bio->bi_iter.bi_sector = SB_SECTOR; 297475389aeSChristoph Hellwig __bio_add_page(bio, virt_to_page(out), SB_SIZE, 298475389aeSChristoph Hellwig offset_in_page(out)); 299cafe5635SKent Overstreet 300cafe5635SKent Overstreet out->offset = cpu_to_le64(sb->offset); 301cafe5635SKent Overstreet 302cafe5635SKent Overstreet memcpy(out->uuid, sb->uuid, 16); 303cafe5635SKent Overstreet memcpy(out->set_uuid, sb->set_uuid, 16); 304cafe5635SKent Overstreet memcpy(out->label, sb->label, SB_LABEL_SIZE); 305cafe5635SKent Overstreet 306cafe5635SKent Overstreet out->flags = cpu_to_le64(sb->flags); 307cafe5635SKent Overstreet out->seq = cpu_to_le64(sb->seq); 308cafe5635SKent Overstreet 309cafe5635SKent Overstreet out->last_mount = cpu_to_le32(sb->last_mount); 310cafe5635SKent Overstreet out->first_bucket = cpu_to_le16(sb->first_bucket); 311cafe5635SKent Overstreet out->keys = cpu_to_le16(sb->keys); 312cafe5635SKent Overstreet 313cafe5635SKent Overstreet for (i = 0; i < sb->keys; i++) 314cafe5635SKent Overstreet out->d[i] = cpu_to_le64(sb->d[i]); 315cafe5635SKent Overstreet 316d721a43fSColy Li if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) { 317d721a43fSColy Li out->feature_compat = cpu_to_le64(sb->feature_compat); 318d721a43fSColy Li out->feature_incompat = cpu_to_le64(sb->feature_incompat); 319d721a43fSColy Li out->feature_ro_compat = cpu_to_le64(sb->feature_ro_compat); 320d721a43fSColy Li } 321d721a43fSColy Li 322d721a43fSColy Li out->version = cpu_to_le64(sb->version); 323cafe5635SKent Overstreet out->csum = csum_set(out); 324cafe5635SKent Overstreet 32546f5aa88SJoe Perches pr_debug("ver %llu, flags %llu, seq %llu\n", 326cafe5635SKent Overstreet sb->version, sb->flags, sb->seq); 327cafe5635SKent Overstreet 3284e49ea4aSMike Christie submit_bio(bio); 329cafe5635SKent Overstreet } 330cafe5635SKent Overstreet 331cb7a583eSKent Overstreet static void bch_write_bdev_super_unlock(struct closure *cl) 332cb7a583eSKent Overstreet { 333cb7a583eSKent Overstreet struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write); 334cb7a583eSKent Overstreet 335cb7a583eSKent Overstreet up(&dc->sb_write_mutex); 336cb7a583eSKent Overstreet } 337cb7a583eSKent Overstreet 338cafe5635SKent Overstreet void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent) 339cafe5635SKent Overstreet { 340cb7a583eSKent Overstreet struct closure *cl = &dc->sb_write; 341cafe5635SKent Overstreet struct bio *bio = &dc->sb_bio; 342cafe5635SKent Overstreet 343cb7a583eSKent Overstreet down(&dc->sb_write_mutex); 344cb7a583eSKent Overstreet closure_init(cl, parent); 345cafe5635SKent Overstreet 346475389aeSChristoph Hellwig bio_init(bio, dc->sb_bv, 1); 34774d46992SChristoph Hellwig bio_set_dev(bio, dc->bdev); 348cafe5635SKent Overstreet bio->bi_end_io = write_bdev_super_endio; 349cafe5635SKent Overstreet bio->bi_private = dc; 350cafe5635SKent Overstreet 351cafe5635SKent Overstreet closure_get(cl); 35227a40ab9SColy Li /* I/O request sent to backing device */ 353475389aeSChristoph Hellwig __write_super(&dc->sb, dc->sb_disk, bio); 354cafe5635SKent Overstreet 355cb7a583eSKent Overstreet closure_return_with_destructor(cl, bch_write_bdev_super_unlock); 356cafe5635SKent Overstreet } 357cafe5635SKent Overstreet 3584246a0b6SChristoph Hellwig static void write_super_endio(struct bio *bio) 359cafe5635SKent Overstreet { 360cafe5635SKent Overstreet struct cache *ca = bio->bi_private; 361cafe5635SKent Overstreet 3625138ac67SColy Li /* is_read = 0 */ 3635138ac67SColy Li bch_count_io_errors(ca, bio->bi_status, 0, 3645138ac67SColy Li "writing superblock"); 365cb7a583eSKent Overstreet closure_put(&ca->set->sb_write); 366cb7a583eSKent Overstreet } 367cb7a583eSKent Overstreet 368cb7a583eSKent Overstreet static void bcache_write_super_unlock(struct closure *cl) 369cb7a583eSKent Overstreet { 370cb7a583eSKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, sb_write); 371cb7a583eSKent Overstreet 372cb7a583eSKent Overstreet up(&c->sb_write_mutex); 373cafe5635SKent Overstreet } 374cafe5635SKent Overstreet 375cafe5635SKent Overstreet void bcache_write_super(struct cache_set *c) 376cafe5635SKent Overstreet { 377cb7a583eSKent Overstreet struct closure *cl = &c->sb_write; 37808fdb2cdSColy Li struct cache *ca = c->cache; 37908fdb2cdSColy Li struct bio *bio = &ca->sb_bio; 38008fdb2cdSColy Li unsigned int version = BCACHE_SB_VERSION_CDEV_WITH_UUID; 381cafe5635SKent Overstreet 382cb7a583eSKent Overstreet down(&c->sb_write_mutex); 383cb7a583eSKent Overstreet closure_init(cl, &c->cl); 384cafe5635SKent Overstreet 3854a784266SColy Li ca->sb.seq++; 386cafe5635SKent Overstreet 3874a784266SColy Li if (ca->sb.version < version) 388d721a43fSColy Li ca->sb.version = version; 389cafe5635SKent Overstreet 390475389aeSChristoph Hellwig bio_init(bio, ca->sb_bv, 1); 39174d46992SChristoph Hellwig bio_set_dev(bio, ca->bdev); 392cafe5635SKent Overstreet bio->bi_end_io = write_super_endio; 393cafe5635SKent Overstreet bio->bi_private = ca; 394cafe5635SKent Overstreet 395cafe5635SKent Overstreet closure_get(cl); 396475389aeSChristoph Hellwig __write_super(&ca->sb, ca->sb_disk, bio); 397cafe5635SKent Overstreet 398cb7a583eSKent Overstreet closure_return_with_destructor(cl, bcache_write_super_unlock); 399cafe5635SKent Overstreet } 400cafe5635SKent Overstreet 401cafe5635SKent Overstreet /* UUID io */ 402cafe5635SKent Overstreet 4034246a0b6SChristoph Hellwig static void uuid_endio(struct bio *bio) 404cafe5635SKent Overstreet { 405cafe5635SKent Overstreet struct closure *cl = bio->bi_private; 406cb7a583eSKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 407cafe5635SKent Overstreet 4084e4cbee9SChristoph Hellwig cache_set_err_on(bio->bi_status, c, "accessing uuids"); 409cafe5635SKent Overstreet bch_bbio_free(bio, c); 410cafe5635SKent Overstreet closure_put(cl); 411cafe5635SKent Overstreet } 412cafe5635SKent Overstreet 413cb7a583eSKent Overstreet static void uuid_io_unlock(struct closure *cl) 414cb7a583eSKent Overstreet { 415cb7a583eSKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 416cb7a583eSKent Overstreet 417cb7a583eSKent Overstreet up(&c->uuid_write_mutex); 418cb7a583eSKent Overstreet } 419cb7a583eSKent Overstreet 420ad0d9e76SMike Christie static void uuid_io(struct cache_set *c, int op, unsigned long op_flags, 421cafe5635SKent Overstreet struct bkey *k, struct closure *parent) 422cafe5635SKent Overstreet { 423cb7a583eSKent Overstreet struct closure *cl = &c->uuid_write; 424cafe5635SKent Overstreet struct uuid_entry *u; 4256f10f7d1SColy Li unsigned int i; 42685b1492eSKent Overstreet char buf[80]; 427cafe5635SKent Overstreet 428cafe5635SKent Overstreet BUG_ON(!parent); 429cb7a583eSKent Overstreet down(&c->uuid_write_mutex); 430cb7a583eSKent Overstreet closure_init(cl, parent); 431cafe5635SKent Overstreet 432cafe5635SKent Overstreet for (i = 0; i < KEY_PTRS(k); i++) { 433cafe5635SKent Overstreet struct bio *bio = bch_bbio_alloc(c); 434cafe5635SKent Overstreet 4351eff9d32SJens Axboe bio->bi_opf = REQ_SYNC | REQ_META | op_flags; 4364f024f37SKent Overstreet bio->bi_iter.bi_size = KEY_SIZE(k) << 9; 437cafe5635SKent Overstreet 438cafe5635SKent Overstreet bio->bi_end_io = uuid_endio; 439cafe5635SKent Overstreet bio->bi_private = cl; 440ad0d9e76SMike Christie bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags); 441169ef1cfSKent Overstreet bch_bio_map(bio, c->uuids); 442cafe5635SKent Overstreet 443cafe5635SKent Overstreet bch_submit_bbio(bio, c, k, i); 444cafe5635SKent Overstreet 445ad0d9e76SMike Christie if (op != REQ_OP_WRITE) 446cafe5635SKent Overstreet break; 447cafe5635SKent Overstreet } 448cafe5635SKent Overstreet 449dc9d98d6SKent Overstreet bch_extent_to_text(buf, sizeof(buf), k); 45046f5aa88SJoe Perches pr_debug("%s UUIDs at %s\n", op == REQ_OP_WRITE ? "wrote" : "read", buf); 451cafe5635SKent Overstreet 452cafe5635SKent Overstreet for (u = c->uuids; u < c->uuids + c->nr_uuids; u++) 453169ef1cfSKent Overstreet if (!bch_is_zero(u->uuid, 16)) 45446f5aa88SJoe Perches pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u\n", 455cafe5635SKent Overstreet u - c->uuids, u->uuid, u->label, 456cafe5635SKent Overstreet u->first_reg, u->last_reg, u->invalidated); 457cafe5635SKent Overstreet 458cb7a583eSKent Overstreet closure_return_with_destructor(cl, uuid_io_unlock); 459cafe5635SKent Overstreet } 460cafe5635SKent Overstreet 461cafe5635SKent Overstreet static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl) 462cafe5635SKent Overstreet { 463cafe5635SKent Overstreet struct bkey *k = &j->uuid_bucket; 464cafe5635SKent Overstreet 46565d45231SKent Overstreet if (__bch_btree_ptr_invalid(c, k)) 466cafe5635SKent Overstreet return "bad uuid pointer"; 467cafe5635SKent Overstreet 468cafe5635SKent Overstreet bkey_copy(&c->uuid_bucket, k); 46970fd7614SChristoph Hellwig uuid_io(c, REQ_OP_READ, 0, k, cl); 470cafe5635SKent Overstreet 471cafe5635SKent Overstreet if (j->version < BCACHE_JSET_VERSION_UUIDv1) { 472cafe5635SKent Overstreet struct uuid_entry_v0 *u0 = (void *) c->uuids; 473cafe5635SKent Overstreet struct uuid_entry *u1 = (void *) c->uuids; 474cafe5635SKent Overstreet int i; 475cafe5635SKent Overstreet 476cafe5635SKent Overstreet closure_sync(cl); 477cafe5635SKent Overstreet 478cafe5635SKent Overstreet /* 479cafe5635SKent Overstreet * Since the new uuid entry is bigger than the old, we have to 480cafe5635SKent Overstreet * convert starting at the highest memory address and work down 481cafe5635SKent Overstreet * in order to do it in place 482cafe5635SKent Overstreet */ 483cafe5635SKent Overstreet 484cafe5635SKent Overstreet for (i = c->nr_uuids - 1; 485cafe5635SKent Overstreet i >= 0; 486cafe5635SKent Overstreet --i) { 487cafe5635SKent Overstreet memcpy(u1[i].uuid, u0[i].uuid, 16); 488cafe5635SKent Overstreet memcpy(u1[i].label, u0[i].label, 32); 489cafe5635SKent Overstreet 490cafe5635SKent Overstreet u1[i].first_reg = u0[i].first_reg; 491cafe5635SKent Overstreet u1[i].last_reg = u0[i].last_reg; 492cafe5635SKent Overstreet u1[i].invalidated = u0[i].invalidated; 493cafe5635SKent Overstreet 494cafe5635SKent Overstreet u1[i].flags = 0; 495cafe5635SKent Overstreet u1[i].sectors = 0; 496cafe5635SKent Overstreet } 497cafe5635SKent Overstreet } 498cafe5635SKent Overstreet 499cafe5635SKent Overstreet return NULL; 500cafe5635SKent Overstreet } 501cafe5635SKent Overstreet 502cafe5635SKent Overstreet static int __uuid_write(struct cache_set *c) 503cafe5635SKent Overstreet { 504cafe5635SKent Overstreet BKEY_PADDED(key) k; 505cafe5635SKent Overstreet struct closure cl; 5064a784266SColy Li struct cache *ca = c->cache; 50721e478ddSColy Li unsigned int size; 508cafe5635SKent Overstreet 5091fae7cf0SColy Li closure_init_stack(&cl); 510cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 511cafe5635SKent Overstreet 51217e4aed8SColy Li if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, true)) 513cafe5635SKent Overstreet return 1; 514cafe5635SKent Overstreet 5154a784266SColy Li size = meta_bucket_pages(&ca->sb) * PAGE_SECTORS; 51621e478ddSColy Li SET_KEY_SIZE(&k.key, size); 517ad0d9e76SMike Christie uuid_io(c, REQ_OP_WRITE, 0, &k.key, &cl); 518cafe5635SKent Overstreet closure_sync(&cl); 519cafe5635SKent Overstreet 5207a55948dSShenghui Wang /* Only one bucket used for uuid write */ 5217a55948dSShenghui Wang atomic_long_add(ca->sb.bucket_size, &ca->meta_sectors_written); 5227a55948dSShenghui Wang 523cafe5635SKent Overstreet bkey_copy(&c->uuid_bucket, &k.key); 5243a3b6a4eSKent Overstreet bkey_put(c, &k.key); 525cafe5635SKent Overstreet return 0; 526cafe5635SKent Overstreet } 527cafe5635SKent Overstreet 528cafe5635SKent Overstreet int bch_uuid_write(struct cache_set *c) 529cafe5635SKent Overstreet { 530cafe5635SKent Overstreet int ret = __uuid_write(c); 531cafe5635SKent Overstreet 532cafe5635SKent Overstreet if (!ret) 533cafe5635SKent Overstreet bch_journal_meta(c, NULL); 534cafe5635SKent Overstreet 535cafe5635SKent Overstreet return ret; 536cafe5635SKent Overstreet } 537cafe5635SKent Overstreet 538cafe5635SKent Overstreet static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid) 539cafe5635SKent Overstreet { 540cafe5635SKent Overstreet struct uuid_entry *u; 541cafe5635SKent Overstreet 542cafe5635SKent Overstreet for (u = c->uuids; 543cafe5635SKent Overstreet u < c->uuids + c->nr_uuids; u++) 544cafe5635SKent Overstreet if (!memcmp(u->uuid, uuid, 16)) 545cafe5635SKent Overstreet return u; 546cafe5635SKent Overstreet 547cafe5635SKent Overstreet return NULL; 548cafe5635SKent Overstreet } 549cafe5635SKent Overstreet 550cafe5635SKent Overstreet static struct uuid_entry *uuid_find_empty(struct cache_set *c) 551cafe5635SKent Overstreet { 552cafe5635SKent Overstreet static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 5531fae7cf0SColy Li 554cafe5635SKent Overstreet return uuid_find(c, zero_uuid); 555cafe5635SKent Overstreet } 556cafe5635SKent Overstreet 557cafe5635SKent Overstreet /* 558cafe5635SKent Overstreet * Bucket priorities/gens: 559cafe5635SKent Overstreet * 560cafe5635SKent Overstreet * For each bucket, we store on disk its 561cafe5635SKent Overstreet * 8 bit gen 562cafe5635SKent Overstreet * 16 bit priority 563cafe5635SKent Overstreet * 564cafe5635SKent Overstreet * See alloc.c for an explanation of the gen. The priority is used to implement 565cafe5635SKent Overstreet * lru (and in the future other) cache replacement policies; for most purposes 566cafe5635SKent Overstreet * it's just an opaque integer. 567cafe5635SKent Overstreet * 568cafe5635SKent Overstreet * The gens and the priorities don't have a whole lot to do with each other, and 569cafe5635SKent Overstreet * it's actually the gens that must be written out at specific times - it's no 570cafe5635SKent Overstreet * big deal if the priorities don't get written, if we lose them we just reuse 571cafe5635SKent Overstreet * buckets in suboptimal order. 572cafe5635SKent Overstreet * 573cafe5635SKent Overstreet * On disk they're stored in a packed array, and in as many buckets are required 574cafe5635SKent Overstreet * to fit them all. The buckets we use to store them form a list; the journal 575cafe5635SKent Overstreet * header points to the first bucket, the first bucket points to the second 576cafe5635SKent Overstreet * bucket, et cetera. 577cafe5635SKent Overstreet * 578cafe5635SKent Overstreet * This code is used by the allocation code; periodically (whenever it runs out 579cafe5635SKent Overstreet * of buckets to allocate from) the allocation code will invalidate some 580cafe5635SKent Overstreet * buckets, but it can't use those buckets until their new gens are safely on 581cafe5635SKent Overstreet * disk. 582cafe5635SKent Overstreet */ 583cafe5635SKent Overstreet 5844246a0b6SChristoph Hellwig static void prio_endio(struct bio *bio) 585cafe5635SKent Overstreet { 586cafe5635SKent Overstreet struct cache *ca = bio->bi_private; 587cafe5635SKent Overstreet 5884e4cbee9SChristoph Hellwig cache_set_err_on(bio->bi_status, ca->set, "accessing priorities"); 589cafe5635SKent Overstreet bch_bbio_free(bio, ca->set); 590cafe5635SKent Overstreet closure_put(&ca->prio); 591cafe5635SKent Overstreet } 592cafe5635SKent Overstreet 593ad0d9e76SMike Christie static void prio_io(struct cache *ca, uint64_t bucket, int op, 594ad0d9e76SMike Christie unsigned long op_flags) 595cafe5635SKent Overstreet { 596cafe5635SKent Overstreet struct closure *cl = &ca->prio; 597cafe5635SKent Overstreet struct bio *bio = bch_bbio_alloc(ca->set); 598cafe5635SKent Overstreet 599cafe5635SKent Overstreet closure_init_stack(cl); 600cafe5635SKent Overstreet 6014f024f37SKent Overstreet bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size; 60274d46992SChristoph Hellwig bio_set_dev(bio, ca->bdev); 603c954ac8dSColy Li bio->bi_iter.bi_size = meta_bucket_bytes(&ca->sb); 604cafe5635SKent Overstreet 605cafe5635SKent Overstreet bio->bi_end_io = prio_endio; 606cafe5635SKent Overstreet bio->bi_private = ca; 607ad0d9e76SMike Christie bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags); 608169ef1cfSKent Overstreet bch_bio_map(bio, ca->disk_buckets); 609cafe5635SKent Overstreet 610771f393eSColy Li closure_bio_submit(ca->set, bio, &ca->prio); 611cafe5635SKent Overstreet closure_sync(cl); 612cafe5635SKent Overstreet } 613cafe5635SKent Overstreet 61484c529aeSAndrea Righi int bch_prio_write(struct cache *ca, bool wait) 615cafe5635SKent Overstreet { 616cafe5635SKent Overstreet int i; 617cafe5635SKent Overstreet struct bucket *b; 618cafe5635SKent Overstreet struct closure cl; 619cafe5635SKent Overstreet 62046f5aa88SJoe Perches pr_debug("free_prio=%zu, free_none=%zu, free_inc=%zu\n", 62184c529aeSAndrea Righi fifo_used(&ca->free[RESERVE_PRIO]), 62284c529aeSAndrea Righi fifo_used(&ca->free[RESERVE_NONE]), 62384c529aeSAndrea Righi fifo_used(&ca->free_inc)); 62484c529aeSAndrea Righi 62584c529aeSAndrea Righi /* 62684c529aeSAndrea Righi * Pre-check if there are enough free buckets. In the non-blocking 62784c529aeSAndrea Righi * scenario it's better to fail early rather than starting to allocate 62884c529aeSAndrea Righi * buckets and do a cleanup later in case of failure. 62984c529aeSAndrea Righi */ 63084c529aeSAndrea Righi if (!wait) { 63184c529aeSAndrea Righi size_t avail = fifo_used(&ca->free[RESERVE_PRIO]) + 63284c529aeSAndrea Righi fifo_used(&ca->free[RESERVE_NONE]); 63384c529aeSAndrea Righi if (prio_buckets(ca) > avail) 63484c529aeSAndrea Righi return -ENOMEM; 63584c529aeSAndrea Righi } 63684c529aeSAndrea Righi 637cafe5635SKent Overstreet closure_init_stack(&cl); 638cafe5635SKent Overstreet 639cafe5635SKent Overstreet lockdep_assert_held(&ca->set->bucket_lock); 640cafe5635SKent Overstreet 641cafe5635SKent Overstreet ca->disk_buckets->seq++; 642cafe5635SKent Overstreet 643cafe5635SKent Overstreet atomic_long_add(ca->sb.bucket_size * prio_buckets(ca), 644cafe5635SKent Overstreet &ca->meta_sectors_written); 645cafe5635SKent Overstreet 646cafe5635SKent Overstreet for (i = prio_buckets(ca) - 1; i >= 0; --i) { 647cafe5635SKent Overstreet long bucket; 648cafe5635SKent Overstreet struct prio_set *p = ca->disk_buckets; 649b1a67b0fSKent Overstreet struct bucket_disk *d = p->data; 650b1a67b0fSKent Overstreet struct bucket_disk *end = d + prios_per_bucket(ca); 651cafe5635SKent Overstreet 652cafe5635SKent Overstreet for (b = ca->buckets + i * prios_per_bucket(ca); 653cafe5635SKent Overstreet b < ca->buckets + ca->sb.nbuckets && d < end; 654cafe5635SKent Overstreet b++, d++) { 655cafe5635SKent Overstreet d->prio = cpu_to_le16(b->prio); 656cafe5635SKent Overstreet d->gen = b->gen; 657cafe5635SKent Overstreet } 658cafe5635SKent Overstreet 659cafe5635SKent Overstreet p->next_bucket = ca->prio_buckets[i + 1]; 66081ab4190SKent Overstreet p->magic = pset_magic(&ca->sb); 661c954ac8dSColy Li p->csum = bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8); 662cafe5635SKent Overstreet 66384c529aeSAndrea Righi bucket = bch_bucket_alloc(ca, RESERVE_PRIO, wait); 664cafe5635SKent Overstreet BUG_ON(bucket == -1); 665cafe5635SKent Overstreet 666cafe5635SKent Overstreet mutex_unlock(&ca->set->bucket_lock); 667ad0d9e76SMike Christie prio_io(ca, bucket, REQ_OP_WRITE, 0); 668cafe5635SKent Overstreet mutex_lock(&ca->set->bucket_lock); 669cafe5635SKent Overstreet 670cafe5635SKent Overstreet ca->prio_buckets[i] = bucket; 671cafe5635SKent Overstreet atomic_dec_bug(&ca->buckets[bucket].pin); 672cafe5635SKent Overstreet } 673cafe5635SKent Overstreet 674cafe5635SKent Overstreet mutex_unlock(&ca->set->bucket_lock); 675cafe5635SKent Overstreet 676cafe5635SKent Overstreet bch_journal_meta(ca->set, &cl); 677cafe5635SKent Overstreet closure_sync(&cl); 678cafe5635SKent Overstreet 679cafe5635SKent Overstreet mutex_lock(&ca->set->bucket_lock); 680cafe5635SKent Overstreet 681cafe5635SKent Overstreet /* 682cafe5635SKent Overstreet * Don't want the old priorities to get garbage collected until after we 683cafe5635SKent Overstreet * finish writing the new ones, and they're journalled 684cafe5635SKent Overstreet */ 6852531d9eeSKent Overstreet for (i = 0; i < prio_buckets(ca); i++) { 6862531d9eeSKent Overstreet if (ca->prio_last_buckets[i]) 6872531d9eeSKent Overstreet __bch_bucket_free(ca, 6882531d9eeSKent Overstreet &ca->buckets[ca->prio_last_buckets[i]]); 6892531d9eeSKent Overstreet 690cafe5635SKent Overstreet ca->prio_last_buckets[i] = ca->prio_buckets[i]; 691cafe5635SKent Overstreet } 69284c529aeSAndrea Righi return 0; 6932531d9eeSKent Overstreet } 694cafe5635SKent Overstreet 69549d08d59SColy Li static int prio_read(struct cache *ca, uint64_t bucket) 696cafe5635SKent Overstreet { 697cafe5635SKent Overstreet struct prio_set *p = ca->disk_buckets; 698cafe5635SKent Overstreet struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d; 699cafe5635SKent Overstreet struct bucket *b; 7006f10f7d1SColy Li unsigned int bucket_nr = 0; 70149d08d59SColy Li int ret = -EIO; 702cafe5635SKent Overstreet 703cafe5635SKent Overstreet for (b = ca->buckets; 704cafe5635SKent Overstreet b < ca->buckets + ca->sb.nbuckets; 705cafe5635SKent Overstreet b++, d++) { 706cafe5635SKent Overstreet if (d == end) { 707cafe5635SKent Overstreet ca->prio_buckets[bucket_nr] = bucket; 708cafe5635SKent Overstreet ca->prio_last_buckets[bucket_nr] = bucket; 709cafe5635SKent Overstreet bucket_nr++; 710cafe5635SKent Overstreet 71170fd7614SChristoph Hellwig prio_io(ca, bucket, REQ_OP_READ, 0); 712cafe5635SKent Overstreet 713b0d30981SColy Li if (p->csum != 714c954ac8dSColy Li bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8)) { 71546f5aa88SJoe Perches pr_warn("bad csum reading priorities\n"); 71649d08d59SColy Li goto out; 71749d08d59SColy Li } 718cafe5635SKent Overstreet 71949d08d59SColy Li if (p->magic != pset_magic(&ca->sb)) { 72046f5aa88SJoe Perches pr_warn("bad magic reading priorities\n"); 72149d08d59SColy Li goto out; 72249d08d59SColy Li } 723cafe5635SKent Overstreet 724cafe5635SKent Overstreet bucket = p->next_bucket; 725cafe5635SKent Overstreet d = p->data; 726cafe5635SKent Overstreet } 727cafe5635SKent Overstreet 728cafe5635SKent Overstreet b->prio = le16_to_cpu(d->prio); 7293a2fd9d5SKent Overstreet b->gen = b->last_gc = d->gen; 730cafe5635SKent Overstreet } 73149d08d59SColy Li 73249d08d59SColy Li ret = 0; 73349d08d59SColy Li out: 73449d08d59SColy Li return ret; 735cafe5635SKent Overstreet } 736cafe5635SKent Overstreet 737cafe5635SKent Overstreet /* Bcache device */ 738cafe5635SKent Overstreet 739cafe5635SKent Overstreet static int open_dev(struct block_device *b, fmode_t mode) 740cafe5635SKent Overstreet { 741cafe5635SKent Overstreet struct bcache_device *d = b->bd_disk->private_data; 7421fae7cf0SColy Li 743c4d951ddSKent Overstreet if (test_bit(BCACHE_DEV_CLOSING, &d->flags)) 744cafe5635SKent Overstreet return -ENXIO; 745cafe5635SKent Overstreet 746cafe5635SKent Overstreet closure_get(&d->cl); 747cafe5635SKent Overstreet return 0; 748cafe5635SKent Overstreet } 749cafe5635SKent Overstreet 750867e1162SEmil Goode static void release_dev(struct gendisk *b, fmode_t mode) 751cafe5635SKent Overstreet { 752cafe5635SKent Overstreet struct bcache_device *d = b->private_data; 7531fae7cf0SColy Li 754cafe5635SKent Overstreet closure_put(&d->cl); 755cafe5635SKent Overstreet } 756cafe5635SKent Overstreet 757cafe5635SKent Overstreet static int ioctl_dev(struct block_device *b, fmode_t mode, 758cafe5635SKent Overstreet unsigned int cmd, unsigned long arg) 759cafe5635SKent Overstreet { 760cafe5635SKent Overstreet struct bcache_device *d = b->bd_disk->private_data; 7610f0709e6SColy Li 762cafe5635SKent Overstreet return d->ioctl(d, mode, cmd, arg); 763cafe5635SKent Overstreet } 764cafe5635SKent Overstreet 765c62b37d9SChristoph Hellwig static const struct block_device_operations bcache_cached_ops = { 766c62b37d9SChristoph Hellwig .submit_bio = cached_dev_submit_bio, 767c62b37d9SChristoph Hellwig .open = open_dev, 768c62b37d9SChristoph Hellwig .release = release_dev, 769c62b37d9SChristoph Hellwig .ioctl = ioctl_dev, 770c62b37d9SChristoph Hellwig .owner = THIS_MODULE, 771c62b37d9SChristoph Hellwig }; 772c62b37d9SChristoph Hellwig 773c62b37d9SChristoph Hellwig static const struct block_device_operations bcache_flash_ops = { 774c62b37d9SChristoph Hellwig .submit_bio = flash_dev_submit_bio, 775cafe5635SKent Overstreet .open = open_dev, 776cafe5635SKent Overstreet .release = release_dev, 777cafe5635SKent Overstreet .ioctl = ioctl_dev, 778cafe5635SKent Overstreet .owner = THIS_MODULE, 779cafe5635SKent Overstreet }; 780cafe5635SKent Overstreet 781cafe5635SKent Overstreet void bcache_device_stop(struct bcache_device *d) 782cafe5635SKent Overstreet { 783c4d951ddSKent Overstreet if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags)) 78463d63b51SColy Li /* 78563d63b51SColy Li * closure_fn set to 78663d63b51SColy Li * - cached device: cached_dev_flush() 78763d63b51SColy Li * - flash dev: flash_dev_flush() 78863d63b51SColy Li */ 789cafe5635SKent Overstreet closure_queue(&d->cl); 790cafe5635SKent Overstreet } 791cafe5635SKent Overstreet 792ee668506SKent Overstreet static void bcache_device_unlink(struct bcache_device *d) 793ee668506SKent Overstreet { 794c4d951ddSKent Overstreet lockdep_assert_held(&bch_register_lock); 795c4d951ddSKent Overstreet 796c4d951ddSKent Overstreet if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) { 79708fdb2cdSColy Li struct cache *ca = d->c->cache; 798ee668506SKent Overstreet 799ee668506SKent Overstreet sysfs_remove_link(&d->c->kobj, d->name); 800ee668506SKent Overstreet sysfs_remove_link(&d->kobj, "cache"); 801ee668506SKent Overstreet 802ee668506SKent Overstreet bd_unlink_disk_holder(ca->bdev, d->disk); 803ee668506SKent Overstreet } 804c4d951ddSKent Overstreet } 805ee668506SKent Overstreet 806ee668506SKent Overstreet static void bcache_device_link(struct bcache_device *d, struct cache_set *c, 807ee668506SKent Overstreet const char *name) 808ee668506SKent Overstreet { 80908fdb2cdSColy Li struct cache *ca = c->cache; 8104b6efb4bSColy Li int ret; 811ee668506SKent Overstreet 812ee668506SKent Overstreet bd_link_disk_holder(ca->bdev, d->disk); 813ee668506SKent Overstreet 814ee668506SKent Overstreet snprintf(d->name, BCACHEDEVNAME_SIZE, 815ee668506SKent Overstreet "%s%u", name, d->id); 816ee668506SKent Overstreet 8174b6efb4bSColy Li ret = sysfs_create_link(&d->kobj, &c->kobj, "cache"); 8184b6efb4bSColy Li if (ret < 0) 81946f5aa88SJoe Perches pr_err("Couldn't create device -> cache set symlink\n"); 8204b6efb4bSColy Li 8214b6efb4bSColy Li ret = sysfs_create_link(&c->kobj, &d->kobj, d->name); 8224b6efb4bSColy Li if (ret < 0) 82346f5aa88SJoe Perches pr_err("Couldn't create cache set -> device symlink\n"); 824fecaee6fSZheng Liu 825fecaee6fSZheng Liu clear_bit(BCACHE_DEV_UNLINK_DONE, &d->flags); 826ee668506SKent Overstreet } 827ee668506SKent Overstreet 828cafe5635SKent Overstreet static void bcache_device_detach(struct bcache_device *d) 829cafe5635SKent Overstreet { 830cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 831cafe5635SKent Overstreet 832ea8c5356SColy Li atomic_dec(&d->c->attached_dev_nr); 833ea8c5356SColy Li 834c4d951ddSKent Overstreet if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) { 835cafe5635SKent Overstreet struct uuid_entry *u = d->c->uuids + d->id; 836cafe5635SKent Overstreet 837cafe5635SKent Overstreet SET_UUID_FLASH_ONLY(u, 0); 838cafe5635SKent Overstreet memcpy(u->uuid, invalid_uuid, 16); 83975cbb3f1SArnd Bergmann u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds()); 840cafe5635SKent Overstreet bch_uuid_write(d->c); 841cafe5635SKent Overstreet } 842cafe5635SKent Overstreet 843ee668506SKent Overstreet bcache_device_unlink(d); 844ee668506SKent Overstreet 845cafe5635SKent Overstreet d->c->devices[d->id] = NULL; 846cafe5635SKent Overstreet closure_put(&d->c->caching); 847cafe5635SKent Overstreet d->c = NULL; 848cafe5635SKent Overstreet } 849cafe5635SKent Overstreet 850cafe5635SKent Overstreet static void bcache_device_attach(struct bcache_device *d, struct cache_set *c, 8516f10f7d1SColy Li unsigned int id) 852cafe5635SKent Overstreet { 853cafe5635SKent Overstreet d->id = id; 854cafe5635SKent Overstreet d->c = c; 855cafe5635SKent Overstreet c->devices[id] = d; 856cafe5635SKent Overstreet 8572831231dSColy Li if (id >= c->devices_max_used) 8582831231dSColy Li c->devices_max_used = id + 1; 8592831231dSColy Li 860cafe5635SKent Overstreet closure_get(&c->caching); 861cafe5635SKent Overstreet } 862cafe5635SKent Overstreet 8631dbe32adSColy Li static inline int first_minor_to_idx(int first_minor) 8641dbe32adSColy Li { 8651dbe32adSColy Li return (first_minor/BCACHE_MINORS); 8661dbe32adSColy Li } 8671dbe32adSColy Li 8681dbe32adSColy Li static inline int idx_to_first_minor(int idx) 8691dbe32adSColy Li { 8701dbe32adSColy Li return (idx * BCACHE_MINORS); 8711dbe32adSColy Li } 8721dbe32adSColy Li 873cafe5635SKent Overstreet static void bcache_device_free(struct bcache_device *d) 874cafe5635SKent Overstreet { 8752d886951SColy Li struct gendisk *disk = d->disk; 8762d886951SColy Li 877cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 878cafe5635SKent Overstreet 8792d886951SColy Li if (disk) 88046f5aa88SJoe Perches pr_info("%s stopped\n", disk->disk_name); 8812d886951SColy Li else 88246f5aa88SJoe Perches pr_err("bcache device (NULL gendisk) stopped\n"); 883cafe5635SKent Overstreet 884cafe5635SKent Overstreet if (d->c) 885cafe5635SKent Overstreet bcache_device_detach(d); 8862d886951SColy Li 8872d886951SColy Li if (disk) { 888bc70852fSChristoph Hellwig blk_cleanup_disk(disk); 8891dbe32adSColy Li ida_simple_remove(&bcache_device_idx, 8902d886951SColy Li first_minor_to_idx(disk->first_minor)); 89128935ab5SKent Overstreet } 892cafe5635SKent Overstreet 893d19936a2SKent Overstreet bioset_exit(&d->bio_split); 894958b4338SPekka Enberg kvfree(d->full_dirty_stripes); 895958b4338SPekka Enberg kvfree(d->stripe_sectors_dirty); 896cafe5635SKent Overstreet 897cafe5635SKent Overstreet closure_debug_destroy(&d->cl); 898cafe5635SKent Overstreet } 899cafe5635SKent Overstreet 9006f10f7d1SColy Li static int bcache_device_init(struct bcache_device *d, unsigned int block_size, 901c62b37d9SChristoph Hellwig sector_t sectors, struct block_device *cached_bdev, 902c62b37d9SChristoph Hellwig const struct block_device_operations *ops) 903cafe5635SKent Overstreet { 904cafe5635SKent Overstreet struct request_queue *q; 9055f2b18ecSBart Van Assche const size_t max_stripes = min_t(size_t, INT_MAX, 9065f2b18ecSBart Van Assche SIZE_MAX / sizeof(atomic_t)); 90765f0f017SColy Li uint64_t n; 9081dbe32adSColy Li int idx; 909279afbadSKent Overstreet 9102d679fc7SKent Overstreet if (!d->stripe_size) 9112d679fc7SKent Overstreet d->stripe_size = 1 << 31; 912279afbadSKent Overstreet 91365f0f017SColy Li n = DIV_ROUND_UP_ULL(sectors, d->stripe_size); 91465f0f017SColy Li if (!n || n > max_stripes) { 91565f0f017SColy Li pr_err("nr_stripes too large or invalid: %llu (start sector beyond end of disk?)\n", 91665f0f017SColy Li n); 917279afbadSKent Overstreet return -ENOMEM; 91848a915a8SKent Overstreet } 91965f0f017SColy Li d->nr_stripes = n; 920279afbadSKent Overstreet 921279afbadSKent Overstreet n = d->nr_stripes * sizeof(atomic_t); 922bc4e54f6SMichal Hocko d->stripe_sectors_dirty = kvzalloc(n, GFP_KERNEL); 923279afbadSKent Overstreet if (!d->stripe_sectors_dirty) 924279afbadSKent Overstreet return -ENOMEM; 925cafe5635SKent Overstreet 92648a915a8SKent Overstreet n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long); 927bc4e54f6SMichal Hocko d->full_dirty_stripes = kvzalloc(n, GFP_KERNEL); 92848a915a8SKent Overstreet if (!d->full_dirty_stripes) 929224b0683SChristoph Hellwig goto out_free_stripe_sectors_dirty; 93048a915a8SKent Overstreet 9311dbe32adSColy Li idx = ida_simple_get(&bcache_device_idx, 0, 9321dbe32adSColy Li BCACHE_DEVICE_IDX_MAX, GFP_KERNEL); 9331dbe32adSColy Li if (idx < 0) 934224b0683SChristoph Hellwig goto out_free_full_dirty_stripes; 935b8c0d911SEric Wheeler 936d19936a2SKent Overstreet if (bioset_init(&d->bio_split, 4, offsetof(struct bbio, bio), 9379b4e9f5aSFlorian Schmaus BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER)) 938224b0683SChristoph Hellwig goto out_ida_remove; 9399b4e9f5aSFlorian Schmaus 940bc70852fSChristoph Hellwig d->disk = blk_alloc_disk(NUMA_NO_NODE); 9419b4e9f5aSFlorian Schmaus if (!d->disk) 942224b0683SChristoph Hellwig goto out_bioset_exit; 943cafe5635SKent Overstreet 944279afbadSKent Overstreet set_capacity(d->disk, sectors); 9451dbe32adSColy Li snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", idx); 946cafe5635SKent Overstreet 947cafe5635SKent Overstreet d->disk->major = bcache_major; 9481dbe32adSColy Li d->disk->first_minor = idx_to_first_minor(idx); 949bc70852fSChristoph Hellwig d->disk->minors = BCACHE_MINORS; 950c62b37d9SChristoph Hellwig d->disk->fops = ops; 951cafe5635SKent Overstreet d->disk->private_data = d; 952cafe5635SKent Overstreet 953bc70852fSChristoph Hellwig q = d->disk->queue; 954cafe5635SKent Overstreet q->limits.max_hw_sectors = UINT_MAX; 955cafe5635SKent Overstreet q->limits.max_sectors = UINT_MAX; 956cafe5635SKent Overstreet q->limits.max_segment_size = UINT_MAX; 957a8affc03SChristoph Hellwig q->limits.max_segments = BIO_MAX_VECS; 9582bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, UINT_MAX); 95990db6919SKent Overstreet q->limits.discard_granularity = 512; 960cafe5635SKent Overstreet q->limits.io_min = block_size; 961cafe5635SKent Overstreet q->limits.logical_block_size = block_size; 962cafe5635SKent Overstreet q->limits.physical_block_size = block_size; 963dcacbc12SMauricio Faria de Oliveira 964dcacbc12SMauricio Faria de Oliveira if (q->limits.logical_block_size > PAGE_SIZE && cached_bdev) { 965dcacbc12SMauricio Faria de Oliveira /* 966dcacbc12SMauricio Faria de Oliveira * This should only happen with BCACHE_SB_VERSION_BDEV. 967dcacbc12SMauricio Faria de Oliveira * Block/page size is checked for BCACHE_SB_VERSION_CDEV. 968dcacbc12SMauricio Faria de Oliveira */ 9694b25bbf5SColy Li pr_info("%s: sb/logical block size (%u) greater than page size (%lu) falling back to device logical block size (%u)\n", 970dcacbc12SMauricio Faria de Oliveira d->disk->disk_name, q->limits.logical_block_size, 971dcacbc12SMauricio Faria de Oliveira PAGE_SIZE, bdev_logical_block_size(cached_bdev)); 972dcacbc12SMauricio Faria de Oliveira 973dcacbc12SMauricio Faria de Oliveira /* This also adjusts physical block size/min io size if needed */ 974dcacbc12SMauricio Faria de Oliveira blk_queue_logical_block_size(q, bdev_logical_block_size(cached_bdev)); 975dcacbc12SMauricio Faria de Oliveira } 976dcacbc12SMauricio Faria de Oliveira 97744e1ebe2SBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NONROT, d->disk->queue); 97844e1ebe2SBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, d->disk->queue); 97944e1ebe2SBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, d->disk->queue); 980cafe5635SKent Overstreet 98184b4ff9eSJens Axboe blk_queue_write_cache(q, true, true); 98254d12f2bSKent Overstreet 983cafe5635SKent Overstreet return 0; 9849b4e9f5aSFlorian Schmaus 985224b0683SChristoph Hellwig out_bioset_exit: 986224b0683SChristoph Hellwig bioset_exit(&d->bio_split); 987224b0683SChristoph Hellwig out_ida_remove: 9889b4e9f5aSFlorian Schmaus ida_simple_remove(&bcache_device_idx, idx); 989224b0683SChristoph Hellwig out_free_full_dirty_stripes: 990224b0683SChristoph Hellwig kvfree(d->full_dirty_stripes); 991224b0683SChristoph Hellwig out_free_stripe_sectors_dirty: 992224b0683SChristoph Hellwig kvfree(d->stripe_sectors_dirty); 9939b4e9f5aSFlorian Schmaus return -ENOMEM; 9949b4e9f5aSFlorian Schmaus 995cafe5635SKent Overstreet } 996cafe5635SKent Overstreet 997cafe5635SKent Overstreet /* Cached device */ 998cafe5635SKent Overstreet 999cafe5635SKent Overstreet static void calc_cached_dev_sectors(struct cache_set *c) 1000cafe5635SKent Overstreet { 1001cafe5635SKent Overstreet uint64_t sectors = 0; 1002cafe5635SKent Overstreet struct cached_dev *dc; 1003cafe5635SKent Overstreet 1004cafe5635SKent Overstreet list_for_each_entry(dc, &c->cached_devs, list) 1005cafe5635SKent Overstreet sectors += bdev_sectors(dc->bdev); 1006cafe5635SKent Overstreet 1007cafe5635SKent Overstreet c->cached_dev_sectors = sectors; 1008cafe5635SKent Overstreet } 1009cafe5635SKent Overstreet 10100f0709e6SColy Li #define BACKING_DEV_OFFLINE_TIMEOUT 5 10110f0709e6SColy Li static int cached_dev_status_update(void *arg) 10120f0709e6SColy Li { 10130f0709e6SColy Li struct cached_dev *dc = arg; 10140f0709e6SColy Li struct request_queue *q; 10150f0709e6SColy Li 10160f0709e6SColy Li /* 10170f0709e6SColy Li * If this delayed worker is stopping outside, directly quit here. 10180f0709e6SColy Li * dc->io_disable might be set via sysfs interface, so check it 10190f0709e6SColy Li * here too. 10200f0709e6SColy Li */ 10210f0709e6SColy Li while (!kthread_should_stop() && !dc->io_disable) { 10220f0709e6SColy Li q = bdev_get_queue(dc->bdev); 10230f0709e6SColy Li if (blk_queue_dying(q)) 10240f0709e6SColy Li dc->offline_seconds++; 10250f0709e6SColy Li else 10260f0709e6SColy Li dc->offline_seconds = 0; 10270f0709e6SColy Li 10280f0709e6SColy Li if (dc->offline_seconds >= BACKING_DEV_OFFLINE_TIMEOUT) { 102946f5aa88SJoe Perches pr_err("%s: device offline for %d seconds\n", 10300f0709e6SColy Li dc->backing_dev_name, 10310f0709e6SColy Li BACKING_DEV_OFFLINE_TIMEOUT); 103246f5aa88SJoe Perches pr_err("%s: disable I/O request due to backing device offline\n", 103346f5aa88SJoe Perches dc->disk.name); 10340f0709e6SColy Li dc->io_disable = true; 10350f0709e6SColy Li /* let others know earlier that io_disable is true */ 10360f0709e6SColy Li smp_mb(); 10370f0709e6SColy Li bcache_device_stop(&dc->disk); 10380f0709e6SColy Li break; 10390f0709e6SColy Li } 10400f0709e6SColy Li schedule_timeout_interruptible(HZ); 10410f0709e6SColy Li } 10420f0709e6SColy Li 10430f0709e6SColy Li wait_for_kthread_stop(); 10440f0709e6SColy Li return 0; 10450f0709e6SColy Li } 10460f0709e6SColy Li 10470f0709e6SColy Li 10480b13efecSColy Li int bch_cached_dev_run(struct cached_dev *dc) 1049cafe5635SKent Overstreet { 105013e1db65SZhiqiang Liu int ret = 0; 1051cafe5635SKent Overstreet struct bcache_device *d = &dc->disk; 1052792732d9SGeliang Tang char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL); 1053a25c32beSGabriel de Perthuis char *env[] = { 1054a25c32beSGabriel de Perthuis "DRIVER=bcache", 1055a25c32beSGabriel de Perthuis kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid), 1056792732d9SGeliang Tang kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""), 1057ab9e1400SGabriel de Perthuis NULL, 1058a25c32beSGabriel de Perthuis }; 1059cafe5635SKent Overstreet 1060e0faa3d7SColy Li if (dc->io_disable) { 106146f5aa88SJoe Perches pr_err("I/O disabled on cached dev %s\n", 1062e0faa3d7SColy Li dc->backing_dev_name); 106313e1db65SZhiqiang Liu ret = -EIO; 106413e1db65SZhiqiang Liu goto out; 1065e0faa3d7SColy Li } 10660b13efecSColy Li 10674d4d8573SAl Viro if (atomic_xchg(&dc->running, 1)) { 106846f5aa88SJoe Perches pr_info("cached dev %s is running already\n", 1069e0faa3d7SColy Li dc->backing_dev_name); 107013e1db65SZhiqiang Liu ret = -EBUSY; 107113e1db65SZhiqiang Liu goto out; 10724d4d8573SAl Viro } 1073cafe5635SKent Overstreet 1074cafe5635SKent Overstreet if (!d->c && 1075cafe5635SKent Overstreet BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) { 1076cafe5635SKent Overstreet struct closure cl; 10771fae7cf0SColy Li 1078cafe5635SKent Overstreet closure_init_stack(&cl); 1079cafe5635SKent Overstreet 1080cafe5635SKent Overstreet SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE); 1081cafe5635SKent Overstreet bch_write_bdev_super(dc, &cl); 1082cafe5635SKent Overstreet closure_sync(&cl); 1083cafe5635SKent Overstreet } 1084cafe5635SKent Overstreet 1085cafe5635SKent Overstreet add_disk(d->disk); 1086ee668506SKent Overstreet bd_link_disk_holder(dc->bdev, dc->disk.disk); 10873be11dbaSColy Li /* 10883be11dbaSColy Li * won't show up in the uevent file, use udevadm monitor -e instead 10893be11dbaSColy Li * only class / kset properties are persistent 10903be11dbaSColy Li */ 1091cafe5635SKent Overstreet kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); 1092a25c32beSGabriel de Perthuis 1093cafe5635SKent Overstreet if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") || 10940b13efecSColy Li sysfs_create_link(&disk_to_dev(d->disk)->kobj, 10950b13efecSColy Li &d->kobj, "bcache")) { 109646f5aa88SJoe Perches pr_err("Couldn't create bcache dev <-> disk sysfs symlinks\n"); 109713e1db65SZhiqiang Liu ret = -ENOMEM; 109813e1db65SZhiqiang Liu goto out; 10990b13efecSColy Li } 11000f0709e6SColy Li 11010f0709e6SColy Li dc->status_update_thread = kthread_run(cached_dev_status_update, 11020f0709e6SColy Li dc, "bcache_status_update"); 11030f0709e6SColy Li if (IS_ERR(dc->status_update_thread)) { 110446f5aa88SJoe Perches pr_warn("failed to create bcache_status_update kthread, continue to run without monitoring backing device status\n"); 11050f0709e6SColy Li } 11060b13efecSColy Li 110713e1db65SZhiqiang Liu out: 110813e1db65SZhiqiang Liu kfree(env[1]); 110913e1db65SZhiqiang Liu kfree(env[2]); 111013e1db65SZhiqiang Liu kfree(buf); 111113e1db65SZhiqiang Liu return ret; 1112cafe5635SKent Overstreet } 1113cafe5635SKent Overstreet 11143fd47bfeSColy Li /* 11153fd47bfeSColy Li * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed 11163fd47bfeSColy Li * work dc->writeback_rate_update is running. Wait until the routine 11173fd47bfeSColy Li * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to 11183fd47bfeSColy Li * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out 11193fd47bfeSColy Li * seconds, give up waiting here and continue to cancel it too. 11203fd47bfeSColy Li */ 11213fd47bfeSColy Li static void cancel_writeback_rate_update_dwork(struct cached_dev *dc) 11223fd47bfeSColy Li { 11233fd47bfeSColy Li int time_out = WRITEBACK_RATE_UPDATE_SECS_MAX * HZ; 11243fd47bfeSColy Li 11253fd47bfeSColy Li do { 11263fd47bfeSColy Li if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING, 11273fd47bfeSColy Li &dc->disk.flags)) 11283fd47bfeSColy Li break; 11293fd47bfeSColy Li time_out--; 11303fd47bfeSColy Li schedule_timeout_interruptible(1); 11313fd47bfeSColy Li } while (time_out > 0); 11323fd47bfeSColy Li 11333fd47bfeSColy Li if (time_out == 0) 113446f5aa88SJoe Perches pr_warn("give up waiting for dc->writeback_write_update to quit\n"); 11353fd47bfeSColy Li 11363fd47bfeSColy Li cancel_delayed_work_sync(&dc->writeback_rate_update); 11373fd47bfeSColy Li } 11383fd47bfeSColy Li 1139cafe5635SKent Overstreet static void cached_dev_detach_finish(struct work_struct *w) 1140cafe5635SKent Overstreet { 1141cafe5635SKent Overstreet struct cached_dev *dc = container_of(w, struct cached_dev, detach); 1142cafe5635SKent Overstreet 1143c4d951ddSKent Overstreet BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)); 11443b304d24SElena Reshetova BUG_ON(refcount_read(&dc->count)); 1145cafe5635SKent Overstreet 1146cafe5635SKent Overstreet 11473fd47bfeSColy Li if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)) 11483fd47bfeSColy Li cancel_writeback_rate_update_dwork(dc); 11493fd47bfeSColy Li 11508d29c442STang Junhui if (!IS_ERR_OR_NULL(dc->writeback_thread)) { 11518d29c442STang Junhui kthread_stop(dc->writeback_thread); 11528d29c442STang Junhui dc->writeback_thread = NULL; 11538d29c442STang Junhui } 11548d29c442STang Junhui 115597ba3b81SColy Li mutex_lock(&bch_register_lock); 115697ba3b81SColy Li 115746010141SShenghui Wang calc_cached_dev_sectors(dc->disk.c); 1158cafe5635SKent Overstreet bcache_device_detach(&dc->disk); 1159cafe5635SKent Overstreet list_move(&dc->list, &uncached_devices); 1160cafe5635SKent Overstreet 1161c4d951ddSKent Overstreet clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags); 11625b1016e6SKent Overstreet clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags); 1163c4d951ddSKent Overstreet 1164cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1165cafe5635SKent Overstreet 116646f5aa88SJoe Perches pr_info("Caching disabled for %s\n", dc->backing_dev_name); 1167cafe5635SKent Overstreet 1168cafe5635SKent Overstreet /* Drop ref we took in cached_dev_detach() */ 1169cafe5635SKent Overstreet closure_put(&dc->disk.cl); 1170cafe5635SKent Overstreet } 1171cafe5635SKent Overstreet 1172cafe5635SKent Overstreet void bch_cached_dev_detach(struct cached_dev *dc) 1173cafe5635SKent Overstreet { 1174cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 1175cafe5635SKent Overstreet 1176c4d951ddSKent Overstreet if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags)) 1177cafe5635SKent Overstreet return; 1178cafe5635SKent Overstreet 1179c4d951ddSKent Overstreet if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) 1180cafe5635SKent Overstreet return; 1181cafe5635SKent Overstreet 1182cafe5635SKent Overstreet /* 1183cafe5635SKent Overstreet * Block the device from being closed and freed until we're finished 1184cafe5635SKent Overstreet * detaching 1185cafe5635SKent Overstreet */ 1186cafe5635SKent Overstreet closure_get(&dc->disk.cl); 1187cafe5635SKent Overstreet 1188cafe5635SKent Overstreet bch_writeback_queue(dc); 11893fd47bfeSColy Li 1190cafe5635SKent Overstreet cached_dev_put(dc); 1191cafe5635SKent Overstreet } 1192cafe5635SKent Overstreet 119373ac105bSTang Junhui int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c, 119473ac105bSTang Junhui uint8_t *set_uuid) 1195cafe5635SKent Overstreet { 119675cbb3f1SArnd Bergmann uint32_t rtime = cpu_to_le32((u32)ktime_get_real_seconds()); 1197cafe5635SKent Overstreet struct uuid_entry *u; 119886755b7aSMichael Lyle struct cached_dev *exist_dc, *t; 11990b13efecSColy Li int ret = 0; 1200cafe5635SKent Overstreet 12011132e56eSColy Li if ((set_uuid && memcmp(set_uuid, c->set_uuid, 16)) || 12021132e56eSColy Li (!set_uuid && memcmp(dc->sb.set_uuid, c->set_uuid, 16))) 1203cafe5635SKent Overstreet return -ENOENT; 1204cafe5635SKent Overstreet 1205cafe5635SKent Overstreet if (dc->disk.c) { 120646f5aa88SJoe Perches pr_err("Can't attach %s: already attached\n", 12076e916a7eSColy Li dc->backing_dev_name); 1208cafe5635SKent Overstreet return -EINVAL; 1209cafe5635SKent Overstreet } 1210cafe5635SKent Overstreet 1211cafe5635SKent Overstreet if (test_bit(CACHE_SET_STOPPING, &c->flags)) { 121246f5aa88SJoe Perches pr_err("Can't attach %s: shutting down\n", 12136e916a7eSColy Li dc->backing_dev_name); 1214cafe5635SKent Overstreet return -EINVAL; 1215cafe5635SKent Overstreet } 1216cafe5635SKent Overstreet 12174a784266SColy Li if (dc->sb.block_size < c->cache->sb.block_size) { 1218cafe5635SKent Overstreet /* Will die */ 121946f5aa88SJoe Perches pr_err("Couldn't attach %s: block size less than set's block size\n", 12206e916a7eSColy Li dc->backing_dev_name); 1221cafe5635SKent Overstreet return -EINVAL; 1222cafe5635SKent Overstreet } 1223cafe5635SKent Overstreet 122486755b7aSMichael Lyle /* Check whether already attached */ 122586755b7aSMichael Lyle list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) { 122686755b7aSMichael Lyle if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) { 122746f5aa88SJoe Perches pr_err("Tried to attach %s but duplicate UUID already attached\n", 12286e916a7eSColy Li dc->backing_dev_name); 122986755b7aSMichael Lyle 123086755b7aSMichael Lyle return -EINVAL; 123186755b7aSMichael Lyle } 123286755b7aSMichael Lyle } 123386755b7aSMichael Lyle 1234cafe5635SKent Overstreet u = uuid_find(c, dc->sb.uuid); 1235cafe5635SKent Overstreet 1236cafe5635SKent Overstreet if (u && 1237cafe5635SKent Overstreet (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE || 1238cafe5635SKent Overstreet BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) { 1239cafe5635SKent Overstreet memcpy(u->uuid, invalid_uuid, 16); 124075cbb3f1SArnd Bergmann u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds()); 1241cafe5635SKent Overstreet u = NULL; 1242cafe5635SKent Overstreet } 1243cafe5635SKent Overstreet 1244cafe5635SKent Overstreet if (!u) { 1245cafe5635SKent Overstreet if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { 124646f5aa88SJoe Perches pr_err("Couldn't find uuid for %s in set\n", 12476e916a7eSColy Li dc->backing_dev_name); 1248cafe5635SKent Overstreet return -ENOENT; 1249cafe5635SKent Overstreet } 1250cafe5635SKent Overstreet 1251cafe5635SKent Overstreet u = uuid_find_empty(c); 1252cafe5635SKent Overstreet if (!u) { 125346f5aa88SJoe Perches pr_err("Not caching %s, no room for UUID\n", 12546e916a7eSColy Li dc->backing_dev_name); 1255cafe5635SKent Overstreet return -EINVAL; 1256cafe5635SKent Overstreet } 1257cafe5635SKent Overstreet } 1258cafe5635SKent Overstreet 12593be11dbaSColy Li /* 12603be11dbaSColy Li * Deadlocks since we're called via sysfs... 12613be11dbaSColy Li * sysfs_remove_file(&dc->kobj, &sysfs_attach); 1262cafe5635SKent Overstreet */ 1263cafe5635SKent Overstreet 1264169ef1cfSKent Overstreet if (bch_is_zero(u->uuid, 16)) { 1265cafe5635SKent Overstreet struct closure cl; 12661fae7cf0SColy Li 1267cafe5635SKent Overstreet closure_init_stack(&cl); 1268cafe5635SKent Overstreet 1269cafe5635SKent Overstreet memcpy(u->uuid, dc->sb.uuid, 16); 1270cafe5635SKent Overstreet memcpy(u->label, dc->sb.label, SB_LABEL_SIZE); 1271cafe5635SKent Overstreet u->first_reg = u->last_reg = rtime; 1272cafe5635SKent Overstreet bch_uuid_write(c); 1273cafe5635SKent Overstreet 12741132e56eSColy Li memcpy(dc->sb.set_uuid, c->set_uuid, 16); 1275cafe5635SKent Overstreet SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN); 1276cafe5635SKent Overstreet 1277cafe5635SKent Overstreet bch_write_bdev_super(dc, &cl); 1278cafe5635SKent Overstreet closure_sync(&cl); 1279cafe5635SKent Overstreet } else { 1280cafe5635SKent Overstreet u->last_reg = rtime; 1281cafe5635SKent Overstreet bch_uuid_write(c); 1282cafe5635SKent Overstreet } 1283cafe5635SKent Overstreet 1284cafe5635SKent Overstreet bcache_device_attach(&dc->disk, c, u - c->uuids); 1285cafe5635SKent Overstreet list_move(&dc->list, &c->cached_devs); 1286cafe5635SKent Overstreet calc_cached_dev_sectors(c); 1287cafe5635SKent Overstreet 1288cafe5635SKent Overstreet /* 1289cafe5635SKent Overstreet * dc->c must be set before dc->count != 0 - paired with the mb in 1290cafe5635SKent Overstreet * cached_dev_get() 1291cafe5635SKent Overstreet */ 1292eb2b3d03SColy Li smp_wmb(); 12933b304d24SElena Reshetova refcount_set(&dc->count, 1); 1294cafe5635SKent Overstreet 129507cc6ef8SEric Wheeler /* Block writeback thread, but spawn it */ 129607cc6ef8SEric Wheeler down_write(&dc->writeback_lock); 129707cc6ef8SEric Wheeler if (bch_cached_dev_writeback_start(dc)) { 129807cc6ef8SEric Wheeler up_write(&dc->writeback_lock); 129946f5aa88SJoe Perches pr_err("Couldn't start writeback facilities for %s\n", 1300633bb2ceSColy Li dc->disk.disk->disk_name); 13019e5c3535SSlava Pestov return -ENOMEM; 130207cc6ef8SEric Wheeler } 13039e5c3535SSlava Pestov 1304cafe5635SKent Overstreet if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { 1305cafe5635SKent Overstreet atomic_set(&dc->has_dirty, 1); 1306cafe5635SKent Overstreet bch_writeback_queue(dc); 1307cafe5635SKent Overstreet } 1308cafe5635SKent Overstreet 13092e17a262STang Junhui bch_sectors_dirty_init(&dc->disk); 13102e17a262STang Junhui 13110b13efecSColy Li ret = bch_cached_dev_run(dc); 13120b13efecSColy Li if (ret && (ret != -EBUSY)) { 13130b13efecSColy Li up_write(&dc->writeback_lock); 13145c2a634cSColy Li /* 13155c2a634cSColy Li * bch_register_lock is held, bcache_device_stop() is not 13165c2a634cSColy Li * able to be directly called. The kthread and kworker 13175c2a634cSColy Li * created previously in bch_cached_dev_writeback_start() 13185c2a634cSColy Li * have to be stopped manually here. 13195c2a634cSColy Li */ 13205c2a634cSColy Li kthread_stop(dc->writeback_thread); 13215c2a634cSColy Li cancel_writeback_rate_update_dwork(dc); 132246f5aa88SJoe Perches pr_err("Couldn't run cached device %s\n", 1323633bb2ceSColy Li dc->backing_dev_name); 13240b13efecSColy Li return ret; 13250b13efecSColy Li } 13260b13efecSColy Li 1327ee668506SKent Overstreet bcache_device_link(&dc->disk, c, "bdev"); 1328ea8c5356SColy Li atomic_inc(&c->attached_dev_nr); 1329cafe5635SKent Overstreet 13305342fd42SColy Li if (bch_has_feature_obso_large_bucket(&(c->cache->sb))) { 13315342fd42SColy Li pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n"); 13325342fd42SColy Li pr_err("Please update to the latest bcache-tools to create the cache device\n"); 13335342fd42SColy Li set_disk_ro(dc->disk.disk, 1); 13345342fd42SColy Li } 13355342fd42SColy Li 133607cc6ef8SEric Wheeler /* Allow the writeback thread to proceed */ 133707cc6ef8SEric Wheeler up_write(&dc->writeback_lock); 133807cc6ef8SEric Wheeler 133946f5aa88SJoe Perches pr_info("Caching %s as %s on set %pU\n", 13406e916a7eSColy Li dc->backing_dev_name, 13416e916a7eSColy Li dc->disk.disk->disk_name, 13421132e56eSColy Li dc->disk.c->set_uuid); 1343cafe5635SKent Overstreet return 0; 1344cafe5635SKent Overstreet } 1345cafe5635SKent Overstreet 13462d17456eSColy Li /* when dc->disk.kobj released */ 1347cafe5635SKent Overstreet void bch_cached_dev_release(struct kobject *kobj) 1348cafe5635SKent Overstreet { 1349cafe5635SKent Overstreet struct cached_dev *dc = container_of(kobj, struct cached_dev, 1350cafe5635SKent Overstreet disk.kobj); 1351cafe5635SKent Overstreet kfree(dc); 1352cafe5635SKent Overstreet module_put(THIS_MODULE); 1353cafe5635SKent Overstreet } 1354cafe5635SKent Overstreet 1355cafe5635SKent Overstreet static void cached_dev_free(struct closure *cl) 1356cafe5635SKent Overstreet { 1357cafe5635SKent Overstreet struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); 1358cafe5635SKent Overstreet 13593fd47bfeSColy Li if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)) 13603fd47bfeSColy Li cancel_writeback_rate_update_dwork(dc); 13613fd47bfeSColy Li 1362a664d0f0SSlava Pestov if (!IS_ERR_OR_NULL(dc->writeback_thread)) 13635e6926daSKent Overstreet kthread_stop(dc->writeback_thread); 13640f0709e6SColy Li if (!IS_ERR_OR_NULL(dc->status_update_thread)) 13650f0709e6SColy Li kthread_stop(dc->status_update_thread); 1366cafe5635SKent Overstreet 136780265d8dSColy Li mutex_lock(&bch_register_lock); 136880265d8dSColy Li 1369b75f4aedSChristoph Hellwig if (atomic_read(&dc->running)) { 1370ee668506SKent Overstreet bd_unlink_disk_holder(dc->bdev, dc->disk.disk); 1371b75f4aedSChristoph Hellwig del_gendisk(dc->disk.disk); 1372b75f4aedSChristoph Hellwig } 1373cafe5635SKent Overstreet bcache_device_free(&dc->disk); 1374cafe5635SKent Overstreet list_del(&dc->list); 1375cafe5635SKent Overstreet 1376cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1377cafe5635SKent Overstreet 1378475389aeSChristoph Hellwig if (dc->sb_disk) 1379475389aeSChristoph Hellwig put_page(virt_to_page(dc->sb_disk)); 1380e8547d42SLiang Chen 13810781c874SKent Overstreet if (!IS_ERR_OR_NULL(dc->bdev)) 1382cafe5635SKent Overstreet blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 1383cafe5635SKent Overstreet 1384cafe5635SKent Overstreet wake_up(&unregister_wait); 1385cafe5635SKent Overstreet 1386cafe5635SKent Overstreet kobject_put(&dc->disk.kobj); 1387cafe5635SKent Overstreet } 1388cafe5635SKent Overstreet 1389cafe5635SKent Overstreet static void cached_dev_flush(struct closure *cl) 1390cafe5635SKent Overstreet { 1391cafe5635SKent Overstreet struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); 1392cafe5635SKent Overstreet struct bcache_device *d = &dc->disk; 1393cafe5635SKent Overstreet 1394c9502ea4SKent Overstreet mutex_lock(&bch_register_lock); 1395c9502ea4SKent Overstreet bcache_device_unlink(d); 1396c9502ea4SKent Overstreet mutex_unlock(&bch_register_lock); 1397c9502ea4SKent Overstreet 1398cafe5635SKent Overstreet bch_cache_accounting_destroy(&dc->accounting); 1399cafe5635SKent Overstreet kobject_del(&d->kobj); 1400cafe5635SKent Overstreet 1401cafe5635SKent Overstreet continue_at(cl, cached_dev_free, system_wq); 1402cafe5635SKent Overstreet } 1403cafe5635SKent Overstreet 14046f10f7d1SColy Li static int cached_dev_init(struct cached_dev *dc, unsigned int block_size) 1405cafe5635SKent Overstreet { 1406f59fce84SKent Overstreet int ret; 1407cafe5635SKent Overstreet struct io *io; 1408f59fce84SKent Overstreet struct request_queue *q = bdev_get_queue(dc->bdev); 1409cafe5635SKent Overstreet 1410cafe5635SKent Overstreet __module_get(THIS_MODULE); 1411cafe5635SKent Overstreet INIT_LIST_HEAD(&dc->list); 1412f59fce84SKent Overstreet closure_init(&dc->disk.cl, NULL); 1413f59fce84SKent Overstreet set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq); 1414cafe5635SKent Overstreet kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype); 1415cafe5635SKent Overstreet INIT_WORK(&dc->detach, cached_dev_detach_finish); 1416cb7a583eSKent Overstreet sema_init(&dc->sb_write_mutex, 1); 1417f59fce84SKent Overstreet INIT_LIST_HEAD(&dc->io_lru); 1418f59fce84SKent Overstreet spin_lock_init(&dc->io_lock); 1419f59fce84SKent Overstreet bch_cache_accounting_init(&dc->accounting, &dc->disk.cl); 1420cafe5635SKent Overstreet 1421cafe5635SKent Overstreet dc->sequential_cutoff = 4 << 20; 1422cafe5635SKent Overstreet 1423cafe5635SKent Overstreet for (io = dc->io; io < dc->io + RECENT_IO; io++) { 1424cafe5635SKent Overstreet list_add(&io->lru, &dc->io_lru); 1425cafe5635SKent Overstreet hlist_add_head(&io->hash, dc->io_hash + RECENT_IO); 1426cafe5635SKent Overstreet } 1427cafe5635SKent Overstreet 1428c78afc62SKent Overstreet dc->disk.stripe_size = q->limits.io_opt >> 9; 1429c78afc62SKent Overstreet 1430c78afc62SKent Overstreet if (dc->disk.stripe_size) 1431c78afc62SKent Overstreet dc->partial_stripes_expensive = 1432c78afc62SKent Overstreet q->limits.raid_partial_stripes_expensive; 1433c78afc62SKent Overstreet 1434279afbadSKent Overstreet ret = bcache_device_init(&dc->disk, block_size, 1435a782483cSChristoph Hellwig bdev_nr_sectors(dc->bdev) - dc->sb.data_offset, 1436c62b37d9SChristoph Hellwig dc->bdev, &bcache_cached_ops); 1437f59fce84SKent Overstreet if (ret) 1438f59fce84SKent Overstreet return ret; 1439f59fce84SKent Overstreet 14405d4ce78bSChristoph Hellwig blk_queue_io_opt(dc->disk.disk->queue, 14415d4ce78bSChristoph Hellwig max(queue_io_opt(dc->disk.disk->queue), queue_io_opt(q))); 1442f59fce84SKent Overstreet 1443c7b7bd07SColy Li atomic_set(&dc->io_errors, 0); 1444c7b7bd07SColy Li dc->io_disable = false; 1445c7b7bd07SColy Li dc->error_limit = DEFAULT_CACHED_DEV_ERROR_LIMIT; 14467e027ca4SColy Li /* default to auto */ 14477e027ca4SColy Li dc->stop_when_cache_set_failed = BCH_CACHED_DEV_STOP_AUTO; 14487e027ca4SColy Li 1449f59fce84SKent Overstreet bch_cached_dev_request_init(dc); 1450f59fce84SKent Overstreet bch_cached_dev_writeback_init(dc); 1451cafe5635SKent Overstreet return 0; 1452cafe5635SKent Overstreet } 1453cafe5635SKent Overstreet 1454cafe5635SKent Overstreet /* Cached device - bcache superblock */ 1455cafe5635SKent Overstreet 1456cfa0c56dSChristoph Hellwig static int register_bdev(struct cache_sb *sb, struct cache_sb_disk *sb_disk, 1457cafe5635SKent Overstreet struct block_device *bdev, 1458cafe5635SKent Overstreet struct cached_dev *dc) 1459cafe5635SKent Overstreet { 1460cafe5635SKent Overstreet const char *err = "cannot allocate memory"; 1461cafe5635SKent Overstreet struct cache_set *c; 14620b13efecSColy Li int ret = -ENOMEM; 1463cafe5635SKent Overstreet 14646e916a7eSColy Li bdevname(bdev, dc->backing_dev_name); 1465cafe5635SKent Overstreet memcpy(&dc->sb, sb, sizeof(struct cache_sb)); 1466cafe5635SKent Overstreet dc->bdev = bdev; 1467cafe5635SKent Overstreet dc->bdev->bd_holder = dc; 1468475389aeSChristoph Hellwig dc->sb_disk = sb_disk; 14696e916a7eSColy Li 1470f59fce84SKent Overstreet if (cached_dev_init(dc, sb->block_size << 9)) 1471f59fce84SKent Overstreet goto err; 1472cafe5635SKent Overstreet 1473cafe5635SKent Overstreet err = "error creating kobject"; 14748d65269fSChristoph Hellwig if (kobject_add(&dc->disk.kobj, bdev_kobj(bdev), "bcache")) 1475cafe5635SKent Overstreet goto err; 1476cafe5635SKent Overstreet if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj)) 1477cafe5635SKent Overstreet goto err; 1478cafe5635SKent Overstreet 147946f5aa88SJoe Perches pr_info("registered backing device %s\n", dc->backing_dev_name); 1480f59fce84SKent Overstreet 1481cafe5635SKent Overstreet list_add(&dc->list, &uncached_devices); 1482e57fd746SColy Li /* attach to a matched cache set if it exists */ 1483cafe5635SKent Overstreet list_for_each_entry(c, &bch_cache_sets, list) 148473ac105bSTang Junhui bch_cached_dev_attach(dc, c, NULL); 1485cafe5635SKent Overstreet 1486cafe5635SKent Overstreet if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE || 14870b13efecSColy Li BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) { 14880b13efecSColy Li err = "failed to run cached device"; 14890b13efecSColy Li ret = bch_cached_dev_run(dc); 14900b13efecSColy Li if (ret) 14910b13efecSColy Li goto err; 14920b13efecSColy Li } 1493cafe5635SKent Overstreet 149488c12d42SColy Li return 0; 1495cafe5635SKent Overstreet err: 149646f5aa88SJoe Perches pr_notice("error %s: %s\n", dc->backing_dev_name, err); 1497f59fce84SKent Overstreet bcache_device_stop(&dc->disk); 14980b13efecSColy Li return ret; 1499cafe5635SKent Overstreet } 1500cafe5635SKent Overstreet 1501cafe5635SKent Overstreet /* Flash only volumes */ 1502cafe5635SKent Overstreet 15032d17456eSColy Li /* When d->kobj released */ 1504cafe5635SKent Overstreet void bch_flash_dev_release(struct kobject *kobj) 1505cafe5635SKent Overstreet { 1506cafe5635SKent Overstreet struct bcache_device *d = container_of(kobj, struct bcache_device, 1507cafe5635SKent Overstreet kobj); 1508cafe5635SKent Overstreet kfree(d); 1509cafe5635SKent Overstreet } 1510cafe5635SKent Overstreet 1511cafe5635SKent Overstreet static void flash_dev_free(struct closure *cl) 1512cafe5635SKent Overstreet { 1513cafe5635SKent Overstreet struct bcache_device *d = container_of(cl, struct bcache_device, cl); 15141fae7cf0SColy Li 1515e5112201SSlava Pestov mutex_lock(&bch_register_lock); 151699a27d59STang Junhui atomic_long_sub(bcache_dev_sectors_dirty(d), 151799a27d59STang Junhui &d->c->flash_dev_dirty_sectors); 1518b75f4aedSChristoph Hellwig del_gendisk(d->disk); 1519cafe5635SKent Overstreet bcache_device_free(d); 1520e5112201SSlava Pestov mutex_unlock(&bch_register_lock); 1521cafe5635SKent Overstreet kobject_put(&d->kobj); 1522cafe5635SKent Overstreet } 1523cafe5635SKent Overstreet 1524cafe5635SKent Overstreet static void flash_dev_flush(struct closure *cl) 1525cafe5635SKent Overstreet { 1526cafe5635SKent Overstreet struct bcache_device *d = container_of(cl, struct bcache_device, cl); 1527cafe5635SKent Overstreet 1528e5112201SSlava Pestov mutex_lock(&bch_register_lock); 1529ee668506SKent Overstreet bcache_device_unlink(d); 1530e5112201SSlava Pestov mutex_unlock(&bch_register_lock); 1531cafe5635SKent Overstreet kobject_del(&d->kobj); 1532cafe5635SKent Overstreet continue_at(cl, flash_dev_free, system_wq); 1533cafe5635SKent Overstreet } 1534cafe5635SKent Overstreet 1535cafe5635SKent Overstreet static int flash_dev_run(struct cache_set *c, struct uuid_entry *u) 1536cafe5635SKent Overstreet { 1537cafe5635SKent Overstreet struct bcache_device *d = kzalloc(sizeof(struct bcache_device), 1538cafe5635SKent Overstreet GFP_KERNEL); 1539cafe5635SKent Overstreet if (!d) 1540cafe5635SKent Overstreet return -ENOMEM; 1541cafe5635SKent Overstreet 1542cafe5635SKent Overstreet closure_init(&d->cl, NULL); 1543cafe5635SKent Overstreet set_closure_fn(&d->cl, flash_dev_flush, system_wq); 1544cafe5635SKent Overstreet 1545cafe5635SKent Overstreet kobject_init(&d->kobj, &bch_flash_dev_ktype); 1546cafe5635SKent Overstreet 15474e1ebae3SColy Li if (bcache_device_init(d, block_bytes(c->cache), u->sectors, 1548c62b37d9SChristoph Hellwig NULL, &bcache_flash_ops)) 1549cafe5635SKent Overstreet goto err; 1550cafe5635SKent Overstreet 1551cafe5635SKent Overstreet bcache_device_attach(d, c, u - c->uuids); 1552175206cfSTang Junhui bch_sectors_dirty_init(d); 1553cafe5635SKent Overstreet bch_flash_dev_request_init(d); 1554cafe5635SKent Overstreet add_disk(d->disk); 1555cafe5635SKent Overstreet 1556cafe5635SKent Overstreet if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache")) 1557cafe5635SKent Overstreet goto err; 1558cafe5635SKent Overstreet 1559cafe5635SKent Overstreet bcache_device_link(d, c, "volume"); 1560cafe5635SKent Overstreet 15615342fd42SColy Li if (bch_has_feature_obso_large_bucket(&c->cache->sb)) { 15625342fd42SColy Li pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n"); 15635342fd42SColy Li pr_err("Please update to the latest bcache-tools to create the cache device\n"); 15645342fd42SColy Li set_disk_ro(d->disk, 1); 15655342fd42SColy Li } 15665342fd42SColy Li 1567cafe5635SKent Overstreet return 0; 1568cafe5635SKent Overstreet err: 1569cafe5635SKent Overstreet kobject_put(&d->kobj); 1570cafe5635SKent Overstreet return -ENOMEM; 1571cafe5635SKent Overstreet } 1572cafe5635SKent Overstreet 1573cafe5635SKent Overstreet static int flash_devs_run(struct cache_set *c) 1574cafe5635SKent Overstreet { 1575cafe5635SKent Overstreet int ret = 0; 1576cafe5635SKent Overstreet struct uuid_entry *u; 1577cafe5635SKent Overstreet 1578cafe5635SKent Overstreet for (u = c->uuids; 157902aa8a8bSColy Li u < c->uuids + c->nr_uuids && !ret; 1580cafe5635SKent Overstreet u++) 1581cafe5635SKent Overstreet if (UUID_FLASH_ONLY(u)) 1582cafe5635SKent Overstreet ret = flash_dev_run(c, u); 1583cafe5635SKent Overstreet 1584cafe5635SKent Overstreet return ret; 1585cafe5635SKent Overstreet } 1586cafe5635SKent Overstreet 1587cafe5635SKent Overstreet int bch_flash_dev_create(struct cache_set *c, uint64_t size) 1588cafe5635SKent Overstreet { 1589cafe5635SKent Overstreet struct uuid_entry *u; 1590cafe5635SKent Overstreet 1591cafe5635SKent Overstreet if (test_bit(CACHE_SET_STOPPING, &c->flags)) 1592cafe5635SKent Overstreet return -EINTR; 1593cafe5635SKent Overstreet 1594bf0c55c9SSlava Pestov if (!test_bit(CACHE_SET_RUNNING, &c->flags)) 1595bf0c55c9SSlava Pestov return -EPERM; 1596bf0c55c9SSlava Pestov 1597cafe5635SKent Overstreet u = uuid_find_empty(c); 1598cafe5635SKent Overstreet if (!u) { 159946f5aa88SJoe Perches pr_err("Can't create volume, no room for UUID\n"); 1600cafe5635SKent Overstreet return -EINVAL; 1601cafe5635SKent Overstreet } 1602cafe5635SKent Overstreet 1603cafe5635SKent Overstreet get_random_bytes(u->uuid, 16); 1604cafe5635SKent Overstreet memset(u->label, 0, 32); 160575cbb3f1SArnd Bergmann u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds()); 1606cafe5635SKent Overstreet 1607cafe5635SKent Overstreet SET_UUID_FLASH_ONLY(u, 1); 1608cafe5635SKent Overstreet u->sectors = size >> 9; 1609cafe5635SKent Overstreet 1610cafe5635SKent Overstreet bch_uuid_write(c); 1611cafe5635SKent Overstreet 1612cafe5635SKent Overstreet return flash_dev_run(c, u); 1613cafe5635SKent Overstreet } 1614cafe5635SKent Overstreet 1615c7b7bd07SColy Li bool bch_cached_dev_error(struct cached_dev *dc) 1616c7b7bd07SColy Li { 1617c7b7bd07SColy Li if (!dc || test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags)) 1618c7b7bd07SColy Li return false; 1619c7b7bd07SColy Li 1620c7b7bd07SColy Li dc->io_disable = true; 1621c7b7bd07SColy Li /* make others know io_disable is true earlier */ 1622c7b7bd07SColy Li smp_mb(); 1623c7b7bd07SColy Li 1624c7b7bd07SColy Li pr_err("stop %s: too many IO errors on backing device %s\n", 16256e916a7eSColy Li dc->disk.disk->disk_name, dc->backing_dev_name); 1626c7b7bd07SColy Li 1627c7b7bd07SColy Li bcache_device_stop(&dc->disk); 1628c7b7bd07SColy Li return true; 1629c7b7bd07SColy Li } 1630c7b7bd07SColy Li 1631cafe5635SKent Overstreet /* Cache set */ 1632cafe5635SKent Overstreet 1633cafe5635SKent Overstreet __printf(2, 3) 1634cafe5635SKent Overstreet bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...) 1635cafe5635SKent Overstreet { 163646f5aa88SJoe Perches struct va_format vaf; 1637cafe5635SKent Overstreet va_list args; 1638cafe5635SKent Overstreet 163977c320ebSKent Overstreet if (c->on_error != ON_ERROR_PANIC && 164077c320ebSKent Overstreet test_bit(CACHE_SET_STOPPING, &c->flags)) 1641cafe5635SKent Overstreet return false; 1642cafe5635SKent Overstreet 1643771f393eSColy Li if (test_and_set_bit(CACHE_SET_IO_DISABLE, &c->flags)) 164446f5aa88SJoe Perches pr_info("CACHE_SET_IO_DISABLE already set\n"); 1645771f393eSColy Li 16463be11dbaSColy Li /* 16473be11dbaSColy Li * XXX: we can be called from atomic context 16483be11dbaSColy Li * acquire_console_sem(); 1649cafe5635SKent Overstreet */ 1650cafe5635SKent Overstreet 1651cafe5635SKent Overstreet va_start(args, fmt); 1652cafe5635SKent Overstreet 165346f5aa88SJoe Perches vaf.fmt = fmt; 165446f5aa88SJoe Perches vaf.va = &args; 165546f5aa88SJoe Perches 165646f5aa88SJoe Perches pr_err("error on %pU: %pV, disabling caching\n", 16571132e56eSColy Li c->set_uuid, &vaf); 165846f5aa88SJoe Perches 165946f5aa88SJoe Perches va_end(args); 1660cafe5635SKent Overstreet 166177c320ebSKent Overstreet if (c->on_error == ON_ERROR_PANIC) 166277c320ebSKent Overstreet panic("panic forced after error\n"); 166377c320ebSKent Overstreet 1664cafe5635SKent Overstreet bch_cache_set_unregister(c); 1665cafe5635SKent Overstreet return true; 1666cafe5635SKent Overstreet } 1667cafe5635SKent Overstreet 16682d17456eSColy Li /* When c->kobj released */ 1669cafe5635SKent Overstreet void bch_cache_set_release(struct kobject *kobj) 1670cafe5635SKent Overstreet { 1671cafe5635SKent Overstreet struct cache_set *c = container_of(kobj, struct cache_set, kobj); 16721fae7cf0SColy Li 1673cafe5635SKent Overstreet kfree(c); 1674cafe5635SKent Overstreet module_put(THIS_MODULE); 1675cafe5635SKent Overstreet } 1676cafe5635SKent Overstreet 1677cafe5635SKent Overstreet static void cache_set_free(struct closure *cl) 1678cafe5635SKent Overstreet { 1679cafe5635SKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, cl); 1680cafe5635SKent Overstreet struct cache *ca; 1681cafe5635SKent Overstreet 1682cafe5635SKent Overstreet debugfs_remove(c->debug); 1683cafe5635SKent Overstreet 1684cafe5635SKent Overstreet bch_open_buckets_free(c); 1685cafe5635SKent Overstreet bch_btree_cache_free(c); 1686cafe5635SKent Overstreet bch_journal_free(c); 1687cafe5635SKent Overstreet 1688a4b732a2SLiang Chen mutex_lock(&bch_register_lock); 16894a784266SColy Li bch_bset_sort_state_free(&c->sort); 16904a784266SColy Li free_pages((unsigned long) c->uuids, ilog2(meta_bucket_pages(&c->cache->sb))); 16914a784266SColy Li 169208fdb2cdSColy Li ca = c->cache; 1693c9a78332SSlava Pestov if (ca) { 1694c9a78332SSlava Pestov ca->set = NULL; 1695697e2349SColy Li c->cache = NULL; 1696cafe5635SKent Overstreet kobject_put(&ca->kobj); 1697c9a78332SSlava Pestov } 1698cafe5635SKent Overstreet 1699cafe5635SKent Overstreet 1700da415a09SNicholas Swenson if (c->moving_gc_wq) 1701da415a09SNicholas Swenson destroy_workqueue(c->moving_gc_wq); 1702d19936a2SKent Overstreet bioset_exit(&c->bio_split); 1703d19936a2SKent Overstreet mempool_exit(&c->fill_iter); 1704d19936a2SKent Overstreet mempool_exit(&c->bio_meta); 1705d19936a2SKent Overstreet mempool_exit(&c->search); 1706cafe5635SKent Overstreet kfree(c->devices); 1707cafe5635SKent Overstreet 1708cafe5635SKent Overstreet list_del(&c->list); 1709cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1710cafe5635SKent Overstreet 17111132e56eSColy Li pr_info("Cache set %pU unregistered\n", c->set_uuid); 1712cafe5635SKent Overstreet wake_up(&unregister_wait); 1713cafe5635SKent Overstreet 1714cafe5635SKent Overstreet closure_debug_destroy(&c->cl); 1715cafe5635SKent Overstreet kobject_put(&c->kobj); 1716cafe5635SKent Overstreet } 1717cafe5635SKent Overstreet 1718cafe5635SKent Overstreet static void cache_set_flush(struct closure *cl) 1719cafe5635SKent Overstreet { 1720cafe5635SKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, caching); 172108fdb2cdSColy Li struct cache *ca = c->cache; 1722cafe5635SKent Overstreet struct btree *b; 1723cafe5635SKent Overstreet 1724cafe5635SKent Overstreet bch_cache_accounting_destroy(&c->accounting); 1725cafe5635SKent Overstreet 1726cafe5635SKent Overstreet kobject_put(&c->internal); 1727cafe5635SKent Overstreet kobject_del(&c->kobj); 1728cafe5635SKent Overstreet 1729b387e9b5SColy Li if (!IS_ERR_OR_NULL(c->gc_thread)) 173072a44517SKent Overstreet kthread_stop(c->gc_thread); 173172a44517SKent Overstreet 1732cafe5635SKent Overstreet if (!IS_ERR_OR_NULL(c->root)) 1733cafe5635SKent Overstreet list_add(&c->root->list, &c->btree_cache); 1734cafe5635SKent Overstreet 1735e6dcbd3eSColy Li /* 1736e6dcbd3eSColy Li * Avoid flushing cached nodes if cache set is retiring 1737e6dcbd3eSColy Li * due to too many I/O errors detected. 1738e6dcbd3eSColy Li */ 1739e6dcbd3eSColy Li if (!test_bit(CACHE_SET_IO_DISABLE, &c->flags)) 17402a285686SKent Overstreet list_for_each_entry(b, &c->btree_cache, list) { 17412a285686SKent Overstreet mutex_lock(&b->write_lock); 1742cafe5635SKent Overstreet if (btree_node_dirty(b)) 17432a285686SKent Overstreet __bch_btree_node_write(b, NULL); 17442a285686SKent Overstreet mutex_unlock(&b->write_lock); 17452a285686SKent Overstreet } 1746cafe5635SKent Overstreet 174779826c35SKent Overstreet if (ca->alloc_thread) 174879826c35SKent Overstreet kthread_stop(ca->alloc_thread); 174979826c35SKent Overstreet 17505b1016e6SKent Overstreet if (c->journal.cur) { 1751dabb4433SKent Overstreet cancel_delayed_work_sync(&c->journal.work); 1752dabb4433SKent Overstreet /* flush last journal entry if needed */ 1753dabb4433SKent Overstreet c->journal.work.work.func(&c->journal.work.work); 17545b1016e6SKent Overstreet } 1755dabb4433SKent Overstreet 1756cafe5635SKent Overstreet closure_return(cl); 1757cafe5635SKent Overstreet } 1758cafe5635SKent Overstreet 17597e027ca4SColy Li /* 17607e027ca4SColy Li * This function is only called when CACHE_SET_IO_DISABLE is set, which means 17617e027ca4SColy Li * cache set is unregistering due to too many I/O errors. In this condition, 17627e027ca4SColy Li * the bcache device might be stopped, it depends on stop_when_cache_set_failed 17637e027ca4SColy Li * value and whether the broken cache has dirty data: 17647e027ca4SColy Li * 17657e027ca4SColy Li * dc->stop_when_cache_set_failed dc->has_dirty stop bcache device 17667e027ca4SColy Li * BCH_CACHED_STOP_AUTO 0 NO 17677e027ca4SColy Li * BCH_CACHED_STOP_AUTO 1 YES 17687e027ca4SColy Li * BCH_CACHED_DEV_STOP_ALWAYS 0 YES 17697e027ca4SColy Li * BCH_CACHED_DEV_STOP_ALWAYS 1 YES 17707e027ca4SColy Li * 17717e027ca4SColy Li * The expected behavior is, if stop_when_cache_set_failed is configured to 17727e027ca4SColy Li * "auto" via sysfs interface, the bcache device will not be stopped if the 17737e027ca4SColy Li * backing device is clean on the broken cache device. 17747e027ca4SColy Li */ 17757e027ca4SColy Li static void conditional_stop_bcache_device(struct cache_set *c, 17767e027ca4SColy Li struct bcache_device *d, 17777e027ca4SColy Li struct cached_dev *dc) 17787e027ca4SColy Li { 17797e027ca4SColy Li if (dc->stop_when_cache_set_failed == BCH_CACHED_DEV_STOP_ALWAYS) { 178046f5aa88SJoe Perches pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.\n", 17811132e56eSColy Li d->disk->disk_name, c->set_uuid); 17827e027ca4SColy Li bcache_device_stop(d); 17837e027ca4SColy Li } else if (atomic_read(&dc->has_dirty)) { 17847e027ca4SColy Li /* 17857e027ca4SColy Li * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO 17867e027ca4SColy Li * and dc->has_dirty == 1 17877e027ca4SColy Li */ 178846f5aa88SJoe Perches pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.\n", 17897e027ca4SColy Li d->disk->disk_name); 17904fd8e138SColy Li /* 17914fd8e138SColy Li * There might be a small time gap that cache set is 17924fd8e138SColy Li * released but bcache device is not. Inside this time 17934fd8e138SColy Li * gap, regular I/O requests will directly go into 17944fd8e138SColy Li * backing device as no cache set attached to. This 17954fd8e138SColy Li * behavior may also introduce potential inconsistence 17964fd8e138SColy Li * data in writeback mode while cache is dirty. 17974fd8e138SColy Li * Therefore before calling bcache_device_stop() due 17984fd8e138SColy Li * to a broken cache device, dc->io_disable should be 17994fd8e138SColy Li * explicitly set to true. 18004fd8e138SColy Li */ 18014fd8e138SColy Li dc->io_disable = true; 18024fd8e138SColy Li /* make others know io_disable is true earlier */ 18034fd8e138SColy Li smp_mb(); 18047e027ca4SColy Li bcache_device_stop(d); 18057e027ca4SColy Li } else { 18067e027ca4SColy Li /* 18077e027ca4SColy Li * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO 18087e027ca4SColy Li * and dc->has_dirty == 0 18097e027ca4SColy Li */ 181046f5aa88SJoe Perches pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.\n", 18117e027ca4SColy Li d->disk->disk_name); 18127e027ca4SColy Li } 18137e027ca4SColy Li } 18147e027ca4SColy Li 1815cafe5635SKent Overstreet static void __cache_set_unregister(struct closure *cl) 1816cafe5635SKent Overstreet { 1817cafe5635SKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, caching); 18185caa52afSKent Overstreet struct cached_dev *dc; 18197e027ca4SColy Li struct bcache_device *d; 1820cafe5635SKent Overstreet size_t i; 1821cafe5635SKent Overstreet 1822cafe5635SKent Overstreet mutex_lock(&bch_register_lock); 1823cafe5635SKent Overstreet 18247e027ca4SColy Li for (i = 0; i < c->devices_max_used; i++) { 18257e027ca4SColy Li d = c->devices[i]; 18267e027ca4SColy Li if (!d) 18277e027ca4SColy Li continue; 18287e027ca4SColy Li 18295caa52afSKent Overstreet if (!UUID_FLASH_ONLY(&c->uuids[i]) && 18305caa52afSKent Overstreet test_bit(CACHE_SET_UNREGISTERING, &c->flags)) { 18317e027ca4SColy Li dc = container_of(d, struct cached_dev, disk); 18325caa52afSKent Overstreet bch_cached_dev_detach(dc); 18337e027ca4SColy Li if (test_bit(CACHE_SET_IO_DISABLE, &c->flags)) 18347e027ca4SColy Li conditional_stop_bcache_device(c, d, dc); 18355caa52afSKent Overstreet } else { 18367e027ca4SColy Li bcache_device_stop(d); 18375caa52afSKent Overstreet } 18385caa52afSKent Overstreet } 1839cafe5635SKent Overstreet 1840cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1841cafe5635SKent Overstreet 1842cafe5635SKent Overstreet continue_at(cl, cache_set_flush, system_wq); 1843cafe5635SKent Overstreet } 1844cafe5635SKent Overstreet 1845cafe5635SKent Overstreet void bch_cache_set_stop(struct cache_set *c) 1846cafe5635SKent Overstreet { 1847cafe5635SKent Overstreet if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags)) 184863d63b51SColy Li /* closure_fn set to __cache_set_unregister() */ 1849cafe5635SKent Overstreet closure_queue(&c->caching); 1850cafe5635SKent Overstreet } 1851cafe5635SKent Overstreet 1852cafe5635SKent Overstreet void bch_cache_set_unregister(struct cache_set *c) 1853cafe5635SKent Overstreet { 1854cafe5635SKent Overstreet set_bit(CACHE_SET_UNREGISTERING, &c->flags); 1855cafe5635SKent Overstreet bch_cache_set_stop(c); 1856cafe5635SKent Overstreet } 1857cafe5635SKent Overstreet 1858de1fafabSColy Li #define alloc_meta_bucket_pages(gfp, sb) \ 1859de1fafabSColy Li ((void *) __get_free_pages(__GFP_ZERO|__GFP_COMP|gfp, ilog2(meta_bucket_pages(sb)))) 1860cafe5635SKent Overstreet 1861cafe5635SKent Overstreet struct cache_set *bch_cache_set_alloc(struct cache_sb *sb) 1862cafe5635SKent Overstreet { 1863cafe5635SKent Overstreet int iter_size; 18644a784266SColy Li struct cache *ca = container_of(sb, struct cache, sb); 1865cafe5635SKent Overstreet struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL); 18661fae7cf0SColy Li 1867cafe5635SKent Overstreet if (!c) 1868cafe5635SKent Overstreet return NULL; 1869cafe5635SKent Overstreet 1870cafe5635SKent Overstreet __module_get(THIS_MODULE); 1871cafe5635SKent Overstreet closure_init(&c->cl, NULL); 1872cafe5635SKent Overstreet set_closure_fn(&c->cl, cache_set_free, system_wq); 1873cafe5635SKent Overstreet 1874cafe5635SKent Overstreet closure_init(&c->caching, &c->cl); 1875cafe5635SKent Overstreet set_closure_fn(&c->caching, __cache_set_unregister, system_wq); 1876cafe5635SKent Overstreet 1877cafe5635SKent Overstreet /* Maybe create continue_at_noreturn() and use it here? */ 1878cafe5635SKent Overstreet closure_set_stopped(&c->cl); 1879cafe5635SKent Overstreet closure_put(&c->cl); 1880cafe5635SKent Overstreet 1881cafe5635SKent Overstreet kobject_init(&c->kobj, &bch_cache_set_ktype); 1882cafe5635SKent Overstreet kobject_init(&c->internal, &bch_cache_set_internal_ktype); 1883cafe5635SKent Overstreet 1884cafe5635SKent Overstreet bch_cache_accounting_init(&c->accounting, &c->cl); 1885cafe5635SKent Overstreet 18861132e56eSColy Li memcpy(c->set_uuid, sb->set_uuid, 16); 1887d721a43fSColy Li 18884a784266SColy Li c->cache = ca; 18894a784266SColy Li c->cache->set = c; 1890cafe5635SKent Overstreet c->bucket_bits = ilog2(sb->bucket_size); 1891cafe5635SKent Overstreet c->block_bits = ilog2(sb->block_size); 18924a784266SColy Li c->nr_uuids = meta_bucket_bytes(sb) / sizeof(struct uuid_entry); 18932831231dSColy Li c->devices_max_used = 0; 1894ea8c5356SColy Li atomic_set(&c->attached_dev_nr, 0); 18954a784266SColy Li c->btree_pages = meta_bucket_pages(sb); 1896cafe5635SKent Overstreet if (c->btree_pages > BTREE_MAX_PAGES) 1897cafe5635SKent Overstreet c->btree_pages = max_t(int, c->btree_pages / 4, 1898cafe5635SKent Overstreet BTREE_MAX_PAGES); 1899cafe5635SKent Overstreet 1900cb7a583eSKent Overstreet sema_init(&c->sb_write_mutex, 1); 1901e8e1d468SKent Overstreet mutex_init(&c->bucket_lock); 19020a63b66dSKent Overstreet init_waitqueue_head(&c->btree_cache_wait); 190334cf78bfSGuoju Fang spin_lock_init(&c->btree_cannibalize_lock); 190435fcd848SKent Overstreet init_waitqueue_head(&c->bucket_wait); 1905be628be0SKent Overstreet init_waitqueue_head(&c->gc_wait); 1906cb7a583eSKent Overstreet sema_init(&c->uuid_write_mutex, 1); 190765d22e91SKent Overstreet 190865d22e91SKent Overstreet spin_lock_init(&c->btree_gc_time.lock); 190965d22e91SKent Overstreet spin_lock_init(&c->btree_split_time.lock); 191065d22e91SKent Overstreet spin_lock_init(&c->btree_read_time.lock); 1911e8e1d468SKent Overstreet 1912cafe5635SKent Overstreet bch_moving_init_cache_set(c); 1913cafe5635SKent Overstreet 1914cafe5635SKent Overstreet INIT_LIST_HEAD(&c->list); 1915cafe5635SKent Overstreet INIT_LIST_HEAD(&c->cached_devs); 1916cafe5635SKent Overstreet INIT_LIST_HEAD(&c->btree_cache); 1917cafe5635SKent Overstreet INIT_LIST_HEAD(&c->btree_cache_freeable); 1918cafe5635SKent Overstreet INIT_LIST_HEAD(&c->btree_cache_freed); 1919cafe5635SKent Overstreet INIT_LIST_HEAD(&c->data_buckets); 1920cafe5635SKent Overstreet 19216907dc49SColy Li iter_size = ((meta_bucket_pages(sb) * PAGE_SECTORS) / sb->block_size + 1) * 1922cafe5635SKent Overstreet sizeof(struct btree_iter_set); 1923cafe5635SKent Overstreet 1924a42d3c64SColy Li c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL); 1925a42d3c64SColy Li if (!c->devices) 1926a42d3c64SColy Li goto err; 1927a42d3c64SColy Li 1928a42d3c64SColy Li if (mempool_init_slab_pool(&c->search, 32, bch_search_cache)) 1929a42d3c64SColy Li goto err; 1930a42d3c64SColy Li 1931a42d3c64SColy Li if (mempool_init_kmalloc_pool(&c->bio_meta, 2, 1932a42d3c64SColy Li sizeof(struct bbio) + 19334a784266SColy Li sizeof(struct bio_vec) * meta_bucket_pages(sb))) 1934a42d3c64SColy Li goto err; 1935a42d3c64SColy Li 1936a42d3c64SColy Li if (mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size)) 1937a42d3c64SColy Li goto err; 1938a42d3c64SColy Li 1939a42d3c64SColy Li if (bioset_init(&c->bio_split, 4, offsetof(struct bbio, bio), 1940faa8e2c4SMing Lei BIOSET_NEED_RESCUER)) 1941a42d3c64SColy Li goto err; 1942a42d3c64SColy Li 19434a784266SColy Li c->uuids = alloc_meta_bucket_pages(GFP_KERNEL, sb); 1944a42d3c64SColy Li if (!c->uuids) 1945a42d3c64SColy Li goto err; 1946a42d3c64SColy Li 1947a42d3c64SColy Li c->moving_gc_wq = alloc_workqueue("bcache_gc", WQ_MEM_RECLAIM, 0); 1948a42d3c64SColy Li if (!c->moving_gc_wq) 1949a42d3c64SColy Li goto err; 1950a42d3c64SColy Li 1951a42d3c64SColy Li if (bch_journal_alloc(c)) 1952a42d3c64SColy Li goto err; 1953a42d3c64SColy Li 1954a42d3c64SColy Li if (bch_btree_cache_alloc(c)) 1955a42d3c64SColy Li goto err; 1956a42d3c64SColy Li 1957a42d3c64SColy Li if (bch_open_buckets_alloc(c)) 1958a42d3c64SColy Li goto err; 1959a42d3c64SColy Li 1960a42d3c64SColy Li if (bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages))) 1961cafe5635SKent Overstreet goto err; 1962cafe5635SKent Overstreet 1963cafe5635SKent Overstreet c->congested_read_threshold_us = 2000; 1964cafe5635SKent Overstreet c->congested_write_threshold_us = 20000; 19657ba0d830SColy Li c->error_limit = DEFAULT_IO_ERROR_LIMIT; 1966c5fcdedcSColy Li c->idle_max_writeback_rate_enabled = 1; 1967771f393eSColy Li WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE, &c->flags)); 1968cafe5635SKent Overstreet 1969cafe5635SKent Overstreet return c; 1970cafe5635SKent Overstreet err: 1971cafe5635SKent Overstreet bch_cache_set_unregister(c); 1972cafe5635SKent Overstreet return NULL; 1973cafe5635SKent Overstreet } 1974cafe5635SKent Overstreet 1975ce3e4cfbSColy Li static int run_cache_set(struct cache_set *c) 1976cafe5635SKent Overstreet { 1977cafe5635SKent Overstreet const char *err = "cannot allocate memory"; 1978cafe5635SKent Overstreet struct cached_dev *dc, *t; 197908fdb2cdSColy Li struct cache *ca = c->cache; 1980c18536a7SKent Overstreet struct closure cl; 198195f18c9dSShenghui Wang LIST_HEAD(journal); 198295f18c9dSShenghui Wang struct journal_replay *l; 1983cafe5635SKent Overstreet 1984c18536a7SKent Overstreet closure_init_stack(&cl); 1985cafe5635SKent Overstreet 198608fdb2cdSColy Li c->nbuckets = ca->sb.nbuckets; 1987be628be0SKent Overstreet set_gc_sectors(c); 1988cafe5635SKent Overstreet 19896f9414e0SColy Li if (CACHE_SYNC(&c->cache->sb)) { 1990cafe5635SKent Overstreet struct bkey *k; 1991cafe5635SKent Overstreet struct jset *j; 1992cafe5635SKent Overstreet 1993cafe5635SKent Overstreet err = "cannot allocate memory for journal"; 1994c18536a7SKent Overstreet if (bch_journal_read(c, &journal)) 1995cafe5635SKent Overstreet goto err; 1996cafe5635SKent Overstreet 199746f5aa88SJoe Perches pr_debug("btree_journal_read() done\n"); 1998cafe5635SKent Overstreet 1999cafe5635SKent Overstreet err = "no journal entries found"; 2000cafe5635SKent Overstreet if (list_empty(&journal)) 2001cafe5635SKent Overstreet goto err; 2002cafe5635SKent Overstreet 2003cafe5635SKent Overstreet j = &list_entry(journal.prev, struct journal_replay, list)->j; 2004cafe5635SKent Overstreet 2005cafe5635SKent Overstreet err = "IO error reading priorities"; 200649d08d59SColy Li if (prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev])) 200749d08d59SColy Li goto err; 2008cafe5635SKent Overstreet 2009cafe5635SKent Overstreet /* 2010cafe5635SKent Overstreet * If prio_read() fails it'll call cache_set_error and we'll 2011cafe5635SKent Overstreet * tear everything down right away, but if we perhaps checked 2012cafe5635SKent Overstreet * sooner we could avoid journal replay. 2013cafe5635SKent Overstreet */ 2014cafe5635SKent Overstreet 2015cafe5635SKent Overstreet k = &j->btree_root; 2016cafe5635SKent Overstreet 2017cafe5635SKent Overstreet err = "bad btree root"; 201865d45231SKent Overstreet if (__bch_btree_ptr_invalid(c, k)) 2019cafe5635SKent Overstreet goto err; 2020cafe5635SKent Overstreet 2021cafe5635SKent Overstreet err = "error reading btree root"; 2022b0d30981SColy Li c->root = bch_btree_node_get(c, NULL, k, 2023b0d30981SColy Li j->btree_level, 2024b0d30981SColy Li true, NULL); 2025cafe5635SKent Overstreet if (IS_ERR_OR_NULL(c->root)) 2026cafe5635SKent Overstreet goto err; 2027cafe5635SKent Overstreet 2028cafe5635SKent Overstreet list_del_init(&c->root->list); 2029cafe5635SKent Overstreet rw_unlock(true, c->root); 2030cafe5635SKent Overstreet 2031c18536a7SKent Overstreet err = uuid_read(c, j, &cl); 2032cafe5635SKent Overstreet if (err) 2033cafe5635SKent Overstreet goto err; 2034cafe5635SKent Overstreet 2035cafe5635SKent Overstreet err = "error in recovery"; 2036c18536a7SKent Overstreet if (bch_btree_check(c)) 2037cafe5635SKent Overstreet goto err; 2038cafe5635SKent Overstreet 2039cafe5635SKent Overstreet bch_journal_mark(c, &journal); 20402531d9eeSKent Overstreet bch_initial_gc_finish(c); 204146f5aa88SJoe Perches pr_debug("btree_check() done\n"); 2042cafe5635SKent Overstreet 2043cafe5635SKent Overstreet /* 2044cafe5635SKent Overstreet * bcache_journal_next() can't happen sooner, or 2045cafe5635SKent Overstreet * btree_gc_finish() will give spurious errors about last_gc > 2046cafe5635SKent Overstreet * gc_gen - this is a hack but oh well. 2047cafe5635SKent Overstreet */ 2048cafe5635SKent Overstreet bch_journal_next(&c->journal); 2049cafe5635SKent Overstreet 2050119ba0f8SKent Overstreet err = "error starting allocator thread"; 2051119ba0f8SKent Overstreet if (bch_cache_allocator_start(ca)) 2052119ba0f8SKent Overstreet goto err; 2053cafe5635SKent Overstreet 2054cafe5635SKent Overstreet /* 2055cafe5635SKent Overstreet * First place it's safe to allocate: btree_check() and 2056cafe5635SKent Overstreet * btree_gc_finish() have to run before we have buckets to 2057cafe5635SKent Overstreet * allocate, and bch_bucket_alloc_set() might cause a journal 2058cafe5635SKent Overstreet * entry to be written so bcache_journal_next() has to be called 2059cafe5635SKent Overstreet * first. 2060cafe5635SKent Overstreet * 2061cafe5635SKent Overstreet * If the uuids were in the old format we have to rewrite them 2062cafe5635SKent Overstreet * before the next journal entry is written: 2063cafe5635SKent Overstreet */ 2064cafe5635SKent Overstreet if (j->version < BCACHE_JSET_VERSION_UUID) 2065cafe5635SKent Overstreet __uuid_write(c); 2066cafe5635SKent Overstreet 2067ce3e4cfbSColy Li err = "bcache: replay journal failed"; 2068ce3e4cfbSColy Li if (bch_journal_replay(c, &journal)) 2069ce3e4cfbSColy Li goto err; 2070cafe5635SKent Overstreet } else { 20716f10f7d1SColy Li unsigned int j; 2072cafe5635SKent Overstreet 207308fdb2cdSColy Li pr_notice("invalidating existing data\n"); 2074cafe5635SKent Overstreet ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7, 2075cafe5635SKent Overstreet 2, SB_JOURNAL_BUCKETS); 2076cafe5635SKent Overstreet 2077cafe5635SKent Overstreet for (j = 0; j < ca->sb.keys; j++) 2078cafe5635SKent Overstreet ca->sb.d[j] = ca->sb.first_bucket + j; 2079cafe5635SKent Overstreet 20802531d9eeSKent Overstreet bch_initial_gc_finish(c); 2081cafe5635SKent Overstreet 2082119ba0f8SKent Overstreet err = "error starting allocator thread"; 2083119ba0f8SKent Overstreet if (bch_cache_allocator_start(ca)) 2084119ba0f8SKent Overstreet goto err; 2085cafe5635SKent Overstreet 2086cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 208784c529aeSAndrea Righi bch_prio_write(ca, true); 2088cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 2089cafe5635SKent Overstreet 2090cafe5635SKent Overstreet err = "cannot allocate new UUID bucket"; 2091cafe5635SKent Overstreet if (__uuid_write(c)) 209272a44517SKent Overstreet goto err; 2093cafe5635SKent Overstreet 2094cafe5635SKent Overstreet err = "cannot allocate new btree root"; 20952452cc89SSlava Pestov c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL); 2096cafe5635SKent Overstreet if (IS_ERR_OR_NULL(c->root)) 209772a44517SKent Overstreet goto err; 2098cafe5635SKent Overstreet 20992a285686SKent Overstreet mutex_lock(&c->root->write_lock); 2100cafe5635SKent Overstreet bkey_copy_key(&c->root->key, &MAX_KEY); 2101c18536a7SKent Overstreet bch_btree_node_write(c->root, &cl); 21022a285686SKent Overstreet mutex_unlock(&c->root->write_lock); 2103cafe5635SKent Overstreet 2104cafe5635SKent Overstreet bch_btree_set_root(c->root); 2105cafe5635SKent Overstreet rw_unlock(true, c->root); 2106cafe5635SKent Overstreet 2107cafe5635SKent Overstreet /* 2108cafe5635SKent Overstreet * We don't want to write the first journal entry until 2109cafe5635SKent Overstreet * everything is set up - fortunately journal entries won't be 2110cafe5635SKent Overstreet * written until the SET_CACHE_SYNC() here: 2111cafe5635SKent Overstreet */ 21126f9414e0SColy Li SET_CACHE_SYNC(&c->cache->sb, true); 2113cafe5635SKent Overstreet 2114cafe5635SKent Overstreet bch_journal_next(&c->journal); 2115c18536a7SKent Overstreet bch_journal_meta(c, &cl); 2116cafe5635SKent Overstreet } 2117cafe5635SKent Overstreet 211872a44517SKent Overstreet err = "error starting gc thread"; 211972a44517SKent Overstreet if (bch_gc_thread_start(c)) 212072a44517SKent Overstreet goto err; 212172a44517SKent Overstreet 2122c18536a7SKent Overstreet closure_sync(&cl); 21234a784266SColy Li c->cache->sb.last_mount = (u32)ktime_get_real_seconds(); 2124cafe5635SKent Overstreet bcache_write_super(c); 2125cafe5635SKent Overstreet 21265342fd42SColy Li if (bch_has_feature_obso_large_bucket(&c->cache->sb)) 21275342fd42SColy Li pr_err("Detect obsoleted large bucket layout, all attached bcache device will be read-only\n"); 21285342fd42SColy Li 2129cafe5635SKent Overstreet list_for_each_entry_safe(dc, t, &uncached_devices, list) 213073ac105bSTang Junhui bch_cached_dev_attach(dc, c, NULL); 2131cafe5635SKent Overstreet 2132cafe5635SKent Overstreet flash_devs_run(c); 2133cafe5635SKent Overstreet 2134bf0c55c9SSlava Pestov set_bit(CACHE_SET_RUNNING, &c->flags); 2135ce3e4cfbSColy Li return 0; 2136cafe5635SKent Overstreet err: 213795f18c9dSShenghui Wang while (!list_empty(&journal)) { 213895f18c9dSShenghui Wang l = list_first_entry(&journal, struct journal_replay, list); 213995f18c9dSShenghui Wang list_del(&l->list); 214095f18c9dSShenghui Wang kfree(l); 214195f18c9dSShenghui Wang } 214295f18c9dSShenghui Wang 2143c18536a7SKent Overstreet closure_sync(&cl); 214468a53c95SColy Li 2145c8694948SKees Cook bch_cache_set_error(c, "%s", err); 2146ce3e4cfbSColy Li 2147ce3e4cfbSColy Li return -EIO; 2148cafe5635SKent Overstreet } 2149cafe5635SKent Overstreet 2150cafe5635SKent Overstreet static const char *register_cache_set(struct cache *ca) 2151cafe5635SKent Overstreet { 2152cafe5635SKent Overstreet char buf[12]; 2153cafe5635SKent Overstreet const char *err = "cannot allocate memory"; 2154cafe5635SKent Overstreet struct cache_set *c; 2155cafe5635SKent Overstreet 2156cafe5635SKent Overstreet list_for_each_entry(c, &bch_cache_sets, list) 21571132e56eSColy Li if (!memcmp(c->set_uuid, ca->sb.set_uuid, 16)) { 2158697e2349SColy Li if (c->cache) 2159cafe5635SKent Overstreet return "duplicate cache set member"; 2160cafe5635SKent Overstreet 2161cafe5635SKent Overstreet goto found; 2162cafe5635SKent Overstreet } 2163cafe5635SKent Overstreet 2164cafe5635SKent Overstreet c = bch_cache_set_alloc(&ca->sb); 2165cafe5635SKent Overstreet if (!c) 2166cafe5635SKent Overstreet return err; 2167cafe5635SKent Overstreet 2168cafe5635SKent Overstreet err = "error creating kobject"; 21691132e56eSColy Li if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->set_uuid) || 2170cafe5635SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal")) 2171cafe5635SKent Overstreet goto err; 2172cafe5635SKent Overstreet 2173cafe5635SKent Overstreet if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj)) 2174cafe5635SKent Overstreet goto err; 2175cafe5635SKent Overstreet 2176cafe5635SKent Overstreet bch_debug_init_cache_set(c); 2177cafe5635SKent Overstreet 2178cafe5635SKent Overstreet list_add(&c->list, &bch_cache_sets); 2179cafe5635SKent Overstreet found: 2180cafe5635SKent Overstreet sprintf(buf, "cache%i", ca->sb.nr_this_dev); 2181cafe5635SKent Overstreet if (sysfs_create_link(&ca->kobj, &c->kobj, "set") || 2182cafe5635SKent Overstreet sysfs_create_link(&c->kobj, &ca->kobj, buf)) 2183cafe5635SKent Overstreet goto err; 2184cafe5635SKent Overstreet 2185d83353b3SKent Overstreet kobject_get(&ca->kobj); 2186cafe5635SKent Overstreet ca->set = c; 2187697e2349SColy Li ca->set->cache = ca; 2188cafe5635SKent Overstreet 2189ce3e4cfbSColy Li err = "failed to run cache set"; 2190ce3e4cfbSColy Li if (run_cache_set(c) < 0) 2191ce3e4cfbSColy Li goto err; 2192cafe5635SKent Overstreet 2193cafe5635SKent Overstreet return NULL; 2194cafe5635SKent Overstreet err: 2195cafe5635SKent Overstreet bch_cache_set_unregister(c); 2196cafe5635SKent Overstreet return err; 2197cafe5635SKent Overstreet } 2198cafe5635SKent Overstreet 2199cafe5635SKent Overstreet /* Cache device */ 2200cafe5635SKent Overstreet 22012d17456eSColy Li /* When ca->kobj released */ 2202cafe5635SKent Overstreet void bch_cache_release(struct kobject *kobj) 2203cafe5635SKent Overstreet { 2204cafe5635SKent Overstreet struct cache *ca = container_of(kobj, struct cache, kobj); 22056f10f7d1SColy Li unsigned int i; 2206cafe5635SKent Overstreet 2207c9a78332SSlava Pestov if (ca->set) { 2208697e2349SColy Li BUG_ON(ca->set->cache != ca); 2209697e2349SColy Li ca->set->cache = NULL; 2210c9a78332SSlava Pestov } 2211cafe5635SKent Overstreet 2212c954ac8dSColy Li free_pages((unsigned long) ca->disk_buckets, ilog2(meta_bucket_pages(&ca->sb))); 2213cafe5635SKent Overstreet kfree(ca->prio_buckets); 2214cafe5635SKent Overstreet vfree(ca->buckets); 2215cafe5635SKent Overstreet 2216cafe5635SKent Overstreet free_heap(&ca->heap); 2217cafe5635SKent Overstreet free_fifo(&ca->free_inc); 221878365411SKent Overstreet 221978365411SKent Overstreet for (i = 0; i < RESERVE_NR; i++) 222078365411SKent Overstreet free_fifo(&ca->free[i]); 2221cafe5635SKent Overstreet 2222475389aeSChristoph Hellwig if (ca->sb_disk) 2223475389aeSChristoph Hellwig put_page(virt_to_page(ca->sb_disk)); 2224cafe5635SKent Overstreet 22250781c874SKent Overstreet if (!IS_ERR_OR_NULL(ca->bdev)) 2226cafe5635SKent Overstreet blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 2227cafe5635SKent Overstreet 2228cafe5635SKent Overstreet kfree(ca); 2229cafe5635SKent Overstreet module_put(THIS_MODULE); 2230cafe5635SKent Overstreet } 2231cafe5635SKent Overstreet 2232c50d4d5dSYijing Wang static int cache_alloc(struct cache *ca) 2233cafe5635SKent Overstreet { 2234cafe5635SKent Overstreet size_t free; 2235682811b3STang Junhui size_t btree_buckets; 2236cafe5635SKent Overstreet struct bucket *b; 2237f6027bcaSDongbo Cao int ret = -ENOMEM; 2238f6027bcaSDongbo Cao const char *err = NULL; 2239cafe5635SKent Overstreet 2240cafe5635SKent Overstreet __module_get(THIS_MODULE); 2241cafe5635SKent Overstreet kobject_init(&ca->kobj, &bch_cache_ktype); 2242cafe5635SKent Overstreet 22433a83f467SMing Lei bio_init(&ca->journal.bio, ca->journal.bio.bi_inline_vecs, 8); 2244cafe5635SKent Overstreet 2245682811b3STang Junhui /* 2246682811b3STang Junhui * when ca->sb.njournal_buckets is not zero, journal exists, 2247682811b3STang Junhui * and in bch_journal_replay(), tree node may split, 2248682811b3STang Junhui * so bucket of RESERVE_BTREE type is needed, 2249682811b3STang Junhui * the worst situation is all journal buckets are valid journal, 2250682811b3STang Junhui * and all the keys need to replay, 2251682811b3STang Junhui * so the number of RESERVE_BTREE type buckets should be as much 2252682811b3STang Junhui * as journal buckets 2253682811b3STang Junhui */ 2254682811b3STang Junhui btree_buckets = ca->sb.njournal_buckets ?: 8; 225578365411SKent Overstreet free = roundup_pow_of_two(ca->sb.nbuckets) >> 10; 22563a646fd7SDongbo Cao if (!free) { 22573a646fd7SDongbo Cao ret = -EPERM; 22583a646fd7SDongbo Cao err = "ca->sb.nbuckets is too small"; 22593a646fd7SDongbo Cao goto err_free; 22603a646fd7SDongbo Cao } 2261cafe5635SKent Overstreet 2262f6027bcaSDongbo Cao if (!init_fifo(&ca->free[RESERVE_BTREE], btree_buckets, 2263f6027bcaSDongbo Cao GFP_KERNEL)) { 2264f6027bcaSDongbo Cao err = "ca->free[RESERVE_BTREE] alloc failed"; 2265f6027bcaSDongbo Cao goto err_btree_alloc; 2266f6027bcaSDongbo Cao } 2267f6027bcaSDongbo Cao 2268f6027bcaSDongbo Cao if (!init_fifo_exact(&ca->free[RESERVE_PRIO], prio_buckets(ca), 2269f6027bcaSDongbo Cao GFP_KERNEL)) { 2270f6027bcaSDongbo Cao err = "ca->free[RESERVE_PRIO] alloc failed"; 2271f6027bcaSDongbo Cao goto err_prio_alloc; 2272f6027bcaSDongbo Cao } 2273f6027bcaSDongbo Cao 2274f6027bcaSDongbo Cao if (!init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL)) { 2275f6027bcaSDongbo Cao err = "ca->free[RESERVE_MOVINGGC] alloc failed"; 2276f6027bcaSDongbo Cao goto err_movinggc_alloc; 2277f6027bcaSDongbo Cao } 2278f6027bcaSDongbo Cao 2279f6027bcaSDongbo Cao if (!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL)) { 2280f6027bcaSDongbo Cao err = "ca->free[RESERVE_NONE] alloc failed"; 2281f6027bcaSDongbo Cao goto err_none_alloc; 2282f6027bcaSDongbo Cao } 2283f6027bcaSDongbo Cao 2284f6027bcaSDongbo Cao if (!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL)) { 2285f6027bcaSDongbo Cao err = "ca->free_inc alloc failed"; 2286f6027bcaSDongbo Cao goto err_free_inc_alloc; 2287f6027bcaSDongbo Cao } 2288f6027bcaSDongbo Cao 2289f6027bcaSDongbo Cao if (!init_heap(&ca->heap, free << 3, GFP_KERNEL)) { 2290f6027bcaSDongbo Cao err = "ca->heap alloc failed"; 2291f6027bcaSDongbo Cao goto err_heap_alloc; 2292f6027bcaSDongbo Cao } 2293f6027bcaSDongbo Cao 2294f6027bcaSDongbo Cao ca->buckets = vzalloc(array_size(sizeof(struct bucket), 2295f6027bcaSDongbo Cao ca->sb.nbuckets)); 2296f6027bcaSDongbo Cao if (!ca->buckets) { 2297f6027bcaSDongbo Cao err = "ca->buckets alloc failed"; 2298f6027bcaSDongbo Cao goto err_buckets_alloc; 2299f6027bcaSDongbo Cao } 2300f6027bcaSDongbo Cao 2301f6027bcaSDongbo Cao ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t), 23026396bb22SKees Cook prio_buckets(ca), 2), 2303f6027bcaSDongbo Cao GFP_KERNEL); 2304f6027bcaSDongbo Cao if (!ca->prio_buckets) { 2305f6027bcaSDongbo Cao err = "ca->prio_buckets alloc failed"; 2306f6027bcaSDongbo Cao goto err_prio_buckets_alloc; 2307f6027bcaSDongbo Cao } 2308f6027bcaSDongbo Cao 2309c954ac8dSColy Li ca->disk_buckets = alloc_meta_bucket_pages(GFP_KERNEL, &ca->sb); 2310f6027bcaSDongbo Cao if (!ca->disk_buckets) { 2311f6027bcaSDongbo Cao err = "ca->disk_buckets alloc failed"; 2312f6027bcaSDongbo Cao goto err_disk_buckets_alloc; 2313f6027bcaSDongbo Cao } 2314cafe5635SKent Overstreet 2315cafe5635SKent Overstreet ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca); 2316cafe5635SKent Overstreet 2317cafe5635SKent Overstreet for_each_bucket(b, ca) 2318cafe5635SKent Overstreet atomic_set(&b->pin, 0); 2319cafe5635SKent Overstreet return 0; 2320f6027bcaSDongbo Cao 2321f6027bcaSDongbo Cao err_disk_buckets_alloc: 2322f6027bcaSDongbo Cao kfree(ca->prio_buckets); 2323f6027bcaSDongbo Cao err_prio_buckets_alloc: 2324f6027bcaSDongbo Cao vfree(ca->buckets); 2325f6027bcaSDongbo Cao err_buckets_alloc: 2326f6027bcaSDongbo Cao free_heap(&ca->heap); 2327f6027bcaSDongbo Cao err_heap_alloc: 2328f6027bcaSDongbo Cao free_fifo(&ca->free_inc); 2329f6027bcaSDongbo Cao err_free_inc_alloc: 2330f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_NONE]); 2331f6027bcaSDongbo Cao err_none_alloc: 2332f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_MOVINGGC]); 2333f6027bcaSDongbo Cao err_movinggc_alloc: 2334f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_PRIO]); 2335f6027bcaSDongbo Cao err_prio_alloc: 2336f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_BTREE]); 2337f6027bcaSDongbo Cao err_btree_alloc: 23383a646fd7SDongbo Cao err_free: 2339f6027bcaSDongbo Cao module_put(THIS_MODULE); 2340f6027bcaSDongbo Cao if (err) 234146f5aa88SJoe Perches pr_notice("error %s: %s\n", ca->cache_dev_name, err); 2342f6027bcaSDongbo Cao return ret; 2343cafe5635SKent Overstreet } 2344cafe5635SKent Overstreet 2345cfa0c56dSChristoph Hellwig static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk, 2346cafe5635SKent Overstreet struct block_device *bdev, struct cache *ca) 2347cafe5635SKent Overstreet { 2348d9dc1702SEric Wheeler const char *err = NULL; /* must be set for any error case */ 23499b299728SEric Wheeler int ret = 0; 2350cafe5635SKent Overstreet 23516e916a7eSColy Li bdevname(bdev, ca->cache_dev_name); 2352f59fce84SKent Overstreet memcpy(&ca->sb, sb, sizeof(struct cache_sb)); 2353cafe5635SKent Overstreet ca->bdev = bdev; 2354cafe5635SKent Overstreet ca->bdev->bd_holder = ca; 2355475389aeSChristoph Hellwig ca->sb_disk = sb_disk; 2356f59fce84SKent Overstreet 2357cc40daf9STang Junhui if (blk_queue_discard(bdev_get_queue(bdev))) 2358cafe5635SKent Overstreet ca->discard = CACHE_DISCARD(&ca->sb); 2359cafe5635SKent Overstreet 2360c50d4d5dSYijing Wang ret = cache_alloc(ca); 2361d9dc1702SEric Wheeler if (ret != 0) { 2362bb6d355cSColy Li /* 2363bb6d355cSColy Li * If we failed here, it means ca->kobj is not initialized yet, 2364bb6d355cSColy Li * kobject_put() won't be called and there is no chance to 2365bb6d355cSColy Li * call blkdev_put() to bdev in bch_cache_release(). So we 2366bb6d355cSColy Li * explicitly call blkdev_put() here. 2367bb6d355cSColy Li */ 2368cc40daf9STang Junhui blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 2369d9dc1702SEric Wheeler if (ret == -ENOMEM) 2370d9dc1702SEric Wheeler err = "cache_alloc(): -ENOMEM"; 23713a646fd7SDongbo Cao else if (ret == -EPERM) 23723a646fd7SDongbo Cao err = "cache_alloc(): cache device is too small"; 2373d9dc1702SEric Wheeler else 2374d9dc1702SEric Wheeler err = "cache_alloc(): unknown error"; 2375f59fce84SKent Overstreet goto err; 2376d9dc1702SEric Wheeler } 2377f59fce84SKent Overstreet 23788d65269fSChristoph Hellwig if (kobject_add(&ca->kobj, bdev_kobj(bdev), "bcache")) { 23799b299728SEric Wheeler err = "error calling kobject_add"; 23809b299728SEric Wheeler ret = -ENOMEM; 23819b299728SEric Wheeler goto out; 23829b299728SEric Wheeler } 2383cafe5635SKent Overstreet 23844fa03402SKent Overstreet mutex_lock(&bch_register_lock); 2385cafe5635SKent Overstreet err = register_cache_set(ca); 23864fa03402SKent Overstreet mutex_unlock(&bch_register_lock); 23874fa03402SKent Overstreet 23889b299728SEric Wheeler if (err) { 23899b299728SEric Wheeler ret = -ENODEV; 23909b299728SEric Wheeler goto out; 23919b299728SEric Wheeler } 2392cafe5635SKent Overstreet 239346f5aa88SJoe Perches pr_info("registered cache device %s\n", ca->cache_dev_name); 23949b299728SEric Wheeler 2395d83353b3SKent Overstreet out: 2396d83353b3SKent Overstreet kobject_put(&ca->kobj); 23979b299728SEric Wheeler 2398cafe5635SKent Overstreet err: 23999b299728SEric Wheeler if (err) 240046f5aa88SJoe Perches pr_notice("error %s: %s\n", ca->cache_dev_name, err); 24019b299728SEric Wheeler 24029b299728SEric Wheeler return ret; 2403cafe5635SKent Overstreet } 2404cafe5635SKent Overstreet 2405cafe5635SKent Overstreet /* Global interfaces/init */ 2406cafe5635SKent Overstreet 2407fc2d5988SColy Li static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, 2408fc2d5988SColy Li const char *buffer, size_t size); 24090c277e21SColy Li static ssize_t bch_pending_bdevs_cleanup(struct kobject *k, 24100c277e21SColy Li struct kobj_attribute *attr, 24110c277e21SColy Li const char *buffer, size_t size); 2412cafe5635SKent Overstreet 2413cafe5635SKent Overstreet kobj_attribute_write(register, register_bcache); 2414cafe5635SKent Overstreet kobj_attribute_write(register_quiet, register_bcache); 24150c277e21SColy Li kobj_attribute_write(pendings_cleanup, bch_pending_bdevs_cleanup); 2416cafe5635SKent Overstreet 24174e7b5671SChristoph Hellwig static bool bch_is_open_backing(dev_t dev) 2418b3cf37bfSColy Li { 2419a9dd53adSGabriel de Perthuis struct cache_set *c, *tc; 2420a9dd53adSGabriel de Perthuis struct cached_dev *dc, *t; 2421a9dd53adSGabriel de Perthuis 2422a9dd53adSGabriel de Perthuis list_for_each_entry_safe(c, tc, &bch_cache_sets, list) 2423a9dd53adSGabriel de Perthuis list_for_each_entry_safe(dc, t, &c->cached_devs, list) 24244e7b5671SChristoph Hellwig if (dc->bdev->bd_dev == dev) 2425a9dd53adSGabriel de Perthuis return true; 2426a9dd53adSGabriel de Perthuis list_for_each_entry_safe(dc, t, &uncached_devices, list) 24274e7b5671SChristoph Hellwig if (dc->bdev->bd_dev == dev) 2428a9dd53adSGabriel de Perthuis return true; 2429a9dd53adSGabriel de Perthuis return false; 2430a9dd53adSGabriel de Perthuis } 2431a9dd53adSGabriel de Perthuis 24324e7b5671SChristoph Hellwig static bool bch_is_open_cache(dev_t dev) 2433b3cf37bfSColy Li { 2434a9dd53adSGabriel de Perthuis struct cache_set *c, *tc; 2435a9dd53adSGabriel de Perthuis 243608fdb2cdSColy Li list_for_each_entry_safe(c, tc, &bch_cache_sets, list) { 243708fdb2cdSColy Li struct cache *ca = c->cache; 243808fdb2cdSColy Li 24394e7b5671SChristoph Hellwig if (ca->bdev->bd_dev == dev) 2440a9dd53adSGabriel de Perthuis return true; 244108fdb2cdSColy Li } 244208fdb2cdSColy Li 2443a9dd53adSGabriel de Perthuis return false; 2444a9dd53adSGabriel de Perthuis } 2445a9dd53adSGabriel de Perthuis 24464e7b5671SChristoph Hellwig static bool bch_is_open(dev_t dev) 2447b3cf37bfSColy Li { 24484e7b5671SChristoph Hellwig return bch_is_open_cache(dev) || bch_is_open_backing(dev); 2449a9dd53adSGabriel de Perthuis } 2450a9dd53adSGabriel de Perthuis 24519e23ccf8SColy Li struct async_reg_args { 2452ee4a36f4SColy Li struct delayed_work reg_work; 24539e23ccf8SColy Li char *path; 24549e23ccf8SColy Li struct cache_sb *sb; 24559e23ccf8SColy Li struct cache_sb_disk *sb_disk; 24569e23ccf8SColy Li struct block_device *bdev; 24579e23ccf8SColy Li }; 24589e23ccf8SColy Li 24599e23ccf8SColy Li static void register_bdev_worker(struct work_struct *work) 24609e23ccf8SColy Li { 24619e23ccf8SColy Li int fail = false; 24629e23ccf8SColy Li struct async_reg_args *args = 2463ee4a36f4SColy Li container_of(work, struct async_reg_args, reg_work.work); 24649e23ccf8SColy Li struct cached_dev *dc; 24659e23ccf8SColy Li 24669e23ccf8SColy Li dc = kzalloc(sizeof(*dc), GFP_KERNEL); 24679e23ccf8SColy Li if (!dc) { 24689e23ccf8SColy Li fail = true; 24699e23ccf8SColy Li put_page(virt_to_page(args->sb_disk)); 24709e23ccf8SColy Li blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 24719e23ccf8SColy Li goto out; 24729e23ccf8SColy Li } 24739e23ccf8SColy Li 24749e23ccf8SColy Li mutex_lock(&bch_register_lock); 24759e23ccf8SColy Li if (register_bdev(args->sb, args->sb_disk, args->bdev, dc) < 0) 24769e23ccf8SColy Li fail = true; 24779e23ccf8SColy Li mutex_unlock(&bch_register_lock); 24789e23ccf8SColy Li 24799e23ccf8SColy Li out: 24809e23ccf8SColy Li if (fail) 24819e23ccf8SColy Li pr_info("error %s: fail to register backing device\n", 24829e23ccf8SColy Li args->path); 24839e23ccf8SColy Li kfree(args->sb); 24849e23ccf8SColy Li kfree(args->path); 24859e23ccf8SColy Li kfree(args); 24869e23ccf8SColy Li module_put(THIS_MODULE); 24879e23ccf8SColy Li } 24889e23ccf8SColy Li 24899e23ccf8SColy Li static void register_cache_worker(struct work_struct *work) 24909e23ccf8SColy Li { 24919e23ccf8SColy Li int fail = false; 24929e23ccf8SColy Li struct async_reg_args *args = 2493ee4a36f4SColy Li container_of(work, struct async_reg_args, reg_work.work); 24949e23ccf8SColy Li struct cache *ca; 24959e23ccf8SColy Li 24969e23ccf8SColy Li ca = kzalloc(sizeof(*ca), GFP_KERNEL); 24979e23ccf8SColy Li if (!ca) { 24989e23ccf8SColy Li fail = true; 24999e23ccf8SColy Li put_page(virt_to_page(args->sb_disk)); 25009e23ccf8SColy Li blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 25019e23ccf8SColy Li goto out; 25029e23ccf8SColy Li } 25039e23ccf8SColy Li 25049e23ccf8SColy Li /* blkdev_put() will be called in bch_cache_release() */ 25059e23ccf8SColy Li if (register_cache(args->sb, args->sb_disk, args->bdev, ca) != 0) 25069e23ccf8SColy Li fail = true; 25079e23ccf8SColy Li 25089e23ccf8SColy Li out: 25099e23ccf8SColy Li if (fail) 25109e23ccf8SColy Li pr_info("error %s: fail to register cache device\n", 25119e23ccf8SColy Li args->path); 25129e23ccf8SColy Li kfree(args->sb); 25139e23ccf8SColy Li kfree(args->path); 25149e23ccf8SColy Li kfree(args); 25159e23ccf8SColy Li module_put(THIS_MODULE); 25169e23ccf8SColy Li } 25179e23ccf8SColy Li 2518d7fae7b4SKai Krakow static void register_device_async(struct async_reg_args *args) 25199e23ccf8SColy Li { 25209e23ccf8SColy Li if (SB_IS_BDEV(args->sb)) 2521ee4a36f4SColy Li INIT_DELAYED_WORK(&args->reg_work, register_bdev_worker); 25229e23ccf8SColy Li else 2523ee4a36f4SColy Li INIT_DELAYED_WORK(&args->reg_work, register_cache_worker); 25249e23ccf8SColy Li 2525ee4a36f4SColy Li /* 10 jiffies is enough for a delay */ 2526ee4a36f4SColy Li queue_delayed_work(system_wq, &args->reg_work, 10); 25279e23ccf8SColy Li } 25289e23ccf8SColy Li 2529cafe5635SKent Overstreet static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, 2530cafe5635SKent Overstreet const char *buffer, size_t size) 2531cafe5635SKent Overstreet { 253250246693SChristoph Hellwig const char *err; 253329cda393SColy Li char *path = NULL; 253450246693SChristoph Hellwig struct cache_sb *sb; 2535cfa0c56dSChristoph Hellwig struct cache_sb_disk *sb_disk; 2536fc8f19ccSChristoph Hellwig struct block_device *bdev; 253750246693SChristoph Hellwig ssize_t ret; 2538a58e88bfSColy Li bool async_registration = false; 2539a58e88bfSColy Li 2540a58e88bfSColy Li #ifdef CONFIG_BCACHE_ASYNC_REGISTRATION 2541a58e88bfSColy Li async_registration = true; 2542a58e88bfSColy Li #endif 2543cafe5635SKent Overstreet 254450246693SChristoph Hellwig ret = -EBUSY; 254529cda393SColy Li err = "failed to reference bcache module"; 2546cafe5635SKent Overstreet if (!try_module_get(THIS_MODULE)) 254750246693SChristoph Hellwig goto out; 2548cafe5635SKent Overstreet 2549a59ff6ccSColy Li /* For latest state of bcache_is_reboot */ 2550a59ff6ccSColy Li smp_mb(); 255129cda393SColy Li err = "bcache is in reboot"; 2552a59ff6ccSColy Li if (bcache_is_reboot) 255350246693SChristoph Hellwig goto out_module_put; 2554a59ff6ccSColy Li 255550246693SChristoph Hellwig ret = -ENOMEM; 255650246693SChristoph Hellwig err = "cannot allocate memory"; 2557a56489d4SFlorian Schmaus path = kstrndup(buffer, size, GFP_KERNEL); 2558a56489d4SFlorian Schmaus if (!path) 255950246693SChristoph Hellwig goto out_module_put; 2560a56489d4SFlorian Schmaus 2561a56489d4SFlorian Schmaus sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL); 2562a56489d4SFlorian Schmaus if (!sb) 256350246693SChristoph Hellwig goto out_free_path; 2564cafe5635SKent Overstreet 256550246693SChristoph Hellwig ret = -EINVAL; 2566cafe5635SKent Overstreet err = "failed to open device"; 2567cafe5635SKent Overstreet bdev = blkdev_get_by_path(strim(path), 2568cafe5635SKent Overstreet FMODE_READ|FMODE_WRITE|FMODE_EXCL, 2569cafe5635SKent Overstreet sb); 2570f59fce84SKent Overstreet if (IS_ERR(bdev)) { 2571a9dd53adSGabriel de Perthuis if (bdev == ERR_PTR(-EBUSY)) { 25724e7b5671SChristoph Hellwig dev_t dev; 25734e7b5671SChristoph Hellwig 2574789d21dbSJianjian Huo mutex_lock(&bch_register_lock); 25754e7b5671SChristoph Hellwig if (lookup_bdev(strim(path), &dev) == 0 && 25764e7b5671SChristoph Hellwig bch_is_open(dev)) 2577a9dd53adSGabriel de Perthuis err = "device already registered"; 2578a9dd53adSGabriel de Perthuis else 2579cafe5635SKent Overstreet err = "device busy"; 2580789d21dbSJianjian Huo mutex_unlock(&bch_register_lock); 2581d7076f21SGabriel de Perthuis if (attr == &ksysfs_register_quiet) 258250246693SChristoph Hellwig goto done; 2583a9dd53adSGabriel de Perthuis } 258450246693SChristoph Hellwig goto out_free_sb; 2585f59fce84SKent Overstreet } 2586f59fce84SKent Overstreet 2587f59fce84SKent Overstreet err = "failed to set blocksize"; 2588f59fce84SKent Overstreet if (set_blocksize(bdev, 4096)) 258950246693SChristoph Hellwig goto out_blkdev_put; 2590cafe5635SKent Overstreet 2591cfa0c56dSChristoph Hellwig err = read_super(sb, bdev, &sb_disk); 2592cafe5635SKent Overstreet if (err) 259350246693SChristoph Hellwig goto out_blkdev_put; 2594cafe5635SKent Overstreet 2595cc40daf9STang Junhui err = "failed to register device"; 2596a58e88bfSColy Li 2597a58e88bfSColy Li if (async_registration) { 25989e23ccf8SColy Li /* register in asynchronous way */ 25999e23ccf8SColy Li struct async_reg_args *args = 26009e23ccf8SColy Li kzalloc(sizeof(struct async_reg_args), GFP_KERNEL); 26019e23ccf8SColy Li 26029e23ccf8SColy Li if (!args) { 26039e23ccf8SColy Li ret = -ENOMEM; 26049e23ccf8SColy Li err = "cannot allocate memory"; 26059e23ccf8SColy Li goto out_put_sb_page; 26069e23ccf8SColy Li } 26079e23ccf8SColy Li 26089e23ccf8SColy Li args->path = path; 26099e23ccf8SColy Li args->sb = sb; 26109e23ccf8SColy Li args->sb_disk = sb_disk; 26119e23ccf8SColy Li args->bdev = bdev; 2612d7fae7b4SKai Krakow register_device_async(args); 26139e23ccf8SColy Li /* No wait and returns to user space */ 26149e23ccf8SColy Li goto async_done; 26159e23ccf8SColy Li } 26169e23ccf8SColy Li 26172903381fSKent Overstreet if (SB_IS_BDEV(sb)) { 2618cafe5635SKent Overstreet struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL); 26191fae7cf0SColy Li 2620*d55f7cb2SChao Yu if (!dc) { 2621*d55f7cb2SChao Yu ret = -ENOMEM; 2622*d55f7cb2SChao Yu err = "cannot allocate memory"; 262350246693SChristoph Hellwig goto out_put_sb_page; 2624*d55f7cb2SChao Yu } 2625cafe5635SKent Overstreet 26264fa03402SKent Overstreet mutex_lock(&bch_register_lock); 2627cfa0c56dSChristoph Hellwig ret = register_bdev(sb, sb_disk, bdev, dc); 26284fa03402SKent Overstreet mutex_unlock(&bch_register_lock); 2629bb6d355cSColy Li /* blkdev_put() will be called in cached_dev_free() */ 2630fc8f19ccSChristoph Hellwig if (ret < 0) 2631fc8f19ccSChristoph Hellwig goto out_free_sb; 2632cafe5635SKent Overstreet } else { 2633cafe5635SKent Overstreet struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL); 26341fae7cf0SColy Li 2635*d55f7cb2SChao Yu if (!ca) { 2636*d55f7cb2SChao Yu ret = -ENOMEM; 2637*d55f7cb2SChao Yu err = "cannot allocate memory"; 263850246693SChristoph Hellwig goto out_put_sb_page; 2639*d55f7cb2SChao Yu } 2640cafe5635SKent Overstreet 2641bb6d355cSColy Li /* blkdev_put() will be called in bch_cache_release() */ 2642*d55f7cb2SChao Yu ret = register_cache(sb, sb_disk, bdev, ca); 2643*d55f7cb2SChao Yu if (ret) 2644fc8f19ccSChristoph Hellwig goto out_free_sb; 264550246693SChristoph Hellwig } 264650246693SChristoph Hellwig 264750246693SChristoph Hellwig done: 2648f59fce84SKent Overstreet kfree(sb); 2649f59fce84SKent Overstreet kfree(path); 2650f59fce84SKent Overstreet module_put(THIS_MODULE); 26519e23ccf8SColy Li async_done: 265250246693SChristoph Hellwig return size; 2653f59fce84SKent Overstreet 265450246693SChristoph Hellwig out_put_sb_page: 2655cfa0c56dSChristoph Hellwig put_page(virt_to_page(sb_disk)); 265650246693SChristoph Hellwig out_blkdev_put: 2657cafe5635SKent Overstreet blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 265850246693SChristoph Hellwig out_free_sb: 265950246693SChristoph Hellwig kfree(sb); 266050246693SChristoph Hellwig out_free_path: 266150246693SChristoph Hellwig kfree(path); 2662ae3cd299SColy Li path = NULL; 266350246693SChristoph Hellwig out_module_put: 266450246693SChristoph Hellwig module_put(THIS_MODULE); 266550246693SChristoph Hellwig out: 266646f5aa88SJoe Perches pr_info("error %s: %s\n", path?path:"", err); 266750246693SChristoph Hellwig return ret; 2668cafe5635SKent Overstreet } 2669cafe5635SKent Overstreet 26700c277e21SColy Li 26710c277e21SColy Li struct pdev { 26720c277e21SColy Li struct list_head list; 26730c277e21SColy Li struct cached_dev *dc; 26740c277e21SColy Li }; 26750c277e21SColy Li 26760c277e21SColy Li static ssize_t bch_pending_bdevs_cleanup(struct kobject *k, 26770c277e21SColy Li struct kobj_attribute *attr, 26780c277e21SColy Li const char *buffer, 26790c277e21SColy Li size_t size) 26800c277e21SColy Li { 26810c277e21SColy Li LIST_HEAD(pending_devs); 26820c277e21SColy Li ssize_t ret = size; 26830c277e21SColy Li struct cached_dev *dc, *tdc; 26840c277e21SColy Li struct pdev *pdev, *tpdev; 26850c277e21SColy Li struct cache_set *c, *tc; 26860c277e21SColy Li 26870c277e21SColy Li mutex_lock(&bch_register_lock); 26880c277e21SColy Li list_for_each_entry_safe(dc, tdc, &uncached_devices, list) { 26890c277e21SColy Li pdev = kmalloc(sizeof(struct pdev), GFP_KERNEL); 26900c277e21SColy Li if (!pdev) 26910c277e21SColy Li break; 26920c277e21SColy Li pdev->dc = dc; 26930c277e21SColy Li list_add(&pdev->list, &pending_devs); 26940c277e21SColy Li } 26950c277e21SColy Li 26960c277e21SColy Li list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) { 26970c277e21SColy Li char *pdev_set_uuid = pdev->dc->sb.set_uuid; 2698e8092707SYi Li list_for_each_entry_safe(c, tc, &bch_cache_sets, list) { 26991132e56eSColy Li char *set_uuid = c->set_uuid; 27000c277e21SColy Li 27010c277e21SColy Li if (!memcmp(pdev_set_uuid, set_uuid, 16)) { 27020c277e21SColy Li list_del(&pdev->list); 27030c277e21SColy Li kfree(pdev); 27040c277e21SColy Li break; 27050c277e21SColy Li } 27060c277e21SColy Li } 27070c277e21SColy Li } 27080c277e21SColy Li mutex_unlock(&bch_register_lock); 27090c277e21SColy Li 27100c277e21SColy Li list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) { 271146f5aa88SJoe Perches pr_info("delete pdev %p\n", pdev); 27120c277e21SColy Li list_del(&pdev->list); 27130c277e21SColy Li bcache_device_stop(&pdev->dc->disk); 27140c277e21SColy Li kfree(pdev); 27150c277e21SColy Li } 27160c277e21SColy Li 27170c277e21SColy Li return ret; 27180c277e21SColy Li } 27190c277e21SColy Li 2720cafe5635SKent Overstreet static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x) 2721cafe5635SKent Overstreet { 2722a59ff6ccSColy Li if (bcache_is_reboot) 2723a59ff6ccSColy Li return NOTIFY_DONE; 2724a59ff6ccSColy Li 2725cafe5635SKent Overstreet if (code == SYS_DOWN || 2726cafe5635SKent Overstreet code == SYS_HALT || 2727cafe5635SKent Overstreet code == SYS_POWER_OFF) { 2728cafe5635SKent Overstreet DEFINE_WAIT(wait); 2729cafe5635SKent Overstreet unsigned long start = jiffies; 2730cafe5635SKent Overstreet bool stopped = false; 2731cafe5635SKent Overstreet 2732cafe5635SKent Overstreet struct cache_set *c, *tc; 2733cafe5635SKent Overstreet struct cached_dev *dc, *tdc; 2734cafe5635SKent Overstreet 2735cafe5635SKent Overstreet mutex_lock(&bch_register_lock); 2736cafe5635SKent Overstreet 2737a59ff6ccSColy Li if (bcache_is_reboot) 2738a59ff6ccSColy Li goto out; 2739a59ff6ccSColy Li 2740a59ff6ccSColy Li /* New registration is rejected since now */ 2741a59ff6ccSColy Li bcache_is_reboot = true; 2742a59ff6ccSColy Li /* 2743a59ff6ccSColy Li * Make registering caller (if there is) on other CPU 2744a59ff6ccSColy Li * core know bcache_is_reboot set to true earlier 2745a59ff6ccSColy Li */ 2746a59ff6ccSColy Li smp_mb(); 2747a59ff6ccSColy Li 2748cafe5635SKent Overstreet if (list_empty(&bch_cache_sets) && 2749cafe5635SKent Overstreet list_empty(&uncached_devices)) 2750cafe5635SKent Overstreet goto out; 2751cafe5635SKent Overstreet 2752a59ff6ccSColy Li mutex_unlock(&bch_register_lock); 2753a59ff6ccSColy Li 275446f5aa88SJoe Perches pr_info("Stopping all devices:\n"); 2755cafe5635SKent Overstreet 2756a59ff6ccSColy Li /* 2757a59ff6ccSColy Li * The reason bch_register_lock is not held to call 2758a59ff6ccSColy Li * bch_cache_set_stop() and bcache_device_stop() is to 2759a59ff6ccSColy Li * avoid potential deadlock during reboot, because cache 2760a307e2abSDing Senjie * set or bcache device stopping process will acquire 2761a59ff6ccSColy Li * bch_register_lock too. 2762a59ff6ccSColy Li * 2763a59ff6ccSColy Li * We are safe here because bcache_is_reboot sets to 2764a59ff6ccSColy Li * true already, register_bcache() will reject new 2765a59ff6ccSColy Li * registration now. bcache_is_reboot also makes sure 2766a59ff6ccSColy Li * bcache_reboot() won't be re-entered on by other thread, 2767a59ff6ccSColy Li * so there is no race in following list iteration by 2768a59ff6ccSColy Li * list_for_each_entry_safe(). 2769a59ff6ccSColy Li */ 2770cafe5635SKent Overstreet list_for_each_entry_safe(c, tc, &bch_cache_sets, list) 2771cafe5635SKent Overstreet bch_cache_set_stop(c); 2772cafe5635SKent Overstreet 2773cafe5635SKent Overstreet list_for_each_entry_safe(dc, tdc, &uncached_devices, list) 2774cafe5635SKent Overstreet bcache_device_stop(&dc->disk); 2775cafe5635SKent Overstreet 2776eb8cbb6dSColy Li 2777eb8cbb6dSColy Li /* 2778eb8cbb6dSColy Li * Give an early chance for other kthreads and 2779eb8cbb6dSColy Li * kworkers to stop themselves 2780eb8cbb6dSColy Li */ 2781eb8cbb6dSColy Li schedule(); 2782eb8cbb6dSColy Li 2783cafe5635SKent Overstreet /* What's a condition variable? */ 2784cafe5635SKent Overstreet while (1) { 2785eb8cbb6dSColy Li long timeout = start + 10 * HZ - jiffies; 2786cafe5635SKent Overstreet 2787eb8cbb6dSColy Li mutex_lock(&bch_register_lock); 2788cafe5635SKent Overstreet stopped = list_empty(&bch_cache_sets) && 2789cafe5635SKent Overstreet list_empty(&uncached_devices); 2790cafe5635SKent Overstreet 2791cafe5635SKent Overstreet if (timeout < 0 || stopped) 2792cafe5635SKent Overstreet break; 2793cafe5635SKent Overstreet 2794cafe5635SKent Overstreet prepare_to_wait(&unregister_wait, &wait, 2795cafe5635SKent Overstreet TASK_UNINTERRUPTIBLE); 2796cafe5635SKent Overstreet 2797cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 2798cafe5635SKent Overstreet schedule_timeout(timeout); 2799cafe5635SKent Overstreet } 2800cafe5635SKent Overstreet 2801cafe5635SKent Overstreet finish_wait(&unregister_wait, &wait); 2802cafe5635SKent Overstreet 2803cafe5635SKent Overstreet if (stopped) 280446f5aa88SJoe Perches pr_info("All devices stopped\n"); 2805cafe5635SKent Overstreet else 280646f5aa88SJoe Perches pr_notice("Timeout waiting for devices to be closed\n"); 2807cafe5635SKent Overstreet out: 2808cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 2809cafe5635SKent Overstreet } 2810cafe5635SKent Overstreet 2811cafe5635SKent Overstreet return NOTIFY_DONE; 2812cafe5635SKent Overstreet } 2813cafe5635SKent Overstreet 2814cafe5635SKent Overstreet static struct notifier_block reboot = { 2815cafe5635SKent Overstreet .notifier_call = bcache_reboot, 2816cafe5635SKent Overstreet .priority = INT_MAX, /* before any real devices */ 2817cafe5635SKent Overstreet }; 2818cafe5635SKent Overstreet 2819cafe5635SKent Overstreet static void bcache_exit(void) 2820cafe5635SKent Overstreet { 2821cafe5635SKent Overstreet bch_debug_exit(); 2822cafe5635SKent Overstreet bch_request_exit(); 2823cafe5635SKent Overstreet if (bcache_kobj) 2824cafe5635SKent Overstreet kobject_put(bcache_kobj); 2825cafe5635SKent Overstreet if (bcache_wq) 2826cafe5635SKent Overstreet destroy_workqueue(bcache_wq); 28270f843e65SGuoju Fang if (bch_journal_wq) 28280f843e65SGuoju Fang destroy_workqueue(bch_journal_wq); 2829afe78ab4SKai Krakow if (bch_flush_wq) 2830afe78ab4SKai Krakow destroy_workqueue(bch_flush_wq); 28319f233ffeSKai Krakow bch_btree_exit(); 28320f843e65SGuoju Fang 28335c41c8a7SKent Overstreet if (bcache_major) 2834cafe5635SKent Overstreet unregister_blkdev(bcache_major, "bcache"); 2835cafe5635SKent Overstreet unregister_reboot_notifier(&reboot); 2836330a4db8SLiang Chen mutex_destroy(&bch_register_lock); 2837cafe5635SKent Overstreet } 2838cafe5635SKent Overstreet 28399aaf5165SColy Li /* Check and fixup module parameters */ 28409aaf5165SColy Li static void check_module_parameters(void) 28419aaf5165SColy Li { 28429aaf5165SColy Li if (bch_cutoff_writeback_sync == 0) 28439aaf5165SColy Li bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC; 28449aaf5165SColy Li else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) { 284546f5aa88SJoe Perches pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u\n", 28469aaf5165SColy Li bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX); 28479aaf5165SColy Li bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX; 28489aaf5165SColy Li } 28499aaf5165SColy Li 28509aaf5165SColy Li if (bch_cutoff_writeback == 0) 28519aaf5165SColy Li bch_cutoff_writeback = CUTOFF_WRITEBACK; 28529aaf5165SColy Li else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) { 285346f5aa88SJoe Perches pr_warn("set bch_cutoff_writeback (%u) to max value %u\n", 28549aaf5165SColy Li bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX); 28559aaf5165SColy Li bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX; 28569aaf5165SColy Li } 28579aaf5165SColy Li 28589aaf5165SColy Li if (bch_cutoff_writeback > bch_cutoff_writeback_sync) { 285946f5aa88SJoe Perches pr_warn("set bch_cutoff_writeback (%u) to %u\n", 28609aaf5165SColy Li bch_cutoff_writeback, bch_cutoff_writeback_sync); 28619aaf5165SColy Li bch_cutoff_writeback = bch_cutoff_writeback_sync; 28629aaf5165SColy Li } 28639aaf5165SColy Li } 28649aaf5165SColy Li 2865cafe5635SKent Overstreet static int __init bcache_init(void) 2866cafe5635SKent Overstreet { 2867cafe5635SKent Overstreet static const struct attribute *files[] = { 2868cafe5635SKent Overstreet &ksysfs_register.attr, 2869cafe5635SKent Overstreet &ksysfs_register_quiet.attr, 28700c277e21SColy Li &ksysfs_pendings_cleanup.attr, 2871cafe5635SKent Overstreet NULL 2872cafe5635SKent Overstreet }; 2873cafe5635SKent Overstreet 28749aaf5165SColy Li check_module_parameters(); 28759aaf5165SColy Li 2876cafe5635SKent Overstreet mutex_init(&bch_register_lock); 2877cafe5635SKent Overstreet init_waitqueue_head(&unregister_wait); 2878cafe5635SKent Overstreet register_reboot_notifier(&reboot); 2879cafe5635SKent Overstreet 2880cafe5635SKent Overstreet bcache_major = register_blkdev(0, "bcache"); 28812ecf0cdbSZheng Liu if (bcache_major < 0) { 28822ecf0cdbSZheng Liu unregister_reboot_notifier(&reboot); 2883330a4db8SLiang Chen mutex_destroy(&bch_register_lock); 2884cafe5635SKent Overstreet return bcache_major; 28852ecf0cdbSZheng Liu } 2886cafe5635SKent Overstreet 28879f233ffeSKai Krakow if (bch_btree_init()) 28889f233ffeSKai Krakow goto err; 28899f233ffeSKai Krakow 289016c1fdf4SFlorian Schmaus bcache_wq = alloc_workqueue("bcache", WQ_MEM_RECLAIM, 0); 289116c1fdf4SFlorian Schmaus if (!bcache_wq) 289216c1fdf4SFlorian Schmaus goto err; 289316c1fdf4SFlorian Schmaus 2894afe78ab4SKai Krakow /* 2895afe78ab4SKai Krakow * Let's not make this `WQ_MEM_RECLAIM` for the following reasons: 2896afe78ab4SKai Krakow * 2897afe78ab4SKai Krakow * 1. It used `system_wq` before which also does no memory reclaim. 2898afe78ab4SKai Krakow * 2. With `WQ_MEM_RECLAIM` desktop stalls, increased boot times, and 2899afe78ab4SKai Krakow * reduced throughput can be observed. 2900afe78ab4SKai Krakow * 2901afe78ab4SKai Krakow * We still want to user our own queue to not congest the `system_wq`. 2902afe78ab4SKai Krakow */ 2903afe78ab4SKai Krakow bch_flush_wq = alloc_workqueue("bch_flush", 0, 0); 2904afe78ab4SKai Krakow if (!bch_flush_wq) 2905afe78ab4SKai Krakow goto err; 2906afe78ab4SKai Krakow 29070f843e65SGuoju Fang bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0); 29080f843e65SGuoju Fang if (!bch_journal_wq) 29090f843e65SGuoju Fang goto err; 29100f843e65SGuoju Fang 291116c1fdf4SFlorian Schmaus bcache_kobj = kobject_create_and_add("bcache", fs_kobj); 291216c1fdf4SFlorian Schmaus if (!bcache_kobj) 291316c1fdf4SFlorian Schmaus goto err; 291416c1fdf4SFlorian Schmaus 291516c1fdf4SFlorian Schmaus if (bch_request_init() || 2916330a4db8SLiang Chen sysfs_create_files(bcache_kobj, files)) 2917cafe5635SKent Overstreet goto err; 2918cafe5635SKent Overstreet 291991bafdf0SDongbo Cao bch_debug_init(); 292078ac2107SColy Li closure_debug_init(); 292178ac2107SColy Li 2922a59ff6ccSColy Li bcache_is_reboot = false; 2923a59ff6ccSColy Li 2924cafe5635SKent Overstreet return 0; 2925cafe5635SKent Overstreet err: 2926cafe5635SKent Overstreet bcache_exit(); 2927cafe5635SKent Overstreet return -ENOMEM; 2928cafe5635SKent Overstreet } 2929cafe5635SKent Overstreet 29309aaf5165SColy Li /* 29319aaf5165SColy Li * Module hooks 29329aaf5165SColy Li */ 2933cafe5635SKent Overstreet module_exit(bcache_exit); 2934cafe5635SKent Overstreet module_init(bcache_init); 2935009673d0SColy Li 29369aaf5165SColy Li module_param(bch_cutoff_writeback, uint, 0); 29379aaf5165SColy Li MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback"); 29389aaf5165SColy Li 29399aaf5165SColy Li module_param(bch_cutoff_writeback_sync, uint, 0); 29409aaf5165SColy Li MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff writeback"); 29419aaf5165SColy Li 2942009673d0SColy Li MODULE_DESCRIPTION("Bcache: a Linux block layer cache"); 2943009673d0SColy Li MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 2944009673d0SColy Li MODULE_LICENSE("GPL"); 2945