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