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