-
Notifications
You must be signed in to change notification settings - Fork 29
/
leases.c
1220 lines (939 loc) · 28.8 KB
/
leases.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
#include "leases.h"
#include "distributed/rpc_interface.h"
//#include "filesystem/fs.h"
//#include "filesystem/file.h"
//#include "posix/posix_interface.h"
#include "mlfs/mlfs_interface.h"
#include "filesystem/stat.h"
#include "concurrency/thread.h"
#ifdef KERNFS
#include "fs.h"
#else
#include "filesystem/fs.h"
#include "log/log.h"
#endif
#if MLFS_LEASE
double revoke_sec=0;
//struct timeval start_time, end_time;
static SharedGuts *SharedGuts_create(void *base, size_t size)
{
SharedGuts *guts = base;
//pthread_mutexattr_t attr;
//int rc = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
//assert(rc == 0);
int ret = pthread_spin_init(&guts->mutex, PTHREAD_PROCESS_SHARED);
assert(ret == 0);
guts->base = base;
guts->size = size;
guts->next_allocation = (char *)(guts + 1);
guts->hash = NULL;
return guts;
}
static SharedGuts *SharedGuts_mock()
{
SharedGuts *guts = mlfs_zalloc(sizeof(SharedGuts));
int ret = pthread_spin_init(&guts->mutex, PTHREAD_PROCESS_PRIVATE);
assert(ret == 0);
return guts;
}
SharedTable *SharedTable_create(const char *name, size_t arena_size)
{
SharedTable *t = mlfs_zalloc(sizeof(SharedTable));
t->fd = shm_open(name, O_CREAT | O_RDWR, 0666);
assert(t->fd > 0);
t->size = arena_size;
ftruncate(t->fd, t->size);
t->base = mmap(0, t->size, PROT_READ | PROT_WRITE, MAP_SHARED, t->fd, 0);
assert(t->base != MAP_FAILED);
memset(t->base, 0, t->size);
t->guts = SharedGuts_create(t->base, t->size);
return t;
}
SharedTable *SharedTable_mock()
{
SharedTable *t = mlfs_zalloc(sizeof(SharedTable));
t->guts = SharedGuts_mock();
return t;
}
SharedTable *SharedTable_subscribe(const char *name)
{
SharedTable *t = mlfs_zalloc(sizeof(SharedTable));
t->fd = shm_open(name, O_CREAT | O_RDWR, 0666);
assert(t->fd > 0);
SharedGuts *guts = mmap(NULL, sizeof (SharedGuts), PROT_READ | PROT_WRITE, MAP_SHARED, t->fd, 0);
assert(guts != MAP_FAILED);
pthread_spin_lock(&guts->mutex);
//void *where = guts->base;
void *where = 0;
t->size = guts->size;
munmap(guts, sizeof (SharedGuts));
t->base = mmap(where, t->size, PROT_READ | PROT_WRITE, MAP_SHARED, t->fd, 0);
t->guts = t->base;
//printf("---- DEBUGM --- t->guts->base %p t->base %p\n", t->guts->base, t->base);
//assert(t->guts->base == t->base);
assert(t->guts->size == t->size);
pthread_spin_unlock(&t->guts->mutex);
return t;
}
void SharedTable_unsubscribe(SharedTable *t)
{
munmap(t->base, t->size);
close(t->fd);
}
static void *SharedTable_malloc(SharedTable *t, size_t size)
{
void *ptr = t->guts->next_allocation;
if ((ptr - t->base) + size <= t->size) {
t->guts->next_allocation += size;
} else {
assert(0 && "out of memory");
ptr = NULL;
}
return ptr;
}
// TODO: implement frees for SharedTable
static void SharedTable_free(SharedTable *t, void *p)
{
// no-op
}
static inline mlfs_lease_t *lcache_find(uint32_t inum)
{
mlfs_lease_t *ls;
//pthread_spin_lock(&lcache_spinlock);
HASH_FIND(hash_handle, lease_table->guts->hash, &inum,
sizeof(uint32_t), ls);
//pthread_spin_unlock(&lcache_spinlock);
return ls;
}
static inline mlfs_lease_t *lcache_alloc_add(uint32_t inum)
{
mlfs_lease_t *ls;
#ifdef __cplusplus
ls = static_cast<mlfs_lease_t *>(mlfs_zalloc(sizeof(mlfs_lease_t)));
#else
ls = mlfs_zalloc(sizeof(mlfs_lease_t));
#endif
pthread_spin_init(&ls->mutex, PTHREAD_PROCESS_PRIVATE);
//pthread_mutex_init(&ls->mutex, NULL);
if (!ls)
panic("Fail to allocate lease\n");
ls->inum = inum;
HASH_ADD(hash_handle, lease_table->guts->hash, inum,
sizeof(uint32_t), ls);
return ls;
}
static inline mlfs_lease_t *lcache_add(mlfs_lease_t *ls)
{
uint32_t inum = ls->inum;
pthread_spin_lock(&lease_table->guts->mutex);
HASH_ADD(hash_handle, lease_table->guts->hash, inum,
sizeof(uint32_t), ls);
pthread_spin_unlock(&lease_table->guts->mutex);
return ls;
}
static inline int lcache_del(mlfs_lease_t *ls)
{
pthread_spin_lock(&lease_table->guts->mutex);
HASH_DELETE(hash_handle, lease_table->guts->hash, ls);
free(ls);
pthread_spin_unlock(&lease_table->guts->mutex);
return 0;
}
void lupdate(mlfs_lease_t *ls)
{
}
// Find the inode with number inum on device dev
// and return the in-memory copy. Does not lock
// the inode and does not read it from disk.
mlfs_lease_t* lget(uint32_t inum)
{
mlfs_lease_t *ls;
pthread_spin_lock(&lease_table->guts->mutex);
ls = lcache_find(inum);
if (ls) {
pthread_spin_unlock(&lease_table->guts->mutex);
return ls;
}
ls = lcache_alloc_add(inum);
pthread_spin_unlock(&lease_table->guts->mutex);
return ls;
}
// Allocate a new inode with the given type on device dev.
// A free inode has a type of zero.
mlfs_lease_t* lalloc(uint8_t type, uint32_t inode_nr)
{
uint32_t inum;
int ret;
//struct buffer_head *bp;
mlfs_lease_t *ls;
ls = lcache_find(inode_nr);
if (!ls)
ls = lget(inode_nr);
#if 0
ip->_dinode = (struct dinode *)ip;
if (!(ip->flags & I_VALID)) {
read_ondisk_inode(dev, inode_nr, &dip);
if (dip.itype == 0) {
memset(&dip, 0, sizeof(struct dinode));
dip.itype = type;
dip.dev = dev;
}
sync_inode_from_dinode(ip, &dip);
ip->flags |= I_VALID;
}
ip->i_sb = sb;
ip->i_generation = 0;
ip->i_data_dirty = 0;
ip->nlink = 1;
#endif
return ls;
}
int read_ondisk_lease(uint32_t inum, mlfs_lease_t *ls)
{
int ret;
struct buffer_head *bh;
addr_t lease_block;
lease_block = get_lease_block(g_root_dev, inum);
mlfs_debug("read lease block: %lu\n", lease_block);
bh = bh_get_sync_IO(g_root_dev, lease_block, BH_NO_DATA_ALLOC);
bh->b_size = sizeof(mlfs_lease_t);
bh->b_data = (uint8_t *)ls;
bh->b_offset = sizeof(mlfs_lease_t) * (inum % LPB);
bh_submit_read_sync_IO(bh);
mlfs_io_wait(g_root_dev, 1);
return 0;
}
int write_ondisk_lease(mlfs_lease_t *ls)
{
int ret;
struct buffer_head *bh;
addr_t lease_block;
lease_block = get_lease_block(g_root_dev, ls->inum);
mlfs_debug("write lease block: %lu\n", lease_block);
bh = bh_get_sync_IO(g_root_dev, lease_block, BH_NO_DATA_ALLOC);
bh->b_size = sizeof(mlfs_lease_t);
bh->b_data = (uint8_t *)ls;
bh->b_offset = sizeof(mlfs_lease_t) * (ls->inum % LPB);
ret = mlfs_write(bh);
mlfs_io_wait(g_root_dev, 0);
return ret;
}
#ifdef LIBFS
int acquire_family_lease(uint32_t inum, int type, char *path)
{
mlfs_printf("trying to acquire child/parent leases of type %d for inum %u\n",
type, inum);
// child should always be acquired first, otherwise we deadlock
// due to ordering of namei
acquire_lease(inum, type, path);
acquire_parent_lease(inum, type, path);
return 1;
}
int acquire_parent_lease(uint32_t inum, int type, char *path)
{
mlfs_printf("trying to acquire parent lease of type %d for inum %u\n",
type, inum);
char name[DIRSIZ], parent_path[DIRSIZ];
struct inode *pid;
pid = nameiparent(path, name);
get_parent_path(path, parent_path, name);
acquire_lease(pid->inum, type, parent_path);
return 1;
}
int report_lease_error(uint32_t inum)
{
mlfs_lease_t* ls = lcache_find(inum);
if (!ls)
panic("failed to find lease\n");
ls->errcode = 1;
return 1;
}
// invoked by KernFS upcall
int revoke_lease(int sockfd, uint32_t seq_n, uint32_t inum)
{
mlfs_printf("\x1B[31m [L] KernFS requesting lease revocation for inum %u \x1B[0m\n", inum);
mlfs_lease_t* ls = lcache_find(inum);
if (!ls) {
ls = lcache_alloc_add(inum);
}
if (!ls)
panic("failed to allocate lease\n");
pthread_spin_lock(&ls->mutex); //never calls this if we're using shared locks (causes a deadlock)
if(ls->hid != g_self_id)
panic("kernfs trying to revoke an unowned lease\n");
while(ls->holders > 0) {
#if 0
int holders;
current = time(NULL);
if((current - start) > timeout) {
holders = ls->holders;
if(cmpxchg(&ls->holders, holders, 0) == holders)
goto retry;
}
#else
cpu_relax();
#endif
}
//rpc_lease_flush_response(sockfd, seq_n, inum, ls->lversion, ls->lblock);
rpc_lease_change(ls->mid, g_self_id, ls->inum, LEASE_FREE, ls->lversion, ls->lblock, 0);
ls->state = LEASE_FREE;
ls->hid = ls->mid;
pthread_spin_unlock(&ls->mutex);
mlfs_printf("lease revoked for inum %u\n", inum);
}
int mark_lease_revocable(uint32_t inum)
{
uint64_t start_tsc;
mlfs_lease_t* ls = lcache_find(inum);
if (!ls) {
mlfs_info("[Warning] failed to surrender uncached lease for inum: %u\n", inum);
return 0;
}
//panic("failed to allocate lease\n");
mlfs_info("surrendering lease (state: %d) for inum: %u\n",
ls->state, inum);
//rpc_lease_change(ls->mid, ls->inum, LEASE_FREE, g_fs_log->start_version,
// g_log_sb[0]->start_digest);
if(ls->holders == 0) {
mlfs_info("%s", "[Warning] trying to give up unowned lease.\n");
return -1;
}
struct logheader_meta *loghdr_meta;
loghdr_meta = get_loghdr_meta();
//uint32_t n_digest = atomic_load(&g_log_sb[0]->n_digest);
ls->lversion = g_fs_log->avail_version;
ls->lblock = loghdr_meta->hdr_blkno;
mlfs_printf("marking lease revocable: inum %u block_nr %lu version %u\n", inum, ls->lblock, ls->lversion);
if (enable_perf_stats)
start_tsc = asm_rdtscp();
if(ls->holders == 1) {
#ifndef LAZY_SURRENDER
rpc_lease_change(ls->mid, g_self_id, ls->inum, LEASE_FREE, ls->lversion, ls->lblock, 0);
ls->state = LEASE_FREE;
#endif
#if 0
while(make_digest_request_async(100) == -EBUSY)
cpu_relax();
mlfs_info("%s", "[L] Giving up lease. asynchronous digest!\n");
#endif
ls->holders--;
}
else
ls->holders--;
if (enable_perf_stats)
g_perf_stats.lease_revoke_wait_tsc += (asm_rdtscp() - start_tsc);
mlfs_info("lease surrendered (state: %d) for inum: %u (holder = %u)\n",
ls->state, inum, ls->hid);
}
int acquire_lease(uint32_t inum, int type, char *path)
{
mlfs_printf("LIBFS ID= %d trying to acquire lease of type %d for inum %u\n", g_self_id, type, inum);
mlfs_lease_t* ls = lcache_find(inum);
int i_dirty = 0;
uint64_t start_tsc;
if (!ls) {
ls = lcache_alloc_add(inum);
}
if (!ls)
panic("failed to allocate lease\n");
if (enable_perf_stats)
start_tsc = asm_rdtscp();
pthread_spin_lock(&ls->mutex);
retry:
if(type > ls->state || ls->hid != g_self_id) {
rpc_lease_change(ls->mid, g_self_id, ls->inum, type, 0, 0, 1);
// received invalid response
if(ls->errcode) {
ls->errcode = 0; // clear error code
goto retry;
}
ls->holders++;
// FIXME: remove this; inode cache should be cleared whenever leases
// are revoked
i_dirty = 1;
#if 0
// update size after acquiring inode
// necessary to prevent issues with directory allocation
struct inode *ip;
ip = icache_find(inum);
ilock(ip);
// no need to update size for dirty inodes
//if (ip && !(ip->flags & I_DIRTY)) {
// struct dinode dip;
// read_ondisk_inode(inum, &dip);
// ip->size = dip.size;
//}
sync_inode_ext_tree(ip);
iunlock(ip);
#endif
mlfs_printf("LIBFS ID = %d lease acquired path %s new state %d manager ID = %u\n",
g_self_id, path, type, ls->mid);
}
else {
ls->holders++;
mlfs_printf("lease already exists (inum %u state %d). No RPCs triggered.\n", inum, ls->state);
}
ls->state = type;
ls->hid = g_self_id;
mlfs_info("[DEBUG] after ls->inum %u ls->holders = %d\n", ls->inum, ls->holders);
pthread_spin_unlock(&ls->mutex);
#if 1
if(i_dirty) {
// update size after acquiring inode
// necessary to prevent issues with directory allocation
struct dinode dip;
struct inode *ip;
ip = icache_find(inum);
ilock(ip);
//XXX: check for potential concurrency issues if reading while dinode is being updated by kernfs
read_ondisk_inode(inum, &dip);
sync_inode_from_dinode(ip, &dip);
iunlock(ip);
}
#endif
if (enable_perf_stats)
g_perf_stats.lease_lpc_wait_tsc += (asm_rdtscp() - start_tsc);
return 1;
}
// necessary when dropping a lease
// FIXME: prevent unnecessary cache wipes by only calling this when a lease is in conflict
int purge_dir_caches(uint32_t inum)
{
//purge node's de_cache entry from inode's parent
//purge dcache entry of inode
//purge dcache entries of all children files
//purge decache entries of all files
mlfs_info("purging cache for inum: %u\n", inum);
#if 0
struct inode* inode = icache_find(g_root_dev, inum);
if (!inode)
return 0;
mlfs_info("purging caches for inum: %u\n", inode->inum);
#if 0
struct inode *id, *pid;
uint8_t *dirent_array;
struct mlfs_dirent de = {0};
char name[DIRSIZ];
offset_t off = 0;
pid = nameiparent(path, name);
if ((id = dir_lookup(pid, name, &off)) != NULL) {
get_dirent(pid, &de, off);
mlfs_assert(strcmp(de.name, name) == 0);
de_cache_del(pid, name);
}
dlookup_del(path);
#endif
if(inode->itype == T_DIR)
de_cache_del_all(inode);
fcache_del_all(inode);
icache_del(inode);
free(inode);
//TODO: should also delete path from lookup table
// is there an easy way to do this using just the inum?
//dlookup_del(inode->dev, path);
#endif
return 1;
}
#endif
#ifdef LIBFS
void update_remote_ondisk_lease(uint8_t node_id, mlfs_lease_t *ls)
{
mlfs_debug("writing remote ondisk lease inum %u to peer %u\n", ls->inum, node_id);
addr_t offset = (get_lease_block(g_root_dev, ls->inum) << g_block_size_shift) + sizeof(mlfs_lease_t) * (ls->inum %LPB);
addr_t src = ((uintptr_t)g_bdev[g_root_dev]->map_base_addr) + offset;
addr_t dst = mr_remote_addr(g_peers[node_id]->sockfd[0], MR_NVM_SHARED) + offset;
rdma_meta_t *meta = create_rdma_meta(src, dst, sizeof(mlfs_lease_t));
IBV_WRAPPER_WRITE_ASYNC(g_peers[node_id]->sockfd[0], meta, MR_NVM_SHARED, MR_NVM_SHARED);
}
// For lease acquisitions, lease must be locked beforehand.
int modify_lease_state(int req_id, int inum, int new_state, int version, addr_t logblock)
{
uint64_t start_tsc;
// get lease from cache
mlfs_lease_t* ls = lget(inum);
if (!ls)
panic("failed to allocate lease\n");
mlfs_info("modifying lease inum[%u] from %d to %d Req. ID = %d Holder ID = %d (num holders = %u)\n",
inum, ls->state, new_state, req_id, ls->hid, ls->holders);
if(new_state == LEASE_READ) {
panic("read path not implemented!\n");
}
else if(new_state == LEASE_FREE) {
ls->lversion = version;
ls->lblock = logblock;
if(ls->holders > 0)
ls->holders--;
else
panic("lease surrender failed. Lease has no current owners!\n");
//ls->holders--;
return 1;
}
else {
//pthread_mutex_lock(&ls->mutex);
if(ls->holders == 0) {
goto acquire_lease;
}
else {
if (enable_perf_stats)
start_tsc = asm_rdtscp();
// wait until original lease owner relinquishes its lease
while(ls->holders > 0)
cpu_relax();
if (enable_perf_stats)
g_perf_stats.local_contention_tsc += (asm_rdtscp() - start_tsc);
//FIXME: need to implement this call; we currently assume that the digest checkpoint was reached
//wait_till_digest_checkpoint(g_sync_ctx[ls->hid]->peer, ls->lversion, ls->lblock);
acquire_lease:
if (enable_perf_stats)
start_tsc = asm_rdtscp();
// wait until lease reaches its designated digest checkpoint
// KernFS sets lease version and lblock to zero when this condition is met
while(ls->lversion | ls->lblock)
cpu_relax();
if (enable_perf_stats)
g_perf_stats.local_digestion_tsc += (asm_rdtscp() - start_tsc);
ls->holders++;
ls->state = new_state;
ls->hid = req_id;
//ls->holders--;
goto PERSIST;
}
}
PERSIST:
write_ondisk_lease(ls);
for(int i=0; i<g_n_nodes;i++) {
if(i == g_kernfs_id)
continue;
mlfs_printf("updating remote lease for inum %u\n", ls->inum);
update_remote_ondisk_lease(i, ls);
}
//pthread_mutex_unlock(&ls->mutex);
return 1;
}
void shutdown_lease_protocol()
{
//mlfs_printf("Elapsed time for revocations: %lf\n", revoke_sec);
}
#else
int modify_lease_state(int req_id, int inum, int new_state, int version, addr_t logblock)
{
int do_migrate = 0;
// get lease from cache
mlfs_lease_t* ls = lget(inum);
#if 0
if (!ls) {
ls = lcache_alloc_add(g_root_dev, inum);
}
#endif
if (!ls)
panic("failed to allocate lease\n");
if (enable_perf_stats) {
//mlfs_printf("local_kernfs_id for peer %d is %d\n", req_id, local_kernfs_id(req_id));
//if(g_self_id == local_kernfs_id(req_id))
// g_perf_stats.lease_rpc_local_nr++;
//else
g_perf_stats.lease_rpc_remote_nr++;
}
#ifdef LEASE_MIGRATION
// this KernFS is not the lease manager
if (ls->mid != g_self_id) {
return -ls->mid;
}
#endif
mlfs_printf("modifying lease inum[%u] from %d to %d Req. ID = %d Holder ID = %d (num holders = %u)\n",
inum, ls->state, new_state, req_id, ls->hid, ls->holders);
if(new_state == LEASE_READ) {
panic("read path not implemented!\n");
}
else if(new_state == LEASE_FREE) {
// if previous state is write, update log params
//if(ls->state == LEASE_WRITE) {
// confirm that LibFS has actually held this lease
//mlfs_assert(ls->hid == req_id);
//ls->state = LEASE_FREE;
ls->lversion = version;
ls->lblock = logblock;
//}
if(ls->holders > 0)
ls->holders--;
else
panic("lease surrender failed. Lease has no current owners!\n");
//ls->holders--;
return 1;
}
else {
pthread_spin_lock(&ls->mutex);
#ifdef LEASE_MIGRATION
// this KernFS is not the lease manager
if (ls->mid != g_self_id) {
return -ls->mid;
}
#endif
//goto PERSIST;
time_t start = time(NULL);
time_t current = start;
time_t timeout = 1; // timeout interval in seconds
retry:
if(ls->holders == 0) {
goto acquire_lease;
}
else {
//ls->holders++;
//if(ls->hid == req_id)
// goto acquire_lease;
mlfs_printf("%s", "\x1B[31m [L] Conflict detected: synchronous digest request and wait! \x1B[0m\n");
busy_wait:
//FIXME: temporarily commenting this out
//while(ls->holders > 1)
// cpu_relax();
//if(cmpxchg(&ls->holders, 1, 2) != 1)
// goto busy_wait;
rpc_lease_flush(ls->hid, inum, true);
// wait until original lease owner relinquishes its lease
while(ls->holders > 0)
cpu_relax();
wait_till_digest_checkpoint(g_sync_ctx[ls->hid]->peer, ls->lversion, ls->lblock);
acquire_lease:
//FIXME: temporarily commenting this out
ls->holders++;
//ls->holders = 1;
ls->state = new_state;
ls->hid = req_id;
//ls->holders--;
goto PERSIST;
}
#if 0
while(ls->holders > 0) {
#if 0
int holders;
current = time(NULL);
if((current - start) > timeout) {
holders = ls->holders;
if(cmpxchg(&ls->holders, holders, 0) == holders)
goto retry;
}
#else
cpu_relax();
#endif
}
#endif
}
PERSIST:
#ifdef LEASE_MIGRATION
// We are trying to acquire the lease; update ltime and check if we need to migrate
// update lease acquisition time if it's being accessed from a different node
//if(local_kernfs_id(ls->hid) != local_kernfs_id(req_id))
// mlfs_get_time(&ls->time);
// consecutive lease requests from same LibFS & requester's local kernfs isn't manager
if(local_kernfs_id(req_id) != g_self_id) {
struct timespec now;
//time_stats elaps;
//mlfs_get_time(&ls->time);
//mlfs_get_time(&now);
clock_gettime(CLOCK_MONOTONIC, &now);
//mlfs_printf("time (before): %ld.%06ld s \n", ls->time.tv_sec, ls->time.tv_usec);
//mlfs_printf("time (after): %ld.%06ld s \n", now.tv_sec, now.tv_usec);
double start_sec = (double)(ls->time.tv_sec * 1000000000.0 + (double)ls->time.tv_nsec) / 1000000000.0;
double end_sec = (double)(now.tv_sec * 1000000000.0 + (double)now.tv_nsec) / 1000000000.0;
double sec = end_sec - start_sec;
//timersub(&now, &ls->time, &elaps);
//double sec = (double)(elaps.tv_sec * 1000000.0 + (double)elaps.tv_usec) / 1000000.0;
//double sec = (double)elaps.tv_sec;
mlfs_printf("Lease elapsed time: %f\n", sec);
if(sec >= 1) {
do_migrate = 1;
// migrate lease management to local kernfs
ls->mid = local_kernfs_id(req_id);
// record migration time (to prevent frequent migrations)
//mlfs_get_time(&ls->time);
clock_gettime(CLOCK_MONOTONIC, &ls->time);
mlfs_printf("Migrating lease to KernFS ID = %d\n", ls->mid);
#if 0
// flush all caches for inode
int pos = 0;
while(!bitmap_empty(ip->libfs_ls_bitmap, g_n_max_libfs)) {
pos = find_next_bit(ip->libfs_ls_bitmap, g_n_max_libfs, pos);
if(pos != libfs_id) {
rpc_lease_flush(pos, ip->inum, false);
}
bitmap_clear(ip->libfs_ls_bitmap, pos, 1);
}
#endif
}
}
#endif
write_ondisk_lease(ls);
for(int i=0; i<g_n_nodes;i++) {
if(i == g_self_id)
continue;
update_remote_ondisk_lease(i, ls);
}
if(do_migrate) {
if (enable_perf_stats) {
g_perf_stats.lease_migration_nr++;
}
for(int i=0; i<g_n_nodes;i++) {
if(i == g_self_id)
continue;
rpc_lease_migrate(i, ls->inum, ls->mid);
}
}
pthread_spin_unlock(&ls->mutex);
return 1;
}
int clear_lease_checkpoints(int req_id, int version, addr_t log_block)
{
mlfs_lease_t *item, *tmp;
mlfs_printf("clearing outdated digest checkpoints for leases belonging to peer ID = %d\n", req_id);
pthread_spin_lock(&lease_table->guts->mutex);
//FIXME: keep a list of leases held by req_id to make this go faster
HASH_ITER(hash_handle, lease_table->guts->hash, item, tmp) {
//HASH_DELETE(hash_handle, lease_hash, item);
mlfs_debug("[DEBUG] hash item: lease inum %u state %u\n", item->inum, item->state);
if(item->hid == req_id && ((version == item->lversion && log_block >= item->lblock)
|| version > item->lversion)) {
if(item->lversion || item->lblock) {
item->lversion = 0;
item->lblock = 0;
mlfs_printf("lease digestion checkpoint cleared for inum %u\n", item->inum);
}
}
//mlfs_free(item);
}
pthread_spin_unlock(&lease_table->guts->mutex);
}
int discard_leases(int req_id)
{
//#ifdef LAZY_SURRENDER
mlfs_lease_t *item, *tmp;
mlfs_printf("discarding all leases for peer ID = %d\n", req_id);
//pthread_spin_lock(&lcache_spinlock);
HASH_ITER(hash_handle, lease_table->guts->hash, item, tmp) {
//HASH_DELETE(hash_handle, lease_hash, item);
mlfs_debug("[DEBUG] hash item: lease inum %u state %u\n", item->inum, item->state);
if(item->state == LEASE_WRITE && item->holders > 0) {
if(item->hid == req_id) {
//rpc_lease_change(item->mid, item->inum, LEASE_FREE, 0, 0, 0);
modify_lease_state(req_id, item->inum, LEASE_FREE, 0, 0);
mlfs_debug("lease discarded for inum %u\n", item->inum);
}
}
//mlfs_free(item);
}
//HASH_CLEAR(hash_handle, lease_hash);
//pthread_spin_unlock(&lcache_spinlock);
//#endif
return 0;
}
int update_lease_manager(uint32_t inum, uint32_t new_kernfs_id)
{
mlfs_lease_t* ls = lcache_find(inum);
if (ls) {
mlfs_printf("migrate - inum %u update lease manager from %u to %u\n",
inum, ls->mid, new_kernfs_id);
ls->mid = new_kernfs_id;
}
return 0;
}
#if 0
// modify lease state (this call is potentially blocking)
// FIXME: use locks!
int modify_lease_state(int libfs_id, int inum, int req_type, int log_version, addr_t log_block)
{
struct inode* ip = icache_find(g_root_dev, inum);
if (!ip) {
struct dinode dip;
ip = icache_alloc_add(g_root_dev, inum);
read_ondisk_inode(g_root_dev, inum, &dip);
// FIXME: possible synchronization issue
// Sometimes LibFS requests lease for a dirty (undigested) inode
// As a temporary workaround, just return if this happens
if(dip.itype == 0)
return 1;
//mlfs_assert(dip.itype != 0);
sync_inode_from_dinode(ip, &dip);
ip->i_sb = sb;
if(dip.dev == 0)
return 1;
//mlfs_assert(dip.dev != 0);
}
mlfs_info("modifying lease inum[%u] from %d to %d Req. ID = %d Holder ID= %d (num holders = %u)\n",
inum, ip->lstate, req_type, libfs_id, ip->lhid, ip->lholders);