aboutsummaryrefslogtreecommitdiffstats
path: root/fs/fuse/file.c
diff options
context:
space:
mode:
authorMiklos Szeredi <miklos@szeredi.hu>2005-09-09 16:10:30 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2005-09-09 17:03:45 -0400
commitb6aeadeda22a9aa322fdfcd3f4c69ccf0da5cbdd (patch)
tree794afec0eeb13722550a97783ec0cfb95e5e83cb /fs/fuse/file.c
parent9e6268db496a2592e89457537ea54a496feabb77 (diff)
[PATCH] FUSE - file operations
This patch adds the file operations of FUSE. The following operations are added: o open o flush o release o fsync o readpage o commit_write Signed-off-by: Miklos Szeredi <miklos@szeredi.hu> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'fs/fuse/file.c')
-rw-r--r--fs/fuse/file.c341
1 files changed, 341 insertions, 0 deletions
diff --git a/fs/fuse/file.c b/fs/fuse/file.c
new file mode 100644
index 000000000000..de8c9c702461
--- /dev/null
+++ b/fs/fuse/file.c
@@ -0,0 +1,341 @@
1/*
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
15static int fuse_open(struct inode *inode, struct file *file)
16{
17 struct fuse_conn *fc = get_fuse_conn(inode);
18 struct fuse_req *req;
19 struct fuse_open_in inarg;
20 struct fuse_open_out outarg;
21 struct fuse_file *ff;
22 int err;
23 /* Restarting the syscall is not allowed if O_CREAT and O_EXCL
24 are both set, because creation will fail on the restart */
25 int excl = (file->f_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL);
26
27 err = generic_file_open(inode, file);
28 if (err)
29 return err;
30
31 /* If opening the root node, no lookup has been performed on
32 it, so the attributes must be refreshed */
33 if (get_node_id(inode) == FUSE_ROOT_ID) {
34 int err = fuse_do_getattr(inode);
35 if (err)
36 return err;
37 }
38
39 if (excl)
40 req = fuse_get_request_nonint(fc);
41 else
42 req = fuse_get_request(fc);
43 if (!req)
44 return excl ? -EINTR : -ERESTARTSYS;
45
46 err = -ENOMEM;
47 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
48 if (!ff)
49 goto out_put_request;
50
51 ff->release_req = fuse_request_alloc();
52 if (!ff->release_req) {
53 kfree(ff);
54 goto out_put_request;
55 }
56
57 memset(&inarg, 0, sizeof(inarg));
58 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
59 req->in.h.opcode = FUSE_OPEN;
60 req->in.h.nodeid = get_node_id(inode);
61 req->inode = inode;
62 req->in.numargs = 1;
63 req->in.args[0].size = sizeof(inarg);
64 req->in.args[0].value = &inarg;
65 req->out.numargs = 1;
66 req->out.args[0].size = sizeof(outarg);
67 req->out.args[0].value = &outarg;
68 if (excl)
69 request_send_nonint(fc, req);
70 else
71 request_send(fc, req);
72 err = req->out.h.error;
73 if (!err)
74 invalidate_inode_pages(inode->i_mapping);
75 if (err) {
76 fuse_request_free(ff->release_req);
77 kfree(ff);
78 } else {
79 ff->fh = outarg.fh;
80 file->private_data = ff;
81 }
82
83 out_put_request:
84 fuse_put_request(fc, req);
85 return err;
86}
87
88static int fuse_release(struct inode *inode, struct file *file)
89{
90 struct fuse_conn *fc = get_fuse_conn(inode);
91 struct fuse_file *ff = file->private_data;
92 struct fuse_req *req = ff->release_req;
93 struct fuse_release_in *inarg = &req->misc.release_in;
94
95 inarg->fh = ff->fh;
96 inarg->flags = file->f_flags & ~O_EXCL;
97 req->in.h.opcode = FUSE_RELEASE;
98 req->in.h.nodeid = get_node_id(inode);
99 req->inode = inode;
100 req->in.numargs = 1;
101 req->in.args[0].size = sizeof(struct fuse_release_in);
102 req->in.args[0].value = inarg;
103 request_send_background(fc, req);
104 kfree(ff);
105
106 /* Return value is ignored by VFS */
107 return 0;
108}
109
110static int fuse_flush(struct file *file)
111{
112 struct inode *inode = file->f_dentry->d_inode;
113 struct fuse_conn *fc = get_fuse_conn(inode);
114 struct fuse_file *ff = file->private_data;
115 struct fuse_req *req;
116 struct fuse_flush_in inarg;
117 int err;
118
119 if (fc->no_flush)
120 return 0;
121
122 req = fuse_get_request_nonint(fc);
123 if (!req)
124 return -EINTR;
125
126 memset(&inarg, 0, sizeof(inarg));
127 inarg.fh = ff->fh;
128 req->in.h.opcode = FUSE_FLUSH;
129 req->in.h.nodeid = get_node_id(inode);
130 req->inode = inode;
131 req->file = file;
132 req->in.numargs = 1;
133 req->in.args[0].size = sizeof(inarg);
134 req->in.args[0].value = &inarg;
135 request_send_nonint(fc, req);
136 err = req->out.h.error;
137 fuse_put_request(fc, req);
138 if (err == -ENOSYS) {
139 fc->no_flush = 1;
140 err = 0;
141 }
142 return err;
143}
144
145static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
146{
147 struct inode *inode = de->d_inode;
148 struct fuse_conn *fc = get_fuse_conn(inode);
149 struct fuse_file *ff = file->private_data;
150 struct fuse_req *req;
151 struct fuse_fsync_in inarg;
152 int err;
153
154 if (fc->no_fsync)
155 return 0;
156
157 req = fuse_get_request(fc);
158 if (!req)
159 return -ERESTARTSYS;
160
161 memset(&inarg, 0, sizeof(inarg));
162 inarg.fh = ff->fh;
163 inarg.fsync_flags = datasync ? 1 : 0;
164 req->in.h.opcode = FUSE_FSYNC;
165 req->in.h.nodeid = get_node_id(inode);
166 req->inode = inode;
167 req->file = file;
168 req->in.numargs = 1;
169 req->in.args[0].size = sizeof(inarg);
170 req->in.args[0].value = &inarg;
171 request_send(fc, req);
172 err = req->out.h.error;
173 fuse_put_request(fc, req);
174 if (err == -ENOSYS) {
175 fc->no_fsync = 1;
176 err = 0;
177 }
178 return err;
179}
180
181static ssize_t fuse_send_read(struct fuse_req *req, struct file *file,
182 struct inode *inode, loff_t pos, size_t count)
183{
184 struct fuse_conn *fc = get_fuse_conn(inode);
185 struct fuse_file *ff = file->private_data;
186 struct fuse_read_in inarg;
187
188 memset(&inarg, 0, sizeof(struct fuse_read_in));
189 inarg.fh = ff->fh;
190 inarg.offset = pos;
191 inarg.size = count;
192 req->in.h.opcode = FUSE_READ;
193 req->in.h.nodeid = get_node_id(inode);
194 req->inode = inode;
195 req->file = file;
196 req->in.numargs = 1;
197 req->in.args[0].size = sizeof(struct fuse_read_in);
198 req->in.args[0].value = &inarg;
199 req->out.argpages = 1;
200 req->out.argvar = 1;
201 req->out.numargs = 1;
202 req->out.args[0].size = count;
203 request_send_nonint(fc, req);
204 return req->out.args[0].size;
205}
206
207static int fuse_readpage(struct file *file, struct page *page)
208{
209 struct inode *inode = page->mapping->host;
210 struct fuse_conn *fc = get_fuse_conn(inode);
211 loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT;
212 struct fuse_req *req = fuse_get_request_nonint(fc);
213 int err = -EINTR;
214 if (!req)
215 goto out;
216
217 req->out.page_zeroing = 1;
218 req->num_pages = 1;
219 req->pages[0] = page;
220 fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE);
221 err = req->out.h.error;
222 fuse_put_request(fc, req);
223 if (!err)
224 SetPageUptodate(page);
225 out:
226 unlock_page(page);
227 return err;
228}
229
230static ssize_t fuse_send_write(struct fuse_req *req, struct file *file,
231 struct inode *inode, loff_t pos, size_t count)
232{
233 struct fuse_conn *fc = get_fuse_conn(inode);
234 struct fuse_file *ff = file->private_data;
235 struct fuse_write_in inarg;
236 struct fuse_write_out outarg;
237
238 memset(&inarg, 0, sizeof(struct fuse_write_in));
239 inarg.fh = ff->fh;
240 inarg.offset = pos;
241 inarg.size = count;
242 req->in.h.opcode = FUSE_WRITE;
243 req->in.h.nodeid = get_node_id(inode);
244 req->inode = inode;
245 req->file = file;
246 req->in.argpages = 1;
247 req->in.numargs = 2;
248 req->in.args[0].size = sizeof(struct fuse_write_in);
249 req->in.args[0].value = &inarg;
250 req->in.args[1].size = count;
251 req->out.numargs = 1;
252 req->out.args[0].size = sizeof(struct fuse_write_out);
253 req->out.args[0].value = &outarg;
254 request_send_nonint(fc, req);
255 return outarg.size;
256}
257
258static int fuse_prepare_write(struct file *file, struct page *page,
259 unsigned offset, unsigned to)
260{
261 /* No op */
262 return 0;
263}
264
265static int fuse_commit_write(struct file *file, struct page *page,
266 unsigned offset, unsigned to)
267{
268 int err;
269 ssize_t nres;
270 unsigned count = to - offset;
271 struct inode *inode = page->mapping->host;
272 struct fuse_conn *fc = get_fuse_conn(inode);
273 loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset;
274 struct fuse_req *req = fuse_get_request_nonint(fc);
275 if (!req)
276 return -EINTR;
277
278 req->num_pages = 1;
279 req->pages[0] = page;
280 req->page_offset = offset;
281 nres = fuse_send_write(req, file, inode, pos, count);
282 err = req->out.h.error;
283 fuse_put_request(fc, req);
284 if (!err && nres != count)
285 err = -EIO;
286 if (!err) {
287 pos += count;
288 if (pos > i_size_read(inode))
289 i_size_write(inode, pos);
290
291 if (offset == 0 && to == PAGE_CACHE_SIZE) {
292 clear_page_dirty(page);
293 SetPageUptodate(page);
294 }
295 } else if (err == -EINTR || err == -EIO)
296 fuse_invalidate_attr(inode);
297 return err;
298}
299
300static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
301{
302 if ((vma->vm_flags & VM_SHARED)) {
303 if ((vma->vm_flags & VM_WRITE))
304 return -ENODEV;
305 else
306 vma->vm_flags &= ~VM_MAYWRITE;
307 }
308 return generic_file_mmap(file, vma);
309}
310
311static int fuse_set_page_dirty(struct page *page)
312{
313 printk("fuse_set_page_dirty: should not happen\n");
314 dump_stack();
315 return 0;
316}
317
318static struct file_operations fuse_file_operations = {
319 .llseek = generic_file_llseek,
320 .read = generic_file_read,
321 .write = generic_file_write,
322 .mmap = fuse_file_mmap,
323 .open = fuse_open,
324 .flush = fuse_flush,
325 .release = fuse_release,
326 .fsync = fuse_fsync,
327 .sendfile = generic_file_sendfile,
328};
329
330static struct address_space_operations fuse_file_aops = {
331 .readpage = fuse_readpage,
332 .prepare_write = fuse_prepare_write,
333 .commit_write = fuse_commit_write,
334 .set_page_dirty = fuse_set_page_dirty,
335};
336
337void fuse_init_file_inode(struct inode *inode)
338{
339 inode->i_fop = &fuse_file_operations;
340 inode->i_data.a_ops = &fuse_file_aops;
341}