Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2018 Red Hat. All rights reserved.
0004  *
0005  * This file is released under the GPL.
0006  */
0007 
0008 #include <linux/device-mapper.h>
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011 #include <linux/vmalloc.h>
0012 #include <linux/kthread.h>
0013 #include <linux/dm-io.h>
0014 #include <linux/dm-kcopyd.h>
0015 #include <linux/dax.h>
0016 #include <linux/pfn_t.h>
0017 #include <linux/libnvdimm.h>
0018 #include <linux/delay.h>
0019 #include "dm-io-tracker.h"
0020 
0021 #define DM_MSG_PREFIX "writecache"
0022 
0023 #define HIGH_WATERMARK          50
0024 #define LOW_WATERMARK           45
0025 #define MAX_WRITEBACK_JOBS      min(0x10000000 / PAGE_SIZE, totalram_pages() / 16)
0026 #define ENDIO_LATENCY           16
0027 #define WRITEBACK_LATENCY       64
0028 #define AUTOCOMMIT_BLOCKS_SSD       65536
0029 #define AUTOCOMMIT_BLOCKS_PMEM      64
0030 #define AUTOCOMMIT_MSEC         1000
0031 #define MAX_AGE_DIV         16
0032 #define MAX_AGE_UNSPECIFIED     -1UL
0033 #define PAUSE_WRITEBACK         (HZ * 3)
0034 
0035 #define BITMAP_GRANULARITY  65536
0036 #if BITMAP_GRANULARITY < PAGE_SIZE
0037 #undef BITMAP_GRANULARITY
0038 #define BITMAP_GRANULARITY  PAGE_SIZE
0039 #endif
0040 
0041 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_FS_DAX)
0042 #define DM_WRITECACHE_HAS_PMEM
0043 #endif
0044 
0045 #ifdef DM_WRITECACHE_HAS_PMEM
0046 #define pmem_assign(dest, src)                  \
0047 do {                                \
0048     typeof(dest) uniq = (src);              \
0049     memcpy_flushcache(&(dest), &uniq, sizeof(dest));    \
0050 } while (0)
0051 #else
0052 #define pmem_assign(dest, src)  ((dest) = (src))
0053 #endif
0054 
0055 #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
0056 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
0057 #endif
0058 
0059 #define MEMORY_SUPERBLOCK_MAGIC     0x23489321
0060 #define MEMORY_SUPERBLOCK_VERSION   1
0061 
0062 struct wc_memory_entry {
0063     __le64 original_sector;
0064     __le64 seq_count;
0065 };
0066 
0067 struct wc_memory_superblock {
0068     union {
0069         struct {
0070             __le32 magic;
0071             __le32 version;
0072             __le32 block_size;
0073             __le32 pad;
0074             __le64 n_blocks;
0075             __le64 seq_count;
0076         };
0077         __le64 padding[8];
0078     };
0079     struct wc_memory_entry entries[];
0080 };
0081 
0082 struct wc_entry {
0083     struct rb_node rb_node;
0084     struct list_head lru;
0085     unsigned short wc_list_contiguous;
0086     bool write_in_progress
0087 #if BITS_PER_LONG == 64
0088         :1
0089 #endif
0090     ;
0091     unsigned long index
0092 #if BITS_PER_LONG == 64
0093         :47
0094 #endif
0095     ;
0096     unsigned long age;
0097 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
0098     uint64_t original_sector;
0099     uint64_t seq_count;
0100 #endif
0101 };
0102 
0103 #ifdef DM_WRITECACHE_HAS_PMEM
0104 #define WC_MODE_PMEM(wc)            ((wc)->pmem_mode)
0105 #define WC_MODE_FUA(wc)             ((wc)->writeback_fua)
0106 #else
0107 #define WC_MODE_PMEM(wc)            false
0108 #define WC_MODE_FUA(wc)             false
0109 #endif
0110 #define WC_MODE_SORT_FREELIST(wc)       (!WC_MODE_PMEM(wc))
0111 
0112 struct dm_writecache {
0113     struct mutex lock;
0114     struct list_head lru;
0115     union {
0116         struct list_head freelist;
0117         struct {
0118             struct rb_root freetree;
0119             struct wc_entry *current_free;
0120         };
0121     };
0122     struct rb_root tree;
0123 
0124     size_t freelist_size;
0125     size_t writeback_size;
0126     size_t freelist_high_watermark;
0127     size_t freelist_low_watermark;
0128     unsigned long max_age;
0129     unsigned long pause;
0130 
0131     unsigned uncommitted_blocks;
0132     unsigned autocommit_blocks;
0133     unsigned max_writeback_jobs;
0134 
0135     int error;
0136 
0137     unsigned long autocommit_jiffies;
0138     struct timer_list autocommit_timer;
0139     struct wait_queue_head freelist_wait;
0140 
0141     struct timer_list max_age_timer;
0142 
0143     atomic_t bio_in_progress[2];
0144     struct wait_queue_head bio_in_progress_wait[2];
0145 
0146     struct dm_target *ti;
0147     struct dm_dev *dev;
0148     struct dm_dev *ssd_dev;
0149     sector_t start_sector;
0150     void *memory_map;
0151     uint64_t memory_map_size;
0152     size_t metadata_sectors;
0153     size_t n_blocks;
0154     uint64_t seq_count;
0155     sector_t data_device_sectors;
0156     void *block_start;
0157     struct wc_entry *entries;
0158     unsigned block_size;
0159     unsigned char block_size_bits;
0160 
0161     bool pmem_mode:1;
0162     bool writeback_fua:1;
0163 
0164     bool overwrote_committed:1;
0165     bool memory_vmapped:1;
0166 
0167     bool start_sector_set:1;
0168     bool high_wm_percent_set:1;
0169     bool low_wm_percent_set:1;
0170     bool max_writeback_jobs_set:1;
0171     bool autocommit_blocks_set:1;
0172     bool autocommit_time_set:1;
0173     bool max_age_set:1;
0174     bool writeback_fua_set:1;
0175     bool flush_on_suspend:1;
0176     bool cleaner:1;
0177     bool cleaner_set:1;
0178     bool metadata_only:1;
0179     bool pause_set:1;
0180 
0181     unsigned high_wm_percent_value;
0182     unsigned low_wm_percent_value;
0183     unsigned autocommit_time_value;
0184     unsigned max_age_value;
0185     unsigned pause_value;
0186 
0187     unsigned writeback_all;
0188     struct workqueue_struct *writeback_wq;
0189     struct work_struct writeback_work;
0190     struct work_struct flush_work;
0191 
0192     struct dm_io_tracker iot;
0193 
0194     struct dm_io_client *dm_io;
0195 
0196     raw_spinlock_t endio_list_lock;
0197     struct list_head endio_list;
0198     struct task_struct *endio_thread;
0199 
0200     struct task_struct *flush_thread;
0201     struct bio_list flush_list;
0202 
0203     struct dm_kcopyd_client *dm_kcopyd;
0204     unsigned long *dirty_bitmap;
0205     unsigned dirty_bitmap_size;
0206 
0207     struct bio_set bio_set;
0208     mempool_t copy_pool;
0209 
0210     struct {
0211         unsigned long long reads;
0212         unsigned long long read_hits;
0213         unsigned long long writes;
0214         unsigned long long write_hits_uncommitted;
0215         unsigned long long write_hits_committed;
0216         unsigned long long writes_around;
0217         unsigned long long writes_allocate;
0218         unsigned long long writes_blocked_on_freelist;
0219         unsigned long long flushes;
0220         unsigned long long discards;
0221     } stats;
0222 };
0223 
0224 #define WB_LIST_INLINE      16
0225 
0226 struct writeback_struct {
0227     struct list_head endio_entry;
0228     struct dm_writecache *wc;
0229     struct wc_entry **wc_list;
0230     unsigned wc_list_n;
0231     struct wc_entry *wc_list_inline[WB_LIST_INLINE];
0232     struct bio bio;
0233 };
0234 
0235 struct copy_struct {
0236     struct list_head endio_entry;
0237     struct dm_writecache *wc;
0238     struct wc_entry *e;
0239     unsigned n_entries;
0240     int error;
0241 };
0242 
0243 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
0244                         "A percentage of time allocated for data copying");
0245 
0246 static void wc_lock(struct dm_writecache *wc)
0247 {
0248     mutex_lock(&wc->lock);
0249 }
0250 
0251 static void wc_unlock(struct dm_writecache *wc)
0252 {
0253     mutex_unlock(&wc->lock);
0254 }
0255 
0256 #ifdef DM_WRITECACHE_HAS_PMEM
0257 static int persistent_memory_claim(struct dm_writecache *wc)
0258 {
0259     int r;
0260     loff_t s;
0261     long p, da;
0262     pfn_t pfn;
0263     int id;
0264     struct page **pages;
0265     sector_t offset;
0266 
0267     wc->memory_vmapped = false;
0268 
0269     s = wc->memory_map_size;
0270     p = s >> PAGE_SHIFT;
0271     if (!p) {
0272         r = -EINVAL;
0273         goto err1;
0274     }
0275     if (p != s >> PAGE_SHIFT) {
0276         r = -EOVERFLOW;
0277         goto err1;
0278     }
0279 
0280     offset = get_start_sect(wc->ssd_dev->bdev);
0281     if (offset & (PAGE_SIZE / 512 - 1)) {
0282         r = -EINVAL;
0283         goto err1;
0284     }
0285     offset >>= PAGE_SHIFT - 9;
0286 
0287     id = dax_read_lock();
0288 
0289     da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, DAX_ACCESS,
0290             &wc->memory_map, &pfn);
0291     if (da < 0) {
0292         wc->memory_map = NULL;
0293         r = da;
0294         goto err2;
0295     }
0296     if (!pfn_t_has_page(pfn)) {
0297         wc->memory_map = NULL;
0298         r = -EOPNOTSUPP;
0299         goto err2;
0300     }
0301     if (da != p) {
0302         long i;
0303         wc->memory_map = NULL;
0304         pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
0305         if (!pages) {
0306             r = -ENOMEM;
0307             goto err2;
0308         }
0309         i = 0;
0310         do {
0311             long daa;
0312             daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i,
0313                     p - i, DAX_ACCESS, NULL, &pfn);
0314             if (daa <= 0) {
0315                 r = daa ? daa : -EINVAL;
0316                 goto err3;
0317             }
0318             if (!pfn_t_has_page(pfn)) {
0319                 r = -EOPNOTSUPP;
0320                 goto err3;
0321             }
0322             while (daa-- && i < p) {
0323                 pages[i++] = pfn_t_to_page(pfn);
0324                 pfn.val++;
0325                 if (!(i & 15))
0326                     cond_resched();
0327             }
0328         } while (i < p);
0329         wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
0330         if (!wc->memory_map) {
0331             r = -ENOMEM;
0332             goto err3;
0333         }
0334         kvfree(pages);
0335         wc->memory_vmapped = true;
0336     }
0337 
0338     dax_read_unlock(id);
0339 
0340     wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
0341     wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
0342 
0343     return 0;
0344 err3:
0345     kvfree(pages);
0346 err2:
0347     dax_read_unlock(id);
0348 err1:
0349     return r;
0350 }
0351 #else
0352 static int persistent_memory_claim(struct dm_writecache *wc)
0353 {
0354     return -EOPNOTSUPP;
0355 }
0356 #endif
0357 
0358 static void persistent_memory_release(struct dm_writecache *wc)
0359 {
0360     if (wc->memory_vmapped)
0361         vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
0362 }
0363 
0364 static struct page *persistent_memory_page(void *addr)
0365 {
0366     if (is_vmalloc_addr(addr))
0367         return vmalloc_to_page(addr);
0368     else
0369         return virt_to_page(addr);
0370 }
0371 
0372 static unsigned persistent_memory_page_offset(void *addr)
0373 {
0374     return (unsigned long)addr & (PAGE_SIZE - 1);
0375 }
0376 
0377 static void persistent_memory_flush_cache(void *ptr, size_t size)
0378 {
0379     if (is_vmalloc_addr(ptr))
0380         flush_kernel_vmap_range(ptr, size);
0381 }
0382 
0383 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
0384 {
0385     if (is_vmalloc_addr(ptr))
0386         invalidate_kernel_vmap_range(ptr, size);
0387 }
0388 
0389 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
0390 {
0391     return wc->memory_map;
0392 }
0393 
0394 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
0395 {
0396     return &sb(wc)->entries[e->index];
0397 }
0398 
0399 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
0400 {
0401     return (char *)wc->block_start + (e->index << wc->block_size_bits);
0402 }
0403 
0404 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
0405 {
0406     return wc->start_sector + wc->metadata_sectors +
0407         ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
0408 }
0409 
0410 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
0411 {
0412 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
0413     return e->original_sector;
0414 #else
0415     return le64_to_cpu(memory_entry(wc, e)->original_sector);
0416 #endif
0417 }
0418 
0419 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
0420 {
0421 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
0422     return e->seq_count;
0423 #else
0424     return le64_to_cpu(memory_entry(wc, e)->seq_count);
0425 #endif
0426 }
0427 
0428 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
0429 {
0430 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
0431     e->seq_count = -1;
0432 #endif
0433     pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
0434 }
0435 
0436 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
0437                         uint64_t original_sector, uint64_t seq_count)
0438 {
0439     struct wc_memory_entry me;
0440 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
0441     e->original_sector = original_sector;
0442     e->seq_count = seq_count;
0443 #endif
0444     me.original_sector = cpu_to_le64(original_sector);
0445     me.seq_count = cpu_to_le64(seq_count);
0446     pmem_assign(*memory_entry(wc, e), me);
0447 }
0448 
0449 #define writecache_error(wc, err, msg, arg...)              \
0450 do {                                    \
0451     if (!cmpxchg(&(wc)->error, 0, err))             \
0452         DMERR(msg, ##arg);                  \
0453     wake_up(&(wc)->freelist_wait);                  \
0454 } while (0)
0455 
0456 #define writecache_has_error(wc)    (unlikely(READ_ONCE((wc)->error)))
0457 
0458 static void writecache_flush_all_metadata(struct dm_writecache *wc)
0459 {
0460     if (!WC_MODE_PMEM(wc))
0461         memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
0462 }
0463 
0464 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
0465 {
0466     if (!WC_MODE_PMEM(wc))
0467         __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
0468               wc->dirty_bitmap);
0469 }
0470 
0471 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
0472 
0473 struct io_notify {
0474     struct dm_writecache *wc;
0475     struct completion c;
0476     atomic_t count;
0477 };
0478 
0479 static void writecache_notify_io(unsigned long error, void *context)
0480 {
0481     struct io_notify *endio = context;
0482 
0483     if (unlikely(error != 0))
0484         writecache_error(endio->wc, -EIO, "error writing metadata");
0485     BUG_ON(atomic_read(&endio->count) <= 0);
0486     if (atomic_dec_and_test(&endio->count))
0487         complete(&endio->c);
0488 }
0489 
0490 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
0491 {
0492     wait_event(wc->bio_in_progress_wait[direction],
0493            !atomic_read(&wc->bio_in_progress[direction]));
0494 }
0495 
0496 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
0497 {
0498     struct dm_io_region region;
0499     struct dm_io_request req;
0500     struct io_notify endio = {
0501         wc,
0502         COMPLETION_INITIALIZER_ONSTACK(endio.c),
0503         ATOMIC_INIT(1),
0504     };
0505     unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
0506     unsigned i = 0;
0507 
0508     while (1) {
0509         unsigned j;
0510         i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
0511         if (unlikely(i == bitmap_bits))
0512             break;
0513         j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
0514 
0515         region.bdev = wc->ssd_dev->bdev;
0516         region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
0517         region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
0518 
0519         if (unlikely(region.sector >= wc->metadata_sectors))
0520             break;
0521         if (unlikely(region.sector + region.count > wc->metadata_sectors))
0522             region.count = wc->metadata_sectors - region.sector;
0523 
0524         region.sector += wc->start_sector;
0525         atomic_inc(&endio.count);
0526         req.bi_opf = REQ_OP_WRITE | REQ_SYNC;
0527         req.mem.type = DM_IO_VMA;
0528         req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
0529         req.client = wc->dm_io;
0530         req.notify.fn = writecache_notify_io;
0531         req.notify.context = &endio;
0532 
0533         /* writing via async dm-io (implied by notify.fn above) won't return an error */
0534             (void) dm_io(&req, 1, &region, NULL);
0535         i = j;
0536     }
0537 
0538     writecache_notify_io(0, &endio);
0539     wait_for_completion_io(&endio.c);
0540 
0541     if (wait_for_ios)
0542         writecache_wait_for_ios(wc, WRITE);
0543 
0544     writecache_disk_flush(wc, wc->ssd_dev);
0545 
0546     memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
0547 }
0548 
0549 static void ssd_commit_superblock(struct dm_writecache *wc)
0550 {
0551     int r;
0552     struct dm_io_region region;
0553     struct dm_io_request req;
0554 
0555     region.bdev = wc->ssd_dev->bdev;
0556     region.sector = 0;
0557     region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
0558 
0559     if (unlikely(region.sector + region.count > wc->metadata_sectors))
0560         region.count = wc->metadata_sectors - region.sector;
0561 
0562     region.sector += wc->start_sector;
0563 
0564     req.bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_FUA;
0565     req.mem.type = DM_IO_VMA;
0566     req.mem.ptr.vma = (char *)wc->memory_map;
0567     req.client = wc->dm_io;
0568     req.notify.fn = NULL;
0569     req.notify.context = NULL;
0570 
0571     r = dm_io(&req, 1, &region, NULL);
0572     if (unlikely(r))
0573         writecache_error(wc, r, "error writing superblock");
0574 }
0575 
0576 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
0577 {
0578     if (WC_MODE_PMEM(wc))
0579         pmem_wmb();
0580     else
0581         ssd_commit_flushed(wc, wait_for_ios);
0582 }
0583 
0584 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
0585 {
0586     int r;
0587     struct dm_io_region region;
0588     struct dm_io_request req;
0589 
0590     region.bdev = dev->bdev;
0591     region.sector = 0;
0592     region.count = 0;
0593     req.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
0594     req.mem.type = DM_IO_KMEM;
0595     req.mem.ptr.addr = NULL;
0596     req.client = wc->dm_io;
0597     req.notify.fn = NULL;
0598 
0599     r = dm_io(&req, 1, &region, NULL);
0600     if (unlikely(r))
0601         writecache_error(wc, r, "error flushing metadata: %d", r);
0602 }
0603 
0604 #define WFE_RETURN_FOLLOWING    1
0605 #define WFE_LOWEST_SEQ      2
0606 
0607 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
0608                           uint64_t block, int flags)
0609 {
0610     struct wc_entry *e;
0611     struct rb_node *node = wc->tree.rb_node;
0612 
0613     if (unlikely(!node))
0614         return NULL;
0615 
0616     while (1) {
0617         e = container_of(node, struct wc_entry, rb_node);
0618         if (read_original_sector(wc, e) == block)
0619             break;
0620 
0621         node = (read_original_sector(wc, e) >= block ?
0622             e->rb_node.rb_left : e->rb_node.rb_right);
0623         if (unlikely(!node)) {
0624             if (!(flags & WFE_RETURN_FOLLOWING))
0625                 return NULL;
0626             if (read_original_sector(wc, e) >= block) {
0627                 return e;
0628             } else {
0629                 node = rb_next(&e->rb_node);
0630                 if (unlikely(!node))
0631                     return NULL;
0632                 e = container_of(node, struct wc_entry, rb_node);
0633                 return e;
0634             }
0635         }
0636     }
0637 
0638     while (1) {
0639         struct wc_entry *e2;
0640         if (flags & WFE_LOWEST_SEQ)
0641             node = rb_prev(&e->rb_node);
0642         else
0643             node = rb_next(&e->rb_node);
0644         if (unlikely(!node))
0645             return e;
0646         e2 = container_of(node, struct wc_entry, rb_node);
0647         if (read_original_sector(wc, e2) != block)
0648             return e;
0649         e = e2;
0650     }
0651 }
0652 
0653 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
0654 {
0655     struct wc_entry *e;
0656     struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
0657 
0658     while (*node) {
0659         e = container_of(*node, struct wc_entry, rb_node);
0660         parent = &e->rb_node;
0661         if (read_original_sector(wc, e) > read_original_sector(wc, ins))
0662             node = &parent->rb_left;
0663         else
0664             node = &parent->rb_right;
0665     }
0666     rb_link_node(&ins->rb_node, parent, node);
0667     rb_insert_color(&ins->rb_node, &wc->tree);
0668     list_add(&ins->lru, &wc->lru);
0669     ins->age = jiffies;
0670 }
0671 
0672 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
0673 {
0674     list_del(&e->lru);
0675     rb_erase(&e->rb_node, &wc->tree);
0676 }
0677 
0678 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
0679 {
0680     if (WC_MODE_SORT_FREELIST(wc)) {
0681         struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
0682         if (unlikely(!*node))
0683             wc->current_free = e;
0684         while (*node) {
0685             parent = *node;
0686             if (&e->rb_node < *node)
0687                 node = &parent->rb_left;
0688             else
0689                 node = &parent->rb_right;
0690         }
0691         rb_link_node(&e->rb_node, parent, node);
0692         rb_insert_color(&e->rb_node, &wc->freetree);
0693     } else {
0694         list_add_tail(&e->lru, &wc->freelist);
0695     }
0696     wc->freelist_size++;
0697 }
0698 
0699 static inline void writecache_verify_watermark(struct dm_writecache *wc)
0700 {
0701     if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
0702         queue_work(wc->writeback_wq, &wc->writeback_work);
0703 }
0704 
0705 static void writecache_max_age_timer(struct timer_list *t)
0706 {
0707     struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
0708 
0709     if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
0710         queue_work(wc->writeback_wq, &wc->writeback_work);
0711         mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
0712     }
0713 }
0714 
0715 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
0716 {
0717     struct wc_entry *e;
0718 
0719     if (WC_MODE_SORT_FREELIST(wc)) {
0720         struct rb_node *next;
0721         if (unlikely(!wc->current_free))
0722             return NULL;
0723         e = wc->current_free;
0724         if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
0725             return NULL;
0726         next = rb_next(&e->rb_node);
0727         rb_erase(&e->rb_node, &wc->freetree);
0728         if (unlikely(!next))
0729             next = rb_first(&wc->freetree);
0730         wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
0731     } else {
0732         if (unlikely(list_empty(&wc->freelist)))
0733             return NULL;
0734         e = container_of(wc->freelist.next, struct wc_entry, lru);
0735         if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
0736             return NULL;
0737         list_del(&e->lru);
0738     }
0739     wc->freelist_size--;
0740 
0741     writecache_verify_watermark(wc);
0742 
0743     return e;
0744 }
0745 
0746 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
0747 {
0748     writecache_unlink(wc, e);
0749     writecache_add_to_freelist(wc, e);
0750     clear_seq_count(wc, e);
0751     writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
0752     if (unlikely(waitqueue_active(&wc->freelist_wait)))
0753         wake_up(&wc->freelist_wait);
0754 }
0755 
0756 static void writecache_wait_on_freelist(struct dm_writecache *wc)
0757 {
0758     DEFINE_WAIT(wait);
0759 
0760     prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
0761     wc_unlock(wc);
0762     io_schedule();
0763     finish_wait(&wc->freelist_wait, &wait);
0764     wc_lock(wc);
0765 }
0766 
0767 static void writecache_poison_lists(struct dm_writecache *wc)
0768 {
0769     /*
0770      * Catch incorrect access to these values while the device is suspended.
0771      */
0772     memset(&wc->tree, -1, sizeof wc->tree);
0773     wc->lru.next = LIST_POISON1;
0774     wc->lru.prev = LIST_POISON2;
0775     wc->freelist.next = LIST_POISON1;
0776     wc->freelist.prev = LIST_POISON2;
0777 }
0778 
0779 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
0780 {
0781     writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
0782     if (WC_MODE_PMEM(wc))
0783         writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
0784 }
0785 
0786 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
0787 {
0788     return read_seq_count(wc, e) < wc->seq_count;
0789 }
0790 
0791 static void writecache_flush(struct dm_writecache *wc)
0792 {
0793     struct wc_entry *e, *e2;
0794     bool need_flush_after_free;
0795 
0796     wc->uncommitted_blocks = 0;
0797     del_timer(&wc->autocommit_timer);
0798 
0799     if (list_empty(&wc->lru))
0800         return;
0801 
0802     e = container_of(wc->lru.next, struct wc_entry, lru);
0803     if (writecache_entry_is_committed(wc, e)) {
0804         if (wc->overwrote_committed) {
0805             writecache_wait_for_ios(wc, WRITE);
0806             writecache_disk_flush(wc, wc->ssd_dev);
0807             wc->overwrote_committed = false;
0808         }
0809         return;
0810     }
0811     while (1) {
0812         writecache_flush_entry(wc, e);
0813         if (unlikely(e->lru.next == &wc->lru))
0814             break;
0815         e2 = container_of(e->lru.next, struct wc_entry, lru);
0816         if (writecache_entry_is_committed(wc, e2))
0817             break;
0818         e = e2;
0819         cond_resched();
0820     }
0821     writecache_commit_flushed(wc, true);
0822 
0823     wc->seq_count++;
0824     pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
0825     if (WC_MODE_PMEM(wc))
0826         writecache_commit_flushed(wc, false);
0827     else
0828         ssd_commit_superblock(wc);
0829 
0830     wc->overwrote_committed = false;
0831 
0832     need_flush_after_free = false;
0833     while (1) {
0834         /* Free another committed entry with lower seq-count */
0835         struct rb_node *rb_node = rb_prev(&e->rb_node);
0836 
0837         if (rb_node) {
0838             e2 = container_of(rb_node, struct wc_entry, rb_node);
0839             if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
0840                 likely(!e2->write_in_progress)) {
0841                 writecache_free_entry(wc, e2);
0842                 need_flush_after_free = true;
0843             }
0844         }
0845         if (unlikely(e->lru.prev == &wc->lru))
0846             break;
0847         e = container_of(e->lru.prev, struct wc_entry, lru);
0848         cond_resched();
0849     }
0850 
0851     if (need_flush_after_free)
0852         writecache_commit_flushed(wc, false);
0853 }
0854 
0855 static void writecache_flush_work(struct work_struct *work)
0856 {
0857     struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
0858 
0859     wc_lock(wc);
0860     writecache_flush(wc);
0861     wc_unlock(wc);
0862 }
0863 
0864 static void writecache_autocommit_timer(struct timer_list *t)
0865 {
0866     struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
0867     if (!writecache_has_error(wc))
0868         queue_work(wc->writeback_wq, &wc->flush_work);
0869 }
0870 
0871 static void writecache_schedule_autocommit(struct dm_writecache *wc)
0872 {
0873     if (!timer_pending(&wc->autocommit_timer))
0874         mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
0875 }
0876 
0877 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
0878 {
0879     struct wc_entry *e;
0880     bool discarded_something = false;
0881 
0882     e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
0883     if (unlikely(!e))
0884         return;
0885 
0886     while (read_original_sector(wc, e) < end) {
0887         struct rb_node *node = rb_next(&e->rb_node);
0888 
0889         if (likely(!e->write_in_progress)) {
0890             if (!discarded_something) {
0891                 if (!WC_MODE_PMEM(wc)) {
0892                     writecache_wait_for_ios(wc, READ);
0893                     writecache_wait_for_ios(wc, WRITE);
0894                 }
0895                 discarded_something = true;
0896             }
0897             if (!writecache_entry_is_committed(wc, e))
0898                 wc->uncommitted_blocks--;
0899             writecache_free_entry(wc, e);
0900         }
0901 
0902         if (unlikely(!node))
0903             break;
0904 
0905         e = container_of(node, struct wc_entry, rb_node);
0906     }
0907 
0908     if (discarded_something)
0909         writecache_commit_flushed(wc, false);
0910 }
0911 
0912 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
0913 {
0914     if (wc->writeback_size) {
0915         writecache_wait_on_freelist(wc);
0916         return true;
0917     }
0918     return false;
0919 }
0920 
0921 static void writecache_suspend(struct dm_target *ti)
0922 {
0923     struct dm_writecache *wc = ti->private;
0924     bool flush_on_suspend;
0925 
0926     del_timer_sync(&wc->autocommit_timer);
0927     del_timer_sync(&wc->max_age_timer);
0928 
0929     wc_lock(wc);
0930     writecache_flush(wc);
0931     flush_on_suspend = wc->flush_on_suspend;
0932     if (flush_on_suspend) {
0933         wc->flush_on_suspend = false;
0934         wc->writeback_all++;
0935         queue_work(wc->writeback_wq, &wc->writeback_work);
0936     }
0937     wc_unlock(wc);
0938 
0939     drain_workqueue(wc->writeback_wq);
0940 
0941     wc_lock(wc);
0942     if (flush_on_suspend)
0943         wc->writeback_all--;
0944     while (writecache_wait_for_writeback(wc));
0945 
0946     if (WC_MODE_PMEM(wc))
0947         persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
0948 
0949     writecache_poison_lists(wc);
0950 
0951     wc_unlock(wc);
0952 }
0953 
0954 static int writecache_alloc_entries(struct dm_writecache *wc)
0955 {
0956     size_t b;
0957 
0958     if (wc->entries)
0959         return 0;
0960     wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
0961     if (!wc->entries)
0962         return -ENOMEM;
0963     for (b = 0; b < wc->n_blocks; b++) {
0964         struct wc_entry *e = &wc->entries[b];
0965         e->index = b;
0966         e->write_in_progress = false;
0967         cond_resched();
0968     }
0969 
0970     return 0;
0971 }
0972 
0973 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
0974 {
0975     struct dm_io_region region;
0976     struct dm_io_request req;
0977 
0978     region.bdev = wc->ssd_dev->bdev;
0979     region.sector = wc->start_sector;
0980     region.count = n_sectors;
0981     req.bi_opf = REQ_OP_READ | REQ_SYNC;
0982     req.mem.type = DM_IO_VMA;
0983     req.mem.ptr.vma = (char *)wc->memory_map;
0984     req.client = wc->dm_io;
0985     req.notify.fn = NULL;
0986 
0987     return dm_io(&req, 1, &region, NULL);
0988 }
0989 
0990 static void writecache_resume(struct dm_target *ti)
0991 {
0992     struct dm_writecache *wc = ti->private;
0993     size_t b;
0994     bool need_flush = false;
0995     __le64 sb_seq_count;
0996     int r;
0997 
0998     wc_lock(wc);
0999 
1000     wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
1001 
1002     if (WC_MODE_PMEM(wc)) {
1003         persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
1004     } else {
1005         r = writecache_read_metadata(wc, wc->metadata_sectors);
1006         if (r) {
1007             size_t sb_entries_offset;
1008             writecache_error(wc, r, "unable to read metadata: %d", r);
1009             sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
1010             memset((char *)wc->memory_map + sb_entries_offset, -1,
1011                    (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
1012         }
1013     }
1014 
1015     wc->tree = RB_ROOT;
1016     INIT_LIST_HEAD(&wc->lru);
1017     if (WC_MODE_SORT_FREELIST(wc)) {
1018         wc->freetree = RB_ROOT;
1019         wc->current_free = NULL;
1020     } else {
1021         INIT_LIST_HEAD(&wc->freelist);
1022     }
1023     wc->freelist_size = 0;
1024 
1025     r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1026                   sizeof(uint64_t));
1027     if (r) {
1028         writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
1029         sb_seq_count = cpu_to_le64(0);
1030     }
1031     wc->seq_count = le64_to_cpu(sb_seq_count);
1032 
1033 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
1034     for (b = 0; b < wc->n_blocks; b++) {
1035         struct wc_entry *e = &wc->entries[b];
1036         struct wc_memory_entry wme;
1037         if (writecache_has_error(wc)) {
1038             e->original_sector = -1;
1039             e->seq_count = -1;
1040             continue;
1041         }
1042         r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1043                       sizeof(struct wc_memory_entry));
1044         if (r) {
1045             writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
1046                      (unsigned long)b, r);
1047             e->original_sector = -1;
1048             e->seq_count = -1;
1049         } else {
1050             e->original_sector = le64_to_cpu(wme.original_sector);
1051             e->seq_count = le64_to_cpu(wme.seq_count);
1052         }
1053         cond_resched();
1054     }
1055 #endif
1056     for (b = 0; b < wc->n_blocks; b++) {
1057         struct wc_entry *e = &wc->entries[b];
1058         if (!writecache_entry_is_committed(wc, e)) {
1059             if (read_seq_count(wc, e) != -1) {
1060 erase_this:
1061                 clear_seq_count(wc, e);
1062                 need_flush = true;
1063             }
1064             writecache_add_to_freelist(wc, e);
1065         } else {
1066             struct wc_entry *old;
1067 
1068             old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
1069             if (!old) {
1070                 writecache_insert_entry(wc, e);
1071             } else {
1072                 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1073                     writecache_error(wc, -EINVAL,
1074                          "two identical entries, position %llu, sector %llu, sequence %llu",
1075                          (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1076                          (unsigned long long)read_seq_count(wc, e));
1077                 }
1078                 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1079                     goto erase_this;
1080                 } else {
1081                     writecache_free_entry(wc, old);
1082                     writecache_insert_entry(wc, e);
1083                     need_flush = true;
1084                 }
1085             }
1086         }
1087         cond_resched();
1088     }
1089 
1090     if (need_flush) {
1091         writecache_flush_all_metadata(wc);
1092         writecache_commit_flushed(wc, false);
1093     }
1094 
1095     writecache_verify_watermark(wc);
1096 
1097     if (wc->max_age != MAX_AGE_UNSPECIFIED)
1098         mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1099 
1100     wc_unlock(wc);
1101 }
1102 
1103 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1104 {
1105     if (argc != 1)
1106         return -EINVAL;
1107 
1108     wc_lock(wc);
1109     if (dm_suspended(wc->ti)) {
1110         wc_unlock(wc);
1111         return -EBUSY;
1112     }
1113     if (writecache_has_error(wc)) {
1114         wc_unlock(wc);
1115         return -EIO;
1116     }
1117 
1118     writecache_flush(wc);
1119     wc->writeback_all++;
1120     queue_work(wc->writeback_wq, &wc->writeback_work);
1121     wc_unlock(wc);
1122 
1123     flush_workqueue(wc->writeback_wq);
1124 
1125     wc_lock(wc);
1126     wc->writeback_all--;
1127     if (writecache_has_error(wc)) {
1128         wc_unlock(wc);
1129         return -EIO;
1130     }
1131     wc_unlock(wc);
1132 
1133     return 0;
1134 }
1135 
1136 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1137 {
1138     if (argc != 1)
1139         return -EINVAL;
1140 
1141     wc_lock(wc);
1142     wc->flush_on_suspend = true;
1143     wc_unlock(wc);
1144 
1145     return 0;
1146 }
1147 
1148 static void activate_cleaner(struct dm_writecache *wc)
1149 {
1150     wc->flush_on_suspend = true;
1151     wc->cleaner = true;
1152     wc->freelist_high_watermark = wc->n_blocks;
1153     wc->freelist_low_watermark = wc->n_blocks;
1154 }
1155 
1156 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1157 {
1158     if (argc != 1)
1159         return -EINVAL;
1160 
1161     wc_lock(wc);
1162     activate_cleaner(wc);
1163     if (!dm_suspended(wc->ti))
1164         writecache_verify_watermark(wc);
1165     wc_unlock(wc);
1166 
1167     return 0;
1168 }
1169 
1170 static int process_clear_stats_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1171 {
1172     if (argc != 1)
1173         return -EINVAL;
1174 
1175     wc_lock(wc);
1176     memset(&wc->stats, 0, sizeof wc->stats);
1177     wc_unlock(wc);
1178 
1179     return 0;
1180 }
1181 
1182 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1183                   char *result, unsigned maxlen)
1184 {
1185     int r = -EINVAL;
1186     struct dm_writecache *wc = ti->private;
1187 
1188     if (!strcasecmp(argv[0], "flush"))
1189         r = process_flush_mesg(argc, argv, wc);
1190     else if (!strcasecmp(argv[0], "flush_on_suspend"))
1191         r = process_flush_on_suspend_mesg(argc, argv, wc);
1192     else if (!strcasecmp(argv[0], "cleaner"))
1193         r = process_cleaner_mesg(argc, argv, wc);
1194     else if (!strcasecmp(argv[0], "clear_stats"))
1195         r = process_clear_stats_mesg(argc, argv, wc);
1196     else
1197         DMERR("unrecognised message received: %s", argv[0]);
1198 
1199     return r;
1200 }
1201 
1202 static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
1203 {
1204     /*
1205      * clflushopt performs better with block size 1024, 2048, 4096
1206      * non-temporal stores perform better with block size 512
1207      *
1208      * block size   512             1024            2048            4096
1209      * movnti       496 MB/s        642 MB/s        725 MB/s        744 MB/s
1210      * clflushopt   373 MB/s        688 MB/s        1.1 GB/s        1.2 GB/s
1211      *
1212      * We see that movnti performs better for 512-byte blocks, and
1213      * clflushopt performs better for 1024-byte and larger blocks. So, we
1214      * prefer clflushopt for sizes >= 768.
1215      *
1216      * NOTE: this happens to be the case now (with dm-writecache's single
1217      * threaded model) but re-evaluate this once memcpy_flushcache() is
1218      * enabled to use movdir64b which might invalidate this performance
1219      * advantage seen with cache-allocating-writes plus flushing.
1220      */
1221 #ifdef CONFIG_X86
1222     if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
1223         likely(boot_cpu_data.x86_clflush_size == 64) &&
1224         likely(size >= 768)) {
1225         do {
1226             memcpy((void *)dest, (void *)source, 64);
1227             clflushopt((void *)dest);
1228             dest += 64;
1229             source += 64;
1230             size -= 64;
1231         } while (size >= 64);
1232         return;
1233     }
1234 #endif
1235     memcpy_flushcache(dest, source, size);
1236 }
1237 
1238 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1239 {
1240     void *buf;
1241     unsigned size;
1242     int rw = bio_data_dir(bio);
1243     unsigned remaining_size = wc->block_size;
1244 
1245     do {
1246         struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1247         buf = bvec_kmap_local(&bv);
1248         size = bv.bv_len;
1249         if (unlikely(size > remaining_size))
1250             size = remaining_size;
1251 
1252         if (rw == READ) {
1253             int r;
1254             r = copy_mc_to_kernel(buf, data, size);
1255             flush_dcache_page(bio_page(bio));
1256             if (unlikely(r)) {
1257                 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1258                 bio->bi_status = BLK_STS_IOERR;
1259             }
1260         } else {
1261             flush_dcache_page(bio_page(bio));
1262             memcpy_flushcache_optimized(data, buf, size);
1263         }
1264 
1265         kunmap_local(buf);
1266 
1267         data = (char *)data + size;
1268         remaining_size -= size;
1269         bio_advance(bio, size);
1270     } while (unlikely(remaining_size));
1271 }
1272 
1273 static int writecache_flush_thread(void *data)
1274 {
1275     struct dm_writecache *wc = data;
1276 
1277     while (1) {
1278         struct bio *bio;
1279 
1280         wc_lock(wc);
1281         bio = bio_list_pop(&wc->flush_list);
1282         if (!bio) {
1283             set_current_state(TASK_INTERRUPTIBLE);
1284             wc_unlock(wc);
1285 
1286             if (unlikely(kthread_should_stop())) {
1287                 set_current_state(TASK_RUNNING);
1288                 break;
1289             }
1290 
1291             schedule();
1292             continue;
1293         }
1294 
1295         if (bio_op(bio) == REQ_OP_DISCARD) {
1296             writecache_discard(wc, bio->bi_iter.bi_sector,
1297                        bio_end_sector(bio));
1298             wc_unlock(wc);
1299             bio_set_dev(bio, wc->dev->bdev);
1300             submit_bio_noacct(bio);
1301         } else {
1302             writecache_flush(wc);
1303             wc_unlock(wc);
1304             if (writecache_has_error(wc))
1305                 bio->bi_status = BLK_STS_IOERR;
1306             bio_endio(bio);
1307         }
1308     }
1309 
1310     return 0;
1311 }
1312 
1313 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1314 {
1315     if (bio_list_empty(&wc->flush_list))
1316         wake_up_process(wc->flush_thread);
1317     bio_list_add(&wc->flush_list, bio);
1318 }
1319 
1320 enum wc_map_op {
1321     WC_MAP_SUBMIT,
1322     WC_MAP_REMAP,
1323     WC_MAP_REMAP_ORIGIN,
1324     WC_MAP_RETURN,
1325     WC_MAP_ERROR,
1326 };
1327 
1328 static void writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio,
1329                     struct wc_entry *e)
1330 {
1331     if (e) {
1332         sector_t next_boundary =
1333             read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1334         if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT)
1335             dm_accept_partial_bio(bio, next_boundary);
1336     }
1337 }
1338 
1339 static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio)
1340 {
1341     enum wc_map_op map_op;
1342     struct wc_entry *e;
1343 
1344 read_next_block:
1345     wc->stats.reads++;
1346     e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1347     if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1348         wc->stats.read_hits++;
1349         if (WC_MODE_PMEM(wc)) {
1350             bio_copy_block(wc, bio, memory_data(wc, e));
1351             if (bio->bi_iter.bi_size)
1352                 goto read_next_block;
1353             map_op = WC_MAP_SUBMIT;
1354         } else {
1355             dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1356             bio_set_dev(bio, wc->ssd_dev->bdev);
1357             bio->bi_iter.bi_sector = cache_sector(wc, e);
1358             if (!writecache_entry_is_committed(wc, e))
1359                 writecache_wait_for_ios(wc, WRITE);
1360             map_op = WC_MAP_REMAP;
1361         }
1362     } else {
1363         writecache_map_remap_origin(wc, bio, e);
1364         wc->stats.reads += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits;
1365         map_op = WC_MAP_REMAP_ORIGIN;
1366     }
1367 
1368     return map_op;
1369 }
1370 
1371 static void writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio,
1372                     struct wc_entry *e, bool search_used)
1373 {
1374     unsigned bio_size = wc->block_size;
1375     sector_t start_cache_sec = cache_sector(wc, e);
1376     sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1377 
1378     while (bio_size < bio->bi_iter.bi_size) {
1379         if (!search_used) {
1380             struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1381             if (!f)
1382                 break;
1383             write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1384                             (bio_size >> SECTOR_SHIFT), wc->seq_count);
1385             writecache_insert_entry(wc, f);
1386             wc->uncommitted_blocks++;
1387         } else {
1388             struct wc_entry *f;
1389             struct rb_node *next = rb_next(&e->rb_node);
1390             if (!next)
1391                 break;
1392             f = container_of(next, struct wc_entry, rb_node);
1393             if (f != e + 1)
1394                 break;
1395             if (read_original_sector(wc, f) !=
1396                 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1397                 break;
1398             if (unlikely(f->write_in_progress))
1399                 break;
1400             if (writecache_entry_is_committed(wc, f))
1401                 wc->overwrote_committed = true;
1402             e = f;
1403         }
1404         bio_size += wc->block_size;
1405         current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1406     }
1407 
1408     bio_set_dev(bio, wc->ssd_dev->bdev);
1409     bio->bi_iter.bi_sector = start_cache_sec;
1410     dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1411 
1412     wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1413     wc->stats.writes_allocate += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits;
1414 
1415     if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1416         wc->uncommitted_blocks = 0;
1417         queue_work(wc->writeback_wq, &wc->flush_work);
1418     } else {
1419         writecache_schedule_autocommit(wc);
1420     }
1421 }
1422 
1423 static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio)
1424 {
1425     struct wc_entry *e;
1426 
1427     do {
1428         bool found_entry = false;
1429         bool search_used = false;
1430         if (writecache_has_error(wc)) {
1431             wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1432             return WC_MAP_ERROR;
1433         }
1434         e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1435         if (e) {
1436             if (!writecache_entry_is_committed(wc, e)) {
1437                 wc->stats.write_hits_uncommitted++;
1438                 search_used = true;
1439                 goto bio_copy;
1440             }
1441             wc->stats.write_hits_committed++;
1442             if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1443                 wc->overwrote_committed = true;
1444                 search_used = true;
1445                 goto bio_copy;
1446             }
1447             found_entry = true;
1448         } else {
1449             if (unlikely(wc->cleaner) ||
1450                 (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1451                 goto direct_write;
1452         }
1453         e = writecache_pop_from_freelist(wc, (sector_t)-1);
1454         if (unlikely(!e)) {
1455             if (!WC_MODE_PMEM(wc) && !found_entry) {
1456 direct_write:
1457                 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1458                 writecache_map_remap_origin(wc, bio, e);
1459                 wc->stats.writes_around += bio->bi_iter.bi_size >> wc->block_size_bits;
1460                 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1461                 return WC_MAP_REMAP_ORIGIN;
1462             }
1463             wc->stats.writes_blocked_on_freelist++;
1464             writecache_wait_on_freelist(wc);
1465             continue;
1466         }
1467         write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1468         writecache_insert_entry(wc, e);
1469         wc->uncommitted_blocks++;
1470         wc->stats.writes_allocate++;
1471 bio_copy:
1472         if (WC_MODE_PMEM(wc)) {
1473             bio_copy_block(wc, bio, memory_data(wc, e));
1474             wc->stats.writes++;
1475         } else {
1476             writecache_bio_copy_ssd(wc, bio, e, search_used);
1477             return WC_MAP_REMAP;
1478         }
1479     } while (bio->bi_iter.bi_size);
1480 
1481     if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks))
1482         writecache_flush(wc);
1483     else
1484         writecache_schedule_autocommit(wc);
1485 
1486     return WC_MAP_SUBMIT;
1487 }
1488 
1489 static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio)
1490 {
1491     if (writecache_has_error(wc))
1492         return WC_MAP_ERROR;
1493 
1494     if (WC_MODE_PMEM(wc)) {
1495         wc->stats.flushes++;
1496         writecache_flush(wc);
1497         if (writecache_has_error(wc))
1498             return WC_MAP_ERROR;
1499         else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
1500             return WC_MAP_REMAP_ORIGIN;
1501         return WC_MAP_SUBMIT;
1502     }
1503     /* SSD: */
1504     if (dm_bio_get_target_bio_nr(bio))
1505         return WC_MAP_REMAP_ORIGIN;
1506     wc->stats.flushes++;
1507     writecache_offload_bio(wc, bio);
1508     return WC_MAP_RETURN;
1509 }
1510 
1511 static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio)
1512 {
1513     wc->stats.discards += bio->bi_iter.bi_size >> wc->block_size_bits;
1514 
1515     if (writecache_has_error(wc))
1516         return WC_MAP_ERROR;
1517 
1518     if (WC_MODE_PMEM(wc)) {
1519         writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1520         return WC_MAP_REMAP_ORIGIN;
1521     }
1522     /* SSD: */
1523     writecache_offload_bio(wc, bio);
1524     return WC_MAP_RETURN;
1525 }
1526 
1527 static int writecache_map(struct dm_target *ti, struct bio *bio)
1528 {
1529     struct dm_writecache *wc = ti->private;
1530     enum wc_map_op map_op;
1531 
1532     bio->bi_private = NULL;
1533 
1534     wc_lock(wc);
1535 
1536     if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1537         map_op = writecache_map_flush(wc, bio);
1538         goto done;
1539     }
1540 
1541     bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1542 
1543     if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1544                 (wc->block_size / 512 - 1)) != 0)) {
1545         DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1546               (unsigned long long)bio->bi_iter.bi_sector,
1547               bio->bi_iter.bi_size, wc->block_size);
1548         map_op = WC_MAP_ERROR;
1549         goto done;
1550     }
1551 
1552     if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1553         map_op = writecache_map_discard(wc, bio);
1554         goto done;
1555     }
1556 
1557     if (bio_data_dir(bio) == READ)
1558         map_op = writecache_map_read(wc, bio);
1559     else
1560         map_op = writecache_map_write(wc, bio);
1561 done:
1562     switch (map_op) {
1563     case WC_MAP_REMAP_ORIGIN:
1564         if (likely(wc->pause != 0)) {
1565             if (bio_op(bio) == REQ_OP_WRITE) {
1566                 dm_iot_io_begin(&wc->iot, 1);
1567                 bio->bi_private = (void *)2;
1568             }
1569         }
1570         bio_set_dev(bio, wc->dev->bdev);
1571         wc_unlock(wc);
1572         return DM_MAPIO_REMAPPED;
1573 
1574     case WC_MAP_REMAP:
1575         /* make sure that writecache_end_io decrements bio_in_progress: */
1576         bio->bi_private = (void *)1;
1577         atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1578         wc_unlock(wc);
1579         return DM_MAPIO_REMAPPED;
1580 
1581     case WC_MAP_SUBMIT:
1582         wc_unlock(wc);
1583         bio_endio(bio);
1584         return DM_MAPIO_SUBMITTED;
1585 
1586     case WC_MAP_RETURN:
1587         wc_unlock(wc);
1588         return DM_MAPIO_SUBMITTED;
1589 
1590     case WC_MAP_ERROR:
1591         wc_unlock(wc);
1592         bio_io_error(bio);
1593         return DM_MAPIO_SUBMITTED;
1594 
1595     default:
1596         BUG();
1597         wc_unlock(wc);
1598         return DM_MAPIO_KILL;
1599     }
1600 }
1601 
1602 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1603 {
1604     struct dm_writecache *wc = ti->private;
1605 
1606     if (bio->bi_private == (void *)1) {
1607         int dir = bio_data_dir(bio);
1608         if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1609             if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1610                 wake_up(&wc->bio_in_progress_wait[dir]);
1611     } else if (bio->bi_private == (void *)2) {
1612         dm_iot_io_end(&wc->iot, 1);
1613     }
1614     return 0;
1615 }
1616 
1617 static int writecache_iterate_devices(struct dm_target *ti,
1618                       iterate_devices_callout_fn fn, void *data)
1619 {
1620     struct dm_writecache *wc = ti->private;
1621 
1622     return fn(ti, wc->dev, 0, ti->len, data);
1623 }
1624 
1625 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1626 {
1627     struct dm_writecache *wc = ti->private;
1628 
1629     if (limits->logical_block_size < wc->block_size)
1630         limits->logical_block_size = wc->block_size;
1631 
1632     if (limits->physical_block_size < wc->block_size)
1633         limits->physical_block_size = wc->block_size;
1634 
1635     if (limits->io_min < wc->block_size)
1636         limits->io_min = wc->block_size;
1637 }
1638 
1639 
1640 static void writecache_writeback_endio(struct bio *bio)
1641 {
1642     struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1643     struct dm_writecache *wc = wb->wc;
1644     unsigned long flags;
1645 
1646     raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1647     if (unlikely(list_empty(&wc->endio_list)))
1648         wake_up_process(wc->endio_thread);
1649     list_add_tail(&wb->endio_entry, &wc->endio_list);
1650     raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1651 }
1652 
1653 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1654 {
1655     struct copy_struct *c = ptr;
1656     struct dm_writecache *wc = c->wc;
1657 
1658     c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1659 
1660     raw_spin_lock_irq(&wc->endio_list_lock);
1661     if (unlikely(list_empty(&wc->endio_list)))
1662         wake_up_process(wc->endio_thread);
1663     list_add_tail(&c->endio_entry, &wc->endio_list);
1664     raw_spin_unlock_irq(&wc->endio_list_lock);
1665 }
1666 
1667 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1668 {
1669     unsigned i;
1670     struct writeback_struct *wb;
1671     struct wc_entry *e;
1672     unsigned long n_walked = 0;
1673 
1674     do {
1675         wb = list_entry(list->next, struct writeback_struct, endio_entry);
1676         list_del(&wb->endio_entry);
1677 
1678         if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1679             writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1680                     "write error %d", wb->bio.bi_status);
1681         i = 0;
1682         do {
1683             e = wb->wc_list[i];
1684             BUG_ON(!e->write_in_progress);
1685             e->write_in_progress = false;
1686             INIT_LIST_HEAD(&e->lru);
1687             if (!writecache_has_error(wc))
1688                 writecache_free_entry(wc, e);
1689             BUG_ON(!wc->writeback_size);
1690             wc->writeback_size--;
1691             n_walked++;
1692             if (unlikely(n_walked >= ENDIO_LATENCY)) {
1693                 writecache_commit_flushed(wc, false);
1694                 wc_unlock(wc);
1695                 wc_lock(wc);
1696                 n_walked = 0;
1697             }
1698         } while (++i < wb->wc_list_n);
1699 
1700         if (wb->wc_list != wb->wc_list_inline)
1701             kfree(wb->wc_list);
1702         bio_put(&wb->bio);
1703     } while (!list_empty(list));
1704 }
1705 
1706 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1707 {
1708     struct copy_struct *c;
1709     struct wc_entry *e;
1710 
1711     do {
1712         c = list_entry(list->next, struct copy_struct, endio_entry);
1713         list_del(&c->endio_entry);
1714 
1715         if (unlikely(c->error))
1716             writecache_error(wc, c->error, "copy error");
1717 
1718         e = c->e;
1719         do {
1720             BUG_ON(!e->write_in_progress);
1721             e->write_in_progress = false;
1722             INIT_LIST_HEAD(&e->lru);
1723             if (!writecache_has_error(wc))
1724                 writecache_free_entry(wc, e);
1725 
1726             BUG_ON(!wc->writeback_size);
1727             wc->writeback_size--;
1728             e++;
1729         } while (--c->n_entries);
1730         mempool_free(c, &wc->copy_pool);
1731     } while (!list_empty(list));
1732 }
1733 
1734 static int writecache_endio_thread(void *data)
1735 {
1736     struct dm_writecache *wc = data;
1737 
1738     while (1) {
1739         struct list_head list;
1740 
1741         raw_spin_lock_irq(&wc->endio_list_lock);
1742         if (!list_empty(&wc->endio_list))
1743             goto pop_from_list;
1744         set_current_state(TASK_INTERRUPTIBLE);
1745         raw_spin_unlock_irq(&wc->endio_list_lock);
1746 
1747         if (unlikely(kthread_should_stop())) {
1748             set_current_state(TASK_RUNNING);
1749             break;
1750         }
1751 
1752         schedule();
1753 
1754         continue;
1755 
1756 pop_from_list:
1757         list = wc->endio_list;
1758         list.next->prev = list.prev->next = &list;
1759         INIT_LIST_HEAD(&wc->endio_list);
1760         raw_spin_unlock_irq(&wc->endio_list_lock);
1761 
1762         if (!WC_MODE_FUA(wc))
1763             writecache_disk_flush(wc, wc->dev);
1764 
1765         wc_lock(wc);
1766 
1767         if (WC_MODE_PMEM(wc)) {
1768             __writecache_endio_pmem(wc, &list);
1769         } else {
1770             __writecache_endio_ssd(wc, &list);
1771             writecache_wait_for_ios(wc, READ);
1772         }
1773 
1774         writecache_commit_flushed(wc, false);
1775 
1776         wc_unlock(wc);
1777     }
1778 
1779     return 0;
1780 }
1781 
1782 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
1783 {
1784     struct dm_writecache *wc = wb->wc;
1785     unsigned block_size = wc->block_size;
1786     void *address = memory_data(wc, e);
1787 
1788     persistent_memory_flush_cache(address, block_size);
1789 
1790     if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
1791         return true;
1792 
1793     return bio_add_page(&wb->bio, persistent_memory_page(address),
1794                 block_size, persistent_memory_page_offset(address)) != 0;
1795 }
1796 
1797 struct writeback_list {
1798     struct list_head list;
1799     size_t size;
1800 };
1801 
1802 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1803 {
1804     if (unlikely(wc->max_writeback_jobs)) {
1805         if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1806             wc_lock(wc);
1807             while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1808                 writecache_wait_on_freelist(wc);
1809             wc_unlock(wc);
1810         }
1811     }
1812     cond_resched();
1813 }
1814 
1815 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1816 {
1817     struct wc_entry *e, *f;
1818     struct bio *bio;
1819     struct writeback_struct *wb;
1820     unsigned max_pages;
1821 
1822     while (wbl->size) {
1823         wbl->size--;
1824         e = container_of(wbl->list.prev, struct wc_entry, lru);
1825         list_del(&e->lru);
1826 
1827         max_pages = e->wc_list_contiguous;
1828 
1829         bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE,
1830                        GFP_NOIO, &wc->bio_set);
1831         wb = container_of(bio, struct writeback_struct, bio);
1832         wb->wc = wc;
1833         bio->bi_end_io = writecache_writeback_endio;
1834         bio->bi_iter.bi_sector = read_original_sector(wc, e);
1835         if (max_pages <= WB_LIST_INLINE ||
1836             unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1837                                GFP_NOIO | __GFP_NORETRY |
1838                                __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1839             wb->wc_list = wb->wc_list_inline;
1840             max_pages = WB_LIST_INLINE;
1841         }
1842 
1843         BUG_ON(!wc_add_block(wb, e));
1844 
1845         wb->wc_list[0] = e;
1846         wb->wc_list_n = 1;
1847 
1848         while (wbl->size && wb->wc_list_n < max_pages) {
1849             f = container_of(wbl->list.prev, struct wc_entry, lru);
1850             if (read_original_sector(wc, f) !=
1851                 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1852                 break;
1853             if (!wc_add_block(wb, f))
1854                 break;
1855             wbl->size--;
1856             list_del(&f->lru);
1857             wb->wc_list[wb->wc_list_n++] = f;
1858             e = f;
1859         }
1860         if (WC_MODE_FUA(wc))
1861             bio->bi_opf |= REQ_FUA;
1862         if (writecache_has_error(wc)) {
1863             bio->bi_status = BLK_STS_IOERR;
1864             bio_endio(bio);
1865         } else if (unlikely(!bio_sectors(bio))) {
1866             bio->bi_status = BLK_STS_OK;
1867             bio_endio(bio);
1868         } else {
1869             submit_bio(bio);
1870         }
1871 
1872         __writeback_throttle(wc, wbl);
1873     }
1874 }
1875 
1876 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1877 {
1878     struct wc_entry *e, *f;
1879     struct dm_io_region from, to;
1880     struct copy_struct *c;
1881 
1882     while (wbl->size) {
1883         unsigned n_sectors;
1884 
1885         wbl->size--;
1886         e = container_of(wbl->list.prev, struct wc_entry, lru);
1887         list_del(&e->lru);
1888 
1889         n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1890 
1891         from.bdev = wc->ssd_dev->bdev;
1892         from.sector = cache_sector(wc, e);
1893         from.count = n_sectors;
1894         to.bdev = wc->dev->bdev;
1895         to.sector = read_original_sector(wc, e);
1896         to.count = n_sectors;
1897 
1898         c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1899         c->wc = wc;
1900         c->e = e;
1901         c->n_entries = e->wc_list_contiguous;
1902 
1903         while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1904             wbl->size--;
1905             f = container_of(wbl->list.prev, struct wc_entry, lru);
1906             BUG_ON(f != e + 1);
1907             list_del(&f->lru);
1908             e = f;
1909         }
1910 
1911         if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
1912             if (to.sector >= wc->data_device_sectors) {
1913                 writecache_copy_endio(0, 0, c);
1914                 continue;
1915             }
1916             from.count = to.count = wc->data_device_sectors - to.sector;
1917         }
1918 
1919         dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1920 
1921         __writeback_throttle(wc, wbl);
1922     }
1923 }
1924 
1925 static void writecache_writeback(struct work_struct *work)
1926 {
1927     struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1928     struct blk_plug plug;
1929     struct wc_entry *f, *g, *e = NULL;
1930     struct rb_node *node, *next_node;
1931     struct list_head skipped;
1932     struct writeback_list wbl;
1933     unsigned long n_walked;
1934 
1935     if (!WC_MODE_PMEM(wc)) {
1936         /* Wait for any active kcopyd work on behalf of ssd writeback */
1937         dm_kcopyd_client_flush(wc->dm_kcopyd);
1938     }
1939 
1940     if (likely(wc->pause != 0)) {
1941         while (1) {
1942             unsigned long idle;
1943             if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
1944                 unlikely(dm_suspended(wc->ti)))
1945                 break;
1946             idle = dm_iot_idle_time(&wc->iot);
1947             if (idle >= wc->pause)
1948                 break;
1949             idle = wc->pause - idle;
1950             if (idle > HZ)
1951                 idle = HZ;
1952             schedule_timeout_idle(idle);
1953         }
1954     }
1955 
1956     wc_lock(wc);
1957 restart:
1958     if (writecache_has_error(wc)) {
1959         wc_unlock(wc);
1960         return;
1961     }
1962 
1963     if (unlikely(wc->writeback_all)) {
1964         if (writecache_wait_for_writeback(wc))
1965             goto restart;
1966     }
1967 
1968     if (wc->overwrote_committed) {
1969         writecache_wait_for_ios(wc, WRITE);
1970     }
1971 
1972     n_walked = 0;
1973     INIT_LIST_HEAD(&skipped);
1974     INIT_LIST_HEAD(&wbl.list);
1975     wbl.size = 0;
1976     while (!list_empty(&wc->lru) &&
1977            (wc->writeback_all ||
1978         wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1979         (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1980          wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1981 
1982         n_walked++;
1983         if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1984             likely(!wc->writeback_all)) {
1985             if (likely(!dm_suspended(wc->ti)))
1986                 queue_work(wc->writeback_wq, &wc->writeback_work);
1987             break;
1988         }
1989 
1990         if (unlikely(wc->writeback_all)) {
1991             if (unlikely(!e)) {
1992                 writecache_flush(wc);
1993                 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1994             } else
1995                 e = g;
1996         } else
1997             e = container_of(wc->lru.prev, struct wc_entry, lru);
1998         BUG_ON(e->write_in_progress);
1999         if (unlikely(!writecache_entry_is_committed(wc, e))) {
2000             writecache_flush(wc);
2001         }
2002         node = rb_prev(&e->rb_node);
2003         if (node) {
2004             f = container_of(node, struct wc_entry, rb_node);
2005             if (unlikely(read_original_sector(wc, f) ==
2006                      read_original_sector(wc, e))) {
2007                 BUG_ON(!f->write_in_progress);
2008                 list_move(&e->lru, &skipped);
2009                 cond_resched();
2010                 continue;
2011             }
2012         }
2013         wc->writeback_size++;
2014         list_move(&e->lru, &wbl.list);
2015         wbl.size++;
2016         e->write_in_progress = true;
2017         e->wc_list_contiguous = 1;
2018 
2019         f = e;
2020 
2021         while (1) {
2022             next_node = rb_next(&f->rb_node);
2023             if (unlikely(!next_node))
2024                 break;
2025             g = container_of(next_node, struct wc_entry, rb_node);
2026             if (unlikely(read_original_sector(wc, g) ==
2027                 read_original_sector(wc, f))) {
2028                 f = g;
2029                 continue;
2030             }
2031             if (read_original_sector(wc, g) !=
2032                 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
2033                 break;
2034             if (unlikely(g->write_in_progress))
2035                 break;
2036             if (unlikely(!writecache_entry_is_committed(wc, g)))
2037                 break;
2038 
2039             if (!WC_MODE_PMEM(wc)) {
2040                 if (g != f + 1)
2041                     break;
2042             }
2043 
2044             n_walked++;
2045             //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
2046             //  break;
2047 
2048             wc->writeback_size++;
2049             list_move(&g->lru, &wbl.list);
2050             wbl.size++;
2051             g->write_in_progress = true;
2052             g->wc_list_contiguous = BIO_MAX_VECS;
2053             f = g;
2054             e->wc_list_contiguous++;
2055             if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
2056                 if (unlikely(wc->writeback_all)) {
2057                     next_node = rb_next(&f->rb_node);
2058                     if (likely(next_node))
2059                         g = container_of(next_node, struct wc_entry, rb_node);
2060                 }
2061                 break;
2062             }
2063         }
2064         cond_resched();
2065     }
2066 
2067     if (!list_empty(&skipped)) {
2068         list_splice_tail(&skipped, &wc->lru);
2069         /*
2070          * If we didn't do any progress, we must wait until some
2071          * writeback finishes to avoid burning CPU in a loop
2072          */
2073         if (unlikely(!wbl.size))
2074             writecache_wait_for_writeback(wc);
2075     }
2076 
2077     wc_unlock(wc);
2078 
2079     blk_start_plug(&plug);
2080 
2081     if (WC_MODE_PMEM(wc))
2082         __writecache_writeback_pmem(wc, &wbl);
2083     else
2084         __writecache_writeback_ssd(wc, &wbl);
2085 
2086     blk_finish_plug(&plug);
2087 
2088     if (unlikely(wc->writeback_all)) {
2089         wc_lock(wc);
2090         while (writecache_wait_for_writeback(wc));
2091         wc_unlock(wc);
2092     }
2093 }
2094 
2095 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
2096                  size_t *n_blocks_p, size_t *n_metadata_blocks_p)
2097 {
2098     uint64_t n_blocks, offset;
2099     struct wc_entry e;
2100 
2101     n_blocks = device_size;
2102     do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
2103 
2104     while (1) {
2105         if (!n_blocks)
2106             return -ENOSPC;
2107         /* Verify the following entries[n_blocks] won't overflow */
2108         if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
2109                  sizeof(struct wc_memory_entry)))
2110             return -EFBIG;
2111         offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
2112         offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
2113         if (offset + n_blocks * block_size <= device_size)
2114             break;
2115         n_blocks--;
2116     }
2117 
2118     /* check if the bit field overflows */
2119     e.index = n_blocks;
2120     if (e.index != n_blocks)
2121         return -EFBIG;
2122 
2123     if (n_blocks_p)
2124         *n_blocks_p = n_blocks;
2125     if (n_metadata_blocks_p)
2126         *n_metadata_blocks_p = offset >> __ffs(block_size);
2127     return 0;
2128 }
2129 
2130 static int init_memory(struct dm_writecache *wc)
2131 {
2132     size_t b;
2133     int r;
2134 
2135     r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
2136     if (r)
2137         return r;
2138 
2139     r = writecache_alloc_entries(wc);
2140     if (r)
2141         return r;
2142 
2143     for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
2144         pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
2145     pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
2146     pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
2147     pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
2148     pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
2149 
2150     for (b = 0; b < wc->n_blocks; b++) {
2151         write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
2152         cond_resched();
2153     }
2154 
2155     writecache_flush_all_metadata(wc);
2156     writecache_commit_flushed(wc, false);
2157     pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
2158     writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2159     writecache_commit_flushed(wc, false);
2160 
2161     return 0;
2162 }
2163 
2164 static void writecache_dtr(struct dm_target *ti)
2165 {
2166     struct dm_writecache *wc = ti->private;
2167 
2168     if (!wc)
2169         return;
2170 
2171     if (wc->endio_thread)
2172         kthread_stop(wc->endio_thread);
2173 
2174     if (wc->flush_thread)
2175         kthread_stop(wc->flush_thread);
2176 
2177     bioset_exit(&wc->bio_set);
2178 
2179     mempool_exit(&wc->copy_pool);
2180 
2181     if (wc->writeback_wq)
2182         destroy_workqueue(wc->writeback_wq);
2183 
2184     if (wc->dev)
2185         dm_put_device(ti, wc->dev);
2186 
2187     if (wc->ssd_dev)
2188         dm_put_device(ti, wc->ssd_dev);
2189 
2190     vfree(wc->entries);
2191 
2192     if (wc->memory_map) {
2193         if (WC_MODE_PMEM(wc))
2194             persistent_memory_release(wc);
2195         else
2196             vfree(wc->memory_map);
2197     }
2198 
2199     if (wc->dm_kcopyd)
2200         dm_kcopyd_client_destroy(wc->dm_kcopyd);
2201 
2202     if (wc->dm_io)
2203         dm_io_client_destroy(wc->dm_io);
2204 
2205     vfree(wc->dirty_bitmap);
2206 
2207     kfree(wc);
2208 }
2209 
2210 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2211 {
2212     struct dm_writecache *wc;
2213     struct dm_arg_set as;
2214     const char *string;
2215     unsigned opt_params;
2216     size_t offset, data_size;
2217     int i, r;
2218     char dummy;
2219     int high_wm_percent = HIGH_WATERMARK;
2220     int low_wm_percent = LOW_WATERMARK;
2221     uint64_t x;
2222     struct wc_memory_superblock s;
2223 
2224     static struct dm_arg _args[] = {
2225         {0, 18, "Invalid number of feature args"},
2226     };
2227 
2228     as.argc = argc;
2229     as.argv = argv;
2230 
2231     wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2232     if (!wc) {
2233         ti->error = "Cannot allocate writecache structure";
2234         r = -ENOMEM;
2235         goto bad;
2236     }
2237     ti->private = wc;
2238     wc->ti = ti;
2239 
2240     mutex_init(&wc->lock);
2241     wc->max_age = MAX_AGE_UNSPECIFIED;
2242     writecache_poison_lists(wc);
2243     init_waitqueue_head(&wc->freelist_wait);
2244     timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2245     timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2246 
2247     for (i = 0; i < 2; i++) {
2248         atomic_set(&wc->bio_in_progress[i], 0);
2249         init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2250     }
2251 
2252     wc->dm_io = dm_io_client_create();
2253     if (IS_ERR(wc->dm_io)) {
2254         r = PTR_ERR(wc->dm_io);
2255         ti->error = "Unable to allocate dm-io client";
2256         wc->dm_io = NULL;
2257         goto bad;
2258     }
2259 
2260     wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2261     if (!wc->writeback_wq) {
2262         r = -ENOMEM;
2263         ti->error = "Could not allocate writeback workqueue";
2264         goto bad;
2265     }
2266     INIT_WORK(&wc->writeback_work, writecache_writeback);
2267     INIT_WORK(&wc->flush_work, writecache_flush_work);
2268 
2269     dm_iot_init(&wc->iot);
2270 
2271     raw_spin_lock_init(&wc->endio_list_lock);
2272     INIT_LIST_HEAD(&wc->endio_list);
2273     wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio");
2274     if (IS_ERR(wc->endio_thread)) {
2275         r = PTR_ERR(wc->endio_thread);
2276         wc->endio_thread = NULL;
2277         ti->error = "Couldn't spawn endio thread";
2278         goto bad;
2279     }
2280 
2281     /*
2282      * Parse the mode (pmem or ssd)
2283      */
2284     string = dm_shift_arg(&as);
2285     if (!string)
2286         goto bad_arguments;
2287 
2288     if (!strcasecmp(string, "s")) {
2289         wc->pmem_mode = false;
2290     } else if (!strcasecmp(string, "p")) {
2291 #ifdef DM_WRITECACHE_HAS_PMEM
2292         wc->pmem_mode = true;
2293         wc->writeback_fua = true;
2294 #else
2295         /*
2296          * If the architecture doesn't support persistent memory or
2297          * the kernel doesn't support any DAX drivers, this driver can
2298          * only be used in SSD-only mode.
2299          */
2300         r = -EOPNOTSUPP;
2301         ti->error = "Persistent memory or DAX not supported on this system";
2302         goto bad;
2303 #endif
2304     } else {
2305         goto bad_arguments;
2306     }
2307 
2308     if (WC_MODE_PMEM(wc)) {
2309         r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2310                 offsetof(struct writeback_struct, bio),
2311                 BIOSET_NEED_BVECS);
2312         if (r) {
2313             ti->error = "Could not allocate bio set";
2314             goto bad;
2315         }
2316     } else {
2317         wc->pause = PAUSE_WRITEBACK;
2318         r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2319         if (r) {
2320             ti->error = "Could not allocate mempool";
2321             goto bad;
2322         }
2323     }
2324 
2325     /*
2326      * Parse the origin data device
2327      */
2328     string = dm_shift_arg(&as);
2329     if (!string)
2330         goto bad_arguments;
2331     r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2332     if (r) {
2333         ti->error = "Origin data device lookup failed";
2334         goto bad;
2335     }
2336 
2337     /*
2338      * Parse cache data device (be it pmem or ssd)
2339      */
2340     string = dm_shift_arg(&as);
2341     if (!string)
2342         goto bad_arguments;
2343 
2344     r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2345     if (r) {
2346         ti->error = "Cache data device lookup failed";
2347         goto bad;
2348     }
2349     wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev);
2350 
2351     /*
2352      * Parse the cache block size
2353      */
2354     string = dm_shift_arg(&as);
2355     if (!string)
2356         goto bad_arguments;
2357     if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2358         wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2359         (wc->block_size & (wc->block_size - 1))) {
2360         r = -EINVAL;
2361         ti->error = "Invalid block size";
2362         goto bad;
2363     }
2364     if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2365         wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2366         r = -EINVAL;
2367         ti->error = "Block size is smaller than device logical block size";
2368         goto bad;
2369     }
2370     wc->block_size_bits = __ffs(wc->block_size);
2371 
2372     wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2373     wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2374     wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2375 
2376     /*
2377      * Parse optional arguments
2378      */
2379     r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2380     if (r)
2381         goto bad;
2382 
2383     while (opt_params) {
2384         string = dm_shift_arg(&as), opt_params--;
2385         if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2386             unsigned long long start_sector;
2387             string = dm_shift_arg(&as), opt_params--;
2388             if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2389                 goto invalid_optional;
2390             wc->start_sector = start_sector;
2391             wc->start_sector_set = true;
2392             if (wc->start_sector != start_sector ||
2393                 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2394                 goto invalid_optional;
2395         } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2396             string = dm_shift_arg(&as), opt_params--;
2397             if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2398                 goto invalid_optional;
2399             if (high_wm_percent < 0 || high_wm_percent > 100)
2400                 goto invalid_optional;
2401             wc->high_wm_percent_value = high_wm_percent;
2402             wc->high_wm_percent_set = true;
2403         } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2404             string = dm_shift_arg(&as), opt_params--;
2405             if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2406                 goto invalid_optional;
2407             if (low_wm_percent < 0 || low_wm_percent > 100)
2408                 goto invalid_optional;
2409             wc->low_wm_percent_value = low_wm_percent;
2410             wc->low_wm_percent_set = true;
2411         } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2412             string = dm_shift_arg(&as), opt_params--;
2413             if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2414                 goto invalid_optional;
2415             wc->max_writeback_jobs_set = true;
2416         } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2417             string = dm_shift_arg(&as), opt_params--;
2418             if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2419                 goto invalid_optional;
2420             wc->autocommit_blocks_set = true;
2421         } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2422             unsigned autocommit_msecs;
2423             string = dm_shift_arg(&as), opt_params--;
2424             if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2425                 goto invalid_optional;
2426             if (autocommit_msecs > 3600000)
2427                 goto invalid_optional;
2428             wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2429             wc->autocommit_time_value = autocommit_msecs;
2430             wc->autocommit_time_set = true;
2431         } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2432             unsigned max_age_msecs;
2433             string = dm_shift_arg(&as), opt_params--;
2434             if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2435                 goto invalid_optional;
2436             if (max_age_msecs > 86400000)
2437                 goto invalid_optional;
2438             wc->max_age = msecs_to_jiffies(max_age_msecs);
2439             wc->max_age_set = true;
2440             wc->max_age_value = max_age_msecs;
2441         } else if (!strcasecmp(string, "cleaner")) {
2442             wc->cleaner_set = true;
2443             wc->cleaner = true;
2444         } else if (!strcasecmp(string, "fua")) {
2445             if (WC_MODE_PMEM(wc)) {
2446                 wc->writeback_fua = true;
2447                 wc->writeback_fua_set = true;
2448             } else goto invalid_optional;
2449         } else if (!strcasecmp(string, "nofua")) {
2450             if (WC_MODE_PMEM(wc)) {
2451                 wc->writeback_fua = false;
2452                 wc->writeback_fua_set = true;
2453             } else goto invalid_optional;
2454         } else if (!strcasecmp(string, "metadata_only")) {
2455             wc->metadata_only = true;
2456         } else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
2457             unsigned pause_msecs;
2458             if (WC_MODE_PMEM(wc))
2459                 goto invalid_optional;
2460             string = dm_shift_arg(&as), opt_params--;
2461             if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
2462                 goto invalid_optional;
2463             if (pause_msecs > 60000)
2464                 goto invalid_optional;
2465             wc->pause = msecs_to_jiffies(pause_msecs);
2466             wc->pause_set = true;
2467             wc->pause_value = pause_msecs;
2468         } else {
2469 invalid_optional:
2470             r = -EINVAL;
2471             ti->error = "Invalid optional argument";
2472             goto bad;
2473         }
2474     }
2475 
2476     if (high_wm_percent < low_wm_percent) {
2477         r = -EINVAL;
2478         ti->error = "High watermark must be greater than or equal to low watermark";
2479         goto bad;
2480     }
2481 
2482     if (WC_MODE_PMEM(wc)) {
2483         if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2484             r = -EOPNOTSUPP;
2485             ti->error = "Asynchronous persistent memory not supported as pmem cache";
2486             goto bad;
2487         }
2488 
2489         r = persistent_memory_claim(wc);
2490         if (r) {
2491             ti->error = "Unable to map persistent memory for cache";
2492             goto bad;
2493         }
2494     } else {
2495         size_t n_blocks, n_metadata_blocks;
2496         uint64_t n_bitmap_bits;
2497 
2498         wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2499 
2500         bio_list_init(&wc->flush_list);
2501         wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush");
2502         if (IS_ERR(wc->flush_thread)) {
2503             r = PTR_ERR(wc->flush_thread);
2504             wc->flush_thread = NULL;
2505             ti->error = "Couldn't spawn flush thread";
2506             goto bad;
2507         }
2508 
2509         r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2510                       &n_blocks, &n_metadata_blocks);
2511         if (r) {
2512             ti->error = "Invalid device size";
2513             goto bad;
2514         }
2515 
2516         n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2517                  BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2518         /* this is limitation of test_bit functions */
2519         if (n_bitmap_bits > 1U << 31) {
2520             r = -EFBIG;
2521             ti->error = "Invalid device size";
2522             goto bad;
2523         }
2524 
2525         wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2526         if (!wc->memory_map) {
2527             r = -ENOMEM;
2528             ti->error = "Unable to allocate memory for metadata";
2529             goto bad;
2530         }
2531 
2532         wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2533         if (IS_ERR(wc->dm_kcopyd)) {
2534             r = PTR_ERR(wc->dm_kcopyd);
2535             ti->error = "Unable to allocate dm-kcopyd client";
2536             wc->dm_kcopyd = NULL;
2537             goto bad;
2538         }
2539 
2540         wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2541         wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2542             BITS_PER_LONG * sizeof(unsigned long);
2543         wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2544         if (!wc->dirty_bitmap) {
2545             r = -ENOMEM;
2546             ti->error = "Unable to allocate dirty bitmap";
2547             goto bad;
2548         }
2549 
2550         r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2551         if (r) {
2552             ti->error = "Unable to read first block of metadata";
2553             goto bad;
2554         }
2555     }
2556 
2557     r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
2558     if (r) {
2559         ti->error = "Hardware memory error when reading superblock";
2560         goto bad;
2561     }
2562     if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2563         r = init_memory(wc);
2564         if (r) {
2565             ti->error = "Unable to initialize device";
2566             goto bad;
2567         }
2568         r = copy_mc_to_kernel(&s, sb(wc),
2569                       sizeof(struct wc_memory_superblock));
2570         if (r) {
2571             ti->error = "Hardware memory error when reading superblock";
2572             goto bad;
2573         }
2574     }
2575 
2576     if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2577         ti->error = "Invalid magic in the superblock";
2578         r = -EINVAL;
2579         goto bad;
2580     }
2581 
2582     if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2583         ti->error = "Invalid version in the superblock";
2584         r = -EINVAL;
2585         goto bad;
2586     }
2587 
2588     if (le32_to_cpu(s.block_size) != wc->block_size) {
2589         ti->error = "Block size does not match superblock";
2590         r = -EINVAL;
2591         goto bad;
2592     }
2593 
2594     wc->n_blocks = le64_to_cpu(s.n_blocks);
2595 
2596     offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2597     if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2598 overflow:
2599         ti->error = "Overflow in size calculation";
2600         r = -EINVAL;
2601         goto bad;
2602     }
2603     offset += sizeof(struct wc_memory_superblock);
2604     if (offset < sizeof(struct wc_memory_superblock))
2605         goto overflow;
2606     offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2607     data_size = wc->n_blocks * (size_t)wc->block_size;
2608     if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2609         (offset + data_size < offset))
2610         goto overflow;
2611     if (offset + data_size > wc->memory_map_size) {
2612         ti->error = "Memory area is too small";
2613         r = -EINVAL;
2614         goto bad;
2615     }
2616 
2617     wc->metadata_sectors = offset >> SECTOR_SHIFT;
2618     wc->block_start = (char *)sb(wc) + offset;
2619 
2620     x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2621     x += 50;
2622     do_div(x, 100);
2623     wc->freelist_high_watermark = x;
2624     x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2625     x += 50;
2626     do_div(x, 100);
2627     wc->freelist_low_watermark = x;
2628 
2629     if (wc->cleaner)
2630         activate_cleaner(wc);
2631 
2632     r = writecache_alloc_entries(wc);
2633     if (r) {
2634         ti->error = "Cannot allocate memory";
2635         goto bad;
2636     }
2637 
2638     ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
2639     ti->flush_supported = true;
2640     ti->num_discard_bios = 1;
2641 
2642     if (WC_MODE_PMEM(wc))
2643         persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2644 
2645     return 0;
2646 
2647 bad_arguments:
2648     r = -EINVAL;
2649     ti->error = "Bad arguments";
2650 bad:
2651     writecache_dtr(ti);
2652     return r;
2653 }
2654 
2655 static void writecache_status(struct dm_target *ti, status_type_t type,
2656                   unsigned status_flags, char *result, unsigned maxlen)
2657 {
2658     struct dm_writecache *wc = ti->private;
2659     unsigned extra_args;
2660     unsigned sz = 0;
2661 
2662     switch (type) {
2663     case STATUSTYPE_INFO:
2664         DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
2665                writecache_has_error(wc),
2666                (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2667                (unsigned long long)wc->writeback_size,
2668                wc->stats.reads,
2669                wc->stats.read_hits,
2670                wc->stats.writes,
2671                wc->stats.write_hits_uncommitted,
2672                wc->stats.write_hits_committed,
2673                wc->stats.writes_around,
2674                wc->stats.writes_allocate,
2675                wc->stats.writes_blocked_on_freelist,
2676                wc->stats.flushes,
2677                wc->stats.discards);
2678         break;
2679     case STATUSTYPE_TABLE:
2680         DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2681                 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2682         extra_args = 0;
2683         if (wc->start_sector_set)
2684             extra_args += 2;
2685         if (wc->high_wm_percent_set)
2686             extra_args += 2;
2687         if (wc->low_wm_percent_set)
2688             extra_args += 2;
2689         if (wc->max_writeback_jobs_set)
2690             extra_args += 2;
2691         if (wc->autocommit_blocks_set)
2692             extra_args += 2;
2693         if (wc->autocommit_time_set)
2694             extra_args += 2;
2695         if (wc->max_age_set)
2696             extra_args += 2;
2697         if (wc->cleaner_set)
2698             extra_args++;
2699         if (wc->writeback_fua_set)
2700             extra_args++;
2701         if (wc->metadata_only)
2702             extra_args++;
2703         if (wc->pause_set)
2704             extra_args += 2;
2705 
2706         DMEMIT("%u", extra_args);
2707         if (wc->start_sector_set)
2708             DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2709         if (wc->high_wm_percent_set)
2710             DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2711         if (wc->low_wm_percent_set)
2712             DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
2713         if (wc->max_writeback_jobs_set)
2714             DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2715         if (wc->autocommit_blocks_set)
2716             DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2717         if (wc->autocommit_time_set)
2718             DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2719         if (wc->max_age_set)
2720             DMEMIT(" max_age %u", wc->max_age_value);
2721         if (wc->cleaner_set)
2722             DMEMIT(" cleaner");
2723         if (wc->writeback_fua_set)
2724             DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2725         if (wc->metadata_only)
2726             DMEMIT(" metadata_only");
2727         if (wc->pause_set)
2728             DMEMIT(" pause_writeback %u", wc->pause_value);
2729         break;
2730     case STATUSTYPE_IMA:
2731         *result = '\0';
2732         break;
2733     }
2734 }
2735 
2736 static struct target_type writecache_target = {
2737     .name           = "writecache",
2738     .version        = {1, 6, 0},
2739     .module         = THIS_MODULE,
2740     .ctr            = writecache_ctr,
2741     .dtr            = writecache_dtr,
2742     .status         = writecache_status,
2743     .postsuspend        = writecache_suspend,
2744     .resume         = writecache_resume,
2745     .message        = writecache_message,
2746     .map            = writecache_map,
2747     .end_io         = writecache_end_io,
2748     .iterate_devices    = writecache_iterate_devices,
2749     .io_hints       = writecache_io_hints,
2750 };
2751 
2752 static int __init dm_writecache_init(void)
2753 {
2754     int r;
2755 
2756     r = dm_register_target(&writecache_target);
2757     if (r < 0) {
2758         DMERR("register failed %d", r);
2759         return r;
2760     }
2761 
2762     return 0;
2763 }
2764 
2765 static void __exit dm_writecache_exit(void)
2766 {
2767     dm_unregister_target(&writecache_target);
2768 }
2769 
2770 module_init(dm_writecache_init);
2771 module_exit(dm_writecache_exit);
2772 
2773 MODULE_DESCRIPTION(DM_NAME " writecache target");
2774 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2775 MODULE_LICENSE("GPL");