]>
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> |
7a36094d | 15 | #include <linux/sched/signal.h> |
08cbf542 | 16 | #include <linux/module.h> |
d9d318d3 | 17 | #include <linux/compat.h> |
478e0841 | 18 | #include <linux/swap.h> |
3634a632 | 19 | #include <linux/falloc.h> |
e2e40f2c | 20 | #include <linux/uio.h> |
b6aeaded | 21 | |
4c4f03f7 MS |
22 | struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags, |
23 | struct fuse_page_desc **desc) | |
24 | { | |
25 | struct page **pages; | |
26 | ||
27 | pages = kzalloc(npages * (sizeof(struct page *) + | |
28 | sizeof(struct fuse_page_desc)), flags); | |
29 | *desc = (void *) (pages + npages); | |
30 | ||
31 | return pages; | |
32 | } | |
33 | ||
91fe96b4 MS |
34 | static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
35 | int opcode, struct fuse_open_out *outargp) | |
b6aeaded | 36 | { |
b6aeaded | 37 | struct fuse_open_in inarg; |
7078187a | 38 | FUSE_ARGS(args); |
fd72faac MS |
39 | |
40 | memset(&inarg, 0, sizeof(inarg)); | |
6ff958ed MS |
41 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
42 | if (!fc->atomic_o_trunc) | |
43 | inarg.flags &= ~O_TRUNC; | |
d5b48543 MS |
44 | args.opcode = opcode; |
45 | args.nodeid = nodeid; | |
46 | args.in_numargs = 1; | |
47 | args.in_args[0].size = sizeof(inarg); | |
48 | args.in_args[0].value = &inarg; | |
49 | args.out_numargs = 1; | |
50 | args.out_args[0].size = sizeof(*outargp); | |
51 | args.out_args[0].value = outargp; | |
fd72faac | 52 | |
7078187a | 53 | return fuse_simple_request(fc, &args); |
fd72faac MS |
54 | } |
55 | ||
acf99433 | 56 | struct fuse_file *fuse_file_alloc(struct fuse_conn *fc) |
fd72faac MS |
57 | { |
58 | struct fuse_file *ff; | |
6b2db28a | 59 | |
68227c03 | 60 | ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL); |
6b2db28a TH |
61 | if (unlikely(!ff)) |
62 | return NULL; | |
63 | ||
da5e4714 | 64 | ff->fc = fc; |
4250c066 | 65 | ff->reserved_req = fuse_request_alloc(0); |
6b2db28a TH |
66 | if (unlikely(!ff->reserved_req)) { |
67 | kfree(ff); | |
68 | return NULL; | |
fd72faac | 69 | } |
6b2db28a TH |
70 | |
71 | INIT_LIST_HEAD(&ff->write_entry); | |
5d7bc7e8 | 72 | mutex_init(&ff->readdir.lock); |
4e8c2eb5 | 73 | refcount_set(&ff->count, 1); |
6b2db28a TH |
74 | RB_CLEAR_NODE(&ff->polled_node); |
75 | init_waitqueue_head(&ff->poll_wait); | |
76 | ||
75126f55 | 77 | ff->kh = atomic64_inc_return(&fc->khctr); |
6b2db28a | 78 | |
fd72faac MS |
79 | return ff; |
80 | } | |
81 | ||
82 | void fuse_file_free(struct fuse_file *ff) | |
83 | { | |
33649c91 | 84 | fuse_request_free(ff->reserved_req); |
5d7bc7e8 | 85 | mutex_destroy(&ff->readdir.lock); |
fd72faac MS |
86 | kfree(ff); |
87 | } | |
88 | ||
267d8444 | 89 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) |
c756e0a4 | 90 | { |
4e8c2eb5 | 91 | refcount_inc(&ff->count); |
c756e0a4 MS |
92 | return ff; |
93 | } | |
94 | ||
819c4b3b MS |
95 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
96 | { | |
baebccbe | 97 | iput(req->misc.release.inode); |
819c4b3b MS |
98 | } |
99 | ||
2e64ff15 | 100 | static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir) |
c756e0a4 | 101 | { |
4e8c2eb5 | 102 | if (refcount_dec_and_test(&ff->count)) { |
c756e0a4 | 103 | struct fuse_req *req = ff->reserved_req; |
8b0797a4 | 104 | |
d9a9ea94 | 105 | if (isdir ? ff->fc->no_opendir : ff->fc->no_open) { |
7678ac50 AG |
106 | /* |
107 | * Drop the release request when client does not | |
108 | * implement 'open' | |
109 | */ | |
825d6d33 | 110 | __clear_bit(FR_BACKGROUND, &req->flags); |
baebccbe | 111 | iput(req->misc.release.inode); |
7678ac50 AG |
112 | fuse_put_request(ff->fc, req); |
113 | } else if (sync) { | |
2e38bea9 | 114 | __set_bit(FR_FORCE, &req->flags); |
825d6d33 | 115 | __clear_bit(FR_BACKGROUND, &req->flags); |
5a18ec17 | 116 | fuse_request_send(ff->fc, req); |
baebccbe | 117 | iput(req->misc.release.inode); |
5a18ec17 MS |
118 | fuse_put_request(ff->fc, req); |
119 | } else { | |
120 | req->end = fuse_release_end; | |
825d6d33 | 121 | __set_bit(FR_BACKGROUND, &req->flags); |
5a18ec17 MS |
122 | fuse_request_send_background(ff->fc, req); |
123 | } | |
c756e0a4 MS |
124 | kfree(ff); |
125 | } | |
126 | } | |
127 | ||
08cbf542 TH |
128 | int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file, |
129 | bool isdir) | |
91fe96b4 | 130 | { |
91fe96b4 | 131 | struct fuse_file *ff; |
91fe96b4 MS |
132 | int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
133 | ||
134 | ff = fuse_file_alloc(fc); | |
135 | if (!ff) | |
136 | return -ENOMEM; | |
137 | ||
7678ac50 | 138 | ff->fh = 0; |
fabf7e02 CA |
139 | /* Default for no-open */ |
140 | ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0); | |
d9a9ea94 | 141 | if (isdir ? !fc->no_opendir : !fc->no_open) { |
7678ac50 AG |
142 | struct fuse_open_out outarg; |
143 | int err; | |
144 | ||
145 | err = fuse_send_open(fc, nodeid, file, opcode, &outarg); | |
146 | if (!err) { | |
147 | ff->fh = outarg.fh; | |
148 | ff->open_flags = outarg.open_flags; | |
149 | ||
d9a9ea94 | 150 | } else if (err != -ENOSYS) { |
7678ac50 AG |
151 | fuse_file_free(ff); |
152 | return err; | |
153 | } else { | |
d9a9ea94 CA |
154 | if (isdir) |
155 | fc->no_opendir = 1; | |
156 | else | |
157 | fc->no_open = 1; | |
7678ac50 | 158 | } |
91fe96b4 MS |
159 | } |
160 | ||
161 | if (isdir) | |
7678ac50 | 162 | ff->open_flags &= ~FOPEN_DIRECT_IO; |
91fe96b4 | 163 | |
91fe96b4 | 164 | ff->nodeid = nodeid; |
267d8444 | 165 | file->private_data = ff; |
91fe96b4 MS |
166 | |
167 | return 0; | |
168 | } | |
08cbf542 | 169 | EXPORT_SYMBOL_GPL(fuse_do_open); |
91fe96b4 | 170 | |
650b22b9 PE |
171 | static void fuse_link_write_file(struct file *file) |
172 | { | |
173 | struct inode *inode = file_inode(file); | |
650b22b9 PE |
174 | struct fuse_inode *fi = get_fuse_inode(inode); |
175 | struct fuse_file *ff = file->private_data; | |
176 | /* | |
177 | * file may be written through mmap, so chain it onto the | |
178 | * inodes's write_file list | |
179 | */ | |
f15ecfef | 180 | spin_lock(&fi->lock); |
650b22b9 PE |
181 | if (list_empty(&ff->write_entry)) |
182 | list_add(&ff->write_entry, &fi->write_files); | |
f15ecfef | 183 | spin_unlock(&fi->lock); |
650b22b9 PE |
184 | } |
185 | ||
c7b7143c | 186 | void fuse_finish_open(struct inode *inode, struct file *file) |
fd72faac | 187 | { |
c7b7143c | 188 | struct fuse_file *ff = file->private_data; |
a0822c55 | 189 | struct fuse_conn *fc = get_fuse_conn(inode); |
c7b7143c | 190 | |
c7b7143c | 191 | if (!(ff->open_flags & FOPEN_KEEP_CACHE)) |
b1009979 | 192 | invalidate_inode_pages2(inode->i_mapping); |
bbd84f33 KS |
193 | if (ff->open_flags & FOPEN_STREAM) |
194 | stream_open(inode, file); | |
195 | else if (ff->open_flags & FOPEN_NONSEEKABLE) | |
a7c1b990 | 196 | nonseekable_open(inode, file); |
a0822c55 KS |
197 | if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) { |
198 | struct fuse_inode *fi = get_fuse_inode(inode); | |
199 | ||
f15ecfef | 200 | spin_lock(&fi->lock); |
4510d86f | 201 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
a0822c55 | 202 | i_size_write(inode, 0); |
f15ecfef | 203 | spin_unlock(&fi->lock); |
a0822c55 | 204 | fuse_invalidate_attr(inode); |
75caeecd MP |
205 | if (fc->writeback_cache) |
206 | file_update_time(file); | |
a0822c55 | 207 | } |
4d99ff8f PE |
208 | if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache) |
209 | fuse_link_write_file(file); | |
fd72faac MS |
210 | } |
211 | ||
91fe96b4 | 212 | int fuse_open_common(struct inode *inode, struct file *file, bool isdir) |
fd72faac | 213 | { |
acf99433 | 214 | struct fuse_conn *fc = get_fuse_conn(inode); |
b6aeaded | 215 | int err; |
75caeecd MP |
216 | bool lock_inode = (file->f_flags & O_TRUNC) && |
217 | fc->atomic_o_trunc && | |
218 | fc->writeback_cache; | |
b6aeaded MS |
219 | |
220 | err = generic_file_open(inode, file); | |
221 | if (err) | |
222 | return err; | |
223 | ||
75caeecd | 224 | if (lock_inode) |
5955102c | 225 | inode_lock(inode); |
75caeecd | 226 | |
91fe96b4 | 227 | err = fuse_do_open(fc, get_node_id(inode), file, isdir); |
b6aeaded | 228 | |
75caeecd MP |
229 | if (!err) |
230 | fuse_finish_open(inode, file); | |
91fe96b4 | 231 | |
75caeecd | 232 | if (lock_inode) |
5955102c | 233 | inode_unlock(inode); |
75caeecd MP |
234 | |
235 | return err; | |
b6aeaded MS |
236 | } |
237 | ||
ebf84d0c KT |
238 | static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff, |
239 | int flags, int opcode) | |
64c6d8ed | 240 | { |
8b0797a4 | 241 | struct fuse_conn *fc = ff->fc; |
33649c91 | 242 | struct fuse_req *req = ff->reserved_req; |
b57d4264 | 243 | struct fuse_release_in *inarg = &req->misc.release.in; |
b6aeaded | 244 | |
f15ecfef KT |
245 | /* Inode is NULL on error path of fuse_create_open() */ |
246 | if (likely(fi)) { | |
247 | spin_lock(&fi->lock); | |
248 | list_del(&ff->write_entry); | |
249 | spin_unlock(&fi->lock); | |
250 | } | |
8b0797a4 | 251 | spin_lock(&fc->lock); |
8b0797a4 MS |
252 | if (!RB_EMPTY_NODE(&ff->polled_node)) |
253 | rb_erase(&ff->polled_node, &fc->polled_files); | |
254 | spin_unlock(&fc->lock); | |
255 | ||
357ccf2b | 256 | wake_up_interruptible_all(&ff->poll_wait); |
8b0797a4 | 257 | |
b6aeaded | 258 | inarg->fh = ff->fh; |
fd72faac | 259 | inarg->flags = flags; |
51eb01e7 | 260 | req->in.h.opcode = opcode; |
c7b7143c | 261 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
262 | req->in.numargs = 1; |
263 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
264 | req->in.args[0].value = inarg; | |
fd72faac MS |
265 | } |
266 | ||
2e64ff15 | 267 | void fuse_release_common(struct file *file, bool isdir) |
fd72faac | 268 | { |
ebf84d0c | 269 | struct fuse_inode *fi = get_fuse_inode(file_inode(file)); |
9a87ad3d MS |
270 | struct fuse_file *ff = file->private_data; |
271 | struct fuse_req *req = ff->reserved_req; | |
2e64ff15 | 272 | int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; |
6b2db28a | 273 | |
ebf84d0c | 274 | fuse_prepare_release(fi, ff, file->f_flags, opcode); |
6b2db28a | 275 | |
37fb3a30 MS |
276 | if (ff->flock) { |
277 | struct fuse_release_in *inarg = &req->misc.release.in; | |
278 | inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK; | |
279 | inarg->lock_owner = fuse_lock_owner_id(ff->fc, | |
280 | (fl_owner_t) file); | |
281 | } | |
baebccbe MS |
282 | /* Hold inode until release is finished */ |
283 | req->misc.release.inode = igrab(file_inode(file)); | |
6b2db28a | 284 | |
6b2db28a TH |
285 | /* |
286 | * Normally this will send the RELEASE request, however if | |
287 | * some asynchronous READ or WRITE requests are outstanding, | |
288 | * the sending will be delayed. | |
5a18ec17 MS |
289 | * |
290 | * Make the release synchronous if this is a fuseblk mount, | |
291 | * synchronous RELEASE is allowed (and desirable) in this case | |
292 | * because the server can be trusted not to screw up. | |
6b2db28a | 293 | */ |
1ccd1ea2 | 294 | fuse_file_put(ff, ff->fc->destroy, isdir); |
b6aeaded MS |
295 | } |
296 | ||
04730fef MS |
297 | static int fuse_open(struct inode *inode, struct file *file) |
298 | { | |
91fe96b4 | 299 | return fuse_open_common(inode, file, false); |
04730fef MS |
300 | } |
301 | ||
302 | static int fuse_release(struct inode *inode, struct file *file) | |
303 | { | |
e7cc133c PE |
304 | struct fuse_conn *fc = get_fuse_conn(inode); |
305 | ||
306 | /* see fuse_vma_close() for !writeback_cache case */ | |
307 | if (fc->writeback_cache) | |
1e18bda8 | 308 | write_inode_now(inode, 1); |
b0aa7606 | 309 | |
2e64ff15 | 310 | fuse_release_common(file, false); |
8b0797a4 MS |
311 | |
312 | /* return value is ignored by VFS */ | |
313 | return 0; | |
314 | } | |
315 | ||
ebf84d0c | 316 | void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags) |
8b0797a4 | 317 | { |
4e8c2eb5 | 318 | WARN_ON(refcount_read(&ff->count) > 1); |
ebf84d0c | 319 | fuse_prepare_release(fi, ff, flags, FUSE_RELEASE); |
267d8444 MS |
320 | /* |
321 | * iput(NULL) is a no-op and since the refcount is 1 and everything's | |
322 | * synchronous, we are fine with not doing igrab() here" | |
323 | */ | |
2e64ff15 | 324 | fuse_file_put(ff, true, false); |
04730fef | 325 | } |
08cbf542 | 326 | EXPORT_SYMBOL_GPL(fuse_sync_release); |
04730fef | 327 | |
71421259 | 328 | /* |
9c8ef561 MS |
329 | * Scramble the ID space with XTEA, so that the value of the files_struct |
330 | * pointer is not exposed to userspace. | |
71421259 | 331 | */ |
f3332114 | 332 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 333 | { |
9c8ef561 MS |
334 | u32 *k = fc->scramble_key; |
335 | u64 v = (unsigned long) id; | |
336 | u32 v0 = v; | |
337 | u32 v1 = v >> 32; | |
338 | u32 sum = 0; | |
339 | int i; | |
340 | ||
341 | for (i = 0; i < 32; i++) { | |
342 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
343 | sum += 0x9E3779B9; | |
344 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
345 | } | |
346 | ||
347 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
348 | } |
349 | ||
2fe93bd4 MS |
350 | static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi, |
351 | pgoff_t idx_from, pgoff_t idx_to) | |
352 | { | |
353 | struct fuse_req *req; | |
354 | ||
355 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | |
356 | pgoff_t curr_index; | |
357 | ||
358 | WARN_ON(get_fuse_inode(req->inode) != fi); | |
359 | curr_index = req->misc.write.in.offset >> PAGE_SHIFT; | |
360 | if (idx_from < curr_index + req->num_pages && | |
361 | curr_index <= idx_to) { | |
362 | return req; | |
363 | } | |
364 | } | |
365 | return NULL; | |
366 | } | |
367 | ||
3be5a52b | 368 | /* |
ea8cd333 | 369 | * Check if any page in a range is under writeback |
3be5a52b MS |
370 | * |
371 | * This is currently done by walking the list of writepage requests | |
372 | * for the inode, which can be pretty inefficient. | |
373 | */ | |
ea8cd333 PE |
374 | static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from, |
375 | pgoff_t idx_to) | |
3be5a52b | 376 | { |
3be5a52b | 377 | struct fuse_inode *fi = get_fuse_inode(inode); |
2fe93bd4 | 378 | bool found; |
3be5a52b | 379 | |
f15ecfef | 380 | spin_lock(&fi->lock); |
2fe93bd4 | 381 | found = fuse_find_writeback(fi, idx_from, idx_to); |
f15ecfef | 382 | spin_unlock(&fi->lock); |
3be5a52b MS |
383 | |
384 | return found; | |
385 | } | |
386 | ||
ea8cd333 PE |
387 | static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) |
388 | { | |
389 | return fuse_range_is_writeback(inode, index, index); | |
390 | } | |
391 | ||
3be5a52b MS |
392 | /* |
393 | * Wait for page writeback to be completed. | |
394 | * | |
395 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
396 | * use some other means. | |
397 | */ | |
17b2cbe2 | 398 | static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) |
3be5a52b MS |
399 | { |
400 | struct fuse_inode *fi = get_fuse_inode(inode); | |
401 | ||
402 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
3be5a52b MS |
403 | } |
404 | ||
fe38d7df MP |
405 | /* |
406 | * Wait for all pending writepages on the inode to finish. | |
407 | * | |
408 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
409 | * and waiting for all sent writes to complete. | |
410 | * | |
411 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
412 | * could conflict with truncation. | |
413 | */ | |
414 | static void fuse_sync_writes(struct inode *inode) | |
415 | { | |
416 | fuse_set_nowrite(inode); | |
417 | fuse_release_nowrite(inode); | |
418 | } | |
419 | ||
75e1fcc0 | 420 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 421 | { |
6131ffaa | 422 | struct inode *inode = file_inode(file); |
b6aeaded MS |
423 | struct fuse_conn *fc = get_fuse_conn(inode); |
424 | struct fuse_file *ff = file->private_data; | |
b6aeaded | 425 | struct fuse_flush_in inarg; |
c500ebaa | 426 | FUSE_ARGS(args); |
b6aeaded MS |
427 | int err; |
428 | ||
248d86e8 MS |
429 | if (is_bad_inode(inode)) |
430 | return -EIO; | |
431 | ||
b6aeaded MS |
432 | if (fc->no_flush) |
433 | return 0; | |
434 | ||
1e18bda8 | 435 | err = write_inode_now(inode, 1); |
fe38d7df MP |
436 | if (err) |
437 | return err; | |
438 | ||
5955102c | 439 | inode_lock(inode); |
fe38d7df | 440 | fuse_sync_writes(inode); |
5955102c | 441 | inode_unlock(inode); |
fe38d7df | 442 | |
4a7f4e88 | 443 | err = filemap_check_errors(file->f_mapping); |
9ebce595 MP |
444 | if (err) |
445 | return err; | |
446 | ||
b6aeaded MS |
447 | memset(&inarg, 0, sizeof(inarg)); |
448 | inarg.fh = ff->fh; | |
9c8ef561 | 449 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
c500ebaa MS |
450 | args.opcode = FUSE_FLUSH; |
451 | args.nodeid = get_node_id(inode); | |
452 | args.in_numargs = 1; | |
453 | args.in_args[0].size = sizeof(inarg); | |
454 | args.in_args[0].value = &inarg; | |
455 | args.force = true; | |
456 | ||
457 | err = fuse_simple_request(fc, &args); | |
b6aeaded MS |
458 | if (err == -ENOSYS) { |
459 | fc->no_flush = 1; | |
460 | err = 0; | |
461 | } | |
462 | return err; | |
463 | } | |
464 | ||
02c24a82 | 465 | int fuse_fsync_common(struct file *file, loff_t start, loff_t end, |
a9c2d1e8 | 466 | int datasync, int opcode) |
b6aeaded | 467 | { |
7ea80859 | 468 | struct inode *inode = file->f_mapping->host; |
b6aeaded MS |
469 | struct fuse_conn *fc = get_fuse_conn(inode); |
470 | struct fuse_file *ff = file->private_data; | |
7078187a | 471 | FUSE_ARGS(args); |
b6aeaded | 472 | struct fuse_fsync_in inarg; |
a9c2d1e8 MS |
473 | |
474 | memset(&inarg, 0, sizeof(inarg)); | |
475 | inarg.fh = ff->fh; | |
154603fe | 476 | inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0; |
d5b48543 MS |
477 | args.opcode = opcode; |
478 | args.nodeid = get_node_id(inode); | |
479 | args.in_numargs = 1; | |
480 | args.in_args[0].size = sizeof(inarg); | |
481 | args.in_args[0].value = &inarg; | |
a9c2d1e8 MS |
482 | return fuse_simple_request(fc, &args); |
483 | } | |
484 | ||
485 | static int fuse_fsync(struct file *file, loff_t start, loff_t end, | |
486 | int datasync) | |
487 | { | |
488 | struct inode *inode = file->f_mapping->host; | |
489 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b6aeaded MS |
490 | int err; |
491 | ||
248d86e8 MS |
492 | if (is_bad_inode(inode)) |
493 | return -EIO; | |
494 | ||
5955102c | 495 | inode_lock(inode); |
02c24a82 | 496 | |
3be5a52b MS |
497 | /* |
498 | * Start writeback against all dirty pages of the inode, then | |
499 | * wait for all outstanding writes, before sending the FSYNC | |
500 | * request. | |
501 | */ | |
7e51fe1d | 502 | err = file_write_and_wait_range(file, start, end); |
3be5a52b | 503 | if (err) |
02c24a82 | 504 | goto out; |
3be5a52b MS |
505 | |
506 | fuse_sync_writes(inode); | |
ac7f052b AK |
507 | |
508 | /* | |
509 | * Due to implementation of fuse writeback | |
7e51fe1d | 510 | * file_write_and_wait_range() does not catch errors. |
ac7f052b AK |
511 | * We have to do this directly after fuse_sync_writes() |
512 | */ | |
7e51fe1d | 513 | err = file_check_and_advance_wb_err(file); |
ac7f052b AK |
514 | if (err) |
515 | goto out; | |
516 | ||
1e18bda8 MS |
517 | err = sync_inode_metadata(inode, 1); |
518 | if (err) | |
519 | goto out; | |
3be5a52b | 520 | |
a9c2d1e8 | 521 | if (fc->no_fsync) |
22401e7b | 522 | goto out; |
b0aa7606 | 523 | |
a9c2d1e8 | 524 | err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC); |
b6aeaded | 525 | if (err == -ENOSYS) { |
a9c2d1e8 | 526 | fc->no_fsync = 1; |
b6aeaded MS |
527 | err = 0; |
528 | } | |
02c24a82 | 529 | out: |
5955102c | 530 | inode_unlock(inode); |
b6aeaded | 531 | |
a9c2d1e8 | 532 | return err; |
82547981 MS |
533 | } |
534 | ||
2106cb18 MS |
535 | void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos, |
536 | size_t count, int opcode) | |
b6aeaded | 537 | { |
5c5c5e51 | 538 | struct fuse_read_in *inarg = &req->misc.read.in; |
a6643094 | 539 | struct fuse_file *ff = file->private_data; |
b6aeaded | 540 | |
361b1eb5 MS |
541 | inarg->fh = ff->fh; |
542 | inarg->offset = pos; | |
543 | inarg->size = count; | |
a6643094 | 544 | inarg->flags = file->f_flags; |
361b1eb5 | 545 | req->in.h.opcode = opcode; |
2106cb18 | 546 | req->in.h.nodeid = ff->nodeid; |
b6aeaded MS |
547 | req->in.numargs = 1; |
548 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 549 | req->in.args[0].value = inarg; |
b6aeaded MS |
550 | req->out.argvar = 1; |
551 | req->out.numargs = 1; | |
552 | req->out.args[0].size = count; | |
b6aeaded MS |
553 | } |
554 | ||
00793ca5 | 555 | struct fuse_io_args { |
338f2e3f MS |
556 | union { |
557 | struct { | |
558 | struct fuse_read_in in; | |
559 | } read; | |
560 | struct { | |
561 | struct fuse_write_in in; | |
562 | struct fuse_write_out out; | |
563 | } write; | |
564 | }; | |
00793ca5 MS |
565 | struct fuse_args_pages ap; |
566 | }; | |
567 | ||
568 | void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos, | |
569 | size_t count, int opcode) | |
570 | { | |
571 | struct fuse_file *ff = file->private_data; | |
572 | struct fuse_args *args = &ia->ap.args; | |
573 | ||
574 | ia->read.in.fh = ff->fh; | |
575 | ia->read.in.offset = pos; | |
576 | ia->read.in.size = count; | |
577 | ia->read.in.flags = file->f_flags; | |
578 | args->opcode = opcode; | |
579 | args->nodeid = ff->nodeid; | |
580 | args->in_numargs = 1; | |
581 | args->in_args[0].size = sizeof(ia->read.in); | |
582 | args->in_args[0].value = &ia->read.in; | |
583 | args->out_argvar = true; | |
584 | args->out_numargs = 1; | |
585 | args->out_args[0].size = count; | |
586 | } | |
587 | ||
8fba54ae | 588 | static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty) |
187c5c36 MP |
589 | { |
590 | unsigned i; | |
591 | ||
592 | for (i = 0; i < req->num_pages; i++) { | |
593 | struct page *page = req->pages[i]; | |
8fba54ae | 594 | if (should_dirty) |
187c5c36 MP |
595 | set_page_dirty_lock(page); |
596 | put_page(page); | |
597 | } | |
598 | } | |
599 | ||
744742d6 SF |
600 | static void fuse_io_release(struct kref *kref) |
601 | { | |
602 | kfree(container_of(kref, struct fuse_io_priv, refcnt)); | |
603 | } | |
604 | ||
9d5722b7 CH |
605 | static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io) |
606 | { | |
607 | if (io->err) | |
608 | return io->err; | |
609 | ||
610 | if (io->bytes >= 0 && io->write) | |
611 | return -EIO; | |
612 | ||
613 | return io->bytes < 0 ? io->size : io->bytes; | |
614 | } | |
615 | ||
01e9d11a MP |
616 | /** |
617 | * In case of short read, the caller sets 'pos' to the position of | |
618 | * actual end of fuse request in IO request. Otherwise, if bytes_requested | |
619 | * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1. | |
620 | * | |
621 | * An example: | |
622 | * User requested DIO read of 64K. It was splitted into two 32K fuse requests, | |
623 | * both submitted asynchronously. The first of them was ACKed by userspace as | |
624 | * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The | |
625 | * second request was ACKed as short, e.g. only 1K was read, resulting in | |
626 | * pos == 33K. | |
627 | * | |
628 | * Thus, when all fuse requests are completed, the minimal non-negative 'pos' | |
629 | * will be equal to the length of the longest contiguous fragment of | |
630 | * transferred data starting from the beginning of IO request. | |
631 | */ | |
632 | static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos) | |
633 | { | |
634 | int left; | |
635 | ||
636 | spin_lock(&io->lock); | |
637 | if (err) | |
638 | io->err = io->err ? : err; | |
639 | else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes)) | |
640 | io->bytes = pos; | |
641 | ||
642 | left = --io->reqs; | |
7879c4e5 | 643 | if (!left && io->blocking) |
9d5722b7 | 644 | complete(io->done); |
01e9d11a MP |
645 | spin_unlock(&io->lock); |
646 | ||
7879c4e5 | 647 | if (!left && !io->blocking) { |
9d5722b7 | 648 | ssize_t res = fuse_get_res_by_io(io); |
01e9d11a | 649 | |
9d5722b7 CH |
650 | if (res >= 0) { |
651 | struct inode *inode = file_inode(io->iocb->ki_filp); | |
652 | struct fuse_conn *fc = get_fuse_conn(inode); | |
653 | struct fuse_inode *fi = get_fuse_inode(inode); | |
01e9d11a | 654 | |
f15ecfef | 655 | spin_lock(&fi->lock); |
4510d86f | 656 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
f15ecfef | 657 | spin_unlock(&fi->lock); |
01e9d11a MP |
658 | } |
659 | ||
04b2fa9f | 660 | io->iocb->ki_complete(io->iocb, res, 0); |
01e9d11a | 661 | } |
744742d6 SF |
662 | |
663 | kref_put(&io->refcnt, fuse_io_release); | |
01e9d11a MP |
664 | } |
665 | ||
666 | static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req) | |
667 | { | |
668 | struct fuse_io_priv *io = req->io; | |
669 | ssize_t pos = -1; | |
670 | ||
61c12b49 | 671 | fuse_release_user_pages(req, io->should_dirty); |
01e9d11a MP |
672 | |
673 | if (io->write) { | |
674 | if (req->misc.write.in.size != req->misc.write.out.size) | |
675 | pos = req->misc.write.in.offset - io->offset + | |
676 | req->misc.write.out.size; | |
677 | } else { | |
678 | if (req->misc.read.in.size != req->out.args[0].size) | |
679 | pos = req->misc.read.in.offset - io->offset + | |
680 | req->out.args[0].size; | |
681 | } | |
682 | ||
683 | fuse_aio_complete(io, req->out.h.error, pos); | |
684 | } | |
685 | ||
686 | static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req, | |
687 | size_t num_bytes, struct fuse_io_priv *io) | |
688 | { | |
689 | spin_lock(&io->lock); | |
744742d6 | 690 | kref_get(&io->refcnt); |
01e9d11a MP |
691 | io->size += num_bytes; |
692 | io->reqs++; | |
693 | spin_unlock(&io->lock); | |
694 | ||
695 | req->io = io; | |
696 | req->end = fuse_aio_complete_req; | |
697 | ||
36cf66ed | 698 | __fuse_get_request(req); |
01e9d11a MP |
699 | fuse_request_send_background(fc, req); |
700 | ||
701 | return num_bytes; | |
702 | } | |
703 | ||
36cf66ed | 704 | static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io, |
2106cb18 | 705 | loff_t pos, size_t count, fl_owner_t owner) |
04730fef | 706 | { |
e1c0eecb | 707 | struct file *file = io->iocb->ki_filp; |
2106cb18 MS |
708 | struct fuse_file *ff = file->private_data; |
709 | struct fuse_conn *fc = ff->fc; | |
f3332114 | 710 | |
2106cb18 | 711 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
f3332114 | 712 | if (owner != NULL) { |
5c5c5e51 | 713 | struct fuse_read_in *inarg = &req->misc.read.in; |
f3332114 MS |
714 | |
715 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | |
716 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
717 | } | |
36cf66ed MP |
718 | |
719 | if (io->async) | |
720 | return fuse_async_req_send(fc, req, count, io); | |
721 | ||
b93f858a | 722 | fuse_request_send(fc, req); |
361b1eb5 | 723 | return req->out.args[0].size; |
04730fef MS |
724 | } |
725 | ||
5c5c5e51 MS |
726 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
727 | u64 attr_ver) | |
728 | { | |
729 | struct fuse_conn *fc = get_fuse_conn(inode); | |
730 | struct fuse_inode *fi = get_fuse_inode(inode); | |
731 | ||
f15ecfef | 732 | spin_lock(&fi->lock); |
06a7c3c2 MP |
733 | if (attr_ver == fi->attr_version && size < inode->i_size && |
734 | !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { | |
4510d86f | 735 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
5c5c5e51 MS |
736 | i_size_write(inode, size); |
737 | } | |
f15ecfef | 738 | spin_unlock(&fi->lock); |
5c5c5e51 MS |
739 | } |
740 | ||
a0d45d84 MS |
741 | static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read, |
742 | struct page **pages, unsigned int num_pages) | |
a92adc82 | 743 | { |
8373200b PE |
744 | struct fuse_conn *fc = get_fuse_conn(inode); |
745 | ||
746 | if (fc->writeback_cache) { | |
747 | /* | |
748 | * A hole in a file. Some data after the hole are in page cache, | |
749 | * but have not reached the client fs yet. So, the hole is not | |
750 | * present there. | |
751 | */ | |
752 | int i; | |
09cbfeaf KS |
753 | int start_idx = num_read >> PAGE_SHIFT; |
754 | size_t off = num_read & (PAGE_SIZE - 1); | |
a92adc82 | 755 | |
a0d45d84 MS |
756 | for (i = start_idx; i < num_pages; i++) { |
757 | zero_user_segment(pages[i], off, PAGE_SIZE); | |
8373200b PE |
758 | off = 0; |
759 | } | |
760 | } else { | |
a0d45d84 | 761 | loff_t pos = page_offset(pages[0]) + num_read; |
8373200b PE |
762 | fuse_read_update_size(inode, pos, attr_ver); |
763 | } | |
a92adc82 PE |
764 | } |
765 | ||
482fce55 | 766 | static int fuse_do_readpage(struct file *file, struct page *page) |
b6aeaded MS |
767 | { |
768 | struct inode *inode = page->mapping->host; | |
769 | struct fuse_conn *fc = get_fuse_conn(inode); | |
5c5c5e51 | 770 | loff_t pos = page_offset(page); |
00793ca5 MS |
771 | struct fuse_page_desc desc = { .length = PAGE_SIZE }; |
772 | struct fuse_io_args ia = { | |
773 | .ap.args.page_zeroing = true, | |
774 | .ap.args.out_pages = true, | |
775 | .ap.num_pages = 1, | |
776 | .ap.pages = &page, | |
777 | .ap.descs = &desc, | |
778 | }; | |
779 | ssize_t res; | |
5c5c5e51 | 780 | u64 attr_ver; |
248d86e8 | 781 | |
3be5a52b | 782 | /* |
25985edc | 783 | * Page writeback can extend beyond the lifetime of the |
3be5a52b MS |
784 | * page-cache page, so make sure we read a properly synced |
785 | * page. | |
786 | */ | |
787 | fuse_wait_on_page_writeback(inode, page->index); | |
788 | ||
5c5c5e51 MS |
789 | attr_ver = fuse_get_attr_version(fc); |
790 | ||
00793ca5 MS |
791 | fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ); |
792 | res = fuse_simple_request(fc, &ia.ap.args); | |
793 | if (res < 0) | |
794 | return res; | |
795 | /* | |
796 | * Short read means EOF. If file size is larger, truncate it | |
797 | */ | |
798 | if (res < desc.length) | |
799 | fuse_short_read(inode, attr_ver, res, ia.ap.pages, | |
800 | ia.ap.num_pages); | |
5c5c5e51 | 801 | |
00793ca5 | 802 | SetPageUptodate(page); |
482fce55 | 803 | |
00793ca5 | 804 | return 0; |
482fce55 MP |
805 | } |
806 | ||
807 | static int fuse_readpage(struct file *file, struct page *page) | |
808 | { | |
809 | struct inode *inode = page->mapping->host; | |
810 | int err; | |
811 | ||
812 | err = -EIO; | |
813 | if (is_bad_inode(inode)) | |
814 | goto out; | |
815 | ||
816 | err = fuse_do_readpage(file, page); | |
451418fc | 817 | fuse_invalidate_atime(inode); |
b6aeaded MS |
818 | out: |
819 | unlock_page(page); | |
820 | return err; | |
821 | } | |
822 | ||
c1aa96a5 | 823 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 824 | { |
c1aa96a5 | 825 | int i; |
5c5c5e51 MS |
826 | size_t count = req->misc.read.in.size; |
827 | size_t num_read = req->out.args[0].size; | |
ce534fb0 | 828 | struct address_space *mapping = NULL; |
c1aa96a5 | 829 | |
ce534fb0 MS |
830 | for (i = 0; mapping == NULL && i < req->num_pages; i++) |
831 | mapping = req->pages[i]->mapping; | |
5c5c5e51 | 832 | |
ce534fb0 MS |
833 | if (mapping) { |
834 | struct inode *inode = mapping->host; | |
835 | ||
836 | /* | |
837 | * Short read means EOF. If file size is larger, truncate it | |
838 | */ | |
a92adc82 | 839 | if (!req->out.h.error && num_read < count) |
a0d45d84 MS |
840 | fuse_short_read(inode, req->misc.read.attr_ver, |
841 | num_read, req->pages, req->num_pages); | |
ce534fb0 | 842 | |
451418fc | 843 | fuse_invalidate_atime(inode); |
ce534fb0 | 844 | } |
c1aa96a5 | 845 | |
db50b96c MS |
846 | for (i = 0; i < req->num_pages; i++) { |
847 | struct page *page = req->pages[i]; | |
848 | if (!req->out.h.error) | |
849 | SetPageUptodate(page); | |
c1aa96a5 MS |
850 | else |
851 | SetPageError(page); | |
db50b96c | 852 | unlock_page(page); |
09cbfeaf | 853 | put_page(page); |
db50b96c | 854 | } |
c756e0a4 | 855 | if (req->ff) |
2e64ff15 | 856 | fuse_file_put(req->ff, false, false); |
c1aa96a5 MS |
857 | } |
858 | ||
2106cb18 | 859 | static void fuse_send_readpages(struct fuse_req *req, struct file *file) |
c1aa96a5 | 860 | { |
2106cb18 MS |
861 | struct fuse_file *ff = file->private_data; |
862 | struct fuse_conn *fc = ff->fc; | |
c1aa96a5 | 863 | loff_t pos = page_offset(req->pages[0]); |
09cbfeaf | 864 | size_t count = req->num_pages << PAGE_SHIFT; |
f4975c67 MS |
865 | |
866 | req->out.argpages = 1; | |
c1aa96a5 | 867 | req->out.page_zeroing = 1; |
ce534fb0 | 868 | req->out.page_replace = 1; |
2106cb18 | 869 | fuse_read_fill(req, file, pos, count, FUSE_READ); |
5c5c5e51 | 870 | req->misc.read.attr_ver = fuse_get_attr_version(fc); |
9cd68455 | 871 | if (fc->async_read) { |
c756e0a4 | 872 | req->ff = fuse_file_get(ff); |
9cd68455 | 873 | req->end = fuse_readpages_end; |
b93f858a | 874 | fuse_request_send_background(fc, req); |
9cd68455 | 875 | } else { |
b93f858a | 876 | fuse_request_send(fc, req); |
9cd68455 | 877 | fuse_readpages_end(fc, req); |
e9bb09dd | 878 | fuse_put_request(fc, req); |
9cd68455 | 879 | } |
db50b96c MS |
880 | } |
881 | ||
c756e0a4 | 882 | struct fuse_fill_data { |
db50b96c | 883 | struct fuse_req *req; |
a6643094 | 884 | struct file *file; |
db50b96c | 885 | struct inode *inode; |
f8dbdf81 | 886 | unsigned nr_pages; |
db50b96c MS |
887 | }; |
888 | ||
889 | static int fuse_readpages_fill(void *_data, struct page *page) | |
890 | { | |
c756e0a4 | 891 | struct fuse_fill_data *data = _data; |
db50b96c MS |
892 | struct fuse_req *req = data->req; |
893 | struct inode *inode = data->inode; | |
894 | struct fuse_conn *fc = get_fuse_conn(inode); | |
895 | ||
3be5a52b MS |
896 | fuse_wait_on_page_writeback(inode, page->index); |
897 | ||
db50b96c | 898 | if (req->num_pages && |
5da784cc | 899 | (req->num_pages == fc->max_pages || |
09cbfeaf | 900 | (req->num_pages + 1) * PAGE_SIZE > fc->max_read || |
db50b96c | 901 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { |
5da784cc CS |
902 | unsigned int nr_alloc = min_t(unsigned int, data->nr_pages, |
903 | fc->max_pages); | |
2106cb18 | 904 | fuse_send_readpages(req, data->file); |
8b41e671 MP |
905 | if (fc->async_read) |
906 | req = fuse_get_req_for_background(fc, nr_alloc); | |
907 | else | |
908 | req = fuse_get_req(fc, nr_alloc); | |
909 | ||
910 | data->req = req; | |
ce1d5a49 | 911 | if (IS_ERR(req)) { |
db50b96c | 912 | unlock_page(page); |
ce1d5a49 | 913 | return PTR_ERR(req); |
db50b96c | 914 | } |
db50b96c | 915 | } |
f8dbdf81 MP |
916 | |
917 | if (WARN_ON(req->num_pages >= req->max_pages)) { | |
109728cc | 918 | unlock_page(page); |
f8dbdf81 MP |
919 | fuse_put_request(fc, req); |
920 | return -EIO; | |
921 | } | |
922 | ||
09cbfeaf | 923 | get_page(page); |
db50b96c | 924 | req->pages[req->num_pages] = page; |
85f40aec | 925 | req->page_descs[req->num_pages].length = PAGE_SIZE; |
1729a16c | 926 | req->num_pages++; |
f8dbdf81 | 927 | data->nr_pages--; |
db50b96c MS |
928 | return 0; |
929 | } | |
930 | ||
931 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
932 | struct list_head *pages, unsigned nr_pages) | |
933 | { | |
934 | struct inode *inode = mapping->host; | |
935 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c756e0a4 | 936 | struct fuse_fill_data data; |
db50b96c | 937 | int err; |
5da784cc | 938 | unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages); |
248d86e8 | 939 | |
1d7ea732 | 940 | err = -EIO; |
248d86e8 | 941 | if (is_bad_inode(inode)) |
2e990021 | 942 | goto out; |
248d86e8 | 943 | |
a6643094 | 944 | data.file = file; |
db50b96c | 945 | data.inode = inode; |
8b41e671 MP |
946 | if (fc->async_read) |
947 | data.req = fuse_get_req_for_background(fc, nr_alloc); | |
948 | else | |
949 | data.req = fuse_get_req(fc, nr_alloc); | |
f8dbdf81 | 950 | data.nr_pages = nr_pages; |
1d7ea732 | 951 | err = PTR_ERR(data.req); |
ce1d5a49 | 952 | if (IS_ERR(data.req)) |
2e990021 | 953 | goto out; |
db50b96c MS |
954 | |
955 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
956 | if (!err) { |
957 | if (data.req->num_pages) | |
2106cb18 | 958 | fuse_send_readpages(data.req, file); |
d3406ffa MS |
959 | else |
960 | fuse_put_request(fc, data.req); | |
961 | } | |
2e990021 | 962 | out: |
1d7ea732 | 963 | return err; |
db50b96c MS |
964 | } |
965 | ||
55752a3a | 966 | static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to) |
bcb4be80 MS |
967 | { |
968 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
a8894274 | 969 | struct fuse_conn *fc = get_fuse_conn(inode); |
bcb4be80 | 970 | |
a8894274 BF |
971 | /* |
972 | * In auto invalidate mode, always update attributes on read. | |
973 | * Otherwise, only update if we attempt to read past EOF (to ensure | |
974 | * i_size is up to date). | |
975 | */ | |
976 | if (fc->auto_inval_data || | |
37c20f16 | 977 | (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) { |
bcb4be80 | 978 | int err; |
5b97eeac | 979 | err = fuse_update_attributes(inode, iocb->ki_filp); |
bcb4be80 MS |
980 | if (err) |
981 | return err; | |
982 | } | |
983 | ||
37c20f16 | 984 | return generic_file_read_iter(iocb, to); |
bcb4be80 MS |
985 | } |
986 | ||
2d698b07 | 987 | static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff, |
2106cb18 | 988 | loff_t pos, size_t count) |
b6aeaded | 989 | { |
b25e82e5 MS |
990 | struct fuse_write_in *inarg = &req->misc.write.in; |
991 | struct fuse_write_out *outarg = &req->misc.write.out; | |
b6aeaded | 992 | |
b25e82e5 MS |
993 | inarg->fh = ff->fh; |
994 | inarg->offset = pos; | |
995 | inarg->size = count; | |
b6aeaded | 996 | req->in.h.opcode = FUSE_WRITE; |
2106cb18 | 997 | req->in.h.nodeid = ff->nodeid; |
b6aeaded | 998 | req->in.numargs = 2; |
2106cb18 | 999 | if (ff->fc->minor < 9) |
f3332114 MS |
1000 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; |
1001 | else | |
1002 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
b25e82e5 | 1003 | req->in.args[0].value = inarg; |
b6aeaded MS |
1004 | req->in.args[1].size = count; |
1005 | req->out.numargs = 1; | |
1006 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
b25e82e5 MS |
1007 | req->out.args[0].value = outarg; |
1008 | } | |
1009 | ||
338f2e3f MS |
1010 | static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff, |
1011 | loff_t pos, size_t count) | |
1012 | { | |
1013 | struct fuse_args *args = &ia->ap.args; | |
1014 | ||
1015 | ia->write.in.fh = ff->fh; | |
1016 | ia->write.in.offset = pos; | |
1017 | ia->write.in.size = count; | |
1018 | args->opcode = FUSE_WRITE; | |
1019 | args->nodeid = ff->nodeid; | |
1020 | args->in_numargs = 2; | |
1021 | if (ff->fc->minor < 9) | |
1022 | args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; | |
1023 | else | |
1024 | args->in_args[0].size = sizeof(ia->write.in); | |
1025 | args->in_args[0].value = &ia->write.in; | |
1026 | args->in_args[1].size = count; | |
1027 | args->out_numargs = 1; | |
1028 | args->out_args[0].size = sizeof(ia->write.out); | |
1029 | args->out_args[0].value = &ia->write.out; | |
1030 | } | |
1031 | ||
1032 | static unsigned int fuse_write_flags(struct kiocb *iocb) | |
1033 | { | |
1034 | unsigned int flags = iocb->ki_filp->f_flags; | |
1035 | ||
1036 | if (iocb->ki_flags & IOCB_DSYNC) | |
1037 | flags |= O_DSYNC; | |
1038 | if (iocb->ki_flags & IOCB_SYNC) | |
1039 | flags |= O_SYNC; | |
1040 | ||
1041 | return flags; | |
1042 | } | |
1043 | ||
36cf66ed | 1044 | static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io, |
2106cb18 | 1045 | loff_t pos, size_t count, fl_owner_t owner) |
b25e82e5 | 1046 | { |
e1c0eecb MS |
1047 | struct kiocb *iocb = io->iocb; |
1048 | struct file *file = iocb->ki_filp; | |
2106cb18 MS |
1049 | struct fuse_file *ff = file->private_data; |
1050 | struct fuse_conn *fc = ff->fc; | |
2d698b07 MS |
1051 | struct fuse_write_in *inarg = &req->misc.write.in; |
1052 | ||
2106cb18 | 1053 | fuse_write_fill(req, ff, pos, count); |
338f2e3f | 1054 | inarg->flags = fuse_write_flags(iocb); |
f3332114 | 1055 | if (owner != NULL) { |
f3332114 MS |
1056 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; |
1057 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
1058 | } | |
36cf66ed MP |
1059 | |
1060 | if (io->async) | |
1061 | return fuse_async_req_send(fc, req, count, io); | |
1062 | ||
b93f858a | 1063 | fuse_request_send(fc, req); |
b25e82e5 | 1064 | return req->misc.write.out.size; |
b6aeaded MS |
1065 | } |
1066 | ||
b0aa7606 | 1067 | bool fuse_write_update_size(struct inode *inode, loff_t pos) |
854512ec MS |
1068 | { |
1069 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1070 | struct fuse_inode *fi = get_fuse_inode(inode); | |
b0aa7606 | 1071 | bool ret = false; |
854512ec | 1072 | |
f15ecfef | 1073 | spin_lock(&fi->lock); |
4510d86f | 1074 | fi->attr_version = atomic64_inc_return(&fc->attr_version); |
b0aa7606 | 1075 | if (pos > inode->i_size) { |
854512ec | 1076 | i_size_write(inode, pos); |
b0aa7606 MP |
1077 | ret = true; |
1078 | } | |
f15ecfef | 1079 | spin_unlock(&fi->lock); |
b0aa7606 MP |
1080 | |
1081 | return ret; | |
854512ec MS |
1082 | } |
1083 | ||
338f2e3f MS |
1084 | static ssize_t fuse_send_write_pages(struct fuse_io_args *ia, |
1085 | struct kiocb *iocb, struct inode *inode, | |
1086 | loff_t pos, size_t count) | |
ea9b9907 | 1087 | { |
338f2e3f MS |
1088 | struct fuse_args_pages *ap = &ia->ap; |
1089 | struct file *file = iocb->ki_filp; | |
1090 | struct fuse_file *ff = file->private_data; | |
1091 | struct fuse_conn *fc = ff->fc; | |
1092 | unsigned int offset, i; | |
1093 | int err; | |
ea9b9907 | 1094 | |
338f2e3f MS |
1095 | for (i = 0; i < ap->num_pages; i++) |
1096 | fuse_wait_on_page_writeback(inode, ap->pages[i]->index); | |
ea9b9907 | 1097 | |
338f2e3f MS |
1098 | fuse_write_args_fill(ia, ff, pos, count); |
1099 | ia->write.in.flags = fuse_write_flags(iocb); | |
ea9b9907 | 1100 | |
338f2e3f MS |
1101 | err = fuse_simple_request(fc, &ap->args); |
1102 | ||
1103 | offset = ap->descs[0].offset; | |
1104 | count = ia->write.out.size; | |
1105 | for (i = 0; i < ap->num_pages; i++) { | |
1106 | struct page *page = ap->pages[i]; | |
ea9b9907 | 1107 | |
338f2e3f | 1108 | if (!err && !offset && count >= PAGE_SIZE) |
ea9b9907 NP |
1109 | SetPageUptodate(page); |
1110 | ||
09cbfeaf KS |
1111 | if (count > PAGE_SIZE - offset) |
1112 | count -= PAGE_SIZE - offset; | |
ea9b9907 NP |
1113 | else |
1114 | count = 0; | |
1115 | offset = 0; | |
1116 | ||
1117 | unlock_page(page); | |
09cbfeaf | 1118 | put_page(page); |
ea9b9907 NP |
1119 | } |
1120 | ||
338f2e3f | 1121 | return err; |
ea9b9907 NP |
1122 | } |
1123 | ||
338f2e3f MS |
1124 | static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap, |
1125 | struct address_space *mapping, | |
1126 | struct iov_iter *ii, loff_t pos, | |
1127 | unsigned int max_pages) | |
ea9b9907 NP |
1128 | { |
1129 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
09cbfeaf | 1130 | unsigned offset = pos & (PAGE_SIZE - 1); |
ea9b9907 NP |
1131 | size_t count = 0; |
1132 | int err; | |
1133 | ||
338f2e3f MS |
1134 | ap->args.in_pages = true; |
1135 | ap->descs[0].offset = offset; | |
ea9b9907 NP |
1136 | |
1137 | do { | |
1138 | size_t tmp; | |
1139 | struct page *page; | |
09cbfeaf KS |
1140 | pgoff_t index = pos >> PAGE_SHIFT; |
1141 | size_t bytes = min_t(size_t, PAGE_SIZE - offset, | |
ea9b9907 NP |
1142 | iov_iter_count(ii)); |
1143 | ||
1144 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
1145 | ||
1146 | again: | |
1147 | err = -EFAULT; | |
1148 | if (iov_iter_fault_in_readable(ii, bytes)) | |
1149 | break; | |
1150 | ||
1151 | err = -ENOMEM; | |
54566b2c | 1152 | page = grab_cache_page_write_begin(mapping, index, 0); |
ea9b9907 NP |
1153 | if (!page) |
1154 | break; | |
1155 | ||
931e80e4 | 1156 | if (mapping_writably_mapped(mapping)) |
1157 | flush_dcache_page(page); | |
1158 | ||
ea9b9907 | 1159 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); |
ea9b9907 NP |
1160 | flush_dcache_page(page); |
1161 | ||
3ca8138f | 1162 | iov_iter_advance(ii, tmp); |
ea9b9907 NP |
1163 | if (!tmp) { |
1164 | unlock_page(page); | |
09cbfeaf | 1165 | put_page(page); |
ea9b9907 NP |
1166 | bytes = min(bytes, iov_iter_single_seg_count(ii)); |
1167 | goto again; | |
1168 | } | |
1169 | ||
1170 | err = 0; | |
338f2e3f MS |
1171 | ap->pages[ap->num_pages] = page; |
1172 | ap->descs[ap->num_pages].length = tmp; | |
1173 | ap->num_pages++; | |
ea9b9907 | 1174 | |
ea9b9907 NP |
1175 | count += tmp; |
1176 | pos += tmp; | |
1177 | offset += tmp; | |
09cbfeaf | 1178 | if (offset == PAGE_SIZE) |
ea9b9907 NP |
1179 | offset = 0; |
1180 | ||
78bb6cb9 MS |
1181 | if (!fc->big_writes) |
1182 | break; | |
ea9b9907 | 1183 | } while (iov_iter_count(ii) && count < fc->max_write && |
338f2e3f | 1184 | ap->num_pages < max_pages && offset == 0); |
ea9b9907 NP |
1185 | |
1186 | return count > 0 ? count : err; | |
1187 | } | |
1188 | ||
5da784cc CS |
1189 | static inline unsigned int fuse_wr_pages(loff_t pos, size_t len, |
1190 | unsigned int max_pages) | |
d07f09f5 | 1191 | { |
5da784cc | 1192 | return min_t(unsigned int, |
09cbfeaf KS |
1193 | ((pos + len - 1) >> PAGE_SHIFT) - |
1194 | (pos >> PAGE_SHIFT) + 1, | |
5da784cc | 1195 | max_pages); |
d07f09f5 MP |
1196 | } |
1197 | ||
e1c0eecb | 1198 | static ssize_t fuse_perform_write(struct kiocb *iocb, |
ea9b9907 NP |
1199 | struct address_space *mapping, |
1200 | struct iov_iter *ii, loff_t pos) | |
1201 | { | |
1202 | struct inode *inode = mapping->host; | |
1203 | struct fuse_conn *fc = get_fuse_conn(inode); | |
06a7c3c2 | 1204 | struct fuse_inode *fi = get_fuse_inode(inode); |
ea9b9907 NP |
1205 | int err = 0; |
1206 | ssize_t res = 0; | |
1207 | ||
06a7c3c2 MP |
1208 | if (inode->i_size < pos + iov_iter_count(ii)) |
1209 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
1210 | ||
ea9b9907 | 1211 | do { |
ea9b9907 | 1212 | ssize_t count; |
338f2e3f MS |
1213 | struct fuse_io_args ia = {}; |
1214 | struct fuse_args_pages *ap = &ia.ap; | |
5da784cc CS |
1215 | unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii), |
1216 | fc->max_pages); | |
ea9b9907 | 1217 | |
338f2e3f MS |
1218 | ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs); |
1219 | if (!ap->pages) { | |
1220 | err = -ENOMEM; | |
ea9b9907 NP |
1221 | break; |
1222 | } | |
1223 | ||
338f2e3f | 1224 | count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages); |
ea9b9907 NP |
1225 | if (count <= 0) { |
1226 | err = count; | |
1227 | } else { | |
338f2e3f MS |
1228 | err = fuse_send_write_pages(&ia, iocb, inode, |
1229 | pos, count); | |
ea9b9907 | 1230 | if (!err) { |
338f2e3f MS |
1231 | size_t num_written = ia.write.out.size; |
1232 | ||
ea9b9907 NP |
1233 | res += num_written; |
1234 | pos += num_written; | |
1235 | ||
1236 | /* break out of the loop on short write */ | |
1237 | if (num_written != count) | |
1238 | err = -EIO; | |
1239 | } | |
1240 | } | |
338f2e3f | 1241 | kfree(ap->pages); |
ea9b9907 NP |
1242 | } while (!err && iov_iter_count(ii)); |
1243 | ||
1244 | if (res > 0) | |
1245 | fuse_write_update_size(inode, pos); | |
1246 | ||
06a7c3c2 | 1247 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); |
ea9b9907 NP |
1248 | fuse_invalidate_attr(inode); |
1249 | ||
1250 | return res > 0 ? res : err; | |
1251 | } | |
1252 | ||
55752a3a | 1253 | static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from) |
ea9b9907 NP |
1254 | { |
1255 | struct file *file = iocb->ki_filp; | |
1256 | struct address_space *mapping = file->f_mapping; | |
ea9b9907 | 1257 | ssize_t written = 0; |
4273b793 | 1258 | ssize_t written_buffered = 0; |
ea9b9907 NP |
1259 | struct inode *inode = mapping->host; |
1260 | ssize_t err; | |
4273b793 | 1261 | loff_t endbyte = 0; |
ea9b9907 | 1262 | |
4d99ff8f PE |
1263 | if (get_fuse_conn(inode)->writeback_cache) { |
1264 | /* Update size (EOF optimization) and mode (SUID clearing) */ | |
5b97eeac | 1265 | err = fuse_update_attributes(mapping->host, file); |
4d99ff8f PE |
1266 | if (err) |
1267 | return err; | |
1268 | ||
84c3d55c | 1269 | return generic_file_write_iter(iocb, from); |
4d99ff8f PE |
1270 | } |
1271 | ||
5955102c | 1272 | inode_lock(inode); |
ea9b9907 NP |
1273 | |
1274 | /* We can write back this queue in page reclaim */ | |
de1414a6 | 1275 | current->backing_dev_info = inode_to_bdi(inode); |
ea9b9907 | 1276 | |
3309dd04 AV |
1277 | err = generic_write_checks(iocb, from); |
1278 | if (err <= 0) | |
ea9b9907 NP |
1279 | goto out; |
1280 | ||
5fa8e0a1 | 1281 | err = file_remove_privs(file); |
ea9b9907 NP |
1282 | if (err) |
1283 | goto out; | |
1284 | ||
c3b2da31 JB |
1285 | err = file_update_time(file); |
1286 | if (err) | |
1287 | goto out; | |
ea9b9907 | 1288 | |
2ba48ce5 | 1289 | if (iocb->ki_flags & IOCB_DIRECT) { |
3309dd04 | 1290 | loff_t pos = iocb->ki_pos; |
1af5bb49 | 1291 | written = generic_file_direct_write(iocb, from); |
84c3d55c | 1292 | if (written < 0 || !iov_iter_count(from)) |
4273b793 AA |
1293 | goto out; |
1294 | ||
1295 | pos += written; | |
ea9b9907 | 1296 | |
e1c0eecb | 1297 | written_buffered = fuse_perform_write(iocb, mapping, from, pos); |
4273b793 AA |
1298 | if (written_buffered < 0) { |
1299 | err = written_buffered; | |
1300 | goto out; | |
1301 | } | |
1302 | endbyte = pos + written_buffered - 1; | |
1303 | ||
1304 | err = filemap_write_and_wait_range(file->f_mapping, pos, | |
1305 | endbyte); | |
1306 | if (err) | |
1307 | goto out; | |
1308 | ||
1309 | invalidate_mapping_pages(file->f_mapping, | |
09cbfeaf KS |
1310 | pos >> PAGE_SHIFT, |
1311 | endbyte >> PAGE_SHIFT); | |
4273b793 AA |
1312 | |
1313 | written += written_buffered; | |
1314 | iocb->ki_pos = pos + written_buffered; | |
1315 | } else { | |
e1c0eecb | 1316 | written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos); |
4273b793 | 1317 | if (written >= 0) |
3309dd04 | 1318 | iocb->ki_pos += written; |
4273b793 | 1319 | } |
ea9b9907 NP |
1320 | out: |
1321 | current->backing_dev_info = NULL; | |
5955102c | 1322 | inode_unlock(inode); |
e1c0eecb MS |
1323 | if (written > 0) |
1324 | written = generic_write_sync(iocb, written); | |
ea9b9907 NP |
1325 | |
1326 | return written ? written : err; | |
1327 | } | |
1328 | ||
093f38a2 MS |
1329 | static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs, |
1330 | unsigned int index, | |
1331 | unsigned int nr_pages) | |
85f40aec MP |
1332 | { |
1333 | int i; | |
1334 | ||
7c190c8b | 1335 | for (i = index; i < index + nr_pages; i++) |
093f38a2 | 1336 | descs[i].length = PAGE_SIZE - descs[i].offset; |
85f40aec MP |
1337 | } |
1338 | ||
7c190c8b MP |
1339 | static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii) |
1340 | { | |
1341 | return (unsigned long)ii->iov->iov_base + ii->iov_offset; | |
1342 | } | |
1343 | ||
1344 | static inline size_t fuse_get_frag_size(const struct iov_iter *ii, | |
1345 | size_t max_size) | |
1346 | { | |
1347 | return min(iov_iter_single_seg_count(ii), max_size); | |
1348 | } | |
1349 | ||
b98d023a | 1350 | static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii, |
ce60a2f1 | 1351 | size_t *nbytesp, int write) |
413ef8cb | 1352 | { |
7c190c8b | 1353 | size_t nbytes = 0; /* # bytes already packed in req */ |
742f9927 | 1354 | ssize_t ret = 0; |
b98d023a | 1355 | |
f4975c67 | 1356 | /* Special case for kernel I/O: can copy directly into the buffer */ |
00e23707 | 1357 | if (iov_iter_is_kvec(ii)) { |
7c190c8b MP |
1358 | unsigned long user_addr = fuse_get_user_addr(ii); |
1359 | size_t frag_size = fuse_get_frag_size(ii, *nbytesp); | |
1360 | ||
f4975c67 MS |
1361 | if (write) |
1362 | req->in.args[1].value = (void *) user_addr; | |
1363 | else | |
1364 | req->out.args[0].value = (void *) user_addr; | |
1365 | ||
b98d023a MP |
1366 | iov_iter_advance(ii, frag_size); |
1367 | *nbytesp = frag_size; | |
f4975c67 MS |
1368 | return 0; |
1369 | } | |
413ef8cb | 1370 | |
5565a9d8 | 1371 | while (nbytes < *nbytesp && req->num_pages < req->max_pages) { |
7c190c8b | 1372 | unsigned npages; |
f67da30c | 1373 | size_t start; |
742f9927 | 1374 | ret = iov_iter_get_pages(ii, &req->pages[req->num_pages], |
2c80929c | 1375 | *nbytesp - nbytes, |
c7f3888a AV |
1376 | req->max_pages - req->num_pages, |
1377 | &start); | |
7c190c8b | 1378 | if (ret < 0) |
742f9927 | 1379 | break; |
7c190c8b | 1380 | |
c9c37e2e AV |
1381 | iov_iter_advance(ii, ret); |
1382 | nbytes += ret; | |
7c190c8b | 1383 | |
c9c37e2e AV |
1384 | ret += start; |
1385 | npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE; | |
7c190c8b | 1386 | |
c9c37e2e | 1387 | req->page_descs[req->num_pages].offset = start; |
093f38a2 MS |
1388 | fuse_page_descs_length_init(req->page_descs, req->num_pages, |
1389 | npages); | |
7c190c8b MP |
1390 | |
1391 | req->num_pages += npages; | |
1392 | req->page_descs[req->num_pages - 1].length -= | |
c9c37e2e | 1393 | (PAGE_SIZE - ret) & (PAGE_SIZE - 1); |
7c190c8b | 1394 | } |
f4975c67 MS |
1395 | |
1396 | if (write) | |
1397 | req->in.argpages = 1; | |
1398 | else | |
1399 | req->out.argpages = 1; | |
1400 | ||
7c190c8b | 1401 | *nbytesp = nbytes; |
f4975c67 | 1402 | |
2c932d4c | 1403 | return ret < 0 ? ret : 0; |
413ef8cb MS |
1404 | } |
1405 | ||
d22a943f AV |
1406 | ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, |
1407 | loff_t *ppos, int flags) | |
413ef8cb | 1408 | { |
ea8cd333 PE |
1409 | int write = flags & FUSE_DIO_WRITE; |
1410 | int cuse = flags & FUSE_DIO_CUSE; | |
e1c0eecb | 1411 | struct file *file = io->iocb->ki_filp; |
ea8cd333 | 1412 | struct inode *inode = file->f_mapping->host; |
2106cb18 MS |
1413 | struct fuse_file *ff = file->private_data; |
1414 | struct fuse_conn *fc = ff->fc; | |
413ef8cb MS |
1415 | size_t nmax = write ? fc->max_write : fc->max_read; |
1416 | loff_t pos = *ppos; | |
d22a943f | 1417 | size_t count = iov_iter_count(iter); |
09cbfeaf KS |
1418 | pgoff_t idx_from = pos >> PAGE_SHIFT; |
1419 | pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT; | |
413ef8cb | 1420 | ssize_t res = 0; |
248d86e8 | 1421 | struct fuse_req *req; |
742f9927 | 1422 | int err = 0; |
248d86e8 | 1423 | |
de82b923 | 1424 | if (io->async) |
5da784cc CS |
1425 | req = fuse_get_req_for_background(fc, iov_iter_npages(iter, |
1426 | fc->max_pages)); | |
de82b923 | 1427 | else |
5da784cc | 1428 | req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages)); |
ce1d5a49 MS |
1429 | if (IS_ERR(req)) |
1430 | return PTR_ERR(req); | |
413ef8cb | 1431 | |
ea8cd333 PE |
1432 | if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) { |
1433 | if (!write) | |
5955102c | 1434 | inode_lock(inode); |
ea8cd333 PE |
1435 | fuse_sync_writes(inode); |
1436 | if (!write) | |
5955102c | 1437 | inode_unlock(inode); |
ea8cd333 PE |
1438 | } |
1439 | ||
61c12b49 | 1440 | io->should_dirty = !write && iter_is_iovec(iter); |
413ef8cb | 1441 | while (count) { |
413ef8cb | 1442 | size_t nres; |
2106cb18 | 1443 | fl_owner_t owner = current->files; |
f4975c67 | 1444 | size_t nbytes = min(count, nmax); |
742f9927 AS |
1445 | err = fuse_get_user_pages(req, iter, &nbytes, write); |
1446 | if (err && !nbytes) | |
413ef8cb | 1447 | break; |
f4975c67 | 1448 | |
4a2abf99 MS |
1449 | if (write) { |
1450 | if (!capable(CAP_FSETID)) { | |
1451 | struct fuse_write_in *inarg; | |
1452 | ||
1453 | inarg = &req->misc.write.in; | |
1454 | inarg->write_flags |= FUSE_WRITE_KILL_PRIV; | |
1455 | } | |
36cf66ed | 1456 | nres = fuse_send_write(req, io, pos, nbytes, owner); |
4a2abf99 | 1457 | } else { |
36cf66ed | 1458 | nres = fuse_send_read(req, io, pos, nbytes, owner); |
4a2abf99 | 1459 | } |
2106cb18 | 1460 | |
36cf66ed | 1461 | if (!io->async) |
61c12b49 | 1462 | fuse_release_user_pages(req, io->should_dirty); |
413ef8cb | 1463 | if (req->out.h.error) { |
742f9927 | 1464 | err = req->out.h.error; |
413ef8cb MS |
1465 | break; |
1466 | } else if (nres > nbytes) { | |
742f9927 AS |
1467 | res = 0; |
1468 | err = -EIO; | |
413ef8cb MS |
1469 | break; |
1470 | } | |
1471 | count -= nres; | |
1472 | res += nres; | |
1473 | pos += nres; | |
413ef8cb MS |
1474 | if (nres != nbytes) |
1475 | break; | |
56cf34ff MS |
1476 | if (count) { |
1477 | fuse_put_request(fc, req); | |
de82b923 BF |
1478 | if (io->async) |
1479 | req = fuse_get_req_for_background(fc, | |
5da784cc | 1480 | iov_iter_npages(iter, fc->max_pages)); |
de82b923 | 1481 | else |
5da784cc CS |
1482 | req = fuse_get_req(fc, iov_iter_npages(iter, |
1483 | fc->max_pages)); | |
56cf34ff MS |
1484 | if (IS_ERR(req)) |
1485 | break; | |
1486 | } | |
413ef8cb | 1487 | } |
f60311d5 AA |
1488 | if (!IS_ERR(req)) |
1489 | fuse_put_request(fc, req); | |
d09cb9d7 | 1490 | if (res > 0) |
413ef8cb | 1491 | *ppos = pos; |
413ef8cb | 1492 | |
742f9927 | 1493 | return res > 0 ? res : err; |
413ef8cb | 1494 | } |
08cbf542 | 1495 | EXPORT_SYMBOL_GPL(fuse_direct_io); |
413ef8cb | 1496 | |
36cf66ed | 1497 | static ssize_t __fuse_direct_read(struct fuse_io_priv *io, |
d22a943f AV |
1498 | struct iov_iter *iter, |
1499 | loff_t *ppos) | |
413ef8cb | 1500 | { |
d09cb9d7 | 1501 | ssize_t res; |
e1c0eecb | 1502 | struct inode *inode = file_inode(io->iocb->ki_filp); |
d09cb9d7 | 1503 | |
d22a943f | 1504 | res = fuse_direct_io(io, iter, ppos, 0); |
d09cb9d7 | 1505 | |
9a2eb24d | 1506 | fuse_invalidate_atime(inode); |
d09cb9d7 MS |
1507 | |
1508 | return res; | |
413ef8cb MS |
1509 | } |
1510 | ||
23c94e1c MR |
1511 | static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter); |
1512 | ||
15316263 | 1513 | static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) |
b98d023a | 1514 | { |
23c94e1c MR |
1515 | ssize_t res; |
1516 | ||
1517 | if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { | |
23c94e1c MR |
1518 | res = fuse_direct_IO(iocb, to); |
1519 | } else { | |
1520 | struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); | |
1521 | ||
1522 | res = __fuse_direct_read(&io, to, &iocb->ki_pos); | |
1523 | } | |
1524 | ||
1525 | return res; | |
b98d023a MP |
1526 | } |
1527 | ||
15316263 | 1528 | static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from) |
4273b793 | 1529 | { |
e1c0eecb MS |
1530 | struct inode *inode = file_inode(iocb->ki_filp); |
1531 | struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); | |
15316263 | 1532 | ssize_t res; |
4273b793 | 1533 | |
4273b793 | 1534 | /* Don't allow parallel writes to the same file */ |
5955102c | 1535 | inode_lock(inode); |
3309dd04 | 1536 | res = generic_write_checks(iocb, from); |
23c94e1c MR |
1537 | if (res > 0) { |
1538 | if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) { | |
1539 | res = fuse_direct_IO(iocb, from); | |
1540 | } else { | |
1541 | res = fuse_direct_io(&io, from, &iocb->ki_pos, | |
1542 | FUSE_DIO_WRITE); | |
1543 | } | |
1544 | } | |
812408fb | 1545 | fuse_invalidate_attr(inode); |
bcba24cc | 1546 | if (res > 0) |
15316263 | 1547 | fuse_write_update_size(inode, iocb->ki_pos); |
5955102c | 1548 | inode_unlock(inode); |
4273b793 AA |
1549 | |
1550 | return res; | |
1551 | } | |
1552 | ||
55752a3a MS |
1553 | static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
1554 | { | |
2f7b6f5b MS |
1555 | struct file *file = iocb->ki_filp; |
1556 | struct fuse_file *ff = file->private_data; | |
1557 | ||
1558 | if (is_bad_inode(file_inode(file))) | |
1559 | return -EIO; | |
55752a3a MS |
1560 | |
1561 | if (!(ff->open_flags & FOPEN_DIRECT_IO)) | |
1562 | return fuse_cache_read_iter(iocb, to); | |
1563 | else | |
1564 | return fuse_direct_read_iter(iocb, to); | |
1565 | } | |
1566 | ||
1567 | static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
1568 | { | |
2f7b6f5b MS |
1569 | struct file *file = iocb->ki_filp; |
1570 | struct fuse_file *ff = file->private_data; | |
1571 | ||
1572 | if (is_bad_inode(file_inode(file))) | |
1573 | return -EIO; | |
55752a3a MS |
1574 | |
1575 | if (!(ff->open_flags & FOPEN_DIRECT_IO)) | |
1576 | return fuse_cache_write_iter(iocb, from); | |
1577 | else | |
1578 | return fuse_direct_write_iter(iocb, from); | |
1579 | } | |
1580 | ||
3be5a52b | 1581 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
b6aeaded | 1582 | { |
385b1268 PE |
1583 | int i; |
1584 | ||
1585 | for (i = 0; i < req->num_pages; i++) | |
1586 | __free_page(req->pages[i]); | |
8b284dc4 MS |
1587 | |
1588 | if (req->ff) | |
2e64ff15 | 1589 | fuse_file_put(req->ff, false, false); |
3be5a52b MS |
1590 | } |
1591 | ||
1592 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | |
1593 | { | |
1594 | struct inode *inode = req->inode; | |
1595 | struct fuse_inode *fi = get_fuse_inode(inode); | |
de1414a6 | 1596 | struct backing_dev_info *bdi = inode_to_bdi(inode); |
385b1268 | 1597 | int i; |
3be5a52b MS |
1598 | |
1599 | list_del(&req->writepages_entry); | |
385b1268 | 1600 | for (i = 0; i < req->num_pages; i++) { |
93f78d88 | 1601 | dec_wb_stat(&bdi->wb, WB_WRITEBACK); |
11fb9989 | 1602 | dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP); |
93f78d88 | 1603 | wb_writeout_inc(&bdi->wb); |
385b1268 | 1604 | } |
3be5a52b MS |
1605 | wake_up(&fi->page_waitq); |
1606 | } | |
1607 | ||
f15ecfef | 1608 | /* Called under fi->lock, may release and reacquire it */ |
6eaf4782 MP |
1609 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req, |
1610 | loff_t size) | |
f15ecfef KT |
1611 | __releases(fi->lock) |
1612 | __acquires(fi->lock) | |
3be5a52b | 1613 | { |
e2653bd5 | 1614 | struct fuse_req *aux, *next; |
3be5a52b | 1615 | struct fuse_inode *fi = get_fuse_inode(req->inode); |
3be5a52b | 1616 | struct fuse_write_in *inarg = &req->misc.write.in; |
09cbfeaf | 1617 | __u64 data_size = req->num_pages * PAGE_SIZE; |
63825b4e | 1618 | bool queued; |
3be5a52b | 1619 | |
385b1268 PE |
1620 | if (inarg->offset + data_size <= size) { |
1621 | inarg->size = data_size; | |
3be5a52b | 1622 | } else if (inarg->offset < size) { |
385b1268 | 1623 | inarg->size = size - inarg->offset; |
3be5a52b MS |
1624 | } else { |
1625 | /* Got truncated off completely */ | |
1626 | goto out_free; | |
b6aeaded | 1627 | } |
3be5a52b MS |
1628 | |
1629 | req->in.args[1].size = inarg->size; | |
63825b4e | 1630 | queued = fuse_request_queue_background(fc, req); |
f15ecfef KT |
1631 | /* Fails on broken connection only */ |
1632 | if (unlikely(!queued)) | |
1633 | goto out_free; | |
1634 | ||
1635 | fi->writectr++; | |
3be5a52b MS |
1636 | return; |
1637 | ||
1638 | out_free: | |
1639 | fuse_writepage_finish(fc, req); | |
f15ecfef | 1640 | spin_unlock(&fi->lock); |
e2653bd5 MS |
1641 | |
1642 | /* After fuse_writepage_finish() aux request list is private */ | |
1643 | for (aux = req->misc.write.next; aux; aux = next) { | |
1644 | next = aux->misc.write.next; | |
1645 | aux->misc.write.next = NULL; | |
1646 | fuse_writepage_free(fc, aux); | |
1647 | fuse_put_request(fc, aux); | |
1648 | } | |
1649 | ||
3be5a52b | 1650 | fuse_writepage_free(fc, req); |
e9bb09dd | 1651 | fuse_put_request(fc, req); |
f15ecfef | 1652 | spin_lock(&fi->lock); |
b6aeaded MS |
1653 | } |
1654 | ||
3be5a52b MS |
1655 | /* |
1656 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1657 | * all queued writepage requests. | |
1658 | * | |
f15ecfef | 1659 | * Called with fi->lock |
3be5a52b MS |
1660 | */ |
1661 | void fuse_flush_writepages(struct inode *inode) | |
f15ecfef KT |
1662 | __releases(fi->lock) |
1663 | __acquires(fi->lock) | |
b6aeaded | 1664 | { |
3be5a52b MS |
1665 | struct fuse_conn *fc = get_fuse_conn(inode); |
1666 | struct fuse_inode *fi = get_fuse_inode(inode); | |
9de5be06 | 1667 | loff_t crop = i_size_read(inode); |
3be5a52b MS |
1668 | struct fuse_req *req; |
1669 | ||
1670 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
1671 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | |
1672 | list_del_init(&req->list); | |
6eaf4782 | 1673 | fuse_send_writepage(fc, req, crop); |
3be5a52b MS |
1674 | } |
1675 | } | |
1676 | ||
1677 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | |
1678 | { | |
1679 | struct inode *inode = req->inode; | |
1680 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1681 | ||
1682 | mapping_set_error(inode->i_mapping, req->out.h.error); | |
f15ecfef | 1683 | spin_lock(&fi->lock); |
8b284dc4 | 1684 | while (req->misc.write.next) { |
6eaf4782 MP |
1685 | struct fuse_conn *fc = get_fuse_conn(inode); |
1686 | struct fuse_write_in *inarg = &req->misc.write.in; | |
8b284dc4 MS |
1687 | struct fuse_req *next = req->misc.write.next; |
1688 | req->misc.write.next = next->misc.write.next; | |
1689 | next->misc.write.next = NULL; | |
ce128de6 | 1690 | next->ff = fuse_file_get(req->ff); |
8b284dc4 | 1691 | list_add(&next->writepages_entry, &fi->writepages); |
6eaf4782 MP |
1692 | |
1693 | /* | |
1694 | * Skip fuse_flush_writepages() to make it easy to crop requests | |
1695 | * based on primary request size. | |
1696 | * | |
1697 | * 1st case (trivial): there are no concurrent activities using | |
1698 | * fuse_set/release_nowrite. Then we're on safe side because | |
1699 | * fuse_flush_writepages() would call fuse_send_writepage() | |
1700 | * anyway. | |
1701 | * | |
1702 | * 2nd case: someone called fuse_set_nowrite and it is waiting | |
1703 | * now for completion of all in-flight requests. This happens | |
1704 | * rarely and no more than once per page, so this should be | |
1705 | * okay. | |
1706 | * | |
1707 | * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle | |
1708 | * of fuse_set_nowrite..fuse_release_nowrite section. The fact | |
1709 | * that fuse_set_nowrite returned implies that all in-flight | |
1710 | * requests were completed along with all of their secondary | |
1711 | * requests. Further primary requests are blocked by negative | |
1712 | * writectr. Hence there cannot be any in-flight requests and | |
1713 | * no invocations of fuse_writepage_end() while we're in | |
1714 | * fuse_set_nowrite..fuse_release_nowrite section. | |
1715 | */ | |
1716 | fuse_send_writepage(fc, next, inarg->offset + inarg->size); | |
8b284dc4 | 1717 | } |
3be5a52b MS |
1718 | fi->writectr--; |
1719 | fuse_writepage_finish(fc, req); | |
f15ecfef | 1720 | spin_unlock(&fi->lock); |
3be5a52b MS |
1721 | fuse_writepage_free(fc, req); |
1722 | } | |
1723 | ||
1e18bda8 MS |
1724 | static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc, |
1725 | struct fuse_inode *fi) | |
adcadfa8 | 1726 | { |
72523425 | 1727 | struct fuse_file *ff = NULL; |
adcadfa8 | 1728 | |
f15ecfef | 1729 | spin_lock(&fi->lock); |
1e18bda8 | 1730 | if (!list_empty(&fi->write_files)) { |
72523425 MS |
1731 | ff = list_entry(fi->write_files.next, struct fuse_file, |
1732 | write_entry); | |
1733 | fuse_file_get(ff); | |
1734 | } | |
f15ecfef | 1735 | spin_unlock(&fi->lock); |
adcadfa8 PE |
1736 | |
1737 | return ff; | |
1738 | } | |
1739 | ||
1e18bda8 MS |
1740 | static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc, |
1741 | struct fuse_inode *fi) | |
1742 | { | |
1743 | struct fuse_file *ff = __fuse_write_file_get(fc, fi); | |
1744 | WARN_ON(!ff); | |
1745 | return ff; | |
1746 | } | |
1747 | ||
1748 | int fuse_write_inode(struct inode *inode, struct writeback_control *wbc) | |
1749 | { | |
1750 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1751 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1752 | struct fuse_file *ff; | |
1753 | int err; | |
1754 | ||
1755 | ff = __fuse_write_file_get(fc, fi); | |
ab9e13f7 | 1756 | err = fuse_flush_times(inode, ff); |
1e18bda8 | 1757 | if (ff) |
2e64ff15 | 1758 | fuse_file_put(ff, false, false); |
1e18bda8 MS |
1759 | |
1760 | return err; | |
1761 | } | |
1762 | ||
3be5a52b MS |
1763 | static int fuse_writepage_locked(struct page *page) |
1764 | { | |
1765 | struct address_space *mapping = page->mapping; | |
1766 | struct inode *inode = mapping->host; | |
1767 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1768 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1769 | struct fuse_req *req; | |
3be5a52b | 1770 | struct page *tmp_page; |
72523425 | 1771 | int error = -ENOMEM; |
3be5a52b MS |
1772 | |
1773 | set_page_writeback(page); | |
1774 | ||
4250c066 | 1775 | req = fuse_request_alloc_nofs(1); |
3be5a52b MS |
1776 | if (!req) |
1777 | goto err; | |
1778 | ||
825d6d33 MS |
1779 | /* writeback always goes to bg_queue */ |
1780 | __set_bit(FR_BACKGROUND, &req->flags); | |
3be5a52b MS |
1781 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
1782 | if (!tmp_page) | |
1783 | goto err_free; | |
1784 | ||
72523425 | 1785 | error = -EIO; |
26d614df | 1786 | req->ff = fuse_write_file_get(fc, fi); |
72523425 | 1787 | if (!req->ff) |
27f1b363 | 1788 | goto err_nofile; |
72523425 | 1789 | |
adcadfa8 | 1790 | fuse_write_fill(req, req->ff, page_offset(page), 0); |
3be5a52b MS |
1791 | |
1792 | copy_highpage(tmp_page, page); | |
2d698b07 | 1793 | req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; |
8b284dc4 | 1794 | req->misc.write.next = NULL; |
f4975c67 | 1795 | req->in.argpages = 1; |
3be5a52b MS |
1796 | req->num_pages = 1; |
1797 | req->pages[0] = tmp_page; | |
b2430d75 | 1798 | req->page_descs[0].offset = 0; |
85f40aec | 1799 | req->page_descs[0].length = PAGE_SIZE; |
3be5a52b MS |
1800 | req->end = fuse_writepage_end; |
1801 | req->inode = inode; | |
1802 | ||
93f78d88 | 1803 | inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); |
11fb9989 | 1804 | inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); |
3be5a52b | 1805 | |
f15ecfef | 1806 | spin_lock(&fi->lock); |
3be5a52b MS |
1807 | list_add(&req->writepages_entry, &fi->writepages); |
1808 | list_add_tail(&req->list, &fi->queued_writes); | |
1809 | fuse_flush_writepages(inode); | |
f15ecfef | 1810 | spin_unlock(&fi->lock); |
3be5a52b | 1811 | |
4a4ac4eb MP |
1812 | end_page_writeback(page); |
1813 | ||
3be5a52b MS |
1814 | return 0; |
1815 | ||
27f1b363 MP |
1816 | err_nofile: |
1817 | __free_page(tmp_page); | |
3be5a52b MS |
1818 | err_free: |
1819 | fuse_request_free(req); | |
1820 | err: | |
9183976e | 1821 | mapping_set_error(page->mapping, error); |
3be5a52b | 1822 | end_page_writeback(page); |
72523425 | 1823 | return error; |
3be5a52b MS |
1824 | } |
1825 | ||
1826 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1827 | { | |
1828 | int err; | |
1829 | ||
ff17be08 MS |
1830 | if (fuse_page_is_writeback(page->mapping->host, page->index)) { |
1831 | /* | |
1832 | * ->writepages() should be called for sync() and friends. We | |
1833 | * should only get here on direct reclaim and then we are | |
1834 | * allowed to skip a page which is already in flight | |
1835 | */ | |
1836 | WARN_ON(wbc->sync_mode == WB_SYNC_ALL); | |
1837 | ||
1838 | redirty_page_for_writepage(wbc, page); | |
1839 | return 0; | |
1840 | } | |
1841 | ||
3be5a52b MS |
1842 | err = fuse_writepage_locked(page); |
1843 | unlock_page(page); | |
1844 | ||
1845 | return err; | |
1846 | } | |
1847 | ||
26d614df PE |
1848 | struct fuse_fill_wb_data { |
1849 | struct fuse_req *req; | |
1850 | struct fuse_file *ff; | |
1851 | struct inode *inode; | |
2d033eaa | 1852 | struct page **orig_pages; |
26d614df PE |
1853 | }; |
1854 | ||
1855 | static void fuse_writepages_send(struct fuse_fill_wb_data *data) | |
1856 | { | |
1857 | struct fuse_req *req = data->req; | |
1858 | struct inode *inode = data->inode; | |
26d614df | 1859 | struct fuse_inode *fi = get_fuse_inode(inode); |
2d033eaa MP |
1860 | int num_pages = req->num_pages; |
1861 | int i; | |
26d614df PE |
1862 | |
1863 | req->ff = fuse_file_get(data->ff); | |
f15ecfef | 1864 | spin_lock(&fi->lock); |
26d614df PE |
1865 | list_add_tail(&req->list, &fi->queued_writes); |
1866 | fuse_flush_writepages(inode); | |
f15ecfef | 1867 | spin_unlock(&fi->lock); |
2d033eaa MP |
1868 | |
1869 | for (i = 0; i < num_pages; i++) | |
1870 | end_page_writeback(data->orig_pages[i]); | |
26d614df PE |
1871 | } |
1872 | ||
7f305ca1 MS |
1873 | /* |
1874 | * First recheck under fi->lock if the offending offset is still under | |
419234d5 MS |
1875 | * writeback. If yes, then iterate auxiliary write requests, to see if there's |
1876 | * one already added for a page at this offset. If there's none, then insert | |
1877 | * this new request onto the auxiliary list, otherwise reuse the existing one by | |
7f305ca1 MS |
1878 | * copying the new page contents over to the old temporary page. |
1879 | */ | |
8b284dc4 MS |
1880 | static bool fuse_writepage_in_flight(struct fuse_req *new_req, |
1881 | struct page *page) | |
1882 | { | |
1883 | struct fuse_conn *fc = get_fuse_conn(new_req->inode); | |
1884 | struct fuse_inode *fi = get_fuse_inode(new_req->inode); | |
1885 | struct fuse_req *tmp; | |
1886 | struct fuse_req *old_req; | |
8b284dc4 | 1887 | |
7f305ca1 | 1888 | WARN_ON(new_req->num_pages != 0); |
8b284dc4 | 1889 | |
f15ecfef | 1890 | spin_lock(&fi->lock); |
8b284dc4 | 1891 | list_del(&new_req->writepages_entry); |
2fe93bd4 MS |
1892 | old_req = fuse_find_writeback(fi, page->index, page->index); |
1893 | if (!old_req) { | |
f6011081 | 1894 | list_add(&new_req->writepages_entry, &fi->writepages); |
f15ecfef | 1895 | spin_unlock(&fi->lock); |
2fe93bd4 | 1896 | return false; |
f6011081 | 1897 | } |
8b284dc4 | 1898 | |
f6011081 | 1899 | new_req->num_pages = 1; |
419234d5 | 1900 | for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) { |
7f305ca1 MS |
1901 | pgoff_t curr_index; |
1902 | ||
1903 | WARN_ON(tmp->inode != new_req->inode); | |
09cbfeaf | 1904 | curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT; |
419234d5 MS |
1905 | if (curr_index == page->index) { |
1906 | WARN_ON(tmp->num_pages != 1); | |
1907 | WARN_ON(!test_bit(FR_PENDING, &tmp->flags)); | |
c5de16cc | 1908 | swap(tmp->pages[0], new_req->pages[0]); |
7f305ca1 | 1909 | break; |
8b284dc4 MS |
1910 | } |
1911 | } | |
1912 | ||
7f305ca1 MS |
1913 | if (!tmp) { |
1914 | new_req->misc.write.next = old_req->misc.write.next; | |
1915 | old_req->misc.write.next = new_req; | |
1916 | } | |
41b6e41f | 1917 | |
f15ecfef | 1918 | spin_unlock(&fi->lock); |
7f305ca1 MS |
1919 | |
1920 | if (tmp) { | |
1921 | struct backing_dev_info *bdi = inode_to_bdi(new_req->inode); | |
8b284dc4 | 1922 | |
93f78d88 | 1923 | dec_wb_stat(&bdi->wb, WB_WRITEBACK); |
a2ebba82 | 1924 | dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP); |
93f78d88 | 1925 | wb_writeout_inc(&bdi->wb); |
8b284dc4 MS |
1926 | fuse_writepage_free(fc, new_req); |
1927 | fuse_request_free(new_req); | |
8b284dc4 | 1928 | } |
7f305ca1 | 1929 | |
2fe93bd4 | 1930 | return true; |
8b284dc4 MS |
1931 | } |
1932 | ||
26d614df PE |
1933 | static int fuse_writepages_fill(struct page *page, |
1934 | struct writeback_control *wbc, void *_data) | |
1935 | { | |
1936 | struct fuse_fill_wb_data *data = _data; | |
1937 | struct fuse_req *req = data->req; | |
1938 | struct inode *inode = data->inode; | |
f15ecfef | 1939 | struct fuse_inode *fi = get_fuse_inode(inode); |
26d614df PE |
1940 | struct fuse_conn *fc = get_fuse_conn(inode); |
1941 | struct page *tmp_page; | |
8b284dc4 | 1942 | bool is_writeback; |
26d614df PE |
1943 | int err; |
1944 | ||
1945 | if (!data->ff) { | |
1946 | err = -EIO; | |
1947 | data->ff = fuse_write_file_get(fc, get_fuse_inode(inode)); | |
1948 | if (!data->ff) | |
1949 | goto out_unlock; | |
1950 | } | |
1951 | ||
8b284dc4 MS |
1952 | /* |
1953 | * Being under writeback is unlikely but possible. For example direct | |
1954 | * read to an mmaped fuse file will set the page dirty twice; once when | |
1955 | * the pages are faulted with get_user_pages(), and then after the read | |
1956 | * completed. | |
1957 | */ | |
1958 | is_writeback = fuse_page_is_writeback(inode, page->index); | |
1959 | ||
1960 | if (req && req->num_pages && | |
5da784cc | 1961 | (is_writeback || req->num_pages == fc->max_pages || |
09cbfeaf | 1962 | (req->num_pages + 1) * PAGE_SIZE > fc->max_write || |
8b284dc4 MS |
1963 | data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) { |
1964 | fuse_writepages_send(data); | |
1965 | data->req = NULL; | |
e52a8250 MS |
1966 | } else if (req && req->num_pages == req->max_pages) { |
1967 | if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) { | |
1968 | fuse_writepages_send(data); | |
1969 | req = data->req = NULL; | |
1970 | } | |
26d614df | 1971 | } |
e52a8250 | 1972 | |
26d614df PE |
1973 | err = -ENOMEM; |
1974 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
1975 | if (!tmp_page) | |
1976 | goto out_unlock; | |
1977 | ||
1978 | /* | |
1979 | * The page must not be redirtied until the writeout is completed | |
1980 | * (i.e. userspace has sent a reply to the write request). Otherwise | |
1981 | * there could be more than one temporary page instance for each real | |
1982 | * page. | |
1983 | * | |
1984 | * This is ensured by holding the page lock in page_mkwrite() while | |
1985 | * checking fuse_page_is_writeback(). We already hold the page lock | |
1986 | * since clear_page_dirty_for_io() and keep it held until we add the | |
1987 | * request to the fi->writepages list and increment req->num_pages. | |
1988 | * After this fuse_page_is_writeback() will indicate that the page is | |
1989 | * under writeback, so we can release the page lock. | |
1990 | */ | |
1991 | if (data->req == NULL) { | |
1992 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1993 | ||
1994 | err = -ENOMEM; | |
e52a8250 | 1995 | req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES); |
26d614df PE |
1996 | if (!req) { |
1997 | __free_page(tmp_page); | |
1998 | goto out_unlock; | |
1999 | } | |
2000 | ||
2001 | fuse_write_fill(req, data->ff, page_offset(page), 0); | |
2002 | req->misc.write.in.write_flags |= FUSE_WRITE_CACHE; | |
8b284dc4 | 2003 | req->misc.write.next = NULL; |
26d614df | 2004 | req->in.argpages = 1; |
825d6d33 | 2005 | __set_bit(FR_BACKGROUND, &req->flags); |
26d614df PE |
2006 | req->num_pages = 0; |
2007 | req->end = fuse_writepage_end; | |
2008 | req->inode = inode; | |
2009 | ||
f15ecfef | 2010 | spin_lock(&fi->lock); |
26d614df | 2011 | list_add(&req->writepages_entry, &fi->writepages); |
f15ecfef | 2012 | spin_unlock(&fi->lock); |
26d614df PE |
2013 | |
2014 | data->req = req; | |
2015 | } | |
2016 | set_page_writeback(page); | |
2017 | ||
2018 | copy_highpage(tmp_page, page); | |
2019 | req->pages[req->num_pages] = tmp_page; | |
2020 | req->page_descs[req->num_pages].offset = 0; | |
2021 | req->page_descs[req->num_pages].length = PAGE_SIZE; | |
2022 | ||
93f78d88 | 2023 | inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK); |
11fb9989 | 2024 | inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP); |
8b284dc4 MS |
2025 | |
2026 | err = 0; | |
2027 | if (is_writeback && fuse_writepage_in_flight(req, page)) { | |
2028 | end_page_writeback(page); | |
2029 | data->req = NULL; | |
2030 | goto out_unlock; | |
2031 | } | |
2d033eaa | 2032 | data->orig_pages[req->num_pages] = page; |
26d614df PE |
2033 | |
2034 | /* | |
f15ecfef | 2035 | * Protected by fi->lock against concurrent access by |
26d614df PE |
2036 | * fuse_page_is_writeback(). |
2037 | */ | |
f15ecfef | 2038 | spin_lock(&fi->lock); |
26d614df | 2039 | req->num_pages++; |
f15ecfef | 2040 | spin_unlock(&fi->lock); |
26d614df | 2041 | |
26d614df PE |
2042 | out_unlock: |
2043 | unlock_page(page); | |
2044 | ||
2045 | return err; | |
2046 | } | |
2047 | ||
2048 | static int fuse_writepages(struct address_space *mapping, | |
2049 | struct writeback_control *wbc) | |
2050 | { | |
2051 | struct inode *inode = mapping->host; | |
5da784cc | 2052 | struct fuse_conn *fc = get_fuse_conn(inode); |
26d614df PE |
2053 | struct fuse_fill_wb_data data; |
2054 | int err; | |
2055 | ||
2056 | err = -EIO; | |
2057 | if (is_bad_inode(inode)) | |
2058 | goto out; | |
2059 | ||
2060 | data.inode = inode; | |
2061 | data.req = NULL; | |
2062 | data.ff = NULL; | |
2063 | ||
2d033eaa | 2064 | err = -ENOMEM; |
5da784cc | 2065 | data.orig_pages = kcalloc(fc->max_pages, |
f2b3455e | 2066 | sizeof(struct page *), |
2d033eaa MP |
2067 | GFP_NOFS); |
2068 | if (!data.orig_pages) | |
2069 | goto out; | |
2070 | ||
26d614df PE |
2071 | err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data); |
2072 | if (data.req) { | |
2073 | /* Ignore errors if we can write at least one page */ | |
2074 | BUG_ON(!data.req->num_pages); | |
2075 | fuse_writepages_send(&data); | |
2076 | err = 0; | |
2077 | } | |
2078 | if (data.ff) | |
2e64ff15 | 2079 | fuse_file_put(data.ff, false, false); |
2d033eaa MP |
2080 | |
2081 | kfree(data.orig_pages); | |
26d614df PE |
2082 | out: |
2083 | return err; | |
2084 | } | |
2085 | ||
6b12c1b3 PE |
2086 | /* |
2087 | * It's worthy to make sure that space is reserved on disk for the write, | |
2088 | * but how to implement it without killing performance need more thinking. | |
2089 | */ | |
2090 | static int fuse_write_begin(struct file *file, struct address_space *mapping, | |
2091 | loff_t pos, unsigned len, unsigned flags, | |
2092 | struct page **pagep, void **fsdata) | |
2093 | { | |
09cbfeaf | 2094 | pgoff_t index = pos >> PAGE_SHIFT; |
a455589f | 2095 | struct fuse_conn *fc = get_fuse_conn(file_inode(file)); |
6b12c1b3 PE |
2096 | struct page *page; |
2097 | loff_t fsize; | |
2098 | int err = -ENOMEM; | |
2099 | ||
2100 | WARN_ON(!fc->writeback_cache); | |
2101 | ||
2102 | page = grab_cache_page_write_begin(mapping, index, flags); | |
2103 | if (!page) | |
2104 | goto error; | |
2105 | ||
2106 | fuse_wait_on_page_writeback(mapping->host, page->index); | |
2107 | ||
09cbfeaf | 2108 | if (PageUptodate(page) || len == PAGE_SIZE) |
6b12c1b3 PE |
2109 | goto success; |
2110 | /* | |
2111 | * Check if the start this page comes after the end of file, in which | |
2112 | * case the readpage can be optimized away. | |
2113 | */ | |
2114 | fsize = i_size_read(mapping->host); | |
09cbfeaf KS |
2115 | if (fsize <= (pos & PAGE_MASK)) { |
2116 | size_t off = pos & ~PAGE_MASK; | |
6b12c1b3 PE |
2117 | if (off) |
2118 | zero_user_segment(page, 0, off); | |
2119 | goto success; | |
2120 | } | |
2121 | err = fuse_do_readpage(file, page); | |
2122 | if (err) | |
2123 | goto cleanup; | |
2124 | success: | |
2125 | *pagep = page; | |
2126 | return 0; | |
2127 | ||
2128 | cleanup: | |
2129 | unlock_page(page); | |
09cbfeaf | 2130 | put_page(page); |
6b12c1b3 PE |
2131 | error: |
2132 | return err; | |
2133 | } | |
2134 | ||
2135 | static int fuse_write_end(struct file *file, struct address_space *mapping, | |
2136 | loff_t pos, unsigned len, unsigned copied, | |
2137 | struct page *page, void *fsdata) | |
2138 | { | |
2139 | struct inode *inode = page->mapping->host; | |
2140 | ||
59c3b76c MS |
2141 | /* Haven't copied anything? Skip zeroing, size extending, dirtying. */ |
2142 | if (!copied) | |
2143 | goto unlock; | |
2144 | ||
6b12c1b3 PE |
2145 | if (!PageUptodate(page)) { |
2146 | /* Zero any unwritten bytes at the end of the page */ | |
09cbfeaf | 2147 | size_t endoff = (pos + copied) & ~PAGE_MASK; |
6b12c1b3 | 2148 | if (endoff) |
09cbfeaf | 2149 | zero_user_segment(page, endoff, PAGE_SIZE); |
6b12c1b3 PE |
2150 | SetPageUptodate(page); |
2151 | } | |
2152 | ||
2153 | fuse_write_update_size(inode, pos + copied); | |
2154 | set_page_dirty(page); | |
59c3b76c MS |
2155 | |
2156 | unlock: | |
6b12c1b3 | 2157 | unlock_page(page); |
09cbfeaf | 2158 | put_page(page); |
6b12c1b3 PE |
2159 | |
2160 | return copied; | |
2161 | } | |
2162 | ||
3be5a52b MS |
2163 | static int fuse_launder_page(struct page *page) |
2164 | { | |
2165 | int err = 0; | |
2166 | if (clear_page_dirty_for_io(page)) { | |
2167 | struct inode *inode = page->mapping->host; | |
2168 | err = fuse_writepage_locked(page); | |
2169 | if (!err) | |
2170 | fuse_wait_on_page_writeback(inode, page->index); | |
2171 | } | |
2172 | return err; | |
2173 | } | |
2174 | ||
2175 | /* | |
2176 | * Write back dirty pages now, because there may not be any suitable | |
2177 | * open files later | |
2178 | */ | |
2179 | static void fuse_vma_close(struct vm_area_struct *vma) | |
2180 | { | |
2181 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
2182 | } | |
2183 | ||
2184 | /* | |
2185 | * Wait for writeback against this page to complete before allowing it | |
2186 | * to be marked dirty again, and hence written back again, possibly | |
2187 | * before the previous writepage completed. | |
2188 | * | |
2189 | * Block here, instead of in ->writepage(), so that the userspace fs | |
2190 | * can only block processes actually operating on the filesystem. | |
2191 | * | |
2192 | * Otherwise unprivileged userspace fs would be able to block | |
2193 | * unrelated: | |
2194 | * | |
2195 | * - page migration | |
2196 | * - sync(2) | |
2197 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
2198 | */ | |
46fb504a | 2199 | static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf) |
3be5a52b | 2200 | { |
c2ec175c | 2201 | struct page *page = vmf->page; |
11bac800 | 2202 | struct inode *inode = file_inode(vmf->vma->vm_file); |
cca24370 | 2203 | |
11bac800 | 2204 | file_update_time(vmf->vma->vm_file); |
cca24370 MS |
2205 | lock_page(page); |
2206 | if (page->mapping != inode->i_mapping) { | |
2207 | unlock_page(page); | |
2208 | return VM_FAULT_NOPAGE; | |
2209 | } | |
3be5a52b MS |
2210 | |
2211 | fuse_wait_on_page_writeback(inode, page->index); | |
cca24370 | 2212 | return VM_FAULT_LOCKED; |
3be5a52b MS |
2213 | } |
2214 | ||
f0f37e2f | 2215 | static const struct vm_operations_struct fuse_file_vm_ops = { |
3be5a52b MS |
2216 | .close = fuse_vma_close, |
2217 | .fault = filemap_fault, | |
f1820361 | 2218 | .map_pages = filemap_map_pages, |
3be5a52b MS |
2219 | .page_mkwrite = fuse_page_mkwrite, |
2220 | }; | |
2221 | ||
2222 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
2223 | { | |
55752a3a MS |
2224 | struct fuse_file *ff = file->private_data; |
2225 | ||
2226 | if (ff->open_flags & FOPEN_DIRECT_IO) { | |
2227 | /* Can't provide the coherency needed for MAP_SHARED */ | |
2228 | if (vma->vm_flags & VM_MAYSHARE) | |
2229 | return -ENODEV; | |
2230 | ||
2231 | invalidate_inode_pages2(file->f_mapping); | |
2232 | ||
2233 | return generic_file_mmap(file, vma); | |
2234 | } | |
2235 | ||
650b22b9 PE |
2236 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) |
2237 | fuse_link_write_file(file); | |
2238 | ||
3be5a52b MS |
2239 | file_accessed(file); |
2240 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
2241 | return 0; |
2242 | } | |
2243 | ||
0b6e9ea0 SF |
2244 | static int convert_fuse_file_lock(struct fuse_conn *fc, |
2245 | const struct fuse_file_lock *ffl, | |
71421259 MS |
2246 | struct file_lock *fl) |
2247 | { | |
2248 | switch (ffl->type) { | |
2249 | case F_UNLCK: | |
2250 | break; | |
2251 | ||
2252 | case F_RDLCK: | |
2253 | case F_WRLCK: | |
2254 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
2255 | ffl->end < ffl->start) | |
2256 | return -EIO; | |
2257 | ||
2258 | fl->fl_start = ffl->start; | |
2259 | fl->fl_end = ffl->end; | |
0b6e9ea0 SF |
2260 | |
2261 | /* | |
9d5b86ac BC |
2262 | * Convert pid into init's pid namespace. The locks API will |
2263 | * translate it into the caller's pid namespace. | |
0b6e9ea0 SF |
2264 | */ |
2265 | rcu_read_lock(); | |
9d5b86ac | 2266 | fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns); |
0b6e9ea0 | 2267 | rcu_read_unlock(); |
71421259 MS |
2268 | break; |
2269 | ||
2270 | default: | |
2271 | return -EIO; | |
2272 | } | |
2273 | fl->fl_type = ffl->type; | |
2274 | return 0; | |
2275 | } | |
2276 | ||
7078187a | 2277 | static void fuse_lk_fill(struct fuse_args *args, struct file *file, |
a9ff4f87 | 2278 | const struct file_lock *fl, int opcode, pid_t pid, |
7078187a | 2279 | int flock, struct fuse_lk_in *inarg) |
71421259 | 2280 | { |
6131ffaa | 2281 | struct inode *inode = file_inode(file); |
9c8ef561 | 2282 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 | 2283 | struct fuse_file *ff = file->private_data; |
7078187a MS |
2284 | |
2285 | memset(inarg, 0, sizeof(*inarg)); | |
2286 | inarg->fh = ff->fh; | |
2287 | inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner); | |
2288 | inarg->lk.start = fl->fl_start; | |
2289 | inarg->lk.end = fl->fl_end; | |
2290 | inarg->lk.type = fl->fl_type; | |
2291 | inarg->lk.pid = pid; | |
a9ff4f87 | 2292 | if (flock) |
7078187a | 2293 | inarg->lk_flags |= FUSE_LK_FLOCK; |
d5b48543 MS |
2294 | args->opcode = opcode; |
2295 | args->nodeid = get_node_id(inode); | |
2296 | args->in_numargs = 1; | |
2297 | args->in_args[0].size = sizeof(*inarg); | |
2298 | args->in_args[0].value = inarg; | |
71421259 MS |
2299 | } |
2300 | ||
2301 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
2302 | { | |
6131ffaa | 2303 | struct inode *inode = file_inode(file); |
71421259 | 2304 | struct fuse_conn *fc = get_fuse_conn(inode); |
7078187a MS |
2305 | FUSE_ARGS(args); |
2306 | struct fuse_lk_in inarg; | |
71421259 MS |
2307 | struct fuse_lk_out outarg; |
2308 | int err; | |
2309 | ||
7078187a | 2310 | fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg); |
d5b48543 MS |
2311 | args.out_numargs = 1; |
2312 | args.out_args[0].size = sizeof(outarg); | |
2313 | args.out_args[0].value = &outarg; | |
7078187a | 2314 | err = fuse_simple_request(fc, &args); |
71421259 | 2315 | if (!err) |
0b6e9ea0 | 2316 | err = convert_fuse_file_lock(fc, &outarg.lk, fl); |
71421259 MS |
2317 | |
2318 | return err; | |
2319 | } | |
2320 | ||
a9ff4f87 | 2321 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 2322 | { |
6131ffaa | 2323 | struct inode *inode = file_inode(file); |
71421259 | 2324 | struct fuse_conn *fc = get_fuse_conn(inode); |
7078187a MS |
2325 | FUSE_ARGS(args); |
2326 | struct fuse_lk_in inarg; | |
71421259 | 2327 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; |
0b6e9ea0 SF |
2328 | struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL; |
2329 | pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns); | |
71421259 MS |
2330 | int err; |
2331 | ||
8fb47a4f | 2332 | if (fl->fl_lmops && fl->fl_lmops->lm_grant) { |
48e90761 MS |
2333 | /* NLM needs asynchronous locks, which we don't support yet */ |
2334 | return -ENOLCK; | |
2335 | } | |
2336 | ||
71421259 | 2337 | /* Unlock on close is handled by the flush method */ |
50f2112c | 2338 | if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX) |
71421259 MS |
2339 | return 0; |
2340 | ||
0b6e9ea0 | 2341 | fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg); |
7078187a | 2342 | err = fuse_simple_request(fc, &args); |
71421259 | 2343 | |
a4d27e75 MS |
2344 | /* locking is restartable */ |
2345 | if (err == -EINTR) | |
2346 | err = -ERESTARTSYS; | |
7078187a | 2347 | |
71421259 MS |
2348 | return err; |
2349 | } | |
2350 | ||
2351 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
2352 | { | |
6131ffaa | 2353 | struct inode *inode = file_inode(file); |
71421259 MS |
2354 | struct fuse_conn *fc = get_fuse_conn(inode); |
2355 | int err; | |
2356 | ||
48e90761 MS |
2357 | if (cmd == F_CANCELLK) { |
2358 | err = 0; | |
2359 | } else if (cmd == F_GETLK) { | |
71421259 | 2360 | if (fc->no_lock) { |
9d6a8c5c | 2361 | posix_test_lock(file, fl); |
71421259 MS |
2362 | err = 0; |
2363 | } else | |
2364 | err = fuse_getlk(file, fl); | |
2365 | } else { | |
2366 | if (fc->no_lock) | |
48e90761 | 2367 | err = posix_lock_file(file, fl, NULL); |
71421259 | 2368 | else |
a9ff4f87 | 2369 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
2370 | } |
2371 | return err; | |
2372 | } | |
2373 | ||
a9ff4f87 MS |
2374 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
2375 | { | |
6131ffaa | 2376 | struct inode *inode = file_inode(file); |
a9ff4f87 MS |
2377 | struct fuse_conn *fc = get_fuse_conn(inode); |
2378 | int err; | |
2379 | ||
37fb3a30 | 2380 | if (fc->no_flock) { |
4f656367 | 2381 | err = locks_lock_file_wait(file, fl); |
a9ff4f87 | 2382 | } else { |
37fb3a30 MS |
2383 | struct fuse_file *ff = file->private_data; |
2384 | ||
a9ff4f87 | 2385 | /* emulate flock with POSIX locks */ |
37fb3a30 | 2386 | ff->flock = true; |
a9ff4f87 MS |
2387 | err = fuse_setlk(file, fl, 1); |
2388 | } | |
2389 | ||
2390 | return err; | |
2391 | } | |
2392 | ||
b2d2272f MS |
2393 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
2394 | { | |
2395 | struct inode *inode = mapping->host; | |
2396 | struct fuse_conn *fc = get_fuse_conn(inode); | |
7078187a | 2397 | FUSE_ARGS(args); |
b2d2272f MS |
2398 | struct fuse_bmap_in inarg; |
2399 | struct fuse_bmap_out outarg; | |
2400 | int err; | |
2401 | ||
2402 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
2403 | return 0; | |
2404 | ||
b2d2272f MS |
2405 | memset(&inarg, 0, sizeof(inarg)); |
2406 | inarg.block = block; | |
2407 | inarg.blocksize = inode->i_sb->s_blocksize; | |
d5b48543 MS |
2408 | args.opcode = FUSE_BMAP; |
2409 | args.nodeid = get_node_id(inode); | |
2410 | args.in_numargs = 1; | |
2411 | args.in_args[0].size = sizeof(inarg); | |
2412 | args.in_args[0].value = &inarg; | |
2413 | args.out_numargs = 1; | |
2414 | args.out_args[0].size = sizeof(outarg); | |
2415 | args.out_args[0].value = &outarg; | |
7078187a | 2416 | err = fuse_simple_request(fc, &args); |
b2d2272f MS |
2417 | if (err == -ENOSYS) |
2418 | fc->no_bmap = 1; | |
2419 | ||
2420 | return err ? 0 : outarg.block; | |
2421 | } | |
2422 | ||
0b5da8db R |
2423 | static loff_t fuse_lseek(struct file *file, loff_t offset, int whence) |
2424 | { | |
2425 | struct inode *inode = file->f_mapping->host; | |
2426 | struct fuse_conn *fc = get_fuse_conn(inode); | |
2427 | struct fuse_file *ff = file->private_data; | |
2428 | FUSE_ARGS(args); | |
2429 | struct fuse_lseek_in inarg = { | |
2430 | .fh = ff->fh, | |
2431 | .offset = offset, | |
2432 | .whence = whence | |
2433 | }; | |
2434 | struct fuse_lseek_out outarg; | |
2435 | int err; | |
2436 | ||
2437 | if (fc->no_lseek) | |
2438 | goto fallback; | |
2439 | ||
d5b48543 MS |
2440 | args.opcode = FUSE_LSEEK; |
2441 | args.nodeid = ff->nodeid; | |
2442 | args.in_numargs = 1; | |
2443 | args.in_args[0].size = sizeof(inarg); | |
2444 | args.in_args[0].value = &inarg; | |
2445 | args.out_numargs = 1; | |
2446 | args.out_args[0].size = sizeof(outarg); | |
2447 | args.out_args[0].value = &outarg; | |
0b5da8db R |
2448 | err = fuse_simple_request(fc, &args); |
2449 | if (err) { | |
2450 | if (err == -ENOSYS) { | |
2451 | fc->no_lseek = 1; | |
2452 | goto fallback; | |
2453 | } | |
2454 | return err; | |
2455 | } | |
2456 | ||
2457 | return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes); | |
2458 | ||
2459 | fallback: | |
5b97eeac | 2460 | err = fuse_update_attributes(inode, file); |
0b5da8db R |
2461 | if (!err) |
2462 | return generic_file_llseek(file, offset, whence); | |
2463 | else | |
2464 | return err; | |
2465 | } | |
2466 | ||
965c8e59 | 2467 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence) |
5559b8f4 MS |
2468 | { |
2469 | loff_t retval; | |
6131ffaa | 2470 | struct inode *inode = file_inode(file); |
5559b8f4 | 2471 | |
0b5da8db R |
2472 | switch (whence) { |
2473 | case SEEK_SET: | |
2474 | case SEEK_CUR: | |
2475 | /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */ | |
965c8e59 | 2476 | retval = generic_file_llseek(file, offset, whence); |
0b5da8db R |
2477 | break; |
2478 | case SEEK_END: | |
5955102c | 2479 | inode_lock(inode); |
5b97eeac | 2480 | retval = fuse_update_attributes(inode, file); |
0b5da8db R |
2481 | if (!retval) |
2482 | retval = generic_file_llseek(file, offset, whence); | |
5955102c | 2483 | inode_unlock(inode); |
0b5da8db R |
2484 | break; |
2485 | case SEEK_HOLE: | |
2486 | case SEEK_DATA: | |
5955102c | 2487 | inode_lock(inode); |
0b5da8db | 2488 | retval = fuse_lseek(file, offset, whence); |
5955102c | 2489 | inode_unlock(inode); |
0b5da8db R |
2490 | break; |
2491 | default: | |
2492 | retval = -EINVAL; | |
2493 | } | |
c07c3d19 | 2494 | |
5559b8f4 MS |
2495 | return retval; |
2496 | } | |
2497 | ||
d9d318d3 MS |
2498 | /* |
2499 | * CUSE servers compiled on 32bit broke on 64bit kernels because the | |
2500 | * ABI was defined to be 'struct iovec' which is different on 32bit | |
2501 | * and 64bit. Fortunately we can determine which structure the server | |
2502 | * used from the size of the reply. | |
2503 | */ | |
1baa26b2 MS |
2504 | static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src, |
2505 | size_t transferred, unsigned count, | |
2506 | bool is_compat) | |
d9d318d3 MS |
2507 | { |
2508 | #ifdef CONFIG_COMPAT | |
2509 | if (count * sizeof(struct compat_iovec) == transferred) { | |
2510 | struct compat_iovec *ciov = src; | |
2511 | unsigned i; | |
2512 | ||
2513 | /* | |
2514 | * With this interface a 32bit server cannot support | |
2515 | * non-compat (i.e. ones coming from 64bit apps) ioctl | |
2516 | * requests | |
2517 | */ | |
2518 | if (!is_compat) | |
2519 | return -EINVAL; | |
2520 | ||
2521 | for (i = 0; i < count; i++) { | |
2522 | dst[i].iov_base = compat_ptr(ciov[i].iov_base); | |
2523 | dst[i].iov_len = ciov[i].iov_len; | |
2524 | } | |
2525 | return 0; | |
2526 | } | |
2527 | #endif | |
2528 | ||
2529 | if (count * sizeof(struct iovec) != transferred) | |
2530 | return -EIO; | |
2531 | ||
2532 | memcpy(dst, src, transferred); | |
2533 | return 0; | |
2534 | } | |
2535 | ||
7572777e | 2536 | /* Make sure iov_length() won't overflow */ |
5da784cc CS |
2537 | static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov, |
2538 | size_t count) | |
7572777e MS |
2539 | { |
2540 | size_t n; | |
5da784cc | 2541 | u32 max = fc->max_pages << PAGE_SHIFT; |
7572777e | 2542 | |
fb6ccff6 | 2543 | for (n = 0; n < count; n++, iov++) { |
7572777e MS |
2544 | if (iov->iov_len > (size_t) max) |
2545 | return -ENOMEM; | |
2546 | max -= iov->iov_len; | |
2547 | } | |
2548 | return 0; | |
2549 | } | |
2550 | ||
1baa26b2 MS |
2551 | static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst, |
2552 | void *src, size_t transferred, unsigned count, | |
2553 | bool is_compat) | |
2554 | { | |
2555 | unsigned i; | |
2556 | struct fuse_ioctl_iovec *fiov = src; | |
2557 | ||
2558 | if (fc->minor < 16) { | |
2559 | return fuse_copy_ioctl_iovec_old(dst, src, transferred, | |
2560 | count, is_compat); | |
2561 | } | |
2562 | ||
2563 | if (count * sizeof(struct fuse_ioctl_iovec) != transferred) | |
2564 | return -EIO; | |
2565 | ||
2566 | for (i = 0; i < count; i++) { | |
2567 | /* Did the server supply an inappropriate value? */ | |
2568 | if (fiov[i].base != (unsigned long) fiov[i].base || | |
2569 | fiov[i].len != (unsigned long) fiov[i].len) | |
2570 | return -EIO; | |
2571 | ||
2572 | dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base; | |
2573 | dst[i].iov_len = (size_t) fiov[i].len; | |
2574 | ||
2575 | #ifdef CONFIG_COMPAT | |
2576 | if (is_compat && | |
2577 | (ptr_to_compat(dst[i].iov_base) != fiov[i].base || | |
2578 | (compat_size_t) dst[i].iov_len != fiov[i].len)) | |
2579 | return -EIO; | |
2580 | #endif | |
2581 | } | |
2582 | ||
2583 | return 0; | |
2584 | } | |
2585 | ||
2586 | ||
59efec7b TH |
2587 | /* |
2588 | * For ioctls, there is no generic way to determine how much memory | |
2589 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
2590 | * to dereference the passed pointer, so the parameter requires deep | |
2591 | * copying but FUSE has no idea whatsoever about what to copy in or | |
2592 | * out. | |
2593 | * | |
2594 | * This is solved by allowing FUSE server to retry ioctl with | |
2595 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
2596 | * needs to read in the following structure. | |
2597 | * | |
2598 | * struct a { | |
2599 | * char *buf; | |
2600 | * size_t buflen; | |
2601 | * } | |
2602 | * | |
2603 | * On the first callout to FUSE server, inarg->in_size and | |
2604 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
2605 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
2606 | * the actual iov array to | |
2607 | * | |
2608 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
2609 | * | |
2610 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
2611 | * On the second round, the server has access to the structure and | |
2612 | * from that it can tell what to look for next, so on the invocation, | |
2613 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
2614 | * | |
2615 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
2616 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
2617 | * | |
2618 | * FUSE will copy both struct a and the pointed buffer from the | |
2619 | * process doing the ioctl and retry ioctl with both struct a and the | |
2620 | * buffer. | |
2621 | * | |
2622 | * This time, FUSE server has everything it needs and completes ioctl | |
2623 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
2624 | * | |
2625 | * Copying data out works the same way. | |
2626 | * | |
2627 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
2628 | * automatically initializes in and out iovs by decoding @cmd with | |
2629 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
2630 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
2631 | * behavior for all FUSE servers. | |
2632 | */ | |
08cbf542 TH |
2633 | long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, |
2634 | unsigned int flags) | |
59efec7b | 2635 | { |
59efec7b | 2636 | struct fuse_file *ff = file->private_data; |
d36f2487 | 2637 | struct fuse_conn *fc = ff->fc; |
59efec7b TH |
2638 | struct fuse_ioctl_in inarg = { |
2639 | .fh = ff->fh, | |
2640 | .cmd = cmd, | |
2641 | .arg = arg, | |
2642 | .flags = flags | |
2643 | }; | |
2644 | struct fuse_ioctl_out outarg; | |
8ac83505 | 2645 | struct iovec *iov_page = NULL; |
59efec7b | 2646 | struct iovec *in_iov = NULL, *out_iov = NULL; |
093f38a2 MS |
2647 | unsigned int in_iovs = 0, out_iovs = 0, max_pages; |
2648 | size_t in_size, out_size, c; | |
2649 | ssize_t transferred; | |
acbe5fda MS |
2650 | int err, i; |
2651 | struct iov_iter ii; | |
093f38a2 | 2652 | struct fuse_args_pages ap = {}; |
59efec7b | 2653 | |
1baa26b2 MS |
2654 | #if BITS_PER_LONG == 32 |
2655 | inarg.flags |= FUSE_IOCTL_32BIT; | |
2656 | #else | |
6407f44a | 2657 | if (flags & FUSE_IOCTL_COMPAT) { |
1baa26b2 | 2658 | inarg.flags |= FUSE_IOCTL_32BIT; |
6407f44a IA |
2659 | #ifdef CONFIG_X86_X32 |
2660 | if (in_x32_syscall()) | |
2661 | inarg.flags |= FUSE_IOCTL_COMPAT_X32; | |
2662 | #endif | |
2663 | } | |
1baa26b2 MS |
2664 | #endif |
2665 | ||
59efec7b | 2666 | /* assume all the iovs returned by client always fits in a page */ |
1baa26b2 | 2667 | BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); |
59efec7b | 2668 | |
59efec7b | 2669 | err = -ENOMEM; |
093f38a2 | 2670 | ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs); |
8ac83505 | 2671 | iov_page = (struct iovec *) __get_free_page(GFP_KERNEL); |
093f38a2 | 2672 | if (!ap.pages || !iov_page) |
59efec7b TH |
2673 | goto out; |
2674 | ||
093f38a2 MS |
2675 | fuse_page_descs_length_init(ap.descs, 0, fc->max_pages); |
2676 | ||
59efec7b TH |
2677 | /* |
2678 | * If restricted, initialize IO parameters as encoded in @cmd. | |
2679 | * RETRY from server is not allowed. | |
2680 | */ | |
2681 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
8ac83505 | 2682 | struct iovec *iov = iov_page; |
59efec7b | 2683 | |
c9f0523d | 2684 | iov->iov_base = (void __user *)arg; |
59efec7b TH |
2685 | iov->iov_len = _IOC_SIZE(cmd); |
2686 | ||
2687 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
2688 | in_iov = iov; | |
2689 | in_iovs = 1; | |
2690 | } | |
2691 | ||
2692 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
2693 | out_iov = iov; | |
2694 | out_iovs = 1; | |
2695 | } | |
2696 | } | |
2697 | ||
2698 | retry: | |
2699 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
2700 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
2701 | ||
2702 | /* | |
2703 | * Out data can be used either for actual out data or iovs, | |
2704 | * make sure there always is at least one page. | |
2705 | */ | |
2706 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
2707 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
2708 | ||
2709 | /* make sure there are enough buffer pages and init request with them */ | |
2710 | err = -ENOMEM; | |
5da784cc | 2711 | if (max_pages > fc->max_pages) |
59efec7b | 2712 | goto out; |
093f38a2 MS |
2713 | while (ap.num_pages < max_pages) { |
2714 | ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
2715 | if (!ap.pages[ap.num_pages]) | |
59efec7b | 2716 | goto out; |
093f38a2 | 2717 | ap.num_pages++; |
59efec7b TH |
2718 | } |
2719 | ||
59efec7b TH |
2720 | |
2721 | /* okay, let's send it to the client */ | |
093f38a2 MS |
2722 | ap.args.opcode = FUSE_IOCTL; |
2723 | ap.args.nodeid = ff->nodeid; | |
2724 | ap.args.in_numargs = 1; | |
2725 | ap.args.in_args[0].size = sizeof(inarg); | |
2726 | ap.args.in_args[0].value = &inarg; | |
59efec7b | 2727 | if (in_size) { |
093f38a2 MS |
2728 | ap.args.in_numargs++; |
2729 | ap.args.in_args[1].size = in_size; | |
2730 | ap.args.in_pages = true; | |
59efec7b | 2731 | |
acbe5fda MS |
2732 | err = -EFAULT; |
2733 | iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size); | |
093f38a2 MS |
2734 | for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) { |
2735 | c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii); | |
acbe5fda MS |
2736 | if (c != PAGE_SIZE && iov_iter_count(&ii)) |
2737 | goto out; | |
2738 | } | |
59efec7b TH |
2739 | } |
2740 | ||
093f38a2 MS |
2741 | ap.args.out_numargs = 2; |
2742 | ap.args.out_args[0].size = sizeof(outarg); | |
2743 | ap.args.out_args[0].value = &outarg; | |
2744 | ap.args.out_args[1].size = out_size; | |
2745 | ap.args.out_pages = true; | |
2746 | ap.args.out_argvar = true; | |
59efec7b | 2747 | |
093f38a2 MS |
2748 | transferred = fuse_simple_request(fc, &ap.args); |
2749 | err = transferred; | |
2750 | if (transferred < 0) | |
59efec7b TH |
2751 | goto out; |
2752 | ||
2753 | /* did it ask for retry? */ | |
2754 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
8ac83505 | 2755 | void *vaddr; |
59efec7b TH |
2756 | |
2757 | /* no retry if in restricted mode */ | |
2758 | err = -EIO; | |
2759 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
2760 | goto out; | |
2761 | ||
2762 | in_iovs = outarg.in_iovs; | |
2763 | out_iovs = outarg.out_iovs; | |
2764 | ||
2765 | /* | |
2766 | * Make sure things are in boundary, separate checks | |
2767 | * are to protect against overflow. | |
2768 | */ | |
2769 | err = -ENOMEM; | |
2770 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
2771 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
2772 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
2773 | goto out; | |
2774 | ||
093f38a2 | 2775 | vaddr = kmap_atomic(ap.pages[0]); |
1baa26b2 | 2776 | err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr, |
d9d318d3 MS |
2777 | transferred, in_iovs + out_iovs, |
2778 | (flags & FUSE_IOCTL_COMPAT) != 0); | |
2408f6ef | 2779 | kunmap_atomic(vaddr); |
d9d318d3 MS |
2780 | if (err) |
2781 | goto out; | |
59efec7b | 2782 | |
8ac83505 | 2783 | in_iov = iov_page; |
59efec7b TH |
2784 | out_iov = in_iov + in_iovs; |
2785 | ||
5da784cc | 2786 | err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs); |
7572777e MS |
2787 | if (err) |
2788 | goto out; | |
2789 | ||
5da784cc | 2790 | err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs); |
7572777e MS |
2791 | if (err) |
2792 | goto out; | |
2793 | ||
59efec7b TH |
2794 | goto retry; |
2795 | } | |
2796 | ||
2797 | err = -EIO; | |
2798 | if (transferred > inarg.out_size) | |
2799 | goto out; | |
2800 | ||
acbe5fda MS |
2801 | err = -EFAULT; |
2802 | iov_iter_init(&ii, READ, out_iov, out_iovs, transferred); | |
093f38a2 MS |
2803 | for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) { |
2804 | c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii); | |
acbe5fda MS |
2805 | if (c != PAGE_SIZE && iov_iter_count(&ii)) |
2806 | goto out; | |
2807 | } | |
2808 | err = 0; | |
59efec7b | 2809 | out: |
8ac83505 | 2810 | free_page((unsigned long) iov_page); |
093f38a2 MS |
2811 | while (ap.num_pages) |
2812 | __free_page(ap.pages[--ap.num_pages]); | |
2813 | kfree(ap.pages); | |
59efec7b TH |
2814 | |
2815 | return err ? err : outarg.result; | |
2816 | } | |
08cbf542 | 2817 | EXPORT_SYMBOL_GPL(fuse_do_ioctl); |
59efec7b | 2818 | |
b18da0c5 MS |
2819 | long fuse_ioctl_common(struct file *file, unsigned int cmd, |
2820 | unsigned long arg, unsigned int flags) | |
d36f2487 | 2821 | { |
6131ffaa | 2822 | struct inode *inode = file_inode(file); |
d36f2487 MS |
2823 | struct fuse_conn *fc = get_fuse_conn(inode); |
2824 | ||
c2132c1b | 2825 | if (!fuse_allow_current_process(fc)) |
d36f2487 MS |
2826 | return -EACCES; |
2827 | ||
2828 | if (is_bad_inode(inode)) | |
2829 | return -EIO; | |
2830 | ||
2831 | return fuse_do_ioctl(file, cmd, arg, flags); | |
2832 | } | |
2833 | ||
59efec7b TH |
2834 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, |
2835 | unsigned long arg) | |
2836 | { | |
b18da0c5 | 2837 | return fuse_ioctl_common(file, cmd, arg, 0); |
59efec7b TH |
2838 | } |
2839 | ||
2840 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
2841 | unsigned long arg) | |
2842 | { | |
b18da0c5 | 2843 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT); |
59efec7b TH |
2844 | } |
2845 | ||
95668a69 TH |
2846 | /* |
2847 | * All files which have been polled are linked to RB tree | |
2848 | * fuse_conn->polled_files which is indexed by kh. Walk the tree and | |
2849 | * find the matching one. | |
2850 | */ | |
2851 | static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh, | |
2852 | struct rb_node **parent_out) | |
2853 | { | |
2854 | struct rb_node **link = &fc->polled_files.rb_node; | |
2855 | struct rb_node *last = NULL; | |
2856 | ||
2857 | while (*link) { | |
2858 | struct fuse_file *ff; | |
2859 | ||
2860 | last = *link; | |
2861 | ff = rb_entry(last, struct fuse_file, polled_node); | |
2862 | ||
2863 | if (kh < ff->kh) | |
2864 | link = &last->rb_left; | |
2865 | else if (kh > ff->kh) | |
2866 | link = &last->rb_right; | |
2867 | else | |
2868 | return link; | |
2869 | } | |
2870 | ||
2871 | if (parent_out) | |
2872 | *parent_out = last; | |
2873 | return link; | |
2874 | } | |
2875 | ||
2876 | /* | |
2877 | * The file is about to be polled. Make sure it's on the polled_files | |
2878 | * RB tree. Note that files once added to the polled_files tree are | |
2879 | * not removed before the file is released. This is because a file | |
2880 | * polled once is likely to be polled again. | |
2881 | */ | |
2882 | static void fuse_register_polled_file(struct fuse_conn *fc, | |
2883 | struct fuse_file *ff) | |
2884 | { | |
2885 | spin_lock(&fc->lock); | |
2886 | if (RB_EMPTY_NODE(&ff->polled_node)) { | |
f3846266 | 2887 | struct rb_node **link, *uninitialized_var(parent); |
95668a69 TH |
2888 | |
2889 | link = fuse_find_polled_node(fc, ff->kh, &parent); | |
2890 | BUG_ON(*link); | |
2891 | rb_link_node(&ff->polled_node, parent, link); | |
2892 | rb_insert_color(&ff->polled_node, &fc->polled_files); | |
2893 | } | |
2894 | spin_unlock(&fc->lock); | |
2895 | } | |
2896 | ||
076ccb76 | 2897 | __poll_t fuse_file_poll(struct file *file, poll_table *wait) |
95668a69 | 2898 | { |
95668a69 | 2899 | struct fuse_file *ff = file->private_data; |
797759aa | 2900 | struct fuse_conn *fc = ff->fc; |
95668a69 TH |
2901 | struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh }; |
2902 | struct fuse_poll_out outarg; | |
7078187a | 2903 | FUSE_ARGS(args); |
95668a69 TH |
2904 | int err; |
2905 | ||
2906 | if (fc->no_poll) | |
2907 | return DEFAULT_POLLMASK; | |
2908 | ||
2909 | poll_wait(file, &ff->poll_wait, wait); | |
c71d227f | 2910 | inarg.events = mangle_poll(poll_requested_events(wait)); |
95668a69 TH |
2911 | |
2912 | /* | |
2913 | * Ask for notification iff there's someone waiting for it. | |
2914 | * The client may ignore the flag and always notify. | |
2915 | */ | |
2916 | if (waitqueue_active(&ff->poll_wait)) { | |
2917 | inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY; | |
2918 | fuse_register_polled_file(fc, ff); | |
2919 | } | |
2920 | ||
d5b48543 MS |
2921 | args.opcode = FUSE_POLL; |
2922 | args.nodeid = ff->nodeid; | |
2923 | args.in_numargs = 1; | |
2924 | args.in_args[0].size = sizeof(inarg); | |
2925 | args.in_args[0].value = &inarg; | |
2926 | args.out_numargs = 1; | |
2927 | args.out_args[0].size = sizeof(outarg); | |
2928 | args.out_args[0].value = &outarg; | |
7078187a | 2929 | err = fuse_simple_request(fc, &args); |
95668a69 TH |
2930 | |
2931 | if (!err) | |
c71d227f | 2932 | return demangle_poll(outarg.revents); |
95668a69 TH |
2933 | if (err == -ENOSYS) { |
2934 | fc->no_poll = 1; | |
2935 | return DEFAULT_POLLMASK; | |
2936 | } | |
a9a08845 | 2937 | return EPOLLERR; |
95668a69 | 2938 | } |
08cbf542 | 2939 | EXPORT_SYMBOL_GPL(fuse_file_poll); |
95668a69 TH |
2940 | |
2941 | /* | |
2942 | * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and | |
2943 | * wakes up the poll waiters. | |
2944 | */ | |
2945 | int fuse_notify_poll_wakeup(struct fuse_conn *fc, | |
2946 | struct fuse_notify_poll_wakeup_out *outarg) | |
2947 | { | |
2948 | u64 kh = outarg->kh; | |
2949 | struct rb_node **link; | |
2950 | ||
2951 | spin_lock(&fc->lock); | |
2952 | ||
2953 | link = fuse_find_polled_node(fc, kh, NULL); | |
2954 | if (*link) { | |
2955 | struct fuse_file *ff; | |
2956 | ||
2957 | ff = rb_entry(*link, struct fuse_file, polled_node); | |
2958 | wake_up_interruptible_sync(&ff->poll_wait); | |
2959 | } | |
2960 | ||
2961 | spin_unlock(&fc->lock); | |
2962 | return 0; | |
2963 | } | |
2964 | ||
efb9fa9e MP |
2965 | static void fuse_do_truncate(struct file *file) |
2966 | { | |
2967 | struct inode *inode = file->f_mapping->host; | |
2968 | struct iattr attr; | |
2969 | ||
2970 | attr.ia_valid = ATTR_SIZE; | |
2971 | attr.ia_size = i_size_read(inode); | |
2972 | ||
2973 | attr.ia_file = file; | |
2974 | attr.ia_valid |= ATTR_FILE; | |
2975 | ||
62490330 | 2976 | fuse_do_setattr(file_dentry(file), &attr, file); |
efb9fa9e MP |
2977 | } |
2978 | ||
5da784cc | 2979 | static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off) |
e5c5f05d | 2980 | { |
5da784cc | 2981 | return round_up(off, fc->max_pages << PAGE_SHIFT); |
e5c5f05d MP |
2982 | } |
2983 | ||
4273b793 | 2984 | static ssize_t |
c8b8e32d | 2985 | fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
4273b793 | 2986 | { |
9d5722b7 | 2987 | DECLARE_COMPLETION_ONSTACK(wait); |
4273b793 | 2988 | ssize_t ret = 0; |
60b9df7a MS |
2989 | struct file *file = iocb->ki_filp; |
2990 | struct fuse_file *ff = file->private_data; | |
e5c5f05d | 2991 | bool async_dio = ff->fc->async_dio; |
4273b793 | 2992 | loff_t pos = 0; |
bcba24cc MP |
2993 | struct inode *inode; |
2994 | loff_t i_size; | |
a6cbcd4a | 2995 | size_t count = iov_iter_count(iter); |
c8b8e32d | 2996 | loff_t offset = iocb->ki_pos; |
36cf66ed | 2997 | struct fuse_io_priv *io; |
4273b793 | 2998 | |
4273b793 | 2999 | pos = offset; |
bcba24cc MP |
3000 | inode = file->f_mapping->host; |
3001 | i_size = i_size_read(inode); | |
4273b793 | 3002 | |
6f673763 | 3003 | if ((iov_iter_rw(iter) == READ) && (offset > i_size)) |
9fe55eea SW |
3004 | return 0; |
3005 | ||
439ee5f0 | 3006 | /* optimization for short read */ |
6f673763 | 3007 | if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) { |
439ee5f0 MP |
3008 | if (offset >= i_size) |
3009 | return 0; | |
5da784cc | 3010 | iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset)); |
6b775b18 | 3011 | count = iov_iter_count(iter); |
439ee5f0 MP |
3012 | } |
3013 | ||
bcba24cc | 3014 | io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL); |
36cf66ed MP |
3015 | if (!io) |
3016 | return -ENOMEM; | |
bcba24cc | 3017 | spin_lock_init(&io->lock); |
744742d6 | 3018 | kref_init(&io->refcnt); |
bcba24cc MP |
3019 | io->reqs = 1; |
3020 | io->bytes = -1; | |
3021 | io->size = 0; | |
3022 | io->offset = offset; | |
6f673763 | 3023 | io->write = (iov_iter_rw(iter) == WRITE); |
bcba24cc | 3024 | io->err = 0; |
bcba24cc MP |
3025 | /* |
3026 | * By default, we want to optimize all I/Os with async request | |
60b9df7a | 3027 | * submission to the client filesystem if supported. |
bcba24cc | 3028 | */ |
e5c5f05d | 3029 | io->async = async_dio; |
bcba24cc | 3030 | io->iocb = iocb; |
7879c4e5 | 3031 | io->blocking = is_sync_kiocb(iocb); |
bcba24cc MP |
3032 | |
3033 | /* | |
7879c4e5 AS |
3034 | * We cannot asynchronously extend the size of a file. |
3035 | * In such case the aio will behave exactly like sync io. | |
bcba24cc | 3036 | */ |
7879c4e5 AS |
3037 | if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE) |
3038 | io->blocking = true; | |
4273b793 | 3039 | |
7879c4e5 | 3040 | if (io->async && io->blocking) { |
744742d6 SF |
3041 | /* |
3042 | * Additional reference to keep io around after | |
3043 | * calling fuse_aio_complete() | |
3044 | */ | |
3045 | kref_get(&io->refcnt); | |
9d5722b7 | 3046 | io->done = &wait; |
744742d6 | 3047 | } |
9d5722b7 | 3048 | |
6f673763 | 3049 | if (iov_iter_rw(iter) == WRITE) { |
6b775b18 | 3050 | ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE); |
812408fb AV |
3051 | fuse_invalidate_attr(inode); |
3052 | } else { | |
d22a943f | 3053 | ret = __fuse_direct_read(io, iter, &pos); |
812408fb | 3054 | } |
36cf66ed | 3055 | |
bcba24cc | 3056 | if (io->async) { |
ebacb812 LC |
3057 | bool blocking = io->blocking; |
3058 | ||
bcba24cc MP |
3059 | fuse_aio_complete(io, ret < 0 ? ret : 0, -1); |
3060 | ||
3061 | /* we have a non-extending, async request, so return */ | |
ebacb812 | 3062 | if (!blocking) |
bcba24cc MP |
3063 | return -EIOCBQUEUED; |
3064 | ||
9d5722b7 CH |
3065 | wait_for_completion(&wait); |
3066 | ret = fuse_get_res_by_io(io); | |
bcba24cc MP |
3067 | } |
3068 | ||
744742d6 | 3069 | kref_put(&io->refcnt, fuse_io_release); |
9d5722b7 | 3070 | |
6f673763 | 3071 | if (iov_iter_rw(iter) == WRITE) { |
efb9fa9e MP |
3072 | if (ret > 0) |
3073 | fuse_write_update_size(inode, pos); | |
3074 | else if (ret < 0 && offset + count > i_size) | |
3075 | fuse_do_truncate(file); | |
3076 | } | |
4273b793 AA |
3077 | |
3078 | return ret; | |
3079 | } | |
3080 | ||
26eb3bae MS |
3081 | static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end) |
3082 | { | |
3083 | int err = filemap_write_and_wait_range(inode->i_mapping, start, end); | |
3084 | ||
3085 | if (!err) | |
3086 | fuse_sync_writes(inode); | |
3087 | ||
3088 | return err; | |
3089 | } | |
3090 | ||
cdadb11c MS |
3091 | static long fuse_file_fallocate(struct file *file, int mode, loff_t offset, |
3092 | loff_t length) | |
05ba1f08 AP |
3093 | { |
3094 | struct fuse_file *ff = file->private_data; | |
1c68271c | 3095 | struct inode *inode = file_inode(file); |
0ab08f57 | 3096 | struct fuse_inode *fi = get_fuse_inode(inode); |
05ba1f08 | 3097 | struct fuse_conn *fc = ff->fc; |
7078187a | 3098 | FUSE_ARGS(args); |
05ba1f08 AP |
3099 | struct fuse_fallocate_in inarg = { |
3100 | .fh = ff->fh, | |
3101 | .offset = offset, | |
3102 | .length = length, | |
3103 | .mode = mode | |
3104 | }; | |
3105 | int err; | |
14c14414 MP |
3106 | bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) || |
3107 | (mode & FALLOC_FL_PUNCH_HOLE); | |
05ba1f08 | 3108 | |
4adb8302 MS |
3109 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
3110 | return -EOPNOTSUPP; | |
3111 | ||
519c6040 MS |
3112 | if (fc->no_fallocate) |
3113 | return -EOPNOTSUPP; | |
3114 | ||
14c14414 | 3115 | if (lock_inode) { |
5955102c | 3116 | inode_lock(inode); |
bde52788 MP |
3117 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
3118 | loff_t endbyte = offset + length - 1; | |
26eb3bae MS |
3119 | |
3120 | err = fuse_writeback_range(inode, offset, endbyte); | |
bde52788 MP |
3121 | if (err) |
3122 | goto out; | |
bde52788 | 3123 | } |
3634a632 BF |
3124 | } |
3125 | ||
0cbade02 LB |
3126 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
3127 | offset + length > i_size_read(inode)) { | |
3128 | err = inode_newsize_ok(inode, offset + length); | |
3129 | if (err) | |
35d6fcbb | 3130 | goto out; |
0cbade02 LB |
3131 | } |
3132 | ||
0ab08f57 MP |
3133 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
3134 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
3135 | ||
d5b48543 MS |
3136 | args.opcode = FUSE_FALLOCATE; |
3137 | args.nodeid = ff->nodeid; | |
3138 | args.in_numargs = 1; | |
3139 | args.in_args[0].size = sizeof(inarg); | |
3140 | args.in_args[0].value = &inarg; | |
7078187a | 3141 | err = fuse_simple_request(fc, &args); |
519c6040 MS |
3142 | if (err == -ENOSYS) { |
3143 | fc->no_fallocate = 1; | |
3144 | err = -EOPNOTSUPP; | |
3145 | } | |
bee6c307 BF |
3146 | if (err) |
3147 | goto out; | |
3148 | ||
3149 | /* we could have extended the file */ | |
b0aa7606 MP |
3150 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
3151 | bool changed = fuse_write_update_size(inode, offset + length); | |
3152 | ||
93d2269d MS |
3153 | if (changed && fc->writeback_cache) |
3154 | file_update_time(file); | |
b0aa7606 | 3155 | } |
bee6c307 BF |
3156 | |
3157 | if (mode & FALLOC_FL_PUNCH_HOLE) | |
3158 | truncate_pagecache_range(inode, offset, offset + length - 1); | |
3159 | ||
3160 | fuse_invalidate_attr(inode); | |
3161 | ||
3634a632 | 3162 | out: |
0ab08f57 MP |
3163 | if (!(mode & FALLOC_FL_KEEP_SIZE)) |
3164 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state); | |
3165 | ||
bde52788 | 3166 | if (lock_inode) |
5955102c | 3167 | inode_unlock(inode); |
3634a632 | 3168 | |
05ba1f08 AP |
3169 | return err; |
3170 | } | |
05ba1f08 | 3171 | |
64bf5ff5 DC |
3172 | static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in, |
3173 | struct file *file_out, loff_t pos_out, | |
3174 | size_t len, unsigned int flags) | |
88bc7d50 NV |
3175 | { |
3176 | struct fuse_file *ff_in = file_in->private_data; | |
3177 | struct fuse_file *ff_out = file_out->private_data; | |
a2bc9236 | 3178 | struct inode *inode_in = file_inode(file_in); |
88bc7d50 NV |
3179 | struct inode *inode_out = file_inode(file_out); |
3180 | struct fuse_inode *fi_out = get_fuse_inode(inode_out); | |
3181 | struct fuse_conn *fc = ff_in->fc; | |
3182 | FUSE_ARGS(args); | |
3183 | struct fuse_copy_file_range_in inarg = { | |
3184 | .fh_in = ff_in->fh, | |
3185 | .off_in = pos_in, | |
3186 | .nodeid_out = ff_out->nodeid, | |
3187 | .fh_out = ff_out->fh, | |
3188 | .off_out = pos_out, | |
3189 | .len = len, | |
3190 | .flags = flags | |
3191 | }; | |
3192 | struct fuse_write_out outarg; | |
3193 | ssize_t err; | |
3194 | /* mark unstable when write-back is not used, and file_out gets | |
3195 | * extended */ | |
3196 | bool is_unstable = (!fc->writeback_cache) && | |
3197 | ((pos_out + len) > inode_out->i_size); | |
3198 | ||
3199 | if (fc->no_copy_file_range) | |
3200 | return -EOPNOTSUPP; | |
3201 | ||
5dae222a AG |
3202 | if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) |
3203 | return -EXDEV; | |
3204 | ||
a2bc9236 MS |
3205 | if (fc->writeback_cache) { |
3206 | inode_lock(inode_in); | |
26eb3bae | 3207 | err = fuse_writeback_range(inode_in, pos_in, pos_in + len); |
a2bc9236 MS |
3208 | inode_unlock(inode_in); |
3209 | if (err) | |
3210 | return err; | |
3211 | } | |
3212 | ||
88bc7d50 NV |
3213 | inode_lock(inode_out); |
3214 | ||
fe0da9c0 AG |
3215 | err = file_modified(file_out); |
3216 | if (err) | |
3217 | goto out; | |
3218 | ||
88bc7d50 | 3219 | if (fc->writeback_cache) { |
26eb3bae | 3220 | err = fuse_writeback_range(inode_out, pos_out, pos_out + len); |
88bc7d50 NV |
3221 | if (err) |
3222 | goto out; | |
88bc7d50 NV |
3223 | } |
3224 | ||
3225 | if (is_unstable) | |
3226 | set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); | |
3227 | ||
d5b48543 MS |
3228 | args.opcode = FUSE_COPY_FILE_RANGE; |
3229 | args.nodeid = ff_in->nodeid; | |
3230 | args.in_numargs = 1; | |
3231 | args.in_args[0].size = sizeof(inarg); | |
3232 | args.in_args[0].value = &inarg; | |
3233 | args.out_numargs = 1; | |
3234 | args.out_args[0].size = sizeof(outarg); | |
3235 | args.out_args[0].value = &outarg; | |
88bc7d50 NV |
3236 | err = fuse_simple_request(fc, &args); |
3237 | if (err == -ENOSYS) { | |
3238 | fc->no_copy_file_range = 1; | |
3239 | err = -EOPNOTSUPP; | |
3240 | } | |
3241 | if (err) | |
3242 | goto out; | |
3243 | ||
3244 | if (fc->writeback_cache) { | |
3245 | fuse_write_update_size(inode_out, pos_out + outarg.size); | |
3246 | file_update_time(file_out); | |
3247 | } | |
3248 | ||
3249 | fuse_invalidate_attr(inode_out); | |
3250 | ||
3251 | err = outarg.size; | |
3252 | out: | |
3253 | if (is_unstable) | |
3254 | clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state); | |
3255 | ||
3256 | inode_unlock(inode_out); | |
fe0da9c0 | 3257 | file_accessed(file_in); |
88bc7d50 NV |
3258 | |
3259 | return err; | |
3260 | } | |
3261 | ||
64bf5ff5 DC |
3262 | static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off, |
3263 | struct file *dst_file, loff_t dst_off, | |
3264 | size_t len, unsigned int flags) | |
3265 | { | |
3266 | ssize_t ret; | |
3267 | ||
3268 | ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off, | |
3269 | len, flags); | |
3270 | ||
5dae222a | 3271 | if (ret == -EOPNOTSUPP || ret == -EXDEV) |
64bf5ff5 DC |
3272 | ret = generic_copy_file_range(src_file, src_off, dst_file, |
3273 | dst_off, len, flags); | |
3274 | return ret; | |
3275 | } | |
3276 | ||
4b6f5d20 | 3277 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 3278 | .llseek = fuse_file_llseek, |
37c20f16 | 3279 | .read_iter = fuse_file_read_iter, |
84c3d55c | 3280 | .write_iter = fuse_file_write_iter, |
b6aeaded MS |
3281 | .mmap = fuse_file_mmap, |
3282 | .open = fuse_open, | |
3283 | .flush = fuse_flush, | |
3284 | .release = fuse_release, | |
3285 | .fsync = fuse_fsync, | |
71421259 | 3286 | .lock = fuse_file_lock, |
a9ff4f87 | 3287 | .flock = fuse_file_flock, |
3c3db095 MS |
3288 | .splice_read = generic_file_splice_read, |
3289 | .splice_write = iter_file_splice_write, | |
59efec7b TH |
3290 | .unlocked_ioctl = fuse_file_ioctl, |
3291 | .compat_ioctl = fuse_file_compat_ioctl, | |
95668a69 | 3292 | .poll = fuse_file_poll, |
05ba1f08 | 3293 | .fallocate = fuse_file_fallocate, |
d4136d60 | 3294 | .copy_file_range = fuse_copy_file_range, |
413ef8cb MS |
3295 | }; |
3296 | ||
f5e54d6e | 3297 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 3298 | .readpage = fuse_readpage, |
3be5a52b | 3299 | .writepage = fuse_writepage, |
26d614df | 3300 | .writepages = fuse_writepages, |
3be5a52b | 3301 | .launder_page = fuse_launder_page, |
db50b96c | 3302 | .readpages = fuse_readpages, |
3be5a52b | 3303 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 3304 | .bmap = fuse_bmap, |
4273b793 | 3305 | .direct_IO = fuse_direct_IO, |
6b12c1b3 PE |
3306 | .write_begin = fuse_write_begin, |
3307 | .write_end = fuse_write_end, | |
b6aeaded MS |
3308 | }; |
3309 | ||
3310 | void fuse_init_file_inode(struct inode *inode) | |
3311 | { | |
ab2257e9 MS |
3312 | struct fuse_inode *fi = get_fuse_inode(inode); |
3313 | ||
45323fb7 MS |
3314 | inode->i_fop = &fuse_file_operations; |
3315 | inode->i_data.a_ops = &fuse_file_aops; | |
ab2257e9 MS |
3316 | |
3317 | INIT_LIST_HEAD(&fi->write_files); | |
3318 | INIT_LIST_HEAD(&fi->queued_writes); | |
3319 | fi->writectr = 0; | |
3320 | init_waitqueue_head(&fi->page_waitq); | |
3321 | INIT_LIST_HEAD(&fi->writepages); | |
b6aeaded | 3322 | } |