blob: d93f2a1aa7de563a22d743ed58ba2b6432cef618 [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
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100130 if (ff->fc->no_open) {
131 /*
132 * Drop the release request when client does not
133 * implement 'open'
134 */
135 req->background = 0;
136 path_put(&req->misc.release.path);
137 fuse_put_request(ff->fc, req);
138 } else if (sync) {
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400139 req->background = 0;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100140 fuse_request_send(ff->fc, req);
141 path_put(&req->misc.release.path);
142 fuse_put_request(ff->fc, req);
143 } else {
144 req->end = fuse_release_end;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400145 req->background = 1;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100146 fuse_request_send_background(ff->fc, req);
147 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700148 kfree(ff);
149 }
150}
151
Tejun Heo08cbf542009-04-14 10:54:53 +0900152int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
153 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200155 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200156 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
157
158 ff = fuse_file_alloc(fc);
159 if (!ff)
160 return -ENOMEM;
161
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100162 ff->fh = 0;
163 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
164 if (!fc->no_open || isdir) {
165 struct fuse_open_out outarg;
166 int err;
167
168 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
169 if (!err) {
170 ff->fh = outarg.fh;
171 ff->open_flags = outarg.open_flags;
172
173 } else if (err != -ENOSYS || isdir) {
174 fuse_file_free(ff);
175 return err;
176 } else {
177 fc->no_open = 1;
178 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200179 }
180
181 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100182 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200183
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200184 ff->nodeid = nodeid;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200185 file->private_data = fuse_file_get(ff);
186
187 return 0;
188}
Tejun Heo08cbf542009-04-14 10:54:53 +0900189EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200190
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400191static void fuse_link_write_file(struct file *file)
192{
193 struct inode *inode = file_inode(file);
194 struct fuse_conn *fc = get_fuse_conn(inode);
195 struct fuse_inode *fi = get_fuse_inode(inode);
196 struct fuse_file *ff = file->private_data;
197 /*
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
200 */
201 spin_lock(&fc->lock);
202 if (list_empty(&ff->write_entry))
203 list_add(&ff->write_entry, &fi->write_files);
204 spin_unlock(&fc->lock);
205}
206
Miklos Szeredic7b71432009-04-28 16:56:37 +0200207void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800208{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200209 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800210 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200211
212 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800213 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200214 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700215 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200216 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200217 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800218 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
219 struct fuse_inode *fi = get_fuse_inode(inode);
220
221 spin_lock(&fc->lock);
222 fi->attr_version = ++fc->attr_version;
223 i_size_write(inode, 0);
224 spin_unlock(&fc->lock);
225 fuse_invalidate_attr(inode);
226 }
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800227}
228
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200229int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800230{
Tejun Heoacf99432008-11-26 12:03:55 +0100231 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700232 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700233
234 err = generic_file_open(inode, file);
235 if (err)
236 return err;
237
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200238 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800239 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200240 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700241
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200242 fuse_finish_open(inode, file);
243
244 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700245}
246
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200247static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800248{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200249 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700250 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800251 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700252
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200253 spin_lock(&fc->lock);
254 list_del(&ff->write_entry);
255 if (!RB_EMPTY_NODE(&ff->polled_node))
256 rb_erase(&ff->polled_node, &fc->polled_files);
257 spin_unlock(&fc->lock);
258
Bryan Green357ccf22011-03-01 16:43:52 -0800259 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200260
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700261 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800262 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700263 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200264 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700265 req->in.numargs = 1;
266 req->in.args[0].size = sizeof(struct fuse_release_in);
267 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800268}
269
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200270void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800271{
Tejun Heo6b2db282009-04-14 10:54:49 +0900272 struct fuse_file *ff;
273 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700274
Tejun Heo6b2db282009-04-14 10:54:49 +0900275 ff = file->private_data;
276 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200277 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700278
Tejun Heo6b2db282009-04-14 10:54:49 +0900279 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200280 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100281
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200282 if (ff->flock) {
283 struct fuse_release_in *inarg = &req->misc.release.in;
284 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
285 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
286 (fl_owner_t) file);
287 }
Tejun Heo6b2db282009-04-14 10:54:49 +0900288 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200289 path_get(&file->f_path);
290 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700291
Tejun Heo6b2db282009-04-14 10:54:49 +0900292 /*
293 * Normally this will send the RELEASE request, however if
294 * some asynchronous READ or WRITE requests are outstanding,
295 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100296 *
297 * Make the release synchronous if this is a fuseblk mount,
298 * synchronous RELEASE is allowed (and desirable) in this case
299 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900300 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100301 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700302}
303
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700304static int fuse_open(struct inode *inode, struct file *file)
305{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200306 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700307}
308
309static int fuse_release(struct inode *inode, struct file *file)
310{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400311 struct fuse_conn *fc = get_fuse_conn(inode);
312
313 /* see fuse_vma_close() for !writeback_cache case */
314 if (fc->writeback_cache)
315 filemap_write_and_wait(file->f_mapping);
316
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400317 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state))
318 fuse_flush_mtime(file, true);
319
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200320 fuse_release_common(file, FUSE_RELEASE);
321
322 /* return value is ignored by VFS */
323 return 0;
324}
325
326void fuse_sync_release(struct fuse_file *ff, int flags)
327{
328 WARN_ON(atomic_read(&ff->count) > 1);
329 fuse_prepare_release(ff, flags, FUSE_RELEASE);
330 ff->reserved_req->force = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400331 ff->reserved_req->background = 0;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200332 fuse_request_send(ff->fc, ff->reserved_req);
333 fuse_put_request(ff->fc, ff->reserved_req);
334 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700335}
Tejun Heo08cbf542009-04-14 10:54:53 +0900336EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700337
Miklos Szeredi71421252006-06-25 05:48:52 -0700338/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700339 * Scramble the ID space with XTEA, so that the value of the files_struct
340 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700341 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700342u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700343{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700344 u32 *k = fc->scramble_key;
345 u64 v = (unsigned long) id;
346 u32 v0 = v;
347 u32 v1 = v >> 32;
348 u32 sum = 0;
349 int i;
350
351 for (i = 0; i < 32; i++) {
352 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
353 sum += 0x9E3779B9;
354 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
355 }
356
357 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700358}
359
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700360/*
361 * Check if page is under writeback
362 *
363 * This is currently done by walking the list of writepage requests
364 * for the inode, which can be pretty inefficient.
365 */
366static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
367{
368 struct fuse_conn *fc = get_fuse_conn(inode);
369 struct fuse_inode *fi = get_fuse_inode(inode);
370 struct fuse_req *req;
371 bool found = false;
372
373 spin_lock(&fc->lock);
374 list_for_each_entry(req, &fi->writepages, writepages_entry) {
375 pgoff_t curr_index;
376
377 BUG_ON(req->inode != inode);
378 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
Pavel Emelyanov385b1262013-06-29 21:42:48 +0400379 if (curr_index <= index &&
380 index < curr_index + req->num_pages) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700381 found = true;
382 break;
383 }
384 }
385 spin_unlock(&fc->lock);
386
387 return found;
388}
389
390/*
391 * Wait for page writeback to be completed.
392 *
393 * Since fuse doesn't rely on the VM writeback tracking, this has to
394 * use some other means.
395 */
396static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
397{
398 struct fuse_inode *fi = get_fuse_inode(inode);
399
400 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
401 return 0;
402}
403
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400404/*
405 * Wait for all pending writepages on the inode to finish.
406 *
407 * This is currently done by blocking further writes with FUSE_NOWRITE
408 * and waiting for all sent writes to complete.
409 *
410 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
411 * could conflict with truncation.
412 */
413static void fuse_sync_writes(struct inode *inode)
414{
415 fuse_set_nowrite(inode);
416 fuse_release_nowrite(inode);
417}
418
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700419static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700420{
Al Viro6131ffa2013-02-27 16:59:05 -0500421 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700422 struct fuse_conn *fc = get_fuse_conn(inode);
423 struct fuse_file *ff = file->private_data;
424 struct fuse_req *req;
425 struct fuse_flush_in inarg;
426 int err;
427
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800428 if (is_bad_inode(inode))
429 return -EIO;
430
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700431 if (fc->no_flush)
432 return 0;
433
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400434 err = filemap_write_and_wait(file->f_mapping);
435 if (err)
436 return err;
437
438 mutex_lock(&inode->i_mutex);
439 fuse_sync_writes(inode);
440 mutex_unlock(&inode->i_mutex);
441
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400442 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700443 memset(&inarg, 0, sizeof(inarg));
444 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700445 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700446 req->in.h.opcode = FUSE_FLUSH;
447 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 req->in.numargs = 1;
449 req->in.args[0].size = sizeof(inarg);
450 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700451 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100452 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700453 err = req->out.h.error;
454 fuse_put_request(fc, req);
455 if (err == -ENOSYS) {
456 fc->no_flush = 1;
457 err = 0;
458 }
459 return err;
460}
461
Josef Bacik02c24a82011-07-16 20:44:56 -0400462int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
463 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700464{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200465 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700466 struct fuse_conn *fc = get_fuse_conn(inode);
467 struct fuse_file *ff = file->private_data;
468 struct fuse_req *req;
469 struct fuse_fsync_in inarg;
470 int err;
471
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800472 if (is_bad_inode(inode))
473 return -EIO;
474
Josef Bacik02c24a82011-07-16 20:44:56 -0400475 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
476 if (err)
477 return err;
478
Miklos Szeredi82547982005-09-09 13:10:38 -0700479 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700480 return 0;
481
Josef Bacik02c24a82011-07-16 20:44:56 -0400482 mutex_lock(&inode->i_mutex);
483
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700484 /*
485 * Start writeback against all dirty pages of the inode, then
486 * wait for all outstanding writes, before sending the FSYNC
487 * request.
488 */
489 err = write_inode_now(inode, 0);
490 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400491 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700492
493 fuse_sync_writes(inode);
494
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400495 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
496 int err = fuse_flush_mtime(file, false);
497 if (err)
498 goto out;
499 }
500
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400501 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400502 if (IS_ERR(req)) {
503 err = PTR_ERR(req);
504 goto out;
505 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700506
507 memset(&inarg, 0, sizeof(inarg));
508 inarg.fh = ff->fh;
509 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700510 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700511 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700512 req->in.numargs = 1;
513 req->in.args[0].size = sizeof(inarg);
514 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100515 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516 err = req->out.h.error;
517 fuse_put_request(fc, req);
518 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700519 if (isdir)
520 fc->no_fsyncdir = 1;
521 else
522 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700523 err = 0;
524 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400525out:
526 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700527 return err;
528}
529
Josef Bacik02c24a82011-07-16 20:44:56 -0400530static int fuse_fsync(struct file *file, loff_t start, loff_t end,
531 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700532{
Josef Bacik02c24a82011-07-16 20:44:56 -0400533 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700534}
535
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200536void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
537 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700538{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700539 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800540 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700541
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800542 inarg->fh = ff->fh;
543 inarg->offset = pos;
544 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800545 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800546 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200547 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700548 req->in.numargs = 1;
549 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800550 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700551 req->out.argvar = 1;
552 req->out.numargs = 1;
553 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700554}
555
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400556static void fuse_release_user_pages(struct fuse_req *req, int write)
557{
558 unsigned i;
559
560 for (i = 0; i < req->num_pages; i++) {
561 struct page *page = req->pages[i];
562 if (write)
563 set_page_dirty_lock(page);
564 put_page(page);
565 }
566}
567
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400568/**
569 * In case of short read, the caller sets 'pos' to the position of
570 * actual end of fuse request in IO request. Otherwise, if bytes_requested
571 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
572 *
573 * An example:
574 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
575 * both submitted asynchronously. The first of them was ACKed by userspace as
576 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
577 * second request was ACKed as short, e.g. only 1K was read, resulting in
578 * pos == 33K.
579 *
580 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
581 * will be equal to the length of the longest contiguous fragment of
582 * transferred data starting from the beginning of IO request.
583 */
584static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
585{
586 int left;
587
588 spin_lock(&io->lock);
589 if (err)
590 io->err = io->err ? : err;
591 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
592 io->bytes = pos;
593
594 left = --io->reqs;
595 spin_unlock(&io->lock);
596
597 if (!left) {
598 long res;
599
600 if (io->err)
601 res = io->err;
602 else if (io->bytes >= 0 && io->write)
603 res = -EIO;
604 else {
605 res = io->bytes < 0 ? io->size : io->bytes;
606
607 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400608 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400609 struct fuse_conn *fc = get_fuse_conn(inode);
610 struct fuse_inode *fi = get_fuse_inode(inode);
611
612 spin_lock(&fc->lock);
613 fi->attr_version = ++fc->attr_version;
614 spin_unlock(&fc->lock);
615 }
616 }
617
618 aio_complete(io->iocb, res, 0);
619 kfree(io);
620 }
621}
622
623static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
624{
625 struct fuse_io_priv *io = req->io;
626 ssize_t pos = -1;
627
628 fuse_release_user_pages(req, !io->write);
629
630 if (io->write) {
631 if (req->misc.write.in.size != req->misc.write.out.size)
632 pos = req->misc.write.in.offset - io->offset +
633 req->misc.write.out.size;
634 } else {
635 if (req->misc.read.in.size != req->out.args[0].size)
636 pos = req->misc.read.in.offset - io->offset +
637 req->out.args[0].size;
638 }
639
640 fuse_aio_complete(io, req->out.h.error, pos);
641}
642
643static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
644 size_t num_bytes, struct fuse_io_priv *io)
645{
646 spin_lock(&io->lock);
647 io->size += num_bytes;
648 io->reqs++;
649 spin_unlock(&io->lock);
650
651 req->io = io;
652 req->end = fuse_aio_complete_req;
653
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400654 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400655 fuse_request_send_background(fc, req);
656
657 return num_bytes;
658}
659
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400660static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200661 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700662{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400663 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200664 struct fuse_file *ff = file->private_data;
665 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700666
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200667 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700668 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700669 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700670
671 inarg->read_flags |= FUSE_READ_LOCKOWNER;
672 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
673 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400674
675 if (io->async)
676 return fuse_async_req_send(fc, req, count, io);
677
Tejun Heob93f8582008-11-26 12:03:55 +0100678 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800679 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700680}
681
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700682static void fuse_read_update_size(struct inode *inode, loff_t size,
683 u64 attr_ver)
684{
685 struct fuse_conn *fc = get_fuse_conn(inode);
686 struct fuse_inode *fi = get_fuse_inode(inode);
687
688 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400689 if (attr_ver == fi->attr_version && size < inode->i_size &&
690 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700691 fi->attr_version = ++fc->attr_version;
692 i_size_write(inode, size);
693 }
694 spin_unlock(&fc->lock);
695}
696
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400697static void fuse_short_read(struct fuse_req *req, struct inode *inode,
698 u64 attr_ver)
699{
700 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400701 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400702
Pavel Emelyanov83732002013-10-10 17:10:46 +0400703 if (fc->writeback_cache) {
704 /*
705 * A hole in a file. Some data after the hole are in page cache,
706 * but have not reached the client fs yet. So, the hole is not
707 * present there.
708 */
709 int i;
710 int start_idx = num_read >> PAGE_CACHE_SHIFT;
711 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
712
713 for (i = start_idx; i < req->num_pages; i++) {
714 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
715 off = 0;
716 }
717 } else {
718 loff_t pos = page_offset(req->pages[0]) + num_read;
719 fuse_read_update_size(inode, pos, attr_ver);
720 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400721}
722
Maxim Patlasov482fce52013-10-10 17:11:25 +0400723static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700724{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400725 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700726 struct inode *inode = page->mapping->host;
727 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800728 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700729 size_t num_read;
730 loff_t pos = page_offset(page);
731 size_t count = PAGE_CACHE_SIZE;
732 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800733 int err;
734
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700735 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300736 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700737 * page-cache page, so make sure we read a properly synced
738 * page.
739 */
740 fuse_wait_on_page_writeback(inode, page->index);
741
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400742 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700743 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400744 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700746 attr_ver = fuse_get_attr_version(fc);
747
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700748 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200749 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700750 req->num_pages = 1;
751 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400752 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400753 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700754 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700755
756 if (!err) {
757 /*
758 * Short read means EOF. If file size is larger, truncate it
759 */
760 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400761 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700762
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700763 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700764 }
765
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400766 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400767
768 return err;
769}
770
771static int fuse_readpage(struct file *file, struct page *page)
772{
773 struct inode *inode = page->mapping->host;
774 int err;
775
776 err = -EIO;
777 if (is_bad_inode(inode))
778 goto out;
779
780 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800781 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700782 out:
783 unlock_page(page);
784 return err;
785}
786
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800787static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700788{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800789 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700790 size_t count = req->misc.read.in.size;
791 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200792 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800793
Miklos Szeredice534fb2010-05-25 15:06:07 +0200794 for (i = 0; mapping == NULL && i < req->num_pages; i++)
795 mapping = req->pages[i]->mapping;
796
797 if (mapping) {
798 struct inode *inode = mapping->host;
799
800 /*
801 * Short read means EOF. If file size is larger, truncate it
802 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400803 if (!req->out.h.error && num_read < count)
804 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200805
Andrew Gallagher451418f2013-11-05 03:55:43 -0800806 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700807 }
808
Miklos Szeredidb50b962005-09-09 13:10:33 -0700809 for (i = 0; i < req->num_pages; i++) {
810 struct page *page = req->pages[i];
811 if (!req->out.h.error)
812 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800813 else
814 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700815 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200816 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700817 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700818 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100819 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800820}
821
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200822static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800823{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200824 struct fuse_file *ff = file->private_data;
825 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800826 loff_t pos = page_offset(req->pages[0]);
827 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200828
829 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800830 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200831 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200832 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700833 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800834 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700835 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800836 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100837 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800838 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100839 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800840 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100841 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800842 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700843}
844
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700845struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700846 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800847 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700848 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400849 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700850};
851
852static int fuse_readpages_fill(void *_data, struct page *page)
853{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700854 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700855 struct fuse_req *req = data->req;
856 struct inode *inode = data->inode;
857 struct fuse_conn *fc = get_fuse_conn(inode);
858
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700859 fuse_wait_on_page_writeback(inode, page->index);
860
Miklos Szeredidb50b962005-09-09 13:10:33 -0700861 if (req->num_pages &&
862 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
863 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
864 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400865 int nr_alloc = min_t(unsigned, data->nr_pages,
866 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200867 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400868 if (fc->async_read)
869 req = fuse_get_req_for_background(fc, nr_alloc);
870 else
871 req = fuse_get_req(fc, nr_alloc);
872
873 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700874 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700875 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700876 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700877 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400879
880 if (WARN_ON(req->num_pages >= req->max_pages)) {
881 fuse_put_request(fc, req);
882 return -EIO;
883 }
884
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200885 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700886 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400887 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100888 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400889 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 return 0;
891}
892
893static int fuse_readpages(struct file *file, struct address_space *mapping,
894 struct list_head *pages, unsigned nr_pages)
895{
896 struct inode *inode = mapping->host;
897 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700898 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700899 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400900 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800901
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700902 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800903 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800904 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800905
Miklos Szeredia6643092007-11-28 16:22:00 -0800906 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400908 if (fc->async_read)
909 data.req = fuse_get_req_for_background(fc, nr_alloc);
910 else
911 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400912 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700913 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700914 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800915 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700916
917 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700918 if (!err) {
919 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200920 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700921 else
922 fuse_put_request(fc, data.req);
923 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800924out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700925 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700926}
927
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800928static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
929 unsigned long nr_segs, loff_t pos)
930{
931 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400932 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800933
Brian Fostera8894272012-07-16 15:23:50 -0400934 /*
935 * In auto invalidate mode, always update attributes on read.
936 * Otherwise, only update if we attempt to read past EOF (to ensure
937 * i_size is up to date).
938 */
939 if (fc->auto_inval_data ||
940 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800941 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800942 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
943 if (err)
944 return err;
945 }
946
947 return generic_file_aio_read(iocb, iov, nr_segs, pos);
948}
949
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200950static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200951 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700952{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700953 struct fuse_write_in *inarg = &req->misc.write.in;
954 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700955
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700956 inarg->fh = ff->fh;
957 inarg->offset = pos;
958 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700959 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200960 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700961 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200962 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700963 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
964 else
965 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700966 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700967 req->in.args[1].size = count;
968 req->out.numargs = 1;
969 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700970 req->out.args[0].value = outarg;
971}
972
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400973static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200974 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700975{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400976 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200977 struct fuse_file *ff = file->private_data;
978 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200979 struct fuse_write_in *inarg = &req->misc.write.in;
980
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200981 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200982 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700983 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700984 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
985 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
986 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400987
988 if (io->async)
989 return fuse_async_req_send(fc, req, count, io);
990
Tejun Heob93f8582008-11-26 12:03:55 +0100991 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700992 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700993}
994
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400995bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700996{
997 struct fuse_conn *fc = get_fuse_conn(inode);
998 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400999 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001000
1001 spin_lock(&fc->lock);
1002 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001005 ret = true;
1006 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001007 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001008
1009 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001010}
1011
Nick Pigginea9b9902008-04-30 00:54:42 -07001012static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1013 struct inode *inode, loff_t pos,
1014 size_t count)
1015{
1016 size_t res;
1017 unsigned offset;
1018 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001019 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -07001020
1021 for (i = 0; i < req->num_pages; i++)
1022 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1023
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001024 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001025
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001026 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001027 count = res;
1028 for (i = 0; i < req->num_pages; i++) {
1029 struct page *page = req->pages[i];
1030
1031 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1032 SetPageUptodate(page);
1033
1034 if (count > PAGE_CACHE_SIZE - offset)
1035 count -= PAGE_CACHE_SIZE - offset;
1036 else
1037 count = 0;
1038 offset = 0;
1039
1040 unlock_page(page);
1041 page_cache_release(page);
1042 }
1043
1044 return res;
1045}
1046
1047static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1048 struct address_space *mapping,
1049 struct iov_iter *ii, loff_t pos)
1050{
1051 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1052 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1053 size_t count = 0;
1054 int err;
1055
Miklos Szeredif4975c62009-04-02 14:25:34 +02001056 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001057 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001058
1059 do {
1060 size_t tmp;
1061 struct page *page;
1062 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1063 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1064 iov_iter_count(ii));
1065
1066 bytes = min_t(size_t, bytes, fc->max_write - count);
1067
1068 again:
1069 err = -EFAULT;
1070 if (iov_iter_fault_in_readable(ii, bytes))
1071 break;
1072
1073 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001074 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001075 if (!page)
1076 break;
1077
anfei zhou931e80e2010-02-02 13:44:02 -08001078 if (mapping_writably_mapped(mapping))
1079 flush_dcache_page(page);
1080
Nick Pigginea9b9902008-04-30 00:54:42 -07001081 pagefault_disable();
1082 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1083 pagefault_enable();
1084 flush_dcache_page(page);
1085
Johannes Weiner478e0842011-07-25 22:35:35 +02001086 mark_page_accessed(page);
1087
Nick Pigginea9b9902008-04-30 00:54:42 -07001088 if (!tmp) {
1089 unlock_page(page);
1090 page_cache_release(page);
1091 bytes = min(bytes, iov_iter_single_seg_count(ii));
1092 goto again;
1093 }
1094
1095 err = 0;
1096 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001097 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001098 req->num_pages++;
1099
1100 iov_iter_advance(ii, tmp);
1101 count += tmp;
1102 pos += tmp;
1103 offset += tmp;
1104 if (offset == PAGE_CACHE_SIZE)
1105 offset = 0;
1106
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001107 if (!fc->big_writes)
1108 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001109 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001110 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001111
1112 return count > 0 ? count : err;
1113}
1114
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001115static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1116{
1117 return min_t(unsigned,
1118 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1119 (pos >> PAGE_CACHE_SHIFT) + 1,
1120 FUSE_MAX_PAGES_PER_REQ);
1121}
1122
Nick Pigginea9b9902008-04-30 00:54:42 -07001123static ssize_t fuse_perform_write(struct file *file,
1124 struct address_space *mapping,
1125 struct iov_iter *ii, loff_t pos)
1126{
1127 struct inode *inode = mapping->host;
1128 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001129 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001130 int err = 0;
1131 ssize_t res = 0;
1132
1133 if (is_bad_inode(inode))
1134 return -EIO;
1135
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001136 if (inode->i_size < pos + iov_iter_count(ii))
1137 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1138
Nick Pigginea9b9902008-04-30 00:54:42 -07001139 do {
1140 struct fuse_req *req;
1141 ssize_t count;
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001142 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001143
Maxim Patlasovd07f09f52012-10-26 19:49:00 +04001144 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001145 if (IS_ERR(req)) {
1146 err = PTR_ERR(req);
1147 break;
1148 }
1149
1150 count = fuse_fill_write_pages(req, mapping, ii, pos);
1151 if (count <= 0) {
1152 err = count;
1153 } else {
1154 size_t num_written;
1155
1156 num_written = fuse_send_write_pages(req, file, inode,
1157 pos, count);
1158 err = req->out.h.error;
1159 if (!err) {
1160 res += num_written;
1161 pos += num_written;
1162
1163 /* break out of the loop on short write */
1164 if (num_written != count)
1165 err = -EIO;
1166 }
1167 }
1168 fuse_put_request(fc, req);
1169 } while (!err && iov_iter_count(ii));
1170
1171 if (res > 0)
1172 fuse_write_update_size(inode, pos);
1173
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001174 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001175 fuse_invalidate_attr(inode);
1176
1177 return res > 0 ? res : err;
1178}
1179
1180static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1181 unsigned long nr_segs, loff_t pos)
1182{
1183 struct file *file = iocb->ki_filp;
1184 struct address_space *mapping = file->f_mapping;
1185 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001186 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001187 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001188 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001189 struct inode *inode = mapping->host;
1190 ssize_t err;
1191 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001192 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001193
1194 WARN_ON(iocb->ki_pos != pos);
1195
Anand Avati4273b792012-02-17 12:46:25 -05001196 ocount = 0;
1197 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001198 if (err)
1199 return err;
1200
Anand Avati4273b792012-02-17 12:46:25 -05001201 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001202 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001203
1204 /* We can write back this queue in page reclaim */
1205 current->backing_dev_info = mapping->backing_dev_info;
1206
1207 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1208 if (err)
1209 goto out;
1210
1211 if (count == 0)
1212 goto out;
1213
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001214 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001215 if (err)
1216 goto out;
1217
Josef Bacikc3b2da32012-03-26 09:59:21 -04001218 err = file_update_time(file);
1219 if (err)
1220 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001221
Anand Avati4273b792012-02-17 12:46:25 -05001222 if (file->f_flags & O_DIRECT) {
1223 written = generic_file_direct_write(iocb, iov, &nr_segs,
1224 pos, &iocb->ki_pos,
1225 count, ocount);
1226 if (written < 0 || written == count)
1227 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001228
Anand Avati4273b792012-02-17 12:46:25 -05001229 pos += written;
1230 count -= written;
1231
1232 iov_iter_init(&i, iov, nr_segs, count, written);
1233 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1234 if (written_buffered < 0) {
1235 err = written_buffered;
1236 goto out;
1237 }
1238 endbyte = pos + written_buffered - 1;
1239
1240 err = filemap_write_and_wait_range(file->f_mapping, pos,
1241 endbyte);
1242 if (err)
1243 goto out;
1244
1245 invalidate_mapping_pages(file->f_mapping,
1246 pos >> PAGE_CACHE_SHIFT,
1247 endbyte >> PAGE_CACHE_SHIFT);
1248
1249 written += written_buffered;
1250 iocb->ki_pos = pos + written_buffered;
1251 } else {
1252 iov_iter_init(&i, iov, nr_segs, count, 0);
1253 written = fuse_perform_write(file, mapping, &i, pos);
1254 if (written >= 0)
1255 iocb->ki_pos = pos + written;
1256 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001257out:
1258 current->backing_dev_info = NULL;
1259 mutex_unlock(&inode->i_mutex);
1260
1261 return written ? written : err;
1262}
1263
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001264static inline void fuse_page_descs_length_init(struct fuse_req *req,
1265 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001266{
1267 int i;
1268
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001269 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001270 req->page_descs[i].length = PAGE_SIZE -
1271 req->page_descs[i].offset;
1272}
1273
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001274static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1275{
1276 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1277}
1278
1279static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1280 size_t max_size)
1281{
1282 return min(iov_iter_single_seg_count(ii), max_size);
1283}
1284
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001285static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001286 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001287{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001288 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001289
Miklos Szeredif4975c62009-04-02 14:25:34 +02001290 /* Special case for kernel I/O: can copy directly into the buffer */
1291 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001292 unsigned long user_addr = fuse_get_user_addr(ii);
1293 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1294
Miklos Szeredif4975c62009-04-02 14:25:34 +02001295 if (write)
1296 req->in.args[1].value = (void *) user_addr;
1297 else
1298 req->out.args[0].value = (void *) user_addr;
1299
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001300 iov_iter_advance(ii, frag_size);
1301 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001302 return 0;
1303 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001304
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001305 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001306 unsigned npages;
1307 unsigned long user_addr = fuse_get_user_addr(ii);
1308 unsigned offset = user_addr & ~PAGE_MASK;
1309 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1310 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001311
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001312 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001313 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1314
1315 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1316 npages = clamp(npages, 1U, n);
1317
1318 ret = get_user_pages_fast(user_addr, npages, !write,
1319 &req->pages[req->num_pages]);
1320 if (ret < 0)
1321 return ret;
1322
1323 npages = ret;
1324 frag_size = min_t(size_t, frag_size,
1325 (npages << PAGE_SHIFT) - offset);
1326 iov_iter_advance(ii, frag_size);
1327
1328 req->page_descs[req->num_pages].offset = offset;
1329 fuse_page_descs_length_init(req, req->num_pages, npages);
1330
1331 req->num_pages += npages;
1332 req->page_descs[req->num_pages - 1].length -=
1333 (npages << PAGE_SHIFT) - offset - frag_size;
1334
1335 nbytes += frag_size;
1336 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001337
1338 if (write)
1339 req->in.argpages = 1;
1340 else
1341 req->out.argpages = 1;
1342
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001343 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001344
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001345 return 0;
1346}
1347
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001348static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1349{
1350 struct iov_iter ii = *ii_p;
1351 int npages = 0;
1352
1353 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1354 unsigned long user_addr = fuse_get_user_addr(&ii);
1355 unsigned offset = user_addr & ~PAGE_MASK;
1356 size_t frag_size = iov_iter_single_seg_count(&ii);
1357
1358 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1359 iov_iter_advance(&ii, frag_size);
1360 }
1361
1362 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1363}
1364
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001365ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001366 unsigned long nr_segs, size_t count, loff_t *ppos,
1367 int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001368{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001369 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001370 struct fuse_file *ff = file->private_data;
1371 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001372 size_t nmax = write ? fc->max_write : fc->max_read;
1373 loff_t pos = *ppos;
1374 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001375 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001376 struct iov_iter ii;
1377
1378 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001379
Brian Fosterde82b922013-05-14 11:25:39 -04001380 if (io->async)
1381 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1382 else
1383 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001384 if (IS_ERR(req))
1385 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001386
1387 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001388 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001389 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001390 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001391 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001392 if (err) {
1393 res = err;
1394 break;
1395 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001396
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001397 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001398 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001399 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001400 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001401
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001402 if (!io->async)
1403 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001404 if (req->out.h.error) {
1405 if (!res)
1406 res = req->out.h.error;
1407 break;
1408 } else if (nres > nbytes) {
1409 res = -EIO;
1410 break;
1411 }
1412 count -= nres;
1413 res += nres;
1414 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001415 if (nres != nbytes)
1416 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001417 if (count) {
1418 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001419 if (io->async)
1420 req = fuse_get_req_for_background(fc,
1421 fuse_iter_npages(&ii));
1422 else
1423 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001424 if (IS_ERR(req))
1425 break;
1426 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001427 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001428 if (!IS_ERR(req))
1429 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001430 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001431 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001432
1433 return res;
1434}
Tejun Heo08cbf542009-04-14 10:54:53 +09001435EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001436
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001437static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1438 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001439 unsigned long nr_segs, loff_t *ppos,
1440 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001441{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001442 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001443 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001444 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001445
1446 if (is_bad_inode(inode))
1447 return -EIO;
1448
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001449 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001450
1451 fuse_invalidate_attr(inode);
1452
1453 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001454}
1455
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001456static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1457 size_t count, loff_t *ppos)
1458{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001459 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001460 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001461 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001462}
1463
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001464static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1465 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001466 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001467{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001468 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001469 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001470 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001471 ssize_t res;
1472
1473 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001474 if (!res)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001475 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1);
Anand Avati4273b792012-02-17 12:46:25 -05001476
1477 fuse_invalidate_attr(inode);
1478
1479 return res;
1480}
1481
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001482static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1483 size_t count, loff_t *ppos)
1484{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001485 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001486 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001487 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001488 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001489
1490 if (is_bad_inode(inode))
1491 return -EIO;
1492
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001493 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001494 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001495 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001496 if (res > 0)
1497 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001498 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001499
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001500 return res;
1501}
1502
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001503static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001504{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001505 int i;
1506
1507 for (i = 0; i < req->num_pages; i++)
1508 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001509
1510 if (req->ff)
1511 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001512}
1513
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001514static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001515{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001516 struct inode *inode = req->inode;
1517 struct fuse_inode *fi = get_fuse_inode(inode);
1518 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001519 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001520
1521 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001522 for (i = 0; i < req->num_pages; i++) {
1523 dec_bdi_stat(bdi, BDI_WRITEBACK);
1524 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1525 bdi_writeout_inc(bdi);
1526 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001527 wake_up(&fi->page_waitq);
1528}
1529
1530/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001531static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1532 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001533__releases(fc->lock)
1534__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001535{
1536 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001537 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001538 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001539
1540 if (!fc->connected)
1541 goto out_free;
1542
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001543 if (inarg->offset + data_size <= size) {
1544 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001545 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001546 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001547 } else {
1548 /* Got truncated off completely */
1549 goto out_free;
1550 }
1551
1552 req->in.args[1].size = inarg->size;
1553 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001554 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001555 return;
1556
1557 out_free:
1558 fuse_writepage_finish(fc, req);
1559 spin_unlock(&fc->lock);
1560 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001561 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001562 spin_lock(&fc->lock);
1563}
1564
1565/*
1566 * If fi->writectr is positive (no truncate or fsync going on) send
1567 * all queued writepage requests.
1568 *
1569 * Called with fc->lock
1570 */
1571void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001572__releases(fc->lock)
1573__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001574{
1575 struct fuse_conn *fc = get_fuse_conn(inode);
1576 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001577 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001578 struct fuse_req *req;
1579
1580 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1581 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1582 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001583 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001584 }
1585}
1586
1587static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1588{
1589 struct inode *inode = req->inode;
1590 struct fuse_inode *fi = get_fuse_inode(inode);
1591
1592 mapping_set_error(inode->i_mapping, req->out.h.error);
1593 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001594 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001595 struct fuse_conn *fc = get_fuse_conn(inode);
1596 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001597 struct fuse_req *next = req->misc.write.next;
1598 req->misc.write.next = next->misc.write.next;
1599 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001600 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001601 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001602
1603 /*
1604 * Skip fuse_flush_writepages() to make it easy to crop requests
1605 * based on primary request size.
1606 *
1607 * 1st case (trivial): there are no concurrent activities using
1608 * fuse_set/release_nowrite. Then we're on safe side because
1609 * fuse_flush_writepages() would call fuse_send_writepage()
1610 * anyway.
1611 *
1612 * 2nd case: someone called fuse_set_nowrite and it is waiting
1613 * now for completion of all in-flight requests. This happens
1614 * rarely and no more than once per page, so this should be
1615 * okay.
1616 *
1617 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1618 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1619 * that fuse_set_nowrite returned implies that all in-flight
1620 * requests were completed along with all of their secondary
1621 * requests. Further primary requests are blocked by negative
1622 * writectr. Hence there cannot be any in-flight requests and
1623 * no invocations of fuse_writepage_end() while we're in
1624 * fuse_set_nowrite..fuse_release_nowrite section.
1625 */
1626 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001627 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001628 fi->writectr--;
1629 fuse_writepage_finish(fc, req);
1630 spin_unlock(&fc->lock);
1631 fuse_writepage_free(fc, req);
1632}
1633
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001634static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1635 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001636{
Miklos Szeredi72523422013-10-01 16:44:52 +02001637 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001638
1639 spin_lock(&fc->lock);
Miklos Szeredi72523422013-10-01 16:44:52 +02001640 if (!WARN_ON(list_empty(&fi->write_files))) {
1641 ff = list_entry(fi->write_files.next, struct fuse_file,
1642 write_entry);
1643 fuse_file_get(ff);
1644 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001645 spin_unlock(&fc->lock);
1646
1647 return ff;
1648}
1649
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001650static int fuse_writepage_locked(struct page *page)
1651{
1652 struct address_space *mapping = page->mapping;
1653 struct inode *inode = mapping->host;
1654 struct fuse_conn *fc = get_fuse_conn(inode);
1655 struct fuse_inode *fi = get_fuse_inode(inode);
1656 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001657 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001658 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001659
1660 set_page_writeback(page);
1661
Maxim Patlasov4250c062012-10-26 19:48:07 +04001662 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001663 if (!req)
1664 goto err;
1665
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001666 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001667 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1668 if (!tmp_page)
1669 goto err_free;
1670
Miklos Szeredi72523422013-10-01 16:44:52 +02001671 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001672 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001673 if (!req->ff)
1674 goto err_free;
1675
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001676 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001677
1678 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001679 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001680 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001681 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001682 req->num_pages = 1;
1683 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001684 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001685 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001686 req->end = fuse_writepage_end;
1687 req->inode = inode;
1688
1689 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1690 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001691
1692 spin_lock(&fc->lock);
1693 list_add(&req->writepages_entry, &fi->writepages);
1694 list_add_tail(&req->list, &fi->queued_writes);
1695 fuse_flush_writepages(inode);
1696 spin_unlock(&fc->lock);
1697
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001698 end_page_writeback(page);
1699
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001700 return 0;
1701
1702err_free:
1703 fuse_request_free(req);
1704err:
1705 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001706 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001707}
1708
1709static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1710{
1711 int err;
1712
Miklos Szerediff17be02013-10-01 16:44:53 +02001713 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1714 /*
1715 * ->writepages() should be called for sync() and friends. We
1716 * should only get here on direct reclaim and then we are
1717 * allowed to skip a page which is already in flight
1718 */
1719 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1720
1721 redirty_page_for_writepage(wbc, page);
1722 return 0;
1723 }
1724
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001725 err = fuse_writepage_locked(page);
1726 unlock_page(page);
1727
1728 return err;
1729}
1730
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001731struct fuse_fill_wb_data {
1732 struct fuse_req *req;
1733 struct fuse_file *ff;
1734 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001735 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001736};
1737
1738static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1739{
1740 struct fuse_req *req = data->req;
1741 struct inode *inode = data->inode;
1742 struct fuse_conn *fc = get_fuse_conn(inode);
1743 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001744 int num_pages = req->num_pages;
1745 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001746
1747 req->ff = fuse_file_get(data->ff);
1748 spin_lock(&fc->lock);
1749 list_add_tail(&req->list, &fi->queued_writes);
1750 fuse_flush_writepages(inode);
1751 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001752
1753 for (i = 0; i < num_pages; i++)
1754 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001755}
1756
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001757static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1758 struct page *page)
1759{
1760 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1761 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1762 struct fuse_req *tmp;
1763 struct fuse_req *old_req;
1764 bool found = false;
1765 pgoff_t curr_index;
1766
1767 BUG_ON(new_req->num_pages != 0);
1768
1769 spin_lock(&fc->lock);
1770 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001771 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1772 BUG_ON(old_req->inode != new_req->inode);
1773 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1774 if (curr_index <= page->index &&
1775 page->index < curr_index + old_req->num_pages) {
1776 found = true;
1777 break;
1778 }
1779 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001780 if (!found) {
1781 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001782 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001783 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001784
Maxim Patlasovf6011082013-10-02 15:01:07 +04001785 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001786 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1787 BUG_ON(tmp->inode != new_req->inode);
1788 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1789 if (tmp->num_pages == 1 &&
1790 curr_index == page->index) {
1791 old_req = tmp;
1792 }
1793 }
1794
1795 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1796 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001797 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1798
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001799 copy_highpage(old_req->pages[0], page);
1800 spin_unlock(&fc->lock);
1801
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001802 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001803 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001804 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001805 fuse_writepage_free(fc, new_req);
1806 fuse_request_free(new_req);
1807 goto out;
1808 } else {
1809 new_req->misc.write.next = old_req->misc.write.next;
1810 old_req->misc.write.next = new_req;
1811 }
1812out_unlock:
1813 spin_unlock(&fc->lock);
1814out:
1815 return found;
1816}
1817
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001818static int fuse_writepages_fill(struct page *page,
1819 struct writeback_control *wbc, void *_data)
1820{
1821 struct fuse_fill_wb_data *data = _data;
1822 struct fuse_req *req = data->req;
1823 struct inode *inode = data->inode;
1824 struct fuse_conn *fc = get_fuse_conn(inode);
1825 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001826 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001827 int err;
1828
1829 if (!data->ff) {
1830 err = -EIO;
1831 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1832 if (!data->ff)
1833 goto out_unlock;
1834 }
1835
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001836 /*
1837 * Being under writeback is unlikely but possible. For example direct
1838 * read to an mmaped fuse file will set the page dirty twice; once when
1839 * the pages are faulted with get_user_pages(), and then after the read
1840 * completed.
1841 */
1842 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001843
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001844 if (req && req->num_pages &&
1845 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1846 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1847 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1848 fuse_writepages_send(data);
1849 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001850 }
1851 err = -ENOMEM;
1852 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1853 if (!tmp_page)
1854 goto out_unlock;
1855
1856 /*
1857 * The page must not be redirtied until the writeout is completed
1858 * (i.e. userspace has sent a reply to the write request). Otherwise
1859 * there could be more than one temporary page instance for each real
1860 * page.
1861 *
1862 * This is ensured by holding the page lock in page_mkwrite() while
1863 * checking fuse_page_is_writeback(). We already hold the page lock
1864 * since clear_page_dirty_for_io() and keep it held until we add the
1865 * request to the fi->writepages list and increment req->num_pages.
1866 * After this fuse_page_is_writeback() will indicate that the page is
1867 * under writeback, so we can release the page lock.
1868 */
1869 if (data->req == NULL) {
1870 struct fuse_inode *fi = get_fuse_inode(inode);
1871
1872 err = -ENOMEM;
1873 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1874 if (!req) {
1875 __free_page(tmp_page);
1876 goto out_unlock;
1877 }
1878
1879 fuse_write_fill(req, data->ff, page_offset(page), 0);
1880 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001881 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001882 req->in.argpages = 1;
1883 req->background = 1;
1884 req->num_pages = 0;
1885 req->end = fuse_writepage_end;
1886 req->inode = inode;
1887
1888 spin_lock(&fc->lock);
1889 list_add(&req->writepages_entry, &fi->writepages);
1890 spin_unlock(&fc->lock);
1891
1892 data->req = req;
1893 }
1894 set_page_writeback(page);
1895
1896 copy_highpage(tmp_page, page);
1897 req->pages[req->num_pages] = tmp_page;
1898 req->page_descs[req->num_pages].offset = 0;
1899 req->page_descs[req->num_pages].length = PAGE_SIZE;
1900
1901 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1902 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001903
1904 err = 0;
1905 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1906 end_page_writeback(page);
1907 data->req = NULL;
1908 goto out_unlock;
1909 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001910 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001911
1912 /*
1913 * Protected by fc->lock against concurrent access by
1914 * fuse_page_is_writeback().
1915 */
1916 spin_lock(&fc->lock);
1917 req->num_pages++;
1918 spin_unlock(&fc->lock);
1919
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001920out_unlock:
1921 unlock_page(page);
1922
1923 return err;
1924}
1925
1926static int fuse_writepages(struct address_space *mapping,
1927 struct writeback_control *wbc)
1928{
1929 struct inode *inode = mapping->host;
1930 struct fuse_fill_wb_data data;
1931 int err;
1932
1933 err = -EIO;
1934 if (is_bad_inode(inode))
1935 goto out;
1936
1937 data.inode = inode;
1938 data.req = NULL;
1939 data.ff = NULL;
1940
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001941 err = -ENOMEM;
1942 data.orig_pages = kzalloc(sizeof(struct page *) *
1943 FUSE_MAX_PAGES_PER_REQ,
1944 GFP_NOFS);
1945 if (!data.orig_pages)
1946 goto out;
1947
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001948 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1949 if (data.req) {
1950 /* Ignore errors if we can write at least one page */
1951 BUG_ON(!data.req->num_pages);
1952 fuse_writepages_send(&data);
1953 err = 0;
1954 }
1955 if (data.ff)
1956 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001957
1958 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001959out:
1960 return err;
1961}
1962
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001963/*
1964 * It's worthy to make sure that space is reserved on disk for the write,
1965 * but how to implement it without killing performance need more thinking.
1966 */
1967static int fuse_write_begin(struct file *file, struct address_space *mapping,
1968 loff_t pos, unsigned len, unsigned flags,
1969 struct page **pagep, void **fsdata)
1970{
1971 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1972 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
1973 struct page *page;
1974 loff_t fsize;
1975 int err = -ENOMEM;
1976
1977 WARN_ON(!fc->writeback_cache);
1978
1979 page = grab_cache_page_write_begin(mapping, index, flags);
1980 if (!page)
1981 goto error;
1982
1983 fuse_wait_on_page_writeback(mapping->host, page->index);
1984
1985 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
1986 goto success;
1987 /*
1988 * Check if the start this page comes after the end of file, in which
1989 * case the readpage can be optimized away.
1990 */
1991 fsize = i_size_read(mapping->host);
1992 if (fsize <= (pos & PAGE_CACHE_MASK)) {
1993 size_t off = pos & ~PAGE_CACHE_MASK;
1994 if (off)
1995 zero_user_segment(page, 0, off);
1996 goto success;
1997 }
1998 err = fuse_do_readpage(file, page);
1999 if (err)
2000 goto cleanup;
2001success:
2002 *pagep = page;
2003 return 0;
2004
2005cleanup:
2006 unlock_page(page);
2007 page_cache_release(page);
2008error:
2009 return err;
2010}
2011
2012static int fuse_write_end(struct file *file, struct address_space *mapping,
2013 loff_t pos, unsigned len, unsigned copied,
2014 struct page *page, void *fsdata)
2015{
2016 struct inode *inode = page->mapping->host;
2017
2018 if (!PageUptodate(page)) {
2019 /* Zero any unwritten bytes at the end of the page */
2020 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2021 if (endoff)
2022 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2023 SetPageUptodate(page);
2024 }
2025
2026 fuse_write_update_size(inode, pos + copied);
2027 set_page_dirty(page);
2028 unlock_page(page);
2029 page_cache_release(page);
2030
2031 return copied;
2032}
2033
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002034static int fuse_launder_page(struct page *page)
2035{
2036 int err = 0;
2037 if (clear_page_dirty_for_io(page)) {
2038 struct inode *inode = page->mapping->host;
2039 err = fuse_writepage_locked(page);
2040 if (!err)
2041 fuse_wait_on_page_writeback(inode, page->index);
2042 }
2043 return err;
2044}
2045
2046/*
2047 * Write back dirty pages now, because there may not be any suitable
2048 * open files later
2049 */
2050static void fuse_vma_close(struct vm_area_struct *vma)
2051{
2052 filemap_write_and_wait(vma->vm_file->f_mapping);
2053}
2054
2055/*
2056 * Wait for writeback against this page to complete before allowing it
2057 * to be marked dirty again, and hence written back again, possibly
2058 * before the previous writepage completed.
2059 *
2060 * Block here, instead of in ->writepage(), so that the userspace fs
2061 * can only block processes actually operating on the filesystem.
2062 *
2063 * Otherwise unprivileged userspace fs would be able to block
2064 * unrelated:
2065 *
2066 * - page migration
2067 * - sync(2)
2068 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2069 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002070static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002071{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002072 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002073 struct inode *inode = file_inode(vma->vm_file);
2074
2075 file_update_time(vma->vm_file);
2076 lock_page(page);
2077 if (page->mapping != inode->i_mapping) {
2078 unlock_page(page);
2079 return VM_FAULT_NOPAGE;
2080 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002081
2082 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002083 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002084}
2085
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002086static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002087 .close = fuse_vma_close,
2088 .fault = filemap_fault,
2089 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002090 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002091};
2092
2093static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2094{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002095 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2096 fuse_link_write_file(file);
2097
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002098 file_accessed(file);
2099 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002100 return 0;
2101}
2102
Miklos Szeredifc280c92009-04-02 14:25:35 +02002103static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2104{
2105 /* Can't provide the coherency needed for MAP_SHARED */
2106 if (vma->vm_flags & VM_MAYSHARE)
2107 return -ENODEV;
2108
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002109 invalidate_inode_pages2(file->f_mapping);
2110
Miklos Szeredifc280c92009-04-02 14:25:35 +02002111 return generic_file_mmap(file, vma);
2112}
2113
Miklos Szeredi71421252006-06-25 05:48:52 -07002114static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2115 struct file_lock *fl)
2116{
2117 switch (ffl->type) {
2118 case F_UNLCK:
2119 break;
2120
2121 case F_RDLCK:
2122 case F_WRLCK:
2123 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2124 ffl->end < ffl->start)
2125 return -EIO;
2126
2127 fl->fl_start = ffl->start;
2128 fl->fl_end = ffl->end;
2129 fl->fl_pid = ffl->pid;
2130 break;
2131
2132 default:
2133 return -EIO;
2134 }
2135 fl->fl_type = ffl->type;
2136 return 0;
2137}
2138
2139static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002140 const struct file_lock *fl, int opcode, pid_t pid,
2141 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002142{
Al Viro6131ffa2013-02-27 16:59:05 -05002143 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002144 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002145 struct fuse_file *ff = file->private_data;
2146 struct fuse_lk_in *arg = &req->misc.lk_in;
2147
2148 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002149 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002150 arg->lk.start = fl->fl_start;
2151 arg->lk.end = fl->fl_end;
2152 arg->lk.type = fl->fl_type;
2153 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002154 if (flock)
2155 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002156 req->in.h.opcode = opcode;
2157 req->in.h.nodeid = get_node_id(inode);
2158 req->in.numargs = 1;
2159 req->in.args[0].size = sizeof(*arg);
2160 req->in.args[0].value = arg;
2161}
2162
2163static int fuse_getlk(struct file *file, struct file_lock *fl)
2164{
Al Viro6131ffa2013-02-27 16:59:05 -05002165 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002166 struct fuse_conn *fc = get_fuse_conn(inode);
2167 struct fuse_req *req;
2168 struct fuse_lk_out outarg;
2169 int err;
2170
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002171 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002172 if (IS_ERR(req))
2173 return PTR_ERR(req);
2174
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002175 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002176 req->out.numargs = 1;
2177 req->out.args[0].size = sizeof(outarg);
2178 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002179 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002180 err = req->out.h.error;
2181 fuse_put_request(fc, req);
2182 if (!err)
2183 err = convert_fuse_file_lock(&outarg.lk, fl);
2184
2185 return err;
2186}
2187
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002188static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002189{
Al Viro6131ffa2013-02-27 16:59:05 -05002190 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002191 struct fuse_conn *fc = get_fuse_conn(inode);
2192 struct fuse_req *req;
2193 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2194 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2195 int err;
2196
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002197 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002198 /* NLM needs asynchronous locks, which we don't support yet */
2199 return -ENOLCK;
2200 }
2201
Miklos Szeredi71421252006-06-25 05:48:52 -07002202 /* Unlock on close is handled by the flush method */
2203 if (fl->fl_flags & FL_CLOSE)
2204 return 0;
2205
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002206 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002207 if (IS_ERR(req))
2208 return PTR_ERR(req);
2209
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002210 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002211 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002212 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002213 /* locking is restartable */
2214 if (err == -EINTR)
2215 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002216 fuse_put_request(fc, req);
2217 return err;
2218}
2219
2220static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2221{
Al Viro6131ffa2013-02-27 16:59:05 -05002222 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002223 struct fuse_conn *fc = get_fuse_conn(inode);
2224 int err;
2225
Miklos Szeredi48e90762008-07-25 01:49:02 -07002226 if (cmd == F_CANCELLK) {
2227 err = 0;
2228 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002229 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002230 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002231 err = 0;
2232 } else
2233 err = fuse_getlk(file, fl);
2234 } else {
2235 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002236 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002237 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002238 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002239 }
2240 return err;
2241}
2242
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002243static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2244{
Al Viro6131ffa2013-02-27 16:59:05 -05002245 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002246 struct fuse_conn *fc = get_fuse_conn(inode);
2247 int err;
2248
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002249 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002250 err = flock_lock_file_wait(file, fl);
2251 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002252 struct fuse_file *ff = file->private_data;
2253
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002254 /* emulate flock with POSIX locks */
2255 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002256 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002257 err = fuse_setlk(file, fl, 1);
2258 }
2259
2260 return err;
2261}
2262
Miklos Szeredib2d22722006-12-06 20:35:51 -08002263static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2264{
2265 struct inode *inode = mapping->host;
2266 struct fuse_conn *fc = get_fuse_conn(inode);
2267 struct fuse_req *req;
2268 struct fuse_bmap_in inarg;
2269 struct fuse_bmap_out outarg;
2270 int err;
2271
2272 if (!inode->i_sb->s_bdev || fc->no_bmap)
2273 return 0;
2274
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002275 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002276 if (IS_ERR(req))
2277 return 0;
2278
2279 memset(&inarg, 0, sizeof(inarg));
2280 inarg.block = block;
2281 inarg.blocksize = inode->i_sb->s_blocksize;
2282 req->in.h.opcode = FUSE_BMAP;
2283 req->in.h.nodeid = get_node_id(inode);
2284 req->in.numargs = 1;
2285 req->in.args[0].size = sizeof(inarg);
2286 req->in.args[0].value = &inarg;
2287 req->out.numargs = 1;
2288 req->out.args[0].size = sizeof(outarg);
2289 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002290 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002291 err = req->out.h.error;
2292 fuse_put_request(fc, req);
2293 if (err == -ENOSYS)
2294 fc->no_bmap = 1;
2295
2296 return err ? 0 : outarg.block;
2297}
2298
Andrew Morton965c8e52012-12-17 15:59:39 -08002299static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002300{
2301 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002302 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002303
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002304 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002305 if (whence == SEEK_CUR || whence == SEEK_SET)
2306 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002307
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002308 mutex_lock(&inode->i_mutex);
2309 retval = fuse_update_attributes(inode, NULL, file, NULL);
2310 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002311 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002312 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002313
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002314 return retval;
2315}
2316
Tejun Heo59efec72008-11-26 12:03:55 +01002317static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2318 unsigned int nr_segs, size_t bytes, bool to_user)
2319{
2320 struct iov_iter ii;
2321 int page_idx = 0;
2322
2323 if (!bytes)
2324 return 0;
2325
2326 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2327
2328 while (iov_iter_count(&ii)) {
2329 struct page *page = pages[page_idx++];
2330 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002331 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002332
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002333 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002334
2335 while (todo) {
2336 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2337 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2338 size_t copy = min(todo, iov_len);
2339 size_t left;
2340
2341 if (!to_user)
2342 left = copy_from_user(kaddr, uaddr, copy);
2343 else
2344 left = copy_to_user(uaddr, kaddr, copy);
2345
2346 if (unlikely(left))
2347 return -EFAULT;
2348
2349 iov_iter_advance(&ii, copy);
2350 todo -= copy;
2351 kaddr += copy;
2352 }
2353
Jens Axboe0bd87182009-11-03 11:40:44 +01002354 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002355 }
2356
2357 return 0;
2358}
2359
2360/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002361 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2362 * ABI was defined to be 'struct iovec' which is different on 32bit
2363 * and 64bit. Fortunately we can determine which structure the server
2364 * used from the size of the reply.
2365 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002366static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2367 size_t transferred, unsigned count,
2368 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002369{
2370#ifdef CONFIG_COMPAT
2371 if (count * sizeof(struct compat_iovec) == transferred) {
2372 struct compat_iovec *ciov = src;
2373 unsigned i;
2374
2375 /*
2376 * With this interface a 32bit server cannot support
2377 * non-compat (i.e. ones coming from 64bit apps) ioctl
2378 * requests
2379 */
2380 if (!is_compat)
2381 return -EINVAL;
2382
2383 for (i = 0; i < count; i++) {
2384 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2385 dst[i].iov_len = ciov[i].iov_len;
2386 }
2387 return 0;
2388 }
2389#endif
2390
2391 if (count * sizeof(struct iovec) != transferred)
2392 return -EIO;
2393
2394 memcpy(dst, src, transferred);
2395 return 0;
2396}
2397
Miklos Szeredi75727772010-11-30 16:39:27 +01002398/* Make sure iov_length() won't overflow */
2399static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2400{
2401 size_t n;
2402 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2403
Zach Brownfb6ccff2012-07-24 12:10:11 -07002404 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002405 if (iov->iov_len > (size_t) max)
2406 return -ENOMEM;
2407 max -= iov->iov_len;
2408 }
2409 return 0;
2410}
2411
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002412static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2413 void *src, size_t transferred, unsigned count,
2414 bool is_compat)
2415{
2416 unsigned i;
2417 struct fuse_ioctl_iovec *fiov = src;
2418
2419 if (fc->minor < 16) {
2420 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2421 count, is_compat);
2422 }
2423
2424 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2425 return -EIO;
2426
2427 for (i = 0; i < count; i++) {
2428 /* Did the server supply an inappropriate value? */
2429 if (fiov[i].base != (unsigned long) fiov[i].base ||
2430 fiov[i].len != (unsigned long) fiov[i].len)
2431 return -EIO;
2432
2433 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2434 dst[i].iov_len = (size_t) fiov[i].len;
2435
2436#ifdef CONFIG_COMPAT
2437 if (is_compat &&
2438 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2439 (compat_size_t) dst[i].iov_len != fiov[i].len))
2440 return -EIO;
2441#endif
2442 }
2443
2444 return 0;
2445}
2446
2447
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002448/*
Tejun Heo59efec72008-11-26 12:03:55 +01002449 * For ioctls, there is no generic way to determine how much memory
2450 * needs to be read and/or written. Furthermore, ioctls are allowed
2451 * to dereference the passed pointer, so the parameter requires deep
2452 * copying but FUSE has no idea whatsoever about what to copy in or
2453 * out.
2454 *
2455 * This is solved by allowing FUSE server to retry ioctl with
2456 * necessary in/out iovecs. Let's assume the ioctl implementation
2457 * needs to read in the following structure.
2458 *
2459 * struct a {
2460 * char *buf;
2461 * size_t buflen;
2462 * }
2463 *
2464 * On the first callout to FUSE server, inarg->in_size and
2465 * inarg->out_size will be NULL; then, the server completes the ioctl
2466 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2467 * the actual iov array to
2468 *
2469 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2470 *
2471 * which tells FUSE to copy in the requested area and retry the ioctl.
2472 * On the second round, the server has access to the structure and
2473 * from that it can tell what to look for next, so on the invocation,
2474 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2475 *
2476 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2477 * { .iov_base = a.buf, .iov_len = a.buflen } }
2478 *
2479 * FUSE will copy both struct a and the pointed buffer from the
2480 * process doing the ioctl and retry ioctl with both struct a and the
2481 * buffer.
2482 *
2483 * This time, FUSE server has everything it needs and completes ioctl
2484 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2485 *
2486 * Copying data out works the same way.
2487 *
2488 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2489 * automatically initializes in and out iovs by decoding @cmd with
2490 * _IOC_* macros and the server is not allowed to request RETRY. This
2491 * limits ioctl data transfers to well-formed ioctls and is the forced
2492 * behavior for all FUSE servers.
2493 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002494long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2495 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002496{
Tejun Heo59efec72008-11-26 12:03:55 +01002497 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002498 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002499 struct fuse_ioctl_in inarg = {
2500 .fh = ff->fh,
2501 .cmd = cmd,
2502 .arg = arg,
2503 .flags = flags
2504 };
2505 struct fuse_ioctl_out outarg;
2506 struct fuse_req *req = NULL;
2507 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002508 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002509 struct iovec *in_iov = NULL, *out_iov = NULL;
2510 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2511 size_t in_size, out_size, transferred;
2512 int err;
2513
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002514#if BITS_PER_LONG == 32
2515 inarg.flags |= FUSE_IOCTL_32BIT;
2516#else
2517 if (flags & FUSE_IOCTL_COMPAT)
2518 inarg.flags |= FUSE_IOCTL_32BIT;
2519#endif
2520
Tejun Heo59efec72008-11-26 12:03:55 +01002521 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002522 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002523
Tejun Heo59efec72008-11-26 12:03:55 +01002524 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002525 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002526 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002527 if (!pages || !iov_page)
2528 goto out;
2529
2530 /*
2531 * If restricted, initialize IO parameters as encoded in @cmd.
2532 * RETRY from server is not allowed.
2533 */
2534 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002535 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002536
Miklos Szeredic9f05232008-12-02 14:49:42 +01002537 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002538 iov->iov_len = _IOC_SIZE(cmd);
2539
2540 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2541 in_iov = iov;
2542 in_iovs = 1;
2543 }
2544
2545 if (_IOC_DIR(cmd) & _IOC_READ) {
2546 out_iov = iov;
2547 out_iovs = 1;
2548 }
2549 }
2550
2551 retry:
2552 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2553 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2554
2555 /*
2556 * Out data can be used either for actual out data or iovs,
2557 * make sure there always is at least one page.
2558 */
2559 out_size = max_t(size_t, out_size, PAGE_SIZE);
2560 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2561
2562 /* make sure there are enough buffer pages and init request with them */
2563 err = -ENOMEM;
2564 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2565 goto out;
2566 while (num_pages < max_pages) {
2567 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2568 if (!pages[num_pages])
2569 goto out;
2570 num_pages++;
2571 }
2572
Maxim Patlasov54b96672012-10-26 19:49:13 +04002573 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002574 if (IS_ERR(req)) {
2575 err = PTR_ERR(req);
2576 req = NULL;
2577 goto out;
2578 }
2579 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2580 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002581 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002582
2583 /* okay, let's send it to the client */
2584 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002585 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002586 req->in.numargs = 1;
2587 req->in.args[0].size = sizeof(inarg);
2588 req->in.args[0].value = &inarg;
2589 if (in_size) {
2590 req->in.numargs++;
2591 req->in.args[1].size = in_size;
2592 req->in.argpages = 1;
2593
2594 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2595 false);
2596 if (err)
2597 goto out;
2598 }
2599
2600 req->out.numargs = 2;
2601 req->out.args[0].size = sizeof(outarg);
2602 req->out.args[0].value = &outarg;
2603 req->out.args[1].size = out_size;
2604 req->out.argpages = 1;
2605 req->out.argvar = 1;
2606
Tejun Heob93f8582008-11-26 12:03:55 +01002607 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002608 err = req->out.h.error;
2609 transferred = req->out.args[1].size;
2610 fuse_put_request(fc, req);
2611 req = NULL;
2612 if (err)
2613 goto out;
2614
2615 /* did it ask for retry? */
2616 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002617 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002618
2619 /* no retry if in restricted mode */
2620 err = -EIO;
2621 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2622 goto out;
2623
2624 in_iovs = outarg.in_iovs;
2625 out_iovs = outarg.out_iovs;
2626
2627 /*
2628 * Make sure things are in boundary, separate checks
2629 * are to protect against overflow.
2630 */
2631 err = -ENOMEM;
2632 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2633 out_iovs > FUSE_IOCTL_MAX_IOV ||
2634 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2635 goto out;
2636
Cong Wang2408f6e2011-11-25 23:14:30 +08002637 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002638 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002639 transferred, in_iovs + out_iovs,
2640 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002641 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002642 if (err)
2643 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002644
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002645 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002646 out_iov = in_iov + in_iovs;
2647
Miklos Szeredi75727772010-11-30 16:39:27 +01002648 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2649 if (err)
2650 goto out;
2651
2652 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2653 if (err)
2654 goto out;
2655
Tejun Heo59efec72008-11-26 12:03:55 +01002656 goto retry;
2657 }
2658
2659 err = -EIO;
2660 if (transferred > inarg.out_size)
2661 goto out;
2662
2663 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2664 out:
2665 if (req)
2666 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002667 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002668 while (num_pages)
2669 __free_page(pages[--num_pages]);
2670 kfree(pages);
2671
2672 return err ? err : outarg.result;
2673}
Tejun Heo08cbf542009-04-14 10:54:53 +09002674EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002675
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002676long fuse_ioctl_common(struct file *file, unsigned int cmd,
2677 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002678{
Al Viro6131ffa2013-02-27 16:59:05 -05002679 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002680 struct fuse_conn *fc = get_fuse_conn(inode);
2681
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002682 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002683 return -EACCES;
2684
2685 if (is_bad_inode(inode))
2686 return -EIO;
2687
2688 return fuse_do_ioctl(file, cmd, arg, flags);
2689}
2690
Tejun Heo59efec72008-11-26 12:03:55 +01002691static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2692 unsigned long arg)
2693{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002694 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002695}
2696
2697static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2698 unsigned long arg)
2699{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002700 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002701}
2702
Tejun Heo95668a62008-11-26 12:03:55 +01002703/*
2704 * All files which have been polled are linked to RB tree
2705 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2706 * find the matching one.
2707 */
2708static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2709 struct rb_node **parent_out)
2710{
2711 struct rb_node **link = &fc->polled_files.rb_node;
2712 struct rb_node *last = NULL;
2713
2714 while (*link) {
2715 struct fuse_file *ff;
2716
2717 last = *link;
2718 ff = rb_entry(last, struct fuse_file, polled_node);
2719
2720 if (kh < ff->kh)
2721 link = &last->rb_left;
2722 else if (kh > ff->kh)
2723 link = &last->rb_right;
2724 else
2725 return link;
2726 }
2727
2728 if (parent_out)
2729 *parent_out = last;
2730 return link;
2731}
2732
2733/*
2734 * The file is about to be polled. Make sure it's on the polled_files
2735 * RB tree. Note that files once added to the polled_files tree are
2736 * not removed before the file is released. This is because a file
2737 * polled once is likely to be polled again.
2738 */
2739static void fuse_register_polled_file(struct fuse_conn *fc,
2740 struct fuse_file *ff)
2741{
2742 spin_lock(&fc->lock);
2743 if (RB_EMPTY_NODE(&ff->polled_node)) {
2744 struct rb_node **link, *parent;
2745
2746 link = fuse_find_polled_node(fc, ff->kh, &parent);
2747 BUG_ON(*link);
2748 rb_link_node(&ff->polled_node, parent, link);
2749 rb_insert_color(&ff->polled_node, &fc->polled_files);
2750 }
2751 spin_unlock(&fc->lock);
2752}
2753
Tejun Heo08cbf542009-04-14 10:54:53 +09002754unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002755{
Tejun Heo95668a62008-11-26 12:03:55 +01002756 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002757 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002758 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2759 struct fuse_poll_out outarg;
2760 struct fuse_req *req;
2761 int err;
2762
2763 if (fc->no_poll)
2764 return DEFAULT_POLLMASK;
2765
2766 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002767 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002768
2769 /*
2770 * Ask for notification iff there's someone waiting for it.
2771 * The client may ignore the flag and always notify.
2772 */
2773 if (waitqueue_active(&ff->poll_wait)) {
2774 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2775 fuse_register_polled_file(fc, ff);
2776 }
2777
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002778 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002779 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002780 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002781
2782 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002783 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002784 req->in.numargs = 1;
2785 req->in.args[0].size = sizeof(inarg);
2786 req->in.args[0].value = &inarg;
2787 req->out.numargs = 1;
2788 req->out.args[0].size = sizeof(outarg);
2789 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002790 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002791 err = req->out.h.error;
2792 fuse_put_request(fc, req);
2793
2794 if (!err)
2795 return outarg.revents;
2796 if (err == -ENOSYS) {
2797 fc->no_poll = 1;
2798 return DEFAULT_POLLMASK;
2799 }
2800 return POLLERR;
2801}
Tejun Heo08cbf542009-04-14 10:54:53 +09002802EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002803
2804/*
2805 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2806 * wakes up the poll waiters.
2807 */
2808int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2809 struct fuse_notify_poll_wakeup_out *outarg)
2810{
2811 u64 kh = outarg->kh;
2812 struct rb_node **link;
2813
2814 spin_lock(&fc->lock);
2815
2816 link = fuse_find_polled_node(fc, kh, NULL);
2817 if (*link) {
2818 struct fuse_file *ff;
2819
2820 ff = rb_entry(*link, struct fuse_file, polled_node);
2821 wake_up_interruptible_sync(&ff->poll_wait);
2822 }
2823
2824 spin_unlock(&fc->lock);
2825 return 0;
2826}
2827
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002828static void fuse_do_truncate(struct file *file)
2829{
2830 struct inode *inode = file->f_mapping->host;
2831 struct iattr attr;
2832
2833 attr.ia_valid = ATTR_SIZE;
2834 attr.ia_size = i_size_read(inode);
2835
2836 attr.ia_file = file;
2837 attr.ia_valid |= ATTR_FILE;
2838
2839 fuse_do_setattr(inode, &attr, file);
2840}
2841
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002842static inline loff_t fuse_round_up(loff_t off)
2843{
2844 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2845}
2846
Anand Avati4273b792012-02-17 12:46:25 -05002847static ssize_t
2848fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2849 loff_t offset, unsigned long nr_segs)
2850{
2851 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002852 struct file *file = iocb->ki_filp;
2853 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002854 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002855 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002856 struct inode *inode;
2857 loff_t i_size;
2858 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002859 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002860
Anand Avati4273b792012-02-17 12:46:25 -05002861 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002862 inode = file->f_mapping->host;
2863 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002864
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002865 if ((rw == READ) && (offset > i_size))
2866 return 0;
2867
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002868 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002869 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002870 if (offset >= i_size)
2871 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002872 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002873 }
2874
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002875 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002876 if (!io)
2877 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002878 spin_lock_init(&io->lock);
2879 io->reqs = 1;
2880 io->bytes = -1;
2881 io->size = 0;
2882 io->offset = offset;
2883 io->write = (rw == WRITE);
2884 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002885 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002886 /*
2887 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002888 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002889 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002890 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002891 io->iocb = iocb;
2892
2893 /*
2894 * We cannot asynchronously extend the size of a file. We have no method
2895 * to wait on real async I/O requests, so we must submit this request
2896 * synchronously.
2897 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002898 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002899 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002900
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002901 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002902 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002903 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002904 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002905
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002906 if (io->async) {
2907 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2908
2909 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002910 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002911 return -EIOCBQUEUED;
2912
2913 ret = wait_on_sync_kiocb(iocb);
2914 } else {
2915 kfree(io);
2916 }
2917
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002918 if (rw == WRITE) {
2919 if (ret > 0)
2920 fuse_write_update_size(inode, pos);
2921 else if (ret < 0 && offset + count > i_size)
2922 fuse_do_truncate(file);
2923 }
Anand Avati4273b792012-02-17 12:46:25 -05002924
2925 return ret;
2926}
2927
Miklos Szeredicdadb112012-11-10 16:55:56 +01002928static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2929 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002930{
2931 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002932 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002933 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002934 struct fuse_conn *fc = ff->fc;
2935 struct fuse_req *req;
2936 struct fuse_fallocate_in inarg = {
2937 .fh = ff->fh,
2938 .offset = offset,
2939 .length = length,
2940 .mode = mode
2941 };
2942 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002943 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2944 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002945
Miklos Szeredi519c6042012-04-26 10:56:36 +02002946 if (fc->no_fallocate)
2947 return -EOPNOTSUPP;
2948
Maxim Patlasov14c14412013-06-13 12:16:39 +04002949 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002950 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002951 if (mode & FALLOC_FL_PUNCH_HOLE) {
2952 loff_t endbyte = offset + length - 1;
2953 err = filemap_write_and_wait_range(inode->i_mapping,
2954 offset, endbyte);
2955 if (err)
2956 goto out;
2957
2958 fuse_sync_writes(inode);
2959 }
Brian Foster3634a632013-05-17 09:30:32 -04002960 }
2961
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002962 if (!(mode & FALLOC_FL_KEEP_SIZE))
2963 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2964
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002965 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04002966 if (IS_ERR(req)) {
2967 err = PTR_ERR(req);
2968 goto out;
2969 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002970
2971 req->in.h.opcode = FUSE_FALLOCATE;
2972 req->in.h.nodeid = ff->nodeid;
2973 req->in.numargs = 1;
2974 req->in.args[0].size = sizeof(inarg);
2975 req->in.args[0].value = &inarg;
2976 fuse_request_send(fc, req);
2977 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002978 if (err == -ENOSYS) {
2979 fc->no_fallocate = 1;
2980 err = -EOPNOTSUPP;
2981 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002982 fuse_put_request(fc, req);
2983
Brian Fosterbee6c302013-05-17 15:27:34 -04002984 if (err)
2985 goto out;
2986
2987 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002988 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2989 bool changed = fuse_write_update_size(inode, offset + length);
2990
2991 if (changed && fc->writeback_cache) {
2992 struct fuse_inode *fi = get_fuse_inode(inode);
2993
2994 inode->i_mtime = current_fs_time(inode->i_sb);
2995 set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
2996 }
2997 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002998
2999 if (mode & FALLOC_FL_PUNCH_HOLE)
3000 truncate_pagecache_range(inode, offset, offset + length - 1);
3001
3002 fuse_invalidate_attr(inode);
3003
Brian Foster3634a632013-05-17 09:30:32 -04003004out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003005 if (!(mode & FALLOC_FL_KEEP_SIZE))
3006 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3007
Maxim Patlasovbde52782013-09-13 19:19:54 +04003008 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04003009 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04003010
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003011 return err;
3012}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003013
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003014static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003015 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003016 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08003017 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003018 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07003019 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003020 .mmap = fuse_file_mmap,
3021 .open = fuse_open,
3022 .flush = fuse_flush,
3023 .release = fuse_release,
3024 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003025 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003026 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003027 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003028 .unlocked_ioctl = fuse_file_ioctl,
3029 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003030 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003031 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003032};
3033
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003034static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003035 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003036 .read = fuse_direct_read,
3037 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003038 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003039 .open = fuse_open,
3040 .flush = fuse_flush,
3041 .release = fuse_release,
3042 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003043 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003044 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003045 .unlocked_ioctl = fuse_file_ioctl,
3046 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003047 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003048 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003049 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003050};
3051
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003052static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003053 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003054 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003055 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003056 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003057 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003058 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003059 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003060 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003061 .write_begin = fuse_write_begin,
3062 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003063};
3064
3065void fuse_init_file_inode(struct inode *inode)
3066{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003067 inode->i_fop = &fuse_file_operations;
3068 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003069}