]>
Commit | Line | Data |
---|---|---|
b6aeaded MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
56cf34ff | 3 | Copyright (C) 2001-2006 Miklos Szeredi <[email protected]> |
b6aeaded MS |
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> | |
e8edc6e0 | 14 | #include <linux/sched.h> |
b6aeaded | 15 | |
4b6f5d20 | 16 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 17 | |
fd72faac MS |
18 | static int fuse_send_open(struct inode *inode, struct file *file, int isdir, |
19 | struct fuse_open_out *outargp) | |
b6aeaded MS |
20 | { |
21 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b6aeaded | 22 | struct fuse_open_in inarg; |
fd72faac MS |
23 | struct fuse_req *req; |
24 | int err; | |
25 | ||
ce1d5a49 MS |
26 | req = fuse_get_req(fc); |
27 | if (IS_ERR(req)) | |
28 | return PTR_ERR(req); | |
fd72faac MS |
29 | |
30 | memset(&inarg, 0, sizeof(inarg)); | |
31 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); | |
32 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; | |
33 | req->in.h.nodeid = get_node_id(inode); | |
fd72faac MS |
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); | |
43 | ||
44 | return err; | |
45 | } | |
46 | ||
47 | struct fuse_file *fuse_file_alloc(void) | |
48 | { | |
49 | struct fuse_file *ff; | |
50 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); | |
51 | if (ff) { | |
33649c91 MS |
52 | ff->reserved_req = fuse_request_alloc(); |
53 | if (!ff->reserved_req) { | |
fd72faac MS |
54 | kfree(ff); |
55 | ff = NULL; | |
56 | } | |
57 | } | |
58 | return ff; | |
59 | } | |
60 | ||
61 | void fuse_file_free(struct fuse_file *ff) | |
62 | { | |
33649c91 | 63 | fuse_request_free(ff->reserved_req); |
fd72faac MS |
64 | kfree(ff); |
65 | } | |
66 | ||
67 | void fuse_finish_open(struct inode *inode, struct file *file, | |
68 | struct fuse_file *ff, struct fuse_open_out *outarg) | |
69 | { | |
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)) | |
fc0ecff6 | 73 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
fd72faac MS |
74 | ff->fh = outarg->fh; |
75 | file->private_data = ff; | |
76 | } | |
77 | ||
78 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) | |
79 | { | |
b6aeaded MS |
80 | struct fuse_open_out outarg; |
81 | struct fuse_file *ff; | |
82 | int err; | |
b6aeaded | 83 | |
dd190d06 MS |
84 | /* VFS checks this, but only _after_ ->open() */ |
85 | if (file->f_flags & O_DIRECT) | |
86 | return -EINVAL; | |
87 | ||
b6aeaded MS |
88 | err = generic_file_open(inode, file); |
89 | if (err) | |
90 | return err; | |
91 | ||
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) { | |
fd72faac | 95 | err = fuse_do_getattr(inode); |
b6aeaded MS |
96 | if (err) |
97 | return err; | |
98 | } | |
99 | ||
fd72faac | 100 | ff = fuse_file_alloc(); |
b6aeaded | 101 | if (!ff) |
fd72faac | 102 | return -ENOMEM; |
b6aeaded | 103 | |
fd72faac MS |
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); | |
b6aeaded MS |
111 | } |
112 | ||
b6aeaded MS |
113 | return err; |
114 | } | |
115 | ||
51eb01e7 MS |
116 | struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, |
117 | int opcode) | |
64c6d8ed | 118 | { |
33649c91 | 119 | struct fuse_req *req = ff->reserved_req; |
b6aeaded MS |
120 | struct fuse_release_in *inarg = &req->misc.release_in; |
121 | ||
122 | inarg->fh = ff->fh; | |
fd72faac | 123 | inarg->flags = flags; |
51eb01e7 | 124 | req->in.h.opcode = opcode; |
fd72faac | 125 | req->in.h.nodeid = nodeid; |
b6aeaded MS |
126 | req->in.numargs = 1; |
127 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
128 | req->in.args[0].value = inarg; | |
b6aeaded | 129 | kfree(ff); |
51eb01e7 MS |
130 | |
131 | return req; | |
fd72faac MS |
132 | } |
133 | ||
134 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) | |
135 | { | |
136 | struct fuse_file *ff = file->private_data; | |
137 | if (ff) { | |
138 | struct fuse_conn *fc = get_fuse_conn(inode); | |
51eb01e7 MS |
139 | struct fuse_req *req; |
140 | ||
141 | req = fuse_release_fill(ff, get_node_id(inode), file->f_flags, | |
142 | isdir ? FUSE_RELEASEDIR : FUSE_RELEASE); | |
143 | ||
144 | /* Hold vfsmount and dentry until release is finished */ | |
7706a9d6 JS |
145 | req->vfsmount = mntget(file->f_path.mnt); |
146 | req->dentry = dget(file->f_path.dentry); | |
51eb01e7 | 147 | request_send_background(fc, req); |
fd72faac | 148 | } |
b6aeaded MS |
149 | |
150 | /* Return value is ignored by VFS */ | |
151 | return 0; | |
152 | } | |
153 | ||
04730fef MS |
154 | static int fuse_open(struct inode *inode, struct file *file) |
155 | { | |
156 | return fuse_open_common(inode, file, 0); | |
157 | } | |
158 | ||
159 | static int fuse_release(struct inode *inode, struct file *file) | |
160 | { | |
161 | return fuse_release_common(inode, file, 0); | |
162 | } | |
163 | ||
71421259 | 164 | /* |
9c8ef561 MS |
165 | * Scramble the ID space with XTEA, so that the value of the files_struct |
166 | * pointer is not exposed to userspace. | |
71421259 | 167 | */ |
9c8ef561 | 168 | static u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 169 | { |
9c8ef561 MS |
170 | u32 *k = fc->scramble_key; |
171 | u64 v = (unsigned long) id; | |
172 | u32 v0 = v; | |
173 | u32 v1 = v >> 32; | |
174 | u32 sum = 0; | |
175 | int i; | |
176 | ||
177 | for (i = 0; i < 32; i++) { | |
178 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
179 | sum += 0x9E3779B9; | |
180 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
181 | } | |
182 | ||
183 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
184 | } |
185 | ||
75e1fcc0 | 186 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 187 | { |
7706a9d6 | 188 | struct inode *inode = file->f_path.dentry->d_inode; |
b6aeaded MS |
189 | struct fuse_conn *fc = get_fuse_conn(inode); |
190 | struct fuse_file *ff = file->private_data; | |
191 | struct fuse_req *req; | |
192 | struct fuse_flush_in inarg; | |
193 | int err; | |
194 | ||
248d86e8 MS |
195 | if (is_bad_inode(inode)) |
196 | return -EIO; | |
197 | ||
b6aeaded MS |
198 | if (fc->no_flush) |
199 | return 0; | |
200 | ||
33649c91 | 201 | req = fuse_get_req_nofail(fc, file); |
b6aeaded MS |
202 | memset(&inarg, 0, sizeof(inarg)); |
203 | inarg.fh = ff->fh; | |
9c8ef561 | 204 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
b6aeaded MS |
205 | req->in.h.opcode = FUSE_FLUSH; |
206 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
207 | req->in.numargs = 1; |
208 | req->in.args[0].size = sizeof(inarg); | |
209 | req->in.args[0].value = &inarg; | |
71421259 | 210 | req->force = 1; |
7c352bdf | 211 | request_send(fc, req); |
b6aeaded MS |
212 | err = req->out.h.error; |
213 | fuse_put_request(fc, req); | |
214 | if (err == -ENOSYS) { | |
215 | fc->no_flush = 1; | |
216 | err = 0; | |
217 | } | |
218 | return err; | |
219 | } | |
220 | ||
82547981 MS |
221 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
222 | int isdir) | |
b6aeaded MS |
223 | { |
224 | struct inode *inode = de->d_inode; | |
225 | struct fuse_conn *fc = get_fuse_conn(inode); | |
226 | struct fuse_file *ff = file->private_data; | |
227 | struct fuse_req *req; | |
228 | struct fuse_fsync_in inarg; | |
229 | int err; | |
230 | ||
248d86e8 MS |
231 | if (is_bad_inode(inode)) |
232 | return -EIO; | |
233 | ||
82547981 | 234 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
235 | return 0; |
236 | ||
ce1d5a49 MS |
237 | req = fuse_get_req(fc); |
238 | if (IS_ERR(req)) | |
239 | return PTR_ERR(req); | |
b6aeaded MS |
240 | |
241 | memset(&inarg, 0, sizeof(inarg)); | |
242 | inarg.fh = ff->fh; | |
243 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 244 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 245 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
246 | req->in.numargs = 1; |
247 | req->in.args[0].size = sizeof(inarg); | |
248 | req->in.args[0].value = &inarg; | |
249 | request_send(fc, req); | |
250 | err = req->out.h.error; | |
251 | fuse_put_request(fc, req); | |
252 | if (err == -ENOSYS) { | |
82547981 MS |
253 | if (isdir) |
254 | fc->no_fsyncdir = 1; | |
255 | else | |
256 | fc->no_fsync = 1; | |
b6aeaded MS |
257 | err = 0; |
258 | } | |
259 | return err; | |
260 | } | |
261 | ||
82547981 MS |
262 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
263 | { | |
264 | return fuse_fsync_common(file, de, datasync, 0); | |
265 | } | |
266 | ||
361b1eb5 MS |
267 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
268 | struct inode *inode, loff_t pos, size_t count, int opcode) | |
b6aeaded | 269 | { |
b6aeaded | 270 | struct fuse_file *ff = file->private_data; |
361b1eb5 | 271 | struct fuse_read_in *inarg = &req->misc.read_in; |
b6aeaded | 272 | |
361b1eb5 MS |
273 | inarg->fh = ff->fh; |
274 | inarg->offset = pos; | |
275 | inarg->size = count; | |
276 | req->in.h.opcode = opcode; | |
b6aeaded | 277 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
278 | req->in.numargs = 1; |
279 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 280 | req->in.args[0].value = inarg; |
b6aeaded MS |
281 | req->out.argpages = 1; |
282 | req->out.argvar = 1; | |
283 | req->out.numargs = 1; | |
284 | req->out.args[0].size = count; | |
b6aeaded MS |
285 | } |
286 | ||
8bfc016d MS |
287 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
288 | struct inode *inode, loff_t pos, size_t count) | |
04730fef | 289 | { |
361b1eb5 MS |
290 | struct fuse_conn *fc = get_fuse_conn(inode); |
291 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); | |
292 | request_send(fc, req); | |
293 | return req->out.args[0].size; | |
04730fef MS |
294 | } |
295 | ||
b6aeaded MS |
296 | static int fuse_readpage(struct file *file, struct page *page) |
297 | { | |
298 | struct inode *inode = page->mapping->host; | |
299 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 MS |
300 | struct fuse_req *req; |
301 | int err; | |
302 | ||
303 | err = -EIO; | |
304 | if (is_bad_inode(inode)) | |
305 | goto out; | |
306 | ||
ce1d5a49 MS |
307 | req = fuse_get_req(fc); |
308 | err = PTR_ERR(req); | |
309 | if (IS_ERR(req)) | |
b6aeaded MS |
310 | goto out; |
311 | ||
312 | req->out.page_zeroing = 1; | |
313 | req->num_pages = 1; | |
314 | req->pages[0] = page; | |
4633a22e | 315 | fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE); |
b6aeaded MS |
316 | err = req->out.h.error; |
317 | fuse_put_request(fc, req); | |
318 | if (!err) | |
319 | SetPageUptodate(page); | |
b36c31ba | 320 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
321 | out: |
322 | unlock_page(page); | |
323 | return err; | |
324 | } | |
325 | ||
c1aa96a5 | 326 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 327 | { |
c1aa96a5 MS |
328 | int i; |
329 | ||
330 | fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */ | |
331 | ||
db50b96c MS |
332 | for (i = 0; i < req->num_pages; i++) { |
333 | struct page *page = req->pages[i]; | |
334 | if (!req->out.h.error) | |
335 | SetPageUptodate(page); | |
c1aa96a5 MS |
336 | else |
337 | SetPageError(page); | |
db50b96c MS |
338 | unlock_page(page); |
339 | } | |
c1aa96a5 MS |
340 | fuse_put_request(fc, req); |
341 | } | |
342 | ||
343 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, | |
344 | struct inode *inode) | |
345 | { | |
346 | struct fuse_conn *fc = get_fuse_conn(inode); | |
347 | loff_t pos = page_offset(req->pages[0]); | |
348 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
349 | req->out.page_zeroing = 1; | |
c1aa96a5 | 350 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
9cd68455 | 351 | if (fc->async_read) { |
51eb01e7 MS |
352 | get_file(file); |
353 | req->file = file; | |
9cd68455 MS |
354 | req->end = fuse_readpages_end; |
355 | request_send_background(fc, req); | |
356 | } else { | |
357 | request_send(fc, req); | |
358 | fuse_readpages_end(fc, req); | |
359 | } | |
db50b96c MS |
360 | } |
361 | ||
362 | struct fuse_readpages_data { | |
363 | struct fuse_req *req; | |
364 | struct file *file; | |
365 | struct inode *inode; | |
366 | }; | |
367 | ||
368 | static int fuse_readpages_fill(void *_data, struct page *page) | |
369 | { | |
370 | struct fuse_readpages_data *data = _data; | |
371 | struct fuse_req *req = data->req; | |
372 | struct inode *inode = data->inode; | |
373 | struct fuse_conn *fc = get_fuse_conn(inode); | |
374 | ||
375 | if (req->num_pages && | |
376 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
377 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
378 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
c1aa96a5 | 379 | fuse_send_readpages(req, data->file, inode); |
ce1d5a49 MS |
380 | data->req = req = fuse_get_req(fc); |
381 | if (IS_ERR(req)) { | |
db50b96c | 382 | unlock_page(page); |
ce1d5a49 | 383 | return PTR_ERR(req); |
db50b96c | 384 | } |
db50b96c MS |
385 | } |
386 | req->pages[req->num_pages] = page; | |
387 | req->num_pages ++; | |
388 | return 0; | |
389 | } | |
390 | ||
391 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
392 | struct list_head *pages, unsigned nr_pages) | |
393 | { | |
394 | struct inode *inode = mapping->host; | |
395 | struct fuse_conn *fc = get_fuse_conn(inode); | |
396 | struct fuse_readpages_data data; | |
397 | int err; | |
248d86e8 | 398 | |
1d7ea732 | 399 | err = -EIO; |
248d86e8 | 400 | if (is_bad_inode(inode)) |
2e990021 | 401 | goto out; |
248d86e8 | 402 | |
db50b96c MS |
403 | data.file = file; |
404 | data.inode = inode; | |
ce1d5a49 | 405 | data.req = fuse_get_req(fc); |
1d7ea732 | 406 | err = PTR_ERR(data.req); |
ce1d5a49 | 407 | if (IS_ERR(data.req)) |
2e990021 | 408 | goto out; |
db50b96c MS |
409 | |
410 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
411 | if (!err) { |
412 | if (data.req->num_pages) | |
413 | fuse_send_readpages(data.req, file, inode); | |
414 | else | |
415 | fuse_put_request(fc, data.req); | |
416 | } | |
2e990021 | 417 | out: |
1d7ea732 | 418 | return err; |
db50b96c MS |
419 | } |
420 | ||
04730fef MS |
421 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, |
422 | struct inode *inode, loff_t pos, size_t count) | |
b6aeaded MS |
423 | { |
424 | struct fuse_conn *fc = get_fuse_conn(inode); | |
425 | struct fuse_file *ff = file->private_data; | |
426 | struct fuse_write_in inarg; | |
427 | struct fuse_write_out outarg; | |
428 | ||
429 | memset(&inarg, 0, sizeof(struct fuse_write_in)); | |
430 | inarg.fh = ff->fh; | |
431 | inarg.offset = pos; | |
432 | inarg.size = count; | |
433 | req->in.h.opcode = FUSE_WRITE; | |
434 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
435 | req->in.argpages = 1; |
436 | req->in.numargs = 2; | |
437 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
438 | req->in.args[0].value = &inarg; | |
439 | req->in.args[1].size = count; | |
440 | req->out.numargs = 1; | |
441 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
442 | req->out.args[0].value = &outarg; | |
7c352bdf | 443 | request_send(fc, req); |
b6aeaded MS |
444 | return outarg.size; |
445 | } | |
446 | ||
447 | static int fuse_prepare_write(struct file *file, struct page *page, | |
448 | unsigned offset, unsigned to) | |
449 | { | |
450 | /* No op */ | |
451 | return 0; | |
452 | } | |
453 | ||
454 | static int fuse_commit_write(struct file *file, struct page *page, | |
455 | unsigned offset, unsigned to) | |
456 | { | |
457 | int err; | |
04730fef | 458 | size_t nres; |
b6aeaded MS |
459 | unsigned count = to - offset; |
460 | struct inode *inode = page->mapping->host; | |
461 | struct fuse_conn *fc = get_fuse_conn(inode); | |
4633a22e | 462 | loff_t pos = page_offset(page) + offset; |
248d86e8 MS |
463 | struct fuse_req *req; |
464 | ||
465 | if (is_bad_inode(inode)) | |
466 | return -EIO; | |
467 | ||
ce1d5a49 MS |
468 | req = fuse_get_req(fc); |
469 | if (IS_ERR(req)) | |
470 | return PTR_ERR(req); | |
b6aeaded MS |
471 | |
472 | req->num_pages = 1; | |
473 | req->pages[0] = page; | |
474 | req->page_offset = offset; | |
475 | nres = fuse_send_write(req, file, inode, pos, count); | |
476 | err = req->out.h.error; | |
477 | fuse_put_request(fc, req); | |
478 | if (!err && nres != count) | |
479 | err = -EIO; | |
480 | if (!err) { | |
481 | pos += count; | |
9ffbb916 MS |
482 | spin_lock(&fc->lock); |
483 | if (pos > inode->i_size) | |
b6aeaded | 484 | i_size_write(inode, pos); |
9ffbb916 | 485 | spin_unlock(&fc->lock); |
b6aeaded | 486 | |
9280f682 | 487 | if (offset == 0 && to == PAGE_CACHE_SIZE) |
b6aeaded | 488 | SetPageUptodate(page); |
b36c31ba MS |
489 | } |
490 | fuse_invalidate_attr(inode); | |
b6aeaded MS |
491 | return err; |
492 | } | |
493 | ||
413ef8cb MS |
494 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
495 | { | |
496 | unsigned i; | |
497 | ||
498 | for (i = 0; i < req->num_pages; i++) { | |
499 | struct page *page = req->pages[i]; | |
500 | if (write) | |
501 | set_page_dirty_lock(page); | |
502 | put_page(page); | |
503 | } | |
504 | } | |
505 | ||
506 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | |
507 | unsigned nbytes, int write) | |
508 | { | |
509 | unsigned long user_addr = (unsigned long) buf; | |
510 | unsigned offset = user_addr & ~PAGE_MASK; | |
511 | int npages; | |
512 | ||
513 | /* This doesn't work with nfsd */ | |
514 | if (!current->mm) | |
515 | return -EPERM; | |
516 | ||
517 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | |
518 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
6ad84aca | 519 | npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ); |
413ef8cb MS |
520 | down_read(¤t->mm->mmap_sem); |
521 | npages = get_user_pages(current, current->mm, user_addr, npages, write, | |
522 | 0, req->pages, NULL); | |
523 | up_read(¤t->mm->mmap_sem); | |
524 | if (npages < 0) | |
525 | return npages; | |
526 | ||
527 | req->num_pages = npages; | |
528 | req->page_offset = offset; | |
529 | return 0; | |
530 | } | |
531 | ||
532 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |
533 | size_t count, loff_t *ppos, int write) | |
534 | { | |
7706a9d6 | 535 | struct inode *inode = file->f_path.dentry->d_inode; |
413ef8cb MS |
536 | struct fuse_conn *fc = get_fuse_conn(inode); |
537 | size_t nmax = write ? fc->max_write : fc->max_read; | |
538 | loff_t pos = *ppos; | |
539 | ssize_t res = 0; | |
248d86e8 MS |
540 | struct fuse_req *req; |
541 | ||
542 | if (is_bad_inode(inode)) | |
543 | return -EIO; | |
544 | ||
ce1d5a49 MS |
545 | req = fuse_get_req(fc); |
546 | if (IS_ERR(req)) | |
547 | return PTR_ERR(req); | |
413ef8cb MS |
548 | |
549 | while (count) { | |
413ef8cb MS |
550 | size_t nres; |
551 | size_t nbytes = min(count, nmax); | |
552 | int err = fuse_get_user_pages(req, buf, nbytes, !write); | |
553 | if (err) { | |
554 | res = err; | |
555 | break; | |
556 | } | |
6ad84aca MS |
557 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
558 | nbytes = min(count, nbytes); | |
413ef8cb MS |
559 | if (write) |
560 | nres = fuse_send_write(req, file, inode, pos, nbytes); | |
561 | else | |
562 | nres = fuse_send_read(req, file, inode, pos, nbytes); | |
563 | fuse_release_user_pages(req, !write); | |
564 | if (req->out.h.error) { | |
565 | if (!res) | |
566 | res = req->out.h.error; | |
567 | break; | |
568 | } else if (nres > nbytes) { | |
569 | res = -EIO; | |
570 | break; | |
571 | } | |
572 | count -= nres; | |
573 | res += nres; | |
574 | pos += nres; | |
575 | buf += nres; | |
576 | if (nres != nbytes) | |
577 | break; | |
56cf34ff MS |
578 | if (count) { |
579 | fuse_put_request(fc, req); | |
580 | req = fuse_get_req(fc); | |
581 | if (IS_ERR(req)) | |
582 | break; | |
583 | } | |
413ef8cb MS |
584 | } |
585 | fuse_put_request(fc, req); | |
586 | if (res > 0) { | |
9ffbb916 MS |
587 | if (write) { |
588 | spin_lock(&fc->lock); | |
589 | if (pos > inode->i_size) | |
590 | i_size_write(inode, pos); | |
591 | spin_unlock(&fc->lock); | |
592 | } | |
413ef8cb | 593 | *ppos = pos; |
b36c31ba MS |
594 | } |
595 | fuse_invalidate_attr(inode); | |
413ef8cb MS |
596 | |
597 | return res; | |
598 | } | |
599 | ||
600 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, | |
601 | size_t count, loff_t *ppos) | |
602 | { | |
603 | return fuse_direct_io(file, buf, count, ppos, 0); | |
604 | } | |
605 | ||
606 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | |
607 | size_t count, loff_t *ppos) | |
608 | { | |
7706a9d6 | 609 | struct inode *inode = file->f_path.dentry->d_inode; |
413ef8cb MS |
610 | ssize_t res; |
611 | /* Don't allow parallel writes to the same file */ | |
1b1dcc1b | 612 | mutex_lock(&inode->i_mutex); |
889f7848 MS |
613 | res = generic_write_checks(file, ppos, &count, 0); |
614 | if (!res) | |
615 | res = fuse_direct_io(file, buf, count, ppos, 1); | |
1b1dcc1b | 616 | mutex_unlock(&inode->i_mutex); |
413ef8cb MS |
617 | return res; |
618 | } | |
619 | ||
b6aeaded MS |
620 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) |
621 | { | |
622 | if ((vma->vm_flags & VM_SHARED)) { | |
623 | if ((vma->vm_flags & VM_WRITE)) | |
624 | return -ENODEV; | |
625 | else | |
626 | vma->vm_flags &= ~VM_MAYWRITE; | |
627 | } | |
628 | return generic_file_mmap(file, vma); | |
629 | } | |
630 | ||
631 | static int fuse_set_page_dirty(struct page *page) | |
632 | { | |
633 | printk("fuse_set_page_dirty: should not happen\n"); | |
634 | dump_stack(); | |
635 | return 0; | |
636 | } | |
637 | ||
71421259 MS |
638 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
639 | struct file_lock *fl) | |
640 | { | |
641 | switch (ffl->type) { | |
642 | case F_UNLCK: | |
643 | break; | |
644 | ||
645 | case F_RDLCK: | |
646 | case F_WRLCK: | |
647 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
648 | ffl->end < ffl->start) | |
649 | return -EIO; | |
650 | ||
651 | fl->fl_start = ffl->start; | |
652 | fl->fl_end = ffl->end; | |
653 | fl->fl_pid = ffl->pid; | |
654 | break; | |
655 | ||
656 | default: | |
657 | return -EIO; | |
658 | } | |
659 | fl->fl_type = ffl->type; | |
660 | return 0; | |
661 | } | |
662 | ||
663 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, | |
664 | const struct file_lock *fl, int opcode, pid_t pid) | |
665 | { | |
7706a9d6 | 666 | struct inode *inode = file->f_path.dentry->d_inode; |
9c8ef561 | 667 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 MS |
668 | struct fuse_file *ff = file->private_data; |
669 | struct fuse_lk_in *arg = &req->misc.lk_in; | |
670 | ||
671 | arg->fh = ff->fh; | |
9c8ef561 | 672 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
71421259 MS |
673 | arg->lk.start = fl->fl_start; |
674 | arg->lk.end = fl->fl_end; | |
675 | arg->lk.type = fl->fl_type; | |
676 | arg->lk.pid = pid; | |
677 | req->in.h.opcode = opcode; | |
678 | req->in.h.nodeid = get_node_id(inode); | |
679 | req->in.numargs = 1; | |
680 | req->in.args[0].size = sizeof(*arg); | |
681 | req->in.args[0].value = arg; | |
682 | } | |
683 | ||
684 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
685 | { | |
7706a9d6 | 686 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
687 | struct fuse_conn *fc = get_fuse_conn(inode); |
688 | struct fuse_req *req; | |
689 | struct fuse_lk_out outarg; | |
690 | int err; | |
691 | ||
692 | req = fuse_get_req(fc); | |
693 | if (IS_ERR(req)) | |
694 | return PTR_ERR(req); | |
695 | ||
696 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0); | |
697 | req->out.numargs = 1; | |
698 | req->out.args[0].size = sizeof(outarg); | |
699 | req->out.args[0].value = &outarg; | |
700 | request_send(fc, req); | |
701 | err = req->out.h.error; | |
702 | fuse_put_request(fc, req); | |
703 | if (!err) | |
704 | err = convert_fuse_file_lock(&outarg.lk, fl); | |
705 | ||
706 | return err; | |
707 | } | |
708 | ||
709 | static int fuse_setlk(struct file *file, struct file_lock *fl) | |
710 | { | |
7706a9d6 | 711 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
712 | struct fuse_conn *fc = get_fuse_conn(inode); |
713 | struct fuse_req *req; | |
714 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; | |
715 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; | |
716 | int err; | |
717 | ||
718 | /* Unlock on close is handled by the flush method */ | |
719 | if (fl->fl_flags & FL_CLOSE) | |
720 | return 0; | |
721 | ||
722 | req = fuse_get_req(fc); | |
723 | if (IS_ERR(req)) | |
724 | return PTR_ERR(req); | |
725 | ||
726 | fuse_lk_fill(req, file, fl, opcode, pid); | |
727 | request_send(fc, req); | |
728 | err = req->out.h.error; | |
a4d27e75 MS |
729 | /* locking is restartable */ |
730 | if (err == -EINTR) | |
731 | err = -ERESTARTSYS; | |
71421259 MS |
732 | fuse_put_request(fc, req); |
733 | return err; | |
734 | } | |
735 | ||
736 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
737 | { | |
7706a9d6 | 738 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
739 | struct fuse_conn *fc = get_fuse_conn(inode); |
740 | int err; | |
741 | ||
742 | if (cmd == F_GETLK) { | |
743 | if (fc->no_lock) { | |
9d6a8c5c | 744 | posix_test_lock(file, fl); |
71421259 MS |
745 | err = 0; |
746 | } else | |
747 | err = fuse_getlk(file, fl); | |
748 | } else { | |
749 | if (fc->no_lock) | |
750 | err = posix_lock_file_wait(file, fl); | |
751 | else | |
752 | err = fuse_setlk(file, fl); | |
753 | } | |
754 | return err; | |
755 | } | |
756 | ||
b2d2272f MS |
757 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
758 | { | |
759 | struct inode *inode = mapping->host; | |
760 | struct fuse_conn *fc = get_fuse_conn(inode); | |
761 | struct fuse_req *req; | |
762 | struct fuse_bmap_in inarg; | |
763 | struct fuse_bmap_out outarg; | |
764 | int err; | |
765 | ||
766 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
767 | return 0; | |
768 | ||
769 | req = fuse_get_req(fc); | |
770 | if (IS_ERR(req)) | |
771 | return 0; | |
772 | ||
773 | memset(&inarg, 0, sizeof(inarg)); | |
774 | inarg.block = block; | |
775 | inarg.blocksize = inode->i_sb->s_blocksize; | |
776 | req->in.h.opcode = FUSE_BMAP; | |
777 | req->in.h.nodeid = get_node_id(inode); | |
778 | req->in.numargs = 1; | |
779 | req->in.args[0].size = sizeof(inarg); | |
780 | req->in.args[0].value = &inarg; | |
781 | req->out.numargs = 1; | |
782 | req->out.args[0].size = sizeof(outarg); | |
783 | req->out.args[0].value = &outarg; | |
784 | request_send(fc, req); | |
785 | err = req->out.h.error; | |
786 | fuse_put_request(fc, req); | |
787 | if (err == -ENOSYS) | |
788 | fc->no_bmap = 1; | |
789 | ||
790 | return err ? 0 : outarg.block; | |
791 | } | |
792 | ||
4b6f5d20 | 793 | static const struct file_operations fuse_file_operations = { |
b6aeaded | 794 | .llseek = generic_file_llseek, |
543ade1f BP |
795 | .read = do_sync_read, |
796 | .aio_read = generic_file_aio_read, | |
797 | .write = do_sync_write, | |
798 | .aio_write = generic_file_aio_write, | |
b6aeaded MS |
799 | .mmap = fuse_file_mmap, |
800 | .open = fuse_open, | |
801 | .flush = fuse_flush, | |
802 | .release = fuse_release, | |
803 | .fsync = fuse_fsync, | |
71421259 | 804 | .lock = fuse_file_lock, |
5ffc4ef4 | 805 | .splice_read = generic_file_splice_read, |
b6aeaded MS |
806 | }; |
807 | ||
4b6f5d20 | 808 | static const struct file_operations fuse_direct_io_file_operations = { |
413ef8cb MS |
809 | .llseek = generic_file_llseek, |
810 | .read = fuse_direct_read, | |
811 | .write = fuse_direct_write, | |
812 | .open = fuse_open, | |
813 | .flush = fuse_flush, | |
814 | .release = fuse_release, | |
815 | .fsync = fuse_fsync, | |
71421259 | 816 | .lock = fuse_file_lock, |
5ffc4ef4 | 817 | /* no mmap and splice_read */ |
413ef8cb MS |
818 | }; |
819 | ||
f5e54d6e | 820 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded MS |
821 | .readpage = fuse_readpage, |
822 | .prepare_write = fuse_prepare_write, | |
823 | .commit_write = fuse_commit_write, | |
db50b96c | 824 | .readpages = fuse_readpages, |
b6aeaded | 825 | .set_page_dirty = fuse_set_page_dirty, |
b2d2272f | 826 | .bmap = fuse_bmap, |
b6aeaded MS |
827 | }; |
828 | ||
829 | void fuse_init_file_inode(struct inode *inode) | |
830 | { | |
45323fb7 MS |
831 | inode->i_fop = &fuse_file_operations; |
832 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 833 | } |