38#define FUSE_USE_VERSION 34
56#include "passthrough_helpers.h"
62#if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && !defined __cplusplus
63_Static_assert(
sizeof(
fuse_ino_t) >=
sizeof(uintptr_t),
64 "fuse_ino_t too small to hold uintptr_t values!");
66struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct \
67 {
unsigned _uintptr_to_must_hold_fuse_ino_t:
68 ((
sizeof(
fuse_ino_t) >=
sizeof(uintptr_t)) ? 1 : -1); };
72 struct lo_inode *next;
73 struct lo_inode *prev;
87 pthread_mutex_t mutex;
99static const struct fuse_opt lo_opts[] = {
101 offsetof(
struct lo_data, writeback), 1 },
103 offsetof(
struct lo_data, writeback), 0 },
105 offsetof(
struct lo_data, source), 0 },
107 offsetof(
struct lo_data, flock), 1 },
109 offsetof(
struct lo_data, flock), 0 },
111 offsetof(
struct lo_data, xattr), 1 },
113 offsetof(
struct lo_data, xattr), 0 },
115 offsetof(
struct lo_data, timeout), 0 },
117 offsetof(
struct lo_data, timeout_set), 1 },
119 offsetof(
struct lo_data, cache), CACHE_NEVER },
121 offsetof(
struct lo_data, cache), CACHE_NORMAL },
123 offsetof(
struct lo_data, cache), CACHE_ALWAYS },
128static void passthrough_ll_help(
void)
131" -o writeback Enable writeback\n"
132" -o no_writeback Disable write back\n"
133" -o source=/home/dir Source directory to be mounted\n"
134" -o flock Enable flock\n"
135" -o no_flock Disable flock\n"
136" -o xattr Enable xattr\n"
137" -o no_xattr Disable xattr\n"
138" -o timeout=1.0 Caching timeout\n"
139" -o timeout=0/1 Timeout is set\n"
140" -o cache=never Disable cache\n"
141" -o cache=auto Auto enable cache\n"
142" -o cache=always Cache always\n");
153 return &lo_data(req)->root;
155 return (
struct lo_inode *) (uintptr_t) ino;
160 return lo_inode(req, ino)->fd;
165 return lo_data(req)->debug != 0;
168static void lo_init(
void *userdata,
171 struct lo_data *lo = (
struct lo_data*) userdata;
179 fuse_log(FUSE_LOG_DEBUG,
"lo_init: activating writeback\n");
184 fuse_log(FUSE_LOG_DEBUG,
"lo_init: activating flock locks\n");
189static void lo_destroy(
void *userdata)
191 struct lo_data *lo = (
struct lo_data*) userdata;
193 while (lo->root.next != &lo->root) {
194 struct lo_inode* next = lo->root.next;
195 lo->root.next = next->next;
205 struct lo_data *lo = lo_data(req);
209 res = fstatat(lo_fd(req, ino),
"", &buf, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
221 struct lo_inode *inode = lo_inode(req, ino);
225 if (valid & FUSE_SET_ATTR_MODE) {
227 res = fchmod(fi->
fh, attr->st_mode);
229 sprintf(procname,
"/proc/self/fd/%i", ifd);
230 res = chmod(procname, attr->st_mode);
235 if (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
236 uid_t uid = (valid & FUSE_SET_ATTR_UID) ?
237 attr->st_uid : (uid_t) -1;
238 gid_t gid = (valid & FUSE_SET_ATTR_GID) ?
239 attr->st_gid : (gid_t) -1;
241 res = fchownat(ifd,
"", uid, gid,
242 AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
246 if (valid & FUSE_SET_ATTR_SIZE) {
248 res = ftruncate(fi->
fh, attr->st_size);
250 sprintf(procname,
"/proc/self/fd/%i", ifd);
251 res = truncate(procname, attr->st_size);
256 if (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
257 struct timespec tv[2];
261 tv[0].tv_nsec = UTIME_OMIT;
262 tv[1].tv_nsec = UTIME_OMIT;
264 if (valid & FUSE_SET_ATTR_ATIME_NOW)
265 tv[0].tv_nsec = UTIME_NOW;
266 else if (valid & FUSE_SET_ATTR_ATIME)
267 tv[0] = attr->st_atim;
269 if (valid & FUSE_SET_ATTR_MTIME_NOW)
270 tv[1].tv_nsec = UTIME_NOW;
271 else if (valid & FUSE_SET_ATTR_MTIME)
272 tv[1] = attr->st_mtim;
275 res = futimens(fi->
fh, tv);
277 sprintf(procname,
"/proc/self/fd/%i", ifd);
278 res = utimensat(AT_FDCWD, procname, tv, 0);
284 return lo_getattr(req, ino, fi);
291static struct lo_inode *lo_find(
struct lo_data *lo,
struct stat *st)
294 struct lo_inode *ret = NULL;
296 pthread_mutex_lock(&lo->mutex);
297 for (p = lo->root.next; p != &lo->root; p = p->next) {
298 if (p->ino == st->st_ino && p->dev == st->st_dev) {
299 assert(p->refcount > 0);
305 pthread_mutex_unlock(&lo->mutex);
315 struct lo_data *lo = lo_data(req);
316 struct lo_inode *inode;
318 memset(e, 0,
sizeof(*e));
322 newfd = openat(lo_fd(req, parent), name, O_PATH | O_NOFOLLOW);
326 res = fstatat(newfd,
"", &e->
attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
330 inode = lo_find(lo_data(req), &e->
attr);
335 struct lo_inode *prev, *next;
338 inode = calloc(1,
sizeof(
struct lo_inode));
344 inode->ino = e->
attr.st_ino;
345 inode->dev = e->
attr.st_dev;
347 pthread_mutex_lock(&lo->mutex);
354 pthread_mutex_unlock(&lo->mutex);
356 e->
ino = (uintptr_t) inode;
359 fuse_log(FUSE_LOG_DEBUG,
" %lli/%s -> %lli\n",
360 (
unsigned long long) parent, name, (
unsigned long long) e->
ino);
377 fuse_log(FUSE_LOG_DEBUG,
"lo_lookup(parent=%" PRIu64
", name=%s)\n",
380 err = lo_do_lookup(req, parent, name, &e);
388 const char *name, mode_t mode, dev_t rdev,
393 struct lo_inode *dir = lo_inode(req, parent);
396 res = mknod_wrapper(dir->fd, name, link, mode, rdev);
402 saverr = lo_do_lookup(req, parent, name, &e);
407 fuse_log(FUSE_LOG_DEBUG,
" %lli/%s -> %lli\n",
408 (
unsigned long long) parent, name, (
unsigned long long) e.
ino);
418 const char *name, mode_t mode, dev_t rdev)
420 lo_mknod_symlink(req, parent, name, mode, rdev, NULL);
426 lo_mknod_symlink(req, parent, name, S_IFDIR | mode, 0, NULL);
429static void lo_symlink(
fuse_req_t req,
const char *link,
432 lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link);
439 struct lo_data *lo = lo_data(req);
440 struct lo_inode *inode = lo_inode(req, ino);
449 sprintf(procname,
"/proc/self/fd/%i", inode->fd);
450 res = linkat(AT_FDCWD, procname, lo_fd(req, parent), name,
455 res = fstatat(inode->fd,
"", &e.
attr, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
459 pthread_mutex_lock(&lo->mutex);
461 pthread_mutex_unlock(&lo->mutex);
462 e.
ino = (uintptr_t) inode;
465 fuse_log(FUSE_LOG_DEBUG,
" %lli/%s -> %lli\n",
466 (
unsigned long long) parent, name,
467 (
unsigned long long) e.
ino);
481 res = unlinkat(lo_fd(req, parent), name, AT_REMOVEDIR);
497 res = renameat(lo_fd(req, parent), name,
498 lo_fd(req, newparent), newname);
507 res = unlinkat(lo_fd(req, parent), name, 0);
512static void unref_inode(
struct lo_data *lo,
struct lo_inode *inode, uint64_t n)
517 pthread_mutex_lock(&lo->mutex);
518 assert(inode->refcount >= n);
519 inode->refcount -= n;
520 if (!inode->refcount) {
521 struct lo_inode *prev, *next;
528 pthread_mutex_unlock(&lo->mutex);
533 pthread_mutex_unlock(&lo->mutex);
539 struct lo_data *lo = lo_data(req);
540 struct lo_inode *inode = lo_inode(req, ino);
543 fuse_log(FUSE_LOG_DEBUG,
" forget %lli %lli -%lli\n",
544 (
unsigned long long) ino,
545 (
unsigned long long) inode->refcount,
546 (
unsigned long long) nlookup);
549 unref_inode(lo, inode, nlookup);
554 lo_forget_one(req, ino, nlookup);
558static void lo_forget_multi(
fuse_req_t req,
size_t count,
559 struct fuse_forget_data *forgets)
563 for (i = 0; i < count; i++)
564 lo_forget_one(req, forgets[i].ino, forgets[i].nlookup);
570 char buf[PATH_MAX + 1];
573 res = readlinkat(lo_fd(req, ino),
"", buf,
sizeof(buf));
577 if (res ==
sizeof(buf))
587 struct dirent *entry;
593 return (
struct lo_dirp *) (uintptr_t) fi->
fh;
599 struct lo_data *lo = lo_data(req);
603 d = calloc(1,
sizeof(
struct lo_dirp));
607 fd = openat(lo_fd(req, ino),
".", O_RDONLY);
611 d->dp = fdopendir(fd);
618 fi->
fh = (uintptr_t) d;
619 if (lo->cache == CACHE_ALWAYS)
635static int is_dot_or_dotdot(
const char *name)
637 return name[0] ==
'.' && (name[1] ==
'\0' ||
638 (name[1] ==
'.' && name[2] ==
'\0'));
644 struct lo_dirp *d = lo_dirp(fi);
652 buf = calloc(1, size);
659 if (offset != d->offset) {
660 seekdir(d->dp, offset);
671 d->entry = readdir(d->dp);
681 nextoff = d->entry->d_off;
682 name = d->entry->d_name;
686 if (is_dot_or_dotdot(name)) {
688 .
attr.st_ino = d->entry->d_ino,
689 .attr.st_mode = d->entry->d_type << 12,
692 err = lo_do_lookup(req, ino, name, &e);
702 .st_ino = d->entry->d_ino,
703 .st_mode = d->entry->d_type << 12,
710 lo_forget_one(req, entry_ino, 1);
727 if (err && rem == size)
737 lo_do_readdir(req, ino, size, offset, fi, 0);
743 lo_do_readdir(req, ino, size, offset, fi, 1);
748 struct lo_dirp *d = lo_dirp(fi);
759 struct lo_data *lo = lo_data(req);
764 fuse_log(FUSE_LOG_DEBUG,
"lo_create(parent=%" PRIu64
", name=%s)\n",
767 fd = openat(lo_fd(req, parent), name,
768 (fi->
flags | O_CREAT) & ~O_NOFOLLOW, mode);
773 if (lo->cache == CACHE_NEVER)
775 else if (lo->cache == CACHE_ALWAYS)
778 err = lo_do_lookup(req, parent, name, &e);
789 int fd = dirfd(lo_dirp(fi)->dp);
802 struct lo_data *lo = lo_data(req);
805 fuse_log(FUSE_LOG_DEBUG,
"lo_open(ino=%" PRIu64
", flags=%d)\n",
810 if (lo->writeback && (fi->
flags & O_ACCMODE) == O_WRONLY) {
811 fi->
flags &= ~O_ACCMODE;
821 if (lo->writeback && (fi->
flags & O_APPEND))
822 fi->
flags &= ~O_APPEND;
824 sprintf(buf,
"/proc/self/fd/%i", lo_fd(req, ino));
825 fd = open(buf, fi->
flags & ~O_NOFOLLOW);
830 if (lo->cache == CACHE_NEVER)
832 else if (lo->cache == CACHE_ALWAYS)
849 res = close(dup(fi->
fh));
859 res = fdatasync(fi->
fh);
871 fuse_log(FUSE_LOG_DEBUG,
"lo_read(ino=%" PRIu64
", size=%zd, "
872 "off=%lu)\n", ino, size, (
unsigned long) offset);
894 fuse_log(FUSE_LOG_DEBUG,
"lo_write(ino=%" PRIu64
", size=%zd, off=%lu)\n",
895 ino, out_buf.
buf[0].
size, (
unsigned long)
off);
907 struct statvfs stbuf;
909 res = fstatvfs(lo_fd(req, ino), &stbuf);
919 int err = EOPNOTSUPP;
923 err = fallocate(fi->
fh, mode, offset, length);
927#elif defined(HAVE_POSIX_FALLOCATE)
933 err = posix_fallocate(fi->
fh, offset, length);
945 res = flock(fi->
fh, op);
955 struct lo_inode *inode = lo_inode(req, ino);
960 if (!lo_data(req)->xattr)
964 fuse_log(FUSE_LOG_DEBUG,
"lo_getxattr(ino=%" PRIu64
", name=%s size=%zd)\n",
968 sprintf(procname,
"/proc/self/fd/%i", inode->fd);
971 value = malloc(size);
975 ret = getxattr(procname, name, value, size);
984 ret = getxattr(procname, name, NULL, 0);
1005 struct lo_inode *inode = lo_inode(req, ino);
1010 if (!lo_data(req)->xattr)
1013 if (lo_debug(req)) {
1014 fuse_log(FUSE_LOG_DEBUG,
"lo_listxattr(ino=%" PRIu64
", size=%zd)\n",
1018 sprintf(procname,
"/proc/self/fd/%i", inode->fd);
1021 value = malloc(size);
1025 ret = listxattr(procname, value, size);
1034 ret = listxattr(procname, NULL, 0);
1052 const char *value,
size_t size,
int flags)
1055 struct lo_inode *inode = lo_inode(req, ino);
1060 if (!lo_data(req)->xattr)
1063 if (lo_debug(req)) {
1064 fuse_log(FUSE_LOG_DEBUG,
"lo_setxattr(ino=%" PRIu64
", name=%s value=%s size=%zd)\n",
1065 ino, name, value, size);
1068 sprintf(procname,
"/proc/self/fd/%i", inode->fd);
1070 ret = setxattr(procname, name, value, size, flags);
1071 saverr = ret == -1 ? errno : 0;
1080 struct lo_inode *inode = lo_inode(req, ino);
1085 if (!lo_data(req)->xattr)
1088 if (lo_debug(req)) {
1089 fuse_log(FUSE_LOG_DEBUG,
"lo_removexattr(ino=%" PRIu64
", name=%s)\n",
1093 sprintf(procname,
"/proc/self/fd/%i", inode->fd);
1095 ret = removexattr(procname, name);
1096 saverr = ret == -1 ? errno : 0;
1102#ifdef HAVE_COPY_FILE_RANGE
1112 fuse_log(FUSE_LOG_DEBUG,
"lo_copy_file_range(ino=%" PRIu64
"/fd=%lu, "
1113 "off=%lu, ino=%" PRIu64
"/fd=%lu, "
1114 "off=%lu, size=%zd, flags=0x%x)\n",
1115 ino_in, fi_in->
fh, off_in, ino_out, fi_out->
fh, off_out,
1118 res = copy_file_range(fi_in->
fh, &off_in, fi_out->
fh, &off_out, len,
1133 res = lseek(fi->
fh, off, whence);
1142 .destroy = lo_destroy,
1143 .lookup = lo_lookup,
1146 .symlink = lo_symlink,
1148 .unlink = lo_unlink,
1150 .rename = lo_rename,
1151 .forget = lo_forget,
1152 .forget_multi = lo_forget_multi,
1153 .getattr = lo_getattr,
1154 .setattr = lo_setattr,
1155 .readlink = lo_readlink,
1156 .opendir = lo_opendir,
1157 .readdir = lo_readdir,
1158 .readdirplus = lo_readdirplus,
1159 .releasedir = lo_releasedir,
1160 .fsyncdir = lo_fsyncdir,
1161 .create = lo_create,
1163 .release = lo_release,
1167 .write_buf = lo_write_buf,
1168 .statfs = lo_statfs,
1169 .fallocate = lo_fallocate,
1171 .getxattr = lo_getxattr,
1172 .listxattr = lo_listxattr,
1173 .setxattr = lo_setxattr,
1174 .removexattr = lo_removexattr,
1175#ifdef HAVE_COPY_FILE_RANGE
1176 .copy_file_range = lo_copy_file_range,
1181int main(
int argc,
char *argv[])
1184 struct fuse_session *se;
1187 struct lo_data lo = { .debug = 0,
1194 pthread_mutex_init(&lo.mutex, NULL);
1195 lo.root.next = lo.root.prev = &lo.root;
1197 lo.cache = CACHE_NORMAL;
1199 if (fuse_parse_cmdline(&args, &opts) != 0)
1201 if (opts.show_help) {
1202 printf(
"usage: %s [options] <mountpoint>\n\n", argv[0]);
1205 passthrough_ll_help();
1208 }
else if (opts.show_version) {
1215 if(opts.mountpoint == NULL) {
1216 printf(
"usage: %s [options] <mountpoint>\n", argv[0]);
1217 printf(
" %s --help\n", argv[0]);
1225 lo.debug = opts.debug;
1226 lo.root.refcount = 2;
1231 res = lstat(lo.source, &stat);
1233 fuse_log(FUSE_LOG_ERR,
"failed to stat source (\"%s\"): %m\n",
1237 if (!S_ISDIR(stat.st_mode)) {
1238 fuse_log(FUSE_LOG_ERR,
"source is not a directory\n");
1243 lo.source = strdup(
"/");
1245 fuse_log(FUSE_LOG_ERR,
"fuse: memory allocation failed\n");
1249 if (!lo.timeout_set) {
1260 lo.timeout = 86400.0;
1263 }
else if (lo.timeout < 0) {
1264 fuse_log(FUSE_LOG_ERR,
"timeout is negative (%lf)\n",
1269 lo.root.fd = open(lo.source, O_PATH);
1270 if (lo.root.fd == -1) {
1271 fuse_log(FUSE_LOG_ERR,
"open(\"%s\", O_PATH): %m\n",
1289 if (opts.singlethread)
1292 config.clone_fd = opts.clone_fd;
1293 config.max_idle_threads = opts.max_idle_threads;
1294 ret = fuse_session_loop_mt(se, &config);
1303 free(opts.mountpoint);
1306 if (lo.root.fd >= 0)
int fuse_set_signal_handlers(struct fuse_session *se)
size_t fuse_buf_size(const struct fuse_bufvec *bufv)
#define FUSE_CAP_WRITEBACK_CACHE
#define FUSE_CAP_EXPORT_SUPPORT
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
const char * fuse_pkgversion(void)
void fuse_remove_signal_handlers(struct fuse_session *se)
int fuse_daemonize(int foreground)
#define FUSE_CAP_FLOCK_LOCKS
void fuse_log(enum fuse_log_level level, const char *fmt,...)
void fuse_session_destroy(struct fuse_session *se)
int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
int fuse_reply_err(fuse_req_t req, int err)
void * fuse_req_userdata(fuse_req_t req)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
struct fuse_req * fuse_req_t
size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct fuse_entry_param *e, off_t off)
int fuse_reply_readlink(fuse_req_t req, const char *link)
int fuse_session_loop(struct fuse_session *se)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
void fuse_session_unmount(struct fuse_session *se)
void fuse_cmdline_help(void)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
void fuse_reply_none(fuse_req_t req)
void fuse_lowlevel_help(void)
int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
int fuse_reply_write(fuse_req_t req, size_t count)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, const struct fuse_file_info *fi)
int fuse_reply_lseek(fuse_req_t req, off_t off)
void fuse_lowlevel_version(void)
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
int fuse_reply_xattr(fuse_req_t req, size_t count)
void fuse_opt_free_args(struct fuse_args *args)
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
#define FUSE_ARGS_INIT(argc, argv)
enum fuse_buf_flags flags
unsigned int cache_readdir
void(* init)(void *userdata, struct fuse_conn_info *conn)