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> 19cafe5635SKent Overstreet #include <linux/debugfs.h> 20cafe5635SKent Overstreet #include <linux/genhd.h> 2128935ab5SKent Overstreet #include <linux/idr.h> 2279826c35SKent Overstreet #include <linux/kthread.h> 23ee4a36f4SColy Li #include <linux/workqueue.h> 24cafe5635SKent Overstreet #include <linux/module.h> 25cafe5635SKent Overstreet #include <linux/random.h> 26cafe5635SKent Overstreet #include <linux/reboot.h> 27cafe5635SKent Overstreet #include <linux/sysfs.h> 28cafe5635SKent Overstreet 299aaf5165SColy Li unsigned int bch_cutoff_writeback; 309aaf5165SColy Li unsigned int bch_cutoff_writeback_sync; 319aaf5165SColy Li 32cafe5635SKent Overstreet static const char bcache_magic[] = { 33cafe5635SKent Overstreet 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, 34cafe5635SKent Overstreet 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 35cafe5635SKent Overstreet }; 36cafe5635SKent Overstreet 37cafe5635SKent Overstreet static const char invalid_uuid[] = { 38cafe5635SKent Overstreet 0xa0, 0x3e, 0xf8, 0xed, 0x3e, 0xe1, 0xb8, 0x78, 39cafe5635SKent Overstreet 0xc8, 0x50, 0xfc, 0x5e, 0xcb, 0x16, 0xcd, 0x99 40cafe5635SKent Overstreet }; 41cafe5635SKent Overstreet 42cafe5635SKent Overstreet static struct kobject *bcache_kobj; 43cafe5635SKent Overstreet struct mutex bch_register_lock; 44a59ff6ccSColy Li bool bcache_is_reboot; 45cafe5635SKent Overstreet LIST_HEAD(bch_cache_sets); 46cafe5635SKent Overstreet static LIST_HEAD(uncached_devices); 47cafe5635SKent Overstreet 4828935ab5SKent Overstreet static int bcache_major; 491dbe32adSColy Li static DEFINE_IDA(bcache_device_idx); 50cafe5635SKent Overstreet static wait_queue_head_t unregister_wait; 51cafe5635SKent Overstreet struct workqueue_struct *bcache_wq; 52*afe78ab4SKai Krakow struct workqueue_struct *bch_flush_wq; 530f843e65SGuoju Fang struct workqueue_struct *bch_journal_wq; 54cafe5635SKent Overstreet 55a59ff6ccSColy Li 56cafe5635SKent Overstreet #define BTREE_MAX_PAGES (256 * 1024 / PAGE_SIZE) 571dbe32adSColy Li /* limitation of partitions number on single bcache device */ 581dbe32adSColy Li #define BCACHE_MINORS 128 591dbe32adSColy Li /* limitation of bcache devices number on single system */ 601dbe32adSColy Li #define BCACHE_DEVICE_IDX_MAX ((1U << MINORBITS)/BCACHE_MINORS) 61cafe5635SKent Overstreet 62cafe5635SKent Overstreet /* Superblock */ 63cafe5635SKent Overstreet 64ffa47032SColy Li static unsigned int get_bucket_size(struct cache_sb *sb, struct cache_sb_disk *s) 65ffa47032SColy Li { 66ffa47032SColy Li unsigned int bucket_size = le16_to_cpu(s->bucket_size); 67ffa47032SColy Li 68b16671e8SColy Li if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) { 69b16671e8SColy Li if (bch_has_feature_large_bucket(sb)) { 70b16671e8SColy Li unsigned int max, order; 71b16671e8SColy Li 72b16671e8SColy Li max = sizeof(unsigned int) * BITS_PER_BYTE - 1; 73b16671e8SColy Li order = le16_to_cpu(s->bucket_size); 74b16671e8SColy Li /* 75b16671e8SColy Li * bcache tool will make sure the overflow won't 76b16671e8SColy Li * happen, an error message here is enough. 77b16671e8SColy Li */ 78b16671e8SColy Li if (order > max) 79b16671e8SColy Li pr_err("Bucket size (1 << %u) overflows\n", 80b16671e8SColy Li order); 81b16671e8SColy Li bucket_size = 1 << order; 82b16671e8SColy Li } else if (bch_has_feature_obso_large_bucket(sb)) { 83b16671e8SColy Li bucket_size += 84b16671e8SColy Li le16_to_cpu(s->obso_bucket_size_hi) << 16; 85b16671e8SColy Li } 86b16671e8SColy Li } 87ffa47032SColy Li 88ffa47032SColy Li return bucket_size; 89ffa47032SColy Li } 90ffa47032SColy Li 915b21403cSColy Li static const char *read_super_common(struct cache_sb *sb, struct block_device *bdev, 925b21403cSColy Li struct cache_sb_disk *s) 93cafe5635SKent Overstreet { 94cafe5635SKent Overstreet const char *err; 956f10f7d1SColy Li unsigned int i; 96cafe5635SKent Overstreet 97cafe5635SKent Overstreet sb->first_bucket= le16_to_cpu(s->first_bucket); 985b21403cSColy Li sb->nbuckets = le64_to_cpu(s->nbuckets); 99ffa47032SColy Li sb->bucket_size = get_bucket_size(sb, s); 100cafe5635SKent Overstreet 1015b21403cSColy Li sb->nr_in_set = le16_to_cpu(s->nr_in_set); 1025b21403cSColy Li sb->nr_this_dev = le16_to_cpu(s->nr_this_dev); 103cafe5635SKent Overstreet 104cafe5635SKent Overstreet err = "Too many journal buckets"; 105cafe5635SKent Overstreet if (sb->keys > SB_JOURNAL_BUCKETS) 106cafe5635SKent Overstreet goto err; 107cafe5635SKent Overstreet 108cafe5635SKent Overstreet err = "Too many buckets"; 109cafe5635SKent Overstreet if (sb->nbuckets > LONG_MAX) 110cafe5635SKent Overstreet goto err; 111cafe5635SKent Overstreet 112cafe5635SKent Overstreet err = "Not enough buckets"; 113cafe5635SKent Overstreet if (sb->nbuckets < 1 << 7) 114cafe5635SKent Overstreet goto err; 115cafe5635SKent Overstreet 116c557a5f7SColy Li err = "Bad block size (not power of 2)"; 117c557a5f7SColy Li if (!is_power_of_2(sb->block_size)) 118c557a5f7SColy Li goto err; 119c557a5f7SColy Li 120c557a5f7SColy Li err = "Bad block size (larger than page size)"; 121c557a5f7SColy Li if (sb->block_size > PAGE_SECTORS) 122c557a5f7SColy Li goto err; 123c557a5f7SColy Li 124c557a5f7SColy Li err = "Bad bucket size (not power of 2)"; 125c557a5f7SColy Li if (!is_power_of_2(sb->bucket_size)) 126c557a5f7SColy Li goto err; 127c557a5f7SColy Li 128c557a5f7SColy Li err = "Bad bucket size (smaller than page size)"; 129c557a5f7SColy Li if (sb->bucket_size < PAGE_SECTORS) 1302903381fSKent Overstreet goto err; 1312903381fSKent Overstreet 132cafe5635SKent Overstreet err = "Invalid superblock: device too small"; 133b0d30981SColy Li if (get_capacity(bdev->bd_disk) < 134b0d30981SColy Li sb->bucket_size * sb->nbuckets) 135cafe5635SKent Overstreet goto err; 136cafe5635SKent Overstreet 137cafe5635SKent Overstreet err = "Bad UUID"; 138169ef1cfSKent Overstreet if (bch_is_zero(sb->set_uuid, 16)) 139cafe5635SKent Overstreet goto err; 140cafe5635SKent Overstreet 141cafe5635SKent Overstreet err = "Bad cache device number in set"; 142cafe5635SKent Overstreet if (!sb->nr_in_set || 143cafe5635SKent Overstreet sb->nr_in_set <= sb->nr_this_dev || 144cafe5635SKent Overstreet sb->nr_in_set > MAX_CACHES_PER_SET) 145cafe5635SKent Overstreet goto err; 146cafe5635SKent Overstreet 147cafe5635SKent Overstreet err = "Journal buckets not sequential"; 148cafe5635SKent Overstreet for (i = 0; i < sb->keys; i++) 149cafe5635SKent Overstreet if (sb->d[i] != sb->first_bucket + i) 150cafe5635SKent Overstreet goto err; 151cafe5635SKent Overstreet 152cafe5635SKent Overstreet err = "Too many journal buckets"; 153cafe5635SKent Overstreet if (sb->first_bucket + sb->keys > sb->nbuckets) 154cafe5635SKent Overstreet goto err; 155cafe5635SKent Overstreet 156cafe5635SKent Overstreet err = "Invalid superblock: first bucket comes before end of super"; 157cafe5635SKent Overstreet if (sb->first_bucket * sb->bucket_size < 16) 158cafe5635SKent Overstreet goto err; 1592903381fSKent Overstreet 1605b21403cSColy Li err = NULL; 1615b21403cSColy Li err: 1625b21403cSColy Li return err; 1635b21403cSColy Li } 1645b21403cSColy Li 1655b21403cSColy Li 166cafe5635SKent Overstreet static const char *read_super(struct cache_sb *sb, struct block_device *bdev, 167cafe5635SKent Overstreet struct cache_sb_disk **res) 168cafe5635SKent Overstreet { 169cafe5635SKent Overstreet const char *err; 170cafe5635SKent Overstreet struct cache_sb_disk *s; 171cafe5635SKent Overstreet struct page *page; 172cafe5635SKent Overstreet unsigned int i; 173cafe5635SKent Overstreet 174cafe5635SKent Overstreet page = read_cache_page_gfp(bdev->bd_inode->i_mapping, 175cafe5635SKent Overstreet SB_OFFSET >> PAGE_SHIFT, GFP_KERNEL); 176cafe5635SKent Overstreet if (IS_ERR(page)) 177cafe5635SKent Overstreet return "IO error"; 178cafe5635SKent Overstreet s = page_address(page) + offset_in_page(SB_OFFSET); 179cafe5635SKent Overstreet 180cafe5635SKent Overstreet sb->offset = le64_to_cpu(s->offset); 181cafe5635SKent Overstreet sb->version = le64_to_cpu(s->version); 182cafe5635SKent Overstreet 183cafe5635SKent Overstreet memcpy(sb->magic, s->magic, 16); 184cafe5635SKent Overstreet memcpy(sb->uuid, s->uuid, 16); 185cafe5635SKent Overstreet memcpy(sb->set_uuid, s->set_uuid, 16); 186cafe5635SKent Overstreet memcpy(sb->label, s->label, SB_LABEL_SIZE); 187cafe5635SKent Overstreet 188cafe5635SKent Overstreet sb->flags = le64_to_cpu(s->flags); 189cafe5635SKent Overstreet sb->seq = le64_to_cpu(s->seq); 190cafe5635SKent Overstreet sb->last_mount = le32_to_cpu(s->last_mount); 191cafe5635SKent Overstreet sb->keys = le16_to_cpu(s->keys); 192cafe5635SKent Overstreet 193cafe5635SKent Overstreet for (i = 0; i < SB_JOURNAL_BUCKETS; i++) 194cafe5635SKent Overstreet sb->d[i] = le64_to_cpu(s->d[i]); 195cafe5635SKent Overstreet 196cafe5635SKent Overstreet pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u\n", 197cafe5635SKent Overstreet sb->version, sb->flags, sb->seq, sb->keys); 198cafe5635SKent Overstreet 199cafe5635SKent Overstreet err = "Not a bcache superblock (bad offset)"; 200cafe5635SKent Overstreet if (sb->offset != SB_SECTOR) 201cafe5635SKent Overstreet goto err; 202cafe5635SKent Overstreet 203cafe5635SKent Overstreet err = "Not a bcache superblock (bad magic)"; 204cafe5635SKent Overstreet if (memcmp(sb->magic, bcache_magic, 16)) 205cafe5635SKent Overstreet goto err; 206cafe5635SKent Overstreet 207cafe5635SKent Overstreet err = "Bad checksum"; 208cafe5635SKent Overstreet if (s->csum != csum_set(s)) 209cafe5635SKent Overstreet goto err; 210cafe5635SKent Overstreet 211cafe5635SKent Overstreet err = "Bad UUID"; 212cafe5635SKent Overstreet if (bch_is_zero(sb->uuid, 16)) 213cafe5635SKent Overstreet goto err; 214cafe5635SKent Overstreet 215cafe5635SKent Overstreet sb->block_size = le16_to_cpu(s->block_size); 216cafe5635SKent Overstreet 217cafe5635SKent Overstreet err = "Superblock block size smaller than device block size"; 218cafe5635SKent Overstreet if (sb->block_size << 9 < bdev_logical_block_size(bdev)) 219cafe5635SKent Overstreet goto err; 220cafe5635SKent Overstreet 221cafe5635SKent Overstreet switch (sb->version) { 222cafe5635SKent Overstreet case BCACHE_SB_VERSION_BDEV: 223cafe5635SKent Overstreet sb->data_offset = BDEV_DATA_START_DEFAULT; 224cafe5635SKent Overstreet break; 225cafe5635SKent Overstreet case BCACHE_SB_VERSION_BDEV_WITH_OFFSET: 226d721a43fSColy Li case BCACHE_SB_VERSION_BDEV_WITH_FEATURES: 227cafe5635SKent Overstreet sb->data_offset = le64_to_cpu(s->data_offset); 228cafe5635SKent Overstreet 229cafe5635SKent Overstreet err = "Bad data offset"; 230cafe5635SKent Overstreet if (sb->data_offset < BDEV_DATA_START_DEFAULT) 231cafe5635SKent Overstreet goto err; 232cafe5635SKent Overstreet 233cafe5635SKent Overstreet break; 234cafe5635SKent Overstreet case BCACHE_SB_VERSION_CDEV: 235cafe5635SKent Overstreet case BCACHE_SB_VERSION_CDEV_WITH_UUID: 2365b21403cSColy Li err = read_super_common(sb, bdev, s); 2375b21403cSColy Li if (err) 238cafe5635SKent Overstreet goto err; 2392903381fSKent Overstreet break; 240d721a43fSColy Li case BCACHE_SB_VERSION_CDEV_WITH_FEATURES: 241ffa47032SColy Li /* 242ffa47032SColy Li * Feature bits are needed in read_super_common(), 243ffa47032SColy Li * convert them firstly. 244ffa47032SColy Li */ 245d721a43fSColy Li sb->feature_compat = le64_to_cpu(s->feature_compat); 246d721a43fSColy Li sb->feature_incompat = le64_to_cpu(s->feature_incompat); 247d721a43fSColy Li sb->feature_ro_compat = le64_to_cpu(s->feature_ro_compat); 2481dfc0686SColy Li 2491dfc0686SColy Li /* Check incompatible features */ 2501dfc0686SColy Li err = "Unsupported compatible feature found"; 2511dfc0686SColy Li if (bch_has_unknown_compat_features(sb)) 2521dfc0686SColy Li goto err; 2531dfc0686SColy Li 2541dfc0686SColy Li err = "Unsupported read-only compatible feature found"; 2551dfc0686SColy Li if (bch_has_unknown_ro_compat_features(sb)) 2561dfc0686SColy Li goto err; 2571dfc0686SColy Li 2581dfc0686SColy Li err = "Unsupported incompatible feature found"; 2591dfc0686SColy Li if (bch_has_unknown_incompat_features(sb)) 2601dfc0686SColy Li goto err; 2611dfc0686SColy Li 262ffa47032SColy Li err = read_super_common(sb, bdev, s); 263ffa47032SColy Li if (err) 264ffa47032SColy Li goto err; 2652903381fSKent Overstreet break; 2662903381fSKent Overstreet default: 2672903381fSKent Overstreet err = "Unsupported superblock version"; 2682903381fSKent Overstreet goto err; 2692903381fSKent Overstreet } 2702903381fSKent Overstreet 27175cbb3f1SArnd Bergmann sb->last_mount = (u32)ktime_get_real_seconds(); 272cfa0c56dSChristoph Hellwig *res = s; 2736321bef0SChristoph Hellwig return NULL; 274cafe5635SKent Overstreet err: 2756321bef0SChristoph Hellwig put_page(page); 276cafe5635SKent Overstreet return err; 277cafe5635SKent Overstreet } 278cafe5635SKent Overstreet 2794246a0b6SChristoph Hellwig static void write_bdev_super_endio(struct bio *bio) 280cafe5635SKent Overstreet { 281cafe5635SKent Overstreet struct cached_dev *dc = bio->bi_private; 28208ec1e62SColy Li 28308ec1e62SColy Li if (bio->bi_status) 28408ec1e62SColy Li bch_count_backing_io_errors(dc, bio); 285cafe5635SKent Overstreet 286cb7a583eSKent Overstreet closure_put(&dc->sb_write); 287cafe5635SKent Overstreet } 288cafe5635SKent Overstreet 289475389aeSChristoph Hellwig static void __write_super(struct cache_sb *sb, struct cache_sb_disk *out, 290475389aeSChristoph Hellwig struct bio *bio) 291cafe5635SKent Overstreet { 2926f10f7d1SColy Li unsigned int i; 293cafe5635SKent Overstreet 294475389aeSChristoph Hellwig bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_META; 2954f024f37SKent Overstreet bio->bi_iter.bi_sector = SB_SECTOR; 296475389aeSChristoph Hellwig __bio_add_page(bio, virt_to_page(out), SB_SIZE, 297475389aeSChristoph Hellwig offset_in_page(out)); 298cafe5635SKent Overstreet 299cafe5635SKent Overstreet out->offset = cpu_to_le64(sb->offset); 300cafe5635SKent Overstreet 301cafe5635SKent Overstreet memcpy(out->uuid, sb->uuid, 16); 302cafe5635SKent Overstreet memcpy(out->set_uuid, sb->set_uuid, 16); 303cafe5635SKent Overstreet memcpy(out->label, sb->label, SB_LABEL_SIZE); 304cafe5635SKent Overstreet 305cafe5635SKent Overstreet out->flags = cpu_to_le64(sb->flags); 306cafe5635SKent Overstreet out->seq = cpu_to_le64(sb->seq); 307cafe5635SKent Overstreet 308cafe5635SKent Overstreet out->last_mount = cpu_to_le32(sb->last_mount); 309cafe5635SKent Overstreet out->first_bucket = cpu_to_le16(sb->first_bucket); 310cafe5635SKent Overstreet out->keys = cpu_to_le16(sb->keys); 311cafe5635SKent Overstreet 312cafe5635SKent Overstreet for (i = 0; i < sb->keys; i++) 313cafe5635SKent Overstreet out->d[i] = cpu_to_le64(sb->d[i]); 314cafe5635SKent Overstreet 315d721a43fSColy Li if (sb->version >= BCACHE_SB_VERSION_CDEV_WITH_FEATURES) { 316d721a43fSColy Li out->feature_compat = cpu_to_le64(sb->feature_compat); 317d721a43fSColy Li out->feature_incompat = cpu_to_le64(sb->feature_incompat); 318d721a43fSColy Li out->feature_ro_compat = cpu_to_le64(sb->feature_ro_compat); 319d721a43fSColy Li } 320d721a43fSColy Li 321d721a43fSColy Li out->version = cpu_to_le64(sb->version); 322cafe5635SKent Overstreet out->csum = csum_set(out); 323cafe5635SKent Overstreet 32446f5aa88SJoe Perches pr_debug("ver %llu, flags %llu, seq %llu\n", 325cafe5635SKent Overstreet sb->version, sb->flags, sb->seq); 326cafe5635SKent Overstreet 3274e49ea4aSMike Christie submit_bio(bio); 328cafe5635SKent Overstreet } 329cafe5635SKent Overstreet 330cb7a583eSKent Overstreet static void bch_write_bdev_super_unlock(struct closure *cl) 331cb7a583eSKent Overstreet { 332cb7a583eSKent Overstreet struct cached_dev *dc = container_of(cl, struct cached_dev, sb_write); 333cb7a583eSKent Overstreet 334cb7a583eSKent Overstreet up(&dc->sb_write_mutex); 335cb7a583eSKent Overstreet } 336cb7a583eSKent Overstreet 337cafe5635SKent Overstreet void bch_write_bdev_super(struct cached_dev *dc, struct closure *parent) 338cafe5635SKent Overstreet { 339cb7a583eSKent Overstreet struct closure *cl = &dc->sb_write; 340cafe5635SKent Overstreet struct bio *bio = &dc->sb_bio; 341cafe5635SKent Overstreet 342cb7a583eSKent Overstreet down(&dc->sb_write_mutex); 343cb7a583eSKent Overstreet closure_init(cl, parent); 344cafe5635SKent Overstreet 345475389aeSChristoph Hellwig bio_init(bio, dc->sb_bv, 1); 34674d46992SChristoph Hellwig bio_set_dev(bio, dc->bdev); 347cafe5635SKent Overstreet bio->bi_end_io = write_bdev_super_endio; 348cafe5635SKent Overstreet bio->bi_private = dc; 349cafe5635SKent Overstreet 350cafe5635SKent Overstreet closure_get(cl); 35127a40ab9SColy Li /* I/O request sent to backing device */ 352475389aeSChristoph Hellwig __write_super(&dc->sb, dc->sb_disk, bio); 353cafe5635SKent Overstreet 354cb7a583eSKent Overstreet closure_return_with_destructor(cl, bch_write_bdev_super_unlock); 355cafe5635SKent Overstreet } 356cafe5635SKent Overstreet 3574246a0b6SChristoph Hellwig static void write_super_endio(struct bio *bio) 358cafe5635SKent Overstreet { 359cafe5635SKent Overstreet struct cache *ca = bio->bi_private; 360cafe5635SKent Overstreet 3615138ac67SColy Li /* is_read = 0 */ 3625138ac67SColy Li bch_count_io_errors(ca, bio->bi_status, 0, 3635138ac67SColy Li "writing superblock"); 364cb7a583eSKent Overstreet closure_put(&ca->set->sb_write); 365cb7a583eSKent Overstreet } 366cb7a583eSKent Overstreet 367cb7a583eSKent Overstreet static void bcache_write_super_unlock(struct closure *cl) 368cb7a583eSKent Overstreet { 369cb7a583eSKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, sb_write); 370cb7a583eSKent Overstreet 371cb7a583eSKent Overstreet up(&c->sb_write_mutex); 372cafe5635SKent Overstreet } 373cafe5635SKent Overstreet 374cafe5635SKent Overstreet void bcache_write_super(struct cache_set *c) 375cafe5635SKent Overstreet { 376cb7a583eSKent Overstreet struct closure *cl = &c->sb_write; 37708fdb2cdSColy Li struct cache *ca = c->cache; 37808fdb2cdSColy Li struct bio *bio = &ca->sb_bio; 37908fdb2cdSColy Li unsigned int version = BCACHE_SB_VERSION_CDEV_WITH_UUID; 380cafe5635SKent Overstreet 381cb7a583eSKent Overstreet down(&c->sb_write_mutex); 382cb7a583eSKent Overstreet closure_init(cl, &c->cl); 383cafe5635SKent Overstreet 3844a784266SColy Li ca->sb.seq++; 385cafe5635SKent Overstreet 3864a784266SColy Li if (ca->sb.version < version) 387d721a43fSColy Li ca->sb.version = version; 388cafe5635SKent Overstreet 389475389aeSChristoph Hellwig bio_init(bio, ca->sb_bv, 1); 39074d46992SChristoph Hellwig bio_set_dev(bio, ca->bdev); 391cafe5635SKent Overstreet bio->bi_end_io = write_super_endio; 392cafe5635SKent Overstreet bio->bi_private = ca; 393cafe5635SKent Overstreet 394cafe5635SKent Overstreet closure_get(cl); 395475389aeSChristoph Hellwig __write_super(&ca->sb, ca->sb_disk, bio); 396cafe5635SKent Overstreet 397cb7a583eSKent Overstreet closure_return_with_destructor(cl, bcache_write_super_unlock); 398cafe5635SKent Overstreet } 399cafe5635SKent Overstreet 400cafe5635SKent Overstreet /* UUID io */ 401cafe5635SKent Overstreet 4024246a0b6SChristoph Hellwig static void uuid_endio(struct bio *bio) 403cafe5635SKent Overstreet { 404cafe5635SKent Overstreet struct closure *cl = bio->bi_private; 405cb7a583eSKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 406cafe5635SKent Overstreet 4074e4cbee9SChristoph Hellwig cache_set_err_on(bio->bi_status, c, "accessing uuids"); 408cafe5635SKent Overstreet bch_bbio_free(bio, c); 409cafe5635SKent Overstreet closure_put(cl); 410cafe5635SKent Overstreet } 411cafe5635SKent Overstreet 412cb7a583eSKent Overstreet static void uuid_io_unlock(struct closure *cl) 413cb7a583eSKent Overstreet { 414cb7a583eSKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, uuid_write); 415cb7a583eSKent Overstreet 416cb7a583eSKent Overstreet up(&c->uuid_write_mutex); 417cb7a583eSKent Overstreet } 418cb7a583eSKent Overstreet 419ad0d9e76SMike Christie static void uuid_io(struct cache_set *c, int op, unsigned long op_flags, 420cafe5635SKent Overstreet struct bkey *k, struct closure *parent) 421cafe5635SKent Overstreet { 422cb7a583eSKent Overstreet struct closure *cl = &c->uuid_write; 423cafe5635SKent Overstreet struct uuid_entry *u; 4246f10f7d1SColy Li unsigned int i; 42585b1492eSKent Overstreet char buf[80]; 426cafe5635SKent Overstreet 427cafe5635SKent Overstreet BUG_ON(!parent); 428cb7a583eSKent Overstreet down(&c->uuid_write_mutex); 429cb7a583eSKent Overstreet closure_init(cl, parent); 430cafe5635SKent Overstreet 431cafe5635SKent Overstreet for (i = 0; i < KEY_PTRS(k); i++) { 432cafe5635SKent Overstreet struct bio *bio = bch_bbio_alloc(c); 433cafe5635SKent Overstreet 4341eff9d32SJens Axboe bio->bi_opf = REQ_SYNC | REQ_META | op_flags; 4354f024f37SKent Overstreet bio->bi_iter.bi_size = KEY_SIZE(k) << 9; 436cafe5635SKent Overstreet 437cafe5635SKent Overstreet bio->bi_end_io = uuid_endio; 438cafe5635SKent Overstreet bio->bi_private = cl; 439ad0d9e76SMike Christie bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags); 440169ef1cfSKent Overstreet bch_bio_map(bio, c->uuids); 441cafe5635SKent Overstreet 442cafe5635SKent Overstreet bch_submit_bbio(bio, c, k, i); 443cafe5635SKent Overstreet 444ad0d9e76SMike Christie if (op != REQ_OP_WRITE) 445cafe5635SKent Overstreet break; 446cafe5635SKent Overstreet } 447cafe5635SKent Overstreet 448dc9d98d6SKent Overstreet bch_extent_to_text(buf, sizeof(buf), k); 44946f5aa88SJoe Perches pr_debug("%s UUIDs at %s\n", op == REQ_OP_WRITE ? "wrote" : "read", buf); 450cafe5635SKent Overstreet 451cafe5635SKent Overstreet for (u = c->uuids; u < c->uuids + c->nr_uuids; u++) 452169ef1cfSKent Overstreet if (!bch_is_zero(u->uuid, 16)) 45346f5aa88SJoe Perches pr_debug("Slot %zi: %pU: %s: 1st: %u last: %u inv: %u\n", 454cafe5635SKent Overstreet u - c->uuids, u->uuid, u->label, 455cafe5635SKent Overstreet u->first_reg, u->last_reg, u->invalidated); 456cafe5635SKent Overstreet 457cb7a583eSKent Overstreet closure_return_with_destructor(cl, uuid_io_unlock); 458cafe5635SKent Overstreet } 459cafe5635SKent Overstreet 460cafe5635SKent Overstreet static char *uuid_read(struct cache_set *c, struct jset *j, struct closure *cl) 461cafe5635SKent Overstreet { 462cafe5635SKent Overstreet struct bkey *k = &j->uuid_bucket; 463cafe5635SKent Overstreet 46465d45231SKent Overstreet if (__bch_btree_ptr_invalid(c, k)) 465cafe5635SKent Overstreet return "bad uuid pointer"; 466cafe5635SKent Overstreet 467cafe5635SKent Overstreet bkey_copy(&c->uuid_bucket, k); 46870fd7614SChristoph Hellwig uuid_io(c, REQ_OP_READ, 0, k, cl); 469cafe5635SKent Overstreet 470cafe5635SKent Overstreet if (j->version < BCACHE_JSET_VERSION_UUIDv1) { 471cafe5635SKent Overstreet struct uuid_entry_v0 *u0 = (void *) c->uuids; 472cafe5635SKent Overstreet struct uuid_entry *u1 = (void *) c->uuids; 473cafe5635SKent Overstreet int i; 474cafe5635SKent Overstreet 475cafe5635SKent Overstreet closure_sync(cl); 476cafe5635SKent Overstreet 477cafe5635SKent Overstreet /* 478cafe5635SKent Overstreet * Since the new uuid entry is bigger than the old, we have to 479cafe5635SKent Overstreet * convert starting at the highest memory address and work down 480cafe5635SKent Overstreet * in order to do it in place 481cafe5635SKent Overstreet */ 482cafe5635SKent Overstreet 483cafe5635SKent Overstreet for (i = c->nr_uuids - 1; 484cafe5635SKent Overstreet i >= 0; 485cafe5635SKent Overstreet --i) { 486cafe5635SKent Overstreet memcpy(u1[i].uuid, u0[i].uuid, 16); 487cafe5635SKent Overstreet memcpy(u1[i].label, u0[i].label, 32); 488cafe5635SKent Overstreet 489cafe5635SKent Overstreet u1[i].first_reg = u0[i].first_reg; 490cafe5635SKent Overstreet u1[i].last_reg = u0[i].last_reg; 491cafe5635SKent Overstreet u1[i].invalidated = u0[i].invalidated; 492cafe5635SKent Overstreet 493cafe5635SKent Overstreet u1[i].flags = 0; 494cafe5635SKent Overstreet u1[i].sectors = 0; 495cafe5635SKent Overstreet } 496cafe5635SKent Overstreet } 497cafe5635SKent Overstreet 498cafe5635SKent Overstreet return NULL; 499cafe5635SKent Overstreet } 500cafe5635SKent Overstreet 501cafe5635SKent Overstreet static int __uuid_write(struct cache_set *c) 502cafe5635SKent Overstreet { 503cafe5635SKent Overstreet BKEY_PADDED(key) k; 504cafe5635SKent Overstreet struct closure cl; 5054a784266SColy Li struct cache *ca = c->cache; 50621e478ddSColy Li unsigned int size; 507cafe5635SKent Overstreet 5081fae7cf0SColy Li closure_init_stack(&cl); 509cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 510cafe5635SKent Overstreet 51117e4aed8SColy Li if (bch_bucket_alloc_set(c, RESERVE_BTREE, &k.key, true)) 512cafe5635SKent Overstreet return 1; 513cafe5635SKent Overstreet 5144a784266SColy Li size = meta_bucket_pages(&ca->sb) * PAGE_SECTORS; 51521e478ddSColy Li SET_KEY_SIZE(&k.key, size); 516ad0d9e76SMike Christie uuid_io(c, REQ_OP_WRITE, 0, &k.key, &cl); 517cafe5635SKent Overstreet closure_sync(&cl); 518cafe5635SKent Overstreet 5197a55948dSShenghui Wang /* Only one bucket used for uuid write */ 5207a55948dSShenghui Wang atomic_long_add(ca->sb.bucket_size, &ca->meta_sectors_written); 5217a55948dSShenghui Wang 522cafe5635SKent Overstreet bkey_copy(&c->uuid_bucket, &k.key); 5233a3b6a4eSKent Overstreet bkey_put(c, &k.key); 524cafe5635SKent Overstreet return 0; 525cafe5635SKent Overstreet } 526cafe5635SKent Overstreet 527cafe5635SKent Overstreet int bch_uuid_write(struct cache_set *c) 528cafe5635SKent Overstreet { 529cafe5635SKent Overstreet int ret = __uuid_write(c); 530cafe5635SKent Overstreet 531cafe5635SKent Overstreet if (!ret) 532cafe5635SKent Overstreet bch_journal_meta(c, NULL); 533cafe5635SKent Overstreet 534cafe5635SKent Overstreet return ret; 535cafe5635SKent Overstreet } 536cafe5635SKent Overstreet 537cafe5635SKent Overstreet static struct uuid_entry *uuid_find(struct cache_set *c, const char *uuid) 538cafe5635SKent Overstreet { 539cafe5635SKent Overstreet struct uuid_entry *u; 540cafe5635SKent Overstreet 541cafe5635SKent Overstreet for (u = c->uuids; 542cafe5635SKent Overstreet u < c->uuids + c->nr_uuids; u++) 543cafe5635SKent Overstreet if (!memcmp(u->uuid, uuid, 16)) 544cafe5635SKent Overstreet return u; 545cafe5635SKent Overstreet 546cafe5635SKent Overstreet return NULL; 547cafe5635SKent Overstreet } 548cafe5635SKent Overstreet 549cafe5635SKent Overstreet static struct uuid_entry *uuid_find_empty(struct cache_set *c) 550cafe5635SKent Overstreet { 551cafe5635SKent Overstreet static const char zero_uuid[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 5521fae7cf0SColy Li 553cafe5635SKent Overstreet return uuid_find(c, zero_uuid); 554cafe5635SKent Overstreet } 555cafe5635SKent Overstreet 556cafe5635SKent Overstreet /* 557cafe5635SKent Overstreet * Bucket priorities/gens: 558cafe5635SKent Overstreet * 559cafe5635SKent Overstreet * For each bucket, we store on disk its 560cafe5635SKent Overstreet * 8 bit gen 561cafe5635SKent Overstreet * 16 bit priority 562cafe5635SKent Overstreet * 563cafe5635SKent Overstreet * See alloc.c for an explanation of the gen. The priority is used to implement 564cafe5635SKent Overstreet * lru (and in the future other) cache replacement policies; for most purposes 565cafe5635SKent Overstreet * it's just an opaque integer. 566cafe5635SKent Overstreet * 567cafe5635SKent Overstreet * The gens and the priorities don't have a whole lot to do with each other, and 568cafe5635SKent Overstreet * it's actually the gens that must be written out at specific times - it's no 569cafe5635SKent Overstreet * big deal if the priorities don't get written, if we lose them we just reuse 570cafe5635SKent Overstreet * buckets in suboptimal order. 571cafe5635SKent Overstreet * 572cafe5635SKent Overstreet * On disk they're stored in a packed array, and in as many buckets are required 573cafe5635SKent Overstreet * to fit them all. The buckets we use to store them form a list; the journal 574cafe5635SKent Overstreet * header points to the first bucket, the first bucket points to the second 575cafe5635SKent Overstreet * bucket, et cetera. 576cafe5635SKent Overstreet * 577cafe5635SKent Overstreet * This code is used by the allocation code; periodically (whenever it runs out 578cafe5635SKent Overstreet * of buckets to allocate from) the allocation code will invalidate some 579cafe5635SKent Overstreet * buckets, but it can't use those buckets until their new gens are safely on 580cafe5635SKent Overstreet * disk. 581cafe5635SKent Overstreet */ 582cafe5635SKent Overstreet 5834246a0b6SChristoph Hellwig static void prio_endio(struct bio *bio) 584cafe5635SKent Overstreet { 585cafe5635SKent Overstreet struct cache *ca = bio->bi_private; 586cafe5635SKent Overstreet 5874e4cbee9SChristoph Hellwig cache_set_err_on(bio->bi_status, ca->set, "accessing priorities"); 588cafe5635SKent Overstreet bch_bbio_free(bio, ca->set); 589cafe5635SKent Overstreet closure_put(&ca->prio); 590cafe5635SKent Overstreet } 591cafe5635SKent Overstreet 592ad0d9e76SMike Christie static void prio_io(struct cache *ca, uint64_t bucket, int op, 593ad0d9e76SMike Christie unsigned long op_flags) 594cafe5635SKent Overstreet { 595cafe5635SKent Overstreet struct closure *cl = &ca->prio; 596cafe5635SKent Overstreet struct bio *bio = bch_bbio_alloc(ca->set); 597cafe5635SKent Overstreet 598cafe5635SKent Overstreet closure_init_stack(cl); 599cafe5635SKent Overstreet 6004f024f37SKent Overstreet bio->bi_iter.bi_sector = bucket * ca->sb.bucket_size; 60174d46992SChristoph Hellwig bio_set_dev(bio, ca->bdev); 602c954ac8dSColy Li bio->bi_iter.bi_size = meta_bucket_bytes(&ca->sb); 603cafe5635SKent Overstreet 604cafe5635SKent Overstreet bio->bi_end_io = prio_endio; 605cafe5635SKent Overstreet bio->bi_private = ca; 606ad0d9e76SMike Christie bio_set_op_attrs(bio, op, REQ_SYNC|REQ_META|op_flags); 607169ef1cfSKent Overstreet bch_bio_map(bio, ca->disk_buckets); 608cafe5635SKent Overstreet 609771f393eSColy Li closure_bio_submit(ca->set, bio, &ca->prio); 610cafe5635SKent Overstreet closure_sync(cl); 611cafe5635SKent Overstreet } 612cafe5635SKent Overstreet 61384c529aeSAndrea Righi int bch_prio_write(struct cache *ca, bool wait) 614cafe5635SKent Overstreet { 615cafe5635SKent Overstreet int i; 616cafe5635SKent Overstreet struct bucket *b; 617cafe5635SKent Overstreet struct closure cl; 618cafe5635SKent Overstreet 61946f5aa88SJoe Perches pr_debug("free_prio=%zu, free_none=%zu, free_inc=%zu\n", 62084c529aeSAndrea Righi fifo_used(&ca->free[RESERVE_PRIO]), 62184c529aeSAndrea Righi fifo_used(&ca->free[RESERVE_NONE]), 62284c529aeSAndrea Righi fifo_used(&ca->free_inc)); 62384c529aeSAndrea Righi 62484c529aeSAndrea Righi /* 62584c529aeSAndrea Righi * Pre-check if there are enough free buckets. In the non-blocking 62684c529aeSAndrea Righi * scenario it's better to fail early rather than starting to allocate 62784c529aeSAndrea Righi * buckets and do a cleanup later in case of failure. 62884c529aeSAndrea Righi */ 62984c529aeSAndrea Righi if (!wait) { 63084c529aeSAndrea Righi size_t avail = fifo_used(&ca->free[RESERVE_PRIO]) + 63184c529aeSAndrea Righi fifo_used(&ca->free[RESERVE_NONE]); 63284c529aeSAndrea Righi if (prio_buckets(ca) > avail) 63384c529aeSAndrea Righi return -ENOMEM; 63484c529aeSAndrea Righi } 63584c529aeSAndrea Righi 636cafe5635SKent Overstreet closure_init_stack(&cl); 637cafe5635SKent Overstreet 638cafe5635SKent Overstreet lockdep_assert_held(&ca->set->bucket_lock); 639cafe5635SKent Overstreet 640cafe5635SKent Overstreet ca->disk_buckets->seq++; 641cafe5635SKent Overstreet 642cafe5635SKent Overstreet atomic_long_add(ca->sb.bucket_size * prio_buckets(ca), 643cafe5635SKent Overstreet &ca->meta_sectors_written); 644cafe5635SKent Overstreet 645cafe5635SKent Overstreet for (i = prio_buckets(ca) - 1; i >= 0; --i) { 646cafe5635SKent Overstreet long bucket; 647cafe5635SKent Overstreet struct prio_set *p = ca->disk_buckets; 648b1a67b0fSKent Overstreet struct bucket_disk *d = p->data; 649b1a67b0fSKent Overstreet struct bucket_disk *end = d + prios_per_bucket(ca); 650cafe5635SKent Overstreet 651cafe5635SKent Overstreet for (b = ca->buckets + i * prios_per_bucket(ca); 652cafe5635SKent Overstreet b < ca->buckets + ca->sb.nbuckets && d < end; 653cafe5635SKent Overstreet b++, d++) { 654cafe5635SKent Overstreet d->prio = cpu_to_le16(b->prio); 655cafe5635SKent Overstreet d->gen = b->gen; 656cafe5635SKent Overstreet } 657cafe5635SKent Overstreet 658cafe5635SKent Overstreet p->next_bucket = ca->prio_buckets[i + 1]; 65981ab4190SKent Overstreet p->magic = pset_magic(&ca->sb); 660c954ac8dSColy Li p->csum = bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8); 661cafe5635SKent Overstreet 66284c529aeSAndrea Righi bucket = bch_bucket_alloc(ca, RESERVE_PRIO, wait); 663cafe5635SKent Overstreet BUG_ON(bucket == -1); 664cafe5635SKent Overstreet 665cafe5635SKent Overstreet mutex_unlock(&ca->set->bucket_lock); 666ad0d9e76SMike Christie prio_io(ca, bucket, REQ_OP_WRITE, 0); 667cafe5635SKent Overstreet mutex_lock(&ca->set->bucket_lock); 668cafe5635SKent Overstreet 669cafe5635SKent Overstreet ca->prio_buckets[i] = bucket; 670cafe5635SKent Overstreet atomic_dec_bug(&ca->buckets[bucket].pin); 671cafe5635SKent Overstreet } 672cafe5635SKent Overstreet 673cafe5635SKent Overstreet mutex_unlock(&ca->set->bucket_lock); 674cafe5635SKent Overstreet 675cafe5635SKent Overstreet bch_journal_meta(ca->set, &cl); 676cafe5635SKent Overstreet closure_sync(&cl); 677cafe5635SKent Overstreet 678cafe5635SKent Overstreet mutex_lock(&ca->set->bucket_lock); 679cafe5635SKent Overstreet 680cafe5635SKent Overstreet /* 681cafe5635SKent Overstreet * Don't want the old priorities to get garbage collected until after we 682cafe5635SKent Overstreet * finish writing the new ones, and they're journalled 683cafe5635SKent Overstreet */ 6842531d9eeSKent Overstreet for (i = 0; i < prio_buckets(ca); i++) { 6852531d9eeSKent Overstreet if (ca->prio_last_buckets[i]) 6862531d9eeSKent Overstreet __bch_bucket_free(ca, 6872531d9eeSKent Overstreet &ca->buckets[ca->prio_last_buckets[i]]); 6882531d9eeSKent Overstreet 689cafe5635SKent Overstreet ca->prio_last_buckets[i] = ca->prio_buckets[i]; 690cafe5635SKent Overstreet } 69184c529aeSAndrea Righi return 0; 6922531d9eeSKent Overstreet } 693cafe5635SKent Overstreet 69449d08d59SColy Li static int prio_read(struct cache *ca, uint64_t bucket) 695cafe5635SKent Overstreet { 696cafe5635SKent Overstreet struct prio_set *p = ca->disk_buckets; 697cafe5635SKent Overstreet struct bucket_disk *d = p->data + prios_per_bucket(ca), *end = d; 698cafe5635SKent Overstreet struct bucket *b; 6996f10f7d1SColy Li unsigned int bucket_nr = 0; 70049d08d59SColy Li int ret = -EIO; 701cafe5635SKent Overstreet 702cafe5635SKent Overstreet for (b = ca->buckets; 703cafe5635SKent Overstreet b < ca->buckets + ca->sb.nbuckets; 704cafe5635SKent Overstreet b++, d++) { 705cafe5635SKent Overstreet if (d == end) { 706cafe5635SKent Overstreet ca->prio_buckets[bucket_nr] = bucket; 707cafe5635SKent Overstreet ca->prio_last_buckets[bucket_nr] = bucket; 708cafe5635SKent Overstreet bucket_nr++; 709cafe5635SKent Overstreet 71070fd7614SChristoph Hellwig prio_io(ca, bucket, REQ_OP_READ, 0); 711cafe5635SKent Overstreet 712b0d30981SColy Li if (p->csum != 713c954ac8dSColy Li bch_crc64(&p->magic, meta_bucket_bytes(&ca->sb) - 8)) { 71446f5aa88SJoe Perches pr_warn("bad csum reading priorities\n"); 71549d08d59SColy Li goto out; 71649d08d59SColy Li } 717cafe5635SKent Overstreet 71849d08d59SColy Li if (p->magic != pset_magic(&ca->sb)) { 71946f5aa88SJoe Perches pr_warn("bad magic reading priorities\n"); 72049d08d59SColy Li goto out; 72149d08d59SColy Li } 722cafe5635SKent Overstreet 723cafe5635SKent Overstreet bucket = p->next_bucket; 724cafe5635SKent Overstreet d = p->data; 725cafe5635SKent Overstreet } 726cafe5635SKent Overstreet 727cafe5635SKent Overstreet b->prio = le16_to_cpu(d->prio); 7283a2fd9d5SKent Overstreet b->gen = b->last_gc = d->gen; 729cafe5635SKent Overstreet } 73049d08d59SColy Li 73149d08d59SColy Li ret = 0; 73249d08d59SColy Li out: 73349d08d59SColy Li return ret; 734cafe5635SKent Overstreet } 735cafe5635SKent Overstreet 736cafe5635SKent Overstreet /* Bcache device */ 737cafe5635SKent Overstreet 738cafe5635SKent Overstreet static int open_dev(struct block_device *b, fmode_t mode) 739cafe5635SKent Overstreet { 740cafe5635SKent Overstreet struct bcache_device *d = b->bd_disk->private_data; 7411fae7cf0SColy Li 742c4d951ddSKent Overstreet if (test_bit(BCACHE_DEV_CLOSING, &d->flags)) 743cafe5635SKent Overstreet return -ENXIO; 744cafe5635SKent Overstreet 745cafe5635SKent Overstreet closure_get(&d->cl); 746cafe5635SKent Overstreet return 0; 747cafe5635SKent Overstreet } 748cafe5635SKent Overstreet 749867e1162SEmil Goode static void release_dev(struct gendisk *b, fmode_t mode) 750cafe5635SKent Overstreet { 751cafe5635SKent Overstreet struct bcache_device *d = b->private_data; 7521fae7cf0SColy Li 753cafe5635SKent Overstreet closure_put(&d->cl); 754cafe5635SKent Overstreet } 755cafe5635SKent Overstreet 756cafe5635SKent Overstreet static int ioctl_dev(struct block_device *b, fmode_t mode, 757cafe5635SKent Overstreet unsigned int cmd, unsigned long arg) 758cafe5635SKent Overstreet { 759cafe5635SKent Overstreet struct bcache_device *d = b->bd_disk->private_data; 7600f0709e6SColy Li 761cafe5635SKent Overstreet return d->ioctl(d, mode, cmd, arg); 762cafe5635SKent Overstreet } 763cafe5635SKent Overstreet 764c62b37d9SChristoph Hellwig static const struct block_device_operations bcache_cached_ops = { 765c62b37d9SChristoph Hellwig .submit_bio = cached_dev_submit_bio, 766c62b37d9SChristoph Hellwig .open = open_dev, 767c62b37d9SChristoph Hellwig .release = release_dev, 768c62b37d9SChristoph Hellwig .ioctl = ioctl_dev, 769c62b37d9SChristoph Hellwig .owner = THIS_MODULE, 770c62b37d9SChristoph Hellwig }; 771c62b37d9SChristoph Hellwig 772c62b37d9SChristoph Hellwig static const struct block_device_operations bcache_flash_ops = { 773c62b37d9SChristoph Hellwig .submit_bio = flash_dev_submit_bio, 774cafe5635SKent Overstreet .open = open_dev, 775cafe5635SKent Overstreet .release = release_dev, 776cafe5635SKent Overstreet .ioctl = ioctl_dev, 777cafe5635SKent Overstreet .owner = THIS_MODULE, 778cafe5635SKent Overstreet }; 779cafe5635SKent Overstreet 780cafe5635SKent Overstreet void bcache_device_stop(struct bcache_device *d) 781cafe5635SKent Overstreet { 782c4d951ddSKent Overstreet if (!test_and_set_bit(BCACHE_DEV_CLOSING, &d->flags)) 78363d63b51SColy Li /* 78463d63b51SColy Li * closure_fn set to 78563d63b51SColy Li * - cached device: cached_dev_flush() 78663d63b51SColy Li * - flash dev: flash_dev_flush() 78763d63b51SColy Li */ 788cafe5635SKent Overstreet closure_queue(&d->cl); 789cafe5635SKent Overstreet } 790cafe5635SKent Overstreet 791ee668506SKent Overstreet static void bcache_device_unlink(struct bcache_device *d) 792ee668506SKent Overstreet { 793c4d951ddSKent Overstreet lockdep_assert_held(&bch_register_lock); 794c4d951ddSKent Overstreet 795c4d951ddSKent Overstreet if (d->c && !test_and_set_bit(BCACHE_DEV_UNLINK_DONE, &d->flags)) { 79608fdb2cdSColy Li struct cache *ca = d->c->cache; 797ee668506SKent Overstreet 798ee668506SKent Overstreet sysfs_remove_link(&d->c->kobj, d->name); 799ee668506SKent Overstreet sysfs_remove_link(&d->kobj, "cache"); 800ee668506SKent Overstreet 801ee668506SKent Overstreet bd_unlink_disk_holder(ca->bdev, d->disk); 802ee668506SKent Overstreet } 803c4d951ddSKent Overstreet } 804ee668506SKent Overstreet 805ee668506SKent Overstreet static void bcache_device_link(struct bcache_device *d, struct cache_set *c, 806ee668506SKent Overstreet const char *name) 807ee668506SKent Overstreet { 80808fdb2cdSColy Li struct cache *ca = c->cache; 8094b6efb4bSColy Li int ret; 810ee668506SKent Overstreet 811ee668506SKent Overstreet bd_link_disk_holder(ca->bdev, d->disk); 812ee668506SKent Overstreet 813ee668506SKent Overstreet snprintf(d->name, BCACHEDEVNAME_SIZE, 814ee668506SKent Overstreet "%s%u", name, d->id); 815ee668506SKent Overstreet 8164b6efb4bSColy Li ret = sysfs_create_link(&d->kobj, &c->kobj, "cache"); 8174b6efb4bSColy Li if (ret < 0) 81846f5aa88SJoe Perches pr_err("Couldn't create device -> cache set symlink\n"); 8194b6efb4bSColy Li 8204b6efb4bSColy Li ret = sysfs_create_link(&c->kobj, &d->kobj, d->name); 8214b6efb4bSColy Li if (ret < 0) 82246f5aa88SJoe Perches pr_err("Couldn't create cache set -> device symlink\n"); 823fecaee6fSZheng Liu 824fecaee6fSZheng Liu clear_bit(BCACHE_DEV_UNLINK_DONE, &d->flags); 825ee668506SKent Overstreet } 826ee668506SKent Overstreet 827cafe5635SKent Overstreet static void bcache_device_detach(struct bcache_device *d) 828cafe5635SKent Overstreet { 829cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 830cafe5635SKent Overstreet 831ea8c5356SColy Li atomic_dec(&d->c->attached_dev_nr); 832ea8c5356SColy Li 833c4d951ddSKent Overstreet if (test_bit(BCACHE_DEV_DETACHING, &d->flags)) { 834cafe5635SKent Overstreet struct uuid_entry *u = d->c->uuids + d->id; 835cafe5635SKent Overstreet 836cafe5635SKent Overstreet SET_UUID_FLASH_ONLY(u, 0); 837cafe5635SKent Overstreet memcpy(u->uuid, invalid_uuid, 16); 83875cbb3f1SArnd Bergmann u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds()); 839cafe5635SKent Overstreet bch_uuid_write(d->c); 840cafe5635SKent Overstreet } 841cafe5635SKent Overstreet 842ee668506SKent Overstreet bcache_device_unlink(d); 843ee668506SKent Overstreet 844cafe5635SKent Overstreet d->c->devices[d->id] = NULL; 845cafe5635SKent Overstreet closure_put(&d->c->caching); 846cafe5635SKent Overstreet d->c = NULL; 847cafe5635SKent Overstreet } 848cafe5635SKent Overstreet 849cafe5635SKent Overstreet static void bcache_device_attach(struct bcache_device *d, struct cache_set *c, 8506f10f7d1SColy Li unsigned int id) 851cafe5635SKent Overstreet { 852cafe5635SKent Overstreet d->id = id; 853cafe5635SKent Overstreet d->c = c; 854cafe5635SKent Overstreet c->devices[id] = d; 855cafe5635SKent Overstreet 8562831231dSColy Li if (id >= c->devices_max_used) 8572831231dSColy Li c->devices_max_used = id + 1; 8582831231dSColy Li 859cafe5635SKent Overstreet closure_get(&c->caching); 860cafe5635SKent Overstreet } 861cafe5635SKent Overstreet 8621dbe32adSColy Li static inline int first_minor_to_idx(int first_minor) 8631dbe32adSColy Li { 8641dbe32adSColy Li return (first_minor/BCACHE_MINORS); 8651dbe32adSColy Li } 8661dbe32adSColy Li 8671dbe32adSColy Li static inline int idx_to_first_minor(int idx) 8681dbe32adSColy Li { 8691dbe32adSColy Li return (idx * BCACHE_MINORS); 8701dbe32adSColy Li } 8711dbe32adSColy Li 872cafe5635SKent Overstreet static void bcache_device_free(struct bcache_device *d) 873cafe5635SKent Overstreet { 8742d886951SColy Li struct gendisk *disk = d->disk; 8752d886951SColy Li 876cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 877cafe5635SKent Overstreet 8782d886951SColy Li if (disk) 87946f5aa88SJoe Perches pr_info("%s stopped\n", disk->disk_name); 8802d886951SColy Li else 88146f5aa88SJoe Perches pr_err("bcache device (NULL gendisk) stopped\n"); 882cafe5635SKent Overstreet 883cafe5635SKent Overstreet if (d->c) 884cafe5635SKent Overstreet bcache_device_detach(d); 8852d886951SColy Li 8862d886951SColy Li if (disk) { 88786da9f73SColy Li bool disk_added = (disk->flags & GENHD_FL_UP) != 0; 88886da9f73SColy Li 88986da9f73SColy Li if (disk_added) 8902d886951SColy Li del_gendisk(disk); 8912d886951SColy Li 8922d886951SColy Li if (disk->queue) 8932d886951SColy Li blk_cleanup_queue(disk->queue); 8942d886951SColy Li 8951dbe32adSColy Li ida_simple_remove(&bcache_device_idx, 8962d886951SColy Li first_minor_to_idx(disk->first_minor)); 89786da9f73SColy Li if (disk_added) 8982d886951SColy Li put_disk(disk); 89928935ab5SKent Overstreet } 900cafe5635SKent Overstreet 901d19936a2SKent Overstreet bioset_exit(&d->bio_split); 902958b4338SPekka Enberg kvfree(d->full_dirty_stripes); 903958b4338SPekka Enberg kvfree(d->stripe_sectors_dirty); 904cafe5635SKent Overstreet 905cafe5635SKent Overstreet closure_debug_destroy(&d->cl); 906cafe5635SKent Overstreet } 907cafe5635SKent Overstreet 9086f10f7d1SColy Li static int bcache_device_init(struct bcache_device *d, unsigned int block_size, 909c62b37d9SChristoph Hellwig sector_t sectors, struct block_device *cached_bdev, 910c62b37d9SChristoph Hellwig const struct block_device_operations *ops) 911cafe5635SKent Overstreet { 912cafe5635SKent Overstreet struct request_queue *q; 9135f2b18ecSBart Van Assche const size_t max_stripes = min_t(size_t, INT_MAX, 9145f2b18ecSBart Van Assche SIZE_MAX / sizeof(atomic_t)); 91565f0f017SColy Li uint64_t n; 9161dbe32adSColy Li int idx; 917279afbadSKent Overstreet 9182d679fc7SKent Overstreet if (!d->stripe_size) 9192d679fc7SKent Overstreet d->stripe_size = 1 << 31; 920279afbadSKent Overstreet 92165f0f017SColy Li n = DIV_ROUND_UP_ULL(sectors, d->stripe_size); 92265f0f017SColy Li if (!n || n > max_stripes) { 92365f0f017SColy Li pr_err("nr_stripes too large or invalid: %llu (start sector beyond end of disk?)\n", 92465f0f017SColy Li n); 925279afbadSKent Overstreet return -ENOMEM; 92648a915a8SKent Overstreet } 92765f0f017SColy Li d->nr_stripes = n; 928279afbadSKent Overstreet 929279afbadSKent Overstreet n = d->nr_stripes * sizeof(atomic_t); 930bc4e54f6SMichal Hocko d->stripe_sectors_dirty = kvzalloc(n, GFP_KERNEL); 931279afbadSKent Overstreet if (!d->stripe_sectors_dirty) 932279afbadSKent Overstreet return -ENOMEM; 933cafe5635SKent Overstreet 93448a915a8SKent Overstreet n = BITS_TO_LONGS(d->nr_stripes) * sizeof(unsigned long); 935bc4e54f6SMichal Hocko d->full_dirty_stripes = kvzalloc(n, GFP_KERNEL); 93648a915a8SKent Overstreet if (!d->full_dirty_stripes) 93748a915a8SKent Overstreet return -ENOMEM; 93848a915a8SKent Overstreet 9391dbe32adSColy Li idx = ida_simple_get(&bcache_device_idx, 0, 9401dbe32adSColy Li BCACHE_DEVICE_IDX_MAX, GFP_KERNEL); 9411dbe32adSColy Li if (idx < 0) 9421dbe32adSColy Li return idx; 943b8c0d911SEric Wheeler 944d19936a2SKent Overstreet if (bioset_init(&d->bio_split, 4, offsetof(struct bbio, bio), 9459b4e9f5aSFlorian Schmaus BIOSET_NEED_BVECS|BIOSET_NEED_RESCUER)) 9469b4e9f5aSFlorian Schmaus goto err; 9479b4e9f5aSFlorian Schmaus 9489b4e9f5aSFlorian Schmaus d->disk = alloc_disk(BCACHE_MINORS); 9499b4e9f5aSFlorian Schmaus if (!d->disk) 9509b4e9f5aSFlorian Schmaus goto err; 951cafe5635SKent Overstreet 952279afbadSKent Overstreet set_capacity(d->disk, sectors); 9531dbe32adSColy Li snprintf(d->disk->disk_name, DISK_NAME_LEN, "bcache%i", idx); 954cafe5635SKent Overstreet 955cafe5635SKent Overstreet d->disk->major = bcache_major; 9561dbe32adSColy Li d->disk->first_minor = idx_to_first_minor(idx); 957c62b37d9SChristoph Hellwig d->disk->fops = ops; 958cafe5635SKent Overstreet d->disk->private_data = d; 959cafe5635SKent Overstreet 960c62b37d9SChristoph Hellwig q = blk_alloc_queue(NUMA_NO_NODE); 96128935ab5SKent Overstreet if (!q) 96228935ab5SKent Overstreet return -ENOMEM; 96328935ab5SKent Overstreet 964cafe5635SKent Overstreet d->disk->queue = q; 965cafe5635SKent Overstreet q->limits.max_hw_sectors = UINT_MAX; 966cafe5635SKent Overstreet q->limits.max_sectors = UINT_MAX; 967cafe5635SKent Overstreet q->limits.max_segment_size = UINT_MAX; 968cafe5635SKent Overstreet q->limits.max_segments = BIO_MAX_PAGES; 9692bb4cd5cSJens Axboe blk_queue_max_discard_sectors(q, UINT_MAX); 97090db6919SKent Overstreet q->limits.discard_granularity = 512; 971cafe5635SKent Overstreet q->limits.io_min = block_size; 972cafe5635SKent Overstreet q->limits.logical_block_size = block_size; 973cafe5635SKent Overstreet q->limits.physical_block_size = block_size; 974dcacbc12SMauricio Faria de Oliveira 975dcacbc12SMauricio Faria de Oliveira if (q->limits.logical_block_size > PAGE_SIZE && cached_bdev) { 976dcacbc12SMauricio Faria de Oliveira /* 977dcacbc12SMauricio Faria de Oliveira * This should only happen with BCACHE_SB_VERSION_BDEV. 978dcacbc12SMauricio Faria de Oliveira * Block/page size is checked for BCACHE_SB_VERSION_CDEV. 979dcacbc12SMauricio Faria de Oliveira */ 9804b25bbf5SColy Li pr_info("%s: sb/logical block size (%u) greater than page size (%lu) falling back to device logical block size (%u)\n", 981dcacbc12SMauricio Faria de Oliveira d->disk->disk_name, q->limits.logical_block_size, 982dcacbc12SMauricio Faria de Oliveira PAGE_SIZE, bdev_logical_block_size(cached_bdev)); 983dcacbc12SMauricio Faria de Oliveira 984dcacbc12SMauricio Faria de Oliveira /* This also adjusts physical block size/min io size if needed */ 985dcacbc12SMauricio Faria de Oliveira blk_queue_logical_block_size(q, bdev_logical_block_size(cached_bdev)); 986dcacbc12SMauricio Faria de Oliveira } 987dcacbc12SMauricio Faria de Oliveira 98844e1ebe2SBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NONROT, d->disk->queue); 98944e1ebe2SBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, d->disk->queue); 99044e1ebe2SBart Van Assche blk_queue_flag_set(QUEUE_FLAG_DISCARD, d->disk->queue); 991cafe5635SKent Overstreet 99284b4ff9eSJens Axboe blk_queue_write_cache(q, true, true); 99354d12f2bSKent Overstreet 994cafe5635SKent Overstreet return 0; 9959b4e9f5aSFlorian Schmaus 9969b4e9f5aSFlorian Schmaus err: 9979b4e9f5aSFlorian Schmaus ida_simple_remove(&bcache_device_idx, idx); 9989b4e9f5aSFlorian Schmaus return -ENOMEM; 9999b4e9f5aSFlorian Schmaus 1000cafe5635SKent Overstreet } 1001cafe5635SKent Overstreet 1002cafe5635SKent Overstreet /* Cached device */ 1003cafe5635SKent Overstreet 1004cafe5635SKent Overstreet static void calc_cached_dev_sectors(struct cache_set *c) 1005cafe5635SKent Overstreet { 1006cafe5635SKent Overstreet uint64_t sectors = 0; 1007cafe5635SKent Overstreet struct cached_dev *dc; 1008cafe5635SKent Overstreet 1009cafe5635SKent Overstreet list_for_each_entry(dc, &c->cached_devs, list) 1010cafe5635SKent Overstreet sectors += bdev_sectors(dc->bdev); 1011cafe5635SKent Overstreet 1012cafe5635SKent Overstreet c->cached_dev_sectors = sectors; 1013cafe5635SKent Overstreet } 1014cafe5635SKent Overstreet 10150f0709e6SColy Li #define BACKING_DEV_OFFLINE_TIMEOUT 5 10160f0709e6SColy Li static int cached_dev_status_update(void *arg) 10170f0709e6SColy Li { 10180f0709e6SColy Li struct cached_dev *dc = arg; 10190f0709e6SColy Li struct request_queue *q; 10200f0709e6SColy Li 10210f0709e6SColy Li /* 10220f0709e6SColy Li * If this delayed worker is stopping outside, directly quit here. 10230f0709e6SColy Li * dc->io_disable might be set via sysfs interface, so check it 10240f0709e6SColy Li * here too. 10250f0709e6SColy Li */ 10260f0709e6SColy Li while (!kthread_should_stop() && !dc->io_disable) { 10270f0709e6SColy Li q = bdev_get_queue(dc->bdev); 10280f0709e6SColy Li if (blk_queue_dying(q)) 10290f0709e6SColy Li dc->offline_seconds++; 10300f0709e6SColy Li else 10310f0709e6SColy Li dc->offline_seconds = 0; 10320f0709e6SColy Li 10330f0709e6SColy Li if (dc->offline_seconds >= BACKING_DEV_OFFLINE_TIMEOUT) { 103446f5aa88SJoe Perches pr_err("%s: device offline for %d seconds\n", 10350f0709e6SColy Li dc->backing_dev_name, 10360f0709e6SColy Li BACKING_DEV_OFFLINE_TIMEOUT); 103746f5aa88SJoe Perches pr_err("%s: disable I/O request due to backing device offline\n", 103846f5aa88SJoe Perches dc->disk.name); 10390f0709e6SColy Li dc->io_disable = true; 10400f0709e6SColy Li /* let others know earlier that io_disable is true */ 10410f0709e6SColy Li smp_mb(); 10420f0709e6SColy Li bcache_device_stop(&dc->disk); 10430f0709e6SColy Li break; 10440f0709e6SColy Li } 10450f0709e6SColy Li schedule_timeout_interruptible(HZ); 10460f0709e6SColy Li } 10470f0709e6SColy Li 10480f0709e6SColy Li wait_for_kthread_stop(); 10490f0709e6SColy Li return 0; 10500f0709e6SColy Li } 10510f0709e6SColy Li 10520f0709e6SColy Li 10530b13efecSColy Li int bch_cached_dev_run(struct cached_dev *dc) 1054cafe5635SKent Overstreet { 1055cafe5635SKent Overstreet struct bcache_device *d = &dc->disk; 1056792732d9SGeliang Tang char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL); 1057a25c32beSGabriel de Perthuis char *env[] = { 1058a25c32beSGabriel de Perthuis "DRIVER=bcache", 1059a25c32beSGabriel de Perthuis kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid), 1060792732d9SGeliang Tang kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""), 1061ab9e1400SGabriel de Perthuis NULL, 1062a25c32beSGabriel de Perthuis }; 1063cafe5635SKent Overstreet 1064e0faa3d7SColy Li if (dc->io_disable) { 106546f5aa88SJoe Perches pr_err("I/O disabled on cached dev %s\n", 1066e0faa3d7SColy Li dc->backing_dev_name); 10675d9e06d6SWei Yongjun kfree(env[1]); 10685d9e06d6SWei Yongjun kfree(env[2]); 10695d9e06d6SWei Yongjun kfree(buf); 10700b13efecSColy Li return -EIO; 1071e0faa3d7SColy Li } 10720b13efecSColy Li 10734d4d8573SAl Viro if (atomic_xchg(&dc->running, 1)) { 10744d4d8573SAl Viro kfree(env[1]); 10754d4d8573SAl Viro kfree(env[2]); 1076792732d9SGeliang Tang kfree(buf); 107746f5aa88SJoe Perches pr_info("cached dev %s is running already\n", 1078e0faa3d7SColy Li dc->backing_dev_name); 10790b13efecSColy Li return -EBUSY; 10804d4d8573SAl Viro } 1081cafe5635SKent Overstreet 1082cafe5635SKent Overstreet if (!d->c && 1083cafe5635SKent Overstreet BDEV_STATE(&dc->sb) != BDEV_STATE_NONE) { 1084cafe5635SKent Overstreet struct closure cl; 10851fae7cf0SColy Li 1086cafe5635SKent Overstreet closure_init_stack(&cl); 1087cafe5635SKent Overstreet 1088cafe5635SKent Overstreet SET_BDEV_STATE(&dc->sb, BDEV_STATE_STALE); 1089cafe5635SKent Overstreet bch_write_bdev_super(dc, &cl); 1090cafe5635SKent Overstreet closure_sync(&cl); 1091cafe5635SKent Overstreet } 1092cafe5635SKent Overstreet 1093cafe5635SKent Overstreet add_disk(d->disk); 1094ee668506SKent Overstreet bd_link_disk_holder(dc->bdev, dc->disk.disk); 10953be11dbaSColy Li /* 10963be11dbaSColy Li * won't show up in the uevent file, use udevadm monitor -e instead 10973be11dbaSColy Li * only class / kset properties are persistent 10983be11dbaSColy Li */ 1099cafe5635SKent Overstreet kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); 1100a25c32beSGabriel de Perthuis kfree(env[1]); 1101ab9e1400SGabriel de Perthuis kfree(env[2]); 1102792732d9SGeliang Tang kfree(buf); 1103a25c32beSGabriel de Perthuis 1104cafe5635SKent Overstreet if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") || 11050b13efecSColy Li sysfs_create_link(&disk_to_dev(d->disk)->kobj, 11060b13efecSColy Li &d->kobj, "bcache")) { 110746f5aa88SJoe Perches pr_err("Couldn't create bcache dev <-> disk sysfs symlinks\n"); 11080b13efecSColy Li return -ENOMEM; 11090b13efecSColy Li } 11100f0709e6SColy Li 11110f0709e6SColy Li dc->status_update_thread = kthread_run(cached_dev_status_update, 11120f0709e6SColy Li dc, "bcache_status_update"); 11130f0709e6SColy Li if (IS_ERR(dc->status_update_thread)) { 111446f5aa88SJoe Perches pr_warn("failed to create bcache_status_update kthread, continue to run without monitoring backing device status\n"); 11150f0709e6SColy Li } 11160b13efecSColy Li 11170b13efecSColy Li return 0; 1118cafe5635SKent Overstreet } 1119cafe5635SKent Overstreet 11203fd47bfeSColy Li /* 11213fd47bfeSColy Li * If BCACHE_DEV_RATE_DW_RUNNING is set, it means routine of the delayed 11223fd47bfeSColy Li * work dc->writeback_rate_update is running. Wait until the routine 11233fd47bfeSColy Li * quits (BCACHE_DEV_RATE_DW_RUNNING is clear), then continue to 11243fd47bfeSColy Li * cancel it. If BCACHE_DEV_RATE_DW_RUNNING is not clear after time_out 11253fd47bfeSColy Li * seconds, give up waiting here and continue to cancel it too. 11263fd47bfeSColy Li */ 11273fd47bfeSColy Li static void cancel_writeback_rate_update_dwork(struct cached_dev *dc) 11283fd47bfeSColy Li { 11293fd47bfeSColy Li int time_out = WRITEBACK_RATE_UPDATE_SECS_MAX * HZ; 11303fd47bfeSColy Li 11313fd47bfeSColy Li do { 11323fd47bfeSColy Li if (!test_bit(BCACHE_DEV_RATE_DW_RUNNING, 11333fd47bfeSColy Li &dc->disk.flags)) 11343fd47bfeSColy Li break; 11353fd47bfeSColy Li time_out--; 11363fd47bfeSColy Li schedule_timeout_interruptible(1); 11373fd47bfeSColy Li } while (time_out > 0); 11383fd47bfeSColy Li 11393fd47bfeSColy Li if (time_out == 0) 114046f5aa88SJoe Perches pr_warn("give up waiting for dc->writeback_write_update to quit\n"); 11413fd47bfeSColy Li 11423fd47bfeSColy Li cancel_delayed_work_sync(&dc->writeback_rate_update); 11433fd47bfeSColy Li } 11443fd47bfeSColy Li 1145cafe5635SKent Overstreet static void cached_dev_detach_finish(struct work_struct *w) 1146cafe5635SKent Overstreet { 1147cafe5635SKent Overstreet struct cached_dev *dc = container_of(w, struct cached_dev, detach); 1148cafe5635SKent Overstreet 1149c4d951ddSKent Overstreet BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)); 11503b304d24SElena Reshetova BUG_ON(refcount_read(&dc->count)); 1151cafe5635SKent Overstreet 1152cafe5635SKent Overstreet 11533fd47bfeSColy Li if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)) 11543fd47bfeSColy Li cancel_writeback_rate_update_dwork(dc); 11553fd47bfeSColy Li 11568d29c442STang Junhui if (!IS_ERR_OR_NULL(dc->writeback_thread)) { 11578d29c442STang Junhui kthread_stop(dc->writeback_thread); 11588d29c442STang Junhui dc->writeback_thread = NULL; 11598d29c442STang Junhui } 11608d29c442STang Junhui 116197ba3b81SColy Li mutex_lock(&bch_register_lock); 116297ba3b81SColy Li 116346010141SShenghui Wang calc_cached_dev_sectors(dc->disk.c); 1164cafe5635SKent Overstreet bcache_device_detach(&dc->disk); 1165cafe5635SKent Overstreet list_move(&dc->list, &uncached_devices); 1166cafe5635SKent Overstreet 1167c4d951ddSKent Overstreet clear_bit(BCACHE_DEV_DETACHING, &dc->disk.flags); 11685b1016e6SKent Overstreet clear_bit(BCACHE_DEV_UNLINK_DONE, &dc->disk.flags); 1169c4d951ddSKent Overstreet 1170cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1171cafe5635SKent Overstreet 117246f5aa88SJoe Perches pr_info("Caching disabled for %s\n", dc->backing_dev_name); 1173cafe5635SKent Overstreet 1174cafe5635SKent Overstreet /* Drop ref we took in cached_dev_detach() */ 1175cafe5635SKent Overstreet closure_put(&dc->disk.cl); 1176cafe5635SKent Overstreet } 1177cafe5635SKent Overstreet 1178cafe5635SKent Overstreet void bch_cached_dev_detach(struct cached_dev *dc) 1179cafe5635SKent Overstreet { 1180cafe5635SKent Overstreet lockdep_assert_held(&bch_register_lock); 1181cafe5635SKent Overstreet 1182c4d951ddSKent Overstreet if (test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags)) 1183cafe5635SKent Overstreet return; 1184cafe5635SKent Overstreet 1185c4d951ddSKent Overstreet if (test_and_set_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)) 1186cafe5635SKent Overstreet return; 1187cafe5635SKent Overstreet 1188cafe5635SKent Overstreet /* 1189cafe5635SKent Overstreet * Block the device from being closed and freed until we're finished 1190cafe5635SKent Overstreet * detaching 1191cafe5635SKent Overstreet */ 1192cafe5635SKent Overstreet closure_get(&dc->disk.cl); 1193cafe5635SKent Overstreet 1194cafe5635SKent Overstreet bch_writeback_queue(dc); 11953fd47bfeSColy Li 1196cafe5635SKent Overstreet cached_dev_put(dc); 1197cafe5635SKent Overstreet } 1198cafe5635SKent Overstreet 119973ac105bSTang Junhui int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c, 120073ac105bSTang Junhui uint8_t *set_uuid) 1201cafe5635SKent Overstreet { 120275cbb3f1SArnd Bergmann uint32_t rtime = cpu_to_le32((u32)ktime_get_real_seconds()); 1203cafe5635SKent Overstreet struct uuid_entry *u; 120486755b7aSMichael Lyle struct cached_dev *exist_dc, *t; 12050b13efecSColy Li int ret = 0; 1206cafe5635SKent Overstreet 12071132e56eSColy Li if ((set_uuid && memcmp(set_uuid, c->set_uuid, 16)) || 12081132e56eSColy Li (!set_uuid && memcmp(dc->sb.set_uuid, c->set_uuid, 16))) 1209cafe5635SKent Overstreet return -ENOENT; 1210cafe5635SKent Overstreet 1211cafe5635SKent Overstreet if (dc->disk.c) { 121246f5aa88SJoe Perches pr_err("Can't attach %s: already attached\n", 12136e916a7eSColy Li dc->backing_dev_name); 1214cafe5635SKent Overstreet return -EINVAL; 1215cafe5635SKent Overstreet } 1216cafe5635SKent Overstreet 1217cafe5635SKent Overstreet if (test_bit(CACHE_SET_STOPPING, &c->flags)) { 121846f5aa88SJoe Perches pr_err("Can't attach %s: shutting down\n", 12196e916a7eSColy Li dc->backing_dev_name); 1220cafe5635SKent Overstreet return -EINVAL; 1221cafe5635SKent Overstreet } 1222cafe5635SKent Overstreet 12234a784266SColy Li if (dc->sb.block_size < c->cache->sb.block_size) { 1224cafe5635SKent Overstreet /* Will die */ 122546f5aa88SJoe Perches pr_err("Couldn't attach %s: block size less than set's block size\n", 12266e916a7eSColy Li dc->backing_dev_name); 1227cafe5635SKent Overstreet return -EINVAL; 1228cafe5635SKent Overstreet } 1229cafe5635SKent Overstreet 123086755b7aSMichael Lyle /* Check whether already attached */ 123186755b7aSMichael Lyle list_for_each_entry_safe(exist_dc, t, &c->cached_devs, list) { 123286755b7aSMichael Lyle if (!memcmp(dc->sb.uuid, exist_dc->sb.uuid, 16)) { 123346f5aa88SJoe Perches pr_err("Tried to attach %s but duplicate UUID already attached\n", 12346e916a7eSColy Li dc->backing_dev_name); 123586755b7aSMichael Lyle 123686755b7aSMichael Lyle return -EINVAL; 123786755b7aSMichael Lyle } 123886755b7aSMichael Lyle } 123986755b7aSMichael Lyle 1240cafe5635SKent Overstreet u = uuid_find(c, dc->sb.uuid); 1241cafe5635SKent Overstreet 1242cafe5635SKent Overstreet if (u && 1243cafe5635SKent Overstreet (BDEV_STATE(&dc->sb) == BDEV_STATE_STALE || 1244cafe5635SKent Overstreet BDEV_STATE(&dc->sb) == BDEV_STATE_NONE)) { 1245cafe5635SKent Overstreet memcpy(u->uuid, invalid_uuid, 16); 124675cbb3f1SArnd Bergmann u->invalidated = cpu_to_le32((u32)ktime_get_real_seconds()); 1247cafe5635SKent Overstreet u = NULL; 1248cafe5635SKent Overstreet } 1249cafe5635SKent Overstreet 1250cafe5635SKent Overstreet if (!u) { 1251cafe5635SKent Overstreet if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { 125246f5aa88SJoe Perches pr_err("Couldn't find uuid for %s in set\n", 12536e916a7eSColy Li dc->backing_dev_name); 1254cafe5635SKent Overstreet return -ENOENT; 1255cafe5635SKent Overstreet } 1256cafe5635SKent Overstreet 1257cafe5635SKent Overstreet u = uuid_find_empty(c); 1258cafe5635SKent Overstreet if (!u) { 125946f5aa88SJoe Perches pr_err("Not caching %s, no room for UUID\n", 12606e916a7eSColy Li dc->backing_dev_name); 1261cafe5635SKent Overstreet return -EINVAL; 1262cafe5635SKent Overstreet } 1263cafe5635SKent Overstreet } 1264cafe5635SKent Overstreet 12653be11dbaSColy Li /* 12663be11dbaSColy Li * Deadlocks since we're called via sysfs... 12673be11dbaSColy Li * sysfs_remove_file(&dc->kobj, &sysfs_attach); 1268cafe5635SKent Overstreet */ 1269cafe5635SKent Overstreet 1270169ef1cfSKent Overstreet if (bch_is_zero(u->uuid, 16)) { 1271cafe5635SKent Overstreet struct closure cl; 12721fae7cf0SColy Li 1273cafe5635SKent Overstreet closure_init_stack(&cl); 1274cafe5635SKent Overstreet 1275cafe5635SKent Overstreet memcpy(u->uuid, dc->sb.uuid, 16); 1276cafe5635SKent Overstreet memcpy(u->label, dc->sb.label, SB_LABEL_SIZE); 1277cafe5635SKent Overstreet u->first_reg = u->last_reg = rtime; 1278cafe5635SKent Overstreet bch_uuid_write(c); 1279cafe5635SKent Overstreet 12801132e56eSColy Li memcpy(dc->sb.set_uuid, c->set_uuid, 16); 1281cafe5635SKent Overstreet SET_BDEV_STATE(&dc->sb, BDEV_STATE_CLEAN); 1282cafe5635SKent Overstreet 1283cafe5635SKent Overstreet bch_write_bdev_super(dc, &cl); 1284cafe5635SKent Overstreet closure_sync(&cl); 1285cafe5635SKent Overstreet } else { 1286cafe5635SKent Overstreet u->last_reg = rtime; 1287cafe5635SKent Overstreet bch_uuid_write(c); 1288cafe5635SKent Overstreet } 1289cafe5635SKent Overstreet 1290cafe5635SKent Overstreet bcache_device_attach(&dc->disk, c, u - c->uuids); 1291cafe5635SKent Overstreet list_move(&dc->list, &c->cached_devs); 1292cafe5635SKent Overstreet calc_cached_dev_sectors(c); 1293cafe5635SKent Overstreet 1294cafe5635SKent Overstreet /* 1295cafe5635SKent Overstreet * dc->c must be set before dc->count != 0 - paired with the mb in 1296cafe5635SKent Overstreet * cached_dev_get() 1297cafe5635SKent Overstreet */ 1298eb2b3d03SColy Li smp_wmb(); 12993b304d24SElena Reshetova refcount_set(&dc->count, 1); 1300cafe5635SKent Overstreet 130107cc6ef8SEric Wheeler /* Block writeback thread, but spawn it */ 130207cc6ef8SEric Wheeler down_write(&dc->writeback_lock); 130307cc6ef8SEric Wheeler if (bch_cached_dev_writeback_start(dc)) { 130407cc6ef8SEric Wheeler up_write(&dc->writeback_lock); 130546f5aa88SJoe Perches pr_err("Couldn't start writeback facilities for %s\n", 1306633bb2ceSColy Li dc->disk.disk->disk_name); 13079e5c3535SSlava Pestov return -ENOMEM; 130807cc6ef8SEric Wheeler } 13099e5c3535SSlava Pestov 1310cafe5635SKent Overstreet if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { 1311cafe5635SKent Overstreet atomic_set(&dc->has_dirty, 1); 1312cafe5635SKent Overstreet bch_writeback_queue(dc); 1313cafe5635SKent Overstreet } 1314cafe5635SKent Overstreet 13152e17a262STang Junhui bch_sectors_dirty_init(&dc->disk); 13162e17a262STang Junhui 13170b13efecSColy Li ret = bch_cached_dev_run(dc); 13180b13efecSColy Li if (ret && (ret != -EBUSY)) { 13190b13efecSColy Li up_write(&dc->writeback_lock); 13205c2a634cSColy Li /* 13215c2a634cSColy Li * bch_register_lock is held, bcache_device_stop() is not 13225c2a634cSColy Li * able to be directly called. The kthread and kworker 13235c2a634cSColy Li * created previously in bch_cached_dev_writeback_start() 13245c2a634cSColy Li * have to be stopped manually here. 13255c2a634cSColy Li */ 13265c2a634cSColy Li kthread_stop(dc->writeback_thread); 13275c2a634cSColy Li cancel_writeback_rate_update_dwork(dc); 132846f5aa88SJoe Perches pr_err("Couldn't run cached device %s\n", 1329633bb2ceSColy Li dc->backing_dev_name); 13300b13efecSColy Li return ret; 13310b13efecSColy Li } 13320b13efecSColy Li 1333ee668506SKent Overstreet bcache_device_link(&dc->disk, c, "bdev"); 1334ea8c5356SColy Li atomic_inc(&c->attached_dev_nr); 1335cafe5635SKent Overstreet 13365342fd42SColy Li if (bch_has_feature_obso_large_bucket(&(c->cache->sb))) { 13375342fd42SColy Li pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n"); 13385342fd42SColy Li pr_err("Please update to the latest bcache-tools to create the cache device\n"); 13395342fd42SColy Li set_disk_ro(dc->disk.disk, 1); 13405342fd42SColy Li } 13415342fd42SColy Li 134207cc6ef8SEric Wheeler /* Allow the writeback thread to proceed */ 134307cc6ef8SEric Wheeler up_write(&dc->writeback_lock); 134407cc6ef8SEric Wheeler 134546f5aa88SJoe Perches pr_info("Caching %s as %s on set %pU\n", 13466e916a7eSColy Li dc->backing_dev_name, 13476e916a7eSColy Li dc->disk.disk->disk_name, 13481132e56eSColy Li dc->disk.c->set_uuid); 1349cafe5635SKent Overstreet return 0; 1350cafe5635SKent Overstreet } 1351cafe5635SKent Overstreet 13522d17456eSColy Li /* when dc->disk.kobj released */ 1353cafe5635SKent Overstreet void bch_cached_dev_release(struct kobject *kobj) 1354cafe5635SKent Overstreet { 1355cafe5635SKent Overstreet struct cached_dev *dc = container_of(kobj, struct cached_dev, 1356cafe5635SKent Overstreet disk.kobj); 1357cafe5635SKent Overstreet kfree(dc); 1358cafe5635SKent Overstreet module_put(THIS_MODULE); 1359cafe5635SKent Overstreet } 1360cafe5635SKent Overstreet 1361cafe5635SKent Overstreet static void cached_dev_free(struct closure *cl) 1362cafe5635SKent Overstreet { 1363cafe5635SKent Overstreet struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); 1364cafe5635SKent Overstreet 13653fd47bfeSColy Li if (test_and_clear_bit(BCACHE_DEV_WB_RUNNING, &dc->disk.flags)) 13663fd47bfeSColy Li cancel_writeback_rate_update_dwork(dc); 13673fd47bfeSColy Li 1368a664d0f0SSlava Pestov if (!IS_ERR_OR_NULL(dc->writeback_thread)) 13695e6926daSKent Overstreet kthread_stop(dc->writeback_thread); 13700f0709e6SColy Li if (!IS_ERR_OR_NULL(dc->status_update_thread)) 13710f0709e6SColy Li kthread_stop(dc->status_update_thread); 1372cafe5635SKent Overstreet 137380265d8dSColy Li mutex_lock(&bch_register_lock); 137480265d8dSColy Li 1375f59fce84SKent Overstreet if (atomic_read(&dc->running)) 1376ee668506SKent Overstreet bd_unlink_disk_holder(dc->bdev, dc->disk.disk); 1377cafe5635SKent Overstreet bcache_device_free(&dc->disk); 1378cafe5635SKent Overstreet list_del(&dc->list); 1379cafe5635SKent Overstreet 1380cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1381cafe5635SKent Overstreet 1382475389aeSChristoph Hellwig if (dc->sb_disk) 1383475389aeSChristoph Hellwig put_page(virt_to_page(dc->sb_disk)); 1384e8547d42SLiang Chen 13850781c874SKent Overstreet if (!IS_ERR_OR_NULL(dc->bdev)) 1386cafe5635SKent Overstreet blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 1387cafe5635SKent Overstreet 1388cafe5635SKent Overstreet wake_up(&unregister_wait); 1389cafe5635SKent Overstreet 1390cafe5635SKent Overstreet kobject_put(&dc->disk.kobj); 1391cafe5635SKent Overstreet } 1392cafe5635SKent Overstreet 1393cafe5635SKent Overstreet static void cached_dev_flush(struct closure *cl) 1394cafe5635SKent Overstreet { 1395cafe5635SKent Overstreet struct cached_dev *dc = container_of(cl, struct cached_dev, disk.cl); 1396cafe5635SKent Overstreet struct bcache_device *d = &dc->disk; 1397cafe5635SKent Overstreet 1398c9502ea4SKent Overstreet mutex_lock(&bch_register_lock); 1399c9502ea4SKent Overstreet bcache_device_unlink(d); 1400c9502ea4SKent Overstreet mutex_unlock(&bch_register_lock); 1401c9502ea4SKent Overstreet 1402cafe5635SKent Overstreet bch_cache_accounting_destroy(&dc->accounting); 1403cafe5635SKent Overstreet kobject_del(&d->kobj); 1404cafe5635SKent Overstreet 1405cafe5635SKent Overstreet continue_at(cl, cached_dev_free, system_wq); 1406cafe5635SKent Overstreet } 1407cafe5635SKent Overstreet 14086f10f7d1SColy Li static int cached_dev_init(struct cached_dev *dc, unsigned int block_size) 1409cafe5635SKent Overstreet { 1410f59fce84SKent Overstreet int ret; 1411cafe5635SKent Overstreet struct io *io; 1412f59fce84SKent Overstreet struct request_queue *q = bdev_get_queue(dc->bdev); 1413cafe5635SKent Overstreet 1414cafe5635SKent Overstreet __module_get(THIS_MODULE); 1415cafe5635SKent Overstreet INIT_LIST_HEAD(&dc->list); 1416f59fce84SKent Overstreet closure_init(&dc->disk.cl, NULL); 1417f59fce84SKent Overstreet set_closure_fn(&dc->disk.cl, cached_dev_flush, system_wq); 1418cafe5635SKent Overstreet kobject_init(&dc->disk.kobj, &bch_cached_dev_ktype); 1419cafe5635SKent Overstreet INIT_WORK(&dc->detach, cached_dev_detach_finish); 1420cb7a583eSKent Overstreet sema_init(&dc->sb_write_mutex, 1); 1421f59fce84SKent Overstreet INIT_LIST_HEAD(&dc->io_lru); 1422f59fce84SKent Overstreet spin_lock_init(&dc->io_lock); 1423f59fce84SKent Overstreet bch_cache_accounting_init(&dc->accounting, &dc->disk.cl); 1424cafe5635SKent Overstreet 1425cafe5635SKent Overstreet dc->sequential_cutoff = 4 << 20; 1426cafe5635SKent Overstreet 1427cafe5635SKent Overstreet for (io = dc->io; io < dc->io + RECENT_IO; io++) { 1428cafe5635SKent Overstreet list_add(&io->lru, &dc->io_lru); 1429cafe5635SKent Overstreet hlist_add_head(&io->hash, dc->io_hash + RECENT_IO); 1430cafe5635SKent Overstreet } 1431cafe5635SKent Overstreet 1432c78afc62SKent Overstreet dc->disk.stripe_size = q->limits.io_opt >> 9; 1433c78afc62SKent Overstreet 1434c78afc62SKent Overstreet if (dc->disk.stripe_size) 1435c78afc62SKent Overstreet dc->partial_stripes_expensive = 1436c78afc62SKent Overstreet q->limits.raid_partial_stripes_expensive; 1437c78afc62SKent Overstreet 1438279afbadSKent Overstreet ret = bcache_device_init(&dc->disk, block_size, 1439a782483cSChristoph Hellwig bdev_nr_sectors(dc->bdev) - dc->sb.data_offset, 1440c62b37d9SChristoph Hellwig dc->bdev, &bcache_cached_ops); 1441f59fce84SKent Overstreet if (ret) 1442f59fce84SKent Overstreet return ret; 1443f59fce84SKent Overstreet 14445d4ce78bSChristoph Hellwig blk_queue_io_opt(dc->disk.disk->queue, 14455d4ce78bSChristoph Hellwig max(queue_io_opt(dc->disk.disk->queue), queue_io_opt(q))); 1446f59fce84SKent Overstreet 1447c7b7bd07SColy Li atomic_set(&dc->io_errors, 0); 1448c7b7bd07SColy Li dc->io_disable = false; 1449c7b7bd07SColy Li dc->error_limit = DEFAULT_CACHED_DEV_ERROR_LIMIT; 14507e027ca4SColy Li /* default to auto */ 14517e027ca4SColy Li dc->stop_when_cache_set_failed = BCH_CACHED_DEV_STOP_AUTO; 14527e027ca4SColy Li 1453f59fce84SKent Overstreet bch_cached_dev_request_init(dc); 1454f59fce84SKent Overstreet bch_cached_dev_writeback_init(dc); 1455cafe5635SKent Overstreet return 0; 1456cafe5635SKent Overstreet } 1457cafe5635SKent Overstreet 1458cafe5635SKent Overstreet /* Cached device - bcache superblock */ 1459cafe5635SKent Overstreet 1460cfa0c56dSChristoph Hellwig static int register_bdev(struct cache_sb *sb, struct cache_sb_disk *sb_disk, 1461cafe5635SKent Overstreet struct block_device *bdev, 1462cafe5635SKent Overstreet struct cached_dev *dc) 1463cafe5635SKent Overstreet { 1464cafe5635SKent Overstreet const char *err = "cannot allocate memory"; 1465cafe5635SKent Overstreet struct cache_set *c; 14660b13efecSColy Li int ret = -ENOMEM; 1467cafe5635SKent Overstreet 14686e916a7eSColy Li bdevname(bdev, dc->backing_dev_name); 1469cafe5635SKent Overstreet memcpy(&dc->sb, sb, sizeof(struct cache_sb)); 1470cafe5635SKent Overstreet dc->bdev = bdev; 1471cafe5635SKent Overstreet dc->bdev->bd_holder = dc; 1472475389aeSChristoph Hellwig dc->sb_disk = sb_disk; 14736e916a7eSColy Li 1474f59fce84SKent Overstreet if (cached_dev_init(dc, sb->block_size << 9)) 1475f59fce84SKent Overstreet goto err; 1476cafe5635SKent Overstreet 1477cafe5635SKent Overstreet err = "error creating kobject"; 14788d65269fSChristoph Hellwig if (kobject_add(&dc->disk.kobj, bdev_kobj(bdev), "bcache")) 1479cafe5635SKent Overstreet goto err; 1480cafe5635SKent Overstreet if (bch_cache_accounting_add_kobjs(&dc->accounting, &dc->disk.kobj)) 1481cafe5635SKent Overstreet goto err; 1482cafe5635SKent Overstreet 148346f5aa88SJoe Perches pr_info("registered backing device %s\n", dc->backing_dev_name); 1484f59fce84SKent Overstreet 1485cafe5635SKent Overstreet list_add(&dc->list, &uncached_devices); 1486e57fd746SColy Li /* attach to a matched cache set if it exists */ 1487cafe5635SKent Overstreet list_for_each_entry(c, &bch_cache_sets, list) 148873ac105bSTang Junhui bch_cached_dev_attach(dc, c, NULL); 1489cafe5635SKent Overstreet 1490cafe5635SKent Overstreet if (BDEV_STATE(&dc->sb) == BDEV_STATE_NONE || 14910b13efecSColy Li BDEV_STATE(&dc->sb) == BDEV_STATE_STALE) { 14920b13efecSColy Li err = "failed to run cached device"; 14930b13efecSColy Li ret = bch_cached_dev_run(dc); 14940b13efecSColy Li if (ret) 14950b13efecSColy Li goto err; 14960b13efecSColy Li } 1497cafe5635SKent Overstreet 149888c12d42SColy Li return 0; 1499cafe5635SKent Overstreet err: 150046f5aa88SJoe Perches pr_notice("error %s: %s\n", dc->backing_dev_name, err); 1501f59fce84SKent Overstreet bcache_device_stop(&dc->disk); 15020b13efecSColy Li return ret; 1503cafe5635SKent Overstreet } 1504cafe5635SKent Overstreet 1505cafe5635SKent Overstreet /* Flash only volumes */ 1506cafe5635SKent Overstreet 15072d17456eSColy Li /* When d->kobj released */ 1508cafe5635SKent Overstreet void bch_flash_dev_release(struct kobject *kobj) 1509cafe5635SKent Overstreet { 1510cafe5635SKent Overstreet struct bcache_device *d = container_of(kobj, struct bcache_device, 1511cafe5635SKent Overstreet kobj); 1512cafe5635SKent Overstreet kfree(d); 1513cafe5635SKent Overstreet } 1514cafe5635SKent Overstreet 1515cafe5635SKent Overstreet static void flash_dev_free(struct closure *cl) 1516cafe5635SKent Overstreet { 1517cafe5635SKent Overstreet struct bcache_device *d = container_of(cl, struct bcache_device, cl); 15181fae7cf0SColy Li 1519e5112201SSlava Pestov mutex_lock(&bch_register_lock); 152099a27d59STang Junhui atomic_long_sub(bcache_dev_sectors_dirty(d), 152199a27d59STang Junhui &d->c->flash_dev_dirty_sectors); 1522cafe5635SKent Overstreet bcache_device_free(d); 1523e5112201SSlava Pestov mutex_unlock(&bch_register_lock); 1524cafe5635SKent Overstreet kobject_put(&d->kobj); 1525cafe5635SKent Overstreet } 1526cafe5635SKent Overstreet 1527cafe5635SKent Overstreet static void flash_dev_flush(struct closure *cl) 1528cafe5635SKent Overstreet { 1529cafe5635SKent Overstreet struct bcache_device *d = container_of(cl, struct bcache_device, cl); 1530cafe5635SKent Overstreet 1531e5112201SSlava Pestov mutex_lock(&bch_register_lock); 1532ee668506SKent Overstreet bcache_device_unlink(d); 1533e5112201SSlava Pestov mutex_unlock(&bch_register_lock); 1534cafe5635SKent Overstreet kobject_del(&d->kobj); 1535cafe5635SKent Overstreet continue_at(cl, flash_dev_free, system_wq); 1536cafe5635SKent Overstreet } 1537cafe5635SKent Overstreet 1538cafe5635SKent Overstreet static int flash_dev_run(struct cache_set *c, struct uuid_entry *u) 1539cafe5635SKent Overstreet { 1540cafe5635SKent Overstreet struct bcache_device *d = kzalloc(sizeof(struct bcache_device), 1541cafe5635SKent Overstreet GFP_KERNEL); 1542cafe5635SKent Overstreet if (!d) 1543cafe5635SKent Overstreet return -ENOMEM; 1544cafe5635SKent Overstreet 1545cafe5635SKent Overstreet closure_init(&d->cl, NULL); 1546cafe5635SKent Overstreet set_closure_fn(&d->cl, flash_dev_flush, system_wq); 1547cafe5635SKent Overstreet 1548cafe5635SKent Overstreet kobject_init(&d->kobj, &bch_flash_dev_ktype); 1549cafe5635SKent Overstreet 15504e1ebae3SColy Li if (bcache_device_init(d, block_bytes(c->cache), u->sectors, 1551c62b37d9SChristoph Hellwig NULL, &bcache_flash_ops)) 1552cafe5635SKent Overstreet goto err; 1553cafe5635SKent Overstreet 1554cafe5635SKent Overstreet bcache_device_attach(d, c, u - c->uuids); 1555175206cfSTang Junhui bch_sectors_dirty_init(d); 1556cafe5635SKent Overstreet bch_flash_dev_request_init(d); 1557cafe5635SKent Overstreet add_disk(d->disk); 1558cafe5635SKent Overstreet 1559cafe5635SKent Overstreet if (kobject_add(&d->kobj, &disk_to_dev(d->disk)->kobj, "bcache")) 1560cafe5635SKent Overstreet goto err; 1561cafe5635SKent Overstreet 1562cafe5635SKent Overstreet bcache_device_link(d, c, "volume"); 1563cafe5635SKent Overstreet 15645342fd42SColy Li if (bch_has_feature_obso_large_bucket(&c->cache->sb)) { 15655342fd42SColy Li pr_err("The obsoleted large bucket layout is unsupported, set the bcache device into read-only\n"); 15665342fd42SColy Li pr_err("Please update to the latest bcache-tools to create the cache device\n"); 15675342fd42SColy Li set_disk_ro(d->disk, 1); 15685342fd42SColy Li } 15695342fd42SColy Li 1570cafe5635SKent Overstreet return 0; 1571cafe5635SKent Overstreet err: 1572cafe5635SKent Overstreet kobject_put(&d->kobj); 1573cafe5635SKent Overstreet return -ENOMEM; 1574cafe5635SKent Overstreet } 1575cafe5635SKent Overstreet 1576cafe5635SKent Overstreet static int flash_devs_run(struct cache_set *c) 1577cafe5635SKent Overstreet { 1578cafe5635SKent Overstreet int ret = 0; 1579cafe5635SKent Overstreet struct uuid_entry *u; 1580cafe5635SKent Overstreet 1581cafe5635SKent Overstreet for (u = c->uuids; 158202aa8a8bSColy Li u < c->uuids + c->nr_uuids && !ret; 1583cafe5635SKent Overstreet u++) 1584cafe5635SKent Overstreet if (UUID_FLASH_ONLY(u)) 1585cafe5635SKent Overstreet ret = flash_dev_run(c, u); 1586cafe5635SKent Overstreet 1587cafe5635SKent Overstreet return ret; 1588cafe5635SKent Overstreet } 1589cafe5635SKent Overstreet 1590cafe5635SKent Overstreet int bch_flash_dev_create(struct cache_set *c, uint64_t size) 1591cafe5635SKent Overstreet { 1592cafe5635SKent Overstreet struct uuid_entry *u; 1593cafe5635SKent Overstreet 1594cafe5635SKent Overstreet if (test_bit(CACHE_SET_STOPPING, &c->flags)) 1595cafe5635SKent Overstreet return -EINTR; 1596cafe5635SKent Overstreet 1597bf0c55c9SSlava Pestov if (!test_bit(CACHE_SET_RUNNING, &c->flags)) 1598bf0c55c9SSlava Pestov return -EPERM; 1599bf0c55c9SSlava Pestov 1600cafe5635SKent Overstreet u = uuid_find_empty(c); 1601cafe5635SKent Overstreet if (!u) { 160246f5aa88SJoe Perches pr_err("Can't create volume, no room for UUID\n"); 1603cafe5635SKent Overstreet return -EINVAL; 1604cafe5635SKent Overstreet } 1605cafe5635SKent Overstreet 1606cafe5635SKent Overstreet get_random_bytes(u->uuid, 16); 1607cafe5635SKent Overstreet memset(u->label, 0, 32); 160875cbb3f1SArnd Bergmann u->first_reg = u->last_reg = cpu_to_le32((u32)ktime_get_real_seconds()); 1609cafe5635SKent Overstreet 1610cafe5635SKent Overstreet SET_UUID_FLASH_ONLY(u, 1); 1611cafe5635SKent Overstreet u->sectors = size >> 9; 1612cafe5635SKent Overstreet 1613cafe5635SKent Overstreet bch_uuid_write(c); 1614cafe5635SKent Overstreet 1615cafe5635SKent Overstreet return flash_dev_run(c, u); 1616cafe5635SKent Overstreet } 1617cafe5635SKent Overstreet 1618c7b7bd07SColy Li bool bch_cached_dev_error(struct cached_dev *dc) 1619c7b7bd07SColy Li { 1620c7b7bd07SColy Li if (!dc || test_bit(BCACHE_DEV_CLOSING, &dc->disk.flags)) 1621c7b7bd07SColy Li return false; 1622c7b7bd07SColy Li 1623c7b7bd07SColy Li dc->io_disable = true; 1624c7b7bd07SColy Li /* make others know io_disable is true earlier */ 1625c7b7bd07SColy Li smp_mb(); 1626c7b7bd07SColy Li 1627c7b7bd07SColy Li pr_err("stop %s: too many IO errors on backing device %s\n", 16286e916a7eSColy Li dc->disk.disk->disk_name, dc->backing_dev_name); 1629c7b7bd07SColy Li 1630c7b7bd07SColy Li bcache_device_stop(&dc->disk); 1631c7b7bd07SColy Li return true; 1632c7b7bd07SColy Li } 1633c7b7bd07SColy Li 1634cafe5635SKent Overstreet /* Cache set */ 1635cafe5635SKent Overstreet 1636cafe5635SKent Overstreet __printf(2, 3) 1637cafe5635SKent Overstreet bool bch_cache_set_error(struct cache_set *c, const char *fmt, ...) 1638cafe5635SKent Overstreet { 163946f5aa88SJoe Perches struct va_format vaf; 1640cafe5635SKent Overstreet va_list args; 1641cafe5635SKent Overstreet 164277c320ebSKent Overstreet if (c->on_error != ON_ERROR_PANIC && 164377c320ebSKent Overstreet test_bit(CACHE_SET_STOPPING, &c->flags)) 1644cafe5635SKent Overstreet return false; 1645cafe5635SKent Overstreet 1646771f393eSColy Li if (test_and_set_bit(CACHE_SET_IO_DISABLE, &c->flags)) 164746f5aa88SJoe Perches pr_info("CACHE_SET_IO_DISABLE already set\n"); 1648771f393eSColy Li 16493be11dbaSColy Li /* 16503be11dbaSColy Li * XXX: we can be called from atomic context 16513be11dbaSColy Li * acquire_console_sem(); 1652cafe5635SKent Overstreet */ 1653cafe5635SKent Overstreet 1654cafe5635SKent Overstreet va_start(args, fmt); 1655cafe5635SKent Overstreet 165646f5aa88SJoe Perches vaf.fmt = fmt; 165746f5aa88SJoe Perches vaf.va = &args; 165846f5aa88SJoe Perches 165946f5aa88SJoe Perches pr_err("error on %pU: %pV, disabling caching\n", 16601132e56eSColy Li c->set_uuid, &vaf); 166146f5aa88SJoe Perches 166246f5aa88SJoe Perches va_end(args); 1663cafe5635SKent Overstreet 166477c320ebSKent Overstreet if (c->on_error == ON_ERROR_PANIC) 166577c320ebSKent Overstreet panic("panic forced after error\n"); 166677c320ebSKent Overstreet 1667cafe5635SKent Overstreet bch_cache_set_unregister(c); 1668cafe5635SKent Overstreet return true; 1669cafe5635SKent Overstreet } 1670cafe5635SKent Overstreet 16712d17456eSColy Li /* When c->kobj released */ 1672cafe5635SKent Overstreet void bch_cache_set_release(struct kobject *kobj) 1673cafe5635SKent Overstreet { 1674cafe5635SKent Overstreet struct cache_set *c = container_of(kobj, struct cache_set, kobj); 16751fae7cf0SColy Li 1676cafe5635SKent Overstreet kfree(c); 1677cafe5635SKent Overstreet module_put(THIS_MODULE); 1678cafe5635SKent Overstreet } 1679cafe5635SKent Overstreet 1680cafe5635SKent Overstreet static void cache_set_free(struct closure *cl) 1681cafe5635SKent Overstreet { 1682cafe5635SKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, cl); 1683cafe5635SKent Overstreet struct cache *ca; 1684cafe5635SKent Overstreet 1685cafe5635SKent Overstreet debugfs_remove(c->debug); 1686cafe5635SKent Overstreet 1687cafe5635SKent Overstreet bch_open_buckets_free(c); 1688cafe5635SKent Overstreet bch_btree_cache_free(c); 1689cafe5635SKent Overstreet bch_journal_free(c); 1690cafe5635SKent Overstreet 1691a4b732a2SLiang Chen mutex_lock(&bch_register_lock); 16924a784266SColy Li bch_bset_sort_state_free(&c->sort); 16934a784266SColy Li free_pages((unsigned long) c->uuids, ilog2(meta_bucket_pages(&c->cache->sb))); 16944a784266SColy Li 169508fdb2cdSColy Li ca = c->cache; 1696c9a78332SSlava Pestov if (ca) { 1697c9a78332SSlava Pestov ca->set = NULL; 1698697e2349SColy Li c->cache = NULL; 1699cafe5635SKent Overstreet kobject_put(&ca->kobj); 1700c9a78332SSlava Pestov } 1701cafe5635SKent Overstreet 1702cafe5635SKent Overstreet 1703da415a09SNicholas Swenson if (c->moving_gc_wq) 1704da415a09SNicholas Swenson destroy_workqueue(c->moving_gc_wq); 1705d19936a2SKent Overstreet bioset_exit(&c->bio_split); 1706d19936a2SKent Overstreet mempool_exit(&c->fill_iter); 1707d19936a2SKent Overstreet mempool_exit(&c->bio_meta); 1708d19936a2SKent Overstreet mempool_exit(&c->search); 1709cafe5635SKent Overstreet kfree(c->devices); 1710cafe5635SKent Overstreet 1711cafe5635SKent Overstreet list_del(&c->list); 1712cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1713cafe5635SKent Overstreet 17141132e56eSColy Li pr_info("Cache set %pU unregistered\n", c->set_uuid); 1715cafe5635SKent Overstreet wake_up(&unregister_wait); 1716cafe5635SKent Overstreet 1717cafe5635SKent Overstreet closure_debug_destroy(&c->cl); 1718cafe5635SKent Overstreet kobject_put(&c->kobj); 1719cafe5635SKent Overstreet } 1720cafe5635SKent Overstreet 1721cafe5635SKent Overstreet static void cache_set_flush(struct closure *cl) 1722cafe5635SKent Overstreet { 1723cafe5635SKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, caching); 172408fdb2cdSColy Li struct cache *ca = c->cache; 1725cafe5635SKent Overstreet struct btree *b; 1726cafe5635SKent Overstreet 1727cafe5635SKent Overstreet bch_cache_accounting_destroy(&c->accounting); 1728cafe5635SKent Overstreet 1729cafe5635SKent Overstreet kobject_put(&c->internal); 1730cafe5635SKent Overstreet kobject_del(&c->kobj); 1731cafe5635SKent Overstreet 1732b387e9b5SColy Li if (!IS_ERR_OR_NULL(c->gc_thread)) 173372a44517SKent Overstreet kthread_stop(c->gc_thread); 173472a44517SKent Overstreet 1735cafe5635SKent Overstreet if (!IS_ERR_OR_NULL(c->root)) 1736cafe5635SKent Overstreet list_add(&c->root->list, &c->btree_cache); 1737cafe5635SKent Overstreet 1738e6dcbd3eSColy Li /* 1739e6dcbd3eSColy Li * Avoid flushing cached nodes if cache set is retiring 1740e6dcbd3eSColy Li * due to too many I/O errors detected. 1741e6dcbd3eSColy Li */ 1742e6dcbd3eSColy Li if (!test_bit(CACHE_SET_IO_DISABLE, &c->flags)) 17432a285686SKent Overstreet list_for_each_entry(b, &c->btree_cache, list) { 17442a285686SKent Overstreet mutex_lock(&b->write_lock); 1745cafe5635SKent Overstreet if (btree_node_dirty(b)) 17462a285686SKent Overstreet __bch_btree_node_write(b, NULL); 17472a285686SKent Overstreet mutex_unlock(&b->write_lock); 17482a285686SKent Overstreet } 1749cafe5635SKent Overstreet 175079826c35SKent Overstreet if (ca->alloc_thread) 175179826c35SKent Overstreet kthread_stop(ca->alloc_thread); 175279826c35SKent Overstreet 17535b1016e6SKent Overstreet if (c->journal.cur) { 1754dabb4433SKent Overstreet cancel_delayed_work_sync(&c->journal.work); 1755dabb4433SKent Overstreet /* flush last journal entry if needed */ 1756dabb4433SKent Overstreet c->journal.work.work.func(&c->journal.work.work); 17575b1016e6SKent Overstreet } 1758dabb4433SKent Overstreet 1759cafe5635SKent Overstreet closure_return(cl); 1760cafe5635SKent Overstreet } 1761cafe5635SKent Overstreet 17627e027ca4SColy Li /* 17637e027ca4SColy Li * This function is only called when CACHE_SET_IO_DISABLE is set, which means 17647e027ca4SColy Li * cache set is unregistering due to too many I/O errors. In this condition, 17657e027ca4SColy Li * the bcache device might be stopped, it depends on stop_when_cache_set_failed 17667e027ca4SColy Li * value and whether the broken cache has dirty data: 17677e027ca4SColy Li * 17687e027ca4SColy Li * dc->stop_when_cache_set_failed dc->has_dirty stop bcache device 17697e027ca4SColy Li * BCH_CACHED_STOP_AUTO 0 NO 17707e027ca4SColy Li * BCH_CACHED_STOP_AUTO 1 YES 17717e027ca4SColy Li * BCH_CACHED_DEV_STOP_ALWAYS 0 YES 17727e027ca4SColy Li * BCH_CACHED_DEV_STOP_ALWAYS 1 YES 17737e027ca4SColy Li * 17747e027ca4SColy Li * The expected behavior is, if stop_when_cache_set_failed is configured to 17757e027ca4SColy Li * "auto" via sysfs interface, the bcache device will not be stopped if the 17767e027ca4SColy Li * backing device is clean on the broken cache device. 17777e027ca4SColy Li */ 17787e027ca4SColy Li static void conditional_stop_bcache_device(struct cache_set *c, 17797e027ca4SColy Li struct bcache_device *d, 17807e027ca4SColy Li struct cached_dev *dc) 17817e027ca4SColy Li { 17827e027ca4SColy Li if (dc->stop_when_cache_set_failed == BCH_CACHED_DEV_STOP_ALWAYS) { 178346f5aa88SJoe Perches pr_warn("stop_when_cache_set_failed of %s is \"always\", stop it for failed cache set %pU.\n", 17841132e56eSColy Li d->disk->disk_name, c->set_uuid); 17857e027ca4SColy Li bcache_device_stop(d); 17867e027ca4SColy Li } else if (atomic_read(&dc->has_dirty)) { 17877e027ca4SColy Li /* 17887e027ca4SColy Li * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO 17897e027ca4SColy Li * and dc->has_dirty == 1 17907e027ca4SColy Li */ 179146f5aa88SJoe Perches pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is dirty, stop it to avoid potential data corruption.\n", 17927e027ca4SColy Li d->disk->disk_name); 17934fd8e138SColy Li /* 17944fd8e138SColy Li * There might be a small time gap that cache set is 17954fd8e138SColy Li * released but bcache device is not. Inside this time 17964fd8e138SColy Li * gap, regular I/O requests will directly go into 17974fd8e138SColy Li * backing device as no cache set attached to. This 17984fd8e138SColy Li * behavior may also introduce potential inconsistence 17994fd8e138SColy Li * data in writeback mode while cache is dirty. 18004fd8e138SColy Li * Therefore before calling bcache_device_stop() due 18014fd8e138SColy Li * to a broken cache device, dc->io_disable should be 18024fd8e138SColy Li * explicitly set to true. 18034fd8e138SColy Li */ 18044fd8e138SColy Li dc->io_disable = true; 18054fd8e138SColy Li /* make others know io_disable is true earlier */ 18064fd8e138SColy Li smp_mb(); 18077e027ca4SColy Li bcache_device_stop(d); 18087e027ca4SColy Li } else { 18097e027ca4SColy Li /* 18107e027ca4SColy Li * dc->stop_when_cache_set_failed == BCH_CACHED_STOP_AUTO 18117e027ca4SColy Li * and dc->has_dirty == 0 18127e027ca4SColy Li */ 181346f5aa88SJoe Perches pr_warn("stop_when_cache_set_failed of %s is \"auto\" and cache is clean, keep it alive.\n", 18147e027ca4SColy Li d->disk->disk_name); 18157e027ca4SColy Li } 18167e027ca4SColy Li } 18177e027ca4SColy Li 1818cafe5635SKent Overstreet static void __cache_set_unregister(struct closure *cl) 1819cafe5635SKent Overstreet { 1820cafe5635SKent Overstreet struct cache_set *c = container_of(cl, struct cache_set, caching); 18215caa52afSKent Overstreet struct cached_dev *dc; 18227e027ca4SColy Li struct bcache_device *d; 1823cafe5635SKent Overstreet size_t i; 1824cafe5635SKent Overstreet 1825cafe5635SKent Overstreet mutex_lock(&bch_register_lock); 1826cafe5635SKent Overstreet 18277e027ca4SColy Li for (i = 0; i < c->devices_max_used; i++) { 18287e027ca4SColy Li d = c->devices[i]; 18297e027ca4SColy Li if (!d) 18307e027ca4SColy Li continue; 18317e027ca4SColy Li 18325caa52afSKent Overstreet if (!UUID_FLASH_ONLY(&c->uuids[i]) && 18335caa52afSKent Overstreet test_bit(CACHE_SET_UNREGISTERING, &c->flags)) { 18347e027ca4SColy Li dc = container_of(d, struct cached_dev, disk); 18355caa52afSKent Overstreet bch_cached_dev_detach(dc); 18367e027ca4SColy Li if (test_bit(CACHE_SET_IO_DISABLE, &c->flags)) 18377e027ca4SColy Li conditional_stop_bcache_device(c, d, dc); 18385caa52afSKent Overstreet } else { 18397e027ca4SColy Li bcache_device_stop(d); 18405caa52afSKent Overstreet } 18415caa52afSKent Overstreet } 1842cafe5635SKent Overstreet 1843cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 1844cafe5635SKent Overstreet 1845cafe5635SKent Overstreet continue_at(cl, cache_set_flush, system_wq); 1846cafe5635SKent Overstreet } 1847cafe5635SKent Overstreet 1848cafe5635SKent Overstreet void bch_cache_set_stop(struct cache_set *c) 1849cafe5635SKent Overstreet { 1850cafe5635SKent Overstreet if (!test_and_set_bit(CACHE_SET_STOPPING, &c->flags)) 185163d63b51SColy Li /* closure_fn set to __cache_set_unregister() */ 1852cafe5635SKent Overstreet closure_queue(&c->caching); 1853cafe5635SKent Overstreet } 1854cafe5635SKent Overstreet 1855cafe5635SKent Overstreet void bch_cache_set_unregister(struct cache_set *c) 1856cafe5635SKent Overstreet { 1857cafe5635SKent Overstreet set_bit(CACHE_SET_UNREGISTERING, &c->flags); 1858cafe5635SKent Overstreet bch_cache_set_stop(c); 1859cafe5635SKent Overstreet } 1860cafe5635SKent Overstreet 1861de1fafabSColy Li #define alloc_meta_bucket_pages(gfp, sb) \ 1862de1fafabSColy Li ((void *) __get_free_pages(__GFP_ZERO|__GFP_COMP|gfp, ilog2(meta_bucket_pages(sb)))) 1863cafe5635SKent Overstreet 1864cafe5635SKent Overstreet struct cache_set *bch_cache_set_alloc(struct cache_sb *sb) 1865cafe5635SKent Overstreet { 1866cafe5635SKent Overstreet int iter_size; 18674a784266SColy Li struct cache *ca = container_of(sb, struct cache, sb); 1868cafe5635SKent Overstreet struct cache_set *c = kzalloc(sizeof(struct cache_set), GFP_KERNEL); 18691fae7cf0SColy Li 1870cafe5635SKent Overstreet if (!c) 1871cafe5635SKent Overstreet return NULL; 1872cafe5635SKent Overstreet 1873cafe5635SKent Overstreet __module_get(THIS_MODULE); 1874cafe5635SKent Overstreet closure_init(&c->cl, NULL); 1875cafe5635SKent Overstreet set_closure_fn(&c->cl, cache_set_free, system_wq); 1876cafe5635SKent Overstreet 1877cafe5635SKent Overstreet closure_init(&c->caching, &c->cl); 1878cafe5635SKent Overstreet set_closure_fn(&c->caching, __cache_set_unregister, system_wq); 1879cafe5635SKent Overstreet 1880cafe5635SKent Overstreet /* Maybe create continue_at_noreturn() and use it here? */ 1881cafe5635SKent Overstreet closure_set_stopped(&c->cl); 1882cafe5635SKent Overstreet closure_put(&c->cl); 1883cafe5635SKent Overstreet 1884cafe5635SKent Overstreet kobject_init(&c->kobj, &bch_cache_set_ktype); 1885cafe5635SKent Overstreet kobject_init(&c->internal, &bch_cache_set_internal_ktype); 1886cafe5635SKent Overstreet 1887cafe5635SKent Overstreet bch_cache_accounting_init(&c->accounting, &c->cl); 1888cafe5635SKent Overstreet 18891132e56eSColy Li memcpy(c->set_uuid, sb->set_uuid, 16); 1890d721a43fSColy Li 18914a784266SColy Li c->cache = ca; 18924a784266SColy Li c->cache->set = c; 1893cafe5635SKent Overstreet c->bucket_bits = ilog2(sb->bucket_size); 1894cafe5635SKent Overstreet c->block_bits = ilog2(sb->block_size); 18954a784266SColy Li c->nr_uuids = meta_bucket_bytes(sb) / sizeof(struct uuid_entry); 18962831231dSColy Li c->devices_max_used = 0; 1897ea8c5356SColy Li atomic_set(&c->attached_dev_nr, 0); 18984a784266SColy Li c->btree_pages = meta_bucket_pages(sb); 1899cafe5635SKent Overstreet if (c->btree_pages > BTREE_MAX_PAGES) 1900cafe5635SKent Overstreet c->btree_pages = max_t(int, c->btree_pages / 4, 1901cafe5635SKent Overstreet BTREE_MAX_PAGES); 1902cafe5635SKent Overstreet 1903cb7a583eSKent Overstreet sema_init(&c->sb_write_mutex, 1); 1904e8e1d468SKent Overstreet mutex_init(&c->bucket_lock); 19050a63b66dSKent Overstreet init_waitqueue_head(&c->btree_cache_wait); 190634cf78bfSGuoju Fang spin_lock_init(&c->btree_cannibalize_lock); 190735fcd848SKent Overstreet init_waitqueue_head(&c->bucket_wait); 1908be628be0SKent Overstreet init_waitqueue_head(&c->gc_wait); 1909cb7a583eSKent Overstreet sema_init(&c->uuid_write_mutex, 1); 191065d22e91SKent Overstreet 191165d22e91SKent Overstreet spin_lock_init(&c->btree_gc_time.lock); 191265d22e91SKent Overstreet spin_lock_init(&c->btree_split_time.lock); 191365d22e91SKent Overstreet spin_lock_init(&c->btree_read_time.lock); 1914e8e1d468SKent Overstreet 1915cafe5635SKent Overstreet bch_moving_init_cache_set(c); 1916cafe5635SKent Overstreet 1917cafe5635SKent Overstreet INIT_LIST_HEAD(&c->list); 1918cafe5635SKent Overstreet INIT_LIST_HEAD(&c->cached_devs); 1919cafe5635SKent Overstreet INIT_LIST_HEAD(&c->btree_cache); 1920cafe5635SKent Overstreet INIT_LIST_HEAD(&c->btree_cache_freeable); 1921cafe5635SKent Overstreet INIT_LIST_HEAD(&c->btree_cache_freed); 1922cafe5635SKent Overstreet INIT_LIST_HEAD(&c->data_buckets); 1923cafe5635SKent Overstreet 19246907dc49SColy Li iter_size = ((meta_bucket_pages(sb) * PAGE_SECTORS) / sb->block_size + 1) * 1925cafe5635SKent Overstreet sizeof(struct btree_iter_set); 1926cafe5635SKent Overstreet 1927a42d3c64SColy Li c->devices = kcalloc(c->nr_uuids, sizeof(void *), GFP_KERNEL); 1928a42d3c64SColy Li if (!c->devices) 1929a42d3c64SColy Li goto err; 1930a42d3c64SColy Li 1931a42d3c64SColy Li if (mempool_init_slab_pool(&c->search, 32, bch_search_cache)) 1932a42d3c64SColy Li goto err; 1933a42d3c64SColy Li 1934a42d3c64SColy Li if (mempool_init_kmalloc_pool(&c->bio_meta, 2, 1935a42d3c64SColy Li sizeof(struct bbio) + 19364a784266SColy Li sizeof(struct bio_vec) * meta_bucket_pages(sb))) 1937a42d3c64SColy Li goto err; 1938a42d3c64SColy Li 1939a42d3c64SColy Li if (mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size)) 1940a42d3c64SColy Li goto err; 1941a42d3c64SColy Li 1942a42d3c64SColy Li if (bioset_init(&c->bio_split, 4, offsetof(struct bbio, bio), 1943faa8e2c4SMing Lei BIOSET_NEED_RESCUER)) 1944a42d3c64SColy Li goto err; 1945a42d3c64SColy Li 19464a784266SColy Li c->uuids = alloc_meta_bucket_pages(GFP_KERNEL, sb); 1947a42d3c64SColy Li if (!c->uuids) 1948a42d3c64SColy Li goto err; 1949a42d3c64SColy Li 1950a42d3c64SColy Li c->moving_gc_wq = alloc_workqueue("bcache_gc", WQ_MEM_RECLAIM, 0); 1951a42d3c64SColy Li if (!c->moving_gc_wq) 1952a42d3c64SColy Li goto err; 1953a42d3c64SColy Li 1954a42d3c64SColy Li if (bch_journal_alloc(c)) 1955a42d3c64SColy Li goto err; 1956a42d3c64SColy Li 1957a42d3c64SColy Li if (bch_btree_cache_alloc(c)) 1958a42d3c64SColy Li goto err; 1959a42d3c64SColy Li 1960a42d3c64SColy Li if (bch_open_buckets_alloc(c)) 1961a42d3c64SColy Li goto err; 1962a42d3c64SColy Li 1963a42d3c64SColy Li if (bch_bset_sort_state_init(&c->sort, ilog2(c->btree_pages))) 1964cafe5635SKent Overstreet goto err; 1965cafe5635SKent Overstreet 1966cafe5635SKent Overstreet c->congested_read_threshold_us = 2000; 1967cafe5635SKent Overstreet c->congested_write_threshold_us = 20000; 19687ba0d830SColy Li c->error_limit = DEFAULT_IO_ERROR_LIMIT; 1969c5fcdedcSColy Li c->idle_max_writeback_rate_enabled = 1; 1970771f393eSColy Li WARN_ON(test_and_clear_bit(CACHE_SET_IO_DISABLE, &c->flags)); 1971cafe5635SKent Overstreet 1972cafe5635SKent Overstreet return c; 1973cafe5635SKent Overstreet err: 1974cafe5635SKent Overstreet bch_cache_set_unregister(c); 1975cafe5635SKent Overstreet return NULL; 1976cafe5635SKent Overstreet } 1977cafe5635SKent Overstreet 1978ce3e4cfbSColy Li static int run_cache_set(struct cache_set *c) 1979cafe5635SKent Overstreet { 1980cafe5635SKent Overstreet const char *err = "cannot allocate memory"; 1981cafe5635SKent Overstreet struct cached_dev *dc, *t; 198208fdb2cdSColy Li struct cache *ca = c->cache; 1983c18536a7SKent Overstreet struct closure cl; 198495f18c9dSShenghui Wang LIST_HEAD(journal); 198595f18c9dSShenghui Wang struct journal_replay *l; 1986cafe5635SKent Overstreet 1987c18536a7SKent Overstreet closure_init_stack(&cl); 1988cafe5635SKent Overstreet 198908fdb2cdSColy Li c->nbuckets = ca->sb.nbuckets; 1990be628be0SKent Overstreet set_gc_sectors(c); 1991cafe5635SKent Overstreet 19926f9414e0SColy Li if (CACHE_SYNC(&c->cache->sb)) { 1993cafe5635SKent Overstreet struct bkey *k; 1994cafe5635SKent Overstreet struct jset *j; 1995cafe5635SKent Overstreet 1996cafe5635SKent Overstreet err = "cannot allocate memory for journal"; 1997c18536a7SKent Overstreet if (bch_journal_read(c, &journal)) 1998cafe5635SKent Overstreet goto err; 1999cafe5635SKent Overstreet 200046f5aa88SJoe Perches pr_debug("btree_journal_read() done\n"); 2001cafe5635SKent Overstreet 2002cafe5635SKent Overstreet err = "no journal entries found"; 2003cafe5635SKent Overstreet if (list_empty(&journal)) 2004cafe5635SKent Overstreet goto err; 2005cafe5635SKent Overstreet 2006cafe5635SKent Overstreet j = &list_entry(journal.prev, struct journal_replay, list)->j; 2007cafe5635SKent Overstreet 2008cafe5635SKent Overstreet err = "IO error reading priorities"; 200949d08d59SColy Li if (prio_read(ca, j->prio_bucket[ca->sb.nr_this_dev])) 201049d08d59SColy Li goto err; 2011cafe5635SKent Overstreet 2012cafe5635SKent Overstreet /* 2013cafe5635SKent Overstreet * If prio_read() fails it'll call cache_set_error and we'll 2014cafe5635SKent Overstreet * tear everything down right away, but if we perhaps checked 2015cafe5635SKent Overstreet * sooner we could avoid journal replay. 2016cafe5635SKent Overstreet */ 2017cafe5635SKent Overstreet 2018cafe5635SKent Overstreet k = &j->btree_root; 2019cafe5635SKent Overstreet 2020cafe5635SKent Overstreet err = "bad btree root"; 202165d45231SKent Overstreet if (__bch_btree_ptr_invalid(c, k)) 2022cafe5635SKent Overstreet goto err; 2023cafe5635SKent Overstreet 2024cafe5635SKent Overstreet err = "error reading btree root"; 2025b0d30981SColy Li c->root = bch_btree_node_get(c, NULL, k, 2026b0d30981SColy Li j->btree_level, 2027b0d30981SColy Li true, NULL); 2028cafe5635SKent Overstreet if (IS_ERR_OR_NULL(c->root)) 2029cafe5635SKent Overstreet goto err; 2030cafe5635SKent Overstreet 2031cafe5635SKent Overstreet list_del_init(&c->root->list); 2032cafe5635SKent Overstreet rw_unlock(true, c->root); 2033cafe5635SKent Overstreet 2034c18536a7SKent Overstreet err = uuid_read(c, j, &cl); 2035cafe5635SKent Overstreet if (err) 2036cafe5635SKent Overstreet goto err; 2037cafe5635SKent Overstreet 2038cafe5635SKent Overstreet err = "error in recovery"; 2039c18536a7SKent Overstreet if (bch_btree_check(c)) 2040cafe5635SKent Overstreet goto err; 2041cafe5635SKent Overstreet 2042cafe5635SKent Overstreet bch_journal_mark(c, &journal); 20432531d9eeSKent Overstreet bch_initial_gc_finish(c); 204446f5aa88SJoe Perches pr_debug("btree_check() done\n"); 2045cafe5635SKent Overstreet 2046cafe5635SKent Overstreet /* 2047cafe5635SKent Overstreet * bcache_journal_next() can't happen sooner, or 2048cafe5635SKent Overstreet * btree_gc_finish() will give spurious errors about last_gc > 2049cafe5635SKent Overstreet * gc_gen - this is a hack but oh well. 2050cafe5635SKent Overstreet */ 2051cafe5635SKent Overstreet bch_journal_next(&c->journal); 2052cafe5635SKent Overstreet 2053119ba0f8SKent Overstreet err = "error starting allocator thread"; 2054119ba0f8SKent Overstreet if (bch_cache_allocator_start(ca)) 2055119ba0f8SKent Overstreet goto err; 2056cafe5635SKent Overstreet 2057cafe5635SKent Overstreet /* 2058cafe5635SKent Overstreet * First place it's safe to allocate: btree_check() and 2059cafe5635SKent Overstreet * btree_gc_finish() have to run before we have buckets to 2060cafe5635SKent Overstreet * allocate, and bch_bucket_alloc_set() might cause a journal 2061cafe5635SKent Overstreet * entry to be written so bcache_journal_next() has to be called 2062cafe5635SKent Overstreet * first. 2063cafe5635SKent Overstreet * 2064cafe5635SKent Overstreet * If the uuids were in the old format we have to rewrite them 2065cafe5635SKent Overstreet * before the next journal entry is written: 2066cafe5635SKent Overstreet */ 2067cafe5635SKent Overstreet if (j->version < BCACHE_JSET_VERSION_UUID) 2068cafe5635SKent Overstreet __uuid_write(c); 2069cafe5635SKent Overstreet 2070ce3e4cfbSColy Li err = "bcache: replay journal failed"; 2071ce3e4cfbSColy Li if (bch_journal_replay(c, &journal)) 2072ce3e4cfbSColy Li goto err; 2073cafe5635SKent Overstreet } else { 20746f10f7d1SColy Li unsigned int j; 2075cafe5635SKent Overstreet 207608fdb2cdSColy Li pr_notice("invalidating existing data\n"); 2077cafe5635SKent Overstreet ca->sb.keys = clamp_t(int, ca->sb.nbuckets >> 7, 2078cafe5635SKent Overstreet 2, SB_JOURNAL_BUCKETS); 2079cafe5635SKent Overstreet 2080cafe5635SKent Overstreet for (j = 0; j < ca->sb.keys; j++) 2081cafe5635SKent Overstreet ca->sb.d[j] = ca->sb.first_bucket + j; 2082cafe5635SKent Overstreet 20832531d9eeSKent Overstreet bch_initial_gc_finish(c); 2084cafe5635SKent Overstreet 2085119ba0f8SKent Overstreet err = "error starting allocator thread"; 2086119ba0f8SKent Overstreet if (bch_cache_allocator_start(ca)) 2087119ba0f8SKent Overstreet goto err; 2088cafe5635SKent Overstreet 2089cafe5635SKent Overstreet mutex_lock(&c->bucket_lock); 209084c529aeSAndrea Righi bch_prio_write(ca, true); 2091cafe5635SKent Overstreet mutex_unlock(&c->bucket_lock); 2092cafe5635SKent Overstreet 2093cafe5635SKent Overstreet err = "cannot allocate new UUID bucket"; 2094cafe5635SKent Overstreet if (__uuid_write(c)) 209572a44517SKent Overstreet goto err; 2096cafe5635SKent Overstreet 2097cafe5635SKent Overstreet err = "cannot allocate new btree root"; 20982452cc89SSlava Pestov c->root = __bch_btree_node_alloc(c, NULL, 0, true, NULL); 2099cafe5635SKent Overstreet if (IS_ERR_OR_NULL(c->root)) 210072a44517SKent Overstreet goto err; 2101cafe5635SKent Overstreet 21022a285686SKent Overstreet mutex_lock(&c->root->write_lock); 2103cafe5635SKent Overstreet bkey_copy_key(&c->root->key, &MAX_KEY); 2104c18536a7SKent Overstreet bch_btree_node_write(c->root, &cl); 21052a285686SKent Overstreet mutex_unlock(&c->root->write_lock); 2106cafe5635SKent Overstreet 2107cafe5635SKent Overstreet bch_btree_set_root(c->root); 2108cafe5635SKent Overstreet rw_unlock(true, c->root); 2109cafe5635SKent Overstreet 2110cafe5635SKent Overstreet /* 2111cafe5635SKent Overstreet * We don't want to write the first journal entry until 2112cafe5635SKent Overstreet * everything is set up - fortunately journal entries won't be 2113cafe5635SKent Overstreet * written until the SET_CACHE_SYNC() here: 2114cafe5635SKent Overstreet */ 21156f9414e0SColy Li SET_CACHE_SYNC(&c->cache->sb, true); 2116cafe5635SKent Overstreet 2117cafe5635SKent Overstreet bch_journal_next(&c->journal); 2118c18536a7SKent Overstreet bch_journal_meta(c, &cl); 2119cafe5635SKent Overstreet } 2120cafe5635SKent Overstreet 212172a44517SKent Overstreet err = "error starting gc thread"; 212272a44517SKent Overstreet if (bch_gc_thread_start(c)) 212372a44517SKent Overstreet goto err; 212472a44517SKent Overstreet 2125c18536a7SKent Overstreet closure_sync(&cl); 21264a784266SColy Li c->cache->sb.last_mount = (u32)ktime_get_real_seconds(); 2127cafe5635SKent Overstreet bcache_write_super(c); 2128cafe5635SKent Overstreet 21295342fd42SColy Li if (bch_has_feature_obso_large_bucket(&c->cache->sb)) 21305342fd42SColy Li pr_err("Detect obsoleted large bucket layout, all attached bcache device will be read-only\n"); 21315342fd42SColy Li 2132cafe5635SKent Overstreet list_for_each_entry_safe(dc, t, &uncached_devices, list) 213373ac105bSTang Junhui bch_cached_dev_attach(dc, c, NULL); 2134cafe5635SKent Overstreet 2135cafe5635SKent Overstreet flash_devs_run(c); 2136cafe5635SKent Overstreet 2137bf0c55c9SSlava Pestov set_bit(CACHE_SET_RUNNING, &c->flags); 2138ce3e4cfbSColy Li return 0; 2139cafe5635SKent Overstreet err: 214095f18c9dSShenghui Wang while (!list_empty(&journal)) { 214195f18c9dSShenghui Wang l = list_first_entry(&journal, struct journal_replay, list); 214295f18c9dSShenghui Wang list_del(&l->list); 214395f18c9dSShenghui Wang kfree(l); 214495f18c9dSShenghui Wang } 214595f18c9dSShenghui Wang 2146c18536a7SKent Overstreet closure_sync(&cl); 214768a53c95SColy Li 2148c8694948SKees Cook bch_cache_set_error(c, "%s", err); 2149ce3e4cfbSColy Li 2150ce3e4cfbSColy Li return -EIO; 2151cafe5635SKent Overstreet } 2152cafe5635SKent Overstreet 2153cafe5635SKent Overstreet static const char *register_cache_set(struct cache *ca) 2154cafe5635SKent Overstreet { 2155cafe5635SKent Overstreet char buf[12]; 2156cafe5635SKent Overstreet const char *err = "cannot allocate memory"; 2157cafe5635SKent Overstreet struct cache_set *c; 2158cafe5635SKent Overstreet 2159cafe5635SKent Overstreet list_for_each_entry(c, &bch_cache_sets, list) 21601132e56eSColy Li if (!memcmp(c->set_uuid, ca->sb.set_uuid, 16)) { 2161697e2349SColy Li if (c->cache) 2162cafe5635SKent Overstreet return "duplicate cache set member"; 2163cafe5635SKent Overstreet 2164cafe5635SKent Overstreet goto found; 2165cafe5635SKent Overstreet } 2166cafe5635SKent Overstreet 2167cafe5635SKent Overstreet c = bch_cache_set_alloc(&ca->sb); 2168cafe5635SKent Overstreet if (!c) 2169cafe5635SKent Overstreet return err; 2170cafe5635SKent Overstreet 2171cafe5635SKent Overstreet err = "error creating kobject"; 21721132e56eSColy Li if (kobject_add(&c->kobj, bcache_kobj, "%pU", c->set_uuid) || 2173cafe5635SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal")) 2174cafe5635SKent Overstreet goto err; 2175cafe5635SKent Overstreet 2176cafe5635SKent Overstreet if (bch_cache_accounting_add_kobjs(&c->accounting, &c->kobj)) 2177cafe5635SKent Overstreet goto err; 2178cafe5635SKent Overstreet 2179cafe5635SKent Overstreet bch_debug_init_cache_set(c); 2180cafe5635SKent Overstreet 2181cafe5635SKent Overstreet list_add(&c->list, &bch_cache_sets); 2182cafe5635SKent Overstreet found: 2183cafe5635SKent Overstreet sprintf(buf, "cache%i", ca->sb.nr_this_dev); 2184cafe5635SKent Overstreet if (sysfs_create_link(&ca->kobj, &c->kobj, "set") || 2185cafe5635SKent Overstreet sysfs_create_link(&c->kobj, &ca->kobj, buf)) 2186cafe5635SKent Overstreet goto err; 2187cafe5635SKent Overstreet 2188d83353b3SKent Overstreet kobject_get(&ca->kobj); 2189cafe5635SKent Overstreet ca->set = c; 2190697e2349SColy Li ca->set->cache = ca; 2191cafe5635SKent Overstreet 2192ce3e4cfbSColy Li err = "failed to run cache set"; 2193ce3e4cfbSColy Li if (run_cache_set(c) < 0) 2194ce3e4cfbSColy Li goto err; 2195cafe5635SKent Overstreet 2196cafe5635SKent Overstreet return NULL; 2197cafe5635SKent Overstreet err: 2198cafe5635SKent Overstreet bch_cache_set_unregister(c); 2199cafe5635SKent Overstreet return err; 2200cafe5635SKent Overstreet } 2201cafe5635SKent Overstreet 2202cafe5635SKent Overstreet /* Cache device */ 2203cafe5635SKent Overstreet 22042d17456eSColy Li /* When ca->kobj released */ 2205cafe5635SKent Overstreet void bch_cache_release(struct kobject *kobj) 2206cafe5635SKent Overstreet { 2207cafe5635SKent Overstreet struct cache *ca = container_of(kobj, struct cache, kobj); 22086f10f7d1SColy Li unsigned int i; 2209cafe5635SKent Overstreet 2210c9a78332SSlava Pestov if (ca->set) { 2211697e2349SColy Li BUG_ON(ca->set->cache != ca); 2212697e2349SColy Li ca->set->cache = NULL; 2213c9a78332SSlava Pestov } 2214cafe5635SKent Overstreet 2215c954ac8dSColy Li free_pages((unsigned long) ca->disk_buckets, ilog2(meta_bucket_pages(&ca->sb))); 2216cafe5635SKent Overstreet kfree(ca->prio_buckets); 2217cafe5635SKent Overstreet vfree(ca->buckets); 2218cafe5635SKent Overstreet 2219cafe5635SKent Overstreet free_heap(&ca->heap); 2220cafe5635SKent Overstreet free_fifo(&ca->free_inc); 222178365411SKent Overstreet 222278365411SKent Overstreet for (i = 0; i < RESERVE_NR; i++) 222378365411SKent Overstreet free_fifo(&ca->free[i]); 2224cafe5635SKent Overstreet 2225475389aeSChristoph Hellwig if (ca->sb_disk) 2226475389aeSChristoph Hellwig put_page(virt_to_page(ca->sb_disk)); 2227cafe5635SKent Overstreet 22280781c874SKent Overstreet if (!IS_ERR_OR_NULL(ca->bdev)) 2229cafe5635SKent Overstreet blkdev_put(ca->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 2230cafe5635SKent Overstreet 2231cafe5635SKent Overstreet kfree(ca); 2232cafe5635SKent Overstreet module_put(THIS_MODULE); 2233cafe5635SKent Overstreet } 2234cafe5635SKent Overstreet 2235c50d4d5dSYijing Wang static int cache_alloc(struct cache *ca) 2236cafe5635SKent Overstreet { 2237cafe5635SKent Overstreet size_t free; 2238682811b3STang Junhui size_t btree_buckets; 2239cafe5635SKent Overstreet struct bucket *b; 2240f6027bcaSDongbo Cao int ret = -ENOMEM; 2241f6027bcaSDongbo Cao const char *err = NULL; 2242cafe5635SKent Overstreet 2243cafe5635SKent Overstreet __module_get(THIS_MODULE); 2244cafe5635SKent Overstreet kobject_init(&ca->kobj, &bch_cache_ktype); 2245cafe5635SKent Overstreet 22463a83f467SMing Lei bio_init(&ca->journal.bio, ca->journal.bio.bi_inline_vecs, 8); 2247cafe5635SKent Overstreet 2248682811b3STang Junhui /* 2249682811b3STang Junhui * when ca->sb.njournal_buckets is not zero, journal exists, 2250682811b3STang Junhui * and in bch_journal_replay(), tree node may split, 2251682811b3STang Junhui * so bucket of RESERVE_BTREE type is needed, 2252682811b3STang Junhui * the worst situation is all journal buckets are valid journal, 2253682811b3STang Junhui * and all the keys need to replay, 2254682811b3STang Junhui * so the number of RESERVE_BTREE type buckets should be as much 2255682811b3STang Junhui * as journal buckets 2256682811b3STang Junhui */ 2257682811b3STang Junhui btree_buckets = ca->sb.njournal_buckets ?: 8; 225878365411SKent Overstreet free = roundup_pow_of_two(ca->sb.nbuckets) >> 10; 22593a646fd7SDongbo Cao if (!free) { 22603a646fd7SDongbo Cao ret = -EPERM; 22613a646fd7SDongbo Cao err = "ca->sb.nbuckets is too small"; 22623a646fd7SDongbo Cao goto err_free; 22633a646fd7SDongbo Cao } 2264cafe5635SKent Overstreet 2265f6027bcaSDongbo Cao if (!init_fifo(&ca->free[RESERVE_BTREE], btree_buckets, 2266f6027bcaSDongbo Cao GFP_KERNEL)) { 2267f6027bcaSDongbo Cao err = "ca->free[RESERVE_BTREE] alloc failed"; 2268f6027bcaSDongbo Cao goto err_btree_alloc; 2269f6027bcaSDongbo Cao } 2270f6027bcaSDongbo Cao 2271f6027bcaSDongbo Cao if (!init_fifo_exact(&ca->free[RESERVE_PRIO], prio_buckets(ca), 2272f6027bcaSDongbo Cao GFP_KERNEL)) { 2273f6027bcaSDongbo Cao err = "ca->free[RESERVE_PRIO] alloc failed"; 2274f6027bcaSDongbo Cao goto err_prio_alloc; 2275f6027bcaSDongbo Cao } 2276f6027bcaSDongbo Cao 2277f6027bcaSDongbo Cao if (!init_fifo(&ca->free[RESERVE_MOVINGGC], free, GFP_KERNEL)) { 2278f6027bcaSDongbo Cao err = "ca->free[RESERVE_MOVINGGC] alloc failed"; 2279f6027bcaSDongbo Cao goto err_movinggc_alloc; 2280f6027bcaSDongbo Cao } 2281f6027bcaSDongbo Cao 2282f6027bcaSDongbo Cao if (!init_fifo(&ca->free[RESERVE_NONE], free, GFP_KERNEL)) { 2283f6027bcaSDongbo Cao err = "ca->free[RESERVE_NONE] alloc failed"; 2284f6027bcaSDongbo Cao goto err_none_alloc; 2285f6027bcaSDongbo Cao } 2286f6027bcaSDongbo Cao 2287f6027bcaSDongbo Cao if (!init_fifo(&ca->free_inc, free << 2, GFP_KERNEL)) { 2288f6027bcaSDongbo Cao err = "ca->free_inc alloc failed"; 2289f6027bcaSDongbo Cao goto err_free_inc_alloc; 2290f6027bcaSDongbo Cao } 2291f6027bcaSDongbo Cao 2292f6027bcaSDongbo Cao if (!init_heap(&ca->heap, free << 3, GFP_KERNEL)) { 2293f6027bcaSDongbo Cao err = "ca->heap alloc failed"; 2294f6027bcaSDongbo Cao goto err_heap_alloc; 2295f6027bcaSDongbo Cao } 2296f6027bcaSDongbo Cao 2297f6027bcaSDongbo Cao ca->buckets = vzalloc(array_size(sizeof(struct bucket), 2298f6027bcaSDongbo Cao ca->sb.nbuckets)); 2299f6027bcaSDongbo Cao if (!ca->buckets) { 2300f6027bcaSDongbo Cao err = "ca->buckets alloc failed"; 2301f6027bcaSDongbo Cao goto err_buckets_alloc; 2302f6027bcaSDongbo Cao } 2303f6027bcaSDongbo Cao 2304f6027bcaSDongbo Cao ca->prio_buckets = kzalloc(array3_size(sizeof(uint64_t), 23056396bb22SKees Cook prio_buckets(ca), 2), 2306f6027bcaSDongbo Cao GFP_KERNEL); 2307f6027bcaSDongbo Cao if (!ca->prio_buckets) { 2308f6027bcaSDongbo Cao err = "ca->prio_buckets alloc failed"; 2309f6027bcaSDongbo Cao goto err_prio_buckets_alloc; 2310f6027bcaSDongbo Cao } 2311f6027bcaSDongbo Cao 2312c954ac8dSColy Li ca->disk_buckets = alloc_meta_bucket_pages(GFP_KERNEL, &ca->sb); 2313f6027bcaSDongbo Cao if (!ca->disk_buckets) { 2314f6027bcaSDongbo Cao err = "ca->disk_buckets alloc failed"; 2315f6027bcaSDongbo Cao goto err_disk_buckets_alloc; 2316f6027bcaSDongbo Cao } 2317cafe5635SKent Overstreet 2318cafe5635SKent Overstreet ca->prio_last_buckets = ca->prio_buckets + prio_buckets(ca); 2319cafe5635SKent Overstreet 2320cafe5635SKent Overstreet for_each_bucket(b, ca) 2321cafe5635SKent Overstreet atomic_set(&b->pin, 0); 2322cafe5635SKent Overstreet return 0; 2323f6027bcaSDongbo Cao 2324f6027bcaSDongbo Cao err_disk_buckets_alloc: 2325f6027bcaSDongbo Cao kfree(ca->prio_buckets); 2326f6027bcaSDongbo Cao err_prio_buckets_alloc: 2327f6027bcaSDongbo Cao vfree(ca->buckets); 2328f6027bcaSDongbo Cao err_buckets_alloc: 2329f6027bcaSDongbo Cao free_heap(&ca->heap); 2330f6027bcaSDongbo Cao err_heap_alloc: 2331f6027bcaSDongbo Cao free_fifo(&ca->free_inc); 2332f6027bcaSDongbo Cao err_free_inc_alloc: 2333f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_NONE]); 2334f6027bcaSDongbo Cao err_none_alloc: 2335f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_MOVINGGC]); 2336f6027bcaSDongbo Cao err_movinggc_alloc: 2337f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_PRIO]); 2338f6027bcaSDongbo Cao err_prio_alloc: 2339f6027bcaSDongbo Cao free_fifo(&ca->free[RESERVE_BTREE]); 2340f6027bcaSDongbo Cao err_btree_alloc: 23413a646fd7SDongbo Cao err_free: 2342f6027bcaSDongbo Cao module_put(THIS_MODULE); 2343f6027bcaSDongbo Cao if (err) 234446f5aa88SJoe Perches pr_notice("error %s: %s\n", ca->cache_dev_name, err); 2345f6027bcaSDongbo Cao return ret; 2346cafe5635SKent Overstreet } 2347cafe5635SKent Overstreet 2348cfa0c56dSChristoph Hellwig static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk, 2349cafe5635SKent Overstreet struct block_device *bdev, struct cache *ca) 2350cafe5635SKent Overstreet { 2351d9dc1702SEric Wheeler const char *err = NULL; /* must be set for any error case */ 23529b299728SEric Wheeler int ret = 0; 2353cafe5635SKent Overstreet 23546e916a7eSColy Li bdevname(bdev, ca->cache_dev_name); 2355f59fce84SKent Overstreet memcpy(&ca->sb, sb, sizeof(struct cache_sb)); 2356cafe5635SKent Overstreet ca->bdev = bdev; 2357cafe5635SKent Overstreet ca->bdev->bd_holder = ca; 2358475389aeSChristoph Hellwig ca->sb_disk = sb_disk; 2359f59fce84SKent Overstreet 2360cc40daf9STang Junhui if (blk_queue_discard(bdev_get_queue(bdev))) 2361cafe5635SKent Overstreet ca->discard = CACHE_DISCARD(&ca->sb); 2362cafe5635SKent Overstreet 2363c50d4d5dSYijing Wang ret = cache_alloc(ca); 2364d9dc1702SEric Wheeler if (ret != 0) { 2365bb6d355cSColy Li /* 2366bb6d355cSColy Li * If we failed here, it means ca->kobj is not initialized yet, 2367bb6d355cSColy Li * kobject_put() won't be called and there is no chance to 2368bb6d355cSColy Li * call blkdev_put() to bdev in bch_cache_release(). So we 2369bb6d355cSColy Li * explicitly call blkdev_put() here. 2370bb6d355cSColy Li */ 2371cc40daf9STang Junhui blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 2372d9dc1702SEric Wheeler if (ret == -ENOMEM) 2373d9dc1702SEric Wheeler err = "cache_alloc(): -ENOMEM"; 23743a646fd7SDongbo Cao else if (ret == -EPERM) 23753a646fd7SDongbo Cao err = "cache_alloc(): cache device is too small"; 2376d9dc1702SEric Wheeler else 2377d9dc1702SEric Wheeler err = "cache_alloc(): unknown error"; 2378f59fce84SKent Overstreet goto err; 2379d9dc1702SEric Wheeler } 2380f59fce84SKent Overstreet 23818d65269fSChristoph Hellwig if (kobject_add(&ca->kobj, bdev_kobj(bdev), "bcache")) { 23829b299728SEric Wheeler err = "error calling kobject_add"; 23839b299728SEric Wheeler ret = -ENOMEM; 23849b299728SEric Wheeler goto out; 23859b299728SEric Wheeler } 2386cafe5635SKent Overstreet 23874fa03402SKent Overstreet mutex_lock(&bch_register_lock); 2388cafe5635SKent Overstreet err = register_cache_set(ca); 23894fa03402SKent Overstreet mutex_unlock(&bch_register_lock); 23904fa03402SKent Overstreet 23919b299728SEric Wheeler if (err) { 23929b299728SEric Wheeler ret = -ENODEV; 23939b299728SEric Wheeler goto out; 23949b299728SEric Wheeler } 2395cafe5635SKent Overstreet 239646f5aa88SJoe Perches pr_info("registered cache device %s\n", ca->cache_dev_name); 23979b299728SEric Wheeler 2398d83353b3SKent Overstreet out: 2399d83353b3SKent Overstreet kobject_put(&ca->kobj); 24009b299728SEric Wheeler 2401cafe5635SKent Overstreet err: 24029b299728SEric Wheeler if (err) 240346f5aa88SJoe Perches pr_notice("error %s: %s\n", ca->cache_dev_name, err); 24049b299728SEric Wheeler 24059b299728SEric Wheeler return ret; 2406cafe5635SKent Overstreet } 2407cafe5635SKent Overstreet 2408cafe5635SKent Overstreet /* Global interfaces/init */ 2409cafe5635SKent Overstreet 2410fc2d5988SColy Li static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, 2411fc2d5988SColy Li const char *buffer, size_t size); 24120c277e21SColy Li static ssize_t bch_pending_bdevs_cleanup(struct kobject *k, 24130c277e21SColy Li struct kobj_attribute *attr, 24140c277e21SColy Li const char *buffer, size_t size); 2415cafe5635SKent Overstreet 2416cafe5635SKent Overstreet kobj_attribute_write(register, register_bcache); 2417cafe5635SKent Overstreet kobj_attribute_write(register_quiet, register_bcache); 24180c277e21SColy Li kobj_attribute_write(pendings_cleanup, bch_pending_bdevs_cleanup); 2419cafe5635SKent Overstreet 24204e7b5671SChristoph Hellwig static bool bch_is_open_backing(dev_t dev) 2421b3cf37bfSColy Li { 2422a9dd53adSGabriel de Perthuis struct cache_set *c, *tc; 2423a9dd53adSGabriel de Perthuis struct cached_dev *dc, *t; 2424a9dd53adSGabriel de Perthuis 2425a9dd53adSGabriel de Perthuis list_for_each_entry_safe(c, tc, &bch_cache_sets, list) 2426a9dd53adSGabriel de Perthuis list_for_each_entry_safe(dc, t, &c->cached_devs, list) 24274e7b5671SChristoph Hellwig if (dc->bdev->bd_dev == dev) 2428a9dd53adSGabriel de Perthuis return true; 2429a9dd53adSGabriel de Perthuis list_for_each_entry_safe(dc, t, &uncached_devices, list) 24304e7b5671SChristoph Hellwig if (dc->bdev->bd_dev == dev) 2431a9dd53adSGabriel de Perthuis return true; 2432a9dd53adSGabriel de Perthuis return false; 2433a9dd53adSGabriel de Perthuis } 2434a9dd53adSGabriel de Perthuis 24354e7b5671SChristoph Hellwig static bool bch_is_open_cache(dev_t dev) 2436b3cf37bfSColy Li { 2437a9dd53adSGabriel de Perthuis struct cache_set *c, *tc; 2438a9dd53adSGabriel de Perthuis 243908fdb2cdSColy Li list_for_each_entry_safe(c, tc, &bch_cache_sets, list) { 244008fdb2cdSColy Li struct cache *ca = c->cache; 244108fdb2cdSColy Li 24424e7b5671SChristoph Hellwig if (ca->bdev->bd_dev == dev) 2443a9dd53adSGabriel de Perthuis return true; 244408fdb2cdSColy Li } 244508fdb2cdSColy Li 2446a9dd53adSGabriel de Perthuis return false; 2447a9dd53adSGabriel de Perthuis } 2448a9dd53adSGabriel de Perthuis 24494e7b5671SChristoph Hellwig static bool bch_is_open(dev_t dev) 2450b3cf37bfSColy Li { 24514e7b5671SChristoph Hellwig return bch_is_open_cache(dev) || bch_is_open_backing(dev); 2452a9dd53adSGabriel de Perthuis } 2453a9dd53adSGabriel de Perthuis 24549e23ccf8SColy Li struct async_reg_args { 2455ee4a36f4SColy Li struct delayed_work reg_work; 24569e23ccf8SColy Li char *path; 24579e23ccf8SColy Li struct cache_sb *sb; 24589e23ccf8SColy Li struct cache_sb_disk *sb_disk; 24599e23ccf8SColy Li struct block_device *bdev; 24609e23ccf8SColy Li }; 24619e23ccf8SColy Li 24629e23ccf8SColy Li static void register_bdev_worker(struct work_struct *work) 24639e23ccf8SColy Li { 24649e23ccf8SColy Li int fail = false; 24659e23ccf8SColy Li struct async_reg_args *args = 2466ee4a36f4SColy Li container_of(work, struct async_reg_args, reg_work.work); 24679e23ccf8SColy Li struct cached_dev *dc; 24689e23ccf8SColy Li 24699e23ccf8SColy Li dc = kzalloc(sizeof(*dc), GFP_KERNEL); 24709e23ccf8SColy Li if (!dc) { 24719e23ccf8SColy Li fail = true; 24729e23ccf8SColy Li put_page(virt_to_page(args->sb_disk)); 24739e23ccf8SColy Li blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 24749e23ccf8SColy Li goto out; 24759e23ccf8SColy Li } 24769e23ccf8SColy Li 24779e23ccf8SColy Li mutex_lock(&bch_register_lock); 24789e23ccf8SColy Li if (register_bdev(args->sb, args->sb_disk, args->bdev, dc) < 0) 24799e23ccf8SColy Li fail = true; 24809e23ccf8SColy Li mutex_unlock(&bch_register_lock); 24819e23ccf8SColy Li 24829e23ccf8SColy Li out: 24839e23ccf8SColy Li if (fail) 24849e23ccf8SColy Li pr_info("error %s: fail to register backing device\n", 24859e23ccf8SColy Li args->path); 24869e23ccf8SColy Li kfree(args->sb); 24879e23ccf8SColy Li kfree(args->path); 24889e23ccf8SColy Li kfree(args); 24899e23ccf8SColy Li module_put(THIS_MODULE); 24909e23ccf8SColy Li } 24919e23ccf8SColy Li 24929e23ccf8SColy Li static void register_cache_worker(struct work_struct *work) 24939e23ccf8SColy Li { 24949e23ccf8SColy Li int fail = false; 24959e23ccf8SColy Li struct async_reg_args *args = 2496ee4a36f4SColy Li container_of(work, struct async_reg_args, reg_work.work); 24979e23ccf8SColy Li struct cache *ca; 24989e23ccf8SColy Li 24999e23ccf8SColy Li ca = kzalloc(sizeof(*ca), GFP_KERNEL); 25009e23ccf8SColy Li if (!ca) { 25019e23ccf8SColy Li fail = true; 25029e23ccf8SColy Li put_page(virt_to_page(args->sb_disk)); 25039e23ccf8SColy Li blkdev_put(args->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 25049e23ccf8SColy Li goto out; 25059e23ccf8SColy Li } 25069e23ccf8SColy Li 25079e23ccf8SColy Li /* blkdev_put() will be called in bch_cache_release() */ 25089e23ccf8SColy Li if (register_cache(args->sb, args->sb_disk, args->bdev, ca) != 0) 25099e23ccf8SColy Li fail = true; 25109e23ccf8SColy Li 25119e23ccf8SColy Li out: 25129e23ccf8SColy Li if (fail) 25139e23ccf8SColy Li pr_info("error %s: fail to register cache device\n", 25149e23ccf8SColy Li args->path); 25159e23ccf8SColy Li kfree(args->sb); 25169e23ccf8SColy Li kfree(args->path); 25179e23ccf8SColy Li kfree(args); 25189e23ccf8SColy Li module_put(THIS_MODULE); 25199e23ccf8SColy Li } 25209e23ccf8SColy Li 2521d7fae7b4SKai Krakow static void register_device_async(struct async_reg_args *args) 25229e23ccf8SColy Li { 25239e23ccf8SColy Li if (SB_IS_BDEV(args->sb)) 2524ee4a36f4SColy Li INIT_DELAYED_WORK(&args->reg_work, register_bdev_worker); 25259e23ccf8SColy Li else 2526ee4a36f4SColy Li INIT_DELAYED_WORK(&args->reg_work, register_cache_worker); 25279e23ccf8SColy Li 2528ee4a36f4SColy Li /* 10 jiffies is enough for a delay */ 2529ee4a36f4SColy Li queue_delayed_work(system_wq, &args->reg_work, 10); 25309e23ccf8SColy Li } 25319e23ccf8SColy Li 2532cafe5635SKent Overstreet static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr, 2533cafe5635SKent Overstreet const char *buffer, size_t size) 2534cafe5635SKent Overstreet { 253550246693SChristoph Hellwig const char *err; 253629cda393SColy Li char *path = NULL; 253750246693SChristoph Hellwig struct cache_sb *sb; 2538cfa0c56dSChristoph Hellwig struct cache_sb_disk *sb_disk; 2539fc8f19ccSChristoph Hellwig struct block_device *bdev; 254050246693SChristoph Hellwig ssize_t ret; 2541a58e88bfSColy Li bool async_registration = false; 2542a58e88bfSColy Li 2543a58e88bfSColy Li #ifdef CONFIG_BCACHE_ASYNC_REGISTRATION 2544a58e88bfSColy Li async_registration = true; 2545a58e88bfSColy Li #endif 2546cafe5635SKent Overstreet 254750246693SChristoph Hellwig ret = -EBUSY; 254829cda393SColy Li err = "failed to reference bcache module"; 2549cafe5635SKent Overstreet if (!try_module_get(THIS_MODULE)) 255050246693SChristoph Hellwig goto out; 2551cafe5635SKent Overstreet 2552a59ff6ccSColy Li /* For latest state of bcache_is_reboot */ 2553a59ff6ccSColy Li smp_mb(); 255429cda393SColy Li err = "bcache is in reboot"; 2555a59ff6ccSColy Li if (bcache_is_reboot) 255650246693SChristoph Hellwig goto out_module_put; 2557a59ff6ccSColy Li 255850246693SChristoph Hellwig ret = -ENOMEM; 255950246693SChristoph Hellwig err = "cannot allocate memory"; 2560a56489d4SFlorian Schmaus path = kstrndup(buffer, size, GFP_KERNEL); 2561a56489d4SFlorian Schmaus if (!path) 256250246693SChristoph Hellwig goto out_module_put; 2563a56489d4SFlorian Schmaus 2564a56489d4SFlorian Schmaus sb = kmalloc(sizeof(struct cache_sb), GFP_KERNEL); 2565a56489d4SFlorian Schmaus if (!sb) 256650246693SChristoph Hellwig goto out_free_path; 2567cafe5635SKent Overstreet 256850246693SChristoph Hellwig ret = -EINVAL; 2569cafe5635SKent Overstreet err = "failed to open device"; 2570cafe5635SKent Overstreet bdev = blkdev_get_by_path(strim(path), 2571cafe5635SKent Overstreet FMODE_READ|FMODE_WRITE|FMODE_EXCL, 2572cafe5635SKent Overstreet sb); 2573f59fce84SKent Overstreet if (IS_ERR(bdev)) { 2574a9dd53adSGabriel de Perthuis if (bdev == ERR_PTR(-EBUSY)) { 25754e7b5671SChristoph Hellwig dev_t dev; 25764e7b5671SChristoph Hellwig 2577789d21dbSJianjian Huo mutex_lock(&bch_register_lock); 25784e7b5671SChristoph Hellwig if (lookup_bdev(strim(path), &dev) == 0 && 25794e7b5671SChristoph Hellwig bch_is_open(dev)) 2580a9dd53adSGabriel de Perthuis err = "device already registered"; 2581a9dd53adSGabriel de Perthuis else 2582cafe5635SKent Overstreet err = "device busy"; 2583789d21dbSJianjian Huo mutex_unlock(&bch_register_lock); 2584d7076f21SGabriel de Perthuis if (attr == &ksysfs_register_quiet) 258550246693SChristoph Hellwig goto done; 2586a9dd53adSGabriel de Perthuis } 258750246693SChristoph Hellwig goto out_free_sb; 2588f59fce84SKent Overstreet } 2589f59fce84SKent Overstreet 2590f59fce84SKent Overstreet err = "failed to set blocksize"; 2591f59fce84SKent Overstreet if (set_blocksize(bdev, 4096)) 259250246693SChristoph Hellwig goto out_blkdev_put; 2593cafe5635SKent Overstreet 2594cfa0c56dSChristoph Hellwig err = read_super(sb, bdev, &sb_disk); 2595cafe5635SKent Overstreet if (err) 259650246693SChristoph Hellwig goto out_blkdev_put; 2597cafe5635SKent Overstreet 2598cc40daf9STang Junhui err = "failed to register device"; 2599a58e88bfSColy Li 2600a58e88bfSColy Li if (async_registration) { 26019e23ccf8SColy Li /* register in asynchronous way */ 26029e23ccf8SColy Li struct async_reg_args *args = 26039e23ccf8SColy Li kzalloc(sizeof(struct async_reg_args), GFP_KERNEL); 26049e23ccf8SColy Li 26059e23ccf8SColy Li if (!args) { 26069e23ccf8SColy Li ret = -ENOMEM; 26079e23ccf8SColy Li err = "cannot allocate memory"; 26089e23ccf8SColy Li goto out_put_sb_page; 26099e23ccf8SColy Li } 26109e23ccf8SColy Li 26119e23ccf8SColy Li args->path = path; 26129e23ccf8SColy Li args->sb = sb; 26139e23ccf8SColy Li args->sb_disk = sb_disk; 26149e23ccf8SColy Li args->bdev = bdev; 2615d7fae7b4SKai Krakow register_device_async(args); 26169e23ccf8SColy Li /* No wait and returns to user space */ 26179e23ccf8SColy Li goto async_done; 26189e23ccf8SColy Li } 26199e23ccf8SColy Li 26202903381fSKent Overstreet if (SB_IS_BDEV(sb)) { 2621cafe5635SKent Overstreet struct cached_dev *dc = kzalloc(sizeof(*dc), GFP_KERNEL); 26221fae7cf0SColy Li 2623f59fce84SKent Overstreet if (!dc) 262450246693SChristoph Hellwig goto out_put_sb_page; 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 2635f59fce84SKent Overstreet if (!ca) 263650246693SChristoph Hellwig goto out_put_sb_page; 2637cafe5635SKent Overstreet 2638bb6d355cSColy Li /* blkdev_put() will be called in bch_cache_release() */ 2639cfa0c56dSChristoph Hellwig if (register_cache(sb, sb_disk, bdev, ca) != 0) 2640fc8f19ccSChristoph Hellwig goto out_free_sb; 264150246693SChristoph Hellwig } 264250246693SChristoph Hellwig 264350246693SChristoph Hellwig done: 2644f59fce84SKent Overstreet kfree(sb); 2645f59fce84SKent Overstreet kfree(path); 2646f59fce84SKent Overstreet module_put(THIS_MODULE); 26479e23ccf8SColy Li async_done: 264850246693SChristoph Hellwig return size; 2649f59fce84SKent Overstreet 265050246693SChristoph Hellwig out_put_sb_page: 2651cfa0c56dSChristoph Hellwig put_page(virt_to_page(sb_disk)); 265250246693SChristoph Hellwig out_blkdev_put: 2653cafe5635SKent Overstreet blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 265450246693SChristoph Hellwig out_free_sb: 265550246693SChristoph Hellwig kfree(sb); 265650246693SChristoph Hellwig out_free_path: 265750246693SChristoph Hellwig kfree(path); 2658ae3cd299SColy Li path = NULL; 265950246693SChristoph Hellwig out_module_put: 266050246693SChristoph Hellwig module_put(THIS_MODULE); 266150246693SChristoph Hellwig out: 266246f5aa88SJoe Perches pr_info("error %s: %s\n", path?path:"", err); 266350246693SChristoph Hellwig return ret; 2664cafe5635SKent Overstreet } 2665cafe5635SKent Overstreet 26660c277e21SColy Li 26670c277e21SColy Li struct pdev { 26680c277e21SColy Li struct list_head list; 26690c277e21SColy Li struct cached_dev *dc; 26700c277e21SColy Li }; 26710c277e21SColy Li 26720c277e21SColy Li static ssize_t bch_pending_bdevs_cleanup(struct kobject *k, 26730c277e21SColy Li struct kobj_attribute *attr, 26740c277e21SColy Li const char *buffer, 26750c277e21SColy Li size_t size) 26760c277e21SColy Li { 26770c277e21SColy Li LIST_HEAD(pending_devs); 26780c277e21SColy Li ssize_t ret = size; 26790c277e21SColy Li struct cached_dev *dc, *tdc; 26800c277e21SColy Li struct pdev *pdev, *tpdev; 26810c277e21SColy Li struct cache_set *c, *tc; 26820c277e21SColy Li 26830c277e21SColy Li mutex_lock(&bch_register_lock); 26840c277e21SColy Li list_for_each_entry_safe(dc, tdc, &uncached_devices, list) { 26850c277e21SColy Li pdev = kmalloc(sizeof(struct pdev), GFP_KERNEL); 26860c277e21SColy Li if (!pdev) 26870c277e21SColy Li break; 26880c277e21SColy Li pdev->dc = dc; 26890c277e21SColy Li list_add(&pdev->list, &pending_devs); 26900c277e21SColy Li } 26910c277e21SColy Li 26920c277e21SColy Li list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) { 26930c277e21SColy Li char *pdev_set_uuid = pdev->dc->sb.set_uuid; 2694e8092707SYi Li list_for_each_entry_safe(c, tc, &bch_cache_sets, list) { 26951132e56eSColy Li char *set_uuid = c->set_uuid; 26960c277e21SColy Li 26970c277e21SColy Li if (!memcmp(pdev_set_uuid, set_uuid, 16)) { 26980c277e21SColy Li list_del(&pdev->list); 26990c277e21SColy Li kfree(pdev); 27000c277e21SColy Li break; 27010c277e21SColy Li } 27020c277e21SColy Li } 27030c277e21SColy Li } 27040c277e21SColy Li mutex_unlock(&bch_register_lock); 27050c277e21SColy Li 27060c277e21SColy Li list_for_each_entry_safe(pdev, tpdev, &pending_devs, list) { 270746f5aa88SJoe Perches pr_info("delete pdev %p\n", pdev); 27080c277e21SColy Li list_del(&pdev->list); 27090c277e21SColy Li bcache_device_stop(&pdev->dc->disk); 27100c277e21SColy Li kfree(pdev); 27110c277e21SColy Li } 27120c277e21SColy Li 27130c277e21SColy Li return ret; 27140c277e21SColy Li } 27150c277e21SColy Li 2716cafe5635SKent Overstreet static int bcache_reboot(struct notifier_block *n, unsigned long code, void *x) 2717cafe5635SKent Overstreet { 2718a59ff6ccSColy Li if (bcache_is_reboot) 2719a59ff6ccSColy Li return NOTIFY_DONE; 2720a59ff6ccSColy Li 2721cafe5635SKent Overstreet if (code == SYS_DOWN || 2722cafe5635SKent Overstreet code == SYS_HALT || 2723cafe5635SKent Overstreet code == SYS_POWER_OFF) { 2724cafe5635SKent Overstreet DEFINE_WAIT(wait); 2725cafe5635SKent Overstreet unsigned long start = jiffies; 2726cafe5635SKent Overstreet bool stopped = false; 2727cafe5635SKent Overstreet 2728cafe5635SKent Overstreet struct cache_set *c, *tc; 2729cafe5635SKent Overstreet struct cached_dev *dc, *tdc; 2730cafe5635SKent Overstreet 2731cafe5635SKent Overstreet mutex_lock(&bch_register_lock); 2732cafe5635SKent Overstreet 2733a59ff6ccSColy Li if (bcache_is_reboot) 2734a59ff6ccSColy Li goto out; 2735a59ff6ccSColy Li 2736a59ff6ccSColy Li /* New registration is rejected since now */ 2737a59ff6ccSColy Li bcache_is_reboot = true; 2738a59ff6ccSColy Li /* 2739a59ff6ccSColy Li * Make registering caller (if there is) on other CPU 2740a59ff6ccSColy Li * core know bcache_is_reboot set to true earlier 2741a59ff6ccSColy Li */ 2742a59ff6ccSColy Li smp_mb(); 2743a59ff6ccSColy Li 2744cafe5635SKent Overstreet if (list_empty(&bch_cache_sets) && 2745cafe5635SKent Overstreet list_empty(&uncached_devices)) 2746cafe5635SKent Overstreet goto out; 2747cafe5635SKent Overstreet 2748a59ff6ccSColy Li mutex_unlock(&bch_register_lock); 2749a59ff6ccSColy Li 275046f5aa88SJoe Perches pr_info("Stopping all devices:\n"); 2751cafe5635SKent Overstreet 2752a59ff6ccSColy Li /* 2753a59ff6ccSColy Li * The reason bch_register_lock is not held to call 2754a59ff6ccSColy Li * bch_cache_set_stop() and bcache_device_stop() is to 2755a59ff6ccSColy Li * avoid potential deadlock during reboot, because cache 2756a59ff6ccSColy Li * set or bcache device stopping process will acqurie 2757a59ff6ccSColy Li * bch_register_lock too. 2758a59ff6ccSColy Li * 2759a59ff6ccSColy Li * We are safe here because bcache_is_reboot sets to 2760a59ff6ccSColy Li * true already, register_bcache() will reject new 2761a59ff6ccSColy Li * registration now. bcache_is_reboot also makes sure 2762a59ff6ccSColy Li * bcache_reboot() won't be re-entered on by other thread, 2763a59ff6ccSColy Li * so there is no race in following list iteration by 2764a59ff6ccSColy Li * list_for_each_entry_safe(). 2765a59ff6ccSColy Li */ 2766cafe5635SKent Overstreet list_for_each_entry_safe(c, tc, &bch_cache_sets, list) 2767cafe5635SKent Overstreet bch_cache_set_stop(c); 2768cafe5635SKent Overstreet 2769cafe5635SKent Overstreet list_for_each_entry_safe(dc, tdc, &uncached_devices, list) 2770cafe5635SKent Overstreet bcache_device_stop(&dc->disk); 2771cafe5635SKent Overstreet 2772eb8cbb6dSColy Li 2773eb8cbb6dSColy Li /* 2774eb8cbb6dSColy Li * Give an early chance for other kthreads and 2775eb8cbb6dSColy Li * kworkers to stop themselves 2776eb8cbb6dSColy Li */ 2777eb8cbb6dSColy Li schedule(); 2778eb8cbb6dSColy Li 2779cafe5635SKent Overstreet /* What's a condition variable? */ 2780cafe5635SKent Overstreet while (1) { 2781eb8cbb6dSColy Li long timeout = start + 10 * HZ - jiffies; 2782cafe5635SKent Overstreet 2783eb8cbb6dSColy Li mutex_lock(&bch_register_lock); 2784cafe5635SKent Overstreet stopped = list_empty(&bch_cache_sets) && 2785cafe5635SKent Overstreet list_empty(&uncached_devices); 2786cafe5635SKent Overstreet 2787cafe5635SKent Overstreet if (timeout < 0 || stopped) 2788cafe5635SKent Overstreet break; 2789cafe5635SKent Overstreet 2790cafe5635SKent Overstreet prepare_to_wait(&unregister_wait, &wait, 2791cafe5635SKent Overstreet TASK_UNINTERRUPTIBLE); 2792cafe5635SKent Overstreet 2793cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 2794cafe5635SKent Overstreet schedule_timeout(timeout); 2795cafe5635SKent Overstreet } 2796cafe5635SKent Overstreet 2797cafe5635SKent Overstreet finish_wait(&unregister_wait, &wait); 2798cafe5635SKent Overstreet 2799cafe5635SKent Overstreet if (stopped) 280046f5aa88SJoe Perches pr_info("All devices stopped\n"); 2801cafe5635SKent Overstreet else 280246f5aa88SJoe Perches pr_notice("Timeout waiting for devices to be closed\n"); 2803cafe5635SKent Overstreet out: 2804cafe5635SKent Overstreet mutex_unlock(&bch_register_lock); 2805cafe5635SKent Overstreet } 2806cafe5635SKent Overstreet 2807cafe5635SKent Overstreet return NOTIFY_DONE; 2808cafe5635SKent Overstreet } 2809cafe5635SKent Overstreet 2810cafe5635SKent Overstreet static struct notifier_block reboot = { 2811cafe5635SKent Overstreet .notifier_call = bcache_reboot, 2812cafe5635SKent Overstreet .priority = INT_MAX, /* before any real devices */ 2813cafe5635SKent Overstreet }; 2814cafe5635SKent Overstreet 2815cafe5635SKent Overstreet static void bcache_exit(void) 2816cafe5635SKent Overstreet { 2817cafe5635SKent Overstreet bch_debug_exit(); 2818cafe5635SKent Overstreet bch_request_exit(); 2819cafe5635SKent Overstreet if (bcache_kobj) 2820cafe5635SKent Overstreet kobject_put(bcache_kobj); 2821cafe5635SKent Overstreet if (bcache_wq) 2822cafe5635SKent Overstreet destroy_workqueue(bcache_wq); 28230f843e65SGuoju Fang if (bch_journal_wq) 28240f843e65SGuoju Fang destroy_workqueue(bch_journal_wq); 2825*afe78ab4SKai Krakow if (bch_flush_wq) 2826*afe78ab4SKai Krakow destroy_workqueue(bch_flush_wq); 28279f233ffeSKai Krakow bch_btree_exit(); 28280f843e65SGuoju Fang 28295c41c8a7SKent Overstreet if (bcache_major) 2830cafe5635SKent Overstreet unregister_blkdev(bcache_major, "bcache"); 2831cafe5635SKent Overstreet unregister_reboot_notifier(&reboot); 2832330a4db8SLiang Chen mutex_destroy(&bch_register_lock); 2833cafe5635SKent Overstreet } 2834cafe5635SKent Overstreet 28359aaf5165SColy Li /* Check and fixup module parameters */ 28369aaf5165SColy Li static void check_module_parameters(void) 28379aaf5165SColy Li { 28389aaf5165SColy Li if (bch_cutoff_writeback_sync == 0) 28399aaf5165SColy Li bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC; 28409aaf5165SColy Li else if (bch_cutoff_writeback_sync > CUTOFF_WRITEBACK_SYNC_MAX) { 284146f5aa88SJoe Perches pr_warn("set bch_cutoff_writeback_sync (%u) to max value %u\n", 28429aaf5165SColy Li bch_cutoff_writeback_sync, CUTOFF_WRITEBACK_SYNC_MAX); 28439aaf5165SColy Li bch_cutoff_writeback_sync = CUTOFF_WRITEBACK_SYNC_MAX; 28449aaf5165SColy Li } 28459aaf5165SColy Li 28469aaf5165SColy Li if (bch_cutoff_writeback == 0) 28479aaf5165SColy Li bch_cutoff_writeback = CUTOFF_WRITEBACK; 28489aaf5165SColy Li else if (bch_cutoff_writeback > CUTOFF_WRITEBACK_MAX) { 284946f5aa88SJoe Perches pr_warn("set bch_cutoff_writeback (%u) to max value %u\n", 28509aaf5165SColy Li bch_cutoff_writeback, CUTOFF_WRITEBACK_MAX); 28519aaf5165SColy Li bch_cutoff_writeback = CUTOFF_WRITEBACK_MAX; 28529aaf5165SColy Li } 28539aaf5165SColy Li 28549aaf5165SColy Li if (bch_cutoff_writeback > bch_cutoff_writeback_sync) { 285546f5aa88SJoe Perches pr_warn("set bch_cutoff_writeback (%u) to %u\n", 28569aaf5165SColy Li bch_cutoff_writeback, bch_cutoff_writeback_sync); 28579aaf5165SColy Li bch_cutoff_writeback = bch_cutoff_writeback_sync; 28589aaf5165SColy Li } 28599aaf5165SColy Li } 28609aaf5165SColy Li 2861cafe5635SKent Overstreet static int __init bcache_init(void) 2862cafe5635SKent Overstreet { 2863cafe5635SKent Overstreet static const struct attribute *files[] = { 2864cafe5635SKent Overstreet &ksysfs_register.attr, 2865cafe5635SKent Overstreet &ksysfs_register_quiet.attr, 28660c277e21SColy Li &ksysfs_pendings_cleanup.attr, 2867cafe5635SKent Overstreet NULL 2868cafe5635SKent Overstreet }; 2869cafe5635SKent Overstreet 28709aaf5165SColy Li check_module_parameters(); 28719aaf5165SColy Li 2872cafe5635SKent Overstreet mutex_init(&bch_register_lock); 2873cafe5635SKent Overstreet init_waitqueue_head(&unregister_wait); 2874cafe5635SKent Overstreet register_reboot_notifier(&reboot); 2875cafe5635SKent Overstreet 2876cafe5635SKent Overstreet bcache_major = register_blkdev(0, "bcache"); 28772ecf0cdbSZheng Liu if (bcache_major < 0) { 28782ecf0cdbSZheng Liu unregister_reboot_notifier(&reboot); 2879330a4db8SLiang Chen mutex_destroy(&bch_register_lock); 2880cafe5635SKent Overstreet return bcache_major; 28812ecf0cdbSZheng Liu } 2882cafe5635SKent Overstreet 28839f233ffeSKai Krakow if (bch_btree_init()) 28849f233ffeSKai Krakow goto err; 28859f233ffeSKai Krakow 288616c1fdf4SFlorian Schmaus bcache_wq = alloc_workqueue("bcache", WQ_MEM_RECLAIM, 0); 288716c1fdf4SFlorian Schmaus if (!bcache_wq) 288816c1fdf4SFlorian Schmaus goto err; 288916c1fdf4SFlorian Schmaus 2890*afe78ab4SKai Krakow /* 2891*afe78ab4SKai Krakow * Let's not make this `WQ_MEM_RECLAIM` for the following reasons: 2892*afe78ab4SKai Krakow * 2893*afe78ab4SKai Krakow * 1. It used `system_wq` before which also does no memory reclaim. 2894*afe78ab4SKai Krakow * 2. With `WQ_MEM_RECLAIM` desktop stalls, increased boot times, and 2895*afe78ab4SKai Krakow * reduced throughput can be observed. 2896*afe78ab4SKai Krakow * 2897*afe78ab4SKai Krakow * We still want to user our own queue to not congest the `system_wq`. 2898*afe78ab4SKai Krakow */ 2899*afe78ab4SKai Krakow bch_flush_wq = alloc_workqueue("bch_flush", 0, 0); 2900*afe78ab4SKai Krakow if (!bch_flush_wq) 2901*afe78ab4SKai Krakow goto err; 2902*afe78ab4SKai Krakow 29030f843e65SGuoju Fang bch_journal_wq = alloc_workqueue("bch_journal", WQ_MEM_RECLAIM, 0); 29040f843e65SGuoju Fang if (!bch_journal_wq) 29050f843e65SGuoju Fang goto err; 29060f843e65SGuoju Fang 290716c1fdf4SFlorian Schmaus bcache_kobj = kobject_create_and_add("bcache", fs_kobj); 290816c1fdf4SFlorian Schmaus if (!bcache_kobj) 290916c1fdf4SFlorian Schmaus goto err; 291016c1fdf4SFlorian Schmaus 291116c1fdf4SFlorian Schmaus if (bch_request_init() || 2912330a4db8SLiang Chen sysfs_create_files(bcache_kobj, files)) 2913cafe5635SKent Overstreet goto err; 2914cafe5635SKent Overstreet 291591bafdf0SDongbo Cao bch_debug_init(); 291678ac2107SColy Li closure_debug_init(); 291778ac2107SColy Li 2918a59ff6ccSColy Li bcache_is_reboot = false; 2919a59ff6ccSColy Li 2920cafe5635SKent Overstreet return 0; 2921cafe5635SKent Overstreet err: 2922cafe5635SKent Overstreet bcache_exit(); 2923cafe5635SKent Overstreet return -ENOMEM; 2924cafe5635SKent Overstreet } 2925cafe5635SKent Overstreet 29269aaf5165SColy Li /* 29279aaf5165SColy Li * Module hooks 29289aaf5165SColy Li */ 2929cafe5635SKent Overstreet module_exit(bcache_exit); 2930cafe5635SKent Overstreet module_init(bcache_init); 2931009673d0SColy Li 29329aaf5165SColy Li module_param(bch_cutoff_writeback, uint, 0); 29339aaf5165SColy Li MODULE_PARM_DESC(bch_cutoff_writeback, "threshold to cutoff writeback"); 29349aaf5165SColy Li 29359aaf5165SColy Li module_param(bch_cutoff_writeback_sync, uint, 0); 29369aaf5165SColy Li MODULE_PARM_DESC(bch_cutoff_writeback_sync, "hard threshold to cutoff writeback"); 29379aaf5165SColy Li 2938009673d0SColy Li MODULE_DESCRIPTION("Bcache: a Linux block layer cache"); 2939009673d0SColy Li MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 2940009673d0SColy Li MODULE_LICENSE("GPL"); 2941