blob: 82f7ee581245d349ca0460028844fb6c1345f5f5 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090015#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010016#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020017#include <linux/swap.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070018#include <linux/aio.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070019
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080020static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070021
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020022static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
23 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070024{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080026 struct fuse_req *req;
27 int err;
28
Maxim Patlasovb111c8c2012-10-26 19:48:30 +040029 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -070030 if (IS_ERR(req))
31 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080032
33 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070034 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
35 if (!fc->atomic_o_trunc)
36 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020037 req->in.h.opcode = opcode;
38 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080039 req->in.numargs = 1;
40 req->in.args[0].size = sizeof(inarg);
41 req->in.args[0].value = &inarg;
42 req->out.numargs = 1;
43 req->out.args[0].size = sizeof(*outargp);
44 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010045 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080046 err = req->out.h.error;
47 fuse_put_request(fc, req);
48
49 return err;
50}
51
Tejun Heoacf99432008-11-26 12:03:55 +010052struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080053{
54 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090055
Miklos Szeredifd72faa2005-11-07 00:59:51 -080056 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090057 if (unlikely(!ff))
58 return NULL;
59
Miklos Szeredida5e4712009-04-28 16:56:36 +020060 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040061 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090062 if (unlikely(!ff->reserved_req)) {
63 kfree(ff);
64 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080065 }
Tejun Heo6b2db282009-04-14 10:54:49 +090066
67 INIT_LIST_HEAD(&ff->write_entry);
68 atomic_set(&ff->count, 0);
69 RB_CLEAR_NODE(&ff->polled_node);
70 init_waitqueue_head(&ff->poll_wait);
71
72 spin_lock(&fc->lock);
73 ff->kh = ++fc->khctr;
74 spin_unlock(&fc->lock);
75
Miklos Szeredifd72faa2005-11-07 00:59:51 -080076 return ff;
77}
78
79void fuse_file_free(struct fuse_file *ff)
80{
Miklos Szeredi33649c92006-06-25 05:48:52 -070081 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080082 kfree(ff);
83}
84
Miklos Szeredic7b71432009-04-28 16:56:37 +020085struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070086{
87 atomic_inc(&ff->count);
88 return ff;
89}
90
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010091static void fuse_release_async(struct work_struct *work)
Miklos Szeredi819c4b32007-10-16 23:31:04 -070092{
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010093 struct fuse_req *req;
94 struct fuse_conn *fc;
95 struct path path;
96
97 req = container_of(work, struct fuse_req, misc.release.work);
98 path = req->misc.release.path;
99 fc = get_fuse_conn(path.dentry->d_inode);
100
101 fuse_put_request(fc, req);
102 path_put(&path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -0700103}
104
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100105static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
106{
107 if (fc->destroy_req) {
108 /*
109 * If this is a fuseblk mount, then it's possible that
110 * releasing the path will result in releasing the
111 * super block and sending the DESTROY request. If
112 * the server is single threaded, this would hang.
113 * For this reason do the path_put() in a separate
114 * thread.
115 */
116 atomic_inc(&req->count);
117 INIT_WORK(&req->misc.release.work, fuse_release_async);
118 schedule_work(&req->misc.release.work);
119 } else {
120 path_put(&req->misc.release.path);
121 }
122}
123
124static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700125{
126 if (atomic_dec_and_test(&ff->count)) {
127 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200128
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100129 if (sync) {
130 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;
135 fuse_request_send_background(ff->fc, req);
136 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700137 kfree(ff);
138 }
139}
140
Tejun Heo08cbf542009-04-14 10:54:53 +0900141int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
142 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200143{
144 struct fuse_open_out outarg;
145 struct fuse_file *ff;
146 int err;
147 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
148
149 ff = fuse_file_alloc(fc);
150 if (!ff)
151 return -ENOMEM;
152
153 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
154 if (err) {
155 fuse_file_free(ff);
156 return err;
157 }
158
159 if (isdir)
160 outarg.open_flags &= ~FOPEN_DIRECT_IO;
161
162 ff->fh = outarg.fh;
163 ff->nodeid = nodeid;
164 ff->open_flags = outarg.open_flags;
165 file->private_data = fuse_file_get(ff);
166
167 return 0;
168}
Tejun Heo08cbf542009-04-14 10:54:53 +0900169EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200170
Miklos Szeredic7b71432009-04-28 16:56:37 +0200171void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800172{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200173 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800174 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200175
176 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800177 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200178 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700179 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200180 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200181 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800182 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
183 struct fuse_inode *fi = get_fuse_inode(inode);
184
185 spin_lock(&fc->lock);
186 fi->attr_version = ++fc->attr_version;
187 i_size_write(inode, 0);
188 spin_unlock(&fc->lock);
189 fuse_invalidate_attr(inode);
190 }
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800191}
192
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200193int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800194{
Tejun Heoacf99432008-11-26 12:03:55 +0100195 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700196 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700197
198 err = generic_file_open(inode, file);
199 if (err)
200 return err;
201
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200202 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800203 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200204 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700205
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200206 fuse_finish_open(inode, file);
207
208 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700209}
210
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200211static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800212{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200213 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700214 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800215 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700216
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200217 spin_lock(&fc->lock);
218 list_del(&ff->write_entry);
219 if (!RB_EMPTY_NODE(&ff->polled_node))
220 rb_erase(&ff->polled_node, &fc->polled_files);
221 spin_unlock(&fc->lock);
222
Bryan Green357ccf22011-03-01 16:43:52 -0800223 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200224
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700225 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800226 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700227 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200228 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700229 req->in.numargs = 1;
230 req->in.args[0].size = sizeof(struct fuse_release_in);
231 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800232}
233
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200234void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800235{
Tejun Heo6b2db282009-04-14 10:54:49 +0900236 struct fuse_file *ff;
237 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700238
Tejun Heo6b2db282009-04-14 10:54:49 +0900239 ff = file->private_data;
240 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200241 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700242
Tejun Heo6b2db282009-04-14 10:54:49 +0900243 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200244 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100245
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200246 if (ff->flock) {
247 struct fuse_release_in *inarg = &req->misc.release.in;
248 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
249 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
250 (fl_owner_t) file);
251 }
Tejun Heo6b2db282009-04-14 10:54:49 +0900252 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200253 path_get(&file->f_path);
254 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700255
Tejun Heo6b2db282009-04-14 10:54:49 +0900256 /*
257 * Normally this will send the RELEASE request, however if
258 * some asynchronous READ or WRITE requests are outstanding,
259 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100260 *
261 * Make the release synchronous if this is a fuseblk mount,
262 * synchronous RELEASE is allowed (and desirable) in this case
263 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900264 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100265 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700266}
267
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700268static int fuse_open(struct inode *inode, struct file *file)
269{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200270 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700271}
272
273static int fuse_release(struct inode *inode, struct file *file)
274{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200275 fuse_release_common(file, FUSE_RELEASE);
276
277 /* return value is ignored by VFS */
278 return 0;
279}
280
281void fuse_sync_release(struct fuse_file *ff, int flags)
282{
283 WARN_ON(atomic_read(&ff->count) > 1);
284 fuse_prepare_release(ff, flags, FUSE_RELEASE);
285 ff->reserved_req->force = 1;
286 fuse_request_send(ff->fc, ff->reserved_req);
287 fuse_put_request(ff->fc, ff->reserved_req);
288 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700289}
Tejun Heo08cbf542009-04-14 10:54:53 +0900290EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700291
Miklos Szeredi71421252006-06-25 05:48:52 -0700292/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700293 * Scramble the ID space with XTEA, so that the value of the files_struct
294 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700295 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700296u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700297{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700298 u32 *k = fc->scramble_key;
299 u64 v = (unsigned long) id;
300 u32 v0 = v;
301 u32 v1 = v >> 32;
302 u32 sum = 0;
303 int i;
304
305 for (i = 0; i < 32; i++) {
306 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
307 sum += 0x9E3779B9;
308 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
309 }
310
311 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700312}
313
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700314/*
315 * Check if page is under writeback
316 *
317 * This is currently done by walking the list of writepage requests
318 * for the inode, which can be pretty inefficient.
319 */
320static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
321{
322 struct fuse_conn *fc = get_fuse_conn(inode);
323 struct fuse_inode *fi = get_fuse_inode(inode);
324 struct fuse_req *req;
325 bool found = false;
326
327 spin_lock(&fc->lock);
328 list_for_each_entry(req, &fi->writepages, writepages_entry) {
329 pgoff_t curr_index;
330
331 BUG_ON(req->inode != inode);
332 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
333 if (curr_index == index) {
334 found = true;
335 break;
336 }
337 }
338 spin_unlock(&fc->lock);
339
340 return found;
341}
342
343/*
344 * Wait for page writeback to be completed.
345 *
346 * Since fuse doesn't rely on the VM writeback tracking, this has to
347 * use some other means.
348 */
349static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
350{
351 struct fuse_inode *fi = get_fuse_inode(inode);
352
353 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
354 return 0;
355}
356
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700357static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700358{
Al Viro6131ffa2013-02-27 16:59:05 -0500359 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700360 struct fuse_conn *fc = get_fuse_conn(inode);
361 struct fuse_file *ff = file->private_data;
362 struct fuse_req *req;
363 struct fuse_flush_in inarg;
364 int err;
365
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800366 if (is_bad_inode(inode))
367 return -EIO;
368
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700369 if (fc->no_flush)
370 return 0;
371
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400372 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700373 memset(&inarg, 0, sizeof(inarg));
374 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700375 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700376 req->in.h.opcode = FUSE_FLUSH;
377 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700378 req->in.numargs = 1;
379 req->in.args[0].size = sizeof(inarg);
380 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700381 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100382 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700383 err = req->out.h.error;
384 fuse_put_request(fc, req);
385 if (err == -ENOSYS) {
386 fc->no_flush = 1;
387 err = 0;
388 }
389 return err;
390}
391
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700392/*
393 * Wait for all pending writepages on the inode to finish.
394 *
395 * This is currently done by blocking further writes with FUSE_NOWRITE
396 * and waiting for all sent writes to complete.
397 *
398 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
399 * could conflict with truncation.
400 */
401static void fuse_sync_writes(struct inode *inode)
402{
403 fuse_set_nowrite(inode);
404 fuse_release_nowrite(inode);
405}
406
Josef Bacik02c24a82011-07-16 20:44:56 -0400407int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
408 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700409{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200410 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700411 struct fuse_conn *fc = get_fuse_conn(inode);
412 struct fuse_file *ff = file->private_data;
413 struct fuse_req *req;
414 struct fuse_fsync_in inarg;
415 int err;
416
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800417 if (is_bad_inode(inode))
418 return -EIO;
419
Josef Bacik02c24a82011-07-16 20:44:56 -0400420 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
421 if (err)
422 return err;
423
Miklos Szeredi82547982005-09-09 13:10:38 -0700424 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700425 return 0;
426
Josef Bacik02c24a82011-07-16 20:44:56 -0400427 mutex_lock(&inode->i_mutex);
428
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700429 /*
430 * Start writeback against all dirty pages of the inode, then
431 * wait for all outstanding writes, before sending the FSYNC
432 * request.
433 */
434 err = write_inode_now(inode, 0);
435 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400436 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700437
438 fuse_sync_writes(inode);
439
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400440 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400441 if (IS_ERR(req)) {
442 err = PTR_ERR(req);
443 goto out;
444 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700445
446 memset(&inarg, 0, sizeof(inarg));
447 inarg.fh = ff->fh;
448 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700449 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700450 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700451 req->in.numargs = 1;
452 req->in.args[0].size = sizeof(inarg);
453 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100454 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700455 err = req->out.h.error;
456 fuse_put_request(fc, req);
457 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700458 if (isdir)
459 fc->no_fsyncdir = 1;
460 else
461 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700462 err = 0;
463 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400464out:
465 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700466 return err;
467}
468
Josef Bacik02c24a82011-07-16 20:44:56 -0400469static int fuse_fsync(struct file *file, loff_t start, loff_t end,
470 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700471{
Josef Bacik02c24a82011-07-16 20:44:56 -0400472 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700473}
474
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200475void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
476 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700477{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700478 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800479 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700480
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800481 inarg->fh = ff->fh;
482 inarg->offset = pos;
483 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800484 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800485 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200486 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700487 req->in.numargs = 1;
488 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800489 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700490 req->out.argvar = 1;
491 req->out.numargs = 1;
492 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700493}
494
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800495static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200496 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700497{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200498 struct fuse_file *ff = file->private_data;
499 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700500
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200501 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700502 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700503 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700504
505 inarg->read_flags |= FUSE_READ_LOCKOWNER;
506 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
507 }
Tejun Heob93f8582008-11-26 12:03:55 +0100508 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800509 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700510}
511
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700512static void fuse_read_update_size(struct inode *inode, loff_t size,
513 u64 attr_ver)
514{
515 struct fuse_conn *fc = get_fuse_conn(inode);
516 struct fuse_inode *fi = get_fuse_inode(inode);
517
518 spin_lock(&fc->lock);
519 if (attr_ver == fi->attr_version && size < inode->i_size) {
520 fi->attr_version = ++fc->attr_version;
521 i_size_write(inode, size);
522 }
523 spin_unlock(&fc->lock);
524}
525
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700526static int fuse_readpage(struct file *file, struct page *page)
527{
528 struct inode *inode = page->mapping->host;
529 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800530 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700531 size_t num_read;
532 loff_t pos = page_offset(page);
533 size_t count = PAGE_CACHE_SIZE;
534 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800535 int err;
536
537 err = -EIO;
538 if (is_bad_inode(inode))
539 goto out;
540
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700541 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300542 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700543 * page-cache page, so make sure we read a properly synced
544 * page.
545 */
546 fuse_wait_on_page_writeback(inode, page->index);
547
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400548 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700549 err = PTR_ERR(req);
550 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700551 goto out;
552
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700553 attr_ver = fuse_get_attr_version(fc);
554
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700555 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200556 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700557 req->num_pages = 1;
558 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400559 req->page_descs[0].length = count;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200560 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700561 err = req->out.h.error;
562 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700563
564 if (!err) {
565 /*
566 * Short read means EOF. If file size is larger, truncate it
567 */
568 if (num_read < count)
569 fuse_read_update_size(inode, pos + num_read, attr_ver);
570
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700571 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700572 }
573
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700574 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700575 out:
576 unlock_page(page);
577 return err;
578}
579
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800580static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700581{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800582 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700583 size_t count = req->misc.read.in.size;
584 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200585 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800586
Miklos Szeredice534fb2010-05-25 15:06:07 +0200587 for (i = 0; mapping == NULL && i < req->num_pages; i++)
588 mapping = req->pages[i]->mapping;
589
590 if (mapping) {
591 struct inode *inode = mapping->host;
592
593 /*
594 * Short read means EOF. If file size is larger, truncate it
595 */
596 if (!req->out.h.error && num_read < count) {
597 loff_t pos;
598
599 pos = page_offset(req->pages[0]) + num_read;
600 fuse_read_update_size(inode, pos,
601 req->misc.read.attr_ver);
602 }
603 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700604 }
605
Miklos Szeredidb50b962005-09-09 13:10:33 -0700606 for (i = 0; i < req->num_pages; i++) {
607 struct page *page = req->pages[i];
608 if (!req->out.h.error)
609 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800610 else
611 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700612 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200613 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700614 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700615 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100616 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800617}
618
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200619static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800620{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200621 struct fuse_file *ff = file->private_data;
622 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800623 loff_t pos = page_offset(req->pages[0]);
624 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200625
626 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800627 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200628 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200629 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700630 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800631 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700632 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800633 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100634 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800635 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100636 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800637 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100638 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800639 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700640}
641
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700642struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700643 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800644 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700645 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400646 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700647};
648
649static int fuse_readpages_fill(void *_data, struct page *page)
650{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700651 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700652 struct fuse_req *req = data->req;
653 struct inode *inode = data->inode;
654 struct fuse_conn *fc = get_fuse_conn(inode);
655
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700656 fuse_wait_on_page_writeback(inode, page->index);
657
Miklos Szeredidb50b962005-09-09 13:10:33 -0700658 if (req->num_pages &&
659 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
660 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
661 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400662 int nr_alloc = min_t(unsigned, data->nr_pages,
663 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200664 fuse_send_readpages(req, data->file);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400665 data->req = req = fuse_get_req(fc, nr_alloc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700666 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700667 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700668 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700669 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700670 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400671
672 if (WARN_ON(req->num_pages >= req->max_pages)) {
673 fuse_put_request(fc, req);
674 return -EIO;
675 }
676
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200677 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700678 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400679 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100680 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400681 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700682 return 0;
683}
684
685static int fuse_readpages(struct file *file, struct address_space *mapping,
686 struct list_head *pages, unsigned nr_pages)
687{
688 struct inode *inode = mapping->host;
689 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700690 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700691 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400692 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800693
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700694 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800695 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800696 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800697
Miklos Szeredia6643092007-11-28 16:22:00 -0800698 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700699 data.inode = inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400700 data.req = fuse_get_req(fc, nr_alloc);
701 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700702 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700703 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800704 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700705
706 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700707 if (!err) {
708 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200709 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700710 else
711 fuse_put_request(fc, data.req);
712 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800713out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700714 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700715}
716
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800717static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
718 unsigned long nr_segs, loff_t pos)
719{
720 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400721 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800722
Brian Fostera8894272012-07-16 15:23:50 -0400723 /*
724 * In auto invalidate mode, always update attributes on read.
725 * Otherwise, only update if we attempt to read past EOF (to ensure
726 * i_size is up to date).
727 */
728 if (fc->auto_inval_data ||
729 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800730 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800731 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
732 if (err)
733 return err;
734 }
735
736 return generic_file_aio_read(iocb, iov, nr_segs, pos);
737}
738
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200739static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200740 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700741{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700742 struct fuse_write_in *inarg = &req->misc.write.in;
743 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700744
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700745 inarg->fh = ff->fh;
746 inarg->offset = pos;
747 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700748 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200749 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700750 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200751 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700752 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
753 else
754 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700755 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700756 req->in.args[1].size = count;
757 req->out.numargs = 1;
758 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700759 req->out.args[0].value = outarg;
760}
761
762static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200763 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700764{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200765 struct fuse_file *ff = file->private_data;
766 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200767 struct fuse_write_in *inarg = &req->misc.write.in;
768
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200769 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200770 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700771 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700772 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
773 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
774 }
Tejun Heob93f8582008-11-26 12:03:55 +0100775 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700776 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700777}
778
Miklos Szeredia1d75f22010-07-12 14:41:40 +0200779void fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700780{
781 struct fuse_conn *fc = get_fuse_conn(inode);
782 struct fuse_inode *fi = get_fuse_inode(inode);
783
784 spin_lock(&fc->lock);
785 fi->attr_version = ++fc->attr_version;
786 if (pos > inode->i_size)
787 i_size_write(inode, pos);
788 spin_unlock(&fc->lock);
789}
790
Nick Pigginea9b9902008-04-30 00:54:42 -0700791static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
792 struct inode *inode, loff_t pos,
793 size_t count)
794{
795 size_t res;
796 unsigned offset;
797 unsigned i;
798
799 for (i = 0; i < req->num_pages; i++)
800 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
801
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200802 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700803
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400804 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700805 count = res;
806 for (i = 0; i < req->num_pages; i++) {
807 struct page *page = req->pages[i];
808
809 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
810 SetPageUptodate(page);
811
812 if (count > PAGE_CACHE_SIZE - offset)
813 count -= PAGE_CACHE_SIZE - offset;
814 else
815 count = 0;
816 offset = 0;
817
818 unlock_page(page);
819 page_cache_release(page);
820 }
821
822 return res;
823}
824
825static ssize_t fuse_fill_write_pages(struct fuse_req *req,
826 struct address_space *mapping,
827 struct iov_iter *ii, loff_t pos)
828{
829 struct fuse_conn *fc = get_fuse_conn(mapping->host);
830 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
831 size_t count = 0;
832 int err;
833
Miklos Szeredif4975c62009-04-02 14:25:34 +0200834 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400835 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -0700836
837 do {
838 size_t tmp;
839 struct page *page;
840 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
841 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
842 iov_iter_count(ii));
843
844 bytes = min_t(size_t, bytes, fc->max_write - count);
845
846 again:
847 err = -EFAULT;
848 if (iov_iter_fault_in_readable(ii, bytes))
849 break;
850
851 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800852 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700853 if (!page)
854 break;
855
anfei zhou931e80e2010-02-02 13:44:02 -0800856 if (mapping_writably_mapped(mapping))
857 flush_dcache_page(page);
858
Nick Pigginea9b9902008-04-30 00:54:42 -0700859 pagefault_disable();
860 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
861 pagefault_enable();
862 flush_dcache_page(page);
863
Johannes Weiner478e0842011-07-25 22:35:35 +0200864 mark_page_accessed(page);
865
Nick Pigginea9b9902008-04-30 00:54:42 -0700866 if (!tmp) {
867 unlock_page(page);
868 page_cache_release(page);
869 bytes = min(bytes, iov_iter_single_seg_count(ii));
870 goto again;
871 }
872
873 err = 0;
874 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400875 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -0700876 req->num_pages++;
877
878 iov_iter_advance(ii, tmp);
879 count += tmp;
880 pos += tmp;
881 offset += tmp;
882 if (offset == PAGE_CACHE_SIZE)
883 offset = 0;
884
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700885 if (!fc->big_writes)
886 break;
Nick Pigginea9b9902008-04-30 00:54:42 -0700887 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +0400888 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700889
890 return count > 0 ? count : err;
891}
892
Maxim Patlasovd07f09f2012-10-26 19:49:00 +0400893static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
894{
895 return min_t(unsigned,
896 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
897 (pos >> PAGE_CACHE_SHIFT) + 1,
898 FUSE_MAX_PAGES_PER_REQ);
899}
900
Nick Pigginea9b9902008-04-30 00:54:42 -0700901static ssize_t fuse_perform_write(struct file *file,
902 struct address_space *mapping,
903 struct iov_iter *ii, loff_t pos)
904{
905 struct inode *inode = mapping->host;
906 struct fuse_conn *fc = get_fuse_conn(inode);
907 int err = 0;
908 ssize_t res = 0;
909
910 if (is_bad_inode(inode))
911 return -EIO;
912
913 do {
914 struct fuse_req *req;
915 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +0400916 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -0700917
Maxim Patlasovd07f09f2012-10-26 19:49:00 +0400918 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -0700919 if (IS_ERR(req)) {
920 err = PTR_ERR(req);
921 break;
922 }
923
924 count = fuse_fill_write_pages(req, mapping, ii, pos);
925 if (count <= 0) {
926 err = count;
927 } else {
928 size_t num_written;
929
930 num_written = fuse_send_write_pages(req, file, inode,
931 pos, count);
932 err = req->out.h.error;
933 if (!err) {
934 res += num_written;
935 pos += num_written;
936
937 /* break out of the loop on short write */
938 if (num_written != count)
939 err = -EIO;
940 }
941 }
942 fuse_put_request(fc, req);
943 } while (!err && iov_iter_count(ii));
944
945 if (res > 0)
946 fuse_write_update_size(inode, pos);
947
948 fuse_invalidate_attr(inode);
949
950 return res > 0 ? res : err;
951}
952
953static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
954 unsigned long nr_segs, loff_t pos)
955{
956 struct file *file = iocb->ki_filp;
957 struct address_space *mapping = file->f_mapping;
958 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -0500959 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -0700960 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -0500961 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -0700962 struct inode *inode = mapping->host;
963 ssize_t err;
964 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -0500965 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -0700966
967 WARN_ON(iocb->ki_pos != pos);
968
Anand Avati4273b792012-02-17 12:46:25 -0500969 ocount = 0;
970 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -0700971 if (err)
972 return err;
973
Anand Avati4273b792012-02-17 12:46:25 -0500974 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -0700975 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -0700976
977 /* We can write back this queue in page reclaim */
978 current->backing_dev_info = mapping->backing_dev_info;
979
980 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
981 if (err)
982 goto out;
983
984 if (count == 0)
985 goto out;
986
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200987 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -0700988 if (err)
989 goto out;
990
Josef Bacikc3b2da32012-03-26 09:59:21 -0400991 err = file_update_time(file);
992 if (err)
993 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -0700994
Anand Avati4273b792012-02-17 12:46:25 -0500995 if (file->f_flags & O_DIRECT) {
996 written = generic_file_direct_write(iocb, iov, &nr_segs,
997 pos, &iocb->ki_pos,
998 count, ocount);
999 if (written < 0 || written == count)
1000 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001001
Anand Avati4273b792012-02-17 12:46:25 -05001002 pos += written;
1003 count -= written;
1004
1005 iov_iter_init(&i, iov, nr_segs, count, written);
1006 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1007 if (written_buffered < 0) {
1008 err = written_buffered;
1009 goto out;
1010 }
1011 endbyte = pos + written_buffered - 1;
1012
1013 err = filemap_write_and_wait_range(file->f_mapping, pos,
1014 endbyte);
1015 if (err)
1016 goto out;
1017
1018 invalidate_mapping_pages(file->f_mapping,
1019 pos >> PAGE_CACHE_SHIFT,
1020 endbyte >> PAGE_CACHE_SHIFT);
1021
1022 written += written_buffered;
1023 iocb->ki_pos = pos + written_buffered;
1024 } else {
1025 iov_iter_init(&i, iov, nr_segs, count, 0);
1026 written = fuse_perform_write(file, mapping, &i, pos);
1027 if (written >= 0)
1028 iocb->ki_pos = pos + written;
1029 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001030out:
1031 current->backing_dev_info = NULL;
1032 mutex_unlock(&inode->i_mutex);
1033
1034 return written ? written : err;
1035}
1036
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001037static void fuse_release_user_pages(struct fuse_req *req, int write)
1038{
1039 unsigned i;
1040
1041 for (i = 0; i < req->num_pages; i++) {
1042 struct page *page = req->pages[i];
1043 if (write)
1044 set_page_dirty_lock(page);
1045 put_page(page);
1046 }
1047}
1048
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001049static inline void fuse_page_descs_length_init(struct fuse_req *req,
1050 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001051{
1052 int i;
1053
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001054 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001055 req->page_descs[i].length = PAGE_SIZE -
1056 req->page_descs[i].offset;
1057}
1058
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001059static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1060{
1061 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1062}
1063
1064static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1065 size_t max_size)
1066{
1067 return min(iov_iter_single_seg_count(ii), max_size);
1068}
1069
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001070static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001071 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001072{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001073 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001074
Miklos Szeredif4975c62009-04-02 14:25:34 +02001075 /* Special case for kernel I/O: can copy directly into the buffer */
1076 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001077 unsigned long user_addr = fuse_get_user_addr(ii);
1078 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1079
Miklos Szeredif4975c62009-04-02 14:25:34 +02001080 if (write)
1081 req->in.args[1].value = (void *) user_addr;
1082 else
1083 req->out.args[0].value = (void *) user_addr;
1084
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001085 iov_iter_advance(ii, frag_size);
1086 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001087 return 0;
1088 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001089
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001090 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001091 unsigned npages;
1092 unsigned long user_addr = fuse_get_user_addr(ii);
1093 unsigned offset = user_addr & ~PAGE_MASK;
1094 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1095 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001096
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001097 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001098 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1099
1100 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1101 npages = clamp(npages, 1U, n);
1102
1103 ret = get_user_pages_fast(user_addr, npages, !write,
1104 &req->pages[req->num_pages]);
1105 if (ret < 0)
1106 return ret;
1107
1108 npages = ret;
1109 frag_size = min_t(size_t, frag_size,
1110 (npages << PAGE_SHIFT) - offset);
1111 iov_iter_advance(ii, frag_size);
1112
1113 req->page_descs[req->num_pages].offset = offset;
1114 fuse_page_descs_length_init(req, req->num_pages, npages);
1115
1116 req->num_pages += npages;
1117 req->page_descs[req->num_pages - 1].length -=
1118 (npages << PAGE_SHIFT) - offset - frag_size;
1119
1120 nbytes += frag_size;
1121 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001122
1123 if (write)
1124 req->in.argpages = 1;
1125 else
1126 req->out.argpages = 1;
1127
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001128 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001129
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001130 return 0;
1131}
1132
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001133static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1134{
1135 struct iov_iter ii = *ii_p;
1136 int npages = 0;
1137
1138 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1139 unsigned long user_addr = fuse_get_user_addr(&ii);
1140 unsigned offset = user_addr & ~PAGE_MASK;
1141 size_t frag_size = iov_iter_single_seg_count(&ii);
1142
1143 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1144 iov_iter_advance(&ii, frag_size);
1145 }
1146
1147 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1148}
1149
Miklos Szeredifb05f412012-11-10 16:55:56 +01001150ssize_t fuse_direct_io(struct file *file, const struct iovec *iov,
1151 unsigned long nr_segs, size_t count, loff_t *ppos,
1152 int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001153{
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001154 struct fuse_file *ff = file->private_data;
1155 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001156 size_t nmax = write ? fc->max_write : fc->max_read;
1157 loff_t pos = *ppos;
1158 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001159 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001160 struct iov_iter ii;
1161
1162 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001163
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001164 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001165 if (IS_ERR(req))
1166 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001167
1168 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001169 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001170 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001171 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001172 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001173 if (err) {
1174 res = err;
1175 break;
1176 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001177
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001178 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001179 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001180 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001181 nres = fuse_send_read(req, file, pos, nbytes, owner);
1182
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001183 fuse_release_user_pages(req, !write);
1184 if (req->out.h.error) {
1185 if (!res)
1186 res = req->out.h.error;
1187 break;
1188 } else if (nres > nbytes) {
1189 res = -EIO;
1190 break;
1191 }
1192 count -= nres;
1193 res += nres;
1194 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001195 if (nres != nbytes)
1196 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001197 if (count) {
1198 fuse_put_request(fc, req);
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001199 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001200 if (IS_ERR(req))
1201 break;
1202 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001203 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001204 if (!IS_ERR(req))
1205 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001206 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001207 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001208
1209 return res;
1210}
Tejun Heo08cbf542009-04-14 10:54:53 +09001211EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001212
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001213static ssize_t __fuse_direct_read(struct file *file, const struct iovec *iov,
1214 unsigned long nr_segs, loff_t *ppos)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001215{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001216 ssize_t res;
Al Viro6131ffa2013-02-27 16:59:05 -05001217 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001218
1219 if (is_bad_inode(inode))
1220 return -EIO;
1221
Miklos Szeredifb05f412012-11-10 16:55:56 +01001222 res = fuse_direct_io(file, iov, nr_segs, iov_length(iov, nr_segs),
1223 ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001224
1225 fuse_invalidate_attr(inode);
1226
1227 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001228}
1229
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001230static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1231 size_t count, loff_t *ppos)
1232{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001233 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001234 return __fuse_direct_read(file, &iov, 1, ppos);
1235}
1236
1237static ssize_t __fuse_direct_write(struct file *file, const struct iovec *iov,
1238 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001239{
Al Viro6131ffa2013-02-27 16:59:05 -05001240 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001241 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001242 ssize_t res;
1243
1244 res = generic_write_checks(file, ppos, &count, 0);
1245 if (!res) {
Miklos Szeredifb05f412012-11-10 16:55:56 +01001246 res = fuse_direct_io(file, iov, nr_segs, count, ppos, 1);
Anand Avati4273b792012-02-17 12:46:25 -05001247 if (res > 0)
1248 fuse_write_update_size(inode, *ppos);
1249 }
1250
1251 fuse_invalidate_attr(inode);
1252
1253 return res;
1254}
1255
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001256static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1257 size_t count, loff_t *ppos)
1258{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001259 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001260 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001261 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001262
1263 if (is_bad_inode(inode))
1264 return -EIO;
1265
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001266 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001267 mutex_lock(&inode->i_mutex);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001268 res = __fuse_direct_write(file, &iov, 1, ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001269 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001270
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001271 return res;
1272}
1273
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001274static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001275{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001276 __free_page(req->pages[0]);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +01001277 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001278}
1279
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001280static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001281{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001282 struct inode *inode = req->inode;
1283 struct fuse_inode *fi = get_fuse_inode(inode);
1284 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1285
1286 list_del(&req->writepages_entry);
1287 dec_bdi_stat(bdi, BDI_WRITEBACK);
1288 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1289 bdi_writeout_inc(bdi);
1290 wake_up(&fi->page_waitq);
1291}
1292
1293/* Called under fc->lock, may release and reacquire it */
1294static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001295__releases(fc->lock)
1296__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001297{
1298 struct fuse_inode *fi = get_fuse_inode(req->inode);
1299 loff_t size = i_size_read(req->inode);
1300 struct fuse_write_in *inarg = &req->misc.write.in;
1301
1302 if (!fc->connected)
1303 goto out_free;
1304
1305 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1306 inarg->size = PAGE_CACHE_SIZE;
1307 } else if (inarg->offset < size) {
1308 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1309 } else {
1310 /* Got truncated off completely */
1311 goto out_free;
1312 }
1313
1314 req->in.args[1].size = inarg->size;
1315 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001316 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001317 return;
1318
1319 out_free:
1320 fuse_writepage_finish(fc, req);
1321 spin_unlock(&fc->lock);
1322 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001323 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001324 spin_lock(&fc->lock);
1325}
1326
1327/*
1328 * If fi->writectr is positive (no truncate or fsync going on) send
1329 * all queued writepage requests.
1330 *
1331 * Called with fc->lock
1332 */
1333void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001334__releases(fc->lock)
1335__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001336{
1337 struct fuse_conn *fc = get_fuse_conn(inode);
1338 struct fuse_inode *fi = get_fuse_inode(inode);
1339 struct fuse_req *req;
1340
1341 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1342 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1343 list_del_init(&req->list);
1344 fuse_send_writepage(fc, req);
1345 }
1346}
1347
1348static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1349{
1350 struct inode *inode = req->inode;
1351 struct fuse_inode *fi = get_fuse_inode(inode);
1352
1353 mapping_set_error(inode->i_mapping, req->out.h.error);
1354 spin_lock(&fc->lock);
1355 fi->writectr--;
1356 fuse_writepage_finish(fc, req);
1357 spin_unlock(&fc->lock);
1358 fuse_writepage_free(fc, req);
1359}
1360
1361static int fuse_writepage_locked(struct page *page)
1362{
1363 struct address_space *mapping = page->mapping;
1364 struct inode *inode = mapping->host;
1365 struct fuse_conn *fc = get_fuse_conn(inode);
1366 struct fuse_inode *fi = get_fuse_inode(inode);
1367 struct fuse_req *req;
1368 struct fuse_file *ff;
1369 struct page *tmp_page;
1370
1371 set_page_writeback(page);
1372
Maxim Patlasov4250c062012-10-26 19:48:07 +04001373 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001374 if (!req)
1375 goto err;
1376
1377 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1378 if (!tmp_page)
1379 goto err_free;
1380
1381 spin_lock(&fc->lock);
1382 BUG_ON(list_empty(&fi->write_files));
1383 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1384 req->ff = fuse_file_get(ff);
1385 spin_unlock(&fc->lock);
1386
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001387 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001388
1389 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001390 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001391 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001392 req->num_pages = 1;
1393 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001394 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001395 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001396 req->end = fuse_writepage_end;
1397 req->inode = inode;
1398
1399 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1400 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1401 end_page_writeback(page);
1402
1403 spin_lock(&fc->lock);
1404 list_add(&req->writepages_entry, &fi->writepages);
1405 list_add_tail(&req->list, &fi->queued_writes);
1406 fuse_flush_writepages(inode);
1407 spin_unlock(&fc->lock);
1408
1409 return 0;
1410
1411err_free:
1412 fuse_request_free(req);
1413err:
1414 end_page_writeback(page);
1415 return -ENOMEM;
1416}
1417
1418static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1419{
1420 int err;
1421
1422 err = fuse_writepage_locked(page);
1423 unlock_page(page);
1424
1425 return err;
1426}
1427
1428static int fuse_launder_page(struct page *page)
1429{
1430 int err = 0;
1431 if (clear_page_dirty_for_io(page)) {
1432 struct inode *inode = page->mapping->host;
1433 err = fuse_writepage_locked(page);
1434 if (!err)
1435 fuse_wait_on_page_writeback(inode, page->index);
1436 }
1437 return err;
1438}
1439
1440/*
1441 * Write back dirty pages now, because there may not be any suitable
1442 * open files later
1443 */
1444static void fuse_vma_close(struct vm_area_struct *vma)
1445{
1446 filemap_write_and_wait(vma->vm_file->f_mapping);
1447}
1448
1449/*
1450 * Wait for writeback against this page to complete before allowing it
1451 * to be marked dirty again, and hence written back again, possibly
1452 * before the previous writepage completed.
1453 *
1454 * Block here, instead of in ->writepage(), so that the userspace fs
1455 * can only block processes actually operating on the filesystem.
1456 *
1457 * Otherwise unprivileged userspace fs would be able to block
1458 * unrelated:
1459 *
1460 * - page migration
1461 * - sync(2)
1462 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1463 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001464static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001465{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001466 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001467 /*
1468 * Don't use page->mapping as it may become NULL from a
1469 * concurrent truncate.
1470 */
1471 struct inode *inode = vma->vm_file->f_mapping->host;
1472
1473 fuse_wait_on_page_writeback(inode, page->index);
1474 return 0;
1475}
1476
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001477static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001478 .close = fuse_vma_close,
1479 .fault = filemap_fault,
1480 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001481 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001482};
1483
1484static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1485{
1486 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
Al Viro6131ffa2013-02-27 16:59:05 -05001487 struct inode *inode = file_inode(file);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001488 struct fuse_conn *fc = get_fuse_conn(inode);
1489 struct fuse_inode *fi = get_fuse_inode(inode);
1490 struct fuse_file *ff = file->private_data;
1491 /*
1492 * file may be written through mmap, so chain it onto the
1493 * inodes's write_file list
1494 */
1495 spin_lock(&fc->lock);
1496 if (list_empty(&ff->write_entry))
1497 list_add(&ff->write_entry, &fi->write_files);
1498 spin_unlock(&fc->lock);
1499 }
1500 file_accessed(file);
1501 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001502 return 0;
1503}
1504
Miklos Szeredifc280c92009-04-02 14:25:35 +02001505static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1506{
1507 /* Can't provide the coherency needed for MAP_SHARED */
1508 if (vma->vm_flags & VM_MAYSHARE)
1509 return -ENODEV;
1510
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001511 invalidate_inode_pages2(file->f_mapping);
1512
Miklos Szeredifc280c92009-04-02 14:25:35 +02001513 return generic_file_mmap(file, vma);
1514}
1515
Miklos Szeredi71421252006-06-25 05:48:52 -07001516static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1517 struct file_lock *fl)
1518{
1519 switch (ffl->type) {
1520 case F_UNLCK:
1521 break;
1522
1523 case F_RDLCK:
1524 case F_WRLCK:
1525 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1526 ffl->end < ffl->start)
1527 return -EIO;
1528
1529 fl->fl_start = ffl->start;
1530 fl->fl_end = ffl->end;
1531 fl->fl_pid = ffl->pid;
1532 break;
1533
1534 default:
1535 return -EIO;
1536 }
1537 fl->fl_type = ffl->type;
1538 return 0;
1539}
1540
1541static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001542 const struct file_lock *fl, int opcode, pid_t pid,
1543 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001544{
Al Viro6131ffa2013-02-27 16:59:05 -05001545 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001546 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001547 struct fuse_file *ff = file->private_data;
1548 struct fuse_lk_in *arg = &req->misc.lk_in;
1549
1550 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001551 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001552 arg->lk.start = fl->fl_start;
1553 arg->lk.end = fl->fl_end;
1554 arg->lk.type = fl->fl_type;
1555 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001556 if (flock)
1557 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001558 req->in.h.opcode = opcode;
1559 req->in.h.nodeid = get_node_id(inode);
1560 req->in.numargs = 1;
1561 req->in.args[0].size = sizeof(*arg);
1562 req->in.args[0].value = arg;
1563}
1564
1565static int fuse_getlk(struct file *file, struct file_lock *fl)
1566{
Al Viro6131ffa2013-02-27 16:59:05 -05001567 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001568 struct fuse_conn *fc = get_fuse_conn(inode);
1569 struct fuse_req *req;
1570 struct fuse_lk_out outarg;
1571 int err;
1572
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001573 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07001574 if (IS_ERR(req))
1575 return PTR_ERR(req);
1576
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001577 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001578 req->out.numargs = 1;
1579 req->out.args[0].size = sizeof(outarg);
1580 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001581 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001582 err = req->out.h.error;
1583 fuse_put_request(fc, req);
1584 if (!err)
1585 err = convert_fuse_file_lock(&outarg.lk, fl);
1586
1587 return err;
1588}
1589
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001590static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001591{
Al Viro6131ffa2013-02-27 16:59:05 -05001592 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001593 struct fuse_conn *fc = get_fuse_conn(inode);
1594 struct fuse_req *req;
1595 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1596 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1597 int err;
1598
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001599 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07001600 /* NLM needs asynchronous locks, which we don't support yet */
1601 return -ENOLCK;
1602 }
1603
Miklos Szeredi71421252006-06-25 05:48:52 -07001604 /* Unlock on close is handled by the flush method */
1605 if (fl->fl_flags & FL_CLOSE)
1606 return 0;
1607
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001608 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07001609 if (IS_ERR(req))
1610 return PTR_ERR(req);
1611
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001612 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001613 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001614 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001615 /* locking is restartable */
1616 if (err == -EINTR)
1617 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001618 fuse_put_request(fc, req);
1619 return err;
1620}
1621
1622static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1623{
Al Viro6131ffa2013-02-27 16:59:05 -05001624 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07001625 struct fuse_conn *fc = get_fuse_conn(inode);
1626 int err;
1627
Miklos Szeredi48e90762008-07-25 01:49:02 -07001628 if (cmd == F_CANCELLK) {
1629 err = 0;
1630 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001631 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001632 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001633 err = 0;
1634 } else
1635 err = fuse_getlk(file, fl);
1636 } else {
1637 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001638 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001639 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001640 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001641 }
1642 return err;
1643}
1644
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001645static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1646{
Al Viro6131ffa2013-02-27 16:59:05 -05001647 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001648 struct fuse_conn *fc = get_fuse_conn(inode);
1649 int err;
1650
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001651 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001652 err = flock_lock_file_wait(file, fl);
1653 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001654 struct fuse_file *ff = file->private_data;
1655
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001656 /* emulate flock with POSIX locks */
1657 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001658 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001659 err = fuse_setlk(file, fl, 1);
1660 }
1661
1662 return err;
1663}
1664
Miklos Szeredib2d22722006-12-06 20:35:51 -08001665static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1666{
1667 struct inode *inode = mapping->host;
1668 struct fuse_conn *fc = get_fuse_conn(inode);
1669 struct fuse_req *req;
1670 struct fuse_bmap_in inarg;
1671 struct fuse_bmap_out outarg;
1672 int err;
1673
1674 if (!inode->i_sb->s_bdev || fc->no_bmap)
1675 return 0;
1676
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001677 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001678 if (IS_ERR(req))
1679 return 0;
1680
1681 memset(&inarg, 0, sizeof(inarg));
1682 inarg.block = block;
1683 inarg.blocksize = inode->i_sb->s_blocksize;
1684 req->in.h.opcode = FUSE_BMAP;
1685 req->in.h.nodeid = get_node_id(inode);
1686 req->in.numargs = 1;
1687 req->in.args[0].size = sizeof(inarg);
1688 req->in.args[0].value = &inarg;
1689 req->out.numargs = 1;
1690 req->out.args[0].size = sizeof(outarg);
1691 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001692 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001693 err = req->out.h.error;
1694 fuse_put_request(fc, req);
1695 if (err == -ENOSYS)
1696 fc->no_bmap = 1;
1697
1698 return err ? 0 : outarg.block;
1699}
1700
Andrew Morton965c8e52012-12-17 15:59:39 -08001701static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001702{
1703 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05001704 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001705
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001706 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08001707 if (whence == SEEK_CUR || whence == SEEK_SET)
1708 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04001709
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001710 mutex_lock(&inode->i_mutex);
1711 retval = fuse_update_attributes(inode, NULL, file, NULL);
1712 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08001713 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001714 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01001715
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001716 return retval;
1717}
1718
Tejun Heo59efec72008-11-26 12:03:55 +01001719static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1720 unsigned int nr_segs, size_t bytes, bool to_user)
1721{
1722 struct iov_iter ii;
1723 int page_idx = 0;
1724
1725 if (!bytes)
1726 return 0;
1727
1728 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1729
1730 while (iov_iter_count(&ii)) {
1731 struct page *page = pages[page_idx++];
1732 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001733 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001734
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001735 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001736
1737 while (todo) {
1738 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1739 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1740 size_t copy = min(todo, iov_len);
1741 size_t left;
1742
1743 if (!to_user)
1744 left = copy_from_user(kaddr, uaddr, copy);
1745 else
1746 left = copy_to_user(uaddr, kaddr, copy);
1747
1748 if (unlikely(left))
1749 return -EFAULT;
1750
1751 iov_iter_advance(&ii, copy);
1752 todo -= copy;
1753 kaddr += copy;
1754 }
1755
Jens Axboe0bd87182009-11-03 11:40:44 +01001756 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001757 }
1758
1759 return 0;
1760}
1761
1762/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001763 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1764 * ABI was defined to be 'struct iovec' which is different on 32bit
1765 * and 64bit. Fortunately we can determine which structure the server
1766 * used from the size of the reply.
1767 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001768static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1769 size_t transferred, unsigned count,
1770 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001771{
1772#ifdef CONFIG_COMPAT
1773 if (count * sizeof(struct compat_iovec) == transferred) {
1774 struct compat_iovec *ciov = src;
1775 unsigned i;
1776
1777 /*
1778 * With this interface a 32bit server cannot support
1779 * non-compat (i.e. ones coming from 64bit apps) ioctl
1780 * requests
1781 */
1782 if (!is_compat)
1783 return -EINVAL;
1784
1785 for (i = 0; i < count; i++) {
1786 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1787 dst[i].iov_len = ciov[i].iov_len;
1788 }
1789 return 0;
1790 }
1791#endif
1792
1793 if (count * sizeof(struct iovec) != transferred)
1794 return -EIO;
1795
1796 memcpy(dst, src, transferred);
1797 return 0;
1798}
1799
Miklos Szeredi75727772010-11-30 16:39:27 +01001800/* Make sure iov_length() won't overflow */
1801static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1802{
1803 size_t n;
1804 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1805
Zach Brownfb6ccff2012-07-24 12:10:11 -07001806 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01001807 if (iov->iov_len > (size_t) max)
1808 return -ENOMEM;
1809 max -= iov->iov_len;
1810 }
1811 return 0;
1812}
1813
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001814static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1815 void *src, size_t transferred, unsigned count,
1816 bool is_compat)
1817{
1818 unsigned i;
1819 struct fuse_ioctl_iovec *fiov = src;
1820
1821 if (fc->minor < 16) {
1822 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1823 count, is_compat);
1824 }
1825
1826 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1827 return -EIO;
1828
1829 for (i = 0; i < count; i++) {
1830 /* Did the server supply an inappropriate value? */
1831 if (fiov[i].base != (unsigned long) fiov[i].base ||
1832 fiov[i].len != (unsigned long) fiov[i].len)
1833 return -EIO;
1834
1835 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1836 dst[i].iov_len = (size_t) fiov[i].len;
1837
1838#ifdef CONFIG_COMPAT
1839 if (is_compat &&
1840 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1841 (compat_size_t) dst[i].iov_len != fiov[i].len))
1842 return -EIO;
1843#endif
1844 }
1845
1846 return 0;
1847}
1848
1849
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001850/*
Tejun Heo59efec72008-11-26 12:03:55 +01001851 * For ioctls, there is no generic way to determine how much memory
1852 * needs to be read and/or written. Furthermore, ioctls are allowed
1853 * to dereference the passed pointer, so the parameter requires deep
1854 * copying but FUSE has no idea whatsoever about what to copy in or
1855 * out.
1856 *
1857 * This is solved by allowing FUSE server to retry ioctl with
1858 * necessary in/out iovecs. Let's assume the ioctl implementation
1859 * needs to read in the following structure.
1860 *
1861 * struct a {
1862 * char *buf;
1863 * size_t buflen;
1864 * }
1865 *
1866 * On the first callout to FUSE server, inarg->in_size and
1867 * inarg->out_size will be NULL; then, the server completes the ioctl
1868 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1869 * the actual iov array to
1870 *
1871 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1872 *
1873 * which tells FUSE to copy in the requested area and retry the ioctl.
1874 * On the second round, the server has access to the structure and
1875 * from that it can tell what to look for next, so on the invocation,
1876 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1877 *
1878 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1879 * { .iov_base = a.buf, .iov_len = a.buflen } }
1880 *
1881 * FUSE will copy both struct a and the pointed buffer from the
1882 * process doing the ioctl and retry ioctl with both struct a and the
1883 * buffer.
1884 *
1885 * This time, FUSE server has everything it needs and completes ioctl
1886 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1887 *
1888 * Copying data out works the same way.
1889 *
1890 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1891 * automatically initializes in and out iovs by decoding @cmd with
1892 * _IOC_* macros and the server is not allowed to request RETRY. This
1893 * limits ioctl data transfers to well-formed ioctls and is the forced
1894 * behavior for all FUSE servers.
1895 */
Tejun Heo08cbf542009-04-14 10:54:53 +09001896long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1897 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01001898{
Tejun Heo59efec72008-11-26 12:03:55 +01001899 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001900 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01001901 struct fuse_ioctl_in inarg = {
1902 .fh = ff->fh,
1903 .cmd = cmd,
1904 .arg = arg,
1905 .flags = flags
1906 };
1907 struct fuse_ioctl_out outarg;
1908 struct fuse_req *req = NULL;
1909 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001910 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01001911 struct iovec *in_iov = NULL, *out_iov = NULL;
1912 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1913 size_t in_size, out_size, transferred;
1914 int err;
1915
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001916#if BITS_PER_LONG == 32
1917 inarg.flags |= FUSE_IOCTL_32BIT;
1918#else
1919 if (flags & FUSE_IOCTL_COMPAT)
1920 inarg.flags |= FUSE_IOCTL_32BIT;
1921#endif
1922
Tejun Heo59efec72008-11-26 12:03:55 +01001923 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001924 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01001925
Tejun Heo59efec72008-11-26 12:03:55 +01001926 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01001927 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001928 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01001929 if (!pages || !iov_page)
1930 goto out;
1931
1932 /*
1933 * If restricted, initialize IO parameters as encoded in @cmd.
1934 * RETRY from server is not allowed.
1935 */
1936 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001937 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01001938
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01001939 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01001940 iov->iov_len = _IOC_SIZE(cmd);
1941
1942 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1943 in_iov = iov;
1944 in_iovs = 1;
1945 }
1946
1947 if (_IOC_DIR(cmd) & _IOC_READ) {
1948 out_iov = iov;
1949 out_iovs = 1;
1950 }
1951 }
1952
1953 retry:
1954 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1955 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1956
1957 /*
1958 * Out data can be used either for actual out data or iovs,
1959 * make sure there always is at least one page.
1960 */
1961 out_size = max_t(size_t, out_size, PAGE_SIZE);
1962 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1963
1964 /* make sure there are enough buffer pages and init request with them */
1965 err = -ENOMEM;
1966 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1967 goto out;
1968 while (num_pages < max_pages) {
1969 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1970 if (!pages[num_pages])
1971 goto out;
1972 num_pages++;
1973 }
1974
Maxim Patlasov54b96672012-10-26 19:49:13 +04001975 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01001976 if (IS_ERR(req)) {
1977 err = PTR_ERR(req);
1978 req = NULL;
1979 goto out;
1980 }
1981 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1982 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001983 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01001984
1985 /* okay, let's send it to the client */
1986 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001987 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01001988 req->in.numargs = 1;
1989 req->in.args[0].size = sizeof(inarg);
1990 req->in.args[0].value = &inarg;
1991 if (in_size) {
1992 req->in.numargs++;
1993 req->in.args[1].size = in_size;
1994 req->in.argpages = 1;
1995
1996 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1997 false);
1998 if (err)
1999 goto out;
2000 }
2001
2002 req->out.numargs = 2;
2003 req->out.args[0].size = sizeof(outarg);
2004 req->out.args[0].value = &outarg;
2005 req->out.args[1].size = out_size;
2006 req->out.argpages = 1;
2007 req->out.argvar = 1;
2008
Tejun Heob93f8582008-11-26 12:03:55 +01002009 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002010 err = req->out.h.error;
2011 transferred = req->out.args[1].size;
2012 fuse_put_request(fc, req);
2013 req = NULL;
2014 if (err)
2015 goto out;
2016
2017 /* did it ask for retry? */
2018 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002019 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002020
2021 /* no retry if in restricted mode */
2022 err = -EIO;
2023 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2024 goto out;
2025
2026 in_iovs = outarg.in_iovs;
2027 out_iovs = outarg.out_iovs;
2028
2029 /*
2030 * Make sure things are in boundary, separate checks
2031 * are to protect against overflow.
2032 */
2033 err = -ENOMEM;
2034 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2035 out_iovs > FUSE_IOCTL_MAX_IOV ||
2036 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2037 goto out;
2038
Cong Wang2408f6e2011-11-25 23:14:30 +08002039 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002040 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002041 transferred, in_iovs + out_iovs,
2042 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002043 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002044 if (err)
2045 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002046
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002047 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002048 out_iov = in_iov + in_iovs;
2049
Miklos Szeredi75727772010-11-30 16:39:27 +01002050 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2051 if (err)
2052 goto out;
2053
2054 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2055 if (err)
2056 goto out;
2057
Tejun Heo59efec72008-11-26 12:03:55 +01002058 goto retry;
2059 }
2060
2061 err = -EIO;
2062 if (transferred > inarg.out_size)
2063 goto out;
2064
2065 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2066 out:
2067 if (req)
2068 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002069 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002070 while (num_pages)
2071 __free_page(pages[--num_pages]);
2072 kfree(pages);
2073
2074 return err ? err : outarg.result;
2075}
Tejun Heo08cbf542009-04-14 10:54:53 +09002076EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002077
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002078long fuse_ioctl_common(struct file *file, unsigned int cmd,
2079 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002080{
Al Viro6131ffa2013-02-27 16:59:05 -05002081 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002082 struct fuse_conn *fc = get_fuse_conn(inode);
2083
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002084 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002085 return -EACCES;
2086
2087 if (is_bad_inode(inode))
2088 return -EIO;
2089
2090 return fuse_do_ioctl(file, cmd, arg, flags);
2091}
2092
Tejun Heo59efec72008-11-26 12:03:55 +01002093static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2094 unsigned long arg)
2095{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002096 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002097}
2098
2099static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2100 unsigned long arg)
2101{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002102 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002103}
2104
Tejun Heo95668a62008-11-26 12:03:55 +01002105/*
2106 * All files which have been polled are linked to RB tree
2107 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2108 * find the matching one.
2109 */
2110static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2111 struct rb_node **parent_out)
2112{
2113 struct rb_node **link = &fc->polled_files.rb_node;
2114 struct rb_node *last = NULL;
2115
2116 while (*link) {
2117 struct fuse_file *ff;
2118
2119 last = *link;
2120 ff = rb_entry(last, struct fuse_file, polled_node);
2121
2122 if (kh < ff->kh)
2123 link = &last->rb_left;
2124 else if (kh > ff->kh)
2125 link = &last->rb_right;
2126 else
2127 return link;
2128 }
2129
2130 if (parent_out)
2131 *parent_out = last;
2132 return link;
2133}
2134
2135/*
2136 * The file is about to be polled. Make sure it's on the polled_files
2137 * RB tree. Note that files once added to the polled_files tree are
2138 * not removed before the file is released. This is because a file
2139 * polled once is likely to be polled again.
2140 */
2141static void fuse_register_polled_file(struct fuse_conn *fc,
2142 struct fuse_file *ff)
2143{
2144 spin_lock(&fc->lock);
2145 if (RB_EMPTY_NODE(&ff->polled_node)) {
2146 struct rb_node **link, *parent;
2147
2148 link = fuse_find_polled_node(fc, ff->kh, &parent);
2149 BUG_ON(*link);
2150 rb_link_node(&ff->polled_node, parent, link);
2151 rb_insert_color(&ff->polled_node, &fc->polled_files);
2152 }
2153 spin_unlock(&fc->lock);
2154}
2155
Tejun Heo08cbf542009-04-14 10:54:53 +09002156unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002157{
Tejun Heo95668a62008-11-26 12:03:55 +01002158 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002159 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002160 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2161 struct fuse_poll_out outarg;
2162 struct fuse_req *req;
2163 int err;
2164
2165 if (fc->no_poll)
2166 return DEFAULT_POLLMASK;
2167
2168 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002169 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002170
2171 /*
2172 * Ask for notification iff there's someone waiting for it.
2173 * The client may ignore the flag and always notify.
2174 */
2175 if (waitqueue_active(&ff->poll_wait)) {
2176 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2177 fuse_register_polled_file(fc, ff);
2178 }
2179
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002180 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002181 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002182 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002183
2184 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002185 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002186 req->in.numargs = 1;
2187 req->in.args[0].size = sizeof(inarg);
2188 req->in.args[0].value = &inarg;
2189 req->out.numargs = 1;
2190 req->out.args[0].size = sizeof(outarg);
2191 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002192 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002193 err = req->out.h.error;
2194 fuse_put_request(fc, req);
2195
2196 if (!err)
2197 return outarg.revents;
2198 if (err == -ENOSYS) {
2199 fc->no_poll = 1;
2200 return DEFAULT_POLLMASK;
2201 }
2202 return POLLERR;
2203}
Tejun Heo08cbf542009-04-14 10:54:53 +09002204EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002205
2206/*
2207 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2208 * wakes up the poll waiters.
2209 */
2210int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2211 struct fuse_notify_poll_wakeup_out *outarg)
2212{
2213 u64 kh = outarg->kh;
2214 struct rb_node **link;
2215
2216 spin_lock(&fc->lock);
2217
2218 link = fuse_find_polled_node(fc, kh, NULL);
2219 if (*link) {
2220 struct fuse_file *ff;
2221
2222 ff = rb_entry(*link, struct fuse_file, polled_node);
2223 wake_up_interruptible_sync(&ff->poll_wait);
2224 }
2225
2226 spin_unlock(&fc->lock);
2227 return 0;
2228}
2229
Anand Avati4273b792012-02-17 12:46:25 -05002230static ssize_t
2231fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2232 loff_t offset, unsigned long nr_segs)
2233{
2234 ssize_t ret = 0;
2235 struct file *file = NULL;
2236 loff_t pos = 0;
2237
2238 file = iocb->ki_filp;
2239 pos = offset;
2240
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002241 if (rw == WRITE)
2242 ret = __fuse_direct_write(file, iov, nr_segs, &pos);
2243 else
2244 ret = __fuse_direct_read(file, iov, nr_segs, &pos);
Anand Avati4273b792012-02-17 12:46:25 -05002245
2246 return ret;
2247}
2248
Miklos Szeredicdadb112012-11-10 16:55:56 +01002249static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2250 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002251{
2252 struct fuse_file *ff = file->private_data;
2253 struct fuse_conn *fc = ff->fc;
2254 struct fuse_req *req;
2255 struct fuse_fallocate_in inarg = {
2256 .fh = ff->fh,
2257 .offset = offset,
2258 .length = length,
2259 .mode = mode
2260 };
2261 int err;
2262
Miklos Szeredi519c6042012-04-26 10:56:36 +02002263 if (fc->no_fallocate)
2264 return -EOPNOTSUPP;
2265
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002266 req = fuse_get_req_nopages(fc);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002267 if (IS_ERR(req))
2268 return PTR_ERR(req);
2269
2270 req->in.h.opcode = FUSE_FALLOCATE;
2271 req->in.h.nodeid = ff->nodeid;
2272 req->in.numargs = 1;
2273 req->in.args[0].size = sizeof(inarg);
2274 req->in.args[0].value = &inarg;
2275 fuse_request_send(fc, req);
2276 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02002277 if (err == -ENOSYS) {
2278 fc->no_fallocate = 1;
2279 err = -EOPNOTSUPP;
2280 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002281 fuse_put_request(fc, req);
2282
2283 return err;
2284}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002285
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002286static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002287 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002288 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002289 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002290 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002291 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002292 .mmap = fuse_file_mmap,
2293 .open = fuse_open,
2294 .flush = fuse_flush,
2295 .release = fuse_release,
2296 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002297 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002298 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002299 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002300 .unlocked_ioctl = fuse_file_ioctl,
2301 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002302 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002303 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002304};
2305
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002306static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002307 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002308 .read = fuse_direct_read,
2309 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002310 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002311 .open = fuse_open,
2312 .flush = fuse_flush,
2313 .release = fuse_release,
2314 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002315 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002316 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002317 .unlocked_ioctl = fuse_file_ioctl,
2318 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002319 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002320 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002321 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002322};
2323
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002324static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002325 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002326 .writepage = fuse_writepage,
2327 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002328 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002329 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002330 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05002331 .direct_IO = fuse_direct_IO,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002332};
2333
2334void fuse_init_file_inode(struct inode *inode)
2335{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002336 inode->i_fop = &fuse_file_operations;
2337 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002338}