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