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