13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only 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 25ca7dc242SMikulas Patocka #define MAX_WRITEBACK_JOBS min(0x10000000 / PAGE_SIZE, totalram_pages() / 16) 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 415d2a228bSChristoph Hellwig #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_FS_DAX) 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 131*86a3238cSHeinz Mauelshagen unsigned int uncommitted_blocks; 132*86a3238cSHeinz Mauelshagen unsigned int autocommit_blocks; 133*86a3238cSHeinz Mauelshagen unsigned int 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; 158*86a3238cSHeinz Mauelshagen unsigned int 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 181*86a3238cSHeinz Mauelshagen unsigned int high_wm_percent_value; 182*86a3238cSHeinz Mauelshagen unsigned int low_wm_percent_value; 183*86a3238cSHeinz Mauelshagen unsigned int autocommit_time_value; 184*86a3238cSHeinz Mauelshagen unsigned int max_age_value; 185*86a3238cSHeinz Mauelshagen unsigned int pause_value; 18648debafeSMikulas Patocka 187*86a3238cSHeinz Mauelshagen unsigned int 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; 205*86a3238cSHeinz Mauelshagen unsigned int dirty_bitmap_size; 20648debafeSMikulas Patocka 20748debafeSMikulas Patocka struct bio_set bio_set; 20848debafeSMikulas Patocka mempool_t copy_pool; 209e3a35d03SMikulas Patocka 210e3a35d03SMikulas Patocka struct { 211e3a35d03SMikulas Patocka unsigned long long reads; 212e3a35d03SMikulas Patocka unsigned long long read_hits; 213e3a35d03SMikulas Patocka unsigned long long writes; 214e3a35d03SMikulas Patocka unsigned long long write_hits_uncommitted; 215e3a35d03SMikulas Patocka unsigned long long write_hits_committed; 216e3a35d03SMikulas Patocka unsigned long long writes_around; 217e3a35d03SMikulas Patocka unsigned long long writes_allocate; 218e3a35d03SMikulas Patocka unsigned long long writes_blocked_on_freelist; 219e3a35d03SMikulas Patocka unsigned long long flushes; 220e3a35d03SMikulas Patocka unsigned long long discards; 221e3a35d03SMikulas 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; 230*86a3238cSHeinz Mauelshagen unsigned int 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; 239*86a3238cSHeinz Mauelshagen unsigned int 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 289e511c4a3SJane Chu da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, DAX_ACCESS, 290e511c4a3SJane Chu &wc->memory_map, &pfn); 29148debafeSMikulas Patocka if (da < 0) { 29248debafeSMikulas Patocka wc->memory_map = NULL; 29348debafeSMikulas Patocka r = da; 29448debafeSMikulas Patocka goto err2; 29548debafeSMikulas Patocka } 29648debafeSMikulas Patocka if (!pfn_t_has_page(pfn)) { 29748debafeSMikulas Patocka wc->memory_map = NULL; 29848debafeSMikulas Patocka r = -EOPNOTSUPP; 29948debafeSMikulas Patocka goto err2; 30048debafeSMikulas Patocka } 30148debafeSMikulas Patocka if (da != p) { 30248debafeSMikulas Patocka long i; 30348debafeSMikulas Patocka wc->memory_map = NULL; 30450a7d3baSKees Cook pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL); 30548debafeSMikulas Patocka if (!pages) { 30648debafeSMikulas Patocka r = -ENOMEM; 30748debafeSMikulas Patocka goto err2; 30848debafeSMikulas Patocka } 30948debafeSMikulas Patocka i = 0; 31048debafeSMikulas Patocka do { 31148debafeSMikulas Patocka long daa; 312e511c4a3SJane Chu daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, 313e511c4a3SJane Chu p - i, DAX_ACCESS, NULL, &pfn); 31448debafeSMikulas Patocka if (daa <= 0) { 31548debafeSMikulas Patocka r = daa ? daa : -EINVAL; 31648debafeSMikulas Patocka goto err3; 31748debafeSMikulas Patocka } 31848debafeSMikulas Patocka if (!pfn_t_has_page(pfn)) { 31948debafeSMikulas Patocka r = -EOPNOTSUPP; 32048debafeSMikulas Patocka goto err3; 32148debafeSMikulas Patocka } 32248debafeSMikulas Patocka while (daa-- && i < p) { 32348debafeSMikulas Patocka pages[i++] = pfn_t_to_page(pfn); 32448debafeSMikulas Patocka pfn.val++; 325d35bd764SMikulas Patocka if (!(i & 15)) 326d35bd764SMikulas Patocka cond_resched(); 32748debafeSMikulas Patocka } 32848debafeSMikulas Patocka } while (i < p); 32948debafeSMikulas Patocka wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL); 33048debafeSMikulas Patocka if (!wc->memory_map) { 33148debafeSMikulas Patocka r = -ENOMEM; 33248debafeSMikulas Patocka goto err3; 33348debafeSMikulas Patocka } 33448debafeSMikulas Patocka kvfree(pages); 33548debafeSMikulas Patocka wc->memory_vmapped = true; 33648debafeSMikulas Patocka } 33748debafeSMikulas Patocka 33848debafeSMikulas Patocka dax_read_unlock(id); 339d284f824SMikulas Patocka 340d284f824SMikulas Patocka wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT; 341d284f824SMikulas Patocka wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT; 342d284f824SMikulas Patocka 34348debafeSMikulas Patocka return 0; 34448debafeSMikulas Patocka err3: 34548debafeSMikulas Patocka kvfree(pages); 34648debafeSMikulas Patocka err2: 34748debafeSMikulas Patocka dax_read_unlock(id); 34848debafeSMikulas Patocka err1: 34948debafeSMikulas Patocka return r; 35048debafeSMikulas Patocka } 35148debafeSMikulas Patocka #else 35248debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc) 35348debafeSMikulas Patocka { 354857c4c0aSMike Snitzer return -EOPNOTSUPP; 35548debafeSMikulas Patocka } 35648debafeSMikulas Patocka #endif 35748debafeSMikulas Patocka 35848debafeSMikulas Patocka static void persistent_memory_release(struct dm_writecache *wc) 35948debafeSMikulas Patocka { 36048debafeSMikulas Patocka if (wc->memory_vmapped) 361d284f824SMikulas Patocka vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT)); 36248debafeSMikulas Patocka } 36348debafeSMikulas Patocka 36448debafeSMikulas Patocka static struct page *persistent_memory_page(void *addr) 36548debafeSMikulas Patocka { 36648debafeSMikulas Patocka if (is_vmalloc_addr(addr)) 36748debafeSMikulas Patocka return vmalloc_to_page(addr); 36848debafeSMikulas Patocka else 36948debafeSMikulas Patocka return virt_to_page(addr); 37048debafeSMikulas Patocka } 37148debafeSMikulas Patocka 372*86a3238cSHeinz Mauelshagen static unsigned int persistent_memory_page_offset(void *addr) 37348debafeSMikulas Patocka { 37448debafeSMikulas Patocka return (unsigned long)addr & (PAGE_SIZE - 1); 37548debafeSMikulas Patocka } 37648debafeSMikulas Patocka 37748debafeSMikulas Patocka static void persistent_memory_flush_cache(void *ptr, size_t size) 37848debafeSMikulas Patocka { 37948debafeSMikulas Patocka if (is_vmalloc_addr(ptr)) 38048debafeSMikulas Patocka flush_kernel_vmap_range(ptr, size); 38148debafeSMikulas Patocka } 38248debafeSMikulas Patocka 38348debafeSMikulas Patocka static void persistent_memory_invalidate_cache(void *ptr, size_t size) 38448debafeSMikulas Patocka { 38548debafeSMikulas Patocka if (is_vmalloc_addr(ptr)) 38648debafeSMikulas Patocka invalidate_kernel_vmap_range(ptr, size); 38748debafeSMikulas Patocka } 38848debafeSMikulas Patocka 38948debafeSMikulas Patocka static struct wc_memory_superblock *sb(struct dm_writecache *wc) 39048debafeSMikulas Patocka { 39148debafeSMikulas Patocka return wc->memory_map; 39248debafeSMikulas Patocka } 39348debafeSMikulas Patocka 39448debafeSMikulas Patocka static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e) 39548debafeSMikulas Patocka { 39648debafeSMikulas Patocka return &sb(wc)->entries[e->index]; 39748debafeSMikulas Patocka } 39848debafeSMikulas Patocka 39948debafeSMikulas Patocka static void *memory_data(struct dm_writecache *wc, struct wc_entry *e) 40048debafeSMikulas Patocka { 40148debafeSMikulas Patocka return (char *)wc->block_start + (e->index << wc->block_size_bits); 40248debafeSMikulas Patocka } 40348debafeSMikulas Patocka 40448debafeSMikulas Patocka static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e) 40548debafeSMikulas Patocka { 406d284f824SMikulas Patocka return wc->start_sector + wc->metadata_sectors + 40748debafeSMikulas Patocka ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT)); 40848debafeSMikulas Patocka } 40948debafeSMikulas Patocka 41048debafeSMikulas Patocka static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e) 41148debafeSMikulas Patocka { 41248debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 41348debafeSMikulas Patocka return e->original_sector; 41448debafeSMikulas Patocka #else 41548debafeSMikulas Patocka return le64_to_cpu(memory_entry(wc, e)->original_sector); 41648debafeSMikulas Patocka #endif 41748debafeSMikulas Patocka } 41848debafeSMikulas Patocka 41948debafeSMikulas Patocka static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e) 42048debafeSMikulas Patocka { 42148debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 42248debafeSMikulas Patocka return e->seq_count; 42348debafeSMikulas Patocka #else 42448debafeSMikulas Patocka return le64_to_cpu(memory_entry(wc, e)->seq_count); 42548debafeSMikulas Patocka #endif 42648debafeSMikulas Patocka } 42748debafeSMikulas Patocka 42848debafeSMikulas Patocka static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e) 42948debafeSMikulas Patocka { 43048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 43148debafeSMikulas Patocka e->seq_count = -1; 43248debafeSMikulas Patocka #endif 43348debafeSMikulas Patocka pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1)); 43448debafeSMikulas Patocka } 43548debafeSMikulas Patocka 43648debafeSMikulas Patocka static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e, 43748debafeSMikulas Patocka uint64_t original_sector, uint64_t seq_count) 43848debafeSMikulas Patocka { 43948debafeSMikulas Patocka struct wc_memory_entry me; 44048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 44148debafeSMikulas Patocka e->original_sector = original_sector; 44248debafeSMikulas Patocka e->seq_count = seq_count; 44348debafeSMikulas Patocka #endif 44448debafeSMikulas Patocka me.original_sector = cpu_to_le64(original_sector); 44548debafeSMikulas Patocka me.seq_count = cpu_to_le64(seq_count); 44648debafeSMikulas Patocka pmem_assign(*memory_entry(wc, e), me); 44748debafeSMikulas Patocka } 44848debafeSMikulas Patocka 44948debafeSMikulas Patocka #define writecache_error(wc, err, msg, arg...) \ 45048debafeSMikulas Patocka do { \ 45148debafeSMikulas Patocka if (!cmpxchg(&(wc)->error, 0, err)) \ 45248debafeSMikulas Patocka DMERR(msg, ##arg); \ 45348debafeSMikulas Patocka wake_up(&(wc)->freelist_wait); \ 45448debafeSMikulas Patocka } while (0) 45548debafeSMikulas Patocka 45648debafeSMikulas Patocka #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error))) 45748debafeSMikulas Patocka 45848debafeSMikulas Patocka static void writecache_flush_all_metadata(struct dm_writecache *wc) 45948debafeSMikulas Patocka { 46048debafeSMikulas Patocka if (!WC_MODE_PMEM(wc)) 46148debafeSMikulas Patocka memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size); 46248debafeSMikulas Patocka } 46348debafeSMikulas Patocka 46448debafeSMikulas Patocka static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size) 46548debafeSMikulas Patocka { 46648debafeSMikulas Patocka if (!WC_MODE_PMEM(wc)) 46748debafeSMikulas Patocka __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY, 46848debafeSMikulas Patocka wc->dirty_bitmap); 46948debafeSMikulas Patocka } 47048debafeSMikulas Patocka 47148debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev); 47248debafeSMikulas Patocka 47348debafeSMikulas Patocka struct io_notify { 47448debafeSMikulas Patocka struct dm_writecache *wc; 47548debafeSMikulas Patocka struct completion c; 47648debafeSMikulas Patocka atomic_t count; 47748debafeSMikulas Patocka }; 47848debafeSMikulas Patocka 47948debafeSMikulas Patocka static void writecache_notify_io(unsigned long error, void *context) 48048debafeSMikulas Patocka { 48148debafeSMikulas Patocka struct io_notify *endio = context; 48248debafeSMikulas Patocka 48348debafeSMikulas Patocka if (unlikely(error != 0)) 48448debafeSMikulas Patocka writecache_error(endio->wc, -EIO, "error writing metadata"); 48548debafeSMikulas Patocka BUG_ON(atomic_read(&endio->count) <= 0); 48648debafeSMikulas Patocka if (atomic_dec_and_test(&endio->count)) 48748debafeSMikulas Patocka complete(&endio->c); 48848debafeSMikulas Patocka } 48948debafeSMikulas Patocka 490aa950920SMikulas Patocka static void writecache_wait_for_ios(struct dm_writecache *wc, int direction) 491aa950920SMikulas Patocka { 492aa950920SMikulas Patocka wait_event(wc->bio_in_progress_wait[direction], 493aa950920SMikulas Patocka !atomic_read(&wc->bio_in_progress[direction])); 494aa950920SMikulas Patocka } 495aa950920SMikulas Patocka 496aa950920SMikulas Patocka static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) 49748debafeSMikulas Patocka { 49848debafeSMikulas Patocka struct dm_io_region region; 49948debafeSMikulas Patocka struct dm_io_request req; 50048debafeSMikulas Patocka struct io_notify endio = { 50148debafeSMikulas Patocka wc, 50248debafeSMikulas Patocka COMPLETION_INITIALIZER_ONSTACK(endio.c), 50348debafeSMikulas Patocka ATOMIC_INIT(1), 50448debafeSMikulas Patocka }; 505*86a3238cSHeinz Mauelshagen unsigned int bitmap_bits = wc->dirty_bitmap_size * 8; 506*86a3238cSHeinz Mauelshagen unsigned int i = 0; 50748debafeSMikulas Patocka 50848debafeSMikulas Patocka while (1) { 509*86a3238cSHeinz Mauelshagen unsigned int j; 51048debafeSMikulas Patocka i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i); 51148debafeSMikulas Patocka if (unlikely(i == bitmap_bits)) 51248debafeSMikulas Patocka break; 51348debafeSMikulas Patocka j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i); 51448debafeSMikulas Patocka 51548debafeSMikulas Patocka region.bdev = wc->ssd_dev->bdev; 51648debafeSMikulas Patocka region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT); 51748debafeSMikulas Patocka region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT); 51848debafeSMikulas Patocka 51948debafeSMikulas Patocka if (unlikely(region.sector >= wc->metadata_sectors)) 52048debafeSMikulas Patocka break; 52148debafeSMikulas Patocka if (unlikely(region.sector + region.count > wc->metadata_sectors)) 52248debafeSMikulas Patocka region.count = wc->metadata_sectors - region.sector; 52348debafeSMikulas Patocka 524d284f824SMikulas Patocka region.sector += wc->start_sector; 52548debafeSMikulas Patocka atomic_inc(&endio.count); 526581075e4SBart Van Assche req.bi_opf = REQ_OP_WRITE | 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 564581075e4SBart Van Assche req.bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_FUA; 565dc8a01aeSMikulas Patocka req.mem.type = DM_IO_VMA; 566dc8a01aeSMikulas Patocka req.mem.ptr.vma = (char *)wc->memory_map; 567dc8a01aeSMikulas Patocka req.client = wc->dm_io; 568dc8a01aeSMikulas Patocka req.notify.fn = NULL; 569dc8a01aeSMikulas Patocka req.notify.context = NULL; 570dc8a01aeSMikulas Patocka 571dc8a01aeSMikulas Patocka r = dm_io(&req, 1, ®ion, NULL); 572dc8a01aeSMikulas Patocka if (unlikely(r)) 573dc8a01aeSMikulas Patocka writecache_error(wc, r, "error writing superblock"); 574dc8a01aeSMikulas Patocka } 575dc8a01aeSMikulas Patocka 576aa950920SMikulas Patocka static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) 57748debafeSMikulas Patocka { 57848debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 5793e79f082SAneesh Kumar K.V pmem_wmb(); 58048debafeSMikulas Patocka else 581aa950920SMikulas Patocka ssd_commit_flushed(wc, wait_for_ios); 58248debafeSMikulas Patocka } 58348debafeSMikulas Patocka 58448debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev) 58548debafeSMikulas Patocka { 58648debafeSMikulas Patocka int r; 58748debafeSMikulas Patocka struct dm_io_region region; 58848debafeSMikulas Patocka struct dm_io_request req; 58948debafeSMikulas Patocka 59048debafeSMikulas Patocka region.bdev = dev->bdev; 59148debafeSMikulas Patocka region.sector = 0; 59248debafeSMikulas Patocka region.count = 0; 593581075e4SBart Van Assche req.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; 59448debafeSMikulas Patocka req.mem.type = DM_IO_KMEM; 59548debafeSMikulas Patocka req.mem.ptr.addr = NULL; 59648debafeSMikulas Patocka req.client = wc->dm_io; 59748debafeSMikulas Patocka req.notify.fn = NULL; 59848debafeSMikulas Patocka 59948debafeSMikulas Patocka r = dm_io(&req, 1, ®ion, NULL); 60048debafeSMikulas Patocka if (unlikely(r)) 60148debafeSMikulas Patocka writecache_error(wc, r, "error flushing metadata: %d", r); 60248debafeSMikulas Patocka } 60348debafeSMikulas Patocka 60448debafeSMikulas Patocka #define WFE_RETURN_FOLLOWING 1 60548debafeSMikulas Patocka #define WFE_LOWEST_SEQ 2 60648debafeSMikulas Patocka 60748debafeSMikulas Patocka static struct wc_entry *writecache_find_entry(struct dm_writecache *wc, 60848debafeSMikulas Patocka uint64_t block, int flags) 60948debafeSMikulas Patocka { 61048debafeSMikulas Patocka struct wc_entry *e; 61148debafeSMikulas Patocka struct rb_node *node = wc->tree.rb_node; 61248debafeSMikulas Patocka 61348debafeSMikulas Patocka if (unlikely(!node)) 61448debafeSMikulas Patocka return NULL; 61548debafeSMikulas Patocka 61648debafeSMikulas Patocka while (1) { 61748debafeSMikulas Patocka e = container_of(node, struct wc_entry, rb_node); 61848debafeSMikulas Patocka if (read_original_sector(wc, e) == block) 61948debafeSMikulas Patocka break; 620f8011d33SMikulas Patocka 62148debafeSMikulas Patocka node = (read_original_sector(wc, e) >= block ? 62248debafeSMikulas Patocka e->rb_node.rb_left : e->rb_node.rb_right); 62348debafeSMikulas Patocka if (unlikely(!node)) { 624f8011d33SMikulas Patocka if (!(flags & WFE_RETURN_FOLLOWING)) 62548debafeSMikulas Patocka return NULL; 62648debafeSMikulas Patocka if (read_original_sector(wc, e) >= block) { 627f8011d33SMikulas Patocka return e; 62848debafeSMikulas Patocka } else { 62948debafeSMikulas Patocka node = rb_next(&e->rb_node); 630f8011d33SMikulas Patocka if (unlikely(!node)) 63148debafeSMikulas Patocka return NULL; 63248debafeSMikulas Patocka e = container_of(node, struct wc_entry, rb_node); 633f8011d33SMikulas Patocka return e; 63448debafeSMikulas Patocka } 63548debafeSMikulas Patocka } 63648debafeSMikulas Patocka } 63748debafeSMikulas Patocka 63848debafeSMikulas Patocka while (1) { 63948debafeSMikulas Patocka struct wc_entry *e2; 64048debafeSMikulas Patocka if (flags & WFE_LOWEST_SEQ) 64148debafeSMikulas Patocka node = rb_prev(&e->rb_node); 64248debafeSMikulas Patocka else 64348debafeSMikulas Patocka node = rb_next(&e->rb_node); 64484420b1eSHuaisheng Ye if (unlikely(!node)) 64548debafeSMikulas Patocka return e; 64648debafeSMikulas Patocka e2 = container_of(node, struct wc_entry, rb_node); 64748debafeSMikulas Patocka if (read_original_sector(wc, e2) != block) 64848debafeSMikulas Patocka return e; 64948debafeSMikulas Patocka e = e2; 65048debafeSMikulas Patocka } 65148debafeSMikulas Patocka } 65248debafeSMikulas Patocka 65348debafeSMikulas Patocka static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins) 65448debafeSMikulas Patocka { 65548debafeSMikulas Patocka struct wc_entry *e; 65648debafeSMikulas Patocka struct rb_node **node = &wc->tree.rb_node, *parent = NULL; 65748debafeSMikulas Patocka 65848debafeSMikulas Patocka while (*node) { 65948debafeSMikulas Patocka e = container_of(*node, struct wc_entry, rb_node); 66048debafeSMikulas Patocka parent = &e->rb_node; 66148debafeSMikulas Patocka if (read_original_sector(wc, e) > read_original_sector(wc, ins)) 66248debafeSMikulas Patocka node = &parent->rb_left; 66348debafeSMikulas Patocka else 66448debafeSMikulas Patocka node = &parent->rb_right; 66548debafeSMikulas Patocka } 66648debafeSMikulas Patocka rb_link_node(&ins->rb_node, parent, node); 66748debafeSMikulas Patocka rb_insert_color(&ins->rb_node, &wc->tree); 66848debafeSMikulas Patocka list_add(&ins->lru, &wc->lru); 6693923d485SMikulas Patocka ins->age = jiffies; 67048debafeSMikulas Patocka } 67148debafeSMikulas Patocka 67248debafeSMikulas Patocka static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e) 67348debafeSMikulas Patocka { 67448debafeSMikulas Patocka list_del(&e->lru); 67548debafeSMikulas Patocka rb_erase(&e->rb_node, &wc->tree); 67648debafeSMikulas Patocka } 67748debafeSMikulas Patocka 67848debafeSMikulas Patocka static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e) 67948debafeSMikulas Patocka { 68048debafeSMikulas Patocka if (WC_MODE_SORT_FREELIST(wc)) { 68148debafeSMikulas Patocka struct rb_node **node = &wc->freetree.rb_node, *parent = NULL; 68248debafeSMikulas Patocka if (unlikely(!*node)) 68348debafeSMikulas Patocka wc->current_free = e; 68448debafeSMikulas Patocka while (*node) { 68548debafeSMikulas Patocka parent = *node; 68648debafeSMikulas Patocka if (&e->rb_node < *node) 68748debafeSMikulas Patocka node = &parent->rb_left; 68848debafeSMikulas Patocka else 68948debafeSMikulas Patocka node = &parent->rb_right; 69048debafeSMikulas Patocka } 69148debafeSMikulas Patocka rb_link_node(&e->rb_node, parent, node); 69248debafeSMikulas Patocka rb_insert_color(&e->rb_node, &wc->freetree); 69348debafeSMikulas Patocka } else { 69448debafeSMikulas Patocka list_add_tail(&e->lru, &wc->freelist); 69548debafeSMikulas Patocka } 69648debafeSMikulas Patocka wc->freelist_size++; 69748debafeSMikulas Patocka } 69848debafeSMikulas Patocka 69941c526c5SMikulas Patocka static inline void writecache_verify_watermark(struct dm_writecache *wc) 70041c526c5SMikulas Patocka { 70141c526c5SMikulas Patocka if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark)) 70241c526c5SMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 70341c526c5SMikulas Patocka } 70441c526c5SMikulas Patocka 7053923d485SMikulas Patocka static void writecache_max_age_timer(struct timer_list *t) 7063923d485SMikulas Patocka { 7073923d485SMikulas Patocka struct dm_writecache *wc = from_timer(wc, t, max_age_timer); 7083923d485SMikulas Patocka 7093923d485SMikulas Patocka if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) { 7103923d485SMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 7113923d485SMikulas Patocka mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); 7123923d485SMikulas Patocka } 7133923d485SMikulas Patocka } 7143923d485SMikulas Patocka 715dcd19507SMikulas Patocka static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector) 71648debafeSMikulas Patocka { 71748debafeSMikulas Patocka struct wc_entry *e; 71848debafeSMikulas Patocka 71948debafeSMikulas Patocka if (WC_MODE_SORT_FREELIST(wc)) { 72048debafeSMikulas Patocka struct rb_node *next; 72148debafeSMikulas Patocka if (unlikely(!wc->current_free)) 72248debafeSMikulas Patocka return NULL; 72348debafeSMikulas Patocka e = wc->current_free; 724dcd19507SMikulas Patocka if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) 725dcd19507SMikulas Patocka return NULL; 72648debafeSMikulas Patocka next = rb_next(&e->rb_node); 72748debafeSMikulas Patocka rb_erase(&e->rb_node, &wc->freetree); 72848debafeSMikulas Patocka if (unlikely(!next)) 72948debafeSMikulas Patocka next = rb_first(&wc->freetree); 73048debafeSMikulas Patocka wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL; 73148debafeSMikulas Patocka } else { 73248debafeSMikulas Patocka if (unlikely(list_empty(&wc->freelist))) 73348debafeSMikulas Patocka return NULL; 73448debafeSMikulas Patocka e = container_of(wc->freelist.next, struct wc_entry, lru); 735dcd19507SMikulas Patocka if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) 736dcd19507SMikulas Patocka return NULL; 73748debafeSMikulas Patocka list_del(&e->lru); 73848debafeSMikulas Patocka } 73948debafeSMikulas Patocka wc->freelist_size--; 74041c526c5SMikulas Patocka 74141c526c5SMikulas Patocka writecache_verify_watermark(wc); 74248debafeSMikulas Patocka 74348debafeSMikulas Patocka return e; 74448debafeSMikulas Patocka } 74548debafeSMikulas Patocka 74648debafeSMikulas Patocka static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e) 74748debafeSMikulas Patocka { 74848debafeSMikulas Patocka writecache_unlink(wc, e); 74948debafeSMikulas Patocka writecache_add_to_freelist(wc, e); 75048debafeSMikulas Patocka clear_seq_count(wc, e); 75148debafeSMikulas Patocka writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); 75248debafeSMikulas Patocka if (unlikely(waitqueue_active(&wc->freelist_wait))) 75348debafeSMikulas Patocka wake_up(&wc->freelist_wait); 75448debafeSMikulas Patocka } 75548debafeSMikulas Patocka 75648debafeSMikulas Patocka static void writecache_wait_on_freelist(struct dm_writecache *wc) 75748debafeSMikulas Patocka { 75848debafeSMikulas Patocka DEFINE_WAIT(wait); 75948debafeSMikulas Patocka 76048debafeSMikulas Patocka prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE); 76148debafeSMikulas Patocka wc_unlock(wc); 76248debafeSMikulas Patocka io_schedule(); 76348debafeSMikulas Patocka finish_wait(&wc->freelist_wait, &wait); 76448debafeSMikulas Patocka wc_lock(wc); 76548debafeSMikulas Patocka } 76648debafeSMikulas Patocka 76748debafeSMikulas Patocka static void writecache_poison_lists(struct dm_writecache *wc) 76848debafeSMikulas Patocka { 76948debafeSMikulas Patocka /* 77048debafeSMikulas Patocka * Catch incorrect access to these values while the device is suspended. 77148debafeSMikulas Patocka */ 77248debafeSMikulas Patocka memset(&wc->tree, -1, sizeof wc->tree); 77348debafeSMikulas Patocka wc->lru.next = LIST_POISON1; 77448debafeSMikulas Patocka wc->lru.prev = LIST_POISON2; 77548debafeSMikulas Patocka wc->freelist.next = LIST_POISON1; 77648debafeSMikulas Patocka wc->freelist.prev = LIST_POISON2; 77748debafeSMikulas Patocka } 77848debafeSMikulas Patocka 77948debafeSMikulas Patocka static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e) 78048debafeSMikulas Patocka { 78148debafeSMikulas Patocka writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); 78248debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 78348debafeSMikulas Patocka writecache_flush_region(wc, memory_data(wc, e), wc->block_size); 78448debafeSMikulas Patocka } 78548debafeSMikulas Patocka 78648debafeSMikulas Patocka static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e) 78748debafeSMikulas Patocka { 78848debafeSMikulas Patocka return read_seq_count(wc, e) < wc->seq_count; 78948debafeSMikulas Patocka } 79048debafeSMikulas Patocka 79148debafeSMikulas Patocka static void writecache_flush(struct dm_writecache *wc) 79248debafeSMikulas Patocka { 79348debafeSMikulas Patocka struct wc_entry *e, *e2; 79448debafeSMikulas Patocka bool need_flush_after_free; 79548debafeSMikulas Patocka 79648debafeSMikulas Patocka wc->uncommitted_blocks = 0; 79748debafeSMikulas Patocka del_timer(&wc->autocommit_timer); 79848debafeSMikulas Patocka 79948debafeSMikulas Patocka if (list_empty(&wc->lru)) 80048debafeSMikulas Patocka return; 80148debafeSMikulas Patocka 80248debafeSMikulas Patocka e = container_of(wc->lru.next, struct wc_entry, lru); 80348debafeSMikulas Patocka if (writecache_entry_is_committed(wc, e)) { 80448debafeSMikulas Patocka if (wc->overwrote_committed) { 80548debafeSMikulas Patocka writecache_wait_for_ios(wc, WRITE); 80648debafeSMikulas Patocka writecache_disk_flush(wc, wc->ssd_dev); 80748debafeSMikulas Patocka wc->overwrote_committed = false; 80848debafeSMikulas Patocka } 80948debafeSMikulas Patocka return; 81048debafeSMikulas Patocka } 81148debafeSMikulas Patocka while (1) { 81248debafeSMikulas Patocka writecache_flush_entry(wc, e); 81348debafeSMikulas Patocka if (unlikely(e->lru.next == &wc->lru)) 81448debafeSMikulas Patocka break; 81548debafeSMikulas Patocka e2 = container_of(e->lru.next, struct wc_entry, lru); 81648debafeSMikulas Patocka if (writecache_entry_is_committed(wc, e2)) 81748debafeSMikulas Patocka break; 81848debafeSMikulas Patocka e = e2; 81948debafeSMikulas Patocka cond_resched(); 82048debafeSMikulas Patocka } 821aa950920SMikulas Patocka writecache_commit_flushed(wc, true); 82248debafeSMikulas Patocka 82348debafeSMikulas Patocka wc->seq_count++; 82448debafeSMikulas Patocka pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count)); 825dc8a01aeSMikulas Patocka if (WC_MODE_PMEM(wc)) 826aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 827dc8a01aeSMikulas Patocka else 828dc8a01aeSMikulas Patocka ssd_commit_superblock(wc); 82948debafeSMikulas Patocka 83048debafeSMikulas Patocka wc->overwrote_committed = false; 83148debafeSMikulas Patocka 83248debafeSMikulas Patocka need_flush_after_free = false; 83348debafeSMikulas Patocka while (1) { 83448debafeSMikulas Patocka /* Free another committed entry with lower seq-count */ 83548debafeSMikulas Patocka struct rb_node *rb_node = rb_prev(&e->rb_node); 83648debafeSMikulas Patocka 83748debafeSMikulas Patocka if (rb_node) { 83848debafeSMikulas Patocka e2 = container_of(rb_node, struct wc_entry, rb_node); 83948debafeSMikulas Patocka if (read_original_sector(wc, e2) == read_original_sector(wc, e) && 84048debafeSMikulas Patocka likely(!e2->write_in_progress)) { 84148debafeSMikulas Patocka writecache_free_entry(wc, e2); 84248debafeSMikulas Patocka need_flush_after_free = true; 84348debafeSMikulas Patocka } 84448debafeSMikulas Patocka } 84548debafeSMikulas Patocka if (unlikely(e->lru.prev == &wc->lru)) 84648debafeSMikulas Patocka break; 84748debafeSMikulas Patocka e = container_of(e->lru.prev, struct wc_entry, lru); 84848debafeSMikulas Patocka cond_resched(); 84948debafeSMikulas Patocka } 85048debafeSMikulas Patocka 85148debafeSMikulas Patocka if (need_flush_after_free) 852aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 85348debafeSMikulas Patocka } 85448debafeSMikulas Patocka 85548debafeSMikulas Patocka static void writecache_flush_work(struct work_struct *work) 85648debafeSMikulas Patocka { 85748debafeSMikulas Patocka struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work); 85848debafeSMikulas Patocka 85948debafeSMikulas Patocka wc_lock(wc); 86048debafeSMikulas Patocka writecache_flush(wc); 86148debafeSMikulas Patocka wc_unlock(wc); 86248debafeSMikulas Patocka } 86348debafeSMikulas Patocka 86448debafeSMikulas Patocka static void writecache_autocommit_timer(struct timer_list *t) 86548debafeSMikulas Patocka { 86648debafeSMikulas Patocka struct dm_writecache *wc = from_timer(wc, t, autocommit_timer); 86748debafeSMikulas Patocka if (!writecache_has_error(wc)) 86848debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->flush_work); 86948debafeSMikulas Patocka } 87048debafeSMikulas Patocka 87148debafeSMikulas Patocka static void writecache_schedule_autocommit(struct dm_writecache *wc) 87248debafeSMikulas Patocka { 87348debafeSMikulas Patocka if (!timer_pending(&wc->autocommit_timer)) 87448debafeSMikulas Patocka mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies); 87548debafeSMikulas Patocka } 87648debafeSMikulas Patocka 87748debafeSMikulas Patocka static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end) 87848debafeSMikulas Patocka { 87948debafeSMikulas Patocka struct wc_entry *e; 88048debafeSMikulas Patocka bool discarded_something = false; 88148debafeSMikulas Patocka 88248debafeSMikulas Patocka e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ); 88348debafeSMikulas Patocka if (unlikely(!e)) 88448debafeSMikulas Patocka return; 88548debafeSMikulas Patocka 88648debafeSMikulas Patocka while (read_original_sector(wc, e) < end) { 88748debafeSMikulas Patocka struct rb_node *node = rb_next(&e->rb_node); 88848debafeSMikulas Patocka 88948debafeSMikulas Patocka if (likely(!e->write_in_progress)) { 89048debafeSMikulas Patocka if (!discarded_something) { 891a143e172SHuaisheng Ye if (!WC_MODE_PMEM(wc)) { 89248debafeSMikulas Patocka writecache_wait_for_ios(wc, READ); 89348debafeSMikulas Patocka writecache_wait_for_ios(wc, WRITE); 894a143e172SHuaisheng Ye } 89548debafeSMikulas Patocka discarded_something = true; 89648debafeSMikulas Patocka } 89739495b12SHuaisheng Ye if (!writecache_entry_is_committed(wc, e)) 89839495b12SHuaisheng Ye wc->uncommitted_blocks--; 89948debafeSMikulas Patocka writecache_free_entry(wc, e); 90048debafeSMikulas Patocka } 90148debafeSMikulas Patocka 90284420b1eSHuaisheng Ye if (unlikely(!node)) 90348debafeSMikulas Patocka break; 90448debafeSMikulas Patocka 90548debafeSMikulas Patocka e = container_of(node, struct wc_entry, rb_node); 90648debafeSMikulas Patocka } 90748debafeSMikulas Patocka 90848debafeSMikulas Patocka if (discarded_something) 909aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 91048debafeSMikulas Patocka } 91148debafeSMikulas Patocka 91248debafeSMikulas Patocka static bool writecache_wait_for_writeback(struct dm_writecache *wc) 91348debafeSMikulas Patocka { 91448debafeSMikulas Patocka if (wc->writeback_size) { 91548debafeSMikulas Patocka writecache_wait_on_freelist(wc); 91648debafeSMikulas Patocka return true; 91748debafeSMikulas Patocka } 91848debafeSMikulas Patocka return false; 91948debafeSMikulas Patocka } 92048debafeSMikulas Patocka 92148debafeSMikulas Patocka static void writecache_suspend(struct dm_target *ti) 92248debafeSMikulas Patocka { 92348debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 92448debafeSMikulas Patocka bool flush_on_suspend; 92548debafeSMikulas Patocka 92648debafeSMikulas Patocka del_timer_sync(&wc->autocommit_timer); 9273923d485SMikulas Patocka del_timer_sync(&wc->max_age_timer); 92848debafeSMikulas Patocka 92948debafeSMikulas Patocka wc_lock(wc); 93048debafeSMikulas Patocka writecache_flush(wc); 93148debafeSMikulas Patocka flush_on_suspend = wc->flush_on_suspend; 93248debafeSMikulas Patocka if (flush_on_suspend) { 93348debafeSMikulas Patocka wc->flush_on_suspend = false; 93448debafeSMikulas Patocka wc->writeback_all++; 93548debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 93648debafeSMikulas Patocka } 93748debafeSMikulas Patocka wc_unlock(wc); 93848debafeSMikulas Patocka 939adc0daadSMikulas Patocka drain_workqueue(wc->writeback_wq); 94048debafeSMikulas Patocka 94148debafeSMikulas Patocka wc_lock(wc); 94248debafeSMikulas Patocka if (flush_on_suspend) 94348debafeSMikulas Patocka wc->writeback_all--; 94448debafeSMikulas Patocka while (writecache_wait_for_writeback(wc)); 94548debafeSMikulas Patocka 94648debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 94748debafeSMikulas Patocka persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); 94848debafeSMikulas Patocka 94948debafeSMikulas Patocka writecache_poison_lists(wc); 95048debafeSMikulas Patocka 95148debafeSMikulas Patocka wc_unlock(wc); 95248debafeSMikulas Patocka } 95348debafeSMikulas Patocka 95448debafeSMikulas Patocka static int writecache_alloc_entries(struct dm_writecache *wc) 95548debafeSMikulas Patocka { 95648debafeSMikulas Patocka size_t b; 95748debafeSMikulas Patocka 95848debafeSMikulas Patocka if (wc->entries) 95948debafeSMikulas Patocka return 0; 96050a7d3baSKees Cook wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks)); 96148debafeSMikulas Patocka if (!wc->entries) 96248debafeSMikulas Patocka return -ENOMEM; 96348debafeSMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 96448debafeSMikulas Patocka struct wc_entry *e = &wc->entries[b]; 96548debafeSMikulas Patocka e->index = b; 96648debafeSMikulas Patocka e->write_in_progress = false; 9671edaa447SMikulas Patocka cond_resched(); 96848debafeSMikulas Patocka } 96948debafeSMikulas Patocka 97048debafeSMikulas Patocka return 0; 97148debafeSMikulas Patocka } 97248debafeSMikulas Patocka 97331b22120SMikulas Patocka static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors) 97431b22120SMikulas Patocka { 97531b22120SMikulas Patocka struct dm_io_region region; 97631b22120SMikulas Patocka struct dm_io_request req; 97731b22120SMikulas Patocka 97831b22120SMikulas Patocka region.bdev = wc->ssd_dev->bdev; 97931b22120SMikulas Patocka region.sector = wc->start_sector; 98031b22120SMikulas Patocka region.count = n_sectors; 981581075e4SBart Van Assche req.bi_opf = REQ_OP_READ | REQ_SYNC; 98231b22120SMikulas Patocka req.mem.type = DM_IO_VMA; 98331b22120SMikulas Patocka req.mem.ptr.vma = (char *)wc->memory_map; 98431b22120SMikulas Patocka req.client = wc->dm_io; 98531b22120SMikulas Patocka req.notify.fn = NULL; 98631b22120SMikulas Patocka 98731b22120SMikulas Patocka return dm_io(&req, 1, ®ion, NULL); 98831b22120SMikulas Patocka } 98931b22120SMikulas Patocka 99048debafeSMikulas Patocka static void writecache_resume(struct dm_target *ti) 99148debafeSMikulas Patocka { 99248debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 99348debafeSMikulas Patocka size_t b; 99448debafeSMikulas Patocka bool need_flush = false; 99548debafeSMikulas Patocka __le64 sb_seq_count; 99648debafeSMikulas Patocka int r; 99748debafeSMikulas Patocka 99848debafeSMikulas Patocka wc_lock(wc); 99948debafeSMikulas Patocka 1000d9928ac5SMike Snitzer wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev); 10014134455fSMikulas Patocka 100231b22120SMikulas Patocka if (WC_MODE_PMEM(wc)) { 100348debafeSMikulas Patocka persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size); 100431b22120SMikulas Patocka } else { 100531b22120SMikulas Patocka r = writecache_read_metadata(wc, wc->metadata_sectors); 100631b22120SMikulas Patocka if (r) { 100731b22120SMikulas Patocka size_t sb_entries_offset; 100831b22120SMikulas Patocka writecache_error(wc, r, "unable to read metadata: %d", r); 100931b22120SMikulas Patocka sb_entries_offset = offsetof(struct wc_memory_superblock, entries); 101031b22120SMikulas Patocka memset((char *)wc->memory_map + sb_entries_offset, -1, 101131b22120SMikulas Patocka (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset); 101231b22120SMikulas Patocka } 101331b22120SMikulas Patocka } 101448debafeSMikulas Patocka 101548debafeSMikulas Patocka wc->tree = RB_ROOT; 101648debafeSMikulas Patocka INIT_LIST_HEAD(&wc->lru); 101748debafeSMikulas Patocka if (WC_MODE_SORT_FREELIST(wc)) { 101848debafeSMikulas Patocka wc->freetree = RB_ROOT; 101948debafeSMikulas Patocka wc->current_free = NULL; 102048debafeSMikulas Patocka } else { 102148debafeSMikulas Patocka INIT_LIST_HEAD(&wc->freelist); 102248debafeSMikulas Patocka } 102348debafeSMikulas Patocka wc->freelist_size = 0; 102448debafeSMikulas Patocka 1025ec6347bbSDan Williams r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count, 1026ec6347bbSDan Williams sizeof(uint64_t)); 102748debafeSMikulas Patocka if (r) { 102848debafeSMikulas Patocka writecache_error(wc, r, "hardware memory error when reading superblock: %d", r); 102948debafeSMikulas Patocka sb_seq_count = cpu_to_le64(0); 103048debafeSMikulas Patocka } 103148debafeSMikulas Patocka wc->seq_count = le64_to_cpu(sb_seq_count); 103248debafeSMikulas Patocka 103348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS 103448debafeSMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 103548debafeSMikulas Patocka struct wc_entry *e = &wc->entries[b]; 103648debafeSMikulas Patocka struct wc_memory_entry wme; 103748debafeSMikulas Patocka if (writecache_has_error(wc)) { 103848debafeSMikulas Patocka e->original_sector = -1; 103948debafeSMikulas Patocka e->seq_count = -1; 104048debafeSMikulas Patocka continue; 104148debafeSMikulas Patocka } 1042ec6347bbSDan Williams r = copy_mc_to_kernel(&wme, memory_entry(wc, e), 1043ec6347bbSDan Williams sizeof(struct wc_memory_entry)); 104448debafeSMikulas Patocka if (r) { 104548debafeSMikulas Patocka writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d", 104648debafeSMikulas Patocka (unsigned long)b, r); 104748debafeSMikulas Patocka e->original_sector = -1; 104848debafeSMikulas Patocka e->seq_count = -1; 104948debafeSMikulas Patocka } else { 105048debafeSMikulas Patocka e->original_sector = le64_to_cpu(wme.original_sector); 105148debafeSMikulas Patocka e->seq_count = le64_to_cpu(wme.seq_count); 105248debafeSMikulas Patocka } 10531edaa447SMikulas Patocka cond_resched(); 105448debafeSMikulas Patocka } 105548debafeSMikulas Patocka #endif 105648debafeSMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 105748debafeSMikulas Patocka struct wc_entry *e = &wc->entries[b]; 105848debafeSMikulas Patocka if (!writecache_entry_is_committed(wc, e)) { 105948debafeSMikulas Patocka if (read_seq_count(wc, e) != -1) { 106048debafeSMikulas Patocka erase_this: 106148debafeSMikulas Patocka clear_seq_count(wc, e); 106248debafeSMikulas Patocka need_flush = true; 106348debafeSMikulas Patocka } 106448debafeSMikulas Patocka writecache_add_to_freelist(wc, e); 106548debafeSMikulas Patocka } else { 106648debafeSMikulas Patocka struct wc_entry *old; 106748debafeSMikulas Patocka 106848debafeSMikulas Patocka old = writecache_find_entry(wc, read_original_sector(wc, e), 0); 106948debafeSMikulas Patocka if (!old) { 107048debafeSMikulas Patocka writecache_insert_entry(wc, e); 107148debafeSMikulas Patocka } else { 107248debafeSMikulas Patocka if (read_seq_count(wc, old) == read_seq_count(wc, e)) { 107348debafeSMikulas Patocka writecache_error(wc, -EINVAL, 107448debafeSMikulas Patocka "two identical entries, position %llu, sector %llu, sequence %llu", 107548debafeSMikulas Patocka (unsigned long long)b, (unsigned long long)read_original_sector(wc, e), 107648debafeSMikulas Patocka (unsigned long long)read_seq_count(wc, e)); 107748debafeSMikulas Patocka } 107848debafeSMikulas Patocka if (read_seq_count(wc, old) > read_seq_count(wc, e)) { 107948debafeSMikulas Patocka goto erase_this; 108048debafeSMikulas Patocka } else { 108148debafeSMikulas Patocka writecache_free_entry(wc, old); 108248debafeSMikulas Patocka writecache_insert_entry(wc, e); 108348debafeSMikulas Patocka need_flush = true; 108448debafeSMikulas Patocka } 108548debafeSMikulas Patocka } 108648debafeSMikulas Patocka } 108748debafeSMikulas Patocka cond_resched(); 108848debafeSMikulas Patocka } 108948debafeSMikulas Patocka 109048debafeSMikulas Patocka if (need_flush) { 109148debafeSMikulas Patocka writecache_flush_all_metadata(wc); 1092aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 109348debafeSMikulas Patocka } 109448debafeSMikulas Patocka 109541c526c5SMikulas Patocka writecache_verify_watermark(wc); 109641c526c5SMikulas Patocka 10973923d485SMikulas Patocka if (wc->max_age != MAX_AGE_UNSPECIFIED) 10983923d485SMikulas Patocka mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); 10993923d485SMikulas Patocka 110048debafeSMikulas Patocka wc_unlock(wc); 110148debafeSMikulas Patocka } 110248debafeSMikulas Patocka 1103*86a3238cSHeinz Mauelshagen static int process_flush_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) 110448debafeSMikulas Patocka { 110548debafeSMikulas Patocka if (argc != 1) 110648debafeSMikulas Patocka return -EINVAL; 110748debafeSMikulas Patocka 110848debafeSMikulas Patocka wc_lock(wc); 110948debafeSMikulas Patocka if (dm_suspended(wc->ti)) { 111048debafeSMikulas Patocka wc_unlock(wc); 111148debafeSMikulas Patocka return -EBUSY; 111248debafeSMikulas Patocka } 111348debafeSMikulas Patocka if (writecache_has_error(wc)) { 111448debafeSMikulas Patocka wc_unlock(wc); 111548debafeSMikulas Patocka return -EIO; 111648debafeSMikulas Patocka } 111748debafeSMikulas Patocka 111848debafeSMikulas Patocka writecache_flush(wc); 111948debafeSMikulas Patocka wc->writeback_all++; 112048debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 112148debafeSMikulas Patocka wc_unlock(wc); 112248debafeSMikulas Patocka 112348debafeSMikulas Patocka flush_workqueue(wc->writeback_wq); 112448debafeSMikulas Patocka 112548debafeSMikulas Patocka wc_lock(wc); 112648debafeSMikulas Patocka wc->writeback_all--; 112748debafeSMikulas Patocka if (writecache_has_error(wc)) { 112848debafeSMikulas Patocka wc_unlock(wc); 112948debafeSMikulas Patocka return -EIO; 113048debafeSMikulas Patocka } 113148debafeSMikulas Patocka wc_unlock(wc); 113248debafeSMikulas Patocka 113348debafeSMikulas Patocka return 0; 113448debafeSMikulas Patocka } 113548debafeSMikulas Patocka 1136*86a3238cSHeinz Mauelshagen static int process_flush_on_suspend_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) 113748debafeSMikulas Patocka { 113848debafeSMikulas Patocka if (argc != 1) 113948debafeSMikulas Patocka return -EINVAL; 114048debafeSMikulas Patocka 114148debafeSMikulas Patocka wc_lock(wc); 114248debafeSMikulas Patocka wc->flush_on_suspend = true; 114348debafeSMikulas Patocka wc_unlock(wc); 114448debafeSMikulas Patocka 114548debafeSMikulas Patocka return 0; 114648debafeSMikulas Patocka } 114748debafeSMikulas Patocka 114893de44ebSMikulas Patocka static void activate_cleaner(struct dm_writecache *wc) 114993de44ebSMikulas Patocka { 115093de44ebSMikulas Patocka wc->flush_on_suspend = true; 115193de44ebSMikulas Patocka wc->cleaner = true; 115293de44ebSMikulas Patocka wc->freelist_high_watermark = wc->n_blocks; 115393de44ebSMikulas Patocka wc->freelist_low_watermark = wc->n_blocks; 115493de44ebSMikulas Patocka } 115593de44ebSMikulas Patocka 1156*86a3238cSHeinz Mauelshagen static int process_cleaner_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) 115793de44ebSMikulas Patocka { 115893de44ebSMikulas Patocka if (argc != 1) 115993de44ebSMikulas Patocka return -EINVAL; 116093de44ebSMikulas Patocka 116193de44ebSMikulas Patocka wc_lock(wc); 116293de44ebSMikulas Patocka activate_cleaner(wc); 116393de44ebSMikulas Patocka if (!dm_suspended(wc->ti)) 116493de44ebSMikulas Patocka writecache_verify_watermark(wc); 116593de44ebSMikulas Patocka wc_unlock(wc); 116693de44ebSMikulas Patocka 116793de44ebSMikulas Patocka return 0; 116893de44ebSMikulas Patocka } 116993de44ebSMikulas Patocka 1170*86a3238cSHeinz Mauelshagen static int process_clear_stats_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) 1171e3a35d03SMikulas Patocka { 1172e3a35d03SMikulas Patocka if (argc != 1) 1173e3a35d03SMikulas Patocka return -EINVAL; 1174e3a35d03SMikulas Patocka 1175e3a35d03SMikulas Patocka wc_lock(wc); 1176e3a35d03SMikulas Patocka memset(&wc->stats, 0, sizeof wc->stats); 1177e3a35d03SMikulas Patocka wc_unlock(wc); 1178e3a35d03SMikulas Patocka 1179e3a35d03SMikulas Patocka return 0; 1180e3a35d03SMikulas Patocka } 1181e3a35d03SMikulas Patocka 1182*86a3238cSHeinz Mauelshagen static int writecache_message(struct dm_target *ti, unsigned int argc, char **argv, 1183*86a3238cSHeinz Mauelshagen char *result, unsigned int maxlen) 118448debafeSMikulas Patocka { 118548debafeSMikulas Patocka int r = -EINVAL; 118648debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 118748debafeSMikulas Patocka 118848debafeSMikulas Patocka if (!strcasecmp(argv[0], "flush")) 118948debafeSMikulas Patocka r = process_flush_mesg(argc, argv, wc); 119048debafeSMikulas Patocka else if (!strcasecmp(argv[0], "flush_on_suspend")) 119148debafeSMikulas Patocka r = process_flush_on_suspend_mesg(argc, argv, wc); 119293de44ebSMikulas Patocka else if (!strcasecmp(argv[0], "cleaner")) 119393de44ebSMikulas Patocka r = process_cleaner_mesg(argc, argv, wc); 1194e3a35d03SMikulas Patocka else if (!strcasecmp(argv[0], "clear_stats")) 1195e3a35d03SMikulas Patocka r = process_clear_stats_mesg(argc, argv, wc); 119648debafeSMikulas Patocka else 119748debafeSMikulas Patocka DMERR("unrecognised message received: %s", argv[0]); 119848debafeSMikulas Patocka 119948debafeSMikulas Patocka return r; 120048debafeSMikulas Patocka } 120148debafeSMikulas Patocka 120248338daaSMikulas Patocka static void memcpy_flushcache_optimized(void *dest, void *source, size_t size) 120348338daaSMikulas Patocka { 120448338daaSMikulas Patocka /* 120548338daaSMikulas Patocka * clflushopt performs better with block size 1024, 2048, 4096 120648338daaSMikulas Patocka * non-temporal stores perform better with block size 512 120748338daaSMikulas Patocka * 120848338daaSMikulas Patocka * block size 512 1024 2048 4096 120948338daaSMikulas Patocka * movnti 496 MB/s 642 MB/s 725 MB/s 744 MB/s 121048338daaSMikulas Patocka * clflushopt 373 MB/s 688 MB/s 1.1 GB/s 1.2 GB/s 121148338daaSMikulas Patocka * 121248338daaSMikulas Patocka * We see that movnti performs better for 512-byte blocks, and 121348338daaSMikulas Patocka * clflushopt performs better for 1024-byte and larger blocks. So, we 121448338daaSMikulas Patocka * prefer clflushopt for sizes >= 768. 121548338daaSMikulas Patocka * 121648338daaSMikulas Patocka * NOTE: this happens to be the case now (with dm-writecache's single 121748338daaSMikulas Patocka * threaded model) but re-evaluate this once memcpy_flushcache() is 121848338daaSMikulas Patocka * enabled to use movdir64b which might invalidate this performance 121948338daaSMikulas Patocka * advantage seen with cache-allocating-writes plus flushing. 122048338daaSMikulas Patocka */ 122148338daaSMikulas Patocka #ifdef CONFIG_X86 122248338daaSMikulas Patocka if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) && 122348338daaSMikulas Patocka likely(boot_cpu_data.x86_clflush_size == 64) && 122448338daaSMikulas Patocka likely(size >= 768)) { 122548338daaSMikulas Patocka do { 122648338daaSMikulas Patocka memcpy((void *)dest, (void *)source, 64); 122748338daaSMikulas Patocka clflushopt((void *)dest); 122848338daaSMikulas Patocka dest += 64; 122948338daaSMikulas Patocka source += 64; 123048338daaSMikulas Patocka size -= 64; 123148338daaSMikulas Patocka } while (size >= 64); 123248338daaSMikulas Patocka return; 123348338daaSMikulas Patocka } 123448338daaSMikulas Patocka #endif 123548338daaSMikulas Patocka memcpy_flushcache(dest, source, size); 123648338daaSMikulas Patocka } 123748338daaSMikulas Patocka 123848debafeSMikulas Patocka static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data) 123948debafeSMikulas Patocka { 124048debafeSMikulas Patocka void *buf; 1241*86a3238cSHeinz Mauelshagen unsigned int size; 124248debafeSMikulas Patocka int rw = bio_data_dir(bio); 1243*86a3238cSHeinz Mauelshagen unsigned int remaining_size = wc->block_size; 124448debafeSMikulas Patocka 124548debafeSMikulas Patocka do { 124648debafeSMikulas Patocka struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter); 124718a6234cSChristoph Hellwig buf = bvec_kmap_local(&bv); 124848debafeSMikulas Patocka size = bv.bv_len; 124948debafeSMikulas Patocka if (unlikely(size > remaining_size)) 125048debafeSMikulas Patocka size = remaining_size; 125148debafeSMikulas Patocka 125248debafeSMikulas Patocka if (rw == READ) { 125348debafeSMikulas Patocka int r; 1254ec6347bbSDan Williams r = copy_mc_to_kernel(buf, data, size); 125548debafeSMikulas Patocka flush_dcache_page(bio_page(bio)); 125648debafeSMikulas Patocka if (unlikely(r)) { 125748debafeSMikulas Patocka writecache_error(wc, r, "hardware memory error when reading data: %d", r); 125848debafeSMikulas Patocka bio->bi_status = BLK_STS_IOERR; 125948debafeSMikulas Patocka } 126048debafeSMikulas Patocka } else { 126148debafeSMikulas Patocka flush_dcache_page(bio_page(bio)); 126248338daaSMikulas Patocka memcpy_flushcache_optimized(data, buf, size); 126348debafeSMikulas Patocka } 126448debafeSMikulas Patocka 126518a6234cSChristoph Hellwig kunmap_local(buf); 126648debafeSMikulas Patocka 126748debafeSMikulas Patocka data = (char *)data + size; 126848debafeSMikulas Patocka remaining_size -= size; 126948debafeSMikulas Patocka bio_advance(bio, size); 127048debafeSMikulas Patocka } while (unlikely(remaining_size)); 127148debafeSMikulas Patocka } 127248debafeSMikulas Patocka 127348debafeSMikulas Patocka static int writecache_flush_thread(void *data) 127448debafeSMikulas Patocka { 127548debafeSMikulas Patocka struct dm_writecache *wc = data; 127648debafeSMikulas Patocka 127748debafeSMikulas Patocka while (1) { 127848debafeSMikulas Patocka struct bio *bio; 127948debafeSMikulas Patocka 128048debafeSMikulas Patocka wc_lock(wc); 128148debafeSMikulas Patocka bio = bio_list_pop(&wc->flush_list); 128248debafeSMikulas Patocka if (!bio) { 128348debafeSMikulas Patocka set_current_state(TASK_INTERRUPTIBLE); 128448debafeSMikulas Patocka wc_unlock(wc); 128548debafeSMikulas Patocka 128648debafeSMikulas Patocka if (unlikely(kthread_should_stop())) { 128748debafeSMikulas Patocka set_current_state(TASK_RUNNING); 128848debafeSMikulas Patocka break; 128948debafeSMikulas Patocka } 129048debafeSMikulas Patocka 129148debafeSMikulas Patocka schedule(); 129248debafeSMikulas Patocka continue; 129348debafeSMikulas Patocka } 129448debafeSMikulas Patocka 129548debafeSMikulas Patocka if (bio_op(bio) == REQ_OP_DISCARD) { 129648debafeSMikulas Patocka writecache_discard(wc, bio->bi_iter.bi_sector, 129748debafeSMikulas Patocka bio_end_sector(bio)); 129848debafeSMikulas Patocka wc_unlock(wc); 129948debafeSMikulas Patocka bio_set_dev(bio, wc->dev->bdev); 1300ed00aabdSChristoph Hellwig submit_bio_noacct(bio); 130148debafeSMikulas Patocka } else { 130248debafeSMikulas Patocka writecache_flush(wc); 130348debafeSMikulas Patocka wc_unlock(wc); 130448debafeSMikulas Patocka if (writecache_has_error(wc)) 130548debafeSMikulas Patocka bio->bi_status = BLK_STS_IOERR; 130648debafeSMikulas Patocka bio_endio(bio); 130748debafeSMikulas Patocka } 130848debafeSMikulas Patocka } 130948debafeSMikulas Patocka 131048debafeSMikulas Patocka return 0; 131148debafeSMikulas Patocka } 131248debafeSMikulas Patocka 131348debafeSMikulas Patocka static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio) 131448debafeSMikulas Patocka { 131548debafeSMikulas Patocka if (bio_list_empty(&wc->flush_list)) 131648debafeSMikulas Patocka wake_up_process(wc->flush_thread); 131748debafeSMikulas Patocka bio_list_add(&wc->flush_list, bio); 131848debafeSMikulas Patocka } 131948debafeSMikulas Patocka 1320cdd4d783SMike Snitzer enum wc_map_op { 1321cdd4d783SMike Snitzer WC_MAP_SUBMIT, 1322cdd4d783SMike Snitzer WC_MAP_REMAP, 1323cdd4d783SMike Snitzer WC_MAP_REMAP_ORIGIN, 1324cdd4d783SMike Snitzer WC_MAP_RETURN, 1325cdd4d783SMike Snitzer WC_MAP_ERROR, 1326cdd4d783SMike Snitzer }; 1327cdd4d783SMike Snitzer 13289bc0c92eSMikulas Patocka static void writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio, 13294d020b3aSMike Snitzer struct wc_entry *e) 13304d020b3aSMike Snitzer { 13314d020b3aSMike Snitzer if (e) { 13324d020b3aSMike Snitzer sector_t next_boundary = 13334d020b3aSMike Snitzer read_original_sector(wc, e) - bio->bi_iter.bi_sector; 13344d020b3aSMike Snitzer if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) 13354d020b3aSMike Snitzer dm_accept_partial_bio(bio, next_boundary); 13364d020b3aSMike Snitzer } 13374d020b3aSMike Snitzer } 13384d020b3aSMike Snitzer 1339cdd4d783SMike Snitzer static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio) 1340cdd4d783SMike Snitzer { 1341cdd4d783SMike Snitzer enum wc_map_op map_op; 1342cdd4d783SMike Snitzer struct wc_entry *e; 1343cdd4d783SMike Snitzer 1344cdd4d783SMike Snitzer read_next_block: 1345e3a35d03SMikulas Patocka wc->stats.reads++; 1346cdd4d783SMike Snitzer e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); 1347cdd4d783SMike Snitzer if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) { 1348e3a35d03SMikulas Patocka wc->stats.read_hits++; 1349cdd4d783SMike Snitzer if (WC_MODE_PMEM(wc)) { 1350cdd4d783SMike Snitzer bio_copy_block(wc, bio, memory_data(wc, e)); 1351cdd4d783SMike Snitzer if (bio->bi_iter.bi_size) 1352cdd4d783SMike Snitzer goto read_next_block; 1353cdd4d783SMike Snitzer map_op = WC_MAP_SUBMIT; 1354cdd4d783SMike Snitzer } else { 1355cdd4d783SMike Snitzer dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT); 1356cdd4d783SMike Snitzer bio_set_dev(bio, wc->ssd_dev->bdev); 1357cdd4d783SMike Snitzer bio->bi_iter.bi_sector = cache_sector(wc, e); 1358cdd4d783SMike Snitzer if (!writecache_entry_is_committed(wc, e)) 1359cdd4d783SMike Snitzer writecache_wait_for_ios(wc, WRITE); 1360cdd4d783SMike Snitzer map_op = WC_MAP_REMAP; 1361cdd4d783SMike Snitzer } 1362cdd4d783SMike Snitzer } else { 13639bc0c92eSMikulas Patocka writecache_map_remap_origin(wc, bio, e); 13642c6e755bSMikulas Patocka wc->stats.reads += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits; 13659bc0c92eSMikulas Patocka map_op = WC_MAP_REMAP_ORIGIN; 1366cdd4d783SMike Snitzer } 1367cdd4d783SMike Snitzer 1368cdd4d783SMike Snitzer return map_op; 1369cdd4d783SMike Snitzer } 1370cdd4d783SMike Snitzer 13719bc0c92eSMikulas Patocka static void writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio, 1372cdd4d783SMike Snitzer struct wc_entry *e, bool search_used) 1373cdd4d783SMike Snitzer { 1374*86a3238cSHeinz Mauelshagen unsigned int bio_size = wc->block_size; 1375cdd4d783SMike Snitzer sector_t start_cache_sec = cache_sector(wc, e); 1376cdd4d783SMike Snitzer sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT); 1377cdd4d783SMike Snitzer 1378cdd4d783SMike Snitzer while (bio_size < bio->bi_iter.bi_size) { 1379cdd4d783SMike Snitzer if (!search_used) { 1380cdd4d783SMike Snitzer struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec); 1381cdd4d783SMike Snitzer if (!f) 1382cdd4d783SMike Snitzer break; 1383cdd4d783SMike Snitzer write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector + 1384cdd4d783SMike Snitzer (bio_size >> SECTOR_SHIFT), wc->seq_count); 1385cdd4d783SMike Snitzer writecache_insert_entry(wc, f); 1386cdd4d783SMike Snitzer wc->uncommitted_blocks++; 1387cdd4d783SMike Snitzer } else { 1388cdd4d783SMike Snitzer struct wc_entry *f; 1389cdd4d783SMike Snitzer struct rb_node *next = rb_next(&e->rb_node); 1390cdd4d783SMike Snitzer if (!next) 1391cdd4d783SMike Snitzer break; 1392cdd4d783SMike Snitzer f = container_of(next, struct wc_entry, rb_node); 1393cdd4d783SMike Snitzer if (f != e + 1) 1394cdd4d783SMike Snitzer break; 1395cdd4d783SMike Snitzer if (read_original_sector(wc, f) != 1396cdd4d783SMike Snitzer read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) 1397cdd4d783SMike Snitzer break; 1398cdd4d783SMike Snitzer if (unlikely(f->write_in_progress)) 1399cdd4d783SMike Snitzer break; 1400cdd4d783SMike Snitzer if (writecache_entry_is_committed(wc, f)) 1401cdd4d783SMike Snitzer wc->overwrote_committed = true; 1402cdd4d783SMike Snitzer e = f; 1403cdd4d783SMike Snitzer } 1404cdd4d783SMike Snitzer bio_size += wc->block_size; 1405cdd4d783SMike Snitzer current_cache_sec += wc->block_size >> SECTOR_SHIFT; 1406cdd4d783SMike Snitzer } 1407cdd4d783SMike Snitzer 1408cdd4d783SMike Snitzer bio_set_dev(bio, wc->ssd_dev->bdev); 1409cdd4d783SMike Snitzer bio->bi_iter.bi_sector = start_cache_sec; 1410cdd4d783SMike Snitzer dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT); 1411cdd4d783SMike Snitzer 1412b2676e14SMikulas Patocka wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; 1413b2676e14SMikulas Patocka wc->stats.writes_allocate += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits; 1414b2676e14SMikulas Patocka 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 1423cdd4d783SMike Snitzer static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio) 142448debafeSMikulas Patocka { 142548debafeSMikulas Patocka struct wc_entry *e; 1426cdd4d783SMike Snitzer 1427cdd4d783SMike Snitzer do { 1428cdd4d783SMike Snitzer bool found_entry = false; 1429cdd4d783SMike Snitzer bool search_used = false; 1430b2676e14SMikulas Patocka if (writecache_has_error(wc)) { 1431b2676e14SMikulas Patocka wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; 1432cdd4d783SMike Snitzer return WC_MAP_ERROR; 1433b2676e14SMikulas Patocka } 1434cdd4d783SMike Snitzer e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0); 1435cdd4d783SMike Snitzer if (e) { 1436cdd4d783SMike Snitzer if (!writecache_entry_is_committed(wc, e)) { 1437e3a35d03SMikulas Patocka wc->stats.write_hits_uncommitted++; 1438cdd4d783SMike Snitzer search_used = true; 1439cdd4d783SMike Snitzer goto bio_copy; 1440cdd4d783SMike Snitzer } 1441e3a35d03SMikulas Patocka wc->stats.write_hits_committed++; 1442cdd4d783SMike Snitzer if (!WC_MODE_PMEM(wc) && !e->write_in_progress) { 1443cdd4d783SMike Snitzer wc->overwrote_committed = true; 1444cdd4d783SMike Snitzer search_used = true; 1445cdd4d783SMike Snitzer goto bio_copy; 1446cdd4d783SMike Snitzer } 1447cdd4d783SMike Snitzer found_entry = true; 1448cdd4d783SMike Snitzer } else { 1449cdd4d783SMike Snitzer if (unlikely(wc->cleaner) || 1450cdd4d783SMike Snitzer (wc->metadata_only && !(bio->bi_opf & REQ_META))) 1451cdd4d783SMike Snitzer goto direct_write; 1452cdd4d783SMike Snitzer } 1453cdd4d783SMike Snitzer e = writecache_pop_from_freelist(wc, (sector_t)-1); 1454cdd4d783SMike Snitzer if (unlikely(!e)) { 1455cdd4d783SMike Snitzer if (!WC_MODE_PMEM(wc) && !found_entry) { 1456cdd4d783SMike Snitzer direct_write: 1457cdd4d783SMike Snitzer e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); 14589bc0c92eSMikulas Patocka writecache_map_remap_origin(wc, bio, e); 1459b2676e14SMikulas Patocka wc->stats.writes_around += bio->bi_iter.bi_size >> wc->block_size_bits; 1460b2676e14SMikulas Patocka wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; 14619bc0c92eSMikulas Patocka return WC_MAP_REMAP_ORIGIN; 1462cdd4d783SMike Snitzer } 1463e3a35d03SMikulas Patocka wc->stats.writes_blocked_on_freelist++; 1464cdd4d783SMike Snitzer writecache_wait_on_freelist(wc); 1465cdd4d783SMike Snitzer continue; 1466cdd4d783SMike Snitzer } 1467cdd4d783SMike Snitzer write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count); 1468cdd4d783SMike Snitzer writecache_insert_entry(wc, e); 1469cdd4d783SMike Snitzer wc->uncommitted_blocks++; 1470e3a35d03SMikulas Patocka wc->stats.writes_allocate++; 1471cdd4d783SMike Snitzer bio_copy: 14729bc0c92eSMikulas Patocka if (WC_MODE_PMEM(wc)) { 1473cdd4d783SMike Snitzer bio_copy_block(wc, bio, memory_data(wc, e)); 1474b2676e14SMikulas Patocka wc->stats.writes++; 14759bc0c92eSMikulas Patocka } else { 14769bc0c92eSMikulas Patocka writecache_bio_copy_ssd(wc, bio, e, search_used); 14779bc0c92eSMikulas Patocka return WC_MAP_REMAP; 14789bc0c92eSMikulas Patocka } 1479cdd4d783SMike Snitzer } while (bio->bi_iter.bi_size); 1480cdd4d783SMike Snitzer 1481cdd4d783SMike Snitzer if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks)) 1482cdd4d783SMike Snitzer writecache_flush(wc); 1483cdd4d783SMike Snitzer else 1484cdd4d783SMike Snitzer writecache_schedule_autocommit(wc); 1485cdd4d783SMike Snitzer 1486cdd4d783SMike Snitzer return WC_MAP_SUBMIT; 1487cdd4d783SMike Snitzer } 1488cdd4d783SMike Snitzer 148915cb6f39SMike Snitzer static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio) 149015cb6f39SMike Snitzer { 149115cb6f39SMike Snitzer if (writecache_has_error(wc)) 149215cb6f39SMike Snitzer return WC_MAP_ERROR; 149315cb6f39SMike Snitzer 149415cb6f39SMike Snitzer if (WC_MODE_PMEM(wc)) { 1495e3a35d03SMikulas Patocka wc->stats.flushes++; 149615cb6f39SMike Snitzer writecache_flush(wc); 149715cb6f39SMike Snitzer if (writecache_has_error(wc)) 149815cb6f39SMike Snitzer return WC_MAP_ERROR; 149915cb6f39SMike Snitzer else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only)) 150015cb6f39SMike Snitzer return WC_MAP_REMAP_ORIGIN; 150115cb6f39SMike Snitzer return WC_MAP_SUBMIT; 150215cb6f39SMike Snitzer } 150315cb6f39SMike Snitzer /* SSD: */ 150415cb6f39SMike Snitzer if (dm_bio_get_target_bio_nr(bio)) 150515cb6f39SMike Snitzer return WC_MAP_REMAP_ORIGIN; 1506e3a35d03SMikulas Patocka wc->stats.flushes++; 150715cb6f39SMike Snitzer writecache_offload_bio(wc, bio); 150815cb6f39SMike Snitzer return WC_MAP_RETURN; 150915cb6f39SMike Snitzer } 151015cb6f39SMike Snitzer 151115cb6f39SMike Snitzer static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio) 151215cb6f39SMike Snitzer { 15132ee73ef6SMikulas Patocka wc->stats.discards += bio->bi_iter.bi_size >> wc->block_size_bits; 1514e3a35d03SMikulas Patocka 151515cb6f39SMike Snitzer if (writecache_has_error(wc)) 151615cb6f39SMike Snitzer return WC_MAP_ERROR; 151715cb6f39SMike Snitzer 151815cb6f39SMike Snitzer if (WC_MODE_PMEM(wc)) { 151915cb6f39SMike Snitzer writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio)); 152015cb6f39SMike Snitzer return WC_MAP_REMAP_ORIGIN; 152115cb6f39SMike Snitzer } 152215cb6f39SMike Snitzer /* SSD: */ 152315cb6f39SMike Snitzer writecache_offload_bio(wc, bio); 152415cb6f39SMike Snitzer return WC_MAP_RETURN; 152515cb6f39SMike Snitzer } 152615cb6f39SMike Snitzer 1527cdd4d783SMike Snitzer static int writecache_map(struct dm_target *ti, struct bio *bio) 1528cdd4d783SMike Snitzer { 152948debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 153015cb6f39SMike Snitzer enum wc_map_op map_op; 153148debafeSMikulas Patocka 153248debafeSMikulas Patocka bio->bi_private = NULL; 153348debafeSMikulas Patocka 153448debafeSMikulas Patocka wc_lock(wc); 153548debafeSMikulas Patocka 153648debafeSMikulas Patocka if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { 153715cb6f39SMike Snitzer map_op = writecache_map_flush(wc, bio); 153815cb6f39SMike Snitzer goto done; 153948debafeSMikulas Patocka } 154048debafeSMikulas Patocka 154148debafeSMikulas Patocka bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector); 154248debafeSMikulas Patocka 1543*86a3238cSHeinz Mauelshagen if (unlikely((((unsigned int)bio->bi_iter.bi_sector | bio_sectors(bio)) & 154448debafeSMikulas Patocka (wc->block_size / 512 - 1)) != 0)) { 154548debafeSMikulas Patocka DMERR("I/O is not aligned, sector %llu, size %u, block size %u", 154648debafeSMikulas Patocka (unsigned long long)bio->bi_iter.bi_sector, 154748debafeSMikulas Patocka bio->bi_iter.bi_size, wc->block_size); 154815cb6f39SMike Snitzer map_op = WC_MAP_ERROR; 154915cb6f39SMike Snitzer goto done; 155048debafeSMikulas Patocka } 155148debafeSMikulas Patocka 155248debafeSMikulas Patocka if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) { 155315cb6f39SMike Snitzer map_op = writecache_map_discard(wc, bio); 155415cb6f39SMike Snitzer goto done; 155548debafeSMikulas Patocka } 155648debafeSMikulas Patocka 1557cdd4d783SMike Snitzer if (bio_data_dir(bio) == READ) 1558cdd4d783SMike Snitzer map_op = writecache_map_read(wc, bio); 155948debafeSMikulas Patocka else 1560cdd4d783SMike Snitzer map_op = writecache_map_write(wc, bio); 156115cb6f39SMike Snitzer done: 1562cdd4d783SMike Snitzer switch (map_op) { 1563cdd4d783SMike Snitzer case WC_MAP_REMAP_ORIGIN: 15645c0de3d7SMikulas Patocka if (likely(wc->pause != 0)) { 15655c0de3d7SMikulas Patocka if (bio_op(bio) == REQ_OP_WRITE) { 156695b88f4dSMikulas Patocka dm_iot_io_begin(&wc->iot, 1); 156795b88f4dSMikulas Patocka bio->bi_private = (void *)2; 156895b88f4dSMikulas Patocka } 15695c0de3d7SMikulas Patocka } 157048debafeSMikulas Patocka bio_set_dev(bio, wc->dev->bdev); 157148debafeSMikulas Patocka wc_unlock(wc); 157248debafeSMikulas Patocka return DM_MAPIO_REMAPPED; 157348debafeSMikulas Patocka 1574cdd4d783SMike Snitzer case WC_MAP_REMAP: 157548debafeSMikulas Patocka /* make sure that writecache_end_io decrements bio_in_progress: */ 157648debafeSMikulas Patocka bio->bi_private = (void *)1; 157748debafeSMikulas Patocka atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]); 157848debafeSMikulas Patocka wc_unlock(wc); 157948debafeSMikulas Patocka return DM_MAPIO_REMAPPED; 158048debafeSMikulas Patocka 1581cdd4d783SMike Snitzer case WC_MAP_SUBMIT: 158248debafeSMikulas Patocka wc_unlock(wc); 158348debafeSMikulas Patocka bio_endio(bio); 158448debafeSMikulas Patocka return DM_MAPIO_SUBMITTED; 158548debafeSMikulas Patocka 1586cdd4d783SMike Snitzer case WC_MAP_RETURN: 158748debafeSMikulas Patocka wc_unlock(wc); 158848debafeSMikulas Patocka return DM_MAPIO_SUBMITTED; 158948debafeSMikulas Patocka 1590cdd4d783SMike Snitzer case WC_MAP_ERROR: 159148debafeSMikulas Patocka wc_unlock(wc); 159248debafeSMikulas Patocka bio_io_error(bio); 159348debafeSMikulas Patocka return DM_MAPIO_SUBMITTED; 1594df699cc1SMikulas Patocka 1595df699cc1SMikulas Patocka default: 1596df699cc1SMikulas Patocka BUG(); 1597b7f362d6SMikulas Patocka wc_unlock(wc); 1598b7f362d6SMikulas Patocka return DM_MAPIO_KILL; 159948debafeSMikulas Patocka } 1600cdd4d783SMike Snitzer } 160148debafeSMikulas Patocka 160248debafeSMikulas Patocka static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status) 160348debafeSMikulas Patocka { 160448debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 160548debafeSMikulas Patocka 160695b88f4dSMikulas Patocka if (bio->bi_private == (void *)1) { 160748debafeSMikulas Patocka int dir = bio_data_dir(bio); 160848debafeSMikulas Patocka if (atomic_dec_and_test(&wc->bio_in_progress[dir])) 160948debafeSMikulas Patocka if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir]))) 161048debafeSMikulas Patocka wake_up(&wc->bio_in_progress_wait[dir]); 161195b88f4dSMikulas Patocka } else if (bio->bi_private == (void *)2) { 161295b88f4dSMikulas Patocka dm_iot_io_end(&wc->iot, 1); 161348debafeSMikulas Patocka } 161448debafeSMikulas Patocka return 0; 161548debafeSMikulas Patocka } 161648debafeSMikulas Patocka 161748debafeSMikulas Patocka static int writecache_iterate_devices(struct dm_target *ti, 161848debafeSMikulas Patocka iterate_devices_callout_fn fn, void *data) 161948debafeSMikulas Patocka { 162048debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 162148debafeSMikulas Patocka 162248debafeSMikulas Patocka return fn(ti, wc->dev, 0, ti->len, data); 162348debafeSMikulas Patocka } 162448debafeSMikulas Patocka 162548debafeSMikulas Patocka static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits) 162648debafeSMikulas Patocka { 162748debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 162848debafeSMikulas Patocka 162948debafeSMikulas Patocka if (limits->logical_block_size < wc->block_size) 163048debafeSMikulas Patocka limits->logical_block_size = wc->block_size; 163148debafeSMikulas Patocka 163248debafeSMikulas Patocka if (limits->physical_block_size < wc->block_size) 163348debafeSMikulas Patocka limits->physical_block_size = wc->block_size; 163448debafeSMikulas Patocka 163548debafeSMikulas Patocka if (limits->io_min < wc->block_size) 163648debafeSMikulas Patocka limits->io_min = wc->block_size; 163748debafeSMikulas Patocka } 163848debafeSMikulas Patocka 163948debafeSMikulas Patocka 164048debafeSMikulas Patocka static void writecache_writeback_endio(struct bio *bio) 164148debafeSMikulas Patocka { 164248debafeSMikulas Patocka struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio); 164348debafeSMikulas Patocka struct dm_writecache *wc = wb->wc; 164448debafeSMikulas Patocka unsigned long flags; 164548debafeSMikulas Patocka 164648debafeSMikulas Patocka raw_spin_lock_irqsave(&wc->endio_list_lock, flags); 164748debafeSMikulas Patocka if (unlikely(list_empty(&wc->endio_list))) 164848debafeSMikulas Patocka wake_up_process(wc->endio_thread); 164948debafeSMikulas Patocka list_add_tail(&wb->endio_entry, &wc->endio_list); 165048debafeSMikulas Patocka raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags); 165148debafeSMikulas Patocka } 165248debafeSMikulas Patocka 165348debafeSMikulas Patocka static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr) 165448debafeSMikulas Patocka { 165548debafeSMikulas Patocka struct copy_struct *c = ptr; 165648debafeSMikulas Patocka struct dm_writecache *wc = c->wc; 165748debafeSMikulas Patocka 165848debafeSMikulas Patocka c->error = likely(!(read_err | write_err)) ? 0 : -EIO; 165948debafeSMikulas Patocka 166048debafeSMikulas Patocka raw_spin_lock_irq(&wc->endio_list_lock); 166148debafeSMikulas Patocka if (unlikely(list_empty(&wc->endio_list))) 166248debafeSMikulas Patocka wake_up_process(wc->endio_thread); 166348debafeSMikulas Patocka list_add_tail(&c->endio_entry, &wc->endio_list); 166448debafeSMikulas Patocka raw_spin_unlock_irq(&wc->endio_list_lock); 166548debafeSMikulas Patocka } 166648debafeSMikulas Patocka 166748debafeSMikulas Patocka static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list) 166848debafeSMikulas Patocka { 1669*86a3238cSHeinz Mauelshagen unsigned int i; 167048debafeSMikulas Patocka struct writeback_struct *wb; 167148debafeSMikulas Patocka struct wc_entry *e; 167248debafeSMikulas Patocka unsigned long n_walked = 0; 167348debafeSMikulas Patocka 167448debafeSMikulas Patocka do { 167548debafeSMikulas Patocka wb = list_entry(list->next, struct writeback_struct, endio_entry); 167648debafeSMikulas Patocka list_del(&wb->endio_entry); 167748debafeSMikulas Patocka 167848debafeSMikulas Patocka if (unlikely(wb->bio.bi_status != BLK_STS_OK)) 167948debafeSMikulas Patocka writecache_error(wc, blk_status_to_errno(wb->bio.bi_status), 168048debafeSMikulas Patocka "write error %d", wb->bio.bi_status); 168148debafeSMikulas Patocka i = 0; 168248debafeSMikulas Patocka do { 168348debafeSMikulas Patocka e = wb->wc_list[i]; 168448debafeSMikulas Patocka BUG_ON(!e->write_in_progress); 168548debafeSMikulas Patocka e->write_in_progress = false; 168648debafeSMikulas Patocka INIT_LIST_HEAD(&e->lru); 168748debafeSMikulas Patocka if (!writecache_has_error(wc)) 168848debafeSMikulas Patocka writecache_free_entry(wc, e); 168948debafeSMikulas Patocka BUG_ON(!wc->writeback_size); 169048debafeSMikulas Patocka wc->writeback_size--; 169148debafeSMikulas Patocka n_walked++; 169248debafeSMikulas Patocka if (unlikely(n_walked >= ENDIO_LATENCY)) { 1693aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 169448debafeSMikulas Patocka wc_unlock(wc); 169548debafeSMikulas Patocka wc_lock(wc); 169648debafeSMikulas Patocka n_walked = 0; 169748debafeSMikulas Patocka } 169848debafeSMikulas Patocka } while (++i < wb->wc_list_n); 169948debafeSMikulas Patocka 170048debafeSMikulas Patocka if (wb->wc_list != wb->wc_list_inline) 170148debafeSMikulas Patocka kfree(wb->wc_list); 170248debafeSMikulas Patocka bio_put(&wb->bio); 170348debafeSMikulas Patocka } while (!list_empty(list)); 170448debafeSMikulas Patocka } 170548debafeSMikulas Patocka 170648debafeSMikulas Patocka static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list) 170748debafeSMikulas Patocka { 170848debafeSMikulas Patocka struct copy_struct *c; 170948debafeSMikulas Patocka struct wc_entry *e; 171048debafeSMikulas Patocka 171148debafeSMikulas Patocka do { 171248debafeSMikulas Patocka c = list_entry(list->next, struct copy_struct, endio_entry); 171348debafeSMikulas Patocka list_del(&c->endio_entry); 171448debafeSMikulas Patocka 171548debafeSMikulas Patocka if (unlikely(c->error)) 171648debafeSMikulas Patocka writecache_error(wc, c->error, "copy error"); 171748debafeSMikulas Patocka 171848debafeSMikulas Patocka e = c->e; 171948debafeSMikulas Patocka do { 172048debafeSMikulas Patocka BUG_ON(!e->write_in_progress); 172148debafeSMikulas Patocka e->write_in_progress = false; 172248debafeSMikulas Patocka INIT_LIST_HEAD(&e->lru); 172348debafeSMikulas Patocka if (!writecache_has_error(wc)) 172448debafeSMikulas Patocka writecache_free_entry(wc, e); 172548debafeSMikulas Patocka 172648debafeSMikulas Patocka BUG_ON(!wc->writeback_size); 172748debafeSMikulas Patocka wc->writeback_size--; 172848debafeSMikulas Patocka e++; 172948debafeSMikulas Patocka } while (--c->n_entries); 173048debafeSMikulas Patocka mempool_free(c, &wc->copy_pool); 173148debafeSMikulas Patocka } while (!list_empty(list)); 173248debafeSMikulas Patocka } 173348debafeSMikulas Patocka 173448debafeSMikulas Patocka static int writecache_endio_thread(void *data) 173548debafeSMikulas Patocka { 173648debafeSMikulas Patocka struct dm_writecache *wc = data; 173748debafeSMikulas Patocka 173848debafeSMikulas Patocka while (1) { 173948debafeSMikulas Patocka struct list_head list; 174048debafeSMikulas Patocka 174148debafeSMikulas Patocka raw_spin_lock_irq(&wc->endio_list_lock); 174248debafeSMikulas Patocka if (!list_empty(&wc->endio_list)) 174348debafeSMikulas Patocka goto pop_from_list; 174448debafeSMikulas Patocka set_current_state(TASK_INTERRUPTIBLE); 174548debafeSMikulas Patocka raw_spin_unlock_irq(&wc->endio_list_lock); 174648debafeSMikulas Patocka 174748debafeSMikulas Patocka if (unlikely(kthread_should_stop())) { 174848debafeSMikulas Patocka set_current_state(TASK_RUNNING); 174948debafeSMikulas Patocka break; 175048debafeSMikulas Patocka } 175148debafeSMikulas Patocka 175248debafeSMikulas Patocka schedule(); 175348debafeSMikulas Patocka 175448debafeSMikulas Patocka continue; 175548debafeSMikulas Patocka 175648debafeSMikulas Patocka pop_from_list: 175748debafeSMikulas Patocka list = wc->endio_list; 175848debafeSMikulas Patocka list.next->prev = list.prev->next = &list; 175948debafeSMikulas Patocka INIT_LIST_HEAD(&wc->endio_list); 176048debafeSMikulas Patocka raw_spin_unlock_irq(&wc->endio_list_lock); 176148debafeSMikulas Patocka 176248debafeSMikulas Patocka if (!WC_MODE_FUA(wc)) 176348debafeSMikulas Patocka writecache_disk_flush(wc, wc->dev); 176448debafeSMikulas Patocka 176548debafeSMikulas Patocka wc_lock(wc); 176648debafeSMikulas Patocka 176748debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 176848debafeSMikulas Patocka __writecache_endio_pmem(wc, &list); 176948debafeSMikulas Patocka } else { 177048debafeSMikulas Patocka __writecache_endio_ssd(wc, &list); 177148debafeSMikulas Patocka writecache_wait_for_ios(wc, READ); 177248debafeSMikulas Patocka } 177348debafeSMikulas Patocka 1774aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 177548debafeSMikulas Patocka 177648debafeSMikulas Patocka wc_unlock(wc); 177748debafeSMikulas Patocka } 177848debafeSMikulas Patocka 177948debafeSMikulas Patocka return 0; 178048debafeSMikulas Patocka } 178148debafeSMikulas Patocka 1782620cbe40SMikulas Patocka static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e) 178348debafeSMikulas Patocka { 178448debafeSMikulas Patocka struct dm_writecache *wc = wb->wc; 1785*86a3238cSHeinz Mauelshagen unsigned int block_size = wc->block_size; 178648debafeSMikulas Patocka void *address = memory_data(wc, e); 178748debafeSMikulas Patocka 178848debafeSMikulas Patocka persistent_memory_flush_cache(address, block_size); 17894134455fSMikulas Patocka 17904134455fSMikulas Patocka if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors)) 17914134455fSMikulas Patocka return true; 17924134455fSMikulas Patocka 179348debafeSMikulas Patocka return bio_add_page(&wb->bio, persistent_memory_page(address), 179448debafeSMikulas Patocka block_size, persistent_memory_page_offset(address)) != 0; 179548debafeSMikulas Patocka } 179648debafeSMikulas Patocka 179748debafeSMikulas Patocka struct writeback_list { 179848debafeSMikulas Patocka struct list_head list; 179948debafeSMikulas Patocka size_t size; 180048debafeSMikulas Patocka }; 180148debafeSMikulas Patocka 180248debafeSMikulas Patocka static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl) 180348debafeSMikulas Patocka { 180448debafeSMikulas Patocka if (unlikely(wc->max_writeback_jobs)) { 180548debafeSMikulas Patocka if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) { 180648debafeSMikulas Patocka wc_lock(wc); 180748debafeSMikulas Patocka while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs) 180848debafeSMikulas Patocka writecache_wait_on_freelist(wc); 180948debafeSMikulas Patocka wc_unlock(wc); 181048debafeSMikulas Patocka } 181148debafeSMikulas Patocka } 181248debafeSMikulas Patocka cond_resched(); 181348debafeSMikulas Patocka } 181448debafeSMikulas Patocka 181548debafeSMikulas Patocka static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl) 181648debafeSMikulas Patocka { 181748debafeSMikulas Patocka struct wc_entry *e, *f; 181848debafeSMikulas Patocka struct bio *bio; 181948debafeSMikulas Patocka struct writeback_struct *wb; 1820*86a3238cSHeinz Mauelshagen unsigned int max_pages; 182148debafeSMikulas Patocka 182248debafeSMikulas Patocka while (wbl->size) { 182348debafeSMikulas Patocka wbl->size--; 182448debafeSMikulas Patocka e = container_of(wbl->list.prev, struct wc_entry, lru); 182548debafeSMikulas Patocka list_del(&e->lru); 182648debafeSMikulas Patocka 182748debafeSMikulas Patocka max_pages = e->wc_list_contiguous; 182848debafeSMikulas Patocka 1829609be106SChristoph Hellwig bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE, 1830609be106SChristoph Hellwig GFP_NOIO, &wc->bio_set); 183148debafeSMikulas Patocka wb = container_of(bio, struct writeback_struct, bio); 183248debafeSMikulas Patocka wb->wc = wc; 183309f2d656SHuaisheng Ye bio->bi_end_io = writecache_writeback_endio; 183409f2d656SHuaisheng Ye bio->bi_iter.bi_sector = read_original_sector(wc, e); 183548debafeSMikulas Patocka if (max_pages <= WB_LIST_INLINE || 183650a7d3baSKees Cook unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *), 183748debafeSMikulas Patocka GFP_NOIO | __GFP_NORETRY | 183848debafeSMikulas Patocka __GFP_NOMEMALLOC | __GFP_NOWARN)))) { 183948debafeSMikulas Patocka wb->wc_list = wb->wc_list_inline; 184048debafeSMikulas Patocka max_pages = WB_LIST_INLINE; 184148debafeSMikulas Patocka } 184248debafeSMikulas Patocka 1843620cbe40SMikulas Patocka BUG_ON(!wc_add_block(wb, e)); 184448debafeSMikulas Patocka 184548debafeSMikulas Patocka wb->wc_list[0] = e; 184648debafeSMikulas Patocka wb->wc_list_n = 1; 184748debafeSMikulas Patocka 184848debafeSMikulas Patocka while (wbl->size && wb->wc_list_n < max_pages) { 184948debafeSMikulas Patocka f = container_of(wbl->list.prev, struct wc_entry, lru); 185048debafeSMikulas Patocka if (read_original_sector(wc, f) != 185148debafeSMikulas Patocka read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) 185248debafeSMikulas Patocka break; 1853620cbe40SMikulas Patocka if (!wc_add_block(wb, f)) 185448debafeSMikulas Patocka break; 185548debafeSMikulas Patocka wbl->size--; 185648debafeSMikulas Patocka list_del(&f->lru); 185748debafeSMikulas Patocka wb->wc_list[wb->wc_list_n++] = f; 185848debafeSMikulas Patocka e = f; 185948debafeSMikulas Patocka } 1860609be106SChristoph Hellwig if (WC_MODE_FUA(wc)) 1861609be106SChristoph Hellwig bio->bi_opf |= REQ_FUA; 186248debafeSMikulas Patocka if (writecache_has_error(wc)) { 186348debafeSMikulas Patocka bio->bi_status = BLK_STS_IOERR; 186409f2d656SHuaisheng Ye bio_endio(bio); 18654134455fSMikulas Patocka } else if (unlikely(!bio_sectors(bio))) { 18664134455fSMikulas Patocka bio->bi_status = BLK_STS_OK; 18674134455fSMikulas Patocka bio_endio(bio); 186848debafeSMikulas Patocka } else { 186909f2d656SHuaisheng Ye submit_bio(bio); 187048debafeSMikulas Patocka } 187148debafeSMikulas Patocka 187248debafeSMikulas Patocka __writeback_throttle(wc, wbl); 187348debafeSMikulas Patocka } 187448debafeSMikulas Patocka } 187548debafeSMikulas Patocka 187648debafeSMikulas Patocka static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl) 187748debafeSMikulas Patocka { 187848debafeSMikulas Patocka struct wc_entry *e, *f; 187948debafeSMikulas Patocka struct dm_io_region from, to; 188048debafeSMikulas Patocka struct copy_struct *c; 188148debafeSMikulas Patocka 188248debafeSMikulas Patocka while (wbl->size) { 1883*86a3238cSHeinz Mauelshagen unsigned int n_sectors; 188448debafeSMikulas Patocka 188548debafeSMikulas Patocka wbl->size--; 188648debafeSMikulas Patocka e = container_of(wbl->list.prev, struct wc_entry, lru); 188748debafeSMikulas Patocka list_del(&e->lru); 188848debafeSMikulas Patocka 188948debafeSMikulas Patocka n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT); 189048debafeSMikulas Patocka 189148debafeSMikulas Patocka from.bdev = wc->ssd_dev->bdev; 189248debafeSMikulas Patocka from.sector = cache_sector(wc, e); 189348debafeSMikulas Patocka from.count = n_sectors; 189448debafeSMikulas Patocka to.bdev = wc->dev->bdev; 189548debafeSMikulas Patocka to.sector = read_original_sector(wc, e); 189648debafeSMikulas Patocka to.count = n_sectors; 189748debafeSMikulas Patocka 189848debafeSMikulas Patocka c = mempool_alloc(&wc->copy_pool, GFP_NOIO); 189948debafeSMikulas Patocka c->wc = wc; 190048debafeSMikulas Patocka c->e = e; 190148debafeSMikulas Patocka c->n_entries = e->wc_list_contiguous; 190248debafeSMikulas Patocka 190348debafeSMikulas Patocka while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) { 190448debafeSMikulas Patocka wbl->size--; 190548debafeSMikulas Patocka f = container_of(wbl->list.prev, struct wc_entry, lru); 190648debafeSMikulas Patocka BUG_ON(f != e + 1); 190748debafeSMikulas Patocka list_del(&f->lru); 190848debafeSMikulas Patocka e = f; 190948debafeSMikulas Patocka } 191048debafeSMikulas Patocka 19114134455fSMikulas Patocka if (unlikely(to.sector + to.count > wc->data_device_sectors)) { 19124134455fSMikulas Patocka if (to.sector >= wc->data_device_sectors) { 19134134455fSMikulas Patocka writecache_copy_endio(0, 0, c); 19144134455fSMikulas Patocka continue; 19154134455fSMikulas Patocka } 19164134455fSMikulas Patocka from.count = to.count = wc->data_device_sectors - to.sector; 19174134455fSMikulas Patocka } 19184134455fSMikulas Patocka 191948debafeSMikulas Patocka dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c); 192048debafeSMikulas Patocka 192148debafeSMikulas Patocka __writeback_throttle(wc, wbl); 192248debafeSMikulas Patocka } 192348debafeSMikulas Patocka } 192448debafeSMikulas Patocka 192548debafeSMikulas Patocka static void writecache_writeback(struct work_struct *work) 192648debafeSMikulas Patocka { 192748debafeSMikulas Patocka struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work); 192848debafeSMikulas Patocka struct blk_plug plug; 19293f649ab7SKees Cook struct wc_entry *f, *g, *e = NULL; 193048debafeSMikulas Patocka struct rb_node *node, *next_node; 193148debafeSMikulas Patocka struct list_head skipped; 193248debafeSMikulas Patocka struct writeback_list wbl; 193348debafeSMikulas Patocka unsigned long n_walked; 193448debafeSMikulas Patocka 1935293128b1SMikulas Patocka if (!WC_MODE_PMEM(wc)) { 1936293128b1SMikulas Patocka /* Wait for any active kcopyd work on behalf of ssd writeback */ 1937293128b1SMikulas Patocka dm_kcopyd_client_flush(wc->dm_kcopyd); 1938293128b1SMikulas Patocka } 1939293128b1SMikulas Patocka 19405c0de3d7SMikulas Patocka if (likely(wc->pause != 0)) { 19415c0de3d7SMikulas Patocka while (1) { 19425c0de3d7SMikulas Patocka unsigned long idle; 19435c0de3d7SMikulas Patocka if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) || 19445c0de3d7SMikulas Patocka unlikely(dm_suspended(wc->ti))) 19455c0de3d7SMikulas Patocka break; 19465c0de3d7SMikulas Patocka idle = dm_iot_idle_time(&wc->iot); 19475c0de3d7SMikulas Patocka if (idle >= wc->pause) 19485c0de3d7SMikulas Patocka break; 19495c0de3d7SMikulas Patocka idle = wc->pause - idle; 19505c0de3d7SMikulas Patocka if (idle > HZ) 19515c0de3d7SMikulas Patocka idle = HZ; 19525c0de3d7SMikulas Patocka schedule_timeout_idle(idle); 195395b88f4dSMikulas Patocka } 195495b88f4dSMikulas Patocka } 195595b88f4dSMikulas Patocka 195648debafeSMikulas Patocka wc_lock(wc); 195748debafeSMikulas Patocka restart: 195848debafeSMikulas Patocka if (writecache_has_error(wc)) { 195948debafeSMikulas Patocka wc_unlock(wc); 196048debafeSMikulas Patocka return; 196148debafeSMikulas Patocka } 196248debafeSMikulas Patocka 196348debafeSMikulas Patocka if (unlikely(wc->writeback_all)) { 196448debafeSMikulas Patocka if (writecache_wait_for_writeback(wc)) 196548debafeSMikulas Patocka goto restart; 196648debafeSMikulas Patocka } 196748debafeSMikulas Patocka 196848debafeSMikulas Patocka if (wc->overwrote_committed) { 196948debafeSMikulas Patocka writecache_wait_for_ios(wc, WRITE); 197048debafeSMikulas Patocka } 197148debafeSMikulas Patocka 197248debafeSMikulas Patocka n_walked = 0; 197348debafeSMikulas Patocka INIT_LIST_HEAD(&skipped); 197448debafeSMikulas Patocka INIT_LIST_HEAD(&wbl.list); 197548debafeSMikulas Patocka wbl.size = 0; 197648debafeSMikulas Patocka while (!list_empty(&wc->lru) && 197748debafeSMikulas Patocka (wc->writeback_all || 19783923d485SMikulas Patocka wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark || 19793923d485SMikulas Patocka (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >= 19803923d485SMikulas Patocka wc->max_age - wc->max_age / MAX_AGE_DIV))) { 198148debafeSMikulas Patocka 198248debafeSMikulas Patocka n_walked++; 198348debafeSMikulas Patocka if (unlikely(n_walked > WRITEBACK_LATENCY) && 1984af4f6cabSMikulas Patocka likely(!wc->writeback_all)) { 1985af4f6cabSMikulas Patocka if (likely(!dm_suspended(wc->ti))) 198648debafeSMikulas Patocka queue_work(wc->writeback_wq, &wc->writeback_work); 198748debafeSMikulas Patocka break; 198848debafeSMikulas Patocka } 198948debafeSMikulas Patocka 19905229b489SHuaisheng Ye if (unlikely(wc->writeback_all)) { 19915229b489SHuaisheng Ye if (unlikely(!e)) { 19925229b489SHuaisheng Ye writecache_flush(wc); 19935229b489SHuaisheng Ye e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node); 19945229b489SHuaisheng Ye } else 19955229b489SHuaisheng Ye e = g; 19965229b489SHuaisheng Ye } else 199748debafeSMikulas Patocka e = container_of(wc->lru.prev, struct wc_entry, lru); 199848debafeSMikulas Patocka BUG_ON(e->write_in_progress); 199948debafeSMikulas Patocka if (unlikely(!writecache_entry_is_committed(wc, e))) { 200048debafeSMikulas Patocka writecache_flush(wc); 200148debafeSMikulas Patocka } 200248debafeSMikulas Patocka node = rb_prev(&e->rb_node); 200348debafeSMikulas Patocka if (node) { 200448debafeSMikulas Patocka f = container_of(node, struct wc_entry, rb_node); 200548debafeSMikulas Patocka if (unlikely(read_original_sector(wc, f) == 200648debafeSMikulas Patocka read_original_sector(wc, e))) { 200748debafeSMikulas Patocka BUG_ON(!f->write_in_progress); 20088c77f1cbSBaokun Li list_move(&e->lru, &skipped); 200948debafeSMikulas Patocka cond_resched(); 201048debafeSMikulas Patocka continue; 201148debafeSMikulas Patocka } 201248debafeSMikulas Patocka } 201348debafeSMikulas Patocka wc->writeback_size++; 20148c77f1cbSBaokun Li list_move(&e->lru, &wbl.list); 201548debafeSMikulas Patocka wbl.size++; 201648debafeSMikulas Patocka e->write_in_progress = true; 201748debafeSMikulas Patocka e->wc_list_contiguous = 1; 201848debafeSMikulas Patocka 201948debafeSMikulas Patocka f = e; 202048debafeSMikulas Patocka 202148debafeSMikulas Patocka while (1) { 202248debafeSMikulas Patocka next_node = rb_next(&f->rb_node); 202348debafeSMikulas Patocka if (unlikely(!next_node)) 202448debafeSMikulas Patocka break; 202548debafeSMikulas Patocka g = container_of(next_node, struct wc_entry, rb_node); 202662421b38SHuaisheng Ye if (unlikely(read_original_sector(wc, g) == 202762421b38SHuaisheng Ye read_original_sector(wc, f))) { 202848debafeSMikulas Patocka f = g; 202948debafeSMikulas Patocka continue; 203048debafeSMikulas Patocka } 203148debafeSMikulas Patocka if (read_original_sector(wc, g) != 203248debafeSMikulas Patocka read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT)) 203348debafeSMikulas Patocka break; 203448debafeSMikulas Patocka if (unlikely(g->write_in_progress)) 203548debafeSMikulas Patocka break; 203648debafeSMikulas Patocka if (unlikely(!writecache_entry_is_committed(wc, g))) 203748debafeSMikulas Patocka break; 203848debafeSMikulas Patocka 203948debafeSMikulas Patocka if (!WC_MODE_PMEM(wc)) { 204048debafeSMikulas Patocka if (g != f + 1) 204148debafeSMikulas Patocka break; 204248debafeSMikulas Patocka } 204348debafeSMikulas Patocka 204448debafeSMikulas Patocka n_walked++; 204548debafeSMikulas Patocka //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all)) 204648debafeSMikulas Patocka // break; 204748debafeSMikulas Patocka 204848debafeSMikulas Patocka wc->writeback_size++; 20498c77f1cbSBaokun Li list_move(&g->lru, &wbl.list); 205048debafeSMikulas Patocka wbl.size++; 205148debafeSMikulas Patocka g->write_in_progress = true; 2052a8affc03SChristoph Hellwig g->wc_list_contiguous = BIO_MAX_VECS; 205348debafeSMikulas Patocka f = g; 205448debafeSMikulas Patocka e->wc_list_contiguous++; 2055a8affc03SChristoph Hellwig if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) { 20565229b489SHuaisheng Ye if (unlikely(wc->writeback_all)) { 20575229b489SHuaisheng Ye next_node = rb_next(&f->rb_node); 20585229b489SHuaisheng Ye if (likely(next_node)) 20595229b489SHuaisheng Ye g = container_of(next_node, struct wc_entry, rb_node); 20605229b489SHuaisheng Ye } 206148debafeSMikulas Patocka break; 206248debafeSMikulas Patocka } 20635229b489SHuaisheng Ye } 206448debafeSMikulas Patocka cond_resched(); 206548debafeSMikulas Patocka } 206648debafeSMikulas Patocka 206748debafeSMikulas Patocka if (!list_empty(&skipped)) { 206848debafeSMikulas Patocka list_splice_tail(&skipped, &wc->lru); 206948debafeSMikulas Patocka /* 207048debafeSMikulas Patocka * If we didn't do any progress, we must wait until some 207148debafeSMikulas Patocka * writeback finishes to avoid burning CPU in a loop 207248debafeSMikulas Patocka */ 207348debafeSMikulas Patocka if (unlikely(!wbl.size)) 207448debafeSMikulas Patocka writecache_wait_for_writeback(wc); 207548debafeSMikulas Patocka } 207648debafeSMikulas Patocka 207748debafeSMikulas Patocka wc_unlock(wc); 207848debafeSMikulas Patocka 207948debafeSMikulas Patocka blk_start_plug(&plug); 208048debafeSMikulas Patocka 208148debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 208248debafeSMikulas Patocka __writecache_writeback_pmem(wc, &wbl); 208348debafeSMikulas Patocka else 208448debafeSMikulas Patocka __writecache_writeback_ssd(wc, &wbl); 208548debafeSMikulas Patocka 208648debafeSMikulas Patocka blk_finish_plug(&plug); 208748debafeSMikulas Patocka 208848debafeSMikulas Patocka if (unlikely(wc->writeback_all)) { 208948debafeSMikulas Patocka wc_lock(wc); 209048debafeSMikulas Patocka while (writecache_wait_for_writeback(wc)); 209148debafeSMikulas Patocka wc_unlock(wc); 209248debafeSMikulas Patocka } 209348debafeSMikulas Patocka } 209448debafeSMikulas Patocka 2095*86a3238cSHeinz Mauelshagen static int calculate_memory_size(uint64_t device_size, unsigned int block_size, 209648debafeSMikulas Patocka size_t *n_blocks_p, size_t *n_metadata_blocks_p) 209748debafeSMikulas Patocka { 209848debafeSMikulas Patocka uint64_t n_blocks, offset; 209948debafeSMikulas Patocka struct wc_entry e; 210048debafeSMikulas Patocka 210148debafeSMikulas Patocka n_blocks = device_size; 210248debafeSMikulas Patocka do_div(n_blocks, block_size + sizeof(struct wc_memory_entry)); 210348debafeSMikulas Patocka 210448debafeSMikulas Patocka while (1) { 210548debafeSMikulas Patocka if (!n_blocks) 210648debafeSMikulas Patocka return -ENOSPC; 210748debafeSMikulas Patocka /* Verify the following entries[n_blocks] won't overflow */ 210848debafeSMikulas Patocka if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) / 210948debafeSMikulas Patocka sizeof(struct wc_memory_entry))) 211048debafeSMikulas Patocka return -EFBIG; 211148debafeSMikulas Patocka offset = offsetof(struct wc_memory_superblock, entries[n_blocks]); 211248debafeSMikulas Patocka offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1); 211348debafeSMikulas Patocka if (offset + n_blocks * block_size <= device_size) 211448debafeSMikulas Patocka break; 211548debafeSMikulas Patocka n_blocks--; 211648debafeSMikulas Patocka } 211748debafeSMikulas Patocka 211848debafeSMikulas Patocka /* check if the bit field overflows */ 211948debafeSMikulas Patocka e.index = n_blocks; 212048debafeSMikulas Patocka if (e.index != n_blocks) 212148debafeSMikulas Patocka return -EFBIG; 212248debafeSMikulas Patocka 212348debafeSMikulas Patocka if (n_blocks_p) 212448debafeSMikulas Patocka *n_blocks_p = n_blocks; 212548debafeSMikulas Patocka if (n_metadata_blocks_p) 212648debafeSMikulas Patocka *n_metadata_blocks_p = offset >> __ffs(block_size); 212748debafeSMikulas Patocka return 0; 212848debafeSMikulas Patocka } 212948debafeSMikulas Patocka 213048debafeSMikulas Patocka static int init_memory(struct dm_writecache *wc) 213148debafeSMikulas Patocka { 213248debafeSMikulas Patocka size_t b; 213348debafeSMikulas Patocka int r; 213448debafeSMikulas Patocka 213548debafeSMikulas Patocka r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL); 213648debafeSMikulas Patocka if (r) 213748debafeSMikulas Patocka return r; 213848debafeSMikulas Patocka 213948debafeSMikulas Patocka r = writecache_alloc_entries(wc); 214048debafeSMikulas Patocka if (r) 214148debafeSMikulas Patocka return r; 214248debafeSMikulas Patocka 214348debafeSMikulas Patocka for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++) 214448debafeSMikulas Patocka pmem_assign(sb(wc)->padding[b], cpu_to_le64(0)); 214548debafeSMikulas Patocka pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION)); 214648debafeSMikulas Patocka pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size)); 214748debafeSMikulas Patocka pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks)); 214848debafeSMikulas Patocka pmem_assign(sb(wc)->seq_count, cpu_to_le64(0)); 214948debafeSMikulas Patocka 21501edaa447SMikulas Patocka for (b = 0; b < wc->n_blocks; b++) { 215148debafeSMikulas Patocka write_original_sector_seq_count(wc, &wc->entries[b], -1, -1); 21521edaa447SMikulas Patocka cond_resched(); 21531edaa447SMikulas Patocka } 215448debafeSMikulas Patocka 215548debafeSMikulas Patocka writecache_flush_all_metadata(wc); 2156aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 215748debafeSMikulas Patocka pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC)); 215848debafeSMikulas Patocka writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic); 2159aa950920SMikulas Patocka writecache_commit_flushed(wc, false); 216048debafeSMikulas Patocka 216148debafeSMikulas Patocka return 0; 216248debafeSMikulas Patocka } 216348debafeSMikulas Patocka 216448debafeSMikulas Patocka static void writecache_dtr(struct dm_target *ti) 216548debafeSMikulas Patocka { 216648debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 216748debafeSMikulas Patocka 216848debafeSMikulas Patocka if (!wc) 216948debafeSMikulas Patocka return; 217048debafeSMikulas Patocka 217148debafeSMikulas Patocka if (wc->endio_thread) 217248debafeSMikulas Patocka kthread_stop(wc->endio_thread); 217348debafeSMikulas Patocka 217448debafeSMikulas Patocka if (wc->flush_thread) 217548debafeSMikulas Patocka kthread_stop(wc->flush_thread); 217648debafeSMikulas Patocka 217748debafeSMikulas Patocka bioset_exit(&wc->bio_set); 217848debafeSMikulas Patocka 217948debafeSMikulas Patocka mempool_exit(&wc->copy_pool); 218048debafeSMikulas Patocka 218148debafeSMikulas Patocka if (wc->writeback_wq) 218248debafeSMikulas Patocka destroy_workqueue(wc->writeback_wq); 218348debafeSMikulas Patocka 218448debafeSMikulas Patocka if (wc->dev) 218548debafeSMikulas Patocka dm_put_device(ti, wc->dev); 218648debafeSMikulas Patocka 218748debafeSMikulas Patocka if (wc->ssd_dev) 218848debafeSMikulas Patocka dm_put_device(ti, wc->ssd_dev); 218948debafeSMikulas Patocka 219048debafeSMikulas Patocka vfree(wc->entries); 219148debafeSMikulas Patocka 219248debafeSMikulas Patocka if (wc->memory_map) { 219348debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 219448debafeSMikulas Patocka persistent_memory_release(wc); 219548debafeSMikulas Patocka else 219648debafeSMikulas Patocka vfree(wc->memory_map); 219748debafeSMikulas Patocka } 219848debafeSMikulas Patocka 219948debafeSMikulas Patocka if (wc->dm_kcopyd) 220048debafeSMikulas Patocka dm_kcopyd_client_destroy(wc->dm_kcopyd); 220148debafeSMikulas Patocka 220248debafeSMikulas Patocka if (wc->dm_io) 220348debafeSMikulas Patocka dm_io_client_destroy(wc->dm_io); 220448debafeSMikulas Patocka 220548debafeSMikulas Patocka vfree(wc->dirty_bitmap); 220648debafeSMikulas Patocka 220748debafeSMikulas Patocka kfree(wc); 220848debafeSMikulas Patocka } 220948debafeSMikulas Patocka 2210*86a3238cSHeinz Mauelshagen static int writecache_ctr(struct dm_target *ti, unsigned int argc, char **argv) 221148debafeSMikulas Patocka { 221248debafeSMikulas Patocka struct dm_writecache *wc; 221348debafeSMikulas Patocka struct dm_arg_set as; 221448debafeSMikulas Patocka const char *string; 2215*86a3238cSHeinz Mauelshagen unsigned int opt_params; 221648debafeSMikulas Patocka size_t offset, data_size; 221748debafeSMikulas Patocka int i, r; 221848debafeSMikulas Patocka char dummy; 221948debafeSMikulas Patocka int high_wm_percent = HIGH_WATERMARK; 222048debafeSMikulas Patocka int low_wm_percent = LOW_WATERMARK; 222148debafeSMikulas Patocka uint64_t x; 222248debafeSMikulas Patocka struct wc_memory_superblock s; 222348debafeSMikulas Patocka 222448debafeSMikulas Patocka static struct dm_arg _args[] = { 22255c0de3d7SMikulas Patocka {0, 18, "Invalid number of feature args"}, 222648debafeSMikulas Patocka }; 222748debafeSMikulas Patocka 222848debafeSMikulas Patocka as.argc = argc; 222948debafeSMikulas Patocka as.argv = argv; 223048debafeSMikulas Patocka 223148debafeSMikulas Patocka wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL); 223248debafeSMikulas Patocka if (!wc) { 223348debafeSMikulas Patocka ti->error = "Cannot allocate writecache structure"; 223448debafeSMikulas Patocka r = -ENOMEM; 223548debafeSMikulas Patocka goto bad; 223648debafeSMikulas Patocka } 223748debafeSMikulas Patocka ti->private = wc; 223848debafeSMikulas Patocka wc->ti = ti; 223948debafeSMikulas Patocka 224048debafeSMikulas Patocka mutex_init(&wc->lock); 22413923d485SMikulas Patocka wc->max_age = MAX_AGE_UNSPECIFIED; 224248debafeSMikulas Patocka writecache_poison_lists(wc); 224348debafeSMikulas Patocka init_waitqueue_head(&wc->freelist_wait); 224448debafeSMikulas Patocka timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0); 22453923d485SMikulas Patocka timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0); 224648debafeSMikulas Patocka 224748debafeSMikulas Patocka for (i = 0; i < 2; i++) { 224848debafeSMikulas Patocka atomic_set(&wc->bio_in_progress[i], 0); 224948debafeSMikulas Patocka init_waitqueue_head(&wc->bio_in_progress_wait[i]); 225048debafeSMikulas Patocka } 225148debafeSMikulas Patocka 225248debafeSMikulas Patocka wc->dm_io = dm_io_client_create(); 225348debafeSMikulas Patocka if (IS_ERR(wc->dm_io)) { 225448debafeSMikulas Patocka r = PTR_ERR(wc->dm_io); 225548debafeSMikulas Patocka ti->error = "Unable to allocate dm-io client"; 225648debafeSMikulas Patocka wc->dm_io = NULL; 225748debafeSMikulas Patocka goto bad; 225848debafeSMikulas Patocka } 225948debafeSMikulas Patocka 2260f87e033bSHuaisheng Ye wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1); 226148debafeSMikulas Patocka if (!wc->writeback_wq) { 226248debafeSMikulas Patocka r = -ENOMEM; 226348debafeSMikulas Patocka ti->error = "Could not allocate writeback workqueue"; 226448debafeSMikulas Patocka goto bad; 226548debafeSMikulas Patocka } 226648debafeSMikulas Patocka INIT_WORK(&wc->writeback_work, writecache_writeback); 226748debafeSMikulas Patocka INIT_WORK(&wc->flush_work, writecache_flush_work); 226848debafeSMikulas Patocka 226995b88f4dSMikulas Patocka dm_iot_init(&wc->iot); 227095b88f4dSMikulas Patocka 227148debafeSMikulas Patocka raw_spin_lock_init(&wc->endio_list_lock); 227248debafeSMikulas Patocka INIT_LIST_HEAD(&wc->endio_list); 2273f635237aSCai Huoqing wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio"); 227448debafeSMikulas Patocka if (IS_ERR(wc->endio_thread)) { 227548debafeSMikulas Patocka r = PTR_ERR(wc->endio_thread); 227648debafeSMikulas Patocka wc->endio_thread = NULL; 227748debafeSMikulas Patocka ti->error = "Couldn't spawn endio thread"; 227848debafeSMikulas Patocka goto bad; 227948debafeSMikulas Patocka } 228048debafeSMikulas Patocka 228148debafeSMikulas Patocka /* 228248debafeSMikulas Patocka * Parse the mode (pmem or ssd) 228348debafeSMikulas Patocka */ 228448debafeSMikulas Patocka string = dm_shift_arg(&as); 228548debafeSMikulas Patocka if (!string) 228648debafeSMikulas Patocka goto bad_arguments; 228748debafeSMikulas Patocka 228848debafeSMikulas Patocka if (!strcasecmp(string, "s")) { 228948debafeSMikulas Patocka wc->pmem_mode = false; 229048debafeSMikulas Patocka } else if (!strcasecmp(string, "p")) { 229148debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM 229248debafeSMikulas Patocka wc->pmem_mode = true; 229348debafeSMikulas Patocka wc->writeback_fua = true; 229448debafeSMikulas Patocka #else 229548debafeSMikulas Patocka /* 229648debafeSMikulas Patocka * If the architecture doesn't support persistent memory or 229748debafeSMikulas Patocka * the kernel doesn't support any DAX drivers, this driver can 229848debafeSMikulas Patocka * only be used in SSD-only mode. 229948debafeSMikulas Patocka */ 230048debafeSMikulas Patocka r = -EOPNOTSUPP; 230148debafeSMikulas Patocka ti->error = "Persistent memory or DAX not supported on this system"; 230248debafeSMikulas Patocka goto bad; 230348debafeSMikulas Patocka #endif 230448debafeSMikulas Patocka } else { 230548debafeSMikulas Patocka goto bad_arguments; 230648debafeSMikulas Patocka } 230748debafeSMikulas Patocka 230848debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 230948debafeSMikulas Patocka r = bioset_init(&wc->bio_set, BIO_POOL_SIZE, 231048debafeSMikulas Patocka offsetof(struct writeback_struct, bio), 231148debafeSMikulas Patocka BIOSET_NEED_BVECS); 231248debafeSMikulas Patocka if (r) { 231348debafeSMikulas Patocka ti->error = "Could not allocate bio set"; 231448debafeSMikulas Patocka goto bad; 231548debafeSMikulas Patocka } 231648debafeSMikulas Patocka } else { 23175c0de3d7SMikulas Patocka wc->pause = PAUSE_WRITEBACK; 231848debafeSMikulas Patocka r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct)); 231948debafeSMikulas Patocka if (r) { 232048debafeSMikulas Patocka ti->error = "Could not allocate mempool"; 232148debafeSMikulas Patocka goto bad; 232248debafeSMikulas Patocka } 232348debafeSMikulas Patocka } 232448debafeSMikulas Patocka 232548debafeSMikulas Patocka /* 232648debafeSMikulas Patocka * Parse the origin data device 232748debafeSMikulas Patocka */ 232848debafeSMikulas Patocka string = dm_shift_arg(&as); 232948debafeSMikulas Patocka if (!string) 233048debafeSMikulas Patocka goto bad_arguments; 233148debafeSMikulas Patocka r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev); 233248debafeSMikulas Patocka if (r) { 233348debafeSMikulas Patocka ti->error = "Origin data device lookup failed"; 233448debafeSMikulas Patocka goto bad; 233548debafeSMikulas Patocka } 233648debafeSMikulas Patocka 233748debafeSMikulas Patocka /* 233848debafeSMikulas Patocka * Parse cache data device (be it pmem or ssd) 233948debafeSMikulas Patocka */ 234048debafeSMikulas Patocka string = dm_shift_arg(&as); 234148debafeSMikulas Patocka if (!string) 234248debafeSMikulas Patocka goto bad_arguments; 234348debafeSMikulas Patocka 234448debafeSMikulas Patocka r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev); 234548debafeSMikulas Patocka if (r) { 234648debafeSMikulas Patocka ti->error = "Cache data device lookup failed"; 234748debafeSMikulas Patocka goto bad; 234848debafeSMikulas Patocka } 23496dcbb52cSChristoph Hellwig wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev); 235048debafeSMikulas Patocka 235148debafeSMikulas Patocka /* 235248debafeSMikulas Patocka * Parse the cache block size 235348debafeSMikulas Patocka */ 235448debafeSMikulas Patocka string = dm_shift_arg(&as); 235548debafeSMikulas Patocka if (!string) 235648debafeSMikulas Patocka goto bad_arguments; 235748debafeSMikulas Patocka if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 || 235848debafeSMikulas Patocka wc->block_size < 512 || wc->block_size > PAGE_SIZE || 235948debafeSMikulas Patocka (wc->block_size & (wc->block_size - 1))) { 236048debafeSMikulas Patocka r = -EINVAL; 236148debafeSMikulas Patocka ti->error = "Invalid block size"; 236248debafeSMikulas Patocka goto bad; 236348debafeSMikulas Patocka } 236431b22120SMikulas Patocka if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) || 236531b22120SMikulas Patocka wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) { 236631b22120SMikulas Patocka r = -EINVAL; 236731b22120SMikulas Patocka ti->error = "Block size is smaller than device logical block size"; 236831b22120SMikulas Patocka goto bad; 236931b22120SMikulas Patocka } 237048debafeSMikulas Patocka wc->block_size_bits = __ffs(wc->block_size); 237148debafeSMikulas Patocka 237248debafeSMikulas Patocka wc->max_writeback_jobs = MAX_WRITEBACK_JOBS; 237348debafeSMikulas Patocka wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM; 237448debafeSMikulas Patocka wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC); 237548debafeSMikulas Patocka 237648debafeSMikulas Patocka /* 237748debafeSMikulas Patocka * Parse optional arguments 237848debafeSMikulas Patocka */ 237948debafeSMikulas Patocka r = dm_read_arg_group(_args, &as, &opt_params, &ti->error); 238048debafeSMikulas Patocka if (r) 238148debafeSMikulas Patocka goto bad; 238248debafeSMikulas Patocka 238348debafeSMikulas Patocka while (opt_params) { 238448debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 2385d284f824SMikulas Patocka if (!strcasecmp(string, "start_sector") && opt_params >= 1) { 2386d284f824SMikulas Patocka unsigned long long start_sector; 2387d284f824SMikulas Patocka string = dm_shift_arg(&as), opt_params--; 2388d284f824SMikulas Patocka if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1) 2389d284f824SMikulas Patocka goto invalid_optional; 2390d284f824SMikulas Patocka wc->start_sector = start_sector; 2391054bee16SMikulas Patocka wc->start_sector_set = true; 2392d284f824SMikulas Patocka if (wc->start_sector != start_sector || 2393d284f824SMikulas Patocka wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT) 2394d284f824SMikulas Patocka goto invalid_optional; 2395d284f824SMikulas Patocka } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) { 239648debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 239748debafeSMikulas Patocka if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1) 239848debafeSMikulas Patocka goto invalid_optional; 239948debafeSMikulas Patocka if (high_wm_percent < 0 || high_wm_percent > 100) 240048debafeSMikulas Patocka goto invalid_optional; 2401054bee16SMikulas Patocka wc->high_wm_percent_value = high_wm_percent; 240248debafeSMikulas Patocka wc->high_wm_percent_set = true; 240348debafeSMikulas Patocka } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) { 240448debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 240548debafeSMikulas Patocka if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1) 240648debafeSMikulas Patocka goto invalid_optional; 240748debafeSMikulas Patocka if (low_wm_percent < 0 || low_wm_percent > 100) 240848debafeSMikulas Patocka goto invalid_optional; 2409054bee16SMikulas Patocka wc->low_wm_percent_value = low_wm_percent; 241048debafeSMikulas Patocka wc->low_wm_percent_set = true; 241148debafeSMikulas Patocka } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) { 241248debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 241348debafeSMikulas Patocka if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1) 241448debafeSMikulas Patocka goto invalid_optional; 241548debafeSMikulas Patocka wc->max_writeback_jobs_set = true; 241648debafeSMikulas Patocka } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) { 241748debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 241848debafeSMikulas Patocka if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1) 241948debafeSMikulas Patocka goto invalid_optional; 242048debafeSMikulas Patocka wc->autocommit_blocks_set = true; 242148debafeSMikulas Patocka } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) { 2422*86a3238cSHeinz Mauelshagen unsigned int autocommit_msecs; 242348debafeSMikulas Patocka string = dm_shift_arg(&as), opt_params--; 242448debafeSMikulas Patocka if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1) 242548debafeSMikulas Patocka goto invalid_optional; 242648debafeSMikulas Patocka if (autocommit_msecs > 3600000) 242748debafeSMikulas Patocka goto invalid_optional; 242848debafeSMikulas Patocka wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs); 2429054bee16SMikulas Patocka wc->autocommit_time_value = autocommit_msecs; 243048debafeSMikulas Patocka wc->autocommit_time_set = true; 24313923d485SMikulas Patocka } else if (!strcasecmp(string, "max_age") && opt_params >= 1) { 2432*86a3238cSHeinz Mauelshagen unsigned int max_age_msecs; 24333923d485SMikulas Patocka string = dm_shift_arg(&as), opt_params--; 24343923d485SMikulas Patocka if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1) 24353923d485SMikulas Patocka goto invalid_optional; 24363923d485SMikulas Patocka if (max_age_msecs > 86400000) 24373923d485SMikulas Patocka goto invalid_optional; 24383923d485SMikulas Patocka wc->max_age = msecs_to_jiffies(max_age_msecs); 2439054bee16SMikulas Patocka wc->max_age_set = true; 2440054bee16SMikulas Patocka wc->max_age_value = max_age_msecs; 244193de44ebSMikulas Patocka } else if (!strcasecmp(string, "cleaner")) { 2442054bee16SMikulas Patocka wc->cleaner_set = true; 244393de44ebSMikulas Patocka wc->cleaner = true; 244448debafeSMikulas Patocka } else if (!strcasecmp(string, "fua")) { 244548debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 244648debafeSMikulas Patocka wc->writeback_fua = true; 244748debafeSMikulas Patocka wc->writeback_fua_set = true; 244848debafeSMikulas Patocka } else goto invalid_optional; 244948debafeSMikulas Patocka } else if (!strcasecmp(string, "nofua")) { 245048debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) { 245148debafeSMikulas Patocka wc->writeback_fua = false; 245248debafeSMikulas Patocka wc->writeback_fua_set = true; 245348debafeSMikulas Patocka } else goto invalid_optional; 2454611c3e16SMikulas Patocka } else if (!strcasecmp(string, "metadata_only")) { 2455611c3e16SMikulas Patocka wc->metadata_only = true; 24565c0de3d7SMikulas Patocka } else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) { 2457*86a3238cSHeinz Mauelshagen unsigned int pause_msecs; 24585c0de3d7SMikulas Patocka if (WC_MODE_PMEM(wc)) 24595c0de3d7SMikulas Patocka goto invalid_optional; 24605c0de3d7SMikulas Patocka string = dm_shift_arg(&as), opt_params--; 24615c0de3d7SMikulas Patocka if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1) 24625c0de3d7SMikulas Patocka goto invalid_optional; 24635c0de3d7SMikulas Patocka if (pause_msecs > 60000) 24645c0de3d7SMikulas Patocka goto invalid_optional; 24655c0de3d7SMikulas Patocka wc->pause = msecs_to_jiffies(pause_msecs); 24665c0de3d7SMikulas Patocka wc->pause_set = true; 24675c0de3d7SMikulas Patocka wc->pause_value = pause_msecs; 246848debafeSMikulas Patocka } else { 246948debafeSMikulas Patocka invalid_optional: 247048debafeSMikulas Patocka r = -EINVAL; 247148debafeSMikulas Patocka ti->error = "Invalid optional argument"; 247248debafeSMikulas Patocka goto bad; 247348debafeSMikulas Patocka } 247448debafeSMikulas Patocka } 247548debafeSMikulas Patocka 247648debafeSMikulas Patocka if (high_wm_percent < low_wm_percent) { 247748debafeSMikulas Patocka r = -EINVAL; 247848debafeSMikulas Patocka ti->error = "High watermark must be greater than or equal to low watermark"; 247948debafeSMikulas Patocka goto bad; 248048debafeSMikulas Patocka } 248148debafeSMikulas Patocka 2482d284f824SMikulas Patocka if (WC_MODE_PMEM(wc)) { 2483a4662458SMichal Suchanek if (!dax_synchronous(wc->ssd_dev->dax_dev)) { 2484a4662458SMichal Suchanek r = -EOPNOTSUPP; 2485a4662458SMichal Suchanek ti->error = "Asynchronous persistent memory not supported as pmem cache"; 2486a4662458SMichal Suchanek goto bad; 2487a4662458SMichal Suchanek } 2488a4662458SMichal Suchanek 2489d284f824SMikulas Patocka r = persistent_memory_claim(wc); 2490d284f824SMikulas Patocka if (r) { 2491d284f824SMikulas Patocka ti->error = "Unable to map persistent memory for cache"; 2492d284f824SMikulas Patocka goto bad; 2493d284f824SMikulas Patocka } 2494d284f824SMikulas Patocka } else { 249548debafeSMikulas Patocka size_t n_blocks, n_metadata_blocks; 249648debafeSMikulas Patocka uint64_t n_bitmap_bits; 249748debafeSMikulas Patocka 2498d284f824SMikulas Patocka wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT; 2499d284f824SMikulas Patocka 250048debafeSMikulas Patocka bio_list_init(&wc->flush_list); 2501f635237aSCai Huoqing wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush"); 250248debafeSMikulas Patocka if (IS_ERR(wc->flush_thread)) { 250348debafeSMikulas Patocka r = PTR_ERR(wc->flush_thread); 250448debafeSMikulas Patocka wc->flush_thread = NULL; 2505e8ea141aSShenghui Wang ti->error = "Couldn't spawn flush thread"; 250648debafeSMikulas Patocka goto bad; 250748debafeSMikulas Patocka } 250848debafeSMikulas Patocka 250948debafeSMikulas Patocka r = calculate_memory_size(wc->memory_map_size, wc->block_size, 251048debafeSMikulas Patocka &n_blocks, &n_metadata_blocks); 251148debafeSMikulas Patocka if (r) { 251248debafeSMikulas Patocka ti->error = "Invalid device size"; 251348debafeSMikulas Patocka goto bad; 251448debafeSMikulas Patocka } 251548debafeSMikulas Patocka 251648debafeSMikulas Patocka n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) + 251748debafeSMikulas Patocka BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY; 251848debafeSMikulas Patocka /* this is limitation of test_bit functions */ 251948debafeSMikulas Patocka if (n_bitmap_bits > 1U << 31) { 252048debafeSMikulas Patocka r = -EFBIG; 252148debafeSMikulas Patocka ti->error = "Invalid device size"; 252248debafeSMikulas Patocka goto bad; 252348debafeSMikulas Patocka } 252448debafeSMikulas Patocka 252548debafeSMikulas Patocka wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits); 252648debafeSMikulas Patocka if (!wc->memory_map) { 252748debafeSMikulas Patocka r = -ENOMEM; 252848debafeSMikulas Patocka ti->error = "Unable to allocate memory for metadata"; 252948debafeSMikulas Patocka goto bad; 253048debafeSMikulas Patocka } 253148debafeSMikulas Patocka 253248debafeSMikulas Patocka wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle); 253348debafeSMikulas Patocka if (IS_ERR(wc->dm_kcopyd)) { 253448debafeSMikulas Patocka r = PTR_ERR(wc->dm_kcopyd); 253548debafeSMikulas Patocka ti->error = "Unable to allocate dm-kcopyd client"; 253648debafeSMikulas Patocka wc->dm_kcopyd = NULL; 253748debafeSMikulas Patocka goto bad; 253848debafeSMikulas Patocka } 253948debafeSMikulas Patocka 254048debafeSMikulas Patocka wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT); 254148debafeSMikulas Patocka wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) / 254248debafeSMikulas Patocka BITS_PER_LONG * sizeof(unsigned long); 254348debafeSMikulas Patocka wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size); 254448debafeSMikulas Patocka if (!wc->dirty_bitmap) { 254548debafeSMikulas Patocka r = -ENOMEM; 254648debafeSMikulas Patocka ti->error = "Unable to allocate dirty bitmap"; 254748debafeSMikulas Patocka goto bad; 254848debafeSMikulas Patocka } 254948debafeSMikulas Patocka 255031b22120SMikulas Patocka r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT); 255148debafeSMikulas Patocka if (r) { 255231b22120SMikulas Patocka ti->error = "Unable to read first block of metadata"; 255348debafeSMikulas Patocka goto bad; 255448debafeSMikulas Patocka } 255548debafeSMikulas Patocka } 255648debafeSMikulas Patocka 2557ec6347bbSDan Williams r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock)); 255848debafeSMikulas Patocka if (r) { 255948debafeSMikulas Patocka ti->error = "Hardware memory error when reading superblock"; 256048debafeSMikulas Patocka goto bad; 256148debafeSMikulas Patocka } 256248debafeSMikulas Patocka if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) { 256348debafeSMikulas Patocka r = init_memory(wc); 256448debafeSMikulas Patocka if (r) { 256548debafeSMikulas Patocka ti->error = "Unable to initialize device"; 256648debafeSMikulas Patocka goto bad; 256748debafeSMikulas Patocka } 2568ec6347bbSDan Williams r = copy_mc_to_kernel(&s, sb(wc), 2569ec6347bbSDan Williams sizeof(struct wc_memory_superblock)); 257048debafeSMikulas Patocka if (r) { 257148debafeSMikulas Patocka ti->error = "Hardware memory error when reading superblock"; 257248debafeSMikulas Patocka goto bad; 257348debafeSMikulas Patocka } 257448debafeSMikulas Patocka } 257548debafeSMikulas Patocka 257648debafeSMikulas Patocka if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) { 257748debafeSMikulas Patocka ti->error = "Invalid magic in the superblock"; 257848debafeSMikulas Patocka r = -EINVAL; 257948debafeSMikulas Patocka goto bad; 258048debafeSMikulas Patocka } 258148debafeSMikulas Patocka 258248debafeSMikulas Patocka if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) { 258348debafeSMikulas Patocka ti->error = "Invalid version in the superblock"; 258448debafeSMikulas Patocka r = -EINVAL; 258548debafeSMikulas Patocka goto bad; 258648debafeSMikulas Patocka } 258748debafeSMikulas Patocka 258848debafeSMikulas Patocka if (le32_to_cpu(s.block_size) != wc->block_size) { 258948debafeSMikulas Patocka ti->error = "Block size does not match superblock"; 259048debafeSMikulas Patocka r = -EINVAL; 259148debafeSMikulas Patocka goto bad; 259248debafeSMikulas Patocka } 259348debafeSMikulas Patocka 259448debafeSMikulas Patocka wc->n_blocks = le64_to_cpu(s.n_blocks); 259548debafeSMikulas Patocka 259648debafeSMikulas Patocka offset = wc->n_blocks * sizeof(struct wc_memory_entry); 259748debafeSMikulas Patocka if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) { 259848debafeSMikulas Patocka overflow: 259948debafeSMikulas Patocka ti->error = "Overflow in size calculation"; 260048debafeSMikulas Patocka r = -EINVAL; 260148debafeSMikulas Patocka goto bad; 260248debafeSMikulas Patocka } 260348debafeSMikulas Patocka offset += sizeof(struct wc_memory_superblock); 260448debafeSMikulas Patocka if (offset < sizeof(struct wc_memory_superblock)) 260548debafeSMikulas Patocka goto overflow; 260648debafeSMikulas Patocka offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1); 260748debafeSMikulas Patocka data_size = wc->n_blocks * (size_t)wc->block_size; 260848debafeSMikulas Patocka if (!offset || (data_size / wc->block_size != wc->n_blocks) || 260948debafeSMikulas Patocka (offset + data_size < offset)) 261048debafeSMikulas Patocka goto overflow; 261148debafeSMikulas Patocka if (offset + data_size > wc->memory_map_size) { 261248debafeSMikulas Patocka ti->error = "Memory area is too small"; 261348debafeSMikulas Patocka r = -EINVAL; 261448debafeSMikulas Patocka goto bad; 261548debafeSMikulas Patocka } 261648debafeSMikulas Patocka 261748debafeSMikulas Patocka wc->metadata_sectors = offset >> SECTOR_SHIFT; 261848debafeSMikulas Patocka wc->block_start = (char *)sb(wc) + offset; 261948debafeSMikulas Patocka 262048debafeSMikulas Patocka x = (uint64_t)wc->n_blocks * (100 - high_wm_percent); 262148debafeSMikulas Patocka x += 50; 262248debafeSMikulas Patocka do_div(x, 100); 262348debafeSMikulas Patocka wc->freelist_high_watermark = x; 262448debafeSMikulas Patocka x = (uint64_t)wc->n_blocks * (100 - low_wm_percent); 262548debafeSMikulas Patocka x += 50; 262648debafeSMikulas Patocka do_div(x, 100); 262748debafeSMikulas Patocka wc->freelist_low_watermark = x; 262848debafeSMikulas Patocka 262993de44ebSMikulas Patocka if (wc->cleaner) 263093de44ebSMikulas Patocka activate_cleaner(wc); 263193de44ebSMikulas Patocka 263248debafeSMikulas Patocka r = writecache_alloc_entries(wc); 263348debafeSMikulas Patocka if (r) { 263448debafeSMikulas Patocka ti->error = "Cannot allocate memory"; 263548debafeSMikulas Patocka goto bad; 263648debafeSMikulas Patocka } 263748debafeSMikulas Patocka 2638ee55b92aSMikulas Patocka ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2; 263948debafeSMikulas Patocka ti->flush_supported = true; 264048debafeSMikulas Patocka ti->num_discard_bios = 1; 264148debafeSMikulas Patocka 264248debafeSMikulas Patocka if (WC_MODE_PMEM(wc)) 264348debafeSMikulas Patocka persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); 264448debafeSMikulas Patocka 264548debafeSMikulas Patocka return 0; 264648debafeSMikulas Patocka 264748debafeSMikulas Patocka bad_arguments: 264848debafeSMikulas Patocka r = -EINVAL; 264948debafeSMikulas Patocka ti->error = "Bad arguments"; 265048debafeSMikulas Patocka bad: 265148debafeSMikulas Patocka writecache_dtr(ti); 265248debafeSMikulas Patocka return r; 265348debafeSMikulas Patocka } 265448debafeSMikulas Patocka 265548debafeSMikulas Patocka static void writecache_status(struct dm_target *ti, status_type_t type, 2656*86a3238cSHeinz Mauelshagen unsigned int status_flags, char *result, unsigned int maxlen) 265748debafeSMikulas Patocka { 265848debafeSMikulas Patocka struct dm_writecache *wc = ti->private; 2659*86a3238cSHeinz Mauelshagen unsigned int extra_args; 2660*86a3238cSHeinz Mauelshagen unsigned int sz = 0; 266148debafeSMikulas Patocka 266248debafeSMikulas Patocka switch (type) { 266348debafeSMikulas Patocka case STATUSTYPE_INFO: 2664e3a35d03SMikulas Patocka DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", 2665e3a35d03SMikulas Patocka writecache_has_error(wc), 266648debafeSMikulas Patocka (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size, 2667e3a35d03SMikulas Patocka (unsigned long long)wc->writeback_size, 2668e3a35d03SMikulas Patocka wc->stats.reads, 2669e3a35d03SMikulas Patocka wc->stats.read_hits, 2670e3a35d03SMikulas Patocka wc->stats.writes, 2671e3a35d03SMikulas Patocka wc->stats.write_hits_uncommitted, 2672e3a35d03SMikulas Patocka wc->stats.write_hits_committed, 2673e3a35d03SMikulas Patocka wc->stats.writes_around, 2674e3a35d03SMikulas Patocka wc->stats.writes_allocate, 2675e3a35d03SMikulas Patocka wc->stats.writes_blocked_on_freelist, 2676e3a35d03SMikulas Patocka wc->stats.flushes, 2677e3a35d03SMikulas Patocka wc->stats.discards); 267848debafeSMikulas Patocka break; 267948debafeSMikulas Patocka case STATUSTYPE_TABLE: 268048debafeSMikulas Patocka DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's', 268148debafeSMikulas Patocka wc->dev->name, wc->ssd_dev->name, wc->block_size); 268248debafeSMikulas Patocka extra_args = 0; 2683054bee16SMikulas Patocka if (wc->start_sector_set) 26849ff07e7dSMikulas Patocka extra_args += 2; 2685054bee16SMikulas Patocka if (wc->high_wm_percent_set) 268648debafeSMikulas Patocka extra_args += 2; 2687054bee16SMikulas Patocka if (wc->low_wm_percent_set) 268848debafeSMikulas Patocka extra_args += 2; 268948debafeSMikulas Patocka if (wc->max_writeback_jobs_set) 269048debafeSMikulas Patocka extra_args += 2; 269148debafeSMikulas Patocka if (wc->autocommit_blocks_set) 269248debafeSMikulas Patocka extra_args += 2; 269348debafeSMikulas Patocka if (wc->autocommit_time_set) 269448debafeSMikulas Patocka extra_args += 2; 2695054bee16SMikulas Patocka if (wc->max_age_set) 2696e5d41cbcSMikulas Patocka extra_args += 2; 2697054bee16SMikulas Patocka if (wc->cleaner_set) 269893de44ebSMikulas Patocka extra_args++; 269948debafeSMikulas Patocka if (wc->writeback_fua_set) 270048debafeSMikulas Patocka extra_args++; 2701611c3e16SMikulas Patocka if (wc->metadata_only) 2702611c3e16SMikulas Patocka extra_args++; 27035c0de3d7SMikulas Patocka if (wc->pause_set) 27045c0de3d7SMikulas Patocka extra_args += 2; 270548debafeSMikulas Patocka 270648debafeSMikulas Patocka DMEMIT("%u", extra_args); 2707054bee16SMikulas Patocka if (wc->start_sector_set) 27089ff07e7dSMikulas Patocka DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector); 2709054bee16SMikulas Patocka if (wc->high_wm_percent_set) 2710054bee16SMikulas Patocka DMEMIT(" high_watermark %u", wc->high_wm_percent_value); 2711054bee16SMikulas Patocka if (wc->low_wm_percent_set) 2712054bee16SMikulas Patocka DMEMIT(" low_watermark %u", wc->low_wm_percent_value); 271348debafeSMikulas Patocka if (wc->max_writeback_jobs_set) 271448debafeSMikulas Patocka DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs); 271548debafeSMikulas Patocka if (wc->autocommit_blocks_set) 271648debafeSMikulas Patocka DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks); 271748debafeSMikulas Patocka if (wc->autocommit_time_set) 2718054bee16SMikulas Patocka DMEMIT(" autocommit_time %u", wc->autocommit_time_value); 2719054bee16SMikulas Patocka if (wc->max_age_set) 2720054bee16SMikulas Patocka DMEMIT(" max_age %u", wc->max_age_value); 2721054bee16SMikulas Patocka if (wc->cleaner_set) 272293de44ebSMikulas Patocka DMEMIT(" cleaner"); 272348debafeSMikulas Patocka if (wc->writeback_fua_set) 272448debafeSMikulas Patocka DMEMIT(" %sfua", wc->writeback_fua ? "" : "no"); 2725611c3e16SMikulas Patocka if (wc->metadata_only) 2726611c3e16SMikulas Patocka DMEMIT(" metadata_only"); 27275c0de3d7SMikulas Patocka if (wc->pause_set) 27285c0de3d7SMikulas Patocka DMEMIT(" pause_writeback %u", wc->pause_value); 272948debafeSMikulas Patocka break; 27308ec45662STushar Sugandhi case STATUSTYPE_IMA: 27318ec45662STushar Sugandhi *result = '\0'; 27328ec45662STushar Sugandhi break; 273348debafeSMikulas Patocka } 273448debafeSMikulas Patocka } 273548debafeSMikulas Patocka 273648debafeSMikulas Patocka static struct target_type writecache_target = { 273748debafeSMikulas Patocka .name = "writecache", 2738e3a35d03SMikulas Patocka .version = {1, 6, 0}, 273948debafeSMikulas Patocka .module = THIS_MODULE, 274048debafeSMikulas Patocka .ctr = writecache_ctr, 274148debafeSMikulas Patocka .dtr = writecache_dtr, 274248debafeSMikulas Patocka .status = writecache_status, 274348debafeSMikulas Patocka .postsuspend = writecache_suspend, 274448debafeSMikulas Patocka .resume = writecache_resume, 274548debafeSMikulas Patocka .message = writecache_message, 274648debafeSMikulas Patocka .map = writecache_map, 274748debafeSMikulas Patocka .end_io = writecache_end_io, 274848debafeSMikulas Patocka .iterate_devices = writecache_iterate_devices, 274948debafeSMikulas Patocka .io_hints = writecache_io_hints, 275048debafeSMikulas Patocka }; 275148debafeSMikulas Patocka 275248debafeSMikulas Patocka static int __init dm_writecache_init(void) 275348debafeSMikulas Patocka { 275448debafeSMikulas Patocka int r; 275548debafeSMikulas Patocka 275648debafeSMikulas Patocka r = dm_register_target(&writecache_target); 275748debafeSMikulas Patocka if (r < 0) { 275848debafeSMikulas Patocka DMERR("register failed %d", r); 275948debafeSMikulas Patocka return r; 276048debafeSMikulas Patocka } 276148debafeSMikulas Patocka 276248debafeSMikulas Patocka return 0; 276348debafeSMikulas Patocka } 276448debafeSMikulas Patocka 276548debafeSMikulas Patocka static void __exit dm_writecache_exit(void) 276648debafeSMikulas Patocka { 276748debafeSMikulas Patocka dm_unregister_target(&writecache_target); 276848debafeSMikulas Patocka } 276948debafeSMikulas Patocka 277048debafeSMikulas Patocka module_init(dm_writecache_init); 277148debafeSMikulas Patocka module_exit(dm_writecache_exit); 277248debafeSMikulas Patocka 277348debafeSMikulas Patocka MODULE_DESCRIPTION(DM_NAME " writecache target"); 277448debafeSMikulas Patocka MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>"); 277548debafeSMikulas Patocka MODULE_LICENSE("GPL"); 2776