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