From fe4f9428fc403fa8b99051f52d84ea5bd13f3855 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Mon, 18 Jun 2018 19:31:43 +0200 Subject: libfuse: add copy_file_range() support Add support for the relatively new copy_file_range() syscall. Backend filesystems can now implement an efficient way of cloning/duplicating data ranges within files. See 'man 2 copy_file_range' for more details. --- lib/fuse.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'lib/fuse.c') diff --git a/lib/fuse.c b/lib/fuse.c index b88ef1a..946ae2f 100644 --- a/lib/fuse.c +++ b/lib/fuse.c @@ -2359,6 +2359,29 @@ int fuse_fs_fallocate(struct fuse_fs *fs, const char *path, int mode, return -ENOSYS; } +ssize_t fuse_fs_copy_file_range(struct fuse_fs *fs, const char *path_in, + struct fuse_file_info *fi_in, off_t off_in, + const char *path_out, + struct fuse_file_info *fi_out, off_t off_out, + size_t len, int flags) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.copy_file_range) { + if (fs->debug) + fprintf(stderr, "copy_file_range from %s:%llu to " + "%s:%llu, length: %llu\n", + path_in, + (unsigned long long) off_in, + path_out, + (unsigned long long) off_out, + (unsigned long long) len); + + return fs->op.copy_file_range(path_in, fi_in, off_in, path_out, + fi_out, off_out, len, flags); + } else + return -ENOSYS; +} + static int is_open(struct fuse *f, fuse_ino_t dir, const char *name) { struct node *node; @@ -4290,6 +4313,45 @@ static void fuse_lib_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, reply_err(req, err); } +static void fuse_lib_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in, + off_t off_in, struct fuse_file_info *fi_in, + fuse_ino_t nodeid_out, off_t off_out, + struct fuse_file_info *fi_out, size_t len, + int flags) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path_in, *path_out; + int err; + ssize_t res; + + err = get_path_nullok(f, nodeid_in, &path_in); + if (err) { + reply_err(req, err); + return; + } + + err = get_path_nullok(f, nodeid_out, &path_out); + if (err) { + free_path(f, nodeid_in, path_in); + reply_err(req, err); + return; + } + + fuse_prepare_interrupt(f, req, &d); + res = fuse_fs_copy_file_range(f->fs, path_in, fi_in, off_in, path_out, + fi_out, off_out, len, flags); + fuse_finish_interrupt(f, req, &d); + + if (res >= 0) + fuse_reply_write(req, res); + else + reply_err(req, res); + + free_path(f, nodeid_in, path_in); + free_path(f, nodeid_out, path_out); +} + static int clean_delay(struct fuse *f) { /* @@ -4386,6 +4448,7 @@ static struct fuse_lowlevel_ops fuse_path_ops = { .ioctl = fuse_lib_ioctl, .poll = fuse_lib_poll, .fallocate = fuse_lib_fallocate, + .copy_file_range = fuse_lib_copy_file_range, }; int fuse_notify_poll(struct fuse_pollhandle *ph) -- cgit v1.2.3