blob: 7155f49b2ef6c122f53dd2655996d561bd327679 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
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 Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090015#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010016#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020017#include <linux/swap.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070018
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080019static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070020
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020021static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
22 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070023{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070024 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080025 struct fuse_req *req;
26 int err;
27
Miklos Szeredice1d5a42006-04-10 22:54:58 -070028 req = fuse_get_req(fc);
29 if (IS_ERR(req))
30 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080031
32 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070033 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
34 if (!fc->atomic_o_trunc)
35 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020036 req->in.h.opcode = opcode;
37 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080038 req->in.numargs = 1;
39 req->in.args[0].size = sizeof(inarg);
40 req->in.args[0].value = &inarg;
41 req->out.numargs = 1;
42 req->out.args[0].size = sizeof(*outargp);
43 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010044 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080045 err = req->out.h.error;
46 fuse_put_request(fc, req);
47
48 return err;
49}
50
Tejun Heoacf99432008-11-26 12:03:55 +010051struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080052{
53 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090054
Miklos Szeredifd72faa2005-11-07 00:59:51 -080055 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090056 if (unlikely(!ff))
57 return NULL;
58
Miklos Szeredida5e4712009-04-28 16:56:36 +020059 ff->fc = fc;
Tejun Heo6b2db282009-04-14 10:54:49 +090060 ff->reserved_req = fuse_request_alloc();
61 if (unlikely(!ff->reserved_req)) {
62 kfree(ff);
63 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080064 }
Tejun Heo6b2db282009-04-14 10:54:49 +090065
66 INIT_LIST_HEAD(&ff->write_entry);
67 atomic_set(&ff->count, 0);
68 RB_CLEAR_NODE(&ff->polled_node);
69 init_waitqueue_head(&ff->poll_wait);
70
71 spin_lock(&fc->lock);
72 ff->kh = ++fc->khctr;
73 spin_unlock(&fc->lock);
74
Miklos Szeredifd72faa2005-11-07 00:59:51 -080075 return ff;
76}
77
78void fuse_file_free(struct fuse_file *ff)
79{
Miklos Szeredi33649c92006-06-25 05:48:52 -070080 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080081 kfree(ff);
82}
83
Miklos Szeredic7b71432009-04-28 16:56:37 +020084struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070085{
86 atomic_inc(&ff->count);
87 return ff;
88}
89
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010090static void fuse_release_async(struct work_struct *work)
Miklos Szeredi819c4b32007-10-16 23:31:04 -070091{
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010092 struct fuse_req *req;
93 struct fuse_conn *fc;
94 struct path path;
95
96 req = container_of(work, struct fuse_req, misc.release.work);
97 path = req->misc.release.path;
98 fc = get_fuse_conn(path.dentry->d_inode);
99
100 fuse_put_request(fc, req);
101 path_put(&path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -0700102}
103
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100104static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
105{
106 if (fc->destroy_req) {
107 /*
108 * If this is a fuseblk mount, then it's possible that
109 * releasing the path will result in releasing the
110 * super block and sending the DESTROY request. If
111 * the server is single threaded, this would hang.
112 * For this reason do the path_put() in a separate
113 * thread.
114 */
115 atomic_inc(&req->count);
116 INIT_WORK(&req->misc.release.work, fuse_release_async);
117 schedule_work(&req->misc.release.work);
118 } else {
119 path_put(&req->misc.release.path);
120 }
121}
122
123static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700124{
125 if (atomic_dec_and_test(&ff->count)) {
126 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200127
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100128 if (sync) {
129 fuse_request_send(ff->fc, req);
130 path_put(&req->misc.release.path);
131 fuse_put_request(ff->fc, req);
132 } else {
133 req->end = fuse_release_end;
134 fuse_request_send_background(ff->fc, req);
135 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700136 kfree(ff);
137 }
138}
139
Tejun Heo08cbf542009-04-14 10:54:53 +0900140int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
141 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200142{
143 struct fuse_open_out outarg;
144 struct fuse_file *ff;
145 int err;
146 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
147
148 ff = fuse_file_alloc(fc);
149 if (!ff)
150 return -ENOMEM;
151
152 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
153 if (err) {
154 fuse_file_free(ff);
155 return err;
156 }
157
158 if (isdir)
159 outarg.open_flags &= ~FOPEN_DIRECT_IO;
160
161 ff->fh = outarg.fh;
162 ff->nodeid = nodeid;
163 ff->open_flags = outarg.open_flags;
164 file->private_data = fuse_file_get(ff);
165
166 return 0;
167}
Tejun Heo08cbf542009-04-14 10:54:53 +0900168EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200169
Miklos Szeredic7b71432009-04-28 16:56:37 +0200170void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800171{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200172 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800173 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200174
175 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800176 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200177 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700178 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200179 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200180 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800181 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
182 struct fuse_inode *fi = get_fuse_inode(inode);
183
184 spin_lock(&fc->lock);
185 fi->attr_version = ++fc->attr_version;
186 i_size_write(inode, 0);
187 spin_unlock(&fc->lock);
188 fuse_invalidate_attr(inode);
189 }
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800190}
191
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200192int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800193{
Tejun Heoacf99432008-11-26 12:03:55 +0100194 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700195 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700196
Miklos Szeredidd190d02005-09-30 11:59:02 -0700197 /* VFS checks this, but only _after_ ->open() */
198 if (file->f_flags & O_DIRECT)
199 return -EINVAL;
200
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700201 err = generic_file_open(inode, file);
202 if (err)
203 return err;
204
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200205 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800206 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200207 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700208
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200209 fuse_finish_open(inode, file);
210
211 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700212}
213
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200214static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800215{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200216 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700217 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800218 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700219
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200220 spin_lock(&fc->lock);
221 list_del(&ff->write_entry);
222 if (!RB_EMPTY_NODE(&ff->polled_node))
223 rb_erase(&ff->polled_node, &fc->polled_files);
224 spin_unlock(&fc->lock);
225
Bryan Green357ccf22011-03-01 16:43:52 -0800226 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200227
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700228 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800229 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700230 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200231 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700232 req->in.numargs = 1;
233 req->in.args[0].size = sizeof(struct fuse_release_in);
234 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800235}
236
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200237void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800238{
Tejun Heo6b2db282009-04-14 10:54:49 +0900239 struct fuse_file *ff;
240 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700241
Tejun Heo6b2db282009-04-14 10:54:49 +0900242 ff = file->private_data;
243 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200244 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700245
Tejun Heo6b2db282009-04-14 10:54:49 +0900246 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200247 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100248
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200249 if (ff->flock) {
250 struct fuse_release_in *inarg = &req->misc.release.in;
251 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
252 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
253 (fl_owner_t) file);
254 }
Tejun Heo6b2db282009-04-14 10:54:49 +0900255 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200256 path_get(&file->f_path);
257 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700258
Tejun Heo6b2db282009-04-14 10:54:49 +0900259 /*
260 * Normally this will send the RELEASE request, however if
261 * some asynchronous READ or WRITE requests are outstanding,
262 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100263 *
264 * Make the release synchronous if this is a fuseblk mount,
265 * synchronous RELEASE is allowed (and desirable) in this case
266 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900267 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100268 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700269}
270
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700271static int fuse_open(struct inode *inode, struct file *file)
272{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200273 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700274}
275
276static int fuse_release(struct inode *inode, struct file *file)
277{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200278 fuse_release_common(file, FUSE_RELEASE);
279
280 /* return value is ignored by VFS */
281 return 0;
282}
283
284void fuse_sync_release(struct fuse_file *ff, int flags)
285{
286 WARN_ON(atomic_read(&ff->count) > 1);
287 fuse_prepare_release(ff, flags, FUSE_RELEASE);
288 ff->reserved_req->force = 1;
289 fuse_request_send(ff->fc, ff->reserved_req);
290 fuse_put_request(ff->fc, ff->reserved_req);
291 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700292}
Tejun Heo08cbf542009-04-14 10:54:53 +0900293EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700294
Miklos Szeredi71421252006-06-25 05:48:52 -0700295/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700296 * Scramble the ID space with XTEA, so that the value of the files_struct
297 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700298 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700299u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700300{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700301 u32 *k = fc->scramble_key;
302 u64 v = (unsigned long) id;
303 u32 v0 = v;
304 u32 v1 = v >> 32;
305 u32 sum = 0;
306 int i;
307
308 for (i = 0; i < 32; i++) {
309 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
310 sum += 0x9E3779B9;
311 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
312 }
313
314 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700315}
316
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700317/*
318 * Check if page is under writeback
319 *
320 * This is currently done by walking the list of writepage requests
321 * for the inode, which can be pretty inefficient.
322 */
323static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
324{
325 struct fuse_conn *fc = get_fuse_conn(inode);
326 struct fuse_inode *fi = get_fuse_inode(inode);
327 struct fuse_req *req;
328 bool found = false;
329
330 spin_lock(&fc->lock);
331 list_for_each_entry(req, &fi->writepages, writepages_entry) {
332 pgoff_t curr_index;
333
334 BUG_ON(req->inode != inode);
335 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
336 if (curr_index == index) {
337 found = true;
338 break;
339 }
340 }
341 spin_unlock(&fc->lock);
342
343 return found;
344}
345
346/*
347 * Wait for page writeback to be completed.
348 *
349 * Since fuse doesn't rely on the VM writeback tracking, this has to
350 * use some other means.
351 */
352static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
353{
354 struct fuse_inode *fi = get_fuse_inode(inode);
355
356 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
357 return 0;
358}
359
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700360static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700361{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800362 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700363 struct fuse_conn *fc = get_fuse_conn(inode);
364 struct fuse_file *ff = file->private_data;
365 struct fuse_req *req;
366 struct fuse_flush_in inarg;
367 int err;
368
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800369 if (is_bad_inode(inode))
370 return -EIO;
371
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700372 if (fc->no_flush)
373 return 0;
374
Miklos Szeredi33649c92006-06-25 05:48:52 -0700375 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700376 memset(&inarg, 0, sizeof(inarg));
377 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700378 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700379 req->in.h.opcode = FUSE_FLUSH;
380 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700381 req->in.numargs = 1;
382 req->in.args[0].size = sizeof(inarg);
383 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700384 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100385 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700386 err = req->out.h.error;
387 fuse_put_request(fc, req);
388 if (err == -ENOSYS) {
389 fc->no_flush = 1;
390 err = 0;
391 }
392 return err;
393}
394
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700395/*
396 * Wait for all pending writepages on the inode to finish.
397 *
398 * This is currently done by blocking further writes with FUSE_NOWRITE
399 * and waiting for all sent writes to complete.
400 *
401 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
402 * could conflict with truncation.
403 */
404static void fuse_sync_writes(struct inode *inode)
405{
406 fuse_set_nowrite(inode);
407 fuse_release_nowrite(inode);
408}
409
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200410int fuse_fsync_common(struct file *file, int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700411{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200412 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700413 struct fuse_conn *fc = get_fuse_conn(inode);
414 struct fuse_file *ff = file->private_data;
415 struct fuse_req *req;
416 struct fuse_fsync_in inarg;
417 int err;
418
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800419 if (is_bad_inode(inode))
420 return -EIO;
421
Miklos Szeredi82547982005-09-09 13:10:38 -0700422 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423 return 0;
424
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700425 /*
426 * Start writeback against all dirty pages of the inode, then
427 * wait for all outstanding writes, before sending the FSYNC
428 * request.
429 */
430 err = write_inode_now(inode, 0);
431 if (err)
432 return err;
433
434 fuse_sync_writes(inode);
435
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700436 req = fuse_get_req(fc);
437 if (IS_ERR(req))
438 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700439
440 memset(&inarg, 0, sizeof(inarg));
441 inarg.fh = ff->fh;
442 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700443 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700445 req->in.numargs = 1;
446 req->in.args[0].size = sizeof(inarg);
447 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100448 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 err = req->out.h.error;
450 fuse_put_request(fc, req);
451 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700452 if (isdir)
453 fc->no_fsyncdir = 1;
454 else
455 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700456 err = 0;
457 }
458 return err;
459}
460
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200461static int fuse_fsync(struct file *file, int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700462{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200463 return fuse_fsync_common(file, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700464}
465
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200466void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
467 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700468{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700469 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800470 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700471
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800472 inarg->fh = ff->fh;
473 inarg->offset = pos;
474 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800475 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800476 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200477 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700478 req->in.numargs = 1;
479 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800480 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700481 req->out.argvar = 1;
482 req->out.numargs = 1;
483 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700484}
485
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800486static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200487 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700488{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200489 struct fuse_file *ff = file->private_data;
490 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700491
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200492 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700493 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700494 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700495
496 inarg->read_flags |= FUSE_READ_LOCKOWNER;
497 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
498 }
Tejun Heob93f8582008-11-26 12:03:55 +0100499 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800500 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700501}
502
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700503static void fuse_read_update_size(struct inode *inode, loff_t size,
504 u64 attr_ver)
505{
506 struct fuse_conn *fc = get_fuse_conn(inode);
507 struct fuse_inode *fi = get_fuse_inode(inode);
508
509 spin_lock(&fc->lock);
510 if (attr_ver == fi->attr_version && size < inode->i_size) {
511 fi->attr_version = ++fc->attr_version;
512 i_size_write(inode, size);
513 }
514 spin_unlock(&fc->lock);
515}
516
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700517static int fuse_readpage(struct file *file, struct page *page)
518{
519 struct inode *inode = page->mapping->host;
520 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800521 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700522 size_t num_read;
523 loff_t pos = page_offset(page);
524 size_t count = PAGE_CACHE_SIZE;
525 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800526 int err;
527
528 err = -EIO;
529 if (is_bad_inode(inode))
530 goto out;
531
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700532 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300533 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700534 * page-cache page, so make sure we read a properly synced
535 * page.
536 */
537 fuse_wait_on_page_writeback(inode, page->index);
538
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700539 req = fuse_get_req(fc);
540 err = PTR_ERR(req);
541 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700542 goto out;
543
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700544 attr_ver = fuse_get_attr_version(fc);
545
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700546 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200547 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700548 req->num_pages = 1;
549 req->pages[0] = page;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200550 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700551 err = req->out.h.error;
552 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700553
554 if (!err) {
555 /*
556 * Short read means EOF. If file size is larger, truncate it
557 */
558 if (num_read < count)
559 fuse_read_update_size(inode, pos + num_read, attr_ver);
560
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700561 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700562 }
563
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700564 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700565 out:
566 unlock_page(page);
567 return err;
568}
569
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800570static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700571{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800572 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700573 size_t count = req->misc.read.in.size;
574 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200575 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800576
Miklos Szeredice534fb2010-05-25 15:06:07 +0200577 for (i = 0; mapping == NULL && i < req->num_pages; i++)
578 mapping = req->pages[i]->mapping;
579
580 if (mapping) {
581 struct inode *inode = mapping->host;
582
583 /*
584 * Short read means EOF. If file size is larger, truncate it
585 */
586 if (!req->out.h.error && num_read < count) {
587 loff_t pos;
588
589 pos = page_offset(req->pages[0]) + num_read;
590 fuse_read_update_size(inode, pos,
591 req->misc.read.attr_ver);
592 }
593 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700594 }
595
Miklos Szeredidb50b962005-09-09 13:10:33 -0700596 for (i = 0; i < req->num_pages; i++) {
597 struct page *page = req->pages[i];
598 if (!req->out.h.error)
599 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800600 else
601 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700602 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200603 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700604 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700605 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100606 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800607}
608
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200609static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800610{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200611 struct fuse_file *ff = file->private_data;
612 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800613 loff_t pos = page_offset(req->pages[0]);
614 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200615
616 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800617 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200618 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200619 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700620 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800621 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700622 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800623 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100624 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800625 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100626 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800627 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100628 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800629 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700630}
631
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700632struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700633 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800634 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700635 struct inode *inode;
636};
637
638static int fuse_readpages_fill(void *_data, struct page *page)
639{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700640 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700641 struct fuse_req *req = data->req;
642 struct inode *inode = data->inode;
643 struct fuse_conn *fc = get_fuse_conn(inode);
644
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700645 fuse_wait_on_page_writeback(inode, page->index);
646
Miklos Szeredidb50b962005-09-09 13:10:33 -0700647 if (req->num_pages &&
648 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
649 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
650 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200651 fuse_send_readpages(req, data->file);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700652 data->req = req = fuse_get_req(fc);
653 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700654 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700655 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700656 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700657 }
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200658 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700659 req->pages[req->num_pages] = page;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100660 req->num_pages++;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700661 return 0;
662}
663
664static int fuse_readpages(struct file *file, struct address_space *mapping,
665 struct list_head *pages, unsigned nr_pages)
666{
667 struct inode *inode = mapping->host;
668 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700669 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700670 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800671
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700672 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800673 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800674 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800675
Miklos Szeredia6643092007-11-28 16:22:00 -0800676 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700677 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700678 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700679 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700680 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800681 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700682
683 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700684 if (!err) {
685 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200686 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700687 else
688 fuse_put_request(fc, data.req);
689 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800690out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700691 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700692}
693
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800694static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
695 unsigned long nr_segs, loff_t pos)
696{
697 struct inode *inode = iocb->ki_filp->f_mapping->host;
698
699 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
700 int err;
701 /*
702 * If trying to read past EOF, make sure the i_size
703 * attribute is up-to-date.
704 */
705 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
706 if (err)
707 return err;
708 }
709
710 return generic_file_aio_read(iocb, iov, nr_segs, pos);
711}
712
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200713static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200714 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700715{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700716 struct fuse_write_in *inarg = &req->misc.write.in;
717 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700718
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700719 inarg->fh = ff->fh;
720 inarg->offset = pos;
721 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700722 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200723 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700724 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200725 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700726 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
727 else
728 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700729 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700730 req->in.args[1].size = count;
731 req->out.numargs = 1;
732 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700733 req->out.args[0].value = outarg;
734}
735
736static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200737 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700738{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200739 struct fuse_file *ff = file->private_data;
740 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200741 struct fuse_write_in *inarg = &req->misc.write.in;
742
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200743 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200744 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700745 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700746 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
747 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
748 }
Tejun Heob93f8582008-11-26 12:03:55 +0100749 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700750 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700751}
752
Miklos Szeredia1d75f22010-07-12 14:41:40 +0200753void fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700754{
755 struct fuse_conn *fc = get_fuse_conn(inode);
756 struct fuse_inode *fi = get_fuse_inode(inode);
757
758 spin_lock(&fc->lock);
759 fi->attr_version = ++fc->attr_version;
760 if (pos > inode->i_size)
761 i_size_write(inode, pos);
762 spin_unlock(&fc->lock);
763}
764
Nick Pigginea9b9902008-04-30 00:54:42 -0700765static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
766 struct inode *inode, loff_t pos,
767 size_t count)
768{
769 size_t res;
770 unsigned offset;
771 unsigned i;
772
773 for (i = 0; i < req->num_pages; i++)
774 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
775
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200776 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700777
778 offset = req->page_offset;
779 count = res;
780 for (i = 0; i < req->num_pages; i++) {
781 struct page *page = req->pages[i];
782
783 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
784 SetPageUptodate(page);
785
786 if (count > PAGE_CACHE_SIZE - offset)
787 count -= PAGE_CACHE_SIZE - offset;
788 else
789 count = 0;
790 offset = 0;
791
792 unlock_page(page);
793 page_cache_release(page);
794 }
795
796 return res;
797}
798
799static ssize_t fuse_fill_write_pages(struct fuse_req *req,
800 struct address_space *mapping,
801 struct iov_iter *ii, loff_t pos)
802{
803 struct fuse_conn *fc = get_fuse_conn(mapping->host);
804 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
805 size_t count = 0;
806 int err;
807
Miklos Szeredif4975c62009-04-02 14:25:34 +0200808 req->in.argpages = 1;
Nick Pigginea9b9902008-04-30 00:54:42 -0700809 req->page_offset = offset;
810
811 do {
812 size_t tmp;
813 struct page *page;
814 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
815 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
816 iov_iter_count(ii));
817
818 bytes = min_t(size_t, bytes, fc->max_write - count);
819
820 again:
821 err = -EFAULT;
822 if (iov_iter_fault_in_readable(ii, bytes))
823 break;
824
825 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800826 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700827 if (!page)
828 break;
829
anfei zhou931e80e2010-02-02 13:44:02 -0800830 if (mapping_writably_mapped(mapping))
831 flush_dcache_page(page);
832
Nick Pigginea9b9902008-04-30 00:54:42 -0700833 pagefault_disable();
834 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
835 pagefault_enable();
836 flush_dcache_page(page);
837
Johannes Weiner478e0842011-07-25 22:35:35 +0200838 mark_page_accessed(page);
839
Nick Pigginea9b9902008-04-30 00:54:42 -0700840 if (!tmp) {
841 unlock_page(page);
842 page_cache_release(page);
843 bytes = min(bytes, iov_iter_single_seg_count(ii));
844 goto again;
845 }
846
847 err = 0;
848 req->pages[req->num_pages] = page;
849 req->num_pages++;
850
851 iov_iter_advance(ii, tmp);
852 count += tmp;
853 pos += tmp;
854 offset += tmp;
855 if (offset == PAGE_CACHE_SIZE)
856 offset = 0;
857
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700858 if (!fc->big_writes)
859 break;
Nick Pigginea9b9902008-04-30 00:54:42 -0700860 } while (iov_iter_count(ii) && count < fc->max_write &&
861 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
862
863 return count > 0 ? count : err;
864}
865
866static ssize_t fuse_perform_write(struct file *file,
867 struct address_space *mapping,
868 struct iov_iter *ii, loff_t pos)
869{
870 struct inode *inode = mapping->host;
871 struct fuse_conn *fc = get_fuse_conn(inode);
872 int err = 0;
873 ssize_t res = 0;
874
875 if (is_bad_inode(inode))
876 return -EIO;
877
878 do {
879 struct fuse_req *req;
880 ssize_t count;
881
882 req = fuse_get_req(fc);
883 if (IS_ERR(req)) {
884 err = PTR_ERR(req);
885 break;
886 }
887
888 count = fuse_fill_write_pages(req, mapping, ii, pos);
889 if (count <= 0) {
890 err = count;
891 } else {
892 size_t num_written;
893
894 num_written = fuse_send_write_pages(req, file, inode,
895 pos, count);
896 err = req->out.h.error;
897 if (!err) {
898 res += num_written;
899 pos += num_written;
900
901 /* break out of the loop on short write */
902 if (num_written != count)
903 err = -EIO;
904 }
905 }
906 fuse_put_request(fc, req);
907 } while (!err && iov_iter_count(ii));
908
909 if (res > 0)
910 fuse_write_update_size(inode, pos);
911
912 fuse_invalidate_attr(inode);
913
914 return res > 0 ? res : err;
915}
916
917static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
918 unsigned long nr_segs, loff_t pos)
919{
920 struct file *file = iocb->ki_filp;
921 struct address_space *mapping = file->f_mapping;
922 size_t count = 0;
923 ssize_t written = 0;
924 struct inode *inode = mapping->host;
925 ssize_t err;
926 struct iov_iter i;
927
928 WARN_ON(iocb->ki_pos != pos);
929
930 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
931 if (err)
932 return err;
933
934 mutex_lock(&inode->i_mutex);
935 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
936
937 /* We can write back this queue in page reclaim */
938 current->backing_dev_info = mapping->backing_dev_info;
939
940 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
941 if (err)
942 goto out;
943
944 if (count == 0)
945 goto out;
946
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200947 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -0700948 if (err)
949 goto out;
950
951 file_update_time(file);
952
953 iov_iter_init(&i, iov, nr_segs, count, 0);
954 written = fuse_perform_write(file, mapping, &i, pos);
955 if (written >= 0)
956 iocb->ki_pos = pos + written;
957
958out:
959 current->backing_dev_info = NULL;
960 mutex_unlock(&inode->i_mutex);
961
962 return written ? written : err;
963}
964
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700965static void fuse_release_user_pages(struct fuse_req *req, int write)
966{
967 unsigned i;
968
969 for (i = 0; i < req->num_pages; i++) {
970 struct page *page = req->pages[i];
971 if (write)
972 set_page_dirty_lock(page);
973 put_page(page);
974 }
975}
976
977static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200978 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700979{
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200980 size_t nbytes = *nbytesp;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700981 unsigned long user_addr = (unsigned long) buf;
982 unsigned offset = user_addr & ~PAGE_MASK;
983 int npages;
984
Miklos Szeredif4975c62009-04-02 14:25:34 +0200985 /* Special case for kernel I/O: can copy directly into the buffer */
986 if (segment_eq(get_fs(), KERNEL_DS)) {
987 if (write)
988 req->in.args[1].value = (void *) user_addr;
989 else
990 req->out.args[0].value = (void *) user_addr;
991
992 return 0;
993 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700994
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200995 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700996 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Harvey Harrisonbd730962008-05-01 04:35:15 -0700997 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi1bf94ca2010-05-25 15:06:06 +0200998 npages = get_user_pages_fast(user_addr, npages, !write, req->pages);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700999 if (npages < 0)
1000 return npages;
1001
1002 req->num_pages = npages;
1003 req->page_offset = offset;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001004
1005 if (write)
1006 req->in.argpages = 1;
1007 else
1008 req->out.argpages = 1;
1009
1010 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
1011 *nbytesp = min(*nbytesp, nbytes);
1012
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001013 return 0;
1014}
1015
Tejun Heo08cbf542009-04-14 10:54:53 +09001016ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1017 size_t count, loff_t *ppos, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001018{
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001019 struct fuse_file *ff = file->private_data;
1020 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001021 size_t nmax = write ? fc->max_write : fc->max_read;
1022 loff_t pos = *ppos;
1023 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001024 struct fuse_req *req;
1025
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001026 req = fuse_get_req(fc);
1027 if (IS_ERR(req))
1028 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001029
1030 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001031 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001032 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001033 size_t nbytes = min(count, nmax);
1034 int err = fuse_get_user_pages(req, buf, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001035 if (err) {
1036 res = err;
1037 break;
1038 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001039
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001040 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001041 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001042 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001043 nres = fuse_send_read(req, file, pos, nbytes, owner);
1044
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001045 fuse_release_user_pages(req, !write);
1046 if (req->out.h.error) {
1047 if (!res)
1048 res = req->out.h.error;
1049 break;
1050 } else if (nres > nbytes) {
1051 res = -EIO;
1052 break;
1053 }
1054 count -= nres;
1055 res += nres;
1056 pos += nres;
1057 buf += nres;
1058 if (nres != nbytes)
1059 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001060 if (count) {
1061 fuse_put_request(fc, req);
1062 req = fuse_get_req(fc);
1063 if (IS_ERR(req))
1064 break;
1065 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001066 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001067 if (!IS_ERR(req))
1068 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001069 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001070 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001071
1072 return res;
1073}
Tejun Heo08cbf542009-04-14 10:54:53 +09001074EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001075
1076static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1077 size_t count, loff_t *ppos)
1078{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001079 ssize_t res;
1080 struct inode *inode = file->f_path.dentry->d_inode;
1081
1082 if (is_bad_inode(inode))
1083 return -EIO;
1084
1085 res = fuse_direct_io(file, buf, count, ppos, 0);
1086
1087 fuse_invalidate_attr(inode);
1088
1089 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001090}
1091
1092static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1093 size_t count, loff_t *ppos)
1094{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001095 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001096 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001097
1098 if (is_bad_inode(inode))
1099 return -EIO;
1100
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001101 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001102 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -07001103 res = generic_write_checks(file, ppos, &count, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001104 if (!res) {
Miklos Szeredi889f7842007-05-23 13:57:54 -07001105 res = fuse_direct_io(file, buf, count, ppos, 1);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001106 if (res > 0)
1107 fuse_write_update_size(inode, *ppos);
1108 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001109 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001110
1111 fuse_invalidate_attr(inode);
1112
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001113 return res;
1114}
1115
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001116static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001117{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001118 __free_page(req->pages[0]);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +01001119 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001120}
1121
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001122static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001123{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001124 struct inode *inode = req->inode;
1125 struct fuse_inode *fi = get_fuse_inode(inode);
1126 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1127
1128 list_del(&req->writepages_entry);
1129 dec_bdi_stat(bdi, BDI_WRITEBACK);
1130 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1131 bdi_writeout_inc(bdi);
1132 wake_up(&fi->page_waitq);
1133}
1134
1135/* Called under fc->lock, may release and reacquire it */
1136static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001137__releases(fc->lock)
1138__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001139{
1140 struct fuse_inode *fi = get_fuse_inode(req->inode);
1141 loff_t size = i_size_read(req->inode);
1142 struct fuse_write_in *inarg = &req->misc.write.in;
1143
1144 if (!fc->connected)
1145 goto out_free;
1146
1147 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1148 inarg->size = PAGE_CACHE_SIZE;
1149 } else if (inarg->offset < size) {
1150 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1151 } else {
1152 /* Got truncated off completely */
1153 goto out_free;
1154 }
1155
1156 req->in.args[1].size = inarg->size;
1157 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001158 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001159 return;
1160
1161 out_free:
1162 fuse_writepage_finish(fc, req);
1163 spin_unlock(&fc->lock);
1164 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001165 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001166 spin_lock(&fc->lock);
1167}
1168
1169/*
1170 * If fi->writectr is positive (no truncate or fsync going on) send
1171 * all queued writepage requests.
1172 *
1173 * Called with fc->lock
1174 */
1175void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001176__releases(fc->lock)
1177__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001178{
1179 struct fuse_conn *fc = get_fuse_conn(inode);
1180 struct fuse_inode *fi = get_fuse_inode(inode);
1181 struct fuse_req *req;
1182
1183 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1184 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1185 list_del_init(&req->list);
1186 fuse_send_writepage(fc, req);
1187 }
1188}
1189
1190static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1191{
1192 struct inode *inode = req->inode;
1193 struct fuse_inode *fi = get_fuse_inode(inode);
1194
1195 mapping_set_error(inode->i_mapping, req->out.h.error);
1196 spin_lock(&fc->lock);
1197 fi->writectr--;
1198 fuse_writepage_finish(fc, req);
1199 spin_unlock(&fc->lock);
1200 fuse_writepage_free(fc, req);
1201}
1202
1203static int fuse_writepage_locked(struct page *page)
1204{
1205 struct address_space *mapping = page->mapping;
1206 struct inode *inode = mapping->host;
1207 struct fuse_conn *fc = get_fuse_conn(inode);
1208 struct fuse_inode *fi = get_fuse_inode(inode);
1209 struct fuse_req *req;
1210 struct fuse_file *ff;
1211 struct page *tmp_page;
1212
1213 set_page_writeback(page);
1214
1215 req = fuse_request_alloc_nofs();
1216 if (!req)
1217 goto err;
1218
1219 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1220 if (!tmp_page)
1221 goto err_free;
1222
1223 spin_lock(&fc->lock);
1224 BUG_ON(list_empty(&fi->write_files));
1225 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1226 req->ff = fuse_file_get(ff);
1227 spin_unlock(&fc->lock);
1228
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001229 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001230
1231 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001232 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001233 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001234 req->num_pages = 1;
1235 req->pages[0] = tmp_page;
1236 req->page_offset = 0;
1237 req->end = fuse_writepage_end;
1238 req->inode = inode;
1239
1240 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1241 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1242 end_page_writeback(page);
1243
1244 spin_lock(&fc->lock);
1245 list_add(&req->writepages_entry, &fi->writepages);
1246 list_add_tail(&req->list, &fi->queued_writes);
1247 fuse_flush_writepages(inode);
1248 spin_unlock(&fc->lock);
1249
1250 return 0;
1251
1252err_free:
1253 fuse_request_free(req);
1254err:
1255 end_page_writeback(page);
1256 return -ENOMEM;
1257}
1258
1259static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1260{
1261 int err;
1262
1263 err = fuse_writepage_locked(page);
1264 unlock_page(page);
1265
1266 return err;
1267}
1268
1269static int fuse_launder_page(struct page *page)
1270{
1271 int err = 0;
1272 if (clear_page_dirty_for_io(page)) {
1273 struct inode *inode = page->mapping->host;
1274 err = fuse_writepage_locked(page);
1275 if (!err)
1276 fuse_wait_on_page_writeback(inode, page->index);
1277 }
1278 return err;
1279}
1280
1281/*
1282 * Write back dirty pages now, because there may not be any suitable
1283 * open files later
1284 */
1285static void fuse_vma_close(struct vm_area_struct *vma)
1286{
1287 filemap_write_and_wait(vma->vm_file->f_mapping);
1288}
1289
1290/*
1291 * Wait for writeback against this page to complete before allowing it
1292 * to be marked dirty again, and hence written back again, possibly
1293 * before the previous writepage completed.
1294 *
1295 * Block here, instead of in ->writepage(), so that the userspace fs
1296 * can only block processes actually operating on the filesystem.
1297 *
1298 * Otherwise unprivileged userspace fs would be able to block
1299 * unrelated:
1300 *
1301 * - page migration
1302 * - sync(2)
1303 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1304 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001305static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001306{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001307 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001308 /*
1309 * Don't use page->mapping as it may become NULL from a
1310 * concurrent truncate.
1311 */
1312 struct inode *inode = vma->vm_file->f_mapping->host;
1313
1314 fuse_wait_on_page_writeback(inode, page->index);
1315 return 0;
1316}
1317
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001318static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001319 .close = fuse_vma_close,
1320 .fault = filemap_fault,
1321 .page_mkwrite = fuse_page_mkwrite,
1322};
1323
1324static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1325{
1326 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1327 struct inode *inode = file->f_dentry->d_inode;
1328 struct fuse_conn *fc = get_fuse_conn(inode);
1329 struct fuse_inode *fi = get_fuse_inode(inode);
1330 struct fuse_file *ff = file->private_data;
1331 /*
1332 * file may be written through mmap, so chain it onto the
1333 * inodes's write_file list
1334 */
1335 spin_lock(&fc->lock);
1336 if (list_empty(&ff->write_entry))
1337 list_add(&ff->write_entry, &fi->write_files);
1338 spin_unlock(&fc->lock);
1339 }
1340 file_accessed(file);
1341 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001342 return 0;
1343}
1344
Miklos Szeredifc280c92009-04-02 14:25:35 +02001345static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1346{
1347 /* Can't provide the coherency needed for MAP_SHARED */
1348 if (vma->vm_flags & VM_MAYSHARE)
1349 return -ENODEV;
1350
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001351 invalidate_inode_pages2(file->f_mapping);
1352
Miklos Szeredifc280c92009-04-02 14:25:35 +02001353 return generic_file_mmap(file, vma);
1354}
1355
Miklos Szeredi71421252006-06-25 05:48:52 -07001356static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1357 struct file_lock *fl)
1358{
1359 switch (ffl->type) {
1360 case F_UNLCK:
1361 break;
1362
1363 case F_RDLCK:
1364 case F_WRLCK:
1365 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1366 ffl->end < ffl->start)
1367 return -EIO;
1368
1369 fl->fl_start = ffl->start;
1370 fl->fl_end = ffl->end;
1371 fl->fl_pid = ffl->pid;
1372 break;
1373
1374 default:
1375 return -EIO;
1376 }
1377 fl->fl_type = ffl->type;
1378 return 0;
1379}
1380
1381static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001382 const struct file_lock *fl, int opcode, pid_t pid,
1383 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001384{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001385 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001386 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001387 struct fuse_file *ff = file->private_data;
1388 struct fuse_lk_in *arg = &req->misc.lk_in;
1389
1390 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001391 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001392 arg->lk.start = fl->fl_start;
1393 arg->lk.end = fl->fl_end;
1394 arg->lk.type = fl->fl_type;
1395 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001396 if (flock)
1397 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001398 req->in.h.opcode = opcode;
1399 req->in.h.nodeid = get_node_id(inode);
1400 req->in.numargs = 1;
1401 req->in.args[0].size = sizeof(*arg);
1402 req->in.args[0].value = arg;
1403}
1404
1405static int fuse_getlk(struct file *file, struct file_lock *fl)
1406{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001407 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001408 struct fuse_conn *fc = get_fuse_conn(inode);
1409 struct fuse_req *req;
1410 struct fuse_lk_out outarg;
1411 int err;
1412
1413 req = fuse_get_req(fc);
1414 if (IS_ERR(req))
1415 return PTR_ERR(req);
1416
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001417 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001418 req->out.numargs = 1;
1419 req->out.args[0].size = sizeof(outarg);
1420 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001421 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001422 err = req->out.h.error;
1423 fuse_put_request(fc, req);
1424 if (!err)
1425 err = convert_fuse_file_lock(&outarg.lk, fl);
1426
1427 return err;
1428}
1429
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001430static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001431{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001432 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001433 struct fuse_conn *fc = get_fuse_conn(inode);
1434 struct fuse_req *req;
1435 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1436 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1437 int err;
1438
Miklos Szeredi48e90762008-07-25 01:49:02 -07001439 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1440 /* NLM needs asynchronous locks, which we don't support yet */
1441 return -ENOLCK;
1442 }
1443
Miklos Szeredi71421252006-06-25 05:48:52 -07001444 /* Unlock on close is handled by the flush method */
1445 if (fl->fl_flags & FL_CLOSE)
1446 return 0;
1447
1448 req = fuse_get_req(fc);
1449 if (IS_ERR(req))
1450 return PTR_ERR(req);
1451
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001452 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001453 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001454 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001455 /* locking is restartable */
1456 if (err == -EINTR)
1457 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001458 fuse_put_request(fc, req);
1459 return err;
1460}
1461
1462static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1463{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001464 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001465 struct fuse_conn *fc = get_fuse_conn(inode);
1466 int err;
1467
Miklos Szeredi48e90762008-07-25 01:49:02 -07001468 if (cmd == F_CANCELLK) {
1469 err = 0;
1470 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001471 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001472 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001473 err = 0;
1474 } else
1475 err = fuse_getlk(file, fl);
1476 } else {
1477 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001478 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001479 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001480 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001481 }
1482 return err;
1483}
1484
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001485static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1486{
1487 struct inode *inode = file->f_path.dentry->d_inode;
1488 struct fuse_conn *fc = get_fuse_conn(inode);
1489 int err;
1490
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001491 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001492 err = flock_lock_file_wait(file, fl);
1493 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001494 struct fuse_file *ff = file->private_data;
1495
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001496 /* emulate flock with POSIX locks */
1497 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001498 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001499 err = fuse_setlk(file, fl, 1);
1500 }
1501
1502 return err;
1503}
1504
Miklos Szeredib2d22722006-12-06 20:35:51 -08001505static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1506{
1507 struct inode *inode = mapping->host;
1508 struct fuse_conn *fc = get_fuse_conn(inode);
1509 struct fuse_req *req;
1510 struct fuse_bmap_in inarg;
1511 struct fuse_bmap_out outarg;
1512 int err;
1513
1514 if (!inode->i_sb->s_bdev || fc->no_bmap)
1515 return 0;
1516
1517 req = fuse_get_req(fc);
1518 if (IS_ERR(req))
1519 return 0;
1520
1521 memset(&inarg, 0, sizeof(inarg));
1522 inarg.block = block;
1523 inarg.blocksize = inode->i_sb->s_blocksize;
1524 req->in.h.opcode = FUSE_BMAP;
1525 req->in.h.nodeid = get_node_id(inode);
1526 req->in.numargs = 1;
1527 req->in.args[0].size = sizeof(inarg);
1528 req->in.args[0].value = &inarg;
1529 req->out.numargs = 1;
1530 req->out.args[0].size = sizeof(outarg);
1531 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001532 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001533 err = req->out.h.error;
1534 fuse_put_request(fc, req);
1535 if (err == -ENOSYS)
1536 fc->no_bmap = 1;
1537
1538 return err ? 0 : outarg.block;
1539}
1540
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001541static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1542{
1543 loff_t retval;
1544 struct inode *inode = file->f_path.dentry->d_inode;
1545
1546 mutex_lock(&inode->i_mutex);
1547 switch (origin) {
1548 case SEEK_END:
Miklos Szeredi769415c2008-10-16 16:08:56 +02001549 retval = fuse_update_attributes(inode, NULL, file, NULL);
1550 if (retval)
Dan Carpenter52916582009-03-27 13:36:10 +03001551 goto exit;
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001552 offset += i_size_read(inode);
1553 break;
1554 case SEEK_CUR:
1555 offset += file->f_pos;
1556 }
1557 retval = -EINVAL;
1558 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1559 if (offset != file->f_pos) {
1560 file->f_pos = offset;
1561 file->f_version = 0;
1562 }
1563 retval = offset;
1564 }
Dan Carpenter52916582009-03-27 13:36:10 +03001565exit:
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001566 mutex_unlock(&inode->i_mutex);
1567 return retval;
1568}
1569
Tejun Heo59efec72008-11-26 12:03:55 +01001570static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1571 unsigned int nr_segs, size_t bytes, bool to_user)
1572{
1573 struct iov_iter ii;
1574 int page_idx = 0;
1575
1576 if (!bytes)
1577 return 0;
1578
1579 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1580
1581 while (iov_iter_count(&ii)) {
1582 struct page *page = pages[page_idx++];
1583 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001584 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001585
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001586 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001587
1588 while (todo) {
1589 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1590 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1591 size_t copy = min(todo, iov_len);
1592 size_t left;
1593
1594 if (!to_user)
1595 left = copy_from_user(kaddr, uaddr, copy);
1596 else
1597 left = copy_to_user(uaddr, kaddr, copy);
1598
1599 if (unlikely(left))
1600 return -EFAULT;
1601
1602 iov_iter_advance(&ii, copy);
1603 todo -= copy;
1604 kaddr += copy;
1605 }
1606
Jens Axboe0bd87182009-11-03 11:40:44 +01001607 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001608 }
1609
1610 return 0;
1611}
1612
1613/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001614 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1615 * ABI was defined to be 'struct iovec' which is different on 32bit
1616 * and 64bit. Fortunately we can determine which structure the server
1617 * used from the size of the reply.
1618 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001619static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1620 size_t transferred, unsigned count,
1621 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001622{
1623#ifdef CONFIG_COMPAT
1624 if (count * sizeof(struct compat_iovec) == transferred) {
1625 struct compat_iovec *ciov = src;
1626 unsigned i;
1627
1628 /*
1629 * With this interface a 32bit server cannot support
1630 * non-compat (i.e. ones coming from 64bit apps) ioctl
1631 * requests
1632 */
1633 if (!is_compat)
1634 return -EINVAL;
1635
1636 for (i = 0; i < count; i++) {
1637 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1638 dst[i].iov_len = ciov[i].iov_len;
1639 }
1640 return 0;
1641 }
1642#endif
1643
1644 if (count * sizeof(struct iovec) != transferred)
1645 return -EIO;
1646
1647 memcpy(dst, src, transferred);
1648 return 0;
1649}
1650
Miklos Szeredi75727772010-11-30 16:39:27 +01001651/* Make sure iov_length() won't overflow */
1652static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1653{
1654 size_t n;
1655 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1656
1657 for (n = 0; n < count; n++) {
1658 if (iov->iov_len > (size_t) max)
1659 return -ENOMEM;
1660 max -= iov->iov_len;
1661 }
1662 return 0;
1663}
1664
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001665static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1666 void *src, size_t transferred, unsigned count,
1667 bool is_compat)
1668{
1669 unsigned i;
1670 struct fuse_ioctl_iovec *fiov = src;
1671
1672 if (fc->minor < 16) {
1673 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1674 count, is_compat);
1675 }
1676
1677 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1678 return -EIO;
1679
1680 for (i = 0; i < count; i++) {
1681 /* Did the server supply an inappropriate value? */
1682 if (fiov[i].base != (unsigned long) fiov[i].base ||
1683 fiov[i].len != (unsigned long) fiov[i].len)
1684 return -EIO;
1685
1686 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1687 dst[i].iov_len = (size_t) fiov[i].len;
1688
1689#ifdef CONFIG_COMPAT
1690 if (is_compat &&
1691 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1692 (compat_size_t) dst[i].iov_len != fiov[i].len))
1693 return -EIO;
1694#endif
1695 }
1696
1697 return 0;
1698}
1699
1700
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001701/*
Tejun Heo59efec72008-11-26 12:03:55 +01001702 * For ioctls, there is no generic way to determine how much memory
1703 * needs to be read and/or written. Furthermore, ioctls are allowed
1704 * to dereference the passed pointer, so the parameter requires deep
1705 * copying but FUSE has no idea whatsoever about what to copy in or
1706 * out.
1707 *
1708 * This is solved by allowing FUSE server to retry ioctl with
1709 * necessary in/out iovecs. Let's assume the ioctl implementation
1710 * needs to read in the following structure.
1711 *
1712 * struct a {
1713 * char *buf;
1714 * size_t buflen;
1715 * }
1716 *
1717 * On the first callout to FUSE server, inarg->in_size and
1718 * inarg->out_size will be NULL; then, the server completes the ioctl
1719 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1720 * the actual iov array to
1721 *
1722 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1723 *
1724 * which tells FUSE to copy in the requested area and retry the ioctl.
1725 * On the second round, the server has access to the structure and
1726 * from that it can tell what to look for next, so on the invocation,
1727 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1728 *
1729 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1730 * { .iov_base = a.buf, .iov_len = a.buflen } }
1731 *
1732 * FUSE will copy both struct a and the pointed buffer from the
1733 * process doing the ioctl and retry ioctl with both struct a and the
1734 * buffer.
1735 *
1736 * This time, FUSE server has everything it needs and completes ioctl
1737 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1738 *
1739 * Copying data out works the same way.
1740 *
1741 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1742 * automatically initializes in and out iovs by decoding @cmd with
1743 * _IOC_* macros and the server is not allowed to request RETRY. This
1744 * limits ioctl data transfers to well-formed ioctls and is the forced
1745 * behavior for all FUSE servers.
1746 */
Tejun Heo08cbf542009-04-14 10:54:53 +09001747long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1748 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01001749{
Tejun Heo59efec72008-11-26 12:03:55 +01001750 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001751 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01001752 struct fuse_ioctl_in inarg = {
1753 .fh = ff->fh,
1754 .cmd = cmd,
1755 .arg = arg,
1756 .flags = flags
1757 };
1758 struct fuse_ioctl_out outarg;
1759 struct fuse_req *req = NULL;
1760 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001761 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01001762 struct iovec *in_iov = NULL, *out_iov = NULL;
1763 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1764 size_t in_size, out_size, transferred;
1765 int err;
1766
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001767#if BITS_PER_LONG == 32
1768 inarg.flags |= FUSE_IOCTL_32BIT;
1769#else
1770 if (flags & FUSE_IOCTL_COMPAT)
1771 inarg.flags |= FUSE_IOCTL_32BIT;
1772#endif
1773
Tejun Heo59efec72008-11-26 12:03:55 +01001774 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001775 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01001776
Tejun Heo59efec72008-11-26 12:03:55 +01001777 err = -ENOMEM;
1778 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001779 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01001780 if (!pages || !iov_page)
1781 goto out;
1782
1783 /*
1784 * If restricted, initialize IO parameters as encoded in @cmd.
1785 * RETRY from server is not allowed.
1786 */
1787 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001788 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01001789
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01001790 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01001791 iov->iov_len = _IOC_SIZE(cmd);
1792
1793 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1794 in_iov = iov;
1795 in_iovs = 1;
1796 }
1797
1798 if (_IOC_DIR(cmd) & _IOC_READ) {
1799 out_iov = iov;
1800 out_iovs = 1;
1801 }
1802 }
1803
1804 retry:
1805 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1806 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1807
1808 /*
1809 * Out data can be used either for actual out data or iovs,
1810 * make sure there always is at least one page.
1811 */
1812 out_size = max_t(size_t, out_size, PAGE_SIZE);
1813 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1814
1815 /* make sure there are enough buffer pages and init request with them */
1816 err = -ENOMEM;
1817 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1818 goto out;
1819 while (num_pages < max_pages) {
1820 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1821 if (!pages[num_pages])
1822 goto out;
1823 num_pages++;
1824 }
1825
1826 req = fuse_get_req(fc);
1827 if (IS_ERR(req)) {
1828 err = PTR_ERR(req);
1829 req = NULL;
1830 goto out;
1831 }
1832 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1833 req->num_pages = num_pages;
1834
1835 /* okay, let's send it to the client */
1836 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001837 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01001838 req->in.numargs = 1;
1839 req->in.args[0].size = sizeof(inarg);
1840 req->in.args[0].value = &inarg;
1841 if (in_size) {
1842 req->in.numargs++;
1843 req->in.args[1].size = in_size;
1844 req->in.argpages = 1;
1845
1846 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1847 false);
1848 if (err)
1849 goto out;
1850 }
1851
1852 req->out.numargs = 2;
1853 req->out.args[0].size = sizeof(outarg);
1854 req->out.args[0].value = &outarg;
1855 req->out.args[1].size = out_size;
1856 req->out.argpages = 1;
1857 req->out.argvar = 1;
1858
Tejun Heob93f8582008-11-26 12:03:55 +01001859 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01001860 err = req->out.h.error;
1861 transferred = req->out.args[1].size;
1862 fuse_put_request(fc, req);
1863 req = NULL;
1864 if (err)
1865 goto out;
1866
1867 /* did it ask for retry? */
1868 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001869 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001870
1871 /* no retry if in restricted mode */
1872 err = -EIO;
1873 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1874 goto out;
1875
1876 in_iovs = outarg.in_iovs;
1877 out_iovs = outarg.out_iovs;
1878
1879 /*
1880 * Make sure things are in boundary, separate checks
1881 * are to protect against overflow.
1882 */
1883 err = -ENOMEM;
1884 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1885 out_iovs > FUSE_IOCTL_MAX_IOV ||
1886 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1887 goto out;
1888
Tejun Heo59efec72008-11-26 12:03:55 +01001889 vaddr = kmap_atomic(pages[0], KM_USER0);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001890 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001891 transferred, in_iovs + out_iovs,
1892 (flags & FUSE_IOCTL_COMPAT) != 0);
Tejun Heo59efec72008-11-26 12:03:55 +01001893 kunmap_atomic(vaddr, KM_USER0);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001894 if (err)
1895 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01001896
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001897 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01001898 out_iov = in_iov + in_iovs;
1899
Miklos Szeredi75727772010-11-30 16:39:27 +01001900 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
1901 if (err)
1902 goto out;
1903
1904 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
1905 if (err)
1906 goto out;
1907
Tejun Heo59efec72008-11-26 12:03:55 +01001908 goto retry;
1909 }
1910
1911 err = -EIO;
1912 if (transferred > inarg.out_size)
1913 goto out;
1914
1915 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1916 out:
1917 if (req)
1918 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001919 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01001920 while (num_pages)
1921 __free_page(pages[--num_pages]);
1922 kfree(pages);
1923
1924 return err ? err : outarg.result;
1925}
Tejun Heo08cbf542009-04-14 10:54:53 +09001926EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01001927
Miklos Szeredid36f2482009-04-28 16:56:39 +02001928static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
1929 unsigned long arg, unsigned int flags)
1930{
1931 struct inode *inode = file->f_dentry->d_inode;
1932 struct fuse_conn *fc = get_fuse_conn(inode);
1933
1934 if (!fuse_allow_task(fc, current))
1935 return -EACCES;
1936
1937 if (is_bad_inode(inode))
1938 return -EIO;
1939
1940 return fuse_do_ioctl(file, cmd, arg, flags);
1941}
1942
Tejun Heo59efec72008-11-26 12:03:55 +01001943static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1944 unsigned long arg)
1945{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001946 return fuse_file_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01001947}
1948
1949static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1950 unsigned long arg)
1951{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001952 return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01001953}
1954
Tejun Heo95668a62008-11-26 12:03:55 +01001955/*
1956 * All files which have been polled are linked to RB tree
1957 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1958 * find the matching one.
1959 */
1960static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1961 struct rb_node **parent_out)
1962{
1963 struct rb_node **link = &fc->polled_files.rb_node;
1964 struct rb_node *last = NULL;
1965
1966 while (*link) {
1967 struct fuse_file *ff;
1968
1969 last = *link;
1970 ff = rb_entry(last, struct fuse_file, polled_node);
1971
1972 if (kh < ff->kh)
1973 link = &last->rb_left;
1974 else if (kh > ff->kh)
1975 link = &last->rb_right;
1976 else
1977 return link;
1978 }
1979
1980 if (parent_out)
1981 *parent_out = last;
1982 return link;
1983}
1984
1985/*
1986 * The file is about to be polled. Make sure it's on the polled_files
1987 * RB tree. Note that files once added to the polled_files tree are
1988 * not removed before the file is released. This is because a file
1989 * polled once is likely to be polled again.
1990 */
1991static void fuse_register_polled_file(struct fuse_conn *fc,
1992 struct fuse_file *ff)
1993{
1994 spin_lock(&fc->lock);
1995 if (RB_EMPTY_NODE(&ff->polled_node)) {
1996 struct rb_node **link, *parent;
1997
1998 link = fuse_find_polled_node(fc, ff->kh, &parent);
1999 BUG_ON(*link);
2000 rb_link_node(&ff->polled_node, parent, link);
2001 rb_insert_color(&ff->polled_node, &fc->polled_files);
2002 }
2003 spin_unlock(&fc->lock);
2004}
2005
Tejun Heo08cbf542009-04-14 10:54:53 +09002006unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002007{
Tejun Heo95668a62008-11-26 12:03:55 +01002008 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002009 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002010 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2011 struct fuse_poll_out outarg;
2012 struct fuse_req *req;
2013 int err;
2014
2015 if (fc->no_poll)
2016 return DEFAULT_POLLMASK;
2017
2018 poll_wait(file, &ff->poll_wait, wait);
2019
2020 /*
2021 * Ask for notification iff there's someone waiting for it.
2022 * The client may ignore the flag and always notify.
2023 */
2024 if (waitqueue_active(&ff->poll_wait)) {
2025 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2026 fuse_register_polled_file(fc, ff);
2027 }
2028
2029 req = fuse_get_req(fc);
2030 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002031 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002032
2033 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002034 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002035 req->in.numargs = 1;
2036 req->in.args[0].size = sizeof(inarg);
2037 req->in.args[0].value = &inarg;
2038 req->out.numargs = 1;
2039 req->out.args[0].size = sizeof(outarg);
2040 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002041 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002042 err = req->out.h.error;
2043 fuse_put_request(fc, req);
2044
2045 if (!err)
2046 return outarg.revents;
2047 if (err == -ENOSYS) {
2048 fc->no_poll = 1;
2049 return DEFAULT_POLLMASK;
2050 }
2051 return POLLERR;
2052}
Tejun Heo08cbf542009-04-14 10:54:53 +09002053EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002054
2055/*
2056 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2057 * wakes up the poll waiters.
2058 */
2059int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2060 struct fuse_notify_poll_wakeup_out *outarg)
2061{
2062 u64 kh = outarg->kh;
2063 struct rb_node **link;
2064
2065 spin_lock(&fc->lock);
2066
2067 link = fuse_find_polled_node(fc, kh, NULL);
2068 if (*link) {
2069 struct fuse_file *ff;
2070
2071 ff = rb_entry(*link, struct fuse_file, polled_node);
2072 wake_up_interruptible_sync(&ff->poll_wait);
2073 }
2074
2075 spin_unlock(&fc->lock);
2076 return 0;
2077}
2078
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002079static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002080 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002081 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002082 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002083 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002084 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002085 .mmap = fuse_file_mmap,
2086 .open = fuse_open,
2087 .flush = fuse_flush,
2088 .release = fuse_release,
2089 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002090 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002091 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002092 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002093 .unlocked_ioctl = fuse_file_ioctl,
2094 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002095 .poll = fuse_file_poll,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002096};
2097
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002098static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002099 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002100 .read = fuse_direct_read,
2101 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002102 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002103 .open = fuse_open,
2104 .flush = fuse_flush,
2105 .release = fuse_release,
2106 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002107 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002108 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002109 .unlocked_ioctl = fuse_file_ioctl,
2110 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002111 .poll = fuse_file_poll,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002112 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002113};
2114
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002115static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002116 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002117 .writepage = fuse_writepage,
2118 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002119 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002120 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002121 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002122};
2123
2124void fuse_init_file_inode(struct inode *inode)
2125{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002126 inode->i_fop = &fuse_file_operations;
2127 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002128}