This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.c
1534 lines (1379 loc) · 51.5 KB
/
file.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) 2015 The Android Open Source Project
*
* 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 <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "block_allocator.h"
#include "block_map.h"
#include "crypt.h"
#include "debug.h"
#include "file.h"
#include "transaction.h"
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define BIT_MASK(x) (((x) >= sizeof(unsigned long long ) * 8) ? (0ULL-1) : ((1ULL << (x)) - 1))
#define FILE_ENTRY_MAGIC (0x0066797473757274) /* trustyf\0 */
/**
* struct file_entry - On-disk file entry
* @iv: initial value used for encrypt/decrypt
* @magic: FILE_ENTRY_MAGIC.
* @block_map: Block and mac of file block map root node.
* @size: File size in bytes.
* @reserved: Reserved for future use. Write 0, read ignore.
* @path: File path and name.
*/
struct file_entry {
struct iv iv;
uint64_t magic;
struct block_mac block_map;
struct file_info info;
};
static bool file_tree_lookup(struct block_mac *block_mac_out,
struct transaction *tr,
struct block_tree *tree,
struct block_tree_path *tree_path,
const char *file_path,
bool remove);
/**
* path_hash - Convert a path string to hash value
* @tr: Transaction object.
* @path: String to calculate hash for.
*
* Return: non-zero hash value that fits in @tr->fs->block_num_size bytes
* computed from @path
*/
static data_block_t path_hash(struct transaction *tr, const char *path)
{
data_block_t hash = str_hash(path);
hash &= BIT_MASK(tr->fs->block_num_size * 8);
if (!hash) {
hash++; /* 0 key is not supported by block_tree */
}
return hash;
}
/**
* file_block_map_init - Initialize in-memory block map state from file entry
* @tr: Transaction object.
* @block_map: Block map object.
* @file: Block number and mac of file entry.
*
* Load file entry at @file and initialize @block_map with root node stored
* there.
*/
void file_block_map_init(struct transaction *tr,
struct block_map *block_map,
const struct block_mac *file)
{
const struct file_entry *file_entry_ro;
obj_ref_t file_entry_ro_ref = OBJ_REF_INITIAL_VALUE(file_entry_ro_ref);
if (!block_mac_valid(tr, file)) {
goto err;
}
file_entry_ro = block_get(tr, file, NULL, &file_entry_ro_ref);
if (!file_entry_ro) {
goto err;
}
assert(file_entry_ro);
block_map_init(tr, block_map, &file_entry_ro->block_map,
tr->fs->dev->block_size);
block_put(file_entry_ro, &file_entry_ro_ref);
return;
err:
if (tr->failed) {
pr_warn("can't read file entry at %lld, transaction failed\n",
block_mac_to_block(tr, file));
} else {
pr_err("can't read file entry at %lld\n", block_mac_to_block(tr, file));
transaction_fail(tr);
}
}
/**
* file_print - Print information about a file
* @tr: Transaction object.
* @file: File handle object.
*/
void file_print(struct transaction *tr, const struct file_handle *file)
{
const struct file_entry *file_entry_ro;
obj_ref_t file_entry_ro_ref = OBJ_REF_INITIAL_VALUE(file_entry_ro_ref);
struct block_map block_map;
file_block_map_init(tr, &block_map, &file->block_mac);
file_entry_ro = block_get(tr, &file->block_mac, NULL, &file_entry_ro_ref);
if (!file_entry_ro) {
printf("can't read file entry at %lld\n",
block_mac_to_block(tr, &file->block_mac));
return;
}
assert(file_entry_ro);
printf("file at %lld, path %s, size %lld, hash 0x%llx, block map (tree)\n",
block_mac_to_block(tr, &file->block_mac), file_entry_ro->info.path,
file_entry_ro->info.size, path_hash(tr, file_entry_ro->info.path));
block_put(file_entry_ro, &file_entry_ro_ref);
block_tree_print(tr, &block_map.tree);
}
/**
* files_print - Print information about all committed files
* @tr: Transaction object.
*/
void files_print(struct transaction *tr)
{
struct block_tree_path path;
struct file_handle file;
printf("%s: files:\n", __func__);
block_tree_print(tr, &tr->fs->files);
block_tree_walk(tr, &tr->fs->files, 0, true, &path);
while (block_tree_path_get_key(&path)) {
file.block_mac = block_tree_path_get_data_block_mac(&path);
file_print(tr, &file);
block_tree_path_next(&path);
}
}
/**
* file_block_map_update - Update file entry with block_map or size changes
* @tr: Transaction object.
* @block_map: Block map object.
* @file: File handle object.
*
*/
static void file_block_map_update(struct transaction *tr,
struct block_map *block_map,
const char *new_path,
struct file_handle *file)
{
bool found;
data_block_t old_block;
data_block_t file_block;
data_block_t new_block;
struct block_mac block_mac;
const struct file_entry *file_entry_ro;
struct file_entry *file_entry_rw = NULL;
obj_ref_t file_entry_ref = OBJ_REF_INITIAL_VALUE(file_entry_ref);
obj_ref_t file_entry_copy_ref = OBJ_REF_INITIAL_VALUE(file_entry_ref);
struct block_tree_path tree_path;
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return;
}
assert(block_mac_valid(tr, &file->block_mac));
file_entry_ro = block_get(tr, &file->block_mac, NULL, &file_entry_ref);
if (!file_entry_ro) {
assert(tr->failed);
pr_warn("transaction failed, abort\n");
return;
}
assert(file_entry_ro);
found = file_tree_lookup(&block_mac, tr, &tr->files_added, &tree_path,
file_entry_ro->info.path, !!new_path);
if (!found) {
found = file_tree_lookup(&block_mac, tr, &tr->fs->files, &tree_path,
file_entry_ro->info.path, false);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
assert(found);
if (new_path) {
block_tree_insert_block_mac(tr, &tr->files_removed,
block_mac_to_block(tr, &block_mac),
block_mac);
}
old_block = block_mac_to_block(tr, &block_mac);
file_block = block_mac_to_block(tr, &file->block_mac);
if (transaction_block_need_copy(tr, file_block)) {
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
assert((block_map && block_map->tree.root_block_changed) ||
file->size != file_entry_ro->info.size ||
new_path);
assert(old_block == file_block);
new_block = block_allocate(tr);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
assert(new_block);
block_mac_set_block(tr, &block_mac, new_block);
pr_write("copy file block %lld -> %lld\n", file_block, new_block);
/*
* Use block_get_copy instead of block_move since tr->fs->files
* is still used and not updated until file_transaction_complete.
*/
file_entry_rw = block_get_copy(tr, file_entry_ro, new_block,
false, &file_entry_copy_ref); // TODO: fix
block_put(file_entry_ro, &file_entry_ref);
file_entry_ro = file_entry_rw;
obj_ref_transfer(&file_entry_ref, &file_entry_copy_ref);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
block_tree_insert(tr, &tr->files_updated, file_block, new_block); /* TODO: insert mac */
block_free(tr, file_block);
file->block_mac = block_mac;
}
assert(!file_tree_lookup(NULL, tr, &tr->files_added, &tree_path,
file_entry_ro->info.path, false));
block_tree_walk(tr, &tr->files_updated, old_block, false, &tree_path); /* TODO: get path from insert operation */
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
assert(block_tree_path_get_key(&tree_path) == old_block);
block_mac = block_tree_path_get_data_block_mac(&tree_path);
assert(block_mac_to_block(tr, &block_mac) == block_mac_to_block(tr, &file->block_mac));
//assert(block_mac_eq(tr, &block_mac, &file->block_mac)); /* TODO: enable after insert mac TODO */
if (new_path) {
/* TODO: remove by path or, when possible, skip insert instead */
block_tree_remove(tr, &tr->files_updated, old_block, block_mac_to_block(tr, &block_mac));
}
}
if (!file_entry_rw) {
file_entry_rw = block_dirty(tr, file_entry_ro, false);
}
if (block_map) {
pr_write("file at %lld: update block_map %lld -> %lld\n",
block_mac_to_block(tr, &block_mac),
block_mac_to_block(tr, &file_entry_rw->block_map),
block_mac_to_block(tr, &block_map->tree.root));
file_entry_rw->block_map = block_map->tree.root;
}
file_entry_rw->info.size = file->size;
if (new_path) {
strcpy(file_entry_rw->info.path, new_path);
block_put_dirty(tr, file_entry_rw, &file_entry_ref, &block_mac, NULL);
block_tree_insert_block_mac(tr, &tr->files_added, path_hash(tr, new_path), block_mac);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
block_mac_copy(tr, &file->block_mac, &block_mac);
} else {
block_tree_path_put_dirty(tr, &tree_path, tree_path.count,
file_entry_rw, &file_entry_ref);
file->block_mac = tree_path.entry[tree_path.count].block_mac; /* TODO: add better api */
}
/*
* Move to head of list so opening the same file twice in a transaction
* commits the changes to the file_handle that was modified. Writing to
* both handles is not currently supported.
*/
list_delete(&file->node);
list_add_head(&tr->open_files, &file->node);
return;
err:
if (file_entry_rw) {
block_put_dirty_discard(file_entry_rw, &file_entry_ref);
} else {
block_put(file_entry_ro, &file_entry_ref);
}
}
/**
* get_file_block_size - Get file data block size
* @fs: File system state object.
*
* Return: Get size of data block returned by file_get_block and
* file_get_block_write for @fs.
*/
size_t get_file_block_size(struct fs *fs)
{
return fs->dev->block_size - sizeof(struct iv);
}
/**
* file_get_block_etc - Helper function to get a file block for read or write
* @tr: Transaction object.
* @file: File handle object.
* @file_block: File block number. 0 based.
* @read: %true if data should be read from disk, %false otherwise.
* @write: %true if returned data should be writeable, %false otherwise.
* @ref: Pointer to store reference in.
*
* Return: Const block data pointer, or NULL if no valid block is found at
* @file_block or if @write is %true and a block could not be allocated.
*/
static const void *file_get_block_etc(struct transaction *tr,
struct file_handle *file,
data_block_t file_block,
bool read,
bool write,
obj_ref_t *ref)
{
bool found = false;
const void *data = NULL;
struct block_map block_map;
struct block_mac block_mac;
data_block_t old_disk_block;
data_block_t new_block;
bool dirty = false;
if (tr->failed) {
pr_warn("transaction failed, ignore\n");
goto err;
}
file_block_map_init(tr, &block_map, &file->block_mac);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
file->used_by_tr = true;
found = block_map_get(tr, &block_map, file_block, &block_mac);
if (found) {
if (read) {
data = block_get(tr, &block_mac, NULL, ref); /* TODO: pass iv? */
} else {
data = block_get_no_read(tr, block_mac_to_block(tr, &block_mac), ref);
}
/* TODO: handle mac mismatch better */
}
old_disk_block = found ? block_mac_to_block(tr, &block_mac) : 0;
if (write && (!found || transaction_block_need_copy(tr, old_disk_block))) {
new_block = block_allocate(tr);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
assert(new_block);
block_mac_set_block(tr, &block_mac, new_block);
if (found) {
data = block_move(tr, data, new_block, false);
dirty = true;
assert(!tr->failed);
block_free(tr, old_disk_block);
} else {
if (read) {
assert(write);
data = block_get_cleared(tr, new_block, false, ref);
dirty = true;
} else {
data = block_get_no_read(tr, new_block, ref);
}
}
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
/* TODO: get new mac */
assert(!tr->failed);
block_map_set(tr, &block_map, file_block, &block_mac);
file_block_map_update(tr, &block_map, NULL, file);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
}
if (write && !dirty) {
data = block_dirty(tr, data, false);
}
if (!data) {
return NULL;
}
return data + sizeof(struct iv);
err:
if (dirty) {
block_put_dirty_discard((void *)data, ref);
} else if (data) {
block_put(data, ref);
}
return NULL;
}
/**
* file_get_block - Get a file block for read
* @tr: Transaction object.
* @file: File handle object.
* @file_block: File block number. 0 based.
* @ref: Pointer to store reference in.
*
* Return: Const block data pointer, or NULL if no valid block is found at
* @file_block.
*/
const void *file_get_block(struct transaction *tr, struct file_handle *file,
data_block_t file_block, obj_ref_t *ref)
{
return file_get_block_etc(tr, file, file_block, true, false, ref);
}
/**
* file_get_block_write - Helper function to get a file block for write
* @tr: Transaction object.
* @file: File handle object.
* @file_block: File block number. 0 based.
* @read: %true if data should be read from disk, %false otherwise.
* @ref: Pointer to store reference in.
*
* Return: Block data pointer, or NULL if a block could not be allocated.
*/
void *file_get_block_write(struct transaction *tr, struct file_handle *file,
data_block_t file_block, bool read, obj_ref_t *ref)
{
return (void *)file_get_block_etc(tr, file, file_block, read, true, ref);
}
/**
* file_block_put - Release reference to a block returned by file_get_block
* @data: File block data pointer
* @data_ref: Reference pointer to release
*/
void file_block_put(const void *data, obj_ref_t *data_ref)
{
block_put(data - sizeof(struct iv), data_ref);
}
/**
* file_block_put_dirty - Release reference to a dirty file block
* @tr: Transaction
* @file: File handle object.
* @file_block: File block number. 0 based.
* @data: File block data pointer
* @data_ref: Reference pointer to release
*
* Release reference to a file block previously returned by
* file_get_block_write, and update mac value in block_mac.
*/
void file_block_put_dirty(struct transaction *tr,
struct file_handle *file, data_block_t file_block,
void *data, obj_ref_t *data_ref)
{
struct block_map block_map;
file_block_map_init(tr, &block_map, &file->block_mac);
block_map_put_dirty(tr, &block_map, file_block,
data - sizeof(struct iv), data_ref);
file_block_map_update(tr, &block_map, NULL, file);
}
/**
* file_get_info - Get a file informaion for read
* @tr: Transaction object.
* @block_mac: File entry block_mac.
* @ref: Pointer to store reference in.
*
* Return: Const file info pointer, or NULL if no valid block is found at
* @block_mac.
*/
const struct file_info *file_get_info(struct transaction *tr,
const struct block_mac *block_mac,
obj_ref_t *ref)
{
const struct file_entry *file_entry;
file_entry = block_get(tr, block_mac, NULL, ref);
if (!file_entry) {
assert(tr->failed);
pr_warn("transaction failed, abort\n");
return NULL;
}
assert(file_entry);
assert(file_entry->magic == FILE_ENTRY_MAGIC);
return &file_entry->info;
}
/**
* file_info_put - Release reference to a block returned by file_get_info
* @data: File info data pointer
* @data_ref: Reference pointer to release
*/
void file_info_put(const struct file_info *data, obj_ref_t *data_ref)
{
const struct file_entry *entry = containerof(data, struct file_entry, info);
assert(entry->magic == FILE_ENTRY_MAGIC);
block_put(entry, data_ref);
}
/**
* file_get_size - Get file size
* @tr: Transaction
* @file: File handle object.
* @size: Pointer to return size in.
*
* Return: %true if @size was set, %false if @size was not set because @file is
* invalid or @tr has failed.
*/
bool file_get_size(struct transaction *tr,
struct file_handle *file,
data_block_t *size)
{
if (tr->failed) {
pr_warn("transaction failed, ignore\n");
return false;
}
if (!block_mac_valid(tr, &file->block_mac)) {
pr_warn("invalid file handle\n");
return false;
}
file->used_by_tr = true;
*size = file->size;
return true;
}
/**
* file_set_size - Set file size
* @tr: Transaction
* @file: File handle object.
* @size: New file size.
*
* Set file size and free blocks that are not longer needed. Does not clear any
* partial block data.
*/
void file_set_size(struct transaction *tr,
struct file_handle *file,
data_block_t size)
{
data_block_t file_block;
struct block_map block_map;
size_t file_block_size = get_file_block_size(tr->fs);
if (tr->failed) {
pr_warn("transaction failed, ignore\n");
return;
}
file_block_map_init(tr, &block_map, &file->block_mac);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return;
}
if (size == file->size) {
return;
}
if (size < file->size) {
file_block = DIV_ROUND_UP(size, file_block_size);
block_map_truncate(tr, &block_map, file_block);
}
file->size = size;
file_block_map_update(tr, &block_map, NULL, file);
}
/**
* file_tree_lookup - Helper function to search for a file in a specific tree
* @block_mac_out: Block-mac object to return block number and mac in.
* @tr: Transaction object.
* @tree: Tree object.
* @tree_path: Tree path object.
* @file_path: File path string.
* @remove: %true if found entry should be removed, %false otherwise.
*
* Return: %true if @file_path was found in @tree, %false otherwise.
*/
static bool file_tree_lookup(struct block_mac *block_mac_out,
struct transaction *tr,
struct block_tree *tree,
struct block_tree_path *tree_path,
const char *file_path,
bool remove)
{
struct block_mac block_mac;
data_block_t hash = path_hash(tr, file_path);
const struct file_entry *file_entry;
obj_ref_t file_entry_ref = OBJ_REF_INITIAL_VALUE(file_entry_ref);
bool found = false;
assert(strlen(file_path) < sizeof(file_entry->info.path));
assert(sizeof(*file_entry) <= tr->fs->dev->block_size);
block_tree_walk(tr, tree, hash - 1, false, tree_path);
while (block_tree_path_get_key(tree_path) && block_tree_path_get_key(tree_path) < hash) {
block_tree_path_next(tree_path);
}
while (block_tree_path_get_key(tree_path) == hash) {
block_mac = block_tree_path_get_data_block_mac(tree_path);
if (!block_mac_to_block(tr, &block_mac)) {
if (LOCAL_TRACE >= TRACE_LEVEL_WARNING) {
pr_warn("got 0 block pointer for hash %lld while looking for %s, hash 0x%llx\n",
block_tree_path_get_key(tree_path), file_path, hash);
block_tree_print(tr, tree);
}
block_tree_path_next(tree_path);
block_mac = block_tree_path_get_data_block_mac(tree_path);
pr_warn("next %lld, hash 0x%llx\n",
block_mac_to_block(tr, &block_mac),
block_tree_path_get_key(tree_path));
}
if (tr->failed) {
pr_warn("transaction failed, abort\n");
return false;
}
assert(block_mac_to_block(tr, &block_mac));
file_entry = block_get(tr, &block_mac, NULL, &file_entry_ref);
if (!file_entry) {
assert(tr->failed);
pr_warn("transaction failed, abort\n");
return false;
}
assert(file_entry);
assert(file_entry->magic == FILE_ENTRY_MAGIC);
found = !strcmp(file_path, file_entry->info.path);
pr_read("block %lld, hash 0x%llx match, found %d\n",
block_mac_to_block(tr, &block_mac), hash, found);
block_put(file_entry, &file_entry_ref);
if (found) {
if (remove) {
//block_tree_remove_path(tr, tree_path); /* TODO: remove by path */
block_tree_remove(tr, tree, hash, block_mac_to_block(tr, &block_mac)); /* TODO: pass mac */
}
*block_mac_out = block_mac;
return true;
}
block_tree_path_next(tree_path);
}
if (LOCAL_TRACE >= TRACE_LEVEL_READ) {
pr_read("%s: hash 0x%llx does not match 0x%llx\n",
__func__, hash, block_tree_path_get_key(tree_path));
block_tree_print(tr, tree);
}
return false;
}
/**
* file_create - Helper function to create a new file
* @block_mac_out: Block-mac object to return block number and mac in.
* @tr: Transaction object.
* @path: File path string.
*
* Create a new file and insert it into &tr->files_added. Caller must make
* a file matching @path does not already exist.
*
* Return: %true if file was created, %false otherwise.
*/
static bool file_create(struct block_mac *block_mac_out,
struct transaction *tr,
const char *path)
{
data_block_t block;
struct file_entry *file_entry;
obj_ref_t file_entry_ref = OBJ_REF_INITIAL_VALUE(file_entry_ref);
data_block_t hash;
hash = path_hash(tr, path);
block = block_allocate(tr);
pr_write("create file, %s, hash 0x%llx, at block %lld\n",
path, hash, block);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
assert(block);
block_mac_set_block(tr, block_mac_out, block);
file_entry = block_get_cleared(tr, block, false, &file_entry_ref);
assert(file_entry);
file_entry->magic = FILE_ENTRY_MAGIC;
strcpy(file_entry->info.path, path);
block_put_dirty(tr, file_entry, &file_entry_ref, block_mac_out, NULL);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
block_tree_insert_block_mac(tr, &tr->files_added, hash, *block_mac_out);
if (tr->failed) {
pr_warn("transaction failed, abort\n");
goto err;
}
return true;
err:
return false;
}
/**
* file_is_removed - Helper function to check if transaction deletes file
* @tr: Transaction object.
* @block: Block number for file.
*
* Return: %true if file is removed in @tr, %false otherwise.
*/
static bool file_is_removed(struct transaction *tr, data_block_t block)
{
struct block_tree_path tree_path;
block_tree_walk(tr, &tr->files_removed, block, false, &tree_path);
return block_tree_path_get_key(&tree_path) == block;
}
/**
* file_check_updated - Helper function to check if transaction updated file
* @tr: Transaction object.
* @committed_block_mac: Block-mac object to check.
* @block_mac_out: Block-mac object to return block number and mac in
* for the current state of the file in @tr.
*
* Return: %true if file is updated in @tr, %false otherwise.
*/
static bool file_check_updated(struct transaction *tr,
const struct block_mac *committed_block_mac,
struct block_mac *block_mac_out)
{
struct block_tree_path tree_path;
data_block_t block = block_mac_to_block(tr, committed_block_mac);
block_tree_walk(tr, &tr->files_updated, block, false, &tree_path);
if (block_tree_path_get_key(&tree_path) == block) {
pr_read("%lld, already updated in this transaction, use new copy %lld\n",
block, block_tree_path_get_data(&tree_path));
*block_mac_out = block_tree_path_get_data_block_mac(&tree_path);
return true;
}
return false;
}
/**
* file_lookup_not_removed - Helper function to search for a file
* @block_mac_out: Block-mac object to return block number and mac
* in for the current state of the file.
* @committed_block_mac_out: Block-mac object to return block number and mac
* in for the committed state of the file.
* @tr: Transaction object.
* @tree_path: Tree path object.
* @file_path: File path string.
*
* Helper function to search for a file that existed before @tr was activated.
*
* Return: %true if @file_path was found in @tr->fs->files but not found in
* @tr->files_removed, %false otherwise.
*/
static bool file_lookup_not_removed(struct block_mac *block_mac_out,
struct block_mac *committed_block_mac_out,
struct transaction *tr,
struct block_tree_path *tree_path,
const char *file_path)
{
bool found;
struct block_mac block_mac;
found = file_tree_lookup(&block_mac, tr, &tr->fs->files, tree_path,
file_path, false);
if (!found || file_is_removed(tr, block_mac_to_block(tr, &block_mac))) {
if (found) {
pr_read("file %s, %lld in removed\n",
file_path, block_mac_to_block(tr, &block_mac));
} else {
pr_read("file %s not in tr->fs->files\n", file_path);
}
return false;
}
*committed_block_mac_out = block_mac;
block_tree_walk(tr, &tr->files_updated, block_mac_to_block(tr, &block_mac),
false, tree_path);
if (block_tree_path_get_key(tree_path) == block_mac_to_block(tr, &block_mac)) {
pr_read("file %s, %lld, already updated in this transaction, use new copy %lld\n",
file_path, block_mac_to_block(tr, &block_mac),
block_tree_path_get_data(tree_path));
block_mac = block_tree_path_get_data_block_mac(tree_path);
}
pr_read("file %s, %lld, found in tr->fs->files\n",
file_path, block_mac_to_block(tr, &block_mac));
/*
* TODO: mark file (or blocks) in_use so other writers to the same file
* will cause this transaction to fail. Currently conflicts are only
* detected after each transaction writes to the file.
*/
*block_mac_out = block_mac;
return true;
}
/**
* file_find_open - Check if transaction has file open and return it
* @tr: Transaction object.
* @block_mac: Current block and mac value for open file.
*
* Return: file handle of open file if file is open in @tr, NULL otherwise.
*/
static struct file_handle *file_find_open(struct transaction *tr,
const struct block_mac *block_mac)
{
struct file_handle *file;
list_for_every_entry(&tr->open_files, file, struct file_handle, node) {
if (block_mac_same_block(tr, &file->block_mac, block_mac)) {
return file;
}
}
return NULL;
}
/**
* file_iterate - Iterate over all files
* @tr: Transaction object.
* @start_path: First file to not iterate over.
* @added: %false to iterate over committed files, %true to iterate over
* uncommitted files added by @tr.
* @state: client state object containing function to call for each file.
*/
bool file_iterate(struct transaction *tr, const char *start_path, bool added,
struct file_iterate_state *state)
{
struct block_tree_path tree_path;
struct block_mac block_mac;
struct block_tree *tree = added ? &tr->files_added : &tr->fs->files;
bool found;
bool stop;
bool removed = false;
if (start_path == NULL) {
block_tree_walk(tr, tree, 0, true, &tree_path);
} else {
found = file_tree_lookup(&block_mac, tr, tree, &tree_path,
start_path, false);
if (!found) {
return false;
}
block_tree_path_next(&tree_path);
}
while (block_tree_path_get_key(&tree_path)) {
block_mac = block_tree_path_get_data_block_mac(&tree_path);
if (!added) {
removed = file_is_removed(tr, block_mac_to_block(tr, &block_mac));
file_check_updated(tr, &block_mac, &block_mac);
}
stop = state->file(state, tr, &block_mac, added, removed);
if (stop) {
return true;
}
block_tree_path_next(&tree_path);
}
return true;
}
/**
* file_open - Open file
* @tr: Transaction object.
* @path: Path to find or create file at.
* @file: File handle object.
* @create: FILE_OPEN_NO_CREATE, FILE_OPEN_CREATE or
* FILE_OPEN_CREATE_EXCLUSIVE.
*
* Return: %true if file was opened, %false if file could not be opeened.
*/
bool file_open(struct transaction *tr, const char *path,
struct file_handle *file, enum file_create_mode create)
{
bool found;
struct block_mac block_mac;
struct block_mac committed_block_mac = BLOCK_MAC_INITIAL_VALUE(committed_block_mac);
struct block_tree_path tree_path;
const struct file_entry *file_entry_ro;
obj_ref_t file_entry_ref = OBJ_REF_INITIAL_VALUE(file_entry_ref);
assert(tr->fs);
found = file_tree_lookup(&block_mac, tr, &tr->files_added, &tree_path,
path, false);
if (found) {
goto found;
}
found = file_lookup_not_removed(&block_mac, &committed_block_mac,
tr, &tree_path, path);
if (found) {
goto found;
}
if (create != FILE_OPEN_NO_CREATE) {
found = file_create(&block_mac, tr, path);
}
if (found) {
goto created;
}
return false;
found:
if (create == FILE_OPEN_CREATE_EXCLUSIVE) {
return false;
}
if (file_find_open(tr, &block_mac)) {
pr_warn("%s already open\n", path);
return false;
}
created:
file_entry_ro = block_get(tr, &block_mac, NULL, &file_entry_ref);
if (!file_entry_ro) {
assert(tr->failed);
pr_warn("transaction failed, abort\n");
return false;
}
assert(file_entry_ro);
list_add_head(&tr->open_files, &file->node);
file->to_commit_block_mac = committed_block_mac;
file->committed_block_mac = committed_block_mac;
file->block_mac = block_mac;
file->size = file_entry_ro->info.size;
file->used_by_tr = false;
block_put(file_entry_ro, &file_entry_ref);
return true;
}
/**
* file_close - Close file
* @file: File handle object.
*
* Must be called before freeing @file after a successful file_open call.
*/
void file_close(struct file_handle *file)
{
list_delete(&file->node);
}
/**
* file_delete - Delete file
* @tr: Transaction object.
* @path: Path to find file at.
*
* Return: %true if file was found, %false otherwise.
*/