-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
passthrough_hp.cc
1487 lines (1235 loc) · 43.1 KB
/
passthrough_hp.cc
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
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
Copyright (C) 2017 Nikolaus Rath <Nikolaus@rath.org>
Copyright (C) 2018 Valve, Inc
This program can be distributed under the terms of the GNU GPLv2.
See the file COPYING.
*/
/** @file
*
* This is a "high-performance" version of passthrough_ll.c. While
* passthrough_ll.c is designed to be as simple as possible, this
* example intended to be as efficient and correct as possible.
*
* passthrough_hp.cc mirrors a specified "source" directory under a
* specified the mountpoint with as much fidelity and performance as
* possible.
*
* If --nocache is specified, the source directory may be changed
* directly even while mounted and the filesystem will continue
* to work correctly.
*
* Without --nocache, the source directory is assumed to be modified
* only through the passthrough filesystem. This enables much better
* performance, but if changes are made directly to the source, they
* may not be immediately visible under the mountpoint and further
* access to the mountpoint may result in incorrect behavior,
* including data-loss.
*
* On its own, this filesystem fulfills no practical purpose. It is
* intended as a template upon which additional functionality can be
* built.
*
* Unless --nocache is specified, is only possible to write to files
* for which the mounting user has read permissions. This is because
* the writeback cache requires the kernel to be able to issue read
* requests for all files (which the passthrough filesystem cannot
* satisfy if it can't read the file in the underlying filesystem).
*
* ## Source code ##
* \include passthrough_hp.cc
*/
#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
// C includes
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <ftw.h>
#include <fuse_lowlevel.h>
#include <inttypes.h>
#include <string.h>
#include <sys/file.h>
#include <sys/resource.h>
#include <sys/xattr.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <limits.h>
// C++ includes
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include "cxxopts.hpp"
#include <mutex>
#include <syslog.h>
using namespace std;
#define SFS_DEFAULT_THREADS "-1" // take libfuse value as default
#define SFS_DEFAULT_CLONE_FD "0"
/* We are re-using pointers to our `struct sfs_inode` and `struct
sfs_dirp` elements as inodes and file handles. This means that we
must be able to store pointer a pointer in both a fuse_ino_t
variable and a uint64_t variable (used for file handles). */
static_assert(sizeof(fuse_ino_t) >= sizeof(void*),
"void* must fit into fuse_ino_t");
static_assert(sizeof(fuse_ino_t) >= sizeof(uint64_t),
"fuse_ino_t must be at least 64 bits");
/* Forward declarations */
struct Inode;
static Inode& get_inode(fuse_ino_t ino);
static void forget_one(fuse_ino_t ino, uint64_t n);
// Uniquely identifies a file in the source directory tree. This could
// be simplified to just ino_t since we require the source directory
// not to contain any mountpoints. This hasn't been done yet in case
// we need to reconsider this constraint (but relaxing this would have
// the drawback that we can no longer reuse inode numbers, and thus
// readdir() would need to do a full lookup() in order to report the
// right inode number).
typedef std::pair<ino_t, dev_t> SrcId;
// Define a hash function for SrcId
namespace std {
template<>
struct hash<SrcId> {
size_t operator()(const SrcId& id) const {
return hash<ino_t>{}(id.first) ^ hash<dev_t>{}(id.second);
}
};
}
// Maps files in the source directory tree to inodes
typedef std::unordered_map<SrcId, Inode> InodeMap;
struct Inode {
int fd {-1};
dev_t src_dev {0};
ino_t src_ino {0};
int generation {0};
int backing_id {0};
uint64_t nopen {0};
uint64_t nlookup {0};
std::mutex m;
// Delete copy constructor and assignments. We could implement
// move if we need it.
Inode() = default;
Inode(const Inode&) = delete;
Inode(Inode&& inode) = delete;
Inode& operator=(Inode&& inode) = delete;
Inode& operator=(const Inode&) = delete;
~Inode() {
if(fd > 0)
close(fd);
}
};
struct Fs {
// Must be acquired *after* any Inode.m locks.
std::mutex mutex;
InodeMap inodes; // protected by mutex
Inode root;
double timeout;
bool debug;
bool debug_fuse;
bool foreground;
std::string source;
size_t blocksize;
dev_t src_dev;
bool nosplice;
bool nocache;
size_t num_threads;
bool clone_fd;
std::string fuse_mount_options;
bool direct_io;
bool passthrough;
};
static Fs fs{};
#define FUSE_BUF_COPY_FLAGS \
(fs.nosplice ? \
FUSE_BUF_NO_SPLICE : \
static_cast<fuse_buf_copy_flags>(0))
static Inode& get_inode(fuse_ino_t ino) {
if (ino == FUSE_ROOT_ID)
return fs.root;
Inode* inode = reinterpret_cast<Inode*>(ino);
if(inode->fd == -1) {
cerr << "INTERNAL ERROR: Unknown inode " << ino << endl;
abort();
}
return *inode;
}
static int get_fs_fd(fuse_ino_t ino) {
int fd = get_inode(ino).fd;
return fd;
}
static void sfs_init(void *userdata, fuse_conn_info *conn) {
(void)userdata;
if (fs.passthrough && conn->capable & FUSE_CAP_PASSTHROUGH)
conn->want |= FUSE_CAP_PASSTHROUGH;
else
fs.passthrough = false;
/* Passthrough and writeback cache are conflicting modes */
if (fs.timeout && !fs.passthrough &&
conn->capable & FUSE_CAP_WRITEBACK_CACHE)
conn->want |= FUSE_CAP_WRITEBACK_CACHE;
if (conn->capable & FUSE_CAP_FLOCK_LOCKS)
conn->want |= FUSE_CAP_FLOCK_LOCKS;
if (fs.nosplice) {
// FUSE_CAP_SPLICE_READ is enabled in libfuse3 by default,
// see do_init() in in fuse_lowlevel.c
// Just unset both, in case FUSE_CAP_SPLICE_WRITE would also get enabled
// by default.
conn->want &= ~FUSE_CAP_SPLICE_READ;
conn->want &= ~FUSE_CAP_SPLICE_WRITE;
} else {
if (conn->capable & FUSE_CAP_SPLICE_WRITE)
conn->want |= FUSE_CAP_SPLICE_WRITE;
if (conn->capable & FUSE_CAP_SPLICE_READ)
conn->want |= FUSE_CAP_SPLICE_READ;
}
/* This is a local file system - no network coherency needed */
if (conn->capable & FUSE_CAP_DIRECT_IO_ALLOW_MMAP)
conn->want |= FUSE_CAP_DIRECT_IO_ALLOW_MMAP;
/* Disable the receiving and processing of FUSE_INTERRUPT requests */
conn->no_interrupt = 1;
}
static void sfs_getattr(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi)
{
struct stat attr;
int fd = fi ? fi->fh : get_inode(ino).fd;
auto res = fstatat(fd, "", &attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
if (res == -1) {
fuse_reply_err(req, errno);
return;
}
fuse_reply_attr(req, &attr, fs.timeout);
}
static void do_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
int valid, struct fuse_file_info* fi) {
Inode& inode = get_inode(ino);
int ifd = inode.fd;
int res;
if (valid & FUSE_SET_ATTR_MODE) {
if (fi) {
res = fchmod(fi->fh, attr->st_mode);
} else {
char procname[64];
sprintf(procname, "/proc/self/fd/%i", ifd);
res = chmod(procname, attr->st_mode);
}
if (res == -1)
goto out_err;
}
if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : static_cast<uid_t>(-1);
gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : static_cast<gid_t>(-1);
res = fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
if (res == -1)
goto out_err;
}
if (valid & FUSE_SET_ATTR_SIZE) {
if (fi) {
res = ftruncate(fi->fh, attr->st_size);
} else {
char procname[64];
sprintf(procname, "/proc/self/fd/%i", ifd);
res = truncate(procname, attr->st_size);
}
if (res == -1)
goto out_err;
}
if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
struct timespec tv[2];
tv[0].tv_sec = 0;
tv[1].tv_sec = 0;
tv[0].tv_nsec = UTIME_OMIT;
tv[1].tv_nsec = UTIME_OMIT;
if (valid & FUSE_SET_ATTR_ATIME_NOW)
tv[0].tv_nsec = UTIME_NOW;
else if (valid & FUSE_SET_ATTR_ATIME)
tv[0] = attr->st_atim;
if (valid & FUSE_SET_ATTR_MTIME_NOW)
tv[1].tv_nsec = UTIME_NOW;
else if (valid & FUSE_SET_ATTR_MTIME)
tv[1] = attr->st_mtim;
if (fi)
res = futimens(fi->fh, tv);
else {
#ifdef HAVE_UTIMENSAT
char procname[64];
sprintf(procname, "/proc/self/fd/%i", ifd);
res = utimensat(AT_FDCWD, procname, tv, 0);
#else
res = -1;
errno = EOPNOTSUPP;
#endif
}
if (res == -1)
goto out_err;
}
return sfs_getattr(req, ino, fi);
out_err:
fuse_reply_err(req, errno);
}
static void sfs_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
int valid, fuse_file_info *fi) {
(void) ino;
do_setattr(req, ino, attr, valid, fi);
}
static int do_lookup(fuse_ino_t parent, const char *name,
fuse_entry_param *e) {
if (fs.debug)
cerr << "DEBUG: lookup(): name=" << name
<< ", parent=" << parent << endl;
memset(e, 0, sizeof(*e));
e->attr_timeout = fs.timeout;
e->entry_timeout = fs.timeout;
auto newfd = openat(get_fs_fd(parent), name, O_PATH | O_NOFOLLOW);
if (newfd == -1)
return errno;
auto res = fstatat(newfd, "", &e->attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
if (res == -1) {
auto saveerr = errno;
close(newfd);
if (fs.debug)
cerr << "DEBUG: lookup(): fstatat failed" << endl;
return saveerr;
}
if (e->attr.st_dev != fs.src_dev) {
cerr << "WARNING: Mountpoints in the source directory tree will be hidden." << endl;
return ENOTSUP;
} else if (e->attr.st_ino == FUSE_ROOT_ID) {
cerr << "ERROR: Source directory tree must not include inode "
<< FUSE_ROOT_ID << endl;
return EIO;
}
SrcId id {e->attr.st_ino, e->attr.st_dev};
unique_lock<mutex> fs_lock {fs.mutex};
Inode* inode_p;
try {
inode_p = &fs.inodes[id];
} catch (std::bad_alloc&) {
return ENOMEM;
}
e->ino = reinterpret_cast<fuse_ino_t>(inode_p);
Inode& inode {*inode_p};
e->generation = inode.generation;
if (inode.fd == -ENOENT) { // found unlinked inode
if (fs.debug)
cerr << "DEBUG: lookup(): inode " << e->attr.st_ino
<< " recycled; generation=" << inode.generation << endl;
/* fallthrough to new inode but keep existing inode.nlookup */
}
if (inode.fd > 0) { // found existing inode
fs_lock.unlock();
if (fs.debug)
cerr << "DEBUG: lookup(): inode " << e->attr.st_ino
<< " (userspace) already known; fd = " << inode.fd << endl;
lock_guard<mutex> g {inode.m};
inode.nlookup++;
if (fs.debug)
cerr << "DEBUG:" << __func__ << ":" << __LINE__ << " "
<< "inode " << inode.src_ino
<< " count " << inode.nlookup << endl;
close(newfd);
} else { // no existing inode
/* This is just here to make Helgrind happy. It violates the
lock ordering requirement (inode.m must be acquired before
fs.mutex), but this is of no consequence because at this
point no other thread has access to the inode mutex */
lock_guard<mutex> g {inode.m};
inode.src_ino = e->attr.st_ino;
inode.src_dev = e->attr.st_dev;
inode.nlookup++;
if (fs.debug)
cerr << "DEBUG:" << __func__ << ":" << __LINE__ << " "
<< "inode " << inode.src_ino
<< " count " << inode.nlookup << endl;
inode.fd = newfd;
fs_lock.unlock();
if (fs.debug)
cerr << "DEBUG: lookup(): created userspace inode " << e->attr.st_ino
<< "; fd = " << inode.fd << endl;
}
return 0;
}
static void sfs_lookup(fuse_req_t req, fuse_ino_t parent, const char *name) {
fuse_entry_param e {};
auto err = do_lookup(parent, name, &e);
if (err == ENOENT) {
e.attr_timeout = fs.timeout;
e.entry_timeout = fs.timeout;
e.ino = e.attr.st_ino = 0;
fuse_reply_entry(req, &e);
} else if (err) {
if (err == ENFILE || err == EMFILE)
cerr << "ERROR: Reached maximum number of file descriptors." << endl;
fuse_reply_err(req, err);
} else {
fuse_reply_entry(req, &e);
}
}
static void mknod_symlink(fuse_req_t req, fuse_ino_t parent,
const char *name, mode_t mode, dev_t rdev,
const char *link) {
int res;
Inode& inode_p = get_inode(parent);
auto saverr = ENOMEM;
if (S_ISDIR(mode))
res = mkdirat(inode_p.fd, name, mode);
else if (S_ISLNK(mode))
res = symlinkat(link, inode_p.fd, name);
else
res = mknodat(inode_p.fd, name, mode, rdev);
saverr = errno;
if (res == -1)
goto out;
fuse_entry_param e;
saverr = do_lookup(parent, name, &e);
if (saverr)
goto out;
fuse_reply_entry(req, &e);
return;
out:
if (saverr == ENFILE || saverr == EMFILE)
cerr << "ERROR: Reached maximum number of file descriptors." << endl;
fuse_reply_err(req, saverr);
}
static void sfs_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, dev_t rdev) {
mknod_symlink(req, parent, name, mode, rdev, nullptr);
}
static void sfs_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode) {
mknod_symlink(req, parent, name, S_IFDIR | mode, 0, nullptr);
}
static void sfs_symlink(fuse_req_t req, const char *link, fuse_ino_t parent,
const char *name) {
mknod_symlink(req, parent, name, S_IFLNK, 0, link);
}
static void sfs_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent,
const char *name) {
Inode& inode = get_inode(ino);
Inode& inode_p = get_inode(parent);
fuse_entry_param e {};
e.attr_timeout = fs.timeout;
e.entry_timeout = fs.timeout;
char procname[64];
sprintf(procname, "/proc/self/fd/%i", inode.fd);
auto res = linkat(AT_FDCWD, procname, inode_p.fd, name, AT_SYMLINK_FOLLOW);
if (res == -1) {
fuse_reply_err(req, errno);
return;
}
res = fstatat(inode.fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
if (res == -1) {
fuse_reply_err(req, errno);
return;
}
e.ino = reinterpret_cast<fuse_ino_t>(&inode);
{
lock_guard<mutex> g {inode.m};
inode.nlookup++;
if (fs.debug)
cerr << "DEBUG:" << __func__ << ":" << __LINE__ << " "
<< "inode " << inode.src_ino
<< " count " << inode.nlookup << endl;
}
fuse_reply_entry(req, &e);
return;
}
static void sfs_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name) {
Inode& inode_p = get_inode(parent);
lock_guard<mutex> g {inode_p.m};
auto res = unlinkat(inode_p.fd, name, AT_REMOVEDIR);
fuse_reply_err(req, res == -1 ? errno : 0);
}
static void sfs_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
fuse_ino_t newparent, const char *newname,
unsigned int flags) {
Inode& inode_p = get_inode(parent);
Inode& inode_np = get_inode(newparent);
if (flags) {
fuse_reply_err(req, EINVAL);
return;
}
auto res = renameat(inode_p.fd, name, inode_np.fd, newname);
fuse_reply_err(req, res == -1 ? errno : 0);
}
static void sfs_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) {
Inode& inode_p = get_inode(parent);
// Release inode.fd before last unlink like nfsd EXPORT_OP_CLOSE_BEFORE_UNLINK
// to test reused inode numbers.
// Skip this when inode has an open file and when writeback cache is enabled.
if (!fs.timeout) {
fuse_entry_param e;
auto err = do_lookup(parent, name, &e);
if (err) {
fuse_reply_err(req, err);
return;
}
if (e.attr.st_nlink == 1) {
Inode& inode = get_inode(e.ino);
lock_guard<mutex> g {inode.m};
if (inode.fd > 0 && !inode.nopen) {
if (fs.debug)
cerr << "DEBUG: unlink: release inode " << e.attr.st_ino
<< "; fd=" << inode.fd << endl;
lock_guard<mutex> g_fs {fs.mutex};
close(inode.fd);
inode.fd = -ENOENT;
inode.generation++;
}
}
// decrease the ref which lookup above had increased
forget_one(e.ino, 1);
}
auto res = unlinkat(inode_p.fd, name, 0);
fuse_reply_err(req, res == -1 ? errno : 0);
}
static void forget_one(fuse_ino_t ino, uint64_t n) {
Inode& inode = get_inode(ino);
unique_lock<mutex> l {inode.m};
if(n > inode.nlookup) {
cerr << "INTERNAL ERROR: Negative lookup count for inode "
<< inode.src_ino << endl;
abort();
}
inode.nlookup -= n;
if (fs.debug)
cerr << "DEBUG:" << __func__ << ":" << __LINE__ << " "
<< "inode " << inode.src_ino
<< " count " << inode.nlookup << endl;
if (!inode.nlookup) {
if (fs.debug)
cerr << "DEBUG: forget: cleaning up inode " << inode.src_ino << endl;
{
lock_guard<mutex> g_fs {fs.mutex};
l.unlock();
fs.inodes.erase({inode.src_ino, inode.src_dev});
}
} else if (fs.debug)
cerr << "DEBUG: forget: inode " << inode.src_ino
<< " lookup count now " << inode.nlookup << endl;
}
static void sfs_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) {
forget_one(ino, nlookup);
fuse_reply_none(req);
}
static void sfs_forget_multi(fuse_req_t req, size_t count,
fuse_forget_data *forgets) {
for (int i = 0; i < count; i++)
forget_one(forgets[i].ino, forgets[i].nlookup);
fuse_reply_none(req);
}
static void sfs_readlink(fuse_req_t req, fuse_ino_t ino) {
Inode& inode = get_inode(ino);
char buf[PATH_MAX + 1];
auto res = readlinkat(inode.fd, "", buf, sizeof(buf));
if (res == -1)
fuse_reply_err(req, errno);
else if (res == sizeof(buf))
fuse_reply_err(req, ENAMETOOLONG);
else {
buf[res] = '\0';
fuse_reply_readlink(req, buf);
}
}
struct DirHandle {
DIR *dp {nullptr};
off_t offset;
DirHandle() = default;
DirHandle(const DirHandle&) = delete;
DirHandle& operator=(const DirHandle&) = delete;
~DirHandle() {
if(dp)
closedir(dp);
}
};
static DirHandle *get_dir_handle(fuse_file_info *fi) {
return reinterpret_cast<DirHandle*>(fi->fh);
}
static void sfs_opendir(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) {
Inode& inode = get_inode(ino);
auto d = new (nothrow) DirHandle;
if (d == nullptr) {
fuse_reply_err(req, ENOMEM);
return;
}
// Make Helgrind happy - it can't know that there's an implicit
// synchronization due to the fact that other threads cannot
// access d until we've called fuse_reply_*.
lock_guard<mutex> g {inode.m};
auto fd = openat(inode.fd, ".", O_RDONLY);
if (fd == -1)
goto out_errno;
// On success, dir stream takes ownership of fd, so we
// do not have to close it.
d->dp = fdopendir(fd);
if(d->dp == nullptr)
goto out_errno;
d->offset = 0;
fi->fh = reinterpret_cast<uint64_t>(d);
if(fs.timeout) {
fi->keep_cache = 1;
fi->cache_readdir = 1;
}
fuse_reply_open(req, fi);
return;
out_errno:
auto error = errno;
delete d;
if (error == ENFILE || error == EMFILE)
cerr << "ERROR: Reached maximum number of file descriptors." << endl;
fuse_reply_err(req, error);
}
static bool is_dot_or_dotdot(const char *name) {
return name[0] == '.' &&
(name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
}
static void do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t offset, fuse_file_info *fi, const int plus) {
auto d = get_dir_handle(fi);
Inode& inode = get_inode(ino);
lock_guard<mutex> g {inode.m};
char *p;
auto rem = size;
int err = 0, count = 0;
if (fs.debug)
cerr << "DEBUG: readdir(): started with offset "
<< offset << endl;
auto buf = new (nothrow) char[size];
if (!buf) {
fuse_reply_err(req, ENOMEM);
return;
}
p = buf;
if (offset != d->offset) {
if (fs.debug)
cerr << "DEBUG: readdir(): seeking to " << offset << endl;
seekdir(d->dp, offset);
d->offset = offset;
}
while (1) {
bool did_lookup = false;
struct dirent *entry;
errno = 0;
entry = readdir(d->dp);
if (!entry) {
if(errno) {
err = errno;
if (fs.debug)
warn("DEBUG: readdir(): readdir failed with");
goto error;
}
break; // End of stream
}
d->offset = entry->d_off;
fuse_entry_param e{};
size_t entsize;
if (plus) {
if (is_dot_or_dotdot(entry->d_name)) {
/* fuse kernel ignores attributes for these and also does
* not increase lookup count (see fuse_direntplus_link) */
e.attr.st_ino = entry->d_ino;
e.attr.st_mode = entry->d_type << 12;
} else {
err = do_lookup(ino, entry->d_name, &e);
if (err)
goto error;
did_lookup = true;
}
entsize = fuse_add_direntry_plus(req, p, rem, entry->d_name, &e, entry->d_off);
} else {
e.attr.st_ino = entry->d_ino;
e.attr.st_mode = entry->d_type << 12;
entsize = fuse_add_direntry(req, p, rem, entry->d_name, &e.attr, entry->d_off);
}
if (entsize > rem) {
if (fs.debug)
cerr << "DEBUG: readdir(): buffer full, returning data. " << endl;
if (did_lookup)
forget_one(e.ino, 1);
break;
}
p += entsize;
rem -= entsize;
count++;
if (fs.debug) {
cerr << "DEBUG: readdir(): added to buffer: " << entry->d_name
<< ", ino " << e.attr.st_ino << ", offset " << entry->d_off << endl;
}
}
err = 0;
error:
// If there's an error, we can only signal it if we haven't stored
// any entries yet - otherwise we'd end up with wrong lookup
// counts for the entries that are already in the buffer. So we
// return what we've collected until that point.
if (err && rem == size) {
if (err == ENFILE || err == EMFILE)
cerr << "ERROR: Reached maximum number of file descriptors." << endl;
fuse_reply_err(req, err);
} else {
if (fs.debug)
cerr << "DEBUG: readdir(): returning " << count
<< " entries, curr offset " << d->offset << endl;
fuse_reply_buf(req, buf, size - rem);
}
delete[] buf;
return;
}
static void sfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t offset, fuse_file_info *fi) {
// operation logging is done in readdir to reduce code duplication
do_readdir(req, ino, size, offset, fi, 0);
}
static void sfs_readdirplus(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t offset, fuse_file_info *fi) {
// operation logging is done in readdir to reduce code duplication
do_readdir(req, ino, size, offset, fi, 1);
}
static void sfs_releasedir(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) {
(void) ino;
auto d = get_dir_handle(fi);
delete d;
fuse_reply_err(req, 0);
}
static void do_passthrough_open(fuse_req_t req, fuse_ino_t ino, int fd,
fuse_file_info *fi) {
Inode& inode = get_inode(ino);
/* Setup a shared backing file on first open of an inode */
if (inode.backing_id) {
if (fs.debug)
cerr << "DEBUG: reusing shared backing file "
<< inode.backing_id << " for inode " << ino << endl;
fi->backing_id = inode.backing_id;
} else if (!(inode.backing_id = fuse_passthrough_open(req, fd))) {
cerr << "DEBUG: fuse_passthrough_open failed for inode " << ino
<< ", disabling rw passthrough." << endl;
fs.passthrough = false;
} else {
if (fs.debug)
cerr << "DEBUG: setup shared backing file "
<< inode.backing_id << " for inode " << ino << endl;
fi->backing_id = inode.backing_id;
}
/* open in passthrough mode must drop old page cache */
if (fi->backing_id)
fi->keep_cache = false;
}
static void sfs_create_open_flags(fuse_file_info *fi)
{
if (fs.direct_io)
fi->direct_io = 1;
/*
* fi->direct_io (FOPEN_DIRECT_IO) is set to benefit from
* parallel_direct_writes, which kernel cannot do for plain O_DIRECT.
* However, passthrough is preferred, but which is not possible when
* FOPEN_DIRECT_IO is set.
*/
if (!fs.passthrough) {
if (fi->flags & O_DIRECT)
fi->direct_io = 1;
}
/* parallel_direct_writes feature depends on direct_io features.
To make parallel_direct_writes valid, need set fi->direct_io
in current function. */
fi->parallel_direct_writes = 1;
fi->keep_cache = (fs.timeout != 0);
fi->noflush = (fs.timeout == 0 && (fi->flags & O_ACCMODE) == O_RDONLY);
}
static void sfs_create(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, fuse_file_info *fi) {
Inode& inode_p = get_inode(parent);
auto fd = openat(inode_p.fd, name,
(fi->flags | O_CREAT) & ~O_NOFOLLOW, mode);
if (fd == -1) {
auto err = errno;
if (err == ENFILE || err == EMFILE)
cerr << "ERROR: Reached maximum number of file descriptors." << endl;
fuse_reply_err(req, err);
return;
}
fi->fh = fd;
fuse_entry_param e;
auto err = do_lookup(parent, name, &e);
if (err) {
if (err == ENFILE || err == EMFILE)
cerr << "ERROR: Reached maximum number of file descriptors." << endl;
fuse_reply_err(req, err);
return;
}
Inode& inode = get_inode(e.ino);
lock_guard<mutex> g {inode.m};
inode.nopen++;
sfs_create_open_flags(fi);
if (fs.passthrough)
do_passthrough_open(req, e.ino, fd, fi);
fuse_reply_create(req, &e, fi);
}
static void sfs_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
fuse_file_info *fi) {
(void) ino;
int res;
int fd = dirfd(get_dir_handle(fi)->dp);
if (datasync)
res = fdatasync(fd);
else
res = fsync(fd);
fuse_reply_err(req, res == -1 ? errno : 0);
}
static void sfs_open(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) {
Inode& inode = get_inode(ino);
/* With writeback cache, kernel may send read requests even
when userspace opened write-only */
if (fs.timeout && (fi->flags & O_ACCMODE) == O_WRONLY) {
fi->flags &= ~O_ACCMODE;
fi->flags |= O_RDWR;
}
/* With writeback cache, O_APPEND is handled by the kernel. This
breaks atomicity (since the file may change in the underlying
filesystem, so that the kernel's idea of the end of the file
isn't accurate anymore). However, no process should modify the
file in the underlying filesystem once it has been read, so
this is not a problem. */
if (fs.timeout && fi->flags & O_APPEND)
fi->flags &= ~O_APPEND;
/* Unfortunately we cannot use inode.fd, because this was opened
with O_PATH (so it doesn't allow read/write access). */
char buf[64];
sprintf(buf, "/proc/self/fd/%i", inode.fd);
auto fd = open(buf, fi->flags & ~O_NOFOLLOW);
if (fd == -1) {
auto err = errno;
if (err == ENFILE || err == EMFILE)
cerr << "ERROR: Reached maximum number of file descriptors." << endl;
fuse_reply_err(req, err);
return;
}
lock_guard<mutex> g {inode.m};
inode.nopen++;
sfs_create_open_flags(fi);
fi->fh = fd;
if (fs.passthrough)
do_passthrough_open(req, ino, fd, fi);
fuse_reply_open(req, fi);
}
static void sfs_release(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) {
Inode& inode = get_inode(ino);
lock_guard<mutex> g {inode.m};
inode.nopen--;
/* Close the shared backing file on last file close of an inode */
if (inode.backing_id && !inode.nopen) {
if (fuse_passthrough_close(req, inode.backing_id) < 0) {
cerr << "DEBUG: fuse_passthrough_close failed for inode "
<< ino << " backing file " << inode.backing_id << endl;
} else if (fs.debug) {
cerr << "DEBUG: closed backing file " << inode.backing_id
<< " for inode " << ino << endl;
}
inode.backing_id = 0;
}
close(fi->fh);
fuse_reply_err(req, 0);
}
static void sfs_flush(fuse_req_t req, fuse_ino_t ino, fuse_file_info *fi) {
(void) ino;
auto res = close(dup(fi->fh));
fuse_reply_err(req, res == -1 ? errno : 0);
}