blob: 944acc0967521fcda74da5ca7df49c2dcd6ba251 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070018#include <linux/aio.h>
Brian Foster3634a632013-05-17 09:30:32 -040019#include <linux/falloc.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080021static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070022
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020023static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080027 struct fuse_req *req;
28 int err;
29
Maxim Patlasovb111c8c2012-10-26 19:48:30 +040030 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -070031 if (IS_ERR(req))
32 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080033
34 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070035 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
36 if (!fc->atomic_o_trunc)
37 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020038 req->in.h.opcode = opcode;
39 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080040 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 Heob93f8582008-11-26 12:03:55 +010046 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080047 err = req->out.h.error;
48 fuse_put_request(fc, req);
49
50 return err;
51}
52
Tejun Heoacf99432008-11-26 12:03:55 +010053struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054{
55 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090056
Miklos Szeredifd72faa2005-11-07 00:59:51 -080057 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090058 if (unlikely(!ff))
59 return NULL;
60
Miklos Szeredida5e4712009-04-28 16:56:36 +020061 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040062 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090063 if (unlikely(!ff->reserved_req)) {
64 kfree(ff);
65 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080066 }
Tejun Heo6b2db282009-04-14 10:54:49 +090067
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 Szeredifd72faa2005-11-07 00:59:51 -080077 return ff;
78}
79
80void fuse_file_free(struct fuse_file *ff)
81{
Miklos Szeredi33649c92006-06-25 05:48:52 -070082 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080083 kfree(ff);
84}
85
Miklos Szeredic7b71432009-04-28 16:56:37 +020086struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070087{
88 atomic_inc(&ff->count);
89 return ff;
90}
91
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010092static void fuse_release_async(struct work_struct *work)
Miklos Szeredi819c4b32007-10-16 23:31:04 -070093{
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010094 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 Szeredi819c4b32007-10-16 23:31:04 -0700104}
105
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100106static 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
125static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700126{
127 if (atomic_dec_and_test(&ff->count)) {
128 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200129
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100130 if (sync) {
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400131 req->background = 0;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100132 fuse_request_send(ff->fc, req);
133 path_put(&req->misc.release.path);
134 fuse_put_request(ff->fc, req);
135 } else {
136 req->end = fuse_release_end;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400137 req->background = 1;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100138 fuse_request_send_background(ff->fc, req);
139 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700140 kfree(ff);
141 }
142}
143
Tejun Heo08cbf542009-04-14 10:54:53 +0900144int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
145 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200146{
147 struct fuse_open_out outarg;
148 struct fuse_file *ff;
149 int err;
150 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
151
152 ff = fuse_file_alloc(fc);
153 if (!ff)
154 return -ENOMEM;
155
156 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
157 if (err) {
158 fuse_file_free(ff);
159 return err;
160 }
161
162 if (isdir)
163 outarg.open_flags &= ~FOPEN_DIRECT_IO;
164
165 ff->fh = outarg.fh;
166 ff->nodeid = nodeid;
167 ff->open_flags = outarg.open_flags;
168 file->private_data = fuse_file_get(ff);
169
170 return 0;
171}
Tejun Heo08cbf542009-04-14 10:54:53 +0900172EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200173
Miklos Szeredic7b71432009-04-28 16:56:37 +0200174void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800175{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200176 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800177 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200178
179 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800180 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200181 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700182 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200183 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200184 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800185 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
186 struct fuse_inode *fi = get_fuse_inode(inode);
187
188 spin_lock(&fc->lock);
189 fi->attr_version = ++fc->attr_version;
190 i_size_write(inode, 0);
191 spin_unlock(&fc->lock);
192 fuse_invalidate_attr(inode);
193 }
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800194}
195
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200196int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800197{
Tejun Heoacf99432008-11-26 12:03:55 +0100198 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700199 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700200
201 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;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400289 ff->reserved_req->background = 0;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200290 fuse_request_send(ff->fc, ff->reserved_req);
291 fuse_put_request(ff->fc, ff->reserved_req);
292 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700293}
Tejun Heo08cbf542009-04-14 10:54:53 +0900294EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700295
Miklos Szeredi71421252006-06-25 05:48:52 -0700296/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700297 * Scramble the ID space with XTEA, so that the value of the files_struct
298 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700299 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700300u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700301{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700302 u32 *k = fc->scramble_key;
303 u64 v = (unsigned long) id;
304 u32 v0 = v;
305 u32 v1 = v >> 32;
306 u32 sum = 0;
307 int i;
308
309 for (i = 0; i < 32; i++) {
310 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
311 sum += 0x9E3779B9;
312 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
313 }
314
315 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700316}
317
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700318/*
319 * Check if page is under writeback
320 *
321 * This is currently done by walking the list of writepage requests
322 * for the inode, which can be pretty inefficient.
323 */
324static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
325{
326 struct fuse_conn *fc = get_fuse_conn(inode);
327 struct fuse_inode *fi = get_fuse_inode(inode);
328 struct fuse_req *req;
329 bool found = false;
330
331 spin_lock(&fc->lock);
332 list_for_each_entry(req, &fi->writepages, writepages_entry) {
333 pgoff_t curr_index;
334
335 BUG_ON(req->inode != inode);
336 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
Pavel Emelyanov385b1262013-06-29 21:42:48 +0400337 if (curr_index <= index &&
338 index < curr_index + req->num_pages) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700339 found = true;
340 break;
341 }
342 }
343 spin_unlock(&fc->lock);
344
345 return found;
346}
347
348/*
349 * Wait for page writeback to be completed.
350 *
351 * Since fuse doesn't rely on the VM writeback tracking, this has to
352 * use some other means.
353 */
354static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
355{
356 struct fuse_inode *fi = get_fuse_inode(inode);
357
358 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
359 return 0;
360}
361
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700362static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700363{
Al Viro6131ffa2013-02-27 16:59:05 -0500364 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700365 struct fuse_conn *fc = get_fuse_conn(inode);
366 struct fuse_file *ff = file->private_data;
367 struct fuse_req *req;
368 struct fuse_flush_in inarg;
369 int err;
370
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800371 if (is_bad_inode(inode))
372 return -EIO;
373
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700374 if (fc->no_flush)
375 return 0;
376
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400377 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700378 memset(&inarg, 0, sizeof(inarg));
379 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700380 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700381 req->in.h.opcode = FUSE_FLUSH;
382 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700383 req->in.numargs = 1;
384 req->in.args[0].size = sizeof(inarg);
385 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700386 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100387 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700388 err = req->out.h.error;
389 fuse_put_request(fc, req);
390 if (err == -ENOSYS) {
391 fc->no_flush = 1;
392 err = 0;
393 }
394 return err;
395}
396
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700397/*
398 * Wait for all pending writepages on the inode to finish.
399 *
400 * This is currently done by blocking further writes with FUSE_NOWRITE
401 * and waiting for all sent writes to complete.
402 *
403 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
404 * could conflict with truncation.
405 */
406static void fuse_sync_writes(struct inode *inode)
407{
408 fuse_set_nowrite(inode);
409 fuse_release_nowrite(inode);
410}
411
Josef Bacik02c24a82011-07-16 20:44:56 -0400412int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
413 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700414{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200415 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700416 struct fuse_conn *fc = get_fuse_conn(inode);
417 struct fuse_file *ff = file->private_data;
418 struct fuse_req *req;
419 struct fuse_fsync_in inarg;
420 int err;
421
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800422 if (is_bad_inode(inode))
423 return -EIO;
424
Josef Bacik02c24a82011-07-16 20:44:56 -0400425 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
426 if (err)
427 return err;
428
Miklos Szeredi82547982005-09-09 13:10:38 -0700429 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700430 return 0;
431
Josef Bacik02c24a82011-07-16 20:44:56 -0400432 mutex_lock(&inode->i_mutex);
433
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700434 /*
435 * Start writeback against all dirty pages of the inode, then
436 * wait for all outstanding writes, before sending the FSYNC
437 * request.
438 */
439 err = write_inode_now(inode, 0);
440 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400441 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700442
443 fuse_sync_writes(inode);
444
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400445 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400446 if (IS_ERR(req)) {
447 err = PTR_ERR(req);
448 goto out;
449 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700450
451 memset(&inarg, 0, sizeof(inarg));
452 inarg.fh = ff->fh;
453 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700454 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700455 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700456 req->in.numargs = 1;
457 req->in.args[0].size = sizeof(inarg);
458 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100459 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700460 err = req->out.h.error;
461 fuse_put_request(fc, req);
462 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700463 if (isdir)
464 fc->no_fsyncdir = 1;
465 else
466 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700467 err = 0;
468 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400469out:
470 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700471 return err;
472}
473
Josef Bacik02c24a82011-07-16 20:44:56 -0400474static int fuse_fsync(struct file *file, loff_t start, loff_t end,
475 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700476{
Josef Bacik02c24a82011-07-16 20:44:56 -0400477 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700478}
479
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200480void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
481 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700482{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700483 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800484 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700485
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800486 inarg->fh = ff->fh;
487 inarg->offset = pos;
488 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800489 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800490 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200491 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700492 req->in.numargs = 1;
493 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800494 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700495 req->out.argvar = 1;
496 req->out.numargs = 1;
497 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700498}
499
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400500static void fuse_release_user_pages(struct fuse_req *req, int write)
501{
502 unsigned i;
503
504 for (i = 0; i < req->num_pages; i++) {
505 struct page *page = req->pages[i];
506 if (write)
507 set_page_dirty_lock(page);
508 put_page(page);
509 }
510}
511
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400512/**
513 * In case of short read, the caller sets 'pos' to the position of
514 * actual end of fuse request in IO request. Otherwise, if bytes_requested
515 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
516 *
517 * An example:
518 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
519 * both submitted asynchronously. The first of them was ACKed by userspace as
520 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
521 * second request was ACKed as short, e.g. only 1K was read, resulting in
522 * pos == 33K.
523 *
524 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
525 * will be equal to the length of the longest contiguous fragment of
526 * transferred data starting from the beginning of IO request.
527 */
528static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
529{
530 int left;
531
532 spin_lock(&io->lock);
533 if (err)
534 io->err = io->err ? : err;
535 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
536 io->bytes = pos;
537
538 left = --io->reqs;
539 spin_unlock(&io->lock);
540
541 if (!left) {
542 long res;
543
544 if (io->err)
545 res = io->err;
546 else if (io->bytes >= 0 && io->write)
547 res = -EIO;
548 else {
549 res = io->bytes < 0 ? io->size : io->bytes;
550
551 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400552 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400553 struct fuse_conn *fc = get_fuse_conn(inode);
554 struct fuse_inode *fi = get_fuse_inode(inode);
555
556 spin_lock(&fc->lock);
557 fi->attr_version = ++fc->attr_version;
558 spin_unlock(&fc->lock);
559 }
560 }
561
562 aio_complete(io->iocb, res, 0);
563 kfree(io);
564 }
565}
566
567static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
568{
569 struct fuse_io_priv *io = req->io;
570 ssize_t pos = -1;
571
572 fuse_release_user_pages(req, !io->write);
573
574 if (io->write) {
575 if (req->misc.write.in.size != req->misc.write.out.size)
576 pos = req->misc.write.in.offset - io->offset +
577 req->misc.write.out.size;
578 } else {
579 if (req->misc.read.in.size != req->out.args[0].size)
580 pos = req->misc.read.in.offset - io->offset +
581 req->out.args[0].size;
582 }
583
584 fuse_aio_complete(io, req->out.h.error, pos);
585}
586
587static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
588 size_t num_bytes, struct fuse_io_priv *io)
589{
590 spin_lock(&io->lock);
591 io->size += num_bytes;
592 io->reqs++;
593 spin_unlock(&io->lock);
594
595 req->io = io;
596 req->end = fuse_aio_complete_req;
597
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400598 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400599 fuse_request_send_background(fc, req);
600
601 return num_bytes;
602}
603
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400604static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200605 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700606{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400607 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200608 struct fuse_file *ff = file->private_data;
609 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700610
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200611 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700612 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700613 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700614
615 inarg->read_flags |= FUSE_READ_LOCKOWNER;
616 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
617 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400618
619 if (io->async)
620 return fuse_async_req_send(fc, req, count, io);
621
Tejun Heob93f8582008-11-26 12:03:55 +0100622 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800623 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700624}
625
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700626static void fuse_read_update_size(struct inode *inode, loff_t size,
627 u64 attr_ver)
628{
629 struct fuse_conn *fc = get_fuse_conn(inode);
630 struct fuse_inode *fi = get_fuse_inode(inode);
631
632 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400633 if (attr_ver == fi->attr_version && size < inode->i_size &&
634 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700635 fi->attr_version = ++fc->attr_version;
636 i_size_write(inode, size);
637 }
638 spin_unlock(&fc->lock);
639}
640
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700641static int fuse_readpage(struct file *file, struct page *page)
642{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400643 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700644 struct inode *inode = page->mapping->host;
645 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800646 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700647 size_t num_read;
648 loff_t pos = page_offset(page);
649 size_t count = PAGE_CACHE_SIZE;
650 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800651 int err;
652
653 err = -EIO;
654 if (is_bad_inode(inode))
655 goto out;
656
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700657 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300658 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700659 * page-cache page, so make sure we read a properly synced
660 * page.
661 */
662 fuse_wait_on_page_writeback(inode, page->index);
663
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400664 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700665 err = PTR_ERR(req);
666 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700667 goto out;
668
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700669 attr_ver = fuse_get_attr_version(fc);
670
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700671 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200672 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700673 req->num_pages = 1;
674 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400675 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400676 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700677 err = req->out.h.error;
678 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700679
680 if (!err) {
681 /*
682 * Short read means EOF. If file size is larger, truncate it
683 */
684 if (num_read < count)
685 fuse_read_update_size(inode, pos + num_read, attr_ver);
686
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700687 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700688 }
689
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700690 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700691 out:
692 unlock_page(page);
693 return err;
694}
695
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800696static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700697{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800698 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700699 size_t count = req->misc.read.in.size;
700 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200701 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800702
Miklos Szeredice534fb2010-05-25 15:06:07 +0200703 for (i = 0; mapping == NULL && i < req->num_pages; i++)
704 mapping = req->pages[i]->mapping;
705
706 if (mapping) {
707 struct inode *inode = mapping->host;
708
709 /*
710 * Short read means EOF. If file size is larger, truncate it
711 */
712 if (!req->out.h.error && num_read < count) {
713 loff_t pos;
714
715 pos = page_offset(req->pages[0]) + num_read;
716 fuse_read_update_size(inode, pos,
717 req->misc.read.attr_ver);
718 }
719 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700720 }
721
Miklos Szeredidb50b962005-09-09 13:10:33 -0700722 for (i = 0; i < req->num_pages; i++) {
723 struct page *page = req->pages[i];
724 if (!req->out.h.error)
725 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800726 else
727 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700728 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200729 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700730 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700731 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100732 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800733}
734
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200735static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800736{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200737 struct fuse_file *ff = file->private_data;
738 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800739 loff_t pos = page_offset(req->pages[0]);
740 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200741
742 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800743 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200744 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200745 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700746 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800747 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700748 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800749 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100750 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800751 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100752 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800753 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100754 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800755 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700756}
757
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700758struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700759 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800760 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700761 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400762 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700763};
764
765static int fuse_readpages_fill(void *_data, struct page *page)
766{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700767 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700768 struct fuse_req *req = data->req;
769 struct inode *inode = data->inode;
770 struct fuse_conn *fc = get_fuse_conn(inode);
771
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700772 fuse_wait_on_page_writeback(inode, page->index);
773
Miklos Szeredidb50b962005-09-09 13:10:33 -0700774 if (req->num_pages &&
775 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
776 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
777 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400778 int nr_alloc = min_t(unsigned, data->nr_pages,
779 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200780 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400781 if (fc->async_read)
782 req = fuse_get_req_for_background(fc, nr_alloc);
783 else
784 req = fuse_get_req(fc, nr_alloc);
785
786 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700787 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700788 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700789 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700790 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700791 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400792
793 if (WARN_ON(req->num_pages >= req->max_pages)) {
794 fuse_put_request(fc, req);
795 return -EIO;
796 }
797
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200798 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700799 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400800 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100801 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400802 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700803 return 0;
804}
805
806static int fuse_readpages(struct file *file, struct address_space *mapping,
807 struct list_head *pages, unsigned nr_pages)
808{
809 struct inode *inode = mapping->host;
810 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700811 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700812 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400813 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800814
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700815 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800816 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800817 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800818
Miklos Szeredia6643092007-11-28 16:22:00 -0800819 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700820 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400821 if (fc->async_read)
822 data.req = fuse_get_req_for_background(fc, nr_alloc);
823 else
824 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400825 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700826 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700827 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800828 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700829
830 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700831 if (!err) {
832 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200833 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700834 else
835 fuse_put_request(fc, data.req);
836 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800837out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700838 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700839}
840
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800841static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
842 unsigned long nr_segs, loff_t pos)
843{
844 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400845 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800846
Brian Fostera8894272012-07-16 15:23:50 -0400847 /*
848 * In auto invalidate mode, always update attributes on read.
849 * Otherwise, only update if we attempt to read past EOF (to ensure
850 * i_size is up to date).
851 */
852 if (fc->auto_inval_data ||
853 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800854 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800855 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
856 if (err)
857 return err;
858 }
859
860 return generic_file_aio_read(iocb, iov, nr_segs, pos);
861}
862
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200863static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200864 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700865{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700866 struct fuse_write_in *inarg = &req->misc.write.in;
867 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700868
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700869 inarg->fh = ff->fh;
870 inarg->offset = pos;
871 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700872 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200873 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700874 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200875 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700876 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
877 else
878 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700879 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700880 req->in.args[1].size = count;
881 req->out.numargs = 1;
882 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700883 req->out.args[0].value = outarg;
884}
885
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400886static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200887 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700888{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400889 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200890 struct fuse_file *ff = file->private_data;
891 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200892 struct fuse_write_in *inarg = &req->misc.write.in;
893
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200894 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200895 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700896 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700897 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
898 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
899 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400900
901 if (io->async)
902 return fuse_async_req_send(fc, req, count, io);
903
Tejun Heob93f8582008-11-26 12:03:55 +0100904 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700905 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700906}
907
Miklos Szeredia1d75f22010-07-12 14:41:40 +0200908void fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700909{
910 struct fuse_conn *fc = get_fuse_conn(inode);
911 struct fuse_inode *fi = get_fuse_inode(inode);
912
913 spin_lock(&fc->lock);
914 fi->attr_version = ++fc->attr_version;
915 if (pos > inode->i_size)
916 i_size_write(inode, pos);
917 spin_unlock(&fc->lock);
918}
919
Nick Pigginea9b9902008-04-30 00:54:42 -0700920static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
921 struct inode *inode, loff_t pos,
922 size_t count)
923{
924 size_t res;
925 unsigned offset;
926 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400927 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -0700928
929 for (i = 0; i < req->num_pages; i++)
930 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
931
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400932 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700933
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400934 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700935 count = res;
936 for (i = 0; i < req->num_pages; i++) {
937 struct page *page = req->pages[i];
938
939 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
940 SetPageUptodate(page);
941
942 if (count > PAGE_CACHE_SIZE - offset)
943 count -= PAGE_CACHE_SIZE - offset;
944 else
945 count = 0;
946 offset = 0;
947
948 unlock_page(page);
949 page_cache_release(page);
950 }
951
952 return res;
953}
954
955static ssize_t fuse_fill_write_pages(struct fuse_req *req,
956 struct address_space *mapping,
957 struct iov_iter *ii, loff_t pos)
958{
959 struct fuse_conn *fc = get_fuse_conn(mapping->host);
960 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
961 size_t count = 0;
962 int err;
963
Miklos Szeredif4975c62009-04-02 14:25:34 +0200964 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400965 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700966
967 do {
968 size_t tmp;
969 struct page *page;
970 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
971 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
972 iov_iter_count(ii));
973
974 bytes = min_t(size_t, bytes, fc->max_write - count);
975
976 again:
977 err = -EFAULT;
978 if (iov_iter_fault_in_readable(ii, bytes))
979 break;
980
981 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800982 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700983 if (!page)
984 break;
985
anfei zhou931e80e2010-02-02 13:44:02 -0800986 if (mapping_writably_mapped(mapping))
987 flush_dcache_page(page);
988
Nick Pigginea9b9902008-04-30 00:54:42 -0700989 pagefault_disable();
990 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
991 pagefault_enable();
992 flush_dcache_page(page);
993
Johannes Weiner478e0842011-07-25 22:35:35 +0200994 mark_page_accessed(page);
995
Nick Pigginea9b9902008-04-30 00:54:42 -0700996 if (!tmp) {
997 unlock_page(page);
998 page_cache_release(page);
999 bytes = min(bytes, iov_iter_single_seg_count(ii));
1000 goto again;
1001 }
1002
1003 err = 0;
1004 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001005 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001006 req->num_pages++;
1007
1008 iov_iter_advance(ii, tmp);
1009 count += tmp;
1010 pos += tmp;
1011 offset += tmp;
1012 if (offset == PAGE_CACHE_SIZE)
1013 offset = 0;
1014
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001015 if (!fc->big_writes)
1016 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001017 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001018 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001019
1020 return count > 0 ? count : err;
1021}
1022
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001023static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1024{
1025 return min_t(unsigned,
1026 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1027 (pos >> PAGE_CACHE_SHIFT) + 1,
1028 FUSE_MAX_PAGES_PER_REQ);
1029}
1030
Nick Pigginea9b9902008-04-30 00:54:42 -07001031static ssize_t fuse_perform_write(struct file *file,
1032 struct address_space *mapping,
1033 struct iov_iter *ii, loff_t pos)
1034{
1035 struct inode *inode = mapping->host;
1036 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001037 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001038 int err = 0;
1039 ssize_t res = 0;
1040
1041 if (is_bad_inode(inode))
1042 return -EIO;
1043
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001044 if (inode->i_size < pos + iov_iter_count(ii))
1045 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1046
Nick Pigginea9b9902008-04-30 00:54:42 -07001047 do {
1048 struct fuse_req *req;
1049 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001050 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001051
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001052 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001053 if (IS_ERR(req)) {
1054 err = PTR_ERR(req);
1055 break;
1056 }
1057
1058 count = fuse_fill_write_pages(req, mapping, ii, pos);
1059 if (count <= 0) {
1060 err = count;
1061 } else {
1062 size_t num_written;
1063
1064 num_written = fuse_send_write_pages(req, file, inode,
1065 pos, count);
1066 err = req->out.h.error;
1067 if (!err) {
1068 res += num_written;
1069 pos += num_written;
1070
1071 /* break out of the loop on short write */
1072 if (num_written != count)
1073 err = -EIO;
1074 }
1075 }
1076 fuse_put_request(fc, req);
1077 } while (!err && iov_iter_count(ii));
1078
1079 if (res > 0)
1080 fuse_write_update_size(inode, pos);
1081
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001082 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 fuse_invalidate_attr(inode);
1084
1085 return res > 0 ? res : err;
1086}
1087
1088static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1089 unsigned long nr_segs, loff_t pos)
1090{
1091 struct file *file = iocb->ki_filp;
1092 struct address_space *mapping = file->f_mapping;
1093 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001094 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001095 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001096 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001097 struct inode *inode = mapping->host;
1098 ssize_t err;
1099 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001100 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001101
1102 WARN_ON(iocb->ki_pos != pos);
1103
Anand Avati4273b792012-02-17 12:46:25 -05001104 ocount = 0;
1105 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001106 if (err)
1107 return err;
1108
Anand Avati4273b792012-02-17 12:46:25 -05001109 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001110 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001111
1112 /* We can write back this queue in page reclaim */
1113 current->backing_dev_info = mapping->backing_dev_info;
1114
1115 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1116 if (err)
1117 goto out;
1118
1119 if (count == 0)
1120 goto out;
1121
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001122 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001123 if (err)
1124 goto out;
1125
Josef Bacikc3b2da32012-03-26 09:59:21 -04001126 err = file_update_time(file);
1127 if (err)
1128 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001129
Anand Avati4273b792012-02-17 12:46:25 -05001130 if (file->f_flags & O_DIRECT) {
1131 written = generic_file_direct_write(iocb, iov, &nr_segs,
1132 pos, &iocb->ki_pos,
1133 count, ocount);
1134 if (written < 0 || written == count)
1135 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001136
Anand Avati4273b792012-02-17 12:46:25 -05001137 pos += written;
1138 count -= written;
1139
1140 iov_iter_init(&i, iov, nr_segs, count, written);
1141 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1142 if (written_buffered < 0) {
1143 err = written_buffered;
1144 goto out;
1145 }
1146 endbyte = pos + written_buffered - 1;
1147
1148 err = filemap_write_and_wait_range(file->f_mapping, pos,
1149 endbyte);
1150 if (err)
1151 goto out;
1152
1153 invalidate_mapping_pages(file->f_mapping,
1154 pos >> PAGE_CACHE_SHIFT,
1155 endbyte >> PAGE_CACHE_SHIFT);
1156
1157 written += written_buffered;
1158 iocb->ki_pos = pos + written_buffered;
1159 } else {
1160 iov_iter_init(&i, iov, nr_segs, count, 0);
1161 written = fuse_perform_write(file, mapping, &i, pos);
1162 if (written >= 0)
1163 iocb->ki_pos = pos + written;
1164 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001165out:
1166 current->backing_dev_info = NULL;
1167 mutex_unlock(&inode->i_mutex);
1168
1169 return written ? written : err;
1170}
1171
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001172static inline void fuse_page_descs_length_init(struct fuse_req *req,
1173 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001174{
1175 int i;
1176
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001177 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001178 req->page_descs[i].length = PAGE_SIZE -
1179 req->page_descs[i].offset;
1180}
1181
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001182static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1183{
1184 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1185}
1186
1187static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1188 size_t max_size)
1189{
1190 return min(iov_iter_single_seg_count(ii), max_size);
1191}
1192
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001193static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001194 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001195{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001196 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001197
Miklos Szeredif4975c62009-04-02 14:25:34 +02001198 /* Special case for kernel I/O: can copy directly into the buffer */
1199 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001200 unsigned long user_addr = fuse_get_user_addr(ii);
1201 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1202
Miklos Szeredif4975c62009-04-02 14:25:34 +02001203 if (write)
1204 req->in.args[1].value = (void *) user_addr;
1205 else
1206 req->out.args[0].value = (void *) user_addr;
1207
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001208 iov_iter_advance(ii, frag_size);
1209 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001210 return 0;
1211 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001212
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001213 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001214 unsigned npages;
1215 unsigned long user_addr = fuse_get_user_addr(ii);
1216 unsigned offset = user_addr & ~PAGE_MASK;
1217 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1218 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001219
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001220 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001221 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1222
1223 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1224 npages = clamp(npages, 1U, n);
1225
1226 ret = get_user_pages_fast(user_addr, npages, !write,
1227 &req->pages[req->num_pages]);
1228 if (ret < 0)
1229 return ret;
1230
1231 npages = ret;
1232 frag_size = min_t(size_t, frag_size,
1233 (npages << PAGE_SHIFT) - offset);
1234 iov_iter_advance(ii, frag_size);
1235
1236 req->page_descs[req->num_pages].offset = offset;
1237 fuse_page_descs_length_init(req, req->num_pages, npages);
1238
1239 req->num_pages += npages;
1240 req->page_descs[req->num_pages - 1].length -=
1241 (npages << PAGE_SHIFT) - offset - frag_size;
1242
1243 nbytes += frag_size;
1244 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001245
1246 if (write)
1247 req->in.argpages = 1;
1248 else
1249 req->out.argpages = 1;
1250
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001251 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001252
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001253 return 0;
1254}
1255
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001256static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1257{
1258 struct iov_iter ii = *ii_p;
1259 int npages = 0;
1260
1261 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1262 unsigned long user_addr = fuse_get_user_addr(&ii);
1263 unsigned offset = user_addr & ~PAGE_MASK;
1264 size_t frag_size = iov_iter_single_seg_count(&ii);
1265
1266 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1267 iov_iter_advance(&ii, frag_size);
1268 }
1269
1270 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1271}
1272
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001273ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001274 unsigned long nr_segs, size_t count, loff_t *ppos,
1275 int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001276{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001277 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001278 struct fuse_file *ff = file->private_data;
1279 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001280 size_t nmax = write ? fc->max_write : fc->max_read;
1281 loff_t pos = *ppos;
1282 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001283 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001284 struct iov_iter ii;
1285
1286 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001287
Brian Fosterde82b922013-05-14 11:25:39 -04001288 if (io->async)
1289 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1290 else
1291 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001292 if (IS_ERR(req))
1293 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001294
1295 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001296 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001297 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001298 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001299 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001300 if (err) {
1301 res = err;
1302 break;
1303 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001304
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001305 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001306 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001307 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001308 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001309
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001310 if (!io->async)
1311 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001312 if (req->out.h.error) {
1313 if (!res)
1314 res = req->out.h.error;
1315 break;
1316 } else if (nres > nbytes) {
1317 res = -EIO;
1318 break;
1319 }
1320 count -= nres;
1321 res += nres;
1322 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001323 if (nres != nbytes)
1324 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001325 if (count) {
1326 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001327 if (io->async)
1328 req = fuse_get_req_for_background(fc,
1329 fuse_iter_npages(&ii));
1330 else
1331 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001332 if (IS_ERR(req))
1333 break;
1334 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001335 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001336 if (!IS_ERR(req))
1337 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001338 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001339 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001340
1341 return res;
1342}
Tejun Heo08cbf542009-04-14 10:54:53 +09001343EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001344
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001345static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1346 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001347 unsigned long nr_segs, loff_t *ppos,
1348 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001349{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001350 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001351 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001352 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001353
1354 if (is_bad_inode(inode))
1355 return -EIO;
1356
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001357 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001358
1359 fuse_invalidate_attr(inode);
1360
1361 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362}
1363
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001364static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1365 size_t count, loff_t *ppos)
1366{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001367 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001368 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001369 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001370}
1371
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001372static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1373 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001374 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001375{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001376 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001377 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001378 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001379 ssize_t res;
1380
1381 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001382 if (!res)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001383 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1);
Anand Avati4273b792012-02-17 12:46:25 -05001384
1385 fuse_invalidate_attr(inode);
1386
1387 return res;
1388}
1389
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001390static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1391 size_t count, loff_t *ppos)
1392{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001393 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001394 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001395 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001396 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001397
1398 if (is_bad_inode(inode))
1399 return -EIO;
1400
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001401 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001402 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001403 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001404 if (res > 0)
1405 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001406 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001407
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001408 return res;
1409}
1410
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001411static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001412{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001413 int i;
1414
1415 for (i = 0; i < req->num_pages; i++)
1416 __free_page(req->pages[i]);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +01001417 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001418}
1419
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001420static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001421{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001422 struct inode *inode = req->inode;
1423 struct fuse_inode *fi = get_fuse_inode(inode);
1424 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001425 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001426
1427 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001428 for (i = 0; i < req->num_pages; i++) {
1429 dec_bdi_stat(bdi, BDI_WRITEBACK);
1430 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1431 bdi_writeout_inc(bdi);
1432 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001433 wake_up(&fi->page_waitq);
1434}
1435
1436/* Called under fc->lock, may release and reacquire it */
1437static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001438__releases(fc->lock)
1439__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001440{
1441 struct fuse_inode *fi = get_fuse_inode(req->inode);
1442 loff_t size = i_size_read(req->inode);
1443 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001444 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001445
1446 if (!fc->connected)
1447 goto out_free;
1448
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001449 if (inarg->offset + data_size <= size) {
1450 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001451 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001452 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001453 } else {
1454 /* Got truncated off completely */
1455 goto out_free;
1456 }
1457
1458 req->in.args[1].size = inarg->size;
1459 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001460 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001461 return;
1462
1463 out_free:
1464 fuse_writepage_finish(fc, req);
1465 spin_unlock(&fc->lock);
1466 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001467 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001468 spin_lock(&fc->lock);
1469}
1470
1471/*
1472 * If fi->writectr is positive (no truncate or fsync going on) send
1473 * all queued writepage requests.
1474 *
1475 * Called with fc->lock
1476 */
1477void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001478__releases(fc->lock)
1479__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001480{
1481 struct fuse_conn *fc = get_fuse_conn(inode);
1482 struct fuse_inode *fi = get_fuse_inode(inode);
1483 struct fuse_req *req;
1484
1485 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1486 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1487 list_del_init(&req->list);
1488 fuse_send_writepage(fc, req);
1489 }
1490}
1491
1492static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1493{
1494 struct inode *inode = req->inode;
1495 struct fuse_inode *fi = get_fuse_inode(inode);
1496
1497 mapping_set_error(inode->i_mapping, req->out.h.error);
1498 spin_lock(&fc->lock);
1499 fi->writectr--;
1500 fuse_writepage_finish(fc, req);
1501 spin_unlock(&fc->lock);
1502 fuse_writepage_free(fc, req);
1503}
1504
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001505static struct fuse_file *fuse_write_file(struct fuse_conn *fc,
1506 struct fuse_inode *fi)
1507{
1508 struct fuse_file *ff;
1509
1510 spin_lock(&fc->lock);
1511 BUG_ON(list_empty(&fi->write_files));
1512 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1513 fuse_file_get(ff);
1514 spin_unlock(&fc->lock);
1515
1516 return ff;
1517}
1518
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001519static int fuse_writepage_locked(struct page *page)
1520{
1521 struct address_space *mapping = page->mapping;
1522 struct inode *inode = mapping->host;
1523 struct fuse_conn *fc = get_fuse_conn(inode);
1524 struct fuse_inode *fi = get_fuse_inode(inode);
1525 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001526 struct page *tmp_page;
1527
1528 set_page_writeback(page);
1529
Maxim Patlasov4250c062012-10-26 19:48:07 +04001530 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001531 if (!req)
1532 goto err;
1533
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001534 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001535 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1536 if (!tmp_page)
1537 goto err_free;
1538
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001539 req->ff = fuse_write_file(fc, fi);
1540 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001541
1542 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001543 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001544 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001545 req->num_pages = 1;
1546 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001547 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001548 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001549 req->end = fuse_writepage_end;
1550 req->inode = inode;
1551
1552 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1553 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001554
1555 spin_lock(&fc->lock);
1556 list_add(&req->writepages_entry, &fi->writepages);
1557 list_add_tail(&req->list, &fi->queued_writes);
1558 fuse_flush_writepages(inode);
1559 spin_unlock(&fc->lock);
1560
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001561 end_page_writeback(page);
1562
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001563 return 0;
1564
1565err_free:
1566 fuse_request_free(req);
1567err:
1568 end_page_writeback(page);
1569 return -ENOMEM;
1570}
1571
1572static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1573{
1574 int err;
1575
1576 err = fuse_writepage_locked(page);
1577 unlock_page(page);
1578
1579 return err;
1580}
1581
1582static int fuse_launder_page(struct page *page)
1583{
1584 int err = 0;
1585 if (clear_page_dirty_for_io(page)) {
1586 struct inode *inode = page->mapping->host;
1587 err = fuse_writepage_locked(page);
1588 if (!err)
1589 fuse_wait_on_page_writeback(inode, page->index);
1590 }
1591 return err;
1592}
1593
1594/*
1595 * Write back dirty pages now, because there may not be any suitable
1596 * open files later
1597 */
1598static void fuse_vma_close(struct vm_area_struct *vma)
1599{
1600 filemap_write_and_wait(vma->vm_file->f_mapping);
1601}
1602
1603/*
1604 * Wait for writeback against this page to complete before allowing it
1605 * to be marked dirty again, and hence written back again, possibly
1606 * before the previous writepage completed.
1607 *
1608 * Block here, instead of in ->writepage(), so that the userspace fs
1609 * can only block processes actually operating on the filesystem.
1610 *
1611 * Otherwise unprivileged userspace fs would be able to block
1612 * unrelated:
1613 *
1614 * - page migration
1615 * - sync(2)
1616 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1617 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001618static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001619{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001620 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001621 /*
1622 * Don't use page->mapping as it may become NULL from a
1623 * concurrent truncate.
1624 */
1625 struct inode *inode = vma->vm_file->f_mapping->host;
1626
1627 fuse_wait_on_page_writeback(inode, page->index);
1628 return 0;
1629}
1630
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001631static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001632 .close = fuse_vma_close,
1633 .fault = filemap_fault,
1634 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001635 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636};
1637
1638static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1639{
1640 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
Al Viro6131ffa2013-02-27 16:59:05 -05001641 struct inode *inode = file_inode(file);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001642 struct fuse_conn *fc = get_fuse_conn(inode);
1643 struct fuse_inode *fi = get_fuse_inode(inode);
1644 struct fuse_file *ff = file->private_data;
1645 /*
1646 * file may be written through mmap, so chain it onto the
1647 * inodes's write_file list
1648 */
1649 spin_lock(&fc->lock);
1650 if (list_empty(&ff->write_entry))
1651 list_add(&ff->write_entry, &fi->write_files);
1652 spin_unlock(&fc->lock);
1653 }
1654 file_accessed(file);
1655 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001656 return 0;
1657}
1658
Miklos Szeredifc280c92009-04-02 14:25:35 +02001659static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1660{
1661 /* Can't provide the coherency needed for MAP_SHARED */
1662 if (vma->vm_flags & VM_MAYSHARE)
1663 return -ENODEV;
1664
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001665 invalidate_inode_pages2(file->f_mapping);
1666
Miklos Szeredifc280c92009-04-02 14:25:35 +02001667 return generic_file_mmap(file, vma);
1668}
1669
Miklos Szeredi71421252006-06-25 05:48:52 -07001670static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1671 struct file_lock *fl)
1672{
1673 switch (ffl->type) {
1674 case F_UNLCK:
1675 break;
1676
1677 case F_RDLCK:
1678 case F_WRLCK:
1679 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1680 ffl->end < ffl->start)
1681 return -EIO;
1682
1683 fl->fl_start = ffl->start;
1684 fl->fl_end = ffl->end;
1685 fl->fl_pid = ffl->pid;
1686 break;
1687
1688 default:
1689 return -EIO;
1690 }
1691 fl->fl_type = ffl->type;
1692 return 0;
1693}
1694
1695static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001696 const struct file_lock *fl, int opcode, pid_t pid,
1697 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001698{
Al Viro6131ffa2013-02-27 16:59:05 -05001699 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001700 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001701 struct fuse_file *ff = file->private_data;
1702 struct fuse_lk_in *arg = &req->misc.lk_in;
1703
1704 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001705 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001706 arg->lk.start = fl->fl_start;
1707 arg->lk.end = fl->fl_end;
1708 arg->lk.type = fl->fl_type;
1709 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001710 if (flock)
1711 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001712 req->in.h.opcode = opcode;
1713 req->in.h.nodeid = get_node_id(inode);
1714 req->in.numargs = 1;
1715 req->in.args[0].size = sizeof(*arg);
1716 req->in.args[0].value = arg;
1717}
1718
1719static int fuse_getlk(struct file *file, struct file_lock *fl)
1720{
Al Viro6131ffa2013-02-27 16:59:05 -05001721 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001722 struct fuse_conn *fc = get_fuse_conn(inode);
1723 struct fuse_req *req;
1724 struct fuse_lk_out outarg;
1725 int err;
1726
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001727 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07001728 if (IS_ERR(req))
1729 return PTR_ERR(req);
1730
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001731 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001732 req->out.numargs = 1;
1733 req->out.args[0].size = sizeof(outarg);
1734 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001735 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001736 err = req->out.h.error;
1737 fuse_put_request(fc, req);
1738 if (!err)
1739 err = convert_fuse_file_lock(&outarg.lk, fl);
1740
1741 return err;
1742}
1743
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001744static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001745{
Al Viro6131ffa2013-02-27 16:59:05 -05001746 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001747 struct fuse_conn *fc = get_fuse_conn(inode);
1748 struct fuse_req *req;
1749 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1750 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1751 int err;
1752
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001753 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07001754 /* NLM needs asynchronous locks, which we don't support yet */
1755 return -ENOLCK;
1756 }
1757
Miklos Szeredi71421252006-06-25 05:48:52 -07001758 /* Unlock on close is handled by the flush method */
1759 if (fl->fl_flags & FL_CLOSE)
1760 return 0;
1761
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001762 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07001763 if (IS_ERR(req))
1764 return PTR_ERR(req);
1765
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001766 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001767 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001768 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001769 /* locking is restartable */
1770 if (err == -EINTR)
1771 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001772 fuse_put_request(fc, req);
1773 return err;
1774}
1775
1776static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1777{
Al Viro6131ffa2013-02-27 16:59:05 -05001778 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001779 struct fuse_conn *fc = get_fuse_conn(inode);
1780 int err;
1781
Miklos Szeredi48e90762008-07-25 01:49:02 -07001782 if (cmd == F_CANCELLK) {
1783 err = 0;
1784 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001785 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001786 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001787 err = 0;
1788 } else
1789 err = fuse_getlk(file, fl);
1790 } else {
1791 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001792 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001793 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001794 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001795 }
1796 return err;
1797}
1798
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001799static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1800{
Al Viro6131ffa2013-02-27 16:59:05 -05001801 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001802 struct fuse_conn *fc = get_fuse_conn(inode);
1803 int err;
1804
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001805 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001806 err = flock_lock_file_wait(file, fl);
1807 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001808 struct fuse_file *ff = file->private_data;
1809
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001810 /* emulate flock with POSIX locks */
1811 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001812 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001813 err = fuse_setlk(file, fl, 1);
1814 }
1815
1816 return err;
1817}
1818
Miklos Szeredib2d22722006-12-06 20:35:51 -08001819static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1820{
1821 struct inode *inode = mapping->host;
1822 struct fuse_conn *fc = get_fuse_conn(inode);
1823 struct fuse_req *req;
1824 struct fuse_bmap_in inarg;
1825 struct fuse_bmap_out outarg;
1826 int err;
1827
1828 if (!inode->i_sb->s_bdev || fc->no_bmap)
1829 return 0;
1830
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001831 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001832 if (IS_ERR(req))
1833 return 0;
1834
1835 memset(&inarg, 0, sizeof(inarg));
1836 inarg.block = block;
1837 inarg.blocksize = inode->i_sb->s_blocksize;
1838 req->in.h.opcode = FUSE_BMAP;
1839 req->in.h.nodeid = get_node_id(inode);
1840 req->in.numargs = 1;
1841 req->in.args[0].size = sizeof(inarg);
1842 req->in.args[0].value = &inarg;
1843 req->out.numargs = 1;
1844 req->out.args[0].size = sizeof(outarg);
1845 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001846 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001847 err = req->out.h.error;
1848 fuse_put_request(fc, req);
1849 if (err == -ENOSYS)
1850 fc->no_bmap = 1;
1851
1852 return err ? 0 : outarg.block;
1853}
1854
Andrew Morton965c8e52012-12-17 15:59:39 -08001855static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001856{
1857 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05001858 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001859
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001860 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08001861 if (whence == SEEK_CUR || whence == SEEK_SET)
1862 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04001863
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001864 mutex_lock(&inode->i_mutex);
1865 retval = fuse_update_attributes(inode, NULL, file, NULL);
1866 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08001867 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001868 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001869
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001870 return retval;
1871}
1872
Tejun Heo59efec72008-11-26 12:03:55 +01001873static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1874 unsigned int nr_segs, size_t bytes, bool to_user)
1875{
1876 struct iov_iter ii;
1877 int page_idx = 0;
1878
1879 if (!bytes)
1880 return 0;
1881
1882 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1883
1884 while (iov_iter_count(&ii)) {
1885 struct page *page = pages[page_idx++];
1886 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001887 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001888
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001889 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001890
1891 while (todo) {
1892 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1893 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1894 size_t copy = min(todo, iov_len);
1895 size_t left;
1896
1897 if (!to_user)
1898 left = copy_from_user(kaddr, uaddr, copy);
1899 else
1900 left = copy_to_user(uaddr, kaddr, copy);
1901
1902 if (unlikely(left))
1903 return -EFAULT;
1904
1905 iov_iter_advance(&ii, copy);
1906 todo -= copy;
1907 kaddr += copy;
1908 }
1909
Jens Axboe0bd87182009-11-03 11:40:44 +01001910 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001911 }
1912
1913 return 0;
1914}
1915
1916/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001917 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1918 * ABI was defined to be 'struct iovec' which is different on 32bit
1919 * and 64bit. Fortunately we can determine which structure the server
1920 * used from the size of the reply.
1921 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001922static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1923 size_t transferred, unsigned count,
1924 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001925{
1926#ifdef CONFIG_COMPAT
1927 if (count * sizeof(struct compat_iovec) == transferred) {
1928 struct compat_iovec *ciov = src;
1929 unsigned i;
1930
1931 /*
1932 * With this interface a 32bit server cannot support
1933 * non-compat (i.e. ones coming from 64bit apps) ioctl
1934 * requests
1935 */
1936 if (!is_compat)
1937 return -EINVAL;
1938
1939 for (i = 0; i < count; i++) {
1940 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1941 dst[i].iov_len = ciov[i].iov_len;
1942 }
1943 return 0;
1944 }
1945#endif
1946
1947 if (count * sizeof(struct iovec) != transferred)
1948 return -EIO;
1949
1950 memcpy(dst, src, transferred);
1951 return 0;
1952}
1953
Miklos Szeredi75727772010-11-30 16:39:27 +01001954/* Make sure iov_length() won't overflow */
1955static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1956{
1957 size_t n;
1958 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1959
Zach Brownfb6ccff2012-07-24 12:10:11 -07001960 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01001961 if (iov->iov_len > (size_t) max)
1962 return -ENOMEM;
1963 max -= iov->iov_len;
1964 }
1965 return 0;
1966}
1967
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001968static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1969 void *src, size_t transferred, unsigned count,
1970 bool is_compat)
1971{
1972 unsigned i;
1973 struct fuse_ioctl_iovec *fiov = src;
1974
1975 if (fc->minor < 16) {
1976 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1977 count, is_compat);
1978 }
1979
1980 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1981 return -EIO;
1982
1983 for (i = 0; i < count; i++) {
1984 /* Did the server supply an inappropriate value? */
1985 if (fiov[i].base != (unsigned long) fiov[i].base ||
1986 fiov[i].len != (unsigned long) fiov[i].len)
1987 return -EIO;
1988
1989 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1990 dst[i].iov_len = (size_t) fiov[i].len;
1991
1992#ifdef CONFIG_COMPAT
1993 if (is_compat &&
1994 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1995 (compat_size_t) dst[i].iov_len != fiov[i].len))
1996 return -EIO;
1997#endif
1998 }
1999
2000 return 0;
2001}
2002
2003
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002004/*
Tejun Heo59efec72008-11-26 12:03:55 +01002005 * For ioctls, there is no generic way to determine how much memory
2006 * needs to be read and/or written. Furthermore, ioctls are allowed
2007 * to dereference the passed pointer, so the parameter requires deep
2008 * copying but FUSE has no idea whatsoever about what to copy in or
2009 * out.
2010 *
2011 * This is solved by allowing FUSE server to retry ioctl with
2012 * necessary in/out iovecs. Let's assume the ioctl implementation
2013 * needs to read in the following structure.
2014 *
2015 * struct a {
2016 * char *buf;
2017 * size_t buflen;
2018 * }
2019 *
2020 * On the first callout to FUSE server, inarg->in_size and
2021 * inarg->out_size will be NULL; then, the server completes the ioctl
2022 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2023 * the actual iov array to
2024 *
2025 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2026 *
2027 * which tells FUSE to copy in the requested area and retry the ioctl.
2028 * On the second round, the server has access to the structure and
2029 * from that it can tell what to look for next, so on the invocation,
2030 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2031 *
2032 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2033 * { .iov_base = a.buf, .iov_len = a.buflen } }
2034 *
2035 * FUSE will copy both struct a and the pointed buffer from the
2036 * process doing the ioctl and retry ioctl with both struct a and the
2037 * buffer.
2038 *
2039 * This time, FUSE server has everything it needs and completes ioctl
2040 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2041 *
2042 * Copying data out works the same way.
2043 *
2044 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2045 * automatically initializes in and out iovs by decoding @cmd with
2046 * _IOC_* macros and the server is not allowed to request RETRY. This
2047 * limits ioctl data transfers to well-formed ioctls and is the forced
2048 * behavior for all FUSE servers.
2049 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002050long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2051 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002052{
Tejun Heo59efec72008-11-26 12:03:55 +01002053 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002054 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002055 struct fuse_ioctl_in inarg = {
2056 .fh = ff->fh,
2057 .cmd = cmd,
2058 .arg = arg,
2059 .flags = flags
2060 };
2061 struct fuse_ioctl_out outarg;
2062 struct fuse_req *req = NULL;
2063 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002064 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002065 struct iovec *in_iov = NULL, *out_iov = NULL;
2066 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2067 size_t in_size, out_size, transferred;
2068 int err;
2069
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002070#if BITS_PER_LONG == 32
2071 inarg.flags |= FUSE_IOCTL_32BIT;
2072#else
2073 if (flags & FUSE_IOCTL_COMPAT)
2074 inarg.flags |= FUSE_IOCTL_32BIT;
2075#endif
2076
Tejun Heo59efec72008-11-26 12:03:55 +01002077 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002078 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002079
Tejun Heo59efec72008-11-26 12:03:55 +01002080 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002081 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002082 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002083 if (!pages || !iov_page)
2084 goto out;
2085
2086 /*
2087 * If restricted, initialize IO parameters as encoded in @cmd.
2088 * RETRY from server is not allowed.
2089 */
2090 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002091 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002092
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002093 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002094 iov->iov_len = _IOC_SIZE(cmd);
2095
2096 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2097 in_iov = iov;
2098 in_iovs = 1;
2099 }
2100
2101 if (_IOC_DIR(cmd) & _IOC_READ) {
2102 out_iov = iov;
2103 out_iovs = 1;
2104 }
2105 }
2106
2107 retry:
2108 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2109 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2110
2111 /*
2112 * Out data can be used either for actual out data or iovs,
2113 * make sure there always is at least one page.
2114 */
2115 out_size = max_t(size_t, out_size, PAGE_SIZE);
2116 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2117
2118 /* make sure there are enough buffer pages and init request with them */
2119 err = -ENOMEM;
2120 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2121 goto out;
2122 while (num_pages < max_pages) {
2123 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2124 if (!pages[num_pages])
2125 goto out;
2126 num_pages++;
2127 }
2128
Maxim Patlasov54b96672012-10-26 19:49:13 +04002129 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002130 if (IS_ERR(req)) {
2131 err = PTR_ERR(req);
2132 req = NULL;
2133 goto out;
2134 }
2135 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2136 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002137 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002138
2139 /* okay, let's send it to the client */
2140 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002141 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002142 req->in.numargs = 1;
2143 req->in.args[0].size = sizeof(inarg);
2144 req->in.args[0].value = &inarg;
2145 if (in_size) {
2146 req->in.numargs++;
2147 req->in.args[1].size = in_size;
2148 req->in.argpages = 1;
2149
2150 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2151 false);
2152 if (err)
2153 goto out;
2154 }
2155
2156 req->out.numargs = 2;
2157 req->out.args[0].size = sizeof(outarg);
2158 req->out.args[0].value = &outarg;
2159 req->out.args[1].size = out_size;
2160 req->out.argpages = 1;
2161 req->out.argvar = 1;
2162
Tejun Heob93f8582008-11-26 12:03:55 +01002163 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002164 err = req->out.h.error;
2165 transferred = req->out.args[1].size;
2166 fuse_put_request(fc, req);
2167 req = NULL;
2168 if (err)
2169 goto out;
2170
2171 /* did it ask for retry? */
2172 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002173 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002174
2175 /* no retry if in restricted mode */
2176 err = -EIO;
2177 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2178 goto out;
2179
2180 in_iovs = outarg.in_iovs;
2181 out_iovs = outarg.out_iovs;
2182
2183 /*
2184 * Make sure things are in boundary, separate checks
2185 * are to protect against overflow.
2186 */
2187 err = -ENOMEM;
2188 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2189 out_iovs > FUSE_IOCTL_MAX_IOV ||
2190 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2191 goto out;
2192
Cong Wang2408f6e2011-11-25 23:14:30 +08002193 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002194 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002195 transferred, in_iovs + out_iovs,
2196 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002197 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002198 if (err)
2199 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002200
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002201 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002202 out_iov = in_iov + in_iovs;
2203
Miklos Szeredi75727772010-11-30 16:39:27 +01002204 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2205 if (err)
2206 goto out;
2207
2208 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2209 if (err)
2210 goto out;
2211
Tejun Heo59efec72008-11-26 12:03:55 +01002212 goto retry;
2213 }
2214
2215 err = -EIO;
2216 if (transferred > inarg.out_size)
2217 goto out;
2218
2219 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2220 out:
2221 if (req)
2222 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002223 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002224 while (num_pages)
2225 __free_page(pages[--num_pages]);
2226 kfree(pages);
2227
2228 return err ? err : outarg.result;
2229}
Tejun Heo08cbf542009-04-14 10:54:53 +09002230EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002231
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002232long fuse_ioctl_common(struct file *file, unsigned int cmd,
2233 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002234{
Al Viro6131ffa2013-02-27 16:59:05 -05002235 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002236 struct fuse_conn *fc = get_fuse_conn(inode);
2237
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002238 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002239 return -EACCES;
2240
2241 if (is_bad_inode(inode))
2242 return -EIO;
2243
2244 return fuse_do_ioctl(file, cmd, arg, flags);
2245}
2246
Tejun Heo59efec72008-11-26 12:03:55 +01002247static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2248 unsigned long arg)
2249{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002250 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002251}
2252
2253static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2254 unsigned long arg)
2255{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002256 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002257}
2258
Tejun Heo95668a62008-11-26 12:03:55 +01002259/*
2260 * All files which have been polled are linked to RB tree
2261 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2262 * find the matching one.
2263 */
2264static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2265 struct rb_node **parent_out)
2266{
2267 struct rb_node **link = &fc->polled_files.rb_node;
2268 struct rb_node *last = NULL;
2269
2270 while (*link) {
2271 struct fuse_file *ff;
2272
2273 last = *link;
2274 ff = rb_entry(last, struct fuse_file, polled_node);
2275
2276 if (kh < ff->kh)
2277 link = &last->rb_left;
2278 else if (kh > ff->kh)
2279 link = &last->rb_right;
2280 else
2281 return link;
2282 }
2283
2284 if (parent_out)
2285 *parent_out = last;
2286 return link;
2287}
2288
2289/*
2290 * The file is about to be polled. Make sure it's on the polled_files
2291 * RB tree. Note that files once added to the polled_files tree are
2292 * not removed before the file is released. This is because a file
2293 * polled once is likely to be polled again.
2294 */
2295static void fuse_register_polled_file(struct fuse_conn *fc,
2296 struct fuse_file *ff)
2297{
2298 spin_lock(&fc->lock);
2299 if (RB_EMPTY_NODE(&ff->polled_node)) {
2300 struct rb_node **link, *parent;
2301
2302 link = fuse_find_polled_node(fc, ff->kh, &parent);
2303 BUG_ON(*link);
2304 rb_link_node(&ff->polled_node, parent, link);
2305 rb_insert_color(&ff->polled_node, &fc->polled_files);
2306 }
2307 spin_unlock(&fc->lock);
2308}
2309
Tejun Heo08cbf542009-04-14 10:54:53 +09002310unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002311{
Tejun Heo95668a62008-11-26 12:03:55 +01002312 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002313 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002314 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2315 struct fuse_poll_out outarg;
2316 struct fuse_req *req;
2317 int err;
2318
2319 if (fc->no_poll)
2320 return DEFAULT_POLLMASK;
2321
2322 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002323 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002324
2325 /*
2326 * Ask for notification iff there's someone waiting for it.
2327 * The client may ignore the flag and always notify.
2328 */
2329 if (waitqueue_active(&ff->poll_wait)) {
2330 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2331 fuse_register_polled_file(fc, ff);
2332 }
2333
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002334 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002335 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002336 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002337
2338 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002339 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002340 req->in.numargs = 1;
2341 req->in.args[0].size = sizeof(inarg);
2342 req->in.args[0].value = &inarg;
2343 req->out.numargs = 1;
2344 req->out.args[0].size = sizeof(outarg);
2345 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002346 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002347 err = req->out.h.error;
2348 fuse_put_request(fc, req);
2349
2350 if (!err)
2351 return outarg.revents;
2352 if (err == -ENOSYS) {
2353 fc->no_poll = 1;
2354 return DEFAULT_POLLMASK;
2355 }
2356 return POLLERR;
2357}
Tejun Heo08cbf542009-04-14 10:54:53 +09002358EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002359
2360/*
2361 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2362 * wakes up the poll waiters.
2363 */
2364int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2365 struct fuse_notify_poll_wakeup_out *outarg)
2366{
2367 u64 kh = outarg->kh;
2368 struct rb_node **link;
2369
2370 spin_lock(&fc->lock);
2371
2372 link = fuse_find_polled_node(fc, kh, NULL);
2373 if (*link) {
2374 struct fuse_file *ff;
2375
2376 ff = rb_entry(*link, struct fuse_file, polled_node);
2377 wake_up_interruptible_sync(&ff->poll_wait);
2378 }
2379
2380 spin_unlock(&fc->lock);
2381 return 0;
2382}
2383
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002384static void fuse_do_truncate(struct file *file)
2385{
2386 struct inode *inode = file->f_mapping->host;
2387 struct iattr attr;
2388
2389 attr.ia_valid = ATTR_SIZE;
2390 attr.ia_size = i_size_read(inode);
2391
2392 attr.ia_file = file;
2393 attr.ia_valid |= ATTR_FILE;
2394
2395 fuse_do_setattr(inode, &attr, file);
2396}
2397
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002398static inline loff_t fuse_round_up(loff_t off)
2399{
2400 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2401}
2402
Anand Avati4273b792012-02-17 12:46:25 -05002403static ssize_t
2404fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2405 loff_t offset, unsigned long nr_segs)
2406{
2407 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002408 struct file *file = iocb->ki_filp;
2409 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002410 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002411 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002412 struct inode *inode;
2413 loff_t i_size;
2414 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002415 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002416
Anand Avati4273b792012-02-17 12:46:25 -05002417 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002418 inode = file->f_mapping->host;
2419 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002420
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002421 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002422 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002423 if (offset >= i_size)
2424 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002425 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002426 }
2427
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002428 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002429 if (!io)
2430 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002431 spin_lock_init(&io->lock);
2432 io->reqs = 1;
2433 io->bytes = -1;
2434 io->size = 0;
2435 io->offset = offset;
2436 io->write = (rw == WRITE);
2437 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002438 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002439 /*
2440 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002441 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002442 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002443 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002444 io->iocb = iocb;
2445
2446 /*
2447 * We cannot asynchronously extend the size of a file. We have no method
2448 * to wait on real async I/O requests, so we must submit this request
2449 * synchronously.
2450 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002451 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002452 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002453
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002454 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002455 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002456 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002457 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002458
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002459 if (io->async) {
2460 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2461
2462 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002463 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002464 return -EIOCBQUEUED;
2465
2466 ret = wait_on_sync_kiocb(iocb);
2467 } else {
2468 kfree(io);
2469 }
2470
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002471 if (rw == WRITE) {
2472 if (ret > 0)
2473 fuse_write_update_size(inode, pos);
2474 else if (ret < 0 && offset + count > i_size)
2475 fuse_do_truncate(file);
2476 }
Anand Avati4273b792012-02-17 12:46:25 -05002477
2478 return ret;
2479}
2480
Miklos Szeredicdadb112012-11-10 16:55:56 +01002481static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2482 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002483{
2484 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002485 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002486 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002487 struct fuse_conn *fc = ff->fc;
2488 struct fuse_req *req;
2489 struct fuse_fallocate_in inarg = {
2490 .fh = ff->fh,
2491 .offset = offset,
2492 .length = length,
2493 .mode = mode
2494 };
2495 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002496 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2497 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002498
Miklos Szeredi519c6042012-04-26 10:56:36 +02002499 if (fc->no_fallocate)
2500 return -EOPNOTSUPP;
2501
Maxim Patlasov14c14412013-06-13 12:16:39 +04002502 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002503 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002504 if (mode & FALLOC_FL_PUNCH_HOLE) {
2505 loff_t endbyte = offset + length - 1;
2506 err = filemap_write_and_wait_range(inode->i_mapping,
2507 offset, endbyte);
2508 if (err)
2509 goto out;
2510
2511 fuse_sync_writes(inode);
2512 }
Brian Foster3634a632013-05-17 09:30:32 -04002513 }
2514
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002515 if (!(mode & FALLOC_FL_KEEP_SIZE))
2516 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2517
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002518 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04002519 if (IS_ERR(req)) {
2520 err = PTR_ERR(req);
2521 goto out;
2522 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002523
2524 req->in.h.opcode = FUSE_FALLOCATE;
2525 req->in.h.nodeid = ff->nodeid;
2526 req->in.numargs = 1;
2527 req->in.args[0].size = sizeof(inarg);
2528 req->in.args[0].value = &inarg;
2529 fuse_request_send(fc, req);
2530 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002531 if (err == -ENOSYS) {
2532 fc->no_fallocate = 1;
2533 err = -EOPNOTSUPP;
2534 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002535 fuse_put_request(fc, req);
2536
Brian Fosterbee6c302013-05-17 15:27:34 -04002537 if (err)
2538 goto out;
2539
2540 /* we could have extended the file */
2541 if (!(mode & FALLOC_FL_KEEP_SIZE))
2542 fuse_write_update_size(inode, offset + length);
2543
2544 if (mode & FALLOC_FL_PUNCH_HOLE)
2545 truncate_pagecache_range(inode, offset, offset + length - 1);
2546
2547 fuse_invalidate_attr(inode);
2548
Brian Foster3634a632013-05-17 09:30:32 -04002549out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002550 if (!(mode & FALLOC_FL_KEEP_SIZE))
2551 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2552
Maxim Patlasovbde52782013-09-13 19:19:54 +04002553 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04002554 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04002555
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002556 return err;
2557}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002558
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002559static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002560 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002561 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002562 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002563 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002564 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002565 .mmap = fuse_file_mmap,
2566 .open = fuse_open,
2567 .flush = fuse_flush,
2568 .release = fuse_release,
2569 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002570 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002571 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002572 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002573 .unlocked_ioctl = fuse_file_ioctl,
2574 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002575 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002576 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002577};
2578
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002579static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002580 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002581 .read = fuse_direct_read,
2582 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002583 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002584 .open = fuse_open,
2585 .flush = fuse_flush,
2586 .release = fuse_release,
2587 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002588 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002589 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002590 .unlocked_ioctl = fuse_file_ioctl,
2591 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002592 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002593 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002594 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002595};
2596
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002597static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002598 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002599 .writepage = fuse_writepage,
2600 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002601 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002602 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002603 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05002604 .direct_IO = fuse_direct_IO,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002605};
2606
2607void fuse_init_file_inode(struct inode *inode)
2608{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002609 inode->i_fop = &fuse_file_operations;
2610 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002611}