-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdm-snap-mv.c
6828 lines (5292 loc) · 133 KB
/
dm-snap-mv.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
/*
* Copyright (C) 2010 NOVELL, Inc.
*
* Module Author: Cong Meng, NOVELL
*
* This file is released under the GPL.
*/
#include "dm-snap-mv.h"
#include <linux/version.h>
#include <linux/slab.h>
#include <linux/device-mapper.h>
#include <linux/buffer_head.h>
#include <linux/bit_spinlock.h>
#include <linux/kthread.h>
/*
*
* the relationship of the major data structs
* +--> dmd --> orig --> cow --> job_queue
* | *
* | |
* | +--> rollback_info --+
* | | |
* | +--> clean_info -----+--*> job
* | |
* +---------------- map_info -------+
*
* note:
* ---> : pointer
* --*> : embedded
*
*/
/*
* 4: size of [n, 0]
* 12: size of one more co-vc
* 6: size of co or vc
*
* if x/10 use up, count as full, which improves the performance by reduce moving elist
*/
#define DMS_ELIST_FULL_THRESHOLD (((4096 - 4 - 12) / 6) * 7/10)
#define BH_DMS_LOCK (BH_PrivateStart)
#define BH_DMS_WRITTING (BH_PrivateStart + 1)
#define BH_DMS_READING (BH_PrivateStart + 2)
#define DMS_LOCK_BH(bh, flags) \
local_irq_save(flags);\
bit_spin_lock(BH_DMS_LOCK, &(bh->b_state))
#define DMS_UNLOCK_BH(bh, flags) \
bit_spin_unlock(BH_DMS_LOCK, &(bh->b_state));\
local_irq_restore(flags)
// ex: MIDBITS32(AABBBCCCC, 3, 4) = BBB
#define MIDBITS32(x, len, right_len) (((uint32)(x) >> (right_len)) & ((1U << (len)) - 1))
#define RIGHTBITS32(x, len) ((uint32)(x) & ((1U << (len)) - 1))
#define RIGHTBITS64(x, len) ((uint64)(x) & ((1LLU << ((uint64)len)) - 1LLU))
// calc the Bitmap Page NR of the PLOT_NR
#define PLOT_2_BMP_PAGE_IDX(plot_nr) ((plot_nr) >> 14)
#define PLOT_2_BMP_IDX(plot_nr) (RIGHTBITS32(plot_nr, 14))
#define CHUNK_2_BMP_PAGE_IDX(chunk_nr) ((chunk_nr) >> 15)
#define CHUNK_2_BMP_IDX(chunk_nr) (RIGHTBITS32(chunk_nr, 15))
/*
* one index2 plot contains 1K elist-pointers
* one index1 plot contains 1K index2-pointers
* so, one index1 plot contains 1M(2^20) elist-pointers
*/
// convert chunk_dmd to index1 page index
#define CHUNK_2_INDEX1_PAGE_IDX(chunk_dmd) (chunk_dmd >> 20)
// convert chunk_dmd to subscript of index1 page
#define CHUNK_2_INDEX1(chunk_dmd) (MIDBITS32(chunk_dmd, 10, 10))
// convert chunk_dmd to subscript of index2 plot
#define CHUNK_2_INDEX2(chunk_dmd) (RIGHTBITS32(chunk_dmd, 10))
#define ERETRY (-(MAX_ERRNO + 1))
#define IS_RETRY(bh) ((bh) ==( typeof(bh))(ERETRY))
#define IS_EXX(bh) ((bh) >= (typeof(bh))(-MAX_ERRNO))
#define RET_RETRY(bh) if (IS_RETRY((bh))) return ERETRY
#define RET_RETRY_bh(bh) if (IS_RETRY((bh))) return ((dms_bh_t*)ERETRY)
#define CONT_RETRY(bh) if (IS_RETRY((bh))) continue
#define REMB_RETRY(ret, bh) if (IS_RETRY(bh)) (ret) = (bh);
#define RET_EXX(bh) if (IS_EXX((bh))) return (bh)
#define RET_RETRY_EXX(bh) RET_EXX((bh)); RET_RETRY((bh))
/*
* convert version from 1-Dimension to 2-Dimension
* local variable 'orig' must be defined
* sizeof(dms_vtree_disk_t) << 7 = 4096
*/
#define VTREE_DISK(ver_nr) orig->vtree.disk[(ver_nr) >> 7][RIGHTBITS32((ver_nr), 7)]
#define VTREE_BH(ver_nr) orig->vtree.bh[(ver_nr) >> 7]
// local variable 'orig' must be defined
#define INDEX1_DISK(chunk_dmd) \
orig->index1.disk[CHUNK_2_INDEX1_PAGE_IDX(chunk_dmd)][CHUNK_2_INDEX1(chunk_dmd)]
#define INDEX1_BH(chunk_dmd) \
orig->index1.bh[CHUNK_2_INDEX1_PAGE_IDX(chunk_dmd)]
#define INDEX1_DISK_BY_IDX(index1_idx) \
orig->index1.disk[index1_idx >> 10][RIGHTBITS32(index1_idx, 10)]
#define INDEX1_BH_BY_IDX(index1_idx) \
orig->index1.bh[index1_idx >> 10]
#define LOCK_ORIG() down(&orig->orig_lock)
#define UNLOCK_ORIG() up(&orig->orig_lock)
#define BIO_GROUP_HASH_LEN 1024
#define BIO_GROUP_HASH_FUN(x) ((x) & (BIO_GROUP_HASH_LEN - 1))
/*
* | b2 b1
* ---+-------
* 0 | R O
* 1 | W S
*/
#define DMS_MAP_TYPE_RO 0
#define DMS_MAP_TYPE_RS 1
#define DMS_MAP_TYPE_WO 2
#define DMS_MAP_TYPE_WS 3
#define DMS_JOB_TYPE_MAP 0
#define DMS_JOB_TYPE_CLEAN 1
#define DMS_JOB_TYPE_ROLLBACK 2
typedef struct buffer_head dms_bh_t;
typedef struct block_device dms_bdev_t;
typedef struct bio dms_bio_t;
struct list_head dms_origs_lh = LIST_HEAD_INIT(dms_origs_lh);
struct list_head dms_cows_lh = LIST_HEAD_INIT(dms_cows_lh);
struct list_head dms_dmds_lh = LIST_HEAD_INIT(dms_dmds_lh);
// protect the 3 lists above
struct semaphore dms_global_list_lock = __SEMAPHORE_INITIALIZER(dms_global_list_lock, 1);
#define LOCK_G_LISTS() down(&dms_global_list_lock)
#define UNLOCK_G_LISTS() up(&dms_global_list_lock)
typedef struct dms_list_s {
struct dms_list_s *next;
void *load;
} dms_list_t;
typedef struct {
struct list_head node;
dms_bh_t *bh;
dms_list_t *job_head;
} dms_defer_bh_t;
typedef struct
{
dms_bh_t *bh;
dms_misc_disk_t* disk;
} dms_misc_t;
typedef struct
{
dms_vtree_aux_t auxiliary[MAX_NR_OF_VERSIONS];
dms_bh_t *bh[VTREE_SLOT_SIZE];
dms_vtree_disk_t *disk[VTREE_SLOT_SIZE];
} dms_vtree_t;
typedef struct
{
dms_bh_t *bh[INDEX1_SLOT_SIZE];
plot_t *disk[INDEX1_SLOT_SIZE];
} dms_index1_t;
typedef struct
{
dms_bh_t *bh[PLOT_BITMAP_ZONE_SIZE];
void *disk[PLOT_BITMAP_ZONE_SIZE];
} dms_plot_bitmap_t;
typedef struct
{
dms_bh_t *bh[CHUNK_BITMAP_ZONE_SIZE];
void *disk[CHUNK_BITMAP_ZONE_SIZE];
} dms_chunk_bitmap_t;
// Chunk-Copy status:
#define DMS_CC_TO_READ 0
#define DMS_CC_TO_WRITE 1
#define DMS_CC_DONE 2
typedef struct
{
int status;
dms_bio_t *bio;
dms_bdev_t *bdev_s, *bdev_d;
chunk_t chunk_s, chunk_d;
} dms_chunk_copy_t;
typedef struct
{
int job_type;
struct list_head job_node;
// the count of bh IOs this job is waiting
atomic_t io_count;
// there are some places will cause job error in job context:
// BH async end, BIO end, xxx_expand, relocate_tag
int error;
// buffer head list, to be put_bh() while bio done
dms_list_t *read_bh_list;
} dms_job_t;
/*
* cow ID
* cid is embedded in the thread name: "dms_x_sda1". so it can be get by 'current'
*/
#define g_sid (current->comm[4] - '0')
dms_job_t* g_jobs[DMS_MAX_NR_COWS];
#define g_job g_jobs[g_sid]
typedef struct
{
atomic_t gogo;
// 1: signal to quit
// 2: the thread has quited
atomic_t quit;
// list head of job queue
//(list node: job->job_node)
struct list_head list;
spinlock_t list_lock;
struct task_struct *process_task;
} dms_job_thread_t;
typedef struct
{
dms_job_t job;
// the index of index1 pointing to the index2 plot to clean
uint32 index1_idx;
// the index2 plot nr to clean
// if non-zero, current index2 plot is undler cleaning
plot_t index2_plot_nr;
// the bh of the index2 plot
dms_bh_t *index2_plot_bh;
// point to the beginning of index2 plot
plot_t *p_index2_plot;
} dms_clean_info_t;
typedef struct
{
dms_job_t job;
// the dmsetup user-space command waiting for rollback finish
struct task_struct *process_task;
// the tag of snapshot to rollback
// non-zero: rollbacking is in progress
uint16 tag;
uint16 ver_nr;
// the index of index1 pointing to the index2 plot to rollback
uint32 index1_idx;
// the index2 plot nr to rollback
// if non-zero, current index2 plot is undler rollbacking
plot_t index2_plot_nr;
// the bh of the index2 plot
dms_bh_t *index2_plot_bh;
// the pointer of the page of the bh aboves
plot_t *p_index2_plot;
// for chunk copy
chunk_t chunk_alloc[1024];
dms_chunk_copy_t chunk_copy1[1024], chunk_copy2[1024];
uint32 plot_rollback_done[1024/32];
} dms_rollback_info_t;
typedef struct
{
// cow id. start from 0. one cow has one job thread.
int cow_id;
// cow reference count
atomic_t count;
// cow device
dms_bdev_t *bdev;
// node in dms_cows_lh
struct list_head cows_ln;
// job thread info
dms_job_thread_t job_thread;
// make (job->io_count dec + wake up job) and (job->io_count read) be mutual exclusive
spinlock_t wake_iocount_lock;
// cow device metadata
dms_misc_t misc;
dms_plot_bitmap_t plot_bitmap;
dms_chunk_bitmap_t chunk_bitmap;
chunk_t next_alloc_chunk;
// 2 ^ sector_shift sectors per chunk
//chunk_nr = sector_nr >> sector_shift
uint32 sector_shift;
struct list_head defer_bh_head;
} dms_cow_t;
typedef struct
{
int freeing;
uint32 slot_id;
dms_bdev_t *bdev;
// orig device size in sector
uint64 orig_size_in_sect;
// max chunk number of orig dev
uint32 max_chunk_nr;
// max index of index1 in index1 slot
uint32 max_index1_idx;
// node in dms_origs_lh
struct list_head origs_ln;
dms_vtree_t vtree;
dms_index1_t index1;
dms_cow_t *cow;
atomic_t count;
dms_clean_info_t clean_info;
dms_rollback_info_t rollback_info;
// make job and dmsetup-msg-commands be mutual exclusive
struct semaphore orig_lock;
} dms_orig_t;
/*
* one device-mapper device corresponds to one dms_dm_dev_t
*/
typedef struct
{
dms_orig_t *orig;
int tag;
// ver should be inited in dms_dmd_alloc(),
// might be updated in write_snap() and vtree_clean()
atomic_t ver;
char const *name;
// created_verstions_list_node of dms_dmds_lh
struct list_head dmds_ln;
struct dm_dev *dd_orig, *dd_cow;
// WO does NOT need bio group
struct list_head bio_group_hash [3][BIO_GROUP_HASH_LEN];
} dms_dm_dev_t;
typedef struct
{
dms_job_t job;
dms_dm_dev_t *dmd;
int map_type;
dms_list_t *dmd_bios;
uint32 bios_size; // in sector
struct list_head group_bio_node;
// used for chunk copy
dms_chunk_copy_t chunk_copy;
int done;
chunk_t chunk_dmd;
// WO always sets 0 here
// WS & RS set the refered chunk number here for map_and_submit_bios()
chunk_t chunk_cow;
/*
* dms_map_cow() temporarily save newly allocated chunk number here, and clean to 0 after set_exception() done
* map_job_done() should free the chunk if it's not zero
*/
chunk_t chunk_alloc;
int pre_set_excep;
atomic_t ws_dmd_bio_count;
spinlock_t ws_dms_bio_lock;
} dms_map_info_t;
typedef struct
{
// 'dmsetup create' commands
int create_orig, create_snap;
// 'dmsetup message' commands
int snap_orig, snap_snap, del_orig, del_snap, rollback_snap;
// positional arguments
int tag;
char *orig_dev_name, *cow_dev_name;
// options
char *memo, *orig_dev_id;
int format_cow, force_format_cow, bind, chunk_size, background;
// for the dms_construct()
struct dm_dev *dd_orig, *dd_cow;
} dms_parsed_args_t;
/*
* three are 2 kinds of job_lists
* 1. preemptive list
* 2. bh->private: bh read/write waiting list
*/
typedef struct dms_job_list_s {
dms_job_t *job;
struct dms_job_list_s *next;
} dms_job_list_t;
void dms_cow_mark_invalid(dms_cow_t *cow);
void dms_dbg_dump_elist_bh(dms_bh_t *elist_bh);
int dms_elist_find_co(dms_bh_t *elist_bh, chunk_t chunk_dmd);
struct kmem_cache *dms_list_kmem_cache;
dms_list_t* dms_list_alloc(void *load)
{
dms_list_t *dms_list;
dms_list = kmem_cache_alloc(dms_list_kmem_cache, GFP_ATOMIC);
dms_list->load = load;
return dms_list;
}
void dms_list_free(dms_list_t *dms_list)
{
kmem_cache_free(dms_list_kmem_cache, dms_list);
}
/*
* head -> ... => head -> node -> ...
*/
void dms_list_add(void **head, dms_list_t *node)
{
node->next = *head;
*head = node;
}
/*
* dequeue and free the 1st node of list @head
*
* return a load pointer if list is not empty
* return 0 if list is empty
*/
void* dms_list_fetch_load(dms_list_t **head)
{
dms_list_t *temp;
void *load = 0;
if (*head) {
temp = *head;
*head = temp->next;
load = temp->load;
dms_list_free(temp);
}
return load;
}
void dms_list_add_load(dms_list_t **head, void* load)
{
dms_list_t *node;
node = dms_list_alloc(load);
node->next = *head;
*head = node;
}
time_t dms_get_time(void)
{
struct timeval tv;
do_gettimeofday(&tv);
return tv.tv_sec;
}
void* dms_kmalloc(int size, char *name)
{
void *p;
uint32 count = 0;
while (1) {
count++;
p = kmalloc(size, GFP_ATOMIC);
if (p) break;
if ((count & 1023) == 0)
DMERR("tried to kmalloc %u-B %s %u times", size, name ? name : "", count);
}
return p;
}
struct page* dms_page_loop_alloc(void)
{
int count = 0;
struct page *p;
while (1) {
count++;
p = alloc_page(GFP_NOIO);
if (p) break;
if ((count & 1023) == 0)
DMERR("tried alloc a page %u times", count);
}
return p;
}
/*
* @x = 1 << r
*
* return r
*/
int dms_shift(uint32 x)
{
// fls(4)=3 (Find Last Set bit) 4 = 100
return fls(x) - 1;
}
/*
* chunk_nr = sector_nr >> sector_shift
* (1 << sector_shift) sects per chunk
*
* chunk_size: in page
* ex: sector_shift(2) -> 4, 1->3, 16->7
*/
int sector_shift(int chunk_size)
{
return dms_shift(chunk_size) + dms_shift(8);
}
void dms_bh_page_copy(dms_bh_t *bh_dst, dms_bh_t *bh_src)
{
memcpy(page_address(bh_dst->b_page), page_address(bh_src->b_page), PAGE_SIZE);
}
void dms_zero_page(struct page *page)
{
memset(page_address(page), 0, PAGE_SIZE);
}
int dms_get_device(struct dm_target *ti, char *dev_name, struct dm_dev **dd)
{
int r;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
r = dm_get_device(ti, dev_name, 0, ti->len, dm_table_get_mode(ti->table), dd);
#else
r = dm_get_device(ti, dev_name, dm_table_get_mode(ti->table), dd);
#endif
if (r)
DMERR("get %s error", dev_name);
return r;
}
/*
* get the size of @bdev in sectors
*/
sector_t dms_bdev_size_sect(dms_bdev_t *bdev)
{
return bdev->bd_part ? bdev->bd_part->nr_sects : bdev->bd_disk->part0.nr_sects;
}
/*
* similar to snprintf, except that it prints at the end of @buf
* @size: the maximal length(including '\0') of @buf
*/
void dms_snprintf(char *buf, int size, const char *fmt, ...)
{
int len;
va_list args;
len = strlen(buf);
size = size - len;
if (size > 0) {
va_start(args, fmt);
vsnprintf((buf + len), size, fmt, args);
va_end(args);
}
}
/*
* return a string pointing the name of @bdev, which is saved in static memory
*/
char* dms_bdev_name(dms_bdev_t *bdev)
{
static int idx = 0;
static char name[4][64] = {{0}, {0}, {0}, {0}};
idx = (idx + 1) & 3;
name[idx][0] = 0;
dms_snprintf(name[idx], 64, bdev->bd_disk->disk_name);
if (bdev->bd_part && bdev->bd_part->partno)
dms_snprintf(name[idx], 64, "%d", bdev->bd_part->partno);
return name[idx];
}
/*
* return a string pointing the major:minor of @bdev, which is saved in static memory
*/
char* dms_bdev_mm(dms_bdev_t *bdev)
{
static int idx = 0;
static char name[4][16] = {{0}, {0}, {0}, {0}};
idx = (idx + 1) & 3;
name[idx][0] = 0;
dms_snprintf(name[idx], 16, "%d:%d", MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev));
return name[idx];
}
char const* dms_dm_name(struct dm_target *ti)
{
char const* name;
struct mapped_device *md;
md = dm_table_get_md(ti->table);
name = dm_device_name(md);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
dm_put(md);
#endif
return name;
}
void dms_io_err(dms_bdev_t *bdev, char *str)
{
DMERR("%s IO error on %s", str, dms_bdev_name(bdev));
}
/*
* convert page idx to block number
*/
sector_t dms_page_2_block(dms_bdev_t *bdev, uint32 page_idx)
{
uint64 shift;
sector_t block;
shift = PAGE_CACHE_SHIFT - bdev->bd_inode->i_blkbits;
block = (sector_t)(page_idx) << shift;
return block;
}
inline void dms_write_bit(void *addr, uint32 nr, uint32 bit)
{
if (bit)
__set_bit(nr, addr);
else
__clear_bit(nr, addr);
}
inline uint32 dms_read_bit(void *addr, uint32 nr)
{
return test_bit(nr, addr) ? 1 : 0;
}
/*
* @addr:
* low <- -> high
*
* 0 1 2 3 4 5 6 7 (bit)
* [. .] [. .] [bit0 bit1] [. .]
* 0 1 2 3
* |
* @nr
*
* @bits = (bit0 << 1) | bit1
*/
inline void dms_write_2bits(void *addr, uint32 nr, uint32 bits)
{
uint32 bit0, bit1;
bit0 = bits >> 1;
bit1 = bits & 0x1;
dms_write_bit(addr, nr << 1, bit0);
dms_write_bit(addr, (nr << 1) | 1, bit1);
}
inline uint32 dms_read_2bits(void *addr, uint32 nr)
{
uint32 bits, bit0, bit1;
bit0 = dms_read_bit(addr, nr << 1);
bit1 = dms_read_bit(addr, (nr << 1) | 1);
bits = (bit0 << 1) | bit1;
return bits;
}
/*
* CBZS: Chunk_Bitmap_Zone_Size 256
* I1SS: Index1_Slot_Size 8
* MCS: Max_Chunk_Size 16
*
* MOPN: Max Orig Page Number
* MCPN: Max Cow Page Number
* CICS: Command Inputed Chunk Size
*
* MOCN: Max Orig Chunk Number
* MCCN: Max Cow Chunk Number
*
* CS: Chunk Size
*
* all above are in the unit of page
*
* 1 index1 page can index 2^20 orig chunks
* 1 chunk bitmap page has 2^15 bits
*
* restriction:
* MOCN = min{ uint32(-1), (I1SS << 20) - 1) } 2^23-1
* MCCN = min{ uint32(ERETRY - 1), (CBZS << 15) - 1 } 2^22-1
*
* (MOPN >> s) <= MOCN
* (MCPN >> s) <= MCCN
*
* result:
* (1 << min(s)) <= CS <= MCS
*
* calc the chunk size in page
*
* return a positive number if ok
* return 0 if error
*/
int dms_calc_chunk_size(dms_parsed_args_t *pargs)
{
uint32 cbzs = CHUNK_BITMAP_ZONE_SIZE;
uint32 i1ss = INDEX1_SLOT_SIZE;
uint32 mcs = MAX_CHUNK_SIZE;
uint32 cics = pargs->chunk_size;
uint64 mopn = dms_bdev_size_sect(pargs->dd_orig->bdev) >> 3LLU;
uint64 mcpn = dms_bdev_size_sect(pargs->dd_cow->bdev) >> 3LLU;
uint32 mocn = min(
(uint32)(ERETRY - 1),
(cbzs << 15) - 1
);
uint32 mccn = min(
(uint32)(-1),
(i1ss << 20) - 1
);
uint64 s = 0;
uint32 cs;
do {
if (((mcpn >> s) <= mccn) && ((mopn >> s) <= mocn))
break;
s++;
} while (1);
cs = 1 << s;
if (cs > mcs) {
DMERR("calc chunk size error. cs=%u mopn=%llu mcpn=%llu", cs, mopn, mcpn);
return 0;
}
if (cics) {
if ((cs <= cics) && (cics <= mcs))
return cics;
DMERR("invalid option -c %d. %u <= c <= %u", cics, cs, mcs);
return 0;
}
return cs;
}
int dms_dbg_scan_job_queue(dms_cow_t *cow)
{
int i;
unsigned long flags;
struct list_head *next, *curr;
dms_job_thread_t *jt = &cow->job_thread;
dms_job_t *job;
uint32 count[3];
uint32 total;
spin_lock_irqsave(&jt->list_lock, flags);
DMINFO("job thread: gogo=%u quit=%d", atomic_read(&jt->gogo), atomic_read(&jt->quit));
total = 0;
memset(count, 0, sizeof(count));
next = jt->list.next;
while (next != &jt->list) {
// 'curr' may be freed by run_job(), so save 'next' in advance
curr = next;
next = next->next;
job = list_entry(curr, dms_job_t, job_node);
total++;
count[job->job_type]++;
if (total <= 3 || job->job_type)
DMINFO("job: type=%d io-count=%u err=%d",
job->job_type, atomic_read(&job->io_count), job->error
);
}
for (i = 0; i < 3; i++)
DMINFO("job type:%d count:%u", i, count[i]);
spin_unlock_irqrestore(&jt->list_lock, flags);
return 0;
}
char* dms_str_args(unsigned int argc, char **argv)
{
int i;
static char buf[256];
buf[0] = 0;
for (i = 0; i < argc; i++)
dms_snprintf(buf, 256, "%s ", argv[i]);
return buf;
}
/*
* merge the memo who might Scatter in the last few argv
* @p: argv[p] is the 1st segment of the memo
*
* return a char pointer which point to the merged memo string
*/
char* dms_merge_memo(unsigned int argc, char **argv, int p)
{
int i;
for (i = p + 1; i < argc; i++) {
argv[i][-1] = ' ';
}
return argv[p];
}
/*
* return 1 if @str1 is equal to @str2
*/
int dms_strequ(char* str1, char* str2)
{
return !strcmp(str1, str2);
}
/*
* return 0 if ok
* return -EINVAL if error
*/
static int dms_parse_message_args(unsigned int argc, char **argv, dms_parsed_args_t *pargs)
{
int i;
if (!argc) goto bad;
memset(pargs, 0, sizeof(dms_parsed_args_t));
/*
* Snap Orig
* so [-m <memo>]
*/
if (dms_strequ(argv[0], "so")) {
if (argc < 1) goto bad;
pargs->snap_orig = 1;
for (i = 1; i < argc; i++) {
if (dms_strequ(argv[i], "-m")) {
pargs->memo = dms_merge_memo(argc, argv, i + 1);
break;
} else
goto bad;
}
goto ok;
}
/*