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