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