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