forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtdswap.c
1505 lines (1199 loc) · 33.3 KB
/
mtdswap.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
// SPDX-License-Identifier: GPL-2.0-only
/*
* Swap block device support for MTDs
* Turns an MTD device into a swap device with block wear leveling
*
* Copyright © 2007,2011 Nokia Corporation. All rights reserved.
*
* Authors: Jarkko Lavinen <[email protected]>
*
* Based on Richard Purdie's earlier implementation in 2007. Background
* support and lock-less operation written by Adrian Hunter.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/blktrans.h>
#include <linux/rbtree.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/swap.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/device.h>
#include <linux/math64.h>
#define MTDSWAP_PREFIX "mtdswap"
/*
* The number of free eraseblocks when GC should stop
*/
#define CLEAN_BLOCK_THRESHOLD 20
/*
* Number of free eraseblocks below which GC can also collect low frag
* blocks.
*/
#define LOW_FRAG_GC_THRESHOLD 5
/*
* Wear level cost amortization. We want to do wear leveling on the background
* without disturbing gc too much. This is made by defining max GC frequency.
* Frequency value 6 means 1/6 of the GC passes will pick an erase block based
* on the biggest wear difference rather than the biggest dirtiness.
*
* The lower freq2 should be chosen so that it makes sure the maximum erase
* difference will decrease even if a malicious application is deliberately
* trying to make erase differences large.
*/
#define MAX_ERASE_DIFF 4000
#define COLLECT_NONDIRTY_BASE MAX_ERASE_DIFF
#define COLLECT_NONDIRTY_FREQ1 6
#define COLLECT_NONDIRTY_FREQ2 4
#define PAGE_UNDEF UINT_MAX
#define BLOCK_UNDEF UINT_MAX
#define BLOCK_ERROR (UINT_MAX - 1)
#define BLOCK_MAX (UINT_MAX - 2)
#define EBLOCK_BAD (1 << 0)
#define EBLOCK_NOMAGIC (1 << 1)
#define EBLOCK_BITFLIP (1 << 2)
#define EBLOCK_FAILED (1 << 3)
#define EBLOCK_READERR (1 << 4)
#define EBLOCK_IDX_SHIFT 5
struct swap_eb {
struct rb_node rb;
struct rb_root *root;
unsigned int flags;
unsigned int active_count;
unsigned int erase_count;
unsigned int pad; /* speeds up pointer decrement */
};
#define MTDSWAP_ECNT_MIN(rbroot) (rb_entry(rb_first(rbroot), struct swap_eb, \
rb)->erase_count)
#define MTDSWAP_ECNT_MAX(rbroot) (rb_entry(rb_last(rbroot), struct swap_eb, \
rb)->erase_count)
struct mtdswap_tree {
struct rb_root root;
unsigned int count;
};
enum {
MTDSWAP_CLEAN,
MTDSWAP_USED,
MTDSWAP_LOWFRAG,
MTDSWAP_HIFRAG,
MTDSWAP_DIRTY,
MTDSWAP_BITFLIP,
MTDSWAP_FAILING,
MTDSWAP_TREE_CNT,
};
struct mtdswap_dev {
struct mtd_blktrans_dev *mbd_dev;
struct mtd_info *mtd;
struct device *dev;
unsigned int *page_data;
unsigned int *revmap;
unsigned int eblks;
unsigned int spare_eblks;
unsigned int pages_per_eblk;
unsigned int max_erase_count;
struct swap_eb *eb_data;
struct mtdswap_tree trees[MTDSWAP_TREE_CNT];
unsigned long long sect_read_count;
unsigned long long sect_write_count;
unsigned long long mtd_write_count;
unsigned long long mtd_read_count;
unsigned long long discard_count;
unsigned long long discard_page_count;
unsigned int curr_write_pos;
struct swap_eb *curr_write;
char *page_buf;
char *oob_buf;
};
struct mtdswap_oobdata {
__le16 magic;
__le32 count;
} __packed;
#define MTDSWAP_MAGIC_CLEAN 0x2095
#define MTDSWAP_MAGIC_DIRTY (MTDSWAP_MAGIC_CLEAN + 1)
#define MTDSWAP_TYPE_CLEAN 0
#define MTDSWAP_TYPE_DIRTY 1
#define MTDSWAP_OOBSIZE sizeof(struct mtdswap_oobdata)
#define MTDSWAP_ERASE_RETRIES 3 /* Before marking erase block bad */
#define MTDSWAP_IO_RETRIES 3
enum {
MTDSWAP_SCANNED_CLEAN,
MTDSWAP_SCANNED_DIRTY,
MTDSWAP_SCANNED_BITFLIP,
MTDSWAP_SCANNED_BAD,
};
/*
* In the worst case mtdswap_writesect() has allocated the last clean
* page from the current block and is then pre-empted by the GC
* thread. The thread can consume a full erase block when moving a
* block.
*/
#define MIN_SPARE_EBLOCKS 2
#define MIN_ERASE_BLOCKS (MIN_SPARE_EBLOCKS + 1)
#define TREE_ROOT(d, name) (&d->trees[MTDSWAP_ ## name].root)
#define TREE_EMPTY(d, name) (TREE_ROOT(d, name)->rb_node == NULL)
#define TREE_NONEMPTY(d, name) (!TREE_EMPTY(d, name))
#define TREE_COUNT(d, name) (d->trees[MTDSWAP_ ## name].count)
#define MTDSWAP_MBD_TO_MTDSWAP(dev) ((struct mtdswap_dev *)dev->priv)
static char partitions[128] = "";
module_param_string(partitions, partitions, sizeof(partitions), 0444);
MODULE_PARM_DESC(partitions, "MTD partition numbers to use as swap "
"partitions=\"1,3,5\"");
static unsigned int spare_eblocks = 10;
module_param(spare_eblocks, uint, 0444);
MODULE_PARM_DESC(spare_eblocks, "Percentage of spare erase blocks for "
"garbage collection (default 10%)");
static bool header; /* false */
module_param(header, bool, 0444);
MODULE_PARM_DESC(header,
"Include builtin swap header (default 0, without header)");
static int mtdswap_gc(struct mtdswap_dev *d, unsigned int background);
static loff_t mtdswap_eb_offset(struct mtdswap_dev *d, struct swap_eb *eb)
{
return (loff_t)(eb - d->eb_data) * d->mtd->erasesize;
}
static void mtdswap_eb_detach(struct mtdswap_dev *d, struct swap_eb *eb)
{
unsigned int oldidx;
struct mtdswap_tree *tp;
if (eb->root) {
tp = container_of(eb->root, struct mtdswap_tree, root);
oldidx = tp - &d->trees[0];
d->trees[oldidx].count--;
rb_erase(&eb->rb, eb->root);
}
}
static void __mtdswap_rb_add(struct rb_root *root, struct swap_eb *eb)
{
struct rb_node **p, *parent = NULL;
struct swap_eb *cur;
p = &root->rb_node;
while (*p) {
parent = *p;
cur = rb_entry(parent, struct swap_eb, rb);
if (eb->erase_count > cur->erase_count)
p = &(*p)->rb_right;
else
p = &(*p)->rb_left;
}
rb_link_node(&eb->rb, parent, p);
rb_insert_color(&eb->rb, root);
}
static void mtdswap_rb_add(struct mtdswap_dev *d, struct swap_eb *eb, int idx)
{
struct rb_root *root;
if (eb->root == &d->trees[idx].root)
return;
mtdswap_eb_detach(d, eb);
root = &d->trees[idx].root;
__mtdswap_rb_add(root, eb);
eb->root = root;
d->trees[idx].count++;
}
static struct rb_node *mtdswap_rb_index(struct rb_root *root, unsigned int idx)
{
struct rb_node *p;
unsigned int i;
p = rb_first(root);
i = 0;
while (i < idx && p) {
p = rb_next(p);
i++;
}
return p;
}
static int mtdswap_handle_badblock(struct mtdswap_dev *d, struct swap_eb *eb)
{
int ret;
loff_t offset;
d->spare_eblks--;
eb->flags |= EBLOCK_BAD;
mtdswap_eb_detach(d, eb);
eb->root = NULL;
/* badblocks not supported */
if (!mtd_can_have_bb(d->mtd))
return 1;
offset = mtdswap_eb_offset(d, eb);
dev_warn(d->dev, "Marking bad block at %08llx\n", offset);
ret = mtd_block_markbad(d->mtd, offset);
if (ret) {
dev_warn(d->dev, "Mark block bad failed for block at %08llx "
"error %d\n", offset, ret);
return ret;
}
return 1;
}
static int mtdswap_handle_write_error(struct mtdswap_dev *d, struct swap_eb *eb)
{
unsigned int marked = eb->flags & EBLOCK_FAILED;
struct swap_eb *curr_write = d->curr_write;
eb->flags |= EBLOCK_FAILED;
if (curr_write == eb) {
d->curr_write = NULL;
if (!marked && d->curr_write_pos != 0) {
mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
return 0;
}
}
return mtdswap_handle_badblock(d, eb);
}
static int mtdswap_read_oob(struct mtdswap_dev *d, loff_t from,
struct mtd_oob_ops *ops)
{
int ret = mtd_read_oob(d->mtd, from, ops);
if (mtd_is_bitflip(ret))
return ret;
if (ret) {
dev_warn(d->dev, "Read OOB failed %d for block at %08llx\n",
ret, from);
return ret;
}
if (ops->oobretlen < ops->ooblen) {
dev_warn(d->dev, "Read OOB return short read (%zd bytes not "
"%zd) for block at %08llx\n",
ops->oobretlen, ops->ooblen, from);
return -EIO;
}
return 0;
}
static int mtdswap_read_markers(struct mtdswap_dev *d, struct swap_eb *eb)
{
struct mtdswap_oobdata *data, *data2;
int ret;
loff_t offset;
struct mtd_oob_ops ops;
offset = mtdswap_eb_offset(d, eb);
/* Check first if the block is bad. */
if (mtd_can_have_bb(d->mtd) && mtd_block_isbad(d->mtd, offset))
return MTDSWAP_SCANNED_BAD;
ops.ooblen = 2 * d->mtd->oobavail;
ops.oobbuf = d->oob_buf;
ops.ooboffs = 0;
ops.datbuf = NULL;
ops.mode = MTD_OPS_AUTO_OOB;
ret = mtdswap_read_oob(d, offset, &ops);
if (ret && !mtd_is_bitflip(ret))
return ret;
data = (struct mtdswap_oobdata *)d->oob_buf;
data2 = (struct mtdswap_oobdata *)
(d->oob_buf + d->mtd->oobavail);
if (le16_to_cpu(data->magic) == MTDSWAP_MAGIC_CLEAN) {
eb->erase_count = le32_to_cpu(data->count);
if (mtd_is_bitflip(ret))
ret = MTDSWAP_SCANNED_BITFLIP;
else {
if (le16_to_cpu(data2->magic) == MTDSWAP_MAGIC_DIRTY)
ret = MTDSWAP_SCANNED_DIRTY;
else
ret = MTDSWAP_SCANNED_CLEAN;
}
} else {
eb->flags |= EBLOCK_NOMAGIC;
ret = MTDSWAP_SCANNED_DIRTY;
}
return ret;
}
static int mtdswap_write_marker(struct mtdswap_dev *d, struct swap_eb *eb,
u16 marker)
{
struct mtdswap_oobdata n;
int ret;
loff_t offset;
struct mtd_oob_ops ops;
ops.ooboffs = 0;
ops.oobbuf = (uint8_t *)&n;
ops.mode = MTD_OPS_AUTO_OOB;
ops.datbuf = NULL;
if (marker == MTDSWAP_TYPE_CLEAN) {
n.magic = cpu_to_le16(MTDSWAP_MAGIC_CLEAN);
n.count = cpu_to_le32(eb->erase_count);
ops.ooblen = MTDSWAP_OOBSIZE;
offset = mtdswap_eb_offset(d, eb);
} else {
n.magic = cpu_to_le16(MTDSWAP_MAGIC_DIRTY);
ops.ooblen = sizeof(n.magic);
offset = mtdswap_eb_offset(d, eb) + d->mtd->writesize;
}
ret = mtd_write_oob(d->mtd, offset, &ops);
if (ret) {
dev_warn(d->dev, "Write OOB failed for block at %08llx "
"error %d\n", offset, ret);
if (ret == -EIO || mtd_is_eccerr(ret))
mtdswap_handle_write_error(d, eb);
return ret;
}
if (ops.oobretlen != ops.ooblen) {
dev_warn(d->dev, "Short OOB write for block at %08llx: "
"%zd not %zd\n",
offset, ops.oobretlen, ops.ooblen);
return ret;
}
return 0;
}
/*
* Are there any erase blocks without MAGIC_CLEAN header, presumably
* because power was cut off after erase but before header write? We
* need to guestimate the erase count.
*/
static void mtdswap_check_counts(struct mtdswap_dev *d)
{
struct rb_root hist_root = RB_ROOT;
struct rb_node *medrb;
struct swap_eb *eb;
unsigned int i, cnt, median;
cnt = 0;
for (i = 0; i < d->eblks; i++) {
eb = d->eb_data + i;
if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_BAD | EBLOCK_READERR))
continue;
__mtdswap_rb_add(&hist_root, eb);
cnt++;
}
if (cnt == 0)
return;
medrb = mtdswap_rb_index(&hist_root, cnt / 2);
median = rb_entry(medrb, struct swap_eb, rb)->erase_count;
d->max_erase_count = MTDSWAP_ECNT_MAX(&hist_root);
for (i = 0; i < d->eblks; i++) {
eb = d->eb_data + i;
if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_READERR))
eb->erase_count = median;
if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_BAD | EBLOCK_READERR))
continue;
rb_erase(&eb->rb, &hist_root);
}
}
static void mtdswap_scan_eblks(struct mtdswap_dev *d)
{
int status;
unsigned int i, idx;
struct swap_eb *eb;
for (i = 0; i < d->eblks; i++) {
eb = d->eb_data + i;
status = mtdswap_read_markers(d, eb);
if (status < 0)
eb->flags |= EBLOCK_READERR;
else if (status == MTDSWAP_SCANNED_BAD) {
eb->flags |= EBLOCK_BAD;
continue;
}
switch (status) {
case MTDSWAP_SCANNED_CLEAN:
idx = MTDSWAP_CLEAN;
break;
case MTDSWAP_SCANNED_DIRTY:
case MTDSWAP_SCANNED_BITFLIP:
idx = MTDSWAP_DIRTY;
break;
default:
idx = MTDSWAP_FAILING;
}
eb->flags |= (idx << EBLOCK_IDX_SHIFT);
}
mtdswap_check_counts(d);
for (i = 0; i < d->eblks; i++) {
eb = d->eb_data + i;
if (eb->flags & EBLOCK_BAD)
continue;
idx = eb->flags >> EBLOCK_IDX_SHIFT;
mtdswap_rb_add(d, eb, idx);
}
}
/*
* Place eblk into a tree corresponding to its number of active blocks
* it contains.
*/
static void mtdswap_store_eb(struct mtdswap_dev *d, struct swap_eb *eb)
{
unsigned int weight = eb->active_count;
unsigned int maxweight = d->pages_per_eblk;
if (eb == d->curr_write)
return;
if (eb->flags & EBLOCK_BITFLIP)
mtdswap_rb_add(d, eb, MTDSWAP_BITFLIP);
else if (eb->flags & (EBLOCK_READERR | EBLOCK_FAILED))
mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
if (weight == maxweight)
mtdswap_rb_add(d, eb, MTDSWAP_USED);
else if (weight == 0)
mtdswap_rb_add(d, eb, MTDSWAP_DIRTY);
else if (weight > (maxweight/2))
mtdswap_rb_add(d, eb, MTDSWAP_LOWFRAG);
else
mtdswap_rb_add(d, eb, MTDSWAP_HIFRAG);
}
static int mtdswap_erase_block(struct mtdswap_dev *d, struct swap_eb *eb)
{
struct mtd_info *mtd = d->mtd;
struct erase_info erase;
unsigned int retries = 0;
int ret;
eb->erase_count++;
if (eb->erase_count > d->max_erase_count)
d->max_erase_count = eb->erase_count;
retry:
memset(&erase, 0, sizeof(struct erase_info));
erase.addr = mtdswap_eb_offset(d, eb);
erase.len = mtd->erasesize;
ret = mtd_erase(mtd, &erase);
if (ret) {
if (retries++ < MTDSWAP_ERASE_RETRIES) {
dev_warn(d->dev,
"erase of erase block %#llx on %s failed",
erase.addr, mtd->name);
yield();
goto retry;
}
dev_err(d->dev, "Cannot erase erase block %#llx on %s\n",
erase.addr, mtd->name);
mtdswap_handle_badblock(d, eb);
return -EIO;
}
return 0;
}
static int mtdswap_map_free_block(struct mtdswap_dev *d, unsigned int page,
unsigned int *block)
{
int ret;
struct swap_eb *old_eb = d->curr_write;
struct rb_root *clean_root;
struct swap_eb *eb;
if (old_eb == NULL || d->curr_write_pos >= d->pages_per_eblk) {
do {
if (TREE_EMPTY(d, CLEAN))
return -ENOSPC;
clean_root = TREE_ROOT(d, CLEAN);
eb = rb_entry(rb_first(clean_root), struct swap_eb, rb);
rb_erase(&eb->rb, clean_root);
eb->root = NULL;
TREE_COUNT(d, CLEAN)--;
ret = mtdswap_write_marker(d, eb, MTDSWAP_TYPE_DIRTY);
} while (ret == -EIO || mtd_is_eccerr(ret));
if (ret)
return ret;
d->curr_write_pos = 0;
d->curr_write = eb;
if (old_eb)
mtdswap_store_eb(d, old_eb);
}
*block = (d->curr_write - d->eb_data) * d->pages_per_eblk +
d->curr_write_pos;
d->curr_write->active_count++;
d->revmap[*block] = page;
d->curr_write_pos++;
return 0;
}
static unsigned int mtdswap_free_page_cnt(struct mtdswap_dev *d)
{
return TREE_COUNT(d, CLEAN) * d->pages_per_eblk +
d->pages_per_eblk - d->curr_write_pos;
}
static unsigned int mtdswap_enough_free_pages(struct mtdswap_dev *d)
{
return mtdswap_free_page_cnt(d) > d->pages_per_eblk;
}
static int mtdswap_write_block(struct mtdswap_dev *d, char *buf,
unsigned int page, unsigned int *bp, int gc_context)
{
struct mtd_info *mtd = d->mtd;
struct swap_eb *eb;
size_t retlen;
loff_t writepos;
int ret;
retry:
if (!gc_context)
while (!mtdswap_enough_free_pages(d))
if (mtdswap_gc(d, 0) > 0)
return -ENOSPC;
ret = mtdswap_map_free_block(d, page, bp);
eb = d->eb_data + (*bp / d->pages_per_eblk);
if (ret == -EIO || mtd_is_eccerr(ret)) {
d->curr_write = NULL;
eb->active_count--;
d->revmap[*bp] = PAGE_UNDEF;
goto retry;
}
if (ret < 0)
return ret;
writepos = (loff_t)*bp << PAGE_SHIFT;
ret = mtd_write(mtd, writepos, PAGE_SIZE, &retlen, buf);
if (ret == -EIO || mtd_is_eccerr(ret)) {
d->curr_write_pos--;
eb->active_count--;
d->revmap[*bp] = PAGE_UNDEF;
mtdswap_handle_write_error(d, eb);
goto retry;
}
if (ret < 0) {
dev_err(d->dev, "Write to MTD device failed: %d (%zd written)",
ret, retlen);
goto err;
}
if (retlen != PAGE_SIZE) {
dev_err(d->dev, "Short write to MTD device: %zd written",
retlen);
ret = -EIO;
goto err;
}
return ret;
err:
d->curr_write_pos--;
eb->active_count--;
d->revmap[*bp] = PAGE_UNDEF;
return ret;
}
static int mtdswap_move_block(struct mtdswap_dev *d, unsigned int oldblock,
unsigned int *newblock)
{
struct mtd_info *mtd = d->mtd;
struct swap_eb *eb, *oldeb;
int ret;
size_t retlen;
unsigned int page, retries;
loff_t readpos;
page = d->revmap[oldblock];
readpos = (loff_t) oldblock << PAGE_SHIFT;
retries = 0;
retry:
ret = mtd_read(mtd, readpos, PAGE_SIZE, &retlen, d->page_buf);
if (ret < 0 && !mtd_is_bitflip(ret)) {
oldeb = d->eb_data + oldblock / d->pages_per_eblk;
oldeb->flags |= EBLOCK_READERR;
dev_err(d->dev, "Read Error: %d (block %u)\n", ret,
oldblock);
retries++;
if (retries < MTDSWAP_IO_RETRIES)
goto retry;
goto read_error;
}
if (retlen != PAGE_SIZE) {
dev_err(d->dev, "Short read: %zd (block %u)\n", retlen,
oldblock);
ret = -EIO;
goto read_error;
}
ret = mtdswap_write_block(d, d->page_buf, page, newblock, 1);
if (ret < 0) {
d->page_data[page] = BLOCK_ERROR;
dev_err(d->dev, "Write error: %d\n", ret);
return ret;
}
eb = d->eb_data + *newblock / d->pages_per_eblk;
d->page_data[page] = *newblock;
d->revmap[oldblock] = PAGE_UNDEF;
eb = d->eb_data + oldblock / d->pages_per_eblk;
eb->active_count--;
return 0;
read_error:
d->page_data[page] = BLOCK_ERROR;
d->revmap[oldblock] = PAGE_UNDEF;
return ret;
}
static int mtdswap_gc_eblock(struct mtdswap_dev *d, struct swap_eb *eb)
{
unsigned int i, block, eblk_base, newblock;
int ret, errcode;
errcode = 0;
eblk_base = (eb - d->eb_data) * d->pages_per_eblk;
for (i = 0; i < d->pages_per_eblk; i++) {
if (d->spare_eblks < MIN_SPARE_EBLOCKS)
return -ENOSPC;
block = eblk_base + i;
if (d->revmap[block] == PAGE_UNDEF)
continue;
ret = mtdswap_move_block(d, block, &newblock);
if (ret < 0 && !errcode)
errcode = ret;
}
return errcode;
}
static int __mtdswap_choose_gc_tree(struct mtdswap_dev *d)
{
int idx, stopat;
if (TREE_COUNT(d, CLEAN) < LOW_FRAG_GC_THRESHOLD)
stopat = MTDSWAP_LOWFRAG;
else
stopat = MTDSWAP_HIFRAG;
for (idx = MTDSWAP_BITFLIP; idx >= stopat; idx--)
if (d->trees[idx].root.rb_node != NULL)
return idx;
return -1;
}
static int mtdswap_wlfreq(unsigned int maxdiff)
{
unsigned int h, x, y, dist, base;
/*
* Calculate linear ramp down from f1 to f2 when maxdiff goes from
* MAX_ERASE_DIFF to MAX_ERASE_DIFF + COLLECT_NONDIRTY_BASE. Similar
* to triangle with height f1 - f1 and width COLLECT_NONDIRTY_BASE.
*/
dist = maxdiff - MAX_ERASE_DIFF;
if (dist > COLLECT_NONDIRTY_BASE)
dist = COLLECT_NONDIRTY_BASE;
/*
* Modelling the slop as right angular triangle with base
* COLLECT_NONDIRTY_BASE and height freq1 - freq2. The ratio y/x is
* equal to the ratio h/base.
*/
h = COLLECT_NONDIRTY_FREQ1 - COLLECT_NONDIRTY_FREQ2;
base = COLLECT_NONDIRTY_BASE;
x = dist - base;
y = (x * h + base / 2) / base;
return COLLECT_NONDIRTY_FREQ2 + y;
}
static int mtdswap_choose_wl_tree(struct mtdswap_dev *d)
{
static unsigned int pick_cnt;
unsigned int i, idx = -1, wear, max;
struct rb_root *root;
max = 0;
for (i = 0; i <= MTDSWAP_DIRTY; i++) {
root = &d->trees[i].root;
if (root->rb_node == NULL)
continue;
wear = d->max_erase_count - MTDSWAP_ECNT_MIN(root);
if (wear > max) {
max = wear;
idx = i;
}
}
if (max > MAX_ERASE_DIFF && pick_cnt >= mtdswap_wlfreq(max) - 1) {
pick_cnt = 0;
return idx;
}
pick_cnt++;
return -1;
}
static int mtdswap_choose_gc_tree(struct mtdswap_dev *d,
unsigned int background)
{
int idx;
if (TREE_NONEMPTY(d, FAILING) &&
(background || (TREE_EMPTY(d, CLEAN) && TREE_EMPTY(d, DIRTY))))
return MTDSWAP_FAILING;
idx = mtdswap_choose_wl_tree(d);
if (idx >= MTDSWAP_CLEAN)
return idx;
return __mtdswap_choose_gc_tree(d);
}
static struct swap_eb *mtdswap_pick_gc_eblk(struct mtdswap_dev *d,
unsigned int background)
{
struct rb_root *rp = NULL;
struct swap_eb *eb = NULL;
int idx;
if (background && TREE_COUNT(d, CLEAN) > CLEAN_BLOCK_THRESHOLD &&
TREE_EMPTY(d, DIRTY) && TREE_EMPTY(d, FAILING))
return NULL;
idx = mtdswap_choose_gc_tree(d, background);
if (idx < 0)
return NULL;
rp = &d->trees[idx].root;
eb = rb_entry(rb_first(rp), struct swap_eb, rb);
rb_erase(&eb->rb, rp);
eb->root = NULL;
d->trees[idx].count--;
return eb;
}
static unsigned int mtdswap_test_patt(unsigned int i)
{
return i % 2 ? 0x55555555 : 0xAAAAAAAA;
}
static unsigned int mtdswap_eblk_passes(struct mtdswap_dev *d,
struct swap_eb *eb)
{
struct mtd_info *mtd = d->mtd;
unsigned int test, i, j, patt, mtd_pages;
loff_t base, pos;
unsigned int *p1 = (unsigned int *)d->page_buf;
unsigned char *p2 = (unsigned char *)d->oob_buf;
struct mtd_oob_ops ops;
int ret;
ops.mode = MTD_OPS_AUTO_OOB;
ops.len = mtd->writesize;
ops.ooblen = mtd->oobavail;
ops.ooboffs = 0;
ops.datbuf = d->page_buf;
ops.oobbuf = d->oob_buf;
base = mtdswap_eb_offset(d, eb);
mtd_pages = d->pages_per_eblk * PAGE_SIZE / mtd->writesize;
for (test = 0; test < 2; test++) {
pos = base;
for (i = 0; i < mtd_pages; i++) {
patt = mtdswap_test_patt(test + i);
memset(d->page_buf, patt, mtd->writesize);
memset(d->oob_buf, patt, mtd->oobavail);
ret = mtd_write_oob(mtd, pos, &ops);
if (ret)
goto error;
pos += mtd->writesize;
}
pos = base;
for (i = 0; i < mtd_pages; i++) {
ret = mtd_read_oob(mtd, pos, &ops);
if (ret)
goto error;
patt = mtdswap_test_patt(test + i);
for (j = 0; j < mtd->writesize/sizeof(int); j++)
if (p1[j] != patt)
goto error;
for (j = 0; j < mtd->oobavail; j++)
if (p2[j] != (unsigned char)patt)
goto error;
pos += mtd->writesize;
}
ret = mtdswap_erase_block(d, eb);
if (ret)
goto error;
}
eb->flags &= ~EBLOCK_READERR;
return 1;
error:
mtdswap_handle_badblock(d, eb);
return 0;
}
static int mtdswap_gc(struct mtdswap_dev *d, unsigned int background)
{
struct swap_eb *eb;
int ret;
if (d->spare_eblks < MIN_SPARE_EBLOCKS)
return 1;
eb = mtdswap_pick_gc_eblk(d, background);
if (!eb)
return 1;
ret = mtdswap_gc_eblock(d, eb);
if (ret == -ENOSPC)
return 1;
if (eb->flags & EBLOCK_FAILED) {
mtdswap_handle_badblock(d, eb);
return 0;
}
eb->flags &= ~EBLOCK_BITFLIP;
ret = mtdswap_erase_block(d, eb);
if ((eb->flags & EBLOCK_READERR) &&
(ret || !mtdswap_eblk_passes(d, eb)))
return 0;
if (ret == 0)
ret = mtdswap_write_marker(d, eb, MTDSWAP_TYPE_CLEAN);
if (ret == 0)
mtdswap_rb_add(d, eb, MTDSWAP_CLEAN);
else if (ret != -EIO && !mtd_is_eccerr(ret))
mtdswap_rb_add(d, eb, MTDSWAP_DIRTY);
return 0;
}
static void mtdswap_background(struct mtd_blktrans_dev *dev)
{
struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
int ret;
while (1) {
ret = mtdswap_gc(d, 1);
if (ret || mtd_blktrans_cease_background(dev))
return;
}
}
static void mtdswap_cleanup(struct mtdswap_dev *d)
{
vfree(d->eb_data);
vfree(d->revmap);
vfree(d->page_data);
kfree(d->oob_buf);
kfree(d->page_buf);
}
static int mtdswap_flush(struct mtd_blktrans_dev *dev)
{
struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);