LCOV - code coverage report
Current view: top level - fs/fuse - file.c (source / functions) Hit Total Coverage
Test: Real Lines: 0 1396 0.0 %
Date: 2020-10-17 15:46:16 Functions: 0 97 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14