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