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