Commit | Line | Data |
---|---|---|
b6aeaded MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
1729a16c | 3 | Copyright (C) 2001-2008 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> |
08cbf542 | 15 | #include <linux/module.h> |
d9d318d3 | 16 | #include <linux/compat.h> |
478e0841 | 17 | #include <linux/swap.h> |
a27bb332 | 18 | #include <linux/aio.h> |
b6aeaded | 19 | |
4b6f5d20 | 20 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 21 | |
91fe96b4 MS |
22 | static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
23 | int opcode, struct fuse_open_out *outargp) | |
b6aeaded | 24 | { |
b6aeaded | 25 | struct fuse_open_in inarg; |
fd72faac MS |
26 | struct fuse_req *req; |
27 | int err; | |
28 | ||
b111c8c0 | 29 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
30 | if (IS_ERR(req)) |
31 | return PTR_ERR(req); | |
fd72faac MS |
32 | |
33 | memset(&inarg, 0, sizeof(inarg)); | |
6ff958ed MS |
34 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
35 | if (!fc->atomic_o_trunc) | |
36 | inarg.flags &= ~O_TRUNC; | |
91fe96b4 MS |
37 | req->in.h.opcode = opcode; |
38 | req->in.h.nodeid = nodeid; | |
fd72faac MS |
39 | req->in.numargs = 1; |
40 | req->in.args[0].size = sizeof(inarg); | |
41 | req->in.args[0].value = &inarg; | |
42 | req->out.numargs = 1; | |
43 | req->out.args[0].size = sizeof(*outargp); | |
44 | req->out.args[0].value = outargp; | |
b93f858a | 45 | fuse_request_send(fc, req); |
fd72faac MS |
46 | err = req->out.h.error; |
47 | fuse_put_request(fc, req); | |
48 | ||
49 | return err; | |
50 | } | |
51 | ||
acf99433 | 52 | struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) |
fd72faac MS |
53 | { |
54 | struct fuse_file *ff; | |
6b2db28a | 55 | |
fd72faac | 56 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); |
6b2db28a TH |
57 | if (unlikely(!ff)) |
58 | return NULL; | |
59 | ||
da5e4714 | 60 | ff->fc = fc; |
4250c066 | 61 | ff->reserved_req = fuse_request_alloc(0); |
6b2db28a TH |
62 | if (unlikely(!ff->reserved_req)) { |
63 | kfree(ff); | |
64 | return NULL; | |
fd72faac | 65 | } |
6b2db28a TH |
66 | |
67 | INIT_LIST_HEAD(&ff->write_entry); | |
68 | atomic_set(&ff->count, 0); | |
69 | RB_CLEAR_NODE(&ff->polled_node); | |
70 | init_waitqueue_head(&ff->poll_wait); | |
71 | ||
72 | spin_lock(&fc->lock); | |
73 | ff->kh = ++fc->khctr; | |
74 | spin_unlock(&fc->lock); | |
75 | ||
fd72faac MS |
76 | return ff; |
77 | } | |
78 | ||
79 | void fuse_file_free(struct fuse_file *ff) | |
80 | { | |
33649c91 | 81 | fuse_request_free(ff->reserved_req); |
fd72faac MS |
82 | kfree(ff); |
83 | } | |
84 | ||
c7b7143c | 85 | struct fuse_file *fuse_file_get(struct fuse_file *ff) |
c756e0a4 MS |
86 | { |
87 | atomic_inc(&ff->count); | |
88 | return ff; | |
89 | } | |
90 | ||
5a18ec17 MS |
91 | static void fuse_release_async(struct work_struct *work) |
92 | { | |
93 | struct fuse_req *req; | |
94 | struct fuse_conn *fc; | |
95 | struct path path; | |
96 | ||
97 | req = container_of(work, struct fuse_req, misc.release.work); | |
98 | path = req->misc.release.path; | |
99 | fc = get_fuse_conn(path.dentry->d_inode); | |
100 | ||
101 | fuse_put_request(fc, req); | |
102 | path_put(&path); | |
103 | } | |
104 | ||
819c4b3b MS |
105 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
106 | { | |
5a18ec17 MS |
107 | if (fc->destroy_req) { |
108 | /* | |
109 | * If this is a fuseblk mount, then it's possible that | |
110 | * releasing the path will result in releasing the | |
111 | * super block and sending the DESTROY request. If | |
112 | * the server is single threaded, this would hang. | |
113 | * For this reason do the path_put() in a separate | |
114 | * thread. | |
115 | */ | |
116 | atomic_inc(&req->count); | |
117 | INIT_WORK(&req->misc.release.work, fuse_release_async); | |
118 | schedule_work(&req->misc.release.work); | |
119 | } else { | |
120 | path_put(&req->misc.release.path); | |
121 | } | |
819c4b3b MS |
122 | } |
123 | ||
5a18ec17 | 124 | static void fuse_file_put(struct fuse_file *ff, bool sync) |
c756e0a4 MS |
125 | { |
126 | if (atomic_dec_and_test(&ff->count)) { | |
127 | struct fuse_req *req = ff->reserved_req; | |
8b0797a4 | 128 | |
5a18ec17 MS |
129 | if (sync) { |
130 | fuse_request_send(ff->fc, req); | |
131 | path_put(&req->misc.release.path); | |
132 | fuse_put_request(ff->fc, req); | |
133 | } else { | |
134 | req->end = fuse_release_end; | |
135 | fuse_request_send_background(ff->fc, req); | |
136 | } | |
c756e0a4 MS |
137 | kfree(ff); |
138 | } | |
139 | } | |
140 | ||
08cbf542 TH |
141 | int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
142 | bool isdir) | |
91fe96b4 MS |
143 | { |
144 | struct fuse_open_out outarg; | |
145 | struct fuse_file *ff; | |
146 | int err; | |
147 | int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; | |
148 | ||
149 | ff = fuse_file_alloc(fc); | |
150 | if (!ff) | |
151 | return -ENOMEM; | |
152 | ||
153 | err = fuse_send_open(fc, nodeid, file, opcode, &outarg); | |
154 | if (err) { | |
155 | fuse_file_free(ff); | |
156 | return err; | |
157 | } | |
158 | ||
159 | if (isdir) | |
160 | outarg.open_flags &= ~FOPEN_DIRECT_IO; | |
161 | ||
162 | ff->fh = outarg.fh; | |
163 | ff->nodeid = nodeid; | |
164 | ff->open_flags = outarg.open_flags; | |
165 | file->private_data = fuse_file_get(ff); | |
166 | ||
167 | return 0; | |
168 | } | |
08cbf542 | 169 | EXPORT_SYMBOL_GPL(fuse_do_open); |
91fe96b4 | 170 | |
c7b7143c | 171 | void fuse_finish_open(struct inode *inode, struct file *file) |
fd72faac | 172 | { |
c7b7143c | 173 | struct fuse_file *ff = file->private_data; |
a0822c55 | 174 | struct fuse_conn *fc = get_fuse_conn(inode); |
c7b7143c MS |
175 | |
176 | if (ff->open_flags & FOPEN_DIRECT_IO) | |
fd72faac | 177 | file->f_op = &fuse_direct_io_file_operations; |
c7b7143c | 178 | if (!(ff->open_flags & FOPEN_KEEP_CACHE)) |
b1009979 | 179 | invalidate_inode_pages2(inode->i_mapping); |
c7b7143c | 180 | if (ff->open_flags & FOPEN_NONSEEKABLE) |
a7c1b990 | 181 | nonseekable_open(inode, file); |
a0822c55 KS |
182 | if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) { |
183 | struct fuse_inode *fi = get_fuse_inode(inode); | |
184 | ||
185 | spin_lock(&fc->lock); | |
186 | fi->attr_version = ++fc->attr_version; | |
187 | i_size_write(inode, 0); | |
188 | spin_unlock(&fc->lock); | |
189 | fuse_invalidate_attr(inode); | |
190 | } | |
fd72faac MS |
191 | } |
192 | ||
91fe96b4 | 193 | int fuse_open_common(struct inode *inode, struct file *file, bool isdir) |
fd72faac | 194 | { |
acf99433 | 195 | struct fuse_conn *fc = get_fuse_conn(inode); |
b6aeaded | 196 | int err; |
b6aeaded MS |
197 | |
198 | err = generic_file_open(inode, file); | |
199 | if (err) | |
200 | return err; | |
201 | ||
91fe96b4 | 202 | err = fuse_do_open(fc, get_node_id(inode), file, isdir); |
fd72faac | 203 | if (err) |
91fe96b4 | 204 | return err; |
b6aeaded | 205 | |
91fe96b4 MS |
206 | fuse_finish_open(inode, file); |
207 | ||
208 | return 0; | |
b6aeaded MS |
209 | } |
210 | ||
8b0797a4 | 211 | static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode) |
64c6d8ed | 212 | { |
8b0797a4 | 213 | struct fuse_conn *fc = ff->fc; |
33649c91 | 214 | struct fuse_req *req = ff->reserved_req; |
b57d4264 | 215 | struct fuse_release_in *inarg = &req->misc.release.in; |
b6aeaded | 216 | |
8b0797a4 MS |
217 | spin_lock(&fc->lock); |
218 | list_del(&ff->write_entry); | |
219 | if (!RB_EMPTY_NODE(&ff->polled_node)) | |
220 | rb_erase(&ff->polled_node, &fc->polled_files); | |
221 | spin_unlock(&fc->lock); | |
222 | ||
357ccf2b | 223 | wake_up_interruptible_all(&ff->poll_wait); |
8b0797a4 | 224 | |
b6aeaded | 225 | inarg->fh = ff->fh; |
fd72faac | 226 | inarg->flags = flags; |
51eb01e7 | 227 | req->in.h.opcode = opcode; |
c7b7143c | 228 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
229 | req->in.numargs = 1; |
230 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
231 | req->in.args[0].value = inarg; | |
fd72faac MS |
232 | } |
233 | ||
8b0797a4 | 234 | void fuse_release_common(struct file *file, int opcode) |
fd72faac | 235 | { |
6b2db28a TH |
236 | struct fuse_file *ff; |
237 | struct fuse_req *req; | |
b6aeaded | 238 | |
6b2db28a TH |
239 | ff = file->private_data; |
240 | if (unlikely(!ff)) | |
8b0797a4 | 241 | return; |
6b2db28a | 242 | |
6b2db28a | 243 | req = ff->reserved_req; |
8b0797a4 | 244 | fuse_prepare_release(ff, file->f_flags, opcode); |
6b2db28a | 245 | |
37fb3a30 MS |
246 | if (ff->flock) { |
247 | struct fuse_release_in *inarg = &req->misc.release.in; | |
248 | inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; | |
249 | inarg->lock_owner = fuse_lock_owner_id(ff->fc, | |
250 | (fl_owner_t) file); | |
251 | } | |
6b2db28a | 252 | /* Hold vfsmount and dentry until release is finished */ |
b0be46eb MS |
253 | path_get(&file->f_path); |
254 | req->misc.release.path = file->f_path; | |
6b2db28a | 255 | |
6b2db28a TH |
256 | /* |
257 | * Normally this will send the RELEASE request, however if | |
258 | * some asynchronous READ or WRITE requests are outstanding, | |
259 | * the sending will be delayed. | |
5a18ec17 MS |
260 | * |
261 | * Make the release synchronous if this is a fuseblk mount, | |
262 | * synchronous RELEASE is allowed (and desirable) in this case | |
263 | * because the server can be trusted not to screw up. | |
6b2db28a | 264 | */ |
5a18ec17 | 265 | fuse_file_put(ff, ff->fc->destroy_req != NULL); |
b6aeaded MS |
266 | } |
267 | ||
04730fef MS |
268 | static int fuse_open(struct inode *inode, struct file *file) |
269 | { | |
91fe96b4 | 270 | return fuse_open_common(inode, file, false); |
04730fef MS |
271 | } |
272 | ||
273 | static int fuse_release(struct inode *inode, struct file *file) | |
274 | { | |
8b0797a4 MS |
275 | fuse_release_common(file, FUSE_RELEASE); |
276 | ||
277 | /* return value is ignored by VFS */ | |
278 | return 0; | |
279 | } | |
280 | ||
281 | void fuse_sync_release(struct fuse_file *ff, int flags) | |
282 | { | |
283 | WARN_ON(atomic_read(&ff->count) > 1); | |
284 | fuse_prepare_release(ff, flags, FUSE_RELEASE); | |
285 | ff->reserved_req->force = 1; | |
286 | fuse_request_send(ff->fc, ff->reserved_req); | |
287 | fuse_put_request(ff->fc, ff->reserved_req); | |
288 | kfree(ff); | |
04730fef | 289 | } |
08cbf542 | 290 | EXPORT_SYMBOL_GPL(fuse_sync_release); |
04730fef | 291 | |
71421259 | 292 | /* |
9c8ef561 MS |
293 | * Scramble the ID space with XTEA, so that the value of the files_struct |
294 | * pointer is not exposed to userspace. | |
71421259 | 295 | */ |
f3332114 | 296 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 297 | { |
9c8ef561 MS |
298 | u32 *k = fc->scramble_key; |
299 | u64 v = (unsigned long) id; | |
300 | u32 v0 = v; | |
301 | u32 v1 = v >> 32; | |
302 | u32 sum = 0; | |
303 | int i; | |
304 | ||
305 | for (i = 0; i < 32; i++) { | |
306 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
307 | sum += 0x9E3779B9; | |
308 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
309 | } | |
310 | ||
311 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
312 | } |
313 | ||
3be5a52b MS |
314 | /* |
315 | * Check if page is under writeback | |
316 | * | |
317 | * This is currently done by walking the list of writepage requests | |
318 | * for the inode, which can be pretty inefficient. | |
319 | */ | |
320 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) | |
321 | { | |
322 | struct fuse_conn *fc = get_fuse_conn(inode); | |
323 | struct fuse_inode *fi = get_fuse_inode(inode); | |
324 | struct fuse_req *req; | |
325 | bool found = false; | |
326 | ||
327 | spin_lock(&fc->lock); | |
328 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | |
329 | pgoff_t curr_index; | |
330 | ||
331 | BUG_ON(req->inode != inode); | |
332 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; | |
333 | if (curr_index == index) { | |
334 | found = true; | |
335 | break; | |
336 | } | |
337 | } | |
338 | spin_unlock(&fc->lock); | |
339 | ||
340 | return found; | |
341 | } | |
342 | ||
343 | /* | |
344 | * Wait for page writeback to be completed. | |
345 | * | |
346 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
347 | * use some other means. | |
348 | */ | |
349 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) | |
350 | { | |
351 | struct fuse_inode *fi = get_fuse_inode(inode); | |
352 | ||
353 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
354 | return 0; | |
355 | } | |
356 | ||
75e1fcc0 | 357 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 358 | { |
6131ffaa | 359 | struct inode *inode = file_inode(file); |
b6aeaded MS |
360 | struct fuse_conn *fc = get_fuse_conn(inode); |
361 | struct fuse_file *ff = file->private_data; | |
362 | struct fuse_req *req; | |
363 | struct fuse_flush_in inarg; | |
364 | int err; | |
365 | ||
248d86e8 MS |
366 | if (is_bad_inode(inode)) |
367 | return -EIO; | |
368 | ||
b6aeaded MS |
369 | if (fc->no_flush) |
370 | return 0; | |
371 | ||
b111c8c0 | 372 | req = fuse_get_req_nofail_nopages(fc, file); |
b6aeaded MS |
373 | memset(&inarg, 0, sizeof(inarg)); |
374 | inarg.fh = ff->fh; | |
9c8ef561 | 375 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
b6aeaded MS |
376 | req->in.h.opcode = FUSE_FLUSH; |
377 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
378 | req->in.numargs = 1; |
379 | req->in.args[0].size = sizeof(inarg); | |
380 | req->in.args[0].value = &inarg; | |
71421259 | 381 | req->force = 1; |
b93f858a | 382 | fuse_request_send(fc, req); |
b6aeaded MS |
383 | err = req->out.h.error; |
384 | fuse_put_request(fc, req); | |
385 | if (err == -ENOSYS) { | |
386 | fc->no_flush = 1; | |
387 | err = 0; | |
388 | } | |
389 | return err; | |
390 | } | |
391 | ||
3be5a52b MS |
392 | /* |
393 | * Wait for all pending writepages on the inode to finish. | |
394 | * | |
395 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
396 | * and waiting for all sent writes to complete. | |
397 | * | |
398 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
399 | * could conflict with truncation. | |
400 | */ | |
401 | static void fuse_sync_writes(struct inode *inode) | |
402 | { | |
403 | fuse_set_nowrite(inode); | |
404 | fuse_release_nowrite(inode); | |
405 | } | |
406 | ||
02c24a82 JB |
407 | int fuse_fsync_common(struct file *file, loff_t start, loff_t end, |
408 | int datasync, int isdir) | |
b6aeaded | 409 | { |
7ea80859 | 410 | struct inode *inode = file->f_mapping->host; |
b6aeaded MS |
411 | struct fuse_conn *fc = get_fuse_conn(inode); |
412 | struct fuse_file *ff = file->private_data; | |
413 | struct fuse_req *req; | |
414 | struct fuse_fsync_in inarg; | |
415 | int err; | |
416 | ||
248d86e8 MS |
417 | if (is_bad_inode(inode)) |
418 | return -EIO; | |
419 | ||
02c24a82 JB |
420 | err = filemap_write_and_wait_range(inode->i_mapping, start, end); |
421 | if (err) | |
422 | return err; | |
423 | ||
82547981 | 424 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
425 | return 0; |
426 | ||
02c24a82 JB |
427 | mutex_lock(&inode->i_mutex); |
428 | ||
3be5a52b MS |
429 | /* |
430 | * Start writeback against all dirty pages of the inode, then | |
431 | * wait for all outstanding writes, before sending the FSYNC | |
432 | * request. | |
433 | */ | |
434 | err = write_inode_now(inode, 0); | |
435 | if (err) | |
02c24a82 | 436 | goto out; |
3be5a52b MS |
437 | |
438 | fuse_sync_writes(inode); | |
439 | ||
b111c8c0 | 440 | req = fuse_get_req_nopages(fc); |
02c24a82 JB |
441 | if (IS_ERR(req)) { |
442 | err = PTR_ERR(req); | |
443 | goto out; | |
444 | } | |
b6aeaded MS |
445 | |
446 | memset(&inarg, 0, sizeof(inarg)); | |
447 | inarg.fh = ff->fh; | |
448 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 449 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 450 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
451 | req->in.numargs = 1; |
452 | req->in.args[0].size = sizeof(inarg); | |
453 | req->in.args[0].value = &inarg; | |
b93f858a | 454 | fuse_request_send(fc, req); |
b6aeaded MS |
455 | err = req->out.h.error; |
456 | fuse_put_request(fc, req); | |
457 | if (err == -ENOSYS) { | |
82547981 MS |
458 | if (isdir) |
459 | fc->no_fsyncdir = 1; | |
460 | else | |
461 | fc->no_fsync = 1; | |
b6aeaded MS |
462 | err = 0; |
463 | } | |
02c24a82 JB |
464 | out: |
465 | mutex_unlock(&inode->i_mutex); | |
b6aeaded MS |
466 | return err; |
467 | } | |
468 | ||
02c24a82 JB |
469 | static int fuse_fsync(struct file *file, loff_t start, loff_t end, |
470 | int datasync) | |
82547981 | 471 | { |
02c24a82 | 472 | return fuse_fsync_common(file, start, end, datasync, 0); |
82547981 MS |
473 | } |
474 | ||
2106cb18 MS |
475 | void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos, |
476 | size_t count, int opcode) | |
b6aeaded | 477 | { |
5c5c5e51 | 478 | struct fuse_read_in *inarg = &req->misc.read.in; |
a6643094 | 479 | struct fuse_file *ff = file->private_data; |
b6aeaded | 480 | |
361b1eb5 MS |
481 | inarg->fh = ff->fh; |
482 | inarg->offset = pos; | |
483 | inarg->size = count; | |
a6643094 | 484 | inarg->flags = file->f_flags; |
361b1eb5 | 485 | req->in.h.opcode = opcode; |
2106cb18 | 486 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
487 | req->in.numargs = 1; |
488 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 489 | req->in.args[0].value = inarg; |
b6aeaded MS |
490 | req->out.argvar = 1; |
491 | req->out.numargs = 1; | |
492 | req->out.args[0].size = count; | |
b6aeaded MS |
493 | } |
494 | ||
8bfc016d | 495 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
2106cb18 | 496 | loff_t pos, size_t count, fl_owner_t owner) |
04730fef | 497 | { |
2106cb18 MS |
498 | struct fuse_file *ff = file->private_data; |
499 | struct fuse_conn *fc = ff->fc; | |
f3332114 | 500 | |
2106cb18 | 501 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
f3332114 | 502 | if (owner != NULL) { |
5c5c5e51 | 503 | struct fuse_read_in *inarg = &req->misc.read.in; |
f3332114 MS |
504 | |
505 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | |
506 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
507 | } | |
b93f858a | 508 | fuse_request_send(fc, req); |
361b1eb5 | 509 | return req->out.args[0].size; |
04730fef MS |
510 | } |
511 | ||
5c5c5e51 MS |
512 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
513 | u64 attr_ver) | |
514 | { | |
515 | struct fuse_conn *fc = get_fuse_conn(inode); | |
516 | struct fuse_inode *fi = get_fuse_inode(inode); | |
517 | ||
518 | spin_lock(&fc->lock); | |
519 | if (attr_ver == fi->attr_version && size < inode->i_size) { | |
520 | fi->attr_version = ++fc->attr_version; | |
521 | i_size_write(inode, size); | |
522 | } | |
523 | spin_unlock(&fc->lock); | |
524 | } | |
525 | ||
b6aeaded MS |
526 | static int fuse_readpage(struct file *file, struct page *page) |
527 | { | |
528 | struct inode *inode = page->mapping->host; | |
529 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 | 530 | struct fuse_req *req; |
5c5c5e51 MS |
531 | size_t num_read; |
532 | loff_t pos = page_offset(page); | |
533 | size_t count = PAGE_CACHE_SIZE; | |
534 | u64 attr_ver; | |
248d86e8 MS |
535 | int err; |
536 | ||
537 | err = -EIO; | |
538 | if (is_bad_inode(inode)) | |
539 | goto out; | |
540 | ||
3be5a52b | 541 | /* |
25985edc | 542 | * Page writeback can extend beyond the lifetime of the |
3be5a52b MS |
543 | * page-cache page, so make sure we read a properly synced |
544 | * page. | |
545 | */ | |
546 | fuse_wait_on_page_writeback(inode, page->index); | |
547 | ||
b111c8c0 | 548 | req = fuse_get_req(fc, 1); |
ce1d5a49 MS |
549 | err = PTR_ERR(req); |
550 | if (IS_ERR(req)) | |
b6aeaded MS |
551 | goto out; |
552 | ||
5c5c5e51 MS |
553 | attr_ver = fuse_get_attr_version(fc); |
554 | ||
b6aeaded | 555 | req->out.page_zeroing = 1; |
f4975c67 | 556 | req->out.argpages = 1; |
b6aeaded MS |
557 | req->num_pages = 1; |
558 | req->pages[0] = page; | |
85f40aec | 559 | req->page_descs[0].length = count; |
2106cb18 | 560 | num_read = fuse_send_read(req, file, pos, count, NULL); |
b6aeaded MS |
561 | err = req->out.h.error; |
562 | fuse_put_request(fc, req); | |
5c5c5e51 MS |
563 | |
564 | if (!err) { | |
565 | /* | |
566 | * Short read means EOF. If file size is larger, truncate it | |
567 | */ | |
568 | if (num_read < count) | |
569 | fuse_read_update_size(inode, pos + num_read, attr_ver); | |
570 | ||
b6aeaded | 571 | SetPageUptodate(page); |
5c5c5e51 MS |
572 | } |
573 | ||
b36c31ba | 574 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
575 | out: |
576 | unlock_page(page); | |
577 | return err; | |
578 | } | |
579 | ||
c1aa96a5 | 580 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 581 | { |
c1aa96a5 | 582 | int i; |
5c5c5e51 MS |
583 | size_t count = req->misc.read.in.size; |
584 | size_t num_read = req->out.args[0].size; | |
ce534fb0 | 585 | struct address_space *mapping = NULL; |
c1aa96a5 | 586 | |
ce534fb0 MS |
587 | for (i = 0; mapping == NULL && i < req->num_pages; i++) |
588 | mapping = req->pages[i]->mapping; | |
5c5c5e51 | 589 | |
ce534fb0 MS |
590 | if (mapping) { |
591 | struct inode *inode = mapping->host; | |
592 | ||
593 | /* | |
594 | * Short read means EOF. If file size is larger, truncate it | |
595 | */ | |
596 | if (!req->out.h.error && num_read < count) { | |
597 | loff_t pos; | |
598 | ||
599 | pos = page_offset(req->pages[0]) + num_read; | |
600 | fuse_read_update_size(inode, pos, | |
601 | req->misc.read.attr_ver); | |
602 | } | |
603 | fuse_invalidate_attr(inode); /* atime changed */ | |
604 | } | |
c1aa96a5 | 605 | |
db50b96c MS |
606 | for (i = 0; i < req->num_pages; i++) { |
607 | struct page *page = req->pages[i]; | |
608 | if (!req->out.h.error) | |
609 | SetPageUptodate(page); | |
c1aa96a5 MS |
610 | else |
611 | SetPageError(page); | |
db50b96c | 612 | unlock_page(page); |
b5dd3285 | 613 | page_cache_release(page); |
db50b96c | 614 | } |
c756e0a4 | 615 | if (req->ff) |
5a18ec17 | 616 | fuse_file_put(req->ff, false); |
c1aa96a5 MS |
617 | } |
618 | ||
2106cb18 | 619 | static void fuse_send_readpages(struct fuse_req *req, struct file *file) |
c1aa96a5 | 620 | { |
2106cb18 MS |
621 | struct fuse_file *ff = file->private_data; |
622 | struct fuse_conn *fc = ff->fc; | |
c1aa96a5 MS |
623 | loff_t pos = page_offset(req->pages[0]); |
624 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
f4975c67 MS |
625 | |
626 | req->out.argpages = 1; | |
c1aa96a5 | 627 | req->out.page_zeroing = 1; |
ce534fb0 | 628 | req->out.page_replace = 1; |
2106cb18 | 629 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
5c5c5e51 | 630 | req->misc.read.attr_ver = fuse_get_attr_version(fc); |
9cd68455 | 631 | if (fc->async_read) { |
c756e0a4 | 632 | req->ff = fuse_file_get(ff); |
9cd68455 | 633 | req->end = fuse_readpages_end; |
b93f858a | 634 | fuse_request_send_background(fc, req); |
9cd68455 | 635 | } else { |
b93f858a | 636 | fuse_request_send(fc, req); |
9cd68455 | 637 | fuse_readpages_end(fc, req); |
e9bb09dd | 638 | fuse_put_request(fc, req); |
9cd68455 | 639 | } |
db50b96c MS |
640 | } |
641 | ||
c756e0a4 | 642 | struct fuse_fill_data { |
db50b96c | 643 | struct fuse_req *req; |
a6643094 | 644 | struct file *file; |
db50b96c | 645 | struct inode *inode; |
f8dbdf81 | 646 | unsigned nr_pages; |
db50b96c MS |
647 | }; |
648 | ||
649 | static int fuse_readpages_fill(void *_data, struct page *page) | |
650 | { | |
c756e0a4 | 651 | struct fuse_fill_data *data = _data; |
db50b96c MS |
652 | struct fuse_req *req = data->req; |
653 | struct inode *inode = data->inode; | |
654 | struct fuse_conn *fc = get_fuse_conn(inode); | |
655 | ||
3be5a52b MS |
656 | fuse_wait_on_page_writeback(inode, page->index); |
657 | ||
db50b96c MS |
658 | if (req->num_pages && |
659 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
660 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
661 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
f8dbdf81 MP |
662 | int nr_alloc = min_t(unsigned, data->nr_pages, |
663 | FUSE_MAX_PAGES_PER_REQ); | |
2106cb18 | 664 | fuse_send_readpages(req, data->file); |
f8dbdf81 | 665 | data->req = req = fuse_get_req(fc, nr_alloc); |
ce1d5a49 | 666 | if (IS_ERR(req)) { |
db50b96c | 667 | unlock_page(page); |
ce1d5a49 | 668 | return PTR_ERR(req); |
db50b96c | 669 | } |
db50b96c | 670 | } |
f8dbdf81 MP |
671 | |
672 | if (WARN_ON(req->num_pages >= req->max_pages)) { | |
673 | fuse_put_request(fc, req); | |
674 | return -EIO; | |
675 | } | |
676 | ||
b5dd3285 | 677 | page_cache_get(page); |
db50b96c | 678 | req->pages[req->num_pages] = page; |
85f40aec | 679 | req->page_descs[req->num_pages].length = PAGE_SIZE; |
1729a16c | 680 | req->num_pages++; |
f8dbdf81 | 681 | data->nr_pages--; |
db50b96c MS |
682 | return 0; |
683 | } | |
684 | ||
685 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
686 | struct list_head *pages, unsigned nr_pages) | |
687 | { | |
688 | struct inode *inode = mapping->host; | |
689 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c756e0a4 | 690 | struct fuse_fill_data data; |
db50b96c | 691 | int err; |
f8dbdf81 | 692 | int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ); |
248d86e8 | 693 | |
1d7ea732 | 694 | err = -EIO; |
248d86e8 | 695 | if (is_bad_inode(inode)) |
2e990021 | 696 | goto out; |
248d86e8 | 697 | |
a6643094 | 698 | data.file = file; |
db50b96c | 699 | data.inode = inode; |
f8dbdf81 MP |
700 | data.req = fuse_get_req(fc, nr_alloc); |
701 | data.nr_pages = nr_pages; | |
1d7ea732 | 702 | err = PTR_ERR(data.req); |
ce1d5a49 | 703 | if (IS_ERR(data.req)) |
2e990021 | 704 | goto out; |
db50b96c MS |
705 | |
706 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
707 | if (!err) { |
708 | if (data.req->num_pages) | |
2106cb18 | 709 | fuse_send_readpages(data.req, file); |
d3406ffa MS |
710 | else |
711 | fuse_put_request(fc, data.req); | |
712 | } | |
2e990021 | 713 | out: |
1d7ea732 | 714 | return err; |
db50b96c MS |
715 | } |
716 | ||
bcb4be80 MS |
717 | static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
718 | unsigned long nr_segs, loff_t pos) | |
719 | { | |
720 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
a8894274 | 721 | struct fuse_conn *fc = get_fuse_conn(inode); |
bcb4be80 | 722 | |
a8894274 BF |
723 | /* |
724 | * In auto invalidate mode, always update attributes on read. | |
725 | * Otherwise, only update if we attempt to read past EOF (to ensure | |
726 | * i_size is up to date). | |
727 | */ | |
728 | if (fc->auto_inval_data || | |
729 | (pos + iov_length(iov, nr_segs) > i_size_read(inode))) { | |
bcb4be80 | 730 | int err; |
bcb4be80 MS |
731 | err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL); |
732 | if (err) | |
733 | return err; | |
734 | } | |
735 | ||
736 | return generic_file_aio_read(iocb, iov, nr_segs, pos); | |
737 | } | |
738 | ||
2d698b07 | 739 | static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff, |
2106cb18 | 740 | loff_t pos, size_t count) |
b6aeaded | 741 | { |
b25e82e5 MS |
742 | struct fuse_write_in *inarg = &req->misc.write.in; |
743 | struct fuse_write_out *outarg = &req->misc.write.out; | |
b6aeaded | 744 | |
b25e82e5 MS |
745 | inarg->fh = ff->fh; |
746 | inarg->offset = pos; | |
747 | inarg->size = count; | |
b6aeaded | 748 | req->in.h.opcode = FUSE_WRITE; |
2106cb18 | 749 | req->in.h.nodeid = ff->nodeid; |
b6aeaded | 750 | req->in.numargs = 2; |
2106cb18 | 751 | if (ff->fc->minor < 9) |
f3332114 MS |
752 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
753 | else | |
754 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
b25e82e5 | 755 | req->in.args[0].value = inarg; |
b6aeaded MS |
756 | req->in.args[1].size = count; |
757 | req->out.numargs = 1; | |
758 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
b25e82e5 MS |
759 | req->out.args[0].value = outarg; |
760 | } | |
761 | ||
762 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, | |
2106cb18 | 763 | loff_t pos, size_t count, fl_owner_t owner) |
b25e82e5 | 764 | { |
2106cb18 MS |
765 | struct fuse_file *ff = file->private_data; |
766 | struct fuse_conn *fc = ff->fc; | |
2d698b07 MS |
767 | struct fuse_write_in *inarg = &req->misc.write.in; |
768 | ||
2106cb18 | 769 | fuse_write_fill(req, ff, pos, count); |
2d698b07 | 770 | inarg->flags = file->f_flags; |
f3332114 | 771 | if (owner != NULL) { |
f3332114 MS |
772 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
773 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
774 | } | |
b93f858a | 775 | fuse_request_send(fc, req); |
b25e82e5 | 776 | return req->misc.write.out.size; |
b6aeaded MS |
777 | } |
778 | ||
a1d75f25 | 779 | void fuse_write_update_size(struct inode *inode, loff_t pos) |
854512ec MS |
780 | { |
781 | struct fuse_conn *fc = get_fuse_conn(inode); | |
782 | struct fuse_inode *fi = get_fuse_inode(inode); | |
783 | ||
784 | spin_lock(&fc->lock); | |
785 | fi->attr_version = ++fc->attr_version; | |
786 | if (pos > inode->i_size) | |
787 | i_size_write(inode, pos); | |
788 | spin_unlock(&fc->lock); | |
789 | } | |
790 | ||
ea9b9907 NP |
791 | static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file, |
792 | struct inode *inode, loff_t pos, | |
793 | size_t count) | |
794 | { | |
795 | size_t res; | |
796 | unsigned offset; | |
797 | unsigned i; | |
798 | ||
799 | for (i = 0; i < req->num_pages; i++) | |
800 | fuse_wait_on_page_writeback(inode, req->pages[i]->index); | |
801 | ||
2106cb18 | 802 | res = fuse_send_write(req, file, pos, count, NULL); |
ea9b9907 | 803 | |
b2430d75 | 804 | offset = req->page_descs[0].offset; |
ea9b9907 NP |
805 | count = res; |
806 | for (i = 0; i < req->num_pages; i++) { | |
807 | struct page *page = req->pages[i]; | |
808 | ||
809 | if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE) | |
810 | SetPageUptodate(page); | |
811 | ||
812 | if (count > PAGE_CACHE_SIZE - offset) | |
813 | count -= PAGE_CACHE_SIZE - offset; | |
814 | else | |
815 | count = 0; | |
816 | offset = 0; | |
817 | ||
818 | unlock_page(page); | |
819 | page_cache_release(page); | |
820 | } | |
821 | ||
822 | return res; | |
823 | } | |
824 | ||
825 | static ssize_t fuse_fill_write_pages(struct fuse_req *req, | |
826 | struct address_space *mapping, | |
827 | struct iov_iter *ii, loff_t pos) | |
828 | { | |
829 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
830 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | |
831 | size_t count = 0; | |
832 | int err; | |
833 | ||
f4975c67 | 834 | req->in.argpages = 1; |
b2430d75 | 835 | req->page_descs[0].offset = offset; |
ea9b9907 NP |
836 | |
837 | do { | |
838 | size_t tmp; | |
839 | struct page *page; | |
840 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
841 | size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset, | |
842 | iov_iter_count(ii)); | |
843 | ||
844 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
845 | ||
846 | again: | |
847 | err = -EFAULT; | |
848 | if (iov_iter_fault_in_readable(ii, bytes)) | |
849 | break; | |
850 | ||
851 | err = -ENOMEM; | |
54566b2c | 852 | page = grab_cache_page_write_begin(mapping, index, 0); |
ea9b9907 NP |
853 | if (!page) |
854 | break; | |
855 | ||
931e80e4 | 856 | if (mapping_writably_mapped(mapping)) |
857 | flush_dcache_page(page); | |
858 | ||
ea9b9907 NP |
859 | pagefault_disable(); |
860 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); | |
861 | pagefault_enable(); | |
862 | flush_dcache_page(page); | |
863 | ||
478e0841 JW |
864 | mark_page_accessed(page); |
865 | ||
ea9b9907 NP |
866 | if (!tmp) { |
867 | unlock_page(page); | |
868 | page_cache_release(page); | |
869 | bytes = min(bytes, iov_iter_single_seg_count(ii)); | |
870 | goto again; | |
871 | } | |
872 | ||
873 | err = 0; | |
874 | req->pages[req->num_pages] = page; | |
85f40aec | 875 | req->page_descs[req->num_pages].length = tmp; |
ea9b9907 NP |
876 | req->num_pages++; |
877 | ||
878 | iov_iter_advance(ii, tmp); | |
879 | count += tmp; | |
880 | pos += tmp; | |
881 | offset += tmp; | |
882 | if (offset == PAGE_CACHE_SIZE) | |
883 | offset = 0; | |
884 | ||
78bb6cb9 MS |
885 | if (!fc->big_writes) |
886 | break; | |
ea9b9907 | 887 | } while (iov_iter_count(ii) && count < fc->max_write && |
d07f09f5 | 888 | req->num_pages < req->max_pages && offset == 0); |
ea9b9907 NP |
889 | |
890 | return count > 0 ? count : err; | |
891 | } | |
892 | ||
d07f09f5 MP |
893 | static inline unsigned fuse_wr_pages(loff_t pos, size_t len) |
894 | { | |
895 | return min_t(unsigned, | |
896 | ((pos + len - 1) >> PAGE_CACHE_SHIFT) - | |
897 | (pos >> PAGE_CACHE_SHIFT) + 1, | |
898 | FUSE_MAX_PAGES_PER_REQ); | |
899 | } | |
900 | ||
ea9b9907 NP |
901 | static ssize_t fuse_perform_write(struct file *file, |
902 | struct address_space *mapping, | |
903 | struct iov_iter *ii, loff_t pos) | |
904 | { | |
905 | struct inode *inode = mapping->host; | |
906 | struct fuse_conn *fc = get_fuse_conn(inode); | |
907 | int err = 0; | |
908 | ssize_t res = 0; | |
909 | ||
910 | if (is_bad_inode(inode)) | |
911 | return -EIO; | |
912 | ||
913 | do { | |
914 | struct fuse_req *req; | |
915 | ssize_t count; | |
d07f09f5 | 916 | unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii)); |
ea9b9907 | 917 | |
d07f09f5 | 918 | req = fuse_get_req(fc, nr_pages); |
ea9b9907 NP |
919 | if (IS_ERR(req)) { |
920 | err = PTR_ERR(req); | |
921 | break; | |
922 | } | |
923 | ||
924 | count = fuse_fill_write_pages(req, mapping, ii, pos); | |
925 | if (count <= 0) { | |
926 | err = count; | |
927 | } else { | |
928 | size_t num_written; | |
929 | ||
930 | num_written = fuse_send_write_pages(req, file, inode, | |
931 | pos, count); | |
932 | err = req->out.h.error; | |
933 | if (!err) { | |
934 | res += num_written; | |
935 | pos += num_written; | |
936 | ||
937 | /* break out of the loop on short write */ | |
938 | if (num_written != count) | |
939 | err = -EIO; | |
940 | } | |
941 | } | |
942 | fuse_put_request(fc, req); | |
943 | } while (!err && iov_iter_count(ii)); | |
944 | ||
945 | if (res > 0) | |
946 | fuse_write_update_size(inode, pos); | |
947 | ||
948 | fuse_invalidate_attr(inode); | |
949 | ||
950 | return res > 0 ? res : err; | |
951 | } | |
952 | ||
953 | static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | |
954 | unsigned long nr_segs, loff_t pos) | |
955 | { | |
956 | struct file *file = iocb->ki_filp; | |
957 | struct address_space *mapping = file->f_mapping; | |
958 | size_t count = 0; | |
4273b793 | 959 | size_t ocount = 0; |
ea9b9907 | 960 | ssize_t written = 0; |
4273b793 | 961 | ssize_t written_buffered = 0; |
ea9b9907 NP |
962 | struct inode *inode = mapping->host; |
963 | ssize_t err; | |
964 | struct iov_iter i; | |
4273b793 | 965 | loff_t endbyte = 0; |
ea9b9907 NP |
966 | |
967 | WARN_ON(iocb->ki_pos != pos); | |
968 | ||
4273b793 AA |
969 | ocount = 0; |
970 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); | |
ea9b9907 NP |
971 | if (err) |
972 | return err; | |
973 | ||
4273b793 | 974 | count = ocount; |
ea9b9907 | 975 | mutex_lock(&inode->i_mutex); |
ea9b9907 NP |
976 | |
977 | /* We can write back this queue in page reclaim */ | |
978 | current->backing_dev_info = mapping->backing_dev_info; | |
979 | ||
980 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
981 | if (err) | |
982 | goto out; | |
983 | ||
984 | if (count == 0) | |
985 | goto out; | |
986 | ||
2f1936b8 | 987 | err = file_remove_suid(file); |
ea9b9907 NP |
988 | if (err) |
989 | goto out; | |
990 | ||
c3b2da31 JB |
991 | err = file_update_time(file); |
992 | if (err) | |
993 | goto out; | |
ea9b9907 | 994 | |
4273b793 AA |
995 | if (file->f_flags & O_DIRECT) { |
996 | written = generic_file_direct_write(iocb, iov, &nr_segs, | |
997 | pos, &iocb->ki_pos, | |
998 | count, ocount); | |
999 | if (written < 0 || written == count) | |
1000 | goto out; | |
1001 | ||
1002 | pos += written; | |
1003 | count -= written; | |
ea9b9907 | 1004 | |
4273b793 AA |
1005 | iov_iter_init(&i, iov, nr_segs, count, written); |
1006 | written_buffered = fuse_perform_write(file, mapping, &i, pos); | |
1007 | if (written_buffered < 0) { | |
1008 | err = written_buffered; | |
1009 | goto out; | |
1010 | } | |
1011 | endbyte = pos + written_buffered - 1; | |
1012 | ||
1013 | err = filemap_write_and_wait_range(file->f_mapping, pos, | |
1014 | endbyte); | |
1015 | if (err) | |
1016 | goto out; | |
1017 | ||
1018 | invalidate_mapping_pages(file->f_mapping, | |
1019 | pos >> PAGE_CACHE_SHIFT, | |
1020 | endbyte >> PAGE_CACHE_SHIFT); | |
1021 | ||
1022 | written += written_buffered; | |
1023 | iocb->ki_pos = pos + written_buffered; | |
1024 | } else { | |
1025 | iov_iter_init(&i, iov, nr_segs, count, 0); | |
1026 | written = fuse_perform_write(file, mapping, &i, pos); | |
1027 | if (written >= 0) | |
1028 | iocb->ki_pos = pos + written; | |
1029 | } | |
ea9b9907 NP |
1030 | out: |
1031 | current->backing_dev_info = NULL; | |
1032 | mutex_unlock(&inode->i_mutex); | |
1033 | ||
1034 | return written ? written : err; | |
1035 | } | |
1036 | ||
413ef8cb MS |
1037 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
1038 | { | |
1039 | unsigned i; | |
1040 | ||
1041 | for (i = 0; i < req->num_pages; i++) { | |
1042 | struct page *page = req->pages[i]; | |
1043 | if (write) | |
1044 | set_page_dirty_lock(page); | |
1045 | put_page(page); | |
1046 | } | |
1047 | } | |
1048 | ||
7c190c8b MP |
1049 | static inline void fuse_page_descs_length_init(struct fuse_req *req, |
1050 | unsigned index, unsigned nr_pages) | |
85f40aec MP |
1051 | { |
1052 | int i; | |
1053 | ||
7c190c8b | 1054 | for (i = index; i < index + nr_pages; i++) |
85f40aec MP |
1055 | req->page_descs[i].length = PAGE_SIZE - |
1056 | req->page_descs[i].offset; | |
1057 | } | |
1058 | ||
7c190c8b MP |
1059 | static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) |
1060 | { | |
1061 | return (unsigned long)ii->iov->iov_base + ii->iov_offset; | |
1062 | } | |
1063 | ||
1064 | static inline size_t fuse_get_frag_size(const struct iov_iter *ii, | |
1065 | size_t max_size) | |
1066 | { | |
1067 | return min(iov_iter_single_seg_count(ii), max_size); | |
1068 | } | |
1069 | ||
b98d023a | 1070 | static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii, |
ce60a2f1 | 1071 | size_t *nbytesp, int write) |
413ef8cb | 1072 | { |
7c190c8b | 1073 | size_t nbytes = 0; /* # bytes already packed in req */ |
b98d023a | 1074 | |
f4975c67 MS |
1075 | /* Special case for kernel I/O: can copy directly into the buffer */ |
1076 | if (segment_eq(get_fs(), KERNEL_DS)) { | |
7c190c8b MP |
1077 | unsigned long user_addr = fuse_get_user_addr(ii); |
1078 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp); | |
1079 | ||
f4975c67 MS |
1080 | if (write) |
1081 | req->in.args[1].value = (void *) user_addr; | |
1082 | else | |
1083 | req->out.args[0].value = (void *) user_addr; | |
1084 | ||
b98d023a MP |
1085 | iov_iter_advance(ii, frag_size); |
1086 | *nbytesp = frag_size; | |
f4975c67 MS |
1087 | return 0; |
1088 | } | |
413ef8cb | 1089 | |
5565a9d8 | 1090 | while (nbytes < *nbytesp && req->num_pages < req->max_pages) { |
7c190c8b MP |
1091 | unsigned npages; |
1092 | unsigned long user_addr = fuse_get_user_addr(ii); | |
1093 | unsigned offset = user_addr & ~PAGE_MASK; | |
1094 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes); | |
1095 | int ret; | |
413ef8cb | 1096 | |
5565a9d8 | 1097 | unsigned n = req->max_pages - req->num_pages; |
7c190c8b MP |
1098 | frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT); |
1099 | ||
1100 | npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1101 | npages = clamp(npages, 1U, n); | |
1102 | ||
1103 | ret = get_user_pages_fast(user_addr, npages, !write, | |
1104 | &req->pages[req->num_pages]); | |
1105 | if (ret < 0) | |
1106 | return ret; | |
1107 | ||
1108 | npages = ret; | |
1109 | frag_size = min_t(size_t, frag_size, | |
1110 | (npages << PAGE_SHIFT) - offset); | |
1111 | iov_iter_advance(ii, frag_size); | |
1112 | ||
1113 | req->page_descs[req->num_pages].offset = offset; | |
1114 | fuse_page_descs_length_init(req, req->num_pages, npages); | |
1115 | ||
1116 | req->num_pages += npages; | |
1117 | req->page_descs[req->num_pages - 1].length -= | |
1118 | (npages << PAGE_SHIFT) - offset - frag_size; | |
1119 | ||
1120 | nbytes += frag_size; | |
1121 | } | |
f4975c67 MS |
1122 | |
1123 | if (write) | |
1124 | req->in.argpages = 1; | |
1125 | else | |
1126 | req->out.argpages = 1; | |
1127 | ||
7c190c8b | 1128 | *nbytesp = nbytes; |
f4975c67 | 1129 | |
413ef8cb MS |
1130 | return 0; |
1131 | } | |
1132 | ||
5565a9d8 MP |
1133 | static inline int fuse_iter_npages(const struct iov_iter *ii_p) |
1134 | { | |
1135 | struct iov_iter ii = *ii_p; | |
1136 | int npages = 0; | |
1137 | ||
1138 | while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) { | |
1139 | unsigned long user_addr = fuse_get_user_addr(&ii); | |
1140 | unsigned offset = user_addr & ~PAGE_MASK; | |
1141 | size_t frag_size = iov_iter_single_seg_count(&ii); | |
1142 | ||
1143 | npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1144 | iov_iter_advance(&ii, frag_size); | |
1145 | } | |
1146 | ||
1147 | return min(npages, FUSE_MAX_PAGES_PER_REQ); | |
1148 | } | |
1149 | ||
fb05f41f MS |
1150 | ssize_t fuse_direct_io(struct file *file, const struct iovec *iov, |
1151 | unsigned long nr_segs, size_t count, loff_t *ppos, | |
1152 | int write) | |
413ef8cb | 1153 | { |
2106cb18 MS |
1154 | struct fuse_file *ff = file->private_data; |
1155 | struct fuse_conn *fc = ff->fc; | |
413ef8cb MS |
1156 | size_t nmax = write ? fc->max_write : fc->max_read; |
1157 | loff_t pos = *ppos; | |
1158 | ssize_t res = 0; | |
248d86e8 | 1159 | struct fuse_req *req; |
b98d023a MP |
1160 | struct iov_iter ii; |
1161 | ||
1162 | iov_iter_init(&ii, iov, nr_segs, count, 0); | |
248d86e8 | 1163 | |
5565a9d8 | 1164 | req = fuse_get_req(fc, fuse_iter_npages(&ii)); |
ce1d5a49 MS |
1165 | if (IS_ERR(req)) |
1166 | return PTR_ERR(req); | |
413ef8cb MS |
1167 | |
1168 | while (count) { | |
413ef8cb | 1169 | size_t nres; |
2106cb18 | 1170 | fl_owner_t owner = current->files; |
f4975c67 | 1171 | size_t nbytes = min(count, nmax); |
b98d023a | 1172 | int err = fuse_get_user_pages(req, &ii, &nbytes, write); |
413ef8cb MS |
1173 | if (err) { |
1174 | res = err; | |
1175 | break; | |
1176 | } | |
f4975c67 | 1177 | |
413ef8cb | 1178 | if (write) |
2106cb18 | 1179 | nres = fuse_send_write(req, file, pos, nbytes, owner); |
413ef8cb | 1180 | else |
2106cb18 MS |
1181 | nres = fuse_send_read(req, file, pos, nbytes, owner); |
1182 | ||
413ef8cb MS |
1183 | fuse_release_user_pages(req, !write); |
1184 | if (req->out.h.error) { | |
1185 | if (!res) | |
1186 | res = req->out.h.error; | |
1187 | break; | |
1188 | } else if (nres > nbytes) { | |
1189 | res = -EIO; | |
1190 | break; | |
1191 | } | |
1192 | count -= nres; | |
1193 | res += nres; | |
1194 | pos += nres; | |
413ef8cb MS |
1195 | if (nres != nbytes) |
1196 | break; | |
56cf34ff MS |
1197 | if (count) { |
1198 | fuse_put_request(fc, req); | |
5565a9d8 | 1199 | req = fuse_get_req(fc, fuse_iter_npages(&ii)); |
56cf34ff MS |
1200 | if (IS_ERR(req)) |
1201 | break; | |
1202 | } | |
413ef8cb | 1203 | } |
f60311d5 AA |
1204 | if (!IS_ERR(req)) |
1205 | fuse_put_request(fc, req); | |
d09cb9d7 | 1206 | if (res > 0) |
413ef8cb | 1207 | *ppos = pos; |
413ef8cb MS |
1208 | |
1209 | return res; | |
1210 | } | |
08cbf542 | 1211 | EXPORT_SYMBOL_GPL(fuse_direct_io); |
413ef8cb | 1212 | |
b98d023a MP |
1213 | static ssize_t __fuse_direct_read(struct file *file, const struct iovec *iov, |
1214 | unsigned long nr_segs, loff_t *ppos) | |
413ef8cb | 1215 | { |
d09cb9d7 | 1216 | ssize_t res; |
6131ffaa | 1217 | struct inode *inode = file_inode(file); |
d09cb9d7 MS |
1218 | |
1219 | if (is_bad_inode(inode)) | |
1220 | return -EIO; | |
1221 | ||
fb05f41f MS |
1222 | res = fuse_direct_io(file, iov, nr_segs, iov_length(iov, nr_segs), |
1223 | ppos, 0); | |
d09cb9d7 MS |
1224 | |
1225 | fuse_invalidate_attr(inode); | |
1226 | ||
1227 | return res; | |
413ef8cb MS |
1228 | } |
1229 | ||
b98d023a MP |
1230 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, |
1231 | size_t count, loff_t *ppos) | |
1232 | { | |
fb05f41f | 1233 | struct iovec iov = { .iov_base = buf, .iov_len = count }; |
b98d023a MP |
1234 | return __fuse_direct_read(file, &iov, 1, ppos); |
1235 | } | |
1236 | ||
1237 | static ssize_t __fuse_direct_write(struct file *file, const struct iovec *iov, | |
1238 | unsigned long nr_segs, loff_t *ppos) | |
413ef8cb | 1239 | { |
6131ffaa | 1240 | struct inode *inode = file_inode(file); |
b98d023a | 1241 | size_t count = iov_length(iov, nr_segs); |
413ef8cb | 1242 | ssize_t res; |
d09cb9d7 | 1243 | |
889f7848 | 1244 | res = generic_write_checks(file, ppos, &count, 0); |
d09cb9d7 | 1245 | if (!res) { |
fb05f41f | 1246 | res = fuse_direct_io(file, iov, nr_segs, count, ppos, 1); |
d09cb9d7 MS |
1247 | if (res > 0) |
1248 | fuse_write_update_size(inode, *ppos); | |
1249 | } | |
d09cb9d7 MS |
1250 | |
1251 | fuse_invalidate_attr(inode); | |
1252 | ||
413ef8cb MS |
1253 | return res; |
1254 | } | |
1255 | ||
4273b793 AA |
1256 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, |
1257 | size_t count, loff_t *ppos) | |
1258 | { | |
fb05f41f | 1259 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
6131ffaa | 1260 | struct inode *inode = file_inode(file); |
4273b793 AA |
1261 | ssize_t res; |
1262 | ||
1263 | if (is_bad_inode(inode)) | |
1264 | return -EIO; | |
1265 | ||
1266 | /* Don't allow parallel writes to the same file */ | |
1267 | mutex_lock(&inode->i_mutex); | |
b98d023a | 1268 | res = __fuse_direct_write(file, &iov, 1, ppos); |
4273b793 AA |
1269 | mutex_unlock(&inode->i_mutex); |
1270 | ||
1271 | return res; | |
1272 | } | |
1273 | ||
3be5a52b | 1274 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
b6aeaded | 1275 | { |
3be5a52b | 1276 | __free_page(req->pages[0]); |
5a18ec17 | 1277 | fuse_file_put(req->ff, false); |
3be5a52b MS |
1278 | } |
1279 | ||
1280 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | |
1281 | { | |
1282 | struct inode *inode = req->inode; | |
1283 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1284 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; | |
1285 | ||
1286 | list_del(&req->writepages_entry); | |
1287 | dec_bdi_stat(bdi, BDI_WRITEBACK); | |
1288 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); | |
1289 | bdi_writeout_inc(bdi); | |
1290 | wake_up(&fi->page_waitq); | |
1291 | } | |
1292 | ||
1293 | /* Called under fc->lock, may release and reacquire it */ | |
1294 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) | |
b9ca67b2 MS |
1295 | __releases(fc->lock) |
1296 | __acquires(fc->lock) | |
3be5a52b MS |
1297 | { |
1298 | struct fuse_inode *fi = get_fuse_inode(req->inode); | |
1299 | loff_t size = i_size_read(req->inode); | |
1300 | struct fuse_write_in *inarg = &req->misc.write.in; | |
1301 | ||
1302 | if (!fc->connected) | |
1303 | goto out_free; | |
1304 | ||
1305 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { | |
1306 | inarg->size = PAGE_CACHE_SIZE; | |
1307 | } else if (inarg->offset < size) { | |
1308 | inarg->size = size & (PAGE_CACHE_SIZE - 1); | |
1309 | } else { | |
1310 | /* Got truncated off completely */ | |
1311 | goto out_free; | |
b6aeaded | 1312 | } |
3be5a52b MS |
1313 | |
1314 | req->in.args[1].size = inarg->size; | |
1315 | fi->writectr++; | |
b93f858a | 1316 | fuse_request_send_background_locked(fc, req); |
3be5a52b MS |
1317 | return; |
1318 | ||
1319 | out_free: | |
1320 | fuse_writepage_finish(fc, req); | |
1321 | spin_unlock(&fc->lock); | |
1322 | fuse_writepage_free(fc, req); | |
e9bb09dd | 1323 | fuse_put_request(fc, req); |
3be5a52b | 1324 | spin_lock(&fc->lock); |
b6aeaded MS |
1325 | } |
1326 | ||
3be5a52b MS |
1327 | /* |
1328 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1329 | * all queued writepage requests. | |
1330 | * | |
1331 | * Called with fc->lock | |
1332 | */ | |
1333 | void fuse_flush_writepages(struct inode *inode) | |
b9ca67b2 MS |
1334 | __releases(fc->lock) |
1335 | __acquires(fc->lock) | |
b6aeaded | 1336 | { |
3be5a52b MS |
1337 | struct fuse_conn *fc = get_fuse_conn(inode); |
1338 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1339 | struct fuse_req *req; | |
1340 | ||
1341 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
1342 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | |
1343 | list_del_init(&req->list); | |
1344 | fuse_send_writepage(fc, req); | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | |
1349 | { | |
1350 | struct inode *inode = req->inode; | |
1351 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1352 | ||
1353 | mapping_set_error(inode->i_mapping, req->out.h.error); | |
1354 | spin_lock(&fc->lock); | |
1355 | fi->writectr--; | |
1356 | fuse_writepage_finish(fc, req); | |
1357 | spin_unlock(&fc->lock); | |
1358 | fuse_writepage_free(fc, req); | |
1359 | } | |
1360 | ||
1361 | static int fuse_writepage_locked(struct page *page) | |
1362 | { | |
1363 | struct address_space *mapping = page->mapping; | |
1364 | struct inode *inode = mapping->host; | |
1365 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1366 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1367 | struct fuse_req *req; | |
1368 | struct fuse_file *ff; | |
1369 | struct page *tmp_page; | |
1370 | ||
1371 | set_page_writeback(page); | |
1372 | ||
4250c066 | 1373 | req = fuse_request_alloc_nofs(1); |
3be5a52b MS |
1374 | if (!req) |
1375 | goto err; | |
1376 | ||
1377 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
1378 | if (!tmp_page) | |
1379 | goto err_free; | |
1380 | ||
1381 | spin_lock(&fc->lock); | |
1382 | BUG_ON(list_empty(&fi->write_files)); | |
1383 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); | |
1384 | req->ff = fuse_file_get(ff); | |
1385 | spin_unlock(&fc->lock); | |
1386 | ||
2106cb18 | 1387 | fuse_write_fill(req, ff, page_offset(page), 0); |
3be5a52b MS |
1388 | |
1389 | copy_highpage(tmp_page, page); | |
2d698b07 | 1390 | req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; |
f4975c67 | 1391 | req->in.argpages = 1; |
3be5a52b MS |
1392 | req->num_pages = 1; |
1393 | req->pages[0] = tmp_page; | |
b2430d75 | 1394 | req->page_descs[0].offset = 0; |
85f40aec | 1395 | req->page_descs[0].length = PAGE_SIZE; |
3be5a52b MS |
1396 | req->end = fuse_writepage_end; |
1397 | req->inode = inode; | |
1398 | ||
1399 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); | |
1400 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); | |
1401 | end_page_writeback(page); | |
1402 | ||
1403 | spin_lock(&fc->lock); | |
1404 | list_add(&req->writepages_entry, &fi->writepages); | |
1405 | list_add_tail(&req->list, &fi->queued_writes); | |
1406 | fuse_flush_writepages(inode); | |
1407 | spin_unlock(&fc->lock); | |
1408 | ||
1409 | return 0; | |
1410 | ||
1411 | err_free: | |
1412 | fuse_request_free(req); | |
1413 | err: | |
1414 | end_page_writeback(page); | |
1415 | return -ENOMEM; | |
1416 | } | |
1417 | ||
1418 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1419 | { | |
1420 | int err; | |
1421 | ||
1422 | err = fuse_writepage_locked(page); | |
1423 | unlock_page(page); | |
1424 | ||
1425 | return err; | |
1426 | } | |
1427 | ||
1428 | static int fuse_launder_page(struct page *page) | |
1429 | { | |
1430 | int err = 0; | |
1431 | if (clear_page_dirty_for_io(page)) { | |
1432 | struct inode *inode = page->mapping->host; | |
1433 | err = fuse_writepage_locked(page); | |
1434 | if (!err) | |
1435 | fuse_wait_on_page_writeback(inode, page->index); | |
1436 | } | |
1437 | return err; | |
1438 | } | |
1439 | ||
1440 | /* | |
1441 | * Write back dirty pages now, because there may not be any suitable | |
1442 | * open files later | |
1443 | */ | |
1444 | static void fuse_vma_close(struct vm_area_struct *vma) | |
1445 | { | |
1446 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Wait for writeback against this page to complete before allowing it | |
1451 | * to be marked dirty again, and hence written back again, possibly | |
1452 | * before the previous writepage completed. | |
1453 | * | |
1454 | * Block here, instead of in ->writepage(), so that the userspace fs | |
1455 | * can only block processes actually operating on the filesystem. | |
1456 | * | |
1457 | * Otherwise unprivileged userspace fs would be able to block | |
1458 | * unrelated: | |
1459 | * | |
1460 | * - page migration | |
1461 | * - sync(2) | |
1462 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
1463 | */ | |
c2ec175c | 1464 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
3be5a52b | 1465 | { |
c2ec175c | 1466 | struct page *page = vmf->page; |
3be5a52b MS |
1467 | /* |
1468 | * Don't use page->mapping as it may become NULL from a | |
1469 | * concurrent truncate. | |
1470 | */ | |
1471 | struct inode *inode = vma->vm_file->f_mapping->host; | |
1472 | ||
1473 | fuse_wait_on_page_writeback(inode, page->index); | |
1474 | return 0; | |
1475 | } | |
1476 | ||
f0f37e2f | 1477 | static const struct vm_operations_struct fuse_file_vm_ops = { |
3be5a52b MS |
1478 | .close = fuse_vma_close, |
1479 | .fault = filemap_fault, | |
1480 | .page_mkwrite = fuse_page_mkwrite, | |
0b173bc4 | 1481 | .remap_pages = generic_file_remap_pages, |
3be5a52b MS |
1482 | }; |
1483 | ||
1484 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
1485 | { | |
1486 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { | |
6131ffaa | 1487 | struct inode *inode = file_inode(file); |
3be5a52b MS |
1488 | struct fuse_conn *fc = get_fuse_conn(inode); |
1489 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1490 | struct fuse_file *ff = file->private_data; | |
1491 | /* | |
1492 | * file may be written through mmap, so chain it onto the | |
1493 | * inodes's write_file list | |
1494 | */ | |
1495 | spin_lock(&fc->lock); | |
1496 | if (list_empty(&ff->write_entry)) | |
1497 | list_add(&ff->write_entry, &fi->write_files); | |
1498 | spin_unlock(&fc->lock); | |
1499 | } | |
1500 | file_accessed(file); | |
1501 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
1502 | return 0; |
1503 | } | |
1504 | ||
fc280c96 MS |
1505 | static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma) |
1506 | { | |
1507 | /* Can't provide the coherency needed for MAP_SHARED */ | |
1508 | if (vma->vm_flags & VM_MAYSHARE) | |
1509 | return -ENODEV; | |
1510 | ||
3121bfe7 MS |
1511 | invalidate_inode_pages2(file->f_mapping); |
1512 | ||
fc280c96 MS |
1513 | return generic_file_mmap(file, vma); |
1514 | } | |
1515 | ||
71421259 MS |
1516 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
1517 | struct file_lock *fl) | |
1518 | { | |
1519 | switch (ffl->type) { | |
1520 | case F_UNLCK: | |
1521 | break; | |
1522 | ||
1523 | case F_RDLCK: | |
1524 | case F_WRLCK: | |
1525 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
1526 | ffl->end < ffl->start) | |
1527 | return -EIO; | |
1528 | ||
1529 | fl->fl_start = ffl->start; | |
1530 | fl->fl_end = ffl->end; | |
1531 | fl->fl_pid = ffl->pid; | |
1532 | break; | |
1533 | ||
1534 | default: | |
1535 | return -EIO; | |
1536 | } | |
1537 | fl->fl_type = ffl->type; | |
1538 | return 0; | |
1539 | } | |
1540 | ||
1541 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, | |
a9ff4f87 MS |
1542 | const struct file_lock *fl, int opcode, pid_t pid, |
1543 | int flock) | |
71421259 | 1544 | { |
6131ffaa | 1545 | struct inode *inode = file_inode(file); |
9c8ef561 | 1546 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 MS |
1547 | struct fuse_file *ff = file->private_data; |
1548 | struct fuse_lk_in *arg = &req->misc.lk_in; | |
1549 | ||
1550 | arg->fh = ff->fh; | |
9c8ef561 | 1551 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
71421259 MS |
1552 | arg->lk.start = fl->fl_start; |
1553 | arg->lk.end = fl->fl_end; | |
1554 | arg->lk.type = fl->fl_type; | |
1555 | arg->lk.pid = pid; | |
a9ff4f87 MS |
1556 | if (flock) |
1557 | arg->lk_flags |= FUSE_LK_FLOCK; | |
71421259 MS |
1558 | req->in.h.opcode = opcode; |
1559 | req->in.h.nodeid = get_node_id(inode); | |
1560 | req->in.numargs = 1; | |
1561 | req->in.args[0].size = sizeof(*arg); | |
1562 | req->in.args[0].value = arg; | |
1563 | } | |
1564 | ||
1565 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
1566 | { | |
6131ffaa | 1567 | struct inode *inode = file_inode(file); |
71421259 MS |
1568 | struct fuse_conn *fc = get_fuse_conn(inode); |
1569 | struct fuse_req *req; | |
1570 | struct fuse_lk_out outarg; | |
1571 | int err; | |
1572 | ||
b111c8c0 | 1573 | req = fuse_get_req_nopages(fc); |
71421259 MS |
1574 | if (IS_ERR(req)) |
1575 | return PTR_ERR(req); | |
1576 | ||
a9ff4f87 | 1577 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
71421259 MS |
1578 | req->out.numargs = 1; |
1579 | req->out.args[0].size = sizeof(outarg); | |
1580 | req->out.args[0].value = &outarg; | |
b93f858a | 1581 | fuse_request_send(fc, req); |
71421259 MS |
1582 | err = req->out.h.error; |
1583 | fuse_put_request(fc, req); | |
1584 | if (!err) | |
1585 | err = convert_fuse_file_lock(&outarg.lk, fl); | |
1586 | ||
1587 | return err; | |
1588 | } | |
1589 | ||
a9ff4f87 | 1590 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 1591 | { |
6131ffaa | 1592 | struct inode *inode = file_inode(file); |
71421259 MS |
1593 | struct fuse_conn *fc = get_fuse_conn(inode); |
1594 | struct fuse_req *req; | |
1595 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; | |
1596 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; | |
1597 | int err; | |
1598 | ||
8fb47a4f | 1599 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) { |
48e90761 MS |
1600 | /* NLM needs asynchronous locks, which we don't support yet */ |
1601 | return -ENOLCK; | |
1602 | } | |
1603 | ||
71421259 MS |
1604 | /* Unlock on close is handled by the flush method */ |
1605 | if (fl->fl_flags & FL_CLOSE) | |
1606 | return 0; | |
1607 | ||
b111c8c0 | 1608 | req = fuse_get_req_nopages(fc); |
71421259 MS |
1609 | if (IS_ERR(req)) |
1610 | return PTR_ERR(req); | |
1611 | ||
a9ff4f87 | 1612 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
b93f858a | 1613 | fuse_request_send(fc, req); |
71421259 | 1614 | err = req->out.h.error; |
a4d27e75 MS |
1615 | /* locking is restartable */ |
1616 | if (err == -EINTR) | |
1617 | err = -ERESTARTSYS; | |
71421259 MS |
1618 | fuse_put_request(fc, req); |
1619 | return err; | |
1620 | } | |
1621 | ||
1622 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
1623 | { | |
6131ffaa | 1624 | struct inode *inode = file_inode(file); |
71421259 MS |
1625 | struct fuse_conn *fc = get_fuse_conn(inode); |
1626 | int err; | |
1627 | ||
48e90761 MS |
1628 | if (cmd == F_CANCELLK) { |
1629 | err = 0; | |
1630 | } else if (cmd == F_GETLK) { | |
71421259 | 1631 | if (fc->no_lock) { |
9d6a8c5c | 1632 | posix_test_lock(file, fl); |
71421259 MS |
1633 | err = 0; |
1634 | } else | |
1635 | err = fuse_getlk(file, fl); | |
1636 | } else { | |
1637 | if (fc->no_lock) | |
48e90761 | 1638 | err = posix_lock_file(file, fl, NULL); |
71421259 | 1639 | else |
a9ff4f87 | 1640 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
1641 | } |
1642 | return err; | |
1643 | } | |
1644 | ||
a9ff4f87 MS |
1645 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
1646 | { | |
6131ffaa | 1647 | struct inode *inode = file_inode(file); |
a9ff4f87 MS |
1648 | struct fuse_conn *fc = get_fuse_conn(inode); |
1649 | int err; | |
1650 | ||
37fb3a30 | 1651 | if (fc->no_flock) { |
a9ff4f87 MS |
1652 | err = flock_lock_file_wait(file, fl); |
1653 | } else { | |
37fb3a30 MS |
1654 | struct fuse_file *ff = file->private_data; |
1655 | ||
a9ff4f87 MS |
1656 | /* emulate flock with POSIX locks */ |
1657 | fl->fl_owner = (fl_owner_t) file; | |
37fb3a30 | 1658 | ff->flock = true; |
a9ff4f87 MS |
1659 | err = fuse_setlk(file, fl, 1); |
1660 | } | |
1661 | ||
1662 | return err; | |
1663 | } | |
1664 | ||
b2d2272f MS |
1665 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
1666 | { | |
1667 | struct inode *inode = mapping->host; | |
1668 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1669 | struct fuse_req *req; | |
1670 | struct fuse_bmap_in inarg; | |
1671 | struct fuse_bmap_out outarg; | |
1672 | int err; | |
1673 | ||
1674 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
1675 | return 0; | |
1676 | ||
b111c8c0 | 1677 | req = fuse_get_req_nopages(fc); |
b2d2272f MS |
1678 | if (IS_ERR(req)) |
1679 | return 0; | |
1680 | ||
1681 | memset(&inarg, 0, sizeof(inarg)); | |
1682 | inarg.block = block; | |
1683 | inarg.blocksize = inode->i_sb->s_blocksize; | |
1684 | req->in.h.opcode = FUSE_BMAP; | |
1685 | req->in.h.nodeid = get_node_id(inode); | |
1686 | req->in.numargs = 1; | |
1687 | req->in.args[0].size = sizeof(inarg); | |
1688 | req->in.args[0].value = &inarg; | |
1689 | req->out.numargs = 1; | |
1690 | req->out.args[0].size = sizeof(outarg); | |
1691 | req->out.args[0].value = &outarg; | |
b93f858a | 1692 | fuse_request_send(fc, req); |
b2d2272f MS |
1693 | err = req->out.h.error; |
1694 | fuse_put_request(fc, req); | |
1695 | if (err == -ENOSYS) | |
1696 | fc->no_bmap = 1; | |
1697 | ||
1698 | return err ? 0 : outarg.block; | |
1699 | } | |
1700 | ||
965c8e59 | 1701 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) |
5559b8f4 MS |
1702 | { |
1703 | loff_t retval; | |
6131ffaa | 1704 | struct inode *inode = file_inode(file); |
5559b8f4 | 1705 | |
c07c3d19 | 1706 | /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ |
965c8e59 AM |
1707 | if (whence == SEEK_CUR || whence == SEEK_SET) |
1708 | return generic_file_llseek(file, offset, whence); | |
06222e49 | 1709 | |
c07c3d19 MS |
1710 | mutex_lock(&inode->i_mutex); |
1711 | retval = fuse_update_attributes(inode, NULL, file, NULL); | |
1712 | if (!retval) | |
965c8e59 | 1713 | retval = generic_file_llseek(file, offset, whence); |
5559b8f4 | 1714 | mutex_unlock(&inode->i_mutex); |
c07c3d19 | 1715 | |
5559b8f4 MS |
1716 | return retval; |
1717 | } | |
1718 | ||
59efec7b TH |
1719 | static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov, |
1720 | unsigned int nr_segs, size_t bytes, bool to_user) | |
1721 | { | |
1722 | struct iov_iter ii; | |
1723 | int page_idx = 0; | |
1724 | ||
1725 | if (!bytes) | |
1726 | return 0; | |
1727 | ||
1728 | iov_iter_init(&ii, iov, nr_segs, bytes, 0); | |
1729 | ||
1730 | while (iov_iter_count(&ii)) { | |
1731 | struct page *page = pages[page_idx++]; | |
1732 | size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii)); | |
4aa0edd2 | 1733 | void *kaddr; |
59efec7b | 1734 | |
4aa0edd2 | 1735 | kaddr = kmap(page); |
59efec7b TH |
1736 | |
1737 | while (todo) { | |
1738 | char __user *uaddr = ii.iov->iov_base + ii.iov_offset; | |
1739 | size_t iov_len = ii.iov->iov_len - ii.iov_offset; | |
1740 | size_t copy = min(todo, iov_len); | |
1741 | size_t left; | |
1742 | ||
1743 | if (!to_user) | |
1744 | left = copy_from_user(kaddr, uaddr, copy); | |
1745 | else | |
1746 | left = copy_to_user(uaddr, kaddr, copy); | |
1747 | ||
1748 | if (unlikely(left)) | |
1749 | return -EFAULT; | |
1750 | ||
1751 | iov_iter_advance(&ii, copy); | |
1752 | todo -= copy; | |
1753 | kaddr += copy; | |
1754 | } | |
1755 | ||
0bd87182 | 1756 | kunmap(page); |
59efec7b TH |
1757 | } |
1758 | ||
1759 | return 0; | |
1760 | } | |
1761 | ||
d9d318d3 MS |
1762 | /* |
1763 | * CUSE servers compiled on 32bit broke on 64bit kernels because the | |
1764 | * ABI was defined to be 'struct iovec' which is different on 32bit | |
1765 | * and 64bit. Fortunately we can determine which structure the server | |
1766 | * used from the size of the reply. | |
1767 | */ | |
1baa26b2 MS |
1768 | static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, |
1769 | size_t transferred, unsigned count, | |
1770 | bool is_compat) | |
d9d318d3 MS |
1771 | { |
1772 | #ifdef CONFIG_COMPAT | |
1773 | if (count * sizeof(struct compat_iovec) == transferred) { | |
1774 | struct compat_iovec *ciov = src; | |
1775 | unsigned i; | |
1776 | ||
1777 | /* | |
1778 | * With this interface a 32bit server cannot support | |
1779 | * non-compat (i.e. ones coming from 64bit apps) ioctl | |
1780 | * requests | |
1781 | */ | |
1782 | if (!is_compat) | |
1783 | return -EINVAL; | |
1784 | ||
1785 | for (i = 0; i < count; i++) { | |
1786 | dst[i].iov_base = compat_ptr(ciov[i].iov_base); | |
1787 | dst[i].iov_len = ciov[i].iov_len; | |
1788 | } | |
1789 | return 0; | |
1790 | } | |
1791 | #endif | |
1792 | ||
1793 | if (count * sizeof(struct iovec) != transferred) | |
1794 | return -EIO; | |
1795 | ||
1796 | memcpy(dst, src, transferred); | |
1797 | return 0; | |
1798 | } | |
1799 | ||
7572777e MS |
1800 | /* Make sure iov_length() won't overflow */ |
1801 | static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count) | |
1802 | { | |
1803 | size_t n; | |
1804 | u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT; | |
1805 | ||
fb6ccff6 | 1806 | for (n = 0; n < count; n++, iov++) { |
7572777e MS |
1807 | if (iov->iov_len > (size_t) max) |
1808 | return -ENOMEM; | |
1809 | max -= iov->iov_len; | |
1810 | } | |
1811 | return 0; | |
1812 | } | |
1813 | ||
1baa26b2 MS |
1814 | static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, |
1815 | void *src, size_t transferred, unsigned count, | |
1816 | bool is_compat) | |
1817 | { | |
1818 | unsigned i; | |
1819 | struct fuse_ioctl_iovec *fiov = src; | |
1820 | ||
1821 | if (fc->minor < 16) { | |
1822 | return fuse_copy_ioctl_iovec_old(dst, src, transferred, | |
1823 | count, is_compat); | |
1824 | } | |
1825 | ||
1826 | if (count * sizeof(struct fuse_ioctl_iovec) != transferred) | |
1827 | return -EIO; | |
1828 | ||
1829 | for (i = 0; i < count; i++) { | |
1830 | /* Did the server supply an inappropriate value? */ | |
1831 | if (fiov[i].base != (unsigned long) fiov[i].base || | |
1832 | fiov[i].len != (unsigned long) fiov[i].len) | |
1833 | return -EIO; | |
1834 | ||
1835 | dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; | |
1836 | dst[i].iov_len = (size_t) fiov[i].len; | |
1837 | ||
1838 | #ifdef CONFIG_COMPAT | |
1839 | if (is_compat && | |
1840 | (ptr_to_compat(dst[i].iov_base) != fiov[i].base || | |
1841 | (compat_size_t) dst[i].iov_len != fiov[i].len)) | |
1842 | return -EIO; | |
1843 | #endif | |
1844 | } | |
1845 | ||
1846 | return 0; | |
1847 | } | |
1848 | ||
1849 | ||
59efec7b TH |
1850 | /* |
1851 | * For ioctls, there is no generic way to determine how much memory | |
1852 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
1853 | * to dereference the passed pointer, so the parameter requires deep | |
1854 | * copying but FUSE has no idea whatsoever about what to copy in or | |
1855 | * out. | |
1856 | * | |
1857 | * This is solved by allowing FUSE server to retry ioctl with | |
1858 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
1859 | * needs to read in the following structure. | |
1860 | * | |
1861 | * struct a { | |
1862 | * char *buf; | |
1863 | * size_t buflen; | |
1864 | * } | |
1865 | * | |
1866 | * On the first callout to FUSE server, inarg->in_size and | |
1867 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
1868 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
1869 | * the actual iov array to | |
1870 | * | |
1871 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
1872 | * | |
1873 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
1874 | * On the second round, the server has access to the structure and | |
1875 | * from that it can tell what to look for next, so on the invocation, | |
1876 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
1877 | * | |
1878 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
1879 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
1880 | * | |
1881 | * FUSE will copy both struct a and the pointed buffer from the | |
1882 | * process doing the ioctl and retry ioctl with both struct a and the | |
1883 | * buffer. | |
1884 | * | |
1885 | * This time, FUSE server has everything it needs and completes ioctl | |
1886 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
1887 | * | |
1888 | * Copying data out works the same way. | |
1889 | * | |
1890 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
1891 | * automatically initializes in and out iovs by decoding @cmd with | |
1892 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
1893 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
1894 | * behavior for all FUSE servers. | |
1895 | */ | |
08cbf542 TH |
1896 | long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, |
1897 | unsigned int flags) | |
59efec7b | 1898 | { |
59efec7b | 1899 | struct fuse_file *ff = file->private_data; |
d36f2487 | 1900 | struct fuse_conn *fc = ff->fc; |
59efec7b TH |
1901 | struct fuse_ioctl_in inarg = { |
1902 | .fh = ff->fh, | |
1903 | .cmd = cmd, | |
1904 | .arg = arg, | |
1905 | .flags = flags | |
1906 | }; | |
1907 | struct fuse_ioctl_out outarg; | |
1908 | struct fuse_req *req = NULL; | |
1909 | struct page **pages = NULL; | |
8ac83505 | 1910 | struct iovec *iov_page = NULL; |
59efec7b TH |
1911 | struct iovec *in_iov = NULL, *out_iov = NULL; |
1912 | unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages; | |
1913 | size_t in_size, out_size, transferred; | |
1914 | int err; | |
1915 | ||
1baa26b2 MS |
1916 | #if BITS_PER_LONG == 32 |
1917 | inarg.flags |= FUSE_IOCTL_32BIT; | |
1918 | #else | |
1919 | if (flags & FUSE_IOCTL_COMPAT) | |
1920 | inarg.flags |= FUSE_IOCTL_32BIT; | |
1921 | #endif | |
1922 | ||
59efec7b | 1923 | /* assume all the iovs returned by client always fits in a page */ |
1baa26b2 | 1924 | BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); |
59efec7b | 1925 | |
59efec7b | 1926 | err = -ENOMEM; |
c411cc88 | 1927 | pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL); |
8ac83505 | 1928 | iov_page = (struct iovec *) __get_free_page(GFP_KERNEL); |
59efec7b TH |
1929 | if (!pages || !iov_page) |
1930 | goto out; | |
1931 | ||
1932 | /* | |
1933 | * If restricted, initialize IO parameters as encoded in @cmd. | |
1934 | * RETRY from server is not allowed. | |
1935 | */ | |
1936 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
8ac83505 | 1937 | struct iovec *iov = iov_page; |
59efec7b | 1938 | |
c9f0523d | 1939 | iov->iov_base = (void __user *)arg; |
59efec7b TH |
1940 | iov->iov_len = _IOC_SIZE(cmd); |
1941 | ||
1942 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
1943 | in_iov = iov; | |
1944 | in_iovs = 1; | |
1945 | } | |
1946 | ||
1947 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
1948 | out_iov = iov; | |
1949 | out_iovs = 1; | |
1950 | } | |
1951 | } | |
1952 | ||
1953 | retry: | |
1954 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
1955 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
1956 | ||
1957 | /* | |
1958 | * Out data can be used either for actual out data or iovs, | |
1959 | * make sure there always is at least one page. | |
1960 | */ | |
1961 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
1962 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
1963 | ||
1964 | /* make sure there are enough buffer pages and init request with them */ | |
1965 | err = -ENOMEM; | |
1966 | if (max_pages > FUSE_MAX_PAGES_PER_REQ) | |
1967 | goto out; | |
1968 | while (num_pages < max_pages) { | |
1969 | pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
1970 | if (!pages[num_pages]) | |
1971 | goto out; | |
1972 | num_pages++; | |
1973 | } | |
1974 | ||
54b96670 | 1975 | req = fuse_get_req(fc, num_pages); |
59efec7b TH |
1976 | if (IS_ERR(req)) { |
1977 | err = PTR_ERR(req); | |
1978 | req = NULL; | |
1979 | goto out; | |
1980 | } | |
1981 | memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages); | |
1982 | req->num_pages = num_pages; | |
7c190c8b | 1983 | fuse_page_descs_length_init(req, 0, req->num_pages); |
59efec7b TH |
1984 | |
1985 | /* okay, let's send it to the client */ | |
1986 | req->in.h.opcode = FUSE_IOCTL; | |
d36f2487 | 1987 | req->in.h.nodeid = ff->nodeid; |
59efec7b TH |
1988 | req->in.numargs = 1; |
1989 | req->in.args[0].size = sizeof(inarg); | |
1990 | req->in.args[0].value = &inarg; | |
1991 | if (in_size) { | |
1992 | req->in.numargs++; | |
1993 | req->in.args[1].size = in_size; | |
1994 | req->in.argpages = 1; | |
1995 | ||
1996 | err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size, | |
1997 | false); | |
1998 | if (err) | |
1999 | goto out; | |
2000 | } | |
2001 | ||
2002 | req->out.numargs = 2; | |
2003 | req->out.args[0].size = sizeof(outarg); | |
2004 | req->out.args[0].value = &outarg; | |
2005 | req->out.args[1].size = out_size; | |
2006 | req->out.argpages = 1; | |
2007 | req->out.argvar = 1; | |
2008 | ||
b93f858a | 2009 | fuse_request_send(fc, req); |
59efec7b TH |
2010 | err = req->out.h.error; |
2011 | transferred = req->out.args[1].size; | |
2012 | fuse_put_request(fc, req); | |
2013 | req = NULL; | |
2014 | if (err) | |
2015 | goto out; | |
2016 | ||
2017 | /* did it ask for retry? */ | |
2018 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
8ac83505 | 2019 | void *vaddr; |
59efec7b TH |
2020 | |
2021 | /* no retry if in restricted mode */ | |
2022 | err = -EIO; | |
2023 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
2024 | goto out; | |
2025 | ||
2026 | in_iovs = outarg.in_iovs; | |
2027 | out_iovs = outarg.out_iovs; | |
2028 | ||
2029 | /* | |
2030 | * Make sure things are in boundary, separate checks | |
2031 | * are to protect against overflow. | |
2032 | */ | |
2033 | err = -ENOMEM; | |
2034 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
2035 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
2036 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
2037 | goto out; | |
2038 | ||
2408f6ef | 2039 | vaddr = kmap_atomic(pages[0]); |
1baa26b2 | 2040 | err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr, |
d9d318d3 MS |
2041 | transferred, in_iovs + out_iovs, |
2042 | (flags & FUSE_IOCTL_COMPAT) != 0); | |
2408f6ef | 2043 | kunmap_atomic(vaddr); |
d9d318d3 MS |
2044 | if (err) |
2045 | goto out; | |
59efec7b | 2046 | |
8ac83505 | 2047 | in_iov = iov_page; |
59efec7b TH |
2048 | out_iov = in_iov + in_iovs; |
2049 | ||
7572777e MS |
2050 | err = fuse_verify_ioctl_iov(in_iov, in_iovs); |
2051 | if (err) | |
2052 | goto out; | |
2053 | ||
2054 | err = fuse_verify_ioctl_iov(out_iov, out_iovs); | |
2055 | if (err) | |
2056 | goto out; | |
2057 | ||
59efec7b TH |
2058 | goto retry; |
2059 | } | |
2060 | ||
2061 | err = -EIO; | |
2062 | if (transferred > inarg.out_size) | |
2063 | goto out; | |
2064 | ||
2065 | err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true); | |
2066 | out: | |
2067 | if (req) | |
2068 | fuse_put_request(fc, req); | |
8ac83505 | 2069 | free_page((unsigned long) iov_page); |
59efec7b TH |
2070 | while (num_pages) |
2071 | __free_page(pages[--num_pages]); | |
2072 | kfree(pages); | |
2073 | ||
2074 | return err ? err : outarg.result; | |
2075 | } | |
08cbf542 | 2076 | EXPORT_SYMBOL_GPL(fuse_do_ioctl); |
59efec7b | 2077 | |
b18da0c5 MS |
2078 | long fuse_ioctl_common(struct file *file, unsigned int cmd, |
2079 | unsigned long arg, unsigned int flags) | |
d36f2487 | 2080 | { |
6131ffaa | 2081 | struct inode *inode = file_inode(file); |
d36f2487 MS |
2082 | struct fuse_conn *fc = get_fuse_conn(inode); |
2083 | ||
c2132c1b | 2084 | if (!fuse_allow_current_process(fc)) |
d36f2487 MS |
2085 | return -EACCES; |
2086 | ||
2087 | if (is_bad_inode(inode)) | |
2088 | return -EIO; | |
2089 | ||
2090 | return fuse_do_ioctl(file, cmd, arg, flags); | |
2091 | } | |
2092 | ||
59efec7b TH |
2093 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, |
2094 | unsigned long arg) | |
2095 | { | |
b18da0c5 | 2096 | return fuse_ioctl_common(file, cmd, arg, 0); |
59efec7b TH |
2097 | } |
2098 | ||
2099 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
2100 | unsigned long arg) | |
2101 | { | |
b18da0c5 | 2102 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); |
59efec7b TH |
2103 | } |
2104 | ||
95668a69 TH |
2105 | /* |
2106 | * All files which have been polled are linked to RB tree | |
2107 | * fuse_conn->polled_files which is indexed by kh. Walk the tree and | |
2108 | * find the matching one. | |
2109 | */ | |
2110 | static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, | |
2111 | struct rb_node **parent_out) | |
2112 | { | |
2113 | struct rb_node **link = &fc->polled_files.rb_node; | |
2114 | struct rb_node *last = NULL; | |
2115 | ||
2116 | while (*link) { | |
2117 | struct fuse_file *ff; | |
2118 | ||
2119 | last = *link; | |
2120 | ff = rb_entry(last, struct fuse_file, polled_node); | |
2121 | ||
2122 | if (kh < ff->kh) | |
2123 | link = &last->rb_left; | |
2124 | else if (kh > ff->kh) | |
2125 | link = &last->rb_right; | |
2126 | else | |
2127 | return link; | |
2128 | } | |
2129 | ||
2130 | if (parent_out) | |
2131 | *parent_out = last; | |
2132 | return link; | |
2133 | } | |
2134 | ||
2135 | /* | |
2136 | * The file is about to be polled. Make sure it's on the polled_files | |
2137 | * RB tree. Note that files once added to the polled_files tree are | |
2138 | * not removed before the file is released. This is because a file | |
2139 | * polled once is likely to be polled again. | |
2140 | */ | |
2141 | static void fuse_register_polled_file(struct fuse_conn *fc, | |
2142 | struct fuse_file *ff) | |
2143 | { | |
2144 | spin_lock(&fc->lock); | |
2145 | if (RB_EMPTY_NODE(&ff->polled_node)) { | |
2146 | struct rb_node **link, *parent; | |
2147 | ||
2148 | link = fuse_find_polled_node(fc, ff->kh, &parent); | |
2149 | BUG_ON(*link); | |
2150 | rb_link_node(&ff->polled_node, parent, link); | |
2151 | rb_insert_color(&ff->polled_node, &fc->polled_files); | |
2152 | } | |
2153 | spin_unlock(&fc->lock); | |
2154 | } | |
2155 | ||
08cbf542 | 2156 | unsigned fuse_file_poll(struct file *file, poll_table *wait) |
95668a69 | 2157 | { |
95668a69 | 2158 | struct fuse_file *ff = file->private_data; |
797759aa | 2159 | struct fuse_conn *fc = ff->fc; |
95668a69 TH |
2160 | struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; |
2161 | struct fuse_poll_out outarg; | |
2162 | struct fuse_req *req; | |
2163 | int err; | |
2164 | ||
2165 | if (fc->no_poll) | |
2166 | return DEFAULT_POLLMASK; | |
2167 | ||
2168 | poll_wait(file, &ff->poll_wait, wait); | |
0415d291 | 2169 | inarg.events = (__u32)poll_requested_events(wait); |
95668a69 TH |
2170 | |
2171 | /* | |
2172 | * Ask for notification iff there's someone waiting for it. | |
2173 | * The client may ignore the flag and always notify. | |
2174 | */ | |
2175 | if (waitqueue_active(&ff->poll_wait)) { | |
2176 | inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; | |
2177 | fuse_register_polled_file(fc, ff); | |
2178 | } | |
2179 | ||
b111c8c0 | 2180 | req = fuse_get_req_nopages(fc); |
95668a69 | 2181 | if (IS_ERR(req)) |
201fa69a | 2182 | return POLLERR; |
95668a69 TH |
2183 | |
2184 | req->in.h.opcode = FUSE_POLL; | |
797759aa | 2185 | req->in.h.nodeid = ff->nodeid; |
95668a69 TH |
2186 | req->in.numargs = 1; |
2187 | req->in.args[0].size = sizeof(inarg); | |
2188 | req->in.args[0].value = &inarg; | |
2189 | req->out.numargs = 1; | |
2190 | req->out.args[0].size = sizeof(outarg); | |
2191 | req->out.args[0].value = &outarg; | |
b93f858a | 2192 | fuse_request_send(fc, req); |
95668a69 TH |
2193 | err = req->out.h.error; |
2194 | fuse_put_request(fc, req); | |
2195 | ||
2196 | if (!err) | |
2197 | return outarg.revents; | |
2198 | if (err == -ENOSYS) { | |
2199 | fc->no_poll = 1; | |
2200 | return DEFAULT_POLLMASK; | |
2201 | } | |
2202 | return POLLERR; | |
2203 | } | |
08cbf542 | 2204 | EXPORT_SYMBOL_GPL(fuse_file_poll); |
95668a69 TH |
2205 | |
2206 | /* | |
2207 | * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and | |
2208 | * wakes up the poll waiters. | |
2209 | */ | |
2210 | int fuse_notify_poll_wakeup(struct fuse_conn *fc, | |
2211 | struct fuse_notify_poll_wakeup_out *outarg) | |
2212 | { | |
2213 | u64 kh = outarg->kh; | |
2214 | struct rb_node **link; | |
2215 | ||
2216 | spin_lock(&fc->lock); | |
2217 | ||
2218 | link = fuse_find_polled_node(fc, kh, NULL); | |
2219 | if (*link) { | |
2220 | struct fuse_file *ff; | |
2221 | ||
2222 | ff = rb_entry(*link, struct fuse_file, polled_node); | |
2223 | wake_up_interruptible_sync(&ff->poll_wait); | |
2224 | } | |
2225 | ||
2226 | spin_unlock(&fc->lock); | |
2227 | return 0; | |
2228 | } | |
2229 | ||
4273b793 AA |
2230 | static ssize_t |
2231 | fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | |
2232 | loff_t offset, unsigned long nr_segs) | |
2233 | { | |
2234 | ssize_t ret = 0; | |
2235 | struct file *file = NULL; | |
2236 | loff_t pos = 0; | |
2237 | ||
2238 | file = iocb->ki_filp; | |
2239 | pos = offset; | |
2240 | ||
b98d023a MP |
2241 | if (rw == WRITE) |
2242 | ret = __fuse_direct_write(file, iov, nr_segs, &pos); | |
2243 | else | |
2244 | ret = __fuse_direct_read(file, iov, nr_segs, &pos); | |
4273b793 AA |
2245 | |
2246 | return ret; | |
2247 | } | |
2248 | ||
cdadb11c MS |
2249 | static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, |
2250 | loff_t length) | |
05ba1f08 AP |
2251 | { |
2252 | struct fuse_file *ff = file->private_data; | |
2253 | struct fuse_conn *fc = ff->fc; | |
2254 | struct fuse_req *req; | |
2255 | struct fuse_fallocate_in inarg = { | |
2256 | .fh = ff->fh, | |
2257 | .offset = offset, | |
2258 | .length = length, | |
2259 | .mode = mode | |
2260 | }; | |
2261 | int err; | |
2262 | ||
519c6040 MS |
2263 | if (fc->no_fallocate) |
2264 | return -EOPNOTSUPP; | |
2265 | ||
b111c8c0 | 2266 | req = fuse_get_req_nopages(fc); |
05ba1f08 AP |
2267 | if (IS_ERR(req)) |
2268 | return PTR_ERR(req); | |
2269 | ||
2270 | req->in.h.opcode = FUSE_FALLOCATE; | |
2271 | req->in.h.nodeid = ff->nodeid; | |
2272 | req->in.numargs = 1; | |
2273 | req->in.args[0].size = sizeof(inarg); | |
2274 | req->in.args[0].value = &inarg; | |
2275 | fuse_request_send(fc, req); | |
2276 | err = req->out.h.error; | |
519c6040 MS |
2277 | if (err == -ENOSYS) { |
2278 | fc->no_fallocate = 1; | |
2279 | err = -EOPNOTSUPP; | |
2280 | } | |
05ba1f08 AP |
2281 | fuse_put_request(fc, req); |
2282 | ||
2283 | return err; | |
2284 | } | |
05ba1f08 | 2285 | |
4b6f5d20 | 2286 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 2287 | .llseek = fuse_file_llseek, |
543ade1f | 2288 | .read = do_sync_read, |
bcb4be80 | 2289 | .aio_read = fuse_file_aio_read, |
543ade1f | 2290 | .write = do_sync_write, |
ea9b9907 | 2291 | .aio_write = fuse_file_aio_write, |
b6aeaded MS |
2292 | .mmap = fuse_file_mmap, |
2293 | .open = fuse_open, | |
2294 | .flush = fuse_flush, | |
2295 | .release = fuse_release, | |
2296 | .fsync = fuse_fsync, | |
71421259 | 2297 | .lock = fuse_file_lock, |
a9ff4f87 | 2298 | .flock = fuse_file_flock, |
5ffc4ef4 | 2299 | .splice_read = generic_file_splice_read, |
59efec7b TH |
2300 | .unlocked_ioctl = fuse_file_ioctl, |
2301 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 2302 | .poll = fuse_file_poll, |
05ba1f08 | 2303 | .fallocate = fuse_file_fallocate, |
b6aeaded MS |
2304 | }; |
2305 | ||
4b6f5d20 | 2306 | static const struct file_operations fuse_direct_io_file_operations = { |
5559b8f4 | 2307 | .llseek = fuse_file_llseek, |
413ef8cb MS |
2308 | .read = fuse_direct_read, |
2309 | .write = fuse_direct_write, | |
fc280c96 | 2310 | .mmap = fuse_direct_mmap, |
413ef8cb MS |
2311 | .open = fuse_open, |
2312 | .flush = fuse_flush, | |
2313 | .release = fuse_release, | |
2314 | .fsync = fuse_fsync, | |
71421259 | 2315 | .lock = fuse_file_lock, |
a9ff4f87 | 2316 | .flock = fuse_file_flock, |
59efec7b TH |
2317 | .unlocked_ioctl = fuse_file_ioctl, |
2318 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 2319 | .poll = fuse_file_poll, |
05ba1f08 | 2320 | .fallocate = fuse_file_fallocate, |
fc280c96 | 2321 | /* no splice_read */ |
413ef8cb MS |
2322 | }; |
2323 | ||
f5e54d6e | 2324 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 2325 | .readpage = fuse_readpage, |
3be5a52b MS |
2326 | .writepage = fuse_writepage, |
2327 | .launder_page = fuse_launder_page, | |
db50b96c | 2328 | .readpages = fuse_readpages, |
3be5a52b | 2329 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 2330 | .bmap = fuse_bmap, |
4273b793 | 2331 | .direct_IO = fuse_direct_IO, |
b6aeaded MS |
2332 | }; |
2333 | ||
2334 | void fuse_init_file_inode(struct inode *inode) | |
2335 | { | |
45323fb7 MS |
2336 | inode->i_fop = &fuse_file_operations; |
2337 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 2338 | } |