Lines Matching full:wc

100 #define WC_MODE_PMEM(wc)			((wc)->pmem_mode)  argument
101 #define WC_MODE_FUA(wc) ((wc)->writeback_fua) argument
103 #define WC_MODE_PMEM(wc) false argument
104 #define WC_MODE_FUA(wc) false argument
106 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc)) argument
224 struct dm_writecache *wc; member
233 struct dm_writecache *wc; member
242 static void wc_lock(struct dm_writecache *wc) in wc_lock() argument
244 mutex_lock(&wc->lock); in wc_lock()
247 static void wc_unlock(struct dm_writecache *wc) in wc_unlock() argument
249 mutex_unlock(&wc->lock); in wc_unlock()
253 static int persistent_memory_claim(struct dm_writecache *wc) in persistent_memory_claim() argument
263 wc->memory_vmapped = false; in persistent_memory_claim()
265 s = wc->memory_map_size; in persistent_memory_claim()
276 offset = get_start_sect(wc->ssd_dev->bdev); in persistent_memory_claim()
285 da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, DAX_ACCESS, in persistent_memory_claim()
286 &wc->memory_map, &pfn); in persistent_memory_claim()
288 wc->memory_map = NULL; in persistent_memory_claim()
293 wc->memory_map = NULL; in persistent_memory_claim()
300 wc->memory_map = NULL; in persistent_memory_claim()
310 daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, in persistent_memory_claim()
327 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL); in persistent_memory_claim()
328 if (!wc->memory_map) { in persistent_memory_claim()
333 wc->memory_vmapped = true; in persistent_memory_claim()
338 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT; in persistent_memory_claim()
339 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT; in persistent_memory_claim()
350 static int persistent_memory_claim(struct dm_writecache *wc) in persistent_memory_claim() argument
356 static void persistent_memory_release(struct dm_writecache *wc) in persistent_memory_release() argument
358 if (wc->memory_vmapped) in persistent_memory_release()
359 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT)); in persistent_memory_release()
387 static struct wc_memory_superblock *sb(struct dm_writecache *wc) in sb() argument
389 return wc->memory_map; in sb()
392 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e) in memory_entry() argument
394 return &sb(wc)->entries[e->index]; in memory_entry()
397 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e) in memory_data() argument
399 return (char *)wc->block_start + (e->index << wc->block_size_bits); in memory_data()
402 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e) in cache_sector() argument
404 return wc->start_sector + wc->metadata_sectors + in cache_sector()
405 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT)); in cache_sector()
408 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e) in read_original_sector() argument
413 return le64_to_cpu(memory_entry(wc, e)->original_sector); in read_original_sector()
417 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e) in read_seq_count() argument
422 return le64_to_cpu(memory_entry(wc, e)->seq_count); in read_seq_count()
426 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e) in clear_seq_count() argument
431 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1)); in clear_seq_count()
434 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e, in write_original_sector_seq_count() argument
444 pmem_assign(*memory_entry(wc, e), me); in write_original_sector_seq_count()
447 #define writecache_error(wc, err, msg, arg...) \ argument
449 if (!cmpxchg(&(wc)->error, 0, err)) \
451 wake_up(&(wc)->freelist_wait); \
454 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error))) argument
456 static void writecache_flush_all_metadata(struct dm_writecache *wc) in writecache_flush_all_metadata() argument
458 if (!WC_MODE_PMEM(wc)) in writecache_flush_all_metadata()
459 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size); in writecache_flush_all_metadata()
462 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size) in writecache_flush_region() argument
464 if (!WC_MODE_PMEM(wc)) in writecache_flush_region()
465 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY, in writecache_flush_region()
466 wc->dirty_bitmap); in writecache_flush_region()
469 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
472 struct dm_writecache *wc; member
482 writecache_error(endio->wc, -EIO, "error writing metadata"); in writecache_notify_io()
488 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction) in writecache_wait_for_ios() argument
490 wait_event(wc->bio_in_progress_wait[direction], in writecache_wait_for_ios()
491 !atomic_read(&wc->bio_in_progress[direction])); in writecache_wait_for_ios()
494 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) in ssd_commit_flushed() argument
499 wc, in ssd_commit_flushed()
503 unsigned int bitmap_bits = wc->dirty_bitmap_size * 8; in ssd_commit_flushed()
509 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i); in ssd_commit_flushed()
512 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i); in ssd_commit_flushed()
514 region.bdev = wc->ssd_dev->bdev; in ssd_commit_flushed()
518 if (unlikely(region.sector >= wc->metadata_sectors)) in ssd_commit_flushed()
520 if (unlikely(region.sector + region.count > wc->metadata_sectors)) in ssd_commit_flushed()
521 region.count = wc->metadata_sectors - region.sector; in ssd_commit_flushed()
523 region.sector += wc->start_sector; in ssd_commit_flushed()
527 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY; in ssd_commit_flushed()
528 req.client = wc->dm_io; in ssd_commit_flushed()
541 writecache_wait_for_ios(wc, WRITE); in ssd_commit_flushed()
543 writecache_disk_flush(wc, wc->ssd_dev); in ssd_commit_flushed()
545 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size); in ssd_commit_flushed()
548 static void ssd_commit_superblock(struct dm_writecache *wc) in ssd_commit_superblock() argument
554 region.bdev = wc->ssd_dev->bdev; in ssd_commit_superblock()
556 region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT; in ssd_commit_superblock()
558 if (unlikely(region.sector + region.count > wc->metadata_sectors)) in ssd_commit_superblock()
559 region.count = wc->metadata_sectors - region.sector; in ssd_commit_superblock()
561 region.sector += wc->start_sector; in ssd_commit_superblock()
565 req.mem.ptr.vma = (char *)wc->memory_map; in ssd_commit_superblock()
566 req.client = wc->dm_io; in ssd_commit_superblock()
572 writecache_error(wc, r, "error writing superblock"); in ssd_commit_superblock()
575 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios) in writecache_commit_flushed() argument
577 if (WC_MODE_PMEM(wc)) in writecache_commit_flushed()
580 ssd_commit_flushed(wc, wait_for_ios); in writecache_commit_flushed()
583 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev) in writecache_disk_flush() argument
595 req.client = wc->dm_io; in writecache_disk_flush()
600 writecache_error(wc, r, "error flushing metadata: %d", r); in writecache_disk_flush()
606 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc, in writecache_find_entry() argument
610 struct rb_node *node = wc->tree.rb_node; in writecache_find_entry()
617 if (read_original_sector(wc, e) == block) in writecache_find_entry()
620 node = (read_original_sector(wc, e) >= block ? in writecache_find_entry()
625 if (read_original_sector(wc, e) >= block) in writecache_find_entry()
647 if (read_original_sector(wc, e2) != block) in writecache_find_entry()
653 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins) in writecache_insert_entry() argument
656 struct rb_node **node = &wc->tree.rb_node, *parent = NULL; in writecache_insert_entry()
661 if (read_original_sector(wc, e) > read_original_sector(wc, ins)) in writecache_insert_entry()
667 rb_insert_color(&ins->rb_node, &wc->tree); in writecache_insert_entry()
668 list_add(&ins->lru, &wc->lru); in writecache_insert_entry()
672 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e) in writecache_unlink() argument
675 rb_erase(&e->rb_node, &wc->tree); in writecache_unlink()
678 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e) in writecache_add_to_freelist() argument
680 if (WC_MODE_SORT_FREELIST(wc)) { in writecache_add_to_freelist()
681 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL; in writecache_add_to_freelist()
684 wc->current_free = e; in writecache_add_to_freelist()
693 rb_insert_color(&e->rb_node, &wc->freetree); in writecache_add_to_freelist()
695 list_add_tail(&e->lru, &wc->freelist); in writecache_add_to_freelist()
697 wc->freelist_size++; in writecache_add_to_freelist()
700 static inline void writecache_verify_watermark(struct dm_writecache *wc) in writecache_verify_watermark() argument
702 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark)) in writecache_verify_watermark()
703 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_verify_watermark()
708 struct dm_writecache *wc = timer_container_of(wc, t, max_age_timer); in writecache_max_age_timer() local
710 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) { in writecache_max_age_timer()
711 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_max_age_timer()
712 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); in writecache_max_age_timer()
716 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_se… in writecache_pop_from_freelist() argument
720 if (WC_MODE_SORT_FREELIST(wc)) { in writecache_pop_from_freelist()
723 if (unlikely(!wc->current_free)) in writecache_pop_from_freelist()
725 e = wc->current_free; in writecache_pop_from_freelist()
726 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) in writecache_pop_from_freelist()
729 rb_erase(&e->rb_node, &wc->freetree); in writecache_pop_from_freelist()
731 next = rb_first(&wc->freetree); in writecache_pop_from_freelist()
732 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL; in writecache_pop_from_freelist()
734 if (unlikely(list_empty(&wc->freelist))) in writecache_pop_from_freelist()
736 e = container_of(wc->freelist.next, struct wc_entry, lru); in writecache_pop_from_freelist()
737 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector)) in writecache_pop_from_freelist()
741 wc->freelist_size--; in writecache_pop_from_freelist()
743 writecache_verify_watermark(wc); in writecache_pop_from_freelist()
748 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e) in writecache_free_entry() argument
750 writecache_unlink(wc, e); in writecache_free_entry()
751 writecache_add_to_freelist(wc, e); in writecache_free_entry()
752 clear_seq_count(wc, e); in writecache_free_entry()
753 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); in writecache_free_entry()
754 if (unlikely(waitqueue_active(&wc->freelist_wait))) in writecache_free_entry()
755 wake_up(&wc->freelist_wait); in writecache_free_entry()
758 static void writecache_wait_on_freelist(struct dm_writecache *wc) in writecache_wait_on_freelist() argument
762 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE); in writecache_wait_on_freelist()
763 wc_unlock(wc); in writecache_wait_on_freelist()
765 finish_wait(&wc->freelist_wait, &wait); in writecache_wait_on_freelist()
766 wc_lock(wc); in writecache_wait_on_freelist()
769 static void writecache_poison_lists(struct dm_writecache *wc) in writecache_poison_lists() argument
774 memset(&wc->tree, -1, sizeof(wc->tree)); in writecache_poison_lists()
775 wc->lru.next = LIST_POISON1; in writecache_poison_lists()
776 wc->lru.prev = LIST_POISON2; in writecache_poison_lists()
777 wc->freelist.next = LIST_POISON1; in writecache_poison_lists()
778 wc->freelist.prev = LIST_POISON2; in writecache_poison_lists()
781 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e) in writecache_flush_entry() argument
783 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry)); in writecache_flush_entry()
784 if (WC_MODE_PMEM(wc)) in writecache_flush_entry()
785 writecache_flush_region(wc, memory_data(wc, e), wc->block_size); in writecache_flush_entry()
788 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e) in writecache_entry_is_committed() argument
790 return read_seq_count(wc, e) < wc->seq_count; in writecache_entry_is_committed()
793 static void writecache_flush(struct dm_writecache *wc) in writecache_flush() argument
798 wc->uncommitted_blocks = 0; in writecache_flush()
799 timer_delete(&wc->autocommit_timer); in writecache_flush()
801 if (list_empty(&wc->lru)) in writecache_flush()
804 e = container_of(wc->lru.next, struct wc_entry, lru); in writecache_flush()
805 if (writecache_entry_is_committed(wc, e)) { in writecache_flush()
806 if (wc->overwrote_committed) { in writecache_flush()
807 writecache_wait_for_ios(wc, WRITE); in writecache_flush()
808 writecache_disk_flush(wc, wc->ssd_dev); in writecache_flush()
809 wc->overwrote_committed = false; in writecache_flush()
814 writecache_flush_entry(wc, e); in writecache_flush()
815 if (unlikely(e->lru.next == &wc->lru)) in writecache_flush()
818 if (writecache_entry_is_committed(wc, e2)) in writecache_flush()
823 writecache_commit_flushed(wc, true); in writecache_flush()
825 wc->seq_count++; in writecache_flush()
826 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count)); in writecache_flush()
827 if (WC_MODE_PMEM(wc)) in writecache_flush()
828 writecache_commit_flushed(wc, false); in writecache_flush()
830 ssd_commit_superblock(wc); in writecache_flush()
832 wc->overwrote_committed = false; in writecache_flush()
841 if (read_original_sector(wc, e2) == read_original_sector(wc, e) && in writecache_flush()
843 writecache_free_entry(wc, e2); in writecache_flush()
847 if (unlikely(e->lru.prev == &wc->lru)) in writecache_flush()
854 writecache_commit_flushed(wc, false); in writecache_flush()
859 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work); in writecache_flush_work() local
861 wc_lock(wc); in writecache_flush_work()
862 writecache_flush(wc); in writecache_flush_work()
863 wc_unlock(wc); in writecache_flush_work()
868 struct dm_writecache *wc = timer_container_of(wc, t, autocommit_timer); in writecache_autocommit_timer() local
870 if (!writecache_has_error(wc)) in writecache_autocommit_timer()
871 queue_work(wc->writeback_wq, &wc->flush_work); in writecache_autocommit_timer()
874 static void writecache_schedule_autocommit(struct dm_writecache *wc) in writecache_schedule_autocommit() argument
876 if (!timer_pending(&wc->autocommit_timer)) in writecache_schedule_autocommit()
877 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies); in writecache_schedule_autocommit()
880 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end) in writecache_discard() argument
885 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ); in writecache_discard()
889 while (read_original_sector(wc, e) < end) { in writecache_discard()
894 if (!WC_MODE_PMEM(wc)) { in writecache_discard()
895 writecache_wait_for_ios(wc, READ); in writecache_discard()
896 writecache_wait_for_ios(wc, WRITE); in writecache_discard()
900 if (!writecache_entry_is_committed(wc, e)) in writecache_discard()
901 wc->uncommitted_blocks--; in writecache_discard()
902 writecache_free_entry(wc, e); in writecache_discard()
912 writecache_commit_flushed(wc, false); in writecache_discard()
915 static bool writecache_wait_for_writeback(struct dm_writecache *wc) in writecache_wait_for_writeback() argument
917 if (wc->writeback_size) { in writecache_wait_for_writeback()
918 writecache_wait_on_freelist(wc); in writecache_wait_for_writeback()
926 struct dm_writecache *wc = ti->private; in writecache_suspend() local
929 timer_delete_sync(&wc->autocommit_timer); in writecache_suspend()
930 timer_delete_sync(&wc->max_age_timer); in writecache_suspend()
932 wc_lock(wc); in writecache_suspend()
933 writecache_flush(wc); in writecache_suspend()
934 flush_on_suspend = wc->flush_on_suspend; in writecache_suspend()
936 wc->flush_on_suspend = false; in writecache_suspend()
937 wc->writeback_all++; in writecache_suspend()
938 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_suspend()
940 wc_unlock(wc); in writecache_suspend()
942 drain_workqueue(wc->writeback_wq); in writecache_suspend()
944 wc_lock(wc); in writecache_suspend()
946 wc->writeback_all--; in writecache_suspend()
947 while (writecache_wait_for_writeback(wc)) in writecache_suspend()
950 if (WC_MODE_PMEM(wc)) in writecache_suspend()
951 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); in writecache_suspend()
953 writecache_poison_lists(wc); in writecache_suspend()
955 wc_unlock(wc); in writecache_suspend()
958 static int writecache_alloc_entries(struct dm_writecache *wc) in writecache_alloc_entries() argument
962 if (wc->entries) in writecache_alloc_entries()
964 wc->entries = vmalloc_array(wc->n_blocks, sizeof(struct wc_entry)); in writecache_alloc_entries()
965 if (!wc->entries) in writecache_alloc_entries()
967 for (b = 0; b < wc->n_blocks; b++) { in writecache_alloc_entries()
968 struct wc_entry *e = &wc->entries[b]; in writecache_alloc_entries()
978 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors) in writecache_read_metadata() argument
983 region.bdev = wc->ssd_dev->bdev; in writecache_read_metadata()
984 region.sector = wc->start_sector; in writecache_read_metadata()
988 req.mem.ptr.vma = (char *)wc->memory_map; in writecache_read_metadata()
989 req.client = wc->dm_io; in writecache_read_metadata()
997 struct dm_writecache *wc = ti->private; in writecache_resume() local
1003 wc_lock(wc); in writecache_resume()
1005 wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev); in writecache_resume()
1007 if (WC_MODE_PMEM(wc)) { in writecache_resume()
1008 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size); in writecache_resume()
1010 r = writecache_read_metadata(wc, wc->metadata_sectors); in writecache_resume()
1014 writecache_error(wc, r, "unable to read metadata: %d", r); in writecache_resume()
1016 memset((char *)wc->memory_map + sb_entries_offset, -1, in writecache_resume()
1017 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset); in writecache_resume()
1021 wc->tree = RB_ROOT; in writecache_resume()
1022 INIT_LIST_HEAD(&wc->lru); in writecache_resume()
1023 if (WC_MODE_SORT_FREELIST(wc)) { in writecache_resume()
1024 wc->freetree = RB_ROOT; in writecache_resume()
1025 wc->current_free = NULL; in writecache_resume()
1027 INIT_LIST_HEAD(&wc->freelist); in writecache_resume()
1029 wc->freelist_size = 0; in writecache_resume()
1031 r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count, in writecache_resume()
1034 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r); in writecache_resume()
1037 wc->seq_count = le64_to_cpu(sb_seq_count); in writecache_resume()
1040 for (b = 0; b < wc->n_blocks; b++) { in writecache_resume()
1041 struct wc_entry *e = &wc->entries[b]; in writecache_resume()
1044 if (writecache_has_error(wc)) { in writecache_resume()
1049 r = copy_mc_to_kernel(&wme, memory_entry(wc, e), in writecache_resume()
1052 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d", in writecache_resume()
1063 for (b = 0; b < wc->n_blocks; b++) { in writecache_resume()
1064 struct wc_entry *e = &wc->entries[b]; in writecache_resume()
1066 if (!writecache_entry_is_committed(wc, e)) { in writecache_resume()
1067 if (read_seq_count(wc, e) != -1) { in writecache_resume()
1069 clear_seq_count(wc, e); in writecache_resume()
1072 writecache_add_to_freelist(wc, e); in writecache_resume()
1076 old = writecache_find_entry(wc, read_original_sector(wc, e), 0); in writecache_resume()
1078 writecache_insert_entry(wc, e); in writecache_resume()
1080 if (read_seq_count(wc, old) == read_seq_count(wc, e)) { in writecache_resume()
1081 writecache_error(wc, -EINVAL, in writecache_resume()
1083 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e), in writecache_resume()
1084 (unsigned long long)read_seq_count(wc, e)); in writecache_resume()
1086 if (read_seq_count(wc, old) > read_seq_count(wc, e)) { in writecache_resume()
1089 writecache_free_entry(wc, old); in writecache_resume()
1090 writecache_insert_entry(wc, e); in writecache_resume()
1099 writecache_flush_all_metadata(wc); in writecache_resume()
1100 writecache_commit_flushed(wc, false); in writecache_resume()
1103 writecache_verify_watermark(wc); in writecache_resume()
1105 if (wc->max_age != MAX_AGE_UNSPECIFIED) in writecache_resume()
1106 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV); in writecache_resume()
1108 wc_unlock(wc); in writecache_resume()
1111 static int process_flush_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_flush_mesg() argument
1116 wc_lock(wc); in process_flush_mesg()
1117 if (dm_suspended(wc->ti)) { in process_flush_mesg()
1118 wc_unlock(wc); in process_flush_mesg()
1121 if (writecache_has_error(wc)) { in process_flush_mesg()
1122 wc_unlock(wc); in process_flush_mesg()
1126 writecache_flush(wc); in process_flush_mesg()
1127 wc->writeback_all++; in process_flush_mesg()
1128 queue_work(wc->writeback_wq, &wc->writeback_work); in process_flush_mesg()
1129 wc_unlock(wc); in process_flush_mesg()
1131 flush_workqueue(wc->writeback_wq); in process_flush_mesg()
1133 wc_lock(wc); in process_flush_mesg()
1134 wc->writeback_all--; in process_flush_mesg()
1135 if (writecache_has_error(wc)) { in process_flush_mesg()
1136 wc_unlock(wc); in process_flush_mesg()
1139 wc_unlock(wc); in process_flush_mesg()
1144 static int process_flush_on_suspend_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_flush_on_suspend_mesg() argument
1149 wc_lock(wc); in process_flush_on_suspend_mesg()
1150 wc->flush_on_suspend = true; in process_flush_on_suspend_mesg()
1151 wc_unlock(wc); in process_flush_on_suspend_mesg()
1156 static void activate_cleaner(struct dm_writecache *wc) in activate_cleaner() argument
1158 wc->flush_on_suspend = true; in activate_cleaner()
1159 wc->cleaner = true; in activate_cleaner()
1160 wc->freelist_high_watermark = wc->n_blocks; in activate_cleaner()
1161 wc->freelist_low_watermark = wc->n_blocks; in activate_cleaner()
1164 static int process_cleaner_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_cleaner_mesg() argument
1169 wc_lock(wc); in process_cleaner_mesg()
1170 activate_cleaner(wc); in process_cleaner_mesg()
1171 if (!dm_suspended(wc->ti)) in process_cleaner_mesg()
1172 writecache_verify_watermark(wc); in process_cleaner_mesg()
1173 wc_unlock(wc); in process_cleaner_mesg()
1178 static int process_clear_stats_mesg(unsigned int argc, char **argv, struct dm_writecache *wc) in process_clear_stats_mesg() argument
1183 wc_lock(wc); in process_clear_stats_mesg()
1184 memset(&wc->stats, 0, sizeof(wc->stats)); in process_clear_stats_mesg()
1185 wc_unlock(wc); in process_clear_stats_mesg()
1194 struct dm_writecache *wc = ti->private; in writecache_message() local
1197 r = process_flush_mesg(argc, argv, wc); in writecache_message()
1199 r = process_flush_on_suspend_mesg(argc, argv, wc); in writecache_message()
1201 r = process_cleaner_mesg(argc, argv, wc); in writecache_message()
1203 r = process_clear_stats_mesg(argc, argv, wc); in writecache_message()
1246 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data) in bio_copy_block() argument
1251 unsigned int remaining_size = wc->block_size; in bio_copy_block()
1267 writecache_error(wc, r, "hardware memory error when reading data: %d", r); in bio_copy_block()
1285 struct dm_writecache *wc = data; in writecache_flush_thread() local
1290 wc_lock(wc); in writecache_flush_thread()
1291 bio = bio_list_pop(&wc->flush_list); in writecache_flush_thread()
1294 wc_unlock(wc); in writecache_flush_thread()
1306 writecache_discard(wc, bio->bi_iter.bi_sector, in writecache_flush_thread()
1308 wc_unlock(wc); in writecache_flush_thread()
1309 bio_set_dev(bio, wc->dev->bdev); in writecache_flush_thread()
1312 writecache_flush(wc); in writecache_flush_thread()
1313 wc_unlock(wc); in writecache_flush_thread()
1314 if (writecache_has_error(wc)) in writecache_flush_thread()
1323 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio) in writecache_offload_bio() argument
1325 if (bio_list_empty(&wc->flush_list)) in writecache_offload_bio()
1326 wake_up_process(wc->flush_thread); in writecache_offload_bio()
1327 bio_list_add(&wc->flush_list, bio); in writecache_offload_bio()
1338 static void writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio, in writecache_map_remap_origin() argument
1343 read_original_sector(wc, e) - bio->bi_iter.bi_sector; in writecache_map_remap_origin()
1349 static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio) in writecache_map_read() argument
1355 wc->stats.reads++; in writecache_map_read()
1356 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); in writecache_map_read()
1357 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) { in writecache_map_read()
1358 wc->stats.read_hits++; in writecache_map_read()
1359 if (WC_MODE_PMEM(wc)) { in writecache_map_read()
1360 bio_copy_block(wc, bio, memory_data(wc, e)); in writecache_map_read()
1365 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT); in writecache_map_read()
1366 bio_set_dev(bio, wc->ssd_dev->bdev); in writecache_map_read()
1367 bio->bi_iter.bi_sector = cache_sector(wc, e); in writecache_map_read()
1368 if (!writecache_entry_is_committed(wc, e)) in writecache_map_read()
1369 writecache_wait_for_ios(wc, WRITE); in writecache_map_read()
1373 writecache_map_remap_origin(wc, bio, e); in writecache_map_read()
1374 wc->stats.reads += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits; in writecache_map_read()
1381 static void writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio, in writecache_bio_copy_ssd() argument
1384 unsigned int bio_size = wc->block_size; in writecache_bio_copy_ssd()
1385 sector_t start_cache_sec = cache_sector(wc, e); in writecache_bio_copy_ssd()
1390 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec); in writecache_bio_copy_ssd()
1394 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector + in writecache_bio_copy_ssd()
1395 (bio_size >> SECTOR_SHIFT), wc->seq_count); in writecache_bio_copy_ssd()
1396 writecache_insert_entry(wc, f); in writecache_bio_copy_ssd()
1397 wc->uncommitted_blocks++; in writecache_bio_copy_ssd()
1407 if (read_original_sector(wc, f) != in writecache_bio_copy_ssd()
1408 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) in writecache_bio_copy_ssd()
1412 if (writecache_entry_is_committed(wc, f)) in writecache_bio_copy_ssd()
1413 wc->overwrote_committed = true; in writecache_bio_copy_ssd()
1416 bio_size += wc->block_size; in writecache_bio_copy_ssd()
1417 current_cache_sec += wc->block_size >> SECTOR_SHIFT; in writecache_bio_copy_ssd()
1420 bio_set_dev(bio, wc->ssd_dev->bdev); in writecache_bio_copy_ssd()
1424 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_bio_copy_ssd()
1425 wc->stats.writes_allocate += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits; in writecache_bio_copy_ssd()
1427 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) { in writecache_bio_copy_ssd()
1428 wc->uncommitted_blocks = 0; in writecache_bio_copy_ssd()
1429 queue_work(wc->writeback_wq, &wc->flush_work); in writecache_bio_copy_ssd()
1431 writecache_schedule_autocommit(wc); in writecache_bio_copy_ssd()
1435 static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio) in writecache_map_write() argument
1443 if (writecache_has_error(wc)) { in writecache_map_write()
1444 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_write()
1447 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0); in writecache_map_write()
1449 if (!writecache_entry_is_committed(wc, e)) { in writecache_map_write()
1450 wc->stats.write_hits_uncommitted++; in writecache_map_write()
1454 wc->stats.write_hits_committed++; in writecache_map_write()
1455 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) { in writecache_map_write()
1456 wc->overwrote_committed = true; in writecache_map_write()
1462 if (unlikely(wc->cleaner) || in writecache_map_write()
1463 (wc->metadata_only && !(bio->bi_opf & REQ_META))) in writecache_map_write()
1466 e = writecache_pop_from_freelist(wc, (sector_t)-1); in writecache_map_write()
1468 if (!WC_MODE_PMEM(wc) && !found_entry) { in writecache_map_write()
1470 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING); in writecache_map_write()
1471 writecache_map_remap_origin(wc, bio, e); in writecache_map_write()
1472 wc->stats.writes_around += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_write()
1473 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_write()
1476 wc->stats.writes_blocked_on_freelist++; in writecache_map_write()
1477 writecache_wait_on_freelist(wc); in writecache_map_write()
1480 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count); in writecache_map_write()
1481 writecache_insert_entry(wc, e); in writecache_map_write()
1482 wc->uncommitted_blocks++; in writecache_map_write()
1483 wc->stats.writes_allocate++; in writecache_map_write()
1485 if (WC_MODE_PMEM(wc)) { in writecache_map_write()
1486 bio_copy_block(wc, bio, memory_data(wc, e)); in writecache_map_write()
1487 wc->stats.writes++; in writecache_map_write()
1489 writecache_bio_copy_ssd(wc, bio, e, search_used); in writecache_map_write()
1494 if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks)) in writecache_map_write()
1495 writecache_flush(wc); in writecache_map_write()
1497 writecache_schedule_autocommit(wc); in writecache_map_write()
1502 static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio) in writecache_map_flush() argument
1504 if (writecache_has_error(wc)) in writecache_map_flush()
1507 if (WC_MODE_PMEM(wc)) { in writecache_map_flush()
1508 wc->stats.flushes++; in writecache_map_flush()
1509 writecache_flush(wc); in writecache_map_flush()
1510 if (writecache_has_error(wc)) in writecache_map_flush()
1512 else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only)) in writecache_map_flush()
1519 wc->stats.flushes++; in writecache_map_flush()
1520 writecache_offload_bio(wc, bio); in writecache_map_flush()
1524 static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio) in writecache_map_discard() argument
1526 wc->stats.discards += bio->bi_iter.bi_size >> wc->block_size_bits; in writecache_map_discard()
1528 if (writecache_has_error(wc)) in writecache_map_discard()
1531 if (WC_MODE_PMEM(wc)) { in writecache_map_discard()
1532 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio)); in writecache_map_discard()
1536 writecache_offload_bio(wc, bio); in writecache_map_discard()
1542 struct dm_writecache *wc = ti->private; in writecache_map() local
1547 wc_lock(wc); in writecache_map()
1550 map_op = writecache_map_flush(wc, bio); in writecache_map()
1557 (wc->block_size / 512 - 1)) != 0)) { in writecache_map()
1560 bio->bi_iter.bi_size, wc->block_size); in writecache_map()
1566 map_op = writecache_map_discard(wc, bio); in writecache_map()
1571 map_op = writecache_map_read(wc, bio); in writecache_map()
1573 map_op = writecache_map_write(wc, bio); in writecache_map()
1577 if (likely(wc->pause != 0)) { in writecache_map()
1579 dm_iot_io_begin(&wc->iot, 1); in writecache_map()
1583 bio_set_dev(bio, wc->dev->bdev); in writecache_map()
1584 wc_unlock(wc); in writecache_map()
1590 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]); in writecache_map()
1591 wc_unlock(wc); in writecache_map()
1595 wc_unlock(wc); in writecache_map()
1600 wc_unlock(wc); in writecache_map()
1604 wc_unlock(wc); in writecache_map()
1610 wc_unlock(wc); in writecache_map()
1617 struct dm_writecache *wc = ti->private; in writecache_end_io() local
1622 if (atomic_dec_and_test(&wc->bio_in_progress[dir])) in writecache_end_io()
1623 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir]))) in writecache_end_io()
1624 wake_up(&wc->bio_in_progress_wait[dir]); in writecache_end_io()
1626 dm_iot_io_end(&wc->iot, 1); in writecache_end_io()
1634 struct dm_writecache *wc = ti->private; in writecache_iterate_devices() local
1636 return fn(ti, wc->dev, 0, ti->len, data); in writecache_iterate_devices()
1641 struct dm_writecache *wc = ti->private; in writecache_io_hints() local
1643 if (limits->logical_block_size < wc->block_size) in writecache_io_hints()
1644 limits->logical_block_size = wc->block_size; in writecache_io_hints()
1646 if (limits->physical_block_size < wc->block_size) in writecache_io_hints()
1647 limits->physical_block_size = wc->block_size; in writecache_io_hints()
1649 if (limits->io_min < wc->block_size) in writecache_io_hints()
1650 limits->io_min = wc->block_size; in writecache_io_hints()
1657 struct dm_writecache *wc = wb->wc; in writecache_writeback_endio() local
1660 raw_spin_lock_irqsave(&wc->endio_list_lock, flags); in writecache_writeback_endio()
1661 if (unlikely(list_empty(&wc->endio_list))) in writecache_writeback_endio()
1662 wake_up_process(wc->endio_thread); in writecache_writeback_endio()
1663 list_add_tail(&wb->endio_entry, &wc->endio_list); in writecache_writeback_endio()
1664 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags); in writecache_writeback_endio()
1670 struct dm_writecache *wc = c->wc; in writecache_copy_endio() local
1674 raw_spin_lock_irq(&wc->endio_list_lock); in writecache_copy_endio()
1675 if (unlikely(list_empty(&wc->endio_list))) in writecache_copy_endio()
1676 wake_up_process(wc->endio_thread); in writecache_copy_endio()
1677 list_add_tail(&c->endio_entry, &wc->endio_list); in writecache_copy_endio()
1678 raw_spin_unlock_irq(&wc->endio_list_lock); in writecache_copy_endio()
1681 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list) in __writecache_endio_pmem() argument
1693 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status), in __writecache_endio_pmem()
1701 if (!writecache_has_error(wc)) in __writecache_endio_pmem()
1702 writecache_free_entry(wc, e); in __writecache_endio_pmem()
1703 BUG_ON(!wc->writeback_size); in __writecache_endio_pmem()
1704 wc->writeback_size--; in __writecache_endio_pmem()
1707 writecache_commit_flushed(wc, false); in __writecache_endio_pmem()
1708 wc_unlock(wc); in __writecache_endio_pmem()
1709 wc_lock(wc); in __writecache_endio_pmem()
1720 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list) in __writecache_endio_ssd() argument
1730 writecache_error(wc, c->error, "copy error"); in __writecache_endio_ssd()
1737 if (!writecache_has_error(wc)) in __writecache_endio_ssd()
1738 writecache_free_entry(wc, e); in __writecache_endio_ssd()
1740 BUG_ON(!wc->writeback_size); in __writecache_endio_ssd()
1741 wc->writeback_size--; in __writecache_endio_ssd()
1744 mempool_free(c, &wc->copy_pool); in __writecache_endio_ssd()
1750 struct dm_writecache *wc = data; in writecache_endio_thread() local
1755 raw_spin_lock_irq(&wc->endio_list_lock); in writecache_endio_thread()
1756 if (!list_empty(&wc->endio_list)) in writecache_endio_thread()
1759 raw_spin_unlock_irq(&wc->endio_list_lock); in writecache_endio_thread()
1771 list = wc->endio_list; in writecache_endio_thread()
1773 INIT_LIST_HEAD(&wc->endio_list); in writecache_endio_thread()
1774 raw_spin_unlock_irq(&wc->endio_list_lock); in writecache_endio_thread()
1776 if (!WC_MODE_FUA(wc)) in writecache_endio_thread()
1777 writecache_disk_flush(wc, wc->dev); in writecache_endio_thread()
1779 wc_lock(wc); in writecache_endio_thread()
1781 if (WC_MODE_PMEM(wc)) { in writecache_endio_thread()
1782 __writecache_endio_pmem(wc, &list); in writecache_endio_thread()
1784 __writecache_endio_ssd(wc, &list); in writecache_endio_thread()
1785 writecache_wait_for_ios(wc, READ); in writecache_endio_thread()
1788 writecache_commit_flushed(wc, false); in writecache_endio_thread()
1790 wc_unlock(wc); in writecache_endio_thread()
1798 struct dm_writecache *wc = wb->wc; in wc_add_block() local
1799 unsigned int block_size = wc->block_size; in wc_add_block()
1800 void *address = memory_data(wc, e); in wc_add_block()
1804 if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors)) in wc_add_block()
1816 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl) in __writeback_throttle() argument
1818 if (unlikely(wc->max_writeback_jobs)) { in __writeback_throttle()
1819 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) { in __writeback_throttle()
1820 wc_lock(wc); in __writeback_throttle()
1821 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs) in __writeback_throttle()
1822 writecache_wait_on_freelist(wc); in __writeback_throttle()
1823 wc_unlock(wc); in __writeback_throttle()
1829 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl) in __writecache_writeback_pmem() argument
1843 bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE, in __writecache_writeback_pmem()
1844 GFP_NOIO, &wc->bio_set); in __writecache_writeback_pmem()
1846 wb->wc = wc; in __writecache_writeback_pmem()
1848 bio->bi_iter.bi_sector = read_original_sector(wc, e); in __writecache_writeback_pmem()
1867 if (read_original_sector(wc, f) != in __writecache_writeback_pmem()
1868 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT)) in __writecache_writeback_pmem()
1877 if (WC_MODE_FUA(wc)) in __writecache_writeback_pmem()
1879 if (writecache_has_error(wc)) { in __writecache_writeback_pmem()
1889 __writeback_throttle(wc, wbl); in __writecache_writeback_pmem()
1893 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl) in __writecache_writeback_ssd() argument
1906 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT); in __writecache_writeback_ssd()
1908 from.bdev = wc->ssd_dev->bdev; in __writecache_writeback_ssd()
1909 from.sector = cache_sector(wc, e); in __writecache_writeback_ssd()
1911 to.bdev = wc->dev->bdev; in __writecache_writeback_ssd()
1912 to.sector = read_original_sector(wc, e); in __writecache_writeback_ssd()
1915 c = mempool_alloc(&wc->copy_pool, GFP_NOIO); in __writecache_writeback_ssd()
1916 c->wc = wc; in __writecache_writeback_ssd()
1920 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) { in __writecache_writeback_ssd()
1928 if (unlikely(to.sector + to.count > wc->data_device_sectors)) { in __writecache_writeback_ssd()
1929 if (to.sector >= wc->data_device_sectors) { in __writecache_writeback_ssd()
1933 from.count = to.count = wc->data_device_sectors - to.sector; in __writecache_writeback_ssd()
1936 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c); in __writecache_writeback_ssd()
1938 __writeback_throttle(wc, wbl); in __writecache_writeback_ssd()
1944 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work); in writecache_writeback() local
1952 if (!WC_MODE_PMEM(wc)) { in writecache_writeback()
1954 dm_kcopyd_client_flush(wc->dm_kcopyd); in writecache_writeback()
1957 if (likely(wc->pause != 0)) { in writecache_writeback()
1961 if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) || in writecache_writeback()
1962 unlikely(dm_suspended(wc->ti))) in writecache_writeback()
1964 idle = dm_iot_idle_time(&wc->iot); in writecache_writeback()
1965 if (idle >= wc->pause) in writecache_writeback()
1967 idle = wc->pause - idle; in writecache_writeback()
1974 wc_lock(wc); in writecache_writeback()
1976 if (writecache_has_error(wc)) { in writecache_writeback()
1977 wc_unlock(wc); in writecache_writeback()
1981 if (unlikely(wc->writeback_all)) { in writecache_writeback()
1982 if (writecache_wait_for_writeback(wc)) in writecache_writeback()
1986 if (wc->overwrote_committed) in writecache_writeback()
1987 writecache_wait_for_ios(wc, WRITE); in writecache_writeback()
1993 while (!list_empty(&wc->lru) && in writecache_writeback()
1994 (wc->writeback_all || in writecache_writeback()
1995 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark || in writecache_writeback()
1996 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >= in writecache_writeback()
1997 wc->max_age - wc->max_age / MAX_AGE_DIV))) { in writecache_writeback()
2001 likely(!wc->writeback_all)) { in writecache_writeback()
2002 if (likely(!dm_suspended(wc->ti))) in writecache_writeback()
2003 queue_work(wc->writeback_wq, &wc->writeback_work); in writecache_writeback()
2007 if (unlikely(wc->writeback_all)) { in writecache_writeback()
2009 writecache_flush(wc); in writecache_writeback()
2010 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node); in writecache_writeback()
2014 e = container_of(wc->lru.prev, struct wc_entry, lru); in writecache_writeback()
2016 if (unlikely(!writecache_entry_is_committed(wc, e))) in writecache_writeback()
2017 writecache_flush(wc); in writecache_writeback()
2022 if (unlikely(read_original_sector(wc, f) == in writecache_writeback()
2023 read_original_sector(wc, e))) { in writecache_writeback()
2030 wc->writeback_size++; in writecache_writeback()
2043 if (unlikely(read_original_sector(wc, g) == in writecache_writeback()
2044 read_original_sector(wc, f))) { in writecache_writeback()
2048 if (read_original_sector(wc, g) != in writecache_writeback()
2049 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT)) in writecache_writeback()
2053 if (unlikely(!writecache_entry_is_committed(wc, g))) in writecache_writeback()
2056 if (!WC_MODE_PMEM(wc)) { in writecache_writeback()
2062 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all)) in writecache_writeback()
2065 wc->writeback_size++; in writecache_writeback()
2073 if (unlikely(wc->writeback_all)) { in writecache_writeback()
2085 list_splice_tail(&skipped, &wc->lru); in writecache_writeback()
2091 writecache_wait_for_writeback(wc); in writecache_writeback()
2094 wc_unlock(wc); in writecache_writeback()
2098 if (WC_MODE_PMEM(wc)) in writecache_writeback()
2099 __writecache_writeback_pmem(wc, &wbl); in writecache_writeback()
2101 __writecache_writeback_ssd(wc, &wbl); in writecache_writeback()
2105 if (unlikely(wc->writeback_all)) { in writecache_writeback()
2106 wc_lock(wc); in writecache_writeback()
2107 while (writecache_wait_for_writeback(wc)) in writecache_writeback()
2109 wc_unlock(wc); in writecache_writeback()
2148 static int init_memory(struct dm_writecache *wc) in init_memory() argument
2153 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL); in init_memory()
2157 r = writecache_alloc_entries(wc); in init_memory()
2161 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++) in init_memory()
2162 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0)); in init_memory()
2163 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION)); in init_memory()
2164 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size)); in init_memory()
2165 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks)); in init_memory()
2166 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0)); in init_memory()
2168 for (b = 0; b < wc->n_blocks; b++) { in init_memory()
2169 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1); in init_memory()
2173 writecache_flush_all_metadata(wc); in init_memory()
2174 writecache_commit_flushed(wc, false); in init_memory()
2175 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC)); in init_memory()
2176 writecache_flush_region(wc, &sb(wc)->magic, sizeof(sb(wc)->magic)); in init_memory()
2177 writecache_commit_flushed(wc, false); in init_memory()
2184 struct dm_writecache *wc = ti->private; in writecache_dtr() local
2186 if (!wc) in writecache_dtr()
2189 if (wc->endio_thread) in writecache_dtr()
2190 kthread_stop(wc->endio_thread); in writecache_dtr()
2192 if (wc->flush_thread) in writecache_dtr()
2193 kthread_stop(wc->flush_thread); in writecache_dtr()
2195 bioset_exit(&wc->bio_set); in writecache_dtr()
2197 mempool_exit(&wc->copy_pool); in writecache_dtr()
2199 if (wc->writeback_wq) in writecache_dtr()
2200 destroy_workqueue(wc->writeback_wq); in writecache_dtr()
2202 if (wc->dev) in writecache_dtr()
2203 dm_put_device(ti, wc->dev); in writecache_dtr()
2205 if (wc->ssd_dev) in writecache_dtr()
2206 dm_put_device(ti, wc->ssd_dev); in writecache_dtr()
2208 vfree(wc->entries); in writecache_dtr()
2210 if (wc->memory_map) { in writecache_dtr()
2211 if (WC_MODE_PMEM(wc)) in writecache_dtr()
2212 persistent_memory_release(wc); in writecache_dtr()
2214 vfree(wc->memory_map); in writecache_dtr()
2217 if (wc->dm_kcopyd) in writecache_dtr()
2218 dm_kcopyd_client_destroy(wc->dm_kcopyd); in writecache_dtr()
2220 if (wc->dm_io) in writecache_dtr()
2221 dm_io_client_destroy(wc->dm_io); in writecache_dtr()
2223 vfree(wc->dirty_bitmap); in writecache_dtr()
2225 kfree(wc); in writecache_dtr()
2230 struct dm_writecache *wc; in writecache_ctr() local
2249 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL); in writecache_ctr()
2250 if (!wc) { in writecache_ctr()
2255 ti->private = wc; in writecache_ctr()
2256 wc->ti = ti; in writecache_ctr()
2258 mutex_init(&wc->lock); in writecache_ctr()
2259 wc->max_age = MAX_AGE_UNSPECIFIED; in writecache_ctr()
2260 writecache_poison_lists(wc); in writecache_ctr()
2261 init_waitqueue_head(&wc->freelist_wait); in writecache_ctr()
2262 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0); in writecache_ctr()
2263 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0); in writecache_ctr()
2266 atomic_set(&wc->bio_in_progress[i], 0); in writecache_ctr()
2267 init_waitqueue_head(&wc->bio_in_progress_wait[i]); in writecache_ctr()
2270 wc->dm_io = dm_io_client_create(); in writecache_ctr()
2271 if (IS_ERR(wc->dm_io)) { in writecache_ctr()
2272 r = PTR_ERR(wc->dm_io); in writecache_ctr()
2274 wc->dm_io = NULL; in writecache_ctr()
2278 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1); in writecache_ctr()
2279 if (!wc->writeback_wq) { in writecache_ctr()
2284 INIT_WORK(&wc->writeback_work, writecache_writeback); in writecache_ctr()
2285 INIT_WORK(&wc->flush_work, writecache_flush_work); in writecache_ctr()
2287 dm_iot_init(&wc->iot); in writecache_ctr()
2289 raw_spin_lock_init(&wc->endio_list_lock); in writecache_ctr()
2290 INIT_LIST_HEAD(&wc->endio_list); in writecache_ctr()
2291 wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio"); in writecache_ctr()
2292 if (IS_ERR(wc->endio_thread)) { in writecache_ctr()
2293 r = PTR_ERR(wc->endio_thread); in writecache_ctr()
2294 wc->endio_thread = NULL; in writecache_ctr()
2307 wc->pmem_mode = false; in writecache_ctr()
2310 wc->pmem_mode = true; in writecache_ctr()
2311 wc->writeback_fua = true; in writecache_ctr()
2326 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2327 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE, in writecache_ctr()
2335 wc->pause = PAUSE_WRITEBACK; in writecache_ctr()
2336 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct)); in writecache_ctr()
2349 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev); in writecache_ctr()
2362 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev); in writecache_ctr()
2367 wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev); in writecache_ctr()
2375 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 || in writecache_ctr()
2376 wc->block_size < 512 || wc->block_size > PAGE_SIZE || in writecache_ctr()
2377 (wc->block_size & (wc->block_size - 1))) { in writecache_ctr()
2382 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) || in writecache_ctr()
2383 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) { in writecache_ctr()
2388 wc->block_size_bits = __ffs(wc->block_size); in writecache_ctr()
2390 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS; in writecache_ctr()
2391 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM; in writecache_ctr()
2392 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC); in writecache_ctr()
2409 wc->start_sector = start_sector; in writecache_ctr()
2410 wc->start_sector_set = true; in writecache_ctr()
2411 if (wc->start_sector != start_sector || in writecache_ctr()
2412 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT) in writecache_ctr()
2420 wc->high_wm_percent_value = high_wm_percent; in writecache_ctr()
2421 wc->high_wm_percent_set = true; in writecache_ctr()
2428 wc->low_wm_percent_value = low_wm_percent; in writecache_ctr()
2429 wc->low_wm_percent_set = true; in writecache_ctr()
2432 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1) in writecache_ctr()
2434 wc->max_writeback_jobs_set = true; in writecache_ctr()
2437 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1) in writecache_ctr()
2439 wc->autocommit_blocks_set = true; in writecache_ctr()
2448 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs); in writecache_ctr()
2449 wc->autocommit_time_value = autocommit_msecs; in writecache_ctr()
2450 wc->autocommit_time_set = true; in writecache_ctr()
2459 wc->max_age = msecs_to_jiffies(max_age_msecs); in writecache_ctr()
2460 wc->max_age_set = true; in writecache_ctr()
2461 wc->max_age_value = max_age_msecs; in writecache_ctr()
2463 wc->cleaner_set = true; in writecache_ctr()
2464 wc->cleaner = true; in writecache_ctr()
2466 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2467 wc->writeback_fua = true; in writecache_ctr()
2468 wc->writeback_fua_set = true; in writecache_ctr()
2472 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2473 wc->writeback_fua = false; in writecache_ctr()
2474 wc->writeback_fua_set = true; in writecache_ctr()
2478 wc->metadata_only = true; in writecache_ctr()
2482 if (WC_MODE_PMEM(wc)) in writecache_ctr()
2489 wc->pause = msecs_to_jiffies(pause_msecs); in writecache_ctr()
2490 wc->pause_set = true; in writecache_ctr()
2491 wc->pause_value = pause_msecs; in writecache_ctr()
2506 if (WC_MODE_PMEM(wc)) { in writecache_ctr()
2507 if (!dax_synchronous(wc->ssd_dev->dax_dev)) { in writecache_ctr()
2513 r = persistent_memory_claim(wc); in writecache_ctr()
2522 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT; in writecache_ctr()
2524 bio_list_init(&wc->flush_list); in writecache_ctr()
2525 wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush"); in writecache_ctr()
2526 if (IS_ERR(wc->flush_thread)) { in writecache_ctr()
2527 r = PTR_ERR(wc->flush_thread); in writecache_ctr()
2528 wc->flush_thread = NULL; in writecache_ctr()
2533 r = calculate_memory_size(wc->memory_map_size, wc->block_size, in writecache_ctr()
2540 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) + in writecache_ctr()
2549 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits); in writecache_ctr()
2550 if (!wc->memory_map) { in writecache_ctr()
2556 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle); in writecache_ctr()
2557 if (IS_ERR(wc->dm_kcopyd)) { in writecache_ctr()
2558 r = PTR_ERR(wc->dm_kcopyd); in writecache_ctr()
2560 wc->dm_kcopyd = NULL; in writecache_ctr()
2564 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT); in writecache_ctr()
2565 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) / in writecache_ctr()
2567 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size); in writecache_ctr()
2568 if (!wc->dirty_bitmap) { in writecache_ctr()
2574 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT); in writecache_ctr()
2581 r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock)); in writecache_ctr()
2587 r = init_memory(wc); in writecache_ctr()
2592 r = copy_mc_to_kernel(&s, sb(wc), in writecache_ctr()
2612 if (le32_to_cpu(s.block_size) != wc->block_size) { in writecache_ctr()
2618 wc->n_blocks = le64_to_cpu(s.n_blocks); in writecache_ctr()
2620 offset = wc->n_blocks * sizeof(struct wc_memory_entry); in writecache_ctr()
2621 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) { in writecache_ctr()
2630 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1); in writecache_ctr()
2631 data_size = wc->n_blocks * (size_t)wc->block_size; in writecache_ctr()
2632 if (!offset || (data_size / wc->block_size != wc->n_blocks) || in writecache_ctr()
2635 if (offset + data_size > wc->memory_map_size) { in writecache_ctr()
2641 wc->metadata_sectors = offset >> SECTOR_SHIFT; in writecache_ctr()
2642 wc->block_start = (char *)sb(wc) + offset; in writecache_ctr()
2644 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent); in writecache_ctr()
2647 wc->freelist_high_watermark = x; in writecache_ctr()
2648 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent); in writecache_ctr()
2651 wc->freelist_low_watermark = x; in writecache_ctr()
2653 if (wc->cleaner) in writecache_ctr()
2654 activate_cleaner(wc); in writecache_ctr()
2656 r = writecache_alloc_entries(wc); in writecache_ctr()
2662 ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2; in writecache_ctr()
2666 if (WC_MODE_PMEM(wc)) in writecache_ctr()
2667 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size); in writecache_ctr()
2682 struct dm_writecache *wc = ti->private; in writecache_status() local
2689 writecache_has_error(wc), in writecache_status()
2690 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size, in writecache_status()
2691 (unsigned long long)wc->writeback_size, in writecache_status()
2692 wc->stats.reads, in writecache_status()
2693 wc->stats.read_hits, in writecache_status()
2694 wc->stats.writes, in writecache_status()
2695 wc->stats.write_hits_uncommitted, in writecache_status()
2696 wc->stats.write_hits_committed, in writecache_status()
2697 wc->stats.writes_around, in writecache_status()
2698 wc->stats.writes_allocate, in writecache_status()
2699 wc->stats.writes_blocked_on_freelist, in writecache_status()
2700 wc->stats.flushes, in writecache_status()
2701 wc->stats.discards); in writecache_status()
2704 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's', in writecache_status()
2705 wc->dev->name, wc->ssd_dev->name, wc->block_size); in writecache_status()
2707 if (wc->start_sector_set) in writecache_status()
2709 if (wc->high_wm_percent_set) in writecache_status()
2711 if (wc->low_wm_percent_set) in writecache_status()
2713 if (wc->max_writeback_jobs_set) in writecache_status()
2715 if (wc->autocommit_blocks_set) in writecache_status()
2717 if (wc->autocommit_time_set) in writecache_status()
2719 if (wc->max_age_set) in writecache_status()
2721 if (wc->cleaner_set) in writecache_status()
2723 if (wc->writeback_fua_set) in writecache_status()
2725 if (wc->metadata_only) in writecache_status()
2727 if (wc->pause_set) in writecache_status()
2731 if (wc->start_sector_set) in writecache_status()
2732 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector); in writecache_status()
2733 if (wc->high_wm_percent_set) in writecache_status()
2734 DMEMIT(" high_watermark %u", wc->high_wm_percent_value); in writecache_status()
2735 if (wc->low_wm_percent_set) in writecache_status()
2736 DMEMIT(" low_watermark %u", wc->low_wm_percent_value); in writecache_status()
2737 if (wc->max_writeback_jobs_set) in writecache_status()
2738 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs); in writecache_status()
2739 if (wc->autocommit_blocks_set) in writecache_status()
2740 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks); in writecache_status()
2741 if (wc->autocommit_time_set) in writecache_status()
2742 DMEMIT(" autocommit_time %u", wc->autocommit_time_value); in writecache_status()
2743 if (wc->max_age_set) in writecache_status()
2744 DMEMIT(" max_age %u", wc->max_age_value); in writecache_status()
2745 if (wc->cleaner_set) in writecache_status()
2747 if (wc->writeback_fua_set) in writecache_status()
2748 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no"); in writecache_status()
2749 if (wc->metadata_only) in writecache_status()
2751 if (wc->pause_set) in writecache_status()
2752 DMEMIT(" pause_writeback %u", wc->pause_value); in writecache_status()