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