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