[PATCH] USB: S3C2410: use clk_enable() to ensure 48MHz to OHCI core
[linux-2.6/openmoko-kernel/knife-kernel.git] / fs / fuse / file.c
blobfc342cf7c2cc7881dbfafa09097d6de52b34bdca
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
15 static const struct file_operations fuse_direct_io_file_operations;
17 static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
18 struct fuse_open_out *outargp)
20 struct fuse_conn *fc = get_fuse_conn(inode);
21 struct fuse_open_in inarg;
22 struct fuse_req *req;
23 int err;
25 req = fuse_get_req(fc);
26 if (IS_ERR(req))
27 return PTR_ERR(req);
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);
44 return err;
47 struct fuse_file *fuse_file_alloc(void)
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;
58 return ff;
61 void fuse_file_free(struct fuse_file *ff)
63 fuse_request_free(ff->release_req);
64 kfree(ff);
67 void fuse_finish_open(struct inode *inode, struct file *file,
68 struct fuse_file *ff, struct fuse_open_out *outarg)
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;
78 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
80 struct fuse_open_out outarg;
81 struct fuse_file *ff;
82 int err;
84 /* VFS checks this, but only _after_ ->open() */
85 if (file->f_flags & O_DIRECT)
86 return -EINVAL;
88 err = generic_file_open(inode, file);
89 if (err)
90 return err;
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) {
95 err = fuse_do_getattr(inode);
96 if (err)
97 return err;
100 ff = fuse_file_alloc();
101 if (!ff)
102 return -ENOMEM;
104 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);
113 return err;
116 /* Special case for failed iget in CREATE */
117 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
119 /* 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);
129 void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
130 u64 nodeid, struct inode *inode, int flags, int isdir)
132 struct fuse_req * req = ff->release_req;
133 struct fuse_release_in *inarg = &req->misc.release_in;
135 inarg->fh = ff->fh;
136 inarg->flags = flags;
137 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
138 req->in.h.nodeid = nodeid;
139 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);
144 if (!inode)
145 req->end = fuse_release_end;
146 kfree(ff);
149 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
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);
158 /* Return value is ignored by VFS */
159 return 0;
162 static int fuse_open(struct inode *inode, struct file *file)
164 return fuse_open_common(inode, file, 0);
167 static int fuse_release(struct inode *inode, struct file *file)
169 return fuse_release_common(inode, file, 0);
172 static int fuse_flush(struct file *file)
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;
181 if (is_bad_inode(inode))
182 return -EIO;
184 if (fc->no_flush)
185 return 0;
187 req = fuse_get_req(fc);
188 if (IS_ERR(req))
189 return PTR_ERR(req);
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;
200 request_send(fc, req);
201 err = req->out.h.error;
202 fuse_put_request(fc, req);
203 if (err == -ENOSYS) {
204 fc->no_flush = 1;
205 err = 0;
207 return err;
210 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
211 int isdir)
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;
220 if (is_bad_inode(inode))
221 return -EIO;
223 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
224 return 0;
226 req = fuse_get_req(fc);
227 if (IS_ERR(req))
228 return PTR_ERR(req);
230 memset(&inarg, 0, sizeof(inarg));
231 inarg.fh = ff->fh;
232 inarg.fsync_flags = datasync ? 1 : 0;
233 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
234 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) {
244 if (isdir)
245 fc->no_fsyncdir = 1;
246 else
247 fc->no_fsync = 1;
248 err = 0;
250 return err;
253 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
255 return fuse_fsync_common(file, de, datasync, 0);
258 void fuse_read_fill(struct fuse_req *req, struct file *file,
259 struct inode *inode, loff_t pos, size_t count, int opcode)
261 struct fuse_file *ff = file->private_data;
262 struct fuse_read_in *inarg = &req->misc.read_in;
264 inarg->fh = ff->fh;
265 inarg->offset = pos;
266 inarg->size = count;
267 req->in.h.opcode = opcode;
268 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);
273 req->in.args[0].value = inarg;
274 req->out.argpages = 1;
275 req->out.argvar = 1;
276 req->out.numargs = 1;
277 req->out.args[0].size = count;
280 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
281 struct inode *inode, loff_t pos, size_t count)
283 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;
289 static int fuse_readpage(struct file *file, struct page *page)
291 struct inode *inode = page->mapping->host;
292 struct fuse_conn *fc = get_fuse_conn(inode);
293 struct fuse_req *req;
294 int err;
296 err = -EIO;
297 if (is_bad_inode(inode))
298 goto out;
300 req = fuse_get_req(fc);
301 err = PTR_ERR(req);
302 if (IS_ERR(req))
303 goto out;
305 req->out.page_zeroing = 1;
306 req->num_pages = 1;
307 req->pages[0] = page;
308 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
309 err = req->out.h.error;
310 fuse_put_request(fc, req);
311 if (!err)
312 SetPageUptodate(page);
313 fuse_invalidate_attr(inode); /* atime changed */
314 out:
315 unlock_page(page);
316 return err;
319 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
321 int i;
323 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
325 for (i = 0; i < req->num_pages; i++) {
326 struct page *page = req->pages[i];
327 if (!req->out.h.error)
328 SetPageUptodate(page);
329 else
330 SetPageError(page);
331 unlock_page(page);
333 fuse_put_request(fc, req);
336 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
337 struct inode *inode)
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;
343 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
344 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);
353 struct fuse_readpages_data {
354 struct fuse_req *req;
355 struct file *file;
356 struct inode *inode;
359 static int fuse_readpages_fill(void *_data, struct page *page)
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);
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)) {
370 fuse_send_readpages(req, data->file, inode);
371 data->req = req = fuse_get_req(fc);
372 if (IS_ERR(req)) {
373 unlock_page(page);
374 return PTR_ERR(req);
377 req->pages[req->num_pages] = page;
378 req->num_pages ++;
379 return 0;
382 static int fuse_readpages(struct file *file, struct address_space *mapping,
383 struct list_head *pages, unsigned nr_pages)
385 struct inode *inode = mapping->host;
386 struct fuse_conn *fc = get_fuse_conn(inode);
387 struct fuse_readpages_data data;
388 int err;
390 if (is_bad_inode(inode))
391 return -EIO;
393 data.file = file;
394 data.inode = inode;
395 data.req = fuse_get_req(fc);
396 if (IS_ERR(data.req))
397 return PTR_ERR(data.req);
399 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
400 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);
406 return err;
409 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
410 struct inode *inode, loff_t pos, size_t count)
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;
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;
433 request_send(fc, req);
434 return outarg.size;
437 static int fuse_prepare_write(struct file *file, struct page *page,
438 unsigned offset, unsigned to)
440 /* No op */
441 return 0;
444 static int fuse_commit_write(struct file *file, struct page *page,
445 unsigned offset, unsigned to)
447 int err;
448 size_t nres;
449 unsigned count = to - offset;
450 struct inode *inode = page->mapping->host;
451 struct fuse_conn *fc = get_fuse_conn(inode);
452 loff_t pos = page_offset(page) + offset;
453 struct fuse_req *req;
455 if (is_bad_inode(inode))
456 return -EIO;
458 req = fuse_get_req(fc);
459 if (IS_ERR(req))
460 return PTR_ERR(req);
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);
475 if (offset == 0 && to == PAGE_CACHE_SIZE) {
476 clear_page_dirty(page);
477 SetPageUptodate(page);
480 fuse_invalidate_attr(inode);
481 return err;
484 static void fuse_release_user_pages(struct fuse_req *req, int write)
486 unsigned i;
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);
496 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
497 unsigned nbytes, int write)
499 unsigned long user_addr = (unsigned long) buf;
500 unsigned offset = user_addr & ~PAGE_MASK;
501 int npages;
503 /* This doesn't work with nfsd */
504 if (!current->mm)
505 return -EPERM;
507 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
508 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
509 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
510 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;
517 req->num_pages = npages;
518 req->page_offset = offset;
519 return 0;
522 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
523 size_t count, loff_t *ppos, int write)
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;
530 struct fuse_req *req;
532 if (is_bad_inode(inode))
533 return -EIO;
535 req = fuse_get_req(fc);
536 if (IS_ERR(req))
537 return PTR_ERR(req);
539 while (count) {
540 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;
547 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
548 nbytes = min(count, nbytes);
549 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;
562 count -= nres;
563 res += nres;
564 pos += nres;
565 buf += nres;
566 if (nres != nbytes)
567 break;
568 if (count) {
569 fuse_put_request(fc, req);
570 req = fuse_get_req(fc);
571 if (IS_ERR(req))
572 break;
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;
581 fuse_invalidate_attr(inode);
583 return res;
586 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
587 size_t count, loff_t *ppos)
589 return fuse_direct_io(file, buf, count, ppos, 0);
592 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
593 size_t count, loff_t *ppos)
595 struct inode *inode = file->f_dentry->d_inode;
596 ssize_t res;
597 /* Don't allow parallel writes to the same file */
598 mutex_lock(&inode->i_mutex);
599 res = fuse_direct_io(file, buf, count, ppos, 1);
600 mutex_unlock(&inode->i_mutex);
601 return res;
604 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
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;
612 return generic_file_mmap(file, vma);
615 static int fuse_set_page_dirty(struct page *page)
617 printk("fuse_set_page_dirty: should not happen\n");
618 dump_stack();
619 return 0;
622 static const struct file_operations fuse_file_operations = {
623 .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,
634 static const struct file_operations fuse_direct_io_file_operations = {
635 .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 */
645 static struct address_space_operations fuse_file_aops = {
646 .readpage = fuse_readpage,
647 .prepare_write = fuse_prepare_write,
648 .commit_write = fuse_commit_write,
649 .readpages = fuse_readpages,
650 .set_page_dirty = fuse_set_page_dirty,
653 void fuse_init_file_inode(struct inode *inode)
655 inode->i_fop = &fuse_file_operations;
656 inode->i_data.a_ops = &fuse_file_aops;