]>
Commit | Line | Data |
---|---|---|
b6aeaded MS |
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 | ||
15 | static 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; | |
1e9a4ed9 | 73 | if (!err && !(fc->flags & FUSE_KERNEL_CACHE)) |
b6aeaded MS |
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 | ||
88 | static 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 | ||
110 | static 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 | ||
145 | static 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 | ||
181 | static 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 | ||
207 | static 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 | ||
db50b96c MS |
230 | static int fuse_send_readpages(struct fuse_req *req, struct file *file, |
231 | struct inode *inode) | |
232 | { | |
233 | loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT; | |
234 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
235 | unsigned i; | |
236 | req->out.page_zeroing = 1; | |
237 | fuse_send_read(req, file, inode, pos, count); | |
238 | for (i = 0; i < req->num_pages; i++) { | |
239 | struct page *page = req->pages[i]; | |
240 | if (!req->out.h.error) | |
241 | SetPageUptodate(page); | |
242 | unlock_page(page); | |
243 | } | |
244 | return req->out.h.error; | |
245 | } | |
246 | ||
247 | struct fuse_readpages_data { | |
248 | struct fuse_req *req; | |
249 | struct file *file; | |
250 | struct inode *inode; | |
251 | }; | |
252 | ||
253 | static int fuse_readpages_fill(void *_data, struct page *page) | |
254 | { | |
255 | struct fuse_readpages_data *data = _data; | |
256 | struct fuse_req *req = data->req; | |
257 | struct inode *inode = data->inode; | |
258 | struct fuse_conn *fc = get_fuse_conn(inode); | |
259 | ||
260 | if (req->num_pages && | |
261 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
262 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
263 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
264 | int err = fuse_send_readpages(req, data->file, inode); | |
265 | if (err) { | |
266 | unlock_page(page); | |
267 | return err; | |
268 | } | |
269 | fuse_reset_request(req); | |
270 | } | |
271 | req->pages[req->num_pages] = page; | |
272 | req->num_pages ++; | |
273 | return 0; | |
274 | } | |
275 | ||
276 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
277 | struct list_head *pages, unsigned nr_pages) | |
278 | { | |
279 | struct inode *inode = mapping->host; | |
280 | struct fuse_conn *fc = get_fuse_conn(inode); | |
281 | struct fuse_readpages_data data; | |
282 | int err; | |
283 | data.file = file; | |
284 | data.inode = inode; | |
285 | data.req = fuse_get_request_nonint(fc); | |
286 | if (!data.req) | |
287 | return -EINTR; | |
288 | ||
289 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
290 | if (!err && data.req->num_pages) | |
291 | err = fuse_send_readpages(data.req, file, inode); | |
292 | fuse_put_request(fc, data.req); | |
293 | return err; | |
294 | } | |
295 | ||
b6aeaded MS |
296 | static ssize_t fuse_send_write(struct fuse_req *req, struct file *file, |
297 | struct inode *inode, loff_t pos, size_t count) | |
298 | { | |
299 | struct fuse_conn *fc = get_fuse_conn(inode); | |
300 | struct fuse_file *ff = file->private_data; | |
301 | struct fuse_write_in inarg; | |
302 | struct fuse_write_out outarg; | |
303 | ||
304 | memset(&inarg, 0, sizeof(struct fuse_write_in)); | |
305 | inarg.fh = ff->fh; | |
306 | inarg.offset = pos; | |
307 | inarg.size = count; | |
308 | req->in.h.opcode = FUSE_WRITE; | |
309 | req->in.h.nodeid = get_node_id(inode); | |
310 | req->inode = inode; | |
311 | req->file = file; | |
312 | req->in.argpages = 1; | |
313 | req->in.numargs = 2; | |
314 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
315 | req->in.args[0].value = &inarg; | |
316 | req->in.args[1].size = count; | |
317 | req->out.numargs = 1; | |
318 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
319 | req->out.args[0].value = &outarg; | |
320 | request_send_nonint(fc, req); | |
321 | return outarg.size; | |
322 | } | |
323 | ||
324 | static int fuse_prepare_write(struct file *file, struct page *page, | |
325 | unsigned offset, unsigned to) | |
326 | { | |
327 | /* No op */ | |
328 | return 0; | |
329 | } | |
330 | ||
331 | static int fuse_commit_write(struct file *file, struct page *page, | |
332 | unsigned offset, unsigned to) | |
333 | { | |
334 | int err; | |
335 | ssize_t nres; | |
336 | unsigned count = to - offset; | |
337 | struct inode *inode = page->mapping->host; | |
338 | struct fuse_conn *fc = get_fuse_conn(inode); | |
339 | loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset; | |
340 | struct fuse_req *req = fuse_get_request_nonint(fc); | |
341 | if (!req) | |
342 | return -EINTR; | |
343 | ||
344 | req->num_pages = 1; | |
345 | req->pages[0] = page; | |
346 | req->page_offset = offset; | |
347 | nres = fuse_send_write(req, file, inode, pos, count); | |
348 | err = req->out.h.error; | |
349 | fuse_put_request(fc, req); | |
350 | if (!err && nres != count) | |
351 | err = -EIO; | |
352 | if (!err) { | |
353 | pos += count; | |
354 | if (pos > i_size_read(inode)) | |
355 | i_size_write(inode, pos); | |
356 | ||
357 | if (offset == 0 && to == PAGE_CACHE_SIZE) { | |
358 | clear_page_dirty(page); | |
359 | SetPageUptodate(page); | |
360 | } | |
361 | } else if (err == -EINTR || err == -EIO) | |
362 | fuse_invalidate_attr(inode); | |
363 | return err; | |
364 | } | |
365 | ||
413ef8cb MS |
366 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
367 | { | |
368 | unsigned i; | |
369 | ||
370 | for (i = 0; i < req->num_pages; i++) { | |
371 | struct page *page = req->pages[i]; | |
372 | if (write) | |
373 | set_page_dirty_lock(page); | |
374 | put_page(page); | |
375 | } | |
376 | } | |
377 | ||
378 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | |
379 | unsigned nbytes, int write) | |
380 | { | |
381 | unsigned long user_addr = (unsigned long) buf; | |
382 | unsigned offset = user_addr & ~PAGE_MASK; | |
383 | int npages; | |
384 | ||
385 | /* This doesn't work with nfsd */ | |
386 | if (!current->mm) | |
387 | return -EPERM; | |
388 | ||
389 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | |
390 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
391 | npages = min(npages, FUSE_MAX_PAGES_PER_REQ); | |
392 | down_read(¤t->mm->mmap_sem); | |
393 | npages = get_user_pages(current, current->mm, user_addr, npages, write, | |
394 | 0, req->pages, NULL); | |
395 | up_read(¤t->mm->mmap_sem); | |
396 | if (npages < 0) | |
397 | return npages; | |
398 | ||
399 | req->num_pages = npages; | |
400 | req->page_offset = offset; | |
401 | return 0; | |
402 | } | |
403 | ||
404 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |
405 | size_t count, loff_t *ppos, int write) | |
406 | { | |
407 | struct inode *inode = file->f_dentry->d_inode; | |
408 | struct fuse_conn *fc = get_fuse_conn(inode); | |
409 | size_t nmax = write ? fc->max_write : fc->max_read; | |
410 | loff_t pos = *ppos; | |
411 | ssize_t res = 0; | |
412 | struct fuse_req *req = fuse_get_request(fc); | |
413 | if (!req) | |
414 | return -ERESTARTSYS; | |
415 | ||
416 | while (count) { | |
417 | size_t tmp; | |
418 | size_t nres; | |
419 | size_t nbytes = min(count, nmax); | |
420 | int err = fuse_get_user_pages(req, buf, nbytes, !write); | |
421 | if (err) { | |
422 | res = err; | |
423 | break; | |
424 | } | |
425 | tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset; | |
426 | nbytes = min(nbytes, tmp); | |
427 | if (write) | |
428 | nres = fuse_send_write(req, file, inode, pos, nbytes); | |
429 | else | |
430 | nres = fuse_send_read(req, file, inode, pos, nbytes); | |
431 | fuse_release_user_pages(req, !write); | |
432 | if (req->out.h.error) { | |
433 | if (!res) | |
434 | res = req->out.h.error; | |
435 | break; | |
436 | } else if (nres > nbytes) { | |
437 | res = -EIO; | |
438 | break; | |
439 | } | |
440 | count -= nres; | |
441 | res += nres; | |
442 | pos += nres; | |
443 | buf += nres; | |
444 | if (nres != nbytes) | |
445 | break; | |
446 | if (count) | |
447 | fuse_reset_request(req); | |
448 | } | |
449 | fuse_put_request(fc, req); | |
450 | if (res > 0) { | |
451 | if (write && pos > i_size_read(inode)) | |
452 | i_size_write(inode, pos); | |
453 | *ppos = pos; | |
454 | } else if (write && (res == -EINTR || res == -EIO)) | |
455 | fuse_invalidate_attr(inode); | |
456 | ||
457 | return res; | |
458 | } | |
459 | ||
460 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, | |
461 | size_t count, loff_t *ppos) | |
462 | { | |
463 | return fuse_direct_io(file, buf, count, ppos, 0); | |
464 | } | |
465 | ||
466 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | |
467 | size_t count, loff_t *ppos) | |
468 | { | |
469 | struct inode *inode = file->f_dentry->d_inode; | |
470 | ssize_t res; | |
471 | /* Don't allow parallel writes to the same file */ | |
472 | down(&inode->i_sem); | |
473 | res = fuse_direct_io(file, buf, count, ppos, 1); | |
474 | up(&inode->i_sem); | |
475 | return res; | |
476 | } | |
477 | ||
b6aeaded MS |
478 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
479 | { | |
480 | if ((vma->vm_flags & VM_SHARED)) { | |
481 | if ((vma->vm_flags & VM_WRITE)) | |
482 | return -ENODEV; | |
483 | else | |
484 | vma->vm_flags &= ~VM_MAYWRITE; | |
485 | } | |
486 | return generic_file_mmap(file, vma); | |
487 | } | |
488 | ||
489 | static int fuse_set_page_dirty(struct page *page) | |
490 | { | |
491 | printk("fuse_set_page_dirty: should not happen\n"); | |
492 | dump_stack(); | |
493 | return 0; | |
494 | } | |
495 | ||
496 | static struct file_operations fuse_file_operations = { | |
497 | .llseek = generic_file_llseek, | |
498 | .read = generic_file_read, | |
499 | .write = generic_file_write, | |
500 | .mmap = fuse_file_mmap, | |
501 | .open = fuse_open, | |
502 | .flush = fuse_flush, | |
503 | .release = fuse_release, | |
504 | .fsync = fuse_fsync, | |
505 | .sendfile = generic_file_sendfile, | |
506 | }; | |
507 | ||
413ef8cb MS |
508 | static struct file_operations fuse_direct_io_file_operations = { |
509 | .llseek = generic_file_llseek, | |
510 | .read = fuse_direct_read, | |
511 | .write = fuse_direct_write, | |
512 | .open = fuse_open, | |
513 | .flush = fuse_flush, | |
514 | .release = fuse_release, | |
515 | .fsync = fuse_fsync, | |
516 | /* no mmap and sendfile */ | |
517 | }; | |
518 | ||
b6aeaded MS |
519 | static struct address_space_operations fuse_file_aops = { |
520 | .readpage = fuse_readpage, | |
521 | .prepare_write = fuse_prepare_write, | |
522 | .commit_write = fuse_commit_write, | |
db50b96c | 523 | .readpages = fuse_readpages, |
b6aeaded MS |
524 | .set_page_dirty = fuse_set_page_dirty, |
525 | }; | |
526 | ||
527 | void fuse_init_file_inode(struct inode *inode) | |
528 | { | |
413ef8cb MS |
529 | struct fuse_conn *fc = get_fuse_conn(inode); |
530 | ||
531 | if (fc->flags & FUSE_DIRECT_IO) | |
532 | inode->i_fop = &fuse_direct_io_file_operations; | |
533 | else { | |
534 | inode->i_fop = &fuse_file_operations; | |
535 | inode->i_data.a_ops = &fuse_file_aops; | |
536 | } | |
b6aeaded | 537 | } |