Lines Matching +full:no +full:- +full:memory +full:- +full:wc
1 // SPDX-License-Identifier: GPL-2.0-only
8 #include <linux/device-mapper.h>
13 #include <linux/dm-io.h>
14 #include <linux/dm-kcopyd.h>
19 #include "dm-io-tracker.h"
32 #define MAX_AGE_UNSPECIFIED -1UL
101 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode) argument
102 #define WC_MODE_FUA(wc) ((wc)->writeback_fua) argument
104 #define WC_MODE_PMEM(wc) false argument
105 #define WC_MODE_FUA(wc) false argument
107 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc)) argument
225 struct dm_writecache *wc; member
234 struct dm_writecache *wc; member
243 static void wc_lock(struct dm_writecache *wc) in wc_lock() argument
245 mutex_lock(&wc->lock); in wc_lock()
248 static void wc_unlock(struct dm_writecache *wc) in wc_unlock() argument
250 mutex_unlock(&wc->lock); in wc_unlock()
254 static int persistent_memory_claim(struct dm_writecache *wc) in persistent_memory_claim() argument
264 wc->memory_vmapped = false; in persistent_memory_claim()
266 s = wc->memory_map_size; in persistent_memory_claim()
269 r = -EINVAL; in persistent_memory_claim()
273 r = -EOVERFLOW; in persistent_memory_claim()
277 offset = get_start_sect(wc->ssd_dev->bdev); in persistent_memory_claim()
278 if (offset & (PAGE_SIZE / 512 - 1)) { in persistent_memory_claim()
279 r = -EINVAL; in persistent_memory_claim()
282 offset >>= PAGE_SHIFT - 9; in persistent_memory_claim()
286 da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, DAX_ACCESS, in persistent_memory_claim()
287 &wc->memory_map, &pfn); in persistent_memory_claim()
289 wc->memory_map = NULL; in persistent_memory_claim()
294 wc->memory_map = NULL; in persistent_memory_claim()
295 r = -EOPNOTSUPP; in persistent_memory_claim()
301 wc->memory_map = NULL; in persistent_memory_claim()
304 r = -ENOMEM; in persistent_memory_claim()
311 daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, in persistent_memory_claim()
312 p - i, DAX_ACCESS, NULL, &pfn); in persistent_memory_claim()
314 r = daa ? daa : -EINVAL; in persistent_memory_claim()
318 r = -EOPNOTSUPP; in persistent_memory_claim()
321 while (daa-- && i < p) { in persistent_memory_claim()
328 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL); in persistent_memory_claim()
329 if (!wc->memory_map) { in persistent_memory_claim()
330 r = -ENOMEM; in persistent_memory_claim()
334 wc->memory_vmapped = true; in persistent_memory_claim()
339 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT; in persistent_memory_claim()
340 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT; in persistent_memory_claim()
351 static int persistent_memory_claim(struct dm_writecache *wc) in persistent_memory_claim() argument
353 return -EOPNOTSUPP; in persistent_memory_claim()
357 static void persistent_memory_release(struct dm_writecache *wc) in persistent_memory_release() argument
359 if (wc->memory_vmapped) in persistent_memory_release()
360 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT)); in persistent_memory_release()
373 return (unsigned long)addr & (PAGE_SIZE - 1); in persistent_memory_page_offset()
388 static struct wc_memory_superblock *sb(struct dm_writecache *wc) in sb() argument
390 return wc->memory_map; in sb()
393 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e) in memory_entry() argument
395 return &sb(wc)->entries[e->index]; in memory_entry()
398 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e) in memory_data() argument
400 return (char *)wc->block_start + (e->index << wc->block_size_bits); in memory_data()
403 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e) in cache_sector() argument
405 return wc->start_sector + wc->metadata_sectors + in cache_sector()
406 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT)); in cache_sector()
409 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e) in read_original_sector() argument
412 return e->original_sector; in read_original_sector()
414 return le64_to_cpu(memory_entry(wc, e)->original_sector); in read_original_sector()
418 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e) in read_seq_count() argument
421 return e->seq_count; in read_seq_count()
423 return le64_to_cpu(memory_entry(wc, e)->seq_count); in read_seq_count()
427 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e) in clear_seq_count() argument
430 e->seq_count = -1; in clear_seq_count()
432 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1)); in clear_seq_count()
435 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e, in write_original_sector_seq_count() argument
440 e->original_sector = original_sector; in write_original_sector_seq_count()
441 e->seq_count = seq_count; in write_original_sector_seq_count()
445 pmem_assign(*memory_entry(wc, e), me); in write_original_sector_seq_count()
448 #define writecache_error(wc, err, msg, arg...) \ argument
450 if (!cmpxchg(&(wc)->error, 0, err)) \
452 wake_up(&(wc)->freelist_wait); \
455 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error))) argument
457 static void writecache_flush_all_metadata(struct dm_writecache *wc) in writecache_flush_all_metadata() argument
459 if (!WC_MODE_PMEM(wc)) in writecache_flush_all_metadata()
460 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size); in writecache_flush_all_metadata()
463 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size) in writecache_flush_region() argument
465 if (!WC_MODE_PMEM(wc)) in writecache_flush_region()
466 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY, in writecache_flush_region()
467 wc->dirty_bitmap); in writecache_flush_region()
470 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
473 struct dm_writecache *wc; member
483 writecache_error(endio->wc, -EIO, "error writing metadata"); in writecache_notify_io()
484 BUG_ON(atomic_read(&endio->count) <= 0); in writecache_notify_io()
485 if (atomic_dec_and_test(&endio->count)) in writecache_notify_io()
486 complete(&endio->c); in writecache_notify_io()
489 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction) in writecache_wait_for_ios() argument
491 wait_event(wc->bio_in_progress_wait[direction], in writecache_wait_for_ios()
492 !atomic_read(&wc->bio_in_progress[direction])); in writecache_wait_for_ios()
495 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) in ssd_commit_flushed() argument
500 wc, in ssd_commit_flushed()
504 unsigned int bitmap_bits = wc->dirty_bitmap_size * 8; in ssd_commit_flushed()
510 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i); in ssd_commit_flushed()
513 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i); in ssd_commit_flushed()
515 region.bdev = wc->ssd_dev->bdev; in ssd_commit_flushed()
517 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT); in ssd_commit_flushed()
519 if (unlikely(region.sector >= wc->metadata_sectors)) in ssd_commit_flushed()
521 if (unlikely(region.sector + region.count > wc->metadata_sectors)) in ssd_commit_flushed()
522 region.count = wc->metadata_sectors - region.sector; in ssd_commit_flushed()
524 region.sector += wc->start_sector; in ssd_commit_flushed()
528 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY; in ssd_commit_flushed()
529 req.client = wc->dm_io; in ssd_commit_flushed()
533 /* writing via async dm-io (implied by notify.fn above) won't return an error */ in ssd_commit_flushed()
542 writecache_wait_for_ios(wc, WRITE); in ssd_commit_flushed()
544 writecache_disk_flush(wc, wc->ssd_dev); in ssd_commit_flushed()
546 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size); in ssd_commit_flushed()
549 static void ssd_commit_superblock(struct dm_writecache *wc) in ssd_commit_superblock() argument
555 region.bdev = wc->ssd_dev->bdev; in ssd_commit_superblock()
557 region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT; in ssd_commit_superblock()
559 if (unlikely(region.sector + region.count > wc->metadata_sectors)) in ssd_commit_superblock()
560 region.count = wc->metadata_sectors - region.sector; in ssd_commit_superblock()
562 region.sector += wc->start_sector; in ssd_commit_superblock()
566 req.mem.ptr.vma = (char *)wc->memory_map; in ssd_commit_superblock()
567 req.client = wc->dm_io; in ssd_commit_superblock()
573 writecache_error(wc, r, "error writing superblock"); in ssd_commit_superblock()
576 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) in writecache_commit_flushed() argument
578 if (WC_MODE_PMEM(wc)) in writecache_commit_flushed()
581 ssd_commit_flushed(wc, wait_for_ios); in writecache_commit_flushed()
584 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev) in writecache_disk_flush() argument
590 region.bdev = dev->bdev; in writecache_disk_flush()
596 req.client = wc->dm_io; in writecache_disk_flush()
601 writecache_error(wc, r, "error flushing metadata: %d", r); in writecache_disk_flush()
607 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc, in writecache_find_entry() argument
611 struct rb_node *node = wc->tree.rb_node; in writecache_find_entry()
618 if (read_original_sector(wc, e) == block) in writecache_find_entry()
621 node = (read_original_sector(wc, e) >= block ? in writecache_find_entry()
622 e->rb_node.rb_left : e->rb_node.rb_right); in writecache_find_entry()
626 if (read_original_sector(wc, e) >= block) in writecache_find_entry()
629 node = rb_next(&e->rb_node); in writecache_find_entry()
642 node = rb_prev(&e->rb_node); in writecache_find_entry()
644 node = rb_next(&e->rb_node); in writecache_find_entry()
648 if (read_original_sector(wc, e2) != block) in writecache_find_entry()
654 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins) in writecache_insert_entry() argument
657 struct rb_node **node = &wc->tree.rb_node, *parent = NULL; in writecache_insert_entry()
661 parent = &e->rb_node; in writecache_insert_entry()
662 if (read_original_sector(wc, e) > read_original_sector(wc, ins)) in writecache_insert_entry()
663 node = &parent->rb_left; in writecache_insert_entry()
665 node = &parent->rb_right; in writecache_insert_entry()
667 rb_link_node(&ins->rb_node, parent, node); in writecache_insert_entry()
668 rb_insert_color(&ins->rb_node, &wc->tree); in writecache_insert_entry()
669 list_add(&ins->lru, &wc->lru); in writecache_insert_entry()
670 ins->age = jiffies; in writecache_insert_entry()
673 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e) in writecache_unlink() argument
675 list_del(&e->lru); in writecache_unlink()
676 rb_erase(&e->rb_node, &wc->tree); in writecache_unlink()
679 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e) in writecache_add_to_freelist() argument
681 if (WC_MODE_SORT_FREELIST(wc)) { in writecache_add_to_freelist()
682 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL; in writecache_add_to_freelist()
685 wc->current_free = e; in writecache_add_to_freelist()
688 if (&e->rb_node < *node) in writecache_add_to_freelist()
689 node = &parent->rb_left; in writecache_add_to_freelist()
691 node = &parent->rb_right; in writecache_add_to_freelist()
693 rb_link_node(&e->rb_node, parent, node); in writecache_add_to_freelist()
694 rb_insert_color(&e->rb_node, &wc->freetree); in writecache_add_to_freelist()
696 list_add_tail(&e->lru, &wc->freelist); in writecache_add_to_freelist()
698 wc->freelist_size++; in writecache_add_to_freelist()
701 static inline void writecache_verify_watermark(struct dm_writecache *wc) in writecache_verify_watermark() argument
703 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark)) in writecache_verify_watermark()
704 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_verify_watermark()
709 struct dm_writecache *wc = from_timer(wc, t, max_age_timer); in writecache_max_age_timer() local
711 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) { in writecache_max_age_timer()
712 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_max_age_timer()
713 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); in writecache_max_age_timer()
717 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_se… in writecache_pop_from_freelist() argument
721 if (WC_MODE_SORT_FREELIST(wc)) { in writecache_pop_from_freelist()
724 if (unlikely(!wc->current_free)) in writecache_pop_from_freelist()
726 e = wc->current_free; in writecache_pop_from_freelist()
727 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) in writecache_pop_from_freelist()
729 next = rb_next(&e->rb_node); in writecache_pop_from_freelist()
730 rb_erase(&e->rb_node, &wc->freetree); in writecache_pop_from_freelist()
732 next = rb_first(&wc->freetree); in writecache_pop_from_freelist()
733 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL; in writecache_pop_from_freelist()
735 if (unlikely(list_empty(&wc->freelist))) in writecache_pop_from_freelist()
737 e = container_of(wc->freelist.next, struct wc_entry, lru); in writecache_pop_from_freelist()
738 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) in writecache_pop_from_freelist()
740 list_del(&e->lru); in writecache_pop_from_freelist()
742 wc->freelist_size--; in writecache_pop_from_freelist()
744 writecache_verify_watermark(wc); in writecache_pop_from_freelist()
749 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e) in writecache_free_entry() argument
751 writecache_unlink(wc, e); in writecache_free_entry()
752 writecache_add_to_freelist(wc, e); in writecache_free_entry()
753 clear_seq_count(wc, e); in writecache_free_entry()
754 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); in writecache_free_entry()
755 if (unlikely(waitqueue_active(&wc->freelist_wait))) in writecache_free_entry()
756 wake_up(&wc->freelist_wait); in writecache_free_entry()
759 static void writecache_wait_on_freelist(struct dm_writecache *wc) in writecache_wait_on_freelist() argument
763 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE); in writecache_wait_on_freelist()
764 wc_unlock(wc); in writecache_wait_on_freelist()
766 finish_wait(&wc->freelist_wait, &wait); in writecache_wait_on_freelist()
767 wc_lock(wc); in writecache_wait_on_freelist()
770 static void writecache_poison_lists(struct dm_writecache *wc) in writecache_poison_lists() argument
775 memset(&wc->tree, -1, sizeof(wc->tree)); in writecache_poison_lists()
776 wc->lru.next = LIST_POISON1; in writecache_poison_lists()
777 wc->lru.prev = LIST_POISON2; in writecache_poison_lists()
778 wc->freelist.next = LIST_POISON1; in writecache_poison_lists()
779 wc->freelist.prev = LIST_POISON2; in writecache_poison_lists()
782 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e) in writecache_flush_entry() argument
784 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); in writecache_flush_entry()
785 if (WC_MODE_PMEM(wc)) in writecache_flush_entry()
786 writecache_flush_region(wc, memory_data(wc, e), wc->block_size); in writecache_flush_entry()
789 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e) in writecache_entry_is_committed() argument
791 return read_seq_count(wc, e) < wc->seq_count; in writecache_entry_is_committed()
794 static void writecache_flush(struct dm_writecache *wc) in writecache_flush() argument
799 wc->uncommitted_blocks = 0; in writecache_flush()
800 del_timer(&wc->autocommit_timer); in writecache_flush()
802 if (list_empty(&wc->lru)) in writecache_flush()
805 e = container_of(wc->lru.next, struct wc_entry, lru); in writecache_flush()
806 if (writecache_entry_is_committed(wc, e)) { in writecache_flush()
807 if (wc->overwrote_committed) { in writecache_flush()
808 writecache_wait_for_ios(wc, WRITE); in writecache_flush()
809 writecache_disk_flush(wc, wc->ssd_dev); in writecache_flush()
810 wc->overwrote_committed = false; in writecache_flush()
815 writecache_flush_entry(wc, e); in writecache_flush()
816 if (unlikely(e->lru.next == &wc->lru)) in writecache_flush()
818 e2 = container_of(e->lru.next, struct wc_entry, lru); in writecache_flush()
819 if (writecache_entry_is_committed(wc, e2)) in writecache_flush()
824 writecache_commit_flushed(wc, true); in writecache_flush()
826 wc->seq_count++; in writecache_flush()
827 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count)); in writecache_flush()
828 if (WC_MODE_PMEM(wc)) in writecache_flush()
829 writecache_commit_flushed(wc, false); in writecache_flush()
831 ssd_commit_superblock(wc); in writecache_flush()
833 wc->overwrote_committed = false; in writecache_flush()
837 /* Free another committed entry with lower seq-count */ in writecache_flush()
838 struct rb_node *rb_node = rb_prev(&e->rb_node); in writecache_flush()
842 if (read_original_sector(wc, e2) == read_original_sector(wc, e) && in writecache_flush()
843 likely(!e2->write_in_progress)) { in writecache_flush()
844 writecache_free_entry(wc, e2); in writecache_flush()
848 if (unlikely(e->lru.prev == &wc->lru)) in writecache_flush()
850 e = container_of(e->lru.prev, struct wc_entry, lru); in writecache_flush()
855 writecache_commit_flushed(wc, false); in writecache_flush()
860 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work); in writecache_flush_work() local
862 wc_lock(wc); in writecache_flush_work()
863 writecache_flush(wc); in writecache_flush_work()
864 wc_unlock(wc); in writecache_flush_work()
869 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer); in writecache_autocommit_timer() local
871 if (!writecache_has_error(wc)) in writecache_autocommit_timer()
872 queue_work(wc->writeback_wq, &wc->flush_work); in writecache_autocommit_timer()
875 static void writecache_schedule_autocommit(struct dm_writecache *wc) in writecache_schedule_autocommit() argument
877 if (!timer_pending(&wc->autocommit_timer)) in writecache_schedule_autocommit()
878 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies); in writecache_schedule_autocommit()
881 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end) in writecache_discard() argument
886 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ); in writecache_discard()
890 while (read_original_sector(wc, e) < end) { in writecache_discard()
891 struct rb_node *node = rb_next(&e->rb_node); in writecache_discard()
893 if (likely(!e->write_in_progress)) { in writecache_discard()
895 if (!WC_MODE_PMEM(wc)) { in writecache_discard()
896 writecache_wait_for_ios(wc, READ); in writecache_discard()
897 writecache_wait_for_ios(wc, WRITE); in writecache_discard()
901 if (!writecache_entry_is_committed(wc, e)) in writecache_discard()
902 wc->uncommitted_blocks--; in writecache_discard()
903 writecache_free_entry(wc, e); in writecache_discard()
913 writecache_commit_flushed(wc, false); in writecache_discard()
916 static bool writecache_wait_for_writeback(struct dm_writecache *wc) in writecache_wait_for_writeback() argument
918 if (wc->writeback_size) { in writecache_wait_for_writeback()
919 writecache_wait_on_freelist(wc); in writecache_wait_for_writeback()
927 struct dm_writecache *wc = ti->private; in writecache_suspend() local
930 del_timer_sync(&wc->autocommit_timer); in writecache_suspend()
931 del_timer_sync(&wc->max_age_timer); in writecache_suspend()
933 wc_lock(wc); in writecache_suspend()
934 writecache_flush(wc); in writecache_suspend()
935 flush_on_suspend = wc->flush_on_suspend; in writecache_suspend()
937 wc->flush_on_suspend = false; in writecache_suspend()
938 wc->writeback_all++; in writecache_suspend()
939 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_suspend()
941 wc_unlock(wc); in writecache_suspend()
943 drain_workqueue(wc->writeback_wq); in writecache_suspend()
945 wc_lock(wc); in writecache_suspend()
947 wc->writeback_all--; in writecache_suspend()
948 while (writecache_wait_for_writeback(wc)) in writecache_suspend()
951 if (WC_MODE_PMEM(wc)) in writecache_suspend()
952 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); in writecache_suspend()
954 writecache_poison_lists(wc); in writecache_suspend()
956 wc_unlock(wc); in writecache_suspend()
959 static int writecache_alloc_entries(struct dm_writecache *wc) in writecache_alloc_entries() argument
963 if (wc->entries) in writecache_alloc_entries()
965 wc->entries = vmalloc_array(wc->n_blocks, sizeof(struct wc_entry)); in writecache_alloc_entries()
966 if (!wc->entries) in writecache_alloc_entries()
967 return -ENOMEM; in writecache_alloc_entries()
968 for (b = 0; b < wc->n_blocks; b++) { in writecache_alloc_entries()
969 struct wc_entry *e = &wc->entries[b]; in writecache_alloc_entries()
971 e->index = b; in writecache_alloc_entries()
972 e->write_in_progress = false; in writecache_alloc_entries()
979 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors) in writecache_read_metadata() argument
984 region.bdev = wc->ssd_dev->bdev; in writecache_read_metadata()
985 region.sector = wc->start_sector; in writecache_read_metadata()
989 req.mem.ptr.vma = (char *)wc->memory_map; in writecache_read_metadata()
990 req.client = wc->dm_io; in writecache_read_metadata()
998 struct dm_writecache *wc = ti->private; in writecache_resume() local
1004 wc_lock(wc); in writecache_resume()
1006 wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev); in writecache_resume()
1008 if (WC_MODE_PMEM(wc)) { in writecache_resume()
1009 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size); in writecache_resume()
1011 r = writecache_read_metadata(wc, wc->metadata_sectors); in writecache_resume()
1015 writecache_error(wc, r, "unable to read metadata: %d", r); in writecache_resume()
1017 memset((char *)wc->memory_map + sb_entries_offset, -1, in writecache_resume()
1018 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset); in writecache_resume()
1022 wc->tree = RB_ROOT; in writecache_resume()
1023 INIT_LIST_HEAD(&wc->lru); in writecache_resume()
1024 if (WC_MODE_SORT_FREELIST(wc)) { in writecache_resume()
1025 wc->freetree = RB_ROOT; in writecache_resume()
1026 wc->current_free = NULL; in writecache_resume()
1028 INIT_LIST_HEAD(&wc->freelist); in writecache_resume()
1030 wc->freelist_size = 0; in writecache_resume()
1032 r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count, in writecache_resume()
1035 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r); in writecache_resume()
1038 wc->seq_count = le64_to_cpu(sb_seq_count); in writecache_resume()
1041 for (b = 0; b < wc->n_blocks; b++) { in writecache_resume()
1042 struct wc_entry *e = &wc->entries[b]; in writecache_resume()
1045 if (writecache_has_error(wc)) { in writecache_resume()
1046 e->original_sector = -1; in writecache_resume()
1047 e->seq_count = -1; in writecache_resume()
1050 r = copy_mc_to_kernel(&wme, memory_entry(wc, e), in writecache_resume()
1053 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d", in writecache_resume()
1055 e->original_sector = -1; in writecache_resume()
1056 e->seq_count = -1; in writecache_resume()
1058 e->original_sector = le64_to_cpu(wme.original_sector); in writecache_resume()
1059 e->seq_count = le64_to_cpu(wme.seq_count); in writecache_resume()
1064 for (b = 0; b < wc->n_blocks; b++) { in writecache_resume()
1065 struct wc_entry *e = &wc->entries[b]; in writecache_resume()
1067 if (!writecache_entry_is_committed(wc, e)) { in writecache_resume()
1068 if (read_seq_count(wc, e) != -1) { in writecache_resume()
1070 clear_seq_count(wc, e); in writecache_resume()
1073 writecache_add_to_freelist(wc, e); in writecache_resume()
1077 old = writecache_find_entry(wc, read_original_sector(wc, e), 0); in writecache_resume()
1079 writecache_insert_entry(wc, e); in writecache_resume()
1081 if (read_seq_count(wc, old) == read_seq_count(wc, e)) { in writecache_resume()
1082 writecache_error(wc, -EINVAL, in writecache_resume()
1084 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e), in writecache_resume()
1085 (unsigned long long)read_seq_count(wc, e)); in writecache_resume()
1087 if (read_seq_count(wc, old) > read_seq_count(wc, e)) { in writecache_resume()
1090 writecache_free_entry(wc, old); in writecache_resume()
1091 writecache_insert_entry(wc, e); in writecache_resume()
1100 writecache_flush_all_metadata(wc); in writecache_resume()
1101 writecache_commit_flushed(wc, false); in writecache_resume()
1104 writecache_verify_watermark(wc); in writecache_resume()
1106 if (wc->max_age != MAX_AGE_UNSPECIFIED) in writecache_resume()
1107 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); in writecache_resume()
1109 wc_unlock(wc); in writecache_resume()
1112 static int process_flush_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_flush_mesg() argument
1115 return -EINVAL; in process_flush_mesg()
1117 wc_lock(wc); in process_flush_mesg()
1118 if (dm_suspended(wc->ti)) { in process_flush_mesg()
1119 wc_unlock(wc); in process_flush_mesg()
1120 return -EBUSY; in process_flush_mesg()
1122 if (writecache_has_error(wc)) { in process_flush_mesg()
1123 wc_unlock(wc); in process_flush_mesg()
1124 return -EIO; in process_flush_mesg()
1127 writecache_flush(wc); in process_flush_mesg()
1128 wc->writeback_all++; in process_flush_mesg()
1129 queue_work(wc->writeback_wq, &wc->writeback_work); in process_flush_mesg()
1130 wc_unlock(wc); in process_flush_mesg()
1132 flush_workqueue(wc->writeback_wq); in process_flush_mesg()
1134 wc_lock(wc); in process_flush_mesg()
1135 wc->writeback_all--; in process_flush_mesg()
1136 if (writecache_has_error(wc)) { in process_flush_mesg()
1137 wc_unlock(wc); in process_flush_mesg()
1138 return -EIO; in process_flush_mesg()
1140 wc_unlock(wc); in process_flush_mesg()
1145 static int process_flush_on_suspend_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_flush_on_suspend_mesg() argument
1148 return -EINVAL; in process_flush_on_suspend_mesg()
1150 wc_lock(wc); in process_flush_on_suspend_mesg()
1151 wc->flush_on_suspend = true; in process_flush_on_suspend_mesg()
1152 wc_unlock(wc); in process_flush_on_suspend_mesg()
1157 static void activate_cleaner(struct dm_writecache *wc) in activate_cleaner() argument
1159 wc->flush_on_suspend = true; in activate_cleaner()
1160 wc->cleaner = true; in activate_cleaner()
1161 wc->freelist_high_watermark = wc->n_blocks; in activate_cleaner()
1162 wc->freelist_low_watermark = wc->n_blocks; in activate_cleaner()
1165 static int process_cleaner_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_cleaner_mesg() argument
1168 return -EINVAL; in process_cleaner_mesg()
1170 wc_lock(wc); in process_cleaner_mesg()
1171 activate_cleaner(wc); in process_cleaner_mesg()
1172 if (!dm_suspended(wc->ti)) in process_cleaner_mesg()
1173 writecache_verify_watermark(wc); in process_cleaner_mesg()
1174 wc_unlock(wc); in process_cleaner_mesg()
1179 static int process_clear_stats_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_clear_stats_mesg() argument
1182 return -EINVAL; in process_clear_stats_mesg()
1184 wc_lock(wc); in process_clear_stats_mesg()
1185 memset(&wc->stats, 0, sizeof(wc->stats)); in process_clear_stats_mesg()
1186 wc_unlock(wc); in process_clear_stats_mesg()
1194 int r = -EINVAL; in writecache_message()
1195 struct dm_writecache *wc = ti->private; in writecache_message() local
1198 r = process_flush_mesg(argc, argv, wc); in writecache_message()
1200 r = process_flush_on_suspend_mesg(argc, argv, wc); in writecache_message()
1202 r = process_cleaner_mesg(argc, argv, wc); in writecache_message()
1204 r = process_clear_stats_mesg(argc, argv, wc); in writecache_message()
1215 * non-temporal stores perform better with block size 512 in memcpy_flushcache_optimized()
1221 * We see that movnti performs better for 512-byte blocks, and in memcpy_flushcache_optimized()
1222 * clflushopt performs better for 1024-byte and larger blocks. So, we in memcpy_flushcache_optimized()
1225 * NOTE: this happens to be the case now (with dm-writecache's single in memcpy_flushcache_optimized()
1226 * threaded model) but re-evaluate this once memcpy_flushcache() is in memcpy_flushcache_optimized()
1228 * advantage seen with cache-allocating-writes plus flushing. in memcpy_flushcache_optimized()
1239 size -= 64; in memcpy_flushcache_optimized()
1247 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data) in bio_copy_block() argument
1252 unsigned int remaining_size = wc->block_size; in bio_copy_block()
1255 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter); in bio_copy_block()
1268 writecache_error(wc, r, "hardware memory error when reading data: %d", r); in bio_copy_block()
1269 bio->bi_status = BLK_STS_IOERR; in bio_copy_block()
1279 remaining_size -= size; in bio_copy_block()
1286 struct dm_writecache *wc = data; in writecache_flush_thread() local
1291 wc_lock(wc); in writecache_flush_thread()
1292 bio = bio_list_pop(&wc->flush_list); in writecache_flush_thread()
1295 wc_unlock(wc); in writecache_flush_thread()
1307 writecache_discard(wc, bio->bi_iter.bi_sector, in writecache_flush_thread()
1309 wc_unlock(wc); in writecache_flush_thread()
1310 bio_set_dev(bio, wc->dev->bdev); in writecache_flush_thread()
1313 writecache_flush(wc); in writecache_flush_thread()
1314 wc_unlock(wc); in writecache_flush_thread()
1315 if (writecache_has_error(wc)) in writecache_flush_thread()
1316 bio->bi_status = BLK_STS_IOERR; in writecache_flush_thread()
1324 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio) in writecache_offload_bio() argument
1326 if (bio_list_empty(&wc->flush_list)) in writecache_offload_bio()
1327 wake_up_process(wc->flush_thread); in writecache_offload_bio()
1328 bio_list_add(&wc->flush_list, bio); in writecache_offload_bio()
1339 static void writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio, in writecache_map_remap_origin() argument
1344 read_original_sector(wc, e) - bio->bi_iter.bi_sector; in writecache_map_remap_origin()
1345 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) in writecache_map_remap_origin()
1350 static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio) in writecache_map_read() argument
1356 wc->stats.reads++; in writecache_map_read()
1357 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); in writecache_map_read()
1358 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) { in writecache_map_read()
1359 wc->stats.read_hits++; in writecache_map_read()
1360 if (WC_MODE_PMEM(wc)) { in writecache_map_read()
1361 bio_copy_block(wc, bio, memory_data(wc, e)); in writecache_map_read()
1362 if (bio->bi_iter.bi_size) in writecache_map_read()
1366 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT); in writecache_map_read()
1367 bio_set_dev(bio, wc->ssd_dev->bdev); in writecache_map_read()
1368 bio->bi_iter.bi_sector = cache_sector(wc, e); in writecache_map_read()
1369 if (!writecache_entry_is_committed(wc, e)) in writecache_map_read()
1370 writecache_wait_for_ios(wc, WRITE); in writecache_map_read()
1374 writecache_map_remap_origin(wc, bio, e); in writecache_map_read()
1375 wc->stats.reads += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits; in writecache_map_read()
1382 static void writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio, in writecache_bio_copy_ssd() argument
1385 unsigned int bio_size = wc->block_size; in writecache_bio_copy_ssd()
1386 sector_t start_cache_sec = cache_sector(wc, e); in writecache_bio_copy_ssd()
1389 while (bio_size < bio->bi_iter.bi_size) { in writecache_bio_copy_ssd()
1391 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec); in writecache_bio_copy_ssd()
1395 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector + in writecache_bio_copy_ssd()
1396 (bio_size >> SECTOR_SHIFT), wc->seq_count); in writecache_bio_copy_ssd()
1397 writecache_insert_entry(wc, f); in writecache_bio_copy_ssd()
1398 wc->uncommitted_blocks++; in writecache_bio_copy_ssd()
1401 struct rb_node *next = rb_next(&e->rb_node); in writecache_bio_copy_ssd()
1408 if (read_original_sector(wc, f) != in writecache_bio_copy_ssd()
1409 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) in writecache_bio_copy_ssd()
1411 if (unlikely(f->write_in_progress)) in writecache_bio_copy_ssd()
1413 if (writecache_entry_is_committed(wc, f)) in writecache_bio_copy_ssd()
1414 wc->overwrote_committed = true; in writecache_bio_copy_ssd()
1417 bio_size += wc->block_size; in writecache_bio_copy_ssd()
1418 current_cache_sec += wc->block_size >> SECTOR_SHIFT; in writecache_bio_copy_ssd()
1421 bio_set_dev(bio, wc->ssd_dev->bdev); in writecache_bio_copy_ssd()
1422 bio->bi_iter.bi_sector = start_cache_sec; in writecache_bio_copy_ssd()
1425 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_bio_copy_ssd()
1426 wc->stats.writes_allocate += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits; in writecache_bio_copy_ssd()
1428 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) { in writecache_bio_copy_ssd()
1429 wc->uncommitted_blocks = 0; in writecache_bio_copy_ssd()
1430 queue_work(wc->writeback_wq, &wc->flush_work); in writecache_bio_copy_ssd()
1432 writecache_schedule_autocommit(wc); in writecache_bio_copy_ssd()
1436 static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio) in writecache_map_write() argument
1444 if (writecache_has_error(wc)) { in writecache_map_write()
1445 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_write()
1448 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0); in writecache_map_write()
1450 if (!writecache_entry_is_committed(wc, e)) { in writecache_map_write()
1451 wc->stats.write_hits_uncommitted++; in writecache_map_write()
1455 wc->stats.write_hits_committed++; in writecache_map_write()
1456 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) { in writecache_map_write()
1457 wc->overwrote_committed = true; in writecache_map_write()
1463 if (unlikely(wc->cleaner) || in writecache_map_write()
1464 (wc->metadata_only && !(bio->bi_opf & REQ_META))) in writecache_map_write()
1467 e = writecache_pop_from_freelist(wc, (sector_t)-1); in writecache_map_write()
1469 if (!WC_MODE_PMEM(wc) && !found_entry) { in writecache_map_write()
1471 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); in writecache_map_write()
1472 writecache_map_remap_origin(wc, bio, e); in writecache_map_write()
1473 wc->stats.writes_around += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_write()
1474 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_write()
1477 wc->stats.writes_blocked_on_freelist++; in writecache_map_write()
1478 writecache_wait_on_freelist(wc); in writecache_map_write()
1481 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count); in writecache_map_write()
1482 writecache_insert_entry(wc, e); in writecache_map_write()
1483 wc->uncommitted_blocks++; in writecache_map_write()
1484 wc->stats.writes_allocate++; in writecache_map_write()
1486 if (WC_MODE_PMEM(wc)) { in writecache_map_write()
1487 bio_copy_block(wc, bio, memory_data(wc, e)); in writecache_map_write()
1488 wc->stats.writes++; in writecache_map_write()
1490 writecache_bio_copy_ssd(wc, bio, e, search_used); in writecache_map_write()
1493 } while (bio->bi_iter.bi_size); in writecache_map_write()
1495 if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks)) in writecache_map_write()
1496 writecache_flush(wc); in writecache_map_write()
1498 writecache_schedule_autocommit(wc); in writecache_map_write()
1503 static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio) in writecache_map_flush() argument
1505 if (writecache_has_error(wc)) in writecache_map_flush()
1508 if (WC_MODE_PMEM(wc)) { in writecache_map_flush()
1509 wc->stats.flushes++; in writecache_map_flush()
1510 writecache_flush(wc); in writecache_map_flush()
1511 if (writecache_has_error(wc)) in writecache_map_flush()
1513 else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only)) in writecache_map_flush()
1520 wc->stats.flushes++; in writecache_map_flush()
1521 writecache_offload_bio(wc, bio); in writecache_map_flush()
1525 static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio) in writecache_map_discard() argument
1527 wc->stats.discards += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_discard()
1529 if (writecache_has_error(wc)) in writecache_map_discard()
1532 if (WC_MODE_PMEM(wc)) { in writecache_map_discard()
1533 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio)); in writecache_map_discard()
1537 writecache_offload_bio(wc, bio); in writecache_map_discard()
1543 struct dm_writecache *wc = ti->private; in writecache_map() local
1546 bio->bi_private = NULL; in writecache_map()
1548 wc_lock(wc); in writecache_map()
1550 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { in writecache_map()
1551 map_op = writecache_map_flush(wc, bio); in writecache_map()
1555 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector); in writecache_map()
1557 if (unlikely((((unsigned int)bio->bi_iter.bi_sector | bio_sectors(bio)) & in writecache_map()
1558 (wc->block_size / 512 - 1)) != 0)) { in writecache_map()
1560 (unsigned long long)bio->bi_iter.bi_sector, in writecache_map()
1561 bio->bi_iter.bi_size, wc->block_size); in writecache_map()
1567 map_op = writecache_map_discard(wc, bio); in writecache_map()
1572 map_op = writecache_map_read(wc, bio); in writecache_map()
1574 map_op = writecache_map_write(wc, bio); in writecache_map()
1578 if (likely(wc->pause != 0)) { in writecache_map()
1580 dm_iot_io_begin(&wc->iot, 1); in writecache_map()
1581 bio->bi_private = (void *)2; in writecache_map()
1584 bio_set_dev(bio, wc->dev->bdev); in writecache_map()
1585 wc_unlock(wc); in writecache_map()
1590 bio->bi_private = (void *)1; in writecache_map()
1591 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]); in writecache_map()
1592 wc_unlock(wc); in writecache_map()
1596 wc_unlock(wc); in writecache_map()
1601 wc_unlock(wc); in writecache_map()
1605 wc_unlock(wc); in writecache_map()
1611 wc_unlock(wc); in writecache_map()
1618 struct dm_writecache *wc = ti->private; in writecache_end_io() local
1620 if (bio->bi_private == (void *)1) { in writecache_end_io()
1623 if (atomic_dec_and_test(&wc->bio_in_progress[dir])) in writecache_end_io()
1624 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir]))) in writecache_end_io()
1625 wake_up(&wc->bio_in_progress_wait[dir]); in writecache_end_io()
1626 } else if (bio->bi_private == (void *)2) { in writecache_end_io()
1627 dm_iot_io_end(&wc->iot, 1); in writecache_end_io()
1635 struct dm_writecache *wc = ti->private; in writecache_iterate_devices() local
1637 return fn(ti, wc->dev, 0, ti->len, data); in writecache_iterate_devices()
1642 struct dm_writecache *wc = ti->private; in writecache_io_hints() local
1644 if (limits->logical_block_size < wc->block_size) in writecache_io_hints()
1645 limits->logical_block_size = wc->block_size; in writecache_io_hints()
1647 if (limits->physical_block_size < wc->block_size) in writecache_io_hints()
1648 limits->physical_block_size = wc->block_size; in writecache_io_hints()
1650 if (limits->io_min < wc->block_size) in writecache_io_hints()
1651 limits->io_min = wc->block_size; in writecache_io_hints()
1658 struct dm_writecache *wc = wb->wc; in writecache_writeback_endio() local
1661 raw_spin_lock_irqsave(&wc->endio_list_lock, flags); in writecache_writeback_endio()
1662 if (unlikely(list_empty(&wc->endio_list))) in writecache_writeback_endio()
1663 wake_up_process(wc->endio_thread); in writecache_writeback_endio()
1664 list_add_tail(&wb->endio_entry, &wc->endio_list); in writecache_writeback_endio()
1665 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags); in writecache_writeback_endio()
1671 struct dm_writecache *wc = c->wc; in writecache_copy_endio() local
1673 c->error = likely(!(read_err | write_err)) ? 0 : -EIO; in writecache_copy_endio()
1675 raw_spin_lock_irq(&wc->endio_list_lock); in writecache_copy_endio()
1676 if (unlikely(list_empty(&wc->endio_list))) in writecache_copy_endio()
1677 wake_up_process(wc->endio_thread); in writecache_copy_endio()
1678 list_add_tail(&c->endio_entry, &wc->endio_list); in writecache_copy_endio()
1679 raw_spin_unlock_irq(&wc->endio_list_lock); in writecache_copy_endio()
1682 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list) in __writecache_endio_pmem() argument
1690 wb = list_entry(list->next, struct writeback_struct, endio_entry); in __writecache_endio_pmem()
1691 list_del(&wb->endio_entry); in __writecache_endio_pmem()
1693 if (unlikely(wb->bio.bi_status != BLK_STS_OK)) in __writecache_endio_pmem()
1694 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status), in __writecache_endio_pmem()
1695 "write error %d", wb->bio.bi_status); in __writecache_endio_pmem()
1698 e = wb->wc_list[i]; in __writecache_endio_pmem()
1699 BUG_ON(!e->write_in_progress); in __writecache_endio_pmem()
1700 e->write_in_progress = false; in __writecache_endio_pmem()
1701 INIT_LIST_HEAD(&e->lru); in __writecache_endio_pmem()
1702 if (!writecache_has_error(wc)) in __writecache_endio_pmem()
1703 writecache_free_entry(wc, e); in __writecache_endio_pmem()
1704 BUG_ON(!wc->writeback_size); in __writecache_endio_pmem()
1705 wc->writeback_size--; in __writecache_endio_pmem()
1708 writecache_commit_flushed(wc, false); in __writecache_endio_pmem()
1709 wc_unlock(wc); in __writecache_endio_pmem()
1710 wc_lock(wc); in __writecache_endio_pmem()
1713 } while (++i < wb->wc_list_n); in __writecache_endio_pmem()
1715 if (wb->wc_list != wb->wc_list_inline) in __writecache_endio_pmem()
1716 kfree(wb->wc_list); in __writecache_endio_pmem()
1717 bio_put(&wb->bio); in __writecache_endio_pmem()
1721 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list) in __writecache_endio_ssd() argument
1727 c = list_entry(list->next, struct copy_struct, endio_entry); in __writecache_endio_ssd()
1728 list_del(&c->endio_entry); in __writecache_endio_ssd()
1730 if (unlikely(c->error)) in __writecache_endio_ssd()
1731 writecache_error(wc, c->error, "copy error"); in __writecache_endio_ssd()
1733 e = c->e; in __writecache_endio_ssd()
1735 BUG_ON(!e->write_in_progress); in __writecache_endio_ssd()
1736 e->write_in_progress = false; in __writecache_endio_ssd()
1737 INIT_LIST_HEAD(&e->lru); in __writecache_endio_ssd()
1738 if (!writecache_has_error(wc)) in __writecache_endio_ssd()
1739 writecache_free_entry(wc, e); in __writecache_endio_ssd()
1741 BUG_ON(!wc->writeback_size); in __writecache_endio_ssd()
1742 wc->writeback_size--; in __writecache_endio_ssd()
1744 } while (--c->n_entries); in __writecache_endio_ssd()
1745 mempool_free(c, &wc->copy_pool); in __writecache_endio_ssd()
1751 struct dm_writecache *wc = data; in writecache_endio_thread() local
1756 raw_spin_lock_irq(&wc->endio_list_lock); in writecache_endio_thread()
1757 if (!list_empty(&wc->endio_list)) in writecache_endio_thread()
1760 raw_spin_unlock_irq(&wc->endio_list_lock); in writecache_endio_thread()
1772 list = wc->endio_list; in writecache_endio_thread()
1773 list.next->prev = list.prev->next = &list; in writecache_endio_thread()
1774 INIT_LIST_HEAD(&wc->endio_list); in writecache_endio_thread()
1775 raw_spin_unlock_irq(&wc->endio_list_lock); in writecache_endio_thread()
1777 if (!WC_MODE_FUA(wc)) in writecache_endio_thread()
1778 writecache_disk_flush(wc, wc->dev); in writecache_endio_thread()
1780 wc_lock(wc); in writecache_endio_thread()
1782 if (WC_MODE_PMEM(wc)) { in writecache_endio_thread()
1783 __writecache_endio_pmem(wc, &list); in writecache_endio_thread()
1785 __writecache_endio_ssd(wc, &list); in writecache_endio_thread()
1786 writecache_wait_for_ios(wc, READ); in writecache_endio_thread()
1789 writecache_commit_flushed(wc, false); in writecache_endio_thread()
1791 wc_unlock(wc); in writecache_endio_thread()
1799 struct dm_writecache *wc = wb->wc; in wc_add_block() local
1800 unsigned int block_size = wc->block_size; in wc_add_block()
1801 void *address = memory_data(wc, e); in wc_add_block()
1805 if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors)) in wc_add_block()
1808 return bio_add_page(&wb->bio, persistent_memory_page(address), in wc_add_block()
1817 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl) in __writeback_throttle() argument
1819 if (unlikely(wc->max_writeback_jobs)) { in __writeback_throttle()
1820 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) { in __writeback_throttle()
1821 wc_lock(wc); in __writeback_throttle()
1822 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs) in __writeback_throttle()
1823 writecache_wait_on_freelist(wc); in __writeback_throttle()
1824 wc_unlock(wc); in __writeback_throttle()
1830 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl) in __writecache_writeback_pmem() argument
1837 while (wbl->size) { in __writecache_writeback_pmem()
1838 wbl->size--; in __writecache_writeback_pmem()
1839 e = container_of(wbl->list.prev, struct wc_entry, lru); in __writecache_writeback_pmem()
1840 list_del(&e->lru); in __writecache_writeback_pmem()
1842 max_pages = e->wc_list_contiguous; in __writecache_writeback_pmem()
1844 bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE, in __writecache_writeback_pmem()
1845 GFP_NOIO, &wc->bio_set); in __writecache_writeback_pmem()
1847 wb->wc = wc; in __writecache_writeback_pmem()
1848 bio->bi_end_io = writecache_writeback_endio; in __writecache_writeback_pmem()
1849 bio->bi_iter.bi_sector = read_original_sector(wc, e); in __writecache_writeback_pmem()
1852 wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *), in __writecache_writeback_pmem()
1856 if (likely(max_pages <= WB_LIST_INLINE) || unlikely(!wb->wc_list)) { in __writecache_writeback_pmem()
1857 wb->wc_list = wb->wc_list_inline; in __writecache_writeback_pmem()
1863 wb->wc_list[0] = e; in __writecache_writeback_pmem()
1864 wb->wc_list_n = 1; in __writecache_writeback_pmem()
1866 while (wbl->size && wb->wc_list_n < max_pages) { in __writecache_writeback_pmem()
1867 f = container_of(wbl->list.prev, struct wc_entry, lru); in __writecache_writeback_pmem()
1868 if (read_original_sector(wc, f) != in __writecache_writeback_pmem()
1869 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) in __writecache_writeback_pmem()
1873 wbl->size--; in __writecache_writeback_pmem()
1874 list_del(&f->lru); in __writecache_writeback_pmem()
1875 wb->wc_list[wb->wc_list_n++] = f; in __writecache_writeback_pmem()
1878 if (WC_MODE_FUA(wc)) in __writecache_writeback_pmem()
1879 bio->bi_opf |= REQ_FUA; in __writecache_writeback_pmem()
1880 if (writecache_has_error(wc)) { in __writecache_writeback_pmem()
1881 bio->bi_status = BLK_STS_IOERR; in __writecache_writeback_pmem()
1884 bio->bi_status = BLK_STS_OK; in __writecache_writeback_pmem()
1890 __writeback_throttle(wc, wbl); in __writecache_writeback_pmem()
1894 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl) in __writecache_writeback_ssd() argument
1900 while (wbl->size) { in __writecache_writeback_ssd()
1903 wbl->size--; in __writecache_writeback_ssd()
1904 e = container_of(wbl->list.prev, struct wc_entry, lru); in __writecache_writeback_ssd()
1905 list_del(&e->lru); in __writecache_writeback_ssd()
1907 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT); in __writecache_writeback_ssd()
1909 from.bdev = wc->ssd_dev->bdev; in __writecache_writeback_ssd()
1910 from.sector = cache_sector(wc, e); in __writecache_writeback_ssd()
1912 to.bdev = wc->dev->bdev; in __writecache_writeback_ssd()
1913 to.sector = read_original_sector(wc, e); in __writecache_writeback_ssd()
1916 c = mempool_alloc(&wc->copy_pool, GFP_NOIO); in __writecache_writeback_ssd()
1917 c->wc = wc; in __writecache_writeback_ssd()
1918 c->e = e; in __writecache_writeback_ssd()
1919 c->n_entries = e->wc_list_contiguous; in __writecache_writeback_ssd()
1921 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) { in __writecache_writeback_ssd()
1922 wbl->size--; in __writecache_writeback_ssd()
1923 f = container_of(wbl->list.prev, struct wc_entry, lru); in __writecache_writeback_ssd()
1925 list_del(&f->lru); in __writecache_writeback_ssd()
1929 if (unlikely(to.sector + to.count > wc->data_device_sectors)) { in __writecache_writeback_ssd()
1930 if (to.sector >= wc->data_device_sectors) { in __writecache_writeback_ssd()
1934 from.count = to.count = wc->data_device_sectors - to.sector; in __writecache_writeback_ssd()
1937 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c); in __writecache_writeback_ssd()
1939 __writeback_throttle(wc, wbl); in __writecache_writeback_ssd()
1945 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work); in writecache_writeback() local
1953 if (!WC_MODE_PMEM(wc)) { in writecache_writeback()
1955 dm_kcopyd_client_flush(wc->dm_kcopyd); in writecache_writeback()
1958 if (likely(wc->pause != 0)) { in writecache_writeback()
1962 if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) || in writecache_writeback()
1963 unlikely(dm_suspended(wc->ti))) in writecache_writeback()
1965 idle = dm_iot_idle_time(&wc->iot); in writecache_writeback()
1966 if (idle >= wc->pause) in writecache_writeback()
1968 idle = wc->pause - idle; in writecache_writeback()
1975 wc_lock(wc); in writecache_writeback()
1977 if (writecache_has_error(wc)) { in writecache_writeback()
1978 wc_unlock(wc); in writecache_writeback()
1982 if (unlikely(wc->writeback_all)) { in writecache_writeback()
1983 if (writecache_wait_for_writeback(wc)) in writecache_writeback()
1987 if (wc->overwrote_committed) in writecache_writeback()
1988 writecache_wait_for_ios(wc, WRITE); in writecache_writeback()
1994 while (!list_empty(&wc->lru) && in writecache_writeback()
1995 (wc->writeback_all || in writecache_writeback()
1996 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark || in writecache_writeback()
1997 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >= in writecache_writeback()
1998 wc->max_age - wc->max_age / MAX_AGE_DIV))) { in writecache_writeback()
2002 likely(!wc->writeback_all)) { in writecache_writeback()
2003 if (likely(!dm_suspended(wc->ti))) in writecache_writeback()
2004 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_writeback()
2008 if (unlikely(wc->writeback_all)) { in writecache_writeback()
2010 writecache_flush(wc); in writecache_writeback()
2011 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node); in writecache_writeback()
2015 e = container_of(wc->lru.prev, struct wc_entry, lru); in writecache_writeback()
2016 BUG_ON(e->write_in_progress); in writecache_writeback()
2017 if (unlikely(!writecache_entry_is_committed(wc, e))) in writecache_writeback()
2018 writecache_flush(wc); in writecache_writeback()
2020 node = rb_prev(&e->rb_node); in writecache_writeback()
2023 if (unlikely(read_original_sector(wc, f) == in writecache_writeback()
2024 read_original_sector(wc, e))) { in writecache_writeback()
2025 BUG_ON(!f->write_in_progress); in writecache_writeback()
2026 list_move(&e->lru, &skipped); in writecache_writeback()
2031 wc->writeback_size++; in writecache_writeback()
2032 list_move(&e->lru, &wbl.list); in writecache_writeback()
2034 e->write_in_progress = true; in writecache_writeback()
2035 e->wc_list_contiguous = 1; in writecache_writeback()
2040 next_node = rb_next(&f->rb_node); in writecache_writeback()
2044 if (unlikely(read_original_sector(wc, g) == in writecache_writeback()
2045 read_original_sector(wc, f))) { in writecache_writeback()
2049 if (read_original_sector(wc, g) != in writecache_writeback()
2050 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT)) in writecache_writeback()
2052 if (unlikely(g->write_in_progress)) in writecache_writeback()
2054 if (unlikely(!writecache_entry_is_committed(wc, g))) in writecache_writeback()
2057 if (!WC_MODE_PMEM(wc)) { in writecache_writeback()
2063 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all)) in writecache_writeback()
2066 wc->writeback_size++; in writecache_writeback()
2067 list_move(&g->lru, &wbl.list); in writecache_writeback()
2069 g->write_in_progress = true; in writecache_writeback()
2070 g->wc_list_contiguous = BIO_MAX_VECS; in writecache_writeback()
2072 e->wc_list_contiguous++; in writecache_writeback()
2073 if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) { in writecache_writeback()
2074 if (unlikely(wc->writeback_all)) { in writecache_writeback()
2075 next_node = rb_next(&f->rb_node); in writecache_writeback()
2086 list_splice_tail(&skipped, &wc->lru); in writecache_writeback()
2092 writecache_wait_for_writeback(wc); in writecache_writeback()
2095 wc_unlock(wc); in writecache_writeback()
2099 if (WC_MODE_PMEM(wc)) in writecache_writeback()
2100 __writecache_writeback_pmem(wc, &wbl); in writecache_writeback()
2102 __writecache_writeback_ssd(wc, &wbl); in writecache_writeback()
2106 if (unlikely(wc->writeback_all)) { in writecache_writeback()
2107 wc_lock(wc); in writecache_writeback()
2108 while (writecache_wait_for_writeback(wc)) in writecache_writeback()
2110 wc_unlock(wc); in writecache_writeback()
2125 return -ENOSPC; in calculate_memory_size()
2127 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) / in calculate_memory_size()
2129 return -EFBIG; in calculate_memory_size()
2131 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1); in calculate_memory_size()
2134 n_blocks--; in calculate_memory_size()
2140 return -EFBIG; in calculate_memory_size()
2149 static int init_memory(struct dm_writecache *wc) in init_memory() argument
2154 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL); in init_memory()
2158 r = writecache_alloc_entries(wc); in init_memory()
2162 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++) in init_memory()
2163 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0)); in init_memory()
2164 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION)); in init_memory()
2165 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size)); in init_memory()
2166 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks)); in init_memory()
2167 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0)); in init_memory()
2169 for (b = 0; b < wc->n_blocks; b++) { in init_memory()
2170 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1); in init_memory()
2174 writecache_flush_all_metadata(wc); in init_memory()
2175 writecache_commit_flushed(wc, false); in init_memory()
2176 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC)); in init_memory()
2177 writecache_flush_region(wc, &sb(wc)->magic, sizeof(sb(wc)->magic)); in init_memory()
2178 writecache_commit_flushed(wc, false); in init_memory()
2185 struct dm_writecache *wc = ti->private; in writecache_dtr() local
2187 if (!wc) in writecache_dtr()
2190 if (wc->endio_thread) in writecache_dtr()
2191 kthread_stop(wc->endio_thread); in writecache_dtr()
2193 if (wc->flush_thread) in writecache_dtr()
2194 kthread_stop(wc->flush_thread); in writecache_dtr()
2196 bioset_exit(&wc->bio_set); in writecache_dtr()
2198 mempool_exit(&wc->copy_pool); in writecache_dtr()
2200 if (wc->writeback_wq) in writecache_dtr()
2201 destroy_workqueue(wc->writeback_wq); in writecache_dtr()
2203 if (wc->dev) in writecache_dtr()
2204 dm_put_device(ti, wc->dev); in writecache_dtr()
2206 if (wc->ssd_dev) in writecache_dtr()
2207 dm_put_device(ti, wc->ssd_dev); in writecache_dtr()
2209 vfree(wc->entries); in writecache_dtr()
2211 if (wc->memory_map) { in writecache_dtr()
2212 if (WC_MODE_PMEM(wc)) in writecache_dtr()
2213 persistent_memory_release(wc); in writecache_dtr()
2215 vfree(wc->memory_map); in writecache_dtr()
2218 if (wc->dm_kcopyd) in writecache_dtr()
2219 dm_kcopyd_client_destroy(wc->dm_kcopyd); in writecache_dtr()
2221 if (wc->dm_io) in writecache_dtr()
2222 dm_io_client_destroy(wc->dm_io); in writecache_dtr()
2224 vfree(wc->dirty_bitmap); in writecache_dtr()
2226 kfree(wc); in writecache_dtr()
2231 struct dm_writecache *wc; in writecache_ctr() local
2250 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL); in writecache_ctr()
2251 if (!wc) { in writecache_ctr()
2252 ti->error = "Cannot allocate writecache structure"; in writecache_ctr()
2253 r = -ENOMEM; in writecache_ctr()
2256 ti->private = wc; in writecache_ctr()
2257 wc->ti = ti; in writecache_ctr()
2259 mutex_init(&wc->lock); in writecache_ctr()
2260 wc->max_age = MAX_AGE_UNSPECIFIED; in writecache_ctr()
2261 writecache_poison_lists(wc); in writecache_ctr()
2262 init_waitqueue_head(&wc->freelist_wait); in writecache_ctr()
2263 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0); in writecache_ctr()
2264 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0); in writecache_ctr()
2267 atomic_set(&wc->bio_in_progress[i], 0); in writecache_ctr()
2268 init_waitqueue_head(&wc->bio_in_progress_wait[i]); in writecache_ctr()
2271 wc->dm_io = dm_io_client_create(); in writecache_ctr()
2272 if (IS_ERR(wc->dm_io)) { in writecache_ctr()
2273 r = PTR_ERR(wc->dm_io); in writecache_ctr()
2274 ti->error = "Unable to allocate dm-io client"; in writecache_ctr()
2275 wc->dm_io = NULL; in writecache_ctr()
2279 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1); in writecache_ctr()
2280 if (!wc->writeback_wq) { in writecache_ctr()
2281 r = -ENOMEM; in writecache_ctr()
2282 ti->error = "Could not allocate writeback workqueue"; in writecache_ctr()
2285 INIT_WORK(&wc->writeback_work, writecache_writeback); in writecache_ctr()
2286 INIT_WORK(&wc->flush_work, writecache_flush_work); in writecache_ctr()
2288 dm_iot_init(&wc->iot); in writecache_ctr()
2290 raw_spin_lock_init(&wc->endio_list_lock); in writecache_ctr()
2291 INIT_LIST_HEAD(&wc->endio_list); in writecache_ctr()
2292 wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio"); in writecache_ctr()
2293 if (IS_ERR(wc->endio_thread)) { in writecache_ctr()
2294 r = PTR_ERR(wc->endio_thread); in writecache_ctr()
2295 wc->endio_thread = NULL; in writecache_ctr()
2296 ti->error = "Couldn't spawn endio thread"; in writecache_ctr()
2308 wc->pmem_mode = false; in writecache_ctr()
2311 wc->pmem_mode = true; in writecache_ctr()
2312 wc->writeback_fua = true; in writecache_ctr()
2315 * If the architecture doesn't support persistent memory or in writecache_ctr()
2317 * only be used in SSD-only mode. in writecache_ctr()
2319 r = -EOPNOTSUPP; in writecache_ctr()
2320 ti->error = "Persistent memory or DAX not supported on this system"; in writecache_ctr()
2327 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2328 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE, in writecache_ctr()
2332 ti->error = "Could not allocate bio set"; in writecache_ctr()
2336 wc->pause = PAUSE_WRITEBACK; in writecache_ctr()
2337 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct)); in writecache_ctr()
2339 ti->error = "Could not allocate mempool"; in writecache_ctr()
2350 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev); in writecache_ctr()
2352 ti->error = "Origin data device lookup failed"; in writecache_ctr()
2363 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev); in writecache_ctr()
2365 ti->error = "Cache data device lookup failed"; in writecache_ctr()
2368 wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev); in writecache_ctr()
2376 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 || in writecache_ctr()
2377 wc->block_size < 512 || wc->block_size > PAGE_SIZE || in writecache_ctr()
2378 (wc->block_size & (wc->block_size - 1))) { in writecache_ctr()
2379 r = -EINVAL; in writecache_ctr()
2380 ti->error = "Invalid block size"; in writecache_ctr()
2383 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) || in writecache_ctr()
2384 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) { in writecache_ctr()
2385 r = -EINVAL; in writecache_ctr()
2386 ti->error = "Block size is smaller than device logical block size"; in writecache_ctr()
2389 wc->block_size_bits = __ffs(wc->block_size); in writecache_ctr()
2391 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS; in writecache_ctr()
2392 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM; in writecache_ctr()
2393 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC); in writecache_ctr()
2398 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error); in writecache_ctr()
2403 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2407 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2410 wc->start_sector = start_sector; in writecache_ctr()
2411 wc->start_sector_set = true; in writecache_ctr()
2412 if (wc->start_sector != start_sector || in writecache_ctr()
2413 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT) in writecache_ctr()
2416 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2421 wc->high_wm_percent_value = high_wm_percent; in writecache_ctr()
2422 wc->high_wm_percent_set = true; in writecache_ctr()
2424 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2429 wc->low_wm_percent_value = low_wm_percent; in writecache_ctr()
2430 wc->low_wm_percent_set = true; in writecache_ctr()
2432 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2433 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1) in writecache_ctr()
2435 wc->max_writeback_jobs_set = true; in writecache_ctr()
2437 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2438 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1) in writecache_ctr()
2440 wc->autocommit_blocks_set = true; in writecache_ctr()
2444 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2449 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs); in writecache_ctr()
2450 wc->autocommit_time_value = autocommit_msecs; in writecache_ctr()
2451 wc->autocommit_time_set = true; in writecache_ctr()
2455 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2460 wc->max_age = msecs_to_jiffies(max_age_msecs); in writecache_ctr()
2461 wc->max_age_set = true; in writecache_ctr()
2462 wc->max_age_value = max_age_msecs; in writecache_ctr()
2464 wc->cleaner_set = true; in writecache_ctr()
2465 wc->cleaner = true; in writecache_ctr()
2467 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2468 wc->writeback_fua = true; in writecache_ctr()
2469 wc->writeback_fua_set = true; in writecache_ctr()
2473 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2474 wc->writeback_fua = false; in writecache_ctr()
2475 wc->writeback_fua_set = true; in writecache_ctr()
2479 wc->metadata_only = true; in writecache_ctr()
2483 if (WC_MODE_PMEM(wc)) in writecache_ctr()
2485 string = dm_shift_arg(&as), opt_params--; in writecache_ctr()
2490 wc->pause = msecs_to_jiffies(pause_msecs); in writecache_ctr()
2491 wc->pause_set = true; in writecache_ctr()
2492 wc->pause_value = pause_msecs; in writecache_ctr()
2495 r = -EINVAL; in writecache_ctr()
2496 ti->error = "Invalid optional argument"; in writecache_ctr()
2502 r = -EINVAL; in writecache_ctr()
2503 ti->error = "High watermark must be greater than or equal to low watermark"; in writecache_ctr()
2507 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2508 if (!dax_synchronous(wc->ssd_dev->dax_dev)) { in writecache_ctr()
2509 r = -EOPNOTSUPP; in writecache_ctr()
2510 ti->error = "Asynchronous persistent memory not supported as pmem cache"; in writecache_ctr()
2514 r = persistent_memory_claim(wc); in writecache_ctr()
2516 ti->error = "Unable to map persistent memory for cache"; in writecache_ctr()
2523 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT; in writecache_ctr()
2525 bio_list_init(&wc->flush_list); in writecache_ctr()
2526 wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush"); in writecache_ctr()
2527 if (IS_ERR(wc->flush_thread)) { in writecache_ctr()
2528 r = PTR_ERR(wc->flush_thread); in writecache_ctr()
2529 wc->flush_thread = NULL; in writecache_ctr()
2530 ti->error = "Couldn't spawn flush thread"; in writecache_ctr()
2534 r = calculate_memory_size(wc->memory_map_size, wc->block_size, in writecache_ctr()
2537 ti->error = "Invalid device size"; in writecache_ctr()
2541 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) + in writecache_ctr()
2542 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY; in writecache_ctr()
2545 r = -EFBIG; in writecache_ctr()
2546 ti->error = "Invalid device size"; in writecache_ctr()
2550 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits); in writecache_ctr()
2551 if (!wc->memory_map) { in writecache_ctr()
2552 r = -ENOMEM; in writecache_ctr()
2553 ti->error = "Unable to allocate memory for metadata"; in writecache_ctr()
2557 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle); in writecache_ctr()
2558 if (IS_ERR(wc->dm_kcopyd)) { in writecache_ctr()
2559 r = PTR_ERR(wc->dm_kcopyd); in writecache_ctr()
2560 ti->error = "Unable to allocate dm-kcopyd client"; in writecache_ctr()
2561 wc->dm_kcopyd = NULL; in writecache_ctr()
2565 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT); in writecache_ctr()
2566 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) / in writecache_ctr()
2568 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size); in writecache_ctr()
2569 if (!wc->dirty_bitmap) { in writecache_ctr()
2570 r = -ENOMEM; in writecache_ctr()
2571 ti->error = "Unable to allocate dirty bitmap"; in writecache_ctr()
2575 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT); in writecache_ctr()
2577 ti->error = "Unable to read first block of metadata"; in writecache_ctr()
2582 r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock)); in writecache_ctr()
2584 ti->error = "Hardware memory error when reading superblock"; in writecache_ctr()
2588 r = init_memory(wc); in writecache_ctr()
2590 ti->error = "Unable to initialize device"; in writecache_ctr()
2593 r = copy_mc_to_kernel(&s, sb(wc), in writecache_ctr()
2596 ti->error = "Hardware memory error when reading superblock"; in writecache_ctr()
2602 ti->error = "Invalid magic in the superblock"; in writecache_ctr()
2603 r = -EINVAL; in writecache_ctr()
2608 ti->error = "Invalid version in the superblock"; in writecache_ctr()
2609 r = -EINVAL; in writecache_ctr()
2613 if (le32_to_cpu(s.block_size) != wc->block_size) { in writecache_ctr()
2614 ti->error = "Block size does not match superblock"; in writecache_ctr()
2615 r = -EINVAL; in writecache_ctr()
2619 wc->n_blocks = le64_to_cpu(s.n_blocks); in writecache_ctr()
2621 offset = wc->n_blocks * sizeof(struct wc_memory_entry); in writecache_ctr()
2622 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) { in writecache_ctr()
2624 ti->error = "Overflow in size calculation"; in writecache_ctr()
2625 r = -EINVAL; in writecache_ctr()
2631 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1); in writecache_ctr()
2632 data_size = wc->n_blocks * (size_t)wc->block_size; in writecache_ctr()
2633 if (!offset || (data_size / wc->block_size != wc->n_blocks) || in writecache_ctr()
2636 if (offset + data_size > wc->memory_map_size) { in writecache_ctr()
2637 ti->error = "Memory area is too small"; in writecache_ctr()
2638 r = -EINVAL; in writecache_ctr()
2642 wc->metadata_sectors = offset >> SECTOR_SHIFT; in writecache_ctr()
2643 wc->block_start = (char *)sb(wc) + offset; in writecache_ctr()
2645 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent); in writecache_ctr()
2648 wc->freelist_high_watermark = x; in writecache_ctr()
2649 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent); in writecache_ctr()
2652 wc->freelist_low_watermark = x; in writecache_ctr()
2654 if (wc->cleaner) in writecache_ctr()
2655 activate_cleaner(wc); in writecache_ctr()
2657 r = writecache_alloc_entries(wc); in writecache_ctr()
2659 ti->error = "Cannot allocate memory"; in writecache_ctr()
2663 ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2; in writecache_ctr()
2664 ti->flush_supported = true; in writecache_ctr()
2665 ti->num_discard_bios = 1; in writecache_ctr()
2667 if (WC_MODE_PMEM(wc)) in writecache_ctr()
2668 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); in writecache_ctr()
2673 r = -EINVAL; in writecache_ctr()
2674 ti->error = "Bad arguments"; in writecache_ctr()
2683 struct dm_writecache *wc = ti->private; in writecache_status() local
2690 writecache_has_error(wc), in writecache_status()
2691 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size, in writecache_status()
2692 (unsigned long long)wc->writeback_size, in writecache_status()
2693 wc->stats.reads, in writecache_status()
2694 wc->stats.read_hits, in writecache_status()
2695 wc->stats.writes, in writecache_status()
2696 wc->stats.write_hits_uncommitted, in writecache_status()
2697 wc->stats.write_hits_committed, in writecache_status()
2698 wc->stats.writes_around, in writecache_status()
2699 wc->stats.writes_allocate, in writecache_status()
2700 wc->stats.writes_blocked_on_freelist, in writecache_status()
2701 wc->stats.flushes, in writecache_status()
2702 wc->stats.discards); in writecache_status()
2705 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's', in writecache_status()
2706 wc->dev->name, wc->ssd_dev->name, wc->block_size); in writecache_status()
2708 if (wc->start_sector_set) in writecache_status()
2710 if (wc->high_wm_percent_set) in writecache_status()
2712 if (wc->low_wm_percent_set) in writecache_status()
2714 if (wc->max_writeback_jobs_set) in writecache_status()
2716 if (wc->autocommit_blocks_set) in writecache_status()
2718 if (wc->autocommit_time_set) in writecache_status()
2720 if (wc->max_age_set) in writecache_status()
2722 if (wc->cleaner_set) in writecache_status()
2724 if (wc->writeback_fua_set) in writecache_status()
2726 if (wc->metadata_only) in writecache_status()
2728 if (wc->pause_set) in writecache_status()
2732 if (wc->start_sector_set) in writecache_status()
2733 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector); in writecache_status()
2734 if (wc->high_wm_percent_set) in writecache_status()
2735 DMEMIT(" high_watermark %u", wc->high_wm_percent_value); in writecache_status()
2736 if (wc->low_wm_percent_set) in writecache_status()
2737 DMEMIT(" low_watermark %u", wc->low_wm_percent_value); in writecache_status()
2738 if (wc->max_writeback_jobs_set) in writecache_status()
2739 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs); in writecache_status()
2740 if (wc->autocommit_blocks_set) in writecache_status()
2741 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks); in writecache_status()
2742 if (wc->autocommit_time_set) in writecache_status()
2743 DMEMIT(" autocommit_time %u", wc->autocommit_time_value); in writecache_status()
2744 if (wc->max_age_set) in writecache_status()
2745 DMEMIT(" max_age %u", wc->max_age_value); in writecache_status()
2746 if (wc->cleaner_set) in writecache_status()
2748 if (wc->writeback_fua_set) in writecache_status()
2749 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no"); in writecache_status()
2750 if (wc->metadata_only) in writecache_status()
2752 if (wc->pause_set) in writecache_status()
2753 DMEMIT(" pause_writeback %u", wc->pause_value); in writecache_status()
2779 MODULE_AUTHOR("Mikulas Patocka <dm-devel@lists.linux.dev>");