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