blob: 530b1e804a327392725a8a15c6d0a367bcdfc5c1 [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
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700715static int fuse_readpage(struct file *file, struct page *page)
716{
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
727 err = -EIO;
728 if (is_bad_inode(inode))
729 goto out;
730
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700731 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300732 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700733 * page-cache page, so make sure we read a properly synced
734 * page.
735 */
736 fuse_wait_on_page_writeback(inode, page->index);
737
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400738 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700739 err = PTR_ERR(req);
740 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700741 goto out;
742
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700743 attr_ver = fuse_get_attr_version(fc);
744
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200746 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700747 req->num_pages = 1;
748 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400749 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400750 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700751 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700752
753 if (!err) {
754 /*
755 * Short read means EOF. If file size is larger, truncate it
756 */
757 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400758 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700759
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700760 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700761 }
762
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400763 fuse_put_request(fc, req);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800764 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700765 out:
766 unlock_page(page);
767 return err;
768}
769
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800770static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700771{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800772 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700773 size_t count = req->misc.read.in.size;
774 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200775 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800776
Miklos Szeredice534fb2010-05-25 15:06:07 +0200777 for (i = 0; mapping == NULL && i < req->num_pages; i++)
778 mapping = req->pages[i]->mapping;
779
780 if (mapping) {
781 struct inode *inode = mapping->host;
782
783 /*
784 * Short read means EOF. If file size is larger, truncate it
785 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400786 if (!req->out.h.error && num_read < count)
787 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200788
Andrew Gallagher451418f2013-11-05 03:55:43 -0800789 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700790 }
791
Miklos Szeredidb50b962005-09-09 13:10:33 -0700792 for (i = 0; i < req->num_pages; i++) {
793 struct page *page = req->pages[i];
794 if (!req->out.h.error)
795 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800796 else
797 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700798 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200799 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700800 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700801 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100802 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800803}
804
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200805static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800806{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200807 struct fuse_file *ff = file->private_data;
808 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800809 loff_t pos = page_offset(req->pages[0]);
810 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200811
812 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800813 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200814 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200815 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700816 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800817 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700818 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800819 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100820 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800821 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100822 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800823 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100824 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800825 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700826}
827
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700828struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700829 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800830 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700831 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400832 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700833};
834
835static int fuse_readpages_fill(void *_data, struct page *page)
836{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700837 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700838 struct fuse_req *req = data->req;
839 struct inode *inode = data->inode;
840 struct fuse_conn *fc = get_fuse_conn(inode);
841
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700842 fuse_wait_on_page_writeback(inode, page->index);
843
Miklos Szeredidb50b962005-09-09 13:10:33 -0700844 if (req->num_pages &&
845 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
846 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
847 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400848 int nr_alloc = min_t(unsigned, data->nr_pages,
849 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200850 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400851 if (fc->async_read)
852 req = fuse_get_req_for_background(fc, nr_alloc);
853 else
854 req = fuse_get_req(fc, nr_alloc);
855
856 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700857 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700859 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700860 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700861 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400862
863 if (WARN_ON(req->num_pages >= req->max_pages)) {
864 fuse_put_request(fc, req);
865 return -EIO;
866 }
867
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200868 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400870 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100871 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400872 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700873 return 0;
874}
875
876static int fuse_readpages(struct file *file, struct address_space *mapping,
877 struct list_head *pages, unsigned nr_pages)
878{
879 struct inode *inode = mapping->host;
880 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700881 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700882 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400883 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800884
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700885 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800886 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800887 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800888
Miklos Szeredia6643092007-11-28 16:22:00 -0800889 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700890 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400891 if (fc->async_read)
892 data.req = fuse_get_req_for_background(fc, nr_alloc);
893 else
894 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400895 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700896 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700897 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800898 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700899
900 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700901 if (!err) {
902 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200903 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700904 else
905 fuse_put_request(fc, data.req);
906 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800907out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700908 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700909}
910
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800911static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
912 unsigned long nr_segs, loff_t pos)
913{
914 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400915 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800916
Brian Fostera8894272012-07-16 15:23:50 -0400917 /*
918 * In auto invalidate mode, always update attributes on read.
919 * Otherwise, only update if we attempt to read past EOF (to ensure
920 * i_size is up to date).
921 */
922 if (fc->auto_inval_data ||
923 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800924 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800925 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
926 if (err)
927 return err;
928 }
929
930 return generic_file_aio_read(iocb, iov, nr_segs, pos);
931}
932
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200933static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200934 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700935{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700936 struct fuse_write_in *inarg = &req->misc.write.in;
937 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700938
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700939 inarg->fh = ff->fh;
940 inarg->offset = pos;
941 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700942 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200943 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700944 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200945 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700946 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
947 else
948 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700949 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700950 req->in.args[1].size = count;
951 req->out.numargs = 1;
952 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700953 req->out.args[0].value = outarg;
954}
955
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400956static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200957 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700958{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400959 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200960 struct fuse_file *ff = file->private_data;
961 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200962 struct fuse_write_in *inarg = &req->misc.write.in;
963
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200964 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200965 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700966 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700967 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
968 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
969 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400970
971 if (io->async)
972 return fuse_async_req_send(fc, req, count, io);
973
Tejun Heob93f8582008-11-26 12:03:55 +0100974 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700975 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700976}
977
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400978bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700979{
980 struct fuse_conn *fc = get_fuse_conn(inode);
981 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400982 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700983
984 spin_lock(&fc->lock);
985 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400986 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -0700987 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400988 ret = true;
989 }
Miklos Szeredi854512e2008-04-30 00:54:41 -0700990 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400991
992 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700993}
994
Nick Pigginea9b9902008-04-30 00:54:42 -0700995static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
996 struct inode *inode, loff_t pos,
997 size_t count)
998{
999 size_t res;
1000 unsigned offset;
1001 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001002 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -07001003
1004 for (i = 0; i < req->num_pages; i++)
1005 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1006
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001007 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001008
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001009 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001010 count = res;
1011 for (i = 0; i < req->num_pages; i++) {
1012 struct page *page = req->pages[i];
1013
1014 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1015 SetPageUptodate(page);
1016
1017 if (count > PAGE_CACHE_SIZE - offset)
1018 count -= PAGE_CACHE_SIZE - offset;
1019 else
1020 count = 0;
1021 offset = 0;
1022
1023 unlock_page(page);
1024 page_cache_release(page);
1025 }
1026
1027 return res;
1028}
1029
1030static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1031 struct address_space *mapping,
1032 struct iov_iter *ii, loff_t pos)
1033{
1034 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1035 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1036 size_t count = 0;
1037 int err;
1038
Miklos Szeredif4975c62009-04-02 14:25:34 +02001039 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001040 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001041
1042 do {
1043 size_t tmp;
1044 struct page *page;
1045 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1046 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1047 iov_iter_count(ii));
1048
1049 bytes = min_t(size_t, bytes, fc->max_write - count);
1050
1051 again:
1052 err = -EFAULT;
1053 if (iov_iter_fault_in_readable(ii, bytes))
1054 break;
1055
1056 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001057 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001058 if (!page)
1059 break;
1060
anfei zhou931e80e2010-02-02 13:44:02 -08001061 if (mapping_writably_mapped(mapping))
1062 flush_dcache_page(page);
1063
Nick Pigginea9b9902008-04-30 00:54:42 -07001064 pagefault_disable();
1065 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1066 pagefault_enable();
1067 flush_dcache_page(page);
1068
Johannes Weiner478e0842011-07-25 22:35:35 +02001069 mark_page_accessed(page);
1070
Nick Pigginea9b9902008-04-30 00:54:42 -07001071 if (!tmp) {
1072 unlock_page(page);
1073 page_cache_release(page);
1074 bytes = min(bytes, iov_iter_single_seg_count(ii));
1075 goto again;
1076 }
1077
1078 err = 0;
1079 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001080 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001081 req->num_pages++;
1082
1083 iov_iter_advance(ii, tmp);
1084 count += tmp;
1085 pos += tmp;
1086 offset += tmp;
1087 if (offset == PAGE_CACHE_SIZE)
1088 offset = 0;
1089
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001090 if (!fc->big_writes)
1091 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001092 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001093 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001094
1095 return count > 0 ? count : err;
1096}
1097
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001098static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1099{
1100 return min_t(unsigned,
1101 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1102 (pos >> PAGE_CACHE_SHIFT) + 1,
1103 FUSE_MAX_PAGES_PER_REQ);
1104}
1105
Nick Pigginea9b9902008-04-30 00:54:42 -07001106static ssize_t fuse_perform_write(struct file *file,
1107 struct address_space *mapping,
1108 struct iov_iter *ii, loff_t pos)
1109{
1110 struct inode *inode = mapping->host;
1111 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001112 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001113 int err = 0;
1114 ssize_t res = 0;
1115
1116 if (is_bad_inode(inode))
1117 return -EIO;
1118
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001119 if (inode->i_size < pos + iov_iter_count(ii))
1120 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1121
Nick Pigginea9b9902008-04-30 00:54:42 -07001122 do {
1123 struct fuse_req *req;
1124 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001125 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001126
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001127 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001128 if (IS_ERR(req)) {
1129 err = PTR_ERR(req);
1130 break;
1131 }
1132
1133 count = fuse_fill_write_pages(req, mapping, ii, pos);
1134 if (count <= 0) {
1135 err = count;
1136 } else {
1137 size_t num_written;
1138
1139 num_written = fuse_send_write_pages(req, file, inode,
1140 pos, count);
1141 err = req->out.h.error;
1142 if (!err) {
1143 res += num_written;
1144 pos += num_written;
1145
1146 /* break out of the loop on short write */
1147 if (num_written != count)
1148 err = -EIO;
1149 }
1150 }
1151 fuse_put_request(fc, req);
1152 } while (!err && iov_iter_count(ii));
1153
1154 if (res > 0)
1155 fuse_write_update_size(inode, pos);
1156
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001157 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001158 fuse_invalidate_attr(inode);
1159
1160 return res > 0 ? res : err;
1161}
1162
1163static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1164 unsigned long nr_segs, loff_t pos)
1165{
1166 struct file *file = iocb->ki_filp;
1167 struct address_space *mapping = file->f_mapping;
1168 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001169 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001170 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001171 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001172 struct inode *inode = mapping->host;
1173 ssize_t err;
1174 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001175 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001176
1177 WARN_ON(iocb->ki_pos != pos);
1178
Anand Avati4273b792012-02-17 12:46:25 -05001179 ocount = 0;
1180 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001181 if (err)
1182 return err;
1183
Anand Avati4273b792012-02-17 12:46:25 -05001184 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001185 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001186
1187 /* We can write back this queue in page reclaim */
1188 current->backing_dev_info = mapping->backing_dev_info;
1189
1190 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1191 if (err)
1192 goto out;
1193
1194 if (count == 0)
1195 goto out;
1196
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001197 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001198 if (err)
1199 goto out;
1200
Josef Bacikc3b2da32012-03-26 09:59:21 -04001201 err = file_update_time(file);
1202 if (err)
1203 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001204
Anand Avati4273b792012-02-17 12:46:25 -05001205 if (file->f_flags & O_DIRECT) {
1206 written = generic_file_direct_write(iocb, iov, &nr_segs,
1207 pos, &iocb->ki_pos,
1208 count, ocount);
1209 if (written < 0 || written == count)
1210 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001211
Anand Avati4273b792012-02-17 12:46:25 -05001212 pos += written;
1213 count -= written;
1214
1215 iov_iter_init(&i, iov, nr_segs, count, written);
1216 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1217 if (written_buffered < 0) {
1218 err = written_buffered;
1219 goto out;
1220 }
1221 endbyte = pos + written_buffered - 1;
1222
1223 err = filemap_write_and_wait_range(file->f_mapping, pos,
1224 endbyte);
1225 if (err)
1226 goto out;
1227
1228 invalidate_mapping_pages(file->f_mapping,
1229 pos >> PAGE_CACHE_SHIFT,
1230 endbyte >> PAGE_CACHE_SHIFT);
1231
1232 written += written_buffered;
1233 iocb->ki_pos = pos + written_buffered;
1234 } else {
1235 iov_iter_init(&i, iov, nr_segs, count, 0);
1236 written = fuse_perform_write(file, mapping, &i, pos);
1237 if (written >= 0)
1238 iocb->ki_pos = pos + written;
1239 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001240out:
1241 current->backing_dev_info = NULL;
1242 mutex_unlock(&inode->i_mutex);
1243
1244 return written ? written : err;
1245}
1246
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001247static inline void fuse_page_descs_length_init(struct fuse_req *req,
1248 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001249{
1250 int i;
1251
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001252 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001253 req->page_descs[i].length = PAGE_SIZE -
1254 req->page_descs[i].offset;
1255}
1256
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001257static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1258{
1259 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1260}
1261
1262static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1263 size_t max_size)
1264{
1265 return min(iov_iter_single_seg_count(ii), max_size);
1266}
1267
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001268static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001269 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001270{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001271 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001272
Miklos Szeredif4975c62009-04-02 14:25:34 +02001273 /* Special case for kernel I/O: can copy directly into the buffer */
1274 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001275 unsigned long user_addr = fuse_get_user_addr(ii);
1276 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1277
Miklos Szeredif4975c62009-04-02 14:25:34 +02001278 if (write)
1279 req->in.args[1].value = (void *) user_addr;
1280 else
1281 req->out.args[0].value = (void *) user_addr;
1282
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001283 iov_iter_advance(ii, frag_size);
1284 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001285 return 0;
1286 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001287
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001288 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001289 unsigned npages;
1290 unsigned long user_addr = fuse_get_user_addr(ii);
1291 unsigned offset = user_addr & ~PAGE_MASK;
1292 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1293 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001294
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001295 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001296 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1297
1298 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1299 npages = clamp(npages, 1U, n);
1300
1301 ret = get_user_pages_fast(user_addr, npages, !write,
1302 &req->pages[req->num_pages]);
1303 if (ret < 0)
1304 return ret;
1305
1306 npages = ret;
1307 frag_size = min_t(size_t, frag_size,
1308 (npages << PAGE_SHIFT) - offset);
1309 iov_iter_advance(ii, frag_size);
1310
1311 req->page_descs[req->num_pages].offset = offset;
1312 fuse_page_descs_length_init(req, req->num_pages, npages);
1313
1314 req->num_pages += npages;
1315 req->page_descs[req->num_pages - 1].length -=
1316 (npages << PAGE_SHIFT) - offset - frag_size;
1317
1318 nbytes += frag_size;
1319 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001320
1321 if (write)
1322 req->in.argpages = 1;
1323 else
1324 req->out.argpages = 1;
1325
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001326 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001327
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001328 return 0;
1329}
1330
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001331static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1332{
1333 struct iov_iter ii = *ii_p;
1334 int npages = 0;
1335
1336 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1337 unsigned long user_addr = fuse_get_user_addr(&ii);
1338 unsigned offset = user_addr & ~PAGE_MASK;
1339 size_t frag_size = iov_iter_single_seg_count(&ii);
1340
1341 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1342 iov_iter_advance(&ii, frag_size);
1343 }
1344
1345 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1346}
1347
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001348ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001349 unsigned long nr_segs, size_t count, loff_t *ppos,
1350 int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001351{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001352 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001353 struct fuse_file *ff = file->private_data;
1354 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001355 size_t nmax = write ? fc->max_write : fc->max_read;
1356 loff_t pos = *ppos;
1357 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001358 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001359 struct iov_iter ii;
1360
1361 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001362
Brian Fosterde82b922013-05-14 11:25:39 -04001363 if (io->async)
1364 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1365 else
1366 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001367 if (IS_ERR(req))
1368 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001369
1370 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001371 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001372 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001373 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001374 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 if (err) {
1376 res = err;
1377 break;
1378 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001379
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001380 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001381 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001382 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001383 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001384
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001385 if (!io->async)
1386 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001387 if (req->out.h.error) {
1388 if (!res)
1389 res = req->out.h.error;
1390 break;
1391 } else if (nres > nbytes) {
1392 res = -EIO;
1393 break;
1394 }
1395 count -= nres;
1396 res += nres;
1397 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001398 if (nres != nbytes)
1399 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001400 if (count) {
1401 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001402 if (io->async)
1403 req = fuse_get_req_for_background(fc,
1404 fuse_iter_npages(&ii));
1405 else
1406 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001407 if (IS_ERR(req))
1408 break;
1409 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001410 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001411 if (!IS_ERR(req))
1412 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001413 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001414 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001415
1416 return res;
1417}
Tejun Heo08cbf542009-04-14 10:54:53 +09001418EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001419
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001420static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1421 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001422 unsigned long nr_segs, loff_t *ppos,
1423 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001424{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001425 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001426 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001427 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001428
1429 if (is_bad_inode(inode))
1430 return -EIO;
1431
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001432 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001433
1434 fuse_invalidate_attr(inode);
1435
1436 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001437}
1438
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001439static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1440 size_t count, loff_t *ppos)
1441{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001442 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001443 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001444 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001445}
1446
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001447static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1448 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001449 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001450{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001451 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001452 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001453 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001454 ssize_t res;
1455
1456 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001457 if (!res)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001458 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1);
Anand Avati4273b792012-02-17 12:46:25 -05001459
1460 fuse_invalidate_attr(inode);
1461
1462 return res;
1463}
1464
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001465static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1466 size_t count, loff_t *ppos)
1467{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001468 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001469 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001470 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001471 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001472
1473 if (is_bad_inode(inode))
1474 return -EIO;
1475
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001476 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001477 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001478 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001479 if (res > 0)
1480 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001481 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001482
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001483 return res;
1484}
1485
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001486static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001487{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001488 int i;
1489
1490 for (i = 0; i < req->num_pages; i++)
1491 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001492
1493 if (req->ff)
1494 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001495}
1496
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001497static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001498{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001499 struct inode *inode = req->inode;
1500 struct fuse_inode *fi = get_fuse_inode(inode);
1501 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001502 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001503
1504 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001505 for (i = 0; i < req->num_pages; i++) {
1506 dec_bdi_stat(bdi, BDI_WRITEBACK);
1507 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1508 bdi_writeout_inc(bdi);
1509 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001510 wake_up(&fi->page_waitq);
1511}
1512
1513/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001514static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1515 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001516__releases(fc->lock)
1517__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001518{
1519 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001520 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001521 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001522
1523 if (!fc->connected)
1524 goto out_free;
1525
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001526 if (inarg->offset + data_size <= size) {
1527 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001528 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001529 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001530 } else {
1531 /* Got truncated off completely */
1532 goto out_free;
1533 }
1534
1535 req->in.args[1].size = inarg->size;
1536 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001537 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001538 return;
1539
1540 out_free:
1541 fuse_writepage_finish(fc, req);
1542 spin_unlock(&fc->lock);
1543 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001544 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001545 spin_lock(&fc->lock);
1546}
1547
1548/*
1549 * If fi->writectr is positive (no truncate or fsync going on) send
1550 * all queued writepage requests.
1551 *
1552 * Called with fc->lock
1553 */
1554void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001555__releases(fc->lock)
1556__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001557{
1558 struct fuse_conn *fc = get_fuse_conn(inode);
1559 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001560 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001561 struct fuse_req *req;
1562
1563 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1564 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1565 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001566 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001567 }
1568}
1569
1570static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1571{
1572 struct inode *inode = req->inode;
1573 struct fuse_inode *fi = get_fuse_inode(inode);
1574
1575 mapping_set_error(inode->i_mapping, req->out.h.error);
1576 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001577 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001578 struct fuse_conn *fc = get_fuse_conn(inode);
1579 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001580 struct fuse_req *next = req->misc.write.next;
1581 req->misc.write.next = next->misc.write.next;
1582 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001583 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001584 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001585
1586 /*
1587 * Skip fuse_flush_writepages() to make it easy to crop requests
1588 * based on primary request size.
1589 *
1590 * 1st case (trivial): there are no concurrent activities using
1591 * fuse_set/release_nowrite. Then we're on safe side because
1592 * fuse_flush_writepages() would call fuse_send_writepage()
1593 * anyway.
1594 *
1595 * 2nd case: someone called fuse_set_nowrite and it is waiting
1596 * now for completion of all in-flight requests. This happens
1597 * rarely and no more than once per page, so this should be
1598 * okay.
1599 *
1600 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1601 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1602 * that fuse_set_nowrite returned implies that all in-flight
1603 * requests were completed along with all of their secondary
1604 * requests. Further primary requests are blocked by negative
1605 * writectr. Hence there cannot be any in-flight requests and
1606 * no invocations of fuse_writepage_end() while we're in
1607 * fuse_set_nowrite..fuse_release_nowrite section.
1608 */
1609 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001610 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001611 fi->writectr--;
1612 fuse_writepage_finish(fc, req);
1613 spin_unlock(&fc->lock);
1614 fuse_writepage_free(fc, req);
1615}
1616
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001617static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1618 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001619{
Miklos Szeredi72523422013-10-01 16:44:52 +02001620 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001621
1622 spin_lock(&fc->lock);
Miklos Szeredi72523422013-10-01 16:44:52 +02001623 if (!WARN_ON(list_empty(&fi->write_files))) {
1624 ff = list_entry(fi->write_files.next, struct fuse_file,
1625 write_entry);
1626 fuse_file_get(ff);
1627 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001628 spin_unlock(&fc->lock);
1629
1630 return ff;
1631}
1632
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001633static int fuse_writepage_locked(struct page *page)
1634{
1635 struct address_space *mapping = page->mapping;
1636 struct inode *inode = mapping->host;
1637 struct fuse_conn *fc = get_fuse_conn(inode);
1638 struct fuse_inode *fi = get_fuse_inode(inode);
1639 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001640 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001641 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001642
1643 set_page_writeback(page);
1644
Maxim Patlasov4250c062012-10-26 19:48:07 +04001645 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001646 if (!req)
1647 goto err;
1648
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001649 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001650 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1651 if (!tmp_page)
1652 goto err_free;
1653
Miklos Szeredi72523422013-10-01 16:44:52 +02001654 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001655 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001656 if (!req->ff)
1657 goto err_free;
1658
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001659 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001660
1661 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001662 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001663 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001664 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001665 req->num_pages = 1;
1666 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001667 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001668 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001669 req->end = fuse_writepage_end;
1670 req->inode = inode;
1671
1672 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1673 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001674
1675 spin_lock(&fc->lock);
1676 list_add(&req->writepages_entry, &fi->writepages);
1677 list_add_tail(&req->list, &fi->queued_writes);
1678 fuse_flush_writepages(inode);
1679 spin_unlock(&fc->lock);
1680
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001681 end_page_writeback(page);
1682
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001683 return 0;
1684
1685err_free:
1686 fuse_request_free(req);
1687err:
1688 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001689 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001690}
1691
1692static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1693{
1694 int err;
1695
Miklos Szerediff17be02013-10-01 16:44:53 +02001696 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1697 /*
1698 * ->writepages() should be called for sync() and friends. We
1699 * should only get here on direct reclaim and then we are
1700 * allowed to skip a page which is already in flight
1701 */
1702 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1703
1704 redirty_page_for_writepage(wbc, page);
1705 return 0;
1706 }
1707
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001708 err = fuse_writepage_locked(page);
1709 unlock_page(page);
1710
1711 return err;
1712}
1713
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001714struct fuse_fill_wb_data {
1715 struct fuse_req *req;
1716 struct fuse_file *ff;
1717 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001718 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001719};
1720
1721static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1722{
1723 struct fuse_req *req = data->req;
1724 struct inode *inode = data->inode;
1725 struct fuse_conn *fc = get_fuse_conn(inode);
1726 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001727 int num_pages = req->num_pages;
1728 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001729
1730 req->ff = fuse_file_get(data->ff);
1731 spin_lock(&fc->lock);
1732 list_add_tail(&req->list, &fi->queued_writes);
1733 fuse_flush_writepages(inode);
1734 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001735
1736 for (i = 0; i < num_pages; i++)
1737 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001738}
1739
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001740static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1741 struct page *page)
1742{
1743 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1744 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1745 struct fuse_req *tmp;
1746 struct fuse_req *old_req;
1747 bool found = false;
1748 pgoff_t curr_index;
1749
1750 BUG_ON(new_req->num_pages != 0);
1751
1752 spin_lock(&fc->lock);
1753 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001754 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1755 BUG_ON(old_req->inode != new_req->inode);
1756 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1757 if (curr_index <= page->index &&
1758 page->index < curr_index + old_req->num_pages) {
1759 found = true;
1760 break;
1761 }
1762 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001763 if (!found) {
1764 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001765 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001766 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001767
Maxim Patlasovf6011082013-10-02 15:01:07 +04001768 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001769 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1770 BUG_ON(tmp->inode != new_req->inode);
1771 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1772 if (tmp->num_pages == 1 &&
1773 curr_index == page->index) {
1774 old_req = tmp;
1775 }
1776 }
1777
1778 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1779 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001780 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1781
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001782 copy_highpage(old_req->pages[0], page);
1783 spin_unlock(&fc->lock);
1784
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001785 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001786 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001787 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001788 fuse_writepage_free(fc, new_req);
1789 fuse_request_free(new_req);
1790 goto out;
1791 } else {
1792 new_req->misc.write.next = old_req->misc.write.next;
1793 old_req->misc.write.next = new_req;
1794 }
1795out_unlock:
1796 spin_unlock(&fc->lock);
1797out:
1798 return found;
1799}
1800
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001801static int fuse_writepages_fill(struct page *page,
1802 struct writeback_control *wbc, void *_data)
1803{
1804 struct fuse_fill_wb_data *data = _data;
1805 struct fuse_req *req = data->req;
1806 struct inode *inode = data->inode;
1807 struct fuse_conn *fc = get_fuse_conn(inode);
1808 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001809 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001810 int err;
1811
1812 if (!data->ff) {
1813 err = -EIO;
1814 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1815 if (!data->ff)
1816 goto out_unlock;
1817 }
1818
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001819 /*
1820 * Being under writeback is unlikely but possible. For example direct
1821 * read to an mmaped fuse file will set the page dirty twice; once when
1822 * the pages are faulted with get_user_pages(), and then after the read
1823 * completed.
1824 */
1825 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001826
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001827 if (req && req->num_pages &&
1828 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1829 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1830 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1831 fuse_writepages_send(data);
1832 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001833 }
1834 err = -ENOMEM;
1835 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1836 if (!tmp_page)
1837 goto out_unlock;
1838
1839 /*
1840 * The page must not be redirtied until the writeout is completed
1841 * (i.e. userspace has sent a reply to the write request). Otherwise
1842 * there could be more than one temporary page instance for each real
1843 * page.
1844 *
1845 * This is ensured by holding the page lock in page_mkwrite() while
1846 * checking fuse_page_is_writeback(). We already hold the page lock
1847 * since clear_page_dirty_for_io() and keep it held until we add the
1848 * request to the fi->writepages list and increment req->num_pages.
1849 * After this fuse_page_is_writeback() will indicate that the page is
1850 * under writeback, so we can release the page lock.
1851 */
1852 if (data->req == NULL) {
1853 struct fuse_inode *fi = get_fuse_inode(inode);
1854
1855 err = -ENOMEM;
1856 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1857 if (!req) {
1858 __free_page(tmp_page);
1859 goto out_unlock;
1860 }
1861
1862 fuse_write_fill(req, data->ff, page_offset(page), 0);
1863 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001864 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001865 req->in.argpages = 1;
1866 req->background = 1;
1867 req->num_pages = 0;
1868 req->end = fuse_writepage_end;
1869 req->inode = inode;
1870
1871 spin_lock(&fc->lock);
1872 list_add(&req->writepages_entry, &fi->writepages);
1873 spin_unlock(&fc->lock);
1874
1875 data->req = req;
1876 }
1877 set_page_writeback(page);
1878
1879 copy_highpage(tmp_page, page);
1880 req->pages[req->num_pages] = tmp_page;
1881 req->page_descs[req->num_pages].offset = 0;
1882 req->page_descs[req->num_pages].length = PAGE_SIZE;
1883
1884 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1885 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001886
1887 err = 0;
1888 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1889 end_page_writeback(page);
1890 data->req = NULL;
1891 goto out_unlock;
1892 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001893 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001894
1895 /*
1896 * Protected by fc->lock against concurrent access by
1897 * fuse_page_is_writeback().
1898 */
1899 spin_lock(&fc->lock);
1900 req->num_pages++;
1901 spin_unlock(&fc->lock);
1902
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001903out_unlock:
1904 unlock_page(page);
1905
1906 return err;
1907}
1908
1909static int fuse_writepages(struct address_space *mapping,
1910 struct writeback_control *wbc)
1911{
1912 struct inode *inode = mapping->host;
1913 struct fuse_fill_wb_data data;
1914 int err;
1915
1916 err = -EIO;
1917 if (is_bad_inode(inode))
1918 goto out;
1919
1920 data.inode = inode;
1921 data.req = NULL;
1922 data.ff = NULL;
1923
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001924 err = -ENOMEM;
1925 data.orig_pages = kzalloc(sizeof(struct page *) *
1926 FUSE_MAX_PAGES_PER_REQ,
1927 GFP_NOFS);
1928 if (!data.orig_pages)
1929 goto out;
1930
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001931 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1932 if (data.req) {
1933 /* Ignore errors if we can write at least one page */
1934 BUG_ON(!data.req->num_pages);
1935 fuse_writepages_send(&data);
1936 err = 0;
1937 }
1938 if (data.ff)
1939 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001940
1941 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001942out:
1943 return err;
1944}
1945
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001946static int fuse_launder_page(struct page *page)
1947{
1948 int err = 0;
1949 if (clear_page_dirty_for_io(page)) {
1950 struct inode *inode = page->mapping->host;
1951 err = fuse_writepage_locked(page);
1952 if (!err)
1953 fuse_wait_on_page_writeback(inode, page->index);
1954 }
1955 return err;
1956}
1957
1958/*
1959 * Write back dirty pages now, because there may not be any suitable
1960 * open files later
1961 */
1962static void fuse_vma_close(struct vm_area_struct *vma)
1963{
1964 filemap_write_and_wait(vma->vm_file->f_mapping);
1965}
1966
1967/*
1968 * Wait for writeback against this page to complete before allowing it
1969 * to be marked dirty again, and hence written back again, possibly
1970 * before the previous writepage completed.
1971 *
1972 * Block here, instead of in ->writepage(), so that the userspace fs
1973 * can only block processes actually operating on the filesystem.
1974 *
1975 * Otherwise unprivileged userspace fs would be able to block
1976 * unrelated:
1977 *
1978 * - page migration
1979 * - sync(2)
1980 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1981 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001982static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001983{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001984 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02001985 struct inode *inode = file_inode(vma->vm_file);
1986
1987 file_update_time(vma->vm_file);
1988 lock_page(page);
1989 if (page->mapping != inode->i_mapping) {
1990 unlock_page(page);
1991 return VM_FAULT_NOPAGE;
1992 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001993
1994 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02001995 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001996}
1997
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001998static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001999 .close = fuse_vma_close,
2000 .fault = filemap_fault,
2001 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002002 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002003};
2004
2005static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2006{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002007 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2008 fuse_link_write_file(file);
2009
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002010 file_accessed(file);
2011 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002012 return 0;
2013}
2014
Miklos Szeredifc280c92009-04-02 14:25:35 +02002015static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2016{
2017 /* Can't provide the coherency needed for MAP_SHARED */
2018 if (vma->vm_flags & VM_MAYSHARE)
2019 return -ENODEV;
2020
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002021 invalidate_inode_pages2(file->f_mapping);
2022
Miklos Szeredifc280c92009-04-02 14:25:35 +02002023 return generic_file_mmap(file, vma);
2024}
2025
Miklos Szeredi71421252006-06-25 05:48:52 -07002026static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2027 struct file_lock *fl)
2028{
2029 switch (ffl->type) {
2030 case F_UNLCK:
2031 break;
2032
2033 case F_RDLCK:
2034 case F_WRLCK:
2035 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2036 ffl->end < ffl->start)
2037 return -EIO;
2038
2039 fl->fl_start = ffl->start;
2040 fl->fl_end = ffl->end;
2041 fl->fl_pid = ffl->pid;
2042 break;
2043
2044 default:
2045 return -EIO;
2046 }
2047 fl->fl_type = ffl->type;
2048 return 0;
2049}
2050
2051static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002052 const struct file_lock *fl, int opcode, pid_t pid,
2053 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002054{
Al Viro6131ffa2013-02-27 16:59:05 -05002055 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002056 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002057 struct fuse_file *ff = file->private_data;
2058 struct fuse_lk_in *arg = &req->misc.lk_in;
2059
2060 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002061 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002062 arg->lk.start = fl->fl_start;
2063 arg->lk.end = fl->fl_end;
2064 arg->lk.type = fl->fl_type;
2065 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002066 if (flock)
2067 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002068 req->in.h.opcode = opcode;
2069 req->in.h.nodeid = get_node_id(inode);
2070 req->in.numargs = 1;
2071 req->in.args[0].size = sizeof(*arg);
2072 req->in.args[0].value = arg;
2073}
2074
2075static int fuse_getlk(struct file *file, struct file_lock *fl)
2076{
Al Viro6131ffa2013-02-27 16:59:05 -05002077 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002078 struct fuse_conn *fc = get_fuse_conn(inode);
2079 struct fuse_req *req;
2080 struct fuse_lk_out outarg;
2081 int err;
2082
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002083 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002084 if (IS_ERR(req))
2085 return PTR_ERR(req);
2086
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002087 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002088 req->out.numargs = 1;
2089 req->out.args[0].size = sizeof(outarg);
2090 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002091 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002092 err = req->out.h.error;
2093 fuse_put_request(fc, req);
2094 if (!err)
2095 err = convert_fuse_file_lock(&outarg.lk, fl);
2096
2097 return err;
2098}
2099
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002100static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002101{
Al Viro6131ffa2013-02-27 16:59:05 -05002102 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002103 struct fuse_conn *fc = get_fuse_conn(inode);
2104 struct fuse_req *req;
2105 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2106 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2107 int err;
2108
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002109 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002110 /* NLM needs asynchronous locks, which we don't support yet */
2111 return -ENOLCK;
2112 }
2113
Miklos Szeredi71421252006-06-25 05:48:52 -07002114 /* Unlock on close is handled by the flush method */
2115 if (fl->fl_flags & FL_CLOSE)
2116 return 0;
2117
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002118 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002119 if (IS_ERR(req))
2120 return PTR_ERR(req);
2121
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002122 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002123 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002124 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002125 /* locking is restartable */
2126 if (err == -EINTR)
2127 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002128 fuse_put_request(fc, req);
2129 return err;
2130}
2131
2132static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2133{
Al Viro6131ffa2013-02-27 16:59:05 -05002134 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002135 struct fuse_conn *fc = get_fuse_conn(inode);
2136 int err;
2137
Miklos Szeredi48e90762008-07-25 01:49:02 -07002138 if (cmd == F_CANCELLK) {
2139 err = 0;
2140 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002141 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002142 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002143 err = 0;
2144 } else
2145 err = fuse_getlk(file, fl);
2146 } else {
2147 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002148 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002149 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002150 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002151 }
2152 return err;
2153}
2154
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002155static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2156{
Al Viro6131ffa2013-02-27 16:59:05 -05002157 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002158 struct fuse_conn *fc = get_fuse_conn(inode);
2159 int err;
2160
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002161 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002162 err = flock_lock_file_wait(file, fl);
2163 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002164 struct fuse_file *ff = file->private_data;
2165
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002166 /* emulate flock with POSIX locks */
2167 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002168 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002169 err = fuse_setlk(file, fl, 1);
2170 }
2171
2172 return err;
2173}
2174
Miklos Szeredib2d22722006-12-06 20:35:51 -08002175static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2176{
2177 struct inode *inode = mapping->host;
2178 struct fuse_conn *fc = get_fuse_conn(inode);
2179 struct fuse_req *req;
2180 struct fuse_bmap_in inarg;
2181 struct fuse_bmap_out outarg;
2182 int err;
2183
2184 if (!inode->i_sb->s_bdev || fc->no_bmap)
2185 return 0;
2186
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002187 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002188 if (IS_ERR(req))
2189 return 0;
2190
2191 memset(&inarg, 0, sizeof(inarg));
2192 inarg.block = block;
2193 inarg.blocksize = inode->i_sb->s_blocksize;
2194 req->in.h.opcode = FUSE_BMAP;
2195 req->in.h.nodeid = get_node_id(inode);
2196 req->in.numargs = 1;
2197 req->in.args[0].size = sizeof(inarg);
2198 req->in.args[0].value = &inarg;
2199 req->out.numargs = 1;
2200 req->out.args[0].size = sizeof(outarg);
2201 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002202 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002203 err = req->out.h.error;
2204 fuse_put_request(fc, req);
2205 if (err == -ENOSYS)
2206 fc->no_bmap = 1;
2207
2208 return err ? 0 : outarg.block;
2209}
2210
Andrew Morton965c8e52012-12-17 15:59:39 -08002211static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002212{
2213 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002214 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002215
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002216 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002217 if (whence == SEEK_CUR || whence == SEEK_SET)
2218 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002219
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002220 mutex_lock(&inode->i_mutex);
2221 retval = fuse_update_attributes(inode, NULL, file, NULL);
2222 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002223 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002224 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002225
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002226 return retval;
2227}
2228
Tejun Heo59efec72008-11-26 12:03:55 +01002229static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2230 unsigned int nr_segs, size_t bytes, bool to_user)
2231{
2232 struct iov_iter ii;
2233 int page_idx = 0;
2234
2235 if (!bytes)
2236 return 0;
2237
2238 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2239
2240 while (iov_iter_count(&ii)) {
2241 struct page *page = pages[page_idx++];
2242 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002243 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002244
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002245 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002246
2247 while (todo) {
2248 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2249 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2250 size_t copy = min(todo, iov_len);
2251 size_t left;
2252
2253 if (!to_user)
2254 left = copy_from_user(kaddr, uaddr, copy);
2255 else
2256 left = copy_to_user(uaddr, kaddr, copy);
2257
2258 if (unlikely(left))
2259 return -EFAULT;
2260
2261 iov_iter_advance(&ii, copy);
2262 todo -= copy;
2263 kaddr += copy;
2264 }
2265
Jens Axboe0bd87182009-11-03 11:40:44 +01002266 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002267 }
2268
2269 return 0;
2270}
2271
2272/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002273 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2274 * ABI was defined to be 'struct iovec' which is different on 32bit
2275 * and 64bit. Fortunately we can determine which structure the server
2276 * used from the size of the reply.
2277 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002278static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2279 size_t transferred, unsigned count,
2280 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002281{
2282#ifdef CONFIG_COMPAT
2283 if (count * sizeof(struct compat_iovec) == transferred) {
2284 struct compat_iovec *ciov = src;
2285 unsigned i;
2286
2287 /*
2288 * With this interface a 32bit server cannot support
2289 * non-compat (i.e. ones coming from 64bit apps) ioctl
2290 * requests
2291 */
2292 if (!is_compat)
2293 return -EINVAL;
2294
2295 for (i = 0; i < count; i++) {
2296 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2297 dst[i].iov_len = ciov[i].iov_len;
2298 }
2299 return 0;
2300 }
2301#endif
2302
2303 if (count * sizeof(struct iovec) != transferred)
2304 return -EIO;
2305
2306 memcpy(dst, src, transferred);
2307 return 0;
2308}
2309
Miklos Szeredi75727772010-11-30 16:39:27 +01002310/* Make sure iov_length() won't overflow */
2311static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2312{
2313 size_t n;
2314 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2315
Zach Brownfb6ccff2012-07-24 12:10:11 -07002316 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002317 if (iov->iov_len > (size_t) max)
2318 return -ENOMEM;
2319 max -= iov->iov_len;
2320 }
2321 return 0;
2322}
2323
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002324static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2325 void *src, size_t transferred, unsigned count,
2326 bool is_compat)
2327{
2328 unsigned i;
2329 struct fuse_ioctl_iovec *fiov = src;
2330
2331 if (fc->minor < 16) {
2332 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2333 count, is_compat);
2334 }
2335
2336 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2337 return -EIO;
2338
2339 for (i = 0; i < count; i++) {
2340 /* Did the server supply an inappropriate value? */
2341 if (fiov[i].base != (unsigned long) fiov[i].base ||
2342 fiov[i].len != (unsigned long) fiov[i].len)
2343 return -EIO;
2344
2345 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2346 dst[i].iov_len = (size_t) fiov[i].len;
2347
2348#ifdef CONFIG_COMPAT
2349 if (is_compat &&
2350 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2351 (compat_size_t) dst[i].iov_len != fiov[i].len))
2352 return -EIO;
2353#endif
2354 }
2355
2356 return 0;
2357}
2358
2359
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002360/*
Tejun Heo59efec72008-11-26 12:03:55 +01002361 * For ioctls, there is no generic way to determine how much memory
2362 * needs to be read and/or written. Furthermore, ioctls are allowed
2363 * to dereference the passed pointer, so the parameter requires deep
2364 * copying but FUSE has no idea whatsoever about what to copy in or
2365 * out.
2366 *
2367 * This is solved by allowing FUSE server to retry ioctl with
2368 * necessary in/out iovecs. Let's assume the ioctl implementation
2369 * needs to read in the following structure.
2370 *
2371 * struct a {
2372 * char *buf;
2373 * size_t buflen;
2374 * }
2375 *
2376 * On the first callout to FUSE server, inarg->in_size and
2377 * inarg->out_size will be NULL; then, the server completes the ioctl
2378 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2379 * the actual iov array to
2380 *
2381 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2382 *
2383 * which tells FUSE to copy in the requested area and retry the ioctl.
2384 * On the second round, the server has access to the structure and
2385 * from that it can tell what to look for next, so on the invocation,
2386 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2387 *
2388 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2389 * { .iov_base = a.buf, .iov_len = a.buflen } }
2390 *
2391 * FUSE will copy both struct a and the pointed buffer from the
2392 * process doing the ioctl and retry ioctl with both struct a and the
2393 * buffer.
2394 *
2395 * This time, FUSE server has everything it needs and completes ioctl
2396 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2397 *
2398 * Copying data out works the same way.
2399 *
2400 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2401 * automatically initializes in and out iovs by decoding @cmd with
2402 * _IOC_* macros and the server is not allowed to request RETRY. This
2403 * limits ioctl data transfers to well-formed ioctls and is the forced
2404 * behavior for all FUSE servers.
2405 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002406long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2407 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002408{
Tejun Heo59efec72008-11-26 12:03:55 +01002409 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002410 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002411 struct fuse_ioctl_in inarg = {
2412 .fh = ff->fh,
2413 .cmd = cmd,
2414 .arg = arg,
2415 .flags = flags
2416 };
2417 struct fuse_ioctl_out outarg;
2418 struct fuse_req *req = NULL;
2419 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002420 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002421 struct iovec *in_iov = NULL, *out_iov = NULL;
2422 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2423 size_t in_size, out_size, transferred;
2424 int err;
2425
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002426#if BITS_PER_LONG == 32
2427 inarg.flags |= FUSE_IOCTL_32BIT;
2428#else
2429 if (flags & FUSE_IOCTL_COMPAT)
2430 inarg.flags |= FUSE_IOCTL_32BIT;
2431#endif
2432
Tejun Heo59efec72008-11-26 12:03:55 +01002433 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002434 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002435
Tejun Heo59efec72008-11-26 12:03:55 +01002436 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002437 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002438 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002439 if (!pages || !iov_page)
2440 goto out;
2441
2442 /*
2443 * If restricted, initialize IO parameters as encoded in @cmd.
2444 * RETRY from server is not allowed.
2445 */
2446 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002447 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002448
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002449 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002450 iov->iov_len = _IOC_SIZE(cmd);
2451
2452 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2453 in_iov = iov;
2454 in_iovs = 1;
2455 }
2456
2457 if (_IOC_DIR(cmd) & _IOC_READ) {
2458 out_iov = iov;
2459 out_iovs = 1;
2460 }
2461 }
2462
2463 retry:
2464 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2465 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2466
2467 /*
2468 * Out data can be used either for actual out data or iovs,
2469 * make sure there always is at least one page.
2470 */
2471 out_size = max_t(size_t, out_size, PAGE_SIZE);
2472 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2473
2474 /* make sure there are enough buffer pages and init request with them */
2475 err = -ENOMEM;
2476 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2477 goto out;
2478 while (num_pages < max_pages) {
2479 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2480 if (!pages[num_pages])
2481 goto out;
2482 num_pages++;
2483 }
2484
Maxim Patlasov54b96672012-10-26 19:49:13 +04002485 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002486 if (IS_ERR(req)) {
2487 err = PTR_ERR(req);
2488 req = NULL;
2489 goto out;
2490 }
2491 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2492 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002493 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002494
2495 /* okay, let's send it to the client */
2496 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002497 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002498 req->in.numargs = 1;
2499 req->in.args[0].size = sizeof(inarg);
2500 req->in.args[0].value = &inarg;
2501 if (in_size) {
2502 req->in.numargs++;
2503 req->in.args[1].size = in_size;
2504 req->in.argpages = 1;
2505
2506 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2507 false);
2508 if (err)
2509 goto out;
2510 }
2511
2512 req->out.numargs = 2;
2513 req->out.args[0].size = sizeof(outarg);
2514 req->out.args[0].value = &outarg;
2515 req->out.args[1].size = out_size;
2516 req->out.argpages = 1;
2517 req->out.argvar = 1;
2518
Tejun Heob93f8582008-11-26 12:03:55 +01002519 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002520 err = req->out.h.error;
2521 transferred = req->out.args[1].size;
2522 fuse_put_request(fc, req);
2523 req = NULL;
2524 if (err)
2525 goto out;
2526
2527 /* did it ask for retry? */
2528 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002529 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002530
2531 /* no retry if in restricted mode */
2532 err = -EIO;
2533 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2534 goto out;
2535
2536 in_iovs = outarg.in_iovs;
2537 out_iovs = outarg.out_iovs;
2538
2539 /*
2540 * Make sure things are in boundary, separate checks
2541 * are to protect against overflow.
2542 */
2543 err = -ENOMEM;
2544 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2545 out_iovs > FUSE_IOCTL_MAX_IOV ||
2546 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2547 goto out;
2548
Cong Wang2408f6e2011-11-25 23:14:30 +08002549 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002550 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002551 transferred, in_iovs + out_iovs,
2552 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002553 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002554 if (err)
2555 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002556
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002557 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002558 out_iov = in_iov + in_iovs;
2559
Miklos Szeredi75727772010-11-30 16:39:27 +01002560 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2561 if (err)
2562 goto out;
2563
2564 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2565 if (err)
2566 goto out;
2567
Tejun Heo59efec72008-11-26 12:03:55 +01002568 goto retry;
2569 }
2570
2571 err = -EIO;
2572 if (transferred > inarg.out_size)
2573 goto out;
2574
2575 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2576 out:
2577 if (req)
2578 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002579 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002580 while (num_pages)
2581 __free_page(pages[--num_pages]);
2582 kfree(pages);
2583
2584 return err ? err : outarg.result;
2585}
Tejun Heo08cbf542009-04-14 10:54:53 +09002586EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002587
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002588long fuse_ioctl_common(struct file *file, unsigned int cmd,
2589 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002590{
Al Viro6131ffa2013-02-27 16:59:05 -05002591 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002592 struct fuse_conn *fc = get_fuse_conn(inode);
2593
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002594 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002595 return -EACCES;
2596
2597 if (is_bad_inode(inode))
2598 return -EIO;
2599
2600 return fuse_do_ioctl(file, cmd, arg, flags);
2601}
2602
Tejun Heo59efec72008-11-26 12:03:55 +01002603static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2604 unsigned long arg)
2605{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002606 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002607}
2608
2609static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2610 unsigned long arg)
2611{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002612 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002613}
2614
Tejun Heo95668a62008-11-26 12:03:55 +01002615/*
2616 * All files which have been polled are linked to RB tree
2617 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2618 * find the matching one.
2619 */
2620static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2621 struct rb_node **parent_out)
2622{
2623 struct rb_node **link = &fc->polled_files.rb_node;
2624 struct rb_node *last = NULL;
2625
2626 while (*link) {
2627 struct fuse_file *ff;
2628
2629 last = *link;
2630 ff = rb_entry(last, struct fuse_file, polled_node);
2631
2632 if (kh < ff->kh)
2633 link = &last->rb_left;
2634 else if (kh > ff->kh)
2635 link = &last->rb_right;
2636 else
2637 return link;
2638 }
2639
2640 if (parent_out)
2641 *parent_out = last;
2642 return link;
2643}
2644
2645/*
2646 * The file is about to be polled. Make sure it's on the polled_files
2647 * RB tree. Note that files once added to the polled_files tree are
2648 * not removed before the file is released. This is because a file
2649 * polled once is likely to be polled again.
2650 */
2651static void fuse_register_polled_file(struct fuse_conn *fc,
2652 struct fuse_file *ff)
2653{
2654 spin_lock(&fc->lock);
2655 if (RB_EMPTY_NODE(&ff->polled_node)) {
2656 struct rb_node **link, *parent;
2657
2658 link = fuse_find_polled_node(fc, ff->kh, &parent);
2659 BUG_ON(*link);
2660 rb_link_node(&ff->polled_node, parent, link);
2661 rb_insert_color(&ff->polled_node, &fc->polled_files);
2662 }
2663 spin_unlock(&fc->lock);
2664}
2665
Tejun Heo08cbf542009-04-14 10:54:53 +09002666unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002667{
Tejun Heo95668a62008-11-26 12:03:55 +01002668 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002669 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002670 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2671 struct fuse_poll_out outarg;
2672 struct fuse_req *req;
2673 int err;
2674
2675 if (fc->no_poll)
2676 return DEFAULT_POLLMASK;
2677
2678 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002679 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002680
2681 /*
2682 * Ask for notification iff there's someone waiting for it.
2683 * The client may ignore the flag and always notify.
2684 */
2685 if (waitqueue_active(&ff->poll_wait)) {
2686 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2687 fuse_register_polled_file(fc, ff);
2688 }
2689
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002690 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002691 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002692 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002693
2694 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002695 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002696 req->in.numargs = 1;
2697 req->in.args[0].size = sizeof(inarg);
2698 req->in.args[0].value = &inarg;
2699 req->out.numargs = 1;
2700 req->out.args[0].size = sizeof(outarg);
2701 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002702 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002703 err = req->out.h.error;
2704 fuse_put_request(fc, req);
2705
2706 if (!err)
2707 return outarg.revents;
2708 if (err == -ENOSYS) {
2709 fc->no_poll = 1;
2710 return DEFAULT_POLLMASK;
2711 }
2712 return POLLERR;
2713}
Tejun Heo08cbf542009-04-14 10:54:53 +09002714EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002715
2716/*
2717 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2718 * wakes up the poll waiters.
2719 */
2720int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2721 struct fuse_notify_poll_wakeup_out *outarg)
2722{
2723 u64 kh = outarg->kh;
2724 struct rb_node **link;
2725
2726 spin_lock(&fc->lock);
2727
2728 link = fuse_find_polled_node(fc, kh, NULL);
2729 if (*link) {
2730 struct fuse_file *ff;
2731
2732 ff = rb_entry(*link, struct fuse_file, polled_node);
2733 wake_up_interruptible_sync(&ff->poll_wait);
2734 }
2735
2736 spin_unlock(&fc->lock);
2737 return 0;
2738}
2739
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002740static void fuse_do_truncate(struct file *file)
2741{
2742 struct inode *inode = file->f_mapping->host;
2743 struct iattr attr;
2744
2745 attr.ia_valid = ATTR_SIZE;
2746 attr.ia_size = i_size_read(inode);
2747
2748 attr.ia_file = file;
2749 attr.ia_valid |= ATTR_FILE;
2750
2751 fuse_do_setattr(inode, &attr, file);
2752}
2753
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002754static inline loff_t fuse_round_up(loff_t off)
2755{
2756 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2757}
2758
Anand Avati4273b792012-02-17 12:46:25 -05002759static ssize_t
2760fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2761 loff_t offset, unsigned long nr_segs)
2762{
2763 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002764 struct file *file = iocb->ki_filp;
2765 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002766 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002767 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002768 struct inode *inode;
2769 loff_t i_size;
2770 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002771 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002772
Anand Avati4273b792012-02-17 12:46:25 -05002773 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002774 inode = file->f_mapping->host;
2775 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002776
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002777 if ((rw == READ) && (offset > i_size))
2778 return 0;
2779
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002780 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002781 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002782 if (offset >= i_size)
2783 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002784 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002785 }
2786
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002787 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002788 if (!io)
2789 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002790 spin_lock_init(&io->lock);
2791 io->reqs = 1;
2792 io->bytes = -1;
2793 io->size = 0;
2794 io->offset = offset;
2795 io->write = (rw == WRITE);
2796 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002797 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002798 /*
2799 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002800 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002801 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002802 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002803 io->iocb = iocb;
2804
2805 /*
2806 * We cannot asynchronously extend the size of a file. We have no method
2807 * to wait on real async I/O requests, so we must submit this request
2808 * synchronously.
2809 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002810 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002811 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002812
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002813 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002814 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002815 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002816 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002817
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002818 if (io->async) {
2819 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2820
2821 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002822 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002823 return -EIOCBQUEUED;
2824
2825 ret = wait_on_sync_kiocb(iocb);
2826 } else {
2827 kfree(io);
2828 }
2829
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002830 if (rw == WRITE) {
2831 if (ret > 0)
2832 fuse_write_update_size(inode, pos);
2833 else if (ret < 0 && offset + count > i_size)
2834 fuse_do_truncate(file);
2835 }
Anand Avati4273b792012-02-17 12:46:25 -05002836
2837 return ret;
2838}
2839
Miklos Szeredicdadb112012-11-10 16:55:56 +01002840static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2841 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002842{
2843 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002844 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002845 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002846 struct fuse_conn *fc = ff->fc;
2847 struct fuse_req *req;
2848 struct fuse_fallocate_in inarg = {
2849 .fh = ff->fh,
2850 .offset = offset,
2851 .length = length,
2852 .mode = mode
2853 };
2854 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002855 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2856 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002857
Miklos Szeredi519c6042012-04-26 10:56:36 +02002858 if (fc->no_fallocate)
2859 return -EOPNOTSUPP;
2860
Maxim Patlasov14c14412013-06-13 12:16:39 +04002861 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002862 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002863 if (mode & FALLOC_FL_PUNCH_HOLE) {
2864 loff_t endbyte = offset + length - 1;
2865 err = filemap_write_and_wait_range(inode->i_mapping,
2866 offset, endbyte);
2867 if (err)
2868 goto out;
2869
2870 fuse_sync_writes(inode);
2871 }
Brian Foster3634a632013-05-17 09:30:32 -04002872 }
2873
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002874 if (!(mode & FALLOC_FL_KEEP_SIZE))
2875 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2876
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002877 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04002878 if (IS_ERR(req)) {
2879 err = PTR_ERR(req);
2880 goto out;
2881 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002882
2883 req->in.h.opcode = FUSE_FALLOCATE;
2884 req->in.h.nodeid = ff->nodeid;
2885 req->in.numargs = 1;
2886 req->in.args[0].size = sizeof(inarg);
2887 req->in.args[0].value = &inarg;
2888 fuse_request_send(fc, req);
2889 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002890 if (err == -ENOSYS) {
2891 fc->no_fallocate = 1;
2892 err = -EOPNOTSUPP;
2893 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002894 fuse_put_request(fc, req);
2895
Brian Fosterbee6c302013-05-17 15:27:34 -04002896 if (err)
2897 goto out;
2898
2899 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04002900 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2901 bool changed = fuse_write_update_size(inode, offset + length);
2902
2903 if (changed && fc->writeback_cache) {
2904 struct fuse_inode *fi = get_fuse_inode(inode);
2905
2906 inode->i_mtime = current_fs_time(inode->i_sb);
2907 set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
2908 }
2909 }
Brian Fosterbee6c302013-05-17 15:27:34 -04002910
2911 if (mode & FALLOC_FL_PUNCH_HOLE)
2912 truncate_pagecache_range(inode, offset, offset + length - 1);
2913
2914 fuse_invalidate_attr(inode);
2915
Brian Foster3634a632013-05-17 09:30:32 -04002916out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002917 if (!(mode & FALLOC_FL_KEEP_SIZE))
2918 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2919
Maxim Patlasovbde52782013-09-13 19:19:54 +04002920 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04002921 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04002922
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002923 return err;
2924}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002925
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002926static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002927 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002928 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002929 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002930 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002931 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002932 .mmap = fuse_file_mmap,
2933 .open = fuse_open,
2934 .flush = fuse_flush,
2935 .release = fuse_release,
2936 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002937 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002938 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002939 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002940 .unlocked_ioctl = fuse_file_ioctl,
2941 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002942 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002943 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002944};
2945
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002946static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002947 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002948 .read = fuse_direct_read,
2949 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002950 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002951 .open = fuse_open,
2952 .flush = fuse_flush,
2953 .release = fuse_release,
2954 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002955 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002956 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002957 .unlocked_ioctl = fuse_file_ioctl,
2958 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002959 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002960 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002961 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002962};
2963
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002964static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002965 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002966 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002967 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002968 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002969 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002970 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002971 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05002972 .direct_IO = fuse_direct_IO,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002973};
2974
2975void fuse_init_file_inode(struct inode *inode)
2976{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002977 inode->i_fop = &fuse_file_operations;
2978 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002979}