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