blob: d8575304c062357f8b18fb6af8b705c2cb86bd6c [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
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700404static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700405{
Al Viro6131ffa2013-02-27 16:59:05 -0500406 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700407 struct fuse_conn *fc = get_fuse_conn(inode);
408 struct fuse_file *ff = file->private_data;
409 struct fuse_req *req;
410 struct fuse_flush_in inarg;
411 int err;
412
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800413 if (is_bad_inode(inode))
414 return -EIO;
415
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700416 if (fc->no_flush)
417 return 0;
418
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400419 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700420 memset(&inarg, 0, sizeof(inarg));
421 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700422 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423 req->in.h.opcode = FUSE_FLUSH;
424 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700425 req->in.numargs = 1;
426 req->in.args[0].size = sizeof(inarg);
427 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700428 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100429 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700430 err = req->out.h.error;
431 fuse_put_request(fc, req);
432 if (err == -ENOSYS) {
433 fc->no_flush = 1;
434 err = 0;
435 }
436 return err;
437}
438
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700439/*
440 * Wait for all pending writepages on the inode to finish.
441 *
442 * This is currently done by blocking further writes with FUSE_NOWRITE
443 * and waiting for all sent writes to complete.
444 *
445 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
446 * could conflict with truncation.
447 */
448static void fuse_sync_writes(struct inode *inode)
449{
450 fuse_set_nowrite(inode);
451 fuse_release_nowrite(inode);
452}
453
Josef Bacik02c24a82011-07-16 20:44:56 -0400454int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
455 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700456{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200457 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 struct fuse_conn *fc = get_fuse_conn(inode);
459 struct fuse_file *ff = file->private_data;
460 struct fuse_req *req;
461 struct fuse_fsync_in inarg;
462 int err;
463
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800464 if (is_bad_inode(inode))
465 return -EIO;
466
Josef Bacik02c24a82011-07-16 20:44:56 -0400467 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
468 if (err)
469 return err;
470
Miklos Szeredi82547982005-09-09 13:10:38 -0700471 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700472 return 0;
473
Josef Bacik02c24a82011-07-16 20:44:56 -0400474 mutex_lock(&inode->i_mutex);
475
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700476 /*
477 * Start writeback against all dirty pages of the inode, then
478 * wait for all outstanding writes, before sending the FSYNC
479 * request.
480 */
481 err = write_inode_now(inode, 0);
482 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400483 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700484
485 fuse_sync_writes(inode);
486
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400487 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
488 int err = fuse_flush_mtime(file, false);
489 if (err)
490 goto out;
491 }
492
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400493 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400494 if (IS_ERR(req)) {
495 err = PTR_ERR(req);
496 goto out;
497 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700498
499 memset(&inarg, 0, sizeof(inarg));
500 inarg.fh = ff->fh;
501 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700502 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700503 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700504 req->in.numargs = 1;
505 req->in.args[0].size = sizeof(inarg);
506 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100507 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700508 err = req->out.h.error;
509 fuse_put_request(fc, req);
510 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700511 if (isdir)
512 fc->no_fsyncdir = 1;
513 else
514 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700515 err = 0;
516 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400517out:
518 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700519 return err;
520}
521
Josef Bacik02c24a82011-07-16 20:44:56 -0400522static int fuse_fsync(struct file *file, loff_t start, loff_t end,
523 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700524{
Josef Bacik02c24a82011-07-16 20:44:56 -0400525 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700526}
527
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200528void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
529 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700530{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700531 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800532 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700533
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800534 inarg->fh = ff->fh;
535 inarg->offset = pos;
536 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800537 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800538 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200539 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700540 req->in.numargs = 1;
541 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800542 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700543 req->out.argvar = 1;
544 req->out.numargs = 1;
545 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700546}
547
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400548static void fuse_release_user_pages(struct fuse_req *req, int write)
549{
550 unsigned i;
551
552 for (i = 0; i < req->num_pages; i++) {
553 struct page *page = req->pages[i];
554 if (write)
555 set_page_dirty_lock(page);
556 put_page(page);
557 }
558}
559
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400560/**
561 * In case of short read, the caller sets 'pos' to the position of
562 * actual end of fuse request in IO request. Otherwise, if bytes_requested
563 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
564 *
565 * An example:
566 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
567 * both submitted asynchronously. The first of them was ACKed by userspace as
568 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
569 * second request was ACKed as short, e.g. only 1K was read, resulting in
570 * pos == 33K.
571 *
572 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
573 * will be equal to the length of the longest contiguous fragment of
574 * transferred data starting from the beginning of IO request.
575 */
576static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
577{
578 int left;
579
580 spin_lock(&io->lock);
581 if (err)
582 io->err = io->err ? : err;
583 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
584 io->bytes = pos;
585
586 left = --io->reqs;
587 spin_unlock(&io->lock);
588
589 if (!left) {
590 long res;
591
592 if (io->err)
593 res = io->err;
594 else if (io->bytes >= 0 && io->write)
595 res = -EIO;
596 else {
597 res = io->bytes < 0 ? io->size : io->bytes;
598
599 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400600 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400601 struct fuse_conn *fc = get_fuse_conn(inode);
602 struct fuse_inode *fi = get_fuse_inode(inode);
603
604 spin_lock(&fc->lock);
605 fi->attr_version = ++fc->attr_version;
606 spin_unlock(&fc->lock);
607 }
608 }
609
610 aio_complete(io->iocb, res, 0);
611 kfree(io);
612 }
613}
614
615static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
616{
617 struct fuse_io_priv *io = req->io;
618 ssize_t pos = -1;
619
620 fuse_release_user_pages(req, !io->write);
621
622 if (io->write) {
623 if (req->misc.write.in.size != req->misc.write.out.size)
624 pos = req->misc.write.in.offset - io->offset +
625 req->misc.write.out.size;
626 } else {
627 if (req->misc.read.in.size != req->out.args[0].size)
628 pos = req->misc.read.in.offset - io->offset +
629 req->out.args[0].size;
630 }
631
632 fuse_aio_complete(io, req->out.h.error, pos);
633}
634
635static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
636 size_t num_bytes, struct fuse_io_priv *io)
637{
638 spin_lock(&io->lock);
639 io->size += num_bytes;
640 io->reqs++;
641 spin_unlock(&io->lock);
642
643 req->io = io;
644 req->end = fuse_aio_complete_req;
645
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400646 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400647 fuse_request_send_background(fc, req);
648
649 return num_bytes;
650}
651
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400652static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200653 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700654{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400655 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200656 struct fuse_file *ff = file->private_data;
657 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700658
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200659 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700660 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700661 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700662
663 inarg->read_flags |= FUSE_READ_LOCKOWNER;
664 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
665 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400666
667 if (io->async)
668 return fuse_async_req_send(fc, req, count, io);
669
Tejun Heob93f8582008-11-26 12:03:55 +0100670 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800671 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700672}
673
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700674static void fuse_read_update_size(struct inode *inode, loff_t size,
675 u64 attr_ver)
676{
677 struct fuse_conn *fc = get_fuse_conn(inode);
678 struct fuse_inode *fi = get_fuse_inode(inode);
679
680 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400681 if (attr_ver == fi->attr_version && size < inode->i_size &&
682 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700683 fi->attr_version = ++fc->attr_version;
684 i_size_write(inode, size);
685 }
686 spin_unlock(&fc->lock);
687}
688
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400689static void fuse_short_read(struct fuse_req *req, struct inode *inode,
690 u64 attr_ver)
691{
692 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400693 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400694
Pavel Emelyanov83732002013-10-10 17:10:46 +0400695 if (fc->writeback_cache) {
696 /*
697 * A hole in a file. Some data after the hole are in page cache,
698 * but have not reached the client fs yet. So, the hole is not
699 * present there.
700 */
701 int i;
702 int start_idx = num_read >> PAGE_CACHE_SHIFT;
703 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
704
705 for (i = start_idx; i < req->num_pages; i++) {
706 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
707 off = 0;
708 }
709 } else {
710 loff_t pos = page_offset(req->pages[0]) + num_read;
711 fuse_read_update_size(inode, pos, attr_ver);
712 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400713}
714
Maxim Patlasov482fce52013-10-10 17:11:25 +0400715static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700716{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400717 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700718 struct inode *inode = page->mapping->host;
719 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800720 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700721 size_t num_read;
722 loff_t pos = page_offset(page);
723 size_t count = PAGE_CACHE_SIZE;
724 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800725 int err;
726
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700727 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300728 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700729 * page-cache page, so make sure we read a properly synced
730 * page.
731 */
732 fuse_wait_on_page_writeback(inode, page->index);
733
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400734 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700735 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400736 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700737
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700738 attr_ver = fuse_get_attr_version(fc);
739
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700740 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200741 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700742 req->num_pages = 1;
743 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400744 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400745 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700746 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700747
748 if (!err) {
749 /*
750 * Short read means EOF. If file size is larger, truncate it
751 */
752 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400753 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700755 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700756 }
757
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400758 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400759
760 return err;
761}
762
763static int fuse_readpage(struct file *file, struct page *page)
764{
765 struct inode *inode = page->mapping->host;
766 int err;
767
768 err = -EIO;
769 if (is_bad_inode(inode))
770 goto out;
771
772 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800773 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700774 out:
775 unlock_page(page);
776 return err;
777}
778
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800779static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700780{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800781 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700782 size_t count = req->misc.read.in.size;
783 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200784 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800785
Miklos Szeredice534fb2010-05-25 15:06:07 +0200786 for (i = 0; mapping == NULL && i < req->num_pages; i++)
787 mapping = req->pages[i]->mapping;
788
789 if (mapping) {
790 struct inode *inode = mapping->host;
791
792 /*
793 * Short read means EOF. If file size is larger, truncate it
794 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400795 if (!req->out.h.error && num_read < count)
796 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200797
Andrew Gallagher451418f2013-11-05 03:55:43 -0800798 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700799 }
800
Miklos Szeredidb50b962005-09-09 13:10:33 -0700801 for (i = 0; i < req->num_pages; i++) {
802 struct page *page = req->pages[i];
803 if (!req->out.h.error)
804 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800805 else
806 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700807 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200808 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700809 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700810 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100811 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800812}
813
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200814static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800815{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200816 struct fuse_file *ff = file->private_data;
817 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800818 loff_t pos = page_offset(req->pages[0]);
819 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200820
821 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800822 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200823 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200824 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700825 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800826 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700827 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800828 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100829 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800830 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100831 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800832 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100833 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800834 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700835}
836
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700837struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700838 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800839 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700840 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400841 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700842};
843
844static int fuse_readpages_fill(void *_data, struct page *page)
845{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700846 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700847 struct fuse_req *req = data->req;
848 struct inode *inode = data->inode;
849 struct fuse_conn *fc = get_fuse_conn(inode);
850
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700851 fuse_wait_on_page_writeback(inode, page->index);
852
Miklos Szeredidb50b962005-09-09 13:10:33 -0700853 if (req->num_pages &&
854 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
855 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
856 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400857 int nr_alloc = min_t(unsigned, data->nr_pages,
858 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200859 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400860 if (fc->async_read)
861 req = fuse_get_req_for_background(fc, nr_alloc);
862 else
863 req = fuse_get_req(fc, nr_alloc);
864
865 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700866 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700868 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700870 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400871
872 if (WARN_ON(req->num_pages >= req->max_pages)) {
873 fuse_put_request(fc, req);
874 return -EIO;
875 }
876
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200877 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700878 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400879 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100880 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400881 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700882 return 0;
883}
884
885static int fuse_readpages(struct file *file, struct address_space *mapping,
886 struct list_head *pages, unsigned nr_pages)
887{
888 struct inode *inode = mapping->host;
889 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700890 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700891 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400892 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800893
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700894 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800895 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800896 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800897
Miklos Szeredia6643092007-11-28 16:22:00 -0800898 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700899 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400900 if (fc->async_read)
901 data.req = fuse_get_req_for_background(fc, nr_alloc);
902 else
903 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400904 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700905 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700906 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800907 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700908
909 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700910 if (!err) {
911 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200912 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700913 else
914 fuse_put_request(fc, data.req);
915 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800916out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700917 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700918}
919
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800920static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
921 unsigned long nr_segs, loff_t pos)
922{
923 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400924 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800925
Brian Fostera8894272012-07-16 15:23:50 -0400926 /*
927 * In auto invalidate mode, always update attributes on read.
928 * Otherwise, only update if we attempt to read past EOF (to ensure
929 * i_size is up to date).
930 */
931 if (fc->auto_inval_data ||
932 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800933 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800934 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
935 if (err)
936 return err;
937 }
938
939 return generic_file_aio_read(iocb, iov, nr_segs, pos);
940}
941
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200942static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200943 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700944{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700945 struct fuse_write_in *inarg = &req->misc.write.in;
946 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700947
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700948 inarg->fh = ff->fh;
949 inarg->offset = pos;
950 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700951 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200952 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700953 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200954 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700955 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
956 else
957 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700958 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700959 req->in.args[1].size = count;
960 req->out.numargs = 1;
961 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700962 req->out.args[0].value = outarg;
963}
964
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400965static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200966 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700967{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400968 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200969 struct fuse_file *ff = file->private_data;
970 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200971 struct fuse_write_in *inarg = &req->misc.write.in;
972
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200973 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200974 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700975 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700976 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
977 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
978 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400979
980 if (io->async)
981 return fuse_async_req_send(fc, req, count, io);
982
Tejun Heob93f8582008-11-26 12:03:55 +0100983 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700984 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700985}
986
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400987bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700988{
989 struct fuse_conn *fc = get_fuse_conn(inode);
990 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400991 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700992
993 spin_lock(&fc->lock);
994 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400995 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700996 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400997 ret = true;
998 }
Miklos Szeredi854512e2008-04-30 00:54:41 -0700999 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001000
1001 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001002}
1003
Nick Pigginea9b9902008-04-30 00:54:42 -07001004static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1005 struct inode *inode, loff_t pos,
1006 size_t count)
1007{
1008 size_t res;
1009 unsigned offset;
1010 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001011 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -07001012
1013 for (i = 0; i < req->num_pages; i++)
1014 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1015
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001016 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001017
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001018 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001019 count = res;
1020 for (i = 0; i < req->num_pages; i++) {
1021 struct page *page = req->pages[i];
1022
1023 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1024 SetPageUptodate(page);
1025
1026 if (count > PAGE_CACHE_SIZE - offset)
1027 count -= PAGE_CACHE_SIZE - offset;
1028 else
1029 count = 0;
1030 offset = 0;
1031
1032 unlock_page(page);
1033 page_cache_release(page);
1034 }
1035
1036 return res;
1037}
1038
1039static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1040 struct address_space *mapping,
1041 struct iov_iter *ii, loff_t pos)
1042{
1043 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1044 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1045 size_t count = 0;
1046 int err;
1047
Miklos Szeredif4975c62009-04-02 14:25:34 +02001048 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001049 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001050
1051 do {
1052 size_t tmp;
1053 struct page *page;
1054 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1055 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1056 iov_iter_count(ii));
1057
1058 bytes = min_t(size_t, bytes, fc->max_write - count);
1059
1060 again:
1061 err = -EFAULT;
1062 if (iov_iter_fault_in_readable(ii, bytes))
1063 break;
1064
1065 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001066 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001067 if (!page)
1068 break;
1069
anfei zhou931e80e2010-02-02 13:44:02 -08001070 if (mapping_writably_mapped(mapping))
1071 flush_dcache_page(page);
1072
Nick Pigginea9b9902008-04-30 00:54:42 -07001073 pagefault_disable();
1074 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1075 pagefault_enable();
1076 flush_dcache_page(page);
1077
Johannes Weiner478e0842011-07-25 22:35:35 +02001078 mark_page_accessed(page);
1079
Nick Pigginea9b9902008-04-30 00:54:42 -07001080 if (!tmp) {
1081 unlock_page(page);
1082 page_cache_release(page);
1083 bytes = min(bytes, iov_iter_single_seg_count(ii));
1084 goto again;
1085 }
1086
1087 err = 0;
1088 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001089 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 req->num_pages++;
1091
1092 iov_iter_advance(ii, tmp);
1093 count += tmp;
1094 pos += tmp;
1095 offset += tmp;
1096 if (offset == PAGE_CACHE_SIZE)
1097 offset = 0;
1098
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001099 if (!fc->big_writes)
1100 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001101 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001102 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001103
1104 return count > 0 ? count : err;
1105}
1106
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001107static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1108{
1109 return min_t(unsigned,
1110 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1111 (pos >> PAGE_CACHE_SHIFT) + 1,
1112 FUSE_MAX_PAGES_PER_REQ);
1113}
1114
Nick Pigginea9b9902008-04-30 00:54:42 -07001115static ssize_t fuse_perform_write(struct file *file,
1116 struct address_space *mapping,
1117 struct iov_iter *ii, loff_t pos)
1118{
1119 struct inode *inode = mapping->host;
1120 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001121 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001122 int err = 0;
1123 ssize_t res = 0;
1124
1125 if (is_bad_inode(inode))
1126 return -EIO;
1127
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001128 if (inode->i_size < pos + iov_iter_count(ii))
1129 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1130
Nick Pigginea9b9902008-04-30 00:54:42 -07001131 do {
1132 struct fuse_req *req;
1133 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001134 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001135
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001136 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001137 if (IS_ERR(req)) {
1138 err = PTR_ERR(req);
1139 break;
1140 }
1141
1142 count = fuse_fill_write_pages(req, mapping, ii, pos);
1143 if (count <= 0) {
1144 err = count;
1145 } else {
1146 size_t num_written;
1147
1148 num_written = fuse_send_write_pages(req, file, inode,
1149 pos, count);
1150 err = req->out.h.error;
1151 if (!err) {
1152 res += num_written;
1153 pos += num_written;
1154
1155 /* break out of the loop on short write */
1156 if (num_written != count)
1157 err = -EIO;
1158 }
1159 }
1160 fuse_put_request(fc, req);
1161 } while (!err && iov_iter_count(ii));
1162
1163 if (res > 0)
1164 fuse_write_update_size(inode, pos);
1165
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001166 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001167 fuse_invalidate_attr(inode);
1168
1169 return res > 0 ? res : err;
1170}
1171
1172static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1173 unsigned long nr_segs, loff_t pos)
1174{
1175 struct file *file = iocb->ki_filp;
1176 struct address_space *mapping = file->f_mapping;
1177 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001178 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001179 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001180 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001181 struct inode *inode = mapping->host;
1182 ssize_t err;
1183 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001184 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001185
1186 WARN_ON(iocb->ki_pos != pos);
1187
Anand Avati4273b792012-02-17 12:46:25 -05001188 ocount = 0;
1189 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001190 if (err)
1191 return err;
1192
Anand Avati4273b792012-02-17 12:46:25 -05001193 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001194 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001195
1196 /* We can write back this queue in page reclaim */
1197 current->backing_dev_info = mapping->backing_dev_info;
1198
1199 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1200 if (err)
1201 goto out;
1202
1203 if (count == 0)
1204 goto out;
1205
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001206 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001207 if (err)
1208 goto out;
1209
Josef Bacikc3b2da32012-03-26 09:59:21 -04001210 err = file_update_time(file);
1211 if (err)
1212 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001213
Anand Avati4273b792012-02-17 12:46:25 -05001214 if (file->f_flags & O_DIRECT) {
1215 written = generic_file_direct_write(iocb, iov, &nr_segs,
1216 pos, &iocb->ki_pos,
1217 count, ocount);
1218 if (written < 0 || written == count)
1219 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001220
Anand Avati4273b792012-02-17 12:46:25 -05001221 pos += written;
1222 count -= written;
1223
1224 iov_iter_init(&i, iov, nr_segs, count, written);
1225 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1226 if (written_buffered < 0) {
1227 err = written_buffered;
1228 goto out;
1229 }
1230 endbyte = pos + written_buffered - 1;
1231
1232 err = filemap_write_and_wait_range(file->f_mapping, pos,
1233 endbyte);
1234 if (err)
1235 goto out;
1236
1237 invalidate_mapping_pages(file->f_mapping,
1238 pos >> PAGE_CACHE_SHIFT,
1239 endbyte >> PAGE_CACHE_SHIFT);
1240
1241 written += written_buffered;
1242 iocb->ki_pos = pos + written_buffered;
1243 } else {
1244 iov_iter_init(&i, iov, nr_segs, count, 0);
1245 written = fuse_perform_write(file, mapping, &i, pos);
1246 if (written >= 0)
1247 iocb->ki_pos = pos + written;
1248 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001249out:
1250 current->backing_dev_info = NULL;
1251 mutex_unlock(&inode->i_mutex);
1252
1253 return written ? written : err;
1254}
1255
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001256static inline void fuse_page_descs_length_init(struct fuse_req *req,
1257 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001258{
1259 int i;
1260
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001261 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001262 req->page_descs[i].length = PAGE_SIZE -
1263 req->page_descs[i].offset;
1264}
1265
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001266static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1267{
1268 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1269}
1270
1271static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1272 size_t max_size)
1273{
1274 return min(iov_iter_single_seg_count(ii), max_size);
1275}
1276
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001277static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001278 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001279{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001280 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001281
Miklos Szeredif4975c62009-04-02 14:25:34 +02001282 /* Special case for kernel I/O: can copy directly into the buffer */
1283 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001284 unsigned long user_addr = fuse_get_user_addr(ii);
1285 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1286
Miklos Szeredif4975c62009-04-02 14:25:34 +02001287 if (write)
1288 req->in.args[1].value = (void *) user_addr;
1289 else
1290 req->out.args[0].value = (void *) user_addr;
1291
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001292 iov_iter_advance(ii, frag_size);
1293 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001294 return 0;
1295 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001296
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001297 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001298 unsigned npages;
1299 unsigned long user_addr = fuse_get_user_addr(ii);
1300 unsigned offset = user_addr & ~PAGE_MASK;
1301 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1302 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001303
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001304 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001305 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1306
1307 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1308 npages = clamp(npages, 1U, n);
1309
1310 ret = get_user_pages_fast(user_addr, npages, !write,
1311 &req->pages[req->num_pages]);
1312 if (ret < 0)
1313 return ret;
1314
1315 npages = ret;
1316 frag_size = min_t(size_t, frag_size,
1317 (npages << PAGE_SHIFT) - offset);
1318 iov_iter_advance(ii, frag_size);
1319
1320 req->page_descs[req->num_pages].offset = offset;
1321 fuse_page_descs_length_init(req, req->num_pages, npages);
1322
1323 req->num_pages += npages;
1324 req->page_descs[req->num_pages - 1].length -=
1325 (npages << PAGE_SHIFT) - offset - frag_size;
1326
1327 nbytes += frag_size;
1328 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001329
1330 if (write)
1331 req->in.argpages = 1;
1332 else
1333 req->out.argpages = 1;
1334
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001335 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001336
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001337 return 0;
1338}
1339
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001340static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1341{
1342 struct iov_iter ii = *ii_p;
1343 int npages = 0;
1344
1345 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1346 unsigned long user_addr = fuse_get_user_addr(&ii);
1347 unsigned offset = user_addr & ~PAGE_MASK;
1348 size_t frag_size = iov_iter_single_seg_count(&ii);
1349
1350 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1351 iov_iter_advance(&ii, frag_size);
1352 }
1353
1354 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1355}
1356
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001357ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001358 unsigned long nr_segs, size_t count, loff_t *ppos,
1359 int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001360{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001361 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001362 struct fuse_file *ff = file->private_data;
1363 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001364 size_t nmax = write ? fc->max_write : fc->max_read;
1365 loff_t pos = *ppos;
1366 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001367 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001368 struct iov_iter ii;
1369
1370 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001371
Brian Fosterde82b922013-05-14 11:25:39 -04001372 if (io->async)
1373 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1374 else
1375 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001376 if (IS_ERR(req))
1377 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001378
1379 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001380 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001381 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001382 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001383 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001384 if (err) {
1385 res = err;
1386 break;
1387 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001388
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001389 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001390 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001391 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001392 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001393
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001394 if (!io->async)
1395 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396 if (req->out.h.error) {
1397 if (!res)
1398 res = req->out.h.error;
1399 break;
1400 } else if (nres > nbytes) {
1401 res = -EIO;
1402 break;
1403 }
1404 count -= nres;
1405 res += nres;
1406 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001407 if (nres != nbytes)
1408 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001409 if (count) {
1410 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001411 if (io->async)
1412 req = fuse_get_req_for_background(fc,
1413 fuse_iter_npages(&ii));
1414 else
1415 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001416 if (IS_ERR(req))
1417 break;
1418 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001419 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001420 if (!IS_ERR(req))
1421 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001422 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001423 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001424
1425 return res;
1426}
Tejun Heo08cbf542009-04-14 10:54:53 +09001427EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001428
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001429static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1430 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001431 unsigned long nr_segs, loff_t *ppos,
1432 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001433{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001434 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001435 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001436 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001437
1438 if (is_bad_inode(inode))
1439 return -EIO;
1440
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001441 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001442
1443 fuse_invalidate_attr(inode);
1444
1445 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001446}
1447
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001448static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1449 size_t count, loff_t *ppos)
1450{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001451 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001452 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001453 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001454}
1455
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001456static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1457 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001458 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001459{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001460 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001461 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001462 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001463 ssize_t res;
1464
1465 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001466 if (!res)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001467 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1);
Anand Avati4273b792012-02-17 12:46:25 -05001468
1469 fuse_invalidate_attr(inode);
1470
1471 return res;
1472}
1473
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001474static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1475 size_t count, loff_t *ppos)
1476{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001477 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001478 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001479 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001480 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001481
1482 if (is_bad_inode(inode))
1483 return -EIO;
1484
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001485 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001486 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001487 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001488 if (res > 0)
1489 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001490 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001491
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001492 return res;
1493}
1494
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001495static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001496{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001497 int i;
1498
1499 for (i = 0; i < req->num_pages; i++)
1500 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001501
1502 if (req->ff)
1503 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001504}
1505
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001506static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001507{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001508 struct inode *inode = req->inode;
1509 struct fuse_inode *fi = get_fuse_inode(inode);
1510 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001511 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001512
1513 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001514 for (i = 0; i < req->num_pages; i++) {
1515 dec_bdi_stat(bdi, BDI_WRITEBACK);
1516 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1517 bdi_writeout_inc(bdi);
1518 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001519 wake_up(&fi->page_waitq);
1520}
1521
1522/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001523static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1524 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001525__releases(fc->lock)
1526__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001527{
1528 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001529 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001530 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001531
1532 if (!fc->connected)
1533 goto out_free;
1534
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001535 if (inarg->offset + data_size <= size) {
1536 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001537 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001538 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001539 } else {
1540 /* Got truncated off completely */
1541 goto out_free;
1542 }
1543
1544 req->in.args[1].size = inarg->size;
1545 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001546 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001547 return;
1548
1549 out_free:
1550 fuse_writepage_finish(fc, req);
1551 spin_unlock(&fc->lock);
1552 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001553 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001554 spin_lock(&fc->lock);
1555}
1556
1557/*
1558 * If fi->writectr is positive (no truncate or fsync going on) send
1559 * all queued writepage requests.
1560 *
1561 * Called with fc->lock
1562 */
1563void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001564__releases(fc->lock)
1565__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001566{
1567 struct fuse_conn *fc = get_fuse_conn(inode);
1568 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001569 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001570 struct fuse_req *req;
1571
1572 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1573 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1574 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001575 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001576 }
1577}
1578
1579static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1580{
1581 struct inode *inode = req->inode;
1582 struct fuse_inode *fi = get_fuse_inode(inode);
1583
1584 mapping_set_error(inode->i_mapping, req->out.h.error);
1585 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001586 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001587 struct fuse_conn *fc = get_fuse_conn(inode);
1588 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001589 struct fuse_req *next = req->misc.write.next;
1590 req->misc.write.next = next->misc.write.next;
1591 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001592 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001593 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001594
1595 /*
1596 * Skip fuse_flush_writepages() to make it easy to crop requests
1597 * based on primary request size.
1598 *
1599 * 1st case (trivial): there are no concurrent activities using
1600 * fuse_set/release_nowrite. Then we're on safe side because
1601 * fuse_flush_writepages() would call fuse_send_writepage()
1602 * anyway.
1603 *
1604 * 2nd case: someone called fuse_set_nowrite and it is waiting
1605 * now for completion of all in-flight requests. This happens
1606 * rarely and no more than once per page, so this should be
1607 * okay.
1608 *
1609 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1610 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1611 * that fuse_set_nowrite returned implies that all in-flight
1612 * requests were completed along with all of their secondary
1613 * requests. Further primary requests are blocked by negative
1614 * writectr. Hence there cannot be any in-flight requests and
1615 * no invocations of fuse_writepage_end() while we're in
1616 * fuse_set_nowrite..fuse_release_nowrite section.
1617 */
1618 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001619 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001620 fi->writectr--;
1621 fuse_writepage_finish(fc, req);
1622 spin_unlock(&fc->lock);
1623 fuse_writepage_free(fc, req);
1624}
1625
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001626static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1627 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001628{
Miklos Szeredi72523422013-10-01 16:44:52 +02001629 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001630
1631 spin_lock(&fc->lock);
Miklos Szeredi72523422013-10-01 16:44:52 +02001632 if (!WARN_ON(list_empty(&fi->write_files))) {
1633 ff = list_entry(fi->write_files.next, struct fuse_file,
1634 write_entry);
1635 fuse_file_get(ff);
1636 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001637 spin_unlock(&fc->lock);
1638
1639 return ff;
1640}
1641
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001642static int fuse_writepage_locked(struct page *page)
1643{
1644 struct address_space *mapping = page->mapping;
1645 struct inode *inode = mapping->host;
1646 struct fuse_conn *fc = get_fuse_conn(inode);
1647 struct fuse_inode *fi = get_fuse_inode(inode);
1648 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001649 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001650 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001651
1652 set_page_writeback(page);
1653
Maxim Patlasov4250c062012-10-26 19:48:07 +04001654 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001655 if (!req)
1656 goto err;
1657
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001658 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001659 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1660 if (!tmp_page)
1661 goto err_free;
1662
Miklos Szeredi72523422013-10-01 16:44:52 +02001663 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001664 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001665 if (!req->ff)
1666 goto err_free;
1667
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001668 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001669
1670 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001671 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001672 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001673 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001674 req->num_pages = 1;
1675 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001676 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001677 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001678 req->end = fuse_writepage_end;
1679 req->inode = inode;
1680
1681 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1682 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001683
1684 spin_lock(&fc->lock);
1685 list_add(&req->writepages_entry, &fi->writepages);
1686 list_add_tail(&req->list, &fi->queued_writes);
1687 fuse_flush_writepages(inode);
1688 spin_unlock(&fc->lock);
1689
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001690 end_page_writeback(page);
1691
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001692 return 0;
1693
1694err_free:
1695 fuse_request_free(req);
1696err:
1697 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001698 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001699}
1700
1701static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1702{
1703 int err;
1704
Miklos Szerediff17be02013-10-01 16:44:53 +02001705 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1706 /*
1707 * ->writepages() should be called for sync() and friends. We
1708 * should only get here on direct reclaim and then we are
1709 * allowed to skip a page which is already in flight
1710 */
1711 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1712
1713 redirty_page_for_writepage(wbc, page);
1714 return 0;
1715 }
1716
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001717 err = fuse_writepage_locked(page);
1718 unlock_page(page);
1719
1720 return err;
1721}
1722
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001723struct fuse_fill_wb_data {
1724 struct fuse_req *req;
1725 struct fuse_file *ff;
1726 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001727 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001728};
1729
1730static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1731{
1732 struct fuse_req *req = data->req;
1733 struct inode *inode = data->inode;
1734 struct fuse_conn *fc = get_fuse_conn(inode);
1735 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001736 int num_pages = req->num_pages;
1737 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001738
1739 req->ff = fuse_file_get(data->ff);
1740 spin_lock(&fc->lock);
1741 list_add_tail(&req->list, &fi->queued_writes);
1742 fuse_flush_writepages(inode);
1743 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001744
1745 for (i = 0; i < num_pages; i++)
1746 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001747}
1748
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001749static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1750 struct page *page)
1751{
1752 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1753 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1754 struct fuse_req *tmp;
1755 struct fuse_req *old_req;
1756 bool found = false;
1757 pgoff_t curr_index;
1758
1759 BUG_ON(new_req->num_pages != 0);
1760
1761 spin_lock(&fc->lock);
1762 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001763 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1764 BUG_ON(old_req->inode != new_req->inode);
1765 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1766 if (curr_index <= page->index &&
1767 page->index < curr_index + old_req->num_pages) {
1768 found = true;
1769 break;
1770 }
1771 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001772 if (!found) {
1773 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001774 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001775 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001776
Maxim Patlasovf6011082013-10-02 15:01:07 +04001777 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001778 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1779 BUG_ON(tmp->inode != new_req->inode);
1780 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1781 if (tmp->num_pages == 1 &&
1782 curr_index == page->index) {
1783 old_req = tmp;
1784 }
1785 }
1786
1787 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1788 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001789 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1790
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001791 copy_highpage(old_req->pages[0], page);
1792 spin_unlock(&fc->lock);
1793
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001794 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001795 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001796 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001797 fuse_writepage_free(fc, new_req);
1798 fuse_request_free(new_req);
1799 goto out;
1800 } else {
1801 new_req->misc.write.next = old_req->misc.write.next;
1802 old_req->misc.write.next = new_req;
1803 }
1804out_unlock:
1805 spin_unlock(&fc->lock);
1806out:
1807 return found;
1808}
1809
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001810static int fuse_writepages_fill(struct page *page,
1811 struct writeback_control *wbc, void *_data)
1812{
1813 struct fuse_fill_wb_data *data = _data;
1814 struct fuse_req *req = data->req;
1815 struct inode *inode = data->inode;
1816 struct fuse_conn *fc = get_fuse_conn(inode);
1817 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001818 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001819 int err;
1820
1821 if (!data->ff) {
1822 err = -EIO;
1823 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1824 if (!data->ff)
1825 goto out_unlock;
1826 }
1827
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001828 /*
1829 * Being under writeback is unlikely but possible. For example direct
1830 * read to an mmaped fuse file will set the page dirty twice; once when
1831 * the pages are faulted with get_user_pages(), and then after the read
1832 * completed.
1833 */
1834 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001835
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001836 if (req && req->num_pages &&
1837 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1838 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1839 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1840 fuse_writepages_send(data);
1841 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001842 }
1843 err = -ENOMEM;
1844 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1845 if (!tmp_page)
1846 goto out_unlock;
1847
1848 /*
1849 * The page must not be redirtied until the writeout is completed
1850 * (i.e. userspace has sent a reply to the write request). Otherwise
1851 * there could be more than one temporary page instance for each real
1852 * page.
1853 *
1854 * This is ensured by holding the page lock in page_mkwrite() while
1855 * checking fuse_page_is_writeback(). We already hold the page lock
1856 * since clear_page_dirty_for_io() and keep it held until we add the
1857 * request to the fi->writepages list and increment req->num_pages.
1858 * After this fuse_page_is_writeback() will indicate that the page is
1859 * under writeback, so we can release the page lock.
1860 */
1861 if (data->req == NULL) {
1862 struct fuse_inode *fi = get_fuse_inode(inode);
1863
1864 err = -ENOMEM;
1865 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1866 if (!req) {
1867 __free_page(tmp_page);
1868 goto out_unlock;
1869 }
1870
1871 fuse_write_fill(req, data->ff, page_offset(page), 0);
1872 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001873 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001874 req->in.argpages = 1;
1875 req->background = 1;
1876 req->num_pages = 0;
1877 req->end = fuse_writepage_end;
1878 req->inode = inode;
1879
1880 spin_lock(&fc->lock);
1881 list_add(&req->writepages_entry, &fi->writepages);
1882 spin_unlock(&fc->lock);
1883
1884 data->req = req;
1885 }
1886 set_page_writeback(page);
1887
1888 copy_highpage(tmp_page, page);
1889 req->pages[req->num_pages] = tmp_page;
1890 req->page_descs[req->num_pages].offset = 0;
1891 req->page_descs[req->num_pages].length = PAGE_SIZE;
1892
1893 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1894 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001895
1896 err = 0;
1897 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1898 end_page_writeback(page);
1899 data->req = NULL;
1900 goto out_unlock;
1901 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001902 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001903
1904 /*
1905 * Protected by fc->lock against concurrent access by
1906 * fuse_page_is_writeback().
1907 */
1908 spin_lock(&fc->lock);
1909 req->num_pages++;
1910 spin_unlock(&fc->lock);
1911
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001912out_unlock:
1913 unlock_page(page);
1914
1915 return err;
1916}
1917
1918static int fuse_writepages(struct address_space *mapping,
1919 struct writeback_control *wbc)
1920{
1921 struct inode *inode = mapping->host;
1922 struct fuse_fill_wb_data data;
1923 int err;
1924
1925 err = -EIO;
1926 if (is_bad_inode(inode))
1927 goto out;
1928
1929 data.inode = inode;
1930 data.req = NULL;
1931 data.ff = NULL;
1932
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001933 err = -ENOMEM;
1934 data.orig_pages = kzalloc(sizeof(struct page *) *
1935 FUSE_MAX_PAGES_PER_REQ,
1936 GFP_NOFS);
1937 if (!data.orig_pages)
1938 goto out;
1939
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001940 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1941 if (data.req) {
1942 /* Ignore errors if we can write at least one page */
1943 BUG_ON(!data.req->num_pages);
1944 fuse_writepages_send(&data);
1945 err = 0;
1946 }
1947 if (data.ff)
1948 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001949
1950 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001951out:
1952 return err;
1953}
1954
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001955/*
1956 * It's worthy to make sure that space is reserved on disk for the write,
1957 * but how to implement it without killing performance need more thinking.
1958 */
1959static int fuse_write_begin(struct file *file, struct address_space *mapping,
1960 loff_t pos, unsigned len, unsigned flags,
1961 struct page **pagep, void **fsdata)
1962{
1963 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1964 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
1965 struct page *page;
1966 loff_t fsize;
1967 int err = -ENOMEM;
1968
1969 WARN_ON(!fc->writeback_cache);
1970
1971 page = grab_cache_page_write_begin(mapping, index, flags);
1972 if (!page)
1973 goto error;
1974
1975 fuse_wait_on_page_writeback(mapping->host, page->index);
1976
1977 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
1978 goto success;
1979 /*
1980 * Check if the start this page comes after the end of file, in which
1981 * case the readpage can be optimized away.
1982 */
1983 fsize = i_size_read(mapping->host);
1984 if (fsize <= (pos & PAGE_CACHE_MASK)) {
1985 size_t off = pos & ~PAGE_CACHE_MASK;
1986 if (off)
1987 zero_user_segment(page, 0, off);
1988 goto success;
1989 }
1990 err = fuse_do_readpage(file, page);
1991 if (err)
1992 goto cleanup;
1993success:
1994 *pagep = page;
1995 return 0;
1996
1997cleanup:
1998 unlock_page(page);
1999 page_cache_release(page);
2000error:
2001 return err;
2002}
2003
2004static int fuse_write_end(struct file *file, struct address_space *mapping,
2005 loff_t pos, unsigned len, unsigned copied,
2006 struct page *page, void *fsdata)
2007{
2008 struct inode *inode = page->mapping->host;
2009
2010 if (!PageUptodate(page)) {
2011 /* Zero any unwritten bytes at the end of the page */
2012 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2013 if (endoff)
2014 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2015 SetPageUptodate(page);
2016 }
2017
2018 fuse_write_update_size(inode, pos + copied);
2019 set_page_dirty(page);
2020 unlock_page(page);
2021 page_cache_release(page);
2022
2023 return copied;
2024}
2025
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002026static int fuse_launder_page(struct page *page)
2027{
2028 int err = 0;
2029 if (clear_page_dirty_for_io(page)) {
2030 struct inode *inode = page->mapping->host;
2031 err = fuse_writepage_locked(page);
2032 if (!err)
2033 fuse_wait_on_page_writeback(inode, page->index);
2034 }
2035 return err;
2036}
2037
2038/*
2039 * Write back dirty pages now, because there may not be any suitable
2040 * open files later
2041 */
2042static void fuse_vma_close(struct vm_area_struct *vma)
2043{
2044 filemap_write_and_wait(vma->vm_file->f_mapping);
2045}
2046
2047/*
2048 * Wait for writeback against this page to complete before allowing it
2049 * to be marked dirty again, and hence written back again, possibly
2050 * before the previous writepage completed.
2051 *
2052 * Block here, instead of in ->writepage(), so that the userspace fs
2053 * can only block processes actually operating on the filesystem.
2054 *
2055 * Otherwise unprivileged userspace fs would be able to block
2056 * unrelated:
2057 *
2058 * - page migration
2059 * - sync(2)
2060 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2061 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002062static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002063{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002064 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002065 struct inode *inode = file_inode(vma->vm_file);
2066
2067 file_update_time(vma->vm_file);
2068 lock_page(page);
2069 if (page->mapping != inode->i_mapping) {
2070 unlock_page(page);
2071 return VM_FAULT_NOPAGE;
2072 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002073
2074 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002075 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002076}
2077
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002078static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002079 .close = fuse_vma_close,
2080 .fault = filemap_fault,
2081 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002082 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002083};
2084
2085static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2086{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002087 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2088 fuse_link_write_file(file);
2089
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002090 file_accessed(file);
2091 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002092 return 0;
2093}
2094
Miklos Szeredifc280c92009-04-02 14:25:35 +02002095static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2096{
2097 /* Can't provide the coherency needed for MAP_SHARED */
2098 if (vma->vm_flags & VM_MAYSHARE)
2099 return -ENODEV;
2100
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002101 invalidate_inode_pages2(file->f_mapping);
2102
Miklos Szeredifc280c92009-04-02 14:25:35 +02002103 return generic_file_mmap(file, vma);
2104}
2105
Miklos Szeredi71421252006-06-25 05:48:52 -07002106static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2107 struct file_lock *fl)
2108{
2109 switch (ffl->type) {
2110 case F_UNLCK:
2111 break;
2112
2113 case F_RDLCK:
2114 case F_WRLCK:
2115 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2116 ffl->end < ffl->start)
2117 return -EIO;
2118
2119 fl->fl_start = ffl->start;
2120 fl->fl_end = ffl->end;
2121 fl->fl_pid = ffl->pid;
2122 break;
2123
2124 default:
2125 return -EIO;
2126 }
2127 fl->fl_type = ffl->type;
2128 return 0;
2129}
2130
2131static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002132 const struct file_lock *fl, int opcode, pid_t pid,
2133 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002134{
Al Viro6131ffa2013-02-27 16:59:05 -05002135 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002136 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002137 struct fuse_file *ff = file->private_data;
2138 struct fuse_lk_in *arg = &req->misc.lk_in;
2139
2140 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002141 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002142 arg->lk.start = fl->fl_start;
2143 arg->lk.end = fl->fl_end;
2144 arg->lk.type = fl->fl_type;
2145 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002146 if (flock)
2147 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002148 req->in.h.opcode = opcode;
2149 req->in.h.nodeid = get_node_id(inode);
2150 req->in.numargs = 1;
2151 req->in.args[0].size = sizeof(*arg);
2152 req->in.args[0].value = arg;
2153}
2154
2155static int fuse_getlk(struct file *file, struct file_lock *fl)
2156{
Al Viro6131ffa2013-02-27 16:59:05 -05002157 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002158 struct fuse_conn *fc = get_fuse_conn(inode);
2159 struct fuse_req *req;
2160 struct fuse_lk_out outarg;
2161 int err;
2162
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002163 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002164 if (IS_ERR(req))
2165 return PTR_ERR(req);
2166
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002167 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002168 req->out.numargs = 1;
2169 req->out.args[0].size = sizeof(outarg);
2170 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002171 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002172 err = req->out.h.error;
2173 fuse_put_request(fc, req);
2174 if (!err)
2175 err = convert_fuse_file_lock(&outarg.lk, fl);
2176
2177 return err;
2178}
2179
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002180static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002181{
Al Viro6131ffa2013-02-27 16:59:05 -05002182 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002183 struct fuse_conn *fc = get_fuse_conn(inode);
2184 struct fuse_req *req;
2185 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2186 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2187 int err;
2188
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002189 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002190 /* NLM needs asynchronous locks, which we don't support yet */
2191 return -ENOLCK;
2192 }
2193
Miklos Szeredi71421252006-06-25 05:48:52 -07002194 /* Unlock on close is handled by the flush method */
2195 if (fl->fl_flags & FL_CLOSE)
2196 return 0;
2197
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002198 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002199 if (IS_ERR(req))
2200 return PTR_ERR(req);
2201
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002202 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002203 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002204 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002205 /* locking is restartable */
2206 if (err == -EINTR)
2207 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002208 fuse_put_request(fc, req);
2209 return err;
2210}
2211
2212static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2213{
Al Viro6131ffa2013-02-27 16:59:05 -05002214 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002215 struct fuse_conn *fc = get_fuse_conn(inode);
2216 int err;
2217
Miklos Szeredi48e90762008-07-25 01:49:02 -07002218 if (cmd == F_CANCELLK) {
2219 err = 0;
2220 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002221 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002222 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002223 err = 0;
2224 } else
2225 err = fuse_getlk(file, fl);
2226 } else {
2227 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002228 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002229 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002230 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002231 }
2232 return err;
2233}
2234
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002235static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2236{
Al Viro6131ffa2013-02-27 16:59:05 -05002237 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002238 struct fuse_conn *fc = get_fuse_conn(inode);
2239 int err;
2240
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002241 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002242 err = flock_lock_file_wait(file, fl);
2243 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002244 struct fuse_file *ff = file->private_data;
2245
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002246 /* emulate flock with POSIX locks */
2247 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002248 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002249 err = fuse_setlk(file, fl, 1);
2250 }
2251
2252 return err;
2253}
2254
Miklos Szeredib2d22722006-12-06 20:35:51 -08002255static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2256{
2257 struct inode *inode = mapping->host;
2258 struct fuse_conn *fc = get_fuse_conn(inode);
2259 struct fuse_req *req;
2260 struct fuse_bmap_in inarg;
2261 struct fuse_bmap_out outarg;
2262 int err;
2263
2264 if (!inode->i_sb->s_bdev || fc->no_bmap)
2265 return 0;
2266
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002267 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002268 if (IS_ERR(req))
2269 return 0;
2270
2271 memset(&inarg, 0, sizeof(inarg));
2272 inarg.block = block;
2273 inarg.blocksize = inode->i_sb->s_blocksize;
2274 req->in.h.opcode = FUSE_BMAP;
2275 req->in.h.nodeid = get_node_id(inode);
2276 req->in.numargs = 1;
2277 req->in.args[0].size = sizeof(inarg);
2278 req->in.args[0].value = &inarg;
2279 req->out.numargs = 1;
2280 req->out.args[0].size = sizeof(outarg);
2281 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002282 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002283 err = req->out.h.error;
2284 fuse_put_request(fc, req);
2285 if (err == -ENOSYS)
2286 fc->no_bmap = 1;
2287
2288 return err ? 0 : outarg.block;
2289}
2290
Andrew Morton965c8e52012-12-17 15:59:39 -08002291static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002292{
2293 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002294 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002295
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002296 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002297 if (whence == SEEK_CUR || whence == SEEK_SET)
2298 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002299
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002300 mutex_lock(&inode->i_mutex);
2301 retval = fuse_update_attributes(inode, NULL, file, NULL);
2302 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002303 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002304 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002305
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002306 return retval;
2307}
2308
Tejun Heo59efec72008-11-26 12:03:55 +01002309static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2310 unsigned int nr_segs, size_t bytes, bool to_user)
2311{
2312 struct iov_iter ii;
2313 int page_idx = 0;
2314
2315 if (!bytes)
2316 return 0;
2317
2318 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2319
2320 while (iov_iter_count(&ii)) {
2321 struct page *page = pages[page_idx++];
2322 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002323 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002324
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002325 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002326
2327 while (todo) {
2328 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2329 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2330 size_t copy = min(todo, iov_len);
2331 size_t left;
2332
2333 if (!to_user)
2334 left = copy_from_user(kaddr, uaddr, copy);
2335 else
2336 left = copy_to_user(uaddr, kaddr, copy);
2337
2338 if (unlikely(left))
2339 return -EFAULT;
2340
2341 iov_iter_advance(&ii, copy);
2342 todo -= copy;
2343 kaddr += copy;
2344 }
2345
Jens Axboe0bd87182009-11-03 11:40:44 +01002346 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002347 }
2348
2349 return 0;
2350}
2351
2352/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002353 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2354 * ABI was defined to be 'struct iovec' which is different on 32bit
2355 * and 64bit. Fortunately we can determine which structure the server
2356 * used from the size of the reply.
2357 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002358static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2359 size_t transferred, unsigned count,
2360 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002361{
2362#ifdef CONFIG_COMPAT
2363 if (count * sizeof(struct compat_iovec) == transferred) {
2364 struct compat_iovec *ciov = src;
2365 unsigned i;
2366
2367 /*
2368 * With this interface a 32bit server cannot support
2369 * non-compat (i.e. ones coming from 64bit apps) ioctl
2370 * requests
2371 */
2372 if (!is_compat)
2373 return -EINVAL;
2374
2375 for (i = 0; i < count; i++) {
2376 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2377 dst[i].iov_len = ciov[i].iov_len;
2378 }
2379 return 0;
2380 }
2381#endif
2382
2383 if (count * sizeof(struct iovec) != transferred)
2384 return -EIO;
2385
2386 memcpy(dst, src, transferred);
2387 return 0;
2388}
2389
Miklos Szeredi75727772010-11-30 16:39:27 +01002390/* Make sure iov_length() won't overflow */
2391static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2392{
2393 size_t n;
2394 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2395
Zach Brownfb6ccff2012-07-24 12:10:11 -07002396 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002397 if (iov->iov_len > (size_t) max)
2398 return -ENOMEM;
2399 max -= iov->iov_len;
2400 }
2401 return 0;
2402}
2403
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002404static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2405 void *src, size_t transferred, unsigned count,
2406 bool is_compat)
2407{
2408 unsigned i;
2409 struct fuse_ioctl_iovec *fiov = src;
2410
2411 if (fc->minor < 16) {
2412 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2413 count, is_compat);
2414 }
2415
2416 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2417 return -EIO;
2418
2419 for (i = 0; i < count; i++) {
2420 /* Did the server supply an inappropriate value? */
2421 if (fiov[i].base != (unsigned long) fiov[i].base ||
2422 fiov[i].len != (unsigned long) fiov[i].len)
2423 return -EIO;
2424
2425 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2426 dst[i].iov_len = (size_t) fiov[i].len;
2427
2428#ifdef CONFIG_COMPAT
2429 if (is_compat &&
2430 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2431 (compat_size_t) dst[i].iov_len != fiov[i].len))
2432 return -EIO;
2433#endif
2434 }
2435
2436 return 0;
2437}
2438
2439
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002440/*
Tejun Heo59efec72008-11-26 12:03:55 +01002441 * For ioctls, there is no generic way to determine how much memory
2442 * needs to be read and/or written. Furthermore, ioctls are allowed
2443 * to dereference the passed pointer, so the parameter requires deep
2444 * copying but FUSE has no idea whatsoever about what to copy in or
2445 * out.
2446 *
2447 * This is solved by allowing FUSE server to retry ioctl with
2448 * necessary in/out iovecs. Let's assume the ioctl implementation
2449 * needs to read in the following structure.
2450 *
2451 * struct a {
2452 * char *buf;
2453 * size_t buflen;
2454 * }
2455 *
2456 * On the first callout to FUSE server, inarg->in_size and
2457 * inarg->out_size will be NULL; then, the server completes the ioctl
2458 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2459 * the actual iov array to
2460 *
2461 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2462 *
2463 * which tells FUSE to copy in the requested area and retry the ioctl.
2464 * On the second round, the server has access to the structure and
2465 * from that it can tell what to look for next, so on the invocation,
2466 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2467 *
2468 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2469 * { .iov_base = a.buf, .iov_len = a.buflen } }
2470 *
2471 * FUSE will copy both struct a and the pointed buffer from the
2472 * process doing the ioctl and retry ioctl with both struct a and the
2473 * buffer.
2474 *
2475 * This time, FUSE server has everything it needs and completes ioctl
2476 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2477 *
2478 * Copying data out works the same way.
2479 *
2480 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2481 * automatically initializes in and out iovs by decoding @cmd with
2482 * _IOC_* macros and the server is not allowed to request RETRY. This
2483 * limits ioctl data transfers to well-formed ioctls and is the forced
2484 * behavior for all FUSE servers.
2485 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002486long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2487 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002488{
Tejun Heo59efec72008-11-26 12:03:55 +01002489 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002490 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002491 struct fuse_ioctl_in inarg = {
2492 .fh = ff->fh,
2493 .cmd = cmd,
2494 .arg = arg,
2495 .flags = flags
2496 };
2497 struct fuse_ioctl_out outarg;
2498 struct fuse_req *req = NULL;
2499 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002500 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002501 struct iovec *in_iov = NULL, *out_iov = NULL;
2502 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2503 size_t in_size, out_size, transferred;
2504 int err;
2505
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002506#if BITS_PER_LONG == 32
2507 inarg.flags |= FUSE_IOCTL_32BIT;
2508#else
2509 if (flags & FUSE_IOCTL_COMPAT)
2510 inarg.flags |= FUSE_IOCTL_32BIT;
2511#endif
2512
Tejun Heo59efec72008-11-26 12:03:55 +01002513 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002514 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002515
Tejun Heo59efec72008-11-26 12:03:55 +01002516 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002517 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002518 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002519 if (!pages || !iov_page)
2520 goto out;
2521
2522 /*
2523 * If restricted, initialize IO parameters as encoded in @cmd.
2524 * RETRY from server is not allowed.
2525 */
2526 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002527 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002528
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002529 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002530 iov->iov_len = _IOC_SIZE(cmd);
2531
2532 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2533 in_iov = iov;
2534 in_iovs = 1;
2535 }
2536
2537 if (_IOC_DIR(cmd) & _IOC_READ) {
2538 out_iov = iov;
2539 out_iovs = 1;
2540 }
2541 }
2542
2543 retry:
2544 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2545 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2546
2547 /*
2548 * Out data can be used either for actual out data or iovs,
2549 * make sure there always is at least one page.
2550 */
2551 out_size = max_t(size_t, out_size, PAGE_SIZE);
2552 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2553
2554 /* make sure there are enough buffer pages and init request with them */
2555 err = -ENOMEM;
2556 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2557 goto out;
2558 while (num_pages < max_pages) {
2559 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2560 if (!pages[num_pages])
2561 goto out;
2562 num_pages++;
2563 }
2564
Maxim Patlasov54b96672012-10-26 19:49:13 +04002565 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002566 if (IS_ERR(req)) {
2567 err = PTR_ERR(req);
2568 req = NULL;
2569 goto out;
2570 }
2571 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2572 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002573 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002574
2575 /* okay, let's send it to the client */
2576 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002577 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002578 req->in.numargs = 1;
2579 req->in.args[0].size = sizeof(inarg);
2580 req->in.args[0].value = &inarg;
2581 if (in_size) {
2582 req->in.numargs++;
2583 req->in.args[1].size = in_size;
2584 req->in.argpages = 1;
2585
2586 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2587 false);
2588 if (err)
2589 goto out;
2590 }
2591
2592 req->out.numargs = 2;
2593 req->out.args[0].size = sizeof(outarg);
2594 req->out.args[0].value = &outarg;
2595 req->out.args[1].size = out_size;
2596 req->out.argpages = 1;
2597 req->out.argvar = 1;
2598
Tejun Heob93f8582008-11-26 12:03:55 +01002599 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002600 err = req->out.h.error;
2601 transferred = req->out.args[1].size;
2602 fuse_put_request(fc, req);
2603 req = NULL;
2604 if (err)
2605 goto out;
2606
2607 /* did it ask for retry? */
2608 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002609 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002610
2611 /* no retry if in restricted mode */
2612 err = -EIO;
2613 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2614 goto out;
2615
2616 in_iovs = outarg.in_iovs;
2617 out_iovs = outarg.out_iovs;
2618
2619 /*
2620 * Make sure things are in boundary, separate checks
2621 * are to protect against overflow.
2622 */
2623 err = -ENOMEM;
2624 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2625 out_iovs > FUSE_IOCTL_MAX_IOV ||
2626 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2627 goto out;
2628
Cong Wang2408f6e2011-11-25 23:14:30 +08002629 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002630 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002631 transferred, in_iovs + out_iovs,
2632 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002633 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002634 if (err)
2635 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002636
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002637 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002638 out_iov = in_iov + in_iovs;
2639
Miklos Szeredi75727772010-11-30 16:39:27 +01002640 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2641 if (err)
2642 goto out;
2643
2644 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2645 if (err)
2646 goto out;
2647
Tejun Heo59efec72008-11-26 12:03:55 +01002648 goto retry;
2649 }
2650
2651 err = -EIO;
2652 if (transferred > inarg.out_size)
2653 goto out;
2654
2655 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2656 out:
2657 if (req)
2658 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002659 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002660 while (num_pages)
2661 __free_page(pages[--num_pages]);
2662 kfree(pages);
2663
2664 return err ? err : outarg.result;
2665}
Tejun Heo08cbf542009-04-14 10:54:53 +09002666EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002667
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002668long fuse_ioctl_common(struct file *file, unsigned int cmd,
2669 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002670{
Al Viro6131ffa2013-02-27 16:59:05 -05002671 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002672 struct fuse_conn *fc = get_fuse_conn(inode);
2673
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002674 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002675 return -EACCES;
2676
2677 if (is_bad_inode(inode))
2678 return -EIO;
2679
2680 return fuse_do_ioctl(file, cmd, arg, flags);
2681}
2682
Tejun Heo59efec72008-11-26 12:03:55 +01002683static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2684 unsigned long arg)
2685{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002686 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002687}
2688
2689static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2690 unsigned long arg)
2691{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002692 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002693}
2694
Tejun Heo95668a62008-11-26 12:03:55 +01002695/*
2696 * All files which have been polled are linked to RB tree
2697 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2698 * find the matching one.
2699 */
2700static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2701 struct rb_node **parent_out)
2702{
2703 struct rb_node **link = &fc->polled_files.rb_node;
2704 struct rb_node *last = NULL;
2705
2706 while (*link) {
2707 struct fuse_file *ff;
2708
2709 last = *link;
2710 ff = rb_entry(last, struct fuse_file, polled_node);
2711
2712 if (kh < ff->kh)
2713 link = &last->rb_left;
2714 else if (kh > ff->kh)
2715 link = &last->rb_right;
2716 else
2717 return link;
2718 }
2719
2720 if (parent_out)
2721 *parent_out = last;
2722 return link;
2723}
2724
2725/*
2726 * The file is about to be polled. Make sure it's on the polled_files
2727 * RB tree. Note that files once added to the polled_files tree are
2728 * not removed before the file is released. This is because a file
2729 * polled once is likely to be polled again.
2730 */
2731static void fuse_register_polled_file(struct fuse_conn *fc,
2732 struct fuse_file *ff)
2733{
2734 spin_lock(&fc->lock);
2735 if (RB_EMPTY_NODE(&ff->polled_node)) {
2736 struct rb_node **link, *parent;
2737
2738 link = fuse_find_polled_node(fc, ff->kh, &parent);
2739 BUG_ON(*link);
2740 rb_link_node(&ff->polled_node, parent, link);
2741 rb_insert_color(&ff->polled_node, &fc->polled_files);
2742 }
2743 spin_unlock(&fc->lock);
2744}
2745
Tejun Heo08cbf542009-04-14 10:54:53 +09002746unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002747{
Tejun Heo95668a62008-11-26 12:03:55 +01002748 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002749 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002750 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2751 struct fuse_poll_out outarg;
2752 struct fuse_req *req;
2753 int err;
2754
2755 if (fc->no_poll)
2756 return DEFAULT_POLLMASK;
2757
2758 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002759 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002760
2761 /*
2762 * Ask for notification iff there's someone waiting for it.
2763 * The client may ignore the flag and always notify.
2764 */
2765 if (waitqueue_active(&ff->poll_wait)) {
2766 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2767 fuse_register_polled_file(fc, ff);
2768 }
2769
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002770 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002771 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002772 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002773
2774 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002775 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002776 req->in.numargs = 1;
2777 req->in.args[0].size = sizeof(inarg);
2778 req->in.args[0].value = &inarg;
2779 req->out.numargs = 1;
2780 req->out.args[0].size = sizeof(outarg);
2781 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002782 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002783 err = req->out.h.error;
2784 fuse_put_request(fc, req);
2785
2786 if (!err)
2787 return outarg.revents;
2788 if (err == -ENOSYS) {
2789 fc->no_poll = 1;
2790 return DEFAULT_POLLMASK;
2791 }
2792 return POLLERR;
2793}
Tejun Heo08cbf542009-04-14 10:54:53 +09002794EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002795
2796/*
2797 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2798 * wakes up the poll waiters.
2799 */
2800int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2801 struct fuse_notify_poll_wakeup_out *outarg)
2802{
2803 u64 kh = outarg->kh;
2804 struct rb_node **link;
2805
2806 spin_lock(&fc->lock);
2807
2808 link = fuse_find_polled_node(fc, kh, NULL);
2809 if (*link) {
2810 struct fuse_file *ff;
2811
2812 ff = rb_entry(*link, struct fuse_file, polled_node);
2813 wake_up_interruptible_sync(&ff->poll_wait);
2814 }
2815
2816 spin_unlock(&fc->lock);
2817 return 0;
2818}
2819
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002820static void fuse_do_truncate(struct file *file)
2821{
2822 struct inode *inode = file->f_mapping->host;
2823 struct iattr attr;
2824
2825 attr.ia_valid = ATTR_SIZE;
2826 attr.ia_size = i_size_read(inode);
2827
2828 attr.ia_file = file;
2829 attr.ia_valid |= ATTR_FILE;
2830
2831 fuse_do_setattr(inode, &attr, file);
2832}
2833
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002834static inline loff_t fuse_round_up(loff_t off)
2835{
2836 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2837}
2838
Anand Avati4273b792012-02-17 12:46:25 -05002839static ssize_t
2840fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2841 loff_t offset, unsigned long nr_segs)
2842{
2843 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002844 struct file *file = iocb->ki_filp;
2845 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002846 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002847 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002848 struct inode *inode;
2849 loff_t i_size;
2850 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002851 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002852
Anand Avati4273b792012-02-17 12:46:25 -05002853 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002854 inode = file->f_mapping->host;
2855 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002856
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002857 if ((rw == READ) && (offset > i_size))
2858 return 0;
2859
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002860 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002861 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002862 if (offset >= i_size)
2863 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002864 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002865 }
2866
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002867 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002868 if (!io)
2869 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002870 spin_lock_init(&io->lock);
2871 io->reqs = 1;
2872 io->bytes = -1;
2873 io->size = 0;
2874 io->offset = offset;
2875 io->write = (rw == WRITE);
2876 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002877 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002878 /*
2879 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002880 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002881 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002882 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002883 io->iocb = iocb;
2884
2885 /*
2886 * We cannot asynchronously extend the size of a file. We have no method
2887 * to wait on real async I/O requests, so we must submit this request
2888 * synchronously.
2889 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002890 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002891 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002892
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002893 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002894 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002895 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002896 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002897
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002898 if (io->async) {
2899 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2900
2901 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002902 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002903 return -EIOCBQUEUED;
2904
2905 ret = wait_on_sync_kiocb(iocb);
2906 } else {
2907 kfree(io);
2908 }
2909
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002910 if (rw == WRITE) {
2911 if (ret > 0)
2912 fuse_write_update_size(inode, pos);
2913 else if (ret < 0 && offset + count > i_size)
2914 fuse_do_truncate(file);
2915 }
Anand Avati4273b792012-02-17 12:46:25 -05002916
2917 return ret;
2918}
2919
Miklos Szeredicdadb112012-11-10 16:55:56 +01002920static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2921 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002922{
2923 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002924 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002925 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002926 struct fuse_conn *fc = ff->fc;
2927 struct fuse_req *req;
2928 struct fuse_fallocate_in inarg = {
2929 .fh = ff->fh,
2930 .offset = offset,
2931 .length = length,
2932 .mode = mode
2933 };
2934 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002935 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2936 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002937
Miklos Szeredi519c6042012-04-26 10:56:36 +02002938 if (fc->no_fallocate)
2939 return -EOPNOTSUPP;
2940
Maxim Patlasov14c14412013-06-13 12:16:39 +04002941 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002942 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002943 if (mode & FALLOC_FL_PUNCH_HOLE) {
2944 loff_t endbyte = offset + length - 1;
2945 err = filemap_write_and_wait_range(inode->i_mapping,
2946 offset, endbyte);
2947 if (err)
2948 goto out;
2949
2950 fuse_sync_writes(inode);
2951 }
Brian Foster3634a632013-05-17 09:30:32 -04002952 }
2953
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002954 if (!(mode & FALLOC_FL_KEEP_SIZE))
2955 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2956
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002957 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04002958 if (IS_ERR(req)) {
2959 err = PTR_ERR(req);
2960 goto out;
2961 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002962
2963 req->in.h.opcode = FUSE_FALLOCATE;
2964 req->in.h.nodeid = ff->nodeid;
2965 req->in.numargs = 1;
2966 req->in.args[0].size = sizeof(inarg);
2967 req->in.args[0].value = &inarg;
2968 fuse_request_send(fc, req);
2969 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002970 if (err == -ENOSYS) {
2971 fc->no_fallocate = 1;
2972 err = -EOPNOTSUPP;
2973 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002974 fuse_put_request(fc, req);
2975
Brian Fosterbee6c302013-05-17 15:27:34 -04002976 if (err)
2977 goto out;
2978
2979 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002980 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2981 bool changed = fuse_write_update_size(inode, offset + length);
2982
2983 if (changed && fc->writeback_cache) {
2984 struct fuse_inode *fi = get_fuse_inode(inode);
2985
2986 inode->i_mtime = current_fs_time(inode->i_sb);
2987 set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
2988 }
2989 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002990
2991 if (mode & FALLOC_FL_PUNCH_HOLE)
2992 truncate_pagecache_range(inode, offset, offset + length - 1);
2993
2994 fuse_invalidate_attr(inode);
2995
Brian Foster3634a632013-05-17 09:30:32 -04002996out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002997 if (!(mode & FALLOC_FL_KEEP_SIZE))
2998 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2999
Maxim Patlasovbde52782013-09-13 19:19:54 +04003000 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04003001 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04003002
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003003 return err;
3004}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003005
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003006static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003007 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003008 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08003009 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003010 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07003011 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003012 .mmap = fuse_file_mmap,
3013 .open = fuse_open,
3014 .flush = fuse_flush,
3015 .release = fuse_release,
3016 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003017 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003018 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003019 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003020 .unlocked_ioctl = fuse_file_ioctl,
3021 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003022 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003023 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003024};
3025
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003026static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003027 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003028 .read = fuse_direct_read,
3029 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003030 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003031 .open = fuse_open,
3032 .flush = fuse_flush,
3033 .release = fuse_release,
3034 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003035 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003036 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003037 .unlocked_ioctl = fuse_file_ioctl,
3038 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003039 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003040 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003041 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003042};
3043
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003044static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003045 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003046 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003047 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003048 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003049 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003050 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003051 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003052 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003053 .write_begin = fuse_write_begin,
3054 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003055};
3056
3057void fuse_init_file_inode(struct inode *inode)
3058{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003059 inode->i_fop = &fuse_file_operations;
3060 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003061}