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