Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> |
| 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/sched/signal.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/compat.h> |
| 18 | #include <linux/swap.h> |
| 19 | #include <linux/falloc.h> |
| 20 | #include <linux/uio.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 21 | #include <linux/fs.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 23 | static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags, |
| 24 | struct fuse_page_desc **desc) |
| 25 | { |
| 26 | struct page **pages; |
| 27 | |
| 28 | pages = kzalloc(npages * (sizeof(struct page *) + |
| 29 | sizeof(struct fuse_page_desc)), flags); |
| 30 | *desc = (void *) (pages + npages); |
| 31 | |
| 32 | return pages; |
| 33 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 34 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 35 | static int fuse_send_open(struct fuse_mount *fm, u64 nodeid, struct file *file, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 36 | int opcode, struct fuse_open_out *outargp) |
| 37 | { |
| 38 | struct fuse_open_in inarg; |
| 39 | FUSE_ARGS(args); |
| 40 | |
| 41 | memset(&inarg, 0, sizeof(inarg)); |
| 42 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 43 | if (!fm->fc->atomic_o_trunc) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 44 | inarg.flags &= ~O_TRUNC; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 45 | args.opcode = opcode; |
| 46 | args.nodeid = nodeid; |
| 47 | args.in_numargs = 1; |
| 48 | args.in_args[0].size = sizeof(inarg); |
| 49 | args.in_args[0].value = &inarg; |
| 50 | args.out_numargs = 1; |
| 51 | args.out_args[0].size = sizeof(*outargp); |
| 52 | args.out_args[0].value = outargp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 54 | return fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 55 | } |
| 56 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 57 | struct fuse_release_args { |
| 58 | struct fuse_args args; |
| 59 | struct fuse_release_in inarg; |
| 60 | struct inode *inode; |
| 61 | }; |
| 62 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 63 | struct fuse_file *fuse_file_alloc(struct fuse_mount *fm) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | { |
| 65 | struct fuse_file *ff; |
| 66 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 67 | ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 68 | if (unlikely(!ff)) |
| 69 | return NULL; |
| 70 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 71 | ff->fm = fm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 72 | ff->release_args = kzalloc(sizeof(*ff->release_args), |
| 73 | GFP_KERNEL_ACCOUNT); |
| 74 | if (!ff->release_args) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | kfree(ff); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | INIT_LIST_HEAD(&ff->write_entry); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 80 | mutex_init(&ff->readdir.lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | refcount_set(&ff->count, 1); |
| 82 | RB_CLEAR_NODE(&ff->polled_node); |
| 83 | init_waitqueue_head(&ff->poll_wait); |
| 84 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 85 | ff->kh = atomic64_inc_return(&fm->fc->khctr); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 86 | |
| 87 | return ff; |
| 88 | } |
| 89 | |
| 90 | void fuse_file_free(struct fuse_file *ff) |
| 91 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 92 | kfree(ff->release_args); |
| 93 | mutex_destroy(&ff->readdir.lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | kfree(ff); |
| 95 | } |
| 96 | |
| 97 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) |
| 98 | { |
| 99 | refcount_inc(&ff->count); |
| 100 | return ff; |
| 101 | } |
| 102 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 103 | static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 104 | int error) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 105 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 106 | struct fuse_release_args *ra = container_of(args, typeof(*ra), args); |
| 107 | |
| 108 | iput(ra->inode); |
| 109 | kfree(ra); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir) |
| 113 | { |
| 114 | if (refcount_dec_and_test(&ff->count)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 115 | struct fuse_args *args = &ff->release_args->args; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 116 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 117 | if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 118 | /* Do nothing when client does not implement 'open' */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 119 | fuse_release_end(ff->fm, args, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 120 | } else if (sync) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 121 | fuse_simple_request(ff->fm, args); |
| 122 | fuse_release_end(ff->fm, args, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 123 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 124 | args->end = fuse_release_end; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 125 | if (fuse_simple_background(ff->fm, args, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 126 | GFP_KERNEL | __GFP_NOFAIL)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 127 | fuse_release_end(ff->fm, args, -ENOTCONN); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 128 | } |
| 129 | kfree(ff); |
| 130 | } |
| 131 | } |
| 132 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 133 | int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 134 | bool isdir) |
| 135 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 136 | struct fuse_conn *fc = fm->fc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | struct fuse_file *ff; |
| 138 | int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
| 139 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 140 | ff = fuse_file_alloc(fm); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 141 | if (!ff) |
| 142 | return -ENOMEM; |
| 143 | |
| 144 | ff->fh = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 145 | /* Default for no-open */ |
| 146 | ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0); |
| 147 | if (isdir ? !fc->no_opendir : !fc->no_open) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 148 | struct fuse_open_out outarg; |
| 149 | int err; |
| 150 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 151 | err = fuse_send_open(fm, nodeid, file, opcode, &outarg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 152 | if (!err) { |
| 153 | ff->fh = outarg.fh; |
| 154 | ff->open_flags = outarg.open_flags; |
| 155 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 156 | } else if (err != -ENOSYS) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 157 | fuse_file_free(ff); |
| 158 | return err; |
| 159 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 160 | if (isdir) |
| 161 | fc->no_opendir = 1; |
| 162 | else |
| 163 | fc->no_open = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | if (isdir) |
| 168 | ff->open_flags &= ~FOPEN_DIRECT_IO; |
| 169 | |
| 170 | ff->nodeid = nodeid; |
| 171 | file->private_data = ff; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(fuse_do_open); |
| 176 | |
| 177 | static void fuse_link_write_file(struct file *file) |
| 178 | { |
| 179 | struct inode *inode = file_inode(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 181 | struct fuse_file *ff = file->private_data; |
| 182 | /* |
| 183 | * file may be written through mmap, so chain it onto the |
| 184 | * inodes's write_file list |
| 185 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 186 | spin_lock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 187 | if (list_empty(&ff->write_entry)) |
| 188 | list_add(&ff->write_entry, &fi->write_files); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 189 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void fuse_finish_open(struct inode *inode, struct file *file) |
| 193 | { |
| 194 | struct fuse_file *ff = file->private_data; |
| 195 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 196 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 197 | if (ff->open_flags & FOPEN_STREAM) |
| 198 | stream_open(inode, file); |
| 199 | else if (ff->open_flags & FOPEN_NONSEEKABLE) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 200 | nonseekable_open(inode, file); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 201 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 202 | if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) { |
| 203 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 204 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 205 | spin_lock(&fi->lock); |
| 206 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 207 | i_size_write(inode, 0); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 208 | spin_unlock(&fi->lock); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 209 | truncate_pagecache(inode, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 210 | fuse_invalidate_attr(inode); |
| 211 | if (fc->writeback_cache) |
| 212 | file_update_time(file); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 213 | } else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) { |
| 214 | invalidate_inode_pages2(inode->i_mapping); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 215 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 216 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 217 | if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache) |
| 218 | fuse_link_write_file(file); |
| 219 | } |
| 220 | |
| 221 | int fuse_open_common(struct inode *inode, struct file *file, bool isdir) |
| 222 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 223 | struct fuse_mount *fm = get_fuse_mount(inode); |
| 224 | struct fuse_conn *fc = fm->fc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 225 | int err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 226 | bool is_wb_truncate = (file->f_flags & O_TRUNC) && |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 227 | fc->atomic_o_trunc && |
| 228 | fc->writeback_cache; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 229 | bool dax_truncate = (file->f_flags & O_TRUNC) && |
| 230 | fc->atomic_o_trunc && FUSE_IS_DAX(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 231 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 232 | if (fuse_is_bad(inode)) |
| 233 | return -EIO; |
| 234 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 235 | err = generic_file_open(inode, file); |
| 236 | if (err) |
| 237 | return err; |
| 238 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 239 | if (is_wb_truncate || dax_truncate) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 240 | inode_lock(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 241 | fuse_set_nowrite(inode); |
| 242 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 243 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 244 | if (dax_truncate) { |
| 245 | down_write(&get_fuse_inode(inode)->i_mmap_sem); |
| 246 | err = fuse_dax_break_layouts(inode, 0, 0); |
| 247 | if (err) |
| 248 | goto out; |
| 249 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 250 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 251 | err = fuse_do_open(fm, get_node_id(inode), file, isdir); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 252 | if (!err) |
| 253 | fuse_finish_open(inode, file); |
| 254 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 255 | out: |
| 256 | if (dax_truncate) |
| 257 | up_write(&get_fuse_inode(inode)->i_mmap_sem); |
| 258 | |
| 259 | if (is_wb_truncate | dax_truncate) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 260 | fuse_release_nowrite(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 261 | inode_unlock(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 262 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 263 | |
| 264 | return err; |
| 265 | } |
| 266 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 267 | static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff, |
| 268 | int flags, int opcode) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 269 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 270 | struct fuse_conn *fc = ff->fm->fc; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 271 | struct fuse_release_args *ra = ff->release_args; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 272 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 273 | /* Inode is NULL on error path of fuse_create_open() */ |
| 274 | if (likely(fi)) { |
| 275 | spin_lock(&fi->lock); |
| 276 | list_del(&ff->write_entry); |
| 277 | spin_unlock(&fi->lock); |
| 278 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 279 | spin_lock(&fc->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 280 | if (!RB_EMPTY_NODE(&ff->polled_node)) |
| 281 | rb_erase(&ff->polled_node, &fc->polled_files); |
| 282 | spin_unlock(&fc->lock); |
| 283 | |
| 284 | wake_up_interruptible_all(&ff->poll_wait); |
| 285 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 286 | ra->inarg.fh = ff->fh; |
| 287 | ra->inarg.flags = flags; |
| 288 | ra->args.in_numargs = 1; |
| 289 | ra->args.in_args[0].size = sizeof(struct fuse_release_in); |
| 290 | ra->args.in_args[0].value = &ra->inarg; |
| 291 | ra->args.opcode = opcode; |
| 292 | ra->args.nodeid = ff->nodeid; |
| 293 | ra->args.force = true; |
| 294 | ra->args.nocreds = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void fuse_release_common(struct file *file, bool isdir) |
| 298 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 299 | struct fuse_inode *fi = get_fuse_inode(file_inode(file)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 300 | struct fuse_file *ff = file->private_data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 301 | struct fuse_release_args *ra = ff->release_args; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 302 | int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; |
| 303 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 304 | fuse_prepare_release(fi, ff, file->f_flags, opcode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 305 | |
| 306 | if (ff->flock) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 307 | ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 308 | ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 309 | (fl_owner_t) file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 310 | } |
| 311 | /* Hold inode until release is finished */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 312 | ra->inode = igrab(file_inode(file)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * Normally this will send the RELEASE request, however if |
| 316 | * some asynchronous READ or WRITE requests are outstanding, |
| 317 | * the sending will be delayed. |
| 318 | * |
| 319 | * Make the release synchronous if this is a fuseblk mount, |
| 320 | * synchronous RELEASE is allowed (and desirable) in this case |
| 321 | * because the server can be trusted not to screw up. |
| 322 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 323 | fuse_file_put(ff, ff->fm->fc->destroy, isdir); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static int fuse_open(struct inode *inode, struct file *file) |
| 327 | { |
| 328 | return fuse_open_common(inode, file, false); |
| 329 | } |
| 330 | |
| 331 | static int fuse_release(struct inode *inode, struct file *file) |
| 332 | { |
| 333 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 334 | |
| 335 | /* see fuse_vma_close() for !writeback_cache case */ |
| 336 | if (fc->writeback_cache) |
| 337 | write_inode_now(inode, 1); |
| 338 | |
| 339 | fuse_release_common(file, false); |
| 340 | |
| 341 | /* return value is ignored by VFS */ |
| 342 | return 0; |
| 343 | } |
| 344 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 345 | void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 346 | { |
| 347 | WARN_ON(refcount_read(&ff->count) > 1); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 348 | fuse_prepare_release(fi, ff, flags, FUSE_RELEASE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 349 | /* |
| 350 | * iput(NULL) is a no-op and since the refcount is 1 and everything's |
| 351 | * synchronous, we are fine with not doing igrab() here" |
| 352 | */ |
| 353 | fuse_file_put(ff, true, false); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(fuse_sync_release); |
| 356 | |
| 357 | /* |
| 358 | * Scramble the ID space with XTEA, so that the value of the files_struct |
| 359 | * pointer is not exposed to userspace. |
| 360 | */ |
| 361 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
| 362 | { |
| 363 | u32 *k = fc->scramble_key; |
| 364 | u64 v = (unsigned long) id; |
| 365 | u32 v0 = v; |
| 366 | u32 v1 = v >> 32; |
| 367 | u32 sum = 0; |
| 368 | int i; |
| 369 | |
| 370 | for (i = 0; i < 32; i++) { |
| 371 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); |
| 372 | sum += 0x9E3779B9; |
| 373 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); |
| 374 | } |
| 375 | |
| 376 | return (u64) v0 + ((u64) v1 << 32); |
| 377 | } |
| 378 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 379 | struct fuse_writepage_args { |
| 380 | struct fuse_io_args ia; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 381 | struct rb_node writepages_entry; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 382 | struct list_head queue_entry; |
| 383 | struct fuse_writepage_args *next; |
| 384 | struct inode *inode; |
| 385 | }; |
| 386 | |
| 387 | static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi, |
| 388 | pgoff_t idx_from, pgoff_t idx_to) |
| 389 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 390 | struct rb_node *n; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 391 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 392 | n = fi->writepages.rb_node; |
| 393 | |
| 394 | while (n) { |
| 395 | struct fuse_writepage_args *wpa; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 396 | pgoff_t curr_index; |
| 397 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 398 | wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 399 | WARN_ON(get_fuse_inode(wpa->inode) != fi); |
| 400 | curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 401 | if (idx_from >= curr_index + wpa->ia.ap.num_pages) |
| 402 | n = n->rb_right; |
| 403 | else if (idx_to < curr_index) |
| 404 | n = n->rb_left; |
| 405 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 406 | return wpa; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 407 | } |
| 408 | return NULL; |
| 409 | } |
| 410 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 411 | /* |
| 412 | * Check if any page in a range is under writeback |
| 413 | * |
| 414 | * This is currently done by walking the list of writepage requests |
| 415 | * for the inode, which can be pretty inefficient. |
| 416 | */ |
| 417 | static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from, |
| 418 | pgoff_t idx_to) |
| 419 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 420 | struct fuse_inode *fi = get_fuse_inode(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 421 | bool found; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 422 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 423 | spin_lock(&fi->lock); |
| 424 | found = fuse_find_writeback(fi, idx_from, idx_to); |
| 425 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 426 | |
| 427 | return found; |
| 428 | } |
| 429 | |
| 430 | static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) |
| 431 | { |
| 432 | return fuse_range_is_writeback(inode, index, index); |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Wait for page writeback to be completed. |
| 437 | * |
| 438 | * Since fuse doesn't rely on the VM writeback tracking, this has to |
| 439 | * use some other means. |
| 440 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 441 | static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 442 | { |
| 443 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 444 | |
| 445 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Wait for all pending writepages on the inode to finish. |
| 450 | * |
| 451 | * This is currently done by blocking further writes with FUSE_NOWRITE |
| 452 | * and waiting for all sent writes to complete. |
| 453 | * |
| 454 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage |
| 455 | * could conflict with truncation. |
| 456 | */ |
| 457 | static void fuse_sync_writes(struct inode *inode) |
| 458 | { |
| 459 | fuse_set_nowrite(inode); |
| 460 | fuse_release_nowrite(inode); |
| 461 | } |
| 462 | |
| 463 | static int fuse_flush(struct file *file, fl_owner_t id) |
| 464 | { |
| 465 | struct inode *inode = file_inode(file); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 466 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 467 | struct fuse_file *ff = file->private_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 468 | struct fuse_flush_in inarg; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 469 | FUSE_ARGS(args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 470 | int err; |
| 471 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 472 | if (fuse_is_bad(inode)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 473 | return -EIO; |
| 474 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 475 | err = write_inode_now(inode, 1); |
| 476 | if (err) |
| 477 | return err; |
| 478 | |
| 479 | inode_lock(inode); |
| 480 | fuse_sync_writes(inode); |
| 481 | inode_unlock(inode); |
| 482 | |
| 483 | err = filemap_check_errors(file->f_mapping); |
| 484 | if (err) |
| 485 | return err; |
| 486 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 487 | err = 0; |
| 488 | if (fm->fc->no_flush) |
| 489 | goto inval_attr_out; |
| 490 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 491 | memset(&inarg, 0, sizeof(inarg)); |
| 492 | inarg.fh = ff->fh; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 493 | inarg.lock_owner = fuse_lock_owner_id(fm->fc, id); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 494 | args.opcode = FUSE_FLUSH; |
| 495 | args.nodeid = get_node_id(inode); |
| 496 | args.in_numargs = 1; |
| 497 | args.in_args[0].size = sizeof(inarg); |
| 498 | args.in_args[0].value = &inarg; |
| 499 | args.force = true; |
| 500 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 501 | err = fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 502 | if (err == -ENOSYS) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 503 | fm->fc->no_flush = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 504 | err = 0; |
| 505 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 506 | |
| 507 | inval_attr_out: |
| 508 | /* |
| 509 | * In memory i_blocks is not maintained by fuse, if writeback cache is |
| 510 | * enabled, i_blocks from cached attr may not be accurate. |
| 511 | */ |
| 512 | if (!err && fm->fc->writeback_cache) |
| 513 | fuse_invalidate_attr(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 514 | return err; |
| 515 | } |
| 516 | |
| 517 | int fuse_fsync_common(struct file *file, loff_t start, loff_t end, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 518 | int datasync, int opcode) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 519 | { |
| 520 | struct inode *inode = file->f_mapping->host; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 521 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 522 | struct fuse_file *ff = file->private_data; |
| 523 | FUSE_ARGS(args); |
| 524 | struct fuse_fsync_in inarg; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 525 | |
| 526 | memset(&inarg, 0, sizeof(inarg)); |
| 527 | inarg.fh = ff->fh; |
| 528 | inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0; |
| 529 | args.opcode = opcode; |
| 530 | args.nodeid = get_node_id(inode); |
| 531 | args.in_numargs = 1; |
| 532 | args.in_args[0].size = sizeof(inarg); |
| 533 | args.in_args[0].value = &inarg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 534 | return fuse_simple_request(fm, &args); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static int fuse_fsync(struct file *file, loff_t start, loff_t end, |
| 538 | int datasync) |
| 539 | { |
| 540 | struct inode *inode = file->f_mapping->host; |
| 541 | struct fuse_conn *fc = get_fuse_conn(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 542 | int err; |
| 543 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 544 | if (fuse_is_bad(inode)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 545 | return -EIO; |
| 546 | |
| 547 | inode_lock(inode); |
| 548 | |
| 549 | /* |
| 550 | * Start writeback against all dirty pages of the inode, then |
| 551 | * wait for all outstanding writes, before sending the FSYNC |
| 552 | * request. |
| 553 | */ |
| 554 | err = file_write_and_wait_range(file, start, end); |
| 555 | if (err) |
| 556 | goto out; |
| 557 | |
| 558 | fuse_sync_writes(inode); |
| 559 | |
| 560 | /* |
| 561 | * Due to implementation of fuse writeback |
| 562 | * file_write_and_wait_range() does not catch errors. |
| 563 | * We have to do this directly after fuse_sync_writes() |
| 564 | */ |
| 565 | err = file_check_and_advance_wb_err(file); |
| 566 | if (err) |
| 567 | goto out; |
| 568 | |
| 569 | err = sync_inode_metadata(inode, 1); |
| 570 | if (err) |
| 571 | goto out; |
| 572 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 573 | if (fc->no_fsync) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 574 | goto out; |
| 575 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 576 | err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 577 | if (err == -ENOSYS) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 578 | fc->no_fsync = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 579 | err = 0; |
| 580 | } |
| 581 | out: |
| 582 | inode_unlock(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 583 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 584 | return err; |
| 585 | } |
| 586 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 587 | void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos, |
| 588 | size_t count, int opcode) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 589 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 590 | struct fuse_file *ff = file->private_data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 591 | struct fuse_args *args = &ia->ap.args; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 592 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 593 | ia->read.in.fh = ff->fh; |
| 594 | ia->read.in.offset = pos; |
| 595 | ia->read.in.size = count; |
| 596 | ia->read.in.flags = file->f_flags; |
| 597 | args->opcode = opcode; |
| 598 | args->nodeid = ff->nodeid; |
| 599 | args->in_numargs = 1; |
| 600 | args->in_args[0].size = sizeof(ia->read.in); |
| 601 | args->in_args[0].value = &ia->read.in; |
| 602 | args->out_argvar = true; |
| 603 | args->out_numargs = 1; |
| 604 | args->out_args[0].size = count; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 605 | } |
| 606 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 607 | static void fuse_release_user_pages(struct fuse_args_pages *ap, |
| 608 | bool should_dirty) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 609 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 610 | unsigned int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 611 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 612 | for (i = 0; i < ap->num_pages; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 613 | if (should_dirty) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 614 | set_page_dirty_lock(ap->pages[i]); |
| 615 | put_page(ap->pages[i]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | static void fuse_io_release(struct kref *kref) |
| 620 | { |
| 621 | kfree(container_of(kref, struct fuse_io_priv, refcnt)); |
| 622 | } |
| 623 | |
| 624 | static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io) |
| 625 | { |
| 626 | if (io->err) |
| 627 | return io->err; |
| 628 | |
| 629 | if (io->bytes >= 0 && io->write) |
| 630 | return -EIO; |
| 631 | |
| 632 | return io->bytes < 0 ? io->size : io->bytes; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * In case of short read, the caller sets 'pos' to the position of |
| 637 | * actual end of fuse request in IO request. Otherwise, if bytes_requested |
| 638 | * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1. |
| 639 | * |
| 640 | * An example: |
| 641 | * User requested DIO read of 64K. It was splitted into two 32K fuse requests, |
| 642 | * both submitted asynchronously. The first of them was ACKed by userspace as |
| 643 | * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The |
| 644 | * second request was ACKed as short, e.g. only 1K was read, resulting in |
| 645 | * pos == 33K. |
| 646 | * |
| 647 | * Thus, when all fuse requests are completed, the minimal non-negative 'pos' |
| 648 | * will be equal to the length of the longest contiguous fragment of |
| 649 | * transferred data starting from the beginning of IO request. |
| 650 | */ |
| 651 | static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos) |
| 652 | { |
| 653 | int left; |
| 654 | |
| 655 | spin_lock(&io->lock); |
| 656 | if (err) |
| 657 | io->err = io->err ? : err; |
| 658 | else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes)) |
| 659 | io->bytes = pos; |
| 660 | |
| 661 | left = --io->reqs; |
| 662 | if (!left && io->blocking) |
| 663 | complete(io->done); |
| 664 | spin_unlock(&io->lock); |
| 665 | |
| 666 | if (!left && !io->blocking) { |
| 667 | ssize_t res = fuse_get_res_by_io(io); |
| 668 | |
| 669 | if (res >= 0) { |
| 670 | struct inode *inode = file_inode(io->iocb->ki_filp); |
| 671 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 672 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 673 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 674 | spin_lock(&fi->lock); |
| 675 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
| 676 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | io->iocb->ki_complete(io->iocb, res, 0); |
| 680 | } |
| 681 | |
| 682 | kref_put(&io->refcnt, fuse_io_release); |
| 683 | } |
| 684 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 685 | static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io, |
| 686 | unsigned int npages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 687 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 688 | struct fuse_io_args *ia; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 689 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 690 | ia = kzalloc(sizeof(*ia), GFP_KERNEL); |
| 691 | if (ia) { |
| 692 | ia->io = io; |
| 693 | ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL, |
| 694 | &ia->ap.descs); |
| 695 | if (!ia->ap.pages) { |
| 696 | kfree(ia); |
| 697 | ia = NULL; |
| 698 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 699 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 700 | return ia; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 701 | } |
| 702 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 703 | static void fuse_io_free(struct fuse_io_args *ia) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 704 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 705 | kfree(ia->ap.pages); |
| 706 | kfree(ia); |
| 707 | } |
| 708 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 709 | static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 710 | int err) |
| 711 | { |
| 712 | struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args); |
| 713 | struct fuse_io_priv *io = ia->io; |
| 714 | ssize_t pos = -1; |
| 715 | |
| 716 | fuse_release_user_pages(&ia->ap, io->should_dirty); |
| 717 | |
| 718 | if (err) { |
| 719 | /* Nothing */ |
| 720 | } else if (io->write) { |
| 721 | if (ia->write.out.size > ia->write.in.size) { |
| 722 | err = -EIO; |
| 723 | } else if (ia->write.in.size != ia->write.out.size) { |
| 724 | pos = ia->write.in.offset - io->offset + |
| 725 | ia->write.out.size; |
| 726 | } |
| 727 | } else { |
| 728 | u32 outsize = args->out_args[0].size; |
| 729 | |
| 730 | if (ia->read.in.size != outsize) |
| 731 | pos = ia->read.in.offset - io->offset + outsize; |
| 732 | } |
| 733 | |
| 734 | fuse_aio_complete(io, err, pos); |
| 735 | fuse_io_free(ia); |
| 736 | } |
| 737 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 738 | static ssize_t fuse_async_req_send(struct fuse_mount *fm, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 739 | struct fuse_io_args *ia, size_t num_bytes) |
| 740 | { |
| 741 | ssize_t err; |
| 742 | struct fuse_io_priv *io = ia->io; |
| 743 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 744 | spin_lock(&io->lock); |
| 745 | kref_get(&io->refcnt); |
| 746 | io->size += num_bytes; |
| 747 | io->reqs++; |
| 748 | spin_unlock(&io->lock); |
| 749 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 750 | ia->ap.args.end = fuse_aio_complete_req; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 751 | ia->ap.args.may_block = io->should_dirty; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 752 | err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 753 | if (err) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 754 | fuse_aio_complete_req(fm, &ia->ap.args, err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 755 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 756 | return num_bytes; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 757 | } |
| 758 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 759 | static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count, |
| 760 | fl_owner_t owner) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 761 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 762 | struct file *file = ia->io->iocb->ki_filp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 763 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 764 | struct fuse_mount *fm = ff->fm; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 765 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 766 | fuse_read_args_fill(ia, file, pos, count, FUSE_READ); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 767 | if (owner != NULL) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 768 | ia->read.in.read_flags |= FUSE_READ_LOCKOWNER; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 769 | ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 770 | } |
| 771 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 772 | if (ia->io->async) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 773 | return fuse_async_req_send(fm, ia, count); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 774 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 775 | return fuse_simple_request(fm, &ia->ap.args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
| 779 | u64 attr_ver) |
| 780 | { |
| 781 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 782 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 783 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 784 | spin_lock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 785 | if (attr_ver == fi->attr_version && size < inode->i_size && |
| 786 | !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 787 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 788 | i_size_write(inode, size); |
| 789 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 790 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 791 | } |
| 792 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 793 | static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read, |
| 794 | struct fuse_args_pages *ap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 795 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 796 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 797 | |
| 798 | if (fc->writeback_cache) { |
| 799 | /* |
| 800 | * A hole in a file. Some data after the hole are in page cache, |
| 801 | * but have not reached the client fs yet. So, the hole is not |
| 802 | * present there. |
| 803 | */ |
| 804 | int i; |
| 805 | int start_idx = num_read >> PAGE_SHIFT; |
| 806 | size_t off = num_read & (PAGE_SIZE - 1); |
| 807 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 808 | for (i = start_idx; i < ap->num_pages; i++) { |
| 809 | zero_user_segment(ap->pages[i], off, PAGE_SIZE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 810 | off = 0; |
| 811 | } |
| 812 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 813 | loff_t pos = page_offset(ap->pages[0]) + num_read; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 814 | fuse_read_update_size(inode, pos, attr_ver); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | static int fuse_do_readpage(struct file *file, struct page *page) |
| 819 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 820 | struct inode *inode = page->mapping->host; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 821 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 822 | loff_t pos = page_offset(page); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 823 | struct fuse_page_desc desc = { .length = PAGE_SIZE }; |
| 824 | struct fuse_io_args ia = { |
| 825 | .ap.args.page_zeroing = true, |
| 826 | .ap.args.out_pages = true, |
| 827 | .ap.num_pages = 1, |
| 828 | .ap.pages = &page, |
| 829 | .ap.descs = &desc, |
| 830 | }; |
| 831 | ssize_t res; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 832 | u64 attr_ver; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * Page writeback can extend beyond the lifetime of the |
| 836 | * page-cache page, so make sure we read a properly synced |
| 837 | * page. |
| 838 | */ |
| 839 | fuse_wait_on_page_writeback(inode, page->index); |
| 840 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 841 | attr_ver = fuse_get_attr_version(fm->fc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 842 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 843 | /* Don't overflow end offset */ |
| 844 | if (pos + (desc.length - 1) == LLONG_MAX) |
| 845 | desc.length--; |
| 846 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 847 | fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 848 | res = fuse_simple_request(fm, &ia.ap.args); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 849 | if (res < 0) |
| 850 | return res; |
| 851 | /* |
| 852 | * Short read means EOF. If file size is larger, truncate it |
| 853 | */ |
| 854 | if (res < desc.length) |
| 855 | fuse_short_read(inode, attr_ver, res, &ia.ap); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 856 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 857 | SetPageUptodate(page); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 858 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 859 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static int fuse_readpage(struct file *file, struct page *page) |
| 863 | { |
| 864 | struct inode *inode = page->mapping->host; |
| 865 | int err; |
| 866 | |
| 867 | err = -EIO; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 868 | if (fuse_is_bad(inode)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 869 | goto out; |
| 870 | |
| 871 | err = fuse_do_readpage(file, page); |
| 872 | fuse_invalidate_atime(inode); |
| 873 | out: |
| 874 | unlock_page(page); |
| 875 | return err; |
| 876 | } |
| 877 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 878 | static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 879 | int err) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 880 | { |
| 881 | int i; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 882 | struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args); |
| 883 | struct fuse_args_pages *ap = &ia->ap; |
| 884 | size_t count = ia->read.in.size; |
| 885 | size_t num_read = args->out_args[0].size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 886 | struct address_space *mapping = NULL; |
| 887 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 888 | for (i = 0; mapping == NULL && i < ap->num_pages; i++) |
| 889 | mapping = ap->pages[i]->mapping; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 890 | |
| 891 | if (mapping) { |
| 892 | struct inode *inode = mapping->host; |
| 893 | |
| 894 | /* |
| 895 | * Short read means EOF. If file size is larger, truncate it |
| 896 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 897 | if (!err && num_read < count) |
| 898 | fuse_short_read(inode, ia->read.attr_ver, num_read, ap); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 899 | |
| 900 | fuse_invalidate_atime(inode); |
| 901 | } |
| 902 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 903 | for (i = 0; i < ap->num_pages; i++) { |
| 904 | struct page *page = ap->pages[i]; |
| 905 | |
| 906 | if (!err) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 907 | SetPageUptodate(page); |
| 908 | else |
| 909 | SetPageError(page); |
| 910 | unlock_page(page); |
| 911 | put_page(page); |
| 912 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 913 | if (ia->ff) |
| 914 | fuse_file_put(ia->ff, false, false); |
| 915 | |
| 916 | fuse_io_free(ia); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 917 | } |
| 918 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 919 | static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 920 | { |
| 921 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 922 | struct fuse_mount *fm = ff->fm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 923 | struct fuse_args_pages *ap = &ia->ap; |
| 924 | loff_t pos = page_offset(ap->pages[0]); |
| 925 | size_t count = ap->num_pages << PAGE_SHIFT; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 926 | ssize_t res; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 927 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 928 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 929 | ap->args.out_pages = true; |
| 930 | ap->args.page_zeroing = true; |
| 931 | ap->args.page_replace = true; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 932 | |
| 933 | /* Don't overflow end offset */ |
| 934 | if (pos + (count - 1) == LLONG_MAX) { |
| 935 | count--; |
| 936 | ap->descs[ap->num_pages - 1].length--; |
| 937 | } |
| 938 | WARN_ON((loff_t) (pos + count) < 0); |
| 939 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 940 | fuse_read_args_fill(ia, file, pos, count, FUSE_READ); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 941 | ia->read.attr_ver = fuse_get_attr_version(fm->fc); |
| 942 | if (fm->fc->async_read) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 943 | ia->ff = fuse_file_get(ff); |
| 944 | ap->args.end = fuse_readpages_end; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 945 | err = fuse_simple_background(fm, &ap->args, GFP_KERNEL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 946 | if (!err) |
| 947 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 948 | } else { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 949 | res = fuse_simple_request(fm, &ap->args); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 950 | err = res < 0 ? res : 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 951 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 952 | fuse_readpages_end(fm, &ap->args, err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 955 | static void fuse_readahead(struct readahead_control *rac) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 956 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 957 | struct inode *inode = rac->mapping->host; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 958 | struct fuse_conn *fc = get_fuse_conn(inode); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 959 | unsigned int i, max_pages, nr_pages = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 960 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 961 | if (fuse_is_bad(inode)) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 962 | return; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 963 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 964 | max_pages = min_t(unsigned int, fc->max_pages, |
| 965 | fc->max_read / PAGE_SIZE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 966 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 967 | for (;;) { |
| 968 | struct fuse_io_args *ia; |
| 969 | struct fuse_args_pages *ap; |
| 970 | |
| 971 | nr_pages = readahead_count(rac) - nr_pages; |
| 972 | if (nr_pages > max_pages) |
| 973 | nr_pages = max_pages; |
| 974 | if (nr_pages == 0) |
| 975 | break; |
| 976 | ia = fuse_io_alloc(NULL, nr_pages); |
| 977 | if (!ia) |
| 978 | return; |
| 979 | ap = &ia->ap; |
| 980 | nr_pages = __readahead_batch(rac, ap->pages, nr_pages); |
| 981 | for (i = 0; i < nr_pages; i++) { |
| 982 | fuse_wait_on_page_writeback(inode, |
| 983 | readahead_index(rac) + i); |
| 984 | ap->descs[i].length = PAGE_SIZE; |
| 985 | } |
| 986 | ap->num_pages = nr_pages; |
| 987 | fuse_send_readpages(ia, rac->file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 988 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 989 | } |
| 990 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 991 | static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 992 | { |
| 993 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
| 994 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 995 | |
| 996 | /* |
| 997 | * In auto invalidate mode, always update attributes on read. |
| 998 | * Otherwise, only update if we attempt to read past EOF (to ensure |
| 999 | * i_size is up to date). |
| 1000 | */ |
| 1001 | if (fc->auto_inval_data || |
| 1002 | (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) { |
| 1003 | int err; |
| 1004 | err = fuse_update_attributes(inode, iocb->ki_filp); |
| 1005 | if (err) |
| 1006 | return err; |
| 1007 | } |
| 1008 | |
| 1009 | return generic_file_read_iter(iocb, to); |
| 1010 | } |
| 1011 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1012 | static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff, |
| 1013 | loff_t pos, size_t count) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1014 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1015 | struct fuse_args *args = &ia->ap.args; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1016 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1017 | ia->write.in.fh = ff->fh; |
| 1018 | ia->write.in.offset = pos; |
| 1019 | ia->write.in.size = count; |
| 1020 | args->opcode = FUSE_WRITE; |
| 1021 | args->nodeid = ff->nodeid; |
| 1022 | args->in_numargs = 2; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1023 | if (ff->fm->fc->minor < 9) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1024 | args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1025 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1026 | args->in_args[0].size = sizeof(ia->write.in); |
| 1027 | args->in_args[0].value = &ia->write.in; |
| 1028 | args->in_args[1].size = count; |
| 1029 | args->out_numargs = 1; |
| 1030 | args->out_args[0].size = sizeof(ia->write.out); |
| 1031 | args->out_args[0].value = &ia->write.out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1034 | static unsigned int fuse_write_flags(struct kiocb *iocb) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1035 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1036 | unsigned int flags = iocb->ki_filp->f_flags; |
| 1037 | |
| 1038 | if (iocb->ki_flags & IOCB_DSYNC) |
| 1039 | flags |= O_DSYNC; |
| 1040 | if (iocb->ki_flags & IOCB_SYNC) |
| 1041 | flags |= O_SYNC; |
| 1042 | |
| 1043 | return flags; |
| 1044 | } |
| 1045 | |
| 1046 | static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos, |
| 1047 | size_t count, fl_owner_t owner) |
| 1048 | { |
| 1049 | struct kiocb *iocb = ia->io->iocb; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1050 | struct file *file = iocb->ki_filp; |
| 1051 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1052 | struct fuse_mount *fm = ff->fm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1053 | struct fuse_write_in *inarg = &ia->write.in; |
| 1054 | ssize_t err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1055 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1056 | fuse_write_args_fill(ia, ff, pos, count); |
| 1057 | inarg->flags = fuse_write_flags(iocb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1058 | if (owner != NULL) { |
| 1059 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1060 | inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1063 | if (ia->io->async) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1064 | return fuse_async_req_send(fm, ia, count); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1065 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1066 | err = fuse_simple_request(fm, &ia->ap.args); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1067 | if (!err && ia->write.out.size > count) |
| 1068 | err = -EIO; |
| 1069 | |
| 1070 | return err ?: ia->write.out.size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | bool fuse_write_update_size(struct inode *inode, loff_t pos) |
| 1074 | { |
| 1075 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1076 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1077 | bool ret = false; |
| 1078 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1079 | spin_lock(&fi->lock); |
| 1080 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1081 | if (pos > inode->i_size) { |
| 1082 | i_size_write(inode, pos); |
| 1083 | ret = true; |
| 1084 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1085 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1086 | |
| 1087 | return ret; |
| 1088 | } |
| 1089 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1090 | static ssize_t fuse_send_write_pages(struct fuse_io_args *ia, |
| 1091 | struct kiocb *iocb, struct inode *inode, |
| 1092 | loff_t pos, size_t count) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1093 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1094 | struct fuse_args_pages *ap = &ia->ap; |
| 1095 | struct file *file = iocb->ki_filp; |
| 1096 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1097 | struct fuse_mount *fm = ff->fm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1098 | unsigned int offset, i; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1099 | bool short_write; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1100 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1101 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1102 | for (i = 0; i < ap->num_pages; i++) |
| 1103 | fuse_wait_on_page_writeback(inode, ap->pages[i]->index); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1104 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1105 | fuse_write_args_fill(ia, ff, pos, count); |
| 1106 | ia->write.in.flags = fuse_write_flags(iocb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1107 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1108 | err = fuse_simple_request(fm, &ap->args); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1109 | if (!err && ia->write.out.size > count) |
| 1110 | err = -EIO; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1111 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1112 | short_write = ia->write.out.size < count; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1113 | offset = ap->descs[0].offset; |
| 1114 | count = ia->write.out.size; |
| 1115 | for (i = 0; i < ap->num_pages; i++) { |
| 1116 | struct page *page = ap->pages[i]; |
| 1117 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1118 | if (err) { |
| 1119 | ClearPageUptodate(page); |
| 1120 | } else { |
| 1121 | if (count >= PAGE_SIZE - offset) |
| 1122 | count -= PAGE_SIZE - offset; |
| 1123 | else { |
| 1124 | if (short_write) |
| 1125 | ClearPageUptodate(page); |
| 1126 | count = 0; |
| 1127 | } |
| 1128 | offset = 0; |
| 1129 | } |
| 1130 | if (ia->write.page_locked && (i == ap->num_pages - 1)) |
| 1131 | unlock_page(page); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1132 | put_page(page); |
| 1133 | } |
| 1134 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1135 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1138 | static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1139 | struct address_space *mapping, |
| 1140 | struct iov_iter *ii, loff_t pos, |
| 1141 | unsigned int max_pages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1142 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1143 | struct fuse_args_pages *ap = &ia->ap; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1144 | struct fuse_conn *fc = get_fuse_conn(mapping->host); |
| 1145 | unsigned offset = pos & (PAGE_SIZE - 1); |
| 1146 | size_t count = 0; |
| 1147 | int err; |
| 1148 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1149 | ap->args.in_pages = true; |
| 1150 | ap->descs[0].offset = offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1151 | |
| 1152 | do { |
| 1153 | size_t tmp; |
| 1154 | struct page *page; |
| 1155 | pgoff_t index = pos >> PAGE_SHIFT; |
| 1156 | size_t bytes = min_t(size_t, PAGE_SIZE - offset, |
| 1157 | iov_iter_count(ii)); |
| 1158 | |
| 1159 | bytes = min_t(size_t, bytes, fc->max_write - count); |
| 1160 | |
| 1161 | again: |
| 1162 | err = -EFAULT; |
| 1163 | if (iov_iter_fault_in_readable(ii, bytes)) |
| 1164 | break; |
| 1165 | |
| 1166 | err = -ENOMEM; |
| 1167 | page = grab_cache_page_write_begin(mapping, index, 0); |
| 1168 | if (!page) |
| 1169 | break; |
| 1170 | |
| 1171 | if (mapping_writably_mapped(mapping)) |
| 1172 | flush_dcache_page(page); |
| 1173 | |
| 1174 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); |
| 1175 | flush_dcache_page(page); |
| 1176 | |
| 1177 | iov_iter_advance(ii, tmp); |
| 1178 | if (!tmp) { |
| 1179 | unlock_page(page); |
| 1180 | put_page(page); |
| 1181 | bytes = min(bytes, iov_iter_single_seg_count(ii)); |
| 1182 | goto again; |
| 1183 | } |
| 1184 | |
| 1185 | err = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1186 | ap->pages[ap->num_pages] = page; |
| 1187 | ap->descs[ap->num_pages].length = tmp; |
| 1188 | ap->num_pages++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1189 | |
| 1190 | count += tmp; |
| 1191 | pos += tmp; |
| 1192 | offset += tmp; |
| 1193 | if (offset == PAGE_SIZE) |
| 1194 | offset = 0; |
| 1195 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1196 | /* If we copied full page, mark it uptodate */ |
| 1197 | if (tmp == PAGE_SIZE) |
| 1198 | SetPageUptodate(page); |
| 1199 | |
| 1200 | if (PageUptodate(page)) { |
| 1201 | unlock_page(page); |
| 1202 | } else { |
| 1203 | ia->write.page_locked = true; |
| 1204 | break; |
| 1205 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1206 | if (!fc->big_writes) |
| 1207 | break; |
| 1208 | } while (iov_iter_count(ii) && count < fc->max_write && |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1209 | ap->num_pages < max_pages && offset == 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1210 | |
| 1211 | return count > 0 ? count : err; |
| 1212 | } |
| 1213 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1214 | static inline unsigned int fuse_wr_pages(loff_t pos, size_t len, |
| 1215 | unsigned int max_pages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1216 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1217 | return min_t(unsigned int, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1218 | ((pos + len - 1) >> PAGE_SHIFT) - |
| 1219 | (pos >> PAGE_SHIFT) + 1, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1220 | max_pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | static ssize_t fuse_perform_write(struct kiocb *iocb, |
| 1224 | struct address_space *mapping, |
| 1225 | struct iov_iter *ii, loff_t pos) |
| 1226 | { |
| 1227 | struct inode *inode = mapping->host; |
| 1228 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1229 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1230 | int err = 0; |
| 1231 | ssize_t res = 0; |
| 1232 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1233 | if (inode->i_size < pos + iov_iter_count(ii)) |
| 1234 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
| 1235 | |
| 1236 | do { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1237 | ssize_t count; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1238 | struct fuse_io_args ia = {}; |
| 1239 | struct fuse_args_pages *ap = &ia.ap; |
| 1240 | unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii), |
| 1241 | fc->max_pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1242 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1243 | ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs); |
| 1244 | if (!ap->pages) { |
| 1245 | err = -ENOMEM; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | } |
| 1248 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1249 | count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1250 | if (count <= 0) { |
| 1251 | err = count; |
| 1252 | } else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1253 | err = fuse_send_write_pages(&ia, iocb, inode, |
| 1254 | pos, count); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1255 | if (!err) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1256 | size_t num_written = ia.write.out.size; |
| 1257 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1258 | res += num_written; |
| 1259 | pos += num_written; |
| 1260 | |
| 1261 | /* break out of the loop on short write */ |
| 1262 | if (num_written != count) |
| 1263 | err = -EIO; |
| 1264 | } |
| 1265 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1266 | kfree(ap->pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1267 | } while (!err && iov_iter_count(ii)); |
| 1268 | |
| 1269 | if (res > 0) |
| 1270 | fuse_write_update_size(inode, pos); |
| 1271 | |
| 1272 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
| 1273 | fuse_invalidate_attr(inode); |
| 1274 | |
| 1275 | return res > 0 ? res : err; |
| 1276 | } |
| 1277 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1278 | static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1279 | { |
| 1280 | struct file *file = iocb->ki_filp; |
| 1281 | struct address_space *mapping = file->f_mapping; |
| 1282 | ssize_t written = 0; |
| 1283 | ssize_t written_buffered = 0; |
| 1284 | struct inode *inode = mapping->host; |
| 1285 | ssize_t err; |
| 1286 | loff_t endbyte = 0; |
| 1287 | |
| 1288 | if (get_fuse_conn(inode)->writeback_cache) { |
| 1289 | /* Update size (EOF optimization) and mode (SUID clearing) */ |
| 1290 | err = fuse_update_attributes(mapping->host, file); |
| 1291 | if (err) |
| 1292 | return err; |
| 1293 | |
| 1294 | return generic_file_write_iter(iocb, from); |
| 1295 | } |
| 1296 | |
| 1297 | inode_lock(inode); |
| 1298 | |
| 1299 | /* We can write back this queue in page reclaim */ |
| 1300 | current->backing_dev_info = inode_to_bdi(inode); |
| 1301 | |
| 1302 | err = generic_write_checks(iocb, from); |
| 1303 | if (err <= 0) |
| 1304 | goto out; |
| 1305 | |
| 1306 | err = file_remove_privs(file); |
| 1307 | if (err) |
| 1308 | goto out; |
| 1309 | |
| 1310 | err = file_update_time(file); |
| 1311 | if (err) |
| 1312 | goto out; |
| 1313 | |
| 1314 | if (iocb->ki_flags & IOCB_DIRECT) { |
| 1315 | loff_t pos = iocb->ki_pos; |
| 1316 | written = generic_file_direct_write(iocb, from); |
| 1317 | if (written < 0 || !iov_iter_count(from)) |
| 1318 | goto out; |
| 1319 | |
| 1320 | pos += written; |
| 1321 | |
| 1322 | written_buffered = fuse_perform_write(iocb, mapping, from, pos); |
| 1323 | if (written_buffered < 0) { |
| 1324 | err = written_buffered; |
| 1325 | goto out; |
| 1326 | } |
| 1327 | endbyte = pos + written_buffered - 1; |
| 1328 | |
| 1329 | err = filemap_write_and_wait_range(file->f_mapping, pos, |
| 1330 | endbyte); |
| 1331 | if (err) |
| 1332 | goto out; |
| 1333 | |
| 1334 | invalidate_mapping_pages(file->f_mapping, |
| 1335 | pos >> PAGE_SHIFT, |
| 1336 | endbyte >> PAGE_SHIFT); |
| 1337 | |
| 1338 | written += written_buffered; |
| 1339 | iocb->ki_pos = pos + written_buffered; |
| 1340 | } else { |
| 1341 | written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos); |
| 1342 | if (written >= 0) |
| 1343 | iocb->ki_pos += written; |
| 1344 | } |
| 1345 | out: |
| 1346 | current->backing_dev_info = NULL; |
| 1347 | inode_unlock(inode); |
| 1348 | if (written > 0) |
| 1349 | written = generic_write_sync(iocb, written); |
| 1350 | |
| 1351 | return written ? written : err; |
| 1352 | } |
| 1353 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1354 | static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs, |
| 1355 | unsigned int index, |
| 1356 | unsigned int nr_pages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1357 | { |
| 1358 | int i; |
| 1359 | |
| 1360 | for (i = index; i < index + nr_pages; i++) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1361 | descs[i].length = PAGE_SIZE - descs[i].offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) |
| 1365 | { |
| 1366 | return (unsigned long)ii->iov->iov_base + ii->iov_offset; |
| 1367 | } |
| 1368 | |
| 1369 | static inline size_t fuse_get_frag_size(const struct iov_iter *ii, |
| 1370 | size_t max_size) |
| 1371 | { |
| 1372 | return min(iov_iter_single_seg_count(ii), max_size); |
| 1373 | } |
| 1374 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1375 | static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii, |
| 1376 | size_t *nbytesp, int write, |
| 1377 | unsigned int max_pages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1378 | { |
| 1379 | size_t nbytes = 0; /* # bytes already packed in req */ |
| 1380 | ssize_t ret = 0; |
| 1381 | |
| 1382 | /* Special case for kernel I/O: can copy directly into the buffer */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1383 | if (iov_iter_is_kvec(ii)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1384 | unsigned long user_addr = fuse_get_user_addr(ii); |
| 1385 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp); |
| 1386 | |
| 1387 | if (write) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1388 | ap->args.in_args[1].value = (void *) user_addr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1389 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1390 | ap->args.out_args[0].value = (void *) user_addr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1391 | |
| 1392 | iov_iter_advance(ii, frag_size); |
| 1393 | *nbytesp = frag_size; |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1397 | while (nbytes < *nbytesp && ap->num_pages < max_pages) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1398 | unsigned npages; |
| 1399 | size_t start; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1400 | ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages], |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1401 | *nbytesp - nbytes, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1402 | max_pages - ap->num_pages, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1403 | &start); |
| 1404 | if (ret < 0) |
| 1405 | break; |
| 1406 | |
| 1407 | iov_iter_advance(ii, ret); |
| 1408 | nbytes += ret; |
| 1409 | |
| 1410 | ret += start; |
| 1411 | npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE; |
| 1412 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1413 | ap->descs[ap->num_pages].offset = start; |
| 1414 | fuse_page_descs_length_init(ap->descs, ap->num_pages, npages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1415 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1416 | ap->num_pages += npages; |
| 1417 | ap->descs[ap->num_pages - 1].length -= |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1418 | (PAGE_SIZE - ret) & (PAGE_SIZE - 1); |
| 1419 | } |
| 1420 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1421 | ap->args.user_pages = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1422 | if (write) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1423 | ap->args.in_pages = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1424 | else |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1425 | ap->args.out_pages = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1426 | |
| 1427 | *nbytesp = nbytes; |
| 1428 | |
| 1429 | return ret < 0 ? ret : 0; |
| 1430 | } |
| 1431 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1432 | ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, |
| 1433 | loff_t *ppos, int flags) |
| 1434 | { |
| 1435 | int write = flags & FUSE_DIO_WRITE; |
| 1436 | int cuse = flags & FUSE_DIO_CUSE; |
| 1437 | struct file *file = io->iocb->ki_filp; |
| 1438 | struct inode *inode = file->f_mapping->host; |
| 1439 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1440 | struct fuse_conn *fc = ff->fm->fc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1441 | size_t nmax = write ? fc->max_write : fc->max_read; |
| 1442 | loff_t pos = *ppos; |
| 1443 | size_t count = iov_iter_count(iter); |
| 1444 | pgoff_t idx_from = pos >> PAGE_SHIFT; |
| 1445 | pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT; |
| 1446 | ssize_t res = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1447 | int err = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1448 | struct fuse_io_args *ia; |
| 1449 | unsigned int max_pages; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1450 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1451 | max_pages = iov_iter_npages(iter, fc->max_pages); |
| 1452 | ia = fuse_io_alloc(io, max_pages); |
| 1453 | if (!ia) |
| 1454 | return -ENOMEM; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1455 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1456 | ia->io = io; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1457 | if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) { |
| 1458 | if (!write) |
| 1459 | inode_lock(inode); |
| 1460 | fuse_sync_writes(inode); |
| 1461 | if (!write) |
| 1462 | inode_unlock(inode); |
| 1463 | } |
| 1464 | |
| 1465 | io->should_dirty = !write && iter_is_iovec(iter); |
| 1466 | while (count) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1467 | ssize_t nres; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1468 | fl_owner_t owner = current->files; |
| 1469 | size_t nbytes = min(count, nmax); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1470 | |
| 1471 | err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write, |
| 1472 | max_pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1473 | if (err && !nbytes) |
| 1474 | break; |
| 1475 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1476 | if (write) { |
| 1477 | if (!capable(CAP_FSETID)) |
| 1478 | ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1479 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1480 | nres = fuse_send_write(ia, pos, nbytes, owner); |
| 1481 | } else { |
| 1482 | nres = fuse_send_read(ia, pos, nbytes, owner); |
| 1483 | } |
| 1484 | |
| 1485 | if (!io->async || nres < 0) { |
| 1486 | fuse_release_user_pages(&ia->ap, io->should_dirty); |
| 1487 | fuse_io_free(ia); |
| 1488 | } |
| 1489 | ia = NULL; |
| 1490 | if (nres < 0) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1491 | iov_iter_revert(iter, nbytes); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1492 | err = nres; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1493 | break; |
| 1494 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1495 | WARN_ON(nres > nbytes); |
| 1496 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1497 | count -= nres; |
| 1498 | res += nres; |
| 1499 | pos += nres; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1500 | if (nres != nbytes) { |
| 1501 | iov_iter_revert(iter, nbytes - nres); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1502 | break; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1503 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1504 | if (count) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1505 | max_pages = iov_iter_npages(iter, fc->max_pages); |
| 1506 | ia = fuse_io_alloc(io, max_pages); |
| 1507 | if (!ia) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1508 | break; |
| 1509 | } |
| 1510 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1511 | if (ia) |
| 1512 | fuse_io_free(ia); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1513 | if (res > 0) |
| 1514 | *ppos = pos; |
| 1515 | |
| 1516 | return res > 0 ? res : err; |
| 1517 | } |
| 1518 | EXPORT_SYMBOL_GPL(fuse_direct_io); |
| 1519 | |
| 1520 | static ssize_t __fuse_direct_read(struct fuse_io_priv *io, |
| 1521 | struct iov_iter *iter, |
| 1522 | loff_t *ppos) |
| 1523 | { |
| 1524 | ssize_t res; |
| 1525 | struct inode *inode = file_inode(io->iocb->ki_filp); |
| 1526 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1527 | res = fuse_direct_io(io, iter, ppos, 0); |
| 1528 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1529 | fuse_invalidate_atime(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1530 | |
| 1531 | return res; |
| 1532 | } |
| 1533 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1534 | static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter); |
| 1535 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1536 | static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) |
| 1537 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1538 | ssize_t res; |
| 1539 | |
| 1540 | if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { |
| 1541 | res = fuse_direct_IO(iocb, to); |
| 1542 | } else { |
| 1543 | struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); |
| 1544 | |
| 1545 | res = __fuse_direct_read(&io, to, &iocb->ki_pos); |
| 1546 | } |
| 1547 | |
| 1548 | return res; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) |
| 1552 | { |
| 1553 | struct inode *inode = file_inode(iocb->ki_filp); |
| 1554 | struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); |
| 1555 | ssize_t res; |
| 1556 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1557 | /* Don't allow parallel writes to the same file */ |
| 1558 | inode_lock(inode); |
| 1559 | res = generic_write_checks(iocb, from); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1560 | if (res > 0) { |
| 1561 | if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { |
| 1562 | res = fuse_direct_IO(iocb, from); |
| 1563 | } else { |
| 1564 | res = fuse_direct_io(&io, from, &iocb->ki_pos, |
| 1565 | FUSE_DIO_WRITE); |
| 1566 | } |
| 1567 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1568 | fuse_invalidate_attr(inode); |
| 1569 | if (res > 0) |
| 1570 | fuse_write_update_size(inode, iocb->ki_pos); |
| 1571 | inode_unlock(inode); |
| 1572 | |
| 1573 | return res; |
| 1574 | } |
| 1575 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1576 | static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1577 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1578 | struct file *file = iocb->ki_filp; |
| 1579 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1580 | struct inode *inode = file_inode(file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1581 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1582 | if (fuse_is_bad(inode)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1583 | return -EIO; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1584 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1585 | if (FUSE_IS_DAX(inode)) |
| 1586 | return fuse_dax_read_iter(iocb, to); |
| 1587 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1588 | if (!(ff->open_flags & FOPEN_DIRECT_IO)) |
| 1589 | return fuse_cache_read_iter(iocb, to); |
| 1590 | else |
| 1591 | return fuse_direct_read_iter(iocb, to); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1594 | static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1595 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1596 | struct file *file = iocb->ki_filp; |
| 1597 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1598 | struct inode *inode = file_inode(file); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1599 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1600 | if (fuse_is_bad(inode)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1601 | return -EIO; |
| 1602 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1603 | if (FUSE_IS_DAX(inode)) |
| 1604 | return fuse_dax_write_iter(iocb, from); |
| 1605 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1606 | if (!(ff->open_flags & FOPEN_DIRECT_IO)) |
| 1607 | return fuse_cache_write_iter(iocb, from); |
| 1608 | else |
| 1609 | return fuse_direct_write_iter(iocb, from); |
| 1610 | } |
| 1611 | |
| 1612 | static void fuse_writepage_free(struct fuse_writepage_args *wpa) |
| 1613 | { |
| 1614 | struct fuse_args_pages *ap = &wpa->ia.ap; |
| 1615 | int i; |
| 1616 | |
| 1617 | for (i = 0; i < ap->num_pages; i++) |
| 1618 | __free_page(ap->pages[i]); |
| 1619 | |
| 1620 | if (wpa->ia.ff) |
| 1621 | fuse_file_put(wpa->ia.ff, false, false); |
| 1622 | |
| 1623 | kfree(ap->pages); |
| 1624 | kfree(wpa); |
| 1625 | } |
| 1626 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1627 | static void fuse_writepage_finish(struct fuse_mount *fm, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1628 | struct fuse_writepage_args *wpa) |
| 1629 | { |
| 1630 | struct fuse_args_pages *ap = &wpa->ia.ap; |
| 1631 | struct inode *inode = wpa->inode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1632 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1633 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
| 1634 | int i; |
| 1635 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1636 | for (i = 0; i < ap->num_pages; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1637 | dec_wb_stat(&bdi->wb, WB_WRITEBACK); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1638 | dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1639 | wb_writeout_inc(&bdi->wb); |
| 1640 | } |
| 1641 | wake_up(&fi->page_waitq); |
| 1642 | } |
| 1643 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1644 | /* Called under fi->lock, may release and reacquire it */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1645 | static void fuse_send_writepage(struct fuse_mount *fm, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1646 | struct fuse_writepage_args *wpa, loff_t size) |
| 1647 | __releases(fi->lock) |
| 1648 | __acquires(fi->lock) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1649 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1650 | struct fuse_writepage_args *aux, *next; |
| 1651 | struct fuse_inode *fi = get_fuse_inode(wpa->inode); |
| 1652 | struct fuse_write_in *inarg = &wpa->ia.write.in; |
| 1653 | struct fuse_args *args = &wpa->ia.ap.args; |
| 1654 | __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE; |
| 1655 | int err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1656 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1657 | fi->writectr++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1658 | if (inarg->offset + data_size <= size) { |
| 1659 | inarg->size = data_size; |
| 1660 | } else if (inarg->offset < size) { |
| 1661 | inarg->size = size - inarg->offset; |
| 1662 | } else { |
| 1663 | /* Got truncated off completely */ |
| 1664 | goto out_free; |
| 1665 | } |
| 1666 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1667 | args->in_args[1].size = inarg->size; |
| 1668 | args->force = true; |
| 1669 | args->nocreds = true; |
| 1670 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1671 | err = fuse_simple_background(fm, args, GFP_ATOMIC); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1672 | if (err == -ENOMEM) { |
| 1673 | spin_unlock(&fi->lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1674 | err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1675 | spin_lock(&fi->lock); |
| 1676 | } |
| 1677 | |
| 1678 | /* Fails on broken connection only */ |
| 1679 | if (unlikely(err)) |
| 1680 | goto out_free; |
| 1681 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1682 | return; |
| 1683 | |
| 1684 | out_free: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1685 | fi->writectr--; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1686 | rb_erase(&wpa->writepages_entry, &fi->writepages); |
| 1687 | fuse_writepage_finish(fm, wpa); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1688 | spin_unlock(&fi->lock); |
| 1689 | |
| 1690 | /* After fuse_writepage_finish() aux request list is private */ |
| 1691 | for (aux = wpa->next; aux; aux = next) { |
| 1692 | next = aux->next; |
| 1693 | aux->next = NULL; |
| 1694 | fuse_writepage_free(aux); |
| 1695 | } |
| 1696 | |
| 1697 | fuse_writepage_free(wpa); |
| 1698 | spin_lock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | /* |
| 1702 | * If fi->writectr is positive (no truncate or fsync going on) send |
| 1703 | * all queued writepage requests. |
| 1704 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1705 | * Called with fi->lock |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1706 | */ |
| 1707 | void fuse_flush_writepages(struct inode *inode) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1708 | __releases(fi->lock) |
| 1709 | __acquires(fi->lock) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1710 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1711 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1712 | struct fuse_inode *fi = get_fuse_inode(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1713 | loff_t crop = i_size_read(inode); |
| 1714 | struct fuse_writepage_args *wpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1715 | |
| 1716 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1717 | wpa = list_entry(fi->queued_writes.next, |
| 1718 | struct fuse_writepage_args, queue_entry); |
| 1719 | list_del_init(&wpa->queue_entry); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1720 | fuse_send_writepage(fm, wpa, crop); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1724 | static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root, |
| 1725 | struct fuse_writepage_args *wpa) |
| 1726 | { |
| 1727 | pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT; |
| 1728 | pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1; |
| 1729 | struct rb_node **p = &root->rb_node; |
| 1730 | struct rb_node *parent = NULL; |
| 1731 | |
| 1732 | WARN_ON(!wpa->ia.ap.num_pages); |
| 1733 | while (*p) { |
| 1734 | struct fuse_writepage_args *curr; |
| 1735 | pgoff_t curr_index; |
| 1736 | |
| 1737 | parent = *p; |
| 1738 | curr = rb_entry(parent, struct fuse_writepage_args, |
| 1739 | writepages_entry); |
| 1740 | WARN_ON(curr->inode != wpa->inode); |
| 1741 | curr_index = curr->ia.write.in.offset >> PAGE_SHIFT; |
| 1742 | |
| 1743 | if (idx_from >= curr_index + curr->ia.ap.num_pages) |
| 1744 | p = &(*p)->rb_right; |
| 1745 | else if (idx_to < curr_index) |
| 1746 | p = &(*p)->rb_left; |
| 1747 | else |
| 1748 | return curr; |
| 1749 | } |
| 1750 | |
| 1751 | rb_link_node(&wpa->writepages_entry, parent, p); |
| 1752 | rb_insert_color(&wpa->writepages_entry, root); |
| 1753 | return NULL; |
| 1754 | } |
| 1755 | |
| 1756 | static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa) |
| 1757 | { |
| 1758 | WARN_ON(fuse_insert_writeback(root, wpa)); |
| 1759 | } |
| 1760 | |
| 1761 | static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1762 | int error) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1763 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1764 | struct fuse_writepage_args *wpa = |
| 1765 | container_of(args, typeof(*wpa), ia.ap.args); |
| 1766 | struct inode *inode = wpa->inode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1767 | struct fuse_inode *fi = get_fuse_inode(inode); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1768 | struct fuse_conn *fc = get_fuse_conn(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1769 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1770 | mapping_set_error(inode->i_mapping, error); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1771 | /* |
| 1772 | * A writeback finished and this might have updated mtime/ctime on |
| 1773 | * server making local mtime/ctime stale. Hence invalidate attrs. |
| 1774 | * Do this only if writeback_cache is not enabled. If writeback_cache |
| 1775 | * is enabled, we trust local ctime/mtime. |
| 1776 | */ |
| 1777 | if (!fc->writeback_cache) |
| 1778 | fuse_invalidate_attr(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1779 | spin_lock(&fi->lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1780 | rb_erase(&wpa->writepages_entry, &fi->writepages); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1781 | while (wpa->next) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1782 | struct fuse_mount *fm = get_fuse_mount(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1783 | struct fuse_write_in *inarg = &wpa->ia.write.in; |
| 1784 | struct fuse_writepage_args *next = wpa->next; |
| 1785 | |
| 1786 | wpa->next = next->next; |
| 1787 | next->next = NULL; |
| 1788 | next->ia.ff = fuse_file_get(wpa->ia.ff); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1789 | tree_insert(&fi->writepages, next); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1790 | |
| 1791 | /* |
| 1792 | * Skip fuse_flush_writepages() to make it easy to crop requests |
| 1793 | * based on primary request size. |
| 1794 | * |
| 1795 | * 1st case (trivial): there are no concurrent activities using |
| 1796 | * fuse_set/release_nowrite. Then we're on safe side because |
| 1797 | * fuse_flush_writepages() would call fuse_send_writepage() |
| 1798 | * anyway. |
| 1799 | * |
| 1800 | * 2nd case: someone called fuse_set_nowrite and it is waiting |
| 1801 | * now for completion of all in-flight requests. This happens |
| 1802 | * rarely and no more than once per page, so this should be |
| 1803 | * okay. |
| 1804 | * |
| 1805 | * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle |
| 1806 | * of fuse_set_nowrite..fuse_release_nowrite section. The fact |
| 1807 | * that fuse_set_nowrite returned implies that all in-flight |
| 1808 | * requests were completed along with all of their secondary |
| 1809 | * requests. Further primary requests are blocked by negative |
| 1810 | * writectr. Hence there cannot be any in-flight requests and |
| 1811 | * no invocations of fuse_writepage_end() while we're in |
| 1812 | * fuse_set_nowrite..fuse_release_nowrite section. |
| 1813 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1814 | fuse_send_writepage(fm, next, inarg->offset + inarg->size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1815 | } |
| 1816 | fi->writectr--; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1817 | fuse_writepage_finish(fm, wpa); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1818 | spin_unlock(&fi->lock); |
| 1819 | fuse_writepage_free(wpa); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc, |
| 1823 | struct fuse_inode *fi) |
| 1824 | { |
| 1825 | struct fuse_file *ff = NULL; |
| 1826 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1827 | spin_lock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1828 | if (!list_empty(&fi->write_files)) { |
| 1829 | ff = list_entry(fi->write_files.next, struct fuse_file, |
| 1830 | write_entry); |
| 1831 | fuse_file_get(ff); |
| 1832 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1833 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1834 | |
| 1835 | return ff; |
| 1836 | } |
| 1837 | |
| 1838 | static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc, |
| 1839 | struct fuse_inode *fi) |
| 1840 | { |
| 1841 | struct fuse_file *ff = __fuse_write_file_get(fc, fi); |
| 1842 | WARN_ON(!ff); |
| 1843 | return ff; |
| 1844 | } |
| 1845 | |
| 1846 | int fuse_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 1847 | { |
| 1848 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1849 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 1850 | struct fuse_file *ff; |
| 1851 | int err; |
| 1852 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1853 | /* |
| 1854 | * Inode is always written before the last reference is dropped and |
| 1855 | * hence this should not be reached from reclaim. |
| 1856 | * |
| 1857 | * Writing back the inode from reclaim can deadlock if the request |
| 1858 | * processing itself needs an allocation. Allocations triggering |
| 1859 | * reclaim while serving a request can't be prevented, because it can |
| 1860 | * involve any number of unrelated userspace processes. |
| 1861 | */ |
| 1862 | WARN_ON(wbc->for_reclaim); |
| 1863 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1864 | ff = __fuse_write_file_get(fc, fi); |
| 1865 | err = fuse_flush_times(inode, ff); |
| 1866 | if (ff) |
| 1867 | fuse_file_put(ff, false, false); |
| 1868 | |
| 1869 | return err; |
| 1870 | } |
| 1871 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1872 | static struct fuse_writepage_args *fuse_writepage_args_alloc(void) |
| 1873 | { |
| 1874 | struct fuse_writepage_args *wpa; |
| 1875 | struct fuse_args_pages *ap; |
| 1876 | |
| 1877 | wpa = kzalloc(sizeof(*wpa), GFP_NOFS); |
| 1878 | if (wpa) { |
| 1879 | ap = &wpa->ia.ap; |
| 1880 | ap->num_pages = 0; |
| 1881 | ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs); |
| 1882 | if (!ap->pages) { |
| 1883 | kfree(wpa); |
| 1884 | wpa = NULL; |
| 1885 | } |
| 1886 | } |
| 1887 | return wpa; |
| 1888 | |
| 1889 | } |
| 1890 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1891 | static int fuse_writepage_locked(struct page *page) |
| 1892 | { |
| 1893 | struct address_space *mapping = page->mapping; |
| 1894 | struct inode *inode = mapping->host; |
| 1895 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 1896 | struct fuse_inode *fi = get_fuse_inode(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1897 | struct fuse_writepage_args *wpa; |
| 1898 | struct fuse_args_pages *ap; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1899 | struct page *tmp_page; |
| 1900 | int error = -ENOMEM; |
| 1901 | |
| 1902 | set_page_writeback(page); |
| 1903 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1904 | wpa = fuse_writepage_args_alloc(); |
| 1905 | if (!wpa) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1906 | goto err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1907 | ap = &wpa->ia.ap; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1908 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1909 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
| 1910 | if (!tmp_page) |
| 1911 | goto err_free; |
| 1912 | |
| 1913 | error = -EIO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1914 | wpa->ia.ff = fuse_write_file_get(fc, fi); |
| 1915 | if (!wpa->ia.ff) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1916 | goto err_nofile; |
| 1917 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1918 | fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1919 | |
| 1920 | copy_highpage(tmp_page, page); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1921 | wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE; |
| 1922 | wpa->next = NULL; |
| 1923 | ap->args.in_pages = true; |
| 1924 | ap->num_pages = 1; |
| 1925 | ap->pages[0] = tmp_page; |
| 1926 | ap->descs[0].offset = 0; |
| 1927 | ap->descs[0].length = PAGE_SIZE; |
| 1928 | ap->args.end = fuse_writepage_end; |
| 1929 | wpa->inode = inode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1930 | |
| 1931 | inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); |
| 1932 | inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); |
| 1933 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1934 | spin_lock(&fi->lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1935 | tree_insert(&fi->writepages, wpa); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1936 | list_add_tail(&wpa->queue_entry, &fi->queued_writes); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1937 | fuse_flush_writepages(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1938 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1939 | |
| 1940 | end_page_writeback(page); |
| 1941 | |
| 1942 | return 0; |
| 1943 | |
| 1944 | err_nofile: |
| 1945 | __free_page(tmp_page); |
| 1946 | err_free: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1947 | kfree(wpa); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1948 | err: |
| 1949 | mapping_set_error(page->mapping, error); |
| 1950 | end_page_writeback(page); |
| 1951 | return error; |
| 1952 | } |
| 1953 | |
| 1954 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) |
| 1955 | { |
| 1956 | int err; |
| 1957 | |
| 1958 | if (fuse_page_is_writeback(page->mapping->host, page->index)) { |
| 1959 | /* |
| 1960 | * ->writepages() should be called for sync() and friends. We |
| 1961 | * should only get here on direct reclaim and then we are |
| 1962 | * allowed to skip a page which is already in flight |
| 1963 | */ |
| 1964 | WARN_ON(wbc->sync_mode == WB_SYNC_ALL); |
| 1965 | |
| 1966 | redirty_page_for_writepage(wbc, page); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1967 | unlock_page(page); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1968 | return 0; |
| 1969 | } |
| 1970 | |
| 1971 | err = fuse_writepage_locked(page); |
| 1972 | unlock_page(page); |
| 1973 | |
| 1974 | return err; |
| 1975 | } |
| 1976 | |
| 1977 | struct fuse_fill_wb_data { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1978 | struct fuse_writepage_args *wpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1979 | struct fuse_file *ff; |
| 1980 | struct inode *inode; |
| 1981 | struct page **orig_pages; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1982 | unsigned int max_pages; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1983 | }; |
| 1984 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1985 | static bool fuse_pages_realloc(struct fuse_fill_wb_data *data) |
| 1986 | { |
| 1987 | struct fuse_args_pages *ap = &data->wpa->ia.ap; |
| 1988 | struct fuse_conn *fc = get_fuse_conn(data->inode); |
| 1989 | struct page **pages; |
| 1990 | struct fuse_page_desc *descs; |
| 1991 | unsigned int npages = min_t(unsigned int, |
| 1992 | max_t(unsigned int, data->max_pages * 2, |
| 1993 | FUSE_DEFAULT_MAX_PAGES_PER_REQ), |
| 1994 | fc->max_pages); |
| 1995 | WARN_ON(npages <= data->max_pages); |
| 1996 | |
| 1997 | pages = fuse_pages_alloc(npages, GFP_NOFS, &descs); |
| 1998 | if (!pages) |
| 1999 | return false; |
| 2000 | |
| 2001 | memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages); |
| 2002 | memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages); |
| 2003 | kfree(ap->pages); |
| 2004 | ap->pages = pages; |
| 2005 | ap->descs = descs; |
| 2006 | data->max_pages = npages; |
| 2007 | |
| 2008 | return true; |
| 2009 | } |
| 2010 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2011 | static void fuse_writepages_send(struct fuse_fill_wb_data *data) |
| 2012 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2013 | struct fuse_writepage_args *wpa = data->wpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2014 | struct inode *inode = data->inode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2015 | struct fuse_inode *fi = get_fuse_inode(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2016 | int num_pages = wpa->ia.ap.num_pages; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2017 | int i; |
| 2018 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2019 | wpa->ia.ff = fuse_file_get(data->ff); |
| 2020 | spin_lock(&fi->lock); |
| 2021 | list_add_tail(&wpa->queue_entry, &fi->queued_writes); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2022 | fuse_flush_writepages(inode); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2023 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2024 | |
| 2025 | for (i = 0; i < num_pages; i++) |
| 2026 | end_page_writeback(data->orig_pages[i]); |
| 2027 | } |
| 2028 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2029 | /* |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2030 | * Check under fi->lock if the page is under writeback, and insert it onto the |
| 2031 | * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2032 | * one already added for a page at this offset. If there's none, then insert |
| 2033 | * this new request onto the auxiliary list, otherwise reuse the existing one by |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2034 | * swapping the new temp page with the old one. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2035 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2036 | static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa, |
| 2037 | struct page *page) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2038 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2039 | struct fuse_inode *fi = get_fuse_inode(new_wpa->inode); |
| 2040 | struct fuse_writepage_args *tmp; |
| 2041 | struct fuse_writepage_args *old_wpa; |
| 2042 | struct fuse_args_pages *new_ap = &new_wpa->ia.ap; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2043 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2044 | WARN_ON(new_ap->num_pages != 0); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2045 | new_ap->num_pages = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2046 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2047 | spin_lock(&fi->lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2048 | old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2049 | if (!old_wpa) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2050 | spin_unlock(&fi->lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2051 | return true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2054 | for (tmp = old_wpa->next; tmp; tmp = tmp->next) { |
| 2055 | pgoff_t curr_index; |
| 2056 | |
| 2057 | WARN_ON(tmp->inode != new_wpa->inode); |
| 2058 | curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT; |
| 2059 | if (curr_index == page->index) { |
| 2060 | WARN_ON(tmp->ia.ap.num_pages != 1); |
| 2061 | swap(tmp->ia.ap.pages[0], new_ap->pages[0]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2062 | break; |
| 2063 | } |
| 2064 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2065 | |
| 2066 | if (!tmp) { |
| 2067 | new_wpa->next = old_wpa->next; |
| 2068 | old_wpa->next = new_wpa; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2071 | spin_unlock(&fi->lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2072 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2073 | if (tmp) { |
| 2074 | struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2075 | |
| 2076 | dec_wb_stat(&bdi->wb, WB_WRITEBACK); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2077 | dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2078 | wb_writeout_inc(&bdi->wb); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2079 | fuse_writepage_free(new_wpa); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2080 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2081 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2082 | return false; |
| 2083 | } |
| 2084 | |
| 2085 | static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page, |
| 2086 | struct fuse_args_pages *ap, |
| 2087 | struct fuse_fill_wb_data *data) |
| 2088 | { |
| 2089 | WARN_ON(!ap->num_pages); |
| 2090 | |
| 2091 | /* |
| 2092 | * Being under writeback is unlikely but possible. For example direct |
| 2093 | * read to an mmaped fuse file will set the page dirty twice; once when |
| 2094 | * the pages are faulted with get_user_pages(), and then after the read |
| 2095 | * completed. |
| 2096 | */ |
| 2097 | if (fuse_page_is_writeback(data->inode, page->index)) |
| 2098 | return true; |
| 2099 | |
| 2100 | /* Reached max pages */ |
| 2101 | if (ap->num_pages == fc->max_pages) |
| 2102 | return true; |
| 2103 | |
| 2104 | /* Reached max write bytes */ |
| 2105 | if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write) |
| 2106 | return true; |
| 2107 | |
| 2108 | /* Discontinuity */ |
| 2109 | if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index) |
| 2110 | return true; |
| 2111 | |
| 2112 | /* Need to grow the pages array? If so, did the expansion fail? */ |
| 2113 | if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data)) |
| 2114 | return true; |
| 2115 | |
| 2116 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | static int fuse_writepages_fill(struct page *page, |
| 2120 | struct writeback_control *wbc, void *_data) |
| 2121 | { |
| 2122 | struct fuse_fill_wb_data *data = _data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2123 | struct fuse_writepage_args *wpa = data->wpa; |
| 2124 | struct fuse_args_pages *ap = &wpa->ia.ap; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2125 | struct inode *inode = data->inode; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2126 | struct fuse_inode *fi = get_fuse_inode(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2127 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 2128 | struct page *tmp_page; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2129 | int err; |
| 2130 | |
| 2131 | if (!data->ff) { |
| 2132 | err = -EIO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2133 | data->ff = fuse_write_file_get(fc, fi); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2134 | if (!data->ff) |
| 2135 | goto out_unlock; |
| 2136 | } |
| 2137 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2138 | if (wpa && fuse_writepage_need_send(fc, page, ap, data)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2139 | fuse_writepages_send(data); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2140 | data->wpa = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2141 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2142 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2143 | err = -ENOMEM; |
| 2144 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
| 2145 | if (!tmp_page) |
| 2146 | goto out_unlock; |
| 2147 | |
| 2148 | /* |
| 2149 | * The page must not be redirtied until the writeout is completed |
| 2150 | * (i.e. userspace has sent a reply to the write request). Otherwise |
| 2151 | * there could be more than one temporary page instance for each real |
| 2152 | * page. |
| 2153 | * |
| 2154 | * This is ensured by holding the page lock in page_mkwrite() while |
| 2155 | * checking fuse_page_is_writeback(). We already hold the page lock |
| 2156 | * since clear_page_dirty_for_io() and keep it held until we add the |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2157 | * request to the fi->writepages list and increment ap->num_pages. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2158 | * After this fuse_page_is_writeback() will indicate that the page is |
| 2159 | * under writeback, so we can release the page lock. |
| 2160 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2161 | if (data->wpa == NULL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2162 | err = -ENOMEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2163 | wpa = fuse_writepage_args_alloc(); |
| 2164 | if (!wpa) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2165 | __free_page(tmp_page); |
| 2166 | goto out_unlock; |
| 2167 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2168 | data->max_pages = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2169 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2170 | ap = &wpa->ia.ap; |
| 2171 | fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0); |
| 2172 | wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE; |
| 2173 | wpa->next = NULL; |
| 2174 | ap->args.in_pages = true; |
| 2175 | ap->args.end = fuse_writepage_end; |
| 2176 | ap->num_pages = 0; |
| 2177 | wpa->inode = inode; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2178 | } |
| 2179 | set_page_writeback(page); |
| 2180 | |
| 2181 | copy_highpage(tmp_page, page); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2182 | ap->pages[ap->num_pages] = tmp_page; |
| 2183 | ap->descs[ap->num_pages].offset = 0; |
| 2184 | ap->descs[ap->num_pages].length = PAGE_SIZE; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2185 | data->orig_pages[ap->num_pages] = page; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2186 | |
| 2187 | inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); |
| 2188 | inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); |
| 2189 | |
| 2190 | err = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2191 | if (data->wpa) { |
| 2192 | /* |
| 2193 | * Protected by fi->lock against concurrent access by |
| 2194 | * fuse_page_is_writeback(). |
| 2195 | */ |
| 2196 | spin_lock(&fi->lock); |
| 2197 | ap->num_pages++; |
| 2198 | spin_unlock(&fi->lock); |
| 2199 | } else if (fuse_writepage_add(wpa, page)) { |
| 2200 | data->wpa = wpa; |
| 2201 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2202 | end_page_writeback(page); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2203 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2204 | out_unlock: |
| 2205 | unlock_page(page); |
| 2206 | |
| 2207 | return err; |
| 2208 | } |
| 2209 | |
| 2210 | static int fuse_writepages(struct address_space *mapping, |
| 2211 | struct writeback_control *wbc) |
| 2212 | { |
| 2213 | struct inode *inode = mapping->host; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2214 | struct fuse_conn *fc = get_fuse_conn(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2215 | struct fuse_fill_wb_data data; |
| 2216 | int err; |
| 2217 | |
| 2218 | err = -EIO; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2219 | if (fuse_is_bad(inode)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2220 | goto out; |
| 2221 | |
| 2222 | data.inode = inode; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2223 | data.wpa = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2224 | data.ff = NULL; |
| 2225 | |
| 2226 | err = -ENOMEM; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2227 | data.orig_pages = kcalloc(fc->max_pages, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2228 | sizeof(struct page *), |
| 2229 | GFP_NOFS); |
| 2230 | if (!data.orig_pages) |
| 2231 | goto out; |
| 2232 | |
| 2233 | err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2234 | if (data.wpa) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2235 | WARN_ON(!data.wpa->ia.ap.num_pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2236 | fuse_writepages_send(&data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2237 | } |
| 2238 | if (data.ff) |
| 2239 | fuse_file_put(data.ff, false, false); |
| 2240 | |
| 2241 | kfree(data.orig_pages); |
| 2242 | out: |
| 2243 | return err; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * It's worthy to make sure that space is reserved on disk for the write, |
| 2248 | * but how to implement it without killing performance need more thinking. |
| 2249 | */ |
| 2250 | static int fuse_write_begin(struct file *file, struct address_space *mapping, |
| 2251 | loff_t pos, unsigned len, unsigned flags, |
| 2252 | struct page **pagep, void **fsdata) |
| 2253 | { |
| 2254 | pgoff_t index = pos >> PAGE_SHIFT; |
| 2255 | struct fuse_conn *fc = get_fuse_conn(file_inode(file)); |
| 2256 | struct page *page; |
| 2257 | loff_t fsize; |
| 2258 | int err = -ENOMEM; |
| 2259 | |
| 2260 | WARN_ON(!fc->writeback_cache); |
| 2261 | |
| 2262 | page = grab_cache_page_write_begin(mapping, index, flags); |
| 2263 | if (!page) |
| 2264 | goto error; |
| 2265 | |
| 2266 | fuse_wait_on_page_writeback(mapping->host, page->index); |
| 2267 | |
| 2268 | if (PageUptodate(page) || len == PAGE_SIZE) |
| 2269 | goto success; |
| 2270 | /* |
| 2271 | * Check if the start this page comes after the end of file, in which |
| 2272 | * case the readpage can be optimized away. |
| 2273 | */ |
| 2274 | fsize = i_size_read(mapping->host); |
| 2275 | if (fsize <= (pos & PAGE_MASK)) { |
| 2276 | size_t off = pos & ~PAGE_MASK; |
| 2277 | if (off) |
| 2278 | zero_user_segment(page, 0, off); |
| 2279 | goto success; |
| 2280 | } |
| 2281 | err = fuse_do_readpage(file, page); |
| 2282 | if (err) |
| 2283 | goto cleanup; |
| 2284 | success: |
| 2285 | *pagep = page; |
| 2286 | return 0; |
| 2287 | |
| 2288 | cleanup: |
| 2289 | unlock_page(page); |
| 2290 | put_page(page); |
| 2291 | error: |
| 2292 | return err; |
| 2293 | } |
| 2294 | |
| 2295 | static int fuse_write_end(struct file *file, struct address_space *mapping, |
| 2296 | loff_t pos, unsigned len, unsigned copied, |
| 2297 | struct page *page, void *fsdata) |
| 2298 | { |
| 2299 | struct inode *inode = page->mapping->host; |
| 2300 | |
| 2301 | /* Haven't copied anything? Skip zeroing, size extending, dirtying. */ |
| 2302 | if (!copied) |
| 2303 | goto unlock; |
| 2304 | |
| 2305 | if (!PageUptodate(page)) { |
| 2306 | /* Zero any unwritten bytes at the end of the page */ |
| 2307 | size_t endoff = (pos + copied) & ~PAGE_MASK; |
| 2308 | if (endoff) |
| 2309 | zero_user_segment(page, endoff, PAGE_SIZE); |
| 2310 | SetPageUptodate(page); |
| 2311 | } |
| 2312 | |
| 2313 | fuse_write_update_size(inode, pos + copied); |
| 2314 | set_page_dirty(page); |
| 2315 | |
| 2316 | unlock: |
| 2317 | unlock_page(page); |
| 2318 | put_page(page); |
| 2319 | |
| 2320 | return copied; |
| 2321 | } |
| 2322 | |
| 2323 | static int fuse_launder_page(struct page *page) |
| 2324 | { |
| 2325 | int err = 0; |
| 2326 | if (clear_page_dirty_for_io(page)) { |
| 2327 | struct inode *inode = page->mapping->host; |
| 2328 | err = fuse_writepage_locked(page); |
| 2329 | if (!err) |
| 2330 | fuse_wait_on_page_writeback(inode, page->index); |
| 2331 | } |
| 2332 | return err; |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Write back dirty pages now, because there may not be any suitable |
| 2337 | * open files later |
| 2338 | */ |
| 2339 | static void fuse_vma_close(struct vm_area_struct *vma) |
| 2340 | { |
| 2341 | filemap_write_and_wait(vma->vm_file->f_mapping); |
| 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | * Wait for writeback against this page to complete before allowing it |
| 2346 | * to be marked dirty again, and hence written back again, possibly |
| 2347 | * before the previous writepage completed. |
| 2348 | * |
| 2349 | * Block here, instead of in ->writepage(), so that the userspace fs |
| 2350 | * can only block processes actually operating on the filesystem. |
| 2351 | * |
| 2352 | * Otherwise unprivileged userspace fs would be able to block |
| 2353 | * unrelated: |
| 2354 | * |
| 2355 | * - page migration |
| 2356 | * - sync(2) |
| 2357 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER |
| 2358 | */ |
| 2359 | static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf) |
| 2360 | { |
| 2361 | struct page *page = vmf->page; |
| 2362 | struct inode *inode = file_inode(vmf->vma->vm_file); |
| 2363 | |
| 2364 | file_update_time(vmf->vma->vm_file); |
| 2365 | lock_page(page); |
| 2366 | if (page->mapping != inode->i_mapping) { |
| 2367 | unlock_page(page); |
| 2368 | return VM_FAULT_NOPAGE; |
| 2369 | } |
| 2370 | |
| 2371 | fuse_wait_on_page_writeback(inode, page->index); |
| 2372 | return VM_FAULT_LOCKED; |
| 2373 | } |
| 2374 | |
| 2375 | static const struct vm_operations_struct fuse_file_vm_ops = { |
| 2376 | .close = fuse_vma_close, |
| 2377 | .fault = filemap_fault, |
| 2378 | .map_pages = filemap_map_pages, |
| 2379 | .page_mkwrite = fuse_page_mkwrite, |
| 2380 | }; |
| 2381 | |
| 2382 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 2383 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2384 | struct fuse_file *ff = file->private_data; |
| 2385 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2386 | /* DAX mmap is superior to direct_io mmap */ |
| 2387 | if (FUSE_IS_DAX(file_inode(file))) |
| 2388 | return fuse_dax_mmap(file, vma); |
| 2389 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2390 | if (ff->open_flags & FOPEN_DIRECT_IO) { |
| 2391 | /* Can't provide the coherency needed for MAP_SHARED */ |
| 2392 | if (vma->vm_flags & VM_MAYSHARE) |
| 2393 | return -ENODEV; |
| 2394 | |
| 2395 | invalidate_inode_pages2(file->f_mapping); |
| 2396 | |
| 2397 | return generic_file_mmap(file, vma); |
| 2398 | } |
| 2399 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2400 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
| 2401 | fuse_link_write_file(file); |
| 2402 | |
| 2403 | file_accessed(file); |
| 2404 | vma->vm_ops = &fuse_file_vm_ops; |
| 2405 | return 0; |
| 2406 | } |
| 2407 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2408 | static int convert_fuse_file_lock(struct fuse_conn *fc, |
| 2409 | const struct fuse_file_lock *ffl, |
| 2410 | struct file_lock *fl) |
| 2411 | { |
| 2412 | switch (ffl->type) { |
| 2413 | case F_UNLCK: |
| 2414 | break; |
| 2415 | |
| 2416 | case F_RDLCK: |
| 2417 | case F_WRLCK: |
| 2418 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || |
| 2419 | ffl->end < ffl->start) |
| 2420 | return -EIO; |
| 2421 | |
| 2422 | fl->fl_start = ffl->start; |
| 2423 | fl->fl_end = ffl->end; |
| 2424 | |
| 2425 | /* |
| 2426 | * Convert pid into init's pid namespace. The locks API will |
| 2427 | * translate it into the caller's pid namespace. |
| 2428 | */ |
| 2429 | rcu_read_lock(); |
| 2430 | fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns); |
| 2431 | rcu_read_unlock(); |
| 2432 | break; |
| 2433 | |
| 2434 | default: |
| 2435 | return -EIO; |
| 2436 | } |
| 2437 | fl->fl_type = ffl->type; |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
| 2441 | static void fuse_lk_fill(struct fuse_args *args, struct file *file, |
| 2442 | const struct file_lock *fl, int opcode, pid_t pid, |
| 2443 | int flock, struct fuse_lk_in *inarg) |
| 2444 | { |
| 2445 | struct inode *inode = file_inode(file); |
| 2446 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 2447 | struct fuse_file *ff = file->private_data; |
| 2448 | |
| 2449 | memset(inarg, 0, sizeof(*inarg)); |
| 2450 | inarg->fh = ff->fh; |
| 2451 | inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
| 2452 | inarg->lk.start = fl->fl_start; |
| 2453 | inarg->lk.end = fl->fl_end; |
| 2454 | inarg->lk.type = fl->fl_type; |
| 2455 | inarg->lk.pid = pid; |
| 2456 | if (flock) |
| 2457 | inarg->lk_flags |= FUSE_LK_FLOCK; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2458 | args->opcode = opcode; |
| 2459 | args->nodeid = get_node_id(inode); |
| 2460 | args->in_numargs = 1; |
| 2461 | args->in_args[0].size = sizeof(*inarg); |
| 2462 | args->in_args[0].value = inarg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | static int fuse_getlk(struct file *file, struct file_lock *fl) |
| 2466 | { |
| 2467 | struct inode *inode = file_inode(file); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2468 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2469 | FUSE_ARGS(args); |
| 2470 | struct fuse_lk_in inarg; |
| 2471 | struct fuse_lk_out outarg; |
| 2472 | int err; |
| 2473 | |
| 2474 | fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2475 | args.out_numargs = 1; |
| 2476 | args.out_args[0].size = sizeof(outarg); |
| 2477 | args.out_args[0].value = &outarg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2478 | err = fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2479 | if (!err) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2480 | err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2481 | |
| 2482 | return err; |
| 2483 | } |
| 2484 | |
| 2485 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
| 2486 | { |
| 2487 | struct inode *inode = file_inode(file); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2488 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2489 | FUSE_ARGS(args); |
| 2490 | struct fuse_lk_in inarg; |
| 2491 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; |
| 2492 | struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2493 | pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2494 | int err; |
| 2495 | |
| 2496 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) { |
| 2497 | /* NLM needs asynchronous locks, which we don't support yet */ |
| 2498 | return -ENOLCK; |
| 2499 | } |
| 2500 | |
| 2501 | /* Unlock on close is handled by the flush method */ |
| 2502 | if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX) |
| 2503 | return 0; |
| 2504 | |
| 2505 | fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2506 | err = fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2507 | |
| 2508 | /* locking is restartable */ |
| 2509 | if (err == -EINTR) |
| 2510 | err = -ERESTARTSYS; |
| 2511 | |
| 2512 | return err; |
| 2513 | } |
| 2514 | |
| 2515 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) |
| 2516 | { |
| 2517 | struct inode *inode = file_inode(file); |
| 2518 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 2519 | int err; |
| 2520 | |
| 2521 | if (cmd == F_CANCELLK) { |
| 2522 | err = 0; |
| 2523 | } else if (cmd == F_GETLK) { |
| 2524 | if (fc->no_lock) { |
| 2525 | posix_test_lock(file, fl); |
| 2526 | err = 0; |
| 2527 | } else |
| 2528 | err = fuse_getlk(file, fl); |
| 2529 | } else { |
| 2530 | if (fc->no_lock) |
| 2531 | err = posix_lock_file(file, fl, NULL); |
| 2532 | else |
| 2533 | err = fuse_setlk(file, fl, 0); |
| 2534 | } |
| 2535 | return err; |
| 2536 | } |
| 2537 | |
| 2538 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
| 2539 | { |
| 2540 | struct inode *inode = file_inode(file); |
| 2541 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 2542 | int err; |
| 2543 | |
| 2544 | if (fc->no_flock) { |
| 2545 | err = locks_lock_file_wait(file, fl); |
| 2546 | } else { |
| 2547 | struct fuse_file *ff = file->private_data; |
| 2548 | |
| 2549 | /* emulate flock with POSIX locks */ |
| 2550 | ff->flock = true; |
| 2551 | err = fuse_setlk(file, fl, 1); |
| 2552 | } |
| 2553 | |
| 2554 | return err; |
| 2555 | } |
| 2556 | |
| 2557 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
| 2558 | { |
| 2559 | struct inode *inode = mapping->host; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2560 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2561 | FUSE_ARGS(args); |
| 2562 | struct fuse_bmap_in inarg; |
| 2563 | struct fuse_bmap_out outarg; |
| 2564 | int err; |
| 2565 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2566 | if (!inode->i_sb->s_bdev || fm->fc->no_bmap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2567 | return 0; |
| 2568 | |
| 2569 | memset(&inarg, 0, sizeof(inarg)); |
| 2570 | inarg.block = block; |
| 2571 | inarg.blocksize = inode->i_sb->s_blocksize; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2572 | args.opcode = FUSE_BMAP; |
| 2573 | args.nodeid = get_node_id(inode); |
| 2574 | args.in_numargs = 1; |
| 2575 | args.in_args[0].size = sizeof(inarg); |
| 2576 | args.in_args[0].value = &inarg; |
| 2577 | args.out_numargs = 1; |
| 2578 | args.out_args[0].size = sizeof(outarg); |
| 2579 | args.out_args[0].value = &outarg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2580 | err = fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2581 | if (err == -ENOSYS) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2582 | fm->fc->no_bmap = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2583 | |
| 2584 | return err ? 0 : outarg.block; |
| 2585 | } |
| 2586 | |
| 2587 | static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) |
| 2588 | { |
| 2589 | struct inode *inode = file->f_mapping->host; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2590 | struct fuse_mount *fm = get_fuse_mount(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2591 | struct fuse_file *ff = file->private_data; |
| 2592 | FUSE_ARGS(args); |
| 2593 | struct fuse_lseek_in inarg = { |
| 2594 | .fh = ff->fh, |
| 2595 | .offset = offset, |
| 2596 | .whence = whence |
| 2597 | }; |
| 2598 | struct fuse_lseek_out outarg; |
| 2599 | int err; |
| 2600 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2601 | if (fm->fc->no_lseek) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2602 | goto fallback; |
| 2603 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2604 | args.opcode = FUSE_LSEEK; |
| 2605 | args.nodeid = ff->nodeid; |
| 2606 | args.in_numargs = 1; |
| 2607 | args.in_args[0].size = sizeof(inarg); |
| 2608 | args.in_args[0].value = &inarg; |
| 2609 | args.out_numargs = 1; |
| 2610 | args.out_args[0].size = sizeof(outarg); |
| 2611 | args.out_args[0].value = &outarg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2612 | err = fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2613 | if (err) { |
| 2614 | if (err == -ENOSYS) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2615 | fm->fc->no_lseek = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2616 | goto fallback; |
| 2617 | } |
| 2618 | return err; |
| 2619 | } |
| 2620 | |
| 2621 | return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes); |
| 2622 | |
| 2623 | fallback: |
| 2624 | err = fuse_update_attributes(inode, file); |
| 2625 | if (!err) |
| 2626 | return generic_file_llseek(file, offset, whence); |
| 2627 | else |
| 2628 | return err; |
| 2629 | } |
| 2630 | |
| 2631 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) |
| 2632 | { |
| 2633 | loff_t retval; |
| 2634 | struct inode *inode = file_inode(file); |
| 2635 | |
| 2636 | switch (whence) { |
| 2637 | case SEEK_SET: |
| 2638 | case SEEK_CUR: |
| 2639 | /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ |
| 2640 | retval = generic_file_llseek(file, offset, whence); |
| 2641 | break; |
| 2642 | case SEEK_END: |
| 2643 | inode_lock(inode); |
| 2644 | retval = fuse_update_attributes(inode, file); |
| 2645 | if (!retval) |
| 2646 | retval = generic_file_llseek(file, offset, whence); |
| 2647 | inode_unlock(inode); |
| 2648 | break; |
| 2649 | case SEEK_HOLE: |
| 2650 | case SEEK_DATA: |
| 2651 | inode_lock(inode); |
| 2652 | retval = fuse_lseek(file, offset, whence); |
| 2653 | inode_unlock(inode); |
| 2654 | break; |
| 2655 | default: |
| 2656 | retval = -EINVAL; |
| 2657 | } |
| 2658 | |
| 2659 | return retval; |
| 2660 | } |
| 2661 | |
| 2662 | /* |
| 2663 | * CUSE servers compiled on 32bit broke on 64bit kernels because the |
| 2664 | * ABI was defined to be 'struct iovec' which is different on 32bit |
| 2665 | * and 64bit. Fortunately we can determine which structure the server |
| 2666 | * used from the size of the reply. |
| 2667 | */ |
| 2668 | static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, |
| 2669 | size_t transferred, unsigned count, |
| 2670 | bool is_compat) |
| 2671 | { |
| 2672 | #ifdef CONFIG_COMPAT |
| 2673 | if (count * sizeof(struct compat_iovec) == transferred) { |
| 2674 | struct compat_iovec *ciov = src; |
| 2675 | unsigned i; |
| 2676 | |
| 2677 | /* |
| 2678 | * With this interface a 32bit server cannot support |
| 2679 | * non-compat (i.e. ones coming from 64bit apps) ioctl |
| 2680 | * requests |
| 2681 | */ |
| 2682 | if (!is_compat) |
| 2683 | return -EINVAL; |
| 2684 | |
| 2685 | for (i = 0; i < count; i++) { |
| 2686 | dst[i].iov_base = compat_ptr(ciov[i].iov_base); |
| 2687 | dst[i].iov_len = ciov[i].iov_len; |
| 2688 | } |
| 2689 | return 0; |
| 2690 | } |
| 2691 | #endif |
| 2692 | |
| 2693 | if (count * sizeof(struct iovec) != transferred) |
| 2694 | return -EIO; |
| 2695 | |
| 2696 | memcpy(dst, src, transferred); |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
| 2700 | /* Make sure iov_length() won't overflow */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2701 | static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov, |
| 2702 | size_t count) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2703 | { |
| 2704 | size_t n; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2705 | u32 max = fc->max_pages << PAGE_SHIFT; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2706 | |
| 2707 | for (n = 0; n < count; n++, iov++) { |
| 2708 | if (iov->iov_len > (size_t) max) |
| 2709 | return -ENOMEM; |
| 2710 | max -= iov->iov_len; |
| 2711 | } |
| 2712 | return 0; |
| 2713 | } |
| 2714 | |
| 2715 | static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, |
| 2716 | void *src, size_t transferred, unsigned count, |
| 2717 | bool is_compat) |
| 2718 | { |
| 2719 | unsigned i; |
| 2720 | struct fuse_ioctl_iovec *fiov = src; |
| 2721 | |
| 2722 | if (fc->minor < 16) { |
| 2723 | return fuse_copy_ioctl_iovec_old(dst, src, transferred, |
| 2724 | count, is_compat); |
| 2725 | } |
| 2726 | |
| 2727 | if (count * sizeof(struct fuse_ioctl_iovec) != transferred) |
| 2728 | return -EIO; |
| 2729 | |
| 2730 | for (i = 0; i < count; i++) { |
| 2731 | /* Did the server supply an inappropriate value? */ |
| 2732 | if (fiov[i].base != (unsigned long) fiov[i].base || |
| 2733 | fiov[i].len != (unsigned long) fiov[i].len) |
| 2734 | return -EIO; |
| 2735 | |
| 2736 | dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; |
| 2737 | dst[i].iov_len = (size_t) fiov[i].len; |
| 2738 | |
| 2739 | #ifdef CONFIG_COMPAT |
| 2740 | if (is_compat && |
| 2741 | (ptr_to_compat(dst[i].iov_base) != fiov[i].base || |
| 2742 | (compat_size_t) dst[i].iov_len != fiov[i].len)) |
| 2743 | return -EIO; |
| 2744 | #endif |
| 2745 | } |
| 2746 | |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
| 2750 | |
| 2751 | /* |
| 2752 | * For ioctls, there is no generic way to determine how much memory |
| 2753 | * needs to be read and/or written. Furthermore, ioctls are allowed |
| 2754 | * to dereference the passed pointer, so the parameter requires deep |
| 2755 | * copying but FUSE has no idea whatsoever about what to copy in or |
| 2756 | * out. |
| 2757 | * |
| 2758 | * This is solved by allowing FUSE server to retry ioctl with |
| 2759 | * necessary in/out iovecs. Let's assume the ioctl implementation |
| 2760 | * needs to read in the following structure. |
| 2761 | * |
| 2762 | * struct a { |
| 2763 | * char *buf; |
| 2764 | * size_t buflen; |
| 2765 | * } |
| 2766 | * |
| 2767 | * On the first callout to FUSE server, inarg->in_size and |
| 2768 | * inarg->out_size will be NULL; then, the server completes the ioctl |
| 2769 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and |
| 2770 | * the actual iov array to |
| 2771 | * |
| 2772 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } |
| 2773 | * |
| 2774 | * which tells FUSE to copy in the requested area and retry the ioctl. |
| 2775 | * On the second round, the server has access to the structure and |
| 2776 | * from that it can tell what to look for next, so on the invocation, |
| 2777 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to |
| 2778 | * |
| 2779 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, |
| 2780 | * { .iov_base = a.buf, .iov_len = a.buflen } } |
| 2781 | * |
| 2782 | * FUSE will copy both struct a and the pointed buffer from the |
| 2783 | * process doing the ioctl and retry ioctl with both struct a and the |
| 2784 | * buffer. |
| 2785 | * |
| 2786 | * This time, FUSE server has everything it needs and completes ioctl |
| 2787 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. |
| 2788 | * |
| 2789 | * Copying data out works the same way. |
| 2790 | * |
| 2791 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel |
| 2792 | * automatically initializes in and out iovs by decoding @cmd with |
| 2793 | * _IOC_* macros and the server is not allowed to request RETRY. This |
| 2794 | * limits ioctl data transfers to well-formed ioctls and is the forced |
| 2795 | * behavior for all FUSE servers. |
| 2796 | */ |
| 2797 | long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, |
| 2798 | unsigned int flags) |
| 2799 | { |
| 2800 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2801 | struct fuse_mount *fm = ff->fm; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2802 | struct fuse_ioctl_in inarg = { |
| 2803 | .fh = ff->fh, |
| 2804 | .cmd = cmd, |
| 2805 | .arg = arg, |
| 2806 | .flags = flags |
| 2807 | }; |
| 2808 | struct fuse_ioctl_out outarg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2809 | struct iovec *iov_page = NULL; |
| 2810 | struct iovec *in_iov = NULL, *out_iov = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2811 | unsigned int in_iovs = 0, out_iovs = 0, max_pages; |
| 2812 | size_t in_size, out_size, c; |
| 2813 | ssize_t transferred; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2814 | int err, i; |
| 2815 | struct iov_iter ii; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2816 | struct fuse_args_pages ap = {}; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2817 | |
| 2818 | #if BITS_PER_LONG == 32 |
| 2819 | inarg.flags |= FUSE_IOCTL_32BIT; |
| 2820 | #else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2821 | if (flags & FUSE_IOCTL_COMPAT) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2822 | inarg.flags |= FUSE_IOCTL_32BIT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2823 | #ifdef CONFIG_X86_X32 |
| 2824 | if (in_x32_syscall()) |
| 2825 | inarg.flags |= FUSE_IOCTL_COMPAT_X32; |
| 2826 | #endif |
| 2827 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2828 | #endif |
| 2829 | |
| 2830 | /* assume all the iovs returned by client always fits in a page */ |
| 2831 | BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); |
| 2832 | |
| 2833 | err = -ENOMEM; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2834 | ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2835 | iov_page = (struct iovec *) __get_free_page(GFP_KERNEL); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2836 | if (!ap.pages || !iov_page) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2837 | goto out; |
| 2838 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2839 | fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2840 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2841 | /* |
| 2842 | * If restricted, initialize IO parameters as encoded in @cmd. |
| 2843 | * RETRY from server is not allowed. |
| 2844 | */ |
| 2845 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { |
| 2846 | struct iovec *iov = iov_page; |
| 2847 | |
| 2848 | iov->iov_base = (void __user *)arg; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2849 | |
| 2850 | switch (cmd) { |
| 2851 | case FS_IOC_GETFLAGS: |
| 2852 | case FS_IOC_SETFLAGS: |
| 2853 | iov->iov_len = sizeof(int); |
| 2854 | break; |
| 2855 | default: |
| 2856 | iov->iov_len = _IOC_SIZE(cmd); |
| 2857 | break; |
| 2858 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2859 | |
| 2860 | if (_IOC_DIR(cmd) & _IOC_WRITE) { |
| 2861 | in_iov = iov; |
| 2862 | in_iovs = 1; |
| 2863 | } |
| 2864 | |
| 2865 | if (_IOC_DIR(cmd) & _IOC_READ) { |
| 2866 | out_iov = iov; |
| 2867 | out_iovs = 1; |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | retry: |
| 2872 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); |
| 2873 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); |
| 2874 | |
| 2875 | /* |
| 2876 | * Out data can be used either for actual out data or iovs, |
| 2877 | * make sure there always is at least one page. |
| 2878 | */ |
| 2879 | out_size = max_t(size_t, out_size, PAGE_SIZE); |
| 2880 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); |
| 2881 | |
| 2882 | /* make sure there are enough buffer pages and init request with them */ |
| 2883 | err = -ENOMEM; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2884 | if (max_pages > fm->fc->max_pages) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2885 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2886 | while (ap.num_pages < max_pages) { |
| 2887 | ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 2888 | if (!ap.pages[ap.num_pages]) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2889 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2890 | ap.num_pages++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2893 | |
| 2894 | /* okay, let's send it to the client */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2895 | ap.args.opcode = FUSE_IOCTL; |
| 2896 | ap.args.nodeid = ff->nodeid; |
| 2897 | ap.args.in_numargs = 1; |
| 2898 | ap.args.in_args[0].size = sizeof(inarg); |
| 2899 | ap.args.in_args[0].value = &inarg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2900 | if (in_size) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2901 | ap.args.in_numargs++; |
| 2902 | ap.args.in_args[1].size = in_size; |
| 2903 | ap.args.in_pages = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2904 | |
| 2905 | err = -EFAULT; |
| 2906 | iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2907 | for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) { |
| 2908 | c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2909 | if (c != PAGE_SIZE && iov_iter_count(&ii)) |
| 2910 | goto out; |
| 2911 | } |
| 2912 | } |
| 2913 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2914 | ap.args.out_numargs = 2; |
| 2915 | ap.args.out_args[0].size = sizeof(outarg); |
| 2916 | ap.args.out_args[0].value = &outarg; |
| 2917 | ap.args.out_args[1].size = out_size; |
| 2918 | ap.args.out_pages = true; |
| 2919 | ap.args.out_argvar = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2920 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2921 | transferred = fuse_simple_request(fm, &ap.args); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2922 | err = transferred; |
| 2923 | if (transferred < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2924 | goto out; |
| 2925 | |
| 2926 | /* did it ask for retry? */ |
| 2927 | if (outarg.flags & FUSE_IOCTL_RETRY) { |
| 2928 | void *vaddr; |
| 2929 | |
| 2930 | /* no retry if in restricted mode */ |
| 2931 | err = -EIO; |
| 2932 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) |
| 2933 | goto out; |
| 2934 | |
| 2935 | in_iovs = outarg.in_iovs; |
| 2936 | out_iovs = outarg.out_iovs; |
| 2937 | |
| 2938 | /* |
| 2939 | * Make sure things are in boundary, separate checks |
| 2940 | * are to protect against overflow. |
| 2941 | */ |
| 2942 | err = -ENOMEM; |
| 2943 | if (in_iovs > FUSE_IOCTL_MAX_IOV || |
| 2944 | out_iovs > FUSE_IOCTL_MAX_IOV || |
| 2945 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) |
| 2946 | goto out; |
| 2947 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2948 | vaddr = kmap_atomic(ap.pages[0]); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2949 | err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2950 | transferred, in_iovs + out_iovs, |
| 2951 | (flags & FUSE_IOCTL_COMPAT) != 0); |
| 2952 | kunmap_atomic(vaddr); |
| 2953 | if (err) |
| 2954 | goto out; |
| 2955 | |
| 2956 | in_iov = iov_page; |
| 2957 | out_iov = in_iov + in_iovs; |
| 2958 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2959 | err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2960 | if (err) |
| 2961 | goto out; |
| 2962 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2963 | err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2964 | if (err) |
| 2965 | goto out; |
| 2966 | |
| 2967 | goto retry; |
| 2968 | } |
| 2969 | |
| 2970 | err = -EIO; |
| 2971 | if (transferred > inarg.out_size) |
| 2972 | goto out; |
| 2973 | |
| 2974 | err = -EFAULT; |
| 2975 | iov_iter_init(&ii, READ, out_iov, out_iovs, transferred); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2976 | for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) { |
| 2977 | c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2978 | if (c != PAGE_SIZE && iov_iter_count(&ii)) |
| 2979 | goto out; |
| 2980 | } |
| 2981 | err = 0; |
| 2982 | out: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2983 | free_page((unsigned long) iov_page); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2984 | while (ap.num_pages) |
| 2985 | __free_page(ap.pages[--ap.num_pages]); |
| 2986 | kfree(ap.pages); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2987 | |
| 2988 | return err ? err : outarg.result; |
| 2989 | } |
| 2990 | EXPORT_SYMBOL_GPL(fuse_do_ioctl); |
| 2991 | |
| 2992 | long fuse_ioctl_common(struct file *file, unsigned int cmd, |
| 2993 | unsigned long arg, unsigned int flags) |
| 2994 | { |
| 2995 | struct inode *inode = file_inode(file); |
| 2996 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 2997 | |
| 2998 | if (!fuse_allow_current_process(fc)) |
| 2999 | return -EACCES; |
| 3000 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3001 | if (fuse_is_bad(inode)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3002 | return -EIO; |
| 3003 | |
| 3004 | return fuse_do_ioctl(file, cmd, arg, flags); |
| 3005 | } |
| 3006 | |
| 3007 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, |
| 3008 | unsigned long arg) |
| 3009 | { |
| 3010 | return fuse_ioctl_common(file, cmd, arg, 0); |
| 3011 | } |
| 3012 | |
| 3013 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, |
| 3014 | unsigned long arg) |
| 3015 | { |
| 3016 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); |
| 3017 | } |
| 3018 | |
| 3019 | /* |
| 3020 | * All files which have been polled are linked to RB tree |
| 3021 | * fuse_conn->polled_files which is indexed by kh. Walk the tree and |
| 3022 | * find the matching one. |
| 3023 | */ |
| 3024 | static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, |
| 3025 | struct rb_node **parent_out) |
| 3026 | { |
| 3027 | struct rb_node **link = &fc->polled_files.rb_node; |
| 3028 | struct rb_node *last = NULL; |
| 3029 | |
| 3030 | while (*link) { |
| 3031 | struct fuse_file *ff; |
| 3032 | |
| 3033 | last = *link; |
| 3034 | ff = rb_entry(last, struct fuse_file, polled_node); |
| 3035 | |
| 3036 | if (kh < ff->kh) |
| 3037 | link = &last->rb_left; |
| 3038 | else if (kh > ff->kh) |
| 3039 | link = &last->rb_right; |
| 3040 | else |
| 3041 | return link; |
| 3042 | } |
| 3043 | |
| 3044 | if (parent_out) |
| 3045 | *parent_out = last; |
| 3046 | return link; |
| 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * The file is about to be polled. Make sure it's on the polled_files |
| 3051 | * RB tree. Note that files once added to the polled_files tree are |
| 3052 | * not removed before the file is released. This is because a file |
| 3053 | * polled once is likely to be polled again. |
| 3054 | */ |
| 3055 | static void fuse_register_polled_file(struct fuse_conn *fc, |
| 3056 | struct fuse_file *ff) |
| 3057 | { |
| 3058 | spin_lock(&fc->lock); |
| 3059 | if (RB_EMPTY_NODE(&ff->polled_node)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3060 | struct rb_node **link, *parent; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3061 | |
| 3062 | link = fuse_find_polled_node(fc, ff->kh, &parent); |
| 3063 | BUG_ON(*link); |
| 3064 | rb_link_node(&ff->polled_node, parent, link); |
| 3065 | rb_insert_color(&ff->polled_node, &fc->polled_files); |
| 3066 | } |
| 3067 | spin_unlock(&fc->lock); |
| 3068 | } |
| 3069 | |
| 3070 | __poll_t fuse_file_poll(struct file *file, poll_table *wait) |
| 3071 | { |
| 3072 | struct fuse_file *ff = file->private_data; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3073 | struct fuse_mount *fm = ff->fm; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3074 | struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; |
| 3075 | struct fuse_poll_out outarg; |
| 3076 | FUSE_ARGS(args); |
| 3077 | int err; |
| 3078 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3079 | if (fm->fc->no_poll) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3080 | return DEFAULT_POLLMASK; |
| 3081 | |
| 3082 | poll_wait(file, &ff->poll_wait, wait); |
| 3083 | inarg.events = mangle_poll(poll_requested_events(wait)); |
| 3084 | |
| 3085 | /* |
| 3086 | * Ask for notification iff there's someone waiting for it. |
| 3087 | * The client may ignore the flag and always notify. |
| 3088 | */ |
| 3089 | if (waitqueue_active(&ff->poll_wait)) { |
| 3090 | inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3091 | fuse_register_polled_file(fm->fc, ff); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3094 | args.opcode = FUSE_POLL; |
| 3095 | args.nodeid = ff->nodeid; |
| 3096 | args.in_numargs = 1; |
| 3097 | args.in_args[0].size = sizeof(inarg); |
| 3098 | args.in_args[0].value = &inarg; |
| 3099 | args.out_numargs = 1; |
| 3100 | args.out_args[0].size = sizeof(outarg); |
| 3101 | args.out_args[0].value = &outarg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3102 | err = fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3103 | |
| 3104 | if (!err) |
| 3105 | return demangle_poll(outarg.revents); |
| 3106 | if (err == -ENOSYS) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3107 | fm->fc->no_poll = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3108 | return DEFAULT_POLLMASK; |
| 3109 | } |
| 3110 | return EPOLLERR; |
| 3111 | } |
| 3112 | EXPORT_SYMBOL_GPL(fuse_file_poll); |
| 3113 | |
| 3114 | /* |
| 3115 | * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and |
| 3116 | * wakes up the poll waiters. |
| 3117 | */ |
| 3118 | int fuse_notify_poll_wakeup(struct fuse_conn *fc, |
| 3119 | struct fuse_notify_poll_wakeup_out *outarg) |
| 3120 | { |
| 3121 | u64 kh = outarg->kh; |
| 3122 | struct rb_node **link; |
| 3123 | |
| 3124 | spin_lock(&fc->lock); |
| 3125 | |
| 3126 | link = fuse_find_polled_node(fc, kh, NULL); |
| 3127 | if (*link) { |
| 3128 | struct fuse_file *ff; |
| 3129 | |
| 3130 | ff = rb_entry(*link, struct fuse_file, polled_node); |
| 3131 | wake_up_interruptible_sync(&ff->poll_wait); |
| 3132 | } |
| 3133 | |
| 3134 | spin_unlock(&fc->lock); |
| 3135 | return 0; |
| 3136 | } |
| 3137 | |
| 3138 | static void fuse_do_truncate(struct file *file) |
| 3139 | { |
| 3140 | struct inode *inode = file->f_mapping->host; |
| 3141 | struct iattr attr; |
| 3142 | |
| 3143 | attr.ia_valid = ATTR_SIZE; |
| 3144 | attr.ia_size = i_size_read(inode); |
| 3145 | |
| 3146 | attr.ia_file = file; |
| 3147 | attr.ia_valid |= ATTR_FILE; |
| 3148 | |
| 3149 | fuse_do_setattr(file_dentry(file), &attr, file); |
| 3150 | } |
| 3151 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3152 | static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3153 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3154 | return round_up(off, fc->max_pages << PAGE_SHIFT); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | static ssize_t |
| 3158 | fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
| 3159 | { |
| 3160 | DECLARE_COMPLETION_ONSTACK(wait); |
| 3161 | ssize_t ret = 0; |
| 3162 | struct file *file = iocb->ki_filp; |
| 3163 | struct fuse_file *ff = file->private_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3164 | loff_t pos = 0; |
| 3165 | struct inode *inode; |
| 3166 | loff_t i_size; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3167 | size_t count = iov_iter_count(iter), shortened = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3168 | loff_t offset = iocb->ki_pos; |
| 3169 | struct fuse_io_priv *io; |
| 3170 | |
| 3171 | pos = offset; |
| 3172 | inode = file->f_mapping->host; |
| 3173 | i_size = i_size_read(inode); |
| 3174 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3175 | if ((iov_iter_rw(iter) == READ) && (offset >= i_size)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3176 | return 0; |
| 3177 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3178 | io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL); |
| 3179 | if (!io) |
| 3180 | return -ENOMEM; |
| 3181 | spin_lock_init(&io->lock); |
| 3182 | kref_init(&io->refcnt); |
| 3183 | io->reqs = 1; |
| 3184 | io->bytes = -1; |
| 3185 | io->size = 0; |
| 3186 | io->offset = offset; |
| 3187 | io->write = (iov_iter_rw(iter) == WRITE); |
| 3188 | io->err = 0; |
| 3189 | /* |
| 3190 | * By default, we want to optimize all I/Os with async request |
| 3191 | * submission to the client filesystem if supported. |
| 3192 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3193 | io->async = ff->fm->fc->async_dio; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3194 | io->iocb = iocb; |
| 3195 | io->blocking = is_sync_kiocb(iocb); |
| 3196 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3197 | /* optimization for short read */ |
| 3198 | if (io->async && !io->write && offset + count > i_size) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3199 | iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset)); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3200 | shortened = count - iov_iter_count(iter); |
| 3201 | count -= shortened; |
| 3202 | } |
| 3203 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3204 | /* |
| 3205 | * We cannot asynchronously extend the size of a file. |
| 3206 | * In such case the aio will behave exactly like sync io. |
| 3207 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3208 | if ((offset + count > i_size) && io->write) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3209 | io->blocking = true; |
| 3210 | |
| 3211 | if (io->async && io->blocking) { |
| 3212 | /* |
| 3213 | * Additional reference to keep io around after |
| 3214 | * calling fuse_aio_complete() |
| 3215 | */ |
| 3216 | kref_get(&io->refcnt); |
| 3217 | io->done = &wait; |
| 3218 | } |
| 3219 | |
| 3220 | if (iov_iter_rw(iter) == WRITE) { |
| 3221 | ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE); |
| 3222 | fuse_invalidate_attr(inode); |
| 3223 | } else { |
| 3224 | ret = __fuse_direct_read(io, iter, &pos); |
| 3225 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3226 | iov_iter_reexpand(iter, iov_iter_count(iter) + shortened); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3227 | |
| 3228 | if (io->async) { |
| 3229 | bool blocking = io->blocking; |
| 3230 | |
| 3231 | fuse_aio_complete(io, ret < 0 ? ret : 0, -1); |
| 3232 | |
| 3233 | /* we have a non-extending, async request, so return */ |
| 3234 | if (!blocking) |
| 3235 | return -EIOCBQUEUED; |
| 3236 | |
| 3237 | wait_for_completion(&wait); |
| 3238 | ret = fuse_get_res_by_io(io); |
| 3239 | } |
| 3240 | |
| 3241 | kref_put(&io->refcnt, fuse_io_release); |
| 3242 | |
| 3243 | if (iov_iter_rw(iter) == WRITE) { |
| 3244 | if (ret > 0) |
| 3245 | fuse_write_update_size(inode, pos); |
| 3246 | else if (ret < 0 && offset + count > i_size) |
| 3247 | fuse_do_truncate(file); |
| 3248 | } |
| 3249 | |
| 3250 | return ret; |
| 3251 | } |
| 3252 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3253 | static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end) |
| 3254 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3255 | int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3256 | |
| 3257 | if (!err) |
| 3258 | fuse_sync_writes(inode); |
| 3259 | |
| 3260 | return err; |
| 3261 | } |
| 3262 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3263 | static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, |
| 3264 | loff_t length) |
| 3265 | { |
| 3266 | struct fuse_file *ff = file->private_data; |
| 3267 | struct inode *inode = file_inode(file); |
| 3268 | struct fuse_inode *fi = get_fuse_inode(inode); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3269 | struct fuse_mount *fm = ff->fm; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3270 | FUSE_ARGS(args); |
| 3271 | struct fuse_fallocate_in inarg = { |
| 3272 | .fh = ff->fh, |
| 3273 | .offset = offset, |
| 3274 | .length = length, |
| 3275 | .mode = mode |
| 3276 | }; |
| 3277 | int err; |
| 3278 | bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) || |
| 3279 | (mode & FALLOC_FL_PUNCH_HOLE); |
| 3280 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3281 | bool block_faults = FUSE_IS_DAX(inode) && lock_inode; |
| 3282 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3283 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 3284 | return -EOPNOTSUPP; |
| 3285 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3286 | if (fm->fc->no_fallocate) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3287 | return -EOPNOTSUPP; |
| 3288 | |
| 3289 | if (lock_inode) { |
| 3290 | inode_lock(inode); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3291 | if (block_faults) { |
| 3292 | down_write(&fi->i_mmap_sem); |
| 3293 | err = fuse_dax_break_layouts(inode, 0, 0); |
| 3294 | if (err) |
| 3295 | goto out; |
| 3296 | } |
| 3297 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3298 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
| 3299 | loff_t endbyte = offset + length - 1; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3300 | |
| 3301 | err = fuse_writeback_range(inode, offset, endbyte); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3302 | if (err) |
| 3303 | goto out; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3307 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 3308 | offset + length > i_size_read(inode)) { |
| 3309 | err = inode_newsize_ok(inode, offset + length); |
| 3310 | if (err) |
| 3311 | goto out; |
| 3312 | } |
| 3313 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3314 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
| 3315 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
| 3316 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3317 | args.opcode = FUSE_FALLOCATE; |
| 3318 | args.nodeid = ff->nodeid; |
| 3319 | args.in_numargs = 1; |
| 3320 | args.in_args[0].size = sizeof(inarg); |
| 3321 | args.in_args[0].value = &inarg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3322 | err = fuse_simple_request(fm, &args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3323 | if (err == -ENOSYS) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3324 | fm->fc->no_fallocate = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3325 | err = -EOPNOTSUPP; |
| 3326 | } |
| 3327 | if (err) |
| 3328 | goto out; |
| 3329 | |
| 3330 | /* we could have extended the file */ |
| 3331 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
| 3332 | bool changed = fuse_write_update_size(inode, offset + length); |
| 3333 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3334 | if (changed && fm->fc->writeback_cache) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3335 | file_update_time(file); |
| 3336 | } |
| 3337 | |
| 3338 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 3339 | truncate_pagecache_range(inode, offset, offset + length - 1); |
| 3340 | |
| 3341 | fuse_invalidate_attr(inode); |
| 3342 | |
| 3343 | out: |
| 3344 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
| 3345 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
| 3346 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3347 | if (block_faults) |
| 3348 | up_write(&fi->i_mmap_sem); |
| 3349 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3350 | if (lock_inode) |
| 3351 | inode_unlock(inode); |
| 3352 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3353 | fuse_flush_time_update(inode); |
| 3354 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3355 | return err; |
| 3356 | } |
| 3357 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3358 | static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in, |
| 3359 | struct file *file_out, loff_t pos_out, |
| 3360 | size_t len, unsigned int flags) |
| 3361 | { |
| 3362 | struct fuse_file *ff_in = file_in->private_data; |
| 3363 | struct fuse_file *ff_out = file_out->private_data; |
| 3364 | struct inode *inode_in = file_inode(file_in); |
| 3365 | struct inode *inode_out = file_inode(file_out); |
| 3366 | struct fuse_inode *fi_out = get_fuse_inode(inode_out); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3367 | struct fuse_mount *fm = ff_in->fm; |
| 3368 | struct fuse_conn *fc = fm->fc; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3369 | FUSE_ARGS(args); |
| 3370 | struct fuse_copy_file_range_in inarg = { |
| 3371 | .fh_in = ff_in->fh, |
| 3372 | .off_in = pos_in, |
| 3373 | .nodeid_out = ff_out->nodeid, |
| 3374 | .fh_out = ff_out->fh, |
| 3375 | .off_out = pos_out, |
| 3376 | .len = len, |
| 3377 | .flags = flags |
| 3378 | }; |
| 3379 | struct fuse_write_out outarg; |
| 3380 | ssize_t err; |
| 3381 | /* mark unstable when write-back is not used, and file_out gets |
| 3382 | * extended */ |
| 3383 | bool is_unstable = (!fc->writeback_cache) && |
| 3384 | ((pos_out + len) > inode_out->i_size); |
| 3385 | |
| 3386 | if (fc->no_copy_file_range) |
| 3387 | return -EOPNOTSUPP; |
| 3388 | |
| 3389 | if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) |
| 3390 | return -EXDEV; |
| 3391 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3392 | inode_lock(inode_in); |
| 3393 | err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1); |
| 3394 | inode_unlock(inode_in); |
| 3395 | if (err) |
| 3396 | return err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3397 | |
| 3398 | inode_lock(inode_out); |
| 3399 | |
| 3400 | err = file_modified(file_out); |
| 3401 | if (err) |
| 3402 | goto out; |
| 3403 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3404 | /* |
| 3405 | * Write out dirty pages in the destination file before sending the COPY |
| 3406 | * request to userspace. After the request is completed, truncate off |
| 3407 | * pages (including partial ones) from the cache that have been copied, |
| 3408 | * since these contain stale data at that point. |
| 3409 | * |
| 3410 | * This should be mostly correct, but if the COPY writes to partial |
| 3411 | * pages (at the start or end) and the parts not covered by the COPY are |
| 3412 | * written through a memory map after calling fuse_writeback_range(), |
| 3413 | * then these partial page modifications will be lost on truncation. |
| 3414 | * |
| 3415 | * It is unlikely that someone would rely on such mixed style |
| 3416 | * modifications. Yet this does give less guarantees than if the |
| 3417 | * copying was performed with write(2). |
| 3418 | * |
| 3419 | * To fix this a i_mmap_sem style lock could be used to prevent new |
| 3420 | * faults while the copy is ongoing. |
| 3421 | */ |
| 3422 | err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1); |
| 3423 | if (err) |
| 3424 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3425 | |
| 3426 | if (is_unstable) |
| 3427 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); |
| 3428 | |
| 3429 | args.opcode = FUSE_COPY_FILE_RANGE; |
| 3430 | args.nodeid = ff_in->nodeid; |
| 3431 | args.in_numargs = 1; |
| 3432 | args.in_args[0].size = sizeof(inarg); |
| 3433 | args.in_args[0].value = &inarg; |
| 3434 | args.out_numargs = 1; |
| 3435 | args.out_args[0].size = sizeof(outarg); |
| 3436 | args.out_args[0].value = &outarg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3437 | err = fuse_simple_request(fm, &args); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3438 | if (err == -ENOSYS) { |
| 3439 | fc->no_copy_file_range = 1; |
| 3440 | err = -EOPNOTSUPP; |
| 3441 | } |
| 3442 | if (err) |
| 3443 | goto out; |
| 3444 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3445 | truncate_inode_pages_range(inode_out->i_mapping, |
| 3446 | ALIGN_DOWN(pos_out, PAGE_SIZE), |
| 3447 | ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1); |
| 3448 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3449 | if (fc->writeback_cache) { |
| 3450 | fuse_write_update_size(inode_out, pos_out + outarg.size); |
| 3451 | file_update_time(file_out); |
| 3452 | } |
| 3453 | |
| 3454 | fuse_invalidate_attr(inode_out); |
| 3455 | |
| 3456 | err = outarg.size; |
| 3457 | out: |
| 3458 | if (is_unstable) |
| 3459 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); |
| 3460 | |
| 3461 | inode_unlock(inode_out); |
| 3462 | file_accessed(file_in); |
| 3463 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3464 | fuse_flush_time_update(inode_out); |
| 3465 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3466 | return err; |
| 3467 | } |
| 3468 | |
| 3469 | static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off, |
| 3470 | struct file *dst_file, loff_t dst_off, |
| 3471 | size_t len, unsigned int flags) |
| 3472 | { |
| 3473 | ssize_t ret; |
| 3474 | |
| 3475 | ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off, |
| 3476 | len, flags); |
| 3477 | |
| 3478 | if (ret == -EOPNOTSUPP || ret == -EXDEV) |
| 3479 | ret = generic_copy_file_range(src_file, src_off, dst_file, |
| 3480 | dst_off, len, flags); |
| 3481 | return ret; |
| 3482 | } |
| 3483 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3484 | static const struct file_operations fuse_file_operations = { |
| 3485 | .llseek = fuse_file_llseek, |
| 3486 | .read_iter = fuse_file_read_iter, |
| 3487 | .write_iter = fuse_file_write_iter, |
| 3488 | .mmap = fuse_file_mmap, |
| 3489 | .open = fuse_open, |
| 3490 | .flush = fuse_flush, |
| 3491 | .release = fuse_release, |
| 3492 | .fsync = fuse_fsync, |
| 3493 | .lock = fuse_file_lock, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3494 | .get_unmapped_area = thp_get_unmapped_area, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3495 | .flock = fuse_file_flock, |
| 3496 | .splice_read = generic_file_splice_read, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3497 | .splice_write = iter_file_splice_write, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3498 | .unlocked_ioctl = fuse_file_ioctl, |
| 3499 | .compat_ioctl = fuse_file_compat_ioctl, |
| 3500 | .poll = fuse_file_poll, |
| 3501 | .fallocate = fuse_file_fallocate, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3502 | .copy_file_range = fuse_copy_file_range, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3503 | }; |
| 3504 | |
| 3505 | static const struct address_space_operations fuse_file_aops = { |
| 3506 | .readpage = fuse_readpage, |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3507 | .readahead = fuse_readahead, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3508 | .writepage = fuse_writepage, |
| 3509 | .writepages = fuse_writepages, |
| 3510 | .launder_page = fuse_launder_page, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3511 | .set_page_dirty = __set_page_dirty_nobuffers, |
| 3512 | .bmap = fuse_bmap, |
| 3513 | .direct_IO = fuse_direct_IO, |
| 3514 | .write_begin = fuse_write_begin, |
| 3515 | .write_end = fuse_write_end, |
| 3516 | }; |
| 3517 | |
| 3518 | void fuse_init_file_inode(struct inode *inode) |
| 3519 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3520 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 3521 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3522 | inode->i_fop = &fuse_file_operations; |
| 3523 | inode->i_data.a_ops = &fuse_file_aops; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3524 | |
| 3525 | INIT_LIST_HEAD(&fi->write_files); |
| 3526 | INIT_LIST_HEAD(&fi->queued_writes); |
| 3527 | fi->writectr = 0; |
| 3528 | init_waitqueue_head(&fi->page_waitq); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3529 | fi->writepages = RB_ROOT; |
| 3530 | |
| 3531 | if (IS_ENABLED(CONFIG_FUSE_DAX)) |
| 3532 | fuse_dax_inode_init(inode); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3533 | } |