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