-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileops_moneta.c
924 lines (706 loc) · 25.8 KB
/
fileops_moneta.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
// a module which wraps the Moneta interface to standard Posix
#include "nv_common.h"
typedef size_t offset_t;
#include "Moneta.h"
#define ENV_MONETA_FOP "NVP_MONETA_FOP"
#define CHECK_ALIGNMENT 0
BOOST_PP_SEQ_FOR_EACH(DECLARE_WITHOUT_ALIAS_FUNCTS_IWRAP, _moneta_, ALLOPS_FINITEPARAMS_WPAREN)
RETT_OPEN _moneta_OPEN(INTF_OPEN);
RETT_IOCTL _moneta_IOCTL(INTF_IOCTL);
void _moneta_init2(void);
void _moneta_exit_cleanup(void);
struct Moneta_FD {
volatile off64_t *offset; // TODO: this needs a lock
// volatile int *length;
int flags;
void** channel;
char* path;
ino_t serialno;
int managed;
};
// for a given file descriptor (index), stores the index of the file operations to use on that file descriptor
// all values are initialized to ENV_FILTER_FOP
struct Moneta_FD **_moneta_fd_lookup;
void** _moneta_channel;
MODULE_REGISTRATION_F("moneta", _moneta_, _moneta_init2(); )
// the operations which are simiply passed to the underlying file ops
#define MONETA_OPS_SIMPLEWRAP (PIPE) (ACCEPT) (SOCKET)
// _moneta_ does implement open read write seek close truncate dup dup2 readv writev
#define MONETA_WRAP(op) \
RETT_##op _moneta_##op ( INTF_##op ) { \
CHECK_RESOLVE_FILEOPS(_moneta_); \
DEBUG("CALL: "MK_STR(_moneta_##op)" is calling \"%s\"->"#op"\n",\
_moneta_fileops->name); \
return _moneta_fileops->op( CALL_##op ); \
}
#define MONETA_WRAP_IWRAP(r, data, elem) MONETA_WRAP(elem)
BOOST_PP_SEQ_FOR_EACH(MONETA_WRAP_IWRAP, z, MONETA_OPS_SIMPLEWRAP)
void _moneta_init2(void)
{
_moneta_fd_lookup = (struct Moneta_FD**) calloc(OPEN_MAX, sizeof(struct Moneta_FD*));
_moneta_channel = (void**) calloc(1, sizeof(void*));
*_moneta_channel = NULL;
atexit(_moneta_exit_cleanup);
//_nvp_debug_handoff();
}
void _moneta_exit_cleanup(void)
{
DEBUG("_moneta_exit_cleanup called. Closing channel %p\n", *_moneta_channel);
moneta_CloseChannel(*_moneta_channel);
}
RETT_OPEN _moneta_OPEN(INTF_OPEN)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_OPEN\n");
int result;
int managed;
struct stat moneta_st;
struct stat file_st;
char* path_to_stat = (char*) calloc(strlen(path)+1, sizeof(char));
strcpy(path_to_stat, path);
if(*_moneta_channel==NULL)
{
DEBUG("Opening moneta channel...\n");
char* dev = MONETA_CHAR_DEVICE_PATH;
if(!dev) {
WARNING("Couldn't find a moneta device at %s!\n", MONETA_CHAR_DEVICE_PATH);
dev = "NOTFOUND";
}
DEBUG("Opening moneta channel at device \"%s\"\n", MONETA_CHAR_DEVICE_PATH);
//volatile int asdf=1; while(asdf){};
if(moneta_OpenChannel(dev, _moneta_channel)) {
ERROR("moneta_OpenChannel failed: %s\n", strerror(errno));
assert(0);
} else {
DEBUG("Successfully completed moneta_OpenChannel\n");
}
if(moneta_CheckChannel(*_moneta_channel)) {
DEBUG("moneta_CheckChannel failed after open: %s\n", strerror(errno));
assert(0);
} else {
DEBUG("Successfully completed moneta_CheckChannel\n");
}
} else {
if(moneta_CheckChannel(*_moneta_channel)) {
DEBUG("Moneta channel was already open, but failed CheckChannel. Will re-open channel\n");
moneta_ReopenChannel(_moneta_channel);
} else {
DEBUG("Moneta channel was already open, but passes CheckChannel\n");
}
}
DEBUG("by popular demand, O_SYNC will be added (if not already present)\n");
oflag |= O_SYNC;
///////// Let's filter out anything that's not on a moneta device
if( stat(MONETA_BLOCK_DEVICE_PATH, &moneta_st) ) {
WARNING("Failed to get stats for moneta device: \"%s\"\n", strerror(errno));
assert(0);
return -1;
DEBUG("ignoring error... this time.\n");
}
dev_t moneta_dev_id = major(moneta_st.st_rdev);
DEBUG("Got major number for moneta device \"%s\" %i\n", MONETA_BLOCK_DEVICE_PATH, (int)moneta_dev_id);
if(moneta_dev_id != ST_MONETA_DEVICE_ID) {
WARNING("Didn't get the expected device id for moneta device! (expected %i, got %i)\n", (int)ST_MONETA_DEVICE_ID, (int)moneta_dev_id);
}
// if the file doesn't exist, and it is set to O_CREAT...
if(FLAGS_INCLUDE(oflag, O_CREAT) && ((access(path, F_OK)) != 0) )
{
DEBUG("Flags include O_CREAT and file doesn't exist: determining path of file.\n");
char* last_slash = strrchr(path, '/');
if(last_slash == NULL) {
path_to_stat = ".";
} else {
int newlen = last_slash - path;
path_to_stat = (char*) calloc(newlen+1, sizeof(char));
memcpy(path_to_stat, path, newlen);
}
DEBUG("Got path \"%s\"\n", path_to_stat);
} else {
DEBUG("O_CREAT not set OR file exists; path_to_stat == path (%s) (no change)\n", path);
}
if(stat(path_to_stat, &file_st) && !FLAGS_INCLUDE(oflag, O_CREAT)) {
DEBUG("Failed to get file stats for path \"%s\": %s\n", path_to_stat, strerror(errno));
return -1;
}
if( major(file_st.st_dev) == ST_MONETA_DEVICE_ID)
{
DEBUG("st_dev matches expected major number (%i): %s will be managed.\n", ST_MONETA_DEVICE_ID, path);
managed = 1;
} else if(major(file_st.st_rdev) == ST_MONETA_DEVICE_ID) {
DEBUG("major(st_dev) was %i, but major(st_rdev) matched expected major number (%i): %s will be managed.\n",
(int)major(file_st.st_rdev), ST_MONETA_DEVICE_ID, path);
managed = 1;
// } else if( S_ISREG(file_st.st_mode) && getenv(ENV_FILTER_OVERRIDE)) {
// DEBUG("major(st_dev) was %i and major(st_rdev) was %i, but filter OVERRIDE enabled and %s is a regular file: it will be managed!\n", (int)major(file_st.st_dev), (int)major(file_st.st_rdev), path);
// managed = 1;
} else {
DEBUG("major(st_dev) was %i and major(st_rdev) was %i, but we wanted %i: %s will not be managed.\n",
(int)major(file_st.st_dev), (int)major(file_st.st_rdev), ST_MONETA_DEVICE_ID, path);
managed = 0;
}
if(managed) {
if( (S_ISREG(file_st.st_mode)==0) && (FLAGS_INCLUDE(oflag, O_CREAT) && file_st.st_mode == S_IFDIR) ) {
DEBUG("How did a non-regular file get in to _moneta_OPEN?\npath: %s\npath to stat: %s\n", path, path_to_stat);
switch(file_st.st_mode)
{
case S_IFREG: DEBUG("type was regular file\n"); break;
case S_IFDIR: DEBUG("type was directory\n"); break;
case S_IFLNK: DEBUG("type was sym link\n"); break;
case S_IFBLK: DEBUG("type was special block\n"); break;
case S_IFCHR: DEBUG("type was special char\n"); break;
case S_IFIFO: DEBUG("type was special fifo\n"); break;
case S_IFSOCK: DEBUG("type was socket\n"); break;
default: DEBUG("type was UNEXPECTED!\n"); _nvp_debug_handoff(); break;
}
}
DEBUG("_moneta_OPEN: moneta will manage \"%s\"\n", path);
} else {
DEBUG("_moneta_OPEN: moneta will NOT manage \"%s\" (got device major number %i, wanted %i)\n",
path, major(file_st.st_dev), ST_MONETA_DEVICE_ID);
}
///////// Done filtering
if (FLAGS_INCLUDE(oflag, O_CREAT))
{
va_list arg;
va_start(arg, oflag);
int mode = va_arg(arg, int);
va_end(arg);
result = _moneta_fileops->OPEN(path, oflag, mode);
} else {
if(access(path, F_OK)) {
DEBUG("File \"%s\" doesn't exist (and O_CREAT isn't set)\n", path);
// TODO: errno?
return -1;
}
result = _moneta_fileops->OPEN(path, oflag);
}
if (result == -1) {
return -1;
}
if(_moneta_fd_lookup[result]) {
ERROR("FD %i already existed in the _moneta_fd_lookup table!\n", result);
}
struct stat st;
if( stat(path, &st) ) {
DEBUG("Failed to get file stats: %s\n", strerror(errno));
return -1;
}
_moneta_fd_lookup[result] = (struct Moneta_FD*) calloc(1, sizeof(struct Moneta_FD));
int i;
for(i=0; i<OPEN_MAX; i++) {
if(_moneta_fd_lookup[i] && _moneta_fd_lookup[i]->serialno == st.st_ino) {
DEBUG("This file is already open in _moneta_... just an FYI!\n");
// _moneta_fd_lookup[result]->length = _moneta_fd_lookup[i]->length;
break;
}
}
_moneta_fd_lookup[result]->offset = (off64_t*) calloc(1, sizeof(off64_t));
*_moneta_fd_lookup[result]->offset = 0;
_moneta_fd_lookup[result]->flags = oflag;
_moneta_fd_lookup[result]->channel = (void**) calloc(1, sizeof(void*));
_moneta_fd_lookup[result]->channel = _moneta_channel;
_moneta_fd_lookup[result]->path = (char*) calloc(strlen(path)+1, sizeof(char));
strcpy(_moneta_fd_lookup[result]->path, path);
_moneta_fd_lookup[result]->serialno= st.st_ino;
_moneta_fd_lookup[result]->managed = managed;
DEBUG("Calling moneta_FileOpened(%p, %i)\n", *_moneta_channel, result);
if(moneta_FileOpened(*_moneta_channel, result)) {
ERROR("moneta_FileOpened failed, somehow!\n");
assert(0);
}
return result;
}
RETT_CLOSE _moneta_CLOSE(INTF_CLOSE)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_CLOSE\n");
int result = _moneta_fileops->CLOSE(CALL_CLOSE);
if((_moneta_fd_lookup[file]->managed) && (moneta_FileClosed(*_moneta_channel, file))) {
ERROR("moneta_FileClosed failed, somehow!\n");
assert(0);
}
free(_moneta_fd_lookup[file]);
_moneta_fd_lookup[file]= NULL;
return result;
}
RETT_READ _moneta_READ(INTF_READ)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_READ(%i, %p, %i)\n", file, buf, (int)length);
if(length < 0)
{
errno = EINVAL;
return -1;
}
if(length == 0)
{
return 0;
}
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
off64_t file_offset = *mfd->offset;
// printf("File number: %i\nFile offset: %i\nFile length: %i\nRequested read length: %i\n", file, file_offset, file_length, (int)length);
DEBUG("performing moneta_Read(%p, %i, %i, %i, %p)...\n", *mfd->channel, file, file_offset, (int)length, buf );
int result;
if(mfd->managed)
{
result = moneta_Read( *mfd->channel, file, file_offset, length, buf );
if(result < 0) {
DEBUG("moneta_Read(%p, %i, %li, %li, %p) failed! (error code %i)\n",
*mfd->channel, file, file_offset, length, buf, result );
return result;
}
*mfd->offset += result;
}
else
{
result = _moneta_fileops->READ(CALL_READ);
}
return result;
}
RETT_WRITE _moneta_WRITE(INTF_WRITE)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_WRITE(%i, %p, %i)\n", file, buf, (int)length);
if(length < 0)
{
errno = EINVAL;
return -1;
}
if(length == 0)
{
return 0;
}
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
off64_t file_offset = *mfd->offset;
int result;
if(mfd->managed)
{
DEBUG("performing moneta_Write(%p, %i, %li, %li, %p)\n", *mfd->channel, file, file_offset, length, buf);
result = moneta_Write( *mfd->channel, file, file_offset, length, buf );
if(result < 0) {
DEBUG("moneta_Write(%p, %i, %li, %li, %p) failed! (error code %i)\n",
*mfd->channel, file, file_offset, length, buf, result );
return result;
}
*mfd->offset += result;
}
else
{
DEBUG("performing %s->WRITE(CALL_WRITE)\n", _moneta_fileops->name);
result = _moneta_fileops->WRITE(CALL_WRITE);
}
return result;
}
RETT_SEEK _moneta_SEEK(INTF_SEEK)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_SEEK\n");
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
if(!mfd->managed) {
return _moneta_fileops->SEEK(CALL_SEEK);
}
return _moneta_SEEK64(CALL_SEEK);
}
RETT_SEEK64 _moneta_SEEK64(INTF_SEEK64)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_SEEK64\n");
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
if(!mfd->managed) {
return _moneta_fileops->SEEK64(CALL_SEEK64);
}
// only used for SEEK_END
struct stat st;
switch(whence)
{
case SEEK_SET:
if(offset < 0)
{
DEBUG("offset out of range (would result in negative offset).\n");
errno = EINVAL;
return -1;
}
*(mfd->offset) = offset ;
return *(mfd->offset);
case SEEK_CUR:
if((*(mfd->offset) + offset) < 0)
{
DEBUG("offset out of range (would result in negative offset).\n");
errno = EINVAL;
return -1;
}
*(mfd->offset) += offset ;
return *(mfd->offset);
case SEEK_END:
if( stat(mfd->path, &st) ) {
ERROR("Failed to get file stats: %s\n", strerror(errno));
assert(0);
return -1;
}
if( st.st_size + offset < 0 )
{
DEBUG("offset out of range (would result in negative offset).\n");
errno = EINVAL;
return -1;
}
*(mfd->offset) = st.st_size + offset;
return *(mfd->offset);
default:
DEBUG("invalid whence parameter.\n");
errno = EINVAL;
return -1;
}
}
RETT_TRUNC _moneta_TRUNC(INTF_TRUNC)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_TRUNC\n");
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
if(!mfd->managed) {
return _moneta_fileops->TRUNC(CALL_TRUNC);
}
return _moneta_TRUNC64(CALL_TRUNC);
/*
DEBUG("\n"
"TTTTTTTTTTTTTTTTTTTTTTTRRRRRRRRRRRRRRRRR UUUUUUUU UUUUUUUUNNNNNNNN NNNNNNNN CCCCCCCCCCCCC\n"
"T:::::::::::::::::::::TR::::::::::::::::R U::::::U U::::::UN:::::::N N::::::N CCC::::::::::::C\n"
"T:::::::::::::::::::::TR::::::RRRRRR:::::R U::::::U U::::::UN::::::::N N::::::N CC:::::::::::::::C\n"
"T:::::TT:::::::TT:::::TRR:::::R R:::::RUU:::::U U:::::UUN:::::::::N N::::::N C:::::CCCCCCCC::::C\n"
"TTTTTT T:::::T TTTTTT R::::R R:::::R U:::::U U:::::U N::::::::::N N::::::N C:::::C CCCCCC\n"
" T:::::T R::::R R:::::R U:::::D D:::::U N:::::::::::N N::::::NC:::::C\n"
" T:::::T R::::RRRRRR:::::R U:::::D D:::::U N:::::::N::::N N::::::NC:::::C\n"
" T:::::T R:::::::::::::RR U:::::D D:::::U N::::::N N::::N N::::::NC:::::C\n"
" T:::::T R::::RRRRRR:::::R U:::::D D:::::U N::::::N N::::N:::::::NC:::::C\n"
" T:::::T R::::R R:::::R U:::::D D:::::U N::::::N N:::::::::::NC:::::C\n"
" T:::::T R::::R R:::::R U:::::D D:::::U N::::::N N::::::::::NC:::::C\n"
" T:::::T R::::R R:::::R U::::::U U::::::U N::::::N N:::::::::N C:::::C CCCCCC\n"
" TT:::::::TT RR:::::R R:::::R U:::::::UUU:::::::U N::::::N N::::::::N C:::::CCCCCCCC::::C\n"
" T:::::::::T R::::::R R:::::R UU:::::::::::::UU N::::::N N:::::::N CC:::::::::::::::C\n"
" T:::::::::T R::::::R R:::::R UU:::::::::UU N::::::N N::::::N CCC::::::::::::C\n"
" TTTTTTTTTTT RRRRRRRR RRRRRRR UUUUUUUUU NNNNNNNN NNNNNNN CCCCCCCCCCCCC\n"
);
if(length<0) {
DEBUG("Invalid length parameter.\n");
errno = EINVAL;
return -1;
}
struct stat st;
if( stat(mfd->path, &st) ) {
DEBUG("Failed to get file stats: %s\n", strerror(errno));
return -1;
}
int file_length = st.st_size;
if(length >= file_length ) {
// glibc (but not the POSIX spec) extends the file in this case, so I guess we should too.
DEBUG("new length (%i) was >= current length (%i): EXTENDING the file\n", (int)length, file_length );
int result = moneta_Write( *mfd->channel, file, length-1, 1, "\0" );
if(result != 1) {
ERROR("Failed to extend the file by writing 1 byte at position %i: wrote %i bytes\n", (int)length-1, result);
}
return result;
}
if(*(mfd->offset) > length) {
DEBUG("offset was beyond the length of the file... but I'm not changing it\n");
}
return _moneta_fileops->TRUNC(CALL_TRUNC);
*/
}
RETT_TRUNC64 _moneta_TRUNC64(INTF_TRUNC64)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_TRUNC64\n");
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
if(!mfd->managed) {
return _moneta_fileops->TRUNC64(CALL_TRUNC64);
}
DEBUG("\n"
"TTTTTTTTTTTTTTTTTTTTTTTRRRRRRRRRRRRRRRRR UUUUUUUU UUUUUUUUNNNNNNNN NNNNNNNN CCCCCCCCCCCCC\n"
"T:::::::::::::::::::::TR::::::::::::::::R U::::::U U::::::UN:::::::N N::::::N CCC::::::::::::C\n"
"T:::::::::::::::::::::TR::::::RRRRRR:::::R U::::::U U::::::UN::::::::N N::::::N CC:::::::::::::::C\n"
"T:::::TT:::::::TT:::::TRR:::::R R:::::RUU:::::U U:::::UUN:::::::::N N::::::N C:::::CCCCCCCC::::C\n"
"TTTTTT T:::::T TTTTTT R::::R R:::::R U:::::U U:::::U N::::::::::N N::::::N C:::::C CCCCCC\n"
" T:::::T R::::R R:::::R U:::::D D:::::U N:::::::::::N N::::::NC:::::C\n"
" T:::::T R::::RRRRRR:::::R U:::::D D:::::U N:::::::N::::N N::::::NC:::::C\n"
" T:::::T R:::::::::::::RR U:::::D D:::::U N::::::N N::::N N::::::NC:::::C\n"
" T:::::T 64 R::::RRRRRR:::::R U:::::D D:::::U N::::::N N::::N:::::::NC:::::C 64\n"
" T:::::T R::::R R:::::R U:::::D D:::::U N::::::N N:::::::::::NC:::::C\n"
" T:::::T R::::R R:::::R U:::::D D:::::U N::::::N N::::::::::NC:::::C\n"
" T:::::T R::::R R:::::R U::::::U U::::::U N::::::N N:::::::::N C:::::C CCCCCC\n"
" TT:::::::TT RR:::::R R:::::R U:::::::UUU:::::::U N::::::N N::::::::N C:::::CCCCCCCC::::C\n"
" T:::::::::T R::::::R R:::::R UU:::::::::::::UU N::::::N N:::::::N CC:::::::::::::::C\n"
" T:::::::::T R::::::R R:::::R UU:::::::::UU N::::::N N::::::N CCC::::::::::::C\n"
" TTTTTTTTTTT RRRRRRRR RRRRRRR UUUUUUUUU NNNNNNNN NNNNNNN CCCCCCCCCCCCC\n"
);
if(length<0) {
DEBUG("Invalid length parameter.\n");
errno = EINVAL;
return -1;
}
struct stat st;
if( stat(mfd->path, &st) ) {
DEBUG("Failed to get file stats: %s\n", strerror(errno));
return -1;
}
int file_length = st.st_size;
if(length >= file_length ) {
// glibc (but not the POSIX spec) extends the file in this case, so I guess we should too.
DEBUG("new length (%i) was >= current length (%i): EXTENDING the file\n", (int)length, file_length );
int result = moneta_Write( *mfd->channel, file, length-1, 1, "\0" );
if(result != 1) {
ERROR("Failed to extend the file by writing 1 byte at position %i: wrote %i bytes\n", (int)length-1, result);
}
return result;
}
if(*(mfd->offset) > length) {
DEBUG("offset was beyond the length of the file... but I'm not changing it\n");
}
return _moneta_fileops->TRUNC(CALL_TRUNC);
}
RETT_IOCTL _moneta_IOCTL(INTF_IOCTL)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_IOCTL\n");
va_list arg;
va_start(arg, request);
int* third = va_arg(arg, int*);
RETT_IOCTL result = _moneta_fileops->IOCTL(file, request, third);
return result;
}
RETT_DUP _moneta_DUP(INTF_DUP)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_DUP\n");
DEBUG("_moneta_DUP was assigned fileops %p\n", _moneta_fileops);
assert(_moneta_fileops);
DEBUG("that corresponds to %s\n", _moneta_fileops->name);
assert(_moneta_fileops->DUP);
DEBUG("preparing to call %s->DUP(%i)\n", _moneta_fileops->name, file);
int result = _moneta_fileops->DUP(CALL_DUP);
DEBUG("moneta_fileops->DUP returned %i\n", result);
if(!_moneta_fd_lookup[file]->managed) { return result; }
if( (result < 0) || (result == file) ) {
DEBUG("DUP failed.\n");
return result;
}
DEBUG("does _moneta_fd_lookup[%i] exist? (_moneta_fd_lookup = %p)\n", result, _moneta_fd_lookup);
DEBUG("attempting to access _moneta_fd_lookup[%i]\n", result);
DEBUG("_moneta_fd_lookup[%i]=%p\n", result, _moneta_fd_lookup[result]);
DEBUG("file = %i\n", file);
if(_moneta_fd_lookup[result]) {
ERROR("Moneta already has a FD allocated for %i!\n", result);
} else {
DEBUG("moneta_dup is allocating a new fd of size %i\n", (int)sizeof(struct Moneta_FD));
_moneta_fd_lookup[result] = (struct Moneta_FD*) calloc(1, sizeof(struct Moneta_FD));
if(_moneta_fd_lookup[result] == NULL) {
ERROR("_moneta_DUP FAILED to calloc 1 struct of size %i to hold a Moneta_FD!\n", (int)sizeof(struct Moneta_FD));
assert(0);
} else {DEBUG("calloc completed %p, file=%i\n", _moneta_fd_lookup[result], file);}
}
DEBUG("_moneta_fd_lookup[file (%i)] = %p\n", file, _moneta_fd_lookup[file]);
_moneta_fd_lookup[result]->offset = _moneta_fd_lookup[file]->offset;
_moneta_fd_lookup[result]->flags = _moneta_fd_lookup[file]->flags;
_moneta_fd_lookup[result]->channel = _moneta_fd_lookup[file]->channel;
DEBUG("moneta_DUP returning success (%i)\n", result);
return result;
}
RETT_DUP2 _moneta_DUP2(INTF_DUP2)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
//_nvp_debug_handoff();
DEBUG("CALL: _moneta_DUP2\n");
int result = _moneta_fileops->DUP2(CALL_DUP2);
if (file >= 0 && file <= 2) return result;
assert(_moneta_fd_lookup[file]);
if(!_moneta_fd_lookup[file]->managed) { return result; }
if(result != fd2) {
ERROR("DUP2 call had an error.\n");
} else {
DEBUG("DUP2 call completed successfully.\n");
}
if(result < 0) {
ERROR("DUP2 failed.\n");
return result;
}
if(!_moneta_fd_lookup[result]) {
ERROR("Moneta DIDN'T already have a FD allocated for %i!\n", fd2);
_moneta_fd_lookup[result] = (struct Moneta_FD*) calloc(1, sizeof(struct Moneta_FD));
}
assert(_moneta_fd_lookup[result]);
assert(_moneta_fd_lookup[file]->offset);
assert(_moneta_fd_lookup[file]->channel);
_moneta_fd_lookup[result]->offset = _moneta_fd_lookup[file]->offset;
_moneta_fd_lookup[result]->flags = _moneta_fd_lookup[file]->flags;
_moneta_fd_lookup[result]->channel = _moneta_fd_lookup[file]->channel;
return result;
}
RETT_READV _moneta_READV(INTF_READV)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_READV\n");
if(!_moneta_fd_lookup[file]->managed) {
return _moneta_fileops->READV(CALL_READV);
}
//TODO: opportunities for optimization exist here
int fail = 0;
int i;
for(i=0; i<iovcnt; i++)
{
fail |= _moneta_READ(file, iov[i].iov_base, iov[i].iov_len);
if(fail) { break; }
}
if(fail != 0) {
DEBUG("_moneta_READV failed\n");
return -1;
}
return 0;
}
RETT_WRITEV _moneta_WRITEV(INTF_WRITEV)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_WRITEV\n");
if(!_moneta_fd_lookup[file]->managed) {
return _moneta_fileops->WRITEV(CALL_WRITEV);
}
//TODO: opportunities for optimization exist here
int fail = 0;
int i;
for(i=0; i<iovcnt; i++)
{
fail |= _moneta_WRITE(file, iov[i].iov_base, iov[i].iov_len);
if(fail) { break; }
}
if(fail != 0) {
DEBUG("_moneta_WRITEV failed\n");
return -1;
}
return 0;
}
RETT_FORK _moneta_FORK(INTF_FORK)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
DEBUG("CALL: _moneta_FORK\n");
RETT_FORK result = _moneta_fileops->FORK(CALL_FORK);
if(result < 0)
{
ERROR("_moneta_FORK->%s_FORK failed!\n", _moneta_fileops->name);
}
else if(result==0)
{
DEBUG("Child process is calling moneta_ReopenChannel\n");
int r = moneta_ReopenChannel(_moneta_channel);
if(r) {
ERROR("moneta_ReopenChannel(%p) failed: returned %i\n", _moneta_channel, r);
}
}
else
{
DEBUG("Moneta parent has finished fork.\n");
}
return result;
}
RETT_PREAD _moneta_PREAD(INTF_PREAD)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
if(count < 0)
{
errno = EINVAL;
return -1;
}
if(count == 0)
{
return 0;
}
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
if(!mfd->managed) {
return _moneta_fileops->PREAD(CALL_PREAD);
}
DEBUG("CALL: _moneta_PREAD(%i, %p, %i, %i)\n", file, buf, (int)count, (int)offset);
#if CHECK_ALIGNMENT
DEBUG("Checking alignment to %i bytes\n", CHECK_ALIGNMENT);
if(offset%CHECK_ALIGNMENT) {
DEBUG("offset %i is not %i-byte aligned.\n", (int)offset, CHECK_ALIGNMENT);
}
if(count%CHECK_ALIGNMENT) {
DEBUG("count %i is not %i-byte aligned.\n", (int)count, CHECK_ALIGNMENT);
}
#endif
//volatile int cc=1; while(cc){};
return (RETT_PREAD) moneta_Read(*mfd->channel, file, offset, count, buf);
}
RETT_PWRITE _moneta_PWRITE(INTF_PWRITE)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
if(count < 0)
{
errno = EINVAL;
return -1;
}
if(count == 0)
{
return 0;
}
struct Moneta_FD *mfd = _moneta_fd_lookup[file];
if(!mfd->managed) {
return _moneta_fileops->PWRITE(CALL_PWRITE);
}
DEBUG("CALL: _moneta_PWRITE(%i, %p, %i, %i)\n", file, buf, (int)count, (int)offset);
#if CHECK_ALIGNMENT
DEBUG("Checking alignment to %i bytes\n", CHECK_ALIGNMENT);
if(offset%CHECK_ALIGNMENT) {
DEBUG("offset %i is not %i-byte aligned.\n", (int)offset, CHECK_ALIGNMENT);
}
if(count%CHECK_ALIGNMENT) {
DEBUG("count %i is not %i-byte aligned.\n", (int)count, CHECK_ALIGNMENT);
}
#endif
int r = moneta_Write(*mfd->channel, file, offset, count, buf);
DEBUG("CALL: _moneta_PWRITE(%i, %p, %i, %i), returned %d\n", file, buf, (int)count, (int)offset, r);
return (RETT_PWRITE) r;
}
RETT_FSYNC _moneta_FSYNC(INTF_FSYNC)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
if(!_moneta_fd_lookup[file]->managed) {
return _moneta_fileops->FSYNC(CALL_FSYNC);
}
DEBUG("CALL: _moneta_FSYNC: CRUSHED\n");
return 0;
}
RETT_FDSYNC _moneta_FDSYNC(INTF_FDSYNC)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
if(!_moneta_fd_lookup[file]->managed) {
return _moneta_fileops->FDSYNC(CALL_FDSYNC);
}
DEBUG("CALL: _moneta_FDSYNC: CRUSHED\n");
return 0;
}
RETT_MKSTEMP _moneta_MKSTEMP(INTF_MKSTEMP)
{
CHECK_RESOLVE_FILEOPS(_moneta_);
char* suffix = file + strlen(file) - 6; // char* suffix = strstr(file, "XXXXXX");
DEBUG("Called _moneta_mkstemp with template %s; making a new filename...\n", file);
if(suffix == NULL) {
DEBUG("Invalid template string (%s) passed to mkstemp\n", file);
errno = EINVAL;
return -1;
}
// generate a valid file name
int i;
int success = 0;
for(i=0; i<1000000; i++)
{
sprintf(suffix, "%.6i", i);
int fs = access(file, F_OK);
if(fs == 0) { // file doesn't exist; we're good
success = 1;
break;
}
}
if(!success) {
DEBUG("No available file names!\n");
return -1;
}
DEBUG("Generated filename %s. Calling (regular) open...\n", file);
return open(file, O_CREAT | O_EXCL, 0600);
}