This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 418
/
flashcache_conf.c
1773 lines (1653 loc) · 57.8 KB
/
flashcache_conf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* flashcache_conf.c
* FlashCache: Device mapper target for block-level disk caching
*
* Copyright 2010 Facebook, Inc.
* Author: Mohan Srinivasan (mohan@fb.com)
*
* Based on DM-Cache:
* Copyright (C) International Business Machines Corp., 2006
* Author: Ming Zhao (mingzhao@ufl.edu)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; under version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <asm/atomic.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/blkdev.h>
#include <linux/bio.h>
#include <linux/slab.h>
#include <linux/hash.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/pagemap.h>
#include <linux/random.h>
#include <linux/hardirq.h>
#include <linux/sysctl.h>
#include <linux/version.h>
#include <linux/reboot.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
#include "dm.h"
#include "dm-io.h"
#include "dm-bio-list.h"
#include "kcopyd.h"
#else
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
#include "dm.h"
#endif
#include <linux/device-mapper.h>
#include <linux/bio.h>
#include <linux/dm-kcopyd.h>
#endif
#include "flashcache.h"
#include "flashcache_ioctl.h"
struct cache_c *cache_list_head = NULL;
struct work_struct _kcached_wq;
u_int64_t size_hist[33];
struct kmem_cache *_job_cache;
mempool_t *_job_pool;
struct kmem_cache *_pending_job_cache;
mempool_t *_pending_job_pool;
atomic_t nr_cache_jobs;
atomic_t nr_pending_jobs;
extern struct list_head *_pending_jobs;
extern struct list_head *_io_jobs;
extern struct list_head *_md_io_jobs;
extern struct list_head *_md_complete_jobs;
struct flashcache_control_s {
unsigned long synch_flags;
};
struct flashcache_control_s *flashcache_control;
/* Bit offsets for wait_on_bit_lock() */
#define FLASHCACHE_UPDATE_LIST 0
static int flashcache_notify_reboot(struct notifier_block *this,
unsigned long code, void *x);
static void flashcache_sync_for_remove(struct cache_c *dmc);
extern char *flashcache_sw_version;
static int
flashcache_wait_schedule(void *unused)
{
schedule();
return 0;
}
static int
flashcache_jobs_init(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
_job_cache = kmem_cache_create("kcached-jobs",
sizeof(struct kcached_job),
__alignof__(struct kcached_job),
0, NULL, NULL);
#else
_job_cache = kmem_cache_create("kcached-jobs",
sizeof(struct kcached_job),
__alignof__(struct kcached_job),
0, NULL);
#endif
if (!_job_cache)
return -ENOMEM;
_job_pool = mempool_create(MIN_JOBS, mempool_alloc_slab,
mempool_free_slab, _job_cache);
if (!_job_pool) {
kmem_cache_destroy(_job_cache);
return -ENOMEM;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
_pending_job_cache = kmem_cache_create("pending-jobs",
sizeof(struct pending_job),
__alignof__(struct pending_job),
0, NULL, NULL);
#else
_pending_job_cache = kmem_cache_create("pending-jobs",
sizeof(struct pending_job),
__alignof__(struct pending_job),
0, NULL);
#endif
if (!_pending_job_cache) {
mempool_destroy(_job_pool);
kmem_cache_destroy(_job_cache);
return -ENOMEM;
}
_pending_job_pool = mempool_create(MIN_JOBS, mempool_alloc_slab,
mempool_free_slab, _pending_job_cache);
if (!_pending_job_pool) {
kmem_cache_destroy(_pending_job_cache);
mempool_destroy(_job_pool);
kmem_cache_destroy(_job_cache);
return -ENOMEM;
}
return 0;
}
static void
flashcache_jobs_exit(void)
{
VERIFY(flashcache_pending_empty());
VERIFY(flashcache_io_empty());
VERIFY(flashcache_md_io_empty());
VERIFY(flashcache_md_complete_empty());
mempool_destroy(_job_pool);
kmem_cache_destroy(_job_cache);
_job_pool = NULL;
_job_cache = NULL;
mempool_destroy(_pending_job_pool);
kmem_cache_destroy(_pending_job_cache);
_pending_job_pool = NULL;
_pending_job_cache = NULL;
}
static int
flashcache_kcached_init(struct cache_c *dmc)
{
init_waitqueue_head(&dmc->destroyq);
atomic_set(&dmc->nr_jobs, 0);
atomic_set(&dmc->remove_in_prog, 0);
return 0;
}
/*
* Write out the metadata one sector at a time.
* Then dump out the superblock.
*/
static int
flashcache_writeback_md_store(struct cache_c *dmc)
{
struct flash_cacheblock *meta_data_cacheblock, *next_ptr;
struct flash_superblock *header;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
struct io_region where;
#else
struct dm_io_region where;
#endif
int i, j;
int num_valid = 0, num_dirty = 0;
int error;
int write_errors = 0;
int sectors_written = 0, sectors_expected = 0; /* debug */
int slots_written = 0; /* How many cache slots did we fill in this MD io block ? */
meta_data_cacheblock = (struct flash_cacheblock *)vmalloc(METADATA_IO_BLOCKSIZE);
if (!meta_data_cacheblock) {
DMERR("flashcache_writeback_md_store: Unable to allocate memory");
DMERR("flashcache_writeback_md_store: Could not write out cache metadata !");
return 1;
}
where.bdev = dmc->cache_dev->bdev;
where.sector = MD_SECTORS_PER_BLOCK(dmc);
slots_written = 0;
next_ptr = meta_data_cacheblock;
j = MD_SLOTS_PER_BLOCK(dmc);
for (i = 0 ; i < dmc->size ; i++) {
if (dmc->cache[i].cache_state & VALID)
num_valid++;
if (dmc->cache[i].cache_state & DIRTY)
num_dirty++;
next_ptr->dbn = dmc->cache[i].dbn;
#ifdef FLASHCACHE_DO_CHECKSUMS
next_ptr->checksum = dmc->cache[i].checksum;
#endif
next_ptr->cache_state = dmc->cache[i].cache_state &
(INVALID | VALID | DIRTY);
next_ptr++;
slots_written++;
j--;
if (j == 0) {
/*
* Filled the block, write and goto the next metadata block.
*/
if (slots_written == MD_SLOTS_PER_BLOCK(dmc) * METADATA_IO_NUM_BLOCKS(dmc)) {
/*
* Wrote out an entire metadata IO block, write the block to the ssd.
*/
where.count = (slots_written / MD_SLOTS_PER_BLOCK(dmc)) *
MD_SECTORS_PER_BLOCK(dmc);
slots_written = 0;
sectors_written += where.count; /* debug */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, meta_data_cacheblock);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, meta_data_cacheblock);
#endif
if (error) {
write_errors++;
DMERR("flashcache_writeback_md_store: Could not write out cache metadata block %lu error %d !",
where.sector, error);
}
where.sector += where.count; /* Advance offset */
}
/* Move next slot pointer into next block */
next_ptr = (struct flash_cacheblock *)
((caddr_t)meta_data_cacheblock + ((slots_written / MD_SLOTS_PER_BLOCK(dmc)) * MD_BLOCK_BYTES(dmc)));
j = MD_SLOTS_PER_BLOCK(dmc);
}
}
if (next_ptr != meta_data_cacheblock) {
/* Write the remaining last blocks out */
VERIFY(slots_written > 0);
where.count = (slots_written / MD_SLOTS_PER_BLOCK(dmc)) * MD_SECTORS_PER_BLOCK(dmc);
if (slots_written % MD_SLOTS_PER_BLOCK(dmc))
where.count += MD_SECTORS_PER_BLOCK(dmc);
sectors_written += where.count;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, meta_data_cacheblock);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, meta_data_cacheblock);
#endif
if (error) {
write_errors++;
DMERR("flashcache_writeback_md_store: Could not write out cache metadata block %lu error %d !",
where.sector, error);
}
}
/* Debug Tests */
sectors_expected = (dmc->size / MD_SLOTS_PER_BLOCK(dmc)) * MD_SECTORS_PER_BLOCK(dmc);
if (dmc->size % MD_SLOTS_PER_BLOCK(dmc))
sectors_expected += MD_SECTORS_PER_BLOCK(dmc);
if (sectors_expected != sectors_written) {
printk("flashcache_writeback_md_store" "Sector Mismatch ! sectors_expected=%d, sectors_written=%d\n",
sectors_expected, sectors_written);
panic("flashcache_writeback_md_store: sector mismatch\n");
}
vfree((void *)meta_data_cacheblock);
header = (struct flash_superblock *)vmalloc(MD_BLOCK_BYTES(dmc));
if (!header) {
DMERR("flashcache_writeback_md_store: Unable to allocate memory");
DMERR("flashcache_writeback_md_store: Could not write out cache metadata !");
return 1;
}
memset(header, 0, MD_BLOCK_BYTES(dmc));
/* Write the header out last */
if (write_errors == 0) {
if (num_dirty == 0)
header->cache_sb_state = CACHE_MD_STATE_CLEAN;
else
header->cache_sb_state = CACHE_MD_STATE_FASTCLEAN;
} else
header->cache_sb_state = CACHE_MD_STATE_UNSTABLE;
header->block_size = dmc->block_size;
header->md_block_size = dmc->md_block_size;
header->size = dmc->size;
header->assoc = dmc->assoc;
strncpy(header->disk_devname, dmc->disk_devname, DEV_PATHLEN);
strncpy(header->cache_devname, dmc->dm_vdevname, DEV_PATHLEN);
header->cache_devsize = to_sector(dmc->cache_dev->bdev->bd_inode->i_size);
header->disk_devsize = to_sector(dmc->disk_dev->bdev->bd_inode->i_size);
header->cache_version = dmc->on_ssd_version;
DPRINTK("Store metadata to disk: block size(%u), md block size(%u), cache size(%llu)" \
"associativity(%u)",
header->block_size, header->md_block_size, header->size,
header->assoc);
where.sector = 0;
where.count = dmc->md_block_size;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, header);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, header);
#endif
if (error) {
write_errors++;
DMERR("flashcache_writeback_md_store: Could not write out cache metadata superblock %lu error %d !",
where.sector, error);
}
vfree((void *)header);
if (write_errors == 0)
DMINFO("Cache metadata saved to disk");
else {
DMINFO("CRITICAL : There were %d errors in saving cache metadata saved to disk",
write_errors);
if (num_dirty)
DMINFO("CRITICAL : You have likely lost %d dirty blocks", num_dirty);
}
DMINFO("flashcache_writeback_md_store: valid blocks = %d dirty blocks = %d md_sectors = %d\n",
num_valid, num_dirty, dmc->md_blocks * MD_SECTORS_PER_BLOCK(dmc));
return 0;
}
static int
flashcache_writethrough_create(struct cache_c *dmc)
{
sector_t cache_size, dev_size;
sector_t order;
int i;
/*
* Convert size (in sectors) to blocks.
* Then round size (in blocks now) down to a multiple of associativity
*/
dmc->size /= dmc->block_size;
dmc->size = (dmc->size / dmc->assoc) * dmc->assoc;
/* Check cache size against device size */
dev_size = to_sector(dmc->cache_dev->bdev->bd_inode->i_size);
cache_size = dmc->size * dmc->block_size;
if (cache_size > dev_size) {
DMERR("Requested cache size exeeds the cache device's capacity" \
"(%lu>%lu)",
cache_size, dev_size);
return 1;
}
order = dmc->size * sizeof(struct cacheblock);
DMINFO("Allocate %luKB (%luB per) mem for %lu-entry cache" \
"(capacity:%luMB, associativity:%u, block size:%u " \
"sectors(%uKB))",
order >> 10, sizeof(struct cacheblock), dmc->size,
cache_size >> (20-SECTOR_SHIFT), dmc->assoc, dmc->block_size,
dmc->block_size >> (10-SECTOR_SHIFT));
dmc->cache = (struct cacheblock *)vmalloc(order);
if (!dmc->cache) {
DMERR("flashcache_writethrough_create: Unable to allocate cache md");
return 1;
}
/* Initialize the cache structs */
for (i = 0; i < dmc->size ; i++) {
dmc->cache[i].dbn = 0;
#ifdef FLASHCACHE_DO_CHECKSUMS
dmc->cache[i].checksum = 0;
#endif
dmc->cache[i].cache_state = INVALID;
dmc->cache[i].nr_queued = 0;
}
dmc->md_blocks = 0;
return 0;
}
static int
flashcache_writeback_create(struct cache_c *dmc, int force)
{
struct flash_cacheblock *meta_data_cacheblock, *next_ptr;
struct flash_superblock *header;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
struct io_region where;
#else
struct dm_io_region where;
#endif
int i, j, error;
sector_t cache_size, dev_size;
sector_t order;
int sectors_written = 0, sectors_expected = 0; /* debug */
int slots_written = 0; /* How many cache slots did we fill in this MD io block ? */
header = (struct flash_superblock *)vmalloc(MD_BLOCK_BYTES(dmc));
if (!header) {
DMERR("flashcache_writeback_create: Unable to allocate sector");
return 1;
}
where.bdev = dmc->cache_dev->bdev;
where.sector = 0;
where.count = dmc->md_block_size;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, READ, header);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, READ, header);
#endif
if (error) {
vfree((void *)header);
DMERR("flashcache_writeback_create: Could not read cache superblock %lu error %d !",
where.sector, error);
return 1;
}
if (!force &&
((header->cache_sb_state == CACHE_MD_STATE_DIRTY) ||
(header->cache_sb_state == CACHE_MD_STATE_CLEAN) ||
(header->cache_sb_state == CACHE_MD_STATE_FASTCLEAN))) {
vfree((void *)header);
DMERR("flashcache_writeback_create: Existing Cache Detected, use force to re-create");
return 1;
}
/* Compute the size of the metadata, including header.
Note dmc->size is in raw sectors */
dmc->md_blocks = INDEX_TO_MD_BLOCK(dmc, dmc->size / dmc->block_size) + 1 + 1;
dmc->size -= dmc->md_blocks * MD_SECTORS_PER_BLOCK(dmc); /* total sectors available for cache */
dmc->size /= dmc->block_size;
dmc->size = (dmc->size / dmc->assoc) * dmc->assoc;
/* Recompute since dmc->size was possibly trunc'ed down */
dmc->md_blocks = INDEX_TO_MD_BLOCK(dmc, dmc->size) + 1 + 1;
DMINFO("flashcache_writeback_create: md_blocks = %d, md_sectors = %d\n",
dmc->md_blocks, dmc->md_blocks * MD_SECTORS_PER_BLOCK(dmc));
dev_size = to_sector(dmc->cache_dev->bdev->bd_inode->i_size);
cache_size = dmc->md_blocks * MD_SECTORS_PER_BLOCK(dmc) + (dmc->size * dmc->block_size);
if (cache_size > dev_size) {
DMERR("Requested cache size exceeds the cache device's capacity" \
"(%lu>%lu)",
cache_size, dev_size);
vfree((void *)header);
return 1;
}
order = dmc->size * sizeof(struct cacheblock);
DMINFO("Allocate %luKB (%luB per) mem for %lu-entry cache" \
"(capacity:%luMB, associativity:%u, block size:%u " \
"sectors(%uKB))",
order >> 10, sizeof(struct cacheblock), dmc->size,
cache_size >> (20-SECTOR_SHIFT), dmc->assoc, dmc->block_size,
dmc->block_size >> (10-SECTOR_SHIFT));
dmc->cache = (struct cacheblock *)vmalloc(order);
if (!dmc->cache) {
vfree((void *)header);
DMERR("flashcache_writeback_create: Unable to allocate cache md");
return 1;
}
/* Initialize the cache structs */
for (i = 0; i < dmc->size ; i++) {
dmc->cache[i].dbn = 0;
#ifdef FLASHCACHE_DO_CHECKSUMS
dmc->cache[i].checksum = 0;
#endif
dmc->cache[i].cache_state = INVALID;
dmc->cache[i].nr_queued = 0;
}
meta_data_cacheblock = (struct flash_cacheblock *)vmalloc(METADATA_IO_BLOCKSIZE);
if (!meta_data_cacheblock) {
DMERR("flashcache_writeback_create: Unable to allocate memory");
DMERR("flashcache_writeback_create: Could not write out cache metadata !");
return 1;
}
where.sector = MD_SECTORS_PER_BLOCK(dmc);
slots_written = 0;
next_ptr = meta_data_cacheblock;
j = MD_SLOTS_PER_BLOCK(dmc);
for (i = 0 ; i < dmc->size ; i++) {
next_ptr->dbn = dmc->cache[i].dbn;
#ifdef FLASHCACHE_DO_CHECKSUMS
next_ptr->checksum = dmc->cache[i].checksum;
#endif
next_ptr->cache_state = dmc->cache[i].cache_state &
(INVALID | VALID | DIRTY);
next_ptr++;
slots_written++;
j--;
if (j == 0) {
/*
* Filled the block, write and goto the next metadata block.
*/
if (slots_written == MD_SLOTS_PER_BLOCK(dmc) * METADATA_IO_NUM_BLOCKS(dmc)) {
/*
* Wrote out an entire metadata IO block, write the block to the ssd.
*/
where.count = (slots_written / MD_SLOTS_PER_BLOCK(dmc)) * MD_SECTORS_PER_BLOCK(dmc);
slots_written = 0;
sectors_written += where.count; /* debug */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE,
meta_data_cacheblock);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE,
meta_data_cacheblock);
#endif
if (error) {
vfree((void *)header);
vfree((void *)meta_data_cacheblock);
vfree(dmc->cache);
DMERR("flashcache_writeback_create: Could not write cache metadata block %lu error %d !",
where.sector, error);
return 1;
}
where.sector += where.count; /* Advance offset */
}
/* Move next slot pointer into next metadata block */
next_ptr = (struct flash_cacheblock *)
((caddr_t)meta_data_cacheblock + ((slots_written / MD_SLOTS_PER_BLOCK(dmc)) * MD_BLOCK_BYTES(dmc)));
j = MD_SLOTS_PER_BLOCK(dmc);
}
}
if (next_ptr != meta_data_cacheblock) {
/* Write the remaining last blocks out */
VERIFY(slots_written > 0);
where.count = (slots_written / MD_SLOTS_PER_BLOCK(dmc)) * MD_SECTORS_PER_BLOCK(dmc);
if (slots_written % MD_SLOTS_PER_BLOCK(dmc))
where.count += MD_SECTORS_PER_BLOCK(dmc);
sectors_written += where.count;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, meta_data_cacheblock);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, meta_data_cacheblock);
#endif
if (error) {
vfree((void *)header);
vfree((void *)meta_data_cacheblock);
vfree(dmc->cache);
DMERR("flashcache_writeback_create: Could not write cache metadata block %lu error %d !",
where.sector, error);
return 1;
}
}
/* Debug Tests */
sectors_expected = (dmc->size / MD_SLOTS_PER_BLOCK(dmc)) * MD_SECTORS_PER_BLOCK(dmc);
if (dmc->size % MD_SLOTS_PER_BLOCK(dmc))
sectors_expected += MD_SECTORS_PER_BLOCK(dmc);
if (sectors_expected != sectors_written) {
printk("flashcache_writeback_create" "Sector Mismatch ! sectors_expected=%d, sectors_written=%d\n",
sectors_expected, sectors_written);
panic("flashcache_writeback_create: sector mismatch\n");
}
vfree((void *)meta_data_cacheblock);
/* Write the header */
header->cache_sb_state = CACHE_MD_STATE_DIRTY;
header->block_size = dmc->block_size;
header->md_block_size = dmc->md_block_size;
header->size = dmc->size;
header->assoc = dmc->assoc;
strncpy(header->disk_devname, dmc->disk_devname, DEV_PATHLEN);
strncpy(header->cache_devname, dmc->dm_vdevname, DEV_PATHLEN);
header->cache_devsize = to_sector(dmc->cache_dev->bdev->bd_inode->i_size);
header->disk_devsize = to_sector(dmc->disk_dev->bdev->bd_inode->i_size);
dmc->on_ssd_version = header->cache_version = FLASHCACHE_VERSION;
where.sector = 0;
where.count = dmc->md_block_size;
printk("flashcache-dbg: cachedev check - %s %s", header->cache_devname,
dmc->dm_vdevname);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, header);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, header);
#endif
if (error) {
vfree((void *)header);
vfree(dmc->cache);
DMERR("flashcache_writeback_create: Could not write cache superblock %lu error %d !",
where.sector, error);
return 1;
}
vfree((void *)header);
return 0;
}
static int
flashcache_writeback_load(struct cache_c *dmc)
{
struct flash_cacheblock *meta_data_cacheblock, *next_ptr;
struct flash_superblock *header;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
struct io_region where;
#else
struct dm_io_region where;
#endif
int i, j;
u_int64_t size, slots_read;
int clean_shutdown;
int dirty_loaded = 0;
sector_t order, data_size;
int num_valid = 0;
int error;
int sectors_read = 0, sectors_expected = 0; /* Debug */
/*
* We don't know what the preferred block size is, just read off
* the default md blocksize.
*/
header = (struct flash_superblock *)vmalloc(DEFAULT_MD_BLOCK_SIZE);
if (!header) {
DMERR("flashcache_writeback_load: Unable to allocate memory");
return 1;
}
where.bdev = dmc->cache_dev->bdev;
where.sector = 0;
where.count = DEFAULT_MD_BLOCK_SIZE;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, READ, header);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, READ, header);
#endif
if (error) {
vfree((void *)header);
DMERR("flashcache_writeback_load: Could not read cache superblock %lu error %d!",
where.sector, error);
return 1;
}
if (header->cache_version == 1) {
/* Backwards compatibility, md was 512 bytes always in V1.0 */
header->md_block_size = 1;
} else if (header->cache_version > FLASHCACHE_VERSION) {
vfree((void *)header);
DMERR("flashcache_writeback_load: Unknown version %d found in superblock!", header->cache_version);
return 1;
}
dmc->on_ssd_version = header->cache_version;
DPRINTK("Loaded cache conf: version(%d), block size(%u), md block size(%u), cache size(%llu), " \
"associativity(%u)",
header->cache_version, header->block_size, header->md_block_size, header->size,
header->assoc);
if (!((header->cache_sb_state == CACHE_MD_STATE_DIRTY) ||
(header->cache_sb_state == CACHE_MD_STATE_CLEAN) ||
(header->cache_sb_state == CACHE_MD_STATE_FASTCLEAN))) {
vfree((void *)header);
DMERR("flashcache_writeback_load: Corrupt Cache Superblock");
return 1;
}
if (header->cache_sb_state == CACHE_MD_STATE_DIRTY) {
DMINFO("Unclean Shutdown Detected");
printk(KERN_ALERT "Only DIRTY blocks exist in cache");
clean_shutdown = 0;
} else if (header->cache_sb_state == CACHE_MD_STATE_CLEAN) {
DMINFO("Slow (clean) Shutdown Detected");
printk(KERN_ALERT "Only CLEAN blocks exist in cache");
clean_shutdown = 1;
} else {
DMINFO("Fast (clean) Shutdown Detected");
printk(KERN_ALERT "Both CLEAN and DIRTY blocks exist in cache");
clean_shutdown = 1;
}
dmc->block_size = header->block_size;
dmc->md_block_size = header->md_block_size;
dmc->block_shift = ffs(dmc->block_size) - 1;
dmc->block_mask = dmc->block_size - 1;
dmc->size = header->size;
dmc->assoc = header->assoc;
dmc->assoc_shift = ffs(dmc->assoc) - 1;
dmc->md_blocks = INDEX_TO_MD_BLOCK(dmc, dmc->size) + 1 + 1;
DMINFO("flashcache_writeback_load: md_blocks = %d, md_sectors = %d, md_block_size = %d\n",
dmc->md_blocks, dmc->md_blocks * MD_SECTORS_PER_BLOCK(dmc), dmc->md_block_size);
data_size = dmc->size * dmc->block_size;
order = dmc->size * sizeof(struct cacheblock);
DMINFO("Allocate %luKB (%ldB per) mem for %lu-entry cache" \
"(capacity:%luMB, associativity:%u, block size:%u " \
"sectors(%uKB))",
order >> 10, sizeof(struct cacheblock), dmc->size,
(dmc->md_blocks * MD_SECTORS_PER_BLOCK(dmc) + data_size) >> (20-SECTOR_SHIFT),
dmc->assoc, dmc->block_size,
dmc->block_size >> (10-SECTOR_SHIFT));
dmc->cache = (struct cacheblock *)vmalloc(order);
if (!dmc->cache) {
DMERR("load_metadata: Unable to allocate memory");
vfree((void *)header);
return 1;
}
/* Read the metadata in large blocks and populate incore state */
meta_data_cacheblock = (struct flash_cacheblock *)vmalloc(METADATA_IO_BLOCKSIZE);
if (!meta_data_cacheblock) {
vfree((void *)header);
vfree(dmc->cache);
DMERR("flashcache_writeback_load: Unable to allocate memory");
return 1;
}
where.sector = MD_SECTORS_PER_BLOCK(dmc);
size = dmc->size;
i = 0;
while (size > 0) {
slots_read = min(size, (u_int64_t)(MD_SLOTS_PER_BLOCK(dmc) * METADATA_IO_NUM_BLOCKS(dmc)));
if (slots_read % MD_SLOTS_PER_BLOCK(dmc))
where.count = (1 + (slots_read / MD_SLOTS_PER_BLOCK(dmc))) * MD_SECTORS_PER_BLOCK(dmc);
else
where.count = (slots_read / MD_SLOTS_PER_BLOCK(dmc)) * MD_SECTORS_PER_BLOCK(dmc);
sectors_read += where.count; /* Debug */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, READ, meta_data_cacheblock);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, READ, meta_data_cacheblock);
#endif
if (error) {
vfree((void *)header);
vfree(dmc->cache);
vfree((void *)meta_data_cacheblock);
DMERR("flashcache_writeback_load: Could not read cache metadata block %lu error %d !",
where.sector, error);
return 1;
}
where.sector += where.count;
next_ptr = meta_data_cacheblock;
for (j = 0 ; j < slots_read ; j++) {
/*
* XXX - Now that we force each on-ssd metadata cache slot to be a ^2, where
* we are guaranteed that the slots will exactly fit within a sector (and
* a metadata block), we can simplify this logic. We don't need this next test.
*/
if ((j % MD_SLOTS_PER_BLOCK(dmc)) == 0) {
/* Move onto next block */
next_ptr = (struct flash_cacheblock *)
((caddr_t)meta_data_cacheblock + MD_BLOCK_BYTES(dmc) * (j / MD_SLOTS_PER_BLOCK(dmc)));
}
dmc->cache[i].nr_queued = 0;
/*
* If unclean shutdown, only the DIRTY blocks are loaded.
*/
if (clean_shutdown || (next_ptr->cache_state & DIRTY)) {
if (next_ptr->cache_state & DIRTY)
dirty_loaded++;
dmc->cache[i].cache_state = next_ptr->cache_state;
VERIFY((dmc->cache[i].cache_state & (VALID | INVALID))
!= (VALID | INVALID));
if (dmc->cache[i].cache_state & VALID)
num_valid++;
dmc->cache[i].dbn = next_ptr->dbn;
#ifdef FLASHCACHE_DO_CHECKSUMS
if (clean_shutdown)
dmc->cache[i].checksum = next_ptr->checksum;
else {
error = flashcache_read_compute_checksum(dmc, i, block);
if (error) {
vfree((void *)header);
vfree(dmc->cache);
vfree((void *)meta_data_cacheblock);
DMERR("flashcache_writeback_load: Could not read cache metadata block %lu error %d !",
dmc->cache[i].dbn, error);
return 1;
}
}
#endif
} else {
dmc->cache[i].cache_state = INVALID;
dmc->cache[i].dbn = 0;
#ifdef FLASHCACHE_DO_CHECKSUMS
dmc->cache[i].checksum = 0;
#endif
}
next_ptr++;
i++;
}
size -= slots_read;
}
/* Debug Tests */
sectors_expected = (dmc->size / MD_SLOTS_PER_BLOCK(dmc)) * MD_SECTORS_PER_BLOCK(dmc);
if (dmc->size % MD_SLOTS_PER_BLOCK(dmc))
sectors_expected += MD_SECTORS_PER_BLOCK(dmc);
if (sectors_expected != sectors_read) {
printk("flashcache_writeback_load" "Sector Mismatch ! sectors_expected=%d, sectors_read=%d\n",
sectors_expected, sectors_read);
panic("flashcache_writeback_load: sector mismatch\n");
}
vfree((void *)meta_data_cacheblock);
/*
* For writing the superblock out, use the preferred blocksize that
* we read from the superblock above.
*/
if (DEFAULT_MD_BLOCK_SIZE != dmc->md_block_size) {
vfree((void *)header);
header = (struct flash_superblock *)vmalloc(MD_BLOCK_BYTES(dmc));
if (!header) {
DMERR("flashcache_writeback_load: Unable to allocate memory");
return 1;
}
}
/* Before we finish loading, we need to dirty the superblock and
write it out */
header->size = dmc->size;
header->block_size = dmc->block_size;
header->md_block_size = dmc->md_block_size;
header->assoc = dmc->assoc;
header->cache_sb_state = CACHE_MD_STATE_DIRTY;
strncpy(header->disk_devname, dmc->disk_devname, DEV_PATHLEN);
strncpy(header->cache_devname, dmc->dm_vdevname, DEV_PATHLEN);
header->cache_devsize = to_sector(dmc->cache_dev->bdev->bd_inode->i_size);
header->disk_devsize = to_sector(dmc->disk_dev->bdev->bd_inode->i_size);
header->cache_version = dmc->on_ssd_version;
where.sector = 0;
where.count = dmc->md_block_size;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, header);
#else
error = flashcache_dm_io_sync_vm(dmc, &where, WRITE, header);
#endif
if (error) {
vfree((void *)header);
vfree(dmc->cache);
DMERR("flashcache_writeback_load: Could not write cache superblock %lu error %d !",
where.sector, error);
return 1;
}
vfree((void *)header);
DMINFO("flashcache_writeback_load: Cache metadata loaded from disk with %d valid %d DIRTY blocks",
num_valid, dirty_loaded);
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void
flashcache_clean_all_sets(void *data)
{
struct cache_c *dmc = (struct cache_c *)data;
#else
static void
flashcache_clean_all_sets(struct work_struct *work)
{
struct cache_c *dmc = container_of(work, struct cache_c,
delayed_clean.work);
#endif
int i;
for (i = 0 ; i < dmc->num_sets ; i++)
flashcache_clean_set(dmc, i);
}
static int inline
flashcache_get_dev(struct dm_target *ti, char *pth, struct dm_dev **dmd,
char *dmc_dname, sector_t tilen)
{
int rc;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34)
rc = dm_get_device(ti, pth,
dm_table_get_mode(ti->table), dmd);
#else
#if defined(RHEL_MAJOR) && RHEL_MAJOR == 6
rc = dm_get_device(ti, pth,
dm_table_get_mode(ti->table), dmd);
#else
rc = dm_get_device(ti, pth, 0, tilen,
dm_table_get_mode(ti->table), dmd);
#endif
#endif
if (!rc)
strncpy(dmc_dname, pth, DEV_PATHLEN);
return rc;
}
/*
* Construct a cache mapping.
* arg[0]: path to source device
* arg[1]: path to cache device
* arg[2]: md virtual device name
* arg[3]: cache mode (from flashcache.h)
* arg[4]: cache persistence (if set, cache conf is loaded from disk)
* Cache configuration parameters (if not set, default values are used.
* arg[5]: cache block size (in sectors)
* arg[6]: cache size (in blocks)
* arg[7]: cache associativity
* arg[8]: md block size (in sectors)
*/
int
flashcache_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
struct cache_c *dmc;
sector_t i, order;
int r = -EINVAL;
int persistence = 0;
if (argc < 3) {
ti->error = "flashcache: Need at least 3 arguments";
goto bad;
}
dmc = kzalloc(sizeof(*dmc), GFP_KERNEL);
if (dmc == NULL) {
ti->error = "flashcache: Failed to allocate cache context";
r = ENOMEM;
goto bad;
}
dmc->tgt = ti;
if (flashcache_get_dev(ti, argv[0], &dmc->disk_dev,
dmc->disk_devname, ti->len)) {
ti->error = "flashcache: Disk device lookup failed";
goto bad1;
}
if (flashcache_get_dev(ti, argv[1], &dmc->cache_dev,
dmc->cache_devname, 0)) {
ti->error = "flashcache: Cache device lookup failed";
goto bad2;
}
if (sscanf(argv[2], "%s", (char *)&dmc->dm_vdevname) != 1) {
ti->error = "flashcache: Virtual device name lookup failed";
goto bad3;
}
r = flashcache_kcached_init(dmc);
if (r) {
ti->error = "Failed to initialize kcached";
goto bad3;
}
if (sscanf(argv[3], "%u", &dmc->cache_mode) != 1) {
ti->error = "flashcache: sscanf failed, invalid cache mode";
r = -EINVAL;
goto bad3;
}
if (dmc->cache_mode < FLASHCACHE_WRITE_BACK ||
dmc->cache_mode > FLASHCACHE_WRITE_AROUND) {
DMERR("cache_mode = %d", dmc->cache_mode);
ti->error = "flashcache: Invalid cache mode";
r = -EINVAL;
goto bad3;
}
/*
* XXX - Persistence is totally ignored for write through and write around.
* Maybe this should really be moved to the end of the param list ?
*/
if (dmc->cache_mode == FLASHCACHE_WRITE_BACK) {
if (argc >= 5) {
if (sscanf(argv[4], "%u", &persistence) != 1) {
ti->error = "flashcache: sscanf failed, invalid cache persistence";
r = -EINVAL;
goto bad3;
}
if (persistence < CACHE_RELOAD || persistence > CACHE_FORCECREATE) {
DMERR("persistence = %d", persistence);
ti->error = "flashcache: Invalid cache persistence";
r = -EINVAL;
goto bad3;
}
}
if (persistence == CACHE_RELOAD) {
if (flashcache_writeback_load(dmc)) {
ti->error = "flashcache: Cache reload failed";
r = -EINVAL;
goto bad3;
}
goto init; /* Skip reading cache parameters from command line */
}
} else
persistence = CACHE_CREATE;
if (argc >= 6) {
if (sscanf(argv[5], "%u", &dmc->block_size) != 1) {
ti->error = "flashcache: Invalid block size";
r = -EINVAL;
goto bad3;
}
if (!dmc->block_size || (dmc->block_size & (dmc->block_size - 1))) {
ti->error = "flashcache: Invalid block size";
r = -EINVAL;
goto bad3;
}
}
if (!dmc->block_size)
dmc->block_size = DEFAULT_BLOCK_SIZE;
dmc->block_shift = ffs(dmc->block_size) - 1;
dmc->block_mask = dmc->block_size - 1;
/* dmc->size is specified in sectors here, and converted to blocks later */
if (argc >= 7) {
if (sscanf(argv[6], "%lu", &dmc->size) != 1) {
ti->error = "flashcache: Invalid cache size";
r = -EINVAL;
goto bad3;