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