]>
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> |
b6aeaded | 15 | |
4b6f5d20 | 16 | static const struct file_operations fuse_direct_io_file_operations; |
45323fb7 | 17 | |
fd72faac MS |
18 | static int fuse_send_open(struct inode *inode, struct file *file, int isdir, |
19 | struct fuse_open_out *outargp) | |
b6aeaded MS |
20 | { |
21 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b6aeaded | 22 | struct fuse_open_in inarg; |
fd72faac MS |
23 | struct fuse_req *req; |
24 | int err; | |
25 | ||
ce1d5a49 MS |
26 | req = fuse_get_req(fc); |
27 | if (IS_ERR(req)) | |
28 | return PTR_ERR(req); | |
fd72faac MS |
29 | |
30 | memset(&inarg, 0, sizeof(inarg)); | |
6ff958ed MS |
31 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY); |
32 | if (!fc->atomic_o_trunc) | |
33 | inarg.flags &= ~O_TRUNC; | |
fd72faac MS |
34 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; |
35 | req->in.h.nodeid = get_node_id(inode); | |
fd72faac MS |
36 | req->in.numargs = 1; |
37 | req->in.args[0].size = sizeof(inarg); | |
38 | req->in.args[0].value = &inarg; | |
39 | req->out.numargs = 1; | |
40 | req->out.args[0].size = sizeof(*outargp); | |
41 | req->out.args[0].value = outargp; | |
42 | request_send(fc, req); | |
43 | err = req->out.h.error; | |
44 | fuse_put_request(fc, req); | |
45 | ||
46 | return err; | |
47 | } | |
48 | ||
49 | struct fuse_file *fuse_file_alloc(void) | |
50 | { | |
51 | struct fuse_file *ff; | |
52 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); | |
53 | if (ff) { | |
33649c91 MS |
54 | ff->reserved_req = fuse_request_alloc(); |
55 | if (!ff->reserved_req) { | |
fd72faac MS |
56 | kfree(ff); |
57 | ff = NULL; | |
8744969a AB |
58 | } else { |
59 | INIT_LIST_HEAD(&ff->write_entry); | |
60 | atomic_set(&ff->count, 0); | |
fd72faac MS |
61 | } |
62 | } | |
63 | return ff; | |
64 | } | |
65 | ||
66 | void fuse_file_free(struct fuse_file *ff) | |
67 | { | |
33649c91 | 68 | fuse_request_free(ff->reserved_req); |
fd72faac MS |
69 | kfree(ff); |
70 | } | |
71 | ||
c756e0a4 MS |
72 | static struct fuse_file *fuse_file_get(struct fuse_file *ff) |
73 | { | |
74 | atomic_inc(&ff->count); | |
75 | return ff; | |
76 | } | |
77 | ||
819c4b3b MS |
78 | static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req) |
79 | { | |
b57d4264 MS |
80 | dput(req->misc.release.dentry); |
81 | mntput(req->misc.release.vfsmount); | |
819c4b3b MS |
82 | } |
83 | ||
c756e0a4 MS |
84 | static void fuse_file_put(struct fuse_file *ff) |
85 | { | |
86 | if (atomic_dec_and_test(&ff->count)) { | |
87 | struct fuse_req *req = ff->reserved_req; | |
b57d4264 MS |
88 | struct inode *inode = req->misc.release.dentry->d_inode; |
89 | struct fuse_conn *fc = get_fuse_conn(inode); | |
819c4b3b | 90 | req->end = fuse_release_end; |
c756e0a4 MS |
91 | request_send_background(fc, req); |
92 | kfree(ff); | |
93 | } | |
94 | } | |
95 | ||
fd72faac MS |
96 | void fuse_finish_open(struct inode *inode, struct file *file, |
97 | struct fuse_file *ff, struct fuse_open_out *outarg) | |
98 | { | |
99 | if (outarg->open_flags & FOPEN_DIRECT_IO) | |
100 | file->f_op = &fuse_direct_io_file_operations; | |
101 | if (!(outarg->open_flags & FOPEN_KEEP_CACHE)) | |
b1009979 | 102 | invalidate_inode_pages2(inode->i_mapping); |
a7c1b990 TH |
103 | if (outarg->open_flags & FOPEN_NONSEEKABLE) |
104 | nonseekable_open(inode, file); | |
fd72faac | 105 | ff->fh = outarg->fh; |
c756e0a4 | 106 | file->private_data = fuse_file_get(ff); |
fd72faac MS |
107 | } |
108 | ||
109 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) | |
110 | { | |
b6aeaded MS |
111 | struct fuse_open_out outarg; |
112 | struct fuse_file *ff; | |
113 | int err; | |
b6aeaded | 114 | |
dd190d06 MS |
115 | /* VFS checks this, but only _after_ ->open() */ |
116 | if (file->f_flags & O_DIRECT) | |
117 | return -EINVAL; | |
118 | ||
b6aeaded MS |
119 | err = generic_file_open(inode, file); |
120 | if (err) | |
121 | return err; | |
122 | ||
fd72faac | 123 | ff = fuse_file_alloc(); |
b6aeaded | 124 | if (!ff) |
fd72faac | 125 | return -ENOMEM; |
b6aeaded | 126 | |
fd72faac MS |
127 | err = fuse_send_open(inode, file, isdir, &outarg); |
128 | if (err) | |
129 | fuse_file_free(ff); | |
130 | else { | |
131 | if (isdir) | |
132 | outarg.open_flags &= ~FOPEN_DIRECT_IO; | |
133 | fuse_finish_open(inode, file, ff, &outarg); | |
b6aeaded MS |
134 | } |
135 | ||
b6aeaded MS |
136 | return err; |
137 | } | |
138 | ||
c756e0a4 | 139 | void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode) |
64c6d8ed | 140 | { |
33649c91 | 141 | struct fuse_req *req = ff->reserved_req; |
b57d4264 | 142 | struct fuse_release_in *inarg = &req->misc.release.in; |
b6aeaded MS |
143 | |
144 | inarg->fh = ff->fh; | |
fd72faac | 145 | inarg->flags = flags; |
51eb01e7 | 146 | req->in.h.opcode = opcode; |
fd72faac | 147 | req->in.h.nodeid = nodeid; |
b6aeaded MS |
148 | req->in.numargs = 1; |
149 | req->in.args[0].size = sizeof(struct fuse_release_in); | |
150 | req->in.args[0].value = inarg; | |
fd72faac MS |
151 | } |
152 | ||
153 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) | |
154 | { | |
155 | struct fuse_file *ff = file->private_data; | |
156 | if (ff) { | |
93a8c3cd | 157 | struct fuse_conn *fc = get_fuse_conn(inode); |
b57d4264 | 158 | struct fuse_req *req = ff->reserved_req; |
93a8c3cd | 159 | |
c756e0a4 MS |
160 | fuse_release_fill(ff, get_node_id(inode), file->f_flags, |
161 | isdir ? FUSE_RELEASEDIR : FUSE_RELEASE); | |
51eb01e7 MS |
162 | |
163 | /* Hold vfsmount and dentry until release is finished */ | |
b57d4264 MS |
164 | req->misc.release.vfsmount = mntget(file->f_path.mnt); |
165 | req->misc.release.dentry = dget(file->f_path.dentry); | |
93a8c3cd MS |
166 | |
167 | spin_lock(&fc->lock); | |
168 | list_del(&ff->write_entry); | |
169 | spin_unlock(&fc->lock); | |
c756e0a4 MS |
170 | /* |
171 | * Normally this will send the RELEASE request, | |
172 | * however if some asynchronous READ or WRITE requests | |
173 | * are outstanding, the sending will be delayed | |
174 | */ | |
175 | fuse_file_put(ff); | |
fd72faac | 176 | } |
b6aeaded MS |
177 | |
178 | /* Return value is ignored by VFS */ | |
179 | return 0; | |
180 | } | |
181 | ||
04730fef MS |
182 | static int fuse_open(struct inode *inode, struct file *file) |
183 | { | |
184 | return fuse_open_common(inode, file, 0); | |
185 | } | |
186 | ||
187 | static int fuse_release(struct inode *inode, struct file *file) | |
188 | { | |
189 | return fuse_release_common(inode, file, 0); | |
190 | } | |
191 | ||
71421259 | 192 | /* |
9c8ef561 MS |
193 | * Scramble the ID space with XTEA, so that the value of the files_struct |
194 | * pointer is not exposed to userspace. | |
71421259 | 195 | */ |
f3332114 | 196 | u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id) |
71421259 | 197 | { |
9c8ef561 MS |
198 | u32 *k = fc->scramble_key; |
199 | u64 v = (unsigned long) id; | |
200 | u32 v0 = v; | |
201 | u32 v1 = v >> 32; | |
202 | u32 sum = 0; | |
203 | int i; | |
204 | ||
205 | for (i = 0; i < 32; i++) { | |
206 | v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); | |
207 | sum += 0x9E3779B9; | |
208 | v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); | |
209 | } | |
210 | ||
211 | return (u64) v0 + ((u64) v1 << 32); | |
71421259 MS |
212 | } |
213 | ||
3be5a52b MS |
214 | /* |
215 | * Check if page is under writeback | |
216 | * | |
217 | * This is currently done by walking the list of writepage requests | |
218 | * for the inode, which can be pretty inefficient. | |
219 | */ | |
220 | static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index) | |
221 | { | |
222 | struct fuse_conn *fc = get_fuse_conn(inode); | |
223 | struct fuse_inode *fi = get_fuse_inode(inode); | |
224 | struct fuse_req *req; | |
225 | bool found = false; | |
226 | ||
227 | spin_lock(&fc->lock); | |
228 | list_for_each_entry(req, &fi->writepages, writepages_entry) { | |
229 | pgoff_t curr_index; | |
230 | ||
231 | BUG_ON(req->inode != inode); | |
232 | curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT; | |
233 | if (curr_index == index) { | |
234 | found = true; | |
235 | break; | |
236 | } | |
237 | } | |
238 | spin_unlock(&fc->lock); | |
239 | ||
240 | return found; | |
241 | } | |
242 | ||
243 | /* | |
244 | * Wait for page writeback to be completed. | |
245 | * | |
246 | * Since fuse doesn't rely on the VM writeback tracking, this has to | |
247 | * use some other means. | |
248 | */ | |
249 | static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index) | |
250 | { | |
251 | struct fuse_inode *fi = get_fuse_inode(inode); | |
252 | ||
253 | wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index)); | |
254 | return 0; | |
255 | } | |
256 | ||
75e1fcc0 | 257 | static int fuse_flush(struct file *file, fl_owner_t id) |
b6aeaded | 258 | { |
7706a9d6 | 259 | struct inode *inode = file->f_path.dentry->d_inode; |
b6aeaded MS |
260 | struct fuse_conn *fc = get_fuse_conn(inode); |
261 | struct fuse_file *ff = file->private_data; | |
262 | struct fuse_req *req; | |
263 | struct fuse_flush_in inarg; | |
264 | int err; | |
265 | ||
248d86e8 MS |
266 | if (is_bad_inode(inode)) |
267 | return -EIO; | |
268 | ||
b6aeaded MS |
269 | if (fc->no_flush) |
270 | return 0; | |
271 | ||
33649c91 | 272 | req = fuse_get_req_nofail(fc, file); |
b6aeaded MS |
273 | memset(&inarg, 0, sizeof(inarg)); |
274 | inarg.fh = ff->fh; | |
9c8ef561 | 275 | inarg.lock_owner = fuse_lock_owner_id(fc, id); |
b6aeaded MS |
276 | req->in.h.opcode = FUSE_FLUSH; |
277 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
278 | req->in.numargs = 1; |
279 | req->in.args[0].size = sizeof(inarg); | |
280 | req->in.args[0].value = &inarg; | |
71421259 | 281 | req->force = 1; |
7c352bdf | 282 | request_send(fc, req); |
b6aeaded MS |
283 | err = req->out.h.error; |
284 | fuse_put_request(fc, req); | |
285 | if (err == -ENOSYS) { | |
286 | fc->no_flush = 1; | |
287 | err = 0; | |
288 | } | |
289 | return err; | |
290 | } | |
291 | ||
3be5a52b MS |
292 | /* |
293 | * Wait for all pending writepages on the inode to finish. | |
294 | * | |
295 | * This is currently done by blocking further writes with FUSE_NOWRITE | |
296 | * and waiting for all sent writes to complete. | |
297 | * | |
298 | * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage | |
299 | * could conflict with truncation. | |
300 | */ | |
301 | static void fuse_sync_writes(struct inode *inode) | |
302 | { | |
303 | fuse_set_nowrite(inode); | |
304 | fuse_release_nowrite(inode); | |
305 | } | |
306 | ||
82547981 MS |
307 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, |
308 | int isdir) | |
b6aeaded MS |
309 | { |
310 | struct inode *inode = de->d_inode; | |
311 | struct fuse_conn *fc = get_fuse_conn(inode); | |
312 | struct fuse_file *ff = file->private_data; | |
313 | struct fuse_req *req; | |
314 | struct fuse_fsync_in inarg; | |
315 | int err; | |
316 | ||
248d86e8 MS |
317 | if (is_bad_inode(inode)) |
318 | return -EIO; | |
319 | ||
82547981 | 320 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) |
b6aeaded MS |
321 | return 0; |
322 | ||
3be5a52b MS |
323 | /* |
324 | * Start writeback against all dirty pages of the inode, then | |
325 | * wait for all outstanding writes, before sending the FSYNC | |
326 | * request. | |
327 | */ | |
328 | err = write_inode_now(inode, 0); | |
329 | if (err) | |
330 | return err; | |
331 | ||
332 | fuse_sync_writes(inode); | |
333 | ||
ce1d5a49 MS |
334 | req = fuse_get_req(fc); |
335 | if (IS_ERR(req)) | |
336 | return PTR_ERR(req); | |
b6aeaded MS |
337 | |
338 | memset(&inarg, 0, sizeof(inarg)); | |
339 | inarg.fh = ff->fh; | |
340 | inarg.fsync_flags = datasync ? 1 : 0; | |
82547981 | 341 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; |
b6aeaded | 342 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
343 | req->in.numargs = 1; |
344 | req->in.args[0].size = sizeof(inarg); | |
345 | req->in.args[0].value = &inarg; | |
346 | request_send(fc, req); | |
347 | err = req->out.h.error; | |
348 | fuse_put_request(fc, req); | |
349 | if (err == -ENOSYS) { | |
82547981 MS |
350 | if (isdir) |
351 | fc->no_fsyncdir = 1; | |
352 | else | |
353 | fc->no_fsync = 1; | |
b6aeaded MS |
354 | err = 0; |
355 | } | |
356 | return err; | |
357 | } | |
358 | ||
82547981 MS |
359 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) |
360 | { | |
361 | return fuse_fsync_common(file, de, datasync, 0); | |
362 | } | |
363 | ||
a6643094 | 364 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
361b1eb5 | 365 | struct inode *inode, loff_t pos, size_t count, int opcode) |
b6aeaded | 366 | { |
5c5c5e51 | 367 | struct fuse_read_in *inarg = &req->misc.read.in; |
a6643094 | 368 | struct fuse_file *ff = file->private_data; |
b6aeaded | 369 | |
361b1eb5 MS |
370 | inarg->fh = ff->fh; |
371 | inarg->offset = pos; | |
372 | inarg->size = count; | |
a6643094 | 373 | inarg->flags = file->f_flags; |
361b1eb5 | 374 | req->in.h.opcode = opcode; |
b6aeaded | 375 | req->in.h.nodeid = get_node_id(inode); |
b6aeaded MS |
376 | req->in.numargs = 1; |
377 | req->in.args[0].size = sizeof(struct fuse_read_in); | |
c1aa96a5 | 378 | req->in.args[0].value = inarg; |
b6aeaded MS |
379 | req->out.argpages = 1; |
380 | req->out.argvar = 1; | |
381 | req->out.numargs = 1; | |
382 | req->out.args[0].size = count; | |
b6aeaded MS |
383 | } |
384 | ||
8bfc016d | 385 | static size_t fuse_send_read(struct fuse_req *req, struct file *file, |
f3332114 MS |
386 | struct inode *inode, loff_t pos, size_t count, |
387 | fl_owner_t owner) | |
04730fef | 388 | { |
361b1eb5 | 389 | struct fuse_conn *fc = get_fuse_conn(inode); |
f3332114 | 390 | |
a6643094 | 391 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
f3332114 | 392 | if (owner != NULL) { |
5c5c5e51 | 393 | struct fuse_read_in *inarg = &req->misc.read.in; |
f3332114 MS |
394 | |
395 | inarg->read_flags |= FUSE_READ_LOCKOWNER; | |
396 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
397 | } | |
361b1eb5 MS |
398 | request_send(fc, req); |
399 | return req->out.args[0].size; | |
04730fef MS |
400 | } |
401 | ||
5c5c5e51 MS |
402 | static void fuse_read_update_size(struct inode *inode, loff_t size, |
403 | u64 attr_ver) | |
404 | { | |
405 | struct fuse_conn *fc = get_fuse_conn(inode); | |
406 | struct fuse_inode *fi = get_fuse_inode(inode); | |
407 | ||
408 | spin_lock(&fc->lock); | |
409 | if (attr_ver == fi->attr_version && size < inode->i_size) { | |
410 | fi->attr_version = ++fc->attr_version; | |
411 | i_size_write(inode, size); | |
412 | } | |
413 | spin_unlock(&fc->lock); | |
414 | } | |
415 | ||
b6aeaded MS |
416 | static int fuse_readpage(struct file *file, struct page *page) |
417 | { | |
418 | struct inode *inode = page->mapping->host; | |
419 | struct fuse_conn *fc = get_fuse_conn(inode); | |
248d86e8 | 420 | struct fuse_req *req; |
5c5c5e51 MS |
421 | size_t num_read; |
422 | loff_t pos = page_offset(page); | |
423 | size_t count = PAGE_CACHE_SIZE; | |
424 | u64 attr_ver; | |
248d86e8 MS |
425 | int err; |
426 | ||
427 | err = -EIO; | |
428 | if (is_bad_inode(inode)) | |
429 | goto out; | |
430 | ||
3be5a52b MS |
431 | /* |
432 | * Page writeback can extend beyond the liftime of the | |
433 | * page-cache page, so make sure we read a properly synced | |
434 | * page. | |
435 | */ | |
436 | fuse_wait_on_page_writeback(inode, page->index); | |
437 | ||
ce1d5a49 MS |
438 | req = fuse_get_req(fc); |
439 | err = PTR_ERR(req); | |
440 | if (IS_ERR(req)) | |
b6aeaded MS |
441 | goto out; |
442 | ||
5c5c5e51 MS |
443 | attr_ver = fuse_get_attr_version(fc); |
444 | ||
b6aeaded MS |
445 | req->out.page_zeroing = 1; |
446 | req->num_pages = 1; | |
447 | req->pages[0] = page; | |
5c5c5e51 | 448 | num_read = fuse_send_read(req, file, inode, pos, count, NULL); |
b6aeaded MS |
449 | err = req->out.h.error; |
450 | fuse_put_request(fc, req); | |
5c5c5e51 MS |
451 | |
452 | if (!err) { | |
453 | /* | |
454 | * Short read means EOF. If file size is larger, truncate it | |
455 | */ | |
456 | if (num_read < count) | |
457 | fuse_read_update_size(inode, pos + num_read, attr_ver); | |
458 | ||
b6aeaded | 459 | SetPageUptodate(page); |
5c5c5e51 MS |
460 | } |
461 | ||
b36c31ba | 462 | fuse_invalidate_attr(inode); /* atime changed */ |
b6aeaded MS |
463 | out: |
464 | unlock_page(page); | |
465 | return err; | |
466 | } | |
467 | ||
c1aa96a5 | 468 | static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req) |
db50b96c | 469 | { |
c1aa96a5 | 470 | int i; |
5c5c5e51 MS |
471 | size_t count = req->misc.read.in.size; |
472 | size_t num_read = req->out.args[0].size; | |
473 | struct inode *inode = req->pages[0]->mapping->host; | |
c1aa96a5 | 474 | |
5c5c5e51 MS |
475 | /* |
476 | * Short read means EOF. If file size is larger, truncate it | |
477 | */ | |
478 | if (!req->out.h.error && num_read < count) { | |
479 | loff_t pos = page_offset(req->pages[0]) + num_read; | |
480 | fuse_read_update_size(inode, pos, req->misc.read.attr_ver); | |
481 | } | |
482 | ||
483 | fuse_invalidate_attr(inode); /* atime changed */ | |
c1aa96a5 | 484 | |
db50b96c MS |
485 | for (i = 0; i < req->num_pages; i++) { |
486 | struct page *page = req->pages[i]; | |
487 | if (!req->out.h.error) | |
488 | SetPageUptodate(page); | |
c1aa96a5 MS |
489 | else |
490 | SetPageError(page); | |
db50b96c MS |
491 | unlock_page(page); |
492 | } | |
c756e0a4 MS |
493 | if (req->ff) |
494 | fuse_file_put(req->ff); | |
c1aa96a5 MS |
495 | } |
496 | ||
a6643094 | 497 | static void fuse_send_readpages(struct fuse_req *req, struct file *file, |
c1aa96a5 MS |
498 | struct inode *inode) |
499 | { | |
500 | struct fuse_conn *fc = get_fuse_conn(inode); | |
501 | loff_t pos = page_offset(req->pages[0]); | |
502 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | |
503 | req->out.page_zeroing = 1; | |
a6643094 | 504 | fuse_read_fill(req, file, inode, pos, count, FUSE_READ); |
5c5c5e51 | 505 | req->misc.read.attr_ver = fuse_get_attr_version(fc); |
9cd68455 | 506 | if (fc->async_read) { |
a6643094 | 507 | struct fuse_file *ff = file->private_data; |
c756e0a4 | 508 | req->ff = fuse_file_get(ff); |
9cd68455 MS |
509 | req->end = fuse_readpages_end; |
510 | request_send_background(fc, req); | |
511 | } else { | |
512 | request_send(fc, req); | |
513 | fuse_readpages_end(fc, req); | |
e9bb09dd | 514 | fuse_put_request(fc, req); |
9cd68455 | 515 | } |
db50b96c MS |
516 | } |
517 | ||
c756e0a4 | 518 | struct fuse_fill_data { |
db50b96c | 519 | struct fuse_req *req; |
a6643094 | 520 | struct file *file; |
db50b96c MS |
521 | struct inode *inode; |
522 | }; | |
523 | ||
524 | static int fuse_readpages_fill(void *_data, struct page *page) | |
525 | { | |
c756e0a4 | 526 | struct fuse_fill_data *data = _data; |
db50b96c MS |
527 | struct fuse_req *req = data->req; |
528 | struct inode *inode = data->inode; | |
529 | struct fuse_conn *fc = get_fuse_conn(inode); | |
530 | ||
3be5a52b MS |
531 | fuse_wait_on_page_writeback(inode, page->index); |
532 | ||
db50b96c MS |
533 | if (req->num_pages && |
534 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | |
535 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | |
536 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | |
a6643094 | 537 | fuse_send_readpages(req, data->file, inode); |
ce1d5a49 MS |
538 | data->req = req = fuse_get_req(fc); |
539 | if (IS_ERR(req)) { | |
db50b96c | 540 | unlock_page(page); |
ce1d5a49 | 541 | return PTR_ERR(req); |
db50b96c | 542 | } |
db50b96c MS |
543 | } |
544 | req->pages[req->num_pages] = page; | |
1729a16c | 545 | req->num_pages++; |
db50b96c MS |
546 | return 0; |
547 | } | |
548 | ||
549 | static int fuse_readpages(struct file *file, struct address_space *mapping, | |
550 | struct list_head *pages, unsigned nr_pages) | |
551 | { | |
552 | struct inode *inode = mapping->host; | |
553 | struct fuse_conn *fc = get_fuse_conn(inode); | |
c756e0a4 | 554 | struct fuse_fill_data data; |
db50b96c | 555 | int err; |
248d86e8 | 556 | |
1d7ea732 | 557 | err = -EIO; |
248d86e8 | 558 | if (is_bad_inode(inode)) |
2e990021 | 559 | goto out; |
248d86e8 | 560 | |
a6643094 | 561 | data.file = file; |
db50b96c | 562 | data.inode = inode; |
ce1d5a49 | 563 | data.req = fuse_get_req(fc); |
1d7ea732 | 564 | err = PTR_ERR(data.req); |
ce1d5a49 | 565 | if (IS_ERR(data.req)) |
2e990021 | 566 | goto out; |
db50b96c MS |
567 | |
568 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | |
d3406ffa MS |
569 | if (!err) { |
570 | if (data.req->num_pages) | |
a6643094 | 571 | fuse_send_readpages(data.req, file, inode); |
d3406ffa MS |
572 | else |
573 | fuse_put_request(fc, data.req); | |
574 | } | |
2e990021 | 575 | out: |
1d7ea732 | 576 | return err; |
db50b96c MS |
577 | } |
578 | ||
bcb4be80 MS |
579 | static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
580 | unsigned long nr_segs, loff_t pos) | |
581 | { | |
582 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
583 | ||
584 | if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) { | |
585 | int err; | |
586 | /* | |
587 | * If trying to read past EOF, make sure the i_size | |
588 | * attribute is up-to-date. | |
589 | */ | |
590 | err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL); | |
591 | if (err) | |
592 | return err; | |
593 | } | |
594 | ||
595 | return generic_file_aio_read(iocb, iov, nr_segs, pos); | |
596 | } | |
597 | ||
a6643094 | 598 | static void fuse_write_fill(struct fuse_req *req, struct file *file, |
3be5a52b MS |
599 | struct fuse_file *ff, struct inode *inode, |
600 | loff_t pos, size_t count, int writepage) | |
b6aeaded | 601 | { |
f3332114 | 602 | struct fuse_conn *fc = get_fuse_conn(inode); |
b25e82e5 MS |
603 | struct fuse_write_in *inarg = &req->misc.write.in; |
604 | struct fuse_write_out *outarg = &req->misc.write.out; | |
b6aeaded | 605 | |
b25e82e5 MS |
606 | memset(inarg, 0, sizeof(struct fuse_write_in)); |
607 | inarg->fh = ff->fh; | |
608 | inarg->offset = pos; | |
609 | inarg->size = count; | |
610 | inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0; | |
3be5a52b | 611 | inarg->flags = file ? file->f_flags : 0; |
b6aeaded MS |
612 | req->in.h.opcode = FUSE_WRITE; |
613 | req->in.h.nodeid = get_node_id(inode); | |
b6aeaded MS |
614 | req->in.argpages = 1; |
615 | req->in.numargs = 2; | |
f3332114 MS |
616 | if (fc->minor < 9) |
617 | req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE; | |
618 | else | |
619 | req->in.args[0].size = sizeof(struct fuse_write_in); | |
b25e82e5 | 620 | req->in.args[0].value = inarg; |
b6aeaded MS |
621 | req->in.args[1].size = count; |
622 | req->out.numargs = 1; | |
623 | req->out.args[0].size = sizeof(struct fuse_write_out); | |
b25e82e5 MS |
624 | req->out.args[0].value = outarg; |
625 | } | |
626 | ||
627 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, | |
f3332114 MS |
628 | struct inode *inode, loff_t pos, size_t count, |
629 | fl_owner_t owner) | |
b25e82e5 MS |
630 | { |
631 | struct fuse_conn *fc = get_fuse_conn(inode); | |
3be5a52b | 632 | fuse_write_fill(req, file, file->private_data, inode, pos, count, 0); |
f3332114 MS |
633 | if (owner != NULL) { |
634 | struct fuse_write_in *inarg = &req->misc.write.in; | |
635 | inarg->write_flags |= FUSE_WRITE_LOCKOWNER; | |
636 | inarg->lock_owner = fuse_lock_owner_id(fc, owner); | |
637 | } | |
7c352bdf | 638 | request_send(fc, req); |
b25e82e5 | 639 | return req->misc.write.out.size; |
b6aeaded MS |
640 | } |
641 | ||
5e6f58a1 NP |
642 | static int fuse_write_begin(struct file *file, struct address_space *mapping, |
643 | loff_t pos, unsigned len, unsigned flags, | |
644 | struct page **pagep, void **fsdata) | |
b6aeaded | 645 | { |
5e6f58a1 NP |
646 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
647 | ||
648 | *pagep = __grab_cache_page(mapping, index); | |
649 | if (!*pagep) | |
650 | return -ENOMEM; | |
b6aeaded MS |
651 | return 0; |
652 | } | |
653 | ||
854512ec MS |
654 | static void fuse_write_update_size(struct inode *inode, loff_t pos) |
655 | { | |
656 | struct fuse_conn *fc = get_fuse_conn(inode); | |
657 | struct fuse_inode *fi = get_fuse_inode(inode); | |
658 | ||
659 | spin_lock(&fc->lock); | |
660 | fi->attr_version = ++fc->attr_version; | |
661 | if (pos > inode->i_size) | |
662 | i_size_write(inode, pos); | |
663 | spin_unlock(&fc->lock); | |
664 | } | |
665 | ||
5e6f58a1 NP |
666 | static int fuse_buffered_write(struct file *file, struct inode *inode, |
667 | loff_t pos, unsigned count, struct page *page) | |
b6aeaded MS |
668 | { |
669 | int err; | |
04730fef | 670 | size_t nres; |
b6aeaded | 671 | struct fuse_conn *fc = get_fuse_conn(inode); |
5e6f58a1 | 672 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); |
248d86e8 MS |
673 | struct fuse_req *req; |
674 | ||
675 | if (is_bad_inode(inode)) | |
676 | return -EIO; | |
677 | ||
3be5a52b MS |
678 | /* |
679 | * Make sure writepages on the same page are not mixed up with | |
680 | * plain writes. | |
681 | */ | |
682 | fuse_wait_on_page_writeback(inode, page->index); | |
683 | ||
ce1d5a49 MS |
684 | req = fuse_get_req(fc); |
685 | if (IS_ERR(req)) | |
686 | return PTR_ERR(req); | |
b6aeaded MS |
687 | |
688 | req->num_pages = 1; | |
689 | req->pages[0] = page; | |
690 | req->page_offset = offset; | |
f3332114 | 691 | nres = fuse_send_write(req, file, inode, pos, count, NULL); |
b6aeaded MS |
692 | err = req->out.h.error; |
693 | fuse_put_request(fc, req); | |
5e6f58a1 | 694 | if (!err && !nres) |
b6aeaded MS |
695 | err = -EIO; |
696 | if (!err) { | |
5e6f58a1 | 697 | pos += nres; |
854512ec | 698 | fuse_write_update_size(inode, pos); |
5e6f58a1 | 699 | if (count == PAGE_CACHE_SIZE) |
b6aeaded | 700 | SetPageUptodate(page); |
b36c31ba MS |
701 | } |
702 | fuse_invalidate_attr(inode); | |
5e6f58a1 NP |
703 | return err ? err : nres; |
704 | } | |
705 | ||
706 | static int fuse_write_end(struct file *file, struct address_space *mapping, | |
707 | loff_t pos, unsigned len, unsigned copied, | |
708 | struct page *page, void *fsdata) | |
709 | { | |
710 | struct inode *inode = mapping->host; | |
711 | int res = 0; | |
712 | ||
713 | if (copied) | |
714 | res = fuse_buffered_write(file, inode, pos, copied, page); | |
715 | ||
716 | unlock_page(page); | |
717 | page_cache_release(page); | |
718 | return res; | |
b6aeaded MS |
719 | } |
720 | ||
ea9b9907 NP |
721 | static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file, |
722 | struct inode *inode, loff_t pos, | |
723 | size_t count) | |
724 | { | |
725 | size_t res; | |
726 | unsigned offset; | |
727 | unsigned i; | |
728 | ||
729 | for (i = 0; i < req->num_pages; i++) | |
730 | fuse_wait_on_page_writeback(inode, req->pages[i]->index); | |
731 | ||
732 | res = fuse_send_write(req, file, inode, pos, count, NULL); | |
733 | ||
734 | offset = req->page_offset; | |
735 | count = res; | |
736 | for (i = 0; i < req->num_pages; i++) { | |
737 | struct page *page = req->pages[i]; | |
738 | ||
739 | if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE) | |
740 | SetPageUptodate(page); | |
741 | ||
742 | if (count > PAGE_CACHE_SIZE - offset) | |
743 | count -= PAGE_CACHE_SIZE - offset; | |
744 | else | |
745 | count = 0; | |
746 | offset = 0; | |
747 | ||
748 | unlock_page(page); | |
749 | page_cache_release(page); | |
750 | } | |
751 | ||
752 | return res; | |
753 | } | |
754 | ||
755 | static ssize_t fuse_fill_write_pages(struct fuse_req *req, | |
756 | struct address_space *mapping, | |
757 | struct iov_iter *ii, loff_t pos) | |
758 | { | |
759 | struct fuse_conn *fc = get_fuse_conn(mapping->host); | |
760 | unsigned offset = pos & (PAGE_CACHE_SIZE - 1); | |
761 | size_t count = 0; | |
762 | int err; | |
763 | ||
764 | req->page_offset = offset; | |
765 | ||
766 | do { | |
767 | size_t tmp; | |
768 | struct page *page; | |
769 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
770 | size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset, | |
771 | iov_iter_count(ii)); | |
772 | ||
773 | bytes = min_t(size_t, bytes, fc->max_write - count); | |
774 | ||
775 | again: | |
776 | err = -EFAULT; | |
777 | if (iov_iter_fault_in_readable(ii, bytes)) | |
778 | break; | |
779 | ||
780 | err = -ENOMEM; | |
781 | page = __grab_cache_page(mapping, index); | |
782 | if (!page) | |
783 | break; | |
784 | ||
785 | pagefault_disable(); | |
786 | tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes); | |
787 | pagefault_enable(); | |
788 | flush_dcache_page(page); | |
789 | ||
790 | if (!tmp) { | |
791 | unlock_page(page); | |
792 | page_cache_release(page); | |
793 | bytes = min(bytes, iov_iter_single_seg_count(ii)); | |
794 | goto again; | |
795 | } | |
796 | ||
797 | err = 0; | |
798 | req->pages[req->num_pages] = page; | |
799 | req->num_pages++; | |
800 | ||
801 | iov_iter_advance(ii, tmp); | |
802 | count += tmp; | |
803 | pos += tmp; | |
804 | offset += tmp; | |
805 | if (offset == PAGE_CACHE_SIZE) | |
806 | offset = 0; | |
807 | ||
78bb6cb9 MS |
808 | if (!fc->big_writes) |
809 | break; | |
ea9b9907 NP |
810 | } while (iov_iter_count(ii) && count < fc->max_write && |
811 | req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0); | |
812 | ||
813 | return count > 0 ? count : err; | |
814 | } | |
815 | ||
816 | static ssize_t fuse_perform_write(struct file *file, | |
817 | struct address_space *mapping, | |
818 | struct iov_iter *ii, loff_t pos) | |
819 | { | |
820 | struct inode *inode = mapping->host; | |
821 | struct fuse_conn *fc = get_fuse_conn(inode); | |
822 | int err = 0; | |
823 | ssize_t res = 0; | |
824 | ||
825 | if (is_bad_inode(inode)) | |
826 | return -EIO; | |
827 | ||
828 | do { | |
829 | struct fuse_req *req; | |
830 | ssize_t count; | |
831 | ||
832 | req = fuse_get_req(fc); | |
833 | if (IS_ERR(req)) { | |
834 | err = PTR_ERR(req); | |
835 | break; | |
836 | } | |
837 | ||
838 | count = fuse_fill_write_pages(req, mapping, ii, pos); | |
839 | if (count <= 0) { | |
840 | err = count; | |
841 | } else { | |
842 | size_t num_written; | |
843 | ||
844 | num_written = fuse_send_write_pages(req, file, inode, | |
845 | pos, count); | |
846 | err = req->out.h.error; | |
847 | if (!err) { | |
848 | res += num_written; | |
849 | pos += num_written; | |
850 | ||
851 | /* break out of the loop on short write */ | |
852 | if (num_written != count) | |
853 | err = -EIO; | |
854 | } | |
855 | } | |
856 | fuse_put_request(fc, req); | |
857 | } while (!err && iov_iter_count(ii)); | |
858 | ||
859 | if (res > 0) | |
860 | fuse_write_update_size(inode, pos); | |
861 | ||
862 | fuse_invalidate_attr(inode); | |
863 | ||
864 | return res > 0 ? res : err; | |
865 | } | |
866 | ||
867 | static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, | |
868 | unsigned long nr_segs, loff_t pos) | |
869 | { | |
870 | struct file *file = iocb->ki_filp; | |
871 | struct address_space *mapping = file->f_mapping; | |
872 | size_t count = 0; | |
873 | ssize_t written = 0; | |
874 | struct inode *inode = mapping->host; | |
875 | ssize_t err; | |
876 | struct iov_iter i; | |
877 | ||
878 | WARN_ON(iocb->ki_pos != pos); | |
879 | ||
880 | err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ); | |
881 | if (err) | |
882 | return err; | |
883 | ||
884 | mutex_lock(&inode->i_mutex); | |
885 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); | |
886 | ||
887 | /* We can write back this queue in page reclaim */ | |
888 | current->backing_dev_info = mapping->backing_dev_info; | |
889 | ||
890 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
891 | if (err) | |
892 | goto out; | |
893 | ||
894 | if (count == 0) | |
895 | goto out; | |
896 | ||
2f1936b8 | 897 | err = file_remove_suid(file); |
ea9b9907 NP |
898 | if (err) |
899 | goto out; | |
900 | ||
901 | file_update_time(file); | |
902 | ||
903 | iov_iter_init(&i, iov, nr_segs, count, 0); | |
904 | written = fuse_perform_write(file, mapping, &i, pos); | |
905 | if (written >= 0) | |
906 | iocb->ki_pos = pos + written; | |
907 | ||
908 | out: | |
909 | current->backing_dev_info = NULL; | |
910 | mutex_unlock(&inode->i_mutex); | |
911 | ||
912 | return written ? written : err; | |
913 | } | |
914 | ||
413ef8cb MS |
915 | static void fuse_release_user_pages(struct fuse_req *req, int write) |
916 | { | |
917 | unsigned i; | |
918 | ||
919 | for (i = 0; i < req->num_pages; i++) { | |
920 | struct page *page = req->pages[i]; | |
921 | if (write) | |
922 | set_page_dirty_lock(page); | |
923 | put_page(page); | |
924 | } | |
925 | } | |
926 | ||
927 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | |
928 | unsigned nbytes, int write) | |
929 | { | |
930 | unsigned long user_addr = (unsigned long) buf; | |
931 | unsigned offset = user_addr & ~PAGE_MASK; | |
932 | int npages; | |
933 | ||
934 | /* This doesn't work with nfsd */ | |
935 | if (!current->mm) | |
936 | return -EPERM; | |
937 | ||
938 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | |
939 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
bd730967 | 940 | npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ); |
413ef8cb MS |
941 | down_read(¤t->mm->mmap_sem); |
942 | npages = get_user_pages(current, current->mm, user_addr, npages, write, | |
943 | 0, req->pages, NULL); | |
944 | up_read(¤t->mm->mmap_sem); | |
945 | if (npages < 0) | |
946 | return npages; | |
947 | ||
948 | req->num_pages = npages; | |
949 | req->page_offset = offset; | |
950 | return 0; | |
951 | } | |
952 | ||
953 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | |
954 | size_t count, loff_t *ppos, int write) | |
955 | { | |
7706a9d6 | 956 | struct inode *inode = file->f_path.dentry->d_inode; |
413ef8cb MS |
957 | struct fuse_conn *fc = get_fuse_conn(inode); |
958 | size_t nmax = write ? fc->max_write : fc->max_read; | |
959 | loff_t pos = *ppos; | |
960 | ssize_t res = 0; | |
248d86e8 MS |
961 | struct fuse_req *req; |
962 | ||
963 | if (is_bad_inode(inode)) | |
964 | return -EIO; | |
965 | ||
ce1d5a49 MS |
966 | req = fuse_get_req(fc); |
967 | if (IS_ERR(req)) | |
968 | return PTR_ERR(req); | |
413ef8cb MS |
969 | |
970 | while (count) { | |
413ef8cb | 971 | size_t nres; |
e5d9a0df MS |
972 | size_t nbytes_limit = min(count, nmax); |
973 | size_t nbytes; | |
974 | int err = fuse_get_user_pages(req, buf, nbytes_limit, !write); | |
413ef8cb MS |
975 | if (err) { |
976 | res = err; | |
977 | break; | |
978 | } | |
6ad84aca | 979 | nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset; |
e5d9a0df | 980 | nbytes = min(nbytes_limit, nbytes); |
413ef8cb | 981 | if (write) |
f3332114 MS |
982 | nres = fuse_send_write(req, file, inode, pos, nbytes, |
983 | current->files); | |
413ef8cb | 984 | else |
f3332114 MS |
985 | nres = fuse_send_read(req, file, inode, pos, nbytes, |
986 | current->files); | |
413ef8cb MS |
987 | fuse_release_user_pages(req, !write); |
988 | if (req->out.h.error) { | |
989 | if (!res) | |
990 | res = req->out.h.error; | |
991 | break; | |
992 | } else if (nres > nbytes) { | |
993 | res = -EIO; | |
994 | break; | |
995 | } | |
996 | count -= nres; | |
997 | res += nres; | |
998 | pos += nres; | |
999 | buf += nres; | |
1000 | if (nres != nbytes) | |
1001 | break; | |
56cf34ff MS |
1002 | if (count) { |
1003 | fuse_put_request(fc, req); | |
1004 | req = fuse_get_req(fc); | |
1005 | if (IS_ERR(req)) | |
1006 | break; | |
1007 | } | |
413ef8cb MS |
1008 | } |
1009 | fuse_put_request(fc, req); | |
1010 | if (res > 0) { | |
854512ec MS |
1011 | if (write) |
1012 | fuse_write_update_size(inode, pos); | |
413ef8cb | 1013 | *ppos = pos; |
b36c31ba MS |
1014 | } |
1015 | fuse_invalidate_attr(inode); | |
413ef8cb MS |
1016 | |
1017 | return res; | |
1018 | } | |
1019 | ||
1020 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, | |
1021 | size_t count, loff_t *ppos) | |
1022 | { | |
1023 | return fuse_direct_io(file, buf, count, ppos, 0); | |
1024 | } | |
1025 | ||
1026 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | |
1027 | size_t count, loff_t *ppos) | |
1028 | { | |
7706a9d6 | 1029 | struct inode *inode = file->f_path.dentry->d_inode; |
413ef8cb MS |
1030 | ssize_t res; |
1031 | /* Don't allow parallel writes to the same file */ | |
1b1dcc1b | 1032 | mutex_lock(&inode->i_mutex); |
889f7848 MS |
1033 | res = generic_write_checks(file, ppos, &count, 0); |
1034 | if (!res) | |
1035 | res = fuse_direct_io(file, buf, count, ppos, 1); | |
1b1dcc1b | 1036 | mutex_unlock(&inode->i_mutex); |
413ef8cb MS |
1037 | return res; |
1038 | } | |
1039 | ||
3be5a52b | 1040 | static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req) |
b6aeaded | 1041 | { |
3be5a52b MS |
1042 | __free_page(req->pages[0]); |
1043 | fuse_file_put(req->ff); | |
3be5a52b MS |
1044 | } |
1045 | ||
1046 | static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req) | |
1047 | { | |
1048 | struct inode *inode = req->inode; | |
1049 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1050 | struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info; | |
1051 | ||
1052 | list_del(&req->writepages_entry); | |
1053 | dec_bdi_stat(bdi, BDI_WRITEBACK); | |
1054 | dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP); | |
1055 | bdi_writeout_inc(bdi); | |
1056 | wake_up(&fi->page_waitq); | |
1057 | } | |
1058 | ||
1059 | /* Called under fc->lock, may release and reacquire it */ | |
1060 | static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req) | |
1061 | { | |
1062 | struct fuse_inode *fi = get_fuse_inode(req->inode); | |
1063 | loff_t size = i_size_read(req->inode); | |
1064 | struct fuse_write_in *inarg = &req->misc.write.in; | |
1065 | ||
1066 | if (!fc->connected) | |
1067 | goto out_free; | |
1068 | ||
1069 | if (inarg->offset + PAGE_CACHE_SIZE <= size) { | |
1070 | inarg->size = PAGE_CACHE_SIZE; | |
1071 | } else if (inarg->offset < size) { | |
1072 | inarg->size = size & (PAGE_CACHE_SIZE - 1); | |
1073 | } else { | |
1074 | /* Got truncated off completely */ | |
1075 | goto out_free; | |
b6aeaded | 1076 | } |
3be5a52b MS |
1077 | |
1078 | req->in.args[1].size = inarg->size; | |
1079 | fi->writectr++; | |
1080 | request_send_background_locked(fc, req); | |
1081 | return; | |
1082 | ||
1083 | out_free: | |
1084 | fuse_writepage_finish(fc, req); | |
1085 | spin_unlock(&fc->lock); | |
1086 | fuse_writepage_free(fc, req); | |
e9bb09dd | 1087 | fuse_put_request(fc, req); |
3be5a52b | 1088 | spin_lock(&fc->lock); |
b6aeaded MS |
1089 | } |
1090 | ||
3be5a52b MS |
1091 | /* |
1092 | * If fi->writectr is positive (no truncate or fsync going on) send | |
1093 | * all queued writepage requests. | |
1094 | * | |
1095 | * Called with fc->lock | |
1096 | */ | |
1097 | void fuse_flush_writepages(struct inode *inode) | |
b6aeaded | 1098 | { |
3be5a52b MS |
1099 | struct fuse_conn *fc = get_fuse_conn(inode); |
1100 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1101 | struct fuse_req *req; | |
1102 | ||
1103 | while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) { | |
1104 | req = list_entry(fi->queued_writes.next, struct fuse_req, list); | |
1105 | list_del_init(&req->list); | |
1106 | fuse_send_writepage(fc, req); | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req) | |
1111 | { | |
1112 | struct inode *inode = req->inode; | |
1113 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1114 | ||
1115 | mapping_set_error(inode->i_mapping, req->out.h.error); | |
1116 | spin_lock(&fc->lock); | |
1117 | fi->writectr--; | |
1118 | fuse_writepage_finish(fc, req); | |
1119 | spin_unlock(&fc->lock); | |
1120 | fuse_writepage_free(fc, req); | |
1121 | } | |
1122 | ||
1123 | static int fuse_writepage_locked(struct page *page) | |
1124 | { | |
1125 | struct address_space *mapping = page->mapping; | |
1126 | struct inode *inode = mapping->host; | |
1127 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1128 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1129 | struct fuse_req *req; | |
1130 | struct fuse_file *ff; | |
1131 | struct page *tmp_page; | |
1132 | ||
1133 | set_page_writeback(page); | |
1134 | ||
1135 | req = fuse_request_alloc_nofs(); | |
1136 | if (!req) | |
1137 | goto err; | |
1138 | ||
1139 | tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); | |
1140 | if (!tmp_page) | |
1141 | goto err_free; | |
1142 | ||
1143 | spin_lock(&fc->lock); | |
1144 | BUG_ON(list_empty(&fi->write_files)); | |
1145 | ff = list_entry(fi->write_files.next, struct fuse_file, write_entry); | |
1146 | req->ff = fuse_file_get(ff); | |
1147 | spin_unlock(&fc->lock); | |
1148 | ||
1149 | fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1); | |
1150 | ||
1151 | copy_highpage(tmp_page, page); | |
1152 | req->num_pages = 1; | |
1153 | req->pages[0] = tmp_page; | |
1154 | req->page_offset = 0; | |
1155 | req->end = fuse_writepage_end; | |
1156 | req->inode = inode; | |
1157 | ||
1158 | inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK); | |
1159 | inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP); | |
1160 | end_page_writeback(page); | |
1161 | ||
1162 | spin_lock(&fc->lock); | |
1163 | list_add(&req->writepages_entry, &fi->writepages); | |
1164 | list_add_tail(&req->list, &fi->queued_writes); | |
1165 | fuse_flush_writepages(inode); | |
1166 | spin_unlock(&fc->lock); | |
1167 | ||
1168 | return 0; | |
1169 | ||
1170 | err_free: | |
1171 | fuse_request_free(req); | |
1172 | err: | |
1173 | end_page_writeback(page); | |
1174 | return -ENOMEM; | |
1175 | } | |
1176 | ||
1177 | static int fuse_writepage(struct page *page, struct writeback_control *wbc) | |
1178 | { | |
1179 | int err; | |
1180 | ||
1181 | err = fuse_writepage_locked(page); | |
1182 | unlock_page(page); | |
1183 | ||
1184 | return err; | |
1185 | } | |
1186 | ||
1187 | static int fuse_launder_page(struct page *page) | |
1188 | { | |
1189 | int err = 0; | |
1190 | if (clear_page_dirty_for_io(page)) { | |
1191 | struct inode *inode = page->mapping->host; | |
1192 | err = fuse_writepage_locked(page); | |
1193 | if (!err) | |
1194 | fuse_wait_on_page_writeback(inode, page->index); | |
1195 | } | |
1196 | return err; | |
1197 | } | |
1198 | ||
1199 | /* | |
1200 | * Write back dirty pages now, because there may not be any suitable | |
1201 | * open files later | |
1202 | */ | |
1203 | static void fuse_vma_close(struct vm_area_struct *vma) | |
1204 | { | |
1205 | filemap_write_and_wait(vma->vm_file->f_mapping); | |
1206 | } | |
1207 | ||
1208 | /* | |
1209 | * Wait for writeback against this page to complete before allowing it | |
1210 | * to be marked dirty again, and hence written back again, possibly | |
1211 | * before the previous writepage completed. | |
1212 | * | |
1213 | * Block here, instead of in ->writepage(), so that the userspace fs | |
1214 | * can only block processes actually operating on the filesystem. | |
1215 | * | |
1216 | * Otherwise unprivileged userspace fs would be able to block | |
1217 | * unrelated: | |
1218 | * | |
1219 | * - page migration | |
1220 | * - sync(2) | |
1221 | * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER | |
1222 | */ | |
1223 | static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page) | |
1224 | { | |
1225 | /* | |
1226 | * Don't use page->mapping as it may become NULL from a | |
1227 | * concurrent truncate. | |
1228 | */ | |
1229 | struct inode *inode = vma->vm_file->f_mapping->host; | |
1230 | ||
1231 | fuse_wait_on_page_writeback(inode, page->index); | |
1232 | return 0; | |
1233 | } | |
1234 | ||
1235 | static struct vm_operations_struct fuse_file_vm_ops = { | |
1236 | .close = fuse_vma_close, | |
1237 | .fault = filemap_fault, | |
1238 | .page_mkwrite = fuse_page_mkwrite, | |
1239 | }; | |
1240 | ||
1241 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | |
1242 | { | |
1243 | if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) { | |
1244 | struct inode *inode = file->f_dentry->d_inode; | |
1245 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1246 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1247 | struct fuse_file *ff = file->private_data; | |
1248 | /* | |
1249 | * file may be written through mmap, so chain it onto the | |
1250 | * inodes's write_file list | |
1251 | */ | |
1252 | spin_lock(&fc->lock); | |
1253 | if (list_empty(&ff->write_entry)) | |
1254 | list_add(&ff->write_entry, &fi->write_files); | |
1255 | spin_unlock(&fc->lock); | |
1256 | } | |
1257 | file_accessed(file); | |
1258 | vma->vm_ops = &fuse_file_vm_ops; | |
b6aeaded MS |
1259 | return 0; |
1260 | } | |
1261 | ||
71421259 MS |
1262 | static int convert_fuse_file_lock(const struct fuse_file_lock *ffl, |
1263 | struct file_lock *fl) | |
1264 | { | |
1265 | switch (ffl->type) { | |
1266 | case F_UNLCK: | |
1267 | break; | |
1268 | ||
1269 | case F_RDLCK: | |
1270 | case F_WRLCK: | |
1271 | if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX || | |
1272 | ffl->end < ffl->start) | |
1273 | return -EIO; | |
1274 | ||
1275 | fl->fl_start = ffl->start; | |
1276 | fl->fl_end = ffl->end; | |
1277 | fl->fl_pid = ffl->pid; | |
1278 | break; | |
1279 | ||
1280 | default: | |
1281 | return -EIO; | |
1282 | } | |
1283 | fl->fl_type = ffl->type; | |
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | static void fuse_lk_fill(struct fuse_req *req, struct file *file, | |
a9ff4f87 MS |
1288 | const struct file_lock *fl, int opcode, pid_t pid, |
1289 | int flock) | |
71421259 | 1290 | { |
7706a9d6 | 1291 | struct inode *inode = file->f_path.dentry->d_inode; |
9c8ef561 | 1292 | struct fuse_conn *fc = get_fuse_conn(inode); |
71421259 MS |
1293 | struct fuse_file *ff = file->private_data; |
1294 | struct fuse_lk_in *arg = &req->misc.lk_in; | |
1295 | ||
1296 | arg->fh = ff->fh; | |
9c8ef561 | 1297 | arg->owner = fuse_lock_owner_id(fc, fl->fl_owner); |
71421259 MS |
1298 | arg->lk.start = fl->fl_start; |
1299 | arg->lk.end = fl->fl_end; | |
1300 | arg->lk.type = fl->fl_type; | |
1301 | arg->lk.pid = pid; | |
a9ff4f87 MS |
1302 | if (flock) |
1303 | arg->lk_flags |= FUSE_LK_FLOCK; | |
71421259 MS |
1304 | req->in.h.opcode = opcode; |
1305 | req->in.h.nodeid = get_node_id(inode); | |
1306 | req->in.numargs = 1; | |
1307 | req->in.args[0].size = sizeof(*arg); | |
1308 | req->in.args[0].value = arg; | |
1309 | } | |
1310 | ||
1311 | static int fuse_getlk(struct file *file, struct file_lock *fl) | |
1312 | { | |
7706a9d6 | 1313 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1314 | struct fuse_conn *fc = get_fuse_conn(inode); |
1315 | struct fuse_req *req; | |
1316 | struct fuse_lk_out outarg; | |
1317 | int err; | |
1318 | ||
1319 | req = fuse_get_req(fc); | |
1320 | if (IS_ERR(req)) | |
1321 | return PTR_ERR(req); | |
1322 | ||
a9ff4f87 | 1323 | fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0); |
71421259 MS |
1324 | req->out.numargs = 1; |
1325 | req->out.args[0].size = sizeof(outarg); | |
1326 | req->out.args[0].value = &outarg; | |
1327 | request_send(fc, req); | |
1328 | err = req->out.h.error; | |
1329 | fuse_put_request(fc, req); | |
1330 | if (!err) | |
1331 | err = convert_fuse_file_lock(&outarg.lk, fl); | |
1332 | ||
1333 | return err; | |
1334 | } | |
1335 | ||
a9ff4f87 | 1336 | static int fuse_setlk(struct file *file, struct file_lock *fl, int flock) |
71421259 | 1337 | { |
7706a9d6 | 1338 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1339 | struct fuse_conn *fc = get_fuse_conn(inode); |
1340 | struct fuse_req *req; | |
1341 | int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK; | |
1342 | pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0; | |
1343 | int err; | |
1344 | ||
48e90761 MS |
1345 | if (fl->fl_lmops && fl->fl_lmops->fl_grant) { |
1346 | /* NLM needs asynchronous locks, which we don't support yet */ | |
1347 | return -ENOLCK; | |
1348 | } | |
1349 | ||
71421259 MS |
1350 | /* Unlock on close is handled by the flush method */ |
1351 | if (fl->fl_flags & FL_CLOSE) | |
1352 | return 0; | |
1353 | ||
1354 | req = fuse_get_req(fc); | |
1355 | if (IS_ERR(req)) | |
1356 | return PTR_ERR(req); | |
1357 | ||
a9ff4f87 | 1358 | fuse_lk_fill(req, file, fl, opcode, pid, flock); |
71421259 MS |
1359 | request_send(fc, req); |
1360 | err = req->out.h.error; | |
a4d27e75 MS |
1361 | /* locking is restartable */ |
1362 | if (err == -EINTR) | |
1363 | err = -ERESTARTSYS; | |
71421259 MS |
1364 | fuse_put_request(fc, req); |
1365 | return err; | |
1366 | } | |
1367 | ||
1368 | static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl) | |
1369 | { | |
7706a9d6 | 1370 | struct inode *inode = file->f_path.dentry->d_inode; |
71421259 MS |
1371 | struct fuse_conn *fc = get_fuse_conn(inode); |
1372 | int err; | |
1373 | ||
48e90761 MS |
1374 | if (cmd == F_CANCELLK) { |
1375 | err = 0; | |
1376 | } else if (cmd == F_GETLK) { | |
71421259 | 1377 | if (fc->no_lock) { |
9d6a8c5c | 1378 | posix_test_lock(file, fl); |
71421259 MS |
1379 | err = 0; |
1380 | } else | |
1381 | err = fuse_getlk(file, fl); | |
1382 | } else { | |
1383 | if (fc->no_lock) | |
48e90761 | 1384 | err = posix_lock_file(file, fl, NULL); |
71421259 | 1385 | else |
a9ff4f87 | 1386 | err = fuse_setlk(file, fl, 0); |
71421259 MS |
1387 | } |
1388 | return err; | |
1389 | } | |
1390 | ||
a9ff4f87 MS |
1391 | static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl) |
1392 | { | |
1393 | struct inode *inode = file->f_path.dentry->d_inode; | |
1394 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1395 | int err; | |
1396 | ||
1397 | if (fc->no_lock) { | |
1398 | err = flock_lock_file_wait(file, fl); | |
1399 | } else { | |
1400 | /* emulate flock with POSIX locks */ | |
1401 | fl->fl_owner = (fl_owner_t) file; | |
1402 | err = fuse_setlk(file, fl, 1); | |
1403 | } | |
1404 | ||
1405 | return err; | |
1406 | } | |
1407 | ||
b2d2272f MS |
1408 | static sector_t fuse_bmap(struct address_space *mapping, sector_t block) |
1409 | { | |
1410 | struct inode *inode = mapping->host; | |
1411 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1412 | struct fuse_req *req; | |
1413 | struct fuse_bmap_in inarg; | |
1414 | struct fuse_bmap_out outarg; | |
1415 | int err; | |
1416 | ||
1417 | if (!inode->i_sb->s_bdev || fc->no_bmap) | |
1418 | return 0; | |
1419 | ||
1420 | req = fuse_get_req(fc); | |
1421 | if (IS_ERR(req)) | |
1422 | return 0; | |
1423 | ||
1424 | memset(&inarg, 0, sizeof(inarg)); | |
1425 | inarg.block = block; | |
1426 | inarg.blocksize = inode->i_sb->s_blocksize; | |
1427 | req->in.h.opcode = FUSE_BMAP; | |
1428 | req->in.h.nodeid = get_node_id(inode); | |
1429 | req->in.numargs = 1; | |
1430 | req->in.args[0].size = sizeof(inarg); | |
1431 | req->in.args[0].value = &inarg; | |
1432 | req->out.numargs = 1; | |
1433 | req->out.args[0].size = sizeof(outarg); | |
1434 | req->out.args[0].value = &outarg; | |
1435 | request_send(fc, req); | |
1436 | err = req->out.h.error; | |
1437 | fuse_put_request(fc, req); | |
1438 | if (err == -ENOSYS) | |
1439 | fc->no_bmap = 1; | |
1440 | ||
1441 | return err ? 0 : outarg.block; | |
1442 | } | |
1443 | ||
5559b8f4 MS |
1444 | static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin) |
1445 | { | |
1446 | loff_t retval; | |
1447 | struct inode *inode = file->f_path.dentry->d_inode; | |
1448 | ||
1449 | mutex_lock(&inode->i_mutex); | |
1450 | switch (origin) { | |
1451 | case SEEK_END: | |
769415c6 MS |
1452 | retval = fuse_update_attributes(inode, NULL, file, NULL); |
1453 | if (retval) | |
1454 | return retval; | |
5559b8f4 MS |
1455 | offset += i_size_read(inode); |
1456 | break; | |
1457 | case SEEK_CUR: | |
1458 | offset += file->f_pos; | |
1459 | } | |
1460 | retval = -EINVAL; | |
1461 | if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) { | |
1462 | if (offset != file->f_pos) { | |
1463 | file->f_pos = offset; | |
1464 | file->f_version = 0; | |
1465 | } | |
1466 | retval = offset; | |
1467 | } | |
1468 | mutex_unlock(&inode->i_mutex); | |
1469 | return retval; | |
1470 | } | |
1471 | ||
59efec7b TH |
1472 | static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov, |
1473 | unsigned int nr_segs, size_t bytes, bool to_user) | |
1474 | { | |
1475 | struct iov_iter ii; | |
1476 | int page_idx = 0; | |
1477 | ||
1478 | if (!bytes) | |
1479 | return 0; | |
1480 | ||
1481 | iov_iter_init(&ii, iov, nr_segs, bytes, 0); | |
1482 | ||
1483 | while (iov_iter_count(&ii)) { | |
1484 | struct page *page = pages[page_idx++]; | |
1485 | size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii)); | |
1486 | void *kaddr, *map; | |
1487 | ||
1488 | kaddr = map = kmap(page); | |
1489 | ||
1490 | while (todo) { | |
1491 | char __user *uaddr = ii.iov->iov_base + ii.iov_offset; | |
1492 | size_t iov_len = ii.iov->iov_len - ii.iov_offset; | |
1493 | size_t copy = min(todo, iov_len); | |
1494 | size_t left; | |
1495 | ||
1496 | if (!to_user) | |
1497 | left = copy_from_user(kaddr, uaddr, copy); | |
1498 | else | |
1499 | left = copy_to_user(uaddr, kaddr, copy); | |
1500 | ||
1501 | if (unlikely(left)) | |
1502 | return -EFAULT; | |
1503 | ||
1504 | iov_iter_advance(&ii, copy); | |
1505 | todo -= copy; | |
1506 | kaddr += copy; | |
1507 | } | |
1508 | ||
1509 | kunmap(map); | |
1510 | } | |
1511 | ||
1512 | return 0; | |
1513 | } | |
1514 | ||
1515 | /* | |
1516 | * For ioctls, there is no generic way to determine how much memory | |
1517 | * needs to be read and/or written. Furthermore, ioctls are allowed | |
1518 | * to dereference the passed pointer, so the parameter requires deep | |
1519 | * copying but FUSE has no idea whatsoever about what to copy in or | |
1520 | * out. | |
1521 | * | |
1522 | * This is solved by allowing FUSE server to retry ioctl with | |
1523 | * necessary in/out iovecs. Let's assume the ioctl implementation | |
1524 | * needs to read in the following structure. | |
1525 | * | |
1526 | * struct a { | |
1527 | * char *buf; | |
1528 | * size_t buflen; | |
1529 | * } | |
1530 | * | |
1531 | * On the first callout to FUSE server, inarg->in_size and | |
1532 | * inarg->out_size will be NULL; then, the server completes the ioctl | |
1533 | * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and | |
1534 | * the actual iov array to | |
1535 | * | |
1536 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } } | |
1537 | * | |
1538 | * which tells FUSE to copy in the requested area and retry the ioctl. | |
1539 | * On the second round, the server has access to the structure and | |
1540 | * from that it can tell what to look for next, so on the invocation, | |
1541 | * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to | |
1542 | * | |
1543 | * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) }, | |
1544 | * { .iov_base = a.buf, .iov_len = a.buflen } } | |
1545 | * | |
1546 | * FUSE will copy both struct a and the pointed buffer from the | |
1547 | * process doing the ioctl and retry ioctl with both struct a and the | |
1548 | * buffer. | |
1549 | * | |
1550 | * This time, FUSE server has everything it needs and completes ioctl | |
1551 | * without FUSE_IOCTL_RETRY which finishes the ioctl call. | |
1552 | * | |
1553 | * Copying data out works the same way. | |
1554 | * | |
1555 | * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel | |
1556 | * automatically initializes in and out iovs by decoding @cmd with | |
1557 | * _IOC_* macros and the server is not allowed to request RETRY. This | |
1558 | * limits ioctl data transfers to well-formed ioctls and is the forced | |
1559 | * behavior for all FUSE servers. | |
1560 | */ | |
1561 | static long fuse_file_do_ioctl(struct file *file, unsigned int cmd, | |
1562 | unsigned long arg, unsigned int flags) | |
1563 | { | |
1564 | struct inode *inode = file->f_dentry->d_inode; | |
1565 | struct fuse_file *ff = file->private_data; | |
1566 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1567 | struct fuse_ioctl_in inarg = { | |
1568 | .fh = ff->fh, | |
1569 | .cmd = cmd, | |
1570 | .arg = arg, | |
1571 | .flags = flags | |
1572 | }; | |
1573 | struct fuse_ioctl_out outarg; | |
1574 | struct fuse_req *req = NULL; | |
1575 | struct page **pages = NULL; | |
1576 | struct page *iov_page = NULL; | |
1577 | struct iovec *in_iov = NULL, *out_iov = NULL; | |
1578 | unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages; | |
1579 | size_t in_size, out_size, transferred; | |
1580 | int err; | |
1581 | ||
1582 | /* assume all the iovs returned by client always fits in a page */ | |
1583 | BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE); | |
1584 | ||
1585 | if (!fuse_allow_task(fc, current)) | |
1586 | return -EACCES; | |
1587 | ||
1588 | err = -EIO; | |
1589 | if (is_bad_inode(inode)) | |
1590 | goto out; | |
1591 | ||
1592 | err = -ENOMEM; | |
1593 | pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL); | |
1594 | iov_page = alloc_page(GFP_KERNEL); | |
1595 | if (!pages || !iov_page) | |
1596 | goto out; | |
1597 | ||
1598 | /* | |
1599 | * If restricted, initialize IO parameters as encoded in @cmd. | |
1600 | * RETRY from server is not allowed. | |
1601 | */ | |
1602 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) { | |
1603 | struct iovec *iov = page_address(iov_page); | |
1604 | ||
1605 | iov->iov_base = (void *)arg; | |
1606 | iov->iov_len = _IOC_SIZE(cmd); | |
1607 | ||
1608 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
1609 | in_iov = iov; | |
1610 | in_iovs = 1; | |
1611 | } | |
1612 | ||
1613 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
1614 | out_iov = iov; | |
1615 | out_iovs = 1; | |
1616 | } | |
1617 | } | |
1618 | ||
1619 | retry: | |
1620 | inarg.in_size = in_size = iov_length(in_iov, in_iovs); | |
1621 | inarg.out_size = out_size = iov_length(out_iov, out_iovs); | |
1622 | ||
1623 | /* | |
1624 | * Out data can be used either for actual out data or iovs, | |
1625 | * make sure there always is at least one page. | |
1626 | */ | |
1627 | out_size = max_t(size_t, out_size, PAGE_SIZE); | |
1628 | max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE); | |
1629 | ||
1630 | /* make sure there are enough buffer pages and init request with them */ | |
1631 | err = -ENOMEM; | |
1632 | if (max_pages > FUSE_MAX_PAGES_PER_REQ) | |
1633 | goto out; | |
1634 | while (num_pages < max_pages) { | |
1635 | pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
1636 | if (!pages[num_pages]) | |
1637 | goto out; | |
1638 | num_pages++; | |
1639 | } | |
1640 | ||
1641 | req = fuse_get_req(fc); | |
1642 | if (IS_ERR(req)) { | |
1643 | err = PTR_ERR(req); | |
1644 | req = NULL; | |
1645 | goto out; | |
1646 | } | |
1647 | memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages); | |
1648 | req->num_pages = num_pages; | |
1649 | ||
1650 | /* okay, let's send it to the client */ | |
1651 | req->in.h.opcode = FUSE_IOCTL; | |
1652 | req->in.h.nodeid = get_node_id(inode); | |
1653 | req->in.numargs = 1; | |
1654 | req->in.args[0].size = sizeof(inarg); | |
1655 | req->in.args[0].value = &inarg; | |
1656 | if (in_size) { | |
1657 | req->in.numargs++; | |
1658 | req->in.args[1].size = in_size; | |
1659 | req->in.argpages = 1; | |
1660 | ||
1661 | err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size, | |
1662 | false); | |
1663 | if (err) | |
1664 | goto out; | |
1665 | } | |
1666 | ||
1667 | req->out.numargs = 2; | |
1668 | req->out.args[0].size = sizeof(outarg); | |
1669 | req->out.args[0].value = &outarg; | |
1670 | req->out.args[1].size = out_size; | |
1671 | req->out.argpages = 1; | |
1672 | req->out.argvar = 1; | |
1673 | ||
1674 | request_send(fc, req); | |
1675 | err = req->out.h.error; | |
1676 | transferred = req->out.args[1].size; | |
1677 | fuse_put_request(fc, req); | |
1678 | req = NULL; | |
1679 | if (err) | |
1680 | goto out; | |
1681 | ||
1682 | /* did it ask for retry? */ | |
1683 | if (outarg.flags & FUSE_IOCTL_RETRY) { | |
1684 | char *vaddr; | |
1685 | ||
1686 | /* no retry if in restricted mode */ | |
1687 | err = -EIO; | |
1688 | if (!(flags & FUSE_IOCTL_UNRESTRICTED)) | |
1689 | goto out; | |
1690 | ||
1691 | in_iovs = outarg.in_iovs; | |
1692 | out_iovs = outarg.out_iovs; | |
1693 | ||
1694 | /* | |
1695 | * Make sure things are in boundary, separate checks | |
1696 | * are to protect against overflow. | |
1697 | */ | |
1698 | err = -ENOMEM; | |
1699 | if (in_iovs > FUSE_IOCTL_MAX_IOV || | |
1700 | out_iovs > FUSE_IOCTL_MAX_IOV || | |
1701 | in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV) | |
1702 | goto out; | |
1703 | ||
1704 | err = -EIO; | |
1705 | if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred) | |
1706 | goto out; | |
1707 | ||
1708 | /* okay, copy in iovs and retry */ | |
1709 | vaddr = kmap_atomic(pages[0], KM_USER0); | |
1710 | memcpy(page_address(iov_page), vaddr, transferred); | |
1711 | kunmap_atomic(vaddr, KM_USER0); | |
1712 | ||
1713 | in_iov = page_address(iov_page); | |
1714 | out_iov = in_iov + in_iovs; | |
1715 | ||
1716 | goto retry; | |
1717 | } | |
1718 | ||
1719 | err = -EIO; | |
1720 | if (transferred > inarg.out_size) | |
1721 | goto out; | |
1722 | ||
1723 | err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true); | |
1724 | out: | |
1725 | if (req) | |
1726 | fuse_put_request(fc, req); | |
1727 | if (iov_page) | |
1728 | __free_page(iov_page); | |
1729 | while (num_pages) | |
1730 | __free_page(pages[--num_pages]); | |
1731 | kfree(pages); | |
1732 | ||
1733 | return err ? err : outarg.result; | |
1734 | } | |
1735 | ||
1736 | static long fuse_file_ioctl(struct file *file, unsigned int cmd, | |
1737 | unsigned long arg) | |
1738 | { | |
1739 | return fuse_file_do_ioctl(file, cmd, arg, 0); | |
1740 | } | |
1741 | ||
1742 | static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, | |
1743 | unsigned long arg) | |
1744 | { | |
1745 | return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT); | |
1746 | } | |
1747 | ||
4b6f5d20 | 1748 | static const struct file_operations fuse_file_operations = { |
5559b8f4 | 1749 | .llseek = fuse_file_llseek, |
543ade1f | 1750 | .read = do_sync_read, |
bcb4be80 | 1751 | .aio_read = fuse_file_aio_read, |
543ade1f | 1752 | .write = do_sync_write, |
ea9b9907 | 1753 | .aio_write = fuse_file_aio_write, |
b6aeaded MS |
1754 | .mmap = fuse_file_mmap, |
1755 | .open = fuse_open, | |
1756 | .flush = fuse_flush, | |
1757 | .release = fuse_release, | |
1758 | .fsync = fuse_fsync, | |
71421259 | 1759 | .lock = fuse_file_lock, |
a9ff4f87 | 1760 | .flock = fuse_file_flock, |
5ffc4ef4 | 1761 | .splice_read = generic_file_splice_read, |
59efec7b TH |
1762 | .unlocked_ioctl = fuse_file_ioctl, |
1763 | .compat_ioctl = fuse_file_compat_ioctl, | |
b6aeaded MS |
1764 | }; |
1765 | ||
4b6f5d20 | 1766 | static const struct file_operations fuse_direct_io_file_operations = { |
5559b8f4 | 1767 | .llseek = fuse_file_llseek, |
413ef8cb MS |
1768 | .read = fuse_direct_read, |
1769 | .write = fuse_direct_write, | |
1770 | .open = fuse_open, | |
1771 | .flush = fuse_flush, | |
1772 | .release = fuse_release, | |
1773 | .fsync = fuse_fsync, | |
71421259 | 1774 | .lock = fuse_file_lock, |
a9ff4f87 | 1775 | .flock = fuse_file_flock, |
59efec7b TH |
1776 | .unlocked_ioctl = fuse_file_ioctl, |
1777 | .compat_ioctl = fuse_file_compat_ioctl, | |
5ffc4ef4 | 1778 | /* no mmap and splice_read */ |
413ef8cb MS |
1779 | }; |
1780 | ||
f5e54d6e | 1781 | static const struct address_space_operations fuse_file_aops = { |
b6aeaded | 1782 | .readpage = fuse_readpage, |
3be5a52b MS |
1783 | .writepage = fuse_writepage, |
1784 | .launder_page = fuse_launder_page, | |
5e6f58a1 NP |
1785 | .write_begin = fuse_write_begin, |
1786 | .write_end = fuse_write_end, | |
db50b96c | 1787 | .readpages = fuse_readpages, |
3be5a52b | 1788 | .set_page_dirty = __set_page_dirty_nobuffers, |
b2d2272f | 1789 | .bmap = fuse_bmap, |
b6aeaded MS |
1790 | }; |
1791 | ||
1792 | void fuse_init_file_inode(struct inode *inode) | |
1793 | { | |
45323fb7 MS |
1794 | inode->i_fop = &fuse_file_operations; |
1795 | inode->i_data.a_ops = &fuse_file_aops; | |
b6aeaded | 1796 | } |