blob: ab5b84ef43549d3a5aa05f95204feb7f0dfab636 [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
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200248 if (ff->flock) {
249 struct fuse_release_in *inarg = &req->misc.release.in;
250 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
251 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
252 (fl_owner_t) file);
253 }
Tejun Heo6b2db282009-04-14 10:54:49 +0900254 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200255 path_get(&file->f_path);
256 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700257
Tejun Heo6b2db282009-04-14 10:54:49 +0900258 /*
259 * Normally this will send the RELEASE request, however if
260 * some asynchronous READ or WRITE requests are outstanding,
261 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100262 *
263 * Make the release synchronous if this is a fuseblk mount,
264 * synchronous RELEASE is allowed (and desirable) in this case
265 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900266 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100267 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700268}
269
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700270static int fuse_open(struct inode *inode, struct file *file)
271{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200272 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700273}
274
275static int fuse_release(struct inode *inode, struct file *file)
276{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200277 fuse_release_common(file, FUSE_RELEASE);
278
279 /* return value is ignored by VFS */
280 return 0;
281}
282
283void fuse_sync_release(struct fuse_file *ff, int flags)
284{
285 WARN_ON(atomic_read(&ff->count) > 1);
286 fuse_prepare_release(ff, flags, FUSE_RELEASE);
287 ff->reserved_req->force = 1;
288 fuse_request_send(ff->fc, ff->reserved_req);
289 fuse_put_request(ff->fc, ff->reserved_req);
290 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700291}
Tejun Heo08cbf542009-04-14 10:54:53 +0900292EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700293
Miklos Szeredi71421252006-06-25 05:48:52 -0700294/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700295 * Scramble the ID space with XTEA, so that the value of the files_struct
296 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700297 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700298u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700299{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700300 u32 *k = fc->scramble_key;
301 u64 v = (unsigned long) id;
302 u32 v0 = v;
303 u32 v1 = v >> 32;
304 u32 sum = 0;
305 int i;
306
307 for (i = 0; i < 32; i++) {
308 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
309 sum += 0x9E3779B9;
310 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
311 }
312
313 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700314}
315
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700316/*
317 * Check if page is under writeback
318 *
319 * This is currently done by walking the list of writepage requests
320 * for the inode, which can be pretty inefficient.
321 */
322static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
323{
324 struct fuse_conn *fc = get_fuse_conn(inode);
325 struct fuse_inode *fi = get_fuse_inode(inode);
326 struct fuse_req *req;
327 bool found = false;
328
329 spin_lock(&fc->lock);
330 list_for_each_entry(req, &fi->writepages, writepages_entry) {
331 pgoff_t curr_index;
332
333 BUG_ON(req->inode != inode);
334 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
335 if (curr_index == index) {
336 found = true;
337 break;
338 }
339 }
340 spin_unlock(&fc->lock);
341
342 return found;
343}
344
345/*
346 * Wait for page writeback to be completed.
347 *
348 * Since fuse doesn't rely on the VM writeback tracking, this has to
349 * use some other means.
350 */
351static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
352{
353 struct fuse_inode *fi = get_fuse_inode(inode);
354
355 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
356 return 0;
357}
358
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700359static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700360{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800361 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700362 struct fuse_conn *fc = get_fuse_conn(inode);
363 struct fuse_file *ff = file->private_data;
364 struct fuse_req *req;
365 struct fuse_flush_in inarg;
366 int err;
367
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800368 if (is_bad_inode(inode))
369 return -EIO;
370
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700371 if (fc->no_flush)
372 return 0;
373
Miklos Szeredi33649c92006-06-25 05:48:52 -0700374 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700375 memset(&inarg, 0, sizeof(inarg));
376 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700377 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700378 req->in.h.opcode = FUSE_FLUSH;
379 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700380 req->in.numargs = 1;
381 req->in.args[0].size = sizeof(inarg);
382 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700383 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100384 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700385 err = req->out.h.error;
386 fuse_put_request(fc, req);
387 if (err == -ENOSYS) {
388 fc->no_flush = 1;
389 err = 0;
390 }
391 return err;
392}
393
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700394/*
395 * Wait for all pending writepages on the inode to finish.
396 *
397 * This is currently done by blocking further writes with FUSE_NOWRITE
398 * and waiting for all sent writes to complete.
399 *
400 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
401 * could conflict with truncation.
402 */
403static void fuse_sync_writes(struct inode *inode)
404{
405 fuse_set_nowrite(inode);
406 fuse_release_nowrite(inode);
407}
408
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200409int fuse_fsync_common(struct file *file, int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700410{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200411 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700412 struct fuse_conn *fc = get_fuse_conn(inode);
413 struct fuse_file *ff = file->private_data;
414 struct fuse_req *req;
415 struct fuse_fsync_in inarg;
416 int err;
417
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800418 if (is_bad_inode(inode))
419 return -EIO;
420
Miklos Szeredi82547982005-09-09 13:10:38 -0700421 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700422 return 0;
423
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700424 /*
425 * Start writeback against all dirty pages of the inode, then
426 * wait for all outstanding writes, before sending the FSYNC
427 * request.
428 */
429 err = write_inode_now(inode, 0);
430 if (err)
431 return err;
432
433 fuse_sync_writes(inode);
434
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700435 req = fuse_get_req(fc);
436 if (IS_ERR(req))
437 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700438
439 memset(&inarg, 0, sizeof(inarg));
440 inarg.fh = ff->fh;
441 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700442 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700443 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444 req->in.numargs = 1;
445 req->in.args[0].size = sizeof(inarg);
446 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100447 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700448 err = req->out.h.error;
449 fuse_put_request(fc, req);
450 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700451 if (isdir)
452 fc->no_fsyncdir = 1;
453 else
454 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700455 err = 0;
456 }
457 return err;
458}
459
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200460static int fuse_fsync(struct file *file, int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700461{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200462 return fuse_fsync_common(file, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700463}
464
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200465void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
466 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700467{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700468 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800469 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700470
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800471 inarg->fh = ff->fh;
472 inarg->offset = pos;
473 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800474 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800475 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200476 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700477 req->in.numargs = 1;
478 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800479 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700480 req->out.argvar = 1;
481 req->out.numargs = 1;
482 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700483}
484
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800485static size_t fuse_send_read(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200486 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700487{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200488 struct fuse_file *ff = file->private_data;
489 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700490
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200491 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700492 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700493 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700494
495 inarg->read_flags |= FUSE_READ_LOCKOWNER;
496 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
497 }
Tejun Heob93f8582008-11-26 12:03:55 +0100498 fuse_request_send(fc, req);
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800499 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700500}
501
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700502static void fuse_read_update_size(struct inode *inode, loff_t size,
503 u64 attr_ver)
504{
505 struct fuse_conn *fc = get_fuse_conn(inode);
506 struct fuse_inode *fi = get_fuse_inode(inode);
507
508 spin_lock(&fc->lock);
509 if (attr_ver == fi->attr_version && size < inode->i_size) {
510 fi->attr_version = ++fc->attr_version;
511 i_size_write(inode, size);
512 }
513 spin_unlock(&fc->lock);
514}
515
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700516static int fuse_readpage(struct file *file, struct page *page)
517{
518 struct inode *inode = page->mapping->host;
519 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800520 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700521 size_t num_read;
522 loff_t pos = page_offset(page);
523 size_t count = PAGE_CACHE_SIZE;
524 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800525 int err;
526
527 err = -EIO;
528 if (is_bad_inode(inode))
529 goto out;
530
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700531 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300532 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700533 * page-cache page, so make sure we read a properly synced
534 * page.
535 */
536 fuse_wait_on_page_writeback(inode, page->index);
537
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700538 req = fuse_get_req(fc);
539 err = PTR_ERR(req);
540 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700541 goto out;
542
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700543 attr_ver = fuse_get_attr_version(fc);
544
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700545 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200546 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700547 req->num_pages = 1;
548 req->pages[0] = page;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200549 num_read = fuse_send_read(req, file, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700550 err = req->out.h.error;
551 fuse_put_request(fc, req);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700552
553 if (!err) {
554 /*
555 * Short read means EOF. If file size is larger, truncate it
556 */
557 if (num_read < count)
558 fuse_read_update_size(inode, pos + num_read, attr_ver);
559
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700560 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700561 }
562
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700563 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700564 out:
565 unlock_page(page);
566 return err;
567}
568
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800569static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700570{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800571 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700572 size_t count = req->misc.read.in.size;
573 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200574 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800575
Miklos Szeredice534fb2010-05-25 15:06:07 +0200576 for (i = 0; mapping == NULL && i < req->num_pages; i++)
577 mapping = req->pages[i]->mapping;
578
579 if (mapping) {
580 struct inode *inode = mapping->host;
581
582 /*
583 * Short read means EOF. If file size is larger, truncate it
584 */
585 if (!req->out.h.error && num_read < count) {
586 loff_t pos;
587
588 pos = page_offset(req->pages[0]) + num_read;
589 fuse_read_update_size(inode, pos,
590 req->misc.read.attr_ver);
591 }
592 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700593 }
594
Miklos Szeredidb50b962005-09-09 13:10:33 -0700595 for (i = 0; i < req->num_pages; i++) {
596 struct page *page = req->pages[i];
597 if (!req->out.h.error)
598 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800599 else
600 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700601 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200602 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700603 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700604 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100605 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800606}
607
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200608static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800609{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200610 struct fuse_file *ff = file->private_data;
611 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800612 loff_t pos = page_offset(req->pages[0]);
613 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200614
615 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800616 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200617 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200618 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700619 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800620 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700621 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800622 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100623 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800624 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100625 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800626 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100627 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800628 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700629}
630
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700631struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700632 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800633 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700634 struct inode *inode;
635};
636
637static int fuse_readpages_fill(void *_data, struct page *page)
638{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700639 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700640 struct fuse_req *req = data->req;
641 struct inode *inode = data->inode;
642 struct fuse_conn *fc = get_fuse_conn(inode);
643
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700644 fuse_wait_on_page_writeback(inode, page->index);
645
Miklos Szeredidb50b962005-09-09 13:10:33 -0700646 if (req->num_pages &&
647 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
648 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
649 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200650 fuse_send_readpages(req, data->file);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700651 data->req = req = fuse_get_req(fc);
652 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700653 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700654 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700655 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700656 }
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200657 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700658 req->pages[req->num_pages] = page;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100659 req->num_pages++;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700660 return 0;
661}
662
663static int fuse_readpages(struct file *file, struct address_space *mapping,
664 struct list_head *pages, unsigned nr_pages)
665{
666 struct inode *inode = mapping->host;
667 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700668 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700669 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800670
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700671 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800672 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800673 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800674
Miklos Szeredia6643092007-11-28 16:22:00 -0800675 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700676 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700677 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700678 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700679 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800680 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700681
682 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700683 if (!err) {
684 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200685 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700686 else
687 fuse_put_request(fc, data.req);
688 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800689out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700690 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700691}
692
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800693static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
694 unsigned long nr_segs, loff_t pos)
695{
696 struct inode *inode = iocb->ki_filp->f_mapping->host;
697
698 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
699 int err;
700 /*
701 * If trying to read past EOF, make sure the i_size
702 * attribute is up-to-date.
703 */
704 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
705 if (err)
706 return err;
707 }
708
709 return generic_file_aio_read(iocb, iov, nr_segs, pos);
710}
711
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200712static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200713 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700714{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700715 struct fuse_write_in *inarg = &req->misc.write.in;
716 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700717
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700718 inarg->fh = ff->fh;
719 inarg->offset = pos;
720 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700721 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200722 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700723 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200724 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700725 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
726 else
727 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700728 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700729 req->in.args[1].size = count;
730 req->out.numargs = 1;
731 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700732 req->out.args[0].value = outarg;
733}
734
735static size_t fuse_send_write(struct fuse_req *req, struct file *file,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200736 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700737{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200738 struct fuse_file *ff = file->private_data;
739 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200740 struct fuse_write_in *inarg = &req->misc.write.in;
741
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200742 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200743 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700744 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700745 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
746 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
747 }
Tejun Heob93f8582008-11-26 12:03:55 +0100748 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700749 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700750}
751
Miklos Szeredia1d75f22010-07-12 14:41:40 +0200752void fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -0700753{
754 struct fuse_conn *fc = get_fuse_conn(inode);
755 struct fuse_inode *fi = get_fuse_inode(inode);
756
757 spin_lock(&fc->lock);
758 fi->attr_version = ++fc->attr_version;
759 if (pos > inode->i_size)
760 i_size_write(inode, pos);
761 spin_unlock(&fc->lock);
762}
763
Nick Pigginea9b9902008-04-30 00:54:42 -0700764static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
765 struct inode *inode, loff_t pos,
766 size_t count)
767{
768 size_t res;
769 unsigned offset;
770 unsigned i;
771
772 for (i = 0; i < req->num_pages; i++)
773 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
774
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200775 res = fuse_send_write(req, file, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -0700776
777 offset = req->page_offset;
778 count = res;
779 for (i = 0; i < req->num_pages; i++) {
780 struct page *page = req->pages[i];
781
782 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
783 SetPageUptodate(page);
784
785 if (count > PAGE_CACHE_SIZE - offset)
786 count -= PAGE_CACHE_SIZE - offset;
787 else
788 count = 0;
789 offset = 0;
790
791 unlock_page(page);
792 page_cache_release(page);
793 }
794
795 return res;
796}
797
798static ssize_t fuse_fill_write_pages(struct fuse_req *req,
799 struct address_space *mapping,
800 struct iov_iter *ii, loff_t pos)
801{
802 struct fuse_conn *fc = get_fuse_conn(mapping->host);
803 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
804 size_t count = 0;
805 int err;
806
Miklos Szeredif4975c62009-04-02 14:25:34 +0200807 req->in.argpages = 1;
Nick Pigginea9b9902008-04-30 00:54:42 -0700808 req->page_offset = offset;
809
810 do {
811 size_t tmp;
812 struct page *page;
813 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
814 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
815 iov_iter_count(ii));
816
817 bytes = min_t(size_t, bytes, fc->max_write - count);
818
819 again:
820 err = -EFAULT;
821 if (iov_iter_fault_in_readable(ii, bytes))
822 break;
823
824 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -0800825 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -0700826 if (!page)
827 break;
828
anfei zhou931e80e2010-02-02 13:44:02 -0800829 if (mapping_writably_mapped(mapping))
830 flush_dcache_page(page);
831
Nick Pigginea9b9902008-04-30 00:54:42 -0700832 pagefault_disable();
833 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
834 pagefault_enable();
835 flush_dcache_page(page);
836
837 if (!tmp) {
838 unlock_page(page);
839 page_cache_release(page);
840 bytes = min(bytes, iov_iter_single_seg_count(ii));
841 goto again;
842 }
843
844 err = 0;
845 req->pages[req->num_pages] = page;
846 req->num_pages++;
847
848 iov_iter_advance(ii, tmp);
849 count += tmp;
850 pos += tmp;
851 offset += tmp;
852 if (offset == PAGE_CACHE_SIZE)
853 offset = 0;
854
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700855 if (!fc->big_writes)
856 break;
Nick Pigginea9b9902008-04-30 00:54:42 -0700857 } while (iov_iter_count(ii) && count < fc->max_write &&
858 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
859
860 return count > 0 ? count : err;
861}
862
863static ssize_t fuse_perform_write(struct file *file,
864 struct address_space *mapping,
865 struct iov_iter *ii, loff_t pos)
866{
867 struct inode *inode = mapping->host;
868 struct fuse_conn *fc = get_fuse_conn(inode);
869 int err = 0;
870 ssize_t res = 0;
871
872 if (is_bad_inode(inode))
873 return -EIO;
874
875 do {
876 struct fuse_req *req;
877 ssize_t count;
878
879 req = fuse_get_req(fc);
880 if (IS_ERR(req)) {
881 err = PTR_ERR(req);
882 break;
883 }
884
885 count = fuse_fill_write_pages(req, mapping, ii, pos);
886 if (count <= 0) {
887 err = count;
888 } else {
889 size_t num_written;
890
891 num_written = fuse_send_write_pages(req, file, inode,
892 pos, count);
893 err = req->out.h.error;
894 if (!err) {
895 res += num_written;
896 pos += num_written;
897
898 /* break out of the loop on short write */
899 if (num_written != count)
900 err = -EIO;
901 }
902 }
903 fuse_put_request(fc, req);
904 } while (!err && iov_iter_count(ii));
905
906 if (res > 0)
907 fuse_write_update_size(inode, pos);
908
909 fuse_invalidate_attr(inode);
910
911 return res > 0 ? res : err;
912}
913
914static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
915 unsigned long nr_segs, loff_t pos)
916{
917 struct file *file = iocb->ki_filp;
918 struct address_space *mapping = file->f_mapping;
919 size_t count = 0;
920 ssize_t written = 0;
921 struct inode *inode = mapping->host;
922 ssize_t err;
923 struct iov_iter i;
924
925 WARN_ON(iocb->ki_pos != pos);
926
927 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
928 if (err)
929 return err;
930
931 mutex_lock(&inode->i_mutex);
932 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
933
934 /* We can write back this queue in page reclaim */
935 current->backing_dev_info = mapping->backing_dev_info;
936
937 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
938 if (err)
939 goto out;
940
941 if (count == 0)
942 goto out;
943
Miklos Szeredi2f1936b2008-06-24 16:50:14 +0200944 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -0700945 if (err)
946 goto out;
947
948 file_update_time(file);
949
950 iov_iter_init(&i, iov, nr_segs, count, 0);
951 written = fuse_perform_write(file, mapping, &i, pos);
952 if (written >= 0)
953 iocb->ki_pos = pos + written;
954
955out:
956 current->backing_dev_info = NULL;
957 mutex_unlock(&inode->i_mutex);
958
959 return written ? written : err;
960}
961
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700962static void fuse_release_user_pages(struct fuse_req *req, int write)
963{
964 unsigned i;
965
966 for (i = 0; i < req->num_pages; i++) {
967 struct page *page = req->pages[i];
968 if (write)
969 set_page_dirty_lock(page);
970 put_page(page);
971 }
972}
973
974static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200975 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700976{
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200977 size_t nbytes = *nbytesp;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700978 unsigned long user_addr = (unsigned long) buf;
979 unsigned offset = user_addr & ~PAGE_MASK;
980 int npages;
981
Miklos Szeredif4975c62009-04-02 14:25:34 +0200982 /* Special case for kernel I/O: can copy directly into the buffer */
983 if (segment_eq(get_fs(), KERNEL_DS)) {
984 if (write)
985 req->in.args[1].value = (void *) user_addr;
986 else
987 req->out.args[0].value = (void *) user_addr;
988
989 return 0;
990 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700991
Miklos Szeredice60a2f2009-04-09 17:37:52 +0200992 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700993 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Harvey Harrisonbd730962008-05-01 04:35:15 -0700994 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi1bf94ca2010-05-25 15:06:06 +0200995 npages = get_user_pages_fast(user_addr, npages, !write, req->pages);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700996 if (npages < 0)
997 return npages;
998
999 req->num_pages = npages;
1000 req->page_offset = offset;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001001
1002 if (write)
1003 req->in.argpages = 1;
1004 else
1005 req->out.argpages = 1;
1006
1007 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
1008 *nbytesp = min(*nbytesp, nbytes);
1009
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001010 return 0;
1011}
1012
Tejun Heo08cbf542009-04-14 10:54:53 +09001013ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1014 size_t count, loff_t *ppos, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001015{
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001016 struct fuse_file *ff = file->private_data;
1017 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001018 size_t nmax = write ? fc->max_write : fc->max_read;
1019 loff_t pos = *ppos;
1020 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001021 struct fuse_req *req;
1022
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001023 req = fuse_get_req(fc);
1024 if (IS_ERR(req))
1025 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001026
1027 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001028 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001029 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001030 size_t nbytes = min(count, nmax);
1031 int err = fuse_get_user_pages(req, buf, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001032 if (err) {
1033 res = err;
1034 break;
1035 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001036
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001037 if (write)
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001038 nres = fuse_send_write(req, file, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001039 else
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001040 nres = fuse_send_read(req, file, pos, nbytes, owner);
1041
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001042 fuse_release_user_pages(req, !write);
1043 if (req->out.h.error) {
1044 if (!res)
1045 res = req->out.h.error;
1046 break;
1047 } else if (nres > nbytes) {
1048 res = -EIO;
1049 break;
1050 }
1051 count -= nres;
1052 res += nres;
1053 pos += nres;
1054 buf += nres;
1055 if (nres != nbytes)
1056 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001057 if (count) {
1058 fuse_put_request(fc, req);
1059 req = fuse_get_req(fc);
1060 if (IS_ERR(req))
1061 break;
1062 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001063 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001064 if (!IS_ERR(req))
1065 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001066 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001067 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001068
1069 return res;
1070}
Tejun Heo08cbf542009-04-14 10:54:53 +09001071EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001072
1073static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1074 size_t count, loff_t *ppos)
1075{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001076 ssize_t res;
1077 struct inode *inode = file->f_path.dentry->d_inode;
1078
1079 if (is_bad_inode(inode))
1080 return -EIO;
1081
1082 res = fuse_direct_io(file, buf, count, ppos, 0);
1083
1084 fuse_invalidate_attr(inode);
1085
1086 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001087}
1088
1089static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1090 size_t count, loff_t *ppos)
1091{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001092 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001093 ssize_t res;
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001094
1095 if (is_bad_inode(inode))
1096 return -EIO;
1097
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001098 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001099 mutex_lock(&inode->i_mutex);
Miklos Szeredi889f7842007-05-23 13:57:54 -07001100 res = generic_write_checks(file, ppos, &count, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001101 if (!res) {
Miklos Szeredi889f7842007-05-23 13:57:54 -07001102 res = fuse_direct_io(file, buf, count, ppos, 1);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001103 if (res > 0)
1104 fuse_write_update_size(inode, *ppos);
1105 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001106 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001107
1108 fuse_invalidate_attr(inode);
1109
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001110 return res;
1111}
1112
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001113static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001114{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001115 __free_page(req->pages[0]);
Miklos Szeredi5a18ec12011-02-25 14:44:58 +01001116 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001117}
1118
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001119static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001120{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001121 struct inode *inode = req->inode;
1122 struct fuse_inode *fi = get_fuse_inode(inode);
1123 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1124
1125 list_del(&req->writepages_entry);
1126 dec_bdi_stat(bdi, BDI_WRITEBACK);
1127 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1128 bdi_writeout_inc(bdi);
1129 wake_up(&fi->page_waitq);
1130}
1131
1132/* Called under fc->lock, may release and reacquire it */
1133static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001134__releases(fc->lock)
1135__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001136{
1137 struct fuse_inode *fi = get_fuse_inode(req->inode);
1138 loff_t size = i_size_read(req->inode);
1139 struct fuse_write_in *inarg = &req->misc.write.in;
1140
1141 if (!fc->connected)
1142 goto out_free;
1143
1144 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1145 inarg->size = PAGE_CACHE_SIZE;
1146 } else if (inarg->offset < size) {
1147 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1148 } else {
1149 /* Got truncated off completely */
1150 goto out_free;
1151 }
1152
1153 req->in.args[1].size = inarg->size;
1154 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001155 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001156 return;
1157
1158 out_free:
1159 fuse_writepage_finish(fc, req);
1160 spin_unlock(&fc->lock);
1161 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001162 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001163 spin_lock(&fc->lock);
1164}
1165
1166/*
1167 * If fi->writectr is positive (no truncate or fsync going on) send
1168 * all queued writepage requests.
1169 *
1170 * Called with fc->lock
1171 */
1172void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001173__releases(fc->lock)
1174__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001175{
1176 struct fuse_conn *fc = get_fuse_conn(inode);
1177 struct fuse_inode *fi = get_fuse_inode(inode);
1178 struct fuse_req *req;
1179
1180 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1181 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1182 list_del_init(&req->list);
1183 fuse_send_writepage(fc, req);
1184 }
1185}
1186
1187static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1188{
1189 struct inode *inode = req->inode;
1190 struct fuse_inode *fi = get_fuse_inode(inode);
1191
1192 mapping_set_error(inode->i_mapping, req->out.h.error);
1193 spin_lock(&fc->lock);
1194 fi->writectr--;
1195 fuse_writepage_finish(fc, req);
1196 spin_unlock(&fc->lock);
1197 fuse_writepage_free(fc, req);
1198}
1199
1200static int fuse_writepage_locked(struct page *page)
1201{
1202 struct address_space *mapping = page->mapping;
1203 struct inode *inode = mapping->host;
1204 struct fuse_conn *fc = get_fuse_conn(inode);
1205 struct fuse_inode *fi = get_fuse_inode(inode);
1206 struct fuse_req *req;
1207 struct fuse_file *ff;
1208 struct page *tmp_page;
1209
1210 set_page_writeback(page);
1211
1212 req = fuse_request_alloc_nofs();
1213 if (!req)
1214 goto err;
1215
1216 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1217 if (!tmp_page)
1218 goto err_free;
1219
1220 spin_lock(&fc->lock);
1221 BUG_ON(list_empty(&fi->write_files));
1222 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1223 req->ff = fuse_file_get(ff);
1224 spin_unlock(&fc->lock);
1225
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001226 fuse_write_fill(req, ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001227
1228 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001229 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001230 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001231 req->num_pages = 1;
1232 req->pages[0] = tmp_page;
1233 req->page_offset = 0;
1234 req->end = fuse_writepage_end;
1235 req->inode = inode;
1236
1237 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1238 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1239 end_page_writeback(page);
1240
1241 spin_lock(&fc->lock);
1242 list_add(&req->writepages_entry, &fi->writepages);
1243 list_add_tail(&req->list, &fi->queued_writes);
1244 fuse_flush_writepages(inode);
1245 spin_unlock(&fc->lock);
1246
1247 return 0;
1248
1249err_free:
1250 fuse_request_free(req);
1251err:
1252 end_page_writeback(page);
1253 return -ENOMEM;
1254}
1255
1256static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1257{
1258 int err;
1259
1260 err = fuse_writepage_locked(page);
1261 unlock_page(page);
1262
1263 return err;
1264}
1265
1266static int fuse_launder_page(struct page *page)
1267{
1268 int err = 0;
1269 if (clear_page_dirty_for_io(page)) {
1270 struct inode *inode = page->mapping->host;
1271 err = fuse_writepage_locked(page);
1272 if (!err)
1273 fuse_wait_on_page_writeback(inode, page->index);
1274 }
1275 return err;
1276}
1277
1278/*
1279 * Write back dirty pages now, because there may not be any suitable
1280 * open files later
1281 */
1282static void fuse_vma_close(struct vm_area_struct *vma)
1283{
1284 filemap_write_and_wait(vma->vm_file->f_mapping);
1285}
1286
1287/*
1288 * Wait for writeback against this page to complete before allowing it
1289 * to be marked dirty again, and hence written back again, possibly
1290 * before the previous writepage completed.
1291 *
1292 * Block here, instead of in ->writepage(), so that the userspace fs
1293 * can only block processes actually operating on the filesystem.
1294 *
1295 * Otherwise unprivileged userspace fs would be able to block
1296 * unrelated:
1297 *
1298 * - page migration
1299 * - sync(2)
1300 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1301 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07001302static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001303{
Nick Pigginc2ec1752009-03-31 15:23:21 -07001304 struct page *page = vmf->page;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001305 /*
1306 * Don't use page->mapping as it may become NULL from a
1307 * concurrent truncate.
1308 */
1309 struct inode *inode = vma->vm_file->f_mapping->host;
1310
1311 fuse_wait_on_page_writeback(inode, page->index);
1312 return 0;
1313}
1314
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001315static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001316 .close = fuse_vma_close,
1317 .fault = filemap_fault,
1318 .page_mkwrite = fuse_page_mkwrite,
1319};
1320
1321static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1322{
1323 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1324 struct inode *inode = file->f_dentry->d_inode;
1325 struct fuse_conn *fc = get_fuse_conn(inode);
1326 struct fuse_inode *fi = get_fuse_inode(inode);
1327 struct fuse_file *ff = file->private_data;
1328 /*
1329 * file may be written through mmap, so chain it onto the
1330 * inodes's write_file list
1331 */
1332 spin_lock(&fc->lock);
1333 if (list_empty(&ff->write_entry))
1334 list_add(&ff->write_entry, &fi->write_files);
1335 spin_unlock(&fc->lock);
1336 }
1337 file_accessed(file);
1338 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001339 return 0;
1340}
1341
Miklos Szeredifc280c92009-04-02 14:25:35 +02001342static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1343{
1344 /* Can't provide the coherency needed for MAP_SHARED */
1345 if (vma->vm_flags & VM_MAYSHARE)
1346 return -ENODEV;
1347
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02001348 invalidate_inode_pages2(file->f_mapping);
1349
Miklos Szeredifc280c92009-04-02 14:25:35 +02001350 return generic_file_mmap(file, vma);
1351}
1352
Miklos Szeredi71421252006-06-25 05:48:52 -07001353static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1354 struct file_lock *fl)
1355{
1356 switch (ffl->type) {
1357 case F_UNLCK:
1358 break;
1359
1360 case F_RDLCK:
1361 case F_WRLCK:
1362 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1363 ffl->end < ffl->start)
1364 return -EIO;
1365
1366 fl->fl_start = ffl->start;
1367 fl->fl_end = ffl->end;
1368 fl->fl_pid = ffl->pid;
1369 break;
1370
1371 default:
1372 return -EIO;
1373 }
1374 fl->fl_type = ffl->type;
1375 return 0;
1376}
1377
1378static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001379 const struct file_lock *fl, int opcode, pid_t pid,
1380 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001381{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001382 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001383 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07001384 struct fuse_file *ff = file->private_data;
1385 struct fuse_lk_in *arg = &req->misc.lk_in;
1386
1387 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07001388 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07001389 arg->lk.start = fl->fl_start;
1390 arg->lk.end = fl->fl_end;
1391 arg->lk.type = fl->fl_type;
1392 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001393 if (flock)
1394 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07001395 req->in.h.opcode = opcode;
1396 req->in.h.nodeid = get_node_id(inode);
1397 req->in.numargs = 1;
1398 req->in.args[0].size = sizeof(*arg);
1399 req->in.args[0].value = arg;
1400}
1401
1402static int fuse_getlk(struct file *file, struct file_lock *fl)
1403{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001404 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001405 struct fuse_conn *fc = get_fuse_conn(inode);
1406 struct fuse_req *req;
1407 struct fuse_lk_out outarg;
1408 int err;
1409
1410 req = fuse_get_req(fc);
1411 if (IS_ERR(req))
1412 return PTR_ERR(req);
1413
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001414 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001415 req->out.numargs = 1;
1416 req->out.args[0].size = sizeof(outarg);
1417 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001418 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001419 err = req->out.h.error;
1420 fuse_put_request(fc, req);
1421 if (!err)
1422 err = convert_fuse_file_lock(&outarg.lk, fl);
1423
1424 return err;
1425}
1426
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001427static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07001428{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001429 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001430 struct fuse_conn *fc = get_fuse_conn(inode);
1431 struct fuse_req *req;
1432 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1433 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1434 int err;
1435
Miklos Szeredi48e90762008-07-25 01:49:02 -07001436 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1437 /* NLM needs asynchronous locks, which we don't support yet */
1438 return -ENOLCK;
1439 }
1440
Miklos Szeredi71421252006-06-25 05:48:52 -07001441 /* Unlock on close is handled by the flush method */
1442 if (fl->fl_flags & FL_CLOSE)
1443 return 0;
1444
1445 req = fuse_get_req(fc);
1446 if (IS_ERR(req))
1447 return PTR_ERR(req);
1448
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001449 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01001450 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07001451 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001452 /* locking is restartable */
1453 if (err == -EINTR)
1454 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07001455 fuse_put_request(fc, req);
1456 return err;
1457}
1458
1459static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1460{
Josef Sipek7706a9d2006-12-08 02:37:02 -08001461 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -07001462 struct fuse_conn *fc = get_fuse_conn(inode);
1463 int err;
1464
Miklos Szeredi48e90762008-07-25 01:49:02 -07001465 if (cmd == F_CANCELLK) {
1466 err = 0;
1467 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07001468 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05001469 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07001470 err = 0;
1471 } else
1472 err = fuse_getlk(file, fl);
1473 } else {
1474 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07001475 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07001476 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001477 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07001478 }
1479 return err;
1480}
1481
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001482static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1483{
1484 struct inode *inode = file->f_path.dentry->d_inode;
1485 struct fuse_conn *fc = get_fuse_conn(inode);
1486 int err;
1487
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001488 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001489 err = flock_lock_file_wait(file, fl);
1490 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001491 struct fuse_file *ff = file->private_data;
1492
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001493 /* emulate flock with POSIX locks */
1494 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02001495 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07001496 err = fuse_setlk(file, fl, 1);
1497 }
1498
1499 return err;
1500}
1501
Miklos Szeredib2d22722006-12-06 20:35:51 -08001502static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1503{
1504 struct inode *inode = mapping->host;
1505 struct fuse_conn *fc = get_fuse_conn(inode);
1506 struct fuse_req *req;
1507 struct fuse_bmap_in inarg;
1508 struct fuse_bmap_out outarg;
1509 int err;
1510
1511 if (!inode->i_sb->s_bdev || fc->no_bmap)
1512 return 0;
1513
1514 req = fuse_get_req(fc);
1515 if (IS_ERR(req))
1516 return 0;
1517
1518 memset(&inarg, 0, sizeof(inarg));
1519 inarg.block = block;
1520 inarg.blocksize = inode->i_sb->s_blocksize;
1521 req->in.h.opcode = FUSE_BMAP;
1522 req->in.h.nodeid = get_node_id(inode);
1523 req->in.numargs = 1;
1524 req->in.args[0].size = sizeof(inarg);
1525 req->in.args[0].value = &inarg;
1526 req->out.numargs = 1;
1527 req->out.args[0].size = sizeof(outarg);
1528 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001529 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08001530 err = req->out.h.error;
1531 fuse_put_request(fc, req);
1532 if (err == -ENOSYS)
1533 fc->no_bmap = 1;
1534
1535 return err ? 0 : outarg.block;
1536}
1537
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001538static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1539{
1540 loff_t retval;
1541 struct inode *inode = file->f_path.dentry->d_inode;
1542
1543 mutex_lock(&inode->i_mutex);
1544 switch (origin) {
1545 case SEEK_END:
Miklos Szeredi769415c2008-10-16 16:08:56 +02001546 retval = fuse_update_attributes(inode, NULL, file, NULL);
1547 if (retval)
Dan Carpenter52916582009-03-27 13:36:10 +03001548 goto exit;
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001549 offset += i_size_read(inode);
1550 break;
1551 case SEEK_CUR:
1552 offset += file->f_pos;
1553 }
1554 retval = -EINVAL;
1555 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1556 if (offset != file->f_pos) {
1557 file->f_pos = offset;
1558 file->f_version = 0;
1559 }
1560 retval = offset;
1561 }
Dan Carpenter52916582009-03-27 13:36:10 +03001562exit:
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07001563 mutex_unlock(&inode->i_mutex);
1564 return retval;
1565}
1566
Tejun Heo59efec72008-11-26 12:03:55 +01001567static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1568 unsigned int nr_segs, size_t bytes, bool to_user)
1569{
1570 struct iov_iter ii;
1571 int page_idx = 0;
1572
1573 if (!bytes)
1574 return 0;
1575
1576 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1577
1578 while (iov_iter_count(&ii)) {
1579 struct page *page = pages[page_idx++];
1580 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001581 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001582
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02001583 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001584
1585 while (todo) {
1586 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1587 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1588 size_t copy = min(todo, iov_len);
1589 size_t left;
1590
1591 if (!to_user)
1592 left = copy_from_user(kaddr, uaddr, copy);
1593 else
1594 left = copy_to_user(uaddr, kaddr, copy);
1595
1596 if (unlikely(left))
1597 return -EFAULT;
1598
1599 iov_iter_advance(&ii, copy);
1600 todo -= copy;
1601 kaddr += copy;
1602 }
1603
Jens Axboe0bd87182009-11-03 11:40:44 +01001604 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01001605 }
1606
1607 return 0;
1608}
1609
1610/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001611 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1612 * ABI was defined to be 'struct iovec' which is different on 32bit
1613 * and 64bit. Fortunately we can determine which structure the server
1614 * used from the size of the reply.
1615 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001616static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1617 size_t transferred, unsigned count,
1618 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001619{
1620#ifdef CONFIG_COMPAT
1621 if (count * sizeof(struct compat_iovec) == transferred) {
1622 struct compat_iovec *ciov = src;
1623 unsigned i;
1624
1625 /*
1626 * With this interface a 32bit server cannot support
1627 * non-compat (i.e. ones coming from 64bit apps) ioctl
1628 * requests
1629 */
1630 if (!is_compat)
1631 return -EINVAL;
1632
1633 for (i = 0; i < count; i++) {
1634 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1635 dst[i].iov_len = ciov[i].iov_len;
1636 }
1637 return 0;
1638 }
1639#endif
1640
1641 if (count * sizeof(struct iovec) != transferred)
1642 return -EIO;
1643
1644 memcpy(dst, src, transferred);
1645 return 0;
1646}
1647
Miklos Szeredi75727772010-11-30 16:39:27 +01001648/* Make sure iov_length() won't overflow */
1649static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1650{
1651 size_t n;
1652 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1653
1654 for (n = 0; n < count; n++) {
1655 if (iov->iov_len > (size_t) max)
1656 return -ENOMEM;
1657 max -= iov->iov_len;
1658 }
1659 return 0;
1660}
1661
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001662static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1663 void *src, size_t transferred, unsigned count,
1664 bool is_compat)
1665{
1666 unsigned i;
1667 struct fuse_ioctl_iovec *fiov = src;
1668
1669 if (fc->minor < 16) {
1670 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1671 count, is_compat);
1672 }
1673
1674 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1675 return -EIO;
1676
1677 for (i = 0; i < count; i++) {
1678 /* Did the server supply an inappropriate value? */
1679 if (fiov[i].base != (unsigned long) fiov[i].base ||
1680 fiov[i].len != (unsigned long) fiov[i].len)
1681 return -EIO;
1682
1683 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1684 dst[i].iov_len = (size_t) fiov[i].len;
1685
1686#ifdef CONFIG_COMPAT
1687 if (is_compat &&
1688 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1689 (compat_size_t) dst[i].iov_len != fiov[i].len))
1690 return -EIO;
1691#endif
1692 }
1693
1694 return 0;
1695}
1696
1697
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001698/*
Tejun Heo59efec72008-11-26 12:03:55 +01001699 * For ioctls, there is no generic way to determine how much memory
1700 * needs to be read and/or written. Furthermore, ioctls are allowed
1701 * to dereference the passed pointer, so the parameter requires deep
1702 * copying but FUSE has no idea whatsoever about what to copy in or
1703 * out.
1704 *
1705 * This is solved by allowing FUSE server to retry ioctl with
1706 * necessary in/out iovecs. Let's assume the ioctl implementation
1707 * needs to read in the following structure.
1708 *
1709 * struct a {
1710 * char *buf;
1711 * size_t buflen;
1712 * }
1713 *
1714 * On the first callout to FUSE server, inarg->in_size and
1715 * inarg->out_size will be NULL; then, the server completes the ioctl
1716 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1717 * the actual iov array to
1718 *
1719 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1720 *
1721 * which tells FUSE to copy in the requested area and retry the ioctl.
1722 * On the second round, the server has access to the structure and
1723 * from that it can tell what to look for next, so on the invocation,
1724 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1725 *
1726 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1727 * { .iov_base = a.buf, .iov_len = a.buflen } }
1728 *
1729 * FUSE will copy both struct a and the pointed buffer from the
1730 * process doing the ioctl and retry ioctl with both struct a and the
1731 * buffer.
1732 *
1733 * This time, FUSE server has everything it needs and completes ioctl
1734 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1735 *
1736 * Copying data out works the same way.
1737 *
1738 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1739 * automatically initializes in and out iovs by decoding @cmd with
1740 * _IOC_* macros and the server is not allowed to request RETRY. This
1741 * limits ioctl data transfers to well-formed ioctls and is the forced
1742 * behavior for all FUSE servers.
1743 */
Tejun Heo08cbf542009-04-14 10:54:53 +09001744long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1745 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01001746{
Tejun Heo59efec72008-11-26 12:03:55 +01001747 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001748 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01001749 struct fuse_ioctl_in inarg = {
1750 .fh = ff->fh,
1751 .cmd = cmd,
1752 .arg = arg,
1753 .flags = flags
1754 };
1755 struct fuse_ioctl_out outarg;
1756 struct fuse_req *req = NULL;
1757 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001758 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01001759 struct iovec *in_iov = NULL, *out_iov = NULL;
1760 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1761 size_t in_size, out_size, transferred;
1762 int err;
1763
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001764#if BITS_PER_LONG == 32
1765 inarg.flags |= FUSE_IOCTL_32BIT;
1766#else
1767 if (flags & FUSE_IOCTL_COMPAT)
1768 inarg.flags |= FUSE_IOCTL_32BIT;
1769#endif
1770
Tejun Heo59efec72008-11-26 12:03:55 +01001771 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001772 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01001773
Tejun Heo59efec72008-11-26 12:03:55 +01001774 err = -ENOMEM;
1775 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001776 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01001777 if (!pages || !iov_page)
1778 goto out;
1779
1780 /*
1781 * If restricted, initialize IO parameters as encoded in @cmd.
1782 * RETRY from server is not allowed.
1783 */
1784 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001785 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01001786
Miklos Szeredic9f05232008-12-02 14:49:42 +01001787 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01001788 iov->iov_len = _IOC_SIZE(cmd);
1789
1790 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1791 in_iov = iov;
1792 in_iovs = 1;
1793 }
1794
1795 if (_IOC_DIR(cmd) & _IOC_READ) {
1796 out_iov = iov;
1797 out_iovs = 1;
1798 }
1799 }
1800
1801 retry:
1802 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1803 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1804
1805 /*
1806 * Out data can be used either for actual out data or iovs,
1807 * make sure there always is at least one page.
1808 */
1809 out_size = max_t(size_t, out_size, PAGE_SIZE);
1810 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1811
1812 /* make sure there are enough buffer pages and init request with them */
1813 err = -ENOMEM;
1814 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1815 goto out;
1816 while (num_pages < max_pages) {
1817 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1818 if (!pages[num_pages])
1819 goto out;
1820 num_pages++;
1821 }
1822
1823 req = fuse_get_req(fc);
1824 if (IS_ERR(req)) {
1825 err = PTR_ERR(req);
1826 req = NULL;
1827 goto out;
1828 }
1829 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1830 req->num_pages = num_pages;
1831
1832 /* okay, let's send it to the client */
1833 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02001834 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01001835 req->in.numargs = 1;
1836 req->in.args[0].size = sizeof(inarg);
1837 req->in.args[0].value = &inarg;
1838 if (in_size) {
1839 req->in.numargs++;
1840 req->in.args[1].size = in_size;
1841 req->in.argpages = 1;
1842
1843 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1844 false);
1845 if (err)
1846 goto out;
1847 }
1848
1849 req->out.numargs = 2;
1850 req->out.args[0].size = sizeof(outarg);
1851 req->out.args[0].value = &outarg;
1852 req->out.args[1].size = out_size;
1853 req->out.argpages = 1;
1854 req->out.argvar = 1;
1855
Tejun Heob93f8582008-11-26 12:03:55 +01001856 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01001857 err = req->out.h.error;
1858 transferred = req->out.args[1].size;
1859 fuse_put_request(fc, req);
1860 req = NULL;
1861 if (err)
1862 goto out;
1863
1864 /* did it ask for retry? */
1865 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001866 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01001867
1868 /* no retry if in restricted mode */
1869 err = -EIO;
1870 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1871 goto out;
1872
1873 in_iovs = outarg.in_iovs;
1874 out_iovs = outarg.out_iovs;
1875
1876 /*
1877 * Make sure things are in boundary, separate checks
1878 * are to protect against overflow.
1879 */
1880 err = -ENOMEM;
1881 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1882 out_iovs > FUSE_IOCTL_MAX_IOV ||
1883 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1884 goto out;
1885
Tejun Heo59efec72008-11-26 12:03:55 +01001886 vaddr = kmap_atomic(pages[0], KM_USER0);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01001887 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001888 transferred, in_iovs + out_iovs,
1889 (flags & FUSE_IOCTL_COMPAT) != 0);
Tejun Heo59efec72008-11-26 12:03:55 +01001890 kunmap_atomic(vaddr, KM_USER0);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01001891 if (err)
1892 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01001893
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001894 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01001895 out_iov = in_iov + in_iovs;
1896
Miklos Szeredi75727772010-11-30 16:39:27 +01001897 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
1898 if (err)
1899 goto out;
1900
1901 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
1902 if (err)
1903 goto out;
1904
Tejun Heo59efec72008-11-26 12:03:55 +01001905 goto retry;
1906 }
1907
1908 err = -EIO;
1909 if (transferred > inarg.out_size)
1910 goto out;
1911
1912 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1913 out:
1914 if (req)
1915 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01001916 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01001917 while (num_pages)
1918 __free_page(pages[--num_pages]);
1919 kfree(pages);
1920
1921 return err ? err : outarg.result;
1922}
Tejun Heo08cbf542009-04-14 10:54:53 +09001923EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01001924
Miklos Szeredid36f2482009-04-28 16:56:39 +02001925static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
1926 unsigned long arg, unsigned int flags)
1927{
1928 struct inode *inode = file->f_dentry->d_inode;
1929 struct fuse_conn *fc = get_fuse_conn(inode);
1930
1931 if (!fuse_allow_task(fc, current))
1932 return -EACCES;
1933
1934 if (is_bad_inode(inode))
1935 return -EIO;
1936
1937 return fuse_do_ioctl(file, cmd, arg, flags);
1938}
1939
Tejun Heo59efec72008-11-26 12:03:55 +01001940static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1941 unsigned long arg)
1942{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001943 return fuse_file_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01001944}
1945
1946static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1947 unsigned long arg)
1948{
Miklos Szeredid36f2482009-04-28 16:56:39 +02001949 return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01001950}
1951
Tejun Heo95668a62008-11-26 12:03:55 +01001952/*
1953 * All files which have been polled are linked to RB tree
1954 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1955 * find the matching one.
1956 */
1957static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1958 struct rb_node **parent_out)
1959{
1960 struct rb_node **link = &fc->polled_files.rb_node;
1961 struct rb_node *last = NULL;
1962
1963 while (*link) {
1964 struct fuse_file *ff;
1965
1966 last = *link;
1967 ff = rb_entry(last, struct fuse_file, polled_node);
1968
1969 if (kh < ff->kh)
1970 link = &last->rb_left;
1971 else if (kh > ff->kh)
1972 link = &last->rb_right;
1973 else
1974 return link;
1975 }
1976
1977 if (parent_out)
1978 *parent_out = last;
1979 return link;
1980}
1981
1982/*
1983 * The file is about to be polled. Make sure it's on the polled_files
1984 * RB tree. Note that files once added to the polled_files tree are
1985 * not removed before the file is released. This is because a file
1986 * polled once is likely to be polled again.
1987 */
1988static void fuse_register_polled_file(struct fuse_conn *fc,
1989 struct fuse_file *ff)
1990{
1991 spin_lock(&fc->lock);
1992 if (RB_EMPTY_NODE(&ff->polled_node)) {
1993 struct rb_node **link, *parent;
1994
1995 link = fuse_find_polled_node(fc, ff->kh, &parent);
1996 BUG_ON(*link);
1997 rb_link_node(&ff->polled_node, parent, link);
1998 rb_insert_color(&ff->polled_node, &fc->polled_files);
1999 }
2000 spin_unlock(&fc->lock);
2001}
2002
Tejun Heo08cbf542009-04-14 10:54:53 +09002003unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002004{
Tejun Heo95668a62008-11-26 12:03:55 +01002005 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002006 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002007 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2008 struct fuse_poll_out outarg;
2009 struct fuse_req *req;
2010 int err;
2011
2012 if (fc->no_poll)
2013 return DEFAULT_POLLMASK;
2014
2015 poll_wait(file, &ff->poll_wait, wait);
2016
2017 /*
2018 * Ask for notification iff there's someone waiting for it.
2019 * The client may ignore the flag and always notify.
2020 */
2021 if (waitqueue_active(&ff->poll_wait)) {
2022 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2023 fuse_register_polled_file(fc, ff);
2024 }
2025
2026 req = fuse_get_req(fc);
2027 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002028 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002029
2030 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002031 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002032 req->in.numargs = 1;
2033 req->in.args[0].size = sizeof(inarg);
2034 req->in.args[0].value = &inarg;
2035 req->out.numargs = 1;
2036 req->out.args[0].size = sizeof(outarg);
2037 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002038 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002039 err = req->out.h.error;
2040 fuse_put_request(fc, req);
2041
2042 if (!err)
2043 return outarg.revents;
2044 if (err == -ENOSYS) {
2045 fc->no_poll = 1;
2046 return DEFAULT_POLLMASK;
2047 }
2048 return POLLERR;
2049}
Tejun Heo08cbf542009-04-14 10:54:53 +09002050EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002051
2052/*
2053 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2054 * wakes up the poll waiters.
2055 */
2056int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2057 struct fuse_notify_poll_wakeup_out *outarg)
2058{
2059 u64 kh = outarg->kh;
2060 struct rb_node **link;
2061
2062 spin_lock(&fc->lock);
2063
2064 link = fuse_find_polled_node(fc, kh, NULL);
2065 if (*link) {
2066 struct fuse_file *ff;
2067
2068 ff = rb_entry(*link, struct fuse_file, polled_node);
2069 wake_up_interruptible_sync(&ff->poll_wait);
2070 }
2071
2072 spin_unlock(&fc->lock);
2073 return 0;
2074}
2075
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002076static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002077 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002078 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08002079 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07002080 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07002081 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002082 .mmap = fuse_file_mmap,
2083 .open = fuse_open,
2084 .flush = fuse_flush,
2085 .release = fuse_release,
2086 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002087 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002088 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02002089 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01002090 .unlocked_ioctl = fuse_file_ioctl,
2091 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002092 .poll = fuse_file_poll,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002093};
2094
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002095static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002096 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002097 .read = fuse_direct_read,
2098 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002099 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002100 .open = fuse_open,
2101 .flush = fuse_flush,
2102 .release = fuse_release,
2103 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07002104 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002105 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01002106 .unlocked_ioctl = fuse_file_ioctl,
2107 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01002108 .poll = fuse_file_poll,
Miklos Szeredifc280c92009-04-02 14:25:35 +02002109 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07002110};
2111
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002112static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002113 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002114 .writepage = fuse_writepage,
2115 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07002116 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002117 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08002118 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002119};
2120
2121void fuse_init_file_inode(struct inode *inode)
2122{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07002123 inode->i_fop = &fuse_file_operations;
2124 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002125}