blob: 4002889fbcc11a7ce0d44831a55e63075c770794 [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>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070018
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080019static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070020
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020021static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
22 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070023{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070024 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080025 struct fuse_req *req;
26 int err;
27
Maxim Patlasovb111c8c2012-10-26 19:48:30 +040028 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -070029 if (IS_ERR(req))
30 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080031
32 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070033 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
34 if (!fc->atomic_o_trunc)
35 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020036 req->in.h.opcode = opcode;
37 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080038 req->in.numargs = 1;
39 req->in.args[0].size = sizeof(inarg);
40 req->in.args[0].value = &inarg;
41 req->out.numargs = 1;
42 req->out.args[0].size = sizeof(*outargp);
43 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010044 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080045 err = req->out.h.error;
46 fuse_put_request(fc, req);
47
48 return err;
49}
50
Tejun Heoacf99432008-11-26 12:03:55 +010051struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080052{
53 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090054
Miklos Szeredifd72faa2005-11-07 00:59:51 -080055 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090056 if (unlikely(!ff))
57 return NULL;
58
Miklos Szeredida5e4712009-04-28 16:56:36 +020059 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040060 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090061 if (unlikely(!ff->reserved_req)) {
62 kfree(ff);
63 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080064 }
Tejun Heo6b2db282009-04-14 10:54:49 +090065
66 INIT_LIST_HEAD(&ff->write_entry);
67 atomic_set(&ff->count, 0);
68 RB_CLEAR_NODE(&ff->polled_node);
69 init_waitqueue_head(&ff->poll_wait);
70
71 spin_lock(&fc->lock);
72 ff->kh = ++fc->khctr;
73 spin_unlock(&fc->lock);
74
Miklos Szeredifd72faa2005-11-07 00:59:51 -080075 return ff;
76}
77
78void fuse_file_free(struct fuse_file *ff)
79{
Miklos Szeredi33649c92006-06-25 05:48:52 -070080 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080081 kfree(ff);
82}
83
Miklos Szeredic7b71432009-04-28 16:56:37 +020084struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070085{
86 atomic_inc(&ff->count);
87 return ff;
88}
89
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010090static void fuse_release_async(struct work_struct *work)
Miklos Szeredi819c4b32007-10-16 23:31:04 -070091{
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010092 struct fuse_req *req;
93 struct fuse_conn *fc;
94 struct path path;
95
96 req = container_of(work, struct fuse_req, misc.release.work);
97 path = req->misc.release.path;
98 fc = get_fuse_conn(path.dentry->d_inode);
99
100 fuse_put_request(fc, req);
101 path_put(&path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -0700102}
103
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100104static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
105{
106 if (fc->destroy_req) {
107 /*
108 * If this is a fuseblk mount, then it's possible that
109 * releasing the path will result in releasing the
110 * super block and sending the DESTROY request. If
111 * the server is single threaded, this would hang.
112 * For this reason do the path_put() in a separate
113 * thread.
114 */
115 atomic_inc(&req->count);
116 INIT_WORK(&req->misc.release.work, fuse_release_async);
117 schedule_work(&req->misc.release.work);
118 } else {
119 path_put(&req->misc.release.path);
120 }
121}
122
123static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700124{
125 if (atomic_dec_and_test(&ff->count)) {
126 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200127
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100128 if (sync) {
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400129 req->background = 0;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100130 fuse_request_send(ff->fc, req);
131 path_put(&req->misc.release.path);
132 fuse_put_request(ff->fc, req);
133 } else {
134 req->end = fuse_release_end;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400135 req->background = 1;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100136 fuse_request_send_background(ff->fc, req);
137 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700138 kfree(ff);
139 }
140}
141
Tejun Heo08cbf542009-04-14 10:54:53 +0900142int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
143 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200144{
145 struct fuse_open_out outarg;
146 struct fuse_file *ff;
147 int err;
148 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
149
150 ff = fuse_file_alloc(fc);
151 if (!ff)
152 return -ENOMEM;
153
154 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
155 if (err) {
156 fuse_file_free(ff);
157 return err;
158 }
159
160 if (isdir)
161 outarg.open_flags &= ~FOPEN_DIRECT_IO;
162
163 ff->fh = outarg.fh;
164 ff->nodeid = nodeid;
165 ff->open_flags = outarg.open_flags;
166 file->private_data = fuse_file_get(ff);
167
168 return 0;
169}
Tejun Heo08cbf542009-04-14 10:54:53 +0900170EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200171
Miklos Szeredic7b71432009-04-28 16:56:37 +0200172void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800173{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200174 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800175 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200176
177 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800178 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200179 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700180 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200181 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200182 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800183 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
184 struct fuse_inode *fi = get_fuse_inode(inode);
185
186 spin_lock(&fc->lock);
187 fi->attr_version = ++fc->attr_version;
188 i_size_write(inode, 0);
189 spin_unlock(&fc->lock);
190 fuse_invalidate_attr(inode);
191 }
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800192}
193
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200194int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800195{
Tejun Heoacf99432008-11-26 12:03:55 +0100196 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700197 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700198
199 err = generic_file_open(inode, file);
200 if (err)
201 return err;
202
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200203 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800204 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200205 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700206
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200207 fuse_finish_open(inode, file);
208
209 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700210}
211
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200212static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800213{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200214 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700215 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800216 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700217
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200218 spin_lock(&fc->lock);
219 list_del(&ff->write_entry);
220 if (!RB_EMPTY_NODE(&ff->polled_node))
221 rb_erase(&ff->polled_node, &fc->polled_files);
222 spin_unlock(&fc->lock);
223
Bryan Green357ccf22011-03-01 16:43:52 -0800224 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200225
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700226 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800227 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700228 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200229 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700230 req->in.numargs = 1;
231 req->in.args[0].size = sizeof(struct fuse_release_in);
232 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800233}
234
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200235void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800236{
Tejun Heo6b2db282009-04-14 10:54:49 +0900237 struct fuse_file *ff;
238 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700239
Tejun Heo6b2db282009-04-14 10:54:49 +0900240 ff = file->private_data;
241 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200242 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700243
Tejun Heo6b2db282009-04-14 10:54:49 +0900244 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200245 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100246
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200247 if (ff->flock) {
248 struct fuse_release_in *inarg = &req->misc.release.in;
249 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
250 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
251 (fl_owner_t) file);
252 }
Tejun Heo6b2db282009-04-14 10:54:49 +0900253 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200254 path_get(&file->f_path);
255 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700256
Tejun Heo6b2db282009-04-14 10:54:49 +0900257 /*
258 * Normally this will send the RELEASE request, however if
259 * some asynchronous READ or WRITE requests are outstanding,
260 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100261 *
262 * Make the release synchronous if this is a fuseblk mount,
263 * synchronous RELEASE is allowed (and desirable) in this case
264 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900265 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100266 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700267}
268
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700269static int fuse_open(struct inode *inode, struct file *file)
270{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200271 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700272}
273
274static int fuse_release(struct inode *inode, struct file *file)
275{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200276 fuse_release_common(file, FUSE_RELEASE);
277
278 /* return value is ignored by VFS */
279 return 0;
280}
281
282void fuse_sync_release(struct fuse_file *ff, int flags)
283{
284 WARN_ON(atomic_read(&ff->count) > 1);
285 fuse_prepare_release(ff, flags, FUSE_RELEASE);
286 ff->reserved_req->force = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400287 ff->reserved_req->background = 0;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200288 fuse_request_send(ff->fc, ff->reserved_req);
289 fuse_put_request(ff->fc, ff->reserved_req);
290 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700291}
Tejun Heo08cbf542009-04-14 10:54:53 +0900292EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700293
Miklos Szeredi71421252006-06-25 05:48:52 -0700294/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700295 * Scramble the ID space with XTEA, so that the value of the files_struct
296 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700297 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700298u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700299{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700300 u32 *k = fc->scramble_key;
301 u64 v = (unsigned long) id;
302 u32 v0 = v;
303 u32 v1 = v >> 32;
304 u32 sum = 0;
305 int i;
306
307 for (i = 0; i < 32; i++) {
308 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
309 sum += 0x9E3779B9;
310 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
311 }
312
313 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700314}
315
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700316/*
317 * Check if page is under writeback
318 *
319 * This is currently done by walking the list of writepage requests
320 * for the inode, which can be pretty inefficient.
321 */
322static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
323{
324 struct fuse_conn *fc = get_fuse_conn(inode);
325 struct fuse_inode *fi = get_fuse_inode(inode);
326 struct fuse_req *req;
327 bool found = false;
328
329 spin_lock(&fc->lock);
330 list_for_each_entry(req, &fi->writepages, writepages_entry) {
331 pgoff_t curr_index;
332
333 BUG_ON(req->inode != inode);
334 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
335 if (curr_index == index) {
336 found = true;
337 break;
338 }
339 }
340 spin_unlock(&fc->lock);
341
342 return found;
343}
344
345/*
346 * Wait for page writeback to be completed.
347 *
348 * Since fuse doesn't rely on the VM writeback tracking, this has to
349 * use some other means.
350 */
351static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
352{
353 struct fuse_inode *fi = get_fuse_inode(inode);
354
355 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
356 return 0;
357}
358
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700359static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700360{
Al Viro6131ffa2013-02-27 16:59:05 -0500361 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700362 struct fuse_conn *fc = get_fuse_conn(inode);
363 struct fuse_file *ff = file->private_data;
364 struct fuse_req *req;
365 struct fuse_flush_in inarg;
366 int err;
367
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800368 if (is_bad_inode(inode))
369 return -EIO;
370
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700371 if (fc->no_flush)
372 return 0;
373
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400374 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700375 memset(&inarg, 0, sizeof(inarg));
376 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700377 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700378 req->in.h.opcode = FUSE_FLUSH;
379 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700380 req->in.numargs = 1;
381 req->in.args[0].size = sizeof(inarg);
382 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700383 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100384 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700385 err = req->out.h.error;
386 fuse_put_request(fc, req);
387 if (err == -ENOSYS) {
388 fc->no_flush = 1;
389 err = 0;
390 }
391 return err;
392}
393
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700394/*
395 * Wait for all pending writepages on the inode to finish.
396 *
397 * This is currently done by blocking further writes with FUSE_NOWRITE
398 * and waiting for all sent writes to complete.
399 *
400 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
401 * could conflict with truncation.
402 */
403static void fuse_sync_writes(struct inode *inode)
404{
405 fuse_set_nowrite(inode);
406 fuse_release_nowrite(inode);
407}
408
Josef Bacik02c24a82011-07-16 20:44:56 -0400409int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
410 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700411{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200412 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700413 struct fuse_conn *fc = get_fuse_conn(inode);
414 struct fuse_file *ff = file->private_data;
415 struct fuse_req *req;
416 struct fuse_fsync_in inarg;
417 int err;
418
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800419 if (is_bad_inode(inode))
420 return -EIO;
421
Josef Bacik02c24a82011-07-16 20:44:56 -0400422 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
423 if (err)
424 return err;
425
Miklos Szeredi82547982005-09-09 13:10:38 -0700426 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700427 return 0;
428
Josef Bacik02c24a82011-07-16 20:44:56 -0400429 mutex_lock(&inode->i_mutex);
430
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700431 /*
432 * Start writeback against all dirty pages of the inode, then
433 * wait for all outstanding writes, before sending the FSYNC
434 * request.
435 */
436 err = write_inode_now(inode, 0);
437 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400438 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700439
440 fuse_sync_writes(inode);
441
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400442 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400443 if (IS_ERR(req)) {
444 err = PTR_ERR(req);
445 goto out;
446 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447
448 memset(&inarg, 0, sizeof(inarg));
449 inarg.fh = ff->fh;
450 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700451 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700452 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700453 req->in.numargs = 1;
454 req->in.args[0].size = sizeof(inarg);
455 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100456 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700457 err = req->out.h.error;
458 fuse_put_request(fc, req);
459 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700460 if (isdir)
461 fc->no_fsyncdir = 1;
462 else
463 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700464 err = 0;
465 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400466out:
467 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700468 return err;
469}
470
Josef Bacik02c24a82011-07-16 20:44:56 -0400471static int fuse_fsync(struct file *file, loff_t start, loff_t end,
472 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700473{
Josef Bacik02c24a82011-07-16 20:44:56 -0400474 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700475}
476
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200477void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
478 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700479{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700480 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800481 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700482
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800483 inarg->fh = ff->fh;
484 inarg->offset = pos;
485 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800486 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800487 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200488 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700489 req->in.numargs = 1;
490 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800491 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700492 req->out.argvar = 1;
493 req->out.numargs = 1;
494 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700495}
496
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400497static void fuse_release_user_pages(struct fuse_req *req, int write)
498{
499 unsigned i;
500
501 for (i = 0; i < req->num_pages; i++) {
502 struct page *page = req->pages[i];
503 if (write)
504 set_page_dirty_lock(page);
505 put_page(page);
506 }
507}
508
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400509/**
510 * In case of short read, the caller sets 'pos' to the position of
511 * actual end of fuse request in IO request. Otherwise, if bytes_requested
512 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
513 *
514 * An example:
515 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
516 * both submitted asynchronously. The first of them was ACKed by userspace as
517 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
518 * second request was ACKed as short, e.g. only 1K was read, resulting in
519 * pos == 33K.
520 *
521 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
522 * will be equal to the length of the longest contiguous fragment of
523 * transferred data starting from the beginning of IO request.
524 */
525static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
526{
527 int left;
528
529 spin_lock(&io->lock);
530 if (err)
531 io->err = io->err ? : err;
532 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
533 io->bytes = pos;
534
535 left = --io->reqs;
536 spin_unlock(&io->lock);
537
538 if (!left) {
539 long res;
540
541 if (io->err)
542 res = io->err;
543 else if (io->bytes >= 0 && io->write)
544 res = -EIO;
545 else {
546 res = io->bytes < 0 ? io->size : io->bytes;
547
548 if (!is_sync_kiocb(io->iocb)) {
549 struct path *path = &io->iocb->ki_filp->f_path;
550 struct inode *inode = path->dentry->d_inode;
551 struct fuse_conn *fc = get_fuse_conn(inode);
552 struct fuse_inode *fi = get_fuse_inode(inode);
553
554 spin_lock(&fc->lock);
555 fi->attr_version = ++fc->attr_version;
556 spin_unlock(&fc->lock);
557 }
558 }
559
560 aio_complete(io->iocb, res, 0);
561 kfree(io);
562 }
563}
564
565static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
566{
567 struct fuse_io_priv *io = req->io;
568 ssize_t pos = -1;
569
570 fuse_release_user_pages(req, !io->write);
571
572 if (io->write) {
573 if (req->misc.write.in.size != req->misc.write.out.size)
574 pos = req->misc.write.in.offset - io->offset +
575 req->misc.write.out.size;
576 } else {
577 if (req->misc.read.in.size != req->out.args[0].size)
578 pos = req->misc.read.in.offset - io->offset +
579 req->out.args[0].size;
580 }
581
582 fuse_aio_complete(io, req->out.h.error, pos);
583}
584
585static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
586 size_t num_bytes, struct fuse_io_priv *io)
587{
588 spin_lock(&io->lock);
589 io->size += num_bytes;
590 io->reqs++;
591 spin_unlock(&io->lock);
592
593 req->io = io;
594 req->end = fuse_aio_complete_req;
595
596 fuse_request_send_background(fc, req);
597
598 return num_bytes;
599}
600
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800601static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200602 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700603{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200604 struct fuse_file *ff = file->private_data;
605 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700606
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200607 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700608 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700609 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700610
611 inarg->read_flags |= FUSE_READ_LOCKOWNER;
612 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
613 }
Tejun Heob93f8582008-11-26 12:03:55 +0100614 fuse_request_send(fc, req);
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800615 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700616}
617
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700618static void fuse_read_update_size(struct inode *inode, loff_t size,
619 u64 attr_ver)
620{
621 struct fuse_conn *fc = get_fuse_conn(inode);
622 struct fuse_inode *fi = get_fuse_inode(inode);
623
624 spin_lock(&fc->lock);
625 if (attr_ver == fi->attr_version && size < inode->i_size) {
626 fi->attr_version = ++fc->attr_version;
627 i_size_write(inode, size);
628 }
629 spin_unlock(&fc->lock);
630}
631
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700632static int fuse_readpage(struct file *file, struct page *page)
633{
634 struct inode *inode = page->mapping->host;
635 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800636 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700637 size_t num_read;
638 loff_t pos = page_offset(page);
639 size_t count = PAGE_CACHE_SIZE;
640 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800641 int err;
642
643 err = -EIO;
644 if (is_bad_inode(inode))
645 goto out;
646
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700647 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300648 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700649 * page-cache page, so make sure we read a properly synced
650 * page.
651 */
652 fuse_wait_on_page_writeback(inode, page->index);
653
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400654 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700655 err = PTR_ERR(req);
656 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700657 goto out;
658
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700659 attr_ver = fuse_get_attr_version(fc);
660
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700661 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200662 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700663 req->num_pages = 1;
664 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400665 req->page_descs[0].length = count;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200666 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700667 err = req->out.h.error;
668 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700669
670 if (!err) {
671 /*
672 * Short read means EOF. If file size is larger, truncate it
673 */
674 if (num_read < count)
675 fuse_read_update_size(inode, pos + num_read, attr_ver);
676
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700677 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700678 }
679
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700680 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700681 out:
682 unlock_page(page);
683 return err;
684}
685
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800686static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700687{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800688 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700689 size_t count = req->misc.read.in.size;
690 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200691 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800692
Miklos Szeredice534fb2010-05-25 15:06:07 +0200693 for (i = 0; mapping == NULL && i < req->num_pages; i++)
694 mapping = req->pages[i]->mapping;
695
696 if (mapping) {
697 struct inode *inode = mapping->host;
698
699 /*
700 * Short read means EOF. If file size is larger, truncate it
701 */
702 if (!req->out.h.error && num_read < count) {
703 loff_t pos;
704
705 pos = page_offset(req->pages[0]) + num_read;
706 fuse_read_update_size(inode, pos,
707 req->misc.read.attr_ver);
708 }
709 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700710 }
711
Miklos Szeredidb50b962005-09-09 13:10:33 -0700712 for (i = 0; i < req->num_pages; i++) {
713 struct page *page = req->pages[i];
714 if (!req->out.h.error)
715 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800716 else
717 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700718 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200719 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700720 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700721 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100722 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800723}
724
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200725static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800726{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200727 struct fuse_file *ff = file->private_data;
728 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800729 loff_t pos = page_offset(req->pages[0]);
730 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200731
732 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800733 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200734 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200735 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700736 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800737 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700738 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800739 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100740 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800741 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100742 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800743 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100744 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800745 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700746}
747
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700748struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700749 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800750 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700751 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400752 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700753};
754
755static int fuse_readpages_fill(void *_data, struct page *page)
756{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700757 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700758 struct fuse_req *req = data->req;
759 struct inode *inode = data->inode;
760 struct fuse_conn *fc = get_fuse_conn(inode);
761
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700762 fuse_wait_on_page_writeback(inode, page->index);
763
Miklos Szeredidb50b962005-09-09 13:10:33 -0700764 if (req->num_pages &&
765 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
766 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
767 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400768 int nr_alloc = min_t(unsigned, data->nr_pages,
769 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200770 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400771 if (fc->async_read)
772 req = fuse_get_req_for_background(fc, nr_alloc);
773 else
774 req = fuse_get_req(fc, nr_alloc);
775
776 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700777 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700778 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700779 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700780 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700781 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400782
783 if (WARN_ON(req->num_pages >= req->max_pages)) {
784 fuse_put_request(fc, req);
785 return -EIO;
786 }
787
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200788 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700789 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400790 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100791 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400792 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700793 return 0;
794}
795
796static int fuse_readpages(struct file *file, struct address_space *mapping,
797 struct list_head *pages, unsigned nr_pages)
798{
799 struct inode *inode = mapping->host;
800 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700801 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700802 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400803 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800804
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700805 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800806 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800807 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800808
Miklos Szeredia6643092007-11-28 16:22:00 -0800809 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700810 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400811 if (fc->async_read)
812 data.req = fuse_get_req_for_background(fc, nr_alloc);
813 else
814 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400815 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700816 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700817 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800818 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700819
820 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700821 if (!err) {
822 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200823 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700824 else
825 fuse_put_request(fc, data.req);
826 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800827out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700828 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700829}
830
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800831static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
832 unsigned long nr_segs, loff_t pos)
833{
834 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400835 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800836
Brian Fostera8894272012-07-16 15:23:50 -0400837 /*
838 * In auto invalidate mode, always update attributes on read.
839 * Otherwise, only update if we attempt to read past EOF (to ensure
840 * i_size is up to date).
841 */
842 if (fc->auto_inval_data ||
843 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800844 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800845 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
846 if (err)
847 return err;
848 }
849
850 return generic_file_aio_read(iocb, iov, nr_segs, pos);
851}
852
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200853static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200854 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700855{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700856 struct fuse_write_in *inarg = &req->misc.write.in;
857 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700858
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700859 inarg->fh = ff->fh;
860 inarg->offset = pos;
861 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700862 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200863 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700864 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200865 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700866 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
867 else
868 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700869 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700870 req->in.args[1].size = count;
871 req->out.numargs = 1;
872 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700873 req->out.args[0].value = outarg;
874}
875
876static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200877 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700878{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200879 struct fuse_file *ff = file->private_data;
880 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200881 struct fuse_write_in *inarg = &req->misc.write.in;
882
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200883 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200884 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700885 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700886 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
887 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
888 }
Tejun Heob93f8582008-11-26 12:03:55 +0100889 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700890 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700891}
892
Miklos Szeredia1d75f22010-07-12 14:41:40 +0200893void fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700894{
895 struct fuse_conn *fc = get_fuse_conn(inode);
896 struct fuse_inode *fi = get_fuse_inode(inode);
897
898 spin_lock(&fc->lock);
899 fi->attr_version = ++fc->attr_version;
900 if (pos > inode->i_size)
901 i_size_write(inode, pos);
902 spin_unlock(&fc->lock);
903}
904
Nick Pigginea9b9902008-04-30 00:54:42 -0700905static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
906 struct inode *inode, loff_t pos,
907 size_t count)
908{
909 size_t res;
910 unsigned offset;
911 unsigned i;
912
913 for (i = 0; i < req->num_pages; i++)
914 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
915
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200916 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700917
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400918 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700919 count = res;
920 for (i = 0; i < req->num_pages; i++) {
921 struct page *page = req->pages[i];
922
923 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
924 SetPageUptodate(page);
925
926 if (count > PAGE_CACHE_SIZE - offset)
927 count -= PAGE_CACHE_SIZE - offset;
928 else
929 count = 0;
930 offset = 0;
931
932 unlock_page(page);
933 page_cache_release(page);
934 }
935
936 return res;
937}
938
939static ssize_t fuse_fill_write_pages(struct fuse_req *req,
940 struct address_space *mapping,
941 struct iov_iter *ii, loff_t pos)
942{
943 struct fuse_conn *fc = get_fuse_conn(mapping->host);
944 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
945 size_t count = 0;
946 int err;
947
Miklos Szeredif4975c62009-04-02 14:25:34 +0200948 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400949 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700950
951 do {
952 size_t tmp;
953 struct page *page;
954 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
955 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
956 iov_iter_count(ii));
957
958 bytes = min_t(size_t, bytes, fc->max_write - count);
959
960 again:
961 err = -EFAULT;
962 if (iov_iter_fault_in_readable(ii, bytes))
963 break;
964
965 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800966 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700967 if (!page)
968 break;
969
anfei zhou931e80e2010-02-02 13:44:02 -0800970 if (mapping_writably_mapped(mapping))
971 flush_dcache_page(page);
972
Nick Pigginea9b9902008-04-30 00:54:42 -0700973 pagefault_disable();
974 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
975 pagefault_enable();
976 flush_dcache_page(page);
977
Johannes Weiner478e0842011-07-25 22:35:35 +0200978 mark_page_accessed(page);
979
Nick Pigginea9b9902008-04-30 00:54:42 -0700980 if (!tmp) {
981 unlock_page(page);
982 page_cache_release(page);
983 bytes = min(bytes, iov_iter_single_seg_count(ii));
984 goto again;
985 }
986
987 err = 0;
988 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400989 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -0700990 req->num_pages++;
991
992 iov_iter_advance(ii, tmp);
993 count += tmp;
994 pos += tmp;
995 offset += tmp;
996 if (offset == PAGE_CACHE_SIZE)
997 offset = 0;
998
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700999 if (!fc->big_writes)
1000 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001001 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001002 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001003
1004 return count > 0 ? count : err;
1005}
1006
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001007static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1008{
1009 return min_t(unsigned,
1010 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1011 (pos >> PAGE_CACHE_SHIFT) + 1,
1012 FUSE_MAX_PAGES_PER_REQ);
1013}
1014
Nick Pigginea9b9902008-04-30 00:54:42 -07001015static ssize_t fuse_perform_write(struct file *file,
1016 struct address_space *mapping,
1017 struct iov_iter *ii, loff_t pos)
1018{
1019 struct inode *inode = mapping->host;
1020 struct fuse_conn *fc = get_fuse_conn(inode);
1021 int err = 0;
1022 ssize_t res = 0;
1023
1024 if (is_bad_inode(inode))
1025 return -EIO;
1026
1027 do {
1028 struct fuse_req *req;
1029 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001030 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001031
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001032 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001033 if (IS_ERR(req)) {
1034 err = PTR_ERR(req);
1035 break;
1036 }
1037
1038 count = fuse_fill_write_pages(req, mapping, ii, pos);
1039 if (count <= 0) {
1040 err = count;
1041 } else {
1042 size_t num_written;
1043
1044 num_written = fuse_send_write_pages(req, file, inode,
1045 pos, count);
1046 err = req->out.h.error;
1047 if (!err) {
1048 res += num_written;
1049 pos += num_written;
1050
1051 /* break out of the loop on short write */
1052 if (num_written != count)
1053 err = -EIO;
1054 }
1055 }
1056 fuse_put_request(fc, req);
1057 } while (!err && iov_iter_count(ii));
1058
1059 if (res > 0)
1060 fuse_write_update_size(inode, pos);
1061
1062 fuse_invalidate_attr(inode);
1063
1064 return res > 0 ? res : err;
1065}
1066
1067static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1068 unsigned long nr_segs, loff_t pos)
1069{
1070 struct file *file = iocb->ki_filp;
1071 struct address_space *mapping = file->f_mapping;
1072 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001073 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001074 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001075 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001076 struct inode *inode = mapping->host;
1077 ssize_t err;
1078 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001079 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001080
1081 WARN_ON(iocb->ki_pos != pos);
1082
Anand Avati4273b792012-02-17 12:46:25 -05001083 ocount = 0;
1084 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001085 if (err)
1086 return err;
1087
Anand Avati4273b792012-02-17 12:46:25 -05001088 count = ocount;
Jan Kara58ef6a72012-06-12 16:20:42 +02001089 sb_start_write(inode->i_sb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001090 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001091
1092 /* We can write back this queue in page reclaim */
1093 current->backing_dev_info = mapping->backing_dev_info;
1094
1095 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1096 if (err)
1097 goto out;
1098
1099 if (count == 0)
1100 goto out;
1101
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001102 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001103 if (err)
1104 goto out;
1105
Josef Bacikc3b2da32012-03-26 09:59:21 -04001106 err = file_update_time(file);
1107 if (err)
1108 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001109
Anand Avati4273b792012-02-17 12:46:25 -05001110 if (file->f_flags & O_DIRECT) {
1111 written = generic_file_direct_write(iocb, iov, &nr_segs,
1112 pos, &iocb->ki_pos,
1113 count, ocount);
1114 if (written < 0 || written == count)
1115 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001116
Anand Avati4273b792012-02-17 12:46:25 -05001117 pos += written;
1118 count -= written;
1119
1120 iov_iter_init(&i, iov, nr_segs, count, written);
1121 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1122 if (written_buffered < 0) {
1123 err = written_buffered;
1124 goto out;
1125 }
1126 endbyte = pos + written_buffered - 1;
1127
1128 err = filemap_write_and_wait_range(file->f_mapping, pos,
1129 endbyte);
1130 if (err)
1131 goto out;
1132
1133 invalidate_mapping_pages(file->f_mapping,
1134 pos >> PAGE_CACHE_SHIFT,
1135 endbyte >> PAGE_CACHE_SHIFT);
1136
1137 written += written_buffered;
1138 iocb->ki_pos = pos + written_buffered;
1139 } else {
1140 iov_iter_init(&i, iov, nr_segs, count, 0);
1141 written = fuse_perform_write(file, mapping, &i, pos);
1142 if (written >= 0)
1143 iocb->ki_pos = pos + written;
1144 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001145out:
1146 current->backing_dev_info = NULL;
1147 mutex_unlock(&inode->i_mutex);
Jan Kara58ef6a72012-06-12 16:20:42 +02001148 sb_end_write(inode->i_sb);
Nick Pigginea9b9902008-04-30 00:54:42 -07001149
1150 return written ? written : err;
1151}
1152
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001153static inline void fuse_page_descs_length_init(struct fuse_req *req,
1154 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001155{
1156 int i;
1157
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001158 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001159 req->page_descs[i].length = PAGE_SIZE -
1160 req->page_descs[i].offset;
1161}
1162
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001163static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1164{
1165 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1166}
1167
1168static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1169 size_t max_size)
1170{
1171 return min(iov_iter_single_seg_count(ii), max_size);
1172}
1173
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001174static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001175 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001176{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001177 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001178
Miklos Szeredif4975c62009-04-02 14:25:34 +02001179 /* Special case for kernel I/O: can copy directly into the buffer */
1180 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001181 unsigned long user_addr = fuse_get_user_addr(ii);
1182 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1183
Miklos Szeredif4975c62009-04-02 14:25:34 +02001184 if (write)
1185 req->in.args[1].value = (void *) user_addr;
1186 else
1187 req->out.args[0].value = (void *) user_addr;
1188
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001189 iov_iter_advance(ii, frag_size);
1190 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001191 return 0;
1192 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001193
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001194 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001195 unsigned npages;
1196 unsigned long user_addr = fuse_get_user_addr(ii);
1197 unsigned offset = user_addr & ~PAGE_MASK;
1198 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1199 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001200
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001201 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001202 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1203
1204 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1205 npages = clamp(npages, 1U, n);
1206
1207 ret = get_user_pages_fast(user_addr, npages, !write,
1208 &req->pages[req->num_pages]);
1209 if (ret < 0)
1210 return ret;
1211
1212 npages = ret;
1213 frag_size = min_t(size_t, frag_size,
1214 (npages << PAGE_SHIFT) - offset);
1215 iov_iter_advance(ii, frag_size);
1216
1217 req->page_descs[req->num_pages].offset = offset;
1218 fuse_page_descs_length_init(req, req->num_pages, npages);
1219
1220 req->num_pages += npages;
1221 req->page_descs[req->num_pages - 1].length -=
1222 (npages << PAGE_SHIFT) - offset - frag_size;
1223
1224 nbytes += frag_size;
1225 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001226
1227 if (write)
1228 req->in.argpages = 1;
1229 else
1230 req->out.argpages = 1;
1231
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001232 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001233
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001234 return 0;
1235}
1236
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001237static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1238{
1239 struct iov_iter ii = *ii_p;
1240 int npages = 0;
1241
1242 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1243 unsigned long user_addr = fuse_get_user_addr(&ii);
1244 unsigned offset = user_addr & ~PAGE_MASK;
1245 size_t frag_size = iov_iter_single_seg_count(&ii);
1246
1247 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1248 iov_iter_advance(&ii, frag_size);
1249 }
1250
1251 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1252}
1253
Miklos Szeredifb05f412012-11-10 16:55:56 +01001254ssize_t fuse_direct_io(struct file *file, const struct iovec *iov,
1255 unsigned long nr_segs, size_t count, loff_t *ppos,
1256 int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001257{
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001258 struct fuse_file *ff = file->private_data;
1259 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001260 size_t nmax = write ? fc->max_write : fc->max_read;
1261 loff_t pos = *ppos;
1262 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001263 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001264 struct iov_iter ii;
1265
1266 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001267
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001268 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001269 if (IS_ERR(req))
1270 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001271
1272 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001273 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001274 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001275 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001276 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001277 if (err) {
1278 res = err;
1279 break;
1280 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001281
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001282 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001283 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001284 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001285 nres = fuse_send_read(req, file, pos, nbytes, owner);
1286
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001287 fuse_release_user_pages(req, !write);
1288 if (req->out.h.error) {
1289 if (!res)
1290 res = req->out.h.error;
1291 break;
1292 } else if (nres > nbytes) {
1293 res = -EIO;
1294 break;
1295 }
1296 count -= nres;
1297 res += nres;
1298 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001299 if (nres != nbytes)
1300 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001301 if (count) {
1302 fuse_put_request(fc, req);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001303 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001304 if (IS_ERR(req))
1305 break;
1306 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001307 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001308 if (!IS_ERR(req))
1309 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001310 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001311 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001312
1313 return res;
1314}
Tejun Heo08cbf542009-04-14 10:54:53 +09001315EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001316
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001317static ssize_t __fuse_direct_read(struct file *file, const struct iovec *iov,
1318 unsigned long nr_segs, loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001319{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001320 ssize_t res;
Al Viro6131ffa2013-02-27 16:59:05 -05001321 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001322
1323 if (is_bad_inode(inode))
1324 return -EIO;
1325
Miklos Szeredifb05f412012-11-10 16:55:56 +01001326 res = fuse_direct_io(file, iov, nr_segs, iov_length(iov, nr_segs),
1327 ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001328
1329 fuse_invalidate_attr(inode);
1330
1331 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001332}
1333
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001334static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1335 size_t count, loff_t *ppos)
1336{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001337 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001338 return __fuse_direct_read(file, &iov, 1, ppos);
1339}
1340
1341static ssize_t __fuse_direct_write(struct file *file, const struct iovec *iov,
1342 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001343{
Al Viro6131ffa2013-02-27 16:59:05 -05001344 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001345 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001346 ssize_t res;
1347
1348 res = generic_write_checks(file, ppos, &count, 0);
1349 if (!res) {
Miklos Szeredifb05f412012-11-10 16:55:56 +01001350 res = fuse_direct_io(file, iov, nr_segs, count, ppos, 1);
Anand Avati4273b792012-02-17 12:46:25 -05001351 if (res > 0)
1352 fuse_write_update_size(inode, *ppos);
1353 }
1354
1355 fuse_invalidate_attr(inode);
1356
1357 return res;
1358}
1359
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001360static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1361 size_t count, loff_t *ppos)
1362{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001363 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001364 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001365 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001366
1367 if (is_bad_inode(inode))
1368 return -EIO;
1369
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001370 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001371 mutex_lock(&inode->i_mutex);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001372 res = __fuse_direct_write(file, &iov, 1, ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001373 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001374
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001375 return res;
1376}
1377
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001378static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001379{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001380 __free_page(req->pages[0]);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +01001381 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001382}
1383
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001384static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001385{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001386 struct inode *inode = req->inode;
1387 struct fuse_inode *fi = get_fuse_inode(inode);
1388 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1389
1390 list_del(&req->writepages_entry);
1391 dec_bdi_stat(bdi, BDI_WRITEBACK);
1392 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1393 bdi_writeout_inc(bdi);
1394 wake_up(&fi->page_waitq);
1395}
1396
1397/* Called under fc->lock, may release and reacquire it */
1398static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001399__releases(fc->lock)
1400__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001401{
1402 struct fuse_inode *fi = get_fuse_inode(req->inode);
1403 loff_t size = i_size_read(req->inode);
1404 struct fuse_write_in *inarg = &req->misc.write.in;
1405
1406 if (!fc->connected)
1407 goto out_free;
1408
1409 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1410 inarg->size = PAGE_CACHE_SIZE;
1411 } else if (inarg->offset < size) {
1412 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1413 } else {
1414 /* Got truncated off completely */
1415 goto out_free;
1416 }
1417
1418 req->in.args[1].size = inarg->size;
1419 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001420 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001421 return;
1422
1423 out_free:
1424 fuse_writepage_finish(fc, req);
1425 spin_unlock(&fc->lock);
1426 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001427 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001428 spin_lock(&fc->lock);
1429}
1430
1431/*
1432 * If fi->writectr is positive (no truncate or fsync going on) send
1433 * all queued writepage requests.
1434 *
1435 * Called with fc->lock
1436 */
1437void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001438__releases(fc->lock)
1439__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001440{
1441 struct fuse_conn *fc = get_fuse_conn(inode);
1442 struct fuse_inode *fi = get_fuse_inode(inode);
1443 struct fuse_req *req;
1444
1445 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1446 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1447 list_del_init(&req->list);
1448 fuse_send_writepage(fc, req);
1449 }
1450}
1451
1452static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1453{
1454 struct inode *inode = req->inode;
1455 struct fuse_inode *fi = get_fuse_inode(inode);
1456
1457 mapping_set_error(inode->i_mapping, req->out.h.error);
1458 spin_lock(&fc->lock);
1459 fi->writectr--;
1460 fuse_writepage_finish(fc, req);
1461 spin_unlock(&fc->lock);
1462 fuse_writepage_free(fc, req);
1463}
1464
1465static int fuse_writepage_locked(struct page *page)
1466{
1467 struct address_space *mapping = page->mapping;
1468 struct inode *inode = mapping->host;
1469 struct fuse_conn *fc = get_fuse_conn(inode);
1470 struct fuse_inode *fi = get_fuse_inode(inode);
1471 struct fuse_req *req;
1472 struct fuse_file *ff;
1473 struct page *tmp_page;
1474
1475 set_page_writeback(page);
1476
Maxim Patlasov4250c062012-10-26 19:48:07 +04001477 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001478 if (!req)
1479 goto err;
1480
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001481 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001482 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1483 if (!tmp_page)
1484 goto err_free;
1485
1486 spin_lock(&fc->lock);
1487 BUG_ON(list_empty(&fi->write_files));
1488 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1489 req->ff = fuse_file_get(ff);
1490 spin_unlock(&fc->lock);
1491
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001492 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001493
1494 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001495 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001496 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001497 req->num_pages = 1;
1498 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001499 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001500 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001501 req->end = fuse_writepage_end;
1502 req->inode = inode;
1503
1504 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1505 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1506 end_page_writeback(page);
1507
1508 spin_lock(&fc->lock);
1509 list_add(&req->writepages_entry, &fi->writepages);
1510 list_add_tail(&req->list, &fi->queued_writes);
1511 fuse_flush_writepages(inode);
1512 spin_unlock(&fc->lock);
1513
1514 return 0;
1515
1516err_free:
1517 fuse_request_free(req);
1518err:
1519 end_page_writeback(page);
1520 return -ENOMEM;
1521}
1522
1523static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1524{
1525 int err;
1526
1527 err = fuse_writepage_locked(page);
1528 unlock_page(page);
1529
1530 return err;
1531}
1532
1533static int fuse_launder_page(struct page *page)
1534{
1535 int err = 0;
1536 if (clear_page_dirty_for_io(page)) {
1537 struct inode *inode = page->mapping->host;
1538 err = fuse_writepage_locked(page);
1539 if (!err)
1540 fuse_wait_on_page_writeback(inode, page->index);
1541 }
1542 return err;
1543}
1544
1545/*
1546 * Write back dirty pages now, because there may not be any suitable
1547 * open files later
1548 */
1549static void fuse_vma_close(struct vm_area_struct *vma)
1550{
1551 filemap_write_and_wait(vma->vm_file->f_mapping);
1552}
1553
1554/*
1555 * Wait for writeback against this page to complete before allowing it
1556 * to be marked dirty again, and hence written back again, possibly
1557 * before the previous writepage completed.
1558 *
1559 * Block here, instead of in ->writepage(), so that the userspace fs
1560 * can only block processes actually operating on the filesystem.
1561 *
1562 * Otherwise unprivileged userspace fs would be able to block
1563 * unrelated:
1564 *
1565 * - page migration
1566 * - sync(2)
1567 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1568 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001569static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001570{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001571 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001572 /*
1573 * Don't use page->mapping as it may become NULL from a
1574 * concurrent truncate.
1575 */
1576 struct inode *inode = vma->vm_file->f_mapping->host;
1577
1578 fuse_wait_on_page_writeback(inode, page->index);
1579 return 0;
1580}
1581
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001582static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001583 .close = fuse_vma_close,
1584 .fault = filemap_fault,
1585 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001586 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001587};
1588
1589static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1590{
1591 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
Al Viro6131ffa2013-02-27 16:59:05 -05001592 struct inode *inode = file_inode(file);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001593 struct fuse_conn *fc = get_fuse_conn(inode);
1594 struct fuse_inode *fi = get_fuse_inode(inode);
1595 struct fuse_file *ff = file->private_data;
1596 /*
1597 * file may be written through mmap, so chain it onto the
1598 * inodes's write_file list
1599 */
1600 spin_lock(&fc->lock);
1601 if (list_empty(&ff->write_entry))
1602 list_add(&ff->write_entry, &fi->write_files);
1603 spin_unlock(&fc->lock);
1604 }
1605 file_accessed(file);
1606 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001607 return 0;
1608}
1609
Miklos Szeredifc280c92009-04-02 14:25:35 +02001610static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1611{
1612 /* Can't provide the coherency needed for MAP_SHARED */
1613 if (vma->vm_flags & VM_MAYSHARE)
1614 return -ENODEV;
1615
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001616 invalidate_inode_pages2(file->f_mapping);
1617
Miklos Szeredifc280c92009-04-02 14:25:35 +02001618 return generic_file_mmap(file, vma);
1619}
1620
Miklos Szeredi71421252006-06-25 05:48:52 -07001621static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1622 struct file_lock *fl)
1623{
1624 switch (ffl->type) {
1625 case F_UNLCK:
1626 break;
1627
1628 case F_RDLCK:
1629 case F_WRLCK:
1630 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1631 ffl->end < ffl->start)
1632 return -EIO;
1633
1634 fl->fl_start = ffl->start;
1635 fl->fl_end = ffl->end;
1636 fl->fl_pid = ffl->pid;
1637 break;
1638
1639 default:
1640 return -EIO;
1641 }
1642 fl->fl_type = ffl->type;
1643 return 0;
1644}
1645
1646static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001647 const struct file_lock *fl, int opcode, pid_t pid,
1648 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001649{
Al Viro6131ffa2013-02-27 16:59:05 -05001650 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001651 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001652 struct fuse_file *ff = file->private_data;
1653 struct fuse_lk_in *arg = &req->misc.lk_in;
1654
1655 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001656 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001657 arg->lk.start = fl->fl_start;
1658 arg->lk.end = fl->fl_end;
1659 arg->lk.type = fl->fl_type;
1660 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001661 if (flock)
1662 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001663 req->in.h.opcode = opcode;
1664 req->in.h.nodeid = get_node_id(inode);
1665 req->in.numargs = 1;
1666 req->in.args[0].size = sizeof(*arg);
1667 req->in.args[0].value = arg;
1668}
1669
1670static int fuse_getlk(struct file *file, struct file_lock *fl)
1671{
Al Viro6131ffa2013-02-27 16:59:05 -05001672 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001673 struct fuse_conn *fc = get_fuse_conn(inode);
1674 struct fuse_req *req;
1675 struct fuse_lk_out outarg;
1676 int err;
1677
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001678 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07001679 if (IS_ERR(req))
1680 return PTR_ERR(req);
1681
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001682 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001683 req->out.numargs = 1;
1684 req->out.args[0].size = sizeof(outarg);
1685 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001686 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001687 err = req->out.h.error;
1688 fuse_put_request(fc, req);
1689 if (!err)
1690 err = convert_fuse_file_lock(&outarg.lk, fl);
1691
1692 return err;
1693}
1694
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001695static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001696{
Al Viro6131ffa2013-02-27 16:59:05 -05001697 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001698 struct fuse_conn *fc = get_fuse_conn(inode);
1699 struct fuse_req *req;
1700 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1701 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1702 int err;
1703
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001704 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07001705 /* NLM needs asynchronous locks, which we don't support yet */
1706 return -ENOLCK;
1707 }
1708
Miklos Szeredi71421252006-06-25 05:48:52 -07001709 /* Unlock on close is handled by the flush method */
1710 if (fl->fl_flags & FL_CLOSE)
1711 return 0;
1712
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001713 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07001714 if (IS_ERR(req))
1715 return PTR_ERR(req);
1716
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001717 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001718 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001719 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001720 /* locking is restartable */
1721 if (err == -EINTR)
1722 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001723 fuse_put_request(fc, req);
1724 return err;
1725}
1726
1727static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1728{
Al Viro6131ffa2013-02-27 16:59:05 -05001729 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001730 struct fuse_conn *fc = get_fuse_conn(inode);
1731 int err;
1732
Miklos Szeredi48e90762008-07-25 01:49:02 -07001733 if (cmd == F_CANCELLK) {
1734 err = 0;
1735 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001736 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001737 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001738 err = 0;
1739 } else
1740 err = fuse_getlk(file, fl);
1741 } else {
1742 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001743 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001744 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001745 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001746 }
1747 return err;
1748}
1749
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001750static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1751{
Al Viro6131ffa2013-02-27 16:59:05 -05001752 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001753 struct fuse_conn *fc = get_fuse_conn(inode);
1754 int err;
1755
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001756 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001757 err = flock_lock_file_wait(file, fl);
1758 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001759 struct fuse_file *ff = file->private_data;
1760
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001761 /* emulate flock with POSIX locks */
1762 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001763 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001764 err = fuse_setlk(file, fl, 1);
1765 }
1766
1767 return err;
1768}
1769
Miklos Szeredib2d22722006-12-06 20:35:51 -08001770static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1771{
1772 struct inode *inode = mapping->host;
1773 struct fuse_conn *fc = get_fuse_conn(inode);
1774 struct fuse_req *req;
1775 struct fuse_bmap_in inarg;
1776 struct fuse_bmap_out outarg;
1777 int err;
1778
1779 if (!inode->i_sb->s_bdev || fc->no_bmap)
1780 return 0;
1781
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001782 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001783 if (IS_ERR(req))
1784 return 0;
1785
1786 memset(&inarg, 0, sizeof(inarg));
1787 inarg.block = block;
1788 inarg.blocksize = inode->i_sb->s_blocksize;
1789 req->in.h.opcode = FUSE_BMAP;
1790 req->in.h.nodeid = get_node_id(inode);
1791 req->in.numargs = 1;
1792 req->in.args[0].size = sizeof(inarg);
1793 req->in.args[0].value = &inarg;
1794 req->out.numargs = 1;
1795 req->out.args[0].size = sizeof(outarg);
1796 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001797 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001798 err = req->out.h.error;
1799 fuse_put_request(fc, req);
1800 if (err == -ENOSYS)
1801 fc->no_bmap = 1;
1802
1803 return err ? 0 : outarg.block;
1804}
1805
Andrew Morton965c8e52012-12-17 15:59:39 -08001806static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001807{
1808 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05001809 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001810
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001811 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08001812 if (whence == SEEK_CUR || whence == SEEK_SET)
1813 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04001814
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001815 mutex_lock(&inode->i_mutex);
1816 retval = fuse_update_attributes(inode, NULL, file, NULL);
1817 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08001818 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001819 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001820
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001821 return retval;
1822}
1823
Tejun Heo59efec72008-11-26 12:03:55 +01001824static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1825 unsigned int nr_segs, size_t bytes, bool to_user)
1826{
1827 struct iov_iter ii;
1828 int page_idx = 0;
1829
1830 if (!bytes)
1831 return 0;
1832
1833 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1834
1835 while (iov_iter_count(&ii)) {
1836 struct page *page = pages[page_idx++];
1837 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001838 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001839
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001840 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001841
1842 while (todo) {
1843 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1844 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1845 size_t copy = min(todo, iov_len);
1846 size_t left;
1847
1848 if (!to_user)
1849 left = copy_from_user(kaddr, uaddr, copy);
1850 else
1851 left = copy_to_user(uaddr, kaddr, copy);
1852
1853 if (unlikely(left))
1854 return -EFAULT;
1855
1856 iov_iter_advance(&ii, copy);
1857 todo -= copy;
1858 kaddr += copy;
1859 }
1860
Jens Axboe0bd87182009-11-03 11:40:44 +01001861 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001862 }
1863
1864 return 0;
1865}
1866
1867/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001868 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1869 * ABI was defined to be 'struct iovec' which is different on 32bit
1870 * and 64bit. Fortunately we can determine which structure the server
1871 * used from the size of the reply.
1872 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001873static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1874 size_t transferred, unsigned count,
1875 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001876{
1877#ifdef CONFIG_COMPAT
1878 if (count * sizeof(struct compat_iovec) == transferred) {
1879 struct compat_iovec *ciov = src;
1880 unsigned i;
1881
1882 /*
1883 * With this interface a 32bit server cannot support
1884 * non-compat (i.e. ones coming from 64bit apps) ioctl
1885 * requests
1886 */
1887 if (!is_compat)
1888 return -EINVAL;
1889
1890 for (i = 0; i < count; i++) {
1891 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1892 dst[i].iov_len = ciov[i].iov_len;
1893 }
1894 return 0;
1895 }
1896#endif
1897
1898 if (count * sizeof(struct iovec) != transferred)
1899 return -EIO;
1900
1901 memcpy(dst, src, transferred);
1902 return 0;
1903}
1904
Miklos Szeredi75727772010-11-30 16:39:27 +01001905/* Make sure iov_length() won't overflow */
1906static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1907{
1908 size_t n;
1909 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1910
Zach Brownfb6ccff2012-07-24 12:10:11 -07001911 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01001912 if (iov->iov_len > (size_t) max)
1913 return -ENOMEM;
1914 max -= iov->iov_len;
1915 }
1916 return 0;
1917}
1918
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001919static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1920 void *src, size_t transferred, unsigned count,
1921 bool is_compat)
1922{
1923 unsigned i;
1924 struct fuse_ioctl_iovec *fiov = src;
1925
1926 if (fc->minor < 16) {
1927 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1928 count, is_compat);
1929 }
1930
1931 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1932 return -EIO;
1933
1934 for (i = 0; i < count; i++) {
1935 /* Did the server supply an inappropriate value? */
1936 if (fiov[i].base != (unsigned long) fiov[i].base ||
1937 fiov[i].len != (unsigned long) fiov[i].len)
1938 return -EIO;
1939
1940 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1941 dst[i].iov_len = (size_t) fiov[i].len;
1942
1943#ifdef CONFIG_COMPAT
1944 if (is_compat &&
1945 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1946 (compat_size_t) dst[i].iov_len != fiov[i].len))
1947 return -EIO;
1948#endif
1949 }
1950
1951 return 0;
1952}
1953
1954
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001955/*
Tejun Heo59efec72008-11-26 12:03:55 +01001956 * For ioctls, there is no generic way to determine how much memory
1957 * needs to be read and/or written. Furthermore, ioctls are allowed
1958 * to dereference the passed pointer, so the parameter requires deep
1959 * copying but FUSE has no idea whatsoever about what to copy in or
1960 * out.
1961 *
1962 * This is solved by allowing FUSE server to retry ioctl with
1963 * necessary in/out iovecs. Let's assume the ioctl implementation
1964 * needs to read in the following structure.
1965 *
1966 * struct a {
1967 * char *buf;
1968 * size_t buflen;
1969 * }
1970 *
1971 * On the first callout to FUSE server, inarg->in_size and
1972 * inarg->out_size will be NULL; then, the server completes the ioctl
1973 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1974 * the actual iov array to
1975 *
1976 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1977 *
1978 * which tells FUSE to copy in the requested area and retry the ioctl.
1979 * On the second round, the server has access to the structure and
1980 * from that it can tell what to look for next, so on the invocation,
1981 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1982 *
1983 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1984 * { .iov_base = a.buf, .iov_len = a.buflen } }
1985 *
1986 * FUSE will copy both struct a and the pointed buffer from the
1987 * process doing the ioctl and retry ioctl with both struct a and the
1988 * buffer.
1989 *
1990 * This time, FUSE server has everything it needs and completes ioctl
1991 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1992 *
1993 * Copying data out works the same way.
1994 *
1995 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1996 * automatically initializes in and out iovs by decoding @cmd with
1997 * _IOC_* macros and the server is not allowed to request RETRY. This
1998 * limits ioctl data transfers to well-formed ioctls and is the forced
1999 * behavior for all FUSE servers.
2000 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002001long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2002 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002003{
Tejun Heo59efec72008-11-26 12:03:55 +01002004 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002005 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002006 struct fuse_ioctl_in inarg = {
2007 .fh = ff->fh,
2008 .cmd = cmd,
2009 .arg = arg,
2010 .flags = flags
2011 };
2012 struct fuse_ioctl_out outarg;
2013 struct fuse_req *req = NULL;
2014 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002015 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002016 struct iovec *in_iov = NULL, *out_iov = NULL;
2017 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2018 size_t in_size, out_size, transferred;
2019 int err;
2020
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002021#if BITS_PER_LONG == 32
2022 inarg.flags |= FUSE_IOCTL_32BIT;
2023#else
2024 if (flags & FUSE_IOCTL_COMPAT)
2025 inarg.flags |= FUSE_IOCTL_32BIT;
2026#endif
2027
Tejun Heo59efec72008-11-26 12:03:55 +01002028 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002029 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002030
Tejun Heo59efec72008-11-26 12:03:55 +01002031 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002032 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002033 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002034 if (!pages || !iov_page)
2035 goto out;
2036
2037 /*
2038 * If restricted, initialize IO parameters as encoded in @cmd.
2039 * RETRY from server is not allowed.
2040 */
2041 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002042 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002043
Miklos Szeredic9f05232008-12-02 14:49:42 +01002044 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002045 iov->iov_len = _IOC_SIZE(cmd);
2046
2047 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2048 in_iov = iov;
2049 in_iovs = 1;
2050 }
2051
2052 if (_IOC_DIR(cmd) & _IOC_READ) {
2053 out_iov = iov;
2054 out_iovs = 1;
2055 }
2056 }
2057
2058 retry:
2059 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2060 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2061
2062 /*
2063 * Out data can be used either for actual out data or iovs,
2064 * make sure there always is at least one page.
2065 */
2066 out_size = max_t(size_t, out_size, PAGE_SIZE);
2067 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2068
2069 /* make sure there are enough buffer pages and init request with them */
2070 err = -ENOMEM;
2071 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2072 goto out;
2073 while (num_pages < max_pages) {
2074 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2075 if (!pages[num_pages])
2076 goto out;
2077 num_pages++;
2078 }
2079
Maxim Patlasov54b96672012-10-26 19:49:13 +04002080 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002081 if (IS_ERR(req)) {
2082 err = PTR_ERR(req);
2083 req = NULL;
2084 goto out;
2085 }
2086 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2087 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002088 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002089
2090 /* okay, let's send it to the client */
2091 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002092 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002093 req->in.numargs = 1;
2094 req->in.args[0].size = sizeof(inarg);
2095 req->in.args[0].value = &inarg;
2096 if (in_size) {
2097 req->in.numargs++;
2098 req->in.args[1].size = in_size;
2099 req->in.argpages = 1;
2100
2101 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2102 false);
2103 if (err)
2104 goto out;
2105 }
2106
2107 req->out.numargs = 2;
2108 req->out.args[0].size = sizeof(outarg);
2109 req->out.args[0].value = &outarg;
2110 req->out.args[1].size = out_size;
2111 req->out.argpages = 1;
2112 req->out.argvar = 1;
2113
Tejun Heob93f8582008-11-26 12:03:55 +01002114 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002115 err = req->out.h.error;
2116 transferred = req->out.args[1].size;
2117 fuse_put_request(fc, req);
2118 req = NULL;
2119 if (err)
2120 goto out;
2121
2122 /* did it ask for retry? */
2123 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002124 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002125
2126 /* no retry if in restricted mode */
2127 err = -EIO;
2128 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2129 goto out;
2130
2131 in_iovs = outarg.in_iovs;
2132 out_iovs = outarg.out_iovs;
2133
2134 /*
2135 * Make sure things are in boundary, separate checks
2136 * are to protect against overflow.
2137 */
2138 err = -ENOMEM;
2139 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2140 out_iovs > FUSE_IOCTL_MAX_IOV ||
2141 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2142 goto out;
2143
Cong Wang2408f6e2011-11-25 23:14:30 +08002144 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002145 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002146 transferred, in_iovs + out_iovs,
2147 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002148 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002149 if (err)
2150 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002151
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002152 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002153 out_iov = in_iov + in_iovs;
2154
Miklos Szeredi75727772010-11-30 16:39:27 +01002155 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2156 if (err)
2157 goto out;
2158
2159 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2160 if (err)
2161 goto out;
2162
Tejun Heo59efec72008-11-26 12:03:55 +01002163 goto retry;
2164 }
2165
2166 err = -EIO;
2167 if (transferred > inarg.out_size)
2168 goto out;
2169
2170 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2171 out:
2172 if (req)
2173 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002174 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002175 while (num_pages)
2176 __free_page(pages[--num_pages]);
2177 kfree(pages);
2178
2179 return err ? err : outarg.result;
2180}
Tejun Heo08cbf542009-04-14 10:54:53 +09002181EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002182
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002183long fuse_ioctl_common(struct file *file, unsigned int cmd,
2184 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002185{
Al Viro6131ffa2013-02-27 16:59:05 -05002186 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002187 struct fuse_conn *fc = get_fuse_conn(inode);
2188
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002189 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002190 return -EACCES;
2191
2192 if (is_bad_inode(inode))
2193 return -EIO;
2194
2195 return fuse_do_ioctl(file, cmd, arg, flags);
2196}
2197
Tejun Heo59efec72008-11-26 12:03:55 +01002198static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2199 unsigned long arg)
2200{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002201 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002202}
2203
2204static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2205 unsigned long arg)
2206{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002207 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002208}
2209
Tejun Heo95668a62008-11-26 12:03:55 +01002210/*
2211 * All files which have been polled are linked to RB tree
2212 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2213 * find the matching one.
2214 */
2215static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2216 struct rb_node **parent_out)
2217{
2218 struct rb_node **link = &fc->polled_files.rb_node;
2219 struct rb_node *last = NULL;
2220
2221 while (*link) {
2222 struct fuse_file *ff;
2223
2224 last = *link;
2225 ff = rb_entry(last, struct fuse_file, polled_node);
2226
2227 if (kh < ff->kh)
2228 link = &last->rb_left;
2229 else if (kh > ff->kh)
2230 link = &last->rb_right;
2231 else
2232 return link;
2233 }
2234
2235 if (parent_out)
2236 *parent_out = last;
2237 return link;
2238}
2239
2240/*
2241 * The file is about to be polled. Make sure it's on the polled_files
2242 * RB tree. Note that files once added to the polled_files tree are
2243 * not removed before the file is released. This is because a file
2244 * polled once is likely to be polled again.
2245 */
2246static void fuse_register_polled_file(struct fuse_conn *fc,
2247 struct fuse_file *ff)
2248{
2249 spin_lock(&fc->lock);
2250 if (RB_EMPTY_NODE(&ff->polled_node)) {
2251 struct rb_node **link, *parent;
2252
2253 link = fuse_find_polled_node(fc, ff->kh, &parent);
2254 BUG_ON(*link);
2255 rb_link_node(&ff->polled_node, parent, link);
2256 rb_insert_color(&ff->polled_node, &fc->polled_files);
2257 }
2258 spin_unlock(&fc->lock);
2259}
2260
Tejun Heo08cbf542009-04-14 10:54:53 +09002261unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002262{
Tejun Heo95668a62008-11-26 12:03:55 +01002263 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002264 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002265 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2266 struct fuse_poll_out outarg;
2267 struct fuse_req *req;
2268 int err;
2269
2270 if (fc->no_poll)
2271 return DEFAULT_POLLMASK;
2272
2273 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002274 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002275
2276 /*
2277 * Ask for notification iff there's someone waiting for it.
2278 * The client may ignore the flag and always notify.
2279 */
2280 if (waitqueue_active(&ff->poll_wait)) {
2281 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2282 fuse_register_polled_file(fc, ff);
2283 }
2284
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002285 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002286 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002287 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002288
2289 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002290 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002291 req->in.numargs = 1;
2292 req->in.args[0].size = sizeof(inarg);
2293 req->in.args[0].value = &inarg;
2294 req->out.numargs = 1;
2295 req->out.args[0].size = sizeof(outarg);
2296 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002297 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002298 err = req->out.h.error;
2299 fuse_put_request(fc, req);
2300
2301 if (!err)
2302 return outarg.revents;
2303 if (err == -ENOSYS) {
2304 fc->no_poll = 1;
2305 return DEFAULT_POLLMASK;
2306 }
2307 return POLLERR;
2308}
Tejun Heo08cbf542009-04-14 10:54:53 +09002309EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002310
2311/*
2312 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2313 * wakes up the poll waiters.
2314 */
2315int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2316 struct fuse_notify_poll_wakeup_out *outarg)
2317{
2318 u64 kh = outarg->kh;
2319 struct rb_node **link;
2320
2321 spin_lock(&fc->lock);
2322
2323 link = fuse_find_polled_node(fc, kh, NULL);
2324 if (*link) {
2325 struct fuse_file *ff;
2326
2327 ff = rb_entry(*link, struct fuse_file, polled_node);
2328 wake_up_interruptible_sync(&ff->poll_wait);
2329 }
2330
2331 spin_unlock(&fc->lock);
2332 return 0;
2333}
2334
Anand Avati4273b792012-02-17 12:46:25 -05002335static ssize_t
2336fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2337 loff_t offset, unsigned long nr_segs)
2338{
2339 ssize_t ret = 0;
2340 struct file *file = NULL;
2341 loff_t pos = 0;
2342
2343 file = iocb->ki_filp;
2344 pos = offset;
2345
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002346 if (rw == WRITE)
2347 ret = __fuse_direct_write(file, iov, nr_segs, &pos);
2348 else
2349 ret = __fuse_direct_read(file, iov, nr_segs, &pos);
Anand Avati4273b792012-02-17 12:46:25 -05002350
2351 return ret;
2352}
2353
Miklos Szeredicdadb112012-11-10 16:55:56 +01002354static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2355 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002356{
2357 struct fuse_file *ff = file->private_data;
2358 struct fuse_conn *fc = ff->fc;
2359 struct fuse_req *req;
2360 struct fuse_fallocate_in inarg = {
2361 .fh = ff->fh,
2362 .offset = offset,
2363 .length = length,
2364 .mode = mode
2365 };
2366 int err;
2367
Miklos Szeredi519c6042012-04-26 10:56:36 +02002368 if (fc->no_fallocate)
2369 return -EOPNOTSUPP;
2370
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002371 req = fuse_get_req_nopages(fc);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002372 if (IS_ERR(req))
2373 return PTR_ERR(req);
2374
2375 req->in.h.opcode = FUSE_FALLOCATE;
2376 req->in.h.nodeid = ff->nodeid;
2377 req->in.numargs = 1;
2378 req->in.args[0].size = sizeof(inarg);
2379 req->in.args[0].value = &inarg;
2380 fuse_request_send(fc, req);
2381 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002382 if (err == -ENOSYS) {
2383 fc->no_fallocate = 1;
2384 err = -EOPNOTSUPP;
2385 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002386 fuse_put_request(fc, req);
2387
2388 return err;
2389}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002390
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002391static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002392 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002393 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002394 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002395 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002396 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002397 .mmap = fuse_file_mmap,
2398 .open = fuse_open,
2399 .flush = fuse_flush,
2400 .release = fuse_release,
2401 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002402 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002403 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002404 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002405 .unlocked_ioctl = fuse_file_ioctl,
2406 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002407 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002408 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002409};
2410
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002411static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002412 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002413 .read = fuse_direct_read,
2414 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002415 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002416 .open = fuse_open,
2417 .flush = fuse_flush,
2418 .release = fuse_release,
2419 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002420 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002421 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002422 .unlocked_ioctl = fuse_file_ioctl,
2423 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002424 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002425 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002426 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002427};
2428
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002429static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002430 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002431 .writepage = fuse_writepage,
2432 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002433 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002434 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002435 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05002436 .direct_IO = fuse_direct_IO,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002437};
2438
2439void fuse_init_file_inode(struct inode *inode)
2440{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002441 inode->i_fop = &fuse_file_operations;
2442 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002443}