148debafeSMikulas Patocka // SPDX-License-Identifier: GPL-2.0 248debafeSMikulas Patocka /* 348debafeSMikulas Patocka * Copyright (C) 2018 Red Hat. All rights reserved. 448debafeSMikulas Patocka * 548debafeSMikulas Patocka * This file is released under the GPL. 648debafeSMikulas Patocka */ 748debafeSMikulas Patocka 848debafeSMikulas Patocka #include <linux/device-mapper.h> 948debafeSMikulas Patocka #include <linux/module.h> 1048debafeSMikulas Patocka #include <linux/init.h> 1148debafeSMikulas Patocka #include <linux/vmalloc.h> 1248debafeSMikulas Patocka #include <linux/kthread.h> 1348debafeSMikulas Patocka #include <linux/dm-io.h> 1448debafeSMikulas Patocka #include <linux/dm-kcopyd.h> 1548debafeSMikulas Patocka #include <linux/dax.h> 1648debafeSMikulas Patocka #include <linux/pfn_t.h> 1748debafeSMikulas Patocka #include <linux/libnvdimm.h> 1895b88f4dSMikulas Patocka #include <linux/delay.h> 1995b88f4dSMikulas Patocka #include "dm-io-tracker.h" 2048debafeSMikulas Patocka 2148debafeSMikulas Patocka #define DM_MSG_PREFIX "writecache" 2248debafeSMikulas Patocka 2348debafeSMikulas Patocka #define HIGH_WATERMARK 50 2448debafeSMikulas Patocka #define LOW_WATERMARK 45 2548debafeSMikulas Patocka #define MAX_WRITEBACK_JOBS 0 2648debafeSMikulas Patocka #define ENDIO_LATENCY 16 2748debafeSMikulas Patocka #define WRITEBACK_LATENCY 64 2848debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_SSD 65536 2948debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_PMEM 64 3048debafeSMikulas Patocka #define AUTOCOMMIT_MSEC 1000 313923d485SMikulas Patocka #define MAX_AGE_DIV 16 323923d485SMikulas Patocka #define MAX_AGE_UNSPECIFIED -1UL 335c0de3d7SMikulas Patocka #define PAUSE_WRITEBACK (HZ * 3) 3448debafeSMikulas Patocka 3548debafeSMikulas Patocka #define BITMAP_GRANULARITY 65536 3648debafeSMikulas Patocka #if BITMAP_GRANULARITY < PAGE_SIZE 3748debafeSMikulas Patocka #undef BITMAP_GRANULARITY 3848debafeSMikulas Patocka #define BITMAP_GRANULARITY PAGE_SIZE 3948debafeSMikulas Patocka #endif 4048debafeSMikulas Patocka 4148debafeSMikulas Patocka #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER) 4248debafeSMikulas Patocka #define DM_WRITECACHE_HAS_PMEM 4348debafeSMikulas Patocka #endif 4448debafeSMikulas Patocka 4548debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM 4648debafeSMikulas Patocka #define pmem_assign(dest, src) \ 4748debafeSMikulas Patocka do { \ 4848debafeSMikulas Patocka typeof(dest) uniq = (src); \ 4948debafeSMikulas Patocka memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \ 5048debafeSMikulas Patocka } while (0) 5148debafeSMikulas Patocka #else 5248debafeSMikulas Patocka #define pmem_assign(dest, src) ((dest) = (src)) 5348debafeSMikulas Patocka #endif 5448debafeSMikulas Patocka 55ec6347bbSDan Williams #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM) 5648debafeSMikulas Patocka #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 5748debafeSMikulas Patocka #endif 5848debafeSMikulas Patocka 5948debafeSMikulas Patocka #define MEMORY_SUPERBLOCK_MAGIC 0x23489321 6048debafeSMikulas Patocka #define MEMORY_SUPERBLOCK_VERSION 1 6148debafeSMikulas Patocka 6248debafeSMikulas Patocka struct wc_memory_entry { 6348debafeSMikulas Patocka __le64 original_sector; 6448debafeSMikulas Patocka __le64 seq_count; 6548debafeSMikulas Patocka }; 6648debafeSMikulas Patocka 6748debafeSMikulas Patocka struct wc_memory_superblock { 6848debafeSMikulas Patocka union { 6948debafeSMikulas Patocka struct { 7048debafeSMikulas Patocka __le32 magic; 7148debafeSMikulas Patocka __le32 version; 7248debafeSMikulas Patocka __le32 block_size; 7348debafeSMikulas Patocka __le32 pad; 7448debafeSMikulas Patocka __le64 n_blocks; 7548debafeSMikulas Patocka __le64 seq_count; 7648debafeSMikulas Patocka }; 7748debafeSMikulas Patocka __le64 padding[8]; 7848debafeSMikulas Patocka }; 79c40819f2SJulia Lawall struct wc_memory_entry entries[]; 8048debafeSMikulas Patocka }; 8148debafeSMikulas Patocka 8248debafeSMikulas Patocka struct wc_entry { 8348debafeSMikulas Patocka struct rb_node rb_node; 8448debafeSMikulas Patocka struct list_head lru; 8548debafeSMikulas Patocka unsigned short wc_list_contiguous; 8648debafeSMikulas Patocka bool write_in_progress 8748debafeSMikulas Patocka #if BITS_PER_LONG == 64 8848debafeSMikulas Patocka :1 8948debafeSMikulas Patocka #endif 9048debafeSMikulas Patocka ; 9148debafeSMikulas Patocka unsigned long index 9248debafeSMikulas Patocka #if BITS_PER_LONG == 64 9348debafeSMikulas Patocka :47 9448debafeSMikulas Patocka #endif 9548debafeSMikulas Patocka ; 963923d485SMikulas Patocka unsigned long age; 9748debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 9848debafeSMikulas Patocka uint64_t original_sector; 9948debafeSMikulas Patocka uint64_t seq_count; 10048debafeSMikulas Patocka #endif 10148debafeSMikulas Patocka }; 10248debafeSMikulas Patocka 10348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM 10448debafeSMikulas Patocka #define WC_MODE_PMEM(wc) ((wc)->pmem_mode) 10548debafeSMikulas Patocka #define WC_MODE_FUA(wc) ((wc)->writeback_fua) 10648debafeSMikulas Patocka #else 10748debafeSMikulas Patocka #define WC_MODE_PMEM(wc) false 10848debafeSMikulas Patocka #define WC_MODE_FUA(wc) false 10948debafeSMikulas Patocka #endif 11048debafeSMikulas Patocka #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc)) 11148debafeSMikulas Patocka 11248debafeSMikulas Patocka struct dm_writecache { 11348debafeSMikulas Patocka struct mutex lock; 11448debafeSMikulas Patocka struct list_head lru; 11548debafeSMikulas Patocka union { 11648debafeSMikulas Patocka struct list_head freelist; 11748debafeSMikulas Patocka struct { 11848debafeSMikulas Patocka struct rb_root freetree; 11948debafeSMikulas Patocka struct wc_entry *current_free; 12048debafeSMikulas Patocka }; 12148debafeSMikulas Patocka }; 12248debafeSMikulas Patocka struct rb_root tree; 12348debafeSMikulas Patocka 12448debafeSMikulas Patocka size_t freelist_size; 12548debafeSMikulas Patocka size_t writeback_size; 12648debafeSMikulas Patocka size_t freelist_high_watermark; 12748debafeSMikulas Patocka size_t freelist_low_watermark; 1283923d485SMikulas Patocka unsigned long max_age; 1295c0de3d7SMikulas Patocka unsigned long pause; 13048debafeSMikulas Patocka 13148debafeSMikulas Patocka unsigned uncommitted_blocks; 13248debafeSMikulas Patocka unsigned autocommit_blocks; 13348debafeSMikulas Patocka unsigned max_writeback_jobs; 13448debafeSMikulas Patocka 13548debafeSMikulas Patocka int error; 13648debafeSMikulas Patocka 13748debafeSMikulas Patocka unsigned long autocommit_jiffies; 13848debafeSMikulas Patocka struct timer_list autocommit_timer; 13948debafeSMikulas Patocka struct wait_queue_head freelist_wait; 14048debafeSMikulas Patocka 1413923d485SMikulas Patocka struct timer_list max_age_timer; 1423923d485SMikulas Patocka 14348debafeSMikulas Patocka atomic_t bio_in_progress[2]; 14448debafeSMikulas Patocka struct wait_queue_head bio_in_progress_wait[2]; 14548debafeSMikulas Patocka 14648debafeSMikulas Patocka struct dm_target *ti; 14748debafeSMikulas Patocka struct dm_dev *dev; 14848debafeSMikulas Patocka struct dm_dev *ssd_dev; 149d284f824SMikulas Patocka sector_t start_sector; 15048debafeSMikulas Patocka void *memory_map; 15148debafeSMikulas Patocka uint64_t memory_map_size; 15248debafeSMikulas Patocka size_t metadata_sectors; 15348debafeSMikulas Patocka size_t n_blocks; 15448debafeSMikulas Patocka uint64_t seq_count; 1554134455fSMikulas Patocka sector_t data_device_sectors; 15648debafeSMikulas Patocka void *block_start; 15748debafeSMikulas Patocka struct wc_entry *entries; 15848debafeSMikulas Patocka unsigned block_size; 15948debafeSMikulas Patocka unsigned char block_size_bits; 16048debafeSMikulas Patocka 16148debafeSMikulas Patocka bool pmem_mode:1; 16248debafeSMikulas Patocka bool writeback_fua:1; 16348debafeSMikulas Patocka 16448debafeSMikulas Patocka bool overwrote_committed:1; 16548debafeSMikulas Patocka bool memory_vmapped:1; 16648debafeSMikulas Patocka 167054bee16SMikulas Patocka bool start_sector_set:1; 16848debafeSMikulas Patocka bool high_wm_percent_set:1; 16948debafeSMikulas Patocka bool low_wm_percent_set:1; 17048debafeSMikulas Patocka bool max_writeback_jobs_set:1; 17148debafeSMikulas Patocka bool autocommit_blocks_set:1; 17248debafeSMikulas Patocka bool autocommit_time_set:1; 173054bee16SMikulas Patocka bool max_age_set:1; 17448debafeSMikulas Patocka bool writeback_fua_set:1; 17548debafeSMikulas Patocka bool flush_on_suspend:1; 17693de44ebSMikulas Patocka bool cleaner:1; 177054bee16SMikulas Patocka bool cleaner_set:1; 178611c3e16SMikulas Patocka bool metadata_only:1; 1795c0de3d7SMikulas Patocka bool pause_set:1; 180054bee16SMikulas Patocka 181054bee16SMikulas Patocka unsigned high_wm_percent_value; 182054bee16SMikulas Patocka unsigned low_wm_percent_value; 183054bee16SMikulas Patocka unsigned autocommit_time_value; 184054bee16SMikulas Patocka unsigned max_age_value; 1855c0de3d7SMikulas Patocka unsigned pause_value; 18648debafeSMikulas Patocka 18748debafeSMikulas Patocka unsigned writeback_all; 18848debafeSMikulas Patocka struct workqueue_struct *writeback_wq; 18948debafeSMikulas Patocka struct work_struct writeback_work; 19048debafeSMikulas Patocka struct work_struct flush_work; 19148debafeSMikulas Patocka 19295b88f4dSMikulas Patocka struct dm_io_tracker iot; 19395b88f4dSMikulas Patocka 19448debafeSMikulas Patocka struct dm_io_client *dm_io; 19548debafeSMikulas Patocka 19648debafeSMikulas Patocka raw_spinlock_t endio_list_lock; 19748debafeSMikulas Patocka struct list_head endio_list; 19848debafeSMikulas Patocka struct task_struct *endio_thread; 19948debafeSMikulas Patocka 20048debafeSMikulas Patocka struct task_struct *flush_thread; 20148debafeSMikulas Patocka struct bio_list flush_list; 20248debafeSMikulas Patocka 20348debafeSMikulas Patocka struct dm_kcopyd_client *dm_kcopyd; 20448debafeSMikulas Patocka unsigned long *dirty_bitmap; 20548debafeSMikulas Patocka unsigned dirty_bitmap_size; 20648debafeSMikulas Patocka 20748debafeSMikulas Patocka struct bio_set bio_set; 20848debafeSMikulas Patocka mempool_t copy_pool; 209*e3a35d03SMikulas Patocka 210*e3a35d03SMikulas Patocka struct { 211*e3a35d03SMikulas Patocka unsigned long long reads; 212*e3a35d03SMikulas Patocka unsigned long long read_hits; 213*e3a35d03SMikulas Patocka unsigned long long writes; 214*e3a35d03SMikulas Patocka unsigned long long write_hits_uncommitted; 215*e3a35d03SMikulas Patocka unsigned long long write_hits_committed; 216*e3a35d03SMikulas Patocka unsigned long long writes_around; 217*e3a35d03SMikulas Patocka unsigned long long writes_allocate; 218*e3a35d03SMikulas Patocka unsigned long long writes_blocked_on_freelist; 219*e3a35d03SMikulas Patocka unsigned long long flushes; 220*e3a35d03SMikulas Patocka unsigned long long discards; 221*e3a35d03SMikulas Patocka } stats; 22248debafeSMikulas Patocka }; 22348debafeSMikulas Patocka 22448debafeSMikulas Patocka #define WB_LIST_INLINE 16 22548debafeSMikulas Patocka 22648debafeSMikulas Patocka struct writeback_struct { 22748debafeSMikulas Patocka struct list_head endio_entry; 22848debafeSMikulas Patocka struct dm_writecache *wc; 22948debafeSMikulas Patocka struct wc_entry **wc_list; 23048debafeSMikulas Patocka unsigned wc_list_n; 23148debafeSMikulas Patocka struct wc_entry *wc_list_inline[WB_LIST_INLINE]; 23248debafeSMikulas Patocka struct bio bio; 23348debafeSMikulas Patocka }; 23448debafeSMikulas Patocka 23548debafeSMikulas Patocka struct copy_struct { 23648debafeSMikulas Patocka struct list_head endio_entry; 23748debafeSMikulas Patocka struct dm_writecache *wc; 23848debafeSMikulas Patocka struct wc_entry *e; 23948debafeSMikulas Patocka unsigned n_entries; 24048debafeSMikulas Patocka int error; 24148debafeSMikulas Patocka }; 24248debafeSMikulas Patocka 24348debafeSMikulas Patocka DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle, 24448debafeSMikulas Patocka "A percentage of time allocated for data copying"); 24548debafeSMikulas Patocka 24648debafeSMikulas Patocka static void wc_lock(struct dm_writecache *wc) 24748debafeSMikulas Patocka { 24848debafeSMikulas Patocka mutex_lock(&wc->lock); 24948debafeSMikulas Patocka } 25048debafeSMikulas Patocka 25148debafeSMikulas Patocka static void wc_unlock(struct dm_writecache *wc) 25248debafeSMikulas Patocka { 25348debafeSMikulas Patocka mutex_unlock(&wc->lock); 25448debafeSMikulas Patocka } 25548debafeSMikulas Patocka 25648debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM 25748debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc) 25848debafeSMikulas Patocka { 25948debafeSMikulas Patocka int r; 26048debafeSMikulas Patocka loff_t s; 26148debafeSMikulas Patocka long p, da; 26248debafeSMikulas Patocka pfn_t pfn; 26348debafeSMikulas Patocka int id; 26448debafeSMikulas Patocka struct page **pages; 265f9e040efSMikulas Patocka sector_t offset; 26648debafeSMikulas Patocka 26748debafeSMikulas Patocka wc->memory_vmapped = false; 26848debafeSMikulas Patocka 26948debafeSMikulas Patocka s = wc->memory_map_size; 27048debafeSMikulas Patocka p = s >> PAGE_SHIFT; 27148debafeSMikulas Patocka if (!p) { 27248debafeSMikulas Patocka r = -EINVAL; 27348debafeSMikulas Patocka goto err1; 27448debafeSMikulas Patocka } 27548debafeSMikulas Patocka if (p != s >> PAGE_SHIFT) { 27648debafeSMikulas Patocka r = -EOVERFLOW; 27748debafeSMikulas Patocka goto err1; 27848debafeSMikulas Patocka } 27948debafeSMikulas Patocka 280f9e040efSMikulas Patocka offset = get_start_sect(wc->ssd_dev->bdev); 281f9e040efSMikulas Patocka if (offset & (PAGE_SIZE / 512 - 1)) { 282f9e040efSMikulas Patocka r = -EINVAL; 283f9e040efSMikulas Patocka goto err1; 284f9e040efSMikulas Patocka } 285f9e040efSMikulas Patocka offset >>= PAGE_SHIFT - 9; 286f9e040efSMikulas Patocka 28748debafeSMikulas Patocka id = dax_read_lock(); 28848debafeSMikulas Patocka 289f9e040efSMikulas Patocka da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, &wc->memory_map, &pfn); 29048debafeSMikulas Patocka if (da < 0) { 29148debafeSMikulas Patocka wc->memory_map = NULL; 29248debafeSMikulas Patocka r = da; 29348debafeSMikulas Patocka goto err2; 29448debafeSMikulas Patocka } 29548debafeSMikulas Patocka if (!pfn_t_has_page(pfn)) { 29648debafeSMikulas Patocka wc->memory_map = NULL; 29748debafeSMikulas Patocka r = -EOPNOTSUPP; 29848debafeSMikulas Patocka goto err2; 29948debafeSMikulas Patocka } 30048debafeSMikulas Patocka if (da != p) { 30148debafeSMikulas Patocka long i; 30248debafeSMikulas Patocka wc->memory_map = NULL; 30350a7d3baSKees Cook pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL); 30448debafeSMikulas Patocka if (!pages) { 30548debafeSMikulas Patocka r = -ENOMEM; 30648debafeSMikulas Patocka goto err2; 30748debafeSMikulas Patocka } 30848debafeSMikulas Patocka i = 0; 30948debafeSMikulas Patocka do { 31048debafeSMikulas Patocka long daa; 311f9e040efSMikulas Patocka daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, p - i, 312f742267aSHuaisheng Ye NULL, &pfn); 31348debafeSMikulas Patocka if (daa <= 0) { 31448debafeSMikulas Patocka r = daa ? daa : -EINVAL; 31548debafeSMikulas Patocka goto err3; 31648debafeSMikulas Patocka } 31748debafeSMikulas Patocka if (!pfn_t_has_page(pfn)) { 31848debafeSMikulas Patocka r = -EOPNOTSUPP; 31948debafeSMikulas Patocka goto err3; 32048debafeSMikulas Patocka } 32148debafeSMikulas Patocka while (daa-- && i < p) { 32248debafeSMikulas Patocka pages[i++] = pfn_t_to_page(pfn); 32348debafeSMikulas Patocka pfn.val++; 324d35bd764SMikulas Patocka if (!(i & 15)) 325d35bd764SMikulas Patocka cond_resched(); 32648debafeSMikulas Patocka } 32748debafeSMikulas Patocka } while (i < p); 32848debafeSMikulas Patocka wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL); 32948debafeSMikulas Patocka if (!wc->memory_map) { 33048debafeSMikulas Patocka r = -ENOMEM; 33148debafeSMikulas Patocka goto err3; 33248debafeSMikulas Patocka } 33348debafeSMikulas Patocka kvfree(pages); 33448debafeSMikulas Patocka wc->memory_vmapped = true; 33548debafeSMikulas Patocka } 33648debafeSMikulas Patocka 33748debafeSMikulas Patocka dax_read_unlock(id); 338d284f824SMikulas Patocka 339d284f824SMikulas Patocka wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT; 340d284f824SMikulas Patocka wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT; 341d284f824SMikulas Patocka 34248debafeSMikulas Patocka return 0; 34348debafeSMikulas Patocka err3: 34448debafeSMikulas Patocka kvfree(pages); 34548debafeSMikulas Patocka err2: 34648debafeSMikulas Patocka dax_read_unlock(id); 34748debafeSMikulas Patocka err1: 34848debafeSMikulas Patocka return r; 34948debafeSMikulas Patocka } 35048debafeSMikulas Patocka #else 35148debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc) 35248debafeSMikulas Patocka { 353857c4c0aSMike Snitzer return -EOPNOTSUPP; 35448debafeSMikulas Patocka } 35548debafeSMikulas Patocka #endif 35648debafeSMikulas Patocka 35748debafeSMikulas Patocka static void persistent_memory_release(struct dm_writecache *wc) 35848debafeSMikulas Patocka { 35948debafeSMikulas Patocka if (wc->memory_vmapped) 360d284f824SMikulas Patocka vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT)); 36148debafeSMikulas Patocka } 36248debafeSMikulas Patocka 36348debafeSMikulas Patocka static struct page *persistent_memory_page(void *addr) 36448debafeSMikulas Patocka { 36548debafeSMikulas Patocka if (is_vmalloc_addr(addr)) 36648debafeSMikulas Patocka return vmalloc_to_page(addr); 36748debafeSMikulas Patocka else 36848debafeSMikulas Patocka return virt_to_page(addr); 36948debafeSMikulas Patocka } 37048debafeSMikulas Patocka 37148debafeSMikulas Patocka static unsigned persistent_memory_page_offset(void *addr) 37248debafeSMikulas Patocka { 37348debafeSMikulas Patocka return (unsigned long)addr & (PAGE_SIZE - 1); 37448debafeSMikulas Patocka } 37548debafeSMikulas Patocka 37648debafeSMikulas Patocka static void persistent_memory_flush_cache(void *ptr, size_t size) 37748debafeSMikulas Patocka { 37848debafeSMikulas Patocka if (is_vmalloc_addr(ptr)) 37948debafeSMikulas Patocka flush_kernel_vmap_range(ptr, size); 38048debafeSMikulas Patocka } 38148debafeSMikulas Patocka 38248debafeSMikulas Patocka static void persistent_memory_invalidate_cache(void *ptr, size_t size) 38348debafeSMikulas Patocka { 38448debafeSMikulas Patocka if (is_vmalloc_addr(ptr)) 38548debafeSMikulas Patocka invalidate_kernel_vmap_range(ptr, size); 38648debafeSMikulas Patocka } 38748debafeSMikulas Patocka 38848debafeSMikulas Patocka static struct wc_memory_superblock *sb(struct dm_writecache *wc) 38948debafeSMikulas Patocka { 39048debafeSMikulas Patocka return wc->memory_map; 39148debafeSMikulas Patocka } 39248debafeSMikulas Patocka 39348debafeSMikulas Patocka static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e) 39448debafeSMikulas Patocka { 39548debafeSMikulas Patocka return &sb(wc)->entries[e->index]; 39648debafeSMikulas Patocka } 39748debafeSMikulas Patocka 39848debafeSMikulas Patocka static void *memory_data(struct dm_writecache *wc, struct wc_entry *e) 39948debafeSMikulas Patocka { 40048debafeSMikulas Patocka return (char *)wc->block_start + (e->index << wc->block_size_bits); 40148debafeSMikulas Patocka } 40248debafeSMikulas Patocka 40348debafeSMikulas Patocka static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e) 40448debafeSMikulas Patocka { 405d284f824SMikulas Patocka return wc->start_sector + wc->metadata_sectors + 40648debafeSMikulas Patocka ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT)); 40748debafeSMikulas Patocka } 40848debafeSMikulas Patocka 40948debafeSMikulas Patocka static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e) 41048debafeSMikulas Patocka { 41148debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 41248debafeSMikulas Patocka return e->original_sector; 41348debafeSMikulas Patocka #else 41448debafeSMikulas Patocka return le64_to_cpu(memory_entry(wc, e)->original_sector); 41548debafeSMikulas Patocka #endif 41648debafeSMikulas Patocka } 41748debafeSMikulas Patocka 41848debafeSMikulas Patocka static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e) 41948debafeSMikulas Patocka { 42048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 42148debafeSMikulas Patocka return e->seq_count; 42248debafeSMikulas Patocka #else 42348debafeSMikulas Patocka return le64_to_cpu(memory_entry(wc, e)->seq_count); 42448debafeSMikulas Patocka #endif 42548debafeSMikulas Patocka } 42648debafeSMikulas Patocka 42748debafeSMikulas Patocka static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e) 42848debafeSMikulas Patocka { 42948debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 43048debafeSMikulas Patocka e->seq_count = -1; 43148debafeSMikulas Patocka #endif 43248debafeSMikulas Patocka pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1)); 43348debafeSMikulas Patocka } 43448debafeSMikulas Patocka 43548debafeSMikulas Patocka static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e, 43648debafeSMikulas Patocka uint64_t original_sector, uint64_t seq_count) 43748debafeSMikulas Patocka { 43848debafeSMikulas Patocka struct wc_memory_entry me; 43948debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 44048debafeSMikulas Patocka e->original_sector = original_sector; 44148debafeSMikulas Patocka e->seq_count = seq_count; 44248debafeSMikulas Patocka #endif 44348debafeSMikulas Patocka me.original_sector = cpu_to_le64(original_sector); 44448debafeSMikulas Patocka me.seq_count = cpu_to_le64(seq_count); 44548debafeSMikulas Patocka pmem_assign(*memory_entry(wc, e), me); 44648debafeSMikulas Patocka } 44748debafeSMikulas Patocka 44848debafeSMikulas Patocka #define writecache_error(wc, err, msg, arg...) \ 44948debafeSMikulas Patocka do { \ 45048debafeSMikulas Patocka if (!cmpxchg(&(wc)->error, 0, err)) \ 45148debafeSMikulas Patocka DMERR(msg, ##arg); \ 45248debafeSMikulas Patocka wake_up(&(wc)->freelist_wait); \ 45348debafeSMikulas Patocka } while (0) 45448debafeSMikulas Patocka 45548debafeSMikulas Patocka #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error))) 45648debafeSMikulas Patocka 45748debafeSMikulas Patocka static void writecache_flush_all_metadata(struct dm_writecache *wc) 45848debafeSMikulas Patocka { 45948debafeSMikulas Patocka if (!WC_MODE_PMEM(wc)) 46048debafeSMikulas Patocka memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size); 46148debafeSMikulas Patocka } 46248debafeSMikulas Patocka 46348debafeSMikulas Patocka static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size) 46448debafeSMikulas Patocka { 46548debafeSMikulas Patocka if (!WC_MODE_PMEM(wc)) 46648debafeSMikulas Patocka __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY, 46748debafeSMikulas Patocka wc->dirty_bitmap); 46848debafeSMikulas Patocka } 46948debafeSMikulas Patocka 47048debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev); 47148debafeSMikulas Patocka 47248debafeSMikulas Patocka struct io_notify { 47348debafeSMikulas Patocka struct dm_writecache *wc; 47448debafeSMikulas Patocka struct completion c; 47548debafeSMikulas Patocka atomic_t count; 47648debafeSMikulas Patocka }; 47748debafeSMikulas Patocka 47848debafeSMikulas Patocka static void writecache_notify_io(unsigned long error, void *context) 47948debafeSMikulas Patocka { 48048debafeSMikulas Patocka struct io_notify *endio = context; 48148debafeSMikulas Patocka 48248debafeSMikulas Patocka if (unlikely(error != 0)) 48348debafeSMikulas Patocka writecache_error(endio->wc, -EIO, "error writing metadata"); 48448debafeSMikulas Patocka BUG_ON(atomic_read(&endio->count) <= 0); 48548debafeSMikulas Patocka if (atomic_dec_and_test(&endio->count)) 48648debafeSMikulas Patocka complete(&endio->c); 48748debafeSMikulas Patocka } 48848debafeSMikulas Patocka 489aa950920SMikulas Patocka static void writecache_wait_for_ios(struct dm_writecache *wc, int direction) 490aa950920SMikulas Patocka { 491aa950920SMikulas Patocka wait_event(wc->bio_in_progress_wait[direction], 492aa950920SMikulas Patocka !atomic_read(&wc->bio_in_progress[direction])); 493aa950920SMikulas Patocka } 494aa950920SMikulas Patocka 495aa950920SMikulas Patocka static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) 49648debafeSMikulas Patocka { 49748debafeSMikulas Patocka struct dm_io_region region; 49848debafeSMikulas Patocka struct dm_io_request req; 49948debafeSMikulas Patocka struct io_notify endio = { 50048debafeSMikulas Patocka wc, 50148debafeSMikulas Patocka COMPLETION_INITIALIZER_ONSTACK(endio.c), 50248debafeSMikulas Patocka ATOMIC_INIT(1), 50348debafeSMikulas Patocka }; 5041e1132eaSMikulas Patocka unsigned bitmap_bits = wc->dirty_bitmap_size * 8; 50548debafeSMikulas Patocka unsigned i = 0; 50648debafeSMikulas Patocka 50748debafeSMikulas Patocka while (1) { 50848debafeSMikulas Patocka unsigned j; 50948debafeSMikulas Patocka i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i); 51048debafeSMikulas Patocka if (unlikely(i == bitmap_bits)) 51148debafeSMikulas Patocka break; 51248debafeSMikulas Patocka j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i); 51348debafeSMikulas Patocka 51448debafeSMikulas Patocka region.bdev = wc->ssd_dev->bdev; 51548debafeSMikulas Patocka region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT); 51648debafeSMikulas Patocka region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT); 51748debafeSMikulas Patocka 51848debafeSMikulas Patocka if (unlikely(region.sector >= wc->metadata_sectors)) 51948debafeSMikulas Patocka break; 52048debafeSMikulas Patocka if (unlikely(region.sector + region.count > wc->metadata_sectors)) 52148debafeSMikulas Patocka region.count = wc->metadata_sectors - region.sector; 52248debafeSMikulas Patocka 523d284f824SMikulas Patocka region.sector += wc->start_sector; 52448debafeSMikulas Patocka atomic_inc(&endio.count); 52548debafeSMikulas Patocka req.bi_op = REQ_OP_WRITE; 52648debafeSMikulas Patocka req.bi_op_flags = REQ_SYNC; 52748debafeSMikulas Patocka req.mem.type = DM_IO_VMA; 52848debafeSMikulas Patocka req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY; 52948debafeSMikulas Patocka req.client = wc->dm_io; 53048debafeSMikulas Patocka req.notify.fn = writecache_notify_io; 53148debafeSMikulas Patocka req.notify.context = &endio; 53248debafeSMikulas Patocka 53348debafeSMikulas Patocka /* writing via async dm-io (implied by notify.fn above) won't return an error */ 53448debafeSMikulas Patocka (void) dm_io(&req, 1, ®ion, NULL); 53548debafeSMikulas Patocka i = j; 53648debafeSMikulas Patocka } 53748debafeSMikulas Patocka 53848debafeSMikulas Patocka writecache_notify_io(0, &endio); 53948debafeSMikulas Patocka wait_for_completion_io(&endio.c); 54048debafeSMikulas Patocka 541aa950920SMikulas Patocka if (wait_for_ios) 542aa950920SMikulas Patocka writecache_wait_for_ios(wc, WRITE); 543aa950920SMikulas Patocka 54448debafeSMikulas Patocka writecache_disk_flush(wc, wc->ssd_dev); 54548debafeSMikulas Patocka 54648debafeSMikulas Patocka memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size); 54748debafeSMikulas Patocka } 54848debafeSMikulas Patocka 549dc8a01aeSMikulas Patocka static void ssd_commit_superblock(struct dm_writecache *wc) 550dc8a01aeSMikulas Patocka { 551dc8a01aeSMikulas Patocka int r; 552dc8a01aeSMikulas Patocka struct dm_io_region region; 553dc8a01aeSMikulas Patocka struct dm_io_request req; 554dc8a01aeSMikulas Patocka 555dc8a01aeSMikulas Patocka region.bdev = wc->ssd_dev->bdev; 556dc8a01aeSMikulas Patocka region.sector = 0; 557867de40cSMikulas Patocka region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT; 558867de40cSMikulas Patocka 559867de40cSMikulas Patocka if (unlikely(region.sector + region.count > wc->metadata_sectors)) 560867de40cSMikulas Patocka region.count = wc->metadata_sectors - region.sector; 561867de40cSMikulas Patocka 562dc8a01aeSMikulas Patocka region.sector += wc->start_sector; 563dc8a01aeSMikulas Patocka 564dc8a01aeSMikulas Patocka req.bi_op = REQ_OP_WRITE; 565dc8a01aeSMikulas Patocka req.bi_op_flags = REQ_SYNC | REQ_FUA; 566dc8a01aeSMikulas Patocka req.mem.type = DM_IO_VMA; 567dc8a01aeSMikulas Patocka req.mem.ptr.vma = (char *)wc->memory_map; 568dc8a01aeSMikulas Patocka req.client = wc->dm_io; 569dc8a01aeSMikulas Patocka req.notify.fn = NULL; 570dc8a01aeSMikulas Patocka req.notify.context = NULL; 571dc8a01aeSMikulas Patocka 572dc8a01aeSMikulas Patocka r = dm_io(&req, 1, ®ion, NULL); 573dc8a01aeSMikulas Patocka if (unlikely(r)) 574dc8a01aeSMikulas Patocka writecache_error(wc, r, "error writing superblock"); 575dc8a01aeSMikulas Patocka } 576dc8a01aeSMikulas Patocka 577aa950920SMikulas Patocka static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) 57848debafeSMikulas Patocka { 57948debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 5803e79f082SAneesh Kumar K.V pmem_wmb(); 58148debafeSMikulas Patocka else 582aa950920SMikulas Patocka ssd_commit_flushed(wc, wait_for_ios); 58348debafeSMikulas Patocka } 58448debafeSMikulas Patocka 58548debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev) 58648debafeSMikulas Patocka { 58748debafeSMikulas Patocka int r; 58848debafeSMikulas Patocka struct dm_io_region region; 58948debafeSMikulas Patocka struct dm_io_request req; 59048debafeSMikulas Patocka 59148debafeSMikulas Patocka region.bdev = dev->bdev; 59248debafeSMikulas Patocka region.sector = 0; 59348debafeSMikulas Patocka region.count = 0; 59448debafeSMikulas Patocka req.bi_op = REQ_OP_WRITE; 59548debafeSMikulas Patocka req.bi_op_flags = REQ_PREFLUSH; 59648debafeSMikulas Patocka req.mem.type = DM_IO_KMEM; 59748debafeSMikulas Patocka req.mem.ptr.addr = NULL; 59848debafeSMikulas Patocka req.client = wc->dm_io; 59948debafeSMikulas Patocka req.notify.fn = NULL; 60048debafeSMikulas Patocka 60148debafeSMikulas Patocka r = dm_io(&req, 1, ®ion, NULL); 60248debafeSMikulas Patocka if (unlikely(r)) 60348debafeSMikulas Patocka writecache_error(wc, r, "error flushing metadata: %d", r); 60448debafeSMikulas Patocka } 60548debafeSMikulas Patocka 60648debafeSMikulas Patocka #define WFE_RETURN_FOLLOWING 1 60748debafeSMikulas Patocka #define WFE_LOWEST_SEQ 2 60848debafeSMikulas Patocka 60948debafeSMikulas Patocka static struct wc_entry *writecache_find_entry(struct dm_writecache *wc, 61048debafeSMikulas Patocka uint64_t block, int flags) 61148debafeSMikulas Patocka { 61248debafeSMikulas Patocka struct wc_entry *e; 61348debafeSMikulas Patocka struct rb_node *node = wc->tree.rb_node; 61448debafeSMikulas Patocka 61548debafeSMikulas Patocka if (unlikely(!node)) 61648debafeSMikulas Patocka return NULL; 61748debafeSMikulas Patocka 61848debafeSMikulas Patocka while (1) { 61948debafeSMikulas Patocka e = container_of(node, struct wc_entry, rb_node); 62048debafeSMikulas Patocka if (read_original_sector(wc, e) == block) 62148debafeSMikulas Patocka break; 622f8011d33SMikulas Patocka 62348debafeSMikulas Patocka node = (read_original_sector(wc, e) >= block ? 62448debafeSMikulas Patocka e->rb_node.rb_left : e->rb_node.rb_right); 62548debafeSMikulas Patocka if (unlikely(!node)) { 626f8011d33SMikulas Patocka if (!(flags & WFE_RETURN_FOLLOWING)) 62748debafeSMikulas Patocka return NULL; 62848debafeSMikulas Patocka if (read_original_sector(wc, e) >= block) { 629f8011d33SMikulas Patocka return e; 63048debafeSMikulas Patocka } else { 63148debafeSMikulas Patocka node = rb_next(&e->rb_node); 632f8011d33SMikulas Patocka if (unlikely(!node)) 63348debafeSMikulas Patocka return NULL; 63448debafeSMikulas Patocka e = container_of(node, struct wc_entry, rb_node); 635f8011d33SMikulas Patocka return e; 63648debafeSMikulas Patocka } 63748debafeSMikulas Patocka } 63848debafeSMikulas Patocka } 63948debafeSMikulas Patocka 64048debafeSMikulas Patocka while (1) { 64148debafeSMikulas Patocka struct wc_entry *e2; 64248debafeSMikulas Patocka if (flags & WFE_LOWEST_SEQ) 64348debafeSMikulas Patocka node = rb_prev(&e->rb_node); 64448debafeSMikulas Patocka else 64548debafeSMikulas Patocka node = rb_next(&e->rb_node); 64684420b1eSHuaisheng Ye if (unlikely(!node)) 64748debafeSMikulas Patocka return e; 64848debafeSMikulas Patocka e2 = container_of(node, struct wc_entry, rb_node); 64948debafeSMikulas Patocka if (read_original_sector(wc, e2) != block) 65048debafeSMikulas Patocka return e; 65148debafeSMikulas Patocka e = e2; 65248debafeSMikulas Patocka } 65348debafeSMikulas Patocka } 65448debafeSMikulas Patocka 65548debafeSMikulas Patocka static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins) 65648debafeSMikulas Patocka { 65748debafeSMikulas Patocka struct wc_entry *e; 65848debafeSMikulas Patocka struct rb_node **node = &wc->tree.rb_node, *parent = NULL; 65948debafeSMikulas Patocka 66048debafeSMikulas Patocka while (*node) { 66148debafeSMikulas Patocka e = container_of(*node, struct wc_entry, rb_node); 66248debafeSMikulas Patocka parent = &e->rb_node; 66348debafeSMikulas Patocka if (read_original_sector(wc, e) > read_original_sector(wc, ins)) 66448debafeSMikulas Patocka node = &parent->rb_left; 66548debafeSMikulas Patocka else 66648debafeSMikulas Patocka node = &parent->rb_right; 66748debafeSMikulas Patocka } 66848debafeSMikulas Patocka rb_link_node(&ins->rb_node, parent, node); 66948debafeSMikulas Patocka rb_insert_color(&ins->rb_node, &wc->tree); 67048debafeSMikulas Patocka list_add(&ins->lru, &wc->lru); 6713923d485SMikulas Patocka ins->age = jiffies; 67248debafeSMikulas Patocka } 67348debafeSMikulas Patocka 67448debafeSMikulas Patocka static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e) 67548debafeSMikulas Patocka { 67648debafeSMikulas Patocka list_del(&e->lru); 67748debafeSMikulas Patocka rb_erase(&e->rb_node, &wc->tree); 67848debafeSMikulas Patocka } 67948debafeSMikulas Patocka 68048debafeSMikulas Patocka static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e) 68148debafeSMikulas Patocka { 68248debafeSMikulas Patocka if (WC_MODE_SORT_FREELIST(wc)) { 68348debafeSMikulas Patocka struct rb_node **node = &wc->freetree.rb_node, *parent = NULL; 68448debafeSMikulas Patocka if (unlikely(!*node)) 68548debafeSMikulas Patocka wc->current_free = e; 68648debafeSMikulas Patocka while (*node) { 68748debafeSMikulas Patocka parent = *node; 68848debafeSMikulas Patocka if (&e->rb_node < *node) 68948debafeSMikulas Patocka node = &parent->rb_left; 69048debafeSMikulas Patocka else 69148debafeSMikulas Patocka node = &parent->rb_right; 69248debafeSMikulas Patocka } 69348debafeSMikulas Patocka rb_link_node(&e->rb_node, parent, node); 69448debafeSMikulas Patocka rb_insert_color(&e->rb_node, &wc->freetree); 69548debafeSMikulas Patocka } else { 69648debafeSMikulas Patocka list_add_tail(&e->lru, &wc->freelist); 69748debafeSMikulas Patocka } 69848debafeSMikulas Patocka wc->freelist_size++; 69948debafeSMikulas Patocka } 70048debafeSMikulas Patocka 70141c526c5SMikulas Patocka static inline void writecache_verify_watermark(struct dm_writecache *wc) 70241c526c5SMikulas Patocka { 70341c526c5SMikulas Patocka if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark)) 70441c526c5SMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 70541c526c5SMikulas Patocka } 70641c526c5SMikulas Patocka 7073923d485SMikulas Patocka static void writecache_max_age_timer(struct timer_list *t) 7083923d485SMikulas Patocka { 7093923d485SMikulas Patocka struct dm_writecache *wc = from_timer(wc, t, max_age_timer); 7103923d485SMikulas Patocka 7113923d485SMikulas Patocka if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) { 7123923d485SMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 7133923d485SMikulas Patocka mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); 7143923d485SMikulas Patocka } 7153923d485SMikulas Patocka } 7163923d485SMikulas Patocka 717dcd19507SMikulas Patocka static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector) 71848debafeSMikulas Patocka { 71948debafeSMikulas Patocka struct wc_entry *e; 72048debafeSMikulas Patocka 72148debafeSMikulas Patocka if (WC_MODE_SORT_FREELIST(wc)) { 72248debafeSMikulas Patocka struct rb_node *next; 72348debafeSMikulas Patocka if (unlikely(!wc->current_free)) 72448debafeSMikulas Patocka return NULL; 72548debafeSMikulas Patocka e = wc->current_free; 726dcd19507SMikulas Patocka if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) 727dcd19507SMikulas Patocka return NULL; 72848debafeSMikulas Patocka next = rb_next(&e->rb_node); 72948debafeSMikulas Patocka rb_erase(&e->rb_node, &wc->freetree); 73048debafeSMikulas Patocka if (unlikely(!next)) 73148debafeSMikulas Patocka next = rb_first(&wc->freetree); 73248debafeSMikulas Patocka wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL; 73348debafeSMikulas Patocka } else { 73448debafeSMikulas Patocka if (unlikely(list_empty(&wc->freelist))) 73548debafeSMikulas Patocka return NULL; 73648debafeSMikulas Patocka e = container_of(wc->freelist.next, struct wc_entry, lru); 737dcd19507SMikulas Patocka if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) 738dcd19507SMikulas Patocka return NULL; 73948debafeSMikulas Patocka list_del(&e->lru); 74048debafeSMikulas Patocka } 74148debafeSMikulas Patocka wc->freelist_size--; 74241c526c5SMikulas Patocka 74341c526c5SMikulas Patocka writecache_verify_watermark(wc); 74448debafeSMikulas Patocka 74548debafeSMikulas Patocka return e; 74648debafeSMikulas Patocka } 74748debafeSMikulas Patocka 74848debafeSMikulas Patocka static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e) 74948debafeSMikulas Patocka { 75048debafeSMikulas Patocka writecache_unlink(wc, e); 75148debafeSMikulas Patocka writecache_add_to_freelist(wc, e); 75248debafeSMikulas Patocka clear_seq_count(wc, e); 75348debafeSMikulas Patocka writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); 75448debafeSMikulas Patocka if (unlikely(waitqueue_active(&wc->freelist_wait))) 75548debafeSMikulas Patocka wake_up(&wc->freelist_wait); 75648debafeSMikulas Patocka } 75748debafeSMikulas Patocka 75848debafeSMikulas Patocka static void writecache_wait_on_freelist(struct dm_writecache *wc) 75948debafeSMikulas Patocka { 76048debafeSMikulas Patocka DEFINE_WAIT(wait); 76148debafeSMikulas Patocka 76248debafeSMikulas Patocka prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE); 76348debafeSMikulas Patocka wc_unlock(wc); 76448debafeSMikulas Patocka io_schedule(); 76548debafeSMikulas Patocka finish_wait(&wc->freelist_wait, &wait); 76648debafeSMikulas Patocka wc_lock(wc); 76748debafeSMikulas Patocka } 76848debafeSMikulas Patocka 76948debafeSMikulas Patocka static void writecache_poison_lists(struct dm_writecache *wc) 77048debafeSMikulas Patocka { 77148debafeSMikulas Patocka /* 77248debafeSMikulas Patocka * Catch incorrect access to these values while the device is suspended. 77348debafeSMikulas Patocka */ 77448debafeSMikulas Patocka memset(&wc->tree, -1, sizeof wc->tree); 77548debafeSMikulas Patocka wc->lru.next = LIST_POISON1; 77648debafeSMikulas Patocka wc->lru.prev = LIST_POISON2; 77748debafeSMikulas Patocka wc->freelist.next = LIST_POISON1; 77848debafeSMikulas Patocka wc->freelist.prev = LIST_POISON2; 77948debafeSMikulas Patocka } 78048debafeSMikulas Patocka 78148debafeSMikulas Patocka static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e) 78248debafeSMikulas Patocka { 78348debafeSMikulas Patocka writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); 78448debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 78548debafeSMikulas Patocka writecache_flush_region(wc, memory_data(wc, e), wc->block_size); 78648debafeSMikulas Patocka } 78748debafeSMikulas Patocka 78848debafeSMikulas Patocka static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e) 78948debafeSMikulas Patocka { 79048debafeSMikulas Patocka return read_seq_count(wc, e) < wc->seq_count; 79148debafeSMikulas Patocka } 79248debafeSMikulas Patocka 79348debafeSMikulas Patocka static void writecache_flush(struct dm_writecache *wc) 79448debafeSMikulas Patocka { 79548debafeSMikulas Patocka struct wc_entry *e, *e2; 79648debafeSMikulas Patocka bool need_flush_after_free; 79748debafeSMikulas Patocka 79848debafeSMikulas Patocka wc->uncommitted_blocks = 0; 79948debafeSMikulas Patocka del_timer(&wc->autocommit_timer); 80048debafeSMikulas Patocka 80148debafeSMikulas Patocka if (list_empty(&wc->lru)) 80248debafeSMikulas Patocka return; 80348debafeSMikulas Patocka 80448debafeSMikulas Patocka e = container_of(wc->lru.next, struct wc_entry, lru); 80548debafeSMikulas Patocka if (writecache_entry_is_committed(wc, e)) { 80648debafeSMikulas Patocka if (wc->overwrote_committed) { 80748debafeSMikulas Patocka writecache_wait_for_ios(wc, WRITE); 80848debafeSMikulas Patocka writecache_disk_flush(wc, wc->ssd_dev); 80948debafeSMikulas Patocka wc->overwrote_committed = false; 81048debafeSMikulas Patocka } 81148debafeSMikulas Patocka return; 81248debafeSMikulas Patocka } 81348debafeSMikulas Patocka while (1) { 81448debafeSMikulas Patocka writecache_flush_entry(wc, e); 81548debafeSMikulas Patocka if (unlikely(e->lru.next == &wc->lru)) 81648debafeSMikulas Patocka break; 81748debafeSMikulas Patocka e2 = container_of(e->lru.next, struct wc_entry, lru); 81848debafeSMikulas Patocka if (writecache_entry_is_committed(wc, e2)) 81948debafeSMikulas Patocka break; 82048debafeSMikulas Patocka e = e2; 82148debafeSMikulas Patocka cond_resched(); 82248debafeSMikulas Patocka } 823aa950920SMikulas Patocka writecache_commit_flushed(wc, true); 82448debafeSMikulas Patocka 82548debafeSMikulas Patocka wc->seq_count++; 82648debafeSMikulas Patocka pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count)); 827dc8a01aeSMikulas Patocka if (WC_MODE_PMEM(wc)) 828aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 829dc8a01aeSMikulas Patocka else 830dc8a01aeSMikulas Patocka ssd_commit_superblock(wc); 83148debafeSMikulas Patocka 83248debafeSMikulas Patocka wc->overwrote_committed = false; 83348debafeSMikulas Patocka 83448debafeSMikulas Patocka need_flush_after_free = false; 83548debafeSMikulas Patocka while (1) { 83648debafeSMikulas Patocka /* Free another committed entry with lower seq-count */ 83748debafeSMikulas Patocka struct rb_node *rb_node = rb_prev(&e->rb_node); 83848debafeSMikulas Patocka 83948debafeSMikulas Patocka if (rb_node) { 84048debafeSMikulas Patocka e2 = container_of(rb_node, struct wc_entry, rb_node); 84148debafeSMikulas Patocka if (read_original_sector(wc, e2) == read_original_sector(wc, e) && 84248debafeSMikulas Patocka likely(!e2->write_in_progress)) { 84348debafeSMikulas Patocka writecache_free_entry(wc, e2); 84448debafeSMikulas Patocka need_flush_after_free = true; 84548debafeSMikulas Patocka } 84648debafeSMikulas Patocka } 84748debafeSMikulas Patocka if (unlikely(e->lru.prev == &wc->lru)) 84848debafeSMikulas Patocka break; 84948debafeSMikulas Patocka e = container_of(e->lru.prev, struct wc_entry, lru); 85048debafeSMikulas Patocka cond_resched(); 85148debafeSMikulas Patocka } 85248debafeSMikulas Patocka 85348debafeSMikulas Patocka if (need_flush_after_free) 854aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 85548debafeSMikulas Patocka } 85648debafeSMikulas Patocka 85748debafeSMikulas Patocka static void writecache_flush_work(struct work_struct *work) 85848debafeSMikulas Patocka { 85948debafeSMikulas Patocka struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work); 86048debafeSMikulas Patocka 86148debafeSMikulas Patocka wc_lock(wc); 86248debafeSMikulas Patocka writecache_flush(wc); 86348debafeSMikulas Patocka wc_unlock(wc); 86448debafeSMikulas Patocka } 86548debafeSMikulas Patocka 86648debafeSMikulas Patocka static void writecache_autocommit_timer(struct timer_list *t) 86748debafeSMikulas Patocka { 86848debafeSMikulas Patocka struct dm_writecache *wc = from_timer(wc, t, autocommit_timer); 86948debafeSMikulas Patocka if (!writecache_has_error(wc)) 87048debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->flush_work); 87148debafeSMikulas Patocka } 87248debafeSMikulas Patocka 87348debafeSMikulas Patocka static void writecache_schedule_autocommit(struct dm_writecache *wc) 87448debafeSMikulas Patocka { 87548debafeSMikulas Patocka if (!timer_pending(&wc->autocommit_timer)) 87648debafeSMikulas Patocka mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies); 87748debafeSMikulas Patocka } 87848debafeSMikulas Patocka 87948debafeSMikulas Patocka static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end) 88048debafeSMikulas Patocka { 88148debafeSMikulas Patocka struct wc_entry *e; 88248debafeSMikulas Patocka bool discarded_something = false; 88348debafeSMikulas Patocka 88448debafeSMikulas Patocka e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ); 88548debafeSMikulas Patocka if (unlikely(!e)) 88648debafeSMikulas Patocka return; 88748debafeSMikulas Patocka 88848debafeSMikulas Patocka while (read_original_sector(wc, e) < end) { 88948debafeSMikulas Patocka struct rb_node *node = rb_next(&e->rb_node); 89048debafeSMikulas Patocka 89148debafeSMikulas Patocka if (likely(!e->write_in_progress)) { 89248debafeSMikulas Patocka if (!discarded_something) { 893a143e172SHuaisheng Ye if (!WC_MODE_PMEM(wc)) { 89448debafeSMikulas Patocka writecache_wait_for_ios(wc, READ); 89548debafeSMikulas Patocka writecache_wait_for_ios(wc, WRITE); 896a143e172SHuaisheng Ye } 89748debafeSMikulas Patocka discarded_something = true; 89848debafeSMikulas Patocka } 89939495b12SHuaisheng Ye if (!writecache_entry_is_committed(wc, e)) 90039495b12SHuaisheng Ye wc->uncommitted_blocks--; 90148debafeSMikulas Patocka writecache_free_entry(wc, e); 90248debafeSMikulas Patocka } 90348debafeSMikulas Patocka 90484420b1eSHuaisheng Ye if (unlikely(!node)) 90548debafeSMikulas Patocka break; 90648debafeSMikulas Patocka 90748debafeSMikulas Patocka e = container_of(node, struct wc_entry, rb_node); 90848debafeSMikulas Patocka } 90948debafeSMikulas Patocka 91048debafeSMikulas Patocka if (discarded_something) 911aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 91248debafeSMikulas Patocka } 91348debafeSMikulas Patocka 91448debafeSMikulas Patocka static bool writecache_wait_for_writeback(struct dm_writecache *wc) 91548debafeSMikulas Patocka { 91648debafeSMikulas Patocka if (wc->writeback_size) { 91748debafeSMikulas Patocka writecache_wait_on_freelist(wc); 91848debafeSMikulas Patocka return true; 91948debafeSMikulas Patocka } 92048debafeSMikulas Patocka return false; 92148debafeSMikulas Patocka } 92248debafeSMikulas Patocka 92348debafeSMikulas Patocka static void writecache_suspend(struct dm_target *ti) 92448debafeSMikulas Patocka { 92548debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 92648debafeSMikulas Patocka bool flush_on_suspend; 92748debafeSMikulas Patocka 92848debafeSMikulas Patocka del_timer_sync(&wc->autocommit_timer); 9293923d485SMikulas Patocka del_timer_sync(&wc->max_age_timer); 93048debafeSMikulas Patocka 93148debafeSMikulas Patocka wc_lock(wc); 93248debafeSMikulas Patocka writecache_flush(wc); 93348debafeSMikulas Patocka flush_on_suspend = wc->flush_on_suspend; 93448debafeSMikulas Patocka if (flush_on_suspend) { 93548debafeSMikulas Patocka wc->flush_on_suspend = false; 93648debafeSMikulas Patocka wc->writeback_all++; 93748debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 93848debafeSMikulas Patocka } 93948debafeSMikulas Patocka wc_unlock(wc); 94048debafeSMikulas Patocka 941adc0daadSMikulas Patocka drain_workqueue(wc->writeback_wq); 94248debafeSMikulas Patocka 94348debafeSMikulas Patocka wc_lock(wc); 94448debafeSMikulas Patocka if (flush_on_suspend) 94548debafeSMikulas Patocka wc->writeback_all--; 94648debafeSMikulas Patocka while (writecache_wait_for_writeback(wc)); 94748debafeSMikulas Patocka 94848debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 94948debafeSMikulas Patocka persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); 95048debafeSMikulas Patocka 95148debafeSMikulas Patocka writecache_poison_lists(wc); 95248debafeSMikulas Patocka 95348debafeSMikulas Patocka wc_unlock(wc); 95448debafeSMikulas Patocka } 95548debafeSMikulas Patocka 95648debafeSMikulas Patocka static int writecache_alloc_entries(struct dm_writecache *wc) 95748debafeSMikulas Patocka { 95848debafeSMikulas Patocka size_t b; 95948debafeSMikulas Patocka 96048debafeSMikulas Patocka if (wc->entries) 96148debafeSMikulas Patocka return 0; 96250a7d3baSKees Cook wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks)); 96348debafeSMikulas Patocka if (!wc->entries) 96448debafeSMikulas Patocka return -ENOMEM; 96548debafeSMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 96648debafeSMikulas Patocka struct wc_entry *e = &wc->entries[b]; 96748debafeSMikulas Patocka e->index = b; 96848debafeSMikulas Patocka e->write_in_progress = false; 9691edaa447SMikulas Patocka cond_resched(); 97048debafeSMikulas Patocka } 97148debafeSMikulas Patocka 97248debafeSMikulas Patocka return 0; 97348debafeSMikulas Patocka } 97448debafeSMikulas Patocka 97531b22120SMikulas Patocka static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors) 97631b22120SMikulas Patocka { 97731b22120SMikulas Patocka struct dm_io_region region; 97831b22120SMikulas Patocka struct dm_io_request req; 97931b22120SMikulas Patocka 98031b22120SMikulas Patocka region.bdev = wc->ssd_dev->bdev; 98131b22120SMikulas Patocka region.sector = wc->start_sector; 98231b22120SMikulas Patocka region.count = n_sectors; 98331b22120SMikulas Patocka req.bi_op = REQ_OP_READ; 98431b22120SMikulas Patocka req.bi_op_flags = REQ_SYNC; 98531b22120SMikulas Patocka req.mem.type = DM_IO_VMA; 98631b22120SMikulas Patocka req.mem.ptr.vma = (char *)wc->memory_map; 98731b22120SMikulas Patocka req.client = wc->dm_io; 98831b22120SMikulas Patocka req.notify.fn = NULL; 98931b22120SMikulas Patocka 99031b22120SMikulas Patocka return dm_io(&req, 1, ®ion, NULL); 99131b22120SMikulas Patocka } 99231b22120SMikulas Patocka 99348debafeSMikulas Patocka static void writecache_resume(struct dm_target *ti) 99448debafeSMikulas Patocka { 99548debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 99648debafeSMikulas Patocka size_t b; 99748debafeSMikulas Patocka bool need_flush = false; 99848debafeSMikulas Patocka __le64 sb_seq_count; 99948debafeSMikulas Patocka int r; 100048debafeSMikulas Patocka 100148debafeSMikulas Patocka wc_lock(wc); 100248debafeSMikulas Patocka 1003d9928ac5SMike Snitzer wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev); 10044134455fSMikulas Patocka 100531b22120SMikulas Patocka if (WC_MODE_PMEM(wc)) { 100648debafeSMikulas Patocka persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size); 100731b22120SMikulas Patocka } else { 100831b22120SMikulas Patocka r = writecache_read_metadata(wc, wc->metadata_sectors); 100931b22120SMikulas Patocka if (r) { 101031b22120SMikulas Patocka size_t sb_entries_offset; 101131b22120SMikulas Patocka writecache_error(wc, r, "unable to read metadata: %d", r); 101231b22120SMikulas Patocka sb_entries_offset = offsetof(struct wc_memory_superblock, entries); 101331b22120SMikulas Patocka memset((char *)wc->memory_map + sb_entries_offset, -1, 101431b22120SMikulas Patocka (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset); 101531b22120SMikulas Patocka } 101631b22120SMikulas Patocka } 101748debafeSMikulas Patocka 101848debafeSMikulas Patocka wc->tree = RB_ROOT; 101948debafeSMikulas Patocka INIT_LIST_HEAD(&wc->lru); 102048debafeSMikulas Patocka if (WC_MODE_SORT_FREELIST(wc)) { 102148debafeSMikulas Patocka wc->freetree = RB_ROOT; 102248debafeSMikulas Patocka wc->current_free = NULL; 102348debafeSMikulas Patocka } else { 102448debafeSMikulas Patocka INIT_LIST_HEAD(&wc->freelist); 102548debafeSMikulas Patocka } 102648debafeSMikulas Patocka wc->freelist_size = 0; 102748debafeSMikulas Patocka 1028ec6347bbSDan Williams r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count, 1029ec6347bbSDan Williams sizeof(uint64_t)); 103048debafeSMikulas Patocka if (r) { 103148debafeSMikulas Patocka writecache_error(wc, r, "hardware memory error when reading superblock: %d", r); 103248debafeSMikulas Patocka sb_seq_count = cpu_to_le64(0); 103348debafeSMikulas Patocka } 103448debafeSMikulas Patocka wc->seq_count = le64_to_cpu(sb_seq_count); 103548debafeSMikulas Patocka 103648debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 103748debafeSMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 103848debafeSMikulas Patocka struct wc_entry *e = &wc->entries[b]; 103948debafeSMikulas Patocka struct wc_memory_entry wme; 104048debafeSMikulas Patocka if (writecache_has_error(wc)) { 104148debafeSMikulas Patocka e->original_sector = -1; 104248debafeSMikulas Patocka e->seq_count = -1; 104348debafeSMikulas Patocka continue; 104448debafeSMikulas Patocka } 1045ec6347bbSDan Williams r = copy_mc_to_kernel(&wme, memory_entry(wc, e), 1046ec6347bbSDan Williams sizeof(struct wc_memory_entry)); 104748debafeSMikulas Patocka if (r) { 104848debafeSMikulas Patocka writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d", 104948debafeSMikulas Patocka (unsigned long)b, r); 105048debafeSMikulas Patocka e->original_sector = -1; 105148debafeSMikulas Patocka e->seq_count = -1; 105248debafeSMikulas Patocka } else { 105348debafeSMikulas Patocka e->original_sector = le64_to_cpu(wme.original_sector); 105448debafeSMikulas Patocka e->seq_count = le64_to_cpu(wme.seq_count); 105548debafeSMikulas Patocka } 10561edaa447SMikulas Patocka cond_resched(); 105748debafeSMikulas Patocka } 105848debafeSMikulas Patocka #endif 105948debafeSMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 106048debafeSMikulas Patocka struct wc_entry *e = &wc->entries[b]; 106148debafeSMikulas Patocka if (!writecache_entry_is_committed(wc, e)) { 106248debafeSMikulas Patocka if (read_seq_count(wc, e) != -1) { 106348debafeSMikulas Patocka erase_this: 106448debafeSMikulas Patocka clear_seq_count(wc, e); 106548debafeSMikulas Patocka need_flush = true; 106648debafeSMikulas Patocka } 106748debafeSMikulas Patocka writecache_add_to_freelist(wc, e); 106848debafeSMikulas Patocka } else { 106948debafeSMikulas Patocka struct wc_entry *old; 107048debafeSMikulas Patocka 107148debafeSMikulas Patocka old = writecache_find_entry(wc, read_original_sector(wc, e), 0); 107248debafeSMikulas Patocka if (!old) { 107348debafeSMikulas Patocka writecache_insert_entry(wc, e); 107448debafeSMikulas Patocka } else { 107548debafeSMikulas Patocka if (read_seq_count(wc, old) == read_seq_count(wc, e)) { 107648debafeSMikulas Patocka writecache_error(wc, -EINVAL, 107748debafeSMikulas Patocka "two identical entries, position %llu, sector %llu, sequence %llu", 107848debafeSMikulas Patocka (unsigned long long)b, (unsigned long long)read_original_sector(wc, e), 107948debafeSMikulas Patocka (unsigned long long)read_seq_count(wc, e)); 108048debafeSMikulas Patocka } 108148debafeSMikulas Patocka if (read_seq_count(wc, old) > read_seq_count(wc, e)) { 108248debafeSMikulas Patocka goto erase_this; 108348debafeSMikulas Patocka } else { 108448debafeSMikulas Patocka writecache_free_entry(wc, old); 108548debafeSMikulas Patocka writecache_insert_entry(wc, e); 108648debafeSMikulas Patocka need_flush = true; 108748debafeSMikulas Patocka } 108848debafeSMikulas Patocka } 108948debafeSMikulas Patocka } 109048debafeSMikulas Patocka cond_resched(); 109148debafeSMikulas Patocka } 109248debafeSMikulas Patocka 109348debafeSMikulas Patocka if (need_flush) { 109448debafeSMikulas Patocka writecache_flush_all_metadata(wc); 1095aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 109648debafeSMikulas Patocka } 109748debafeSMikulas Patocka 109841c526c5SMikulas Patocka writecache_verify_watermark(wc); 109941c526c5SMikulas Patocka 11003923d485SMikulas Patocka if (wc->max_age != MAX_AGE_UNSPECIFIED) 11013923d485SMikulas Patocka mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); 11023923d485SMikulas Patocka 110348debafeSMikulas Patocka wc_unlock(wc); 110448debafeSMikulas Patocka } 110548debafeSMikulas Patocka 110648debafeSMikulas Patocka static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc) 110748debafeSMikulas Patocka { 110848debafeSMikulas Patocka if (argc != 1) 110948debafeSMikulas Patocka return -EINVAL; 111048debafeSMikulas Patocka 111148debafeSMikulas Patocka wc_lock(wc); 111248debafeSMikulas Patocka if (dm_suspended(wc->ti)) { 111348debafeSMikulas Patocka wc_unlock(wc); 111448debafeSMikulas Patocka return -EBUSY; 111548debafeSMikulas Patocka } 111648debafeSMikulas Patocka if (writecache_has_error(wc)) { 111748debafeSMikulas Patocka wc_unlock(wc); 111848debafeSMikulas Patocka return -EIO; 111948debafeSMikulas Patocka } 112048debafeSMikulas Patocka 112148debafeSMikulas Patocka writecache_flush(wc); 112248debafeSMikulas Patocka wc->writeback_all++; 112348debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 112448debafeSMikulas Patocka wc_unlock(wc); 112548debafeSMikulas Patocka 112648debafeSMikulas Patocka flush_workqueue(wc->writeback_wq); 112748debafeSMikulas Patocka 112848debafeSMikulas Patocka wc_lock(wc); 112948debafeSMikulas Patocka wc->writeback_all--; 113048debafeSMikulas Patocka if (writecache_has_error(wc)) { 113148debafeSMikulas Patocka wc_unlock(wc); 113248debafeSMikulas Patocka return -EIO; 113348debafeSMikulas Patocka } 113448debafeSMikulas Patocka wc_unlock(wc); 113548debafeSMikulas Patocka 113648debafeSMikulas Patocka return 0; 113748debafeSMikulas Patocka } 113848debafeSMikulas Patocka 113948debafeSMikulas Patocka static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc) 114048debafeSMikulas Patocka { 114148debafeSMikulas Patocka if (argc != 1) 114248debafeSMikulas Patocka return -EINVAL; 114348debafeSMikulas Patocka 114448debafeSMikulas Patocka wc_lock(wc); 114548debafeSMikulas Patocka wc->flush_on_suspend = true; 114648debafeSMikulas Patocka wc_unlock(wc); 114748debafeSMikulas Patocka 114848debafeSMikulas Patocka return 0; 114948debafeSMikulas Patocka } 115048debafeSMikulas Patocka 115193de44ebSMikulas Patocka static void activate_cleaner(struct dm_writecache *wc) 115293de44ebSMikulas Patocka { 115393de44ebSMikulas Patocka wc->flush_on_suspend = true; 115493de44ebSMikulas Patocka wc->cleaner = true; 115593de44ebSMikulas Patocka wc->freelist_high_watermark = wc->n_blocks; 115693de44ebSMikulas Patocka wc->freelist_low_watermark = wc->n_blocks; 115793de44ebSMikulas Patocka } 115893de44ebSMikulas Patocka 115993de44ebSMikulas Patocka static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc) 116093de44ebSMikulas Patocka { 116193de44ebSMikulas Patocka if (argc != 1) 116293de44ebSMikulas Patocka return -EINVAL; 116393de44ebSMikulas Patocka 116493de44ebSMikulas Patocka wc_lock(wc); 116593de44ebSMikulas Patocka activate_cleaner(wc); 116693de44ebSMikulas Patocka if (!dm_suspended(wc->ti)) 116793de44ebSMikulas Patocka writecache_verify_watermark(wc); 116893de44ebSMikulas Patocka wc_unlock(wc); 116993de44ebSMikulas Patocka 117093de44ebSMikulas Patocka return 0; 117193de44ebSMikulas Patocka } 117293de44ebSMikulas Patocka 1173*e3a35d03SMikulas Patocka static int process_clear_stats_mesg(unsigned argc, char **argv, struct dm_writecache *wc) 1174*e3a35d03SMikulas Patocka { 1175*e3a35d03SMikulas Patocka if (argc != 1) 1176*e3a35d03SMikulas Patocka return -EINVAL; 1177*e3a35d03SMikulas Patocka 1178*e3a35d03SMikulas Patocka wc_lock(wc); 1179*e3a35d03SMikulas Patocka memset(&wc->stats, 0, sizeof wc->stats); 1180*e3a35d03SMikulas Patocka wc_unlock(wc); 1181*e3a35d03SMikulas Patocka 1182*e3a35d03SMikulas Patocka return 0; 1183*e3a35d03SMikulas Patocka } 1184*e3a35d03SMikulas Patocka 118548debafeSMikulas Patocka static int writecache_message(struct dm_target *ti, unsigned argc, char **argv, 118648debafeSMikulas Patocka char *result, unsigned maxlen) 118748debafeSMikulas Patocka { 118848debafeSMikulas Patocka int r = -EINVAL; 118948debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 119048debafeSMikulas Patocka 119148debafeSMikulas Patocka if (!strcasecmp(argv[0], "flush")) 119248debafeSMikulas Patocka r = process_flush_mesg(argc, argv, wc); 119348debafeSMikulas Patocka else if (!strcasecmp(argv[0], "flush_on_suspend")) 119448debafeSMikulas Patocka r = process_flush_on_suspend_mesg(argc, argv, wc); 119593de44ebSMikulas Patocka else if (!strcasecmp(argv[0], "cleaner")) 119693de44ebSMikulas Patocka r = process_cleaner_mesg(argc, argv, wc); 1197*e3a35d03SMikulas Patocka else if (!strcasecmp(argv[0], "clear_stats")) 1198*e3a35d03SMikulas Patocka r = process_clear_stats_mesg(argc, argv, wc); 119948debafeSMikulas Patocka else 120048debafeSMikulas Patocka DMERR("unrecognised message received: %s", argv[0]); 120148debafeSMikulas Patocka 120248debafeSMikulas Patocka return r; 120348debafeSMikulas Patocka } 120448debafeSMikulas Patocka 120548338daaSMikulas Patocka static void memcpy_flushcache_optimized(void *dest, void *source, size_t size) 120648338daaSMikulas Patocka { 120748338daaSMikulas Patocka /* 120848338daaSMikulas Patocka * clflushopt performs better with block size 1024, 2048, 4096 120948338daaSMikulas Patocka * non-temporal stores perform better with block size 512 121048338daaSMikulas Patocka * 121148338daaSMikulas Patocka * block size 512 1024 2048 4096 121248338daaSMikulas Patocka * movnti 496 MB/s 642 MB/s 725 MB/s 744 MB/s 121348338daaSMikulas Patocka * clflushopt 373 MB/s 688 MB/s 1.1 GB/s 1.2 GB/s 121448338daaSMikulas Patocka * 121548338daaSMikulas Patocka * We see that movnti performs better for 512-byte blocks, and 121648338daaSMikulas Patocka * clflushopt performs better for 1024-byte and larger blocks. So, we 121748338daaSMikulas Patocka * prefer clflushopt for sizes >= 768. 121848338daaSMikulas Patocka * 121948338daaSMikulas Patocka * NOTE: this happens to be the case now (with dm-writecache's single 122048338daaSMikulas Patocka * threaded model) but re-evaluate this once memcpy_flushcache() is 122148338daaSMikulas Patocka * enabled to use movdir64b which might invalidate this performance 122248338daaSMikulas Patocka * advantage seen with cache-allocating-writes plus flushing. 122348338daaSMikulas Patocka */ 122448338daaSMikulas Patocka #ifdef CONFIG_X86 122548338daaSMikulas Patocka if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) && 122648338daaSMikulas Patocka likely(boot_cpu_data.x86_clflush_size == 64) && 122748338daaSMikulas Patocka likely(size >= 768)) { 122848338daaSMikulas Patocka do { 122948338daaSMikulas Patocka memcpy((void *)dest, (void *)source, 64); 123048338daaSMikulas Patocka clflushopt((void *)dest); 123148338daaSMikulas Patocka dest += 64; 123248338daaSMikulas Patocka source += 64; 123348338daaSMikulas Patocka size -= 64; 123448338daaSMikulas Patocka } while (size >= 64); 123548338daaSMikulas Patocka return; 123648338daaSMikulas Patocka } 123748338daaSMikulas Patocka #endif 123848338daaSMikulas Patocka memcpy_flushcache(dest, source, size); 123948338daaSMikulas Patocka } 124048338daaSMikulas Patocka 124148debafeSMikulas Patocka static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data) 124248debafeSMikulas Patocka { 124348debafeSMikulas Patocka void *buf; 124448debafeSMikulas Patocka unsigned size; 124548debafeSMikulas Patocka int rw = bio_data_dir(bio); 124648debafeSMikulas Patocka unsigned remaining_size = wc->block_size; 124748debafeSMikulas Patocka 124848debafeSMikulas Patocka do { 124948debafeSMikulas Patocka struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter); 125018a6234cSChristoph Hellwig buf = bvec_kmap_local(&bv); 125148debafeSMikulas Patocka size = bv.bv_len; 125248debafeSMikulas Patocka if (unlikely(size > remaining_size)) 125348debafeSMikulas Patocka size = remaining_size; 125448debafeSMikulas Patocka 125548debafeSMikulas Patocka if (rw == READ) { 125648debafeSMikulas Patocka int r; 1257ec6347bbSDan Williams r = copy_mc_to_kernel(buf, data, size); 125848debafeSMikulas Patocka flush_dcache_page(bio_page(bio)); 125948debafeSMikulas Patocka if (unlikely(r)) { 126048debafeSMikulas Patocka writecache_error(wc, r, "hardware memory error when reading data: %d", r); 126148debafeSMikulas Patocka bio->bi_status = BLK_STS_IOERR; 126248debafeSMikulas Patocka } 126348debafeSMikulas Patocka } else { 126448debafeSMikulas Patocka flush_dcache_page(bio_page(bio)); 126548338daaSMikulas Patocka memcpy_flushcache_optimized(data, buf, size); 126648debafeSMikulas Patocka } 126748debafeSMikulas Patocka 126818a6234cSChristoph Hellwig kunmap_local(buf); 126948debafeSMikulas Patocka 127048debafeSMikulas Patocka data = (char *)data + size; 127148debafeSMikulas Patocka remaining_size -= size; 127248debafeSMikulas Patocka bio_advance(bio, size); 127348debafeSMikulas Patocka } while (unlikely(remaining_size)); 127448debafeSMikulas Patocka } 127548debafeSMikulas Patocka 127648debafeSMikulas Patocka static int writecache_flush_thread(void *data) 127748debafeSMikulas Patocka { 127848debafeSMikulas Patocka struct dm_writecache *wc = data; 127948debafeSMikulas Patocka 128048debafeSMikulas Patocka while (1) { 128148debafeSMikulas Patocka struct bio *bio; 128248debafeSMikulas Patocka 128348debafeSMikulas Patocka wc_lock(wc); 128448debafeSMikulas Patocka bio = bio_list_pop(&wc->flush_list); 128548debafeSMikulas Patocka if (!bio) { 128648debafeSMikulas Patocka set_current_state(TASK_INTERRUPTIBLE); 128748debafeSMikulas Patocka wc_unlock(wc); 128848debafeSMikulas Patocka 128948debafeSMikulas Patocka if (unlikely(kthread_should_stop())) { 129048debafeSMikulas Patocka set_current_state(TASK_RUNNING); 129148debafeSMikulas Patocka break; 129248debafeSMikulas Patocka } 129348debafeSMikulas Patocka 129448debafeSMikulas Patocka schedule(); 129548debafeSMikulas Patocka continue; 129648debafeSMikulas Patocka } 129748debafeSMikulas Patocka 129848debafeSMikulas Patocka if (bio_op(bio) == REQ_OP_DISCARD) { 129948debafeSMikulas Patocka writecache_discard(wc, bio->bi_iter.bi_sector, 130048debafeSMikulas Patocka bio_end_sector(bio)); 130148debafeSMikulas Patocka wc_unlock(wc); 130248debafeSMikulas Patocka bio_set_dev(bio, wc->dev->bdev); 1303ed00aabdSChristoph Hellwig submit_bio_noacct(bio); 130448debafeSMikulas Patocka } else { 130548debafeSMikulas Patocka writecache_flush(wc); 130648debafeSMikulas Patocka wc_unlock(wc); 130748debafeSMikulas Patocka if (writecache_has_error(wc)) 130848debafeSMikulas Patocka bio->bi_status = BLK_STS_IOERR; 130948debafeSMikulas Patocka bio_endio(bio); 131048debafeSMikulas Patocka } 131148debafeSMikulas Patocka } 131248debafeSMikulas Patocka 131348debafeSMikulas Patocka return 0; 131448debafeSMikulas Patocka } 131548debafeSMikulas Patocka 131648debafeSMikulas Patocka static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio) 131748debafeSMikulas Patocka { 131848debafeSMikulas Patocka if (bio_list_empty(&wc->flush_list)) 131948debafeSMikulas Patocka wake_up_process(wc->flush_thread); 132048debafeSMikulas Patocka bio_list_add(&wc->flush_list, bio); 132148debafeSMikulas Patocka } 132248debafeSMikulas Patocka 1323cdd4d783SMike Snitzer enum wc_map_op { 1324cdd4d783SMike Snitzer WC_MAP_SUBMIT, 1325cdd4d783SMike Snitzer WC_MAP_REMAP, 1326cdd4d783SMike Snitzer WC_MAP_REMAP_ORIGIN, 1327cdd4d783SMike Snitzer WC_MAP_RETURN, 1328cdd4d783SMike Snitzer WC_MAP_ERROR, 1329cdd4d783SMike Snitzer }; 1330cdd4d783SMike Snitzer 13314d020b3aSMike Snitzer static enum wc_map_op writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio, 13324d020b3aSMike Snitzer struct wc_entry *e) 13334d020b3aSMike Snitzer { 13344d020b3aSMike Snitzer if (e) { 13354d020b3aSMike Snitzer sector_t next_boundary = 13364d020b3aSMike Snitzer read_original_sector(wc, e) - bio->bi_iter.bi_sector; 13374d020b3aSMike Snitzer if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) 13384d020b3aSMike Snitzer dm_accept_partial_bio(bio, next_boundary); 13394d020b3aSMike Snitzer } 13404d020b3aSMike Snitzer 13414d020b3aSMike Snitzer return WC_MAP_REMAP_ORIGIN; 13424d020b3aSMike Snitzer } 13434d020b3aSMike Snitzer 1344cdd4d783SMike Snitzer static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio) 1345cdd4d783SMike Snitzer { 1346cdd4d783SMike Snitzer enum wc_map_op map_op; 1347cdd4d783SMike Snitzer struct wc_entry *e; 1348cdd4d783SMike Snitzer 1349cdd4d783SMike Snitzer read_next_block: 1350*e3a35d03SMikulas Patocka wc->stats.reads++; 1351cdd4d783SMike Snitzer e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); 1352cdd4d783SMike Snitzer if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) { 1353*e3a35d03SMikulas Patocka wc->stats.read_hits++; 1354cdd4d783SMike Snitzer if (WC_MODE_PMEM(wc)) { 1355cdd4d783SMike Snitzer bio_copy_block(wc, bio, memory_data(wc, e)); 1356cdd4d783SMike Snitzer if (bio->bi_iter.bi_size) 1357cdd4d783SMike Snitzer goto read_next_block; 1358cdd4d783SMike Snitzer map_op = WC_MAP_SUBMIT; 1359cdd4d783SMike Snitzer } else { 1360cdd4d783SMike Snitzer dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT); 1361cdd4d783SMike Snitzer bio_set_dev(bio, wc->ssd_dev->bdev); 1362cdd4d783SMike Snitzer bio->bi_iter.bi_sector = cache_sector(wc, e); 1363cdd4d783SMike Snitzer if (!writecache_entry_is_committed(wc, e)) 1364cdd4d783SMike Snitzer writecache_wait_for_ios(wc, WRITE); 1365cdd4d783SMike Snitzer map_op = WC_MAP_REMAP; 1366cdd4d783SMike Snitzer } 1367cdd4d783SMike Snitzer } else { 13684d020b3aSMike Snitzer map_op = writecache_map_remap_origin(wc, bio, e); 1369cdd4d783SMike Snitzer } 1370cdd4d783SMike Snitzer 1371cdd4d783SMike Snitzer return map_op; 1372cdd4d783SMike Snitzer } 1373cdd4d783SMike Snitzer 1374cdd4d783SMike Snitzer static enum wc_map_op writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio, 1375cdd4d783SMike Snitzer struct wc_entry *e, bool search_used) 1376cdd4d783SMike Snitzer { 1377cdd4d783SMike Snitzer unsigned bio_size = wc->block_size; 1378cdd4d783SMike Snitzer sector_t start_cache_sec = cache_sector(wc, e); 1379cdd4d783SMike Snitzer sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT); 1380cdd4d783SMike Snitzer 1381cdd4d783SMike Snitzer while (bio_size < bio->bi_iter.bi_size) { 1382cdd4d783SMike Snitzer if (!search_used) { 1383cdd4d783SMike Snitzer struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec); 1384cdd4d783SMike Snitzer if (!f) 1385cdd4d783SMike Snitzer break; 1386cdd4d783SMike Snitzer write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector + 1387cdd4d783SMike Snitzer (bio_size >> SECTOR_SHIFT), wc->seq_count); 1388cdd4d783SMike Snitzer writecache_insert_entry(wc, f); 1389cdd4d783SMike Snitzer wc->uncommitted_blocks++; 1390cdd4d783SMike Snitzer } else { 1391cdd4d783SMike Snitzer struct wc_entry *f; 1392cdd4d783SMike Snitzer struct rb_node *next = rb_next(&e->rb_node); 1393cdd4d783SMike Snitzer if (!next) 1394cdd4d783SMike Snitzer break; 1395cdd4d783SMike Snitzer f = container_of(next, struct wc_entry, rb_node); 1396cdd4d783SMike Snitzer if (f != e + 1) 1397cdd4d783SMike Snitzer break; 1398cdd4d783SMike Snitzer if (read_original_sector(wc, f) != 1399cdd4d783SMike Snitzer read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) 1400cdd4d783SMike Snitzer break; 1401cdd4d783SMike Snitzer if (unlikely(f->write_in_progress)) 1402cdd4d783SMike Snitzer break; 1403cdd4d783SMike Snitzer if (writecache_entry_is_committed(wc, f)) 1404cdd4d783SMike Snitzer wc->overwrote_committed = true; 1405cdd4d783SMike Snitzer e = f; 1406cdd4d783SMike Snitzer } 1407cdd4d783SMike Snitzer bio_size += wc->block_size; 1408cdd4d783SMike Snitzer current_cache_sec += wc->block_size >> SECTOR_SHIFT; 1409cdd4d783SMike Snitzer } 1410cdd4d783SMike Snitzer 1411cdd4d783SMike Snitzer bio_set_dev(bio, wc->ssd_dev->bdev); 1412cdd4d783SMike Snitzer bio->bi_iter.bi_sector = start_cache_sec; 1413cdd4d783SMike Snitzer dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT); 1414cdd4d783SMike Snitzer 1415cdd4d783SMike Snitzer if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) { 1416cdd4d783SMike Snitzer wc->uncommitted_blocks = 0; 1417cdd4d783SMike Snitzer queue_work(wc->writeback_wq, &wc->flush_work); 1418cdd4d783SMike Snitzer } else { 1419cdd4d783SMike Snitzer writecache_schedule_autocommit(wc); 1420cdd4d783SMike Snitzer } 1421cdd4d783SMike Snitzer 1422cdd4d783SMike Snitzer return WC_MAP_REMAP; 1423cdd4d783SMike Snitzer } 1424cdd4d783SMike Snitzer 1425cdd4d783SMike Snitzer static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio) 142648debafeSMikulas Patocka { 142748debafeSMikulas Patocka struct wc_entry *e; 1428cdd4d783SMike Snitzer 1429cdd4d783SMike Snitzer do { 1430cdd4d783SMike Snitzer bool found_entry = false; 1431cdd4d783SMike Snitzer bool search_used = false; 1432*e3a35d03SMikulas Patocka wc->stats.writes++; 1433cdd4d783SMike Snitzer if (writecache_has_error(wc)) 1434cdd4d783SMike Snitzer return WC_MAP_ERROR; 1435cdd4d783SMike Snitzer e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0); 1436cdd4d783SMike Snitzer if (e) { 1437cdd4d783SMike Snitzer if (!writecache_entry_is_committed(wc, e)) { 1438*e3a35d03SMikulas Patocka wc->stats.write_hits_uncommitted++; 1439cdd4d783SMike Snitzer search_used = true; 1440cdd4d783SMike Snitzer goto bio_copy; 1441cdd4d783SMike Snitzer } 1442*e3a35d03SMikulas Patocka wc->stats.write_hits_committed++; 1443cdd4d783SMike Snitzer if (!WC_MODE_PMEM(wc) && !e->write_in_progress) { 1444cdd4d783SMike Snitzer wc->overwrote_committed = true; 1445cdd4d783SMike Snitzer search_used = true; 1446cdd4d783SMike Snitzer goto bio_copy; 1447cdd4d783SMike Snitzer } 1448cdd4d783SMike Snitzer found_entry = true; 1449cdd4d783SMike Snitzer } else { 1450cdd4d783SMike Snitzer if (unlikely(wc->cleaner) || 1451cdd4d783SMike Snitzer (wc->metadata_only && !(bio->bi_opf & REQ_META))) 1452cdd4d783SMike Snitzer goto direct_write; 1453cdd4d783SMike Snitzer } 1454cdd4d783SMike Snitzer e = writecache_pop_from_freelist(wc, (sector_t)-1); 1455cdd4d783SMike Snitzer if (unlikely(!e)) { 1456cdd4d783SMike Snitzer if (!WC_MODE_PMEM(wc) && !found_entry) { 1457cdd4d783SMike Snitzer direct_write: 1458*e3a35d03SMikulas Patocka wc->stats.writes_around++; 1459cdd4d783SMike Snitzer e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); 14604d020b3aSMike Snitzer return writecache_map_remap_origin(wc, bio, e); 1461cdd4d783SMike Snitzer } 1462*e3a35d03SMikulas Patocka wc->stats.writes_blocked_on_freelist++; 1463cdd4d783SMike Snitzer writecache_wait_on_freelist(wc); 1464cdd4d783SMike Snitzer continue; 1465cdd4d783SMike Snitzer } 1466cdd4d783SMike Snitzer write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count); 1467cdd4d783SMike Snitzer writecache_insert_entry(wc, e); 1468cdd4d783SMike Snitzer wc->uncommitted_blocks++; 1469*e3a35d03SMikulas Patocka wc->stats.writes_allocate++; 1470cdd4d783SMike Snitzer bio_copy: 1471cdd4d783SMike Snitzer if (WC_MODE_PMEM(wc)) 1472cdd4d783SMike Snitzer bio_copy_block(wc, bio, memory_data(wc, e)); 1473cdd4d783SMike Snitzer else 1474cdd4d783SMike Snitzer return writecache_bio_copy_ssd(wc, bio, e, search_used); 1475cdd4d783SMike Snitzer } while (bio->bi_iter.bi_size); 1476cdd4d783SMike Snitzer 1477cdd4d783SMike Snitzer if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks)) 1478cdd4d783SMike Snitzer writecache_flush(wc); 1479cdd4d783SMike Snitzer else 1480cdd4d783SMike Snitzer writecache_schedule_autocommit(wc); 1481cdd4d783SMike Snitzer 1482cdd4d783SMike Snitzer return WC_MAP_SUBMIT; 1483cdd4d783SMike Snitzer } 1484cdd4d783SMike Snitzer 148515cb6f39SMike Snitzer static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio) 148615cb6f39SMike Snitzer { 148715cb6f39SMike Snitzer if (writecache_has_error(wc)) 148815cb6f39SMike Snitzer return WC_MAP_ERROR; 148915cb6f39SMike Snitzer 149015cb6f39SMike Snitzer if (WC_MODE_PMEM(wc)) { 1491*e3a35d03SMikulas Patocka wc->stats.flushes++; 149215cb6f39SMike Snitzer writecache_flush(wc); 149315cb6f39SMike Snitzer if (writecache_has_error(wc)) 149415cb6f39SMike Snitzer return WC_MAP_ERROR; 149515cb6f39SMike Snitzer else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only)) 149615cb6f39SMike Snitzer return WC_MAP_REMAP_ORIGIN; 149715cb6f39SMike Snitzer return WC_MAP_SUBMIT; 149815cb6f39SMike Snitzer } 149915cb6f39SMike Snitzer /* SSD: */ 150015cb6f39SMike Snitzer if (dm_bio_get_target_bio_nr(bio)) 150115cb6f39SMike Snitzer return WC_MAP_REMAP_ORIGIN; 1502*e3a35d03SMikulas Patocka wc->stats.flushes++; 150315cb6f39SMike Snitzer writecache_offload_bio(wc, bio); 150415cb6f39SMike Snitzer return WC_MAP_RETURN; 150515cb6f39SMike Snitzer } 150615cb6f39SMike Snitzer 150715cb6f39SMike Snitzer static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio) 150815cb6f39SMike Snitzer { 1509*e3a35d03SMikulas Patocka wc->stats.discards++; 1510*e3a35d03SMikulas Patocka 151115cb6f39SMike Snitzer if (writecache_has_error(wc)) 151215cb6f39SMike Snitzer return WC_MAP_ERROR; 151315cb6f39SMike Snitzer 151415cb6f39SMike Snitzer if (WC_MODE_PMEM(wc)) { 151515cb6f39SMike Snitzer writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio)); 151615cb6f39SMike Snitzer return WC_MAP_REMAP_ORIGIN; 151715cb6f39SMike Snitzer } 151815cb6f39SMike Snitzer /* SSD: */ 151915cb6f39SMike Snitzer writecache_offload_bio(wc, bio); 152015cb6f39SMike Snitzer return WC_MAP_RETURN; 152115cb6f39SMike Snitzer } 152215cb6f39SMike Snitzer 1523cdd4d783SMike Snitzer static int writecache_map(struct dm_target *ti, struct bio *bio) 1524cdd4d783SMike Snitzer { 152548debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 152615cb6f39SMike Snitzer enum wc_map_op map_op; 152748debafeSMikulas Patocka 152848debafeSMikulas Patocka bio->bi_private = NULL; 152948debafeSMikulas Patocka 153048debafeSMikulas Patocka wc_lock(wc); 153148debafeSMikulas Patocka 153248debafeSMikulas Patocka if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { 153315cb6f39SMike Snitzer map_op = writecache_map_flush(wc, bio); 153415cb6f39SMike Snitzer goto done; 153548debafeSMikulas Patocka } 153648debafeSMikulas Patocka 153748debafeSMikulas Patocka bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector); 153848debafeSMikulas Patocka 153948debafeSMikulas Patocka if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) & 154048debafeSMikulas Patocka (wc->block_size / 512 - 1)) != 0)) { 154148debafeSMikulas Patocka DMERR("I/O is not aligned, sector %llu, size %u, block size %u", 154248debafeSMikulas Patocka (unsigned long long)bio->bi_iter.bi_sector, 154348debafeSMikulas Patocka bio->bi_iter.bi_size, wc->block_size); 154415cb6f39SMike Snitzer map_op = WC_MAP_ERROR; 154515cb6f39SMike Snitzer goto done; 154648debafeSMikulas Patocka } 154748debafeSMikulas Patocka 154848debafeSMikulas Patocka if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) { 154915cb6f39SMike Snitzer map_op = writecache_map_discard(wc, bio); 155015cb6f39SMike Snitzer goto done; 155148debafeSMikulas Patocka } 155248debafeSMikulas Patocka 1553cdd4d783SMike Snitzer if (bio_data_dir(bio) == READ) 1554cdd4d783SMike Snitzer map_op = writecache_map_read(wc, bio); 155548debafeSMikulas Patocka else 1556cdd4d783SMike Snitzer map_op = writecache_map_write(wc, bio); 155715cb6f39SMike Snitzer done: 1558cdd4d783SMike Snitzer switch (map_op) { 1559cdd4d783SMike Snitzer case WC_MAP_REMAP_ORIGIN: 15605c0de3d7SMikulas Patocka if (likely(wc->pause != 0)) { 15615c0de3d7SMikulas Patocka if (bio_op(bio) == REQ_OP_WRITE) { 156295b88f4dSMikulas Patocka dm_iot_io_begin(&wc->iot, 1); 156395b88f4dSMikulas Patocka bio->bi_private = (void *)2; 156495b88f4dSMikulas Patocka } 15655c0de3d7SMikulas Patocka } 156648debafeSMikulas Patocka bio_set_dev(bio, wc->dev->bdev); 156748debafeSMikulas Patocka wc_unlock(wc); 156848debafeSMikulas Patocka return DM_MAPIO_REMAPPED; 156948debafeSMikulas Patocka 1570cdd4d783SMike Snitzer case WC_MAP_REMAP: 157148debafeSMikulas Patocka /* make sure that writecache_end_io decrements bio_in_progress: */ 157248debafeSMikulas Patocka bio->bi_private = (void *)1; 157348debafeSMikulas Patocka atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]); 157448debafeSMikulas Patocka wc_unlock(wc); 157548debafeSMikulas Patocka return DM_MAPIO_REMAPPED; 157648debafeSMikulas Patocka 1577cdd4d783SMike Snitzer case WC_MAP_SUBMIT: 157848debafeSMikulas Patocka wc_unlock(wc); 157948debafeSMikulas Patocka bio_endio(bio); 158048debafeSMikulas Patocka return DM_MAPIO_SUBMITTED; 158148debafeSMikulas Patocka 1582cdd4d783SMike Snitzer case WC_MAP_RETURN: 158348debafeSMikulas Patocka wc_unlock(wc); 158448debafeSMikulas Patocka return DM_MAPIO_SUBMITTED; 158548debafeSMikulas Patocka 1586cdd4d783SMike Snitzer case WC_MAP_ERROR: 158748debafeSMikulas Patocka wc_unlock(wc); 158848debafeSMikulas Patocka bio_io_error(bio); 158948debafeSMikulas Patocka return DM_MAPIO_SUBMITTED; 1590df699cc1SMikulas Patocka 1591df699cc1SMikulas Patocka default: 1592df699cc1SMikulas Patocka BUG(); 1593df699cc1SMikulas Patocka return -1; 159448debafeSMikulas Patocka } 1595cdd4d783SMike Snitzer } 159648debafeSMikulas Patocka 159748debafeSMikulas Patocka static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status) 159848debafeSMikulas Patocka { 159948debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 160048debafeSMikulas Patocka 160195b88f4dSMikulas Patocka if (bio->bi_private == (void *)1) { 160248debafeSMikulas Patocka int dir = bio_data_dir(bio); 160348debafeSMikulas Patocka if (atomic_dec_and_test(&wc->bio_in_progress[dir])) 160448debafeSMikulas Patocka if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir]))) 160548debafeSMikulas Patocka wake_up(&wc->bio_in_progress_wait[dir]); 160695b88f4dSMikulas Patocka } else if (bio->bi_private == (void *)2) { 160795b88f4dSMikulas Patocka dm_iot_io_end(&wc->iot, 1); 160848debafeSMikulas Patocka } 160948debafeSMikulas Patocka return 0; 161048debafeSMikulas Patocka } 161148debafeSMikulas Patocka 161248debafeSMikulas Patocka static int writecache_iterate_devices(struct dm_target *ti, 161348debafeSMikulas Patocka iterate_devices_callout_fn fn, void *data) 161448debafeSMikulas Patocka { 161548debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 161648debafeSMikulas Patocka 161748debafeSMikulas Patocka return fn(ti, wc->dev, 0, ti->len, data); 161848debafeSMikulas Patocka } 161948debafeSMikulas Patocka 162048debafeSMikulas Patocka static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits) 162148debafeSMikulas Patocka { 162248debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 162348debafeSMikulas Patocka 162448debafeSMikulas Patocka if (limits->logical_block_size < wc->block_size) 162548debafeSMikulas Patocka limits->logical_block_size = wc->block_size; 162648debafeSMikulas Patocka 162748debafeSMikulas Patocka if (limits->physical_block_size < wc->block_size) 162848debafeSMikulas Patocka limits->physical_block_size = wc->block_size; 162948debafeSMikulas Patocka 163048debafeSMikulas Patocka if (limits->io_min < wc->block_size) 163148debafeSMikulas Patocka limits->io_min = wc->block_size; 163248debafeSMikulas Patocka } 163348debafeSMikulas Patocka 163448debafeSMikulas Patocka 163548debafeSMikulas Patocka static void writecache_writeback_endio(struct bio *bio) 163648debafeSMikulas Patocka { 163748debafeSMikulas Patocka struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio); 163848debafeSMikulas Patocka struct dm_writecache *wc = wb->wc; 163948debafeSMikulas Patocka unsigned long flags; 164048debafeSMikulas Patocka 164148debafeSMikulas Patocka raw_spin_lock_irqsave(&wc->endio_list_lock, flags); 164248debafeSMikulas Patocka if (unlikely(list_empty(&wc->endio_list))) 164348debafeSMikulas Patocka wake_up_process(wc->endio_thread); 164448debafeSMikulas Patocka list_add_tail(&wb->endio_entry, &wc->endio_list); 164548debafeSMikulas Patocka raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags); 164648debafeSMikulas Patocka } 164748debafeSMikulas Patocka 164848debafeSMikulas Patocka static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr) 164948debafeSMikulas Patocka { 165048debafeSMikulas Patocka struct copy_struct *c = ptr; 165148debafeSMikulas Patocka struct dm_writecache *wc = c->wc; 165248debafeSMikulas Patocka 165348debafeSMikulas Patocka c->error = likely(!(read_err | write_err)) ? 0 : -EIO; 165448debafeSMikulas Patocka 165548debafeSMikulas Patocka raw_spin_lock_irq(&wc->endio_list_lock); 165648debafeSMikulas Patocka if (unlikely(list_empty(&wc->endio_list))) 165748debafeSMikulas Patocka wake_up_process(wc->endio_thread); 165848debafeSMikulas Patocka list_add_tail(&c->endio_entry, &wc->endio_list); 165948debafeSMikulas Patocka raw_spin_unlock_irq(&wc->endio_list_lock); 166048debafeSMikulas Patocka } 166148debafeSMikulas Patocka 166248debafeSMikulas Patocka static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list) 166348debafeSMikulas Patocka { 166448debafeSMikulas Patocka unsigned i; 166548debafeSMikulas Patocka struct writeback_struct *wb; 166648debafeSMikulas Patocka struct wc_entry *e; 166748debafeSMikulas Patocka unsigned long n_walked = 0; 166848debafeSMikulas Patocka 166948debafeSMikulas Patocka do { 167048debafeSMikulas Patocka wb = list_entry(list->next, struct writeback_struct, endio_entry); 167148debafeSMikulas Patocka list_del(&wb->endio_entry); 167248debafeSMikulas Patocka 167348debafeSMikulas Patocka if (unlikely(wb->bio.bi_status != BLK_STS_OK)) 167448debafeSMikulas Patocka writecache_error(wc, blk_status_to_errno(wb->bio.bi_status), 167548debafeSMikulas Patocka "write error %d", wb->bio.bi_status); 167648debafeSMikulas Patocka i = 0; 167748debafeSMikulas Patocka do { 167848debafeSMikulas Patocka e = wb->wc_list[i]; 167948debafeSMikulas Patocka BUG_ON(!e->write_in_progress); 168048debafeSMikulas Patocka e->write_in_progress = false; 168148debafeSMikulas Patocka INIT_LIST_HEAD(&e->lru); 168248debafeSMikulas Patocka if (!writecache_has_error(wc)) 168348debafeSMikulas Patocka writecache_free_entry(wc, e); 168448debafeSMikulas Patocka BUG_ON(!wc->writeback_size); 168548debafeSMikulas Patocka wc->writeback_size--; 168648debafeSMikulas Patocka n_walked++; 168748debafeSMikulas Patocka if (unlikely(n_walked >= ENDIO_LATENCY)) { 1688aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 168948debafeSMikulas Patocka wc_unlock(wc); 169048debafeSMikulas Patocka wc_lock(wc); 169148debafeSMikulas Patocka n_walked = 0; 169248debafeSMikulas Patocka } 169348debafeSMikulas Patocka } while (++i < wb->wc_list_n); 169448debafeSMikulas Patocka 169548debafeSMikulas Patocka if (wb->wc_list != wb->wc_list_inline) 169648debafeSMikulas Patocka kfree(wb->wc_list); 169748debafeSMikulas Patocka bio_put(&wb->bio); 169848debafeSMikulas Patocka } while (!list_empty(list)); 169948debafeSMikulas Patocka } 170048debafeSMikulas Patocka 170148debafeSMikulas Patocka static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list) 170248debafeSMikulas Patocka { 170348debafeSMikulas Patocka struct copy_struct *c; 170448debafeSMikulas Patocka struct wc_entry *e; 170548debafeSMikulas Patocka 170648debafeSMikulas Patocka do { 170748debafeSMikulas Patocka c = list_entry(list->next, struct copy_struct, endio_entry); 170848debafeSMikulas Patocka list_del(&c->endio_entry); 170948debafeSMikulas Patocka 171048debafeSMikulas Patocka if (unlikely(c->error)) 171148debafeSMikulas Patocka writecache_error(wc, c->error, "copy error"); 171248debafeSMikulas Patocka 171348debafeSMikulas Patocka e = c->e; 171448debafeSMikulas Patocka do { 171548debafeSMikulas Patocka BUG_ON(!e->write_in_progress); 171648debafeSMikulas Patocka e->write_in_progress = false; 171748debafeSMikulas Patocka INIT_LIST_HEAD(&e->lru); 171848debafeSMikulas Patocka if (!writecache_has_error(wc)) 171948debafeSMikulas Patocka writecache_free_entry(wc, e); 172048debafeSMikulas Patocka 172148debafeSMikulas Patocka BUG_ON(!wc->writeback_size); 172248debafeSMikulas Patocka wc->writeback_size--; 172348debafeSMikulas Patocka e++; 172448debafeSMikulas Patocka } while (--c->n_entries); 172548debafeSMikulas Patocka mempool_free(c, &wc->copy_pool); 172648debafeSMikulas Patocka } while (!list_empty(list)); 172748debafeSMikulas Patocka } 172848debafeSMikulas Patocka 172948debafeSMikulas Patocka static int writecache_endio_thread(void *data) 173048debafeSMikulas Patocka { 173148debafeSMikulas Patocka struct dm_writecache *wc = data; 173248debafeSMikulas Patocka 173348debafeSMikulas Patocka while (1) { 173448debafeSMikulas Patocka struct list_head list; 173548debafeSMikulas Patocka 173648debafeSMikulas Patocka raw_spin_lock_irq(&wc->endio_list_lock); 173748debafeSMikulas Patocka if (!list_empty(&wc->endio_list)) 173848debafeSMikulas Patocka goto pop_from_list; 173948debafeSMikulas Patocka set_current_state(TASK_INTERRUPTIBLE); 174048debafeSMikulas Patocka raw_spin_unlock_irq(&wc->endio_list_lock); 174148debafeSMikulas Patocka 174248debafeSMikulas Patocka if (unlikely(kthread_should_stop())) { 174348debafeSMikulas Patocka set_current_state(TASK_RUNNING); 174448debafeSMikulas Patocka break; 174548debafeSMikulas Patocka } 174648debafeSMikulas Patocka 174748debafeSMikulas Patocka schedule(); 174848debafeSMikulas Patocka 174948debafeSMikulas Patocka continue; 175048debafeSMikulas Patocka 175148debafeSMikulas Patocka pop_from_list: 175248debafeSMikulas Patocka list = wc->endio_list; 175348debafeSMikulas Patocka list.next->prev = list.prev->next = &list; 175448debafeSMikulas Patocka INIT_LIST_HEAD(&wc->endio_list); 175548debafeSMikulas Patocka raw_spin_unlock_irq(&wc->endio_list_lock); 175648debafeSMikulas Patocka 175748debafeSMikulas Patocka if (!WC_MODE_FUA(wc)) 175848debafeSMikulas Patocka writecache_disk_flush(wc, wc->dev); 175948debafeSMikulas Patocka 176048debafeSMikulas Patocka wc_lock(wc); 176148debafeSMikulas Patocka 176248debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 176348debafeSMikulas Patocka __writecache_endio_pmem(wc, &list); 176448debafeSMikulas Patocka } else { 176548debafeSMikulas Patocka __writecache_endio_ssd(wc, &list); 176648debafeSMikulas Patocka writecache_wait_for_ios(wc, READ); 176748debafeSMikulas Patocka } 176848debafeSMikulas Patocka 1769aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 177048debafeSMikulas Patocka 177148debafeSMikulas Patocka wc_unlock(wc); 177248debafeSMikulas Patocka } 177348debafeSMikulas Patocka 177448debafeSMikulas Patocka return 0; 177548debafeSMikulas Patocka } 177648debafeSMikulas Patocka 1777620cbe40SMikulas Patocka static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e) 177848debafeSMikulas Patocka { 177948debafeSMikulas Patocka struct dm_writecache *wc = wb->wc; 178048debafeSMikulas Patocka unsigned block_size = wc->block_size; 178148debafeSMikulas Patocka void *address = memory_data(wc, e); 178248debafeSMikulas Patocka 178348debafeSMikulas Patocka persistent_memory_flush_cache(address, block_size); 17844134455fSMikulas Patocka 17854134455fSMikulas Patocka if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors)) 17864134455fSMikulas Patocka return true; 17874134455fSMikulas Patocka 178848debafeSMikulas Patocka return bio_add_page(&wb->bio, persistent_memory_page(address), 178948debafeSMikulas Patocka block_size, persistent_memory_page_offset(address)) != 0; 179048debafeSMikulas Patocka } 179148debafeSMikulas Patocka 179248debafeSMikulas Patocka struct writeback_list { 179348debafeSMikulas Patocka struct list_head list; 179448debafeSMikulas Patocka size_t size; 179548debafeSMikulas Patocka }; 179648debafeSMikulas Patocka 179748debafeSMikulas Patocka static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl) 179848debafeSMikulas Patocka { 179948debafeSMikulas Patocka if (unlikely(wc->max_writeback_jobs)) { 180048debafeSMikulas Patocka if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) { 180148debafeSMikulas Patocka wc_lock(wc); 180248debafeSMikulas Patocka while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs) 180348debafeSMikulas Patocka writecache_wait_on_freelist(wc); 180448debafeSMikulas Patocka wc_unlock(wc); 180548debafeSMikulas Patocka } 180648debafeSMikulas Patocka } 180748debafeSMikulas Patocka cond_resched(); 180848debafeSMikulas Patocka } 180948debafeSMikulas Patocka 181048debafeSMikulas Patocka static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl) 181148debafeSMikulas Patocka { 181248debafeSMikulas Patocka struct wc_entry *e, *f; 181348debafeSMikulas Patocka struct bio *bio; 181448debafeSMikulas Patocka struct writeback_struct *wb; 181548debafeSMikulas Patocka unsigned max_pages; 181648debafeSMikulas Patocka 181748debafeSMikulas Patocka while (wbl->size) { 181848debafeSMikulas Patocka wbl->size--; 181948debafeSMikulas Patocka e = container_of(wbl->list.prev, struct wc_entry, lru); 182048debafeSMikulas Patocka list_del(&e->lru); 182148debafeSMikulas Patocka 182248debafeSMikulas Patocka max_pages = e->wc_list_contiguous; 182348debafeSMikulas Patocka 182448debafeSMikulas Patocka bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set); 182548debafeSMikulas Patocka wb = container_of(bio, struct writeback_struct, bio); 182648debafeSMikulas Patocka wb->wc = wc; 182709f2d656SHuaisheng Ye bio->bi_end_io = writecache_writeback_endio; 182809f2d656SHuaisheng Ye bio_set_dev(bio, wc->dev->bdev); 182909f2d656SHuaisheng Ye bio->bi_iter.bi_sector = read_original_sector(wc, e); 183048debafeSMikulas Patocka if (max_pages <= WB_LIST_INLINE || 183150a7d3baSKees Cook unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *), 183248debafeSMikulas Patocka GFP_NOIO | __GFP_NORETRY | 183348debafeSMikulas Patocka __GFP_NOMEMALLOC | __GFP_NOWARN)))) { 183448debafeSMikulas Patocka wb->wc_list = wb->wc_list_inline; 183548debafeSMikulas Patocka max_pages = WB_LIST_INLINE; 183648debafeSMikulas Patocka } 183748debafeSMikulas Patocka 1838620cbe40SMikulas Patocka BUG_ON(!wc_add_block(wb, e)); 183948debafeSMikulas Patocka 184048debafeSMikulas Patocka wb->wc_list[0] = e; 184148debafeSMikulas Patocka wb->wc_list_n = 1; 184248debafeSMikulas Patocka 184348debafeSMikulas Patocka while (wbl->size && wb->wc_list_n < max_pages) { 184448debafeSMikulas Patocka f = container_of(wbl->list.prev, struct wc_entry, lru); 184548debafeSMikulas Patocka if (read_original_sector(wc, f) != 184648debafeSMikulas Patocka read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) 184748debafeSMikulas Patocka break; 1848620cbe40SMikulas Patocka if (!wc_add_block(wb, f)) 184948debafeSMikulas Patocka break; 185048debafeSMikulas Patocka wbl->size--; 185148debafeSMikulas Patocka list_del(&f->lru); 185248debafeSMikulas Patocka wb->wc_list[wb->wc_list_n++] = f; 185348debafeSMikulas Patocka e = f; 185448debafeSMikulas Patocka } 185509f2d656SHuaisheng Ye bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA); 185648debafeSMikulas Patocka if (writecache_has_error(wc)) { 185748debafeSMikulas Patocka bio->bi_status = BLK_STS_IOERR; 185809f2d656SHuaisheng Ye bio_endio(bio); 18594134455fSMikulas Patocka } else if (unlikely(!bio_sectors(bio))) { 18604134455fSMikulas Patocka bio->bi_status = BLK_STS_OK; 18614134455fSMikulas Patocka bio_endio(bio); 186248debafeSMikulas Patocka } else { 186309f2d656SHuaisheng Ye submit_bio(bio); 186448debafeSMikulas Patocka } 186548debafeSMikulas Patocka 186648debafeSMikulas Patocka __writeback_throttle(wc, wbl); 186748debafeSMikulas Patocka } 186848debafeSMikulas Patocka } 186948debafeSMikulas Patocka 187048debafeSMikulas Patocka static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl) 187148debafeSMikulas Patocka { 187248debafeSMikulas Patocka struct wc_entry *e, *f; 187348debafeSMikulas Patocka struct dm_io_region from, to; 187448debafeSMikulas Patocka struct copy_struct *c; 187548debafeSMikulas Patocka 187648debafeSMikulas Patocka while (wbl->size) { 187748debafeSMikulas Patocka unsigned n_sectors; 187848debafeSMikulas Patocka 187948debafeSMikulas Patocka wbl->size--; 188048debafeSMikulas Patocka e = container_of(wbl->list.prev, struct wc_entry, lru); 188148debafeSMikulas Patocka list_del(&e->lru); 188248debafeSMikulas Patocka 188348debafeSMikulas Patocka n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT); 188448debafeSMikulas Patocka 188548debafeSMikulas Patocka from.bdev = wc->ssd_dev->bdev; 188648debafeSMikulas Patocka from.sector = cache_sector(wc, e); 188748debafeSMikulas Patocka from.count = n_sectors; 188848debafeSMikulas Patocka to.bdev = wc->dev->bdev; 188948debafeSMikulas Patocka to.sector = read_original_sector(wc, e); 189048debafeSMikulas Patocka to.count = n_sectors; 189148debafeSMikulas Patocka 189248debafeSMikulas Patocka c = mempool_alloc(&wc->copy_pool, GFP_NOIO); 189348debafeSMikulas Patocka c->wc = wc; 189448debafeSMikulas Patocka c->e = e; 189548debafeSMikulas Patocka c->n_entries = e->wc_list_contiguous; 189648debafeSMikulas Patocka 189748debafeSMikulas Patocka while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) { 189848debafeSMikulas Patocka wbl->size--; 189948debafeSMikulas Patocka f = container_of(wbl->list.prev, struct wc_entry, lru); 190048debafeSMikulas Patocka BUG_ON(f != e + 1); 190148debafeSMikulas Patocka list_del(&f->lru); 190248debafeSMikulas Patocka e = f; 190348debafeSMikulas Patocka } 190448debafeSMikulas Patocka 19054134455fSMikulas Patocka if (unlikely(to.sector + to.count > wc->data_device_sectors)) { 19064134455fSMikulas Patocka if (to.sector >= wc->data_device_sectors) { 19074134455fSMikulas Patocka writecache_copy_endio(0, 0, c); 19084134455fSMikulas Patocka continue; 19094134455fSMikulas Patocka } 19104134455fSMikulas Patocka from.count = to.count = wc->data_device_sectors - to.sector; 19114134455fSMikulas Patocka } 19124134455fSMikulas Patocka 191348debafeSMikulas Patocka dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c); 191448debafeSMikulas Patocka 191548debafeSMikulas Patocka __writeback_throttle(wc, wbl); 191648debafeSMikulas Patocka } 191748debafeSMikulas Patocka } 191848debafeSMikulas Patocka 191948debafeSMikulas Patocka static void writecache_writeback(struct work_struct *work) 192048debafeSMikulas Patocka { 192148debafeSMikulas Patocka struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work); 192248debafeSMikulas Patocka struct blk_plug plug; 19233f649ab7SKees Cook struct wc_entry *f, *g, *e = NULL; 192448debafeSMikulas Patocka struct rb_node *node, *next_node; 192548debafeSMikulas Patocka struct list_head skipped; 192648debafeSMikulas Patocka struct writeback_list wbl; 192748debafeSMikulas Patocka unsigned long n_walked; 192848debafeSMikulas Patocka 1929293128b1SMikulas Patocka if (!WC_MODE_PMEM(wc)) { 1930293128b1SMikulas Patocka /* Wait for any active kcopyd work on behalf of ssd writeback */ 1931293128b1SMikulas Patocka dm_kcopyd_client_flush(wc->dm_kcopyd); 1932293128b1SMikulas Patocka } 1933293128b1SMikulas Patocka 19345c0de3d7SMikulas Patocka if (likely(wc->pause != 0)) { 19355c0de3d7SMikulas Patocka while (1) { 19365c0de3d7SMikulas Patocka unsigned long idle; 19375c0de3d7SMikulas Patocka if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) || 19385c0de3d7SMikulas Patocka unlikely(dm_suspended(wc->ti))) 19395c0de3d7SMikulas Patocka break; 19405c0de3d7SMikulas Patocka idle = dm_iot_idle_time(&wc->iot); 19415c0de3d7SMikulas Patocka if (idle >= wc->pause) 19425c0de3d7SMikulas Patocka break; 19435c0de3d7SMikulas Patocka idle = wc->pause - idle; 19445c0de3d7SMikulas Patocka if (idle > HZ) 19455c0de3d7SMikulas Patocka idle = HZ; 19465c0de3d7SMikulas Patocka schedule_timeout_idle(idle); 194795b88f4dSMikulas Patocka } 194895b88f4dSMikulas Patocka } 194995b88f4dSMikulas Patocka 195048debafeSMikulas Patocka wc_lock(wc); 195148debafeSMikulas Patocka restart: 195248debafeSMikulas Patocka if (writecache_has_error(wc)) { 195348debafeSMikulas Patocka wc_unlock(wc); 195448debafeSMikulas Patocka return; 195548debafeSMikulas Patocka } 195648debafeSMikulas Patocka 195748debafeSMikulas Patocka if (unlikely(wc->writeback_all)) { 195848debafeSMikulas Patocka if (writecache_wait_for_writeback(wc)) 195948debafeSMikulas Patocka goto restart; 196048debafeSMikulas Patocka } 196148debafeSMikulas Patocka 196248debafeSMikulas Patocka if (wc->overwrote_committed) { 196348debafeSMikulas Patocka writecache_wait_for_ios(wc, WRITE); 196448debafeSMikulas Patocka } 196548debafeSMikulas Patocka 196648debafeSMikulas Patocka n_walked = 0; 196748debafeSMikulas Patocka INIT_LIST_HEAD(&skipped); 196848debafeSMikulas Patocka INIT_LIST_HEAD(&wbl.list); 196948debafeSMikulas Patocka wbl.size = 0; 197048debafeSMikulas Patocka while (!list_empty(&wc->lru) && 197148debafeSMikulas Patocka (wc->writeback_all || 19723923d485SMikulas Patocka wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark || 19733923d485SMikulas Patocka (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >= 19743923d485SMikulas Patocka wc->max_age - wc->max_age / MAX_AGE_DIV))) { 197548debafeSMikulas Patocka 197648debafeSMikulas Patocka n_walked++; 197748debafeSMikulas Patocka if (unlikely(n_walked > WRITEBACK_LATENCY) && 1978af4f6cabSMikulas Patocka likely(!wc->writeback_all)) { 1979af4f6cabSMikulas Patocka if (likely(!dm_suspended(wc->ti))) 198048debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 198148debafeSMikulas Patocka break; 198248debafeSMikulas Patocka } 198348debafeSMikulas Patocka 19845229b489SHuaisheng Ye if (unlikely(wc->writeback_all)) { 19855229b489SHuaisheng Ye if (unlikely(!e)) { 19865229b489SHuaisheng Ye writecache_flush(wc); 19875229b489SHuaisheng Ye e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node); 19885229b489SHuaisheng Ye } else 19895229b489SHuaisheng Ye e = g; 19905229b489SHuaisheng Ye } else 199148debafeSMikulas Patocka e = container_of(wc->lru.prev, struct wc_entry, lru); 199248debafeSMikulas Patocka BUG_ON(e->write_in_progress); 199348debafeSMikulas Patocka if (unlikely(!writecache_entry_is_committed(wc, e))) { 199448debafeSMikulas Patocka writecache_flush(wc); 199548debafeSMikulas Patocka } 199648debafeSMikulas Patocka node = rb_prev(&e->rb_node); 199748debafeSMikulas Patocka if (node) { 199848debafeSMikulas Patocka f = container_of(node, struct wc_entry, rb_node); 199948debafeSMikulas Patocka if (unlikely(read_original_sector(wc, f) == 200048debafeSMikulas Patocka read_original_sector(wc, e))) { 200148debafeSMikulas Patocka BUG_ON(!f->write_in_progress); 20028c77f1cbSBaokun Li list_move(&e->lru, &skipped); 200348debafeSMikulas Patocka cond_resched(); 200448debafeSMikulas Patocka continue; 200548debafeSMikulas Patocka } 200648debafeSMikulas Patocka } 200748debafeSMikulas Patocka wc->writeback_size++; 20088c77f1cbSBaokun Li list_move(&e->lru, &wbl.list); 200948debafeSMikulas Patocka wbl.size++; 201048debafeSMikulas Patocka e->write_in_progress = true; 201148debafeSMikulas Patocka e->wc_list_contiguous = 1; 201248debafeSMikulas Patocka 201348debafeSMikulas Patocka f = e; 201448debafeSMikulas Patocka 201548debafeSMikulas Patocka while (1) { 201648debafeSMikulas Patocka next_node = rb_next(&f->rb_node); 201748debafeSMikulas Patocka if (unlikely(!next_node)) 201848debafeSMikulas Patocka break; 201948debafeSMikulas Patocka g = container_of(next_node, struct wc_entry, rb_node); 202062421b38SHuaisheng Ye if (unlikely(read_original_sector(wc, g) == 202162421b38SHuaisheng Ye read_original_sector(wc, f))) { 202248debafeSMikulas Patocka f = g; 202348debafeSMikulas Patocka continue; 202448debafeSMikulas Patocka } 202548debafeSMikulas Patocka if (read_original_sector(wc, g) != 202648debafeSMikulas Patocka read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT)) 202748debafeSMikulas Patocka break; 202848debafeSMikulas Patocka if (unlikely(g->write_in_progress)) 202948debafeSMikulas Patocka break; 203048debafeSMikulas Patocka if (unlikely(!writecache_entry_is_committed(wc, g))) 203148debafeSMikulas Patocka break; 203248debafeSMikulas Patocka 203348debafeSMikulas Patocka if (!WC_MODE_PMEM(wc)) { 203448debafeSMikulas Patocka if (g != f + 1) 203548debafeSMikulas Patocka break; 203648debafeSMikulas Patocka } 203748debafeSMikulas Patocka 203848debafeSMikulas Patocka n_walked++; 203948debafeSMikulas Patocka //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all)) 204048debafeSMikulas Patocka // break; 204148debafeSMikulas Patocka 204248debafeSMikulas Patocka wc->writeback_size++; 20438c77f1cbSBaokun Li list_move(&g->lru, &wbl.list); 204448debafeSMikulas Patocka wbl.size++; 204548debafeSMikulas Patocka g->write_in_progress = true; 2046a8affc03SChristoph Hellwig g->wc_list_contiguous = BIO_MAX_VECS; 204748debafeSMikulas Patocka f = g; 204848debafeSMikulas Patocka e->wc_list_contiguous++; 2049a8affc03SChristoph Hellwig if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) { 20505229b489SHuaisheng Ye if (unlikely(wc->writeback_all)) { 20515229b489SHuaisheng Ye next_node = rb_next(&f->rb_node); 20525229b489SHuaisheng Ye if (likely(next_node)) 20535229b489SHuaisheng Ye g = container_of(next_node, struct wc_entry, rb_node); 20545229b489SHuaisheng Ye } 205548debafeSMikulas Patocka break; 205648debafeSMikulas Patocka } 20575229b489SHuaisheng Ye } 205848debafeSMikulas Patocka cond_resched(); 205948debafeSMikulas Patocka } 206048debafeSMikulas Patocka 206148debafeSMikulas Patocka if (!list_empty(&skipped)) { 206248debafeSMikulas Patocka list_splice_tail(&skipped, &wc->lru); 206348debafeSMikulas Patocka /* 206448debafeSMikulas Patocka * If we didn't do any progress, we must wait until some 206548debafeSMikulas Patocka * writeback finishes to avoid burning CPU in a loop 206648debafeSMikulas Patocka */ 206748debafeSMikulas Patocka if (unlikely(!wbl.size)) 206848debafeSMikulas Patocka writecache_wait_for_writeback(wc); 206948debafeSMikulas Patocka } 207048debafeSMikulas Patocka 207148debafeSMikulas Patocka wc_unlock(wc); 207248debafeSMikulas Patocka 207348debafeSMikulas Patocka blk_start_plug(&plug); 207448debafeSMikulas Patocka 207548debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 207648debafeSMikulas Patocka __writecache_writeback_pmem(wc, &wbl); 207748debafeSMikulas Patocka else 207848debafeSMikulas Patocka __writecache_writeback_ssd(wc, &wbl); 207948debafeSMikulas Patocka 208048debafeSMikulas Patocka blk_finish_plug(&plug); 208148debafeSMikulas Patocka 208248debafeSMikulas Patocka if (unlikely(wc->writeback_all)) { 208348debafeSMikulas Patocka wc_lock(wc); 208448debafeSMikulas Patocka while (writecache_wait_for_writeback(wc)); 208548debafeSMikulas Patocka wc_unlock(wc); 208648debafeSMikulas Patocka } 208748debafeSMikulas Patocka } 208848debafeSMikulas Patocka 208948debafeSMikulas Patocka static int calculate_memory_size(uint64_t device_size, unsigned block_size, 209048debafeSMikulas Patocka size_t *n_blocks_p, size_t *n_metadata_blocks_p) 209148debafeSMikulas Patocka { 209248debafeSMikulas Patocka uint64_t n_blocks, offset; 209348debafeSMikulas Patocka struct wc_entry e; 209448debafeSMikulas Patocka 209548debafeSMikulas Patocka n_blocks = device_size; 209648debafeSMikulas Patocka do_div(n_blocks, block_size + sizeof(struct wc_memory_entry)); 209748debafeSMikulas Patocka 209848debafeSMikulas Patocka while (1) { 209948debafeSMikulas Patocka if (!n_blocks) 210048debafeSMikulas Patocka return -ENOSPC; 210148debafeSMikulas Patocka /* Verify the following entries[n_blocks] won't overflow */ 210248debafeSMikulas Patocka if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) / 210348debafeSMikulas Patocka sizeof(struct wc_memory_entry))) 210448debafeSMikulas Patocka return -EFBIG; 210548debafeSMikulas Patocka offset = offsetof(struct wc_memory_superblock, entries[n_blocks]); 210648debafeSMikulas Patocka offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1); 210748debafeSMikulas Patocka if (offset + n_blocks * block_size <= device_size) 210848debafeSMikulas Patocka break; 210948debafeSMikulas Patocka n_blocks--; 211048debafeSMikulas Patocka } 211148debafeSMikulas Patocka 211248debafeSMikulas Patocka /* check if the bit field overflows */ 211348debafeSMikulas Patocka e.index = n_blocks; 211448debafeSMikulas Patocka if (e.index != n_blocks) 211548debafeSMikulas Patocka return -EFBIG; 211648debafeSMikulas Patocka 211748debafeSMikulas Patocka if (n_blocks_p) 211848debafeSMikulas Patocka *n_blocks_p = n_blocks; 211948debafeSMikulas Patocka if (n_metadata_blocks_p) 212048debafeSMikulas Patocka *n_metadata_blocks_p = offset >> __ffs(block_size); 212148debafeSMikulas Patocka return 0; 212248debafeSMikulas Patocka } 212348debafeSMikulas Patocka 212448debafeSMikulas Patocka static int init_memory(struct dm_writecache *wc) 212548debafeSMikulas Patocka { 212648debafeSMikulas Patocka size_t b; 212748debafeSMikulas Patocka int r; 212848debafeSMikulas Patocka 212948debafeSMikulas Patocka r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL); 213048debafeSMikulas Patocka if (r) 213148debafeSMikulas Patocka return r; 213248debafeSMikulas Patocka 213348debafeSMikulas Patocka r = writecache_alloc_entries(wc); 213448debafeSMikulas Patocka if (r) 213548debafeSMikulas Patocka return r; 213648debafeSMikulas Patocka 213748debafeSMikulas Patocka for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++) 213848debafeSMikulas Patocka pmem_assign(sb(wc)->padding[b], cpu_to_le64(0)); 213948debafeSMikulas Patocka pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION)); 214048debafeSMikulas Patocka pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size)); 214148debafeSMikulas Patocka pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks)); 214248debafeSMikulas Patocka pmem_assign(sb(wc)->seq_count, cpu_to_le64(0)); 214348debafeSMikulas Patocka 21441edaa447SMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 214548debafeSMikulas Patocka write_original_sector_seq_count(wc, &wc->entries[b], -1, -1); 21461edaa447SMikulas Patocka cond_resched(); 21471edaa447SMikulas Patocka } 214848debafeSMikulas Patocka 214948debafeSMikulas Patocka writecache_flush_all_metadata(wc); 2150aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 215148debafeSMikulas Patocka pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC)); 215248debafeSMikulas Patocka writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic); 2153aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 215448debafeSMikulas Patocka 215548debafeSMikulas Patocka return 0; 215648debafeSMikulas Patocka } 215748debafeSMikulas Patocka 215848debafeSMikulas Patocka static void writecache_dtr(struct dm_target *ti) 215948debafeSMikulas Patocka { 216048debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 216148debafeSMikulas Patocka 216248debafeSMikulas Patocka if (!wc) 216348debafeSMikulas Patocka return; 216448debafeSMikulas Patocka 216548debafeSMikulas Patocka if (wc->endio_thread) 216648debafeSMikulas Patocka kthread_stop(wc->endio_thread); 216748debafeSMikulas Patocka 216848debafeSMikulas Patocka if (wc->flush_thread) 216948debafeSMikulas Patocka kthread_stop(wc->flush_thread); 217048debafeSMikulas Patocka 217148debafeSMikulas Patocka bioset_exit(&wc->bio_set); 217248debafeSMikulas Patocka 217348debafeSMikulas Patocka mempool_exit(&wc->copy_pool); 217448debafeSMikulas Patocka 217548debafeSMikulas Patocka if (wc->writeback_wq) 217648debafeSMikulas Patocka destroy_workqueue(wc->writeback_wq); 217748debafeSMikulas Patocka 217848debafeSMikulas Patocka if (wc->dev) 217948debafeSMikulas Patocka dm_put_device(ti, wc->dev); 218048debafeSMikulas Patocka 218148debafeSMikulas Patocka if (wc->ssd_dev) 218248debafeSMikulas Patocka dm_put_device(ti, wc->ssd_dev); 218348debafeSMikulas Patocka 218448debafeSMikulas Patocka vfree(wc->entries); 218548debafeSMikulas Patocka 218648debafeSMikulas Patocka if (wc->memory_map) { 218748debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 218848debafeSMikulas Patocka persistent_memory_release(wc); 218948debafeSMikulas Patocka else 219048debafeSMikulas Patocka vfree(wc->memory_map); 219148debafeSMikulas Patocka } 219248debafeSMikulas Patocka 219348debafeSMikulas Patocka if (wc->dm_kcopyd) 219448debafeSMikulas Patocka dm_kcopyd_client_destroy(wc->dm_kcopyd); 219548debafeSMikulas Patocka 219648debafeSMikulas Patocka if (wc->dm_io) 219748debafeSMikulas Patocka dm_io_client_destroy(wc->dm_io); 219848debafeSMikulas Patocka 219948debafeSMikulas Patocka vfree(wc->dirty_bitmap); 220048debafeSMikulas Patocka 220148debafeSMikulas Patocka kfree(wc); 220248debafeSMikulas Patocka } 220348debafeSMikulas Patocka 220448debafeSMikulas Patocka static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv) 220548debafeSMikulas Patocka { 220648debafeSMikulas Patocka struct dm_writecache *wc; 220748debafeSMikulas Patocka struct dm_arg_set as; 220848debafeSMikulas Patocka const char *string; 220948debafeSMikulas Patocka unsigned opt_params; 221048debafeSMikulas Patocka size_t offset, data_size; 221148debafeSMikulas Patocka int i, r; 221248debafeSMikulas Patocka char dummy; 221348debafeSMikulas Patocka int high_wm_percent = HIGH_WATERMARK; 221448debafeSMikulas Patocka int low_wm_percent = LOW_WATERMARK; 221548debafeSMikulas Patocka uint64_t x; 221648debafeSMikulas Patocka struct wc_memory_superblock s; 221748debafeSMikulas Patocka 221848debafeSMikulas Patocka static struct dm_arg _args[] = { 22195c0de3d7SMikulas Patocka {0, 18, "Invalid number of feature args"}, 222048debafeSMikulas Patocka }; 222148debafeSMikulas Patocka 222248debafeSMikulas Patocka as.argc = argc; 222348debafeSMikulas Patocka as.argv = argv; 222448debafeSMikulas Patocka 222548debafeSMikulas Patocka wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL); 222648debafeSMikulas Patocka if (!wc) { 222748debafeSMikulas Patocka ti->error = "Cannot allocate writecache structure"; 222848debafeSMikulas Patocka r = -ENOMEM; 222948debafeSMikulas Patocka goto bad; 223048debafeSMikulas Patocka } 223148debafeSMikulas Patocka ti->private = wc; 223248debafeSMikulas Patocka wc->ti = ti; 223348debafeSMikulas Patocka 223448debafeSMikulas Patocka mutex_init(&wc->lock); 22353923d485SMikulas Patocka wc->max_age = MAX_AGE_UNSPECIFIED; 223648debafeSMikulas Patocka writecache_poison_lists(wc); 223748debafeSMikulas Patocka init_waitqueue_head(&wc->freelist_wait); 223848debafeSMikulas Patocka timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0); 22393923d485SMikulas Patocka timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0); 224048debafeSMikulas Patocka 224148debafeSMikulas Patocka for (i = 0; i < 2; i++) { 224248debafeSMikulas Patocka atomic_set(&wc->bio_in_progress[i], 0); 224348debafeSMikulas Patocka init_waitqueue_head(&wc->bio_in_progress_wait[i]); 224448debafeSMikulas Patocka } 224548debafeSMikulas Patocka 224648debafeSMikulas Patocka wc->dm_io = dm_io_client_create(); 224748debafeSMikulas Patocka if (IS_ERR(wc->dm_io)) { 224848debafeSMikulas Patocka r = PTR_ERR(wc->dm_io); 224948debafeSMikulas Patocka ti->error = "Unable to allocate dm-io client"; 225048debafeSMikulas Patocka wc->dm_io = NULL; 225148debafeSMikulas Patocka goto bad; 225248debafeSMikulas Patocka } 225348debafeSMikulas Patocka 2254f87e033bSHuaisheng Ye wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1); 225548debafeSMikulas Patocka if (!wc->writeback_wq) { 225648debafeSMikulas Patocka r = -ENOMEM; 225748debafeSMikulas Patocka ti->error = "Could not allocate writeback workqueue"; 225848debafeSMikulas Patocka goto bad; 225948debafeSMikulas Patocka } 226048debafeSMikulas Patocka INIT_WORK(&wc->writeback_work, writecache_writeback); 226148debafeSMikulas Patocka INIT_WORK(&wc->flush_work, writecache_flush_work); 226248debafeSMikulas Patocka 226395b88f4dSMikulas Patocka dm_iot_init(&wc->iot); 226495b88f4dSMikulas Patocka 226548debafeSMikulas Patocka raw_spin_lock_init(&wc->endio_list_lock); 226648debafeSMikulas Patocka INIT_LIST_HEAD(&wc->endio_list); 226748debafeSMikulas Patocka wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio"); 226848debafeSMikulas Patocka if (IS_ERR(wc->endio_thread)) { 226948debafeSMikulas Patocka r = PTR_ERR(wc->endio_thread); 227048debafeSMikulas Patocka wc->endio_thread = NULL; 227148debafeSMikulas Patocka ti->error = "Couldn't spawn endio thread"; 227248debafeSMikulas Patocka goto bad; 227348debafeSMikulas Patocka } 227448debafeSMikulas Patocka wake_up_process(wc->endio_thread); 227548debafeSMikulas Patocka 227648debafeSMikulas Patocka /* 227748debafeSMikulas Patocka * Parse the mode (pmem or ssd) 227848debafeSMikulas Patocka */ 227948debafeSMikulas Patocka string = dm_shift_arg(&as); 228048debafeSMikulas Patocka if (!string) 228148debafeSMikulas Patocka goto bad_arguments; 228248debafeSMikulas Patocka 228348debafeSMikulas Patocka if (!strcasecmp(string, "s")) { 228448debafeSMikulas Patocka wc->pmem_mode = false; 228548debafeSMikulas Patocka } else if (!strcasecmp(string, "p")) { 228648debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM 228748debafeSMikulas Patocka wc->pmem_mode = true; 228848debafeSMikulas Patocka wc->writeback_fua = true; 228948debafeSMikulas Patocka #else 229048debafeSMikulas Patocka /* 229148debafeSMikulas Patocka * If the architecture doesn't support persistent memory or 229248debafeSMikulas Patocka * the kernel doesn't support any DAX drivers, this driver can 229348debafeSMikulas Patocka * only be used in SSD-only mode. 229448debafeSMikulas Patocka */ 229548debafeSMikulas Patocka r = -EOPNOTSUPP; 229648debafeSMikulas Patocka ti->error = "Persistent memory or DAX not supported on this system"; 229748debafeSMikulas Patocka goto bad; 229848debafeSMikulas Patocka #endif 229948debafeSMikulas Patocka } else { 230048debafeSMikulas Patocka goto bad_arguments; 230148debafeSMikulas Patocka } 230248debafeSMikulas Patocka 230348debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 230448debafeSMikulas Patocka r = bioset_init(&wc->bio_set, BIO_POOL_SIZE, 230548debafeSMikulas Patocka offsetof(struct writeback_struct, bio), 230648debafeSMikulas Patocka BIOSET_NEED_BVECS); 230748debafeSMikulas Patocka if (r) { 230848debafeSMikulas Patocka ti->error = "Could not allocate bio set"; 230948debafeSMikulas Patocka goto bad; 231048debafeSMikulas Patocka } 231148debafeSMikulas Patocka } else { 23125c0de3d7SMikulas Patocka wc->pause = PAUSE_WRITEBACK; 231348debafeSMikulas Patocka r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct)); 231448debafeSMikulas Patocka if (r) { 231548debafeSMikulas Patocka ti->error = "Could not allocate mempool"; 231648debafeSMikulas Patocka goto bad; 231748debafeSMikulas Patocka } 231848debafeSMikulas Patocka } 231948debafeSMikulas Patocka 232048debafeSMikulas Patocka /* 232148debafeSMikulas Patocka * Parse the origin data device 232248debafeSMikulas Patocka */ 232348debafeSMikulas Patocka string = dm_shift_arg(&as); 232448debafeSMikulas Patocka if (!string) 232548debafeSMikulas Patocka goto bad_arguments; 232648debafeSMikulas Patocka r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev); 232748debafeSMikulas Patocka if (r) { 232848debafeSMikulas Patocka ti->error = "Origin data device lookup failed"; 232948debafeSMikulas Patocka goto bad; 233048debafeSMikulas Patocka } 233148debafeSMikulas Patocka 233248debafeSMikulas Patocka /* 233348debafeSMikulas Patocka * Parse cache data device (be it pmem or ssd) 233448debafeSMikulas Patocka */ 233548debafeSMikulas Patocka string = dm_shift_arg(&as); 233648debafeSMikulas Patocka if (!string) 233748debafeSMikulas Patocka goto bad_arguments; 233848debafeSMikulas Patocka 233948debafeSMikulas Patocka r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev); 234048debafeSMikulas Patocka if (r) { 234148debafeSMikulas Patocka ti->error = "Cache data device lookup failed"; 234248debafeSMikulas Patocka goto bad; 234348debafeSMikulas Patocka } 234448debafeSMikulas Patocka wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode); 234548debafeSMikulas Patocka 234648debafeSMikulas Patocka /* 234748debafeSMikulas Patocka * Parse the cache block size 234848debafeSMikulas Patocka */ 234948debafeSMikulas Patocka string = dm_shift_arg(&as); 235048debafeSMikulas Patocka if (!string) 235148debafeSMikulas Patocka goto bad_arguments; 235248debafeSMikulas Patocka if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 || 235348debafeSMikulas Patocka wc->block_size < 512 || wc->block_size > PAGE_SIZE || 235448debafeSMikulas Patocka (wc->block_size & (wc->block_size - 1))) { 235548debafeSMikulas Patocka r = -EINVAL; 235648debafeSMikulas Patocka ti->error = "Invalid block size"; 235748debafeSMikulas Patocka goto bad; 235848debafeSMikulas Patocka } 235931b22120SMikulas Patocka if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) || 236031b22120SMikulas Patocka wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) { 236131b22120SMikulas Patocka r = -EINVAL; 236231b22120SMikulas Patocka ti->error = "Block size is smaller than device logical block size"; 236331b22120SMikulas Patocka goto bad; 236431b22120SMikulas Patocka } 236548debafeSMikulas Patocka wc->block_size_bits = __ffs(wc->block_size); 236648debafeSMikulas Patocka 236748debafeSMikulas Patocka wc->max_writeback_jobs = MAX_WRITEBACK_JOBS; 236848debafeSMikulas Patocka wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM; 236948debafeSMikulas Patocka wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC); 237048debafeSMikulas Patocka 237148debafeSMikulas Patocka /* 237248debafeSMikulas Patocka * Parse optional arguments 237348debafeSMikulas Patocka */ 237448debafeSMikulas Patocka r = dm_read_arg_group(_args, &as, &opt_params, &ti->error); 237548debafeSMikulas Patocka if (r) 237648debafeSMikulas Patocka goto bad; 237748debafeSMikulas Patocka 237848debafeSMikulas Patocka while (opt_params) { 237948debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 2380d284f824SMikulas Patocka if (!strcasecmp(string, "start_sector") && opt_params >= 1) { 2381d284f824SMikulas Patocka unsigned long long start_sector; 2382d284f824SMikulas Patocka string = dm_shift_arg(&as), opt_params--; 2383d284f824SMikulas Patocka if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1) 2384d284f824SMikulas Patocka goto invalid_optional; 2385d284f824SMikulas Patocka wc->start_sector = start_sector; 2386054bee16SMikulas Patocka wc->start_sector_set = true; 2387d284f824SMikulas Patocka if (wc->start_sector != start_sector || 2388d284f824SMikulas Patocka wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT) 2389d284f824SMikulas Patocka goto invalid_optional; 2390d284f824SMikulas Patocka } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) { 239148debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 239248debafeSMikulas Patocka if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1) 239348debafeSMikulas Patocka goto invalid_optional; 239448debafeSMikulas Patocka if (high_wm_percent < 0 || high_wm_percent > 100) 239548debafeSMikulas Patocka goto invalid_optional; 2396054bee16SMikulas Patocka wc->high_wm_percent_value = high_wm_percent; 239748debafeSMikulas Patocka wc->high_wm_percent_set = true; 239848debafeSMikulas Patocka } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) { 239948debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 240048debafeSMikulas Patocka if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1) 240148debafeSMikulas Patocka goto invalid_optional; 240248debafeSMikulas Patocka if (low_wm_percent < 0 || low_wm_percent > 100) 240348debafeSMikulas Patocka goto invalid_optional; 2404054bee16SMikulas Patocka wc->low_wm_percent_value = low_wm_percent; 240548debafeSMikulas Patocka wc->low_wm_percent_set = true; 240648debafeSMikulas Patocka } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) { 240748debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 240848debafeSMikulas Patocka if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1) 240948debafeSMikulas Patocka goto invalid_optional; 241048debafeSMikulas Patocka wc->max_writeback_jobs_set = true; 241148debafeSMikulas Patocka } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) { 241248debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 241348debafeSMikulas Patocka if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1) 241448debafeSMikulas Patocka goto invalid_optional; 241548debafeSMikulas Patocka wc->autocommit_blocks_set = true; 241648debafeSMikulas Patocka } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) { 241748debafeSMikulas Patocka unsigned autocommit_msecs; 241848debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 241948debafeSMikulas Patocka if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1) 242048debafeSMikulas Patocka goto invalid_optional; 242148debafeSMikulas Patocka if (autocommit_msecs > 3600000) 242248debafeSMikulas Patocka goto invalid_optional; 242348debafeSMikulas Patocka wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs); 2424054bee16SMikulas Patocka wc->autocommit_time_value = autocommit_msecs; 242548debafeSMikulas Patocka wc->autocommit_time_set = true; 24263923d485SMikulas Patocka } else if (!strcasecmp(string, "max_age") && opt_params >= 1) { 24273923d485SMikulas Patocka unsigned max_age_msecs; 24283923d485SMikulas Patocka string = dm_shift_arg(&as), opt_params--; 24293923d485SMikulas Patocka if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1) 24303923d485SMikulas Patocka goto invalid_optional; 24313923d485SMikulas Patocka if (max_age_msecs > 86400000) 24323923d485SMikulas Patocka goto invalid_optional; 24333923d485SMikulas Patocka wc->max_age = msecs_to_jiffies(max_age_msecs); 2434054bee16SMikulas Patocka wc->max_age_set = true; 2435054bee16SMikulas Patocka wc->max_age_value = max_age_msecs; 243693de44ebSMikulas Patocka } else if (!strcasecmp(string, "cleaner")) { 2437054bee16SMikulas Patocka wc->cleaner_set = true; 243893de44ebSMikulas Patocka wc->cleaner = true; 243948debafeSMikulas Patocka } else if (!strcasecmp(string, "fua")) { 244048debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 244148debafeSMikulas Patocka wc->writeback_fua = true; 244248debafeSMikulas Patocka wc->writeback_fua_set = true; 244348debafeSMikulas Patocka } else goto invalid_optional; 244448debafeSMikulas Patocka } else if (!strcasecmp(string, "nofua")) { 244548debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 244648debafeSMikulas Patocka wc->writeback_fua = false; 244748debafeSMikulas Patocka wc->writeback_fua_set = true; 244848debafeSMikulas Patocka } else goto invalid_optional; 2449611c3e16SMikulas Patocka } else if (!strcasecmp(string, "metadata_only")) { 2450611c3e16SMikulas Patocka wc->metadata_only = true; 24515c0de3d7SMikulas Patocka } else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) { 24525c0de3d7SMikulas Patocka unsigned pause_msecs; 24535c0de3d7SMikulas Patocka if (WC_MODE_PMEM(wc)) 24545c0de3d7SMikulas Patocka goto invalid_optional; 24555c0de3d7SMikulas Patocka string = dm_shift_arg(&as), opt_params--; 24565c0de3d7SMikulas Patocka if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1) 24575c0de3d7SMikulas Patocka goto invalid_optional; 24585c0de3d7SMikulas Patocka if (pause_msecs > 60000) 24595c0de3d7SMikulas Patocka goto invalid_optional; 24605c0de3d7SMikulas Patocka wc->pause = msecs_to_jiffies(pause_msecs); 24615c0de3d7SMikulas Patocka wc->pause_set = true; 24625c0de3d7SMikulas Patocka wc->pause_value = pause_msecs; 246348debafeSMikulas Patocka } else { 246448debafeSMikulas Patocka invalid_optional: 246548debafeSMikulas Patocka r = -EINVAL; 246648debafeSMikulas Patocka ti->error = "Invalid optional argument"; 246748debafeSMikulas Patocka goto bad; 246848debafeSMikulas Patocka } 246948debafeSMikulas Patocka } 247048debafeSMikulas Patocka 247148debafeSMikulas Patocka if (high_wm_percent < low_wm_percent) { 247248debafeSMikulas Patocka r = -EINVAL; 247348debafeSMikulas Patocka ti->error = "High watermark must be greater than or equal to low watermark"; 247448debafeSMikulas Patocka goto bad; 247548debafeSMikulas Patocka } 247648debafeSMikulas Patocka 2477d284f824SMikulas Patocka if (WC_MODE_PMEM(wc)) { 2478a4662458SMichal Suchanek if (!dax_synchronous(wc->ssd_dev->dax_dev)) { 2479a4662458SMichal Suchanek r = -EOPNOTSUPP; 2480a4662458SMichal Suchanek ti->error = "Asynchronous persistent memory not supported as pmem cache"; 2481a4662458SMichal Suchanek goto bad; 2482a4662458SMichal Suchanek } 2483a4662458SMichal Suchanek 2484d284f824SMikulas Patocka r = persistent_memory_claim(wc); 2485d284f824SMikulas Patocka if (r) { 2486d284f824SMikulas Patocka ti->error = "Unable to map persistent memory for cache"; 2487d284f824SMikulas Patocka goto bad; 2488d284f824SMikulas Patocka } 2489d284f824SMikulas Patocka } else { 249048debafeSMikulas Patocka size_t n_blocks, n_metadata_blocks; 249148debafeSMikulas Patocka uint64_t n_bitmap_bits; 249248debafeSMikulas Patocka 2493d284f824SMikulas Patocka wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT; 2494d284f824SMikulas Patocka 249548debafeSMikulas Patocka bio_list_init(&wc->flush_list); 249648debafeSMikulas Patocka wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush"); 249748debafeSMikulas Patocka if (IS_ERR(wc->flush_thread)) { 249848debafeSMikulas Patocka r = PTR_ERR(wc->flush_thread); 249948debafeSMikulas Patocka wc->flush_thread = NULL; 2500e8ea141aSShenghui Wang ti->error = "Couldn't spawn flush thread"; 250148debafeSMikulas Patocka goto bad; 250248debafeSMikulas Patocka } 250348debafeSMikulas Patocka wake_up_process(wc->flush_thread); 250448debafeSMikulas Patocka 250548debafeSMikulas Patocka r = calculate_memory_size(wc->memory_map_size, wc->block_size, 250648debafeSMikulas Patocka &n_blocks, &n_metadata_blocks); 250748debafeSMikulas Patocka if (r) { 250848debafeSMikulas Patocka ti->error = "Invalid device size"; 250948debafeSMikulas Patocka goto bad; 251048debafeSMikulas Patocka } 251148debafeSMikulas Patocka 251248debafeSMikulas Patocka n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) + 251348debafeSMikulas Patocka BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY; 251448debafeSMikulas Patocka /* this is limitation of test_bit functions */ 251548debafeSMikulas Patocka if (n_bitmap_bits > 1U << 31) { 251648debafeSMikulas Patocka r = -EFBIG; 251748debafeSMikulas Patocka ti->error = "Invalid device size"; 251848debafeSMikulas Patocka goto bad; 251948debafeSMikulas Patocka } 252048debafeSMikulas Patocka 252148debafeSMikulas Patocka wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits); 252248debafeSMikulas Patocka if (!wc->memory_map) { 252348debafeSMikulas Patocka r = -ENOMEM; 252448debafeSMikulas Patocka ti->error = "Unable to allocate memory for metadata"; 252548debafeSMikulas Patocka goto bad; 252648debafeSMikulas Patocka } 252748debafeSMikulas Patocka 252848debafeSMikulas Patocka wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle); 252948debafeSMikulas Patocka if (IS_ERR(wc->dm_kcopyd)) { 253048debafeSMikulas Patocka r = PTR_ERR(wc->dm_kcopyd); 253148debafeSMikulas Patocka ti->error = "Unable to allocate dm-kcopyd client"; 253248debafeSMikulas Patocka wc->dm_kcopyd = NULL; 253348debafeSMikulas Patocka goto bad; 253448debafeSMikulas Patocka } 253548debafeSMikulas Patocka 253648debafeSMikulas Patocka wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT); 253748debafeSMikulas Patocka wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) / 253848debafeSMikulas Patocka BITS_PER_LONG * sizeof(unsigned long); 253948debafeSMikulas Patocka wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size); 254048debafeSMikulas Patocka if (!wc->dirty_bitmap) { 254148debafeSMikulas Patocka r = -ENOMEM; 254248debafeSMikulas Patocka ti->error = "Unable to allocate dirty bitmap"; 254348debafeSMikulas Patocka goto bad; 254448debafeSMikulas Patocka } 254548debafeSMikulas Patocka 254631b22120SMikulas Patocka r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT); 254748debafeSMikulas Patocka if (r) { 254831b22120SMikulas Patocka ti->error = "Unable to read first block of metadata"; 254948debafeSMikulas Patocka goto bad; 255048debafeSMikulas Patocka } 255148debafeSMikulas Patocka } 255248debafeSMikulas Patocka 2553ec6347bbSDan Williams r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock)); 255448debafeSMikulas Patocka if (r) { 255548debafeSMikulas Patocka ti->error = "Hardware memory error when reading superblock"; 255648debafeSMikulas Patocka goto bad; 255748debafeSMikulas Patocka } 255848debafeSMikulas Patocka if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) { 255948debafeSMikulas Patocka r = init_memory(wc); 256048debafeSMikulas Patocka if (r) { 256148debafeSMikulas Patocka ti->error = "Unable to initialize device"; 256248debafeSMikulas Patocka goto bad; 256348debafeSMikulas Patocka } 2564ec6347bbSDan Williams r = copy_mc_to_kernel(&s, sb(wc), 2565ec6347bbSDan Williams sizeof(struct wc_memory_superblock)); 256648debafeSMikulas Patocka if (r) { 256748debafeSMikulas Patocka ti->error = "Hardware memory error when reading superblock"; 256848debafeSMikulas Patocka goto bad; 256948debafeSMikulas Patocka } 257048debafeSMikulas Patocka } 257148debafeSMikulas Patocka 257248debafeSMikulas Patocka if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) { 257348debafeSMikulas Patocka ti->error = "Invalid magic in the superblock"; 257448debafeSMikulas Patocka r = -EINVAL; 257548debafeSMikulas Patocka goto bad; 257648debafeSMikulas Patocka } 257748debafeSMikulas Patocka 257848debafeSMikulas Patocka if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) { 257948debafeSMikulas Patocka ti->error = "Invalid version in the superblock"; 258048debafeSMikulas Patocka r = -EINVAL; 258148debafeSMikulas Patocka goto bad; 258248debafeSMikulas Patocka } 258348debafeSMikulas Patocka 258448debafeSMikulas Patocka if (le32_to_cpu(s.block_size) != wc->block_size) { 258548debafeSMikulas Patocka ti->error = "Block size does not match superblock"; 258648debafeSMikulas Patocka r = -EINVAL; 258748debafeSMikulas Patocka goto bad; 258848debafeSMikulas Patocka } 258948debafeSMikulas Patocka 259048debafeSMikulas Patocka wc->n_blocks = le64_to_cpu(s.n_blocks); 259148debafeSMikulas Patocka 259248debafeSMikulas Patocka offset = wc->n_blocks * sizeof(struct wc_memory_entry); 259348debafeSMikulas Patocka if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) { 259448debafeSMikulas Patocka overflow: 259548debafeSMikulas Patocka ti->error = "Overflow in size calculation"; 259648debafeSMikulas Patocka r = -EINVAL; 259748debafeSMikulas Patocka goto bad; 259848debafeSMikulas Patocka } 259948debafeSMikulas Patocka offset += sizeof(struct wc_memory_superblock); 260048debafeSMikulas Patocka if (offset < sizeof(struct wc_memory_superblock)) 260148debafeSMikulas Patocka goto overflow; 260248debafeSMikulas Patocka offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1); 260348debafeSMikulas Patocka data_size = wc->n_blocks * (size_t)wc->block_size; 260448debafeSMikulas Patocka if (!offset || (data_size / wc->block_size != wc->n_blocks) || 260548debafeSMikulas Patocka (offset + data_size < offset)) 260648debafeSMikulas Patocka goto overflow; 260748debafeSMikulas Patocka if (offset + data_size > wc->memory_map_size) { 260848debafeSMikulas Patocka ti->error = "Memory area is too small"; 260948debafeSMikulas Patocka r = -EINVAL; 261048debafeSMikulas Patocka goto bad; 261148debafeSMikulas Patocka } 261248debafeSMikulas Patocka 261348debafeSMikulas Patocka wc->metadata_sectors = offset >> SECTOR_SHIFT; 261448debafeSMikulas Patocka wc->block_start = (char *)sb(wc) + offset; 261548debafeSMikulas Patocka 261648debafeSMikulas Patocka x = (uint64_t)wc->n_blocks * (100 - high_wm_percent); 261748debafeSMikulas Patocka x += 50; 261848debafeSMikulas Patocka do_div(x, 100); 261948debafeSMikulas Patocka wc->freelist_high_watermark = x; 262048debafeSMikulas Patocka x = (uint64_t)wc->n_blocks * (100 - low_wm_percent); 262148debafeSMikulas Patocka x += 50; 262248debafeSMikulas Patocka do_div(x, 100); 262348debafeSMikulas Patocka wc->freelist_low_watermark = x; 262448debafeSMikulas Patocka 262593de44ebSMikulas Patocka if (wc->cleaner) 262693de44ebSMikulas Patocka activate_cleaner(wc); 262793de44ebSMikulas Patocka 262848debafeSMikulas Patocka r = writecache_alloc_entries(wc); 262948debafeSMikulas Patocka if (r) { 263048debafeSMikulas Patocka ti->error = "Cannot allocate memory"; 263148debafeSMikulas Patocka goto bad; 263248debafeSMikulas Patocka } 263348debafeSMikulas Patocka 2634ee55b92aSMikulas Patocka ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2; 263548debafeSMikulas Patocka ti->flush_supported = true; 263648debafeSMikulas Patocka ti->num_discard_bios = 1; 263748debafeSMikulas Patocka 263848debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 263948debafeSMikulas Patocka persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); 264048debafeSMikulas Patocka 264148debafeSMikulas Patocka return 0; 264248debafeSMikulas Patocka 264348debafeSMikulas Patocka bad_arguments: 264448debafeSMikulas Patocka r = -EINVAL; 264548debafeSMikulas Patocka ti->error = "Bad arguments"; 264648debafeSMikulas Patocka bad: 264748debafeSMikulas Patocka writecache_dtr(ti); 264848debafeSMikulas Patocka return r; 264948debafeSMikulas Patocka } 265048debafeSMikulas Patocka 265148debafeSMikulas Patocka static void writecache_status(struct dm_target *ti, status_type_t type, 265248debafeSMikulas Patocka unsigned status_flags, char *result, unsigned maxlen) 265348debafeSMikulas Patocka { 265448debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 265548debafeSMikulas Patocka unsigned extra_args; 265648debafeSMikulas Patocka unsigned sz = 0; 265748debafeSMikulas Patocka 265848debafeSMikulas Patocka switch (type) { 265948debafeSMikulas Patocka case STATUSTYPE_INFO: 2660*e3a35d03SMikulas Patocka DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", 2661*e3a35d03SMikulas Patocka writecache_has_error(wc), 266248debafeSMikulas Patocka (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size, 2663*e3a35d03SMikulas Patocka (unsigned long long)wc->writeback_size, 2664*e3a35d03SMikulas Patocka wc->stats.reads, 2665*e3a35d03SMikulas Patocka wc->stats.read_hits, 2666*e3a35d03SMikulas Patocka wc->stats.writes, 2667*e3a35d03SMikulas Patocka wc->stats.write_hits_uncommitted, 2668*e3a35d03SMikulas Patocka wc->stats.write_hits_committed, 2669*e3a35d03SMikulas Patocka wc->stats.writes_around, 2670*e3a35d03SMikulas Patocka wc->stats.writes_allocate, 2671*e3a35d03SMikulas Patocka wc->stats.writes_blocked_on_freelist, 2672*e3a35d03SMikulas Patocka wc->stats.flushes, 2673*e3a35d03SMikulas Patocka wc->stats.discards); 267448debafeSMikulas Patocka break; 267548debafeSMikulas Patocka case STATUSTYPE_TABLE: 267648debafeSMikulas Patocka DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's', 267748debafeSMikulas Patocka wc->dev->name, wc->ssd_dev->name, wc->block_size); 267848debafeSMikulas Patocka extra_args = 0; 2679054bee16SMikulas Patocka if (wc->start_sector_set) 26809ff07e7dSMikulas Patocka extra_args += 2; 2681054bee16SMikulas Patocka if (wc->high_wm_percent_set) 268248debafeSMikulas Patocka extra_args += 2; 2683054bee16SMikulas Patocka if (wc->low_wm_percent_set) 268448debafeSMikulas Patocka extra_args += 2; 268548debafeSMikulas Patocka if (wc->max_writeback_jobs_set) 268648debafeSMikulas Patocka extra_args += 2; 268748debafeSMikulas Patocka if (wc->autocommit_blocks_set) 268848debafeSMikulas Patocka extra_args += 2; 268948debafeSMikulas Patocka if (wc->autocommit_time_set) 269048debafeSMikulas Patocka extra_args += 2; 2691054bee16SMikulas Patocka if (wc->max_age_set) 2692e5d41cbcSMikulas Patocka extra_args += 2; 2693054bee16SMikulas Patocka if (wc->cleaner_set) 269493de44ebSMikulas Patocka extra_args++; 269548debafeSMikulas Patocka if (wc->writeback_fua_set) 269648debafeSMikulas Patocka extra_args++; 2697611c3e16SMikulas Patocka if (wc->metadata_only) 2698611c3e16SMikulas Patocka extra_args++; 26995c0de3d7SMikulas Patocka if (wc->pause_set) 27005c0de3d7SMikulas Patocka extra_args += 2; 270148debafeSMikulas Patocka 270248debafeSMikulas Patocka DMEMIT("%u", extra_args); 2703054bee16SMikulas Patocka if (wc->start_sector_set) 27049ff07e7dSMikulas Patocka DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector); 2705054bee16SMikulas Patocka if (wc->high_wm_percent_set) 2706054bee16SMikulas Patocka DMEMIT(" high_watermark %u", wc->high_wm_percent_value); 2707054bee16SMikulas Patocka if (wc->low_wm_percent_set) 2708054bee16SMikulas Patocka DMEMIT(" low_watermark %u", wc->low_wm_percent_value); 270948debafeSMikulas Patocka if (wc->max_writeback_jobs_set) 271048debafeSMikulas Patocka DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs); 271148debafeSMikulas Patocka if (wc->autocommit_blocks_set) 271248debafeSMikulas Patocka DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks); 271348debafeSMikulas Patocka if (wc->autocommit_time_set) 2714054bee16SMikulas Patocka DMEMIT(" autocommit_time %u", wc->autocommit_time_value); 2715054bee16SMikulas Patocka if (wc->max_age_set) 2716054bee16SMikulas Patocka DMEMIT(" max_age %u", wc->max_age_value); 2717054bee16SMikulas Patocka if (wc->cleaner_set) 271893de44ebSMikulas Patocka DMEMIT(" cleaner"); 271948debafeSMikulas Patocka if (wc->writeback_fua_set) 272048debafeSMikulas Patocka DMEMIT(" %sfua", wc->writeback_fua ? "" : "no"); 2721611c3e16SMikulas Patocka if (wc->metadata_only) 2722611c3e16SMikulas Patocka DMEMIT(" metadata_only"); 27235c0de3d7SMikulas Patocka if (wc->pause_set) 27245c0de3d7SMikulas Patocka DMEMIT(" pause_writeback %u", wc->pause_value); 272548debafeSMikulas Patocka break; 272648debafeSMikulas Patocka } 272748debafeSMikulas Patocka } 272848debafeSMikulas Patocka 272948debafeSMikulas Patocka static struct target_type writecache_target = { 273048debafeSMikulas Patocka .name = "writecache", 2731*e3a35d03SMikulas Patocka .version = {1, 6, 0}, 273248debafeSMikulas Patocka .module = THIS_MODULE, 273348debafeSMikulas Patocka .ctr = writecache_ctr, 273448debafeSMikulas Patocka .dtr = writecache_dtr, 273548debafeSMikulas Patocka .status = writecache_status, 273648debafeSMikulas Patocka .postsuspend = writecache_suspend, 273748debafeSMikulas Patocka .resume = writecache_resume, 273848debafeSMikulas Patocka .message = writecache_message, 273948debafeSMikulas Patocka .map = writecache_map, 274048debafeSMikulas Patocka .end_io = writecache_end_io, 274148debafeSMikulas Patocka .iterate_devices = writecache_iterate_devices, 274248debafeSMikulas Patocka .io_hints = writecache_io_hints, 274348debafeSMikulas Patocka }; 274448debafeSMikulas Patocka 274548debafeSMikulas Patocka static int __init dm_writecache_init(void) 274648debafeSMikulas Patocka { 274748debafeSMikulas Patocka int r; 274848debafeSMikulas Patocka 274948debafeSMikulas Patocka r = dm_register_target(&writecache_target); 275048debafeSMikulas Patocka if (r < 0) { 275148debafeSMikulas Patocka DMERR("register failed %d", r); 275248debafeSMikulas Patocka return r; 275348debafeSMikulas Patocka } 275448debafeSMikulas Patocka 275548debafeSMikulas Patocka return 0; 275648debafeSMikulas Patocka } 275748debafeSMikulas Patocka 275848debafeSMikulas Patocka static void __exit dm_writecache_exit(void) 275948debafeSMikulas Patocka { 276048debafeSMikulas Patocka dm_unregister_target(&writecache_target); 276148debafeSMikulas Patocka } 276248debafeSMikulas Patocka 276348debafeSMikulas Patocka module_init(dm_writecache_init); 276448debafeSMikulas Patocka module_exit(dm_writecache_exit); 276548debafeSMikulas Patocka 276648debafeSMikulas Patocka MODULE_DESCRIPTION(DM_NAME " writecache target"); 276748debafeSMikulas Patocka MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>"); 276848debafeSMikulas Patocka MODULE_LICENSE("GPL"); 2769