-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinode.cpp
1885 lines (1371 loc) · 61.3 KB
/
inode.cpp
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 2015 The Trustees of Princeton University
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "inode.h"
#include "block.h"
// UG-specific inode information, for fskit
struct UG_inode {
char* name; // name of this inode (allowed, since Syndicate does not support links)
struct SG_manifest manifest; // latest manifest of this file's blocks (includes coordinator_id and file_version)
int64_t ms_write_nonce; // last-known write nonce from the MS
int64_t ms_xattr_nonce; // last-known xattr nonce from the MS
unsigned char ms_xattr_hash[ SHA256_DIGEST_LENGTH ]; // last-known xattr hash from the MS
int64_t generation; // last-known generation number of this file
int64_t write_nonce; // uncommited write nonce (initialized to ms_write_nonce; used to indicate dirty data)
int64_t xattr_nonce; // uncommitted xattr nonce
struct timespec refresh_time; // time of last refresh from the ms
struct timespec manifest_refresh_time; // time of last manifest refresh
struct timespec children_refresh_time; // if this is a directory, this is the time the children were last reloaded
uint32_t max_read_freshness; // how long since last refresh, in millis, this inode is to be considered fresh for reading
uint32_t max_write_freshness; // how long since last refresh, in millis, this inode is to be considered fresh for writing
bool read_stale; // if true, this file must be revalidated before the next read
bool write_stale; // if true, this file must be revalidated before the next write
bool dirty; // if true, then we need to flush data on fsync()
int64_t ms_num_children; // the number of children the MS says this inode has
int64_t ms_capacity; // maximum index number of a child in the MS
bool vacuuming; // if true, then we're currently vacuuming this file
bool vacuumed; // if true, then we've already tried to vacuum this file upon discovery (false means we should try again)
UG_dirty_block_map_t* dirty_blocks; // set of locally-modified blocks that must be replicated, either on the next fsync() or last close()
struct SG_manifest replaced_blocks; // set of blocks replaced by writes, that need to be garbage-collected (contains only metadata)
UG_inode_fsync_queue_t* sync_queue; // queue of fsync requests on this inode
struct fskit_entry* entry; // the fskit entry that owns this inode
bool renaming; // if true, then this inode is in the process of getting renamed. Concurrent renames will fail with EBUSY
bool deleting; // if true, then this inode is in the process of being deleted. Concurrent opens and stats will fail
bool creating; // if true, then this inode is in the process of being created. Truncate will be a no-op in this case.
};
// initialize common inode data
// type should be MD_ENTRY_FILE or MD_ENTRY_DIR
// return 0 on success
// return -ENOMEM on OOM
static int UG_inode_init_common( struct UG_inode* inode, char const* name, int type ) {
memset( inode, 0, sizeof(struct UG_inode) );
inode->name = SG_strdup_or_null( name );
if( inode->name == NULL ) {
if( name != NULL ) {
return -ENOMEM;
}
else {
return -EINVAL;
}
}
// regular file?
if( type == MD_ENTRY_FILE ) {
// sync queue
inode->sync_queue = SG_safe_new( UG_inode_fsync_queue_t() );
if( inode->sync_queue == NULL ) {
return -ENOMEM;
}
// dirty blocks
inode->dirty_blocks = SG_safe_new( UG_dirty_block_map_t() );
if( inode->dirty_blocks == NULL ) {
SG_safe_delete( inode->sync_queue );
return -ENOMEM;
}
}
return 0;
}
// initialize an inode, from an entry and basic data
// entry must be write-locked
// return 0 on success
// return -ENOMEM on OOM
int UG_inode_init( struct UG_inode* inode, char const* name, struct fskit_entry* entry, uint64_t volume_id, uint64_t coordinator_id, int64_t file_version ) {
int rc = 0;
rc = UG_inode_init_common( inode, name, fskit_entry_get_type( entry ) == FSKIT_ENTRY_TYPE_FILE ? MD_ENTRY_FILE : MD_ENTRY_DIR );
if( rc != 0 ) {
return rc;
}
// manifest
rc = SG_manifest_init( &inode->manifest, volume_id, coordinator_id, fskit_entry_get_file_id( entry ), file_version );
if( rc != 0 ) {
SG_safe_delete( inode->sync_queue );
SG_safe_delete( inode->dirty_blocks );
return rc;
}
SG_manifest_set_size( &inode->manifest, fskit_entry_get_size( entry ) );
if( fskit_entry_get_type( entry ) == FSKIT_ENTRY_TYPE_FILE ) {
// replaced blocks
rc = SG_manifest_init( &inode->replaced_blocks, volume_id, coordinator_id, fskit_entry_get_file_id( entry ), file_version );
if( rc != 0 ) {
SG_safe_delete( inode->sync_queue );
SG_safe_delete( inode->dirty_blocks );
SG_manifest_free( &inode->manifest );
return rc;
}
}
return 0;
}
// initialize an inode from an fskit_entry, and protobof'ed msent and mmsg
// return 0 on success
// return -ENOMEM on OOM
// return -EINVAL if the file IDs don't match
int UG_inode_init_from_protobuf( struct UG_inode* inode, struct fskit_entry* entry, ms::ms_entry* msent, SG_messages::Manifest* mmsg ) {
int rc = 0;
// sanity check
if( fskit_entry_get_file_id( entry ) != msent->file_id() ) {
return -EINVAL;
}
rc = UG_inode_init_common( inode, msent->name().c_str(), fskit_entry_get_type( entry ) == FSKIT_ENTRY_TYPE_FILE ? MD_ENTRY_FILE : MD_ENTRY_DIR );
if( rc != 0 ) {
return rc;
}
// manifest
rc = SG_manifest_load_from_protobuf( &inode->manifest, mmsg );
if( rc != 0 ) {
SG_safe_delete( inode->sync_queue );
SG_safe_delete( inode->dirty_blocks );
return rc;
}
// fill in the rest
SG_manifest_set_modtime( &inode->manifest, msent->manifest_mtime_sec(), msent->manifest_mtime_nsec() );
inode->write_nonce = msent->write_nonce();
inode->xattr_nonce = msent->xattr_nonce();
inode->generation = msent->generation();
inode->max_read_freshness = msent->max_read_freshness();
inode->max_write_freshness = msent->max_write_freshness();
inode->ms_num_children = msent->num_children();
inode->ms_capacity = msent->capacity();
return 0;
}
// initialize an inode from an exported inode data and an fskit_entry
// NOTE: file ID in inode_data and fent must match, as must their types
// the inode's manifest will be stale, since it currently has no data.
// return 0 on success
// return -ENOMEM on OOM
// return -EINVAL if the data is invalid (i.e. file IDs don't match, and such)
// fent must be at least read-locked
int UG_inode_init_from_export( struct UG_inode* inode, struct md_entry* inode_data, struct fskit_entry* fent ) {
int rc = 0;
int type = fskit_entry_get_type( fent );
uint64_t file_id = fskit_entry_get_file_id( fent );
// ID sanity check
if( inode_data->file_id != file_id && inode_data->file_id > 0 ) {
SG_error("inode_data->file_id == %" PRIX64 ", fent->file_id == %" PRIX64 "\n", inode_data->file_id, file_id );
return -EINVAL;
}
// type sanity check
if( type == FSKIT_ENTRY_TYPE_FILE && inode_data->type > 0 && inode_data->type != MD_ENTRY_FILE ) {
SG_error("inode_data->type == %d, fent->type == %d\n", inode_data->type, type );
return -EINVAL;
}
if( type == FSKIT_ENTRY_TYPE_DIR && inode_data->type > 0 && inode_data->type != MD_ENTRY_DIR ) {
SG_error("inode_data->type == %d, fent->type == %d\n", inode_data->type, type );
return -EINVAL;
}
rc = UG_inode_init( inode, inode_data->name, fent, inode_data->volume, inode_data->coordinator, inode_data->version );
if( rc != 0 ) {
return rc;
}
SG_manifest_set_modtime( &inode->manifest, inode_data->manifest_mtime_sec, inode_data->manifest_mtime_nsec );
inode->write_nonce = inode_data->write_nonce;
inode->xattr_nonce = inode_data->xattr_nonce;
inode->generation = inode_data->generation;
inode->max_read_freshness = inode_data->max_read_freshness;
inode->max_write_freshness = inode_data->max_write_freshness;
inode->ms_num_children = inode_data->num_children;
inode->ms_capacity = inode_data->capacity;
clock_gettime( CLOCK_REALTIME, &inode->refresh_time );
SG_manifest_set_stale( &inode->manifest, true );
return 0;
}
// common fskit entry initialization from an exported inode
// return 0 on success (always succeeds)
static int UG_inode_fskit_common_init( struct fskit_entry* fent, struct md_entry* inode_data ) {
struct timespec ts;
// set ctime, mtime
ts.tv_sec = inode_data->mtime_sec;
ts.tv_nsec = inode_data->mtime_nsec;
fskit_entry_set_mtime( fent, &ts );
ts.tv_sec = inode_data->ctime_sec;
ts.tv_nsec = inode_data->ctime_nsec;
fskit_entry_set_ctime( fent, &ts );
// set size
fskit_entry_set_size( fent, inode_data->size );
return 0;
}
// generate a new fskit entry for a directory
// return 0 on success
// return -ENOMEM on OOM
// return -EINVAL if inode_data doesn't represent a file
// fent must be write-locked
static int UG_inode_fskit_dir_init( struct fskit_entry* fent, struct fskit_entry* parent, struct md_entry* inode_data ) {
int rc = 0;
// sanity check
if( inode_data->type != MD_ENTRY_DIR ) {
SG_error("Inode %" PRIX64 " is not a directory\n", inode_data->file_id);
return -EINVAL;
}
rc = fskit_entry_init_dir( fent, parent, inode_data->file_id, inode_data->owner, inode_data->volume, inode_data->mode );
if( rc != 0 ) {
return rc;
}
UG_inode_fskit_common_init( fent, inode_data );
return 0;
}
// generate a new fskit entry for a regular file
// return 0 on success
// return -ENOMEM on OOM
// return -EINVAL if inode_data doesn't represent a file
// fent must be write-locked
static int UG_inode_fskit_file_init( struct fskit_entry* fent, struct md_entry* inode_data ) {
int rc = 0;
// sanity
if( inode_data->type != MD_ENTRY_FILE ) {
return -EINVAL;
}
rc = fskit_entry_init_file( fent, inode_data->file_id, inode_data->owner, inode_data->volume, inode_data->mode );
if( rc != 0 ) {
return rc;
}
UG_inode_fskit_common_init( fent, inode_data );
return 0;
}
// build an fskit entry from an exported inode
// return 0 on success
// return -ENOMEM on OOM
// fent must be write-locked
int UG_inode_fskit_entry_init( struct fskit_core* fs, struct fskit_entry* fent, struct fskit_entry* parent, struct md_entry* inode_data ) {
int rc = 0;
struct UG_inode* inode = NULL;
// going from file to directory?
if( inode_data->type == MD_ENTRY_FILE ) {
// turn the inode into a directory, and blow away the file's data
// set up the file
rc = UG_inode_fskit_file_init( fent, inode_data );
if( rc != 0 ) {
SG_error("UG_inode_fskit_file_init('%s' (%" PRIX64 ")) rc = %d\n", inode_data->name, inode_data->file_id, rc );
return rc;
}
}
else {
// going to make a directory, and replace this file.
// set up the directory
rc = UG_inode_fskit_dir_init( fent, parent, inode_data );
if( rc != 0 ) {
SG_error("UG_inode_fskit_dir_init('%s' (%" PRIX64 ")) rc = %d\n", inode_data->name, inode_data->file_id, rc );
return rc;
}
}
// build the inode
inode = UG_inode_alloc( 1 );
if( inode == NULL ) {
return -ENOMEM;
}
// set up the inode
rc = UG_inode_init_from_export( inode, inode_data, fent );
if( rc != 0 ) {
SG_error("UG_inode_init_from_export('%s' (%" PRIX64 ")) rc = %d\n", inode_data->name, inode_data->file_id, rc );
SG_manifest_free( &inode->manifest );
SG_safe_free( inode );
return rc;
}
// put the inode into the fent, and fent into inode
UG_inode_bind_fskit_entry( inode, fent );
return 0;
}
// free an inode
// NOTE: destroys its dirty blocks
// always succeeds
int UG_inode_free( struct UG_inode* inode ) {
SG_safe_free( inode->name );
SG_safe_delete( inode->sync_queue );
if( inode->dirty_blocks != NULL ) {
UG_dirty_block_map_free( inode->dirty_blocks );
SG_safe_delete( inode->dirty_blocks );
}
SG_manifest_free( &inode->manifest );
SG_manifest_free( &inode->replaced_blocks );
memset( inode, 0, sizeof(struct UG_inode) );
return 0;
}
// set up a file handle
// NOTE: inode->entry must be read-locked
// return 0 on success
// return -EINVAL if the inode is malformed (NOTE: indicates a bug!)
// return -ENOMEM on OOM
int UG_file_handle_init( struct UG_file_handle* fh, struct UG_inode* inode, int flags ) {
if( inode->entry == NULL ) {
return -EINVAL;
}
fh->evicts = SG_safe_new( UG_inode_block_eviction_map_t() );
if( fh->evicts == NULL ) {
return -ENOMEM;
}
fh->inode_ref = inode;
fh->flags = flags;
return 0;
}
// free a file handle
// return 0 on success
// return -ENOMEM on OOM
int UG_file_handle_free( struct UG_file_handle* fh ) {
SG_safe_delete( fh->evicts );
memset( fh, 0, sizeof(struct UG_file_handle) );
return 0;
}
// export all xattrs for an inode.
// return 0 on success, filling in *ret_xattr_names, *ret_xattr_values, and *ret_xattr_value_lengths if there are xattrs in this inode.
// return -ENOMEM on OOM
// NOTE: inode->entry must be read-locked
int UG_inode_export_xattrs( struct fskit_core* fs, struct UG_inode* inode, char*** ret_xattr_names, char*** ret_xattr_values, size_t** ret_xattr_value_lengths ) {
int rc = 0;
size_t num_xattrs = 0;
ssize_t xattr_list_len = 0;
char** xattr_names = NULL;
char** xattr_values = NULL;
size_t* xattr_value_lengths = NULL;
char* xattr_list = NULL;
char* xattr_ptr = NULL;
xattr_list_len = fskit_flistxattr( fs, inode->entry, NULL, 0 );
if( xattr_list_len < 0 ) {
SG_error("fskit_flistxattr(NULL) rc = %zd\n", xattr_list_len );
return (int)xattr_list_len;
}
if( xattr_list_len == 0 ) {
// no xattrs
*ret_xattr_names = NULL;
*ret_xattr_values = NULL;
*ret_xattr_value_lengths = NULL;
return 0;
}
// get the list of xattrs
xattr_list = SG_CALLOC( char, xattr_list_len );
if( xattr_list == NULL ) {
return -ENOMEM;
}
rc = fskit_flistxattr( fs, inode->entry, xattr_list, xattr_list_len );
if( rc < 0 ) {
SG_error( "fskit_flistxattr(%zd) rc = %d\n", xattr_list_len, rc );
return rc;
}
// how many xattrs?
for( ssize_t i = 0; i < xattr_list_len; i++ ) {
if( xattr_list[i] == '\0' ) {
num_xattrs++;
}
}
xattr_names = SG_CALLOC( char*, num_xattrs );
xattr_values = SG_CALLOC( char*, num_xattrs );
xattr_value_lengths = SG_CALLOC( size_t, num_xattrs );
if( xattr_names == NULL || xattr_values == NULL || xattr_value_lengths == NULL ) {
SG_safe_free( xattr_names );
SG_safe_free( xattr_values );
SG_safe_free( xattr_value_lengths );
SG_safe_free( xattr_list );
return -ENOMEM;
}
// get each xattr
xattr_ptr = xattr_list;
for( size_t i = 0; i < num_xattrs; i++ ) {
char* xattr_name = NULL;
char* xattr_value = NULL;
ssize_t xattr_value_len = 0;
xattr_name = SG_strdup_or_null( xattr_ptr );
if( xattr_name == NULL ) {
rc = -ENOMEM;
goto UG_inode_export_xattrs_fail;
}
xattr_value_len = fskit_fgetxattr( fs, inode->entry, xattr_name, NULL, 0 );
if( xattr_value_len < 0 ) {
rc = xattr_value_len;
SG_safe_free( xattr_name );
goto UG_inode_export_xattrs_fail;
}
xattr_value = SG_CALLOC( char, xattr_value_len );
if( xattr_value == NULL ) {
rc = -ENOMEM;
SG_safe_free( xattr_name );
goto UG_inode_export_xattrs_fail;
}
rc = fskit_fgetxattr( fs, inode->entry, xattr_name, xattr_value, xattr_value_len );
if( rc < 0 ) {
SG_safe_free( xattr_name );
SG_safe_free( xattr_value );
goto UG_inode_export_xattrs_fail;
}
xattr_names[i] = xattr_name;
xattr_values[i] = xattr_value;
xattr_value_lengths[i] = xattr_value_len;
}
*ret_xattr_names = xattr_names;
*ret_xattr_values = xattr_values;
*ret_xattr_value_lengths = xattr_value_lengths;
SG_safe_free( xattr_list );
return 0;
UG_inode_export_xattrs_fail:
SG_FREE_LIST( xattr_names, free );
SG_FREE_LIST( xattr_values, free );
SG_safe_free( xattr_value_lengths );
SG_safe_free( xattr_list );
return rc;
}
// calculate the xattr hash for an inode
// return 0 on success, and fill in xattr_hash (which must be SHA256_DIGEST_LENGTH bytes or more)
// return -ENOMEM on OOM
// return -EINVAL if we do not have any xattrs for this (i.e. we should only have xattrs if we're the coordintaor)
int UG_inode_export_xattr_hash( struct fskit_core* fs, uint64_t gateway_id, struct UG_inode* inode, unsigned char* xattr_hash ) {
int rc = 0;
char** xattr_names = NULL;
char** xattr_values = NULL;
size_t* xattr_lengths = NULL;
if( gateway_id != SG_manifest_get_coordinator( &inode->manifest ) ) {
SG_error("BUG: %" PRIu64 " != %" PRIu64 "\n", gateway_id, SG_manifest_get_coordinator( &inode->manifest ) );
exit(1);
return -EINVAL;
}
rc = UG_inode_export_xattrs( fs, inode, &xattr_names, &xattr_values, &xattr_lengths );
if( rc != 0 ) {
SG_error("UG_inode_export_xattrs(%" PRIX64 ") rc = %d\n", UG_inode_file_id( inode ), rc );
return rc;
}
memset( xattr_hash, 0, SHA256_DIGEST_LENGTH );
rc = ms_client_xattr_hash( xattr_hash, SG_manifest_get_volume_id( &inode->manifest ), UG_inode_file_id( inode ), inode->xattr_nonce, xattr_names, xattr_values, xattr_lengths );
SG_FREE_LIST( xattr_names, free );
SG_FREE_LIST( xattr_values, free );
SG_safe_free( xattr_lengths );
return rc;
}
// export an inode to an md_entry
// NOTE: does *not* set the xattr hash or signature
// return 0 on success
// return -ENOMEM on OOM
// NOTE: src->entry must be read-locked
int UG_inode_export( struct md_entry* dest, struct UG_inode* src, uint64_t parent_id ) {
// get type
int type = fskit_entry_get_type( src->entry );
memset( dest, 0, sizeof(struct md_entry) );
if( type == FSKIT_ENTRY_TYPE_FILE ) {
dest->type = MD_ENTRY_FILE;
}
else if( type == FSKIT_ENTRY_TYPE_DIR ) {
dest->type = MD_ENTRY_DIR;
}
else {
// invalid
return -EINVAL;
}
char* name = SG_strdup_or_null( src->name );
if( name == NULL ) {
return -ENOMEM;
}
dest->type = type;
dest->name = name;
dest->file_id = fskit_entry_get_file_id( src->entry );
fskit_entry_get_ctime( src->entry, &dest->ctime_sec, &dest->ctime_nsec );
fskit_entry_get_mtime( src->entry, &dest->mtime_sec, &dest->mtime_nsec );
if( type == FSKIT_ENTRY_TYPE_FILE ) {
SG_manifest_get_modtime( &src->manifest, &dest->manifest_mtime_sec, &dest->manifest_mtime_nsec );
}
else {
dest->manifest_mtime_sec = 0;
dest->manifest_mtime_nsec = 0;
}
dest->write_nonce = src->write_nonce;
dest->xattr_nonce = src->xattr_nonce;
dest->version = SG_manifest_get_file_version( &src->manifest );
dest->max_read_freshness = src->max_read_freshness;
dest->max_write_freshness = src->max_write_freshness;
dest->owner = fskit_entry_get_owner( src->entry );
dest->coordinator = SG_manifest_get_coordinator( &src->manifest );
dest->volume = SG_manifest_get_volume_id( &src->manifest );
dest->mode = fskit_entry_get_mode( src->entry );
dest->size = fskit_entry_get_size( src->entry );
dest->error = 0;
dest->generation = src->generation;
dest->num_children = src->ms_num_children;
dest->capacity = src->ms_capacity;
dest->parent_id = parent_id;
dest->xattr_hash = NULL;
dest->ent_sig = NULL;
dest->ent_sig_len = 0;
return 0;
}
// does an exported inode's type match the inode's type?
// return 1 if so
// return 0 if not
// NOTE: dest->entry must be read-locked
int UG_inode_export_match_type( struct UG_inode* dest, struct md_entry* src ) {
int type = fskit_entry_get_type( dest->entry );
bool valid_type = false;
if( type == FSKIT_ENTRY_TYPE_FILE && src->type == MD_ENTRY_FILE ) {
valid_type = true;
}
else if( type == FSKIT_ENTRY_TYPE_DIR && src->type == MD_ENTRY_DIR ) {
valid_type = true;
}
if( !valid_type ) {
return 0;
}
else {
return 1;
}
}
// does an exported inode's size match the inode's size?
// return 1 if so, 0 if not
// NOTE: dest->entry must be read-locked
int UG_inode_export_match_size( struct UG_inode* dest, struct md_entry* src ) {
// size matches?
if( fskit_entry_get_size( dest->entry ) != (unsigned)src->size ) {
return 0;
}
else {
return 1;
}
}
// does an exported inode's version match an inode's version?
// return 1 if so, 0 if not
// NOTE: dest->entry must be read-locked
int UG_inode_export_match_version( struct UG_inode* dest, struct md_entry* src ) {
// version matches?
if( UG_inode_file_version( dest ) != src->version ) {
return 0;
}
else {
return 1;
}
}
// does an exported inode's file ID match an inode's file ID?
// return 1 if so, 0 if not
// NOTE: dest->entry must be read-locked
int UG_inode_export_match_file_id( struct UG_inode* dest, struct md_entry* src ) {
// file ID matches?
if( fskit_entry_get_file_id( dest->entry ) != src->file_id ) {
return 0;
}
else {
return 1;
}
}
// does an exported inode's volume ID match an inode's volume ID?
// return 1 if so, 0 if not
// NOTE: dest->entry must be read-locked
int UG_inode_export_match_volume_id( struct UG_inode* dest, struct md_entry* src ) {
// file ID matches?
if( SG_manifest_get_volume_id( &dest->manifest ) != src->volume ) {
return 0;
}
else {
return 1;
}
}
// does an exported inode's name match the inode's name?
// return 1 if so, 0 if not
// return -ENOMEM on OOM
// NOTE: dest->entry must be read-locked
int UG_inode_export_match_name( struct UG_inode* dest, struct md_entry* src ) {
return (strcmp( dest->name, src->name ) == 0);
}
// import inode metadata from an md_entry
// inode must already be initialized
// NOTE: dest's type, file ID, version, name, and size must match src's, if dest has an associated entry.
// The caller must make sure of this out-of-band, since changing these requires some kind of I/O or directory structure clean-up
// return 0 on success
// return -EINVAL if the types, IDs, versions, sizes, or names don't match (and dest has entry data)
// return -EPERM if dest or src or dest->entry are NULL
// NOTE: dest->entry must be write-locked
int UG_inode_import( struct UG_inode* dest, struct md_entry* src ) {
if( src == NULL || dest == NULL ) {
SG_error("src == %p, dest == %p\n", src, dest );
return -EPERM;
}
if( dest->entry == NULL ) {
SG_error("dest->entry == %p\n", dest->entry );
return -EPERM;
}
if( !UG_inode_export_match_volume_id( dest, src ) ) {
SG_error("src->volume_id == %" PRIu64 ", dest->volume_id == %" PRIu64 "\n", src->volume, UG_inode_volume_id( dest ) );
return -EINVAL;
}
if( !UG_inode_export_match_file_id( dest, src ) ) {
SG_error("src->file_id == %" PRIX64 ", dest->file_id == %" PRIX64 "\n", src->file_id, UG_inode_file_id( dest ) );
return -EINVAL;
}
if( UG_inode_export_match_name( dest, src ) <= 0 ) {
SG_error("%" PRIX64 ": src->name == '%s', dest->name == '%s'\n", src->file_id, src->name, dest->name );
return -EINVAL;
}
if( !UG_inode_export_match_size( dest, src ) ) {
SG_error("%" PRIX64 ": src->size == %zu, dest->size == %zu\n", src->file_id, src->size, fskit_entry_get_size( dest->entry ) );
return -EINVAL;
}
if( !UG_inode_export_match_type( dest, src ) ) {
SG_error("%" PRIX64 ": src->type == %d, dest->type == %d\n", src->file_id, src->type, fskit_entry_get_type( dest->entry ) );
return -EINVAL;
}
if( !UG_inode_export_match_version( dest, src ) ) {
SG_error("%" PRIX64 ": src->version = %" PRId64 ", dest->version = %" PRId64 "\n", src->version, src->version, UG_inode_file_version( dest ) );
return -EINVAL;
}
struct timespec ts;
// looks good!
ts.tv_sec = src->ctime_sec;
ts.tv_nsec = src->ctime_nsec;
fskit_entry_set_ctime( dest->entry, &ts );
ts.tv_sec = src->mtime_sec;
ts.tv_nsec = src->mtime_nsec;
fskit_entry_set_mtime( dest->entry, &ts );
dest->ms_write_nonce = src->write_nonce;
dest->ms_xattr_nonce = src->xattr_nonce;
SG_manifest_set_coordinator_id( &dest->manifest, src->coordinator );
SG_manifest_set_owner_id( &dest->manifest, src->owner );
dest->max_read_freshness = src->max_read_freshness;
dest->max_write_freshness = src->max_write_freshness;
fskit_entry_set_owner_and_group( dest->entry, src->owner, src->volume );
fskit_entry_set_mode( dest->entry, src->mode );
dest->generation = src->generation;
dest->ms_num_children = src->num_children;
dest->ms_capacity = src->capacity;
if( src->xattr_hash != NULL ) {
memcpy( dest->ms_xattr_hash, src->xattr_hash, SHA256_DIGEST_LENGTH );
}
else {
memset( dest->ms_xattr_hash, 0, SHA256_DIGEST_LENGTH );
}
return 0;
}
// create or mkdir--publish metadata, set up an fskit entry, and allocate its inode
// return 0 on success
// return -errno on failure (i.e. it exists, we don't have permission, we get a network error, etc.)
// NOTE: fent will be write-locked by fskit
// NOTE: for files, this will disable truncate (so the subsequent trunc(2) that follows a creat(2) does not incur an extra round-trip)
int UG_inode_publish( struct SG_gateway* gateway, struct fskit_entry* fent, struct md_entry* ent_data, struct UG_inode** ret_inode_data ) {
int rc = 0;
struct ms_client* ms = SG_gateway_ms( gateway );
char const* method_name = NULL;
bool is_mkdir = (ent_data->type == MD_ENTRY_DIR);
// inode data
struct UG_inode* inode = NULL;
struct md_entry inode_data_out;
memset( &inode_data_out, 0, sizeof(struct md_entry) );
// make the request
if( is_mkdir ) {
method_name = "ms_client_mkdir";
rc = ms_client_mkdir( ms, &inode_data_out, ent_data );
}
else {
method_name = "ms_client_create";
rc = ms_client_create( ms, &inode_data_out, ent_data );
}
if( rc != 0 ) {
SG_error("%s rc = %d\n", method_name, rc );
md_entry_free( &inode_data_out );
return rc;
}
// update the child with the new inode number
fskit_entry_set_file_id( fent, inode_data_out.file_id );
fskit_entry_set_mode( fent, inode_data_out.mode );
fskit_entry_set_owner( fent, inode_data_out.owner );
// success! create the inode data
inode = UG_inode_alloc( 1 );
if( inode == NULL ) {
return -ENOMEM;
}
rc = UG_inode_init( inode, ent_data->name, fent, ms_client_get_volume_id( ms ), SG_gateway_id( gateway ), inode_data_out.version );
if( rc != 0 ) {
SG_error("UG_inode_init rc = %d\n", rc );
SG_safe_free( inode );
md_entry_free( &inode_data_out );
return rc;
}
UG_inode_set_write_nonce( inode, inode_data_out.write_nonce );
UG_inode_set_max_read_freshness( inode, inode_data_out.max_read_freshness );
UG_inode_set_max_write_freshness( inode, inode_data_out.max_write_freshness );
SG_manifest_set_coordinator_id( UG_inode_manifest( inode ), inode_data_out.coordinator );
// NOTE: should be equal to file's modtime
SG_manifest_set_modtime( UG_inode_manifest( inode ), ent_data->manifest_mtime_sec, ent_data->manifest_mtime_nsec );
// success!
*ret_inode_data = inode;
md_entry_free( &inode_data_out );
UG_inode_bind_fskit_entry( inode, fent );
// mark as creating, so the following trunc(2) call will avoid an extra network round-trip
UG_inode_set_creating( inode, true );
return 0;
}
// does an inode's manifest have a more recent modtime than the given one?
// return true if so; false if not
bool UG_inode_manifest_is_newer_than( struct SG_manifest* manifest, int64_t mtime_sec, int32_t mtime_nsec ) {
struct timespec old_manifest_ts;
struct timespec new_manifest_ts;
new_manifest_ts.tv_sec = mtime_sec;
new_manifest_ts.tv_nsec = mtime_nsec;
old_manifest_ts.tv_sec = SG_manifest_get_modtime_sec( manifest );
old_manifest_ts.tv_nsec = SG_manifest_get_modtime_nsec( manifest );
bool newer = (md_timespec_diff( &new_manifest_ts, &old_manifest_ts ) > 0);
return newer;
}
// merge new manifest block data into an inode's manifest (i.e. from reloading it remotely, or handling a remote write).
// evict now-stale cached data and overwritten dirty blocks.
// remove now-invalid garbage block data.
// return 0 on success, and populate the inode's manifest with the given manifest's block data
// return -ENOMEM on OOM
// NOTE: inode->entry must be write-locked
// NOTE: this method is idempotent, and will partially-succeed if it returns -ENOMEM. Callers are encouraged to try and retry until it succeeds
// NOTE: this method is a commutative and associative on manifests--given manifests A, B, and C, doesn't matter what order they get merged
// NOTE: (i.e. merge( merge(A, B), C ) == merge( A, merge( B, C ) ) and merge( A, B ) == merge( B, A ))
// NOTE: does *NOT* merge size, does *NOT* merge modtime, and does *NOT* attempt to truncate
int UG_inode_manifest_merge_blocks( struct SG_gateway* gateway, struct UG_inode* inode, struct SG_manifest* new_manifest ) {
int rc = 0;
struct md_syndicate_cache* cache = SG_gateway_cache( gateway );
bool replace; // set to true if we will replace blocks in the manifest; false if not (i.e. depends on whether the "new_manifest" is actually newer)
// i.e. if our manifest is newer than the "new" manifest, then don't replace blocks on conflict
replace = !UG_inode_manifest_is_newer_than( UG_inode_manifest( inode ), SG_manifest_get_modtime_sec( new_manifest ), SG_manifest_get_modtime_nsec( new_manifest ) );
// add all blocks in new_manifest
for( SG_manifest_block_iterator itr = SG_manifest_block_iterator_begin( new_manifest ); itr != SG_manifest_block_iterator_end( new_manifest ); itr++ ) {
uint64_t block_id = SG_manifest_block_iterator_id( itr );
struct SG_manifest_block* new_block = SG_manifest_block_iterator_block( itr );
UG_dirty_block_map_t::iterator dirty_block_itr;
struct UG_dirty_block* dirty_block = NULL;
struct SG_manifest_block* replaced_block = NULL;
struct SG_manifest_block* existing_block = SG_manifest_block_lookup( UG_inode_manifest( inode ), block_id );
int64_t existing_block_version = 0;
if( existing_block != NULL ) {
if( SG_manifest_block_version( existing_block ) == SG_manifest_block_version( new_block ) ) {
// already merged, or no change