blob: 48992cac714b413f644dbdd7c97ba87355831677 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Tejun Heo08cbf542009-04-14 10:54:53 +090015#include <linux/module.h>
Miklos Szeredid9d318d2010-11-30 16:39:27 +010016#include <linux/compat.h>
Johannes Weiner478e0842011-07-25 22:35:35 +020017#include <linux/swap.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070018#include <linux/aio.h>
Brian Foster3634a632013-05-17 09:30:32 -040019#include <linux/falloc.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080021static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070022
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020023static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070025{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070026 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080027 struct fuse_req *req;
28 int err;
29
Maxim Patlasovb111c8c2012-10-26 19:48:30 +040030 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -070031 if (IS_ERR(req))
32 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080033
34 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi6ff958e2007-10-18 03:07:02 -070035 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
36 if (!fc->atomic_o_trunc)
37 inarg.flags &= ~O_TRUNC;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +020038 req->in.h.opcode = opcode;
39 req->in.h.nodeid = nodeid;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080040 req->in.numargs = 1;
41 req->in.args[0].size = sizeof(inarg);
42 req->in.args[0].value = &inarg;
43 req->out.numargs = 1;
44 req->out.args[0].size = sizeof(*outargp);
45 req->out.args[0].value = outargp;
Tejun Heob93f8582008-11-26 12:03:55 +010046 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080047 err = req->out.h.error;
48 fuse_put_request(fc, req);
49
50 return err;
51}
52
Tejun Heoacf99432008-11-26 12:03:55 +010053struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054{
55 struct fuse_file *ff;
Tejun Heo6b2db282009-04-14 10:54:49 +090056
Miklos Szeredifd72faa2005-11-07 00:59:51 -080057 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
Tejun Heo6b2db282009-04-14 10:54:49 +090058 if (unlikely(!ff))
59 return NULL;
60
Miklos Szeredida5e4712009-04-28 16:56:36 +020061 ff->fc = fc;
Maxim Patlasov4250c062012-10-26 19:48:07 +040062 ff->reserved_req = fuse_request_alloc(0);
Tejun Heo6b2db282009-04-14 10:54:49 +090063 if (unlikely(!ff->reserved_req)) {
64 kfree(ff);
65 return NULL;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080066 }
Tejun Heo6b2db282009-04-14 10:54:49 +090067
68 INIT_LIST_HEAD(&ff->write_entry);
69 atomic_set(&ff->count, 0);
70 RB_CLEAR_NODE(&ff->polled_node);
71 init_waitqueue_head(&ff->poll_wait);
72
73 spin_lock(&fc->lock);
74 ff->kh = ++fc->khctr;
75 spin_unlock(&fc->lock);
76
Miklos Szeredifd72faa2005-11-07 00:59:51 -080077 return ff;
78}
79
80void fuse_file_free(struct fuse_file *ff)
81{
Miklos Szeredi33649c92006-06-25 05:48:52 -070082 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080083 kfree(ff);
84}
85
Miklos Szeredic7b71432009-04-28 16:56:37 +020086struct fuse_file *fuse_file_get(struct fuse_file *ff)
Miklos Szeredic756e0a2007-10-16 23:31:00 -070087{
88 atomic_inc(&ff->count);
89 return ff;
90}
91
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010092static void fuse_release_async(struct work_struct *work)
Miklos Szeredi819c4b32007-10-16 23:31:04 -070093{
Miklos Szeredi5a18ec12011-02-25 14:44:58 +010094 struct fuse_req *req;
95 struct fuse_conn *fc;
96 struct path path;
97
98 req = container_of(work, struct fuse_req, misc.release.work);
99 path = req->misc.release.path;
100 fc = get_fuse_conn(path.dentry->d_inode);
101
102 fuse_put_request(fc, req);
103 path_put(&path);
Miklos Szeredi819c4b32007-10-16 23:31:04 -0700104}
105
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100106static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
107{
108 if (fc->destroy_req) {
109 /*
110 * If this is a fuseblk mount, then it's possible that
111 * releasing the path will result in releasing the
112 * super block and sending the DESTROY request. If
113 * the server is single threaded, this would hang.
114 * For this reason do the path_put() in a separate
115 * thread.
116 */
117 atomic_inc(&req->count);
118 INIT_WORK(&req->misc.release.work, fuse_release_async);
119 schedule_work(&req->misc.release.work);
120 } else {
121 path_put(&req->misc.release.path);
122 }
123}
124
125static void fuse_file_put(struct fuse_file *ff, bool sync)
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700126{
127 if (atomic_dec_and_test(&ff->count)) {
128 struct fuse_req *req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200129
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100130 if (ff->fc->no_open) {
131 /*
132 * Drop the release request when client does not
133 * implement 'open'
134 */
135 req->background = 0;
136 path_put(&req->misc.release.path);
137 fuse_put_request(ff->fc, req);
138 } else if (sync) {
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400139 req->background = 0;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100140 fuse_request_send(ff->fc, req);
141 path_put(&req->misc.release.path);
142 fuse_put_request(ff->fc, req);
143 } else {
144 req->end = fuse_release_end;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400145 req->background = 1;
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100146 fuse_request_send_background(ff->fc, req);
147 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700148 kfree(ff);
149 }
150}
151
Tejun Heo08cbf542009-04-14 10:54:53 +0900152int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
153 bool isdir)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200154{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200155 struct fuse_file *ff;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200156 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
157
158 ff = fuse_file_alloc(fc);
159 if (!ff)
160 return -ENOMEM;
161
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100162 ff->fh = 0;
163 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
164 if (!fc->no_open || isdir) {
165 struct fuse_open_out outarg;
166 int err;
167
168 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
169 if (!err) {
170 ff->fh = outarg.fh;
171 ff->open_flags = outarg.open_flags;
172
173 } else if (err != -ENOSYS || isdir) {
174 fuse_file_free(ff);
175 return err;
176 } else {
177 fc->no_open = 1;
178 }
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200179 }
180
181 if (isdir)
Andrew Gallagher7678ac52013-11-05 16:05:52 +0100182 ff->open_flags &= ~FOPEN_DIRECT_IO;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200183
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200184 ff->nodeid = nodeid;
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200185 file->private_data = fuse_file_get(ff);
186
187 return 0;
188}
Tejun Heo08cbf542009-04-14 10:54:53 +0900189EXPORT_SYMBOL_GPL(fuse_do_open);
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200190
Pavel Emelyanov650b22b2013-10-10 17:10:04 +0400191static void fuse_link_write_file(struct file *file)
192{
193 struct inode *inode = file_inode(file);
194 struct fuse_conn *fc = get_fuse_conn(inode);
195 struct fuse_inode *fi = get_fuse_inode(inode);
196 struct fuse_file *ff = file->private_data;
197 /*
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
200 */
201 spin_lock(&fc->lock);
202 if (list_empty(&ff->write_entry))
203 list_add(&ff->write_entry, &fi->write_files);
204 spin_unlock(&fc->lock);
205}
206
Miklos Szeredic7b71432009-04-28 16:56:37 +0200207void fuse_finish_open(struct inode *inode, struct file *file)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800208{
Miklos Szeredic7b71432009-04-28 16:56:37 +0200209 struct fuse_file *ff = file->private_data;
Ken Sumralla0822c52010-11-24 12:57:00 -0800210 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200211
212 if (ff->open_flags & FOPEN_DIRECT_IO)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800213 file->f_op = &fuse_direct_io_file_operations;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200214 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
Miklos Szeredib1009972007-10-16 23:31:01 -0700215 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200216 if (ff->open_flags & FOPEN_NONSEEKABLE)
Tejun Heoa7c1b992008-10-16 16:08:57 +0200217 nonseekable_open(inode, file);
Ken Sumralla0822c52010-11-24 12:57:00 -0800218 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
219 struct fuse_inode *fi = get_fuse_inode(inode);
220
221 spin_lock(&fc->lock);
222 fi->attr_version = ++fc->attr_version;
223 i_size_write(inode, 0);
224 spin_unlock(&fc->lock);
225 fuse_invalidate_attr(inode);
226 }
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +0400227 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
228 fuse_link_write_file(file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800229}
230
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200231int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800232{
Tejun Heoacf99432008-11-26 12:03:55 +0100233 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700234 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700235
236 err = generic_file_open(inode, file);
237 if (err)
238 return err;
239
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200240 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800241 if (err)
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200242 return err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700243
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200244 fuse_finish_open(inode, file);
245
246 return 0;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700247}
248
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200249static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800250{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200251 struct fuse_conn *fc = ff->fc;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700252 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib57d4262008-02-06 01:38:39 -0800253 struct fuse_release_in *inarg = &req->misc.release.in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700254
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200255 spin_lock(&fc->lock);
256 list_del(&ff->write_entry);
257 if (!RB_EMPTY_NODE(&ff->polled_node))
258 rb_erase(&ff->polled_node, &fc->polled_files);
259 spin_unlock(&fc->lock);
260
Bryan Green357ccf22011-03-01 16:43:52 -0800261 wake_up_interruptible_all(&ff->poll_wait);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200262
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700263 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800264 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700265 req->in.h.opcode = opcode;
Miklos Szeredic7b71432009-04-28 16:56:37 +0200266 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700267 req->in.numargs = 1;
268 req->in.args[0].size = sizeof(struct fuse_release_in);
269 req->in.args[0].value = inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800270}
271
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200272void fuse_release_common(struct file *file, int opcode)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800273{
Tejun Heo6b2db282009-04-14 10:54:49 +0900274 struct fuse_file *ff;
275 struct fuse_req *req;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -0700276
Tejun Heo6b2db282009-04-14 10:54:49 +0900277 ff = file->private_data;
278 if (unlikely(!ff))
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200279 return;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700280
Tejun Heo6b2db282009-04-14 10:54:49 +0900281 req = ff->reserved_req;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200282 fuse_prepare_release(ff, file->f_flags, opcode);
Tejun Heo95668a62008-11-26 12:03:55 +0100283
Miklos Szeredi37fb3a32011-08-08 16:08:08 +0200284 if (ff->flock) {
285 struct fuse_release_in *inarg = &req->misc.release.in;
286 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
287 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
288 (fl_owner_t) file);
289 }
Tejun Heo6b2db282009-04-14 10:54:49 +0900290 /* Hold vfsmount and dentry until release is finished */
Miklos Szeredib0be46e2009-04-28 16:56:36 +0200291 path_get(&file->f_path);
292 req->misc.release.path = file->f_path;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700293
Tejun Heo6b2db282009-04-14 10:54:49 +0900294 /*
295 * Normally this will send the RELEASE request, however if
296 * some asynchronous READ or WRITE requests are outstanding,
297 * the sending will be delayed.
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100298 *
299 * Make the release synchronous if this is a fuseblk mount,
300 * synchronous RELEASE is allowed (and desirable) in this case
301 * because the server can be trusted not to screw up.
Tejun Heo6b2db282009-04-14 10:54:49 +0900302 */
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100303 fuse_file_put(ff, ff->fc->destroy_req != NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700304}
305
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700306static int fuse_open(struct inode *inode, struct file *file)
307{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +0200308 return fuse_open_common(inode, file, false);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700309}
310
311static int fuse_release(struct inode *inode, struct file *file)
312{
Pavel Emelyanove7cc133c2013-10-10 17:19:06 +0400313 struct fuse_conn *fc = get_fuse_conn(inode);
314
315 /* see fuse_vma_close() for !writeback_cache case */
316 if (fc->writeback_cache)
317 filemap_write_and_wait(file->f_mapping);
318
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400319 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state))
320 fuse_flush_mtime(file, true);
321
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200322 fuse_release_common(file, FUSE_RELEASE);
323
324 /* return value is ignored by VFS */
325 return 0;
326}
327
328void fuse_sync_release(struct fuse_file *ff, int flags)
329{
330 WARN_ON(atomic_read(&ff->count) > 1);
331 fuse_prepare_release(ff, flags, FUSE_RELEASE);
332 ff->reserved_req->force = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400333 ff->reserved_req->background = 0;
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200334 fuse_request_send(ff->fc, ff->reserved_req);
335 fuse_put_request(ff->fc, ff->reserved_req);
336 kfree(ff);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700337}
Tejun Heo08cbf542009-04-14 10:54:53 +0900338EXPORT_SYMBOL_GPL(fuse_sync_release);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700339
Miklos Szeredi71421252006-06-25 05:48:52 -0700340/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700341 * Scramble the ID space with XTEA, so that the value of the files_struct
342 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700343 */
Miklos Szeredif3332112007-10-18 03:07:04 -0700344u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700345{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700346 u32 *k = fc->scramble_key;
347 u64 v = (unsigned long) id;
348 u32 v0 = v;
349 u32 v1 = v >> 32;
350 u32 sum = 0;
351 int i;
352
353 for (i = 0; i < 32; i++) {
354 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
355 sum += 0x9E3779B9;
356 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
357 }
358
359 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700360}
361
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700362/*
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400363 * Check if any page in a range is under writeback
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700364 *
365 * This is currently done by walking the list of writepage requests
366 * for the inode, which can be pretty inefficient.
367 */
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400368static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
369 pgoff_t idx_to)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700370{
371 struct fuse_conn *fc = get_fuse_conn(inode);
372 struct fuse_inode *fi = get_fuse_inode(inode);
373 struct fuse_req *req;
374 bool found = false;
375
376 spin_lock(&fc->lock);
377 list_for_each_entry(req, &fi->writepages, writepages_entry) {
378 pgoff_t curr_index;
379
380 BUG_ON(req->inode != inode);
381 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400382 if (idx_from < curr_index + req->num_pages &&
383 curr_index <= idx_to) {
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700384 found = true;
385 break;
386 }
387 }
388 spin_unlock(&fc->lock);
389
390 return found;
391}
392
Pavel Emelyanovea8cd332013-10-10 17:12:05 +0400393static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
394{
395 return fuse_range_is_writeback(inode, index, index);
396}
397
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700398/*
399 * Wait for page writeback to be completed.
400 *
401 * Since fuse doesn't rely on the VM writeback tracking, this has to
402 * use some other means.
403 */
404static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
405{
406 struct fuse_inode *fi = get_fuse_inode(inode);
407
408 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
409 return 0;
410}
411
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400412/*
413 * Wait for all pending writepages on the inode to finish.
414 *
415 * This is currently done by blocking further writes with FUSE_NOWRITE
416 * and waiting for all sent writes to complete.
417 *
418 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
419 * could conflict with truncation.
420 */
421static void fuse_sync_writes(struct inode *inode)
422{
423 fuse_set_nowrite(inode);
424 fuse_release_nowrite(inode);
425}
426
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700427static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700428{
Al Viro6131ffa2013-02-27 16:59:05 -0500429 struct inode *inode = file_inode(file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700430 struct fuse_conn *fc = get_fuse_conn(inode);
431 struct fuse_file *ff = file->private_data;
432 struct fuse_req *req;
433 struct fuse_flush_in inarg;
434 int err;
435
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800436 if (is_bad_inode(inode))
437 return -EIO;
438
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700439 if (fc->no_flush)
440 return 0;
441
Maxim Patlasovfe38d7d2013-10-10 17:11:54 +0400442 err = filemap_write_and_wait(file->f_mapping);
443 if (err)
444 return err;
445
446 mutex_lock(&inode->i_mutex);
447 fuse_sync_writes(inode);
448 mutex_unlock(&inode->i_mutex);
449
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400450 req = fuse_get_req_nofail_nopages(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700451 memset(&inarg, 0, sizeof(inarg));
452 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700453 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700454 req->in.h.opcode = FUSE_FLUSH;
455 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700456 req->in.numargs = 1;
457 req->in.args[0].size = sizeof(inarg);
458 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700459 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100460 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700461 err = req->out.h.error;
462 fuse_put_request(fc, req);
463 if (err == -ENOSYS) {
464 fc->no_flush = 1;
465 err = 0;
466 }
467 return err;
468}
469
Josef Bacik02c24a82011-07-16 20:44:56 -0400470int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
471 int datasync, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700472{
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200473 struct inode *inode = file->f_mapping->host;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700474 struct fuse_conn *fc = get_fuse_conn(inode);
475 struct fuse_file *ff = file->private_data;
476 struct fuse_req *req;
477 struct fuse_fsync_in inarg;
478 int err;
479
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800480 if (is_bad_inode(inode))
481 return -EIO;
482
Josef Bacik02c24a82011-07-16 20:44:56 -0400483 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
484 if (err)
485 return err;
486
Miklos Szeredi82547982005-09-09 13:10:38 -0700487 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700488 return 0;
489
Josef Bacik02c24a82011-07-16 20:44:56 -0400490 mutex_lock(&inode->i_mutex);
491
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700492 /*
493 * Start writeback against all dirty pages of the inode, then
494 * wait for all outstanding writes, before sending the FSYNC
495 * request.
496 */
497 err = write_inode_now(inode, 0);
498 if (err)
Josef Bacik02c24a82011-07-16 20:44:56 -0400499 goto out;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700500
501 fuse_sync_writes(inode);
502
Maxim Patlasovb0aa7602013-12-26 19:51:11 +0400503 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
504 int err = fuse_flush_mtime(file, false);
505 if (err)
506 goto out;
507 }
508
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400509 req = fuse_get_req_nopages(fc);
Josef Bacik02c24a82011-07-16 20:44:56 -0400510 if (IS_ERR(req)) {
511 err = PTR_ERR(req);
512 goto out;
513 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700514
515 memset(&inarg, 0, sizeof(inarg));
516 inarg.fh = ff->fh;
517 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700518 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700519 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700520 req->in.numargs = 1;
521 req->in.args[0].size = sizeof(inarg);
522 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100523 fuse_request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700524 err = req->out.h.error;
525 fuse_put_request(fc, req);
526 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700527 if (isdir)
528 fc->no_fsyncdir = 1;
529 else
530 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700531 err = 0;
532 }
Josef Bacik02c24a82011-07-16 20:44:56 -0400533out:
534 mutex_unlock(&inode->i_mutex);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700535 return err;
536}
537
Josef Bacik02c24a82011-07-16 20:44:56 -0400538static int fuse_fsync(struct file *file, loff_t start, loff_t end,
539 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -0700540{
Josef Bacik02c24a82011-07-16 20:44:56 -0400541 return fuse_fsync_common(file, start, end, datasync, 0);
Miklos Szeredi82547982005-09-09 13:10:38 -0700542}
543
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200544void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
545 size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700546{
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700547 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredia6643092007-11-28 16:22:00 -0800548 struct fuse_file *ff = file->private_data;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700549
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800550 inarg->fh = ff->fh;
551 inarg->offset = pos;
552 inarg->size = count;
Miklos Szeredia6643092007-11-28 16:22:00 -0800553 inarg->flags = file->f_flags;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800554 req->in.h.opcode = opcode;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200555 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700556 req->in.numargs = 1;
557 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800558 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700559 req->out.argvar = 1;
560 req->out.numargs = 1;
561 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700562}
563
Maxim Patlasov187c5c32012-12-14 19:20:25 +0400564static void fuse_release_user_pages(struct fuse_req *req, int write)
565{
566 unsigned i;
567
568 for (i = 0; i < req->num_pages; i++) {
569 struct page *page = req->pages[i];
570 if (write)
571 set_page_dirty_lock(page);
572 put_page(page);
573 }
574}
575
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400576/**
577 * In case of short read, the caller sets 'pos' to the position of
578 * actual end of fuse request in IO request. Otherwise, if bytes_requested
579 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
580 *
581 * An example:
582 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
583 * both submitted asynchronously. The first of them was ACKed by userspace as
584 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
585 * second request was ACKed as short, e.g. only 1K was read, resulting in
586 * pos == 33K.
587 *
588 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
589 * will be equal to the length of the longest contiguous fragment of
590 * transferred data starting from the beginning of IO request.
591 */
592static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
593{
594 int left;
595
596 spin_lock(&io->lock);
597 if (err)
598 io->err = io->err ? : err;
599 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
600 io->bytes = pos;
601
602 left = --io->reqs;
603 spin_unlock(&io->lock);
604
605 if (!left) {
606 long res;
607
608 if (io->err)
609 res = io->err;
610 else if (io->bytes >= 0 && io->write)
611 res = -EIO;
612 else {
613 res = io->bytes < 0 ? io->size : io->bytes;
614
615 if (!is_sync_kiocb(io->iocb)) {
Al Virocb5e05d2013-06-16 20:05:23 +0400616 struct inode *inode = file_inode(io->iocb->ki_filp);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400617 struct fuse_conn *fc = get_fuse_conn(inode);
618 struct fuse_inode *fi = get_fuse_inode(inode);
619
620 spin_lock(&fc->lock);
621 fi->attr_version = ++fc->attr_version;
622 spin_unlock(&fc->lock);
623 }
624 }
625
626 aio_complete(io->iocb, res, 0);
627 kfree(io);
628 }
629}
630
631static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
632{
633 struct fuse_io_priv *io = req->io;
634 ssize_t pos = -1;
635
636 fuse_release_user_pages(req, !io->write);
637
638 if (io->write) {
639 if (req->misc.write.in.size != req->misc.write.out.size)
640 pos = req->misc.write.in.offset - io->offset +
641 req->misc.write.out.size;
642 } else {
643 if (req->misc.read.in.size != req->out.args[0].size)
644 pos = req->misc.read.in.offset - io->offset +
645 req->out.args[0].size;
646 }
647
648 fuse_aio_complete(io, req->out.h.error, pos);
649}
650
651static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
652 size_t num_bytes, struct fuse_io_priv *io)
653{
654 spin_lock(&io->lock);
655 io->size += num_bytes;
656 io->reqs++;
657 spin_unlock(&io->lock);
658
659 req->io = io;
660 req->end = fuse_aio_complete_req;
661
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400662 __fuse_get_request(req);
Maxim Patlasov01e9d112012-12-14 19:20:41 +0400663 fuse_request_send_background(fc, req);
664
665 return num_bytes;
666}
667
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400668static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200669 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700670{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400671 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200672 struct fuse_file *ff = file->private_data;
673 struct fuse_conn *fc = ff->fc;
Miklos Szeredif3332112007-10-18 03:07:04 -0700674
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200675 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredif3332112007-10-18 03:07:04 -0700676 if (owner != NULL) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700677 struct fuse_read_in *inarg = &req->misc.read.in;
Miklos Szeredif3332112007-10-18 03:07:04 -0700678
679 inarg->read_flags |= FUSE_READ_LOCKOWNER;
680 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
681 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400682
683 if (io->async)
684 return fuse_async_req_send(fc, req, count, io);
685
Tejun Heob93f8582008-11-26 12:03:55 +0100686 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800687 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700688}
689
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700690static void fuse_read_update_size(struct inode *inode, loff_t size,
691 u64 attr_ver)
692{
693 struct fuse_conn *fc = get_fuse_conn(inode);
694 struct fuse_inode *fi = get_fuse_inode(inode);
695
696 spin_lock(&fc->lock);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +0400697 if (attr_ver == fi->attr_version && size < inode->i_size &&
698 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700699 fi->attr_version = ++fc->attr_version;
700 i_size_write(inode, size);
701 }
702 spin_unlock(&fc->lock);
703}
704
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400705static void fuse_short_read(struct fuse_req *req, struct inode *inode,
706 u64 attr_ver)
707{
708 size_t num_read = req->out.args[0].size;
Pavel Emelyanov83732002013-10-10 17:10:46 +0400709 struct fuse_conn *fc = get_fuse_conn(inode);
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400710
Pavel Emelyanov83732002013-10-10 17:10:46 +0400711 if (fc->writeback_cache) {
712 /*
713 * A hole in a file. Some data after the hole are in page cache,
714 * but have not reached the client fs yet. So, the hole is not
715 * present there.
716 */
717 int i;
718 int start_idx = num_read >> PAGE_CACHE_SHIFT;
719 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
720
721 for (i = start_idx; i < req->num_pages; i++) {
722 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
723 off = 0;
724 }
725 } else {
726 loff_t pos = page_offset(req->pages[0]) + num_read;
727 fuse_read_update_size(inode, pos, attr_ver);
728 }
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400729}
730
Maxim Patlasov482fce52013-10-10 17:11:25 +0400731static int fuse_do_readpage(struct file *file, struct page *page)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700732{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400733 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700734 struct inode *inode = page->mapping->host;
735 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800736 struct fuse_req *req;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700737 size_t num_read;
738 loff_t pos = page_offset(page);
739 size_t count = PAGE_CACHE_SIZE;
740 u64 attr_ver;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800741 int err;
742
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700743 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300744 * Page writeback can extend beyond the lifetime of the
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700745 * page-cache page, so make sure we read a properly synced
746 * page.
747 */
748 fuse_wait_on_page_writeback(inode, page->index);
749
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400750 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700751 if (IS_ERR(req))
Maxim Patlasov482fce52013-10-10 17:11:25 +0400752 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700753
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700754 attr_ver = fuse_get_attr_version(fc);
755
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700756 req->out.page_zeroing = 1;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200757 req->out.argpages = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700758 req->num_pages = 1;
759 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400760 req->page_descs[0].length = count;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400761 num_read = fuse_send_read(req, &io, pos, count, NULL);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700762 err = req->out.h.error;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700763
764 if (!err) {
765 /*
766 * Short read means EOF. If file size is larger, truncate it
767 */
768 if (num_read < count)
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400769 fuse_short_read(req, inode, attr_ver);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700770
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700771 SetPageUptodate(page);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700772 }
773
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400774 fuse_put_request(fc, req);
Maxim Patlasov482fce52013-10-10 17:11:25 +0400775
776 return err;
777}
778
779static int fuse_readpage(struct file *file, struct page *page)
780{
781 struct inode *inode = page->mapping->host;
782 int err;
783
784 err = -EIO;
785 if (is_bad_inode(inode))
786 goto out;
787
788 err = fuse_do_readpage(file, page);
Andrew Gallagher451418f2013-11-05 03:55:43 -0800789 fuse_invalidate_atime(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700790 out:
791 unlock_page(page);
792 return err;
793}
794
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800795static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700796{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800797 int i;
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700798 size_t count = req->misc.read.in.size;
799 size_t num_read = req->out.args[0].size;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200800 struct address_space *mapping = NULL;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800801
Miklos Szeredice534fb2010-05-25 15:06:07 +0200802 for (i = 0; mapping == NULL && i < req->num_pages; i++)
803 mapping = req->pages[i]->mapping;
804
805 if (mapping) {
806 struct inode *inode = mapping->host;
807
808 /*
809 * Short read means EOF. If file size is larger, truncate it
810 */
Pavel Emelyanova92adc82013-10-10 17:10:16 +0400811 if (!req->out.h.error && num_read < count)
812 fuse_short_read(req, inode, req->misc.read.attr_ver);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200813
Andrew Gallagher451418f2013-11-05 03:55:43 -0800814 fuse_invalidate_atime(inode);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700815 }
816
Miklos Szeredidb50b962005-09-09 13:10:33 -0700817 for (i = 0; i < req->num_pages; i++) {
818 struct page *page = req->pages[i];
819 if (!req->out.h.error)
820 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800821 else
822 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700823 unlock_page(page);
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200824 page_cache_release(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700825 }
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700826 if (req->ff)
Miklos Szeredi5a18ec12011-02-25 14:44:58 +0100827 fuse_file_put(req->ff, false);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800828}
829
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200830static void fuse_send_readpages(struct fuse_req *req, struct file *file)
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800831{
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200832 struct fuse_file *ff = file->private_data;
833 struct fuse_conn *fc = ff->fc;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800834 loff_t pos = page_offset(req->pages[0]);
835 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
Miklos Szeredif4975c62009-04-02 14:25:34 +0200836
837 req->out.argpages = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800838 req->out.page_zeroing = 1;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200839 req->out.page_replace = 1;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200840 fuse_read_fill(req, file, pos, count, FUSE_READ);
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700841 req->misc.read.attr_ver = fuse_get_attr_version(fc);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800842 if (fc->async_read) {
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700843 req->ff = fuse_file_get(ff);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800844 req->end = fuse_readpages_end;
Tejun Heob93f8582008-11-26 12:03:55 +0100845 fuse_request_send_background(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800846 } else {
Tejun Heob93f8582008-11-26 12:03:55 +0100847 fuse_request_send(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800848 fuse_readpages_end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100849 fuse_put_request(fc, req);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800850 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700851}
852
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700853struct fuse_fill_data {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700854 struct fuse_req *req;
Miklos Szeredia6643092007-11-28 16:22:00 -0800855 struct file *file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700856 struct inode *inode;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400857 unsigned nr_pages;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700858};
859
860static int fuse_readpages_fill(void *_data, struct page *page)
861{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700862 struct fuse_fill_data *data = _data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700863 struct fuse_req *req = data->req;
864 struct inode *inode = data->inode;
865 struct fuse_conn *fc = get_fuse_conn(inode);
866
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700867 fuse_wait_on_page_writeback(inode, page->index);
868
Miklos Szeredidb50b962005-09-09 13:10:33 -0700869 if (req->num_pages &&
870 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
871 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
872 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400873 int nr_alloc = min_t(unsigned, data->nr_pages,
874 FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200875 fuse_send_readpages(req, data->file);
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400876 if (fc->async_read)
877 req = fuse_get_req_for_background(fc, nr_alloc);
878 else
879 req = fuse_get_req(fc, nr_alloc);
880
881 data->req = req;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700882 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700883 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700884 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700885 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700886 }
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400887
888 if (WARN_ON(req->num_pages >= req->max_pages)) {
889 fuse_put_request(fc, req);
890 return -EIO;
891 }
892
Miklos Szeredib5dd3282010-05-25 15:06:06 +0200893 page_cache_get(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700894 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400895 req->page_descs[req->num_pages].length = PAGE_SIZE;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100896 req->num_pages++;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400897 data->nr_pages--;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700898 return 0;
899}
900
901static int fuse_readpages(struct file *file, struct address_space *mapping,
902 struct list_head *pages, unsigned nr_pages)
903{
904 struct inode *inode = mapping->host;
905 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700906 struct fuse_fill_data data;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700907 int err;
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400908 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800909
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700910 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800911 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800912 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800913
Miklos Szeredia6643092007-11-28 16:22:00 -0800914 data.file = file;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700915 data.inode = inode;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400916 if (fc->async_read)
917 data.req = fuse_get_req_for_background(fc, nr_alloc);
918 else
919 data.req = fuse_get_req(fc, nr_alloc);
Maxim Patlasovf8dbdf82012-10-26 19:48:51 +0400920 data.nr_pages = nr_pages;
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700921 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700922 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800923 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700924
925 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700926 if (!err) {
927 if (data.req->num_pages)
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200928 fuse_send_readpages(data.req, file);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700929 else
930 fuse_put_request(fc, data.req);
931 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800932out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700933 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700934}
935
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800936static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
937 unsigned long nr_segs, loff_t pos)
938{
939 struct inode *inode = iocb->ki_filp->f_mapping->host;
Brian Fostera8894272012-07-16 15:23:50 -0400940 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800941
Brian Fostera8894272012-07-16 15:23:50 -0400942 /*
943 * In auto invalidate mode, always update attributes on read.
944 * Otherwise, only update if we attempt to read past EOF (to ensure
945 * i_size is up to date).
946 */
947 if (fc->auto_inval_data ||
948 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800949 int err;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800950 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
951 if (err)
952 return err;
953 }
954
955 return generic_file_aio_read(iocb, iov, nr_segs, pos);
956}
957
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200958static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200959 loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700960{
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700961 struct fuse_write_in *inarg = &req->misc.write.in;
962 struct fuse_write_out *outarg = &req->misc.write.out;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700963
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700964 inarg->fh = ff->fh;
965 inarg->offset = pos;
966 inarg->size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700967 req->in.h.opcode = FUSE_WRITE;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200968 req->in.h.nodeid = ff->nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700969 req->in.numargs = 2;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200970 if (ff->fc->minor < 9)
Miklos Szeredif3332112007-10-18 03:07:04 -0700971 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
972 else
973 req->in.args[0].size = sizeof(struct fuse_write_in);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700974 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700975 req->in.args[1].size = count;
976 req->out.numargs = 1;
977 req->out.args[0].size = sizeof(struct fuse_write_out);
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700978 req->out.args[0].value = outarg;
979}
980
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400981static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200982 loff_t pos, size_t count, fl_owner_t owner)
Miklos Szeredib25e82e2007-10-18 03:07:03 -0700983{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400984 struct file *file = io->file;
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200985 struct fuse_file *ff = file->private_data;
986 struct fuse_conn *fc = ff->fc;
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200987 struct fuse_write_in *inarg = &req->misc.write.in;
988
Miklos Szeredi2106cb12009-04-28 16:56:37 +0200989 fuse_write_fill(req, ff, pos, count);
Miklos Szeredi2d698b02009-04-28 16:56:36 +0200990 inarg->flags = file->f_flags;
Miklos Szeredif3332112007-10-18 03:07:04 -0700991 if (owner != NULL) {
Miklos Szeredif3332112007-10-18 03:07:04 -0700992 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
993 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
994 }
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400995
996 if (io->async)
997 return fuse_async_req_send(fc, req, count, io);
998
Tejun Heob93f8582008-11-26 12:03:55 +0100999 fuse_request_send(fc, req);
Miklos Szeredib25e82e2007-10-18 03:07:03 -07001000 return req->misc.write.out.size;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001001}
1002
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001003bool fuse_write_update_size(struct inode *inode, loff_t pos)
Miklos Szeredi854512e2008-04-30 00:54:41 -07001004{
1005 struct fuse_conn *fc = get_fuse_conn(inode);
1006 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001007 bool ret = false;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001008
1009 spin_lock(&fc->lock);
1010 fi->attr_version = ++fc->attr_version;
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001011 if (pos > inode->i_size) {
Miklos Szeredi854512e2008-04-30 00:54:41 -07001012 i_size_write(inode, pos);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001013 ret = true;
1014 }
Miklos Szeredi854512e2008-04-30 00:54:41 -07001015 spin_unlock(&fc->lock);
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04001016
1017 return ret;
Miklos Szeredi854512e2008-04-30 00:54:41 -07001018}
1019
Nick Pigginea9b9902008-04-30 00:54:42 -07001020static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1021 struct inode *inode, loff_t pos,
1022 size_t count)
1023{
1024 size_t res;
1025 unsigned offset;
1026 unsigned i;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001027 struct fuse_io_priv io = { .async = 0, .file = file };
Nick Pigginea9b9902008-04-30 00:54:42 -07001028
1029 for (i = 0; i < req->num_pages; i++)
1030 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1031
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001032 res = fuse_send_write(req, &io, pos, count, NULL);
Nick Pigginea9b9902008-04-30 00:54:42 -07001033
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001034 offset = req->page_descs[0].offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001035 count = res;
1036 for (i = 0; i < req->num_pages; i++) {
1037 struct page *page = req->pages[i];
1038
1039 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1040 SetPageUptodate(page);
1041
1042 if (count > PAGE_CACHE_SIZE - offset)
1043 count -= PAGE_CACHE_SIZE - offset;
1044 else
1045 count = 0;
1046 offset = 0;
1047
1048 unlock_page(page);
1049 page_cache_release(page);
1050 }
1051
1052 return res;
1053}
1054
1055static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1056 struct address_space *mapping,
1057 struct iov_iter *ii, loff_t pos)
1058{
1059 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1060 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1061 size_t count = 0;
1062 int err;
1063
Miklos Szeredif4975c62009-04-02 14:25:34 +02001064 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001065 req->page_descs[0].offset = offset;
Nick Pigginea9b9902008-04-30 00:54:42 -07001066
1067 do {
1068 size_t tmp;
1069 struct page *page;
1070 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1071 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1072 iov_iter_count(ii));
1073
1074 bytes = min_t(size_t, bytes, fc->max_write - count);
1075
1076 again:
1077 err = -EFAULT;
1078 if (iov_iter_fault_in_readable(ii, bytes))
1079 break;
1080
1081 err = -ENOMEM;
Nick Piggin54566b22009-01-04 12:00:53 -08001082 page = grab_cache_page_write_begin(mapping, index, 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001083 if (!page)
1084 break;
1085
anfei zhou931e80e2010-02-02 13:44:02 -08001086 if (mapping_writably_mapped(mapping))
1087 flush_dcache_page(page);
1088
Nick Pigginea9b9902008-04-30 00:54:42 -07001089 pagefault_disable();
1090 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1091 pagefault_enable();
1092 flush_dcache_page(page);
1093
Johannes Weiner478e0842011-07-25 22:35:35 +02001094 mark_page_accessed(page);
1095
Nick Pigginea9b9902008-04-30 00:54:42 -07001096 if (!tmp) {
1097 unlock_page(page);
1098 page_cache_release(page);
1099 bytes = min(bytes, iov_iter_single_seg_count(ii));
1100 goto again;
1101 }
1102
1103 err = 0;
1104 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001105 req->page_descs[req->num_pages].length = tmp;
Nick Pigginea9b9902008-04-30 00:54:42 -07001106 req->num_pages++;
1107
1108 iov_iter_advance(ii, tmp);
1109 count += tmp;
1110 pos += tmp;
1111 offset += tmp;
1112 if (offset == PAGE_CACHE_SIZE)
1113 offset = 0;
1114
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -07001115 if (!fc->big_writes)
1116 break;
Nick Pigginea9b9902008-04-30 00:54:42 -07001117 } while (iov_iter_count(ii) && count < fc->max_write &&
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001118 req->num_pages < req->max_pages && offset == 0);
Nick Pigginea9b9902008-04-30 00:54:42 -07001119
1120 return count > 0 ? count : err;
1121}
1122
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001123static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1124{
1125 return min_t(unsigned,
1126 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1127 (pos >> PAGE_CACHE_SHIFT) + 1,
1128 FUSE_MAX_PAGES_PER_REQ);
1129}
1130
Nick Pigginea9b9902008-04-30 00:54:42 -07001131static ssize_t fuse_perform_write(struct file *file,
1132 struct address_space *mapping,
1133 struct iov_iter *ii, loff_t pos)
1134{
1135 struct inode *inode = mapping->host;
1136 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001137 struct fuse_inode *fi = get_fuse_inode(inode);
Nick Pigginea9b9902008-04-30 00:54:42 -07001138 int err = 0;
1139 ssize_t res = 0;
1140
1141 if (is_bad_inode(inode))
1142 return -EIO;
1143
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001144 if (inode->i_size < pos + iov_iter_count(ii))
1145 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1146
Nick Pigginea9b9902008-04-30 00:54:42 -07001147 do {
1148 struct fuse_req *req;
1149 ssize_t count;
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001150 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
Nick Pigginea9b9902008-04-30 00:54:42 -07001151
Maxim Patlasovd07f09f2012-10-26 19:49:00 +04001152 req = fuse_get_req(fc, nr_pages);
Nick Pigginea9b9902008-04-30 00:54:42 -07001153 if (IS_ERR(req)) {
1154 err = PTR_ERR(req);
1155 break;
1156 }
1157
1158 count = fuse_fill_write_pages(req, mapping, ii, pos);
1159 if (count <= 0) {
1160 err = count;
1161 } else {
1162 size_t num_written;
1163
1164 num_written = fuse_send_write_pages(req, file, inode,
1165 pos, count);
1166 err = req->out.h.error;
1167 if (!err) {
1168 res += num_written;
1169 pos += num_written;
1170
1171 /* break out of the loop on short write */
1172 if (num_written != count)
1173 err = -EIO;
1174 }
1175 }
1176 fuse_put_request(fc, req);
1177 } while (!err && iov_iter_count(ii));
1178
1179 if (res > 0)
1180 fuse_write_update_size(inode, pos);
1181
Maxim Patlasov06a7c3c2013-08-30 17:06:04 +04001182 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
Nick Pigginea9b9902008-04-30 00:54:42 -07001183 fuse_invalidate_attr(inode);
1184
1185 return res > 0 ? res : err;
1186}
1187
1188static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1189 unsigned long nr_segs, loff_t pos)
1190{
1191 struct file *file = iocb->ki_filp;
1192 struct address_space *mapping = file->f_mapping;
1193 size_t count = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001194 size_t ocount = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001195 ssize_t written = 0;
Anand Avati4273b792012-02-17 12:46:25 -05001196 ssize_t written_buffered = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001197 struct inode *inode = mapping->host;
1198 ssize_t err;
1199 struct iov_iter i;
Anand Avati4273b792012-02-17 12:46:25 -05001200 loff_t endbyte = 0;
Nick Pigginea9b9902008-04-30 00:54:42 -07001201
Pavel Emelyanov4d99ff82013-10-10 17:12:18 +04001202 if (get_fuse_conn(inode)->writeback_cache) {
1203 /* Update size (EOF optimization) and mode (SUID clearing) */
1204 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1205 if (err)
1206 return err;
1207
1208 return generic_file_aio_write(iocb, iov, nr_segs, pos);
1209 }
1210
Nick Pigginea9b9902008-04-30 00:54:42 -07001211 WARN_ON(iocb->ki_pos != pos);
1212
Anand Avati4273b792012-02-17 12:46:25 -05001213 ocount = 0;
1214 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
Nick Pigginea9b9902008-04-30 00:54:42 -07001215 if (err)
1216 return err;
1217
Anand Avati4273b792012-02-17 12:46:25 -05001218 count = ocount;
Nick Pigginea9b9902008-04-30 00:54:42 -07001219 mutex_lock(&inode->i_mutex);
Nick Pigginea9b9902008-04-30 00:54:42 -07001220
1221 /* We can write back this queue in page reclaim */
1222 current->backing_dev_info = mapping->backing_dev_info;
1223
1224 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1225 if (err)
1226 goto out;
1227
1228 if (count == 0)
1229 goto out;
1230
Miklos Szeredi2f1936b2008-06-24 16:50:14 +02001231 err = file_remove_suid(file);
Nick Pigginea9b9902008-04-30 00:54:42 -07001232 if (err)
1233 goto out;
1234
Josef Bacikc3b2da32012-03-26 09:59:21 -04001235 err = file_update_time(file);
1236 if (err)
1237 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001238
Anand Avati4273b792012-02-17 12:46:25 -05001239 if (file->f_flags & O_DIRECT) {
1240 written = generic_file_direct_write(iocb, iov, &nr_segs,
1241 pos, &iocb->ki_pos,
1242 count, ocount);
1243 if (written < 0 || written == count)
1244 goto out;
Nick Pigginea9b9902008-04-30 00:54:42 -07001245
Anand Avati4273b792012-02-17 12:46:25 -05001246 pos += written;
1247 count -= written;
1248
1249 iov_iter_init(&i, iov, nr_segs, count, written);
1250 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1251 if (written_buffered < 0) {
1252 err = written_buffered;
1253 goto out;
1254 }
1255 endbyte = pos + written_buffered - 1;
1256
1257 err = filemap_write_and_wait_range(file->f_mapping, pos,
1258 endbyte);
1259 if (err)
1260 goto out;
1261
1262 invalidate_mapping_pages(file->f_mapping,
1263 pos >> PAGE_CACHE_SHIFT,
1264 endbyte >> PAGE_CACHE_SHIFT);
1265
1266 written += written_buffered;
1267 iocb->ki_pos = pos + written_buffered;
1268 } else {
1269 iov_iter_init(&i, iov, nr_segs, count, 0);
1270 written = fuse_perform_write(file, mapping, &i, pos);
1271 if (written >= 0)
1272 iocb->ki_pos = pos + written;
1273 }
Nick Pigginea9b9902008-04-30 00:54:42 -07001274out:
1275 current->backing_dev_info = NULL;
1276 mutex_unlock(&inode->i_mutex);
1277
1278 return written ? written : err;
1279}
1280
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001281static inline void fuse_page_descs_length_init(struct fuse_req *req,
1282 unsigned index, unsigned nr_pages)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001283{
1284 int i;
1285
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001286 for (i = index; i < index + nr_pages; i++)
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001287 req->page_descs[i].length = PAGE_SIZE -
1288 req->page_descs[i].offset;
1289}
1290
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001291static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1292{
1293 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1294}
1295
1296static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1297 size_t max_size)
1298{
1299 return min(iov_iter_single_seg_count(ii), max_size);
1300}
1301
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001302static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
Miklos Szeredice60a2f2009-04-09 17:37:52 +02001303 size_t *nbytesp, int write)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001304{
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001305 size_t nbytes = 0; /* # bytes already packed in req */
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001306
Miklos Szeredif4975c62009-04-02 14:25:34 +02001307 /* Special case for kernel I/O: can copy directly into the buffer */
1308 if (segment_eq(get_fs(), KERNEL_DS)) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001309 unsigned long user_addr = fuse_get_user_addr(ii);
1310 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1311
Miklos Szeredif4975c62009-04-02 14:25:34 +02001312 if (write)
1313 req->in.args[1].value = (void *) user_addr;
1314 else
1315 req->out.args[0].value = (void *) user_addr;
1316
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001317 iov_iter_advance(ii, frag_size);
1318 *nbytesp = frag_size;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001319 return 0;
1320 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001321
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001322 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001323 unsigned npages;
1324 unsigned long user_addr = fuse_get_user_addr(ii);
1325 unsigned offset = user_addr & ~PAGE_MASK;
1326 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1327 int ret;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001328
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001329 unsigned n = req->max_pages - req->num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001330 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1331
1332 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1333 npages = clamp(npages, 1U, n);
1334
1335 ret = get_user_pages_fast(user_addr, npages, !write,
1336 &req->pages[req->num_pages]);
1337 if (ret < 0)
1338 return ret;
1339
1340 npages = ret;
1341 frag_size = min_t(size_t, frag_size,
1342 (npages << PAGE_SHIFT) - offset);
1343 iov_iter_advance(ii, frag_size);
1344
1345 req->page_descs[req->num_pages].offset = offset;
1346 fuse_page_descs_length_init(req, req->num_pages, npages);
1347
1348 req->num_pages += npages;
1349 req->page_descs[req->num_pages - 1].length -=
1350 (npages << PAGE_SHIFT) - offset - frag_size;
1351
1352 nbytes += frag_size;
1353 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001354
1355 if (write)
1356 req->in.argpages = 1;
1357 else
1358 req->out.argpages = 1;
1359
Maxim Patlasov7c190c82012-10-26 19:50:29 +04001360 *nbytesp = nbytes;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001361
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001362 return 0;
1363}
1364
Maxim Patlasov5565a9d2012-10-26 19:50:36 +04001365static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1366{
1367 struct iov_iter ii = *ii_p;
1368 int npages = 0;
1369
1370 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1371 unsigned long user_addr = fuse_get_user_addr(&ii);
1372 unsigned offset = user_addr & ~PAGE_MASK;
1373 size_t frag_size = iov_iter_single_seg_count(&ii);
1374
1375 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1376 iov_iter_advance(&ii, frag_size);
1377 }
1378
1379 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1380}
1381
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001382ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
Miklos Szeredifb05f412012-11-10 16:55:56 +01001383 unsigned long nr_segs, size_t count, loff_t *ppos,
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001384 int flags)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001385{
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001386 int write = flags & FUSE_DIO_WRITE;
1387 int cuse = flags & FUSE_DIO_CUSE;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001388 struct file *file = io->file;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001389 struct inode *inode = file->f_mapping->host;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001390 struct fuse_file *ff = file->private_data;
1391 struct fuse_conn *fc = ff->fc;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001392 size_t nmax = write ? fc->max_write : fc->max_read;
1393 loff_t pos = *ppos;
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001394 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1395 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001396 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001397 struct fuse_req *req;
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001398 struct iov_iter ii;
1399
1400 iov_iter_init(&ii, iov, nr_segs, count, 0);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001401
Brian Fosterde82b922013-05-14 11:25:39 -04001402 if (io->async)
1403 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1404 else
1405 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001406 if (IS_ERR(req))
1407 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001408
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001409 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1410 if (!write)
1411 mutex_lock(&inode->i_mutex);
1412 fuse_sync_writes(inode);
1413 if (!write)
1414 mutex_unlock(&inode->i_mutex);
1415 }
1416
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001417 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001418 size_t nres;
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001419 fl_owner_t owner = current->files;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001420 size_t nbytes = min(count, nmax);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001421 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001422 if (err) {
1423 res = err;
1424 break;
1425 }
Miklos Szeredif4975c62009-04-02 14:25:34 +02001426
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001427 if (write)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001428 nres = fuse_send_write(req, io, pos, nbytes, owner);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001429 else
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001430 nres = fuse_send_read(req, io, pos, nbytes, owner);
Miklos Szeredi2106cb12009-04-28 16:56:37 +02001431
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001432 if (!io->async)
1433 fuse_release_user_pages(req, !write);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001434 if (req->out.h.error) {
1435 if (!res)
1436 res = req->out.h.error;
1437 break;
1438 } else if (nres > nbytes) {
1439 res = -EIO;
1440 break;
1441 }
1442 count -= nres;
1443 res += nres;
1444 pos += nres;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001445 if (nres != nbytes)
1446 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001447 if (count) {
1448 fuse_put_request(fc, req);
Brian Fosterde82b922013-05-14 11:25:39 -04001449 if (io->async)
1450 req = fuse_get_req_for_background(fc,
1451 fuse_iter_npages(&ii));
1452 else
1453 req = fuse_get_req(fc, fuse_iter_npages(&ii));
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02001454 if (IS_ERR(req))
1455 break;
1456 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001457 }
Anand V. Avatif60311d2009-10-22 06:24:52 -07001458 if (!IS_ERR(req))
1459 fuse_put_request(fc, req);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001460 if (res > 0)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001461 *ppos = pos;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001462
1463 return res;
1464}
Tejun Heo08cbf542009-04-14 10:54:53 +09001465EXPORT_SYMBOL_GPL(fuse_direct_io);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001466
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001467static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1468 const struct iovec *iov,
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001469 unsigned long nr_segs, loff_t *ppos,
1470 size_t count)
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001471{
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001472 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001473 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001474 struct inode *inode = file_inode(file);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001475
1476 if (is_bad_inode(inode))
1477 return -EIO;
1478
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001479 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001480
1481 fuse_invalidate_attr(inode);
1482
1483 return res;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001484}
1485
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001486static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1487 size_t count, loff_t *ppos)
1488{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001489 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredifb05f412012-11-10 16:55:56 +01001490 struct iovec iov = { .iov_base = buf, .iov_len = count };
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04001491 return __fuse_direct_read(&io, &iov, 1, ppos, count);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001492}
1493
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001494static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1495 const struct iovec *iov,
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001496 unsigned long nr_segs, loff_t *ppos)
Anand Avati4273b792012-02-17 12:46:25 -05001497{
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001498 struct file *file = io->file;
Al Viro6131ffa2013-02-27 16:59:05 -05001499 struct inode *inode = file_inode(file);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04001500 size_t count = iov_length(iov, nr_segs);
Anand Avati4273b792012-02-17 12:46:25 -05001501 ssize_t res;
1502
1503 res = generic_write_checks(file, ppos, &count, 0);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001504 if (!res)
Pavel Emelyanovea8cd332013-10-10 17:12:05 +04001505 res = fuse_direct_io(io, iov, nr_segs, count, ppos,
1506 FUSE_DIO_WRITE);
Anand Avati4273b792012-02-17 12:46:25 -05001507
1508 fuse_invalidate_attr(inode);
1509
1510 return res;
1511}
1512
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001513static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1514 size_t count, loff_t *ppos)
1515{
Miklos Szeredifb05f412012-11-10 16:55:56 +01001516 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
Al Viro6131ffa2013-02-27 16:59:05 -05001517 struct inode *inode = file_inode(file);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001518 ssize_t res;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001519 struct fuse_io_priv io = { .async = 0, .file = file };
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001520
1521 if (is_bad_inode(inode))
1522 return -EIO;
1523
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001524 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001525 mutex_lock(&inode->i_mutex);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04001526 res = __fuse_direct_write(&io, &iov, 1, ppos);
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04001527 if (res > 0)
1528 fuse_write_update_size(inode, *ppos);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001529 mutex_unlock(&inode->i_mutex);
Miklos Szeredid09cb9d2009-04-28 16:56:36 +02001530
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07001531 return res;
1532}
1533
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001534static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001535{
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001536 int i;
1537
1538 for (i = 0; i < req->num_pages; i++)
1539 __free_page(req->pages[i]);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001540
1541 if (req->ff)
1542 fuse_file_put(req->ff, false);
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001543}
1544
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001545static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001546{
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001547 struct inode *inode = req->inode;
1548 struct fuse_inode *fi = get_fuse_inode(inode);
1549 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001550 int i;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001551
1552 list_del(&req->writepages_entry);
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001553 for (i = 0; i < req->num_pages; i++) {
1554 dec_bdi_stat(bdi, BDI_WRITEBACK);
1555 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1556 bdi_writeout_inc(bdi);
1557 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001558 wake_up(&fi->page_waitq);
1559}
1560
1561/* Called under fc->lock, may release and reacquire it */
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001562static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1563 loff_t size)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001564__releases(fc->lock)
1565__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001566{
1567 struct fuse_inode *fi = get_fuse_inode(req->inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001568 struct fuse_write_in *inarg = &req->misc.write.in;
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001569 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001570
1571 if (!fc->connected)
1572 goto out_free;
1573
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001574 if (inarg->offset + data_size <= size) {
1575 inarg->size = data_size;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001576 } else if (inarg->offset < size) {
Pavel Emelyanov385b1262013-06-29 21:42:48 +04001577 inarg->size = size - inarg->offset;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001578 } else {
1579 /* Got truncated off completely */
1580 goto out_free;
1581 }
1582
1583 req->in.args[1].size = inarg->size;
1584 fi->writectr++;
Tejun Heob93f8582008-11-26 12:03:55 +01001585 fuse_request_send_background_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001586 return;
1587
1588 out_free:
1589 fuse_writepage_finish(fc, req);
1590 spin_unlock(&fc->lock);
1591 fuse_writepage_free(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001592 fuse_put_request(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001593 spin_lock(&fc->lock);
1594}
1595
1596/*
1597 * If fi->writectr is positive (no truncate or fsync going on) send
1598 * all queued writepage requests.
1599 *
1600 * Called with fc->lock
1601 */
1602void fuse_flush_writepages(struct inode *inode)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001603__releases(fc->lock)
1604__acquires(fc->lock)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001605{
1606 struct fuse_conn *fc = get_fuse_conn(inode);
1607 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001608 size_t crop = i_size_read(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001609 struct fuse_req *req;
1610
1611 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1612 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1613 list_del_init(&req->list);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001614 fuse_send_writepage(fc, req, crop);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001615 }
1616}
1617
1618static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1619{
1620 struct inode *inode = req->inode;
1621 struct fuse_inode *fi = get_fuse_inode(inode);
1622
1623 mapping_set_error(inode->i_mapping, req->out.h.error);
1624 spin_lock(&fc->lock);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001625 while (req->misc.write.next) {
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001626 struct fuse_conn *fc = get_fuse_conn(inode);
1627 struct fuse_write_in *inarg = &req->misc.write.in;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001628 struct fuse_req *next = req->misc.write.next;
1629 req->misc.write.next = next->misc.write.next;
1630 next->misc.write.next = NULL;
Maxim Patlasovce128de2013-10-02 21:38:54 +04001631 next->ff = fuse_file_get(req->ff);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001632 list_add(&next->writepages_entry, &fi->writepages);
Maxim Patlasov6eaf4782013-10-02 21:38:32 +04001633
1634 /*
1635 * Skip fuse_flush_writepages() to make it easy to crop requests
1636 * based on primary request size.
1637 *
1638 * 1st case (trivial): there are no concurrent activities using
1639 * fuse_set/release_nowrite. Then we're on safe side because
1640 * fuse_flush_writepages() would call fuse_send_writepage()
1641 * anyway.
1642 *
1643 * 2nd case: someone called fuse_set_nowrite and it is waiting
1644 * now for completion of all in-flight requests. This happens
1645 * rarely and no more than once per page, so this should be
1646 * okay.
1647 *
1648 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1649 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1650 * that fuse_set_nowrite returned implies that all in-flight
1651 * requests were completed along with all of their secondary
1652 * requests. Further primary requests are blocked by negative
1653 * writectr. Hence there cannot be any in-flight requests and
1654 * no invocations of fuse_writepage_end() while we're in
1655 * fuse_set_nowrite..fuse_release_nowrite section.
1656 */
1657 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001658 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001659 fi->writectr--;
1660 fuse_writepage_finish(fc, req);
1661 spin_unlock(&fc->lock);
1662 fuse_writepage_free(fc, req);
1663}
1664
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001665static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1666 struct fuse_inode *fi)
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001667{
Miklos Szeredi72523422013-10-01 16:44:52 +02001668 struct fuse_file *ff = NULL;
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001669
1670 spin_lock(&fc->lock);
Miklos Szeredi72523422013-10-01 16:44:52 +02001671 if (!WARN_ON(list_empty(&fi->write_files))) {
1672 ff = list_entry(fi->write_files.next, struct fuse_file,
1673 write_entry);
1674 fuse_file_get(ff);
1675 }
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001676 spin_unlock(&fc->lock);
1677
1678 return ff;
1679}
1680
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001681static int fuse_writepage_locked(struct page *page)
1682{
1683 struct address_space *mapping = page->mapping;
1684 struct inode *inode = mapping->host;
1685 struct fuse_conn *fc = get_fuse_conn(inode);
1686 struct fuse_inode *fi = get_fuse_inode(inode);
1687 struct fuse_req *req;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001688 struct page *tmp_page;
Miklos Szeredi72523422013-10-01 16:44:52 +02001689 int error = -ENOMEM;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001690
1691 set_page_writeback(page);
1692
Maxim Patlasov4250c062012-10-26 19:48:07 +04001693 req = fuse_request_alloc_nofs(1);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001694 if (!req)
1695 goto err;
1696
Maxim Patlasov8b41e672013-03-21 18:02:04 +04001697 req->background = 1; /* writeback always goes to bg_queue */
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001698 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1699 if (!tmp_page)
1700 goto err_free;
1701
Miklos Szeredi72523422013-10-01 16:44:52 +02001702 error = -EIO;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001703 req->ff = fuse_write_file_get(fc, fi);
Miklos Szeredi72523422013-10-01 16:44:52 +02001704 if (!req->ff)
1705 goto err_free;
1706
Pavel Emelyanovadcadfa2013-06-29 21:42:20 +04001707 fuse_write_fill(req, req->ff, page_offset(page), 0);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001708
1709 copy_highpage(tmp_page, page);
Miklos Szeredi2d698b02009-04-28 16:56:36 +02001710 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001711 req->misc.write.next = NULL;
Miklos Szeredif4975c62009-04-02 14:25:34 +02001712 req->in.argpages = 1;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001713 req->num_pages = 1;
1714 req->pages[0] = tmp_page;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001715 req->page_descs[0].offset = 0;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001716 req->page_descs[0].length = PAGE_SIZE;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001717 req->end = fuse_writepage_end;
1718 req->inode = inode;
1719
1720 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1721 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001722
1723 spin_lock(&fc->lock);
1724 list_add(&req->writepages_entry, &fi->writepages);
1725 list_add_tail(&req->list, &fi->queued_writes);
1726 fuse_flush_writepages(inode);
1727 spin_unlock(&fc->lock);
1728
Maxim Patlasov4a4ac4e2013-08-12 20:39:30 +04001729 end_page_writeback(page);
1730
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001731 return 0;
1732
1733err_free:
1734 fuse_request_free(req);
1735err:
1736 end_page_writeback(page);
Miklos Szeredi72523422013-10-01 16:44:52 +02001737 return error;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001738}
1739
1740static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1741{
1742 int err;
1743
Miklos Szerediff17be02013-10-01 16:44:53 +02001744 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1745 /*
1746 * ->writepages() should be called for sync() and friends. We
1747 * should only get here on direct reclaim and then we are
1748 * allowed to skip a page which is already in flight
1749 */
1750 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1751
1752 redirty_page_for_writepage(wbc, page);
1753 return 0;
1754 }
1755
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001756 err = fuse_writepage_locked(page);
1757 unlock_page(page);
1758
1759 return err;
1760}
1761
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001762struct fuse_fill_wb_data {
1763 struct fuse_req *req;
1764 struct fuse_file *ff;
1765 struct inode *inode;
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001766 struct page **orig_pages;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001767};
1768
1769static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1770{
1771 struct fuse_req *req = data->req;
1772 struct inode *inode = data->inode;
1773 struct fuse_conn *fc = get_fuse_conn(inode);
1774 struct fuse_inode *fi = get_fuse_inode(inode);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001775 int num_pages = req->num_pages;
1776 int i;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001777
1778 req->ff = fuse_file_get(data->ff);
1779 spin_lock(&fc->lock);
1780 list_add_tail(&req->list, &fi->queued_writes);
1781 fuse_flush_writepages(inode);
1782 spin_unlock(&fc->lock);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001783
1784 for (i = 0; i < num_pages; i++)
1785 end_page_writeback(data->orig_pages[i]);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001786}
1787
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001788static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1789 struct page *page)
1790{
1791 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1792 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1793 struct fuse_req *tmp;
1794 struct fuse_req *old_req;
1795 bool found = false;
1796 pgoff_t curr_index;
1797
1798 BUG_ON(new_req->num_pages != 0);
1799
1800 spin_lock(&fc->lock);
1801 list_del(&new_req->writepages_entry);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001802 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1803 BUG_ON(old_req->inode != new_req->inode);
1804 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1805 if (curr_index <= page->index &&
1806 page->index < curr_index + old_req->num_pages) {
1807 found = true;
1808 break;
1809 }
1810 }
Maxim Patlasovf6011082013-10-02 15:01:07 +04001811 if (!found) {
1812 list_add(&new_req->writepages_entry, &fi->writepages);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001813 goto out_unlock;
Maxim Patlasovf6011082013-10-02 15:01:07 +04001814 }
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001815
Maxim Patlasovf6011082013-10-02 15:01:07 +04001816 new_req->num_pages = 1;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001817 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1818 BUG_ON(tmp->inode != new_req->inode);
1819 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1820 if (tmp->num_pages == 1 &&
1821 curr_index == page->index) {
1822 old_req = tmp;
1823 }
1824 }
1825
1826 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1827 old_req->state == FUSE_REQ_PENDING)) {
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001828 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1829
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001830 copy_highpage(old_req->pages[0], page);
1831 spin_unlock(&fc->lock);
1832
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001833 dec_bdi_stat(bdi, BDI_WRITEBACK);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001834 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
Maxim Patlasov41b6e412013-10-02 21:38:43 +04001835 bdi_writeout_inc(bdi);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001836 fuse_writepage_free(fc, new_req);
1837 fuse_request_free(new_req);
1838 goto out;
1839 } else {
1840 new_req->misc.write.next = old_req->misc.write.next;
1841 old_req->misc.write.next = new_req;
1842 }
1843out_unlock:
1844 spin_unlock(&fc->lock);
1845out:
1846 return found;
1847}
1848
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001849static int fuse_writepages_fill(struct page *page,
1850 struct writeback_control *wbc, void *_data)
1851{
1852 struct fuse_fill_wb_data *data = _data;
1853 struct fuse_req *req = data->req;
1854 struct inode *inode = data->inode;
1855 struct fuse_conn *fc = get_fuse_conn(inode);
1856 struct page *tmp_page;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001857 bool is_writeback;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001858 int err;
1859
1860 if (!data->ff) {
1861 err = -EIO;
1862 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1863 if (!data->ff)
1864 goto out_unlock;
1865 }
1866
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001867 /*
1868 * Being under writeback is unlikely but possible. For example direct
1869 * read to an mmaped fuse file will set the page dirty twice; once when
1870 * the pages are faulted with get_user_pages(), and then after the read
1871 * completed.
1872 */
1873 is_writeback = fuse_page_is_writeback(inode, page->index);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001874
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001875 if (req && req->num_pages &&
1876 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1877 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1878 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1879 fuse_writepages_send(data);
1880 data->req = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001881 }
1882 err = -ENOMEM;
1883 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1884 if (!tmp_page)
1885 goto out_unlock;
1886
1887 /*
1888 * The page must not be redirtied until the writeout is completed
1889 * (i.e. userspace has sent a reply to the write request). Otherwise
1890 * there could be more than one temporary page instance for each real
1891 * page.
1892 *
1893 * This is ensured by holding the page lock in page_mkwrite() while
1894 * checking fuse_page_is_writeback(). We already hold the page lock
1895 * since clear_page_dirty_for_io() and keep it held until we add the
1896 * request to the fi->writepages list and increment req->num_pages.
1897 * After this fuse_page_is_writeback() will indicate that the page is
1898 * under writeback, so we can release the page lock.
1899 */
1900 if (data->req == NULL) {
1901 struct fuse_inode *fi = get_fuse_inode(inode);
1902
1903 err = -ENOMEM;
1904 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1905 if (!req) {
1906 __free_page(tmp_page);
1907 goto out_unlock;
1908 }
1909
1910 fuse_write_fill(req, data->ff, page_offset(page), 0);
1911 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001912 req->misc.write.next = NULL;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001913 req->in.argpages = 1;
1914 req->background = 1;
1915 req->num_pages = 0;
1916 req->end = fuse_writepage_end;
1917 req->inode = inode;
1918
1919 spin_lock(&fc->lock);
1920 list_add(&req->writepages_entry, &fi->writepages);
1921 spin_unlock(&fc->lock);
1922
1923 data->req = req;
1924 }
1925 set_page_writeback(page);
1926
1927 copy_highpage(tmp_page, page);
1928 req->pages[req->num_pages] = tmp_page;
1929 req->page_descs[req->num_pages].offset = 0;
1930 req->page_descs[req->num_pages].length = PAGE_SIZE;
1931
1932 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1933 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
Miklos Szeredi8b284dc2013-10-01 16:44:53 +02001934
1935 err = 0;
1936 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1937 end_page_writeback(page);
1938 data->req = NULL;
1939 goto out_unlock;
1940 }
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001941 data->orig_pages[req->num_pages] = page;
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001942
1943 /*
1944 * Protected by fc->lock against concurrent access by
1945 * fuse_page_is_writeback().
1946 */
1947 spin_lock(&fc->lock);
1948 req->num_pages++;
1949 spin_unlock(&fc->lock);
1950
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001951out_unlock:
1952 unlock_page(page);
1953
1954 return err;
1955}
1956
1957static int fuse_writepages(struct address_space *mapping,
1958 struct writeback_control *wbc)
1959{
1960 struct inode *inode = mapping->host;
1961 struct fuse_fill_wb_data data;
1962 int err;
1963
1964 err = -EIO;
1965 if (is_bad_inode(inode))
1966 goto out;
1967
1968 data.inode = inode;
1969 data.req = NULL;
1970 data.ff = NULL;
1971
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001972 err = -ENOMEM;
1973 data.orig_pages = kzalloc(sizeof(struct page *) *
1974 FUSE_MAX_PAGES_PER_REQ,
1975 GFP_NOFS);
1976 if (!data.orig_pages)
1977 goto out;
1978
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001979 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1980 if (data.req) {
1981 /* Ignore errors if we can write at least one page */
1982 BUG_ON(!data.req->num_pages);
1983 fuse_writepages_send(&data);
1984 err = 0;
1985 }
1986 if (data.ff)
1987 fuse_file_put(data.ff, false);
Maxim Patlasov2d033ea2013-08-16 15:51:41 +04001988
1989 kfree(data.orig_pages);
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04001990out:
1991 return err;
1992}
1993
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04001994/*
1995 * It's worthy to make sure that space is reserved on disk for the write,
1996 * but how to implement it without killing performance need more thinking.
1997 */
1998static int fuse_write_begin(struct file *file, struct address_space *mapping,
1999 loff_t pos, unsigned len, unsigned flags,
2000 struct page **pagep, void **fsdata)
2001{
2002 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2003 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
2004 struct page *page;
2005 loff_t fsize;
2006 int err = -ENOMEM;
2007
2008 WARN_ON(!fc->writeback_cache);
2009
2010 page = grab_cache_page_write_begin(mapping, index, flags);
2011 if (!page)
2012 goto error;
2013
2014 fuse_wait_on_page_writeback(mapping->host, page->index);
2015
2016 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
2017 goto success;
2018 /*
2019 * Check if the start this page comes after the end of file, in which
2020 * case the readpage can be optimized away.
2021 */
2022 fsize = i_size_read(mapping->host);
2023 if (fsize <= (pos & PAGE_CACHE_MASK)) {
2024 size_t off = pos & ~PAGE_CACHE_MASK;
2025 if (off)
2026 zero_user_segment(page, 0, off);
2027 goto success;
2028 }
2029 err = fuse_do_readpage(file, page);
2030 if (err)
2031 goto cleanup;
2032success:
2033 *pagep = page;
2034 return 0;
2035
2036cleanup:
2037 unlock_page(page);
2038 page_cache_release(page);
2039error:
2040 return err;
2041}
2042
2043static int fuse_write_end(struct file *file, struct address_space *mapping,
2044 loff_t pos, unsigned len, unsigned copied,
2045 struct page *page, void *fsdata)
2046{
2047 struct inode *inode = page->mapping->host;
2048
2049 if (!PageUptodate(page)) {
2050 /* Zero any unwritten bytes at the end of the page */
2051 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2052 if (endoff)
2053 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2054 SetPageUptodate(page);
2055 }
2056
2057 fuse_write_update_size(inode, pos + copied);
2058 set_page_dirty(page);
2059 unlock_page(page);
2060 page_cache_release(page);
2061
2062 return copied;
2063}
2064
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002065static int fuse_launder_page(struct page *page)
2066{
2067 int err = 0;
2068 if (clear_page_dirty_for_io(page)) {
2069 struct inode *inode = page->mapping->host;
2070 err = fuse_writepage_locked(page);
2071 if (!err)
2072 fuse_wait_on_page_writeback(inode, page->index);
2073 }
2074 return err;
2075}
2076
2077/*
2078 * Write back dirty pages now, because there may not be any suitable
2079 * open files later
2080 */
2081static void fuse_vma_close(struct vm_area_struct *vma)
2082{
2083 filemap_write_and_wait(vma->vm_file->f_mapping);
2084}
2085
2086/*
2087 * Wait for writeback against this page to complete before allowing it
2088 * to be marked dirty again, and hence written back again, possibly
2089 * before the previous writepage completed.
2090 *
2091 * Block here, instead of in ->writepage(), so that the userspace fs
2092 * can only block processes actually operating on the filesystem.
2093 *
2094 * Otherwise unprivileged userspace fs would be able to block
2095 * unrelated:
2096 *
2097 * - page migration
2098 * - sync(2)
2099 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2100 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07002101static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002102{
Nick Pigginc2ec1752009-03-31 15:23:21 -07002103 struct page *page = vmf->page;
Miklos Szeredicca24372013-10-01 16:44:51 +02002104 struct inode *inode = file_inode(vma->vm_file);
2105
2106 file_update_time(vma->vm_file);
2107 lock_page(page);
2108 if (page->mapping != inode->i_mapping) {
2109 unlock_page(page);
2110 return VM_FAULT_NOPAGE;
2111 }
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002112
2113 fuse_wait_on_page_writeback(inode, page->index);
Miklos Szeredicca24372013-10-01 16:44:51 +02002114 return VM_FAULT_LOCKED;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002115}
2116
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002117static const struct vm_operations_struct fuse_file_vm_ops = {
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002118 .close = fuse_vma_close,
2119 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07002120 .map_pages = filemap_map_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002121 .page_mkwrite = fuse_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07002122 .remap_pages = generic_file_remap_pages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002123};
2124
2125static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2126{
Pavel Emelyanov650b22b2013-10-10 17:10:04 +04002127 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2128 fuse_link_write_file(file);
2129
Miklos Szeredi3be5a522008-04-30 00:54:41 -07002130 file_accessed(file);
2131 vma->vm_ops = &fuse_file_vm_ops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07002132 return 0;
2133}
2134
Miklos Szeredifc280c92009-04-02 14:25:35 +02002135static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2136{
2137 /* Can't provide the coherency needed for MAP_SHARED */
2138 if (vma->vm_flags & VM_MAYSHARE)
2139 return -ENODEV;
2140
Miklos Szeredi3121bfe2009-04-09 17:37:53 +02002141 invalidate_inode_pages2(file->f_mapping);
2142
Miklos Szeredifc280c92009-04-02 14:25:35 +02002143 return generic_file_mmap(file, vma);
2144}
2145
Miklos Szeredi71421252006-06-25 05:48:52 -07002146static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2147 struct file_lock *fl)
2148{
2149 switch (ffl->type) {
2150 case F_UNLCK:
2151 break;
2152
2153 case F_RDLCK:
2154 case F_WRLCK:
2155 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2156 ffl->end < ffl->start)
2157 return -EIO;
2158
2159 fl->fl_start = ffl->start;
2160 fl->fl_end = ffl->end;
2161 fl->fl_pid = ffl->pid;
2162 break;
2163
2164 default:
2165 return -EIO;
2166 }
2167 fl->fl_type = ffl->type;
2168 return 0;
2169}
2170
2171static void fuse_lk_fill(struct fuse_req *req, struct file *file,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002172 const struct file_lock *fl, int opcode, pid_t pid,
2173 int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002174{
Al Viro6131ffa2013-02-27 16:59:05 -05002175 struct inode *inode = file_inode(file);
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002176 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -07002177 struct fuse_file *ff = file->private_data;
2178 struct fuse_lk_in *arg = &req->misc.lk_in;
2179
2180 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -07002181 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -07002182 arg->lk.start = fl->fl_start;
2183 arg->lk.end = fl->fl_end;
2184 arg->lk.type = fl->fl_type;
2185 arg->lk.pid = pid;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002186 if (flock)
2187 arg->lk_flags |= FUSE_LK_FLOCK;
Miklos Szeredi71421252006-06-25 05:48:52 -07002188 req->in.h.opcode = opcode;
2189 req->in.h.nodeid = get_node_id(inode);
2190 req->in.numargs = 1;
2191 req->in.args[0].size = sizeof(*arg);
2192 req->in.args[0].value = arg;
2193}
2194
2195static int fuse_getlk(struct file *file, struct file_lock *fl)
2196{
Al Viro6131ffa2013-02-27 16:59:05 -05002197 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002198 struct fuse_conn *fc = get_fuse_conn(inode);
2199 struct fuse_req *req;
2200 struct fuse_lk_out outarg;
2201 int err;
2202
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002203 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002204 if (IS_ERR(req))
2205 return PTR_ERR(req);
2206
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002207 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002208 req->out.numargs = 1;
2209 req->out.args[0].size = sizeof(outarg);
2210 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002211 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002212 err = req->out.h.error;
2213 fuse_put_request(fc, req);
2214 if (!err)
2215 err = convert_fuse_file_lock(&outarg.lk, fl);
2216
2217 return err;
2218}
2219
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002220static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
Miklos Szeredi71421252006-06-25 05:48:52 -07002221{
Al Viro6131ffa2013-02-27 16:59:05 -05002222 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002223 struct fuse_conn *fc = get_fuse_conn(inode);
2224 struct fuse_req *req;
2225 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2226 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2227 int err;
2228
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04002229 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
Miklos Szeredi48e90762008-07-25 01:49:02 -07002230 /* NLM needs asynchronous locks, which we don't support yet */
2231 return -ENOLCK;
2232 }
2233
Miklos Szeredi71421252006-06-25 05:48:52 -07002234 /* Unlock on close is handled by the flush method */
2235 if (fl->fl_flags & FL_CLOSE)
2236 return 0;
2237
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002238 req = fuse_get_req_nopages(fc);
Miklos Szeredi71421252006-06-25 05:48:52 -07002239 if (IS_ERR(req))
2240 return PTR_ERR(req);
2241
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002242 fuse_lk_fill(req, file, fl, opcode, pid, flock);
Tejun Heob93f8582008-11-26 12:03:55 +01002243 fuse_request_send(fc, req);
Miklos Szeredi71421252006-06-25 05:48:52 -07002244 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002245 /* locking is restartable */
2246 if (err == -EINTR)
2247 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -07002248 fuse_put_request(fc, req);
2249 return err;
2250}
2251
2252static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2253{
Al Viro6131ffa2013-02-27 16:59:05 -05002254 struct inode *inode = file_inode(file);
Miklos Szeredi71421252006-06-25 05:48:52 -07002255 struct fuse_conn *fc = get_fuse_conn(inode);
2256 int err;
2257
Miklos Szeredi48e90762008-07-25 01:49:02 -07002258 if (cmd == F_CANCELLK) {
2259 err = 0;
2260 } else if (cmd == F_GETLK) {
Miklos Szeredi71421252006-06-25 05:48:52 -07002261 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -05002262 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -07002263 err = 0;
2264 } else
2265 err = fuse_getlk(file, fl);
2266 } else {
2267 if (fc->no_lock)
Miklos Szeredi48e90762008-07-25 01:49:02 -07002268 err = posix_lock_file(file, fl, NULL);
Miklos Szeredi71421252006-06-25 05:48:52 -07002269 else
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002270 err = fuse_setlk(file, fl, 0);
Miklos Szeredi71421252006-06-25 05:48:52 -07002271 }
2272 return err;
2273}
2274
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002275static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2276{
Al Viro6131ffa2013-02-27 16:59:05 -05002277 struct inode *inode = file_inode(file);
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002278 struct fuse_conn *fc = get_fuse_conn(inode);
2279 int err;
2280
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002281 if (fc->no_flock) {
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002282 err = flock_lock_file_wait(file, fl);
2283 } else {
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002284 struct fuse_file *ff = file->private_data;
2285
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002286 /* emulate flock with POSIX locks */
2287 fl->fl_owner = (fl_owner_t) file;
Miklos Szeredi37fb3a32011-08-08 16:08:08 +02002288 ff->flock = true;
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07002289 err = fuse_setlk(file, fl, 1);
2290 }
2291
2292 return err;
2293}
2294
Miklos Szeredib2d22722006-12-06 20:35:51 -08002295static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2296{
2297 struct inode *inode = mapping->host;
2298 struct fuse_conn *fc = get_fuse_conn(inode);
2299 struct fuse_req *req;
2300 struct fuse_bmap_in inarg;
2301 struct fuse_bmap_out outarg;
2302 int err;
2303
2304 if (!inode->i_sb->s_bdev || fc->no_bmap)
2305 return 0;
2306
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002307 req = fuse_get_req_nopages(fc);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002308 if (IS_ERR(req))
2309 return 0;
2310
2311 memset(&inarg, 0, sizeof(inarg));
2312 inarg.block = block;
2313 inarg.blocksize = inode->i_sb->s_blocksize;
2314 req->in.h.opcode = FUSE_BMAP;
2315 req->in.h.nodeid = get_node_id(inode);
2316 req->in.numargs = 1;
2317 req->in.args[0].size = sizeof(inarg);
2318 req->in.args[0].value = &inarg;
2319 req->out.numargs = 1;
2320 req->out.args[0].size = sizeof(outarg);
2321 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002322 fuse_request_send(fc, req);
Miklos Szeredib2d22722006-12-06 20:35:51 -08002323 err = req->out.h.error;
2324 fuse_put_request(fc, req);
2325 if (err == -ENOSYS)
2326 fc->no_bmap = 1;
2327
2328 return err ? 0 : outarg.block;
2329}
2330
Andrew Morton965c8e52012-12-17 15:59:39 -08002331static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002332{
2333 loff_t retval;
Al Viro6131ffa2013-02-27 16:59:05 -05002334 struct inode *inode = file_inode(file);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002335
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002336 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
Andrew Morton965c8e52012-12-17 15:59:39 -08002337 if (whence == SEEK_CUR || whence == SEEK_SET)
2338 return generic_file_llseek(file, offset, whence);
Josef Bacik06222e42011-07-18 13:21:38 -04002339
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002340 mutex_lock(&inode->i_mutex);
2341 retval = fuse_update_attributes(inode, NULL, file, NULL);
2342 if (!retval)
Andrew Morton965c8e52012-12-17 15:59:39 -08002343 retval = generic_file_llseek(file, offset, whence);
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002344 mutex_unlock(&inode->i_mutex);
Miklos Szeredic07c3d12011-12-13 11:58:48 +01002345
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07002346 return retval;
2347}
2348
Tejun Heo59efec72008-11-26 12:03:55 +01002349static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2350 unsigned int nr_segs, size_t bytes, bool to_user)
2351{
2352 struct iov_iter ii;
2353 int page_idx = 0;
2354
2355 if (!bytes)
2356 return 0;
2357
2358 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2359
2360 while (iov_iter_count(&ii)) {
2361 struct page *page = pages[page_idx++];
2362 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002363 void *kaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002364
Dan Carpenter4aa0edd2010-05-07 10:28:17 +02002365 kaddr = kmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002366
2367 while (todo) {
2368 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2369 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2370 size_t copy = min(todo, iov_len);
2371 size_t left;
2372
2373 if (!to_user)
2374 left = copy_from_user(kaddr, uaddr, copy);
2375 else
2376 left = copy_to_user(uaddr, kaddr, copy);
2377
2378 if (unlikely(left))
2379 return -EFAULT;
2380
2381 iov_iter_advance(&ii, copy);
2382 todo -= copy;
2383 kaddr += copy;
2384 }
2385
Jens Axboe0bd87182009-11-03 11:40:44 +01002386 kunmap(page);
Tejun Heo59efec72008-11-26 12:03:55 +01002387 }
2388
2389 return 0;
2390}
2391
2392/*
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002393 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2394 * ABI was defined to be 'struct iovec' which is different on 32bit
2395 * and 64bit. Fortunately we can determine which structure the server
2396 * used from the size of the reply.
2397 */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002398static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2399 size_t transferred, unsigned count,
2400 bool is_compat)
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002401{
2402#ifdef CONFIG_COMPAT
2403 if (count * sizeof(struct compat_iovec) == transferred) {
2404 struct compat_iovec *ciov = src;
2405 unsigned i;
2406
2407 /*
2408 * With this interface a 32bit server cannot support
2409 * non-compat (i.e. ones coming from 64bit apps) ioctl
2410 * requests
2411 */
2412 if (!is_compat)
2413 return -EINVAL;
2414
2415 for (i = 0; i < count; i++) {
2416 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2417 dst[i].iov_len = ciov[i].iov_len;
2418 }
2419 return 0;
2420 }
2421#endif
2422
2423 if (count * sizeof(struct iovec) != transferred)
2424 return -EIO;
2425
2426 memcpy(dst, src, transferred);
2427 return 0;
2428}
2429
Miklos Szeredi75727772010-11-30 16:39:27 +01002430/* Make sure iov_length() won't overflow */
2431static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2432{
2433 size_t n;
2434 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2435
Zach Brownfb6ccff2012-07-24 12:10:11 -07002436 for (n = 0; n < count; n++, iov++) {
Miklos Szeredi75727772010-11-30 16:39:27 +01002437 if (iov->iov_len > (size_t) max)
2438 return -ENOMEM;
2439 max -= iov->iov_len;
2440 }
2441 return 0;
2442}
2443
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002444static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2445 void *src, size_t transferred, unsigned count,
2446 bool is_compat)
2447{
2448 unsigned i;
2449 struct fuse_ioctl_iovec *fiov = src;
2450
2451 if (fc->minor < 16) {
2452 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2453 count, is_compat);
2454 }
2455
2456 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2457 return -EIO;
2458
2459 for (i = 0; i < count; i++) {
2460 /* Did the server supply an inappropriate value? */
2461 if (fiov[i].base != (unsigned long) fiov[i].base ||
2462 fiov[i].len != (unsigned long) fiov[i].len)
2463 return -EIO;
2464
2465 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2466 dst[i].iov_len = (size_t) fiov[i].len;
2467
2468#ifdef CONFIG_COMPAT
2469 if (is_compat &&
2470 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2471 (compat_size_t) dst[i].iov_len != fiov[i].len))
2472 return -EIO;
2473#endif
2474 }
2475
2476 return 0;
2477}
2478
2479
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002480/*
Tejun Heo59efec72008-11-26 12:03:55 +01002481 * For ioctls, there is no generic way to determine how much memory
2482 * needs to be read and/or written. Furthermore, ioctls are allowed
2483 * to dereference the passed pointer, so the parameter requires deep
2484 * copying but FUSE has no idea whatsoever about what to copy in or
2485 * out.
2486 *
2487 * This is solved by allowing FUSE server to retry ioctl with
2488 * necessary in/out iovecs. Let's assume the ioctl implementation
2489 * needs to read in the following structure.
2490 *
2491 * struct a {
2492 * char *buf;
2493 * size_t buflen;
2494 * }
2495 *
2496 * On the first callout to FUSE server, inarg->in_size and
2497 * inarg->out_size will be NULL; then, the server completes the ioctl
2498 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2499 * the actual iov array to
2500 *
2501 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2502 *
2503 * which tells FUSE to copy in the requested area and retry the ioctl.
2504 * On the second round, the server has access to the structure and
2505 * from that it can tell what to look for next, so on the invocation,
2506 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2507 *
2508 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2509 * { .iov_base = a.buf, .iov_len = a.buflen } }
2510 *
2511 * FUSE will copy both struct a and the pointed buffer from the
2512 * process doing the ioctl and retry ioctl with both struct a and the
2513 * buffer.
2514 *
2515 * This time, FUSE server has everything it needs and completes ioctl
2516 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2517 *
2518 * Copying data out works the same way.
2519 *
2520 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2521 * automatically initializes in and out iovs by decoding @cmd with
2522 * _IOC_* macros and the server is not allowed to request RETRY. This
2523 * limits ioctl data transfers to well-formed ioctls and is the forced
2524 * behavior for all FUSE servers.
2525 */
Tejun Heo08cbf542009-04-14 10:54:53 +09002526long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2527 unsigned int flags)
Tejun Heo59efec72008-11-26 12:03:55 +01002528{
Tejun Heo59efec72008-11-26 12:03:55 +01002529 struct fuse_file *ff = file->private_data;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002530 struct fuse_conn *fc = ff->fc;
Tejun Heo59efec72008-11-26 12:03:55 +01002531 struct fuse_ioctl_in inarg = {
2532 .fh = ff->fh,
2533 .cmd = cmd,
2534 .arg = arg,
2535 .flags = flags
2536 };
2537 struct fuse_ioctl_out outarg;
2538 struct fuse_req *req = NULL;
2539 struct page **pages = NULL;
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002540 struct iovec *iov_page = NULL;
Tejun Heo59efec72008-11-26 12:03:55 +01002541 struct iovec *in_iov = NULL, *out_iov = NULL;
2542 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2543 size_t in_size, out_size, transferred;
2544 int err;
2545
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002546#if BITS_PER_LONG == 32
2547 inarg.flags |= FUSE_IOCTL_32BIT;
2548#else
2549 if (flags & FUSE_IOCTL_COMPAT)
2550 inarg.flags |= FUSE_IOCTL_32BIT;
2551#endif
2552
Tejun Heo59efec72008-11-26 12:03:55 +01002553 /* assume all the iovs returned by client always fits in a page */
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002554 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
Tejun Heo59efec72008-11-26 12:03:55 +01002555
Tejun Heo59efec72008-11-26 12:03:55 +01002556 err = -ENOMEM;
Thomas Meyerc411cc82011-11-29 22:08:00 +01002557 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002558 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
Tejun Heo59efec72008-11-26 12:03:55 +01002559 if (!pages || !iov_page)
2560 goto out;
2561
2562 /*
2563 * If restricted, initialize IO parameters as encoded in @cmd.
2564 * RETRY from server is not allowed.
2565 */
2566 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002567 struct iovec *iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002568
Miklos Szeredic9f0523d2008-12-02 14:49:42 +01002569 iov->iov_base = (void __user *)arg;
Tejun Heo59efec72008-11-26 12:03:55 +01002570 iov->iov_len = _IOC_SIZE(cmd);
2571
2572 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2573 in_iov = iov;
2574 in_iovs = 1;
2575 }
2576
2577 if (_IOC_DIR(cmd) & _IOC_READ) {
2578 out_iov = iov;
2579 out_iovs = 1;
2580 }
2581 }
2582
2583 retry:
2584 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2585 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2586
2587 /*
2588 * Out data can be used either for actual out data or iovs,
2589 * make sure there always is at least one page.
2590 */
2591 out_size = max_t(size_t, out_size, PAGE_SIZE);
2592 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2593
2594 /* make sure there are enough buffer pages and init request with them */
2595 err = -ENOMEM;
2596 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2597 goto out;
2598 while (num_pages < max_pages) {
2599 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2600 if (!pages[num_pages])
2601 goto out;
2602 num_pages++;
2603 }
2604
Maxim Patlasov54b96672012-10-26 19:49:13 +04002605 req = fuse_get_req(fc, num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002606 if (IS_ERR(req)) {
2607 err = PTR_ERR(req);
2608 req = NULL;
2609 goto out;
2610 }
2611 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2612 req->num_pages = num_pages;
Maxim Patlasov7c190c82012-10-26 19:50:29 +04002613 fuse_page_descs_length_init(req, 0, req->num_pages);
Tejun Heo59efec72008-11-26 12:03:55 +01002614
2615 /* okay, let's send it to the client */
2616 req->in.h.opcode = FUSE_IOCTL;
Miklos Szeredid36f2482009-04-28 16:56:39 +02002617 req->in.h.nodeid = ff->nodeid;
Tejun Heo59efec72008-11-26 12:03:55 +01002618 req->in.numargs = 1;
2619 req->in.args[0].size = sizeof(inarg);
2620 req->in.args[0].value = &inarg;
2621 if (in_size) {
2622 req->in.numargs++;
2623 req->in.args[1].size = in_size;
2624 req->in.argpages = 1;
2625
2626 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2627 false);
2628 if (err)
2629 goto out;
2630 }
2631
2632 req->out.numargs = 2;
2633 req->out.args[0].size = sizeof(outarg);
2634 req->out.args[0].value = &outarg;
2635 req->out.args[1].size = out_size;
2636 req->out.argpages = 1;
2637 req->out.argvar = 1;
2638
Tejun Heob93f8582008-11-26 12:03:55 +01002639 fuse_request_send(fc, req);
Tejun Heo59efec72008-11-26 12:03:55 +01002640 err = req->out.h.error;
2641 transferred = req->out.args[1].size;
2642 fuse_put_request(fc, req);
2643 req = NULL;
2644 if (err)
2645 goto out;
2646
2647 /* did it ask for retry? */
2648 if (outarg.flags & FUSE_IOCTL_RETRY) {
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002649 void *vaddr;
Tejun Heo59efec72008-11-26 12:03:55 +01002650
2651 /* no retry if in restricted mode */
2652 err = -EIO;
2653 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2654 goto out;
2655
2656 in_iovs = outarg.in_iovs;
2657 out_iovs = outarg.out_iovs;
2658
2659 /*
2660 * Make sure things are in boundary, separate checks
2661 * are to protect against overflow.
2662 */
2663 err = -ENOMEM;
2664 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2665 out_iovs > FUSE_IOCTL_MAX_IOV ||
2666 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2667 goto out;
2668
Cong Wang2408f6e2011-11-25 23:14:30 +08002669 vaddr = kmap_atomic(pages[0]);
Miklos Szeredi1baa26b2010-12-07 20:16:56 +01002670 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002671 transferred, in_iovs + out_iovs,
2672 (flags & FUSE_IOCTL_COMPAT) != 0);
Cong Wang2408f6e2011-11-25 23:14:30 +08002673 kunmap_atomic(vaddr);
Miklos Szeredid9d318d2010-11-30 16:39:27 +01002674 if (err)
2675 goto out;
Tejun Heo59efec72008-11-26 12:03:55 +01002676
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002677 in_iov = iov_page;
Tejun Heo59efec72008-11-26 12:03:55 +01002678 out_iov = in_iov + in_iovs;
2679
Miklos Szeredi75727772010-11-30 16:39:27 +01002680 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2681 if (err)
2682 goto out;
2683
2684 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2685 if (err)
2686 goto out;
2687
Tejun Heo59efec72008-11-26 12:03:55 +01002688 goto retry;
2689 }
2690
2691 err = -EIO;
2692 if (transferred > inarg.out_size)
2693 goto out;
2694
2695 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2696 out:
2697 if (req)
2698 fuse_put_request(fc, req);
Miklos Szeredi8ac83502010-12-07 20:16:56 +01002699 free_page((unsigned long) iov_page);
Tejun Heo59efec72008-11-26 12:03:55 +01002700 while (num_pages)
2701 __free_page(pages[--num_pages]);
2702 kfree(pages);
2703
2704 return err ? err : outarg.result;
2705}
Tejun Heo08cbf542009-04-14 10:54:53 +09002706EXPORT_SYMBOL_GPL(fuse_do_ioctl);
Tejun Heo59efec72008-11-26 12:03:55 +01002707
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002708long fuse_ioctl_common(struct file *file, unsigned int cmd,
2709 unsigned long arg, unsigned int flags)
Miklos Szeredid36f2482009-04-28 16:56:39 +02002710{
Al Viro6131ffa2013-02-27 16:59:05 -05002711 struct inode *inode = file_inode(file);
Miklos Szeredid36f2482009-04-28 16:56:39 +02002712 struct fuse_conn *fc = get_fuse_conn(inode);
2713
Anatol Pomozovc2132c12013-01-14 22:30:00 -08002714 if (!fuse_allow_current_process(fc))
Miklos Szeredid36f2482009-04-28 16:56:39 +02002715 return -EACCES;
2716
2717 if (is_bad_inode(inode))
2718 return -EIO;
2719
2720 return fuse_do_ioctl(file, cmd, arg, flags);
2721}
2722
Tejun Heo59efec72008-11-26 12:03:55 +01002723static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2724 unsigned long arg)
2725{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002726 return fuse_ioctl_common(file, cmd, arg, 0);
Tejun Heo59efec72008-11-26 12:03:55 +01002727}
2728
2729static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2730 unsigned long arg)
2731{
Miklos Szeredib18da0c2011-12-13 11:58:49 +01002732 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
Tejun Heo59efec72008-11-26 12:03:55 +01002733}
2734
Tejun Heo95668a62008-11-26 12:03:55 +01002735/*
2736 * All files which have been polled are linked to RB tree
2737 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2738 * find the matching one.
2739 */
2740static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2741 struct rb_node **parent_out)
2742{
2743 struct rb_node **link = &fc->polled_files.rb_node;
2744 struct rb_node *last = NULL;
2745
2746 while (*link) {
2747 struct fuse_file *ff;
2748
2749 last = *link;
2750 ff = rb_entry(last, struct fuse_file, polled_node);
2751
2752 if (kh < ff->kh)
2753 link = &last->rb_left;
2754 else if (kh > ff->kh)
2755 link = &last->rb_right;
2756 else
2757 return link;
2758 }
2759
2760 if (parent_out)
2761 *parent_out = last;
2762 return link;
2763}
2764
2765/*
2766 * The file is about to be polled. Make sure it's on the polled_files
2767 * RB tree. Note that files once added to the polled_files tree are
2768 * not removed before the file is released. This is because a file
2769 * polled once is likely to be polled again.
2770 */
2771static void fuse_register_polled_file(struct fuse_conn *fc,
2772 struct fuse_file *ff)
2773{
2774 spin_lock(&fc->lock);
2775 if (RB_EMPTY_NODE(&ff->polled_node)) {
Rajat Jainf3846262014-02-05 15:24:57 -08002776 struct rb_node **link, *uninitialized_var(parent);
Tejun Heo95668a62008-11-26 12:03:55 +01002777
2778 link = fuse_find_polled_node(fc, ff->kh, &parent);
2779 BUG_ON(*link);
2780 rb_link_node(&ff->polled_node, parent, link);
2781 rb_insert_color(&ff->polled_node, &fc->polled_files);
2782 }
2783 spin_unlock(&fc->lock);
2784}
2785
Tejun Heo08cbf542009-04-14 10:54:53 +09002786unsigned fuse_file_poll(struct file *file, poll_table *wait)
Tejun Heo95668a62008-11-26 12:03:55 +01002787{
Tejun Heo95668a62008-11-26 12:03:55 +01002788 struct fuse_file *ff = file->private_data;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002789 struct fuse_conn *fc = ff->fc;
Tejun Heo95668a62008-11-26 12:03:55 +01002790 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2791 struct fuse_poll_out outarg;
2792 struct fuse_req *req;
2793 int err;
2794
2795 if (fc->no_poll)
2796 return DEFAULT_POLLMASK;
2797
2798 poll_wait(file, &ff->poll_wait, wait);
Enke Chen0415d292013-02-04 16:14:32 +01002799 inarg.events = (__u32)poll_requested_events(wait);
Tejun Heo95668a62008-11-26 12:03:55 +01002800
2801 /*
2802 * Ask for notification iff there's someone waiting for it.
2803 * The client may ignore the flag and always notify.
2804 */
2805 if (waitqueue_active(&ff->poll_wait)) {
2806 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2807 fuse_register_polled_file(fc, ff);
2808 }
2809
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002810 req = fuse_get_req_nopages(fc);
Tejun Heo95668a62008-11-26 12:03:55 +01002811 if (IS_ERR(req))
Miklos Szeredi201fa692009-06-30 20:06:24 +02002812 return POLLERR;
Tejun Heo95668a62008-11-26 12:03:55 +01002813
2814 req->in.h.opcode = FUSE_POLL;
Miklos Szeredi797759a2009-04-28 16:56:41 +02002815 req->in.h.nodeid = ff->nodeid;
Tejun Heo95668a62008-11-26 12:03:55 +01002816 req->in.numargs = 1;
2817 req->in.args[0].size = sizeof(inarg);
2818 req->in.args[0].value = &inarg;
2819 req->out.numargs = 1;
2820 req->out.args[0].size = sizeof(outarg);
2821 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01002822 fuse_request_send(fc, req);
Tejun Heo95668a62008-11-26 12:03:55 +01002823 err = req->out.h.error;
2824 fuse_put_request(fc, req);
2825
2826 if (!err)
2827 return outarg.revents;
2828 if (err == -ENOSYS) {
2829 fc->no_poll = 1;
2830 return DEFAULT_POLLMASK;
2831 }
2832 return POLLERR;
2833}
Tejun Heo08cbf542009-04-14 10:54:53 +09002834EXPORT_SYMBOL_GPL(fuse_file_poll);
Tejun Heo95668a62008-11-26 12:03:55 +01002835
2836/*
2837 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2838 * wakes up the poll waiters.
2839 */
2840int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2841 struct fuse_notify_poll_wakeup_out *outarg)
2842{
2843 u64 kh = outarg->kh;
2844 struct rb_node **link;
2845
2846 spin_lock(&fc->lock);
2847
2848 link = fuse_find_polled_node(fc, kh, NULL);
2849 if (*link) {
2850 struct fuse_file *ff;
2851
2852 ff = rb_entry(*link, struct fuse_file, polled_node);
2853 wake_up_interruptible_sync(&ff->poll_wait);
2854 }
2855
2856 spin_unlock(&fc->lock);
2857 return 0;
2858}
2859
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002860static void fuse_do_truncate(struct file *file)
2861{
2862 struct inode *inode = file->f_mapping->host;
2863 struct iattr attr;
2864
2865 attr.ia_valid = ATTR_SIZE;
2866 attr.ia_size = i_size_read(inode);
2867
2868 attr.ia_file = file;
2869 attr.ia_valid |= ATTR_FILE;
2870
2871 fuse_do_setattr(inode, &attr, file);
2872}
2873
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002874static inline loff_t fuse_round_up(loff_t off)
2875{
2876 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2877}
2878
Anand Avati4273b792012-02-17 12:46:25 -05002879static ssize_t
2880fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2881 loff_t offset, unsigned long nr_segs)
2882{
2883 ssize_t ret = 0;
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002884 struct file *file = iocb->ki_filp;
2885 struct fuse_file *ff = file->private_data;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002886 bool async_dio = ff->fc->async_dio;
Anand Avati4273b792012-02-17 12:46:25 -05002887 loff_t pos = 0;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002888 struct inode *inode;
2889 loff_t i_size;
2890 size_t count = iov_length(iov, nr_segs);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002891 struct fuse_io_priv *io;
Anand Avati4273b792012-02-17 12:46:25 -05002892
Anand Avati4273b792012-02-17 12:46:25 -05002893 pos = offset;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002894 inode = file->f_mapping->host;
2895 i_size = i_size_read(inode);
Anand Avati4273b792012-02-17 12:46:25 -05002896
Steven Whitehouse9fe55ee2014-01-24 14:42:22 +00002897 if ((rw == READ) && (offset > i_size))
2898 return 0;
2899
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002900 /* optimization for short read */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002901 if (async_dio && rw != WRITE && offset + count > i_size) {
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002902 if (offset >= i_size)
2903 return 0;
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002904 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002905 }
2906
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002907 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002908 if (!io)
2909 return -ENOMEM;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002910 spin_lock_init(&io->lock);
2911 io->reqs = 1;
2912 io->bytes = -1;
2913 io->size = 0;
2914 io->offset = offset;
2915 io->write = (rw == WRITE);
2916 io->err = 0;
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002917 io->file = file;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002918 /*
2919 * By default, we want to optimize all I/Os with async request
Miklos Szeredi60b9df72013-05-01 14:37:21 +02002920 * submission to the client filesystem if supported.
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002921 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002922 io->async = async_dio;
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002923 io->iocb = iocb;
2924
2925 /*
2926 * We cannot asynchronously extend the size of a file. We have no method
2927 * to wait on real async I/O requests, so we must submit this request
2928 * synchronously.
2929 */
Maxim Patlasove5c5f052013-05-30 16:41:34 +04002930 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002931 io->async = false;
Anand Avati4273b792012-02-17 12:46:25 -05002932
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002933 if (rw == WRITE)
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002934 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
Maxim Patlasovb98d0232012-10-26 19:50:15 +04002935 else
Maxim Patlasov439ee5f2012-12-14 19:21:26 +04002936 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
Maxim Patlasov36cf66e2012-12-14 19:20:51 +04002937
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002938 if (io->async) {
2939 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2940
2941 /* we have a non-extending, async request, so return */
Brian Fosterc9ecf982013-05-30 15:35:50 -04002942 if (!is_sync_kiocb(iocb))
Maxim Patlasovbcba24c2012-12-14 19:21:08 +04002943 return -EIOCBQUEUED;
2944
2945 ret = wait_on_sync_kiocb(iocb);
2946 } else {
2947 kfree(io);
2948 }
2949
Maxim Patlasovefb9fa92012-12-18 14:05:08 +04002950 if (rw == WRITE) {
2951 if (ret > 0)
2952 fuse_write_update_size(inode, pos);
2953 else if (ret < 0 && offset + count > i_size)
2954 fuse_do_truncate(file);
2955 }
Anand Avati4273b792012-02-17 12:46:25 -05002956
2957 return ret;
2958}
2959
Miklos Szeredicdadb112012-11-10 16:55:56 +01002960static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2961 loff_t length)
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002962{
2963 struct fuse_file *ff = file->private_data;
Brian Foster3634a632013-05-17 09:30:32 -04002964 struct inode *inode = file->f_inode;
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002965 struct fuse_inode *fi = get_fuse_inode(inode);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002966 struct fuse_conn *fc = ff->fc;
2967 struct fuse_req *req;
2968 struct fuse_fallocate_in inarg = {
2969 .fh = ff->fh,
2970 .offset = offset,
2971 .length = length,
2972 .mode = mode
2973 };
2974 int err;
Maxim Patlasov14c14412013-06-13 12:16:39 +04002975 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2976 (mode & FALLOC_FL_PUNCH_HOLE);
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07002977
Miklos Szeredi519c6042012-04-26 10:56:36 +02002978 if (fc->no_fallocate)
2979 return -EOPNOTSUPP;
2980
Maxim Patlasov14c14412013-06-13 12:16:39 +04002981 if (lock_inode) {
Brian Foster3634a632013-05-17 09:30:32 -04002982 mutex_lock(&inode->i_mutex);
Maxim Patlasovbde52782013-09-13 19:19:54 +04002983 if (mode & FALLOC_FL_PUNCH_HOLE) {
2984 loff_t endbyte = offset + length - 1;
2985 err = filemap_write_and_wait_range(inode->i_mapping,
2986 offset, endbyte);
2987 if (err)
2988 goto out;
2989
2990 fuse_sync_writes(inode);
2991 }
Brian Foster3634a632013-05-17 09:30:32 -04002992 }
2993
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04002994 if (!(mode & FALLOC_FL_KEEP_SIZE))
2995 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2996
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04002997 req = fuse_get_req_nopages(fc);
Brian Foster3634a632013-05-17 09:30:32 -04002998 if (IS_ERR(req)) {
2999 err = PTR_ERR(req);
3000 goto out;
3001 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003002
3003 req->in.h.opcode = FUSE_FALLOCATE;
3004 req->in.h.nodeid = ff->nodeid;
3005 req->in.numargs = 1;
3006 req->in.args[0].size = sizeof(inarg);
3007 req->in.args[0].value = &inarg;
3008 fuse_request_send(fc, req);
3009 err = req->out.h.error;
Miklos Szeredi519c6042012-04-26 10:56:36 +02003010 if (err == -ENOSYS) {
3011 fc->no_fallocate = 1;
3012 err = -EOPNOTSUPP;
3013 }
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003014 fuse_put_request(fc, req);
3015
Brian Fosterbee6c302013-05-17 15:27:34 -04003016 if (err)
3017 goto out;
3018
3019 /* we could have extended the file */
Maxim Patlasovb0aa7602013-12-26 19:51:11 +04003020 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3021 bool changed = fuse_write_update_size(inode, offset + length);
3022
3023 if (changed && fc->writeback_cache) {
3024 struct fuse_inode *fi = get_fuse_inode(inode);
3025
3026 inode->i_mtime = current_fs_time(inode->i_sb);
3027 set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
3028 }
3029 }
Brian Fosterbee6c302013-05-17 15:27:34 -04003030
3031 if (mode & FALLOC_FL_PUNCH_HOLE)
3032 truncate_pagecache_range(inode, offset, offset + length - 1);
3033
3034 fuse_invalidate_attr(inode);
3035
Brian Foster3634a632013-05-17 09:30:32 -04003036out:
Maxim Patlasov0ab08f52013-09-13 19:20:16 +04003037 if (!(mode & FALLOC_FL_KEEP_SIZE))
3038 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3039
Maxim Patlasovbde52782013-09-13 19:19:54 +04003040 if (lock_inode)
Brian Foster3634a632013-05-17 09:30:32 -04003041 mutex_unlock(&inode->i_mutex);
Brian Foster3634a632013-05-17 09:30:32 -04003042
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003043 return err;
3044}
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003045
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003046static const struct file_operations fuse_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003047 .llseek = fuse_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003048 .read = do_sync_read,
Miklos Szeredibcb4be82007-11-28 16:21:59 -08003049 .aio_read = fuse_file_aio_read,
Badari Pulavarty543ade12006-09-30 23:28:48 -07003050 .write = do_sync_write,
Nick Pigginea9b9902008-04-30 00:54:42 -07003051 .aio_write = fuse_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003052 .mmap = fuse_file_mmap,
3053 .open = fuse_open,
3054 .flush = fuse_flush,
3055 .release = fuse_release,
3056 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003057 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003058 .flock = fuse_file_flock,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +02003059 .splice_read = generic_file_splice_read,
Tejun Heo59efec72008-11-26 12:03:55 +01003060 .unlocked_ioctl = fuse_file_ioctl,
3061 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003062 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003063 .fallocate = fuse_file_fallocate,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003064};
3065
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003066static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi5559b8f2008-04-30 00:54:45 -07003067 .llseek = fuse_file_llseek,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003068 .read = fuse_direct_read,
3069 .write = fuse_direct_write,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003070 .mmap = fuse_direct_mmap,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003071 .open = fuse_open,
3072 .flush = fuse_flush,
3073 .release = fuse_release,
3074 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -07003075 .lock = fuse_file_lock,
Miklos Szeredia9ff4f82007-10-18 03:07:02 -07003076 .flock = fuse_file_flock,
Tejun Heo59efec72008-11-26 12:03:55 +01003077 .unlocked_ioctl = fuse_file_ioctl,
3078 .compat_ioctl = fuse_file_compat_ioctl,
Tejun Heo95668a62008-11-26 12:03:55 +01003079 .poll = fuse_file_poll,
Anatol Pomozov05ba1f02012-04-22 18:45:24 -07003080 .fallocate = fuse_file_fallocate,
Miklos Szeredifc280c92009-04-02 14:25:35 +02003081 /* no splice_read */
Miklos Szeredi413ef8c2005-09-09 13:10:35 -07003082};
3083
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003084static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003085 .readpage = fuse_readpage,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003086 .writepage = fuse_writepage,
Pavel Emelyanov26d614d2013-06-29 21:45:29 +04003087 .writepages = fuse_writepages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003088 .launder_page = fuse_launder_page,
Miklos Szeredidb50b962005-09-09 13:10:33 -07003089 .readpages = fuse_readpages,
Miklos Szeredi3be5a522008-04-30 00:54:41 -07003090 .set_page_dirty = __set_page_dirty_nobuffers,
Miklos Szeredib2d22722006-12-06 20:35:51 -08003091 .bmap = fuse_bmap,
Anand Avati4273b792012-02-17 12:46:25 -05003092 .direct_IO = fuse_direct_IO,
Pavel Emelyanov6b12c1b2013-10-10 17:11:43 +04003093 .write_begin = fuse_write_begin,
3094 .write_end = fuse_write_end,
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003095};
3096
3097void fuse_init_file_inode(struct inode *inode)
3098{
Miklos Szeredi45323fb2005-09-09 13:10:37 -07003099 inode->i_fop = &fuse_file_operations;
3100 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -07003101}