blob: d480d9af46c964f8b86ced68feb7e787959a4fdb [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>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070017
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080018static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070019
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020020static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
21 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070022{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070023 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080024 struct fuse_req *req;
25 int err;
26
Miklos Szeredice1d5a42006-04-10 22:54:58 -070027 req = fuse_get_req(fc);
28 if (IS_ERR(req))
29 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080030
31 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070032 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
33 if (!fc->atomic_o_trunc)
34 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020035 req->in.h.opcode = opcode;
36 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080037 req->in.numargs = 1;
38 req->in.args[0].size = sizeof(inarg);
39 req->in.args[0].value = &inarg;
40 req->out.numargs = 1;
41 req->out.args[0].size = sizeof(*outargp);
42 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010043 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080044 err = req->out.h.error;
45 fuse_put_request(fc, req);
46
47 return err;
48}
49
Tejun Heoacf99432008-11-26 12:03:55 +010050struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080051{
52 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090053
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090055 if (unlikely(!ff))
56 return NULL;
57
Miklos Szeredida5e4712009-04-28 16:56:36 +020058 ff->fc = fc;
Tejun Heo6b2db282009-04-14 10:54:49 +090059 ff->reserved_req = fuse_request_alloc();
60 if (unlikely(!ff->reserved_req)) {
61 kfree(ff);
62 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080063 }
Tejun Heo6b2db282009-04-14 10:54:49 +090064
65 INIT_LIST_HEAD(&ff->write_entry);
66 atomic_set(&ff->count, 0);
67 RB_CLEAR_NODE(&ff->polled_node);
68 init_waitqueue_head(&ff->poll_wait);
69
70 spin_lock(&fc->lock);
71 ff->kh = ++fc->khctr;
72 spin_unlock(&fc->lock);
73
Miklos Szeredifd72faa2005-11-07 00:59:51 -080074 return ff;
75}
76
77void fuse_file_free(struct fuse_file *ff)
78{
Miklos Szeredi33649c92006-06-25 05:48:52 -070079 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080080 kfree(ff);
81}
82
Miklos Szeredic7b71432009-04-28 16:56:37 +020083struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070084{
85 atomic_inc(&ff->count);
86 return ff;
87}
88
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010089static void fuse_release_async(struct work_struct *work)
Miklos Szeredi819c4b32007-10-16 23:31:04 -070090{
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010091 struct fuse_req *req;
92 struct fuse_conn *fc;
93 struct path path;
94
95 req = container_of(work, struct fuse_req, misc.release.work);
96 path = req->misc.release.path;
97 fc = get_fuse_conn(path.dentry->d_inode);
98
99 fuse_put_request(fc, req);
100 path_put(&path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -0700101}
102
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100103static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
104{
105 if (fc->destroy_req) {
106 /*
107 * If this is a fuseblk mount, then it's possible that
108 * releasing the path will result in releasing the
109 * super block and sending the DESTROY request. If
110 * the server is single threaded, this would hang.
111 * For this reason do the path_put() in a separate
112 * thread.
113 */
114 atomic_inc(&req->count);
115 INIT_WORK(&req->misc.release.work, fuse_release_async);
116 schedule_work(&req->misc.release.work);
117 } else {
118 path_put(&req->misc.release.path);
119 }
120}
121
122static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700123{
124 if (atomic_dec_and_test(&ff->count)) {
125 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200126
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100127 if (sync) {
128 fuse_request_send(ff->fc, req);
129 path_put(&req->misc.release.path);
130 fuse_put_request(ff->fc, req);
131 } else {
132 req->end = fuse_release_end;
133 fuse_request_send_background(ff->fc, req);
134 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700135 kfree(ff);
136 }
137}
138
Tejun Heo08cbf542009-04-14 10:54:53 +0900139int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
140 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200141{
142 struct fuse_open_out outarg;
143 struct fuse_file *ff;
144 int err;
145 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
146
147 ff = fuse_file_alloc(fc);
148 if (!ff)
149 return -ENOMEM;
150
151 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
152 if (err) {
153 fuse_file_free(ff);
154 return err;
155 }
156
157 if (isdir)
158 outarg.open_flags &= ~FOPEN_DIRECT_IO;
159
160 ff->fh = outarg.fh;
161 ff->nodeid = nodeid;
162 ff->open_flags = outarg.open_flags;
163 file->private_data = fuse_file_get(ff);
164
165 return 0;
166}
Tejun Heo08cbf542009-04-14 10:54:53 +0900167EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200168
Miklos Szeredic7b71432009-04-28 16:56:37 +0200169void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800170{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200171 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800172 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200173
174 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800175 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200176 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700177 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200178 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200179 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800180 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
181 struct fuse_inode *fi = get_fuse_inode(inode);
182
183 spin_lock(&fc->lock);
184 fi->attr_version = ++fc->attr_version;
185 i_size_write(inode, 0);
186 spin_unlock(&fc->lock);
187 fuse_invalidate_attr(inode);
188 }
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800189}
190
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200191int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800192{
Tejun Heoacf99432008-11-26 12:03:55 +0100193 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700194 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700195
Miklos Szeredidd190d02005-09-30 11:59:02 -0700196 /* VFS checks this, but only _after_ ->open() */
197 if (file->f_flags & O_DIRECT)
198 return -EINVAL;
199
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700200 err = generic_file_open(inode, file);
201 if (err)
202 return err;
203
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200204 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800205 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200206 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700207
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200208 fuse_finish_open(inode, file);
209
210 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700211}
212
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200213static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800214{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200215 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700216 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800217 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700218
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200219 spin_lock(&fc->lock);
220 list_del(&ff->write_entry);
221 if (!RB_EMPTY_NODE(&ff->polled_node))
222 rb_erase(&ff->polled_node, &fc->polled_files);
223 spin_unlock(&fc->lock);
224
Bryan Green357ccf22011-03-01 16:43:52 -0800225 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200226
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700227 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800228 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700229 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200230 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700231 req->in.numargs = 1;
232 req->in.args[0].size = sizeof(struct fuse_release_in);
233 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800234}
235
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200236void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800237{
Tejun Heo6b2db282009-04-14 10:54:49 +0900238 struct fuse_file *ff;
239 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700240
Tejun Heo6b2db282009-04-14 10:54:49 +0900241 ff = file->private_data;
242 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200243 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700244
Tejun Heo6b2db282009-04-14 10:54:49 +0900245 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200246 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100247
Tejun Heo6b2db282009-04-14 10:54:49 +0900248 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200249 path_get(&file->f_path);
250 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700251
Tejun Heo6b2db282009-04-14 10:54:49 +0900252 /*
253 * Normally this will send the RELEASE request, however if
254 * some asynchronous READ or WRITE requests are outstanding,
255 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100256 *
257 * Make the release synchronous if this is a fuseblk mount,
258 * synchronous RELEASE is allowed (and desirable) in this case
259 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900260 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100261 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700262}
263
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700264static int fuse_open(struct inode *inode, struct file *file)
265{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200266 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700267}
268
269static int fuse_release(struct inode *inode, struct file *file)
270{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200271 fuse_release_common(file, FUSE_RELEASE);
272
273 /* return value is ignored by VFS */
274 return 0;
275}
276
277void fuse_sync_release(struct fuse_file *ff, int flags)
278{
279 WARN_ON(atomic_read(&ff->count) > 1);
280 fuse_prepare_release(ff, flags, FUSE_RELEASE);
281 ff->reserved_req->force = 1;
282 fuse_request_send(ff->fc, ff->reserved_req);
283 fuse_put_request(ff->fc, ff->reserved_req);
284 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700285}
Tejun Heo08cbf542009-04-14 10:54:53 +0900286EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700287
Miklos Szeredi71421252006-06-25 05:48:52 -0700288/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700289 * Scramble the ID space with XTEA, so that the value of the files_struct
290 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700291 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700292u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700293{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700294 u32 *k = fc->scramble_key;
295 u64 v = (unsigned long) id;
296 u32 v0 = v;
297 u32 v1 = v >> 32;
298 u32 sum = 0;
299 int i;
300
301 for (i = 0; i < 32; i++) {
302 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
303 sum += 0x9E3779B9;
304 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
305 }
306
307 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700308}
309
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700310/*
311 * Check if page is under writeback
312 *
313 * This is currently done by walking the list of writepage requests
314 * for the inode, which can be pretty inefficient.
315 */
316static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
317{
318 struct fuse_conn *fc = get_fuse_conn(inode);
319 struct fuse_inode *fi = get_fuse_inode(inode);
320 struct fuse_req *req;
321 bool found = false;
322
323 spin_lock(&fc->lock);
324 list_for_each_entry(req, &fi->writepages, writepages_entry) {
325 pgoff_t curr_index;
326
327 BUG_ON(req->inode != inode);
328 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
329 if (curr_index == index) {
330 found = true;
331 break;
332 }
333 }
334 spin_unlock(&fc->lock);
335
336 return found;
337}
338
339/*
340 * Wait for page writeback to be completed.
341 *
342 * Since fuse doesn't rely on the VM writeback tracking, this has to
343 * use some other means.
344 */
345static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
346{
347 struct fuse_inode *fi = get_fuse_inode(inode);
348
349 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
350 return 0;
351}
352
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700353static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700354{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800355 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700356 struct fuse_conn *fc = get_fuse_conn(inode);
357 struct fuse_file *ff = file->private_data;
358 struct fuse_req *req;
359 struct fuse_flush_in inarg;
360 int err;
361
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800362 if (is_bad_inode(inode))
363 return -EIO;
364
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700365 if (fc->no_flush)
366 return 0;
367
Miklos Szeredi33649c92006-06-25 05:48:52 -0700368 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700369 memset(&inarg, 0, sizeof(inarg));
370 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700371 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700372 req->in.h.opcode = FUSE_FLUSH;
373 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700374 req->in.numargs = 1;
375 req->in.args[0].size = sizeof(inarg);
376 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700377 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100378 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700379 err = req->out.h.error;
380 fuse_put_request(fc, req);
381 if (err == -ENOSYS) {
382 fc->no_flush = 1;
383 err = 0;
384 }
385 return err;
386}
387
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700388/*
389 * Wait for all pending writepages on the inode to finish.
390 *
391 * This is currently done by blocking further writes with FUSE_NOWRITE
392 * and waiting for all sent writes to complete.
393 *
394 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
395 * could conflict with truncation.
396 */
397static void fuse_sync_writes(struct inode *inode)
398{
399 fuse_set_nowrite(inode);
400 fuse_release_nowrite(inode);
401}
402
Josef Bacik02c24a82011-07-16 20:44:56 -0400403int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
404 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700405{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200406 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700407 struct fuse_conn *fc = get_fuse_conn(inode);
408 struct fuse_file *ff = file->private_data;
409 struct fuse_req *req;
410 struct fuse_fsync_in inarg;
411 int err;
412
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800413 if (is_bad_inode(inode))
414 return -EIO;
415
Josef Bacik02c24a82011-07-16 20:44:56 -0400416 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
417 if (err)
418 return err;
419
Miklos Szeredi82547982005-09-09 13:10:38 -0700420 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700421 return 0;
422
Josef Bacik02c24a82011-07-16 20:44:56 -0400423 mutex_lock(&inode->i_mutex);
424
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700425 /*
426 * Start writeback against all dirty pages of the inode, then
427 * wait for all outstanding writes, before sending the FSYNC
428 * request.
429 */
430 err = write_inode_now(inode, 0);
431 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400432 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700433
434 fuse_sync_writes(inode);
435
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700436 req = fuse_get_req(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400437 if (IS_ERR(req)) {
438 err = PTR_ERR(req);
439 goto out;
440 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700441
442 memset(&inarg, 0, sizeof(inarg));
443 inarg.fh = ff->fh;
444 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700445 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700446 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700447 req->in.numargs = 1;
448 req->in.args[0].size = sizeof(inarg);
449 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100450 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700451 err = req->out.h.error;
452 fuse_put_request(fc, req);
453 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700454 if (isdir)
455 fc->no_fsyncdir = 1;
456 else
457 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700458 err = 0;
459 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400460out:
461 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700462 return err;
463}
464
Josef Bacik02c24a82011-07-16 20:44:56 -0400465static int fuse_fsync(struct file *file, loff_t start, loff_t end,
466 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700467{
Josef Bacik02c24a82011-07-16 20:44:56 -0400468 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700469}
470
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200471void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
472 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700473{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700474 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800475 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700476
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800477 inarg->fh = ff->fh;
478 inarg->offset = pos;
479 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800480 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800481 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200482 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700483 req->in.numargs = 1;
484 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800485 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700486 req->out.argvar = 1;
487 req->out.numargs = 1;
488 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700489}
490
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800491static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200492 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700493{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200494 struct fuse_file *ff = file->private_data;
495 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700496
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200497 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700498 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700499 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700500
501 inarg->read_flags |= FUSE_READ_LOCKOWNER;
502 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
503 }
Tejun Heob93f8582008-11-26 12:03:55 +0100504 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800505 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700506}
507
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700508static void fuse_read_update_size(struct inode *inode, loff_t size,
509 u64 attr_ver)
510{
511 struct fuse_conn *fc = get_fuse_conn(inode);
512 struct fuse_inode *fi = get_fuse_inode(inode);
513
514 spin_lock(&fc->lock);
515 if (attr_ver == fi->attr_version && size < inode->i_size) {
516 fi->attr_version = ++fc->attr_version;
517 i_size_write(inode, size);
518 }
519 spin_unlock(&fc->lock);
520}
521
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700522static int fuse_readpage(struct file *file, struct page *page)
523{
524 struct inode *inode = page->mapping->host;
525 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800526 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700527 size_t num_read;
528 loff_t pos = page_offset(page);
529 size_t count = PAGE_CACHE_SIZE;
530 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800531 int err;
532
533 err = -EIO;
534 if (is_bad_inode(inode))
535 goto out;
536
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700537 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300538 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700539 * page-cache page, so make sure we read a properly synced
540 * page.
541 */
542 fuse_wait_on_page_writeback(inode, page->index);
543
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700544 req = fuse_get_req(fc);
545 err = PTR_ERR(req);
546 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700547 goto out;
548
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700549 attr_ver = fuse_get_attr_version(fc);
550
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700551 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200552 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700553 req->num_pages = 1;
554 req->pages[0] = page;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200555 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700556 err = req->out.h.error;
557 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700558
559 if (!err) {
560 /*
561 * Short read means EOF. If file size is larger, truncate it
562 */
563 if (num_read < count)
564 fuse_read_update_size(inode, pos + num_read, attr_ver);
565
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700566 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700567 }
568
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700569 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700570 out:
571 unlock_page(page);
572 return err;
573}
574
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800575static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700576{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800577 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700578 size_t count = req->misc.read.in.size;
579 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200580 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800581
Miklos Szeredice534fb2010-05-25 15:06:07 +0200582 for (i = 0; mapping == NULL && i < req->num_pages; i++)
583 mapping = req->pages[i]->mapping;
584
585 if (mapping) {
586 struct inode *inode = mapping->host;
587
588 /*
589 * Short read means EOF. If file size is larger, truncate it
590 */
591 if (!req->out.h.error && num_read < count) {
592 loff_t pos;
593
594 pos = page_offset(req->pages[0]) + num_read;
595 fuse_read_update_size(inode, pos,
596 req->misc.read.attr_ver);
597 }
598 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700599 }
600
Miklos Szeredidb50b962005-09-09 13:10:33 -0700601 for (i = 0; i < req->num_pages; i++) {
602 struct page *page = req->pages[i];
603 if (!req->out.h.error)
604 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800605 else
606 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700607 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200608 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700609 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700610 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100611 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800612}
613
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200614static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800615{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200616 struct fuse_file *ff = file->private_data;
617 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800618 loff_t pos = page_offset(req->pages[0]);
619 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200620
621 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800622 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200623 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200624 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700625 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800626 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700627 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800628 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100629 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800630 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100631 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800632 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100633 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800634 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700635}
636
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700637struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700638 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800639 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700640 struct inode *inode;
641};
642
643static int fuse_readpages_fill(void *_data, struct page *page)
644{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700645 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700646 struct fuse_req *req = data->req;
647 struct inode *inode = data->inode;
648 struct fuse_conn *fc = get_fuse_conn(inode);
649
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700650 fuse_wait_on_page_writeback(inode, page->index);
651
Miklos Szeredidb50b962005-09-09 13:10:33 -0700652 if (req->num_pages &&
653 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
654 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
655 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200656 fuse_send_readpages(req, data->file);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700657 data->req = req = fuse_get_req(fc);
658 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700659 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700660 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700661 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700662 }
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200663 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700664 req->pages[req->num_pages] = page;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100665 req->num_pages++;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700666 return 0;
667}
668
669static int fuse_readpages(struct file *file, struct address_space *mapping,
670 struct list_head *pages, unsigned nr_pages)
671{
672 struct inode *inode = mapping->host;
673 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700674 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700675 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800676
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700677 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800678 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800679 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800680
Miklos Szeredia6643092007-11-28 16:22:00 -0800681 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700682 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700683 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700684 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700685 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800686 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700687
688 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700689 if (!err) {
690 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200691 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700692 else
693 fuse_put_request(fc, data.req);
694 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800695out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700696 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700697}
698
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800699static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
700 unsigned long nr_segs, loff_t pos)
701{
702 struct inode *inode = iocb->ki_filp->f_mapping->host;
703
704 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
705 int err;
706 /*
707 * If trying to read past EOF, make sure the i_size
708 * attribute is up-to-date.
709 */
710 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
711 if (err)
712 return err;
713 }
714
715 return generic_file_aio_read(iocb, iov, nr_segs, pos);
716}
717
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200718static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200719 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700720{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700721 struct fuse_write_in *inarg = &req->misc.write.in;
722 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700723
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700724 inarg->fh = ff->fh;
725 inarg->offset = pos;
726 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700727 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200728 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700729 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200730 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700731 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
732 else
733 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700734 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700735 req->in.args[1].size = count;
736 req->out.numargs = 1;
737 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700738 req->out.args[0].value = outarg;
739}
740
741static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200742 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700743{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200744 struct fuse_file *ff = file->private_data;
745 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200746 struct fuse_write_in *inarg = &req->misc.write.in;
747
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200748 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200749 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700750 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700751 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
752 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
753 }
Tejun Heob93f8582008-11-26 12:03:55 +0100754 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700755 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700756}
757
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700758static int fuse_write_begin(struct file *file, struct address_space *mapping,
759 loff_t pos, unsigned len, unsigned flags,
760 struct page **pagep, void **fsdata)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700761{
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700762 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
763
Nick Piggin54566b22009-01-04 12:00:53 -0800764 *pagep = grab_cache_page_write_begin(mapping, index, flags);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700765 if (!*pagep)
766 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700767 return 0;
768}
769
Miklos Szeredia1d75f22010-07-12 14:41:40 +0200770void fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700771{
772 struct fuse_conn *fc = get_fuse_conn(inode);
773 struct fuse_inode *fi = get_fuse_inode(inode);
774
775 spin_lock(&fc->lock);
776 fi->attr_version = ++fc->attr_version;
777 if (pos > inode->i_size)
778 i_size_write(inode, pos);
779 spin_unlock(&fc->lock);
780}
781
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700782static int fuse_buffered_write(struct file *file, struct inode *inode,
783 loff_t pos, unsigned count, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700784{
785 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700786 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700787 struct fuse_conn *fc = get_fuse_conn(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700788 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800789 struct fuse_req *req;
790
791 if (is_bad_inode(inode))
792 return -EIO;
793
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700794 /*
795 * Make sure writepages on the same page are not mixed up with
796 * plain writes.
797 */
798 fuse_wait_on_page_writeback(inode, page->index);
799
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700800 req = fuse_get_req(fc);
801 if (IS_ERR(req))
802 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700803
Miklos Szeredif4975c62009-04-02 14:25:34 +0200804 req->in.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700805 req->num_pages = 1;
806 req->pages[0] = page;
807 req->page_offset = offset;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200808 nres = fuse_send_write(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700809 err = req->out.h.error;
810 fuse_put_request(fc, req);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700811 if (!err && !nres)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700812 err = -EIO;
813 if (!err) {
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700814 pos += nres;
Miklos Szeredi854512e2008-04-30 00:54:41 -0700815 fuse_write_update_size(inode, pos);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700816 if (count == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700817 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700818 }
819 fuse_invalidate_attr(inode);
Nick Piggin5e6f58a2007-10-16 01:25:17 -0700820 return err ? err : nres;
821}
822
823static int fuse_write_end(struct file *file, struct address_space *mapping,
824 loff_t pos, unsigned len, unsigned copied,
825 struct page *page, void *fsdata)
826{
827 struct inode *inode = mapping->host;
828 int res = 0;
829
830 if (copied)
831 res = fuse_buffered_write(file, inode, pos, copied, page);
832
833 unlock_page(page);
834 page_cache_release(page);
835 return res;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700836}
837
Nick Pigginea9b9902008-04-30 00:54:42 -0700838static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
839 struct inode *inode, loff_t pos,
840 size_t count)
841{
842 size_t res;
843 unsigned offset;
844 unsigned i;
845
846 for (i = 0; i < req->num_pages; i++)
847 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
848
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200849 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700850
851 offset = req->page_offset;
852 count = res;
853 for (i = 0; i < req->num_pages; i++) {
854 struct page *page = req->pages[i];
855
856 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
857 SetPageUptodate(page);
858
859 if (count > PAGE_CACHE_SIZE - offset)
860 count -= PAGE_CACHE_SIZE - offset;
861 else
862 count = 0;
863 offset = 0;
864
865 unlock_page(page);
866 page_cache_release(page);
867 }
868
869 return res;
870}
871
872static ssize_t fuse_fill_write_pages(struct fuse_req *req,
873 struct address_space *mapping,
874 struct iov_iter *ii, loff_t pos)
875{
876 struct fuse_conn *fc = get_fuse_conn(mapping->host);
877 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
878 size_t count = 0;
879 int err;
880
Miklos Szeredif4975c62009-04-02 14:25:34 +0200881 req->in.argpages = 1;
Nick Pigginea9b9902008-04-30 00:54:42 -0700882 req->page_offset = offset;
883
884 do {
885 size_t tmp;
886 struct page *page;
887 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
888 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
889 iov_iter_count(ii));
890
891 bytes = min_t(size_t, bytes, fc->max_write - count);
892
893 again:
894 err = -EFAULT;
895 if (iov_iter_fault_in_readable(ii, bytes))
896 break;
897
898 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800899 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700900 if (!page)
901 break;
902
anfei zhou931e80e2010-02-02 13:44:02 -0800903 if (mapping_writably_mapped(mapping))
904 flush_dcache_page(page);
905
Nick Pigginea9b9902008-04-30 00:54:42 -0700906 pagefault_disable();
907 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
908 pagefault_enable();
909 flush_dcache_page(page);
910
911 if (!tmp) {
912 unlock_page(page);
913 page_cache_release(page);
914 bytes = min(bytes, iov_iter_single_seg_count(ii));
915 goto again;
916 }
917
918 err = 0;
919 req->pages[req->num_pages] = page;
920 req->num_pages++;
921
922 iov_iter_advance(ii, tmp);
923 count += tmp;
924 pos += tmp;
925 offset += tmp;
926 if (offset == PAGE_CACHE_SIZE)
927 offset = 0;
928
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700929 if (!fc->big_writes)
930 break;
Nick Pigginea9b9902008-04-30 00:54:42 -0700931 } while (iov_iter_count(ii) && count < fc->max_write &&
932 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
933
934 return count > 0 ? count : err;
935}
936
937static ssize_t fuse_perform_write(struct file *file,
938 struct address_space *mapping,
939 struct iov_iter *ii, loff_t pos)
940{
941 struct inode *inode = mapping->host;
942 struct fuse_conn *fc = get_fuse_conn(inode);
943 int err = 0;
944 ssize_t res = 0;
945
946 if (is_bad_inode(inode))
947 return -EIO;
948
949 do {
950 struct fuse_req *req;
951 ssize_t count;
952
953 req = fuse_get_req(fc);
954 if (IS_ERR(req)) {
955 err = PTR_ERR(req);
956 break;
957 }
958
959 count = fuse_fill_write_pages(req, mapping, ii, pos);
960 if (count <= 0) {
961 err = count;
962 } else {
963 size_t num_written;
964
965 num_written = fuse_send_write_pages(req, file, inode,
966 pos, count);
967 err = req->out.h.error;
968 if (!err) {
969 res += num_written;
970 pos += num_written;
971
972 /* break out of the loop on short write */
973 if (num_written != count)
974 err = -EIO;
975 }
976 }
977 fuse_put_request(fc, req);
978 } while (!err && iov_iter_count(ii));
979
980 if (res > 0)
981 fuse_write_update_size(inode, pos);
982
983 fuse_invalidate_attr(inode);
984
985 return res > 0 ? res : err;
986}
987
988static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
989 unsigned long nr_segs, loff_t pos)
990{
991 struct file *file = iocb->ki_filp;
992 struct address_space *mapping = file->f_mapping;
993 size_t count = 0;
994 ssize_t written = 0;
995 struct inode *inode = mapping->host;
996 ssize_t err;
997 struct iov_iter i;
998
999 WARN_ON(iocb->ki_pos != pos);
1000
1001 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
1002 if (err)
1003 return err;
1004
1005 mutex_lock(&inode->i_mutex);
1006 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1007
1008 /* We can write back this queue in page reclaim */
1009 current->backing_dev_info = mapping->backing_dev_info;
1010
1011 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1012 if (err)
1013 goto out;
1014
1015 if (count == 0)
1016 goto out;
1017
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001018 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001019 if (err)
1020 goto out;
1021
1022 file_update_time(file);
1023
1024 iov_iter_init(&i, iov, nr_segs, count, 0);
1025 written = fuse_perform_write(file, mapping, &i, pos);
1026 if (written >= 0)
1027 iocb->ki_pos = pos + written;
1028
1029out:
1030 current->backing_dev_info = NULL;
1031 mutex_unlock(&inode->i_mutex);
1032
1033 return written ? written : err;
1034}
1035
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001036static void fuse_release_user_pages(struct fuse_req *req, int write)
1037{
1038 unsigned i;
1039
1040 for (i = 0; i < req->num_pages; i++) {
1041 struct page *page = req->pages[i];
1042 if (write)
1043 set_page_dirty_lock(page);
1044 put_page(page);
1045 }
1046}
1047
1048static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001049 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001050{
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001051 size_t nbytes = *nbytesp;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001052 unsigned long user_addr = (unsigned long) buf;
1053 unsigned offset = user_addr & ~PAGE_MASK;
1054 int npages;
1055
Miklos Szeredif4975c62009-04-02 14:25:34 +02001056 /* Special case for kernel I/O: can copy directly into the buffer */
1057 if (segment_eq(get_fs(), KERNEL_DS)) {
1058 if (write)
1059 req->in.args[1].value = (void *) user_addr;
1060 else
1061 req->out.args[0].value = (void *) user_addr;
1062
1063 return 0;
1064 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001065
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001066 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001067 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Harvey Harrisonbd730962008-05-01 04:35:15 -07001068 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi1bf94ca2010-05-25 15:06:06 +02001069 npages = get_user_pages_fast(user_addr, npages, !write, req->pages);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001070 if (npages < 0)
1071 return npages;
1072
1073 req->num_pages = npages;
1074 req->page_offset = offset;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001075
1076 if (write)
1077 req->in.argpages = 1;
1078 else
1079 req->out.argpages = 1;
1080
1081 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
1082 *nbytesp = min(*nbytesp, nbytes);
1083
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001084 return 0;
1085}
1086
Tejun Heo08cbf542009-04-14 10:54:53 +09001087ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1088 size_t count, loff_t *ppos, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001089{
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001090 struct fuse_file *ff = file->private_data;
1091 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001092 size_t nmax = write ? fc->max_write : fc->max_read;
1093 loff_t pos = *ppos;
1094 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001095 struct fuse_req *req;
1096
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001097 req = fuse_get_req(fc);
1098 if (IS_ERR(req))
1099 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001100
1101 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001102 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001103 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001104 size_t nbytes = min(count, nmax);
1105 int err = fuse_get_user_pages(req, buf, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001106 if (err) {
1107 res = err;
1108 break;
1109 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001110
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001111 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001112 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001113 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001114 nres = fuse_send_read(req, file, pos, nbytes, owner);
1115
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001116 fuse_release_user_pages(req, !write);
1117 if (req->out.h.error) {
1118 if (!res)
1119 res = req->out.h.error;
1120 break;
1121 } else if (nres > nbytes) {
1122 res = -EIO;
1123 break;
1124 }
1125 count -= nres;
1126 res += nres;
1127 pos += nres;
1128 buf += nres;
1129 if (nres != nbytes)
1130 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001131 if (count) {
1132 fuse_put_request(fc, req);
1133 req = fuse_get_req(fc);
1134 if (IS_ERR(req))
1135 break;
1136 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001137 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001138 if (!IS_ERR(req))
1139 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001140 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001141 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001142
1143 return res;
1144}
Tejun Heo08cbf542009-04-14 10:54:53 +09001145EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001146
1147static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1148 size_t count, loff_t *ppos)
1149{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001150 ssize_t res;
1151 struct inode *inode = file->f_path.dentry->d_inode;
1152
1153 if (is_bad_inode(inode))
1154 return -EIO;
1155
1156 res = fuse_direct_io(file, buf, count, ppos, 0);
1157
1158 fuse_invalidate_attr(inode);
1159
1160 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001161}
1162
1163static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001166 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001167 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001168
1169 if (is_bad_inode(inode))
1170 return -EIO;
1171
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001172 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001173 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -07001174 res = generic_write_checks(file, ppos, &count, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001175 if (!res) {
Miklos Szeredi889f7842007-05-23 13:57:54 -07001176 res = fuse_direct_io(file, buf, count, ppos, 1);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001177 if (res > 0)
1178 fuse_write_update_size(inode, *ppos);
1179 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001180 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001181
1182 fuse_invalidate_attr(inode);
1183
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001184 return res;
1185}
1186
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001187static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001188{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001189 __free_page(req->pages[0]);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +01001190 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001191}
1192
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001193static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001194{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001195 struct inode *inode = req->inode;
1196 struct fuse_inode *fi = get_fuse_inode(inode);
1197 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1198
1199 list_del(&req->writepages_entry);
1200 dec_bdi_stat(bdi, BDI_WRITEBACK);
1201 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1202 bdi_writeout_inc(bdi);
1203 wake_up(&fi->page_waitq);
1204}
1205
1206/* Called under fc->lock, may release and reacquire it */
1207static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001208__releases(fc->lock)
1209__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001210{
1211 struct fuse_inode *fi = get_fuse_inode(req->inode);
1212 loff_t size = i_size_read(req->inode);
1213 struct fuse_write_in *inarg = &req->misc.write.in;
1214
1215 if (!fc->connected)
1216 goto out_free;
1217
1218 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1219 inarg->size = PAGE_CACHE_SIZE;
1220 } else if (inarg->offset < size) {
1221 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1222 } else {
1223 /* Got truncated off completely */
1224 goto out_free;
1225 }
1226
1227 req->in.args[1].size = inarg->size;
1228 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001229 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001230 return;
1231
1232 out_free:
1233 fuse_writepage_finish(fc, req);
1234 spin_unlock(&fc->lock);
1235 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001236 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001237 spin_lock(&fc->lock);
1238}
1239
1240/*
1241 * If fi->writectr is positive (no truncate or fsync going on) send
1242 * all queued writepage requests.
1243 *
1244 * Called with fc->lock
1245 */
1246void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001247__releases(fc->lock)
1248__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001249{
1250 struct fuse_conn *fc = get_fuse_conn(inode);
1251 struct fuse_inode *fi = get_fuse_inode(inode);
1252 struct fuse_req *req;
1253
1254 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1255 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1256 list_del_init(&req->list);
1257 fuse_send_writepage(fc, req);
1258 }
1259}
1260
1261static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1262{
1263 struct inode *inode = req->inode;
1264 struct fuse_inode *fi = get_fuse_inode(inode);
1265
1266 mapping_set_error(inode->i_mapping, req->out.h.error);
1267 spin_lock(&fc->lock);
1268 fi->writectr--;
1269 fuse_writepage_finish(fc, req);
1270 spin_unlock(&fc->lock);
1271 fuse_writepage_free(fc, req);
1272}
1273
1274static int fuse_writepage_locked(struct page *page)
1275{
1276 struct address_space *mapping = page->mapping;
1277 struct inode *inode = mapping->host;
1278 struct fuse_conn *fc = get_fuse_conn(inode);
1279 struct fuse_inode *fi = get_fuse_inode(inode);
1280 struct fuse_req *req;
1281 struct fuse_file *ff;
1282 struct page *tmp_page;
1283
1284 set_page_writeback(page);
1285
1286 req = fuse_request_alloc_nofs();
1287 if (!req)
1288 goto err;
1289
1290 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1291 if (!tmp_page)
1292 goto err_free;
1293
1294 spin_lock(&fc->lock);
1295 BUG_ON(list_empty(&fi->write_files));
1296 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1297 req->ff = fuse_file_get(ff);
1298 spin_unlock(&fc->lock);
1299
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001300 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001301
1302 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001303 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001304 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001305 req->num_pages = 1;
1306 req->pages[0] = tmp_page;
1307 req->page_offset = 0;
1308 req->end = fuse_writepage_end;
1309 req->inode = inode;
1310
1311 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1312 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1313 end_page_writeback(page);
1314
1315 spin_lock(&fc->lock);
1316 list_add(&req->writepages_entry, &fi->writepages);
1317 list_add_tail(&req->list, &fi->queued_writes);
1318 fuse_flush_writepages(inode);
1319 spin_unlock(&fc->lock);
1320
1321 return 0;
1322
1323err_free:
1324 fuse_request_free(req);
1325err:
1326 end_page_writeback(page);
1327 return -ENOMEM;
1328}
1329
1330static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1331{
1332 int err;
1333
1334 err = fuse_writepage_locked(page);
1335 unlock_page(page);
1336
1337 return err;
1338}
1339
1340static int fuse_launder_page(struct page *page)
1341{
1342 int err = 0;
1343 if (clear_page_dirty_for_io(page)) {
1344 struct inode *inode = page->mapping->host;
1345 err = fuse_writepage_locked(page);
1346 if (!err)
1347 fuse_wait_on_page_writeback(inode, page->index);
1348 }
1349 return err;
1350}
1351
1352/*
1353 * Write back dirty pages now, because there may not be any suitable
1354 * open files later
1355 */
1356static void fuse_vma_close(struct vm_area_struct *vma)
1357{
1358 filemap_write_and_wait(vma->vm_file->f_mapping);
1359}
1360
1361/*
1362 * Wait for writeback against this page to complete before allowing it
1363 * to be marked dirty again, and hence written back again, possibly
1364 * before the previous writepage completed.
1365 *
1366 * Block here, instead of in ->writepage(), so that the userspace fs
1367 * can only block processes actually operating on the filesystem.
1368 *
1369 * Otherwise unprivileged userspace fs would be able to block
1370 * unrelated:
1371 *
1372 * - page migration
1373 * - sync(2)
1374 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1375 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001376static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001377{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001378 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001379 /*
1380 * Don't use page->mapping as it may become NULL from a
1381 * concurrent truncate.
1382 */
1383 struct inode *inode = vma->vm_file->f_mapping->host;
1384
1385 fuse_wait_on_page_writeback(inode, page->index);
1386 return 0;
1387}
1388
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001389static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001390 .close = fuse_vma_close,
1391 .fault = filemap_fault,
1392 .page_mkwrite = fuse_page_mkwrite,
1393};
1394
1395static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1396{
1397 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1398 struct inode *inode = file->f_dentry->d_inode;
1399 struct fuse_conn *fc = get_fuse_conn(inode);
1400 struct fuse_inode *fi = get_fuse_inode(inode);
1401 struct fuse_file *ff = file->private_data;
1402 /*
1403 * file may be written through mmap, so chain it onto the
1404 * inodes's write_file list
1405 */
1406 spin_lock(&fc->lock);
1407 if (list_empty(&ff->write_entry))
1408 list_add(&ff->write_entry, &fi->write_files);
1409 spin_unlock(&fc->lock);
1410 }
1411 file_accessed(file);
1412 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001413 return 0;
1414}
1415
Miklos Szeredifc280c92009-04-02 14:25:35 +02001416static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1417{
1418 /* Can't provide the coherency needed for MAP_SHARED */
1419 if (vma->vm_flags & VM_MAYSHARE)
1420 return -ENODEV;
1421
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001422 invalidate_inode_pages2(file->f_mapping);
1423
Miklos Szeredifc280c92009-04-02 14:25:35 +02001424 return generic_file_mmap(file, vma);
1425}
1426
Miklos Szeredi71421252006-06-25 05:48:52 -07001427static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1428 struct file_lock *fl)
1429{
1430 switch (ffl->type) {
1431 case F_UNLCK:
1432 break;
1433
1434 case F_RDLCK:
1435 case F_WRLCK:
1436 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1437 ffl->end < ffl->start)
1438 return -EIO;
1439
1440 fl->fl_start = ffl->start;
1441 fl->fl_end = ffl->end;
1442 fl->fl_pid = ffl->pid;
1443 break;
1444
1445 default:
1446 return -EIO;
1447 }
1448 fl->fl_type = ffl->type;
1449 return 0;
1450}
1451
1452static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001453 const struct file_lock *fl, int opcode, pid_t pid,
1454 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001455{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001456 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001457 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001458 struct fuse_file *ff = file->private_data;
1459 struct fuse_lk_in *arg = &req->misc.lk_in;
1460
1461 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001462 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001463 arg->lk.start = fl->fl_start;
1464 arg->lk.end = fl->fl_end;
1465 arg->lk.type = fl->fl_type;
1466 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001467 if (flock)
1468 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001469 req->in.h.opcode = opcode;
1470 req->in.h.nodeid = get_node_id(inode);
1471 req->in.numargs = 1;
1472 req->in.args[0].size = sizeof(*arg);
1473 req->in.args[0].value = arg;
1474}
1475
1476static int fuse_getlk(struct file *file, struct file_lock *fl)
1477{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001478 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001479 struct fuse_conn *fc = get_fuse_conn(inode);
1480 struct fuse_req *req;
1481 struct fuse_lk_out outarg;
1482 int err;
1483
1484 req = fuse_get_req(fc);
1485 if (IS_ERR(req))
1486 return PTR_ERR(req);
1487
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001488 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001489 req->out.numargs = 1;
1490 req->out.args[0].size = sizeof(outarg);
1491 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001492 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001493 err = req->out.h.error;
1494 fuse_put_request(fc, req);
1495 if (!err)
1496 err = convert_fuse_file_lock(&outarg.lk, fl);
1497
1498 return err;
1499}
1500
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001501static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001502{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001503 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001504 struct fuse_conn *fc = get_fuse_conn(inode);
1505 struct fuse_req *req;
1506 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1507 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1508 int err;
1509
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04001510 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07001511 /* NLM needs asynchronous locks, which we don't support yet */
1512 return -ENOLCK;
1513 }
1514
Miklos Szeredi71421252006-06-25 05:48:52 -07001515 /* Unlock on close is handled by the flush method */
1516 if (fl->fl_flags & FL_CLOSE)
1517 return 0;
1518
1519 req = fuse_get_req(fc);
1520 if (IS_ERR(req))
1521 return PTR_ERR(req);
1522
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001523 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001524 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001525 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001526 /* locking is restartable */
1527 if (err == -EINTR)
1528 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001529 fuse_put_request(fc, req);
1530 return err;
1531}
1532
1533static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1534{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001535 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001536 struct fuse_conn *fc = get_fuse_conn(inode);
1537 int err;
1538
Miklos Szeredi48e90762008-07-25 01:49:02 -07001539 if (cmd == F_CANCELLK) {
1540 err = 0;
1541 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001542 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001543 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001544 err = 0;
1545 } else
1546 err = fuse_getlk(file, fl);
1547 } else {
1548 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001549 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001550 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001551 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001552 }
1553 return err;
1554}
1555
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001556static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1557{
1558 struct inode *inode = file->f_path.dentry->d_inode;
1559 struct fuse_conn *fc = get_fuse_conn(inode);
1560 int err;
1561
1562 if (fc->no_lock) {
1563 err = flock_lock_file_wait(file, fl);
1564 } else {
1565 /* emulate flock with POSIX locks */
1566 fl->fl_owner = (fl_owner_t) file;
1567 err = fuse_setlk(file, fl, 1);
1568 }
1569
1570 return err;
1571}
1572
Miklos Szeredib2d22722006-12-06 20:35:51 -08001573static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1574{
1575 struct inode *inode = mapping->host;
1576 struct fuse_conn *fc = get_fuse_conn(inode);
1577 struct fuse_req *req;
1578 struct fuse_bmap_in inarg;
1579 struct fuse_bmap_out outarg;
1580 int err;
1581
1582 if (!inode->i_sb->s_bdev || fc->no_bmap)
1583 return 0;
1584
1585 req = fuse_get_req(fc);
1586 if (IS_ERR(req))
1587 return 0;
1588
1589 memset(&inarg, 0, sizeof(inarg));
1590 inarg.block = block;
1591 inarg.blocksize = inode->i_sb->s_blocksize;
1592 req->in.h.opcode = FUSE_BMAP;
1593 req->in.h.nodeid = get_node_id(inode);
1594 req->in.numargs = 1;
1595 req->in.args[0].size = sizeof(inarg);
1596 req->in.args[0].value = &inarg;
1597 req->out.numargs = 1;
1598 req->out.args[0].size = sizeof(outarg);
1599 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001600 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001601 err = req->out.h.error;
1602 fuse_put_request(fc, req);
1603 if (err == -ENOSYS)
1604 fc->no_bmap = 1;
1605
1606 return err ? 0 : outarg.block;
1607}
1608
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001609static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1610{
1611 loff_t retval;
1612 struct inode *inode = file->f_path.dentry->d_inode;
1613
1614 mutex_lock(&inode->i_mutex);
Josef Bacik06222e42011-07-18 13:21:38 -04001615 if (origin != SEEK_CUR || origin != SEEK_SET) {
Miklos Szeredi769415c2008-10-16 16:08:56 +02001616 retval = fuse_update_attributes(inode, NULL, file, NULL);
1617 if (retval)
Dan Carpenter52916582009-03-27 13:36:10 +03001618 goto exit;
Josef Bacik06222e42011-07-18 13:21:38 -04001619 }
1620
1621 switch (origin) {
1622 case SEEK_END:
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001623 offset += i_size_read(inode);
1624 break;
1625 case SEEK_CUR:
1626 offset += file->f_pos;
Josef Bacik06222e42011-07-18 13:21:38 -04001627 break;
1628 case SEEK_DATA:
1629 if (offset >= i_size_read(inode)) {
1630 retval = -ENXIO;
1631 goto exit;
1632 }
1633 break;
1634 case SEEK_HOLE:
1635 if (offset >= i_size_read(inode)) {
1636 retval = -ENXIO;
1637 goto exit;
1638 }
1639 offset = i_size_read(inode);
1640 break;
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001641 }
1642 retval = -EINVAL;
1643 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1644 if (offset != file->f_pos) {
1645 file->f_pos = offset;
1646 file->f_version = 0;
1647 }
1648 retval = offset;
1649 }
Dan Carpenter52916582009-03-27 13:36:10 +03001650exit:
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001651 mutex_unlock(&inode->i_mutex);
1652 return retval;
1653}
1654
Tejun Heo59efec72008-11-26 12:03:55 +01001655static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1656 unsigned int nr_segs, size_t bytes, bool to_user)
1657{
1658 struct iov_iter ii;
1659 int page_idx = 0;
1660
1661 if (!bytes)
1662 return 0;
1663
1664 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1665
1666 while (iov_iter_count(&ii)) {
1667 struct page *page = pages[page_idx++];
1668 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001669 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001670
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001671 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001672
1673 while (todo) {
1674 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1675 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1676 size_t copy = min(todo, iov_len);
1677 size_t left;
1678
1679 if (!to_user)
1680 left = copy_from_user(kaddr, uaddr, copy);
1681 else
1682 left = copy_to_user(uaddr, kaddr, copy);
1683
1684 if (unlikely(left))
1685 return -EFAULT;
1686
1687 iov_iter_advance(&ii, copy);
1688 todo -= copy;
1689 kaddr += copy;
1690 }
1691
Jens Axboe0bd87182009-11-03 11:40:44 +01001692 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001693 }
1694
1695 return 0;
1696}
1697
1698/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001699 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1700 * ABI was defined to be 'struct iovec' which is different on 32bit
1701 * and 64bit. Fortunately we can determine which structure the server
1702 * used from the size of the reply.
1703 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001704static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1705 size_t transferred, unsigned count,
1706 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001707{
1708#ifdef CONFIG_COMPAT
1709 if (count * sizeof(struct compat_iovec) == transferred) {
1710 struct compat_iovec *ciov = src;
1711 unsigned i;
1712
1713 /*
1714 * With this interface a 32bit server cannot support
1715 * non-compat (i.e. ones coming from 64bit apps) ioctl
1716 * requests
1717 */
1718 if (!is_compat)
1719 return -EINVAL;
1720
1721 for (i = 0; i < count; i++) {
1722 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1723 dst[i].iov_len = ciov[i].iov_len;
1724 }
1725 return 0;
1726 }
1727#endif
1728
1729 if (count * sizeof(struct iovec) != transferred)
1730 return -EIO;
1731
1732 memcpy(dst, src, transferred);
1733 return 0;
1734}
1735
Miklos Szeredi75727772010-11-30 16:39:27 +01001736/* Make sure iov_length() won't overflow */
1737static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1738{
1739 size_t n;
1740 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1741
1742 for (n = 0; n < count; n++) {
1743 if (iov->iov_len > (size_t) max)
1744 return -ENOMEM;
1745 max -= iov->iov_len;
1746 }
1747 return 0;
1748}
1749
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001750static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1751 void *src, size_t transferred, unsigned count,
1752 bool is_compat)
1753{
1754 unsigned i;
1755 struct fuse_ioctl_iovec *fiov = src;
1756
1757 if (fc->minor < 16) {
1758 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1759 count, is_compat);
1760 }
1761
1762 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1763 return -EIO;
1764
1765 for (i = 0; i < count; i++) {
1766 /* Did the server supply an inappropriate value? */
1767 if (fiov[i].base != (unsigned long) fiov[i].base ||
1768 fiov[i].len != (unsigned long) fiov[i].len)
1769 return -EIO;
1770
1771 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1772 dst[i].iov_len = (size_t) fiov[i].len;
1773
1774#ifdef CONFIG_COMPAT
1775 if (is_compat &&
1776 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1777 (compat_size_t) dst[i].iov_len != fiov[i].len))
1778 return -EIO;
1779#endif
1780 }
1781
1782 return 0;
1783}
1784
1785
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001786/*
Tejun Heo59efec72008-11-26 12:03:55 +01001787 * For ioctls, there is no generic way to determine how much memory
1788 * needs to be read and/or written. Furthermore, ioctls are allowed
1789 * to dereference the passed pointer, so the parameter requires deep
1790 * copying but FUSE has no idea whatsoever about what to copy in or
1791 * out.
1792 *
1793 * This is solved by allowing FUSE server to retry ioctl with
1794 * necessary in/out iovecs. Let's assume the ioctl implementation
1795 * needs to read in the following structure.
1796 *
1797 * struct a {
1798 * char *buf;
1799 * size_t buflen;
1800 * }
1801 *
1802 * On the first callout to FUSE server, inarg->in_size and
1803 * inarg->out_size will be NULL; then, the server completes the ioctl
1804 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1805 * the actual iov array to
1806 *
1807 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1808 *
1809 * which tells FUSE to copy in the requested area and retry the ioctl.
1810 * On the second round, the server has access to the structure and
1811 * from that it can tell what to look for next, so on the invocation,
1812 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1813 *
1814 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1815 * { .iov_base = a.buf, .iov_len = a.buflen } }
1816 *
1817 * FUSE will copy both struct a and the pointed buffer from the
1818 * process doing the ioctl and retry ioctl with both struct a and the
1819 * buffer.
1820 *
1821 * This time, FUSE server has everything it needs and completes ioctl
1822 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1823 *
1824 * Copying data out works the same way.
1825 *
1826 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1827 * automatically initializes in and out iovs by decoding @cmd with
1828 * _IOC_* macros and the server is not allowed to request RETRY. This
1829 * limits ioctl data transfers to well-formed ioctls and is the forced
1830 * behavior for all FUSE servers.
1831 */
Tejun Heo08cbf542009-04-14 10:54:53 +09001832long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1833 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01001834{
Tejun Heo59efec72008-11-26 12:03:55 +01001835 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001836 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01001837 struct fuse_ioctl_in inarg = {
1838 .fh = ff->fh,
1839 .cmd = cmd,
1840 .arg = arg,
1841 .flags = flags
1842 };
1843 struct fuse_ioctl_out outarg;
1844 struct fuse_req *req = NULL;
1845 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001846 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01001847 struct iovec *in_iov = NULL, *out_iov = NULL;
1848 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1849 size_t in_size, out_size, transferred;
1850 int err;
1851
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001852#if BITS_PER_LONG == 32
1853 inarg.flags |= FUSE_IOCTL_32BIT;
1854#else
1855 if (flags & FUSE_IOCTL_COMPAT)
1856 inarg.flags |= FUSE_IOCTL_32BIT;
1857#endif
1858
Tejun Heo59efec72008-11-26 12:03:55 +01001859 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001860 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01001861
Tejun Heo59efec72008-11-26 12:03:55 +01001862 err = -ENOMEM;
1863 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001864 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01001865 if (!pages || !iov_page)
1866 goto out;
1867
1868 /*
1869 * If restricted, initialize IO parameters as encoded in @cmd.
1870 * RETRY from server is not allowed.
1871 */
1872 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001873 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01001874
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01001875 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01001876 iov->iov_len = _IOC_SIZE(cmd);
1877
1878 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1879 in_iov = iov;
1880 in_iovs = 1;
1881 }
1882
1883 if (_IOC_DIR(cmd) & _IOC_READ) {
1884 out_iov = iov;
1885 out_iovs = 1;
1886 }
1887 }
1888
1889 retry:
1890 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1891 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1892
1893 /*
1894 * Out data can be used either for actual out data or iovs,
1895 * make sure there always is at least one page.
1896 */
1897 out_size = max_t(size_t, out_size, PAGE_SIZE);
1898 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1899
1900 /* make sure there are enough buffer pages and init request with them */
1901 err = -ENOMEM;
1902 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1903 goto out;
1904 while (num_pages < max_pages) {
1905 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1906 if (!pages[num_pages])
1907 goto out;
1908 num_pages++;
1909 }
1910
1911 req = fuse_get_req(fc);
1912 if (IS_ERR(req)) {
1913 err = PTR_ERR(req);
1914 req = NULL;
1915 goto out;
1916 }
1917 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1918 req->num_pages = num_pages;
1919
1920 /* okay, let's send it to the client */
1921 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001922 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01001923 req->in.numargs = 1;
1924 req->in.args[0].size = sizeof(inarg);
1925 req->in.args[0].value = &inarg;
1926 if (in_size) {
1927 req->in.numargs++;
1928 req->in.args[1].size = in_size;
1929 req->in.argpages = 1;
1930
1931 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1932 false);
1933 if (err)
1934 goto out;
1935 }
1936
1937 req->out.numargs = 2;
1938 req->out.args[0].size = sizeof(outarg);
1939 req->out.args[0].value = &outarg;
1940 req->out.args[1].size = out_size;
1941 req->out.argpages = 1;
1942 req->out.argvar = 1;
1943
Tejun Heob93f8582008-11-26 12:03:55 +01001944 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01001945 err = req->out.h.error;
1946 transferred = req->out.args[1].size;
1947 fuse_put_request(fc, req);
1948 req = NULL;
1949 if (err)
1950 goto out;
1951
1952 /* did it ask for retry? */
1953 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001954 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001955
1956 /* no retry if in restricted mode */
1957 err = -EIO;
1958 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1959 goto out;
1960
1961 in_iovs = outarg.in_iovs;
1962 out_iovs = outarg.out_iovs;
1963
1964 /*
1965 * Make sure things are in boundary, separate checks
1966 * are to protect against overflow.
1967 */
1968 err = -ENOMEM;
1969 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1970 out_iovs > FUSE_IOCTL_MAX_IOV ||
1971 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1972 goto out;
1973
Tejun Heo59efec72008-11-26 12:03:55 +01001974 vaddr = kmap_atomic(pages[0], KM_USER0);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001975 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001976 transferred, in_iovs + out_iovs,
1977 (flags & FUSE_IOCTL_COMPAT) != 0);
Tejun Heo59efec72008-11-26 12:03:55 +01001978 kunmap_atomic(vaddr, KM_USER0);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001979 if (err)
1980 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01001981
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001982 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01001983 out_iov = in_iov + in_iovs;
1984
Miklos Szeredi75727772010-11-30 16:39:27 +01001985 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
1986 if (err)
1987 goto out;
1988
1989 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
1990 if (err)
1991 goto out;
1992
Tejun Heo59efec72008-11-26 12:03:55 +01001993 goto retry;
1994 }
1995
1996 err = -EIO;
1997 if (transferred > inarg.out_size)
1998 goto out;
1999
2000 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2001 out:
2002 if (req)
2003 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002004 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002005 while (num_pages)
2006 __free_page(pages[--num_pages]);
2007 kfree(pages);
2008
2009 return err ? err : outarg.result;
2010}
Tejun Heo08cbf542009-04-14 10:54:53 +09002011EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002012
Miklos Szeredid36f2482009-04-28 16:56:39 +02002013static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
2014 unsigned long arg, unsigned int flags)
2015{
2016 struct inode *inode = file->f_dentry->d_inode;
2017 struct fuse_conn *fc = get_fuse_conn(inode);
2018
2019 if (!fuse_allow_task(fc, current))
2020 return -EACCES;
2021
2022 if (is_bad_inode(inode))
2023 return -EIO;
2024
2025 return fuse_do_ioctl(file, cmd, arg, flags);
2026}
2027
Tejun Heo59efec72008-11-26 12:03:55 +01002028static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2029 unsigned long arg)
2030{
Miklos Szeredid36f2482009-04-28 16:56:39 +02002031 return fuse_file_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002032}
2033
2034static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2035 unsigned long arg)
2036{
Miklos Szeredid36f2482009-04-28 16:56:39 +02002037 return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002038}
2039
Tejun Heo95668a62008-11-26 12:03:55 +01002040/*
2041 * All files which have been polled are linked to RB tree
2042 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2043 * find the matching one.
2044 */
2045static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2046 struct rb_node **parent_out)
2047{
2048 struct rb_node **link = &fc->polled_files.rb_node;
2049 struct rb_node *last = NULL;
2050
2051 while (*link) {
2052 struct fuse_file *ff;
2053
2054 last = *link;
2055 ff = rb_entry(last, struct fuse_file, polled_node);
2056
2057 if (kh < ff->kh)
2058 link = &last->rb_left;
2059 else if (kh > ff->kh)
2060 link = &last->rb_right;
2061 else
2062 return link;
2063 }
2064
2065 if (parent_out)
2066 *parent_out = last;
2067 return link;
2068}
2069
2070/*
2071 * The file is about to be polled. Make sure it's on the polled_files
2072 * RB tree. Note that files once added to the polled_files tree are
2073 * not removed before the file is released. This is because a file
2074 * polled once is likely to be polled again.
2075 */
2076static void fuse_register_polled_file(struct fuse_conn *fc,
2077 struct fuse_file *ff)
2078{
2079 spin_lock(&fc->lock);
2080 if (RB_EMPTY_NODE(&ff->polled_node)) {
2081 struct rb_node **link, *parent;
2082
2083 link = fuse_find_polled_node(fc, ff->kh, &parent);
2084 BUG_ON(*link);
2085 rb_link_node(&ff->polled_node, parent, link);
2086 rb_insert_color(&ff->polled_node, &fc->polled_files);
2087 }
2088 spin_unlock(&fc->lock);
2089}
2090
Tejun Heo08cbf542009-04-14 10:54:53 +09002091unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002092{
Tejun Heo95668a62008-11-26 12:03:55 +01002093 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002094 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002095 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2096 struct fuse_poll_out outarg;
2097 struct fuse_req *req;
2098 int err;
2099
2100 if (fc->no_poll)
2101 return DEFAULT_POLLMASK;
2102
2103 poll_wait(file, &ff->poll_wait, wait);
2104
2105 /*
2106 * Ask for notification iff there's someone waiting for it.
2107 * The client may ignore the flag and always notify.
2108 */
2109 if (waitqueue_active(&ff->poll_wait)) {
2110 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2111 fuse_register_polled_file(fc, ff);
2112 }
2113
2114 req = fuse_get_req(fc);
2115 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002116 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002117
2118 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002119 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002120 req->in.numargs = 1;
2121 req->in.args[0].size = sizeof(inarg);
2122 req->in.args[0].value = &inarg;
2123 req->out.numargs = 1;
2124 req->out.args[0].size = sizeof(outarg);
2125 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002126 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002127 err = req->out.h.error;
2128 fuse_put_request(fc, req);
2129
2130 if (!err)
2131 return outarg.revents;
2132 if (err == -ENOSYS) {
2133 fc->no_poll = 1;
2134 return DEFAULT_POLLMASK;
2135 }
2136 return POLLERR;
2137}
Tejun Heo08cbf542009-04-14 10:54:53 +09002138EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002139
2140/*
2141 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2142 * wakes up the poll waiters.
2143 */
2144int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2145 struct fuse_notify_poll_wakeup_out *outarg)
2146{
2147 u64 kh = outarg->kh;
2148 struct rb_node **link;
2149
2150 spin_lock(&fc->lock);
2151
2152 link = fuse_find_polled_node(fc, kh, NULL);
2153 if (*link) {
2154 struct fuse_file *ff;
2155
2156 ff = rb_entry(*link, struct fuse_file, polled_node);
2157 wake_up_interruptible_sync(&ff->poll_wait);
2158 }
2159
2160 spin_unlock(&fc->lock);
2161 return 0;
2162}
2163
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002164static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002165 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002166 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002167 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002168 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002169 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002170 .mmap = fuse_file_mmap,
2171 .open = fuse_open,
2172 .flush = fuse_flush,
2173 .release = fuse_release,
2174 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002175 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002176 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002177 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002178 .unlocked_ioctl = fuse_file_ioctl,
2179 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002180 .poll = fuse_file_poll,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002181};
2182
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002183static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002184 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002185 .read = fuse_direct_read,
2186 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002187 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002188 .open = fuse_open,
2189 .flush = fuse_flush,
2190 .release = fuse_release,
2191 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002192 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002193 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002194 .unlocked_ioctl = fuse_file_ioctl,
2195 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002196 .poll = fuse_file_poll,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002197 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002198};
2199
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002200static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002201 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002202 .writepage = fuse_writepage,
2203 .launder_page = fuse_launder_page,
Nick Piggin5e6f58a2007-10-16 01:25:17 -07002204 .write_begin = fuse_write_begin,
2205 .write_end = fuse_write_end,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002206 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002207 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002208 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002209};
2210
2211void fuse_init_file_inode(struct inode *inode)
2212{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002213 inode->i_fop = &fuse_file_operations;
2214 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002215}