blob: c091a17d3ffc20b55ec0a58ffd2e0010f6891a28 [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{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200311 fuse_release_common(file, FUSE_RELEASE);
312
313 /* return value is ignored by VFS */
314 return 0;
315}
316
317void fuse_sync_release(struct fuse_file *ff, int flags)
318{
319 WARN_ON(atomic_read(&ff->count) > 1);
320 fuse_prepare_release(ff, flags, FUSE_RELEASE);
321 ff->reserved_req->force = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400322 ff->reserved_req->background = 0;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200323 fuse_request_send(ff->fc, ff->reserved_req);
324 fuse_put_request(ff->fc, ff->reserved_req);
325 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700326}
Tejun Heo08cbf542009-04-14 10:54:53 +0900327EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700328
Miklos Szeredi71421252006-06-25 05:48:52 -0700329/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700330 * Scramble the ID space with XTEA, so that the value of the files_struct
331 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700332 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700333u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700334{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700335 u32 *k = fc->scramble_key;
336 u64 v = (unsigned long) id;
337 u32 v0 = v;
338 u32 v1 = v >> 32;
339 u32 sum = 0;
340 int i;
341
342 for (i = 0; i < 32; i++) {
343 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
344 sum += 0x9E3779B9;
345 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
346 }
347
348 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700349}
350
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700351/*
352 * Check if page is under writeback
353 *
354 * This is currently done by walking the list of writepage requests
355 * for the inode, which can be pretty inefficient.
356 */
357static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
358{
359 struct fuse_conn *fc = get_fuse_conn(inode);
360 struct fuse_inode *fi = get_fuse_inode(inode);
361 struct fuse_req *req;
362 bool found = false;
363
364 spin_lock(&fc->lock);
365 list_for_each_entry(req, &fi->writepages, writepages_entry) {
366 pgoff_t curr_index;
367
368 BUG_ON(req->inode != inode);
369 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
Pavel Emelyanov385b1262013-06-29 21:42:48 +0400370 if (curr_index <= index &&
371 index < curr_index + req->num_pages) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700372 found = true;
373 break;
374 }
375 }
376 spin_unlock(&fc->lock);
377
378 return found;
379}
380
381/*
382 * Wait for page writeback to be completed.
383 *
384 * Since fuse doesn't rely on the VM writeback tracking, this has to
385 * use some other means.
386 */
387static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
388{
389 struct fuse_inode *fi = get_fuse_inode(inode);
390
391 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
392 return 0;
393}
394
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700395static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700396{
Al Viro6131ffa2013-02-27 16:59:05 -0500397 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700398 struct fuse_conn *fc = get_fuse_conn(inode);
399 struct fuse_file *ff = file->private_data;
400 struct fuse_req *req;
401 struct fuse_flush_in inarg;
402 int err;
403
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800404 if (is_bad_inode(inode))
405 return -EIO;
406
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700407 if (fc->no_flush)
408 return 0;
409
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400410 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700411 memset(&inarg, 0, sizeof(inarg));
412 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700413 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700414 req->in.h.opcode = FUSE_FLUSH;
415 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700416 req->in.numargs = 1;
417 req->in.args[0].size = sizeof(inarg);
418 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700419 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100420 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700421 err = req->out.h.error;
422 fuse_put_request(fc, req);
423 if (err == -ENOSYS) {
424 fc->no_flush = 1;
425 err = 0;
426 }
427 return err;
428}
429
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700430/*
431 * Wait for all pending writepages on the inode to finish.
432 *
433 * This is currently done by blocking further writes with FUSE_NOWRITE
434 * and waiting for all sent writes to complete.
435 *
436 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
437 * could conflict with truncation.
438 */
439static void fuse_sync_writes(struct inode *inode)
440{
441 fuse_set_nowrite(inode);
442 fuse_release_nowrite(inode);
443}
444
Josef Bacik02c24a82011-07-16 20:44:56 -0400445int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
446 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200448 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 struct fuse_conn *fc = get_fuse_conn(inode);
450 struct fuse_file *ff = file->private_data;
451 struct fuse_req *req;
452 struct fuse_fsync_in inarg;
453 int err;
454
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800455 if (is_bad_inode(inode))
456 return -EIO;
457
Josef Bacik02c24a82011-07-16 20:44:56 -0400458 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
459 if (err)
460 return err;
461
Miklos Szeredi82547982005-09-09 13:10:38 -0700462 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700463 return 0;
464
Josef Bacik02c24a82011-07-16 20:44:56 -0400465 mutex_lock(&inode->i_mutex);
466
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700467 /*
468 * Start writeback against all dirty pages of the inode, then
469 * wait for all outstanding writes, before sending the FSYNC
470 * request.
471 */
472 err = write_inode_now(inode, 0);
473 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400474 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700475
476 fuse_sync_writes(inode);
477
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400478 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400479 if (IS_ERR(req)) {
480 err = PTR_ERR(req);
481 goto out;
482 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700483
484 memset(&inarg, 0, sizeof(inarg));
485 inarg.fh = ff->fh;
486 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700487 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700488 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700489 req->in.numargs = 1;
490 req->in.args[0].size = sizeof(inarg);
491 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100492 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700493 err = req->out.h.error;
494 fuse_put_request(fc, req);
495 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700496 if (isdir)
497 fc->no_fsyncdir = 1;
498 else
499 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700500 err = 0;
501 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400502out:
503 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700504 return err;
505}
506
Josef Bacik02c24a82011-07-16 20:44:56 -0400507static int fuse_fsync(struct file *file, loff_t start, loff_t end,
508 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700509{
Josef Bacik02c24a82011-07-16 20:44:56 -0400510 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700511}
512
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200513void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
514 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700515{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700516 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800517 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700518
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800519 inarg->fh = ff->fh;
520 inarg->offset = pos;
521 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800522 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800523 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200524 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700525 req->in.numargs = 1;
526 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800527 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700528 req->out.argvar = 1;
529 req->out.numargs = 1;
530 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531}
532
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400533static void fuse_release_user_pages(struct fuse_req *req, int write)
534{
535 unsigned i;
536
537 for (i = 0; i < req->num_pages; i++) {
538 struct page *page = req->pages[i];
539 if (write)
540 set_page_dirty_lock(page);
541 put_page(page);
542 }
543}
544
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400545/**
546 * In case of short read, the caller sets 'pos' to the position of
547 * actual end of fuse request in IO request. Otherwise, if bytes_requested
548 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
549 *
550 * An example:
551 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
552 * both submitted asynchronously. The first of them was ACKed by userspace as
553 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
554 * second request was ACKed as short, e.g. only 1K was read, resulting in
555 * pos == 33K.
556 *
557 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
558 * will be equal to the length of the longest contiguous fragment of
559 * transferred data starting from the beginning of IO request.
560 */
561static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
562{
563 int left;
564
565 spin_lock(&io->lock);
566 if (err)
567 io->err = io->err ? : err;
568 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
569 io->bytes = pos;
570
571 left = --io->reqs;
572 spin_unlock(&io->lock);
573
574 if (!left) {
575 long res;
576
577 if (io->err)
578 res = io->err;
579 else if (io->bytes >= 0 && io->write)
580 res = -EIO;
581 else {
582 res = io->bytes < 0 ? io->size : io->bytes;
583
584 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400585 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400586 struct fuse_conn *fc = get_fuse_conn(inode);
587 struct fuse_inode *fi = get_fuse_inode(inode);
588
589 spin_lock(&fc->lock);
590 fi->attr_version = ++fc->attr_version;
591 spin_unlock(&fc->lock);
592 }
593 }
594
595 aio_complete(io->iocb, res, 0);
596 kfree(io);
597 }
598}
599
600static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
601{
602 struct fuse_io_priv *io = req->io;
603 ssize_t pos = -1;
604
605 fuse_release_user_pages(req, !io->write);
606
607 if (io->write) {
608 if (req->misc.write.in.size != req->misc.write.out.size)
609 pos = req->misc.write.in.offset - io->offset +
610 req->misc.write.out.size;
611 } else {
612 if (req->misc.read.in.size != req->out.args[0].size)
613 pos = req->misc.read.in.offset - io->offset +
614 req->out.args[0].size;
615 }
616
617 fuse_aio_complete(io, req->out.h.error, pos);
618}
619
620static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
621 size_t num_bytes, struct fuse_io_priv *io)
622{
623 spin_lock(&io->lock);
624 io->size += num_bytes;
625 io->reqs++;
626 spin_unlock(&io->lock);
627
628 req->io = io;
629 req->end = fuse_aio_complete_req;
630
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400631 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400632 fuse_request_send_background(fc, req);
633
634 return num_bytes;
635}
636
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400637static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200638 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700639{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400640 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200641 struct fuse_file *ff = file->private_data;
642 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700643
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200644 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700645 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700646 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700647
648 inarg->read_flags |= FUSE_READ_LOCKOWNER;
649 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
650 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400651
652 if (io->async)
653 return fuse_async_req_send(fc, req, count, io);
654
Tejun Heob93f8582008-11-26 12:03:55 +0100655 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800656 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700657}
658
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700659static void fuse_read_update_size(struct inode *inode, loff_t size,
660 u64 attr_ver)
661{
662 struct fuse_conn *fc = get_fuse_conn(inode);
663 struct fuse_inode *fi = get_fuse_inode(inode);
664
665 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400666 if (attr_ver == fi->attr_version && size < inode->i_size &&
667 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700668 fi->attr_version = ++fc->attr_version;
669 i_size_write(inode, size);
670 }
671 spin_unlock(&fc->lock);
672}
673
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400674static void fuse_short_read(struct fuse_req *req, struct inode *inode,
675 u64 attr_ver)
676{
677 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400678 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400679
Pavel Emelyanov83732002013-10-10 17:10:46 +0400680 if (fc->writeback_cache) {
681 /*
682 * A hole in a file. Some data after the hole are in page cache,
683 * but have not reached the client fs yet. So, the hole is not
684 * present there.
685 */
686 int i;
687 int start_idx = num_read >> PAGE_CACHE_SHIFT;
688 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
689
690 for (i = start_idx; i < req->num_pages; i++) {
691 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
692 off = 0;
693 }
694 } else {
695 loff_t pos = page_offset(req->pages[0]) + num_read;
696 fuse_read_update_size(inode, pos, attr_ver);
697 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400698}
699
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700700static int fuse_readpage(struct file *file, struct page *page)
701{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400702 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700703 struct inode *inode = page->mapping->host;
704 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800705 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700706 size_t num_read;
707 loff_t pos = page_offset(page);
708 size_t count = PAGE_CACHE_SIZE;
709 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800710 int err;
711
712 err = -EIO;
713 if (is_bad_inode(inode))
714 goto out;
715
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700716 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300717 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700718 * page-cache page, so make sure we read a properly synced
719 * page.
720 */
721 fuse_wait_on_page_writeback(inode, page->index);
722
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400723 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700724 err = PTR_ERR(req);
725 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700726 goto out;
727
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700728 attr_ver = fuse_get_attr_version(fc);
729
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700730 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200731 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700732 req->num_pages = 1;
733 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400734 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400735 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700736 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700737
738 if (!err) {
739 /*
740 * Short read means EOF. If file size is larger, truncate it
741 */
742 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400743 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700744
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700745 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700746 }
747
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400748 fuse_put_request(fc, req);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800749 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700750 out:
751 unlock_page(page);
752 return err;
753}
754
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800755static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700756{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800757 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700758 size_t count = req->misc.read.in.size;
759 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200760 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800761
Miklos Szeredice534fb2010-05-25 15:06:07 +0200762 for (i = 0; mapping == NULL && i < req->num_pages; i++)
763 mapping = req->pages[i]->mapping;
764
765 if (mapping) {
766 struct inode *inode = mapping->host;
767
768 /*
769 * Short read means EOF. If file size is larger, truncate it
770 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400771 if (!req->out.h.error && num_read < count)
772 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200773
Andrew Gallagher451418f2013-11-05 03:55:43 -0800774 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700775 }
776
Miklos Szeredidb50b962005-09-09 13:10:33 -0700777 for (i = 0; i < req->num_pages; i++) {
778 struct page *page = req->pages[i];
779 if (!req->out.h.error)
780 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800781 else
782 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700783 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200784 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700785 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700786 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100787 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800788}
789
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200790static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800791{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200792 struct fuse_file *ff = file->private_data;
793 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800794 loff_t pos = page_offset(req->pages[0]);
795 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200796
797 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800798 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200799 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200800 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700801 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800802 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700803 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800804 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100805 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800806 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100807 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800808 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100809 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800810 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700811}
812
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700813struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700814 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800815 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700816 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400817 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700818};
819
820static int fuse_readpages_fill(void *_data, struct page *page)
821{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700822 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700823 struct fuse_req *req = data->req;
824 struct inode *inode = data->inode;
825 struct fuse_conn *fc = get_fuse_conn(inode);
826
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700827 fuse_wait_on_page_writeback(inode, page->index);
828
Miklos Szeredidb50b962005-09-09 13:10:33 -0700829 if (req->num_pages &&
830 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
831 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
832 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400833 int nr_alloc = min_t(unsigned, data->nr_pages,
834 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200835 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400836 if (fc->async_read)
837 req = fuse_get_req_for_background(fc, nr_alloc);
838 else
839 req = fuse_get_req(fc, nr_alloc);
840
841 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700842 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700843 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700844 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700845 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700846 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400847
848 if (WARN_ON(req->num_pages >= req->max_pages)) {
849 fuse_put_request(fc, req);
850 return -EIO;
851 }
852
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200853 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400855 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100856 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400857 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858 return 0;
859}
860
861static int fuse_readpages(struct file *file, struct address_space *mapping,
862 struct list_head *pages, unsigned nr_pages)
863{
864 struct inode *inode = mapping->host;
865 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700866 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700867 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400868 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800869
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700870 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800871 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800872 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800873
Miklos Szeredia6643092007-11-28 16:22:00 -0800874 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700875 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400876 if (fc->async_read)
877 data.req = fuse_get_req_for_background(fc, nr_alloc);
878 else
879 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400880 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700881 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700882 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800883 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700884
885 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700886 if (!err) {
887 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200888 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700889 else
890 fuse_put_request(fc, data.req);
891 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800892out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700893 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700894}
895
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800896static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
897 unsigned long nr_segs, loff_t pos)
898{
899 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400900 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800901
Brian Fostera8894272012-07-16 15:23:50 -0400902 /*
903 * In auto invalidate mode, always update attributes on read.
904 * Otherwise, only update if we attempt to read past EOF (to ensure
905 * i_size is up to date).
906 */
907 if (fc->auto_inval_data ||
908 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800909 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800910 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
911 if (err)
912 return err;
913 }
914
915 return generic_file_aio_read(iocb, iov, nr_segs, pos);
916}
917
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200918static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200919 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700920{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700921 struct fuse_write_in *inarg = &req->misc.write.in;
922 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700923
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700924 inarg->fh = ff->fh;
925 inarg->offset = pos;
926 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700927 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200928 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700929 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200930 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700931 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
932 else
933 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700934 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700935 req->in.args[1].size = count;
936 req->out.numargs = 1;
937 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700938 req->out.args[0].value = outarg;
939}
940
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400941static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200942 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700943{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400944 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200945 struct fuse_file *ff = file->private_data;
946 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200947 struct fuse_write_in *inarg = &req->misc.write.in;
948
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200949 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200950 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700951 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700952 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
953 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
954 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400955
956 if (io->async)
957 return fuse_async_req_send(fc, req, count, io);
958
Tejun Heob93f8582008-11-26 12:03:55 +0100959 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700960 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700961}
962
Miklos Szeredia1d75f22010-07-12 14:41:40 +0200963void fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700964{
965 struct fuse_conn *fc = get_fuse_conn(inode);
966 struct fuse_inode *fi = get_fuse_inode(inode);
967
968 spin_lock(&fc->lock);
969 fi->attr_version = ++fc->attr_version;
970 if (pos > inode->i_size)
971 i_size_write(inode, pos);
972 spin_unlock(&fc->lock);
973}
974
Nick Pigginea9b9902008-04-30 00:54:42 -0700975static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
976 struct inode *inode, loff_t pos,
977 size_t count)
978{
979 size_t res;
980 unsigned offset;
981 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400982 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -0700983
984 for (i = 0; i < req->num_pages; i++)
985 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
986
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400987 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700988
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400989 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700990 count = res;
991 for (i = 0; i < req->num_pages; i++) {
992 struct page *page = req->pages[i];
993
994 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
995 SetPageUptodate(page);
996
997 if (count > PAGE_CACHE_SIZE - offset)
998 count -= PAGE_CACHE_SIZE - offset;
999 else
1000 count = 0;
1001 offset = 0;
1002
1003 unlock_page(page);
1004 page_cache_release(page);
1005 }
1006
1007 return res;
1008}
1009
1010static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1011 struct address_space *mapping,
1012 struct iov_iter *ii, loff_t pos)
1013{
1014 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1015 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1016 size_t count = 0;
1017 int err;
1018
Miklos Szeredif4975c62009-04-02 14:25:34 +02001019 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001020 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001021
1022 do {
1023 size_t tmp;
1024 struct page *page;
1025 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1026 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1027 iov_iter_count(ii));
1028
1029 bytes = min_t(size_t, bytes, fc->max_write - count);
1030
1031 again:
1032 err = -EFAULT;
1033 if (iov_iter_fault_in_readable(ii, bytes))
1034 break;
1035
1036 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001037 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001038 if (!page)
1039 break;
1040
anfei zhou931e80e2010-02-02 13:44:02 -08001041 if (mapping_writably_mapped(mapping))
1042 flush_dcache_page(page);
1043
Nick Pigginea9b9902008-04-30 00:54:42 -07001044 pagefault_disable();
1045 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1046 pagefault_enable();
1047 flush_dcache_page(page);
1048
Johannes Weiner478e0842011-07-25 22:35:35 +02001049 mark_page_accessed(page);
1050
Nick Pigginea9b9902008-04-30 00:54:42 -07001051 if (!tmp) {
1052 unlock_page(page);
1053 page_cache_release(page);
1054 bytes = min(bytes, iov_iter_single_seg_count(ii));
1055 goto again;
1056 }
1057
1058 err = 0;
1059 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001060 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001061 req->num_pages++;
1062
1063 iov_iter_advance(ii, tmp);
1064 count += tmp;
1065 pos += tmp;
1066 offset += tmp;
1067 if (offset == PAGE_CACHE_SIZE)
1068 offset = 0;
1069
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001070 if (!fc->big_writes)
1071 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001072 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001073 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001074
1075 return count > 0 ? count : err;
1076}
1077
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001078static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1079{
1080 return min_t(unsigned,
1081 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1082 (pos >> PAGE_CACHE_SHIFT) + 1,
1083 FUSE_MAX_PAGES_PER_REQ);
1084}
1085
Nick Pigginea9b9902008-04-30 00:54:42 -07001086static ssize_t fuse_perform_write(struct file *file,
1087 struct address_space *mapping,
1088 struct iov_iter *ii, loff_t pos)
1089{
1090 struct inode *inode = mapping->host;
1091 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001092 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001093 int err = 0;
1094 ssize_t res = 0;
1095
1096 if (is_bad_inode(inode))
1097 return -EIO;
1098
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001099 if (inode->i_size < pos + iov_iter_count(ii))
1100 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1101
Nick Pigginea9b9902008-04-30 00:54:42 -07001102 do {
1103 struct fuse_req *req;
1104 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001105 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001106
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001107 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001108 if (IS_ERR(req)) {
1109 err = PTR_ERR(req);
1110 break;
1111 }
1112
1113 count = fuse_fill_write_pages(req, mapping, ii, pos);
1114 if (count <= 0) {
1115 err = count;
1116 } else {
1117 size_t num_written;
1118
1119 num_written = fuse_send_write_pages(req, file, inode,
1120 pos, count);
1121 err = req->out.h.error;
1122 if (!err) {
1123 res += num_written;
1124 pos += num_written;
1125
1126 /* break out of the loop on short write */
1127 if (num_written != count)
1128 err = -EIO;
1129 }
1130 }
1131 fuse_put_request(fc, req);
1132 } while (!err && iov_iter_count(ii));
1133
1134 if (res > 0)
1135 fuse_write_update_size(inode, pos);
1136
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001137 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001138 fuse_invalidate_attr(inode);
1139
1140 return res > 0 ? res : err;
1141}
1142
1143static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1144 unsigned long nr_segs, loff_t pos)
1145{
1146 struct file *file = iocb->ki_filp;
1147 struct address_space *mapping = file->f_mapping;
1148 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001149 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001150 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001151 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001152 struct inode *inode = mapping->host;
1153 ssize_t err;
1154 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001155 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001156
1157 WARN_ON(iocb->ki_pos != pos);
1158
Anand Avati4273b792012-02-17 12:46:25 -05001159 ocount = 0;
1160 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001161 if (err)
1162 return err;
1163
Anand Avati4273b792012-02-17 12:46:25 -05001164 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001165 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001166
1167 /* We can write back this queue in page reclaim */
1168 current->backing_dev_info = mapping->backing_dev_info;
1169
1170 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1171 if (err)
1172 goto out;
1173
1174 if (count == 0)
1175 goto out;
1176
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001177 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001178 if (err)
1179 goto out;
1180
Josef Bacikc3b2da32012-03-26 09:59:21 -04001181 err = file_update_time(file);
1182 if (err)
1183 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001184
Anand Avati4273b792012-02-17 12:46:25 -05001185 if (file->f_flags & O_DIRECT) {
1186 written = generic_file_direct_write(iocb, iov, &nr_segs,
1187 pos, &iocb->ki_pos,
1188 count, ocount);
1189 if (written < 0 || written == count)
1190 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001191
Anand Avati4273b792012-02-17 12:46:25 -05001192 pos += written;
1193 count -= written;
1194
1195 iov_iter_init(&i, iov, nr_segs, count, written);
1196 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1197 if (written_buffered < 0) {
1198 err = written_buffered;
1199 goto out;
1200 }
1201 endbyte = pos + written_buffered - 1;
1202
1203 err = filemap_write_and_wait_range(file->f_mapping, pos,
1204 endbyte);
1205 if (err)
1206 goto out;
1207
1208 invalidate_mapping_pages(file->f_mapping,
1209 pos >> PAGE_CACHE_SHIFT,
1210 endbyte >> PAGE_CACHE_SHIFT);
1211
1212 written += written_buffered;
1213 iocb->ki_pos = pos + written_buffered;
1214 } else {
1215 iov_iter_init(&i, iov, nr_segs, count, 0);
1216 written = fuse_perform_write(file, mapping, &i, pos);
1217 if (written >= 0)
1218 iocb->ki_pos = pos + written;
1219 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001220out:
1221 current->backing_dev_info = NULL;
1222 mutex_unlock(&inode->i_mutex);
1223
1224 return written ? written : err;
1225}
1226
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001227static inline void fuse_page_descs_length_init(struct fuse_req *req,
1228 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001229{
1230 int i;
1231
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001232 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001233 req->page_descs[i].length = PAGE_SIZE -
1234 req->page_descs[i].offset;
1235}
1236
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001237static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1238{
1239 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1240}
1241
1242static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1243 size_t max_size)
1244{
1245 return min(iov_iter_single_seg_count(ii), max_size);
1246}
1247
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001248static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001249 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001250{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001251 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001252
Miklos Szeredif4975c62009-04-02 14:25:34 +02001253 /* Special case for kernel I/O: can copy directly into the buffer */
1254 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001255 unsigned long user_addr = fuse_get_user_addr(ii);
1256 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1257
Miklos Szeredif4975c62009-04-02 14:25:34 +02001258 if (write)
1259 req->in.args[1].value = (void *) user_addr;
1260 else
1261 req->out.args[0].value = (void *) user_addr;
1262
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001263 iov_iter_advance(ii, frag_size);
1264 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001265 return 0;
1266 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001267
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001268 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001269 unsigned npages;
1270 unsigned long user_addr = fuse_get_user_addr(ii);
1271 unsigned offset = user_addr & ~PAGE_MASK;
1272 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1273 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001274
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001275 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001276 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1277
1278 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1279 npages = clamp(npages, 1U, n);
1280
1281 ret = get_user_pages_fast(user_addr, npages, !write,
1282 &req->pages[req->num_pages]);
1283 if (ret < 0)
1284 return ret;
1285
1286 npages = ret;
1287 frag_size = min_t(size_t, frag_size,
1288 (npages << PAGE_SHIFT) - offset);
1289 iov_iter_advance(ii, frag_size);
1290
1291 req->page_descs[req->num_pages].offset = offset;
1292 fuse_page_descs_length_init(req, req->num_pages, npages);
1293
1294 req->num_pages += npages;
1295 req->page_descs[req->num_pages - 1].length -=
1296 (npages << PAGE_SHIFT) - offset - frag_size;
1297
1298 nbytes += frag_size;
1299 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001300
1301 if (write)
1302 req->in.argpages = 1;
1303 else
1304 req->out.argpages = 1;
1305
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001306 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001307
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001308 return 0;
1309}
1310
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001311static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1312{
1313 struct iov_iter ii = *ii_p;
1314 int npages = 0;
1315
1316 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1317 unsigned long user_addr = fuse_get_user_addr(&ii);
1318 unsigned offset = user_addr & ~PAGE_MASK;
1319 size_t frag_size = iov_iter_single_seg_count(&ii);
1320
1321 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1322 iov_iter_advance(&ii, frag_size);
1323 }
1324
1325 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1326}
1327
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001328ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001329 unsigned long nr_segs, size_t count, loff_t *ppos,
1330 int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001331{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001332 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001333 struct fuse_file *ff = file->private_data;
1334 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001335 size_t nmax = write ? fc->max_write : fc->max_read;
1336 loff_t pos = *ppos;
1337 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001338 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001339 struct iov_iter ii;
1340
1341 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001342
Brian Fosterde82b922013-05-14 11:25:39 -04001343 if (io->async)
1344 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1345 else
1346 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001347 if (IS_ERR(req))
1348 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001349
1350 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001351 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001352 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001353 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001354 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001355 if (err) {
1356 res = err;
1357 break;
1358 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001359
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001360 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001361 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001363 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001364
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001365 if (!io->async)
1366 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001367 if (req->out.h.error) {
1368 if (!res)
1369 res = req->out.h.error;
1370 break;
1371 } else if (nres > nbytes) {
1372 res = -EIO;
1373 break;
1374 }
1375 count -= nres;
1376 res += nres;
1377 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001378 if (nres != nbytes)
1379 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001380 if (count) {
1381 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001382 if (io->async)
1383 req = fuse_get_req_for_background(fc,
1384 fuse_iter_npages(&ii));
1385 else
1386 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001387 if (IS_ERR(req))
1388 break;
1389 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001390 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001391 if (!IS_ERR(req))
1392 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001393 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001394 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001395
1396 return res;
1397}
Tejun Heo08cbf542009-04-14 10:54:53 +09001398EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001399
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001400static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1401 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001402 unsigned long nr_segs, loff_t *ppos,
1403 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001404{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001405 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001406 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001407 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001408
1409 if (is_bad_inode(inode))
1410 return -EIO;
1411
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001412 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001413
1414 fuse_invalidate_attr(inode);
1415
1416 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001417}
1418
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001419static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1420 size_t count, loff_t *ppos)
1421{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001422 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001423 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001424 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001425}
1426
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001427static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1428 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001429 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001430{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001431 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001432 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001433 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001434 ssize_t res;
1435
1436 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001437 if (!res)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001438 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1);
Anand Avati4273b792012-02-17 12:46:25 -05001439
1440 fuse_invalidate_attr(inode);
1441
1442 return res;
1443}
1444
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001445static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1446 size_t count, loff_t *ppos)
1447{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001448 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001449 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001450 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001451 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001452
1453 if (is_bad_inode(inode))
1454 return -EIO;
1455
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001456 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001457 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001458 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001459 if (res > 0)
1460 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001461 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001462
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001463 return res;
1464}
1465
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001466static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001467{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001468 int i;
1469
1470 for (i = 0; i < req->num_pages; i++)
1471 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001472
1473 if (req->ff)
1474 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001475}
1476
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001477static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001478{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001479 struct inode *inode = req->inode;
1480 struct fuse_inode *fi = get_fuse_inode(inode);
1481 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001482 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001483
1484 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001485 for (i = 0; i < req->num_pages; i++) {
1486 dec_bdi_stat(bdi, BDI_WRITEBACK);
1487 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1488 bdi_writeout_inc(bdi);
1489 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001490 wake_up(&fi->page_waitq);
1491}
1492
1493/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001494static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1495 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001496__releases(fc->lock)
1497__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001498{
1499 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001500 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001501 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001502
1503 if (!fc->connected)
1504 goto out_free;
1505
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001506 if (inarg->offset + data_size <= size) {
1507 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001508 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001509 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001510 } else {
1511 /* Got truncated off completely */
1512 goto out_free;
1513 }
1514
1515 req->in.args[1].size = inarg->size;
1516 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001517 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001518 return;
1519
1520 out_free:
1521 fuse_writepage_finish(fc, req);
1522 spin_unlock(&fc->lock);
1523 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001524 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001525 spin_lock(&fc->lock);
1526}
1527
1528/*
1529 * If fi->writectr is positive (no truncate or fsync going on) send
1530 * all queued writepage requests.
1531 *
1532 * Called with fc->lock
1533 */
1534void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001535__releases(fc->lock)
1536__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001537{
1538 struct fuse_conn *fc = get_fuse_conn(inode);
1539 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001540 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001541 struct fuse_req *req;
1542
1543 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1544 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1545 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001546 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001547 }
1548}
1549
1550static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1551{
1552 struct inode *inode = req->inode;
1553 struct fuse_inode *fi = get_fuse_inode(inode);
1554
1555 mapping_set_error(inode->i_mapping, req->out.h.error);
1556 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001557 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001558 struct fuse_conn *fc = get_fuse_conn(inode);
1559 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001560 struct fuse_req *next = req->misc.write.next;
1561 req->misc.write.next = next->misc.write.next;
1562 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001563 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001564 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001565
1566 /*
1567 * Skip fuse_flush_writepages() to make it easy to crop requests
1568 * based on primary request size.
1569 *
1570 * 1st case (trivial): there are no concurrent activities using
1571 * fuse_set/release_nowrite. Then we're on safe side because
1572 * fuse_flush_writepages() would call fuse_send_writepage()
1573 * anyway.
1574 *
1575 * 2nd case: someone called fuse_set_nowrite and it is waiting
1576 * now for completion of all in-flight requests. This happens
1577 * rarely and no more than once per page, so this should be
1578 * okay.
1579 *
1580 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1581 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1582 * that fuse_set_nowrite returned implies that all in-flight
1583 * requests were completed along with all of their secondary
1584 * requests. Further primary requests are blocked by negative
1585 * writectr. Hence there cannot be any in-flight requests and
1586 * no invocations of fuse_writepage_end() while we're in
1587 * fuse_set_nowrite..fuse_release_nowrite section.
1588 */
1589 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001590 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001591 fi->writectr--;
1592 fuse_writepage_finish(fc, req);
1593 spin_unlock(&fc->lock);
1594 fuse_writepage_free(fc, req);
1595}
1596
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001597static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1598 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001599{
Miklos Szeredi72523422013-10-01 16:44:52 +02001600 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001601
1602 spin_lock(&fc->lock);
Miklos Szeredi72523422013-10-01 16:44:52 +02001603 if (!WARN_ON(list_empty(&fi->write_files))) {
1604 ff = list_entry(fi->write_files.next, struct fuse_file,
1605 write_entry);
1606 fuse_file_get(ff);
1607 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001608 spin_unlock(&fc->lock);
1609
1610 return ff;
1611}
1612
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001613static int fuse_writepage_locked(struct page *page)
1614{
1615 struct address_space *mapping = page->mapping;
1616 struct inode *inode = mapping->host;
1617 struct fuse_conn *fc = get_fuse_conn(inode);
1618 struct fuse_inode *fi = get_fuse_inode(inode);
1619 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001620 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001621 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001622
1623 set_page_writeback(page);
1624
Maxim Patlasov4250c062012-10-26 19:48:07 +04001625 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001626 if (!req)
1627 goto err;
1628
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001629 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001630 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1631 if (!tmp_page)
1632 goto err_free;
1633
Miklos Szeredi72523422013-10-01 16:44:52 +02001634 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001635 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001636 if (!req->ff)
1637 goto err_free;
1638
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001639 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001640
1641 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001642 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001643 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001644 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001645 req->num_pages = 1;
1646 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001647 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001648 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001649 req->end = fuse_writepage_end;
1650 req->inode = inode;
1651
1652 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1653 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001654
1655 spin_lock(&fc->lock);
1656 list_add(&req->writepages_entry, &fi->writepages);
1657 list_add_tail(&req->list, &fi->queued_writes);
1658 fuse_flush_writepages(inode);
1659 spin_unlock(&fc->lock);
1660
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001661 end_page_writeback(page);
1662
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001663 return 0;
1664
1665err_free:
1666 fuse_request_free(req);
1667err:
1668 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001669 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001670}
1671
1672static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1673{
1674 int err;
1675
Miklos Szerediff17be02013-10-01 16:44:53 +02001676 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1677 /*
1678 * ->writepages() should be called for sync() and friends. We
1679 * should only get here on direct reclaim and then we are
1680 * allowed to skip a page which is already in flight
1681 */
1682 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1683
1684 redirty_page_for_writepage(wbc, page);
1685 return 0;
1686 }
1687
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001688 err = fuse_writepage_locked(page);
1689 unlock_page(page);
1690
1691 return err;
1692}
1693
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001694struct fuse_fill_wb_data {
1695 struct fuse_req *req;
1696 struct fuse_file *ff;
1697 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001698 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001699};
1700
1701static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1702{
1703 struct fuse_req *req = data->req;
1704 struct inode *inode = data->inode;
1705 struct fuse_conn *fc = get_fuse_conn(inode);
1706 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001707 int num_pages = req->num_pages;
1708 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001709
1710 req->ff = fuse_file_get(data->ff);
1711 spin_lock(&fc->lock);
1712 list_add_tail(&req->list, &fi->queued_writes);
1713 fuse_flush_writepages(inode);
1714 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001715
1716 for (i = 0; i < num_pages; i++)
1717 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001718}
1719
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001720static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1721 struct page *page)
1722{
1723 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1724 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1725 struct fuse_req *tmp;
1726 struct fuse_req *old_req;
1727 bool found = false;
1728 pgoff_t curr_index;
1729
1730 BUG_ON(new_req->num_pages != 0);
1731
1732 spin_lock(&fc->lock);
1733 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001734 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1735 BUG_ON(old_req->inode != new_req->inode);
1736 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1737 if (curr_index <= page->index &&
1738 page->index < curr_index + old_req->num_pages) {
1739 found = true;
1740 break;
1741 }
1742 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001743 if (!found) {
1744 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001745 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001746 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001747
Maxim Patlasovf6011082013-10-02 15:01:07 +04001748 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001749 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1750 BUG_ON(tmp->inode != new_req->inode);
1751 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1752 if (tmp->num_pages == 1 &&
1753 curr_index == page->index) {
1754 old_req = tmp;
1755 }
1756 }
1757
1758 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1759 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001760 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1761
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001762 copy_highpage(old_req->pages[0], page);
1763 spin_unlock(&fc->lock);
1764
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001765 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001766 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001767 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001768 fuse_writepage_free(fc, new_req);
1769 fuse_request_free(new_req);
1770 goto out;
1771 } else {
1772 new_req->misc.write.next = old_req->misc.write.next;
1773 old_req->misc.write.next = new_req;
1774 }
1775out_unlock:
1776 spin_unlock(&fc->lock);
1777out:
1778 return found;
1779}
1780
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001781static int fuse_writepages_fill(struct page *page,
1782 struct writeback_control *wbc, void *_data)
1783{
1784 struct fuse_fill_wb_data *data = _data;
1785 struct fuse_req *req = data->req;
1786 struct inode *inode = data->inode;
1787 struct fuse_conn *fc = get_fuse_conn(inode);
1788 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001789 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001790 int err;
1791
1792 if (!data->ff) {
1793 err = -EIO;
1794 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1795 if (!data->ff)
1796 goto out_unlock;
1797 }
1798
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001799 /*
1800 * Being under writeback is unlikely but possible. For example direct
1801 * read to an mmaped fuse file will set the page dirty twice; once when
1802 * the pages are faulted with get_user_pages(), and then after the read
1803 * completed.
1804 */
1805 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001806
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001807 if (req && req->num_pages &&
1808 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1809 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1810 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1811 fuse_writepages_send(data);
1812 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001813 }
1814 err = -ENOMEM;
1815 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1816 if (!tmp_page)
1817 goto out_unlock;
1818
1819 /*
1820 * The page must not be redirtied until the writeout is completed
1821 * (i.e. userspace has sent a reply to the write request). Otherwise
1822 * there could be more than one temporary page instance for each real
1823 * page.
1824 *
1825 * This is ensured by holding the page lock in page_mkwrite() while
1826 * checking fuse_page_is_writeback(). We already hold the page lock
1827 * since clear_page_dirty_for_io() and keep it held until we add the
1828 * request to the fi->writepages list and increment req->num_pages.
1829 * After this fuse_page_is_writeback() will indicate that the page is
1830 * under writeback, so we can release the page lock.
1831 */
1832 if (data->req == NULL) {
1833 struct fuse_inode *fi = get_fuse_inode(inode);
1834
1835 err = -ENOMEM;
1836 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1837 if (!req) {
1838 __free_page(tmp_page);
1839 goto out_unlock;
1840 }
1841
1842 fuse_write_fill(req, data->ff, page_offset(page), 0);
1843 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001844 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001845 req->in.argpages = 1;
1846 req->background = 1;
1847 req->num_pages = 0;
1848 req->end = fuse_writepage_end;
1849 req->inode = inode;
1850
1851 spin_lock(&fc->lock);
1852 list_add(&req->writepages_entry, &fi->writepages);
1853 spin_unlock(&fc->lock);
1854
1855 data->req = req;
1856 }
1857 set_page_writeback(page);
1858
1859 copy_highpage(tmp_page, page);
1860 req->pages[req->num_pages] = tmp_page;
1861 req->page_descs[req->num_pages].offset = 0;
1862 req->page_descs[req->num_pages].length = PAGE_SIZE;
1863
1864 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1865 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001866
1867 err = 0;
1868 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1869 end_page_writeback(page);
1870 data->req = NULL;
1871 goto out_unlock;
1872 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001873 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001874
1875 /*
1876 * Protected by fc->lock against concurrent access by
1877 * fuse_page_is_writeback().
1878 */
1879 spin_lock(&fc->lock);
1880 req->num_pages++;
1881 spin_unlock(&fc->lock);
1882
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001883out_unlock:
1884 unlock_page(page);
1885
1886 return err;
1887}
1888
1889static int fuse_writepages(struct address_space *mapping,
1890 struct writeback_control *wbc)
1891{
1892 struct inode *inode = mapping->host;
1893 struct fuse_fill_wb_data data;
1894 int err;
1895
1896 err = -EIO;
1897 if (is_bad_inode(inode))
1898 goto out;
1899
1900 data.inode = inode;
1901 data.req = NULL;
1902 data.ff = NULL;
1903
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001904 err = -ENOMEM;
1905 data.orig_pages = kzalloc(sizeof(struct page *) *
1906 FUSE_MAX_PAGES_PER_REQ,
1907 GFP_NOFS);
1908 if (!data.orig_pages)
1909 goto out;
1910
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001911 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1912 if (data.req) {
1913 /* Ignore errors if we can write at least one page */
1914 BUG_ON(!data.req->num_pages);
1915 fuse_writepages_send(&data);
1916 err = 0;
1917 }
1918 if (data.ff)
1919 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001920
1921 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001922out:
1923 return err;
1924}
1925
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001926static int fuse_launder_page(struct page *page)
1927{
1928 int err = 0;
1929 if (clear_page_dirty_for_io(page)) {
1930 struct inode *inode = page->mapping->host;
1931 err = fuse_writepage_locked(page);
1932 if (!err)
1933 fuse_wait_on_page_writeback(inode, page->index);
1934 }
1935 return err;
1936}
1937
1938/*
1939 * Write back dirty pages now, because there may not be any suitable
1940 * open files later
1941 */
1942static void fuse_vma_close(struct vm_area_struct *vma)
1943{
1944 filemap_write_and_wait(vma->vm_file->f_mapping);
1945}
1946
1947/*
1948 * Wait for writeback against this page to complete before allowing it
1949 * to be marked dirty again, and hence written back again, possibly
1950 * before the previous writepage completed.
1951 *
1952 * Block here, instead of in ->writepage(), so that the userspace fs
1953 * can only block processes actually operating on the filesystem.
1954 *
1955 * Otherwise unprivileged userspace fs would be able to block
1956 * unrelated:
1957 *
1958 * - page migration
1959 * - sync(2)
1960 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1961 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001962static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001963{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001964 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02001965 struct inode *inode = file_inode(vma->vm_file);
1966
1967 file_update_time(vma->vm_file);
1968 lock_page(page);
1969 if (page->mapping != inode->i_mapping) {
1970 unlock_page(page);
1971 return VM_FAULT_NOPAGE;
1972 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001973
1974 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02001975 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001976}
1977
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001978static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001979 .close = fuse_vma_close,
1980 .fault = filemap_fault,
1981 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001982 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001983};
1984
1985static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1986{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04001987 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
1988 fuse_link_write_file(file);
1989
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001990 file_accessed(file);
1991 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001992 return 0;
1993}
1994
Miklos Szeredifc280c92009-04-02 14:25:35 +02001995static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1996{
1997 /* Can't provide the coherency needed for MAP_SHARED */
1998 if (vma->vm_flags & VM_MAYSHARE)
1999 return -ENODEV;
2000
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002001 invalidate_inode_pages2(file->f_mapping);
2002
Miklos Szeredifc280c92009-04-02 14:25:35 +02002003 return generic_file_mmap(file, vma);
2004}
2005
Miklos Szeredi71421252006-06-25 05:48:52 -07002006static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2007 struct file_lock *fl)
2008{
2009 switch (ffl->type) {
2010 case F_UNLCK:
2011 break;
2012
2013 case F_RDLCK:
2014 case F_WRLCK:
2015 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2016 ffl->end < ffl->start)
2017 return -EIO;
2018
2019 fl->fl_start = ffl->start;
2020 fl->fl_end = ffl->end;
2021 fl->fl_pid = ffl->pid;
2022 break;
2023
2024 default:
2025 return -EIO;
2026 }
2027 fl->fl_type = ffl->type;
2028 return 0;
2029}
2030
2031static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002032 const struct file_lock *fl, int opcode, pid_t pid,
2033 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002034{
Al Viro6131ffa2013-02-27 16:59:05 -05002035 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002036 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002037 struct fuse_file *ff = file->private_data;
2038 struct fuse_lk_in *arg = &req->misc.lk_in;
2039
2040 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002041 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002042 arg->lk.start = fl->fl_start;
2043 arg->lk.end = fl->fl_end;
2044 arg->lk.type = fl->fl_type;
2045 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002046 if (flock)
2047 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002048 req->in.h.opcode = opcode;
2049 req->in.h.nodeid = get_node_id(inode);
2050 req->in.numargs = 1;
2051 req->in.args[0].size = sizeof(*arg);
2052 req->in.args[0].value = arg;
2053}
2054
2055static int fuse_getlk(struct file *file, struct file_lock *fl)
2056{
Al Viro6131ffa2013-02-27 16:59:05 -05002057 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002058 struct fuse_conn *fc = get_fuse_conn(inode);
2059 struct fuse_req *req;
2060 struct fuse_lk_out outarg;
2061 int err;
2062
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002063 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002064 if (IS_ERR(req))
2065 return PTR_ERR(req);
2066
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002067 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002068 req->out.numargs = 1;
2069 req->out.args[0].size = sizeof(outarg);
2070 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002071 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002072 err = req->out.h.error;
2073 fuse_put_request(fc, req);
2074 if (!err)
2075 err = convert_fuse_file_lock(&outarg.lk, fl);
2076
2077 return err;
2078}
2079
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002080static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002081{
Al Viro6131ffa2013-02-27 16:59:05 -05002082 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002083 struct fuse_conn *fc = get_fuse_conn(inode);
2084 struct fuse_req *req;
2085 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2086 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2087 int err;
2088
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002089 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002090 /* NLM needs asynchronous locks, which we don't support yet */
2091 return -ENOLCK;
2092 }
2093
Miklos Szeredi71421252006-06-25 05:48:52 -07002094 /* Unlock on close is handled by the flush method */
2095 if (fl->fl_flags & FL_CLOSE)
2096 return 0;
2097
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002098 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002099 if (IS_ERR(req))
2100 return PTR_ERR(req);
2101
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002102 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002103 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002104 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002105 /* locking is restartable */
2106 if (err == -EINTR)
2107 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002108 fuse_put_request(fc, req);
2109 return err;
2110}
2111
2112static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2113{
Al Viro6131ffa2013-02-27 16:59:05 -05002114 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002115 struct fuse_conn *fc = get_fuse_conn(inode);
2116 int err;
2117
Miklos Szeredi48e90762008-07-25 01:49:02 -07002118 if (cmd == F_CANCELLK) {
2119 err = 0;
2120 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002121 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002122 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002123 err = 0;
2124 } else
2125 err = fuse_getlk(file, fl);
2126 } else {
2127 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002128 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002129 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002130 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002131 }
2132 return err;
2133}
2134
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002135static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2136{
Al Viro6131ffa2013-02-27 16:59:05 -05002137 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002138 struct fuse_conn *fc = get_fuse_conn(inode);
2139 int err;
2140
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002141 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002142 err = flock_lock_file_wait(file, fl);
2143 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002144 struct fuse_file *ff = file->private_data;
2145
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002146 /* emulate flock with POSIX locks */
2147 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002148 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002149 err = fuse_setlk(file, fl, 1);
2150 }
2151
2152 return err;
2153}
2154
Miklos Szeredib2d22722006-12-06 20:35:51 -08002155static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2156{
2157 struct inode *inode = mapping->host;
2158 struct fuse_conn *fc = get_fuse_conn(inode);
2159 struct fuse_req *req;
2160 struct fuse_bmap_in inarg;
2161 struct fuse_bmap_out outarg;
2162 int err;
2163
2164 if (!inode->i_sb->s_bdev || fc->no_bmap)
2165 return 0;
2166
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002167 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002168 if (IS_ERR(req))
2169 return 0;
2170
2171 memset(&inarg, 0, sizeof(inarg));
2172 inarg.block = block;
2173 inarg.blocksize = inode->i_sb->s_blocksize;
2174 req->in.h.opcode = FUSE_BMAP;
2175 req->in.h.nodeid = get_node_id(inode);
2176 req->in.numargs = 1;
2177 req->in.args[0].size = sizeof(inarg);
2178 req->in.args[0].value = &inarg;
2179 req->out.numargs = 1;
2180 req->out.args[0].size = sizeof(outarg);
2181 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002182 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002183 err = req->out.h.error;
2184 fuse_put_request(fc, req);
2185 if (err == -ENOSYS)
2186 fc->no_bmap = 1;
2187
2188 return err ? 0 : outarg.block;
2189}
2190
Andrew Morton965c8e52012-12-17 15:59:39 -08002191static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002192{
2193 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002194 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002195
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002196 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002197 if (whence == SEEK_CUR || whence == SEEK_SET)
2198 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002199
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002200 mutex_lock(&inode->i_mutex);
2201 retval = fuse_update_attributes(inode, NULL, file, NULL);
2202 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002203 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002204 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002205
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002206 return retval;
2207}
2208
Tejun Heo59efec72008-11-26 12:03:55 +01002209static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2210 unsigned int nr_segs, size_t bytes, bool to_user)
2211{
2212 struct iov_iter ii;
2213 int page_idx = 0;
2214
2215 if (!bytes)
2216 return 0;
2217
2218 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2219
2220 while (iov_iter_count(&ii)) {
2221 struct page *page = pages[page_idx++];
2222 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002223 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002224
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002225 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002226
2227 while (todo) {
2228 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2229 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2230 size_t copy = min(todo, iov_len);
2231 size_t left;
2232
2233 if (!to_user)
2234 left = copy_from_user(kaddr, uaddr, copy);
2235 else
2236 left = copy_to_user(uaddr, kaddr, copy);
2237
2238 if (unlikely(left))
2239 return -EFAULT;
2240
2241 iov_iter_advance(&ii, copy);
2242 todo -= copy;
2243 kaddr += copy;
2244 }
2245
Jens Axboe0bd87182009-11-03 11:40:44 +01002246 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002247 }
2248
2249 return 0;
2250}
2251
2252/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002253 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2254 * ABI was defined to be 'struct iovec' which is different on 32bit
2255 * and 64bit. Fortunately we can determine which structure the server
2256 * used from the size of the reply.
2257 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002258static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2259 size_t transferred, unsigned count,
2260 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002261{
2262#ifdef CONFIG_COMPAT
2263 if (count * sizeof(struct compat_iovec) == transferred) {
2264 struct compat_iovec *ciov = src;
2265 unsigned i;
2266
2267 /*
2268 * With this interface a 32bit server cannot support
2269 * non-compat (i.e. ones coming from 64bit apps) ioctl
2270 * requests
2271 */
2272 if (!is_compat)
2273 return -EINVAL;
2274
2275 for (i = 0; i < count; i++) {
2276 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2277 dst[i].iov_len = ciov[i].iov_len;
2278 }
2279 return 0;
2280 }
2281#endif
2282
2283 if (count * sizeof(struct iovec) != transferred)
2284 return -EIO;
2285
2286 memcpy(dst, src, transferred);
2287 return 0;
2288}
2289
Miklos Szeredi75727772010-11-30 16:39:27 +01002290/* Make sure iov_length() won't overflow */
2291static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2292{
2293 size_t n;
2294 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2295
Zach Brownfb6ccff2012-07-24 12:10:11 -07002296 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002297 if (iov->iov_len > (size_t) max)
2298 return -ENOMEM;
2299 max -= iov->iov_len;
2300 }
2301 return 0;
2302}
2303
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002304static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2305 void *src, size_t transferred, unsigned count,
2306 bool is_compat)
2307{
2308 unsigned i;
2309 struct fuse_ioctl_iovec *fiov = src;
2310
2311 if (fc->minor < 16) {
2312 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2313 count, is_compat);
2314 }
2315
2316 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2317 return -EIO;
2318
2319 for (i = 0; i < count; i++) {
2320 /* Did the server supply an inappropriate value? */
2321 if (fiov[i].base != (unsigned long) fiov[i].base ||
2322 fiov[i].len != (unsigned long) fiov[i].len)
2323 return -EIO;
2324
2325 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2326 dst[i].iov_len = (size_t) fiov[i].len;
2327
2328#ifdef CONFIG_COMPAT
2329 if (is_compat &&
2330 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2331 (compat_size_t) dst[i].iov_len != fiov[i].len))
2332 return -EIO;
2333#endif
2334 }
2335
2336 return 0;
2337}
2338
2339
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002340/*
Tejun Heo59efec72008-11-26 12:03:55 +01002341 * For ioctls, there is no generic way to determine how much memory
2342 * needs to be read and/or written. Furthermore, ioctls are allowed
2343 * to dereference the passed pointer, so the parameter requires deep
2344 * copying but FUSE has no idea whatsoever about what to copy in or
2345 * out.
2346 *
2347 * This is solved by allowing FUSE server to retry ioctl with
2348 * necessary in/out iovecs. Let's assume the ioctl implementation
2349 * needs to read in the following structure.
2350 *
2351 * struct a {
2352 * char *buf;
2353 * size_t buflen;
2354 * }
2355 *
2356 * On the first callout to FUSE server, inarg->in_size and
2357 * inarg->out_size will be NULL; then, the server completes the ioctl
2358 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2359 * the actual iov array to
2360 *
2361 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2362 *
2363 * which tells FUSE to copy in the requested area and retry the ioctl.
2364 * On the second round, the server has access to the structure and
2365 * from that it can tell what to look for next, so on the invocation,
2366 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2367 *
2368 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2369 * { .iov_base = a.buf, .iov_len = a.buflen } }
2370 *
2371 * FUSE will copy both struct a and the pointed buffer from the
2372 * process doing the ioctl and retry ioctl with both struct a and the
2373 * buffer.
2374 *
2375 * This time, FUSE server has everything it needs and completes ioctl
2376 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2377 *
2378 * Copying data out works the same way.
2379 *
2380 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2381 * automatically initializes in and out iovs by decoding @cmd with
2382 * _IOC_* macros and the server is not allowed to request RETRY. This
2383 * limits ioctl data transfers to well-formed ioctls and is the forced
2384 * behavior for all FUSE servers.
2385 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002386long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2387 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002388{
Tejun Heo59efec72008-11-26 12:03:55 +01002389 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002390 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002391 struct fuse_ioctl_in inarg = {
2392 .fh = ff->fh,
2393 .cmd = cmd,
2394 .arg = arg,
2395 .flags = flags
2396 };
2397 struct fuse_ioctl_out outarg;
2398 struct fuse_req *req = NULL;
2399 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002400 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002401 struct iovec *in_iov = NULL, *out_iov = NULL;
2402 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2403 size_t in_size, out_size, transferred;
2404 int err;
2405
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002406#if BITS_PER_LONG == 32
2407 inarg.flags |= FUSE_IOCTL_32BIT;
2408#else
2409 if (flags & FUSE_IOCTL_COMPAT)
2410 inarg.flags |= FUSE_IOCTL_32BIT;
2411#endif
2412
Tejun Heo59efec72008-11-26 12:03:55 +01002413 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002414 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002415
Tejun Heo59efec72008-11-26 12:03:55 +01002416 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002417 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002418 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002419 if (!pages || !iov_page)
2420 goto out;
2421
2422 /*
2423 * If restricted, initialize IO parameters as encoded in @cmd.
2424 * RETRY from server is not allowed.
2425 */
2426 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002427 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002428
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002429 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002430 iov->iov_len = _IOC_SIZE(cmd);
2431
2432 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2433 in_iov = iov;
2434 in_iovs = 1;
2435 }
2436
2437 if (_IOC_DIR(cmd) & _IOC_READ) {
2438 out_iov = iov;
2439 out_iovs = 1;
2440 }
2441 }
2442
2443 retry:
2444 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2445 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2446
2447 /*
2448 * Out data can be used either for actual out data or iovs,
2449 * make sure there always is at least one page.
2450 */
2451 out_size = max_t(size_t, out_size, PAGE_SIZE);
2452 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2453
2454 /* make sure there are enough buffer pages and init request with them */
2455 err = -ENOMEM;
2456 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2457 goto out;
2458 while (num_pages < max_pages) {
2459 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2460 if (!pages[num_pages])
2461 goto out;
2462 num_pages++;
2463 }
2464
Maxim Patlasov54b96672012-10-26 19:49:13 +04002465 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002466 if (IS_ERR(req)) {
2467 err = PTR_ERR(req);
2468 req = NULL;
2469 goto out;
2470 }
2471 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2472 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002473 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002474
2475 /* okay, let's send it to the client */
2476 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002477 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002478 req->in.numargs = 1;
2479 req->in.args[0].size = sizeof(inarg);
2480 req->in.args[0].value = &inarg;
2481 if (in_size) {
2482 req->in.numargs++;
2483 req->in.args[1].size = in_size;
2484 req->in.argpages = 1;
2485
2486 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2487 false);
2488 if (err)
2489 goto out;
2490 }
2491
2492 req->out.numargs = 2;
2493 req->out.args[0].size = sizeof(outarg);
2494 req->out.args[0].value = &outarg;
2495 req->out.args[1].size = out_size;
2496 req->out.argpages = 1;
2497 req->out.argvar = 1;
2498
Tejun Heob93f8582008-11-26 12:03:55 +01002499 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002500 err = req->out.h.error;
2501 transferred = req->out.args[1].size;
2502 fuse_put_request(fc, req);
2503 req = NULL;
2504 if (err)
2505 goto out;
2506
2507 /* did it ask for retry? */
2508 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002509 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002510
2511 /* no retry if in restricted mode */
2512 err = -EIO;
2513 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2514 goto out;
2515
2516 in_iovs = outarg.in_iovs;
2517 out_iovs = outarg.out_iovs;
2518
2519 /*
2520 * Make sure things are in boundary, separate checks
2521 * are to protect against overflow.
2522 */
2523 err = -ENOMEM;
2524 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2525 out_iovs > FUSE_IOCTL_MAX_IOV ||
2526 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2527 goto out;
2528
Cong Wang2408f6e2011-11-25 23:14:30 +08002529 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002530 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002531 transferred, in_iovs + out_iovs,
2532 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002533 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002534 if (err)
2535 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002536
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002537 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002538 out_iov = in_iov + in_iovs;
2539
Miklos Szeredi75727772010-11-30 16:39:27 +01002540 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2541 if (err)
2542 goto out;
2543
2544 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2545 if (err)
2546 goto out;
2547
Tejun Heo59efec72008-11-26 12:03:55 +01002548 goto retry;
2549 }
2550
2551 err = -EIO;
2552 if (transferred > inarg.out_size)
2553 goto out;
2554
2555 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2556 out:
2557 if (req)
2558 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002559 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002560 while (num_pages)
2561 __free_page(pages[--num_pages]);
2562 kfree(pages);
2563
2564 return err ? err : outarg.result;
2565}
Tejun Heo08cbf542009-04-14 10:54:53 +09002566EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002567
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002568long fuse_ioctl_common(struct file *file, unsigned int cmd,
2569 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002570{
Al Viro6131ffa2013-02-27 16:59:05 -05002571 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002572 struct fuse_conn *fc = get_fuse_conn(inode);
2573
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002574 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002575 return -EACCES;
2576
2577 if (is_bad_inode(inode))
2578 return -EIO;
2579
2580 return fuse_do_ioctl(file, cmd, arg, flags);
2581}
2582
Tejun Heo59efec72008-11-26 12:03:55 +01002583static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2584 unsigned long arg)
2585{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002586 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002587}
2588
2589static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2590 unsigned long arg)
2591{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002592 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002593}
2594
Tejun Heo95668a62008-11-26 12:03:55 +01002595/*
2596 * All files which have been polled are linked to RB tree
2597 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2598 * find the matching one.
2599 */
2600static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2601 struct rb_node **parent_out)
2602{
2603 struct rb_node **link = &fc->polled_files.rb_node;
2604 struct rb_node *last = NULL;
2605
2606 while (*link) {
2607 struct fuse_file *ff;
2608
2609 last = *link;
2610 ff = rb_entry(last, struct fuse_file, polled_node);
2611
2612 if (kh < ff->kh)
2613 link = &last->rb_left;
2614 else if (kh > ff->kh)
2615 link = &last->rb_right;
2616 else
2617 return link;
2618 }
2619
2620 if (parent_out)
2621 *parent_out = last;
2622 return link;
2623}
2624
2625/*
2626 * The file is about to be polled. Make sure it's on the polled_files
2627 * RB tree. Note that files once added to the polled_files tree are
2628 * not removed before the file is released. This is because a file
2629 * polled once is likely to be polled again.
2630 */
2631static void fuse_register_polled_file(struct fuse_conn *fc,
2632 struct fuse_file *ff)
2633{
2634 spin_lock(&fc->lock);
2635 if (RB_EMPTY_NODE(&ff->polled_node)) {
2636 struct rb_node **link, *parent;
2637
2638 link = fuse_find_polled_node(fc, ff->kh, &parent);
2639 BUG_ON(*link);
2640 rb_link_node(&ff->polled_node, parent, link);
2641 rb_insert_color(&ff->polled_node, &fc->polled_files);
2642 }
2643 spin_unlock(&fc->lock);
2644}
2645
Tejun Heo08cbf542009-04-14 10:54:53 +09002646unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002647{
Tejun Heo95668a62008-11-26 12:03:55 +01002648 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002649 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002650 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2651 struct fuse_poll_out outarg;
2652 struct fuse_req *req;
2653 int err;
2654
2655 if (fc->no_poll)
2656 return DEFAULT_POLLMASK;
2657
2658 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002659 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002660
2661 /*
2662 * Ask for notification iff there's someone waiting for it.
2663 * The client may ignore the flag and always notify.
2664 */
2665 if (waitqueue_active(&ff->poll_wait)) {
2666 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2667 fuse_register_polled_file(fc, ff);
2668 }
2669
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002670 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002671 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002672 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002673
2674 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002675 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002676 req->in.numargs = 1;
2677 req->in.args[0].size = sizeof(inarg);
2678 req->in.args[0].value = &inarg;
2679 req->out.numargs = 1;
2680 req->out.args[0].size = sizeof(outarg);
2681 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002682 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002683 err = req->out.h.error;
2684 fuse_put_request(fc, req);
2685
2686 if (!err)
2687 return outarg.revents;
2688 if (err == -ENOSYS) {
2689 fc->no_poll = 1;
2690 return DEFAULT_POLLMASK;
2691 }
2692 return POLLERR;
2693}
Tejun Heo08cbf542009-04-14 10:54:53 +09002694EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002695
2696/*
2697 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2698 * wakes up the poll waiters.
2699 */
2700int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2701 struct fuse_notify_poll_wakeup_out *outarg)
2702{
2703 u64 kh = outarg->kh;
2704 struct rb_node **link;
2705
2706 spin_lock(&fc->lock);
2707
2708 link = fuse_find_polled_node(fc, kh, NULL);
2709 if (*link) {
2710 struct fuse_file *ff;
2711
2712 ff = rb_entry(*link, struct fuse_file, polled_node);
2713 wake_up_interruptible_sync(&ff->poll_wait);
2714 }
2715
2716 spin_unlock(&fc->lock);
2717 return 0;
2718}
2719
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002720static void fuse_do_truncate(struct file *file)
2721{
2722 struct inode *inode = file->f_mapping->host;
2723 struct iattr attr;
2724
2725 attr.ia_valid = ATTR_SIZE;
2726 attr.ia_size = i_size_read(inode);
2727
2728 attr.ia_file = file;
2729 attr.ia_valid |= ATTR_FILE;
2730
2731 fuse_do_setattr(inode, &attr, file);
2732}
2733
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002734static inline loff_t fuse_round_up(loff_t off)
2735{
2736 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2737}
2738
Anand Avati4273b792012-02-17 12:46:25 -05002739static ssize_t
2740fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2741 loff_t offset, unsigned long nr_segs)
2742{
2743 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002744 struct file *file = iocb->ki_filp;
2745 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002746 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002747 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002748 struct inode *inode;
2749 loff_t i_size;
2750 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002751 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002752
Anand Avati4273b792012-02-17 12:46:25 -05002753 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002754 inode = file->f_mapping->host;
2755 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002756
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002757 if ((rw == READ) && (offset > i_size))
2758 return 0;
2759
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002760 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002761 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002762 if (offset >= i_size)
2763 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002764 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002765 }
2766
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002767 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002768 if (!io)
2769 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002770 spin_lock_init(&io->lock);
2771 io->reqs = 1;
2772 io->bytes = -1;
2773 io->size = 0;
2774 io->offset = offset;
2775 io->write = (rw == WRITE);
2776 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002777 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002778 /*
2779 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002780 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002781 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002782 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002783 io->iocb = iocb;
2784
2785 /*
2786 * We cannot asynchronously extend the size of a file. We have no method
2787 * to wait on real async I/O requests, so we must submit this request
2788 * synchronously.
2789 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002790 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002791 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002792
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002793 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002794 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002795 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002796 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002797
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002798 if (io->async) {
2799 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2800
2801 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002802 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002803 return -EIOCBQUEUED;
2804
2805 ret = wait_on_sync_kiocb(iocb);
2806 } else {
2807 kfree(io);
2808 }
2809
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002810 if (rw == WRITE) {
2811 if (ret > 0)
2812 fuse_write_update_size(inode, pos);
2813 else if (ret < 0 && offset + count > i_size)
2814 fuse_do_truncate(file);
2815 }
Anand Avati4273b792012-02-17 12:46:25 -05002816
2817 return ret;
2818}
2819
Miklos Szeredicdadb112012-11-10 16:55:56 +01002820static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2821 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002822{
2823 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002824 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002825 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002826 struct fuse_conn *fc = ff->fc;
2827 struct fuse_req *req;
2828 struct fuse_fallocate_in inarg = {
2829 .fh = ff->fh,
2830 .offset = offset,
2831 .length = length,
2832 .mode = mode
2833 };
2834 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002835 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2836 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002837
Miklos Szeredi519c6042012-04-26 10:56:36 +02002838 if (fc->no_fallocate)
2839 return -EOPNOTSUPP;
2840
Maxim Patlasov14c14412013-06-13 12:16:39 +04002841 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002842 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002843 if (mode & FALLOC_FL_PUNCH_HOLE) {
2844 loff_t endbyte = offset + length - 1;
2845 err = filemap_write_and_wait_range(inode->i_mapping,
2846 offset, endbyte);
2847 if (err)
2848 goto out;
2849
2850 fuse_sync_writes(inode);
2851 }
Brian Foster3634a632013-05-17 09:30:32 -04002852 }
2853
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002854 if (!(mode & FALLOC_FL_KEEP_SIZE))
2855 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2856
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002857 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04002858 if (IS_ERR(req)) {
2859 err = PTR_ERR(req);
2860 goto out;
2861 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002862
2863 req->in.h.opcode = FUSE_FALLOCATE;
2864 req->in.h.nodeid = ff->nodeid;
2865 req->in.numargs = 1;
2866 req->in.args[0].size = sizeof(inarg);
2867 req->in.args[0].value = &inarg;
2868 fuse_request_send(fc, req);
2869 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002870 if (err == -ENOSYS) {
2871 fc->no_fallocate = 1;
2872 err = -EOPNOTSUPP;
2873 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002874 fuse_put_request(fc, req);
2875
Brian Fosterbee6c302013-05-17 15:27:34 -04002876 if (err)
2877 goto out;
2878
2879 /* we could have extended the file */
2880 if (!(mode & FALLOC_FL_KEEP_SIZE))
2881 fuse_write_update_size(inode, offset + length);
2882
2883 if (mode & FALLOC_FL_PUNCH_HOLE)
2884 truncate_pagecache_range(inode, offset, offset + length - 1);
2885
2886 fuse_invalidate_attr(inode);
2887
Brian Foster3634a632013-05-17 09:30:32 -04002888out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002889 if (!(mode & FALLOC_FL_KEEP_SIZE))
2890 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2891
Maxim Patlasovbde52782013-09-13 19:19:54 +04002892 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04002893 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04002894
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002895 return err;
2896}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002897
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002898static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002899 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002900 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002901 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002902 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002903 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002904 .mmap = fuse_file_mmap,
2905 .open = fuse_open,
2906 .flush = fuse_flush,
2907 .release = fuse_release,
2908 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002909 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002910 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002911 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002912 .unlocked_ioctl = fuse_file_ioctl,
2913 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002914 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002915 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002916};
2917
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002918static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002919 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002920 .read = fuse_direct_read,
2921 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002922 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002923 .open = fuse_open,
2924 .flush = fuse_flush,
2925 .release = fuse_release,
2926 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002927 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002928 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002929 .unlocked_ioctl = fuse_file_ioctl,
2930 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002931 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002932 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002933 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002934};
2935
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002936static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002937 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002938 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04002939 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002940 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002941 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002942 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002943 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05002944 .direct_IO = fuse_direct_IO,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002945};
2946
2947void fuse_init_file_inode(struct inode *inode)
2948{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002949 inode->i_fop = &fuse_file_operations;
2950 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002951}