-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.c
More file actions
900 lines (785 loc) · 22.4 KB
/
Copy pathcommit.c
File metadata and controls
900 lines (785 loc) · 22.4 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
/*
* Copyright (c) 2008, Daniel Phillips
* Copyright (c) 2008, OGAWA Hirofumi
*/
#include "tux3.h"
#ifdef __KERNEL__
#include <linux/kthread.h>
#include <linux/freezer.h>
#endif
#ifndef trace
#define trace trace_on
#endif
static void __delta_transition(struct sb *sb, struct delta_ref *delta_ref);
static void schedule_flush_delta(struct sb *sb);
/*
* Need frontend modification of backend buffers. (modification
* after latest delta commit and before unify).
*
* E.g. frontend modified backend buffers, stage_delta() of when
* unify is called.
*/
#define ALLOW_FRONTEND_MODIFY
/* Initialize the lock and list */
static void init_sb(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
int i;
/* Initialize sb */
for (i = 0; i < ARRAY_SIZE(sb->delta_refs); i++)
atomic_set(&sb->delta_refs[0].refcount, 0);
#if TUX3_FLUSHER == TUX3_FLUSHER_SYNC
init_rwsem(&sb->delta_lock);
#endif
init_waitqueue_head(&sb->delta_event_wq);
INIT_LIST_HEAD(&sb->orphan_add);
INIT_LIST_HEAD(&sb->orphan_del);
stash_init(&sb->defree);
stash_init(&sb->deunify);
INIT_LIST_HEAD(&sb->unify_buffers);
INIT_LIST_HEAD(&sb->alloc_inodes);
spin_lock_init(&sb->forked_buffers_lock);
init_link_circular(&sb->forked_buffers);
spin_lock_init(&sb->dirty_inodes_lock);
/* Initialize sb_delta_dirty */
for (i = 0; i < ARRAY_SIZE(sb->s_ddc); i++)
INIT_LIST_HEAD(&sb->s_ddc[i].dirty_inodes);
}
static void setup_roots(struct sb *sb, struct disksuper *super)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
u64 iroot_val = be64_to_cpu(super->iroot);
u64 oroot_val = be64_to_cpu(sb->super.oroot);
init_btree(itree_btree(sb), sb, unpack_root(iroot_val), &itree_ops);
init_btree(otree_btree(sb), sb, unpack_root(oroot_val), &otree_ops);
}
static loff_t calc_maxbytes(loff_t blocksize)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
return min_t(loff_t, blocksize << MAX_BLOCKS_BITS, MAX_LFS_FILESIZE);
}
/* Setup sb by on-disk super block */
static void __setup_sb(struct sb *sb, struct disksuper *super)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
sb->next_delta = TUX3_INIT_DELTA;
sb->unify = TUX3_INIT_DELTA;
sb->marshal_delta = TUX3_INIT_DELTA - 1;
sb->committed_delta = TUX3_INIT_DELTA - 1;
/* Setup initial delta_ref */
__delta_transition(sb, &sb->delta_refs[0]);
sb->blockbits = be16_to_cpu(super->blockbits);
sb->volblocks = be64_to_cpu(super->volblocks);
sb->version = 0; /* FIXME: not yet implemented */
sb->blocksize = 1 << sb->blockbits;
sb->blockmask = (1 << sb->blockbits) - 1;
sb->entries_per_node = calc_entries_per_node(sb->blocksize);
/* Initialize base indexes for atable */
atable_init_base(sb);
/* vfs fields */
vfs_sb(sb)->s_maxbytes = calc_maxbytes(sb->blocksize);
/* Probably does not belong here (maybe metablock) */
sb->freeinodes = MAX_INODES - be64_to_cpu(super->usedinodes);
sb->freeblocks = sb->volblocks;
sb->nextblock = be64_to_cpu(super->nextblock);
sb->nextinum = TUX_NORMAL_INO;
sb->atomdictsize = be64_to_cpu(super->atomdictsize);
sb->atomgen = be32_to_cpu(super->atomgen);
sb->freeatom = be32_to_cpu(super->freeatom);
/* logchain and logcount are read from super directly */
trace("blocksize %u, blockbits %u, blockmask %08x",
sb->blocksize, sb->blockbits, sb->blockmask);
trace("volblocks %Lu, freeblocks %Lu, freeinodes %Lu, nextblock %Lu",
sb->volblocks, sb->freeblocks, sb->freeinodes, sb->nextblock);
trace("atom_dictsize %Lu, freeatom %u, atomgen %u",
(s64)sb->atomdictsize, sb->freeatom, sb->atomgen);
trace("logchain %Lu, logcount %u",
be64_to_cpu(super->logchain), be32_to_cpu(super->logcount));
setup_roots(sb, super);
}
/* Initialize and setup sb by on-disk super block */
void setup_sb(struct sb *sb, struct disksuper *super)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
init_sb(sb);
__setup_sb(sb, super);
}
/* Load on-disk super block, and call setup_sb() with it */
int load_sb(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
struct disksuper *super = &sb->super;
int err;
/* At least initialize sb, even if load is failed */
init_sb(sb);
err = devio(READ, sb_dev(sb), SB_LOC, super, SB_LEN);
if (err)
return err;
if (memcmp(super->magic, TUX3_MAGIC_STR, sizeof(super->magic)))
return -EINVAL;
__setup_sb(sb, super);
return 0;
}
int save_sb(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
struct disksuper *super = &sb->super;
super->blockbits = cpu_to_be16(sb->blockbits);
super->volblocks = cpu_to_be64(sb->volblocks);
/* Probably does not belong here (maybe metablock) */
super->iroot = cpu_to_be64(pack_root(&itree_btree(sb)->root));
super->oroot = cpu_to_be64(pack_root(&otree_btree(sb)->root));
super->nextblock = cpu_to_be64(sb->nextblock);
super->atomdictsize = cpu_to_be64(sb->atomdictsize);
super->freeatom = cpu_to_be32(sb->freeatom);
super->atomgen = cpu_to_be32(sb->atomgen);
/* logchain and logcount are written to super directly */
return devio(WRITE_SYNC, sb_dev(sb), SB_LOC, super, SB_LEN);
}
/* Delta transition */
static int relog_frontend_defer_as_bfree(struct sb *sb, u64 val)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
log_bfree_relog(sb, val & ~(-1ULL << 48), val >> 48);
return 0;
}
static int relog_as_bfree(struct sb *sb, u64 val)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
log_bfree_relog(sb, val & ~(-1ULL << 48), val >> 48);
return stash_value(&sb->defree, val);
}
/* Obsolete the old unify, then start the log of new unify */
static void new_cycle_log(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
#if 0 /* ALLOW_FRONTEND_MODIFY */
/*
* FIXME: we don't need to write the logs generated by
* frontend at all. However, for now, we are writing those
* logs for debugging.
*/
/* Discard the logs generated by frontend. */
log_finish(sb);
log_finish_cycle(sb, 1);
#endif
/* Initialize logcount to count log blocks on new unify cycle. */
sb->super.logcount = 0;
}
/*
* Flush a snapshot of the allocation map to disk. Physical blocks for
* the bitmaps and new or redirected bitmap btree nodes may be allocated
* during the unify. Any bitmap blocks that are (re)dirtied by these
* allocations will be written out in the next unify cycle.
*/
static int unify_log(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
/* further block allocations belong to the next cycle */
unsigned unify = sb->unify++;
LIST_HEAD(orphan_add);
LIST_HEAD(orphan_del);
trace(">>>>>>>>> commit unify %u", unify);
/*
* Orphan inodes are still living, or orphan inodes in
* sb->otree are dead. And logs will be obsoleted, so, we
* apply those to sb->otree.
*/
/* FIXME: orphan_add/del has no race with frontend for now */
list_splice_init(&sb->orphan_add, &orphan_add);
list_splice_init(&sb->orphan_del, &orphan_del);
/* This is starting the new unify cycle of the log */
new_cycle_log(sb);
/* Add unify log as mark of new unify cycle. */
log_unify(sb);
/* Log to store freeblocks for flushing bitmap data */
log_freeblocks(sb, sb->freeblocks);
#ifdef ALLOW_FRONTEND_MODIFY
/*
* If frontend made defered bfree (i.e. it is not applied to
* bitmap yet), we have to re-log it on this cycle. Because we
* obsolete all logs in past.
*/
stash_walk(sb, &sb->defree, relog_frontend_defer_as_bfree);
#endif
/*
* Re-logging defered bfree blocks after unify as defered
* bfree (LOG_BFREE_RELOG) after delta. With this, we can
* obsolete log records on previous unify.
*/
unstash(sb, &sb->deunify, relog_as_bfree);
/*
* Merge the dirty bnode buffers to volmap dirty list, and
* clean ->unify_buffers up before dirtying bnode buffers on
* this unify. Later, bnode blocks will be flushed via
* volmap with leaves.
*/
list_splice_init(&sb->unify_buffers,
tux3_dirty_buffers(sb->volmap, TUX3_INIT_DELTA));
/*
* tux3_mark_buffer_unify() doesn't dirty inode, so we make
* sure volmap is dirty for unify buffers, now.
*
* See comment in tux3_mark_buffer_unify().
*/
__tux3_mark_inode_dirty(sb->volmap, I_DIRTY_PAGES);
/* Flush bitmap */
trace("> flush bitmap %u", unify);
tux3_flush_inode_internal(sb->bitmap, unify);
trace("< done bitmap %u", unify);
trace("> apply orphan inodes %u", unify);
{
int err;
/*
* This defered deletion of orphan from sb->otree.
* It should be done before adding new orphan, because
* orphan_add may have same inum in orphan_del.
*/
err = tux3_unify_orphan_del(sb, &orphan_del);
if (err)
return err;
/*
* This apply orphan inodes to sb->otree after flushed bitmap.
*/
err = tux3_unify_orphan_add(sb, &orphan_add);
if (err)
return err;
}
trace("< apply orphan inodes %u", unify);
assert(list_empty(&orphan_add));
assert(list_empty(&orphan_del));
trace("<<<<<<<<< commit unify done %u", unify);
return 0;
}
/* Apply frontend modifications to backend buffers, and flush data buffers. */
static int stage_delta(struct sb *sb, unsigned delta)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
int err;
/* Flush inodes */
err = tux3_flush_inodes(sb, delta);
if (err)
return err;
/* Flush atable after inodes. Because inode deletion may dirty atable */
err = tux3_flush_inode_internal(sb->atable, delta);
if (err)
return err;
#if 0
/* FIXME: we have to flush vtable somewhere */
err = tux3_flush_inode_internal(sb->vtable, delta);
if (err)
return err;
#endif
return err;
}
static int write_btree(struct sb *sb, unsigned delta)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
/*
* Flush leaves (and if there is unify, bnodes too) blocks.
* FIXME: Now we are using TUX3_INIT_DELTA for leaves. Do
* we need to per delta dirty buffers?
*/
return tux3_flush_inode_internal(sb->volmap, TUX3_INIT_DELTA);
}
/* allocate and write log blocks */
static int write_log(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
/* Finish to logging in this delta */
log_finish(sb);
log_finish_cycle(sb, 0);
return tux3_flush_inode_internal(sb->logmap, TUX3_INIT_DELTA);
}
/* userland only */
int apply_defered_bfree(struct sb *sb, u64 val)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
return bfree(sb, val & ~(-1ULL << 48), val >> 48);
}
static int commit_delta(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
trace("commit %i logblocks", be32_to_cpu(sb->super.logcount));
int err = save_sb(sb);
if (err)
return err;
/* Commit was finished, apply defered bfree. */
return unstash(sb, &sb->defree, apply_defered_bfree);
}
static void post_commit(struct sb *sb, unsigned delta)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
/*
* Check referencer of forked buffer was gone, and can free.
* FIXME: is this right timing and place to do this?
*/
free_forked_buffers(sb, NULL, 0);
tux3_clear_dirty_inodes(sb, delta);
}
static int need_unify(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
static unsigned crudehack;
return !(++crudehack % 3);
}
enum unify_flags { NO_UNIFY, ALLOW_UNIFY, FORCE_UNIFY, };
/* For debugging */
void tux3_start_backend(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
assert(current->journal_info == NULL);
current->journal_info = sb;
}
void tux3_end_backend(void)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
assert(current->journal_info);
current->journal_info = NULL;
}
/* If true, it is under backend */
int tux3_under_backend(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
return current->journal_info == sb;
}
static int do_commit(struct sb *sb, enum unify_flags unify_flag)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
unsigned delta = sb->marshal_delta;
struct iowait iowait;
int err = 0;
printk(KERN_INFO">>>>>>>>> commit delta %u", delta);
/* further changes of frontend belong to the next delta */
tux3_start_backend(sb);
/* Prepare to wait I/O */
tux3_iowait_init(&iowait);
sb->iowait = &iowait;
/* Add delta log for debugging. */
log_delta(sb);
/*
* NOTE: This works like modification from frontend. (i.e. this
* may generate defree log which is not committed yet at unify.)
*
* - this is before unify to merge modifications to this
* unify, and flush at once for optimization.
*
* - this is required to prevent unexpected buffer state for
* cursor_redirect(). If we applied modification after
* unify_log, it made unexpected dirty state (i.e. leaf is
* still dirty, but parent was already cleaned.)
*/
err = stage_delta(sb, delta);
if (err)
return err;
if ((unify_flag == ALLOW_UNIFY && need_unify(sb)) ||
unify_flag == FORCE_UNIFY) {
err = unify_log(sb);
if (err)
return err;
/* Add delta log for debugging. */
log_delta(sb);
}
write_btree(sb, delta);
write_log(sb);
/* Wait I/O was submitted */
tux3_iowait_wait(&iowait);
/*
* Commit last block (for now, this is sync I/O).
*
* FIXME: If this is not data integrity write, we don't have
* to wait the commit block. The commit block just have to
* written before next block block. (But defree must be after
* commit block.)
*/
commit_delta(sb);
tux3_end_backend();
printk(KERN_INFO"<<<<<<<<< commit done %u", delta);
post_commit(sb, delta);
printk(KERN_INFO"<<<<<<<<< post commit done %u", delta);
return err; /* FIXME: error handling */
}
/*
* Flush delta work
*/
static int flush_delta(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
unsigned delta = sb->marshal_delta;
int err;
#ifndef UNIFY_DEBUG
enum unify_flags unify_flag = ALLOW_UNIFY;
#else
struct delta_ref *delta_ref = sb->pending_delta;
enum unify_flags unify_flag = delta_ref->unify_flag;
sb->pending_delta = NULL;
#endif
err = do_commit(sb, unify_flag);
sb->committed_delta = delta;
clear_bit(TUX3_COMMIT_RUNNING_BIT, &sb->backend_state);
/* Wake up waiters for delta commit */
wake_up_all(&sb->delta_event_wq);
return err;
}
/*
* Provide transaction boundary for delta, and delta transition request.
*/
/* Grab the reference of current delta */
static struct delta_ref *delta_get(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
struct delta_ref *delta_ref;
/*
* Try to grab reference. If failed, retry.
*
* memory barrier pairs with __delta_transition(). But we never
* free ->current_delta, so we don't need rcu_read_lock().
*/
do {
delta_ref = rcu_dereference_check(sb->current_delta, 1);
} while (!atomic_inc_not_zero(&delta_ref->refcount));
trace("delta %u, refcount %u",
delta_ref->delta, atomic_read(&delta_ref->refcount));
return delta_ref;
}
/* Release the reference of delta */
static void delta_put(struct sb *sb, struct delta_ref *delta_ref)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
if (atomic_dec_and_test(&delta_ref->refcount)) {
trace("set TUX3_COMMIT_PENDING_BIT");
set_bit(TUX3_COMMIT_PENDING_BIT, &sb->backend_state);
schedule_flush_delta(sb);
}
trace("delta %u, refcount %u",
delta_ref->delta, atomic_read(&delta_ref->refcount));
}
/* Update current delta */
static void __delta_transition(struct sb *sb, struct delta_ref *delta_ref)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
/* Set the initial refcount is released by try_delta_transition(). */
assert(atomic_read(&delta_ref->refcount) == 0);
atomic_set(&delta_ref->refcount, 1);
/* Assign the delta number */
delta_ref->delta = sb->next_delta++;
#ifdef UNIFY_DEBUG
delta_ref->unify_flag = ALLOW_UNIFY;
#endif
/*
* Update current delta, then release reference.
*
* memory barrier pairs with delta_get().
*/
rcu_assign_pointer(sb->current_delta, delta_ref);
}
/*
* Delta transition.
*
* Find the next delta_ref, then update current delta to it, and
* release previous delta refcount.
*/
static void delta_transition(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
/*
* This is exclusive by TUX3_COMMIT_RUNNING_BIT (no writer),
* so rcu_dereference may not be needed.
*/
struct delta_ref *prev = rcu_dereference_check(sb->current_delta, 1);
struct delta_ref *delta_ref;
/* Find the next delta_ref */
delta_ref = prev + 1;
if (delta_ref == &sb->delta_refs[TUX3_MAX_DELTA])
delta_ref = &sb->delta_refs[0];
/* Update the current delta. */
__delta_transition(sb, delta_ref);
/* Set delta for marshal */
sb->marshal_delta = prev->delta;
#ifdef UNIFY_DEBUG
sb->pending_delta = prev;
#endif
/* Release initial refcount after updated the current delta. */
delta_put(sb, prev);
trace("prev %u, next %u", prev->delta, delta_ref->delta);
/* Wake up waiters for delta transition */
wake_up_all(&sb->delta_event_wq);
#if TUX3_FLUSHER == TUX3_FLUSHER_SYNC
wait_event(sb->delta_event_wq,
test_bit(TUX3_COMMIT_PENDING_BIT, &sb->backend_state));
#endif
}
#define delta_after_eq(a, b) \
(typecheck(unsigned, a) && \
typecheck(unsigned, b) && \
((int)(a) - (int)(b) >= 0))
#include "commit_flusher.c"
#include "commit_flusher_hack.c"
int force_unify(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
return sync_current_delta(sb, FORCE_UNIFY);
}
int force_delta(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
return sync_current_delta(sb, NO_UNIFY);
}
unsigned tux3_get_current_delta(void)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
struct delta_ref *delta_ref = current->journal_info;
assert(delta_ref != NULL);
return delta_ref->delta;
}
/* Choice sb->delta or sb->unify from inode */
unsigned tux3_inode_delta(struct inode *inode)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
unsigned delta;
switch (tux_inode(inode)->inum) {
case TUX_VOLMAP_INO:
case TUX_LOGMAP_INO:
/*
* Note: volmap are special, and has both of
* TUX3_INIT_DELTA and sb->unify. So TUX3_INIT_DELTA
* can be incorrect if delta was used for buffer.
* Note: logmap is similar to volmap, but it doesn't
* have sb->unify buffers.
*/
delta = TUX3_INIT_DELTA;
break;
case TUX_BITMAP_INO:
delta = tux_sb(inode->i_sb)->unify;
break;
default:
delta = tux3_get_current_delta();
break;
}
return delta;
}
/*
* This is used to avoid to run backend (if disabled asynchronous
* backend), and never be blocked. This is used in atomic context, or
* from backend task to avoid to run backend recursively.
*/
void change_begin_atomic(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
assert(current->journal_info == NULL);
current->journal_info = delta_get(sb);
}
/* change_end() without starting do_commit(). Use this only if necessary. */
void change_end_atomic(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
struct delta_ref *delta_ref = current->journal_info;
assert(delta_ref != NULL);
current->journal_info = NULL;
delta_put(sb, delta_ref);
}
/*
* This is used for nested change_begin/end. We should not use this
* usually (nesting change_begin/end is wrong for normal operations).
*
* For now, this is only used for ->evict_inode() debugging.
*/
void change_begin_atomic_nested(struct sb *sb, void **ptr)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
*ptr = current->journal_info;
current->journal_info = NULL;
change_begin_atomic(sb);
}
void change_end_atomic_nested(struct sb *sb, void *ptr)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
change_end_atomic(sb);
current->journal_info = ptr;
}
static int need_delta(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
static unsigned crudehack;
return !(++crudehack % 10);
}
/*
* Normal version of change_begin/end. If there is no special
* requirement, we should use this version.
*
* This checks backend job and run if disabled asynchronous backend,
* and blocked if disabled asynchronous backend and backend is
* running.
*/
void change_begin(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
#if TUX3_FLUSHER == TUX3_FLUSHER_SYNC
down_read(&sb->delta_lock);
#endif
change_begin_atomic(sb);
}
int change_end(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
int err = 0;
change_end_atomic(sb);
#if TUX3_FLUSHER == TUX3_FLUSHER_SYNC
up_read(&sb->delta_lock);
down_write(&sb->delta_lock);
#endif
if (need_delta(sb))
try_delta_transition(sb);
#if TUX3_FLUSHER == TUX3_FLUSHER_SYNC
err = flush_pending_delta(sb);
up_write(&sb->delta_lock);
#endif
return err;
}
/*
* This is used for simplify the error path, or separates big chunk to
* small chunk in loop.
*
* E.g. the following
*
* change_begin()
* while (stop) {
* change_begin_if_need()
* if (do_something() < 0)
* break;
* change_end_if_need()
* }
* change_end_if_need()
*/
void change_begin_if_needed(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
if (current->journal_info == NULL)
change_begin(sb);
}
void change_end_if_needed(struct sb *sb)
{
if(DEBUG_MODE_K==1)
{
printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__);
}
if (current->journal_info)
change_end(sb);
}