/
projfs.c
1842 lines (1533 loc) · 41.9 KB
/
projfs.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
/* Linux Projected Filesystem
Copyright (C) 2018-2019 GitHub, Inc.
See the NOTICE file distributed with this library for additional
information regarding copyright ownership.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library, in the file COPYING; if not,
see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <config.h>
#include <dirent.h>
#include <errno.h>
#include <inttypes.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/syscall.h>
#include <attr/xattr.h>
#include <unistd.h>
#include "fdtable.h"
#include "projfs.h"
#include <fuse3/fuse.h>
// TODO: make this value configurable
#define PROJ_WAIT_MSEC 5000
struct projfs {
char *lowerdir;
char *mountdir;
struct projfs_handlers handlers;
void *user_data;
pthread_mutex_t mutex;
struct fuse_session *session;
int lowerdir_fd;
pthread_t thread_id;
struct fdtable *fdtable;
int error;
};
typedef int (*projfs_handler_t)(struct projfs_event *);
struct projfs_dir {
DIR *dir;
long loc;
struct dirent *ent;
};
// NOTE: only functional within a FUSE file operation!
static inline struct projfs *get_fuse_context_projfs(void)
{
return (struct projfs *)fuse_get_context()->private_data;
}
// NOTE: only functional within a FUSE file operation!
static inline int get_fuse_context_lowerdir_fd(void)
{
return get_fuse_context_projfs()->lowerdir_fd;
}
/**
* @return 0 or a negative errno
*/
static int send_event(projfs_handler_t handler, uint64_t mask, pid_t pid,
const char *path, const char *target_path,
int fd, int perm)
{
struct projfs_event event;
int err;
if (handler == NULL)
return 0;
if (pid == 0)
pid = fuse_get_context()->pid;
event.fs = get_fuse_context_projfs();
event.mask = mask;
event.pid = pid;
event.path = path;
event.target_path = target_path;
event.fd = fd;
err = handler(&event);
if (err < 0) {
// TODO: replace with log output and only when log option set
fprintf(stderr, "projfs: event handler failed: %s; "
"event mask 0x%04" PRIx64 "-%08" PRIx64 ", "
"pid %d, path %s, target path %s\n",
strerror(-err), mask >> 32, mask & 0xFFFFFFFF, pid,
path, (target_path == NULL) ? "" : target_path);
}
else if (perm) {
err = (err == PROJFS_ALLOW) ? 0 : -EPERM;
}
return err;
}
/**
* @return 0 or a negative errno
*/
static int send_proj_event(uint64_t mask, const char *path, int fd)
{
projfs_handler_t handler =
get_fuse_context_projfs()->handlers.handle_proj_event;
return send_event(handler, mask, 0, path, NULL, fd, 0);
}
/**
* @return 0 or a negative errno
*/
static int send_notify_event(uint64_t mask, pid_t pid, const char *path,
const char *target_path)
{
projfs_handler_t handler =
get_fuse_context_projfs()->handlers.handle_notify_event;
return send_event(handler, mask, pid, path, target_path, 0, 0);
}
/**
* @return 0 or a negative errno
*/
static int send_perm_event(uint64_t mask, const char *path,
const char *target_path)
{
projfs_handler_t handler =
get_fuse_context_projfs()->handlers.handle_perm_event;
return send_event(handler, mask, 0, path, target_path, 0, 1);
}
#define PROJ_XATTR_PRE_NAME "user.projection."
#define PROJ_XATTR_PRE_LEN (sizeof(PROJ_XATTR_PRE_NAME) - 1)
#define PROJ_STATE_XATTR_NAME PROJ_XATTR_PRE_NAME"empty"
static int xattr_name_has_prefix(const char *name)
{
if (strncmp(name, PROJ_XATTR_PRE_NAME, PROJ_XATTR_PRE_LEN) == 0)
return 1;
return 0;
}
static int xattr_name_is_reserved(const char *name)
{
if (strcmp(name, PROJ_STATE_XATTR_NAME) == 0)
return 1;
// add other reserved names as they are defined
return 0;
}
static int get_xattr(int fd, const char *name, void *value, ssize_t *size)
{
if (fgetxattr(fd, name, value, *size) == -1) {
if (errno != ENOATTR)
return -1;
*size = -1;
}
return 0;
}
static int set_xattr(int fd, const char *name, const void *value,
ssize_t *size, int flags)
{
if (value == NULL || *size == 0) {
if (fremovexattr(fd, name) == -1) {
if (errno != ENOATTR)
return -1;
*size = -1;
}
return 0;
}
return fsetxattr(fd, name, value, *size, flags);
}
enum proj_state {
PROJ_STATE_ERROR = -1, /* invalid state */
PROJ_STATE_EMPTY, /* unopened (sparse file with metadata) */
PROJ_STATE_POPULATED, /* unmodified (hydrated with data) */
PROJ_STATE_MODIFIED /* no longer projected (fully local) */
};
#define PROJ_STATE_XATTR_VALUE_EMPTY 'y'
#define PROJ_STATE_XATTR_VALUE_POPULATED 'n'
/* The PROJ_STATE_XATTR_NAME xattr is removed for the MODIFIED state. */
static enum proj_state get_proj_state_xattr(int fd)
{
char value;
ssize_t size = sizeof(value);
if (get_xattr(fd, PROJ_STATE_XATTR_NAME, &value, &size) == -1)
return PROJ_STATE_ERROR;
if (size == -1)
return PROJ_STATE_MODIFIED;
switch (value) {
case PROJ_STATE_XATTR_VALUE_POPULATED:
return PROJ_STATE_POPULATED;
case PROJ_STATE_XATTR_VALUE_EMPTY:
return PROJ_STATE_EMPTY;
default:
errno = EINVAL;
return PROJ_STATE_ERROR;
}
}
static int set_proj_state_xattr(int fd, enum proj_state state, int flags)
{
char value;
void *valuep = &value;
ssize_t size = sizeof(value);
switch (state) {
case PROJ_STATE_POPULATED:
value = PROJ_STATE_XATTR_VALUE_POPULATED;
break;
case PROJ_STATE_EMPTY:
value = PROJ_STATE_XATTR_VALUE_EMPTY;
break;
case PROJ_STATE_MODIFIED:
valuep = NULL;
size = 0;
break;
default:
case PROJ_STATE_ERROR:
errno = EINVAL;
return -1;
}
return set_xattr(fd, PROJ_STATE_XATTR_NAME, valuep, &size, flags);
}
struct proj_state_lock {
int lock_fd;
enum proj_state state;
};
/**
* Acquires a lock on path and populates the supplied proj_state_lock argument
* with the open and locked fd, and state based on the
* PROJ_STATE_XATTR_NAME xattr.
*
* @param state_lock structure to fill out (zeroed by this function)
* @param path path relative to lowerdir to lock and open
* @param mode filemode to open path with; the fd populated in user will have
* this mode
* @return 0 or an errno
*/
static int acquire_proj_state_lock(struct proj_state_lock *state_lock,
const char *path, mode_t mode)
{
enum proj_state state;
int err, wait_ms;
struct timespec ts;
memset(state_lock, 0, sizeof(*state_lock));
state_lock->lock_fd = openat(get_fuse_context_lowerdir_fd(),
path, mode);
if (state_lock->lock_fd == -1)
return errno;
wait_ms = PROJ_WAIT_MSEC;
retry_flock:
// TODO: may conflict with locks held by clients; use internal locks
err = flock(state_lock->lock_fd, LOCK_EX | LOCK_NB);
if (err == -1) {
if (errno == EWOULDBLOCK && wait_ms > 0) {
/* sleep 100ms, retry */
ts.tv_sec = 0;
ts.tv_nsec = 1000 * 1000 * 100;
nanosleep(&ts, NULL);
wait_ms -= 100;
goto retry_flock;
}
err = errno;
goto out_close;
}
state = get_proj_state_xattr(state_lock->lock_fd);
if (state == PROJ_STATE_ERROR) {
err = errno;
goto out_close;
}
state_lock->state = state;
return 0;
out_close:
close(state_lock->lock_fd);
return err;
}
/**
* Closes the open fd associated with state_lock, which in turn releases any
* locks associated with the lock_fd.
*
* @param state_lock projection state structure to clean up
*/
static void release_proj_state_lock(struct proj_state_lock *state_lock)
{
if (state_lock->lock_fd == -1)
return;
close(state_lock->lock_fd);
state_lock->lock_fd = -1;
}
/**
* Projects a path by notifying the provider with the given event mask. If the
* provider succeeds, updates the projection state on the path.
*
* @param state_lock current projection state and lock held on inode
* @param path the path of the inode whose projection state should be updated
* @param isdir 1 if the path is a directory; 0 otherwise
* @param state projection state to which the inode should be updated
* @return 0 or an errno
*/
static int project_locked_path(struct proj_state_lock *state_lock,
const char *path, int isdir,
enum proj_state state)
{
int res;
if (isdir || state == PROJ_STATE_POPULATED) {
uint64_t event_mask = PROJFS_CREATE;
if (isdir)
event_mask |= PROJFS_ONDIR;
res = send_proj_event(event_mask, path, state_lock->lock_fd);
} else {
res = send_perm_event(PROJFS_OPEN_PERM, path, NULL);
}
if (res < 0)
return -res;
if (state == PROJ_STATE_POPULATED) {
res = set_proj_state_xattr(state_lock->lock_fd, state,
XATTR_REPLACE);
} else {
res = set_proj_state_xattr(state_lock->lock_fd, state, 0);
}
if (res == -1)
return errno;
state_lock->state = state;
return 0;
}
/**
* Return a copy of path with the last component removed (e.g. "x/y/z" will
* yield "x/y"). If path has only one component, returns ".".
*
* The caller is responsible for freeing the returned string.
*
* @param path path to get parent directory of
* @return name of parent of path; may be NULL if strdup or strndup fails
*/
static char *get_path_parent(char const *path)
{
const char *last = strrchr(path, '/');
if (!last)
return strdup(".");
else
return strndup(path, last - path);
}
/**
* Project a directory. Takes the path, and a flag indicating whether the
* directory is the parent of the path, or the path itself.
*
* @param op op name (for debugging)
* @param path path within lowerdir (from make_relative_path())
* @param parent 1 if we should look at the parent directory containing path, 0
* if we look at path itself
* @return 0 or an errno
*/
static int project_dir(const char *op, const char *path, int parent)
{
struct proj_state_lock state_lock;
int res;
char *lock_path;
(void)op;
if (parent)
lock_path = get_path_parent(path);
else
lock_path = strdup(path);
if (lock_path == NULL)
return errno;
res = acquire_proj_state_lock(&state_lock, lock_path,
O_RDONLY | O_DIRECTORY);
if (res != 0)
goto out;
if (state_lock.state != PROJ_STATE_EMPTY)
goto out_release;
// directories skip intermediate state; either empty or fully local
res = project_locked_path(&state_lock, lock_path, 1,
PROJ_STATE_MODIFIED);
out_release:
release_proj_state_lock(&state_lock);
out:
free(lock_path);
return res;
}
/**
* Project a file. Takes the lower path.
*
* @param op op name (for debugging)
* @param path the lower path (from lowerpath)
* @param state the projection state to apply (populated or modified)
* @return 0 or an errno
*/
static int project_file(const char *op, const char *path,
enum proj_state state)
{
struct proj_state_lock state_lock;
int res;
(void)op;
/* Pass O_NOFOLLOW so we receive ELOOP if path is an existing symlink,
* which we want to ignore, and request a write mode so we receive
* EISDIR if path is a directory.
*/
res = acquire_proj_state_lock(&state_lock, path, O_RDWR | O_NOFOLLOW);
if (res != 0) {
if (res == ELOOP)
return 0;
else
return res;
}
// hydrate empty placeholder file
if (state_lock.state == PROJ_STATE_EMPTY) {
struct stat st;
int reset_mtime;
reset_mtime = (fstat(state_lock.lock_fd, &st) == 0);
res = project_locked_path(&state_lock, path, 0,
PROJ_STATE_POPULATED);
if (res == 0 && reset_mtime) {
struct timespec times[2];
times[0].tv_nsec = UTIME_OMIT;
memcpy(×[1], &st.st_mtim, sizeof(times[1]));
futimens(state_lock.lock_fd, times); // best effort
}
}
// if requested, convert hydrated file to fully local, modified file
if (res == 0 && state_lock.state == PROJ_STATE_POPULATED &&
state == PROJ_STATE_MODIFIED) {
res = project_locked_path(&state_lock, path, 0, state);
}
release_proj_state_lock(&state_lock);
return res;
}
/**
* Makes a path from FUSE usable as a relative path to lowerdir_fd. Removes
* any leading forward slashes. If the resulting path is empty, returns ".".
* */
static inline const char *make_relative_path(const char *path)
{
while (*path == '/')
++path;
if (*path == '\0')
path = ".";
return path;
}
// filesystem ops
static int projfs_op_getattr(char const *path, struct stat *attr,
struct fuse_file_info *fi)
{
int res;
if (fi)
res = fstat(fi->fh, attr);
else {
path = make_relative_path(path);
res = project_dir("getattr", path, 1);
if (res)
return -res;
res = fstatat(get_fuse_context_lowerdir_fd(), path, attr,
AT_SYMLINK_NOFOLLOW);
}
return res == -1 ? -errno : 0;
}
static int projfs_op_readlink(char const *path, char *buf, size_t size)
{
int res;
path = make_relative_path(path);
res = project_dir("readlink", path, 1);
if (res)
return -res;
res = readlinkat(get_fuse_context_lowerdir_fd(), path, buf, size - 1);
if (res == -1)
return -errno;
buf[res] = 0;
return 0;
}
static int projfs_op_link(char const *src, char const *dst)
{
int lowerdir_fd;
int res;
/* NOTE: We require lowerdir to be a directory, so this should
* fail when src is an empty path, as we expect.
*/
src = make_relative_path(src);
res = project_dir("link", src, 1);
if (res)
return -res;
/* hydrate the source file before adding a hard link to it, otherwise
* a user could access the newly created link and end up modifying the
* non-hydrated placeholder */
res = project_file("link", src, PROJ_STATE_POPULATED);
if (res)
return -res;
dst = make_relative_path(dst);
res = project_dir("link2", dst, 1);
if (res)
return -res;
lowerdir_fd = get_fuse_context_lowerdir_fd();
res = linkat(lowerdir_fd, src, lowerdir_fd, dst, 0);
if (res == -1)
return -errno;
// do not report event handler errors after successful link op
(void)send_notify_event(PROJFS_CREATE | PROJFS_ONLINK, 0, src, dst);
return 0;
}
static void *projfs_op_init(struct fuse_conn_info *conn,
struct fuse_config *cfg)
{
(void)conn;
cfg->entry_timeout = 0;
cfg->attr_timeout = 0;
cfg->negative_timeout = 0;
cfg->use_ino = 1;
return get_fuse_context_projfs();
}
#define has_write_mode(fi) ((fi)->flags & (O_WRONLY | O_RDWR))
static int projfs_op_flush(char const *path, struct fuse_file_info *fi)
{
int res, err;
res = close(dup(fi->fh));
err = errno; // errno may be changed by fdtable realloc
if (has_write_mode(fi)) {
// do not report table realloc errors after successful close op
(void)fdtable_replace(get_fuse_context_projfs()->fdtable,
fi->fh, fuse_get_context()->pid);
}
return res == -1 ? -err : 0;
}
static int projfs_op_fsync(char const *path, int datasync,
struct fuse_file_info *fi)
{
int res;
(void)path;
if (datasync)
res = fdatasync(fi->fh);
else
res = fsync(fi->fh);
return res == -1 ? -errno : 0;
}
static int projfs_op_mknod(char const *path, mode_t mode, dev_t rdev)
{
int res;
(void)rdev;
path = make_relative_path(path);
res = project_dir("mknod", path, 1);
if (res)
return -res;
if (S_ISFIFO(mode))
res = mkfifoat(get_fuse_context_lowerdir_fd(), path, mode);
else
return -ENOSYS;
return res == -1 ? -errno : 0;
}
static int projfs_op_symlink(char const *link, char const *path)
{
int res;
path = make_relative_path(path);
res = project_dir("symlink", path, 1);
if (res)
return -res;
res = symlinkat(link, get_fuse_context_lowerdir_fd(), path);
return res == -1 ? -errno : 0;
}
static int projfs_op_create(char const *path, mode_t mode,
struct fuse_file_info *fi)
{
/* FUSE sets fi.flags = O_CREAT | O_EXCL | O_WRONLY in fuse_lib_mknod,
* untouched in fuse_lib_create where it comes straight from
* FUSE_CREATE. In Linux 4.9 at least, we never hit a codepath where
* we send FUSE_CREATE without first checking that O_CREAT is set.
* There's no guarantee O_EXCL (or O_TRUNC) are set, though, so we need
* to hydrate it if it exists. */
int flags = fi->flags & ~O_NOFOLLOW;
int res;
int fd;
path = make_relative_path(path);
res = project_dir("create", path, 1);
if (res)
return -res;
res = project_file("create", path, PROJ_STATE_POPULATED);
if (res && res != ENOENT)
return -res;
fd = openat(get_fuse_context_lowerdir_fd(), path, flags, mode);
if (fd == -1)
return -errno;
fi->fh = fd;
if (has_write_mode(fi)) {
// do not report table realloc errors after successful open op
(void)fdtable_insert(get_fuse_context_projfs()->fdtable,
fd, fuse_get_context()->pid);
}
// do not report event handler errors after successful open op
(void)send_notify_event(PROJFS_CREATE, 0, path, NULL);
return 0;
}
static int projfs_op_open(char const *path, struct fuse_file_info *fi)
{
int flags = fi->flags & ~O_NOFOLLOW;
int res;
int fd;
path = make_relative_path(path);
res = project_dir("open", path, 1);
if (res)
return -res;
/* Per above, allow hydration to fail with ENOENT; if the file
* operation should fail for that reason (i.e. O_CREAT is not specified
* and the file doesn't exist), we'll return the failure from openat(2)
* below.
*/
res = project_file("open", path,
has_write_mode(fi) ? PROJ_STATE_MODIFIED
: PROJ_STATE_POPULATED);
if (res) {
// if path was a directory, try projecting it instead
if (res == EISDIR)
res = project_dir("open", path, 0);
if (res != ENOENT)
return -res;
}
fd = openat(get_fuse_context_lowerdir_fd(), path, flags);
if (fd == -1)
return -errno;
if (has_write_mode(fi)) {
// do not report table realloc errors after successful open op
(void)fdtable_insert(get_fuse_context_projfs()->fdtable,
fd, fuse_get_context()->pid);
}
fi->fh = fd;
return 0;
}
static int projfs_op_statfs(char const *path, struct statvfs *buf)
{
int res;
(void)path;
// TODO: should we return our own filesystem's global info?
res = fstatvfs(get_fuse_context_lowerdir_fd(), buf);
return res == -1 ? -errno : 0;
}
static int projfs_op_read_buf(char const *path, struct fuse_bufvec **bufp,
size_t size, off_t off,
struct fuse_file_info *fi)
{
struct fuse_bufvec *src = malloc(sizeof(*src));
(void) path;
if (!src)
return -errno;
*src = FUSE_BUFVEC_INIT(size);
src->buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
src->buf[0].fd = fi->fh;
src->buf[0].pos = off;
*bufp = src;
return 0;
}
static int projfs_op_write_buf(char const *path, struct fuse_bufvec *src,
off_t off, struct fuse_file_info *fi)
{
struct fuse_bufvec buf = FUSE_BUFVEC_INIT(fuse_buf_size(src));
(void)path;
buf.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
buf.buf[0].fd = fi->fh;
buf.buf[0].pos = off;
return fuse_buf_copy(&buf, src, FUSE_BUF_SPLICE_NONBLOCK);
}
static int projfs_op_release(char const *path, struct fuse_file_info *fi)
{
int res, err;
pid_t pid = 0;
res = close(fi->fh);
err = errno; // errno may be changed by fdtable realloc
if (has_write_mode(fi)) {
// do not report table realloc errors after successful close op
(void)fdtable_remove(get_fuse_context_projfs()->fdtable,
fi->fh, &pid);
}
// return value is ignored by libfuse, but be consistent anyway
if (res == -1)
return -err;
if (has_write_mode(fi)) {
// do not report event handler errors after successful close op
(void)send_notify_event(PROJFS_CLOSE_WRITE, pid,
make_relative_path(path), NULL);
}
return 0;
}
static int projfs_op_unlink(char const *path)
{
int res;
path = make_relative_path(path);
res = send_perm_event(PROJFS_DELETE_PERM, path, NULL);
if (res < 0)
return res;
res = project_dir("unlink", path, 1);
if (res)
return -res;
res = unlinkat(get_fuse_context_lowerdir_fd(), path, 0);
return res == -1 ? -errno : 0;
}
static int projfs_op_mkdir(char const *path, mode_t mode)
{
int res;
path = make_relative_path(path);
res = project_dir("mkdir", path, 1);
if (res)
return -res;
res = mkdirat(get_fuse_context_lowerdir_fd(), path, mode);
if (res == -1)
return -errno;
// do not report event handler errors after successful mkdir op
(void)send_notify_event(PROJFS_CREATE | PROJFS_ONDIR, 0, path, NULL);
return 0;
}
static int projfs_op_rmdir(char const *path)
{
int res;
path = make_relative_path(path);
res = send_perm_event(PROJFS_DELETE_PERM | PROJFS_ONDIR, path, NULL);
if (res < 0)
return res;
res = project_dir("rmdir", path, 1);
if (res)
return -res;
res = unlinkat(get_fuse_context_lowerdir_fd(), path, AT_REMOVEDIR);
return res == -1 ? -errno : 0;
}
static int projfs_op_rename(char const *src, char const *dst,
unsigned int flags)
{
uint64_t mask = PROJFS_MOVE;
int lowerdir_fd;
int res;
src = make_relative_path(src);
res = project_dir("rename", src, 1);
if (res)
return -res;
// always convert to fully local file before renaming
res = project_file("rename", src, PROJ_STATE_MODIFIED);
if (res == EISDIR)
mask |= PROJFS_ONDIR;
else if (res)
return -res;
dst = make_relative_path(dst);
res = project_dir("rename2", dst, 1);
if (res)
return -res;
// TODO: for non Linux, use renameat(); fail if flags != 0
lowerdir_fd = get_fuse_context_lowerdir_fd();
res = syscall(SYS_renameat2, lowerdir_fd, src, lowerdir_fd, dst,
flags);
if (res == -1)
return -errno;
// do not report event handler errors after successful rename op
(void)send_notify_event(mask, 0, src, dst);
return 0;
}
static int projfs_op_opendir(char const *path, struct fuse_file_info *fi)
{
int flags = O_DIRECTORY | O_NOFOLLOW | O_RDONLY;
struct projfs_dir *d;
int fd;
int res = 0;
int err = 0;
path = make_relative_path(path);
res = project_dir("opendir", path, 1);
if (res)
return -res;
res = project_dir("opendir2", path, 0);
if (res)
return -res;
d = calloc(1, sizeof(*d));
if (!d) {
res = -1;
goto out;
}
fd = openat(get_fuse_context_lowerdir_fd(), path, flags);
if (fd == -1) {
res = -1;
goto out_free;
}
d->dir = fdopendir(fd);
if (!d->dir) {
res = -1;
err = errno;
goto out_close;
}
fi->fh = (uintptr_t)d;
goto out;
out_close:
close(fd); // report fopendir() error and ignore any from close()
out_free:
free(d);
out:
return res == -1 ? -(err > 0 ? err : errno) : res;
}
static int projfs_op_readdir(char const *path, void *buf,
fuse_fill_dir_t filler, off_t off,
struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
{
int err = 0;
struct projfs_dir *d = (struct projfs_dir *)fi->fh;
(void)path;
if (off != d->loc) {
seekdir(d->dir, off);
d->ent = NULL;
d->loc = off;
}
while (1) {
struct stat attr;
enum fuse_fill_dir_flags filled = 0;
if (!d->ent) {
errno = 0;
d->ent = readdir(d->dir);
if (!d->ent) {
err = errno;
break;
}
}
if (flags & FUSE_READDIR_PLUS) {
int res = fstatat(
dirfd(d->dir), d->ent->d_name, &attr,
AT_SYMLINK_NOFOLLOW);
// TODO: break and report errors from fstatat()?
if (res != -1)
filled = FUSE_FILL_DIR_PLUS;
}
if (filled == 0) {
memset(&attr, 0, sizeof(attr));
attr.st_ino = d->ent->d_ino;
attr.st_mode = d->ent->d_type << 12;
}
if (filler(buf, d->ent->d_name, &attr, d->ent->d_off, filled))
break;
d->loc = d->ent->d_off;
d->ent = NULL;
}
return -err;
}
static int projfs_op_releasedir(char const *path, struct fuse_file_info *fi)
{
struct projfs_dir *d = (struct projfs_dir *)fi->fh;
int res = closedir(d->dir);
(void)path;
free(d);
// return value is ignored by libfuse, but be consistent anyway