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