blob: a8dc88527fbe61421c5d199c40fa9d3254cebc73 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
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
Miklos Szeredi45323fb2005-09-09 13:10:37 -070015static struct file_operations fuse_direct_io_file_operations;
16
Miklos Szeredi04730fe2005-09-09 13:10:36 -070017int fuse_open_common(struct inode *inode, struct file *file, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070018{
19 struct fuse_conn *fc = get_fuse_conn(inode);
20 struct fuse_req *req;
21 struct fuse_open_in inarg;
22 struct fuse_open_out outarg;
23 struct fuse_file *ff;
24 int err;
25 /* Restarting the syscall is not allowed if O_CREAT and O_EXCL
26 are both set, because creation will fail on the restart */
27 int excl = (file->f_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL);
28
29 err = generic_file_open(inode, file);
30 if (err)
31 return err;
32
33 /* If opening the root node, no lookup has been performed on
34 it, so the attributes must be refreshed */
35 if (get_node_id(inode) == FUSE_ROOT_ID) {
36 int err = fuse_do_getattr(inode);
37 if (err)
38 return err;
39 }
40
41 if (excl)
42 req = fuse_get_request_nonint(fc);
43 else
44 req = fuse_get_request(fc);
45 if (!req)
46 return excl ? -EINTR : -ERESTARTSYS;
47
48 err = -ENOMEM;
49 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
50 if (!ff)
51 goto out_put_request;
52
53 ff->release_req = fuse_request_alloc();
54 if (!ff->release_req) {
55 kfree(ff);
56 goto out_put_request;
57 }
58
59 memset(&inarg, 0, sizeof(inarg));
60 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
Miklos Szeredi04730fe2005-09-09 13:10:36 -070061 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
Miklos Szeredib6aeade2005-09-09 13:10:30 -070062 req->in.h.nodeid = get_node_id(inode);
63 req->inode = inode;
64 req->in.numargs = 1;
65 req->in.args[0].size = sizeof(inarg);
66 req->in.args[0].value = &inarg;
67 req->out.numargs = 1;
68 req->out.args[0].size = sizeof(outarg);
69 req->out.args[0].value = &outarg;
70 if (excl)
71 request_send_nonint(fc, req);
72 else
73 request_send(fc, req);
74 err = req->out.h.error;
Miklos Szeredib6aeade2005-09-09 13:10:30 -070075 if (err) {
76 fuse_request_free(ff->release_req);
77 kfree(ff);
78 } else {
Miklos Szeredi45323fb2005-09-09 13:10:37 -070079 if (!isdir && (outarg.open_flags & FOPEN_DIRECT_IO))
80 file->f_op = &fuse_direct_io_file_operations;
81 if (!(outarg.open_flags & FOPEN_KEEP_CACHE))
82 invalidate_inode_pages(inode->i_mapping);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070083 ff->fh = outarg.fh;
84 file->private_data = ff;
85 }
86
87 out_put_request:
88 fuse_put_request(fc, req);
89 return err;
90}
91
Miklos Szeredi04730fe2005-09-09 13:10:36 -070092int fuse_release_common(struct inode *inode, struct file *file, int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070093{
94 struct fuse_conn *fc = get_fuse_conn(inode);
95 struct fuse_file *ff = file->private_data;
96 struct fuse_req *req = ff->release_req;
97 struct fuse_release_in *inarg = &req->misc.release_in;
98
99 inarg->fh = ff->fh;
100 inarg->flags = file->f_flags & ~O_EXCL;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700101 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700102 req->in.h.nodeid = get_node_id(inode);
103 req->inode = inode;
104 req->in.numargs = 1;
105 req->in.args[0].size = sizeof(struct fuse_release_in);
106 req->in.args[0].value = inarg;
107 request_send_background(fc, req);
108 kfree(ff);
109
110 /* Return value is ignored by VFS */
111 return 0;
112}
113
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700114static int fuse_open(struct inode *inode, struct file *file)
115{
116 return fuse_open_common(inode, file, 0);
117}
118
119static int fuse_release(struct inode *inode, struct file *file)
120{
121 return fuse_release_common(inode, file, 0);
122}
123
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700124static int fuse_flush(struct file *file)
125{
126 struct inode *inode = file->f_dentry->d_inode;
127 struct fuse_conn *fc = get_fuse_conn(inode);
128 struct fuse_file *ff = file->private_data;
129 struct fuse_req *req;
130 struct fuse_flush_in inarg;
131 int err;
132
133 if (fc->no_flush)
134 return 0;
135
136 req = fuse_get_request_nonint(fc);
137 if (!req)
138 return -EINTR;
139
140 memset(&inarg, 0, sizeof(inarg));
141 inarg.fh = ff->fh;
142 req->in.h.opcode = FUSE_FLUSH;
143 req->in.h.nodeid = get_node_id(inode);
144 req->inode = inode;
145 req->file = file;
146 req->in.numargs = 1;
147 req->in.args[0].size = sizeof(inarg);
148 req->in.args[0].value = &inarg;
149 request_send_nonint(fc, req);
150 err = req->out.h.error;
151 fuse_put_request(fc, req);
152 if (err == -ENOSYS) {
153 fc->no_flush = 1;
154 err = 0;
155 }
156 return err;
157}
158
159static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
160{
161 struct inode *inode = de->d_inode;
162 struct fuse_conn *fc = get_fuse_conn(inode);
163 struct fuse_file *ff = file->private_data;
164 struct fuse_req *req;
165 struct fuse_fsync_in inarg;
166 int err;
167
168 if (fc->no_fsync)
169 return 0;
170
171 req = fuse_get_request(fc);
172 if (!req)
173 return -ERESTARTSYS;
174
175 memset(&inarg, 0, sizeof(inarg));
176 inarg.fh = ff->fh;
177 inarg.fsync_flags = datasync ? 1 : 0;
178 req->in.h.opcode = FUSE_FSYNC;
179 req->in.h.nodeid = get_node_id(inode);
180 req->inode = inode;
181 req->file = file;
182 req->in.numargs = 1;
183 req->in.args[0].size = sizeof(inarg);
184 req->in.args[0].value = &inarg;
185 request_send(fc, req);
186 err = req->out.h.error;
187 fuse_put_request(fc, req);
188 if (err == -ENOSYS) {
189 fc->no_fsync = 1;
190 err = 0;
191 }
192 return err;
193}
194
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700195size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
196 struct inode *inode, loff_t pos, size_t count,
197 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700198{
199 struct fuse_conn *fc = get_fuse_conn(inode);
200 struct fuse_file *ff = file->private_data;
201 struct fuse_read_in inarg;
202
203 memset(&inarg, 0, sizeof(struct fuse_read_in));
204 inarg.fh = ff->fh;
205 inarg.offset = pos;
206 inarg.size = count;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700207 req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700208 req->in.h.nodeid = get_node_id(inode);
209 req->inode = inode;
210 req->file = file;
211 req->in.numargs = 1;
212 req->in.args[0].size = sizeof(struct fuse_read_in);
213 req->in.args[0].value = &inarg;
214 req->out.argpages = 1;
215 req->out.argvar = 1;
216 req->out.numargs = 1;
217 req->out.args[0].size = count;
218 request_send_nonint(fc, req);
219 return req->out.args[0].size;
220}
221
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700222static inline size_t fuse_send_read(struct fuse_req *req, struct file *file,
223 struct inode *inode, loff_t pos,
224 size_t count)
225{
226 return fuse_send_read_common(req, file, inode, pos, count, 0);
227}
228
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700229static int fuse_readpage(struct file *file, struct page *page)
230{
231 struct inode *inode = page->mapping->host;
232 struct fuse_conn *fc = get_fuse_conn(inode);
233 loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT;
234 struct fuse_req *req = fuse_get_request_nonint(fc);
235 int err = -EINTR;
236 if (!req)
237 goto out;
238
239 req->out.page_zeroing = 1;
240 req->num_pages = 1;
241 req->pages[0] = page;
242 fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE);
243 err = req->out.h.error;
244 fuse_put_request(fc, req);
245 if (!err)
246 SetPageUptodate(page);
247 out:
248 unlock_page(page);
249 return err;
250}
251
Miklos Szeredidb50b962005-09-09 13:10:33 -0700252static int fuse_send_readpages(struct fuse_req *req, struct file *file,
253 struct inode *inode)
254{
255 loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT;
256 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
257 unsigned i;
258 req->out.page_zeroing = 1;
259 fuse_send_read(req, file, inode, pos, count);
260 for (i = 0; i < req->num_pages; i++) {
261 struct page *page = req->pages[i];
262 if (!req->out.h.error)
263 SetPageUptodate(page);
264 unlock_page(page);
265 }
266 return req->out.h.error;
267}
268
269struct fuse_readpages_data {
270 struct fuse_req *req;
271 struct file *file;
272 struct inode *inode;
273};
274
275static int fuse_readpages_fill(void *_data, struct page *page)
276{
277 struct fuse_readpages_data *data = _data;
278 struct fuse_req *req = data->req;
279 struct inode *inode = data->inode;
280 struct fuse_conn *fc = get_fuse_conn(inode);
281
282 if (req->num_pages &&
283 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
284 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
285 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
286 int err = fuse_send_readpages(req, data->file, inode);
287 if (err) {
288 unlock_page(page);
289 return err;
290 }
291 fuse_reset_request(req);
292 }
293 req->pages[req->num_pages] = page;
294 req->num_pages ++;
295 return 0;
296}
297
298static int fuse_readpages(struct file *file, struct address_space *mapping,
299 struct list_head *pages, unsigned nr_pages)
300{
301 struct inode *inode = mapping->host;
302 struct fuse_conn *fc = get_fuse_conn(inode);
303 struct fuse_readpages_data data;
304 int err;
305 data.file = file;
306 data.inode = inode;
307 data.req = fuse_get_request_nonint(fc);
308 if (!data.req)
309 return -EINTR;
310
311 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
312 if (!err && data.req->num_pages)
313 err = fuse_send_readpages(data.req, file, inode);
314 fuse_put_request(fc, data.req);
315 return err;
316}
317
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700318static size_t fuse_send_write(struct fuse_req *req, struct file *file,
319 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700320{
321 struct fuse_conn *fc = get_fuse_conn(inode);
322 struct fuse_file *ff = file->private_data;
323 struct fuse_write_in inarg;
324 struct fuse_write_out outarg;
325
326 memset(&inarg, 0, sizeof(struct fuse_write_in));
327 inarg.fh = ff->fh;
328 inarg.offset = pos;
329 inarg.size = count;
330 req->in.h.opcode = FUSE_WRITE;
331 req->in.h.nodeid = get_node_id(inode);
332 req->inode = inode;
333 req->file = file;
334 req->in.argpages = 1;
335 req->in.numargs = 2;
336 req->in.args[0].size = sizeof(struct fuse_write_in);
337 req->in.args[0].value = &inarg;
338 req->in.args[1].size = count;
339 req->out.numargs = 1;
340 req->out.args[0].size = sizeof(struct fuse_write_out);
341 req->out.args[0].value = &outarg;
342 request_send_nonint(fc, req);
343 return outarg.size;
344}
345
346static int fuse_prepare_write(struct file *file, struct page *page,
347 unsigned offset, unsigned to)
348{
349 /* No op */
350 return 0;
351}
352
353static int fuse_commit_write(struct file *file, struct page *page,
354 unsigned offset, unsigned to)
355{
356 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700357 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700358 unsigned count = to - offset;
359 struct inode *inode = page->mapping->host;
360 struct fuse_conn *fc = get_fuse_conn(inode);
361 loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset;
362 struct fuse_req *req = fuse_get_request_nonint(fc);
363 if (!req)
364 return -EINTR;
365
366 req->num_pages = 1;
367 req->pages[0] = page;
368 req->page_offset = offset;
369 nres = fuse_send_write(req, file, inode, pos, count);
370 err = req->out.h.error;
371 fuse_put_request(fc, req);
372 if (!err && nres != count)
373 err = -EIO;
374 if (!err) {
375 pos += count;
376 if (pos > i_size_read(inode))
377 i_size_write(inode, pos);
378
379 if (offset == 0 && to == PAGE_CACHE_SIZE) {
380 clear_page_dirty(page);
381 SetPageUptodate(page);
382 }
383 } else if (err == -EINTR || err == -EIO)
384 fuse_invalidate_attr(inode);
385 return err;
386}
387
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700388static void fuse_release_user_pages(struct fuse_req *req, int write)
389{
390 unsigned i;
391
392 for (i = 0; i < req->num_pages; i++) {
393 struct page *page = req->pages[i];
394 if (write)
395 set_page_dirty_lock(page);
396 put_page(page);
397 }
398}
399
400static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
401 unsigned nbytes, int write)
402{
403 unsigned long user_addr = (unsigned long) buf;
404 unsigned offset = user_addr & ~PAGE_MASK;
405 int npages;
406
407 /* This doesn't work with nfsd */
408 if (!current->mm)
409 return -EPERM;
410
411 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
412 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
413 npages = min(npages, FUSE_MAX_PAGES_PER_REQ);
414 down_read(&current->mm->mmap_sem);
415 npages = get_user_pages(current, current->mm, user_addr, npages, write,
416 0, req->pages, NULL);
417 up_read(&current->mm->mmap_sem);
418 if (npages < 0)
419 return npages;
420
421 req->num_pages = npages;
422 req->page_offset = offset;
423 return 0;
424}
425
426static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
427 size_t count, loff_t *ppos, int write)
428{
429 struct inode *inode = file->f_dentry->d_inode;
430 struct fuse_conn *fc = get_fuse_conn(inode);
431 size_t nmax = write ? fc->max_write : fc->max_read;
432 loff_t pos = *ppos;
433 ssize_t res = 0;
434 struct fuse_req *req = fuse_get_request(fc);
435 if (!req)
436 return -ERESTARTSYS;
437
438 while (count) {
439 size_t tmp;
440 size_t nres;
441 size_t nbytes = min(count, nmax);
442 int err = fuse_get_user_pages(req, buf, nbytes, !write);
443 if (err) {
444 res = err;
445 break;
446 }
447 tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset;
448 nbytes = min(nbytes, tmp);
449 if (write)
450 nres = fuse_send_write(req, file, inode, pos, nbytes);
451 else
452 nres = fuse_send_read(req, file, inode, pos, nbytes);
453 fuse_release_user_pages(req, !write);
454 if (req->out.h.error) {
455 if (!res)
456 res = req->out.h.error;
457 break;
458 } else if (nres > nbytes) {
459 res = -EIO;
460 break;
461 }
462 count -= nres;
463 res += nres;
464 pos += nres;
465 buf += nres;
466 if (nres != nbytes)
467 break;
468 if (count)
469 fuse_reset_request(req);
470 }
471 fuse_put_request(fc, req);
472 if (res > 0) {
473 if (write && pos > i_size_read(inode))
474 i_size_write(inode, pos);
475 *ppos = pos;
476 } else if (write && (res == -EINTR || res == -EIO))
477 fuse_invalidate_attr(inode);
478
479 return res;
480}
481
482static ssize_t fuse_direct_read(struct file *file, char __user *buf,
483 size_t count, loff_t *ppos)
484{
485 return fuse_direct_io(file, buf, count, ppos, 0);
486}
487
488static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
489 size_t count, loff_t *ppos)
490{
491 struct inode *inode = file->f_dentry->d_inode;
492 ssize_t res;
493 /* Don't allow parallel writes to the same file */
494 down(&inode->i_sem);
495 res = fuse_direct_io(file, buf, count, ppos, 1);
496 up(&inode->i_sem);
497 return res;
498}
499
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700500static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
501{
502 if ((vma->vm_flags & VM_SHARED)) {
503 if ((vma->vm_flags & VM_WRITE))
504 return -ENODEV;
505 else
506 vma->vm_flags &= ~VM_MAYWRITE;
507 }
508 return generic_file_mmap(file, vma);
509}
510
511static int fuse_set_page_dirty(struct page *page)
512{
513 printk("fuse_set_page_dirty: should not happen\n");
514 dump_stack();
515 return 0;
516}
517
518static struct file_operations fuse_file_operations = {
519 .llseek = generic_file_llseek,
520 .read = generic_file_read,
521 .write = generic_file_write,
522 .mmap = fuse_file_mmap,
523 .open = fuse_open,
524 .flush = fuse_flush,
525 .release = fuse_release,
526 .fsync = fuse_fsync,
527 .sendfile = generic_file_sendfile,
528};
529
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700530static struct file_operations fuse_direct_io_file_operations = {
531 .llseek = generic_file_llseek,
532 .read = fuse_direct_read,
533 .write = fuse_direct_write,
534 .open = fuse_open,
535 .flush = fuse_flush,
536 .release = fuse_release,
537 .fsync = fuse_fsync,
538 /* no mmap and sendfile */
539};
540
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700541static struct address_space_operations fuse_file_aops = {
542 .readpage = fuse_readpage,
543 .prepare_write = fuse_prepare_write,
544 .commit_write = fuse_commit_write,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700545 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700546 .set_page_dirty = fuse_set_page_dirty,
547};
548
549void fuse_init_file_inode(struct inode *inode)
550{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700551 inode->i_fop = &fuse_file_operations;
552 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700553}