aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/fuse/Makefile2
-rw-r--r--fs/fuse/dir.c1
-rw-r--r--fs/fuse/file.c341
-rw-r--r--fs/fuse/fuse_i.h21
-rw-r--r--fs/fuse/inode.c2
5 files changed, 366 insertions, 1 deletions
diff --git a/fs/fuse/Makefile b/fs/fuse/Makefile
index c34e268a0ed3..c3e1f760cac9 100644
--- a/fs/fuse/Makefile
+++ b/fs/fuse/Makefile
@@ -4,4 +4,4 @@
4 4
5obj-$(CONFIG_FUSE_FS) += fuse.o 5obj-$(CONFIG_FUSE_FS) += fuse.o
6 6
7fuse-objs := dev.o dir.o inode.o 7fuse-objs := dev.o dir.o file.o inode.o
diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c
index 92c7188ccd16..8adc1eed164b 100644
--- a/fs/fuse/dir.c
+++ b/fs/fuse/dir.c
@@ -731,6 +731,7 @@ static struct inode_operations fuse_dir_inode_operations = {
731}; 731};
732 732
733static struct file_operations fuse_dir_operations = { 733static struct file_operations fuse_dir_operations = {
734 .llseek = generic_file_llseek,
734 .read = generic_read_dir, 735 .read = generic_read_dir,
735 .readdir = fuse_readdir, 736 .readdir = fuse_readdir,
736 .open = fuse_dir_open, 737 .open = fuse_dir_open,
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}
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h
index 87d25b8f2dc1..b4aa8f7bc2c1 100644
--- a/fs/fuse/fuse_i.h
+++ b/fs/fuse/fuse_i.h
@@ -40,6 +40,15 @@ struct fuse_inode {
40 unsigned long i_time; 40 unsigned long i_time;
41}; 41};
42 42
43/** FUSE specific file data */
44struct fuse_file {
45 /** Request reserved for flush and release */
46 struct fuse_req *release_req;
47
48 /** File handle used by userspace */
49 u64 fh;
50};
51
43/** One input argument of a request */ 52/** One input argument of a request */
44struct fuse_in_arg { 53struct fuse_in_arg {
45 unsigned size; 54 unsigned size;
@@ -136,6 +145,7 @@ struct fuse_req {
136 /** Data for asynchronous requests */ 145 /** Data for asynchronous requests */
137 union { 146 union {
138 struct fuse_forget_in forget_in; 147 struct fuse_forget_in forget_in;
148 struct fuse_release_in release_in;
139 struct fuse_init_in_out init_in_out; 149 struct fuse_init_in_out init_in_out;
140 } misc; 150 } misc;
141 151
@@ -200,6 +210,12 @@ struct fuse_conn {
200 /** Connection failed (version mismatch) */ 210 /** Connection failed (version mismatch) */
201 unsigned conn_error : 1; 211 unsigned conn_error : 1;
202 212
213 /** Is fsync not implemented by fs? */
214 unsigned no_fsync : 1;
215
216 /** Is flush not implemented by fs? */
217 unsigned no_flush : 1;
218
203 /** Backing dev info */ 219 /** Backing dev info */
204 struct backing_dev_info bdi; 220 struct backing_dev_info bdi;
205}; 221};
@@ -264,6 +280,11 @@ void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
264 unsigned long nodeid, u64 nlookup); 280 unsigned long nodeid, u64 nlookup);
265 281
266/** 282/**
283 * Initialise file operations on a regular file
284 */
285void fuse_init_file_inode(struct inode *inode);
286
287/**
267 * Initialise inode operations on regular files and special files 288 * Initialise inode operations on regular files and special files
268 */ 289 */
269void fuse_init_common(struct inode *inode); 290void fuse_init_common(struct inode *inode);
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index fa03f80806e5..f229d6962643 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -124,6 +124,7 @@ static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
124 i_size_write(inode, attr->size); 124 i_size_write(inode, attr->size);
125 if (S_ISREG(inode->i_mode)) { 125 if (S_ISREG(inode->i_mode)) {
126 fuse_init_common(inode); 126 fuse_init_common(inode);
127 fuse_init_file_inode(inode);
127 } else if (S_ISDIR(inode->i_mode)) 128 } else if (S_ISDIR(inode->i_mode))
128 fuse_init_dir(inode); 129 fuse_init_dir(inode);
129 else if (S_ISLNK(inode->i_mode)) 130 else if (S_ISLNK(inode->i_mode))
@@ -137,6 +138,7 @@ static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
137 /* Don't let user create weird files */ 138 /* Don't let user create weird files */
138 inode->i_mode = S_IFREG; 139 inode->i_mode = S_IFREG;
139 fuse_init_common(inode); 140 fuse_init_common(inode);
141 fuse_init_file_inode(inode);
140 } 142 }
141} 143}
142 144