blob: fc342cf7c2cc7881dbfafa09097d6de52b34bdca [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02003 Copyright (C) 2001-2006 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>
14
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080015static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070016
Miklos Szeredifd72faa2005-11-07 00:59:51 -080017static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
18 struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070019{
20 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070021 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080022 struct fuse_req *req;
23 int err;
24
Miklos Szeredice1d5a42006-04-10 22:54:58 -070025 req = fuse_get_req(fc);
26 if (IS_ERR(req))
27 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080028
29 memset(&inarg, 0, sizeof(inarg));
30 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
31 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
32 req->in.h.nodeid = get_node_id(inode);
33 req->inode = inode;
34 req->in.numargs = 1;
35 req->in.args[0].size = sizeof(inarg);
36 req->in.args[0].value = &inarg;
37 req->out.numargs = 1;
38 req->out.args[0].size = sizeof(*outargp);
39 req->out.args[0].value = outargp;
40 request_send(fc, req);
41 err = req->out.h.error;
42 fuse_put_request(fc, req);
43
44 return err;
45}
46
47struct fuse_file *fuse_file_alloc(void)
48{
49 struct fuse_file *ff;
50 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
51 if (ff) {
52 ff->release_req = fuse_request_alloc();
53 if (!ff->release_req) {
54 kfree(ff);
55 ff = NULL;
56 }
57 }
58 return ff;
59}
60
61void fuse_file_free(struct fuse_file *ff)
62{
63 fuse_request_free(ff->release_req);
64 kfree(ff);
65}
66
67void fuse_finish_open(struct inode *inode, struct file *file,
68 struct fuse_file *ff, struct fuse_open_out *outarg)
69{
70 if (outarg->open_flags & FOPEN_DIRECT_IO)
71 file->f_op = &fuse_direct_io_file_operations;
72 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
73 invalidate_inode_pages(inode->i_mapping);
74 ff->fh = outarg->fh;
75 file->private_data = ff;
76}
77
78int fuse_open_common(struct inode *inode, struct file *file, int isdir)
79{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070080 struct fuse_open_out outarg;
81 struct fuse_file *ff;
82 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -070083
Miklos Szeredidd190d02005-09-30 11:59:02 -070084 /* VFS checks this, but only _after_ ->open() */
85 if (file->f_flags & O_DIRECT)
86 return -EINVAL;
87
Miklos Szeredib6aeade2005-09-09 13:10:30 -070088 err = generic_file_open(inode, file);
89 if (err)
90 return err;
91
92 /* If opening the root node, no lookup has been performed on
93 it, so the attributes must be refreshed */
94 if (get_node_id(inode) == FUSE_ROOT_ID) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080095 err = fuse_do_getattr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070096 if (err)
97 return err;
98 }
99
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800100 ff = fuse_file_alloc();
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700101 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800102 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700103
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800104 err = fuse_send_open(inode, file, isdir, &outarg);
105 if (err)
106 fuse_file_free(ff);
107 else {
108 if (isdir)
109 outarg.open_flags &= ~FOPEN_DIRECT_IO;
110 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700111 }
112
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700113 return err;
114}
115
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800116/* Special case for failed iget in CREATE */
117static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
118{
Miklos Szeredi77e7f252006-02-17 13:52:52 -0800119 /* If called from end_io_requests(), req has more than one
120 reference and fuse_reset_request() cannot work */
121 if (fc->connected) {
122 u64 nodeid = req->in.h.nodeid;
123 fuse_reset_request(req);
124 fuse_send_forget(fc, req, nodeid, 1);
125 } else
126 fuse_put_request(fc, req);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800127}
128
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800129void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
130 u64 nodeid, struct inode *inode, int flags, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700131{
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800132 struct fuse_req * req = ff->release_req;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700133 struct fuse_release_in *inarg = &req->misc.release_in;
134
135 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800136 inarg->flags = flags;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700137 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800138 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700139 req->inode = inode;
140 req->in.numargs = 1;
141 req->in.args[0].size = sizeof(struct fuse_release_in);
142 req->in.args[0].value = inarg;
143 request_send_background(fc, req);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800144 if (!inode)
145 req->end = fuse_release_end;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700146 kfree(ff);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800147}
148
149int fuse_release_common(struct inode *inode, struct file *file, int isdir)
150{
151 struct fuse_file *ff = file->private_data;
152 if (ff) {
153 struct fuse_conn *fc = get_fuse_conn(inode);
154 u64 nodeid = get_node_id(inode);
155 fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir);
156 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700157
158 /* Return value is ignored by VFS */
159 return 0;
160}
161
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700162static int fuse_open(struct inode *inode, struct file *file)
163{
164 return fuse_open_common(inode, file, 0);
165}
166
167static int fuse_release(struct inode *inode, struct file *file)
168{
169 return fuse_release_common(inode, file, 0);
170}
171
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700172static int fuse_flush(struct file *file)
173{
174 struct inode *inode = file->f_dentry->d_inode;
175 struct fuse_conn *fc = get_fuse_conn(inode);
176 struct fuse_file *ff = file->private_data;
177 struct fuse_req *req;
178 struct fuse_flush_in inarg;
179 int err;
180
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800181 if (is_bad_inode(inode))
182 return -EIO;
183
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700184 if (fc->no_flush)
185 return 0;
186
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700187 req = fuse_get_req(fc);
188 if (IS_ERR(req))
189 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700190
191 memset(&inarg, 0, sizeof(inarg));
192 inarg.fh = ff->fh;
193 req->in.h.opcode = FUSE_FLUSH;
194 req->in.h.nodeid = get_node_id(inode);
195 req->inode = inode;
196 req->file = file;
197 req->in.numargs = 1;
198 req->in.args[0].size = sizeof(inarg);
199 req->in.args[0].value = &inarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700200 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700201 err = req->out.h.error;
202 fuse_put_request(fc, req);
203 if (err == -ENOSYS) {
204 fc->no_flush = 1;
205 err = 0;
206 }
207 return err;
208}
209
Miklos Szeredi82547982005-09-09 13:10:38 -0700210int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
211 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700212{
213 struct inode *inode = de->d_inode;
214 struct fuse_conn *fc = get_fuse_conn(inode);
215 struct fuse_file *ff = file->private_data;
216 struct fuse_req *req;
217 struct fuse_fsync_in inarg;
218 int err;
219
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800220 if (is_bad_inode(inode))
221 return -EIO;
222
Miklos Szeredi82547982005-09-09 13:10:38 -0700223 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700224 return 0;
225
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700226 req = fuse_get_req(fc);
227 if (IS_ERR(req))
228 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700229
230 memset(&inarg, 0, sizeof(inarg));
231 inarg.fh = ff->fh;
232 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700233 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700234 req->in.h.nodeid = get_node_id(inode);
235 req->inode = inode;
236 req->file = file;
237 req->in.numargs = 1;
238 req->in.args[0].size = sizeof(inarg);
239 req->in.args[0].value = &inarg;
240 request_send(fc, req);
241 err = req->out.h.error;
242 fuse_put_request(fc, req);
243 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700244 if (isdir)
245 fc->no_fsyncdir = 1;
246 else
247 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700248 err = 0;
249 }
250 return err;
251}
252
Miklos Szeredi82547982005-09-09 13:10:38 -0700253static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
254{
255 return fuse_fsync_common(file, de, datasync, 0);
256}
257
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800258void fuse_read_fill(struct fuse_req *req, struct file *file,
259 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700260{
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700261 struct fuse_file *ff = file->private_data;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800262 struct fuse_read_in *inarg = &req->misc.read_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700263
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800264 inarg->fh = ff->fh;
265 inarg->offset = pos;
266 inarg->size = count;
267 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700268 req->in.h.nodeid = get_node_id(inode);
269 req->inode = inode;
270 req->file = file;
271 req->in.numargs = 1;
272 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800273 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700274 req->out.argpages = 1;
275 req->out.argvar = 1;
276 req->out.numargs = 1;
277 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700278}
279
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800280static size_t fuse_send_read(struct fuse_req *req, struct file *file,
281 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700282{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800283 struct fuse_conn *fc = get_fuse_conn(inode);
284 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
285 request_send(fc, req);
286 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700287}
288
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700289static int fuse_readpage(struct file *file, struct page *page)
290{
291 struct inode *inode = page->mapping->host;
292 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800293 struct fuse_req *req;
294 int err;
295
296 err = -EIO;
297 if (is_bad_inode(inode))
298 goto out;
299
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700300 req = fuse_get_req(fc);
301 err = PTR_ERR(req);
302 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700303 goto out;
304
305 req->out.page_zeroing = 1;
306 req->num_pages = 1;
307 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800308 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700309 err = req->out.h.error;
310 fuse_put_request(fc, req);
311 if (!err)
312 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700313 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700314 out:
315 unlock_page(page);
316 return err;
317}
318
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800319static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700320{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800321 int i;
322
323 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
324
Miklos Szeredidb50b962005-09-09 13:10:33 -0700325 for (i = 0; i < req->num_pages; i++) {
326 struct page *page = req->pages[i];
327 if (!req->out.h.error)
328 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800329 else
330 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700331 unlock_page(page);
332 }
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800333 fuse_put_request(fc, req);
334}
335
336static void fuse_send_readpages(struct fuse_req *req, struct file *file,
337 struct inode *inode)
338{
339 struct fuse_conn *fc = get_fuse_conn(inode);
340 loff_t pos = page_offset(req->pages[0]);
341 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
342 req->out.page_zeroing = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800343 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800344 if (fc->async_read) {
345 req->end = fuse_readpages_end;
346 request_send_background(fc, req);
347 } else {
348 request_send(fc, req);
349 fuse_readpages_end(fc, req);
350 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700351}
352
353struct fuse_readpages_data {
354 struct fuse_req *req;
355 struct file *file;
356 struct inode *inode;
357};
358
359static int fuse_readpages_fill(void *_data, struct page *page)
360{
361 struct fuse_readpages_data *data = _data;
362 struct fuse_req *req = data->req;
363 struct inode *inode = data->inode;
364 struct fuse_conn *fc = get_fuse_conn(inode);
365
366 if (req->num_pages &&
367 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
368 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
369 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800370 fuse_send_readpages(req, data->file, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700371 data->req = req = fuse_get_req(fc);
372 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700373 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700374 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700375 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700376 }
377 req->pages[req->num_pages] = page;
378 req->num_pages ++;
379 return 0;
380}
381
382static int fuse_readpages(struct file *file, struct address_space *mapping,
383 struct list_head *pages, unsigned nr_pages)
384{
385 struct inode *inode = mapping->host;
386 struct fuse_conn *fc = get_fuse_conn(inode);
387 struct fuse_readpages_data data;
388 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800389
390 if (is_bad_inode(inode))
391 return -EIO;
392
Miklos Szeredidb50b962005-09-09 13:10:33 -0700393 data.file = file;
394 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700395 data.req = fuse_get_req(fc);
396 if (IS_ERR(data.req))
397 return PTR_ERR(data.req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700398
399 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700400 if (!err) {
401 if (data.req->num_pages)
402 fuse_send_readpages(data.req, file, inode);
403 else
404 fuse_put_request(fc, data.req);
405 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700406 return err;
407}
408
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700409static size_t fuse_send_write(struct fuse_req *req, struct file *file,
410 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700411{
412 struct fuse_conn *fc = get_fuse_conn(inode);
413 struct fuse_file *ff = file->private_data;
414 struct fuse_write_in inarg;
415 struct fuse_write_out outarg;
416
417 memset(&inarg, 0, sizeof(struct fuse_write_in));
418 inarg.fh = ff->fh;
419 inarg.offset = pos;
420 inarg.size = count;
421 req->in.h.opcode = FUSE_WRITE;
422 req->in.h.nodeid = get_node_id(inode);
423 req->inode = inode;
424 req->file = file;
425 req->in.argpages = 1;
426 req->in.numargs = 2;
427 req->in.args[0].size = sizeof(struct fuse_write_in);
428 req->in.args[0].value = &inarg;
429 req->in.args[1].size = count;
430 req->out.numargs = 1;
431 req->out.args[0].size = sizeof(struct fuse_write_out);
432 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700433 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700434 return outarg.size;
435}
436
437static int fuse_prepare_write(struct file *file, struct page *page,
438 unsigned offset, unsigned to)
439{
440 /* No op */
441 return 0;
442}
443
444static int fuse_commit_write(struct file *file, struct page *page,
445 unsigned offset, unsigned to)
446{
447 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700448 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700449 unsigned count = to - offset;
450 struct inode *inode = page->mapping->host;
451 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi4633a222006-01-06 00:19:36 -0800452 loff_t pos = page_offset(page) + offset;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800453 struct fuse_req *req;
454
455 if (is_bad_inode(inode))
456 return -EIO;
457
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700458 req = fuse_get_req(fc);
459 if (IS_ERR(req))
460 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700461
462 req->num_pages = 1;
463 req->pages[0] = page;
464 req->page_offset = offset;
465 nres = fuse_send_write(req, file, inode, pos, count);
466 err = req->out.h.error;
467 fuse_put_request(fc, req);
468 if (!err && nres != count)
469 err = -EIO;
470 if (!err) {
471 pos += count;
472 if (pos > i_size_read(inode))
473 i_size_write(inode, pos);
474
475 if (offset == 0 && to == PAGE_CACHE_SIZE) {
476 clear_page_dirty(page);
477 SetPageUptodate(page);
478 }
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700479 }
480 fuse_invalidate_attr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700481 return err;
482}
483
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700484static void fuse_release_user_pages(struct fuse_req *req, int write)
485{
486 unsigned i;
487
488 for (i = 0; i < req->num_pages; i++) {
489 struct page *page = req->pages[i];
490 if (write)
491 set_page_dirty_lock(page);
492 put_page(page);
493 }
494}
495
496static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
497 unsigned nbytes, int write)
498{
499 unsigned long user_addr = (unsigned long) buf;
500 unsigned offset = user_addr & ~PAGE_MASK;
501 int npages;
502
503 /* This doesn't work with nfsd */
504 if (!current->mm)
505 return -EPERM;
506
507 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
508 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800509 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700510 down_read(&current->mm->mmap_sem);
511 npages = get_user_pages(current, current->mm, user_addr, npages, write,
512 0, req->pages, NULL);
513 up_read(&current->mm->mmap_sem);
514 if (npages < 0)
515 return npages;
516
517 req->num_pages = npages;
518 req->page_offset = offset;
519 return 0;
520}
521
522static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
523 size_t count, loff_t *ppos, int write)
524{
525 struct inode *inode = file->f_dentry->d_inode;
526 struct fuse_conn *fc = get_fuse_conn(inode);
527 size_t nmax = write ? fc->max_write : fc->max_read;
528 loff_t pos = *ppos;
529 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800530 struct fuse_req *req;
531
532 if (is_bad_inode(inode))
533 return -EIO;
534
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700535 req = fuse_get_req(fc);
536 if (IS_ERR(req))
537 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700538
539 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700540 size_t nres;
541 size_t nbytes = min(count, nmax);
542 int err = fuse_get_user_pages(req, buf, nbytes, !write);
543 if (err) {
544 res = err;
545 break;
546 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800547 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
548 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700549 if (write)
550 nres = fuse_send_write(req, file, inode, pos, nbytes);
551 else
552 nres = fuse_send_read(req, file, inode, pos, nbytes);
553 fuse_release_user_pages(req, !write);
554 if (req->out.h.error) {
555 if (!res)
556 res = req->out.h.error;
557 break;
558 } else if (nres > nbytes) {
559 res = -EIO;
560 break;
561 }
562 count -= nres;
563 res += nres;
564 pos += nres;
565 buf += nres;
566 if (nres != nbytes)
567 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200568 if (count) {
569 fuse_put_request(fc, req);
570 req = fuse_get_req(fc);
571 if (IS_ERR(req))
572 break;
573 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700574 }
575 fuse_put_request(fc, req);
576 if (res > 0) {
577 if (write && pos > i_size_read(inode))
578 i_size_write(inode, pos);
579 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700580 }
581 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700582
583 return res;
584}
585
586static ssize_t fuse_direct_read(struct file *file, char __user *buf,
587 size_t count, loff_t *ppos)
588{
589 return fuse_direct_io(file, buf, count, ppos, 0);
590}
591
592static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
593 size_t count, loff_t *ppos)
594{
595 struct inode *inode = file->f_dentry->d_inode;
596 ssize_t res;
597 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800598 mutex_lock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700599 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800600 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700601 return res;
602}
603
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700604static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
605{
606 if ((vma->vm_flags & VM_SHARED)) {
607 if ((vma->vm_flags & VM_WRITE))
608 return -ENODEV;
609 else
610 vma->vm_flags &= ~VM_MAYWRITE;
611 }
612 return generic_file_mmap(file, vma);
613}
614
615static int fuse_set_page_dirty(struct page *page)
616{
617 printk("fuse_set_page_dirty: should not happen\n");
618 dump_stack();
619 return 0;
620}
621
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800622static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700623 .llseek = generic_file_llseek,
624 .read = generic_file_read,
625 .write = generic_file_write,
626 .mmap = fuse_file_mmap,
627 .open = fuse_open,
628 .flush = fuse_flush,
629 .release = fuse_release,
630 .fsync = fuse_fsync,
631 .sendfile = generic_file_sendfile,
632};
633
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800634static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700635 .llseek = generic_file_llseek,
636 .read = fuse_direct_read,
637 .write = fuse_direct_write,
638 .open = fuse_open,
639 .flush = fuse_flush,
640 .release = fuse_release,
641 .fsync = fuse_fsync,
642 /* no mmap and sendfile */
643};
644
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700645static struct address_space_operations fuse_file_aops = {
646 .readpage = fuse_readpage,
647 .prepare_write = fuse_prepare_write,
648 .commit_write = fuse_commit_write,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700649 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700650 .set_page_dirty = fuse_set_page_dirty,
651};
652
653void fuse_init_file_inode(struct inode *inode)
654{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700655 inode->i_fop = &fuse_file_operations;
656 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700657}