~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/fuse/file.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp