This repository was archived by the owner on Jan 13, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathflashcache_main.c
More file actions
2461 lines (2345 loc) · 79.6 KB
/
Copy pathflashcache_main.c
File metadata and controls
2461 lines (2345 loc) · 79.6 KB
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_main.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/pid.h>
#include <linux/jhash.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,21)
#include <linux/device-mapper.h>
#include <linux/bio.h>
#endif
#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"
#ifndef DM_MAPIO_SUBMITTED
#define DM_MAPIO_SUBMITTED 0
#endif
/*
* TODO List :
* 1) Management of non cache pids : Needs improvement. Remove registration
* on process exits (with a pseudo filesstem'ish approach perhaps) ?
* 2) Breaking up the cache spinlock : Right now contention on the spinlock
* is not a problem. Might need change in future.
* 3) Use the standard linked list manipulation macros instead rolling our own.
* 4) Fix a security hole : A malicious process with 'ro' access to a file can
* potentially corrupt file data. This can be fixed by copying the data on a
* cache read miss.
*/
#define FLASHCACHE_SW_VERSION "flashcache-3.1.1"
char *flashcache_sw_version = FLASHCACHE_SW_VERSION;
static void flashcache_read_miss(struct cache_c *dmc, struct bio* bio,
int index);
static void flashcache_write(struct cache_c *dmc, struct bio* bio);
static int flashcache_inval_blocks(struct cache_c *dmc, struct bio *bio);
static void flashcache_dirty_writeback(struct cache_c *dmc, int index);
void flashcache_sync_blocks(struct cache_c *dmc);
static void flashcache_start_uncached_io(struct cache_c *dmc, struct bio *bio);
static void flashcache_setlocks_multiget(struct cache_c *dmc, struct bio *bio);
static void flashcache_setlocks_multidrop(struct cache_c *dmc, struct bio *bio);
extern struct work_struct _kcached_wq;
extern u_int64_t size_hist[];
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
extern struct dm_kcopyd_client *flashcache_kcp_client; /* Kcopyd client for writing back data */
#else
extern struct kcopyd_client *flashcache_kcp_client; /* Kcopyd client for writing back data */
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
extern struct dm_io_client *flashcache_io_client; /* Client memory pool*/
#endif
int dm_io_async_bvec_pl(unsigned int num_regions,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
struct dm_io_region *where,
#else
struct io_region *where,
#endif
int rw,
struct page_list *pl,
io_notify_fn fn,
void *context)
{
struct dm_io_request iorq;
iorq.bi_rw = rw;
iorq.mem.type = DM_IO_PAGE_LIST;
iorq.mem.ptr.pl = pl;
iorq.mem.offset = 0;
iorq.notify.fn = fn;
iorq.notify.context = context;
iorq.client = flashcache_io_client;
return dm_io(&iorq, num_regions, where, NULL);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
int dm_io_async_bvec(unsigned int num_regions,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
struct dm_io_region *where,
#else
struct io_region *where,
#endif
int rw,
struct bio *bio,
io_notify_fn fn,
void *context)
{
struct dm_io_request iorq;
iorq.bi_rw = rw;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
iorq.mem.type = DM_IO_BIO;
iorq.mem.ptr.bio = bio;
#else
iorq.mem.type = DM_IO_BVEC;
iorq.mem.ptr.bvec = bio->bi_io_vec + bio->bi_idx;
#endif
iorq.notify.fn = fn;
iorq.notify.context = context;
iorq.client = flashcache_io_client;
return dm_io(&iorq, num_regions, where, NULL);
}
#endif
/*
* A simple 2-hand clock like algorithm is used to identify dirty blocks
* that lie fallow in the cache and thus are candidates for cleaning.
* Note that we could have such fallow blocks in sets where the dirty blocks
* is under the configured threshold.
* The hands are spaced fallow_delay seconds apart (one sweep runs every
* fallow_delay seconds). The interval is configurable via a sysctl.
* Blocks are moved to DIRTY_FALLOW_1, if they are found to be in DIRTY_FALLOW_1
* for fallow_delay seconds or more, they are moved to DIRTY_FALLOW_1 | DIRTY_FALLOW_2,
* at which point they are eligible for cleaning. Of course any intervening use
* of the block within the interval turns off these 2 bits.
*
* Cleaning of these blocks happens from the flashcache_clean_set() function.
*/
void
flashcache_detect_fallow(struct cache_c *dmc, int index)
{
struct cacheblock *cacheblk = &dmc->cache[index];
if (dmc->cache_mode != FLASHCACHE_WRITE_BACK)
return;
if ((cacheblk->cache_state & DIRTY) &&
((cacheblk->cache_state & BLOCK_IO_INPROG) == 0)) {
if ((cacheblk->cache_state & DIRTY_FALLOW_1) == 0)
cacheblk->cache_state |= DIRTY_FALLOW_1;
else if ((cacheblk->cache_state & DIRTY_FALLOW_2) == 0) {
dmc->cache_sets[index / dmc->assoc].dirty_fallow++;
cacheblk->cache_state |= DIRTY_FALLOW_2;
}
}
}
void
flashcache_clear_fallow(struct cache_c *dmc, int index)
{
struct cacheblock *cacheblk = &dmc->cache[index];
int set = index / dmc->assoc;
if (dmc->cache_mode != FLASHCACHE_WRITE_BACK)
return;
if (cacheblk->cache_state & FALLOW_DOCLEAN) {
if (cacheblk->cache_state & DIRTY_FALLOW_2) {
VERIFY(dmc->cache_sets[set].dirty_fallow > 0);
dmc->cache_sets[set].dirty_fallow--;
}
cacheblk->cache_state &= ~FALLOW_DOCLEAN;
}
}
void
flashcache_io_callback(unsigned long error, void *context)
{
struct kcached_job *job = (struct kcached_job *) context;
struct cache_c *dmc = job->dmc;
struct bio *bio;
unsigned long flags;
int index = job->index;
struct cacheblock *cacheblk = &dmc->cache[index];
unsigned long disk_error = 0;
struct cache_set *cache_set = &dmc->cache_sets[index / dmc->assoc];
VERIFY(index != -1);
bio = job->bio;
VERIFY(bio != NULL);
if (unlikely(error)) {
error = -EIO;
DMERR("flashcache_io_callback: io error %ld block %lu action %d",
error, job->job_io_regions.disk.sector, job->action);
if (!dmc->bypass_cache && dmc->cache_mode != FLASHCACHE_WRITE_BACK) {
DMERR("flashcache_io_callback: switching %s to BYPASS mode",
dmc->cache_devname);
dmc->bypass_cache = 1;
}
}
job->error = error;
switch (job->action) {
case READDISK:
DPRINTK("flashcache_io_callback: READDISK %d",
index);
spin_lock_irqsave(&cache_set->set_spin_lock, flags);
if (unlikely(dmc->sysctl_error_inject & READDISK_ERROR)) {
job->error = error = -EIO;
dmc->sysctl_error_inject &= ~READDISK_ERROR;
}
VERIFY(cacheblk->cache_state & DISKREADINPROG);
spin_unlock_irqrestore(&cache_set->set_spin_lock, flags);
if (likely(error == 0)) {
/* Kick off the write to the cache */
job->action = READFILL;
push_io(job);
schedule_work(&_kcached_wq);
return;
} else {
disk_error = -EIO;
dmc->flashcache_errors.disk_read_errors++;
}
break;
case READCACHE:
DPRINTK("flashcache_io_callback: READCACHE %d",
index);
spin_lock_irqsave(&cache_set->set_spin_lock, flags);
if (unlikely(dmc->sysctl_error_inject & READCACHE_ERROR)) {
job->error = error = -EIO;
dmc->sysctl_error_inject &= ~READCACHE_ERROR;
}
VERIFY(cacheblk->cache_state & CACHEREADINPROG);
spin_unlock_irqrestore(&cache_set->set_spin_lock, flags);
if (unlikely(error))
dmc->flashcache_errors.ssd_read_errors++;
#ifdef FLASHCACHE_DO_CHECKSUMS
if (likely(error == 0)) {
if (flashcache_validate_checksum(job)) {
DMERR("flashcache_io_callback: Checksum mismatch at disk offset %lu",
job->job_io_regions.disk.sector);
error = -EIO;
}
}
#endif
break;
case READFILL:
DPRINTK("flashcache_io_callback: READFILL %d",
index);
spin_lock_irqsave(&cache_set->set_spin_lock, flags);
if (unlikely(dmc->sysctl_error_inject & READFILL_ERROR)) {
job->error = error = -EIO;
dmc->sysctl_error_inject &= ~READFILL_ERROR;
}
if (unlikely(error))
dmc->flashcache_errors.ssd_write_errors++;
VERIFY(cacheblk->cache_state & DISKREADINPROG);
spin_unlock_irqrestore(&cache_set->set_spin_lock, flags);
break;
case WRITECACHE:
DPRINTK("flashcache_io_callback: WRITECACHE %d",
index);
if (unlikely(dmc->sysctl_error_inject & WRITECACHE_ERROR)) {
job->error = error = -EIO;
dmc->sysctl_error_inject &= ~WRITECACHE_ERROR;
}
spin_lock_irqsave(&cache_set->set_spin_lock, flags);
VERIFY(cacheblk->cache_state & CACHEWRITEINPROG);
spin_unlock_irqrestore(&cache_set->set_spin_lock, flags);
if (likely(error == 0)) {
if (dmc->cache_mode == FLASHCACHE_WRITE_BACK) {
#ifdef FLASHCACHE_DO_CHECKSUMS
dmc->flashcache_stats.checksum_store++;
flashcache_store_checksum(job);
/*
* We need to update the metadata on a DIRTY->DIRTY as well
* since we save the checksums.
*/
flashcache_md_write(job);
return;
#else
/* Only do cache metadata update on a non-DIRTY->DIRTY transition */
if ((cacheblk->cache_state & DIRTY) == 0) {
flashcache_md_write(job);
return;
}
#endif
} else { /* cache_mode == WRITE_THROUGH */
/* Writs to both disk and cache completed */
VERIFY(dmc->cache_mode == FLASHCACHE_WRITE_THROUGH);
#ifdef FLASHCACHE_DO_CHECKSUMS
flashcache_store_checksum(job);
job->dmc->flashcache_stats.checksum_store++;
#endif
}
} else {
dmc->flashcache_errors.ssd_write_errors++;
if (dmc->cache_mode == FLASHCACHE_WRITE_THROUGH)
/*
* We don't know if the IO failed because of a ssd write
* error or a disk write error. Bump up both.
* XXX - TO DO. We could check the error bits and allow
* the IO to succeed as long as the disk write suceeded.
* and invalidate the cache block.
*/
disk_error = -EIO;
dmc->flashcache_errors.disk_write_errors++;
}
break;
}
/*
* If we get an error in write through || write around modes,
* we try the disk directly, after invalidating the cached block.
* see flashcache_do_pending_error().
* XXX - We can do the same for writeback as well. But that is more
* work. (a) we cannot fall back to disk when a ssd read of a dirty
* cacheblock fails (b) we'd need to handle ssd metadata write
* failures as well and fall back to disk in those cases as well.
*
* We track disk errors separately. If we get a disk error (in
* writethru or writearound modes) end the IO right here.
*/
if (likely(error == 0) ||
(dmc->cache_mode == FLASHCACHE_WRITE_BACK) ||
disk_error != 0) {
flashcache_bio_endio(bio, error, dmc, &job->io_start_time);
job->bio = NULL;
}
/*
* The INPROG flag is still set. We cannot turn that off until all the pending requests
* processed. We need to loop the pending requests back to a workqueue. We have the job,
* add it to the pending req queue.
*/
spin_lock_irqsave(&cache_set->set_spin_lock, flags);
if (unlikely(error || cacheblk->nr_queued > 0)) {
spin_unlock_irqrestore(&cache_set->set_spin_lock, flags);
push_pending(job);
schedule_work(&_kcached_wq);
} else {
cacheblk->cache_state &= ~BLOCK_IO_INPROG;
spin_unlock_irqrestore(&cache_set->set_spin_lock, flags);
flashcache_free_cache_job(job);
if (atomic_dec_and_test(&dmc->nr_jobs))
wake_up(&dmc->destroyq);
}
}
void
flashcache_free_pending_jobs(struct cache_c *dmc, struct cacheblock *cacheblk,
int error)
{
struct pending_job *pending_job, *freelist = NULL;
int index = cacheblk - &dmc->cache[0];
struct cache_set *cache_set = &dmc->cache_sets[index / dmc->assoc];
VERIFY(spin_is_locked(&cache_set->set_spin_lock));
freelist = flashcache_deq_pending(dmc, index);
while (freelist != NULL) {
pending_job = freelist;
freelist = pending_job->next;
VERIFY(cacheblk->nr_queued > 0);
cacheblk->nr_queued--;
flashcache_bio_endio(pending_job->bio, error, dmc, NULL);
flashcache_free_pending_job(pending_job);
}
VERIFY(cacheblk->nr_queued == 0);
}
/*
* Common error handling for everything.
* 1) If the block isn't dirty, invalidate it.
* 2) De-link all pending IOs that totally or partly overlap this block.
* 3) If it was an SSD error (bio != NULL), issue the invalidated block IO and other de-linked pending IOs uncached to disk.
* 4) Free the job.
*/
static void
flashcache_do_pending_error(struct kcached_job *job)
{
struct cache_c *dmc = job->dmc;
struct cacheblock *cacheblk = &dmc->cache[job->index];
struct bio *bio = job->bio;
int error = job->error;
struct pending_job *pjob_list = NULL, *pjob = NULL;
struct cache_set *cache_set = &dmc->cache_sets[job->index / dmc->assoc];
if (!dmc->bypass_cache) {
DMERR("flashcache_do_pending_error: error %d block %lu action %d",
job->error, job->job_io_regions.disk.sector, job->action);
}
spin_lock_irq(&cache_set->set_spin_lock);
VERIFY(cacheblk->cache_state & VALID);
/* Invalidate block if possible */
if ((cacheblk->cache_state & DIRTY) == 0) {
atomic_dec(&dmc->cached_blocks);
dmc->flashcache_stats.pending_inval++;
flashcache_hash_remove(dmc, job->index);
cacheblk->cache_state &= ~VALID;
cacheblk->cache_state |= INVALID;
} else
VERIFY(dmc->cache_mode == FLASHCACHE_WRITE_BACK);
cacheblk->cache_state &= ~(BLOCK_IO_INPROG);
if ((cacheblk->cache_state & DIRTY) == 0)
flashcache_invalid_insert(dmc, job->index);
/*
* In case of an error in writethrough or writearound modes, if there
* are pending jobs, de-link them from the cacheblock so we can issue disk
* IOs below.
*/
if (bio != NULL) {
VERIFY(dmc->cache_mode != FLASHCACHE_WRITE_BACK);
pjob_list = flashcache_deq_pending(dmc, cacheblk - &dmc->cache[0]);
for (pjob = pjob_list ; pjob != NULL ; pjob = pjob->next) {
VERIFY(cacheblk->nr_queued > 0);
cacheblk->nr_queued--;
}
VERIFY(cacheblk->nr_queued == 0);
} else
flashcache_free_pending_jobs(dmc, cacheblk, job->error);
spin_unlock_irq(&cache_set->set_spin_lock);
if (bio != NULL) {
/*
* Cache (read/write) error in write through or write around
* mode. Issue the IO directly to disk. We've already invalidated
* the cache block above.
*/
if (!dmc->bypass_cache) /* suppress massive console output */
DMERR("flashcache_do_pending_error: Re-launching errored IO"
"to disk, after io error %d block %lu",
error, bio->bi_sector);
flashcache_start_uncached_io(dmc, bio);
while (pjob_list != NULL) {
pjob = pjob_list;
pjob_list = pjob->next;
flashcache_start_uncached_io(dmc, pjob->bio);
flashcache_free_pending_job(pjob);
}
}
flashcache_free_cache_job(job);
if (atomic_dec_and_test(&dmc->nr_jobs))
wake_up(&dmc->destroyq);
}
static void
flashcache_do_pending_noerror(struct kcached_job *job)
{
struct cache_c *dmc = job->dmc;
int index = job->index;
struct pending_job *pending_job, *freelist;
int queued;
struct cacheblock *cacheblk = &dmc->cache[index];
struct cache_set *cache_set = &dmc->cache_sets[index / dmc->assoc];
spin_lock_irq(&cache_set->set_spin_lock);
if (cacheblk->cache_state & DIRTY) {
VERIFY(dmc->cache_mode == FLASHCACHE_WRITE_BACK);
cacheblk->cache_state &= ~(BLOCK_IO_INPROG);
cacheblk->cache_state |= DISKWRITEINPROG;
flashcache_clear_fallow(dmc, index);
spin_unlock_irq(&cache_set->set_spin_lock);
flashcache_dirty_writeback(dmc, index);
goto out;
}
DPRINTK("flashcache_do_pending: Index %d %lx",
index, cacheblk->cache_state);
VERIFY(cacheblk->cache_state & VALID);
atomic_dec(&dmc->cached_blocks);
dmc->flashcache_stats.pending_inval++;
flashcache_hash_remove(dmc, index);
cacheblk->cache_state &= ~VALID;
cacheblk->cache_state |= INVALID;
/*
* The block is in limbo right now. It is not VALID, but the IO_INPROG
* bits are set, so it cannot be reused. So it is safe to drop the
* cache set lock here.
*/
spin_unlock_irq(&cache_set->set_spin_lock);
freelist = flashcache_deq_pending(dmc, index);
while (freelist != NULL) {
pending_job = freelist;
freelist = pending_job->next;
flashcache_setlocks_multiget(dmc, pending_job->bio);
VERIFY(!(cacheblk->cache_state & DIRTY));
VERIFY(cacheblk->nr_queued > 0);
cacheblk->nr_queued--;
if (pending_job->action == INVALIDATE) {
DPRINTK("flashcache_do_pending: INVALIDATE %llu",
next_job->bio->bi_sector);
VERIFY(pending_job->bio != NULL);
queued = flashcache_inval_blocks(dmc, pending_job->bio);
if (queued) {
flashcache_setlocks_multidrop(dmc, pending_job->bio);
if (unlikely(queued < 0)) {
/*
* Memory allocation failure inside inval_blocks.
* Fail this io.
*/
flashcache_bio_endio(pending_job->bio, -EIO, dmc, NULL);
}
flashcache_free_pending_job(pending_job);
continue;
}
}
flashcache_setlocks_multidrop(dmc, pending_job->bio);
DPRINTK("flashcache_do_pending: Sending down IO %llu",
pending_job->bio->bi_sector);
/* Start uncached IO */
flashcache_start_uncached_io(dmc, pending_job->bio);
flashcache_free_pending_job(pending_job);
}
spin_lock_irq(&cache_set->set_spin_lock);
VERIFY(cacheblk->nr_queued == 0);
cacheblk->cache_state &= ~(BLOCK_IO_INPROG);
flashcache_invalid_insert(dmc, index);
spin_unlock_irq(&cache_set->set_spin_lock);
out:
flashcache_free_cache_job(job);
if (atomic_dec_and_test(&dmc->nr_jobs))
wake_up(&dmc->destroyq);
}
void
flashcache_do_pending(struct kcached_job *job)
{
if (job->error)
flashcache_do_pending_error(job);
else
flashcache_do_pending_noerror(job);
}
void
flashcache_do_io(struct kcached_job *job)
{
struct bio *bio = job->bio;
int r = 0;
VERIFY(job->action == READFILL);
VERIFY(job->action == READFILL);
#ifdef FLASHCACHE_DO_CHECKSUMS
flashcache_store_checksum(job);
job->dmc->flashcache_stats.checksum_store++;
#endif
/* Write to cache device */
job->dmc->flashcache_stats.ssd_writes++;
r = dm_io_async_bvec(1, &job->job_io_regions.cache, WRITE, bio,
flashcache_io_callback, job);
VERIFY(r == 0);
/* In our case, dm_io_async_bvec() must always return 0 */
}
/*
* Map a block from the source device to a block in the cache device.
*/
unsigned long
hash_block(struct cache_c *dmc, sector_t dbn)
{
unsigned long set_number, value;
int num_cache_sets = dmc->size >> dmc->assoc_shift;
/*
* Starting in Flashcache SSD Version 3 :
* We map a sequential cluster of disk_assoc blocks onto a given set.
* But each disk_assoc cluster can be randomly placed in any set.
* But if we are running on an older on-ssd cache, we preserve old
* behavior.
*/
if (dmc->on_ssd_version < 3 || dmc->disk_assoc == 0) {
value = (unsigned long)
(dbn >> (dmc->block_shift + dmc->assoc_shift));
} else {
/* Shift out the low disk_assoc bits */
value = (unsigned long) (dbn >> dmc->disk_assoc_shift);
/* Then place it in a random set */
value = jhash_1word(value, 0xbeef);
}
set_number = value % num_cache_sets;
DPRINTK("Hash: %llu(%lu)->%lu", dbn, value, set_number);
return set_number;
}
static void
find_valid_dbn(struct cache_c *dmc, sector_t dbn,
int start_index, int *index)
{
*index = flashcache_hash_lookup(dmc, start_index / dmc->assoc, dbn);
if (*index == -1)
return;
if (dmc->sysctl_reclaim_policy == FLASHCACHE_LRU &&
((dmc->cache[*index].cache_state & BLOCK_IO_INPROG) == 0))
flashcache_lru_accessed(dmc, *index);
/*
* If the block was DIRTY and earmarked for cleaning because it was old, make
* the block young again.
*/
flashcache_clear_fallow(dmc, *index);
}
static int
find_invalid_dbn(struct cache_c *dmc, int set)
{
int index = flashcache_invalid_get(dmc, set);
if (index != -1) {
if (dmc->sysctl_reclaim_policy == FLASHCACHE_LRU)
flashcache_lru_accessed(dmc, index);
VERIFY((dmc->cache[index].cache_state & FALLOW_DOCLEAN) == 0);
}
return index;
}
/* Search for a slot that we can reclaim */
static void
find_reclaim_dbn(struct cache_c *dmc, int start_index, int *index)
{
if (dmc->sysctl_reclaim_policy == FLASHCACHE_FIFO)
flashcache_reclaim_fifo_get_old_block(dmc, start_index, index);
else /* flashcache_reclaim_policy == FLASHCACHE_LRU */
flashcache_reclaim_lru_get_old_block(dmc, start_index, index);
}
/*
* dbn is the starting sector, io_size is the number of sectors.
*/
static int
flashcache_lookup(struct cache_c *dmc, struct bio *bio, int *index)
{
sector_t dbn = bio->bi_sector;
#if DMC_DEBUG
int io_size = to_sector(bio->bi_size);
#endif
unsigned long set_number = hash_block(dmc, dbn);
int invalid, oldest_clean = -1;
int start_index;
start_index = dmc->assoc * set_number;
DPRINTK("Cache lookup : dbn %llu(%lu), set = %d",
dbn, io_size, set_number);
find_valid_dbn(dmc, dbn, start_index, index);
if (*index >= 0) {
DPRINTK("Cache lookup HIT: Block %llu(%lu): VALID index %d",
dbn, io_size, *index);
/* We found the exact range of blocks we are looking for */
return VALID;
}
invalid = find_invalid_dbn(dmc, set_number);
if (invalid == -1) {
/* We didn't find an invalid entry, search for oldest valid entry */
find_reclaim_dbn(dmc, start_index, &oldest_clean);
}
/*
* Cache miss :
* We can't choose an entry marked INPROG, but choose the oldest
* INVALID or the oldest VALID entry.
*/
*index = start_index + dmc->assoc;
if (invalid != -1) {
DPRINTK("Cache lookup MISS (INVALID): dbn %llu(%lu), set = %d, index = %d, start_index = %d",
dbn, io_size, set_number, invalid, start_index);
*index = invalid;
} else if (oldest_clean != -1) {
DPRINTK("Cache lookup MISS (VALID): dbn %llu(%lu), set = %d, index = %d, start_index = %d",
dbn, io_size, set_number, oldest_clean, start_index);
*index = oldest_clean;
} else {
DPRINTK_LITE("Cache read lookup MISS (NOROOM): dbn %llu(%lu), set = %d",
dbn, io_size, set_number);
}
if (*index < (start_index + dmc->assoc))
return INVALID;
else {
dmc->flashcache_stats.noroom++;
return -1;
}
}
/*
* Cache Metadata Update functions
*/
void
flashcache_md_write_callback(unsigned long error, void *context)
{
struct kcached_job *job = (struct kcached_job *)context;
if (unlikely(error))
job->error = -EIO;
else
job->error = 0;
push_md_complete(job);
schedule_work(&_kcached_wq);
}
static int
flashcache_alloc_md_sector(struct kcached_job *job)
{
struct page *page = NULL;
struct cache_c *dmc = job->dmc;
unsigned long addr = 0;
if (likely((dmc->sysctl_error_inject & MD_ALLOC_SECTOR_ERROR) == 0)) {
/* Get physically consecutive pages */
addr = __get_free_pages(GFP_NOIO, get_order(MD_BLOCK_BYTES(job->dmc)));
if (addr)
page = virt_to_page(addr);
} else
dmc->sysctl_error_inject &= ~MD_ALLOC_SECTOR_ERROR;
if (unlikely(page == NULL)) {
job->dmc->flashcache_errors.memory_alloc_errors++;
return -ENOMEM;
} else {
job->pl_base[0].page = page;
job->pl_base[0].next = NULL;
job->md_block = (struct flash_cacheblock *)addr;
return 0;
}
}
static void
flashcache_free_md_sector(struct kcached_job *job)
{
if (job->pl_base[0].page != NULL)
__free_pages(job->pl_base[0].page, get_order(MD_BLOCK_BYTES(job->dmc)));
job->pl_base[0].page = NULL;
}
void
flashcache_md_write_kickoff(struct kcached_job *job)
{
struct cache_c *dmc = job->dmc;
struct flash_cacheblock *md_block;
int md_block_ix;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
struct io_region where;
#else
struct dm_io_region where;
#endif
int i;
struct cache_md_block_head *md_block_head;
struct kcached_job *orig_job = job;
struct cache_set *cache_set = &dmc->cache_sets[job->index / dmc->assoc];
if (flashcache_alloc_md_sector(job)) {
DMERR("flashcache: %d: Cache metadata write failed, cannot alloc page ! block %lu",
job->action, job->job_io_regions.disk.sector);
flashcache_md_write_callback(-EIO, job);
return;
}
/*
* Transfer whatever is on the pending queue to the md_io_inprog queue.
*/
md_block_head = &dmc->md_blocks_buf[INDEX_TO_MD_BLOCK(dmc, job->index)];
spin_lock_irq(&cache_set->set_spin_lock);
spin_lock(&md_block_head->md_block_lock);
md_block_head->md_io_inprog = md_block_head->queued_updates;
md_block_head->queued_updates = NULL;
md_block = job->md_block;
md_block_ix = INDEX_TO_MD_BLOCK(dmc, job->index) * MD_SLOTS_PER_BLOCK(dmc);
/* First copy out the entire md block */
for (i = 0 ;
i < MD_SLOTS_PER_BLOCK(dmc) && md_block_ix < dmc->size ;
i++, md_block_ix++) {
md_block[i].dbn = dmc->cache[md_block_ix].dbn;
#ifdef FLASHCACHE_DO_CHECKSUMS
md_block[i].checksum = dmc->cache[md_block_ix].checksum;
#endif
md_block[i].cache_state =
dmc->cache[md_block_ix].cache_state & (VALID | INVALID | DIRTY);
}
/* Then set/clear the DIRTY bit for the "current" index */
if (job->action == WRITECACHE) {
/* DIRTY the cache block */
md_block[INDEX_TO_MD_BLOCK_OFFSET(dmc, job->index)].cache_state =
(VALID | DIRTY);
} else { /* job->action == WRITEDISK* */
/* un-DIRTY the cache block */
md_block[INDEX_TO_MD_BLOCK_OFFSET(dmc, job->index)].cache_state = VALID;
}
for (job = md_block_head->md_io_inprog ;
job != NULL ;
job = job->next) {
dmc->flashcache_stats.md_write_batch++;
if (job->action == WRITECACHE) {
/* DIRTY the cache block */
md_block[INDEX_TO_MD_BLOCK_OFFSET(dmc, job->index)].cache_state =
(VALID | DIRTY);
} else { /* job->action == WRITEDISK* */
/* un-DIRTY the cache block */
md_block[INDEX_TO_MD_BLOCK_OFFSET(dmc, job->index)].cache_state = VALID;
}
}
spin_unlock(&md_block_head->md_block_lock);
spin_unlock_irq(&cache_set->set_spin_lock);
where.bdev = dmc->cache_dev->bdev;
where.count = MD_SECTORS_PER_BLOCK(dmc);
where.sector = (1 + INDEX_TO_MD_BLOCK(dmc, orig_job->index)) * MD_SECTORS_PER_BLOCK(dmc);
dmc->flashcache_stats.ssd_writes++;
dmc->flashcache_stats.md_ssd_writes++;
dm_io_async_bvec_pl(1, &where, WRITE,
&orig_job->pl_base[0],
flashcache_md_write_callback, orig_job);
}
void
flashcache_md_write_done(struct kcached_job *job)
{
struct cache_c *dmc = job->dmc;
struct cache_md_block_head *md_block_head;
int index, orig_index = job->index;
struct kcached_job *job_list;
int error = job->error;
struct kcached_job *next;
struct cacheblock *cacheblk;
int set;
struct cache_set *cache_set;
VERIFY(!in_interrupt());
VERIFY(job->action == WRITEDISK || job->action == WRITECACHE ||
job->action == WRITEDISK_SYNC);
flashcache_free_md_sector(job);
job->md_block = NULL;
md_block_head = &dmc->md_blocks_buf[INDEX_TO_MD_BLOCK(dmc, job->index)];
job_list = job;
spin_lock_irq(&md_block_head->md_block_lock);
job->next = md_block_head->md_io_inprog;
md_block_head->md_io_inprog = NULL;
spin_unlock_irq(&md_block_head->md_block_lock);
for (job = job_list ; job != NULL ; job = next) {
next = job->next;
job->error = error;
index = job->index;
set = index / dmc->assoc;
cache_set = &dmc->cache_sets[set];
cacheblk = &dmc->cache[index];
spin_lock_irq(&cache_set->set_spin_lock);
if (job->action == WRITECACHE) {
if (unlikely(dmc->sysctl_error_inject & WRITECACHE_MD_ERROR)) {
job->error = -EIO;
dmc->sysctl_error_inject &= ~WRITECACHE_MD_ERROR;
}
if (likely(job->error == 0)) {
if ((cacheblk->cache_state & DIRTY) == 0) {
cache_set->nr_dirty++;
atomic_inc(&dmc->nr_dirty);
}
dmc->flashcache_stats.md_write_dirty++;
cacheblk->cache_state |= DIRTY;
} else
dmc->flashcache_errors.ssd_write_errors++;
flashcache_bio_endio(job->bio, job->error, dmc, &job->io_start_time);
if (job->error || cacheblk->nr_queued > 0) {
if (job->error) {
DMERR("flashcache: WRITE: Cache metadata write failed ! error %d block %lu",
job->error, cacheblk->dbn);
}
spin_unlock_irq(&cache_set->set_spin_lock);
flashcache_do_pending(job);
} else {
cacheblk->cache_state &= ~BLOCK_IO_INPROG;
spin_unlock_irq(&cache_set->set_spin_lock);
flashcache_free_cache_job(job);
if (atomic_dec_and_test(&dmc->nr_jobs))
wake_up(&dmc->destroyq);
}
} else {
int action = job->action;
if (unlikely(dmc->sysctl_error_inject & WRITEDISK_MD_ERROR)) {
job->error = -EIO;
dmc->sysctl_error_inject &= ~WRITEDISK_MD_ERROR;
}
/*
* If we have an error on a WRITEDISK*, no choice but to preserve the
* dirty block in cache. Fail any IOs for this block that occurred while
* the block was being cleaned.
*/
if (likely(job->error == 0)) {
dmc->flashcache_stats.md_write_clean++;
cacheblk->cache_state &= ~DIRTY;
VERIFY(cache_set->nr_dirty > 0);
VERIFY(atomic_read(&dmc->nr_dirty) > 0);
cache_set->nr_dirty--;
atomic_dec(&dmc->nr_dirty);
} else
dmc->flashcache_errors.ssd_write_errors++;
VERIFY(cache_set->clean_inprog > 0);
VERIFY(atomic_read(&dmc->clean_inprog) > 0);
cache_set->clean_inprog--;
atomic_dec(&dmc->clean_inprog);
if (job->error || cacheblk->nr_queued > 0) {
if (job->error) {
DMERR("flashcache: CLEAN: Cache metadata write failed ! error %d block %lu",
job->error, cacheblk->dbn);
}
spin_unlock_irq(&cache_set->set_spin_lock);
flashcache_do_pending(job);
} else {
cacheblk->cache_state &= ~BLOCK_IO_INPROG;
spin_unlock_irq(&cache_set->set_spin_lock);
flashcache_free_cache_job(job);
if (atomic_dec_and_test(&dmc->nr_jobs))
wake_up(&dmc->destroyq);
}
/* Kick off more cleanings */
if (action == WRITEDISK)
flashcache_clean_set(dmc, set, 0);
else
flashcache_sync_blocks(dmc);
dmc->flashcache_stats.cleanings++;
if (action == WRITEDISK_SYNC)
flashcache_update_sync_progress(dmc);
}
}
cache_set = &dmc->cache_sets[orig_index / dmc->assoc];
spin_lock_irq(&cache_set->set_spin_lock);
spin_lock(&md_block_head->md_block_lock);
if (md_block_head->queued_updates != NULL) {
/* peel off the first job from the pending queue and kick that off */
job = md_block_head->queued_updates;
md_block_head->queued_updates = job->next;
spin_unlock(&md_block_head->md_block_lock);
job->next = NULL;
spin_unlock_irq(&cache_set->set_spin_lock);
VERIFY(job->action == WRITEDISK || job->action == WRITECACHE ||
job->action == WRITEDISK_SYNC);
flashcache_md_write_kickoff(job);
} else {
md_block_head->nr_in_prog = 0;
spin_unlock(&md_block_head->md_block_lock);
spin_unlock_irq(&cache_set->set_spin_lock);
}
}
/*
* Kick off a cache metadata update (called from workqueue).
* Cache metadata update IOs to a given metadata sector are serialized using the
* nr_in_prog bit in the md sector bufhead.
* If a metadata IO is already in progress, we queue up incoming metadata updates
* on the pending_jobs list of the md sector bufhead. When kicking off an IO, we
* cluster all these pending updates and do all of them as 1 flash write (that
* logic is in md_write_kickoff), where it switches out the entire pending_jobs
* list and does all of those updates as 1 ssd write.
*/
void
flashcache_md_write(struct kcached_job *job)
{
struct cache_c *dmc = job->dmc;
struct cache_md_block_head *md_block_head;
unsigned long flags;
VERIFY(job->action == WRITEDISK || job->action == WRITECACHE ||
job->action == WRITEDISK_SYNC);
md_block_head = &dmc->md_blocks_buf[INDEX_TO_MD_BLOCK(dmc, job->index)];
spin_lock_irqsave(&md_block_head->md_block_lock, flags);
/* If a write is in progress for this metadata sector, queue this update up */
if (md_block_head->nr_in_prog != 0) {
struct kcached_job **nodepp;
/* A MD update is already in progress, queue this one up for later */
nodepp = &md_block_head->queued_updates;
while (*nodepp != NULL)
nodepp = &((*nodepp)->next);
job->next = NULL;
*nodepp = job;
spin_unlock_irqrestore(&md_block_head->md_block_lock, flags);
} else {
md_block_head->nr_in_prog = 1;
spin_unlock_irqrestore(&md_block_head->md_block_lock, flags);
/*
* Always push to a worker thread. If the driver has
* a completion thread, we could end up deadlocking even
* if the context would be safe enough to write from.