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

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *  fs/nfs/nfs4proc.c
       3                 :            :  *
       4                 :            :  *  Client-side procedure declarations for NFSv4.
       5                 :            :  *
       6                 :            :  *  Copyright (c) 2002 The Regents of the University of Michigan.
       7                 :            :  *  All rights reserved.
       8                 :            :  *
       9                 :            :  *  Kendrick Smith <kmsmith@umich.edu>
      10                 :            :  *  Andy Adamson   <andros@umich.edu>
      11                 :            :  *
      12                 :            :  *  Redistribution and use in source and binary forms, with or without
      13                 :            :  *  modification, are permitted provided that the following conditions
      14                 :            :  *  are met:
      15                 :            :  *
      16                 :            :  *  1. Redistributions of source code must retain the above copyright
      17                 :            :  *     notice, this list of conditions and the following disclaimer.
      18                 :            :  *  2. Redistributions in binary form must reproduce the above copyright
      19                 :            :  *     notice, this list of conditions and the following disclaimer in the
      20                 :            :  *     documentation and/or other materials provided with the distribution.
      21                 :            :  *  3. Neither the name of the University nor the names of its
      22                 :            :  *     contributors may be used to endorse or promote products derived
      23                 :            :  *     from this software without specific prior written permission.
      24                 :            :  *
      25                 :            :  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
      26                 :            :  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
      27                 :            :  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      28                 :            :  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
      29                 :            :  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      30                 :            :  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      31                 :            :  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
      32                 :            :  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
      33                 :            :  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
      34                 :            :  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      35                 :            :  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      36                 :            :  */
      37                 :            : 
      38                 :            : #include <linux/mm.h>
      39                 :            : #include <linux/delay.h>
      40                 :            : #include <linux/errno.h>
      41                 :            : #include <linux/string.h>
      42                 :            : #include <linux/ratelimit.h>
      43                 :            : #include <linux/printk.h>
      44                 :            : #include <linux/slab.h>
      45                 :            : #include <linux/sunrpc/clnt.h>
      46                 :            : #include <linux/nfs.h>
      47                 :            : #include <linux/nfs4.h>
      48                 :            : #include <linux/nfs_fs.h>
      49                 :            : #include <linux/nfs_page.h>
      50                 :            : #include <linux/nfs_mount.h>
      51                 :            : #include <linux/namei.h>
      52                 :            : #include <linux/mount.h>
      53                 :            : #include <linux/module.h>
      54                 :            : #include <linux/xattr.h>
      55                 :            : #include <linux/utsname.h>
      56                 :            : #include <linux/freezer.h>
      57                 :            : #include <linux/iversion.h>
      58                 :            : 
      59                 :            : #include "nfs4_fs.h"
      60                 :            : #include "delegation.h"
      61                 :            : #include "internal.h"
      62                 :            : #include "iostat.h"
      63                 :            : #include "callback.h"
      64                 :            : #include "pnfs.h"
      65                 :            : #include "netns.h"
      66                 :            : #include "nfs4idmap.h"
      67                 :            : #include "nfs4session.h"
      68                 :            : #include "fscache.h"
      69                 :            : 
      70                 :            : #include "nfs4trace.h"
      71                 :            : 
      72                 :            : #define NFSDBG_FACILITY         NFSDBG_PROC
      73                 :            : 
      74                 :            : #define NFS4_BITMASK_SZ         3
      75                 :            : 
      76                 :            : #define NFS4_POLL_RETRY_MIN     (HZ/10)
      77                 :            : #define NFS4_POLL_RETRY_MAX     (15*HZ)
      78                 :            : 
      79                 :            : /* file attributes which can be mapped to nfs attributes */
      80                 :            : #define NFS4_VALID_ATTRS (ATTR_MODE \
      81                 :            :         | ATTR_UID \
      82                 :            :         | ATTR_GID \
      83                 :            :         | ATTR_SIZE \
      84                 :            :         | ATTR_ATIME \
      85                 :            :         | ATTR_MTIME \
      86                 :            :         | ATTR_CTIME \
      87                 :            :         | ATTR_ATIME_SET \
      88                 :            :         | ATTR_MTIME_SET)
      89                 :            : 
      90                 :            : struct nfs4_opendata;
      91                 :            : static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
      92                 :            : static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
      93                 :            : static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
      94                 :            : static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
      95                 :            : static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
      96                 :            : static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
      97                 :            :                             struct nfs_fattr *fattr, struct iattr *sattr,
      98                 :            :                             struct nfs_open_context *ctx, struct nfs4_label *ilabel,
      99                 :            :                             struct nfs4_label *olabel);
     100                 :            : #ifdef CONFIG_NFS_V4_1
     101                 :            : static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
     102                 :            :                 const struct cred *cred,
     103                 :            :                 struct nfs4_slot *slot,
     104                 :            :                 bool is_privileged);
     105                 :            : static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
     106                 :            :                 const struct cred *);
     107                 :            : static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
     108                 :            :                 const struct cred *, bool);
     109                 :            : #endif
     110                 :            : 
     111                 :            : #ifdef CONFIG_NFS_V4_SECURITY_LABEL
     112                 :            : static inline struct nfs4_label *
     113                 :          0 : nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
     114                 :            :         struct iattr *sattr, struct nfs4_label *label)
     115                 :            : {
     116                 :            :         int err;
     117                 :            : 
     118                 :          0 :         if (label == NULL)
     119                 :            :                 return NULL;
     120                 :            : 
     121                 :          0 :         if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
     122                 :            :                 return NULL;
     123                 :            : 
     124                 :          0 :         err = security_dentry_init_security(dentry, sattr->ia_mode,
     125                 :          0 :                                 &dentry->d_name, (void **)&label->label, &label->len);
     126                 :          0 :         if (err == 0)
     127                 :          0 :                 return label;
     128                 :            : 
     129                 :            :         return NULL;
     130                 :            : }
     131                 :            : static inline void
     132                 :            : nfs4_label_release_security(struct nfs4_label *label)
     133                 :            : {
     134                 :          0 :         if (label)
     135                 :          0 :                 security_release_secctx(label->label, label->len);
     136                 :            : }
     137                 :            : static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
     138                 :            : {
     139                 :          0 :         if (label)
     140                 :          0 :                 return server->attr_bitmask;
     141                 :            : 
     142                 :          0 :         return server->attr_bitmask_nl;
     143                 :            : }
     144                 :            : #else
     145                 :            : static inline struct nfs4_label *
     146                 :            : nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
     147                 :            :         struct iattr *sattr, struct nfs4_label *l)
     148                 :            : { return NULL; }
     149                 :            : static inline void
     150                 :            : nfs4_label_release_security(struct nfs4_label *label)
     151                 :            : { return; }
     152                 :            : static inline u32 *
     153                 :            : nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
     154                 :            : { return server->attr_bitmask; }
     155                 :            : #endif
     156                 :            : 
     157                 :            : /* Prevent leaks of NFSv4 errors into userland */
     158                 :            : static int nfs4_map_errors(int err)
     159                 :            : {
     160                 :          0 :         if (err >= -1000)
     161                 :            :                 return err;
     162                 :          0 :         switch (err) {
     163                 :            :         case -NFS4ERR_RESOURCE:
     164                 :            :         case -NFS4ERR_LAYOUTTRYLATER:
     165                 :            :         case -NFS4ERR_RECALLCONFLICT:
     166                 :            :                 return -EREMOTEIO;
     167                 :            :         case -NFS4ERR_WRONGSEC:
     168                 :            :         case -NFS4ERR_WRONG_CRED:
     169                 :            :                 return -EPERM;
     170                 :            :         case -NFS4ERR_BADOWNER:
     171                 :            :         case -NFS4ERR_BADNAME:
     172                 :            :                 return -EINVAL;
     173                 :            :         case -NFS4ERR_SHARE_DENIED:
     174                 :            :                 return -EACCES;
     175                 :            :         case -NFS4ERR_MINOR_VERS_MISMATCH:
     176                 :            :                 return -EPROTONOSUPPORT;
     177                 :            :         case -NFS4ERR_FILE_OPEN:
     178                 :            :                 return -EBUSY;
     179                 :            :         default:
     180                 :            :                 dprintk("%s could not handle NFSv4 error %d\n",
     181                 :            :                                 __func__, -err);
     182                 :            :                 break;
     183                 :            :         }
     184                 :            :         return -EIO;
     185                 :            : }
     186                 :            : 
     187                 :            : /*
     188                 :            :  * This is our standard bitmap for GETATTR requests.
     189                 :            :  */
     190                 :            : const u32 nfs4_fattr_bitmap[3] = {
     191                 :            :         FATTR4_WORD0_TYPE
     192                 :            :         | FATTR4_WORD0_CHANGE
     193                 :            :         | FATTR4_WORD0_SIZE
     194                 :            :         | FATTR4_WORD0_FSID
     195                 :            :         | FATTR4_WORD0_FILEID,
     196                 :            :         FATTR4_WORD1_MODE
     197                 :            :         | FATTR4_WORD1_NUMLINKS
     198                 :            :         | FATTR4_WORD1_OWNER
     199                 :            :         | FATTR4_WORD1_OWNER_GROUP
     200                 :            :         | FATTR4_WORD1_RAWDEV
     201                 :            :         | FATTR4_WORD1_SPACE_USED
     202                 :            :         | FATTR4_WORD1_TIME_ACCESS
     203                 :            :         | FATTR4_WORD1_TIME_METADATA
     204                 :            :         | FATTR4_WORD1_TIME_MODIFY
     205                 :            :         | FATTR4_WORD1_MOUNTED_ON_FILEID,
     206                 :            : #ifdef CONFIG_NFS_V4_SECURITY_LABEL
     207                 :            :         FATTR4_WORD2_SECURITY_LABEL
     208                 :            : #endif
     209                 :            : };
     210                 :            : 
     211                 :            : static const u32 nfs4_pnfs_open_bitmap[3] = {
     212                 :            :         FATTR4_WORD0_TYPE
     213                 :            :         | FATTR4_WORD0_CHANGE
     214                 :            :         | FATTR4_WORD0_SIZE
     215                 :            :         | FATTR4_WORD0_FSID
     216                 :            :         | FATTR4_WORD0_FILEID,
     217                 :            :         FATTR4_WORD1_MODE
     218                 :            :         | FATTR4_WORD1_NUMLINKS
     219                 :            :         | FATTR4_WORD1_OWNER
     220                 :            :         | FATTR4_WORD1_OWNER_GROUP
     221                 :            :         | FATTR4_WORD1_RAWDEV
     222                 :            :         | FATTR4_WORD1_SPACE_USED
     223                 :            :         | FATTR4_WORD1_TIME_ACCESS
     224                 :            :         | FATTR4_WORD1_TIME_METADATA
     225                 :            :         | FATTR4_WORD1_TIME_MODIFY,
     226                 :            :         FATTR4_WORD2_MDSTHRESHOLD
     227                 :            : #ifdef CONFIG_NFS_V4_SECURITY_LABEL
     228                 :            :         | FATTR4_WORD2_SECURITY_LABEL
     229                 :            : #endif
     230                 :            : };
     231                 :            : 
     232                 :            : static const u32 nfs4_open_noattr_bitmap[3] = {
     233                 :            :         FATTR4_WORD0_TYPE
     234                 :            :         | FATTR4_WORD0_FILEID,
     235                 :            : };
     236                 :            : 
     237                 :            : const u32 nfs4_statfs_bitmap[3] = {
     238                 :            :         FATTR4_WORD0_FILES_AVAIL
     239                 :            :         | FATTR4_WORD0_FILES_FREE
     240                 :            :         | FATTR4_WORD0_FILES_TOTAL,
     241                 :            :         FATTR4_WORD1_SPACE_AVAIL
     242                 :            :         | FATTR4_WORD1_SPACE_FREE
     243                 :            :         | FATTR4_WORD1_SPACE_TOTAL
     244                 :            : };
     245                 :            : 
     246                 :            : const u32 nfs4_pathconf_bitmap[3] = {
     247                 :            :         FATTR4_WORD0_MAXLINK
     248                 :            :         | FATTR4_WORD0_MAXNAME,
     249                 :            :         0
     250                 :            : };
     251                 :            : 
     252                 :            : const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
     253                 :            :                         | FATTR4_WORD0_MAXREAD
     254                 :            :                         | FATTR4_WORD0_MAXWRITE
     255                 :            :                         | FATTR4_WORD0_LEASE_TIME,
     256                 :            :                         FATTR4_WORD1_TIME_DELTA
     257                 :            :                         | FATTR4_WORD1_FS_LAYOUT_TYPES,
     258                 :            :                         FATTR4_WORD2_LAYOUT_BLKSIZE
     259                 :            :                         | FATTR4_WORD2_CLONE_BLKSIZE
     260                 :            : };
     261                 :            : 
     262                 :            : const u32 nfs4_fs_locations_bitmap[3] = {
     263                 :            :         FATTR4_WORD0_CHANGE
     264                 :            :         | FATTR4_WORD0_SIZE
     265                 :            :         | FATTR4_WORD0_FSID
     266                 :            :         | FATTR4_WORD0_FILEID
     267                 :            :         | FATTR4_WORD0_FS_LOCATIONS,
     268                 :            :         FATTR4_WORD1_OWNER
     269                 :            :         | FATTR4_WORD1_OWNER_GROUP
     270                 :            :         | FATTR4_WORD1_RAWDEV
     271                 :            :         | FATTR4_WORD1_SPACE_USED
     272                 :            :         | FATTR4_WORD1_TIME_ACCESS
     273                 :            :         | FATTR4_WORD1_TIME_METADATA
     274                 :            :         | FATTR4_WORD1_TIME_MODIFY
     275                 :            :         | FATTR4_WORD1_MOUNTED_ON_FILEID,
     276                 :            : };
     277                 :            : 
     278                 :          0 : static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
     279                 :            :                 struct inode *inode)
     280                 :            : {
     281                 :            :         unsigned long cache_validity;
     282                 :            : 
     283                 :          0 :         memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
     284                 :          0 :         if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
     285                 :          0 :                 return;
     286                 :            : 
     287                 :            :         cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
     288                 :          0 :         if (!(cache_validity & NFS_INO_REVAL_FORCED))
     289                 :          0 :                 cache_validity &= ~(NFS_INO_INVALID_CHANGE
     290                 :            :                                 | NFS_INO_INVALID_SIZE);
     291                 :            : 
     292                 :          0 :         if (!(cache_validity & NFS_INO_INVALID_SIZE))
     293                 :          0 :                 dst[0] &= ~FATTR4_WORD0_SIZE;
     294                 :            : 
     295                 :          0 :         if (!(cache_validity & NFS_INO_INVALID_CHANGE))
     296                 :          0 :                 dst[0] &= ~FATTR4_WORD0_CHANGE;
     297                 :            : }
     298                 :            : 
     299                 :            : static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
     300                 :            :                 const __u32 *src, struct inode *inode)
     301                 :            : {
     302                 :          0 :         nfs4_bitmap_copy_adjust(dst, src, inode);
     303                 :            : }
     304                 :            : 
     305                 :          0 : static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
     306                 :            :                 struct nfs4_readdir_arg *readdir)
     307                 :            : {
     308                 :            :         unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
     309                 :            :         __be32 *start, *p;
     310                 :            : 
     311                 :          0 :         if (cookie > 2) {
     312                 :          0 :                 readdir->cookie = cookie;
     313                 :          0 :                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
     314                 :          0 :                 return;
     315                 :            :         }
     316                 :            : 
     317                 :          0 :         readdir->cookie = 0;
     318                 :          0 :         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
     319                 :          0 :         if (cookie == 2)
     320                 :            :                 return;
     321                 :            :         
     322                 :            :         /*
     323                 :            :          * NFSv4 servers do not return entries for '.' and '..'
     324                 :            :          * Therefore, we fake these entries here.  We let '.'
     325                 :            :          * have cookie 0 and '..' have cookie 1.  Note that
     326                 :            :          * when talking to the server, we always send cookie 0
     327                 :            :          * instead of 1 or 2.
     328                 :            :          */
     329                 :          0 :         start = p = kmap_atomic(*readdir->pages);
     330                 :            :         
     331                 :          0 :         if (cookie == 0) {
     332                 :          0 :                 *p++ = xdr_one;                                  /* next */
     333                 :          0 :                 *p++ = xdr_zero;                   /* cookie, first word */
     334                 :          0 :                 *p++ = xdr_one;                   /* cookie, second word */
     335                 :          0 :                 *p++ = xdr_one;                             /* entry len */
     336                 :          0 :                 memcpy(p, ".\0\0\0", 4);                        /* entry */
     337                 :            :                 p++;
     338                 :          0 :                 *p++ = xdr_one;                         /* bitmap length */
     339                 :          0 :                 *p++ = htonl(attrs);                           /* bitmap */
     340                 :          0 :                 *p++ = htonl(12);             /* attribute buffer length */
     341                 :          0 :                 *p++ = htonl(NF4DIR);
     342                 :            :                 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
     343                 :            :         }
     344                 :            :         
     345                 :          0 :         *p++ = xdr_one;                                  /* next */
     346                 :          0 :         *p++ = xdr_zero;                   /* cookie, first word */
     347                 :          0 :         *p++ = xdr_two;                   /* cookie, second word */
     348                 :          0 :         *p++ = xdr_two;                             /* entry len */
     349                 :          0 :         memcpy(p, "..\0\0", 4);                         /* entry */
     350                 :            :         p++;
     351                 :          0 :         *p++ = xdr_one;                         /* bitmap length */
     352                 :          0 :         *p++ = htonl(attrs);                           /* bitmap */
     353                 :          0 :         *p++ = htonl(12);             /* attribute buffer length */
     354                 :          0 :         *p++ = htonl(NF4DIR);
     355                 :          0 :         p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
     356                 :            : 
     357                 :          0 :         readdir->pgbase = (char *)p - (char *)start;
     358                 :          0 :         readdir->count -= readdir->pgbase;
     359                 :            :         kunmap_atomic(start);
     360                 :            : }
     361                 :            : 
     362                 :            : static void nfs4_test_and_free_stateid(struct nfs_server *server,
     363                 :            :                 nfs4_stateid *stateid,
     364                 :            :                 const struct cred *cred)
     365                 :            : {
     366                 :          0 :         const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
     367                 :            : 
     368                 :          0 :         ops->test_and_free_expired(server, stateid, cred);
     369                 :            : }
     370                 :            : 
     371                 :            : static void __nfs4_free_revoked_stateid(struct nfs_server *server,
     372                 :            :                 nfs4_stateid *stateid,
     373                 :            :                 const struct cred *cred)
     374                 :            : {
     375                 :          0 :         stateid->type = NFS4_REVOKED_STATEID_TYPE;
     376                 :            :         nfs4_test_and_free_stateid(server, stateid, cred);
     377                 :            : }
     378                 :            : 
     379                 :          0 : static void nfs4_free_revoked_stateid(struct nfs_server *server,
     380                 :            :                 const nfs4_stateid *stateid,
     381                 :            :                 const struct cred *cred)
     382                 :            : {
     383                 :            :         nfs4_stateid tmp;
     384                 :            : 
     385                 :            :         nfs4_stateid_copy(&tmp, stateid);
     386                 :            :         __nfs4_free_revoked_stateid(server, &tmp, cred);
     387                 :          0 : }
     388                 :            : 
     389                 :            : static long nfs4_update_delay(long *timeout)
     390                 :            : {
     391                 :            :         long ret;
     392                 :          0 :         if (!timeout)
     393                 :            :                 return NFS4_POLL_RETRY_MAX;
     394                 :          0 :         if (*timeout <= 0)
     395                 :          0 :                 *timeout = NFS4_POLL_RETRY_MIN;
     396                 :          0 :         if (*timeout > NFS4_POLL_RETRY_MAX)
     397                 :          0 :                 *timeout = NFS4_POLL_RETRY_MAX;
     398                 :          0 :         ret = *timeout;
     399                 :          0 :         *timeout <<= 1;
     400                 :            :         return ret;
     401                 :            : }
     402                 :            : 
     403                 :          0 : static int nfs4_delay_killable(long *timeout)
     404                 :            : {
     405                 :          0 :         might_sleep();
     406                 :            : 
     407                 :          0 :         freezable_schedule_timeout_killable_unsafe(
     408                 :            :                 nfs4_update_delay(timeout));
     409                 :          0 :         if (!__fatal_signal_pending(current))
     410                 :            :                 return 0;
     411                 :          0 :         return -EINTR;
     412                 :            : }
     413                 :            : 
     414                 :          0 : static int nfs4_delay_interruptible(long *timeout)
     415                 :            : {
     416                 :          0 :         might_sleep();
     417                 :            : 
     418                 :          0 :         freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
     419                 :          0 :         if (!signal_pending(current))
     420                 :            :                 return 0;
     421                 :          0 :         return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
     422                 :            : }
     423                 :            : 
     424                 :          0 : static int nfs4_delay(long *timeout, bool interruptible)
     425                 :            : {
     426                 :          0 :         if (interruptible)
     427                 :          0 :                 return nfs4_delay_interruptible(timeout);
     428                 :          0 :         return nfs4_delay_killable(timeout);
     429                 :            : }
     430                 :            : 
     431                 :            : static const nfs4_stateid *
     432                 :            : nfs4_recoverable_stateid(const nfs4_stateid *stateid)
     433                 :            : {
     434                 :          0 :         if (!stateid)
     435                 :            :                 return NULL;
     436                 :          0 :         switch (stateid->type) {
     437                 :            :         case NFS4_OPEN_STATEID_TYPE:
     438                 :            :         case NFS4_LOCK_STATEID_TYPE:
     439                 :            :         case NFS4_DELEGATION_STATEID_TYPE:
     440                 :            :                 return stateid;
     441                 :            :         default:
     442                 :            :                 break;
     443                 :            :         }
     444                 :            :         return NULL;
     445                 :            : }
     446                 :            : 
     447                 :            : /* This is the error handling routine for processes that are allowed
     448                 :            :  * to sleep.
     449                 :            :  */
     450                 :          0 : static int nfs4_do_handle_exception(struct nfs_server *server,
     451                 :            :                 int errorcode, struct nfs4_exception *exception)
     452                 :            : {
     453                 :          0 :         struct nfs_client *clp = server->nfs_client;
     454                 :          0 :         struct nfs4_state *state = exception->state;
     455                 :            :         const nfs4_stateid *stateid;
     456                 :          0 :         struct inode *inode = exception->inode;
     457                 :            :         int ret = errorcode;
     458                 :            : 
     459                 :          0 :         exception->delay = 0;
     460                 :          0 :         exception->recovering = 0;
     461                 :          0 :         exception->retry = 0;
     462                 :            : 
     463                 :          0 :         stateid = nfs4_recoverable_stateid(exception->stateid);
     464                 :          0 :         if (stateid == NULL && state != NULL)
     465                 :          0 :                 stateid = nfs4_recoverable_stateid(&state->stateid);
     466                 :            : 
     467                 :          0 :         switch(errorcode) {
     468                 :            :                 case 0:
     469                 :            :                         return 0;
     470                 :            :                 case -NFS4ERR_BADHANDLE:
     471                 :            :                 case -ESTALE:
     472                 :          0 :                         if (inode != NULL && S_ISREG(inode->i_mode))
     473                 :          0 :                                 pnfs_destroy_layout(NFS_I(inode));
     474                 :            :                         break;
     475                 :            :                 case -NFS4ERR_DELEG_REVOKED:
     476                 :            :                 case -NFS4ERR_ADMIN_REVOKED:
     477                 :            :                 case -NFS4ERR_EXPIRED:
     478                 :            :                 case -NFS4ERR_BAD_STATEID:
     479                 :          0 :                         if (inode != NULL && stateid != NULL) {
     480                 :          0 :                                 nfs_inode_find_state_and_recover(inode,
     481                 :            :                                                 stateid);
     482                 :          0 :                                 goto wait_on_recovery;
     483                 :            :                         }
     484                 :            :                         /* Fall through */
     485                 :            :                 case -NFS4ERR_OPENMODE:
     486                 :          0 :                         if (inode) {
     487                 :            :                                 int err;
     488                 :            : 
     489                 :          0 :                                 err = nfs_async_inode_return_delegation(inode,
     490                 :            :                                                 stateid);
     491                 :          0 :                                 if (err == 0)
     492                 :            :                                         goto wait_on_recovery;
     493                 :          0 :                                 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
     494                 :          0 :                                         exception->retry = 1;
     495                 :          0 :                                         break;
     496                 :            :                                 }
     497                 :            :                         }
     498                 :          0 :                         if (state == NULL)
     499                 :            :                                 break;
     500                 :          0 :                         ret = nfs4_schedule_stateid_recovery(server, state);
     501                 :          0 :                         if (ret < 0)
     502                 :            :                                 break;
     503                 :            :                         goto wait_on_recovery;
     504                 :            :                 case -NFS4ERR_STALE_STATEID:
     505                 :            :                 case -NFS4ERR_STALE_CLIENTID:
     506                 :          0 :                         nfs4_schedule_lease_recovery(clp);
     507                 :          0 :                         goto wait_on_recovery;
     508                 :            :                 case -NFS4ERR_MOVED:
     509                 :          0 :                         ret = nfs4_schedule_migration_recovery(server);
     510                 :          0 :                         if (ret < 0)
     511                 :            :                                 break;
     512                 :            :                         goto wait_on_recovery;
     513                 :            :                 case -NFS4ERR_LEASE_MOVED:
     514                 :          0 :                         nfs4_schedule_lease_moved_recovery(clp);
     515                 :          0 :                         goto wait_on_recovery;
     516                 :            : #if defined(CONFIG_NFS_V4_1)
     517                 :            :                 case -NFS4ERR_BADSESSION:
     518                 :            :                 case -NFS4ERR_BADSLOT:
     519                 :            :                 case -NFS4ERR_BAD_HIGH_SLOT:
     520                 :            :                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
     521                 :            :                 case -NFS4ERR_DEADSESSION:
     522                 :            :                 case -NFS4ERR_SEQ_FALSE_RETRY:
     523                 :            :                 case -NFS4ERR_SEQ_MISORDERED:
     524                 :            :                         /* Handled in nfs41_sequence_process() */
     525                 :            :                         goto wait_on_recovery;
     526                 :            : #endif /* defined(CONFIG_NFS_V4_1) */
     527                 :            :                 case -NFS4ERR_FILE_OPEN:
     528                 :          0 :                         if (exception->timeout > HZ) {
     529                 :            :                                 /* We have retried a decent amount, time to
     530                 :            :                                  * fail
     531                 :            :                                  */
     532                 :            :                                 ret = -EBUSY;
     533                 :            :                                 break;
     534                 :            :                         }
     535                 :            :                         /* Fall through */
     536                 :            :                 case -NFS4ERR_DELAY:
     537                 :            :                         nfs_inc_server_stats(server, NFSIOS_DELAY);
     538                 :            :                         /* Fall through */
     539                 :            :                 case -NFS4ERR_GRACE:
     540                 :            :                 case -NFS4ERR_LAYOUTTRYLATER:
     541                 :            :                 case -NFS4ERR_RECALLCONFLICT:
     542                 :          0 :                         exception->delay = 1;
     543                 :          0 :                         return 0;
     544                 :            : 
     545                 :            :                 case -NFS4ERR_RETRY_UNCACHED_REP:
     546                 :            :                 case -NFS4ERR_OLD_STATEID:
     547                 :          0 :                         exception->retry = 1;
     548                 :          0 :                         break;
     549                 :            :                 case -NFS4ERR_BADOWNER:
     550                 :            :                         /* The following works around a Linux server bug! */
     551                 :            :                 case -NFS4ERR_BADNAME:
     552                 :          0 :                         if (server->caps & NFS_CAP_UIDGID_NOMAP) {
     553                 :          0 :                                 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
     554                 :          0 :                                 exception->retry = 1;
     555                 :          0 :                                 printk(KERN_WARNING "NFS: v4 server %s "
     556                 :            :                                                 "does not accept raw "
     557                 :            :                                                 "uid/gids. "
     558                 :            :                                                 "Reenabling the idmapper.\n",
     559                 :          0 :                                                 server->nfs_client->cl_hostname);
     560                 :            :                         }
     561                 :            :         }
     562                 :            :         /* We failed to handle the error */
     563                 :          0 :         return nfs4_map_errors(ret);
     564                 :            : wait_on_recovery:
     565                 :          0 :         exception->recovering = 1;
     566                 :          0 :         return 0;
     567                 :            : }
     568                 :            : 
     569                 :            : /* This is the error handling routine for processes that are allowed
     570                 :            :  * to sleep.
     571                 :            :  */
     572                 :          0 : int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
     573                 :            : {
     574                 :          0 :         struct nfs_client *clp = server->nfs_client;
     575                 :            :         int ret;
     576                 :            : 
     577                 :          0 :         ret = nfs4_do_handle_exception(server, errorcode, exception);
     578                 :          0 :         if (exception->delay) {
     579                 :          0 :                 ret = nfs4_delay(&exception->timeout,
     580                 :            :                                 exception->interruptible);
     581                 :          0 :                 goto out_retry;
     582                 :            :         }
     583                 :          0 :         if (exception->recovering) {
     584                 :          0 :                 ret = nfs4_wait_clnt_recover(clp);
     585                 :          0 :                 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
     586                 :            :                         return -EIO;
     587                 :            :                 goto out_retry;
     588                 :            :         }
     589                 :            :         return ret;
     590                 :            : out_retry:
     591                 :          0 :         if (ret == 0)
     592                 :          0 :                 exception->retry = 1;
     593                 :          0 :         return ret;
     594                 :            : }
     595                 :            : 
     596                 :            : static int
     597                 :          0 : nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
     598                 :            :                 int errorcode, struct nfs4_exception *exception)
     599                 :            : {
     600                 :          0 :         struct nfs_client *clp = server->nfs_client;
     601                 :            :         int ret;
     602                 :            : 
     603                 :          0 :         ret = nfs4_do_handle_exception(server, errorcode, exception);
     604                 :          0 :         if (exception->delay) {
     605                 :          0 :                 rpc_delay(task, nfs4_update_delay(&exception->timeout));
     606                 :          0 :                 goto out_retry;
     607                 :            :         }
     608                 :          0 :         if (exception->recovering) {
     609                 :          0 :                 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
     610                 :          0 :                 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
     611                 :          0 :                         rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
     612                 :            :                 goto out_retry;
     613                 :            :         }
     614                 :          0 :         if (test_bit(NFS_MIG_FAILED, &server->mig_status))
     615                 :            :                 ret = -EIO;
     616                 :          0 :         return ret;
     617                 :            : out_retry:
     618                 :          0 :         if (ret == 0) {
     619                 :          0 :                 exception->retry = 1;
     620                 :            :                 /*
     621                 :            :                  * For NFS4ERR_MOVED, the client transport will need to
     622                 :            :                  * be recomputed after migration recovery has completed.
     623                 :            :                  */
     624                 :          0 :                 if (errorcode == -NFS4ERR_MOVED)
     625                 :          0 :                         rpc_task_release_transport(task);
     626                 :            :         }
     627                 :          0 :         return ret;
     628                 :            : }
     629                 :            : 
     630                 :            : int
     631                 :          0 : nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
     632                 :            :                         struct nfs4_state *state, long *timeout)
     633                 :            : {
     634                 :          0 :         struct nfs4_exception exception = {
     635                 :            :                 .state = state,
     636                 :            :         };
     637                 :            : 
     638                 :          0 :         if (task->tk_status >= 0)
     639                 :            :                 return 0;
     640                 :          0 :         if (timeout)
     641                 :          0 :                 exception.timeout = *timeout;
     642                 :          0 :         task->tk_status = nfs4_async_handle_exception(task, server,
     643                 :            :                         task->tk_status,
     644                 :            :                         &exception);
     645                 :          0 :         if (exception.delay && timeout)
     646                 :          0 :                 *timeout = exception.timeout;
     647                 :          0 :         if (exception.retry)
     648                 :            :                 return -EAGAIN;
     649                 :          0 :         return 0;
     650                 :            : }
     651                 :            : 
     652                 :            : /*
     653                 :            :  * Return 'true' if 'clp' is using an rpc_client that is integrity protected
     654                 :            :  * or 'false' otherwise.
     655                 :            :  */
     656                 :            : static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
     657                 :            : {
     658                 :          0 :         rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
     659                 :          0 :         return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
     660                 :            : }
     661                 :            : 
     662                 :          0 : static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
     663                 :            : {
     664                 :            :         spin_lock(&clp->cl_lock);
     665                 :          0 :         if (time_before(clp->cl_last_renewal,timestamp))
     666                 :          0 :                 clp->cl_last_renewal = timestamp;
     667                 :            :         spin_unlock(&clp->cl_lock);
     668                 :          0 : }
     669                 :            : 
     670                 :          0 : static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
     671                 :            : {
     672                 :          0 :         struct nfs_client *clp = server->nfs_client;
     673                 :            : 
     674                 :          0 :         if (!nfs4_has_session(clp))
     675                 :          0 :                 do_renew_lease(clp, timestamp);
     676                 :          0 : }
     677                 :            : 
     678                 :            : struct nfs4_call_sync_data {
     679                 :            :         const struct nfs_server *seq_server;
     680                 :            :         struct nfs4_sequence_args *seq_args;
     681                 :            :         struct nfs4_sequence_res *seq_res;
     682                 :            : };
     683                 :            : 
     684                 :          0 : void nfs4_init_sequence(struct nfs4_sequence_args *args,
     685                 :            :                         struct nfs4_sequence_res *res, int cache_reply,
     686                 :            :                         int privileged)
     687                 :            : {
     688                 :          0 :         args->sa_slot = NULL;
     689                 :          0 :         args->sa_cache_this = cache_reply;
     690                 :          0 :         args->sa_privileged = privileged;
     691                 :            : 
     692                 :          0 :         res->sr_slot = NULL;
     693                 :          0 : }
     694                 :            : 
     695                 :          0 : static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
     696                 :            : {
     697                 :          0 :         struct nfs4_slot *slot = res->sr_slot;
     698                 :            :         struct nfs4_slot_table *tbl;
     699                 :            : 
     700                 :          0 :         tbl = slot->table;
     701                 :            :         spin_lock(&tbl->slot_tbl_lock);
     702                 :          0 :         if (!nfs41_wake_and_assign_slot(tbl, slot))
     703                 :          0 :                 nfs4_free_slot(tbl, slot);
     704                 :            :         spin_unlock(&tbl->slot_tbl_lock);
     705                 :            : 
     706                 :          0 :         res->sr_slot = NULL;
     707                 :          0 : }
     708                 :            : 
     709                 :            : static int nfs40_sequence_done(struct rpc_task *task,
     710                 :            :                                struct nfs4_sequence_res *res)
     711                 :            : {
     712                 :          0 :         if (res->sr_slot != NULL)
     713                 :          0 :                 nfs40_sequence_free_slot(res);
     714                 :            :         return 1;
     715                 :            : }
     716                 :            : 
     717                 :            : #if defined(CONFIG_NFS_V4_1)
     718                 :            : 
     719                 :          0 : static void nfs41_release_slot(struct nfs4_slot *slot)
     720                 :            : {
     721                 :            :         struct nfs4_session *session;
     722                 :            :         struct nfs4_slot_table *tbl;
     723                 :            :         bool send_new_highest_used_slotid = false;
     724                 :            : 
     725                 :          0 :         if (!slot)
     726                 :          0 :                 return;
     727                 :          0 :         tbl = slot->table;
     728                 :          0 :         session = tbl->session;
     729                 :            : 
     730                 :            :         /* Bump the slot sequence number */
     731                 :          0 :         if (slot->seq_done)
     732                 :          0 :                 slot->seq_nr++;
     733                 :          0 :         slot->seq_done = 0;
     734                 :            : 
     735                 :            :         spin_lock(&tbl->slot_tbl_lock);
     736                 :            :         /* Be nice to the server: try to ensure that the last transmitted
     737                 :            :          * value for highest_user_slotid <= target_highest_slotid
     738                 :            :          */
     739                 :          0 :         if (tbl->highest_used_slotid > tbl->target_highest_slotid)
     740                 :            :                 send_new_highest_used_slotid = true;
     741                 :            : 
     742                 :          0 :         if (nfs41_wake_and_assign_slot(tbl, slot)) {
     743                 :            :                 send_new_highest_used_slotid = false;
     744                 :            :                 goto out_unlock;
     745                 :            :         }
     746                 :          0 :         nfs4_free_slot(tbl, slot);
     747                 :            : 
     748                 :          0 :         if (tbl->highest_used_slotid != NFS4_NO_SLOT)
     749                 :            :                 send_new_highest_used_slotid = false;
     750                 :            : out_unlock:
     751                 :            :         spin_unlock(&tbl->slot_tbl_lock);
     752                 :          0 :         if (send_new_highest_used_slotid)
     753                 :          0 :                 nfs41_notify_server(session->clp);
     754                 :          0 :         if (waitqueue_active(&tbl->slot_waitq))
     755                 :          0 :                 wake_up_all(&tbl->slot_waitq);
     756                 :            : }
     757                 :            : 
     758                 :            : static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
     759                 :            : {
     760                 :          0 :         nfs41_release_slot(res->sr_slot);
     761                 :          0 :         res->sr_slot = NULL;
     762                 :            : }
     763                 :            : 
     764                 :            : static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
     765                 :            :                 u32 seqnr)
     766                 :            : {
     767                 :          0 :         if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
     768                 :          0 :                 slot->seq_nr_highest_sent = seqnr;
     769                 :            : }
     770                 :            : static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
     771                 :            :                 u32 seqnr)
     772                 :            : {
     773                 :          0 :         slot->seq_nr_highest_sent = seqnr;
     774                 :          0 :         slot->seq_nr_last_acked = seqnr;
     775                 :            : }
     776                 :            : 
     777                 :          0 : static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
     778                 :            :                                 struct nfs4_slot *slot)
     779                 :            : {
     780                 :          0 :         struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
     781                 :          0 :         if (!IS_ERR(task))
     782                 :          0 :                 rpc_put_task_async(task);
     783                 :          0 : }
     784                 :            : 
     785                 :          0 : static int nfs41_sequence_process(struct rpc_task *task,
     786                 :            :                 struct nfs4_sequence_res *res)
     787                 :            : {
     788                 :            :         struct nfs4_session *session;
     789                 :          0 :         struct nfs4_slot *slot = res->sr_slot;
     790                 :            :         struct nfs_client *clp;
     791                 :            :         int status;
     792                 :            :         int ret = 1;
     793                 :            : 
     794                 :          0 :         if (slot == NULL)
     795                 :            :                 goto out_noaction;
     796                 :            :         /* don't increment the sequence number if the task wasn't sent */
     797                 :          0 :         if (!RPC_WAS_SENT(task) || slot->seq_done)
     798                 :            :                 goto out;
     799                 :            : 
     800                 :          0 :         session = slot->table->session;
     801                 :          0 :         clp = session->clp;
     802                 :            : 
     803                 :          0 :         trace_nfs4_sequence_done(session, res);
     804                 :            : 
     805                 :          0 :         status = res->sr_status;
     806                 :          0 :         if (task->tk_status == -NFS4ERR_DEADSESSION)
     807                 :            :                 status = -NFS4ERR_DEADSESSION;
     808                 :            : 
     809                 :            :         /* Check the SEQUENCE operation status */
     810                 :          0 :         switch (status) {
     811                 :            :         case 0:
     812                 :            :                 /* Mark this sequence number as having been acked */
     813                 :          0 :                 nfs4_slot_sequence_acked(slot, slot->seq_nr);
     814                 :            :                 /* Update the slot's sequence and clientid lease timer */
     815                 :          0 :                 slot->seq_done = 1;
     816                 :          0 :                 do_renew_lease(clp, res->sr_timestamp);
     817                 :            :                 /* Check sequence flags */
     818                 :          0 :                 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
     819                 :          0 :                                 !!slot->privileged);
     820                 :          0 :                 nfs41_update_target_slotid(slot->table, slot, res);
     821                 :          0 :                 break;
     822                 :            :         case 1:
     823                 :            :                 /*
     824                 :            :                  * sr_status remains 1 if an RPC level error occurred.
     825                 :            :                  * The server may or may not have processed the sequence
     826                 :            :                  * operation..
     827                 :            :                  */
     828                 :          0 :                 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
     829                 :          0 :                 slot->seq_done = 1;
     830                 :          0 :                 goto out;
     831                 :            :         case -NFS4ERR_DELAY:
     832                 :            :                 /* The server detected a resend of the RPC call and
     833                 :            :                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
     834                 :            :                  * of RFC5661.
     835                 :            :                  */
     836                 :            :                 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
     837                 :            :                         __func__,
     838                 :            :                         slot->slot_nr,
     839                 :            :                         slot->seq_nr);
     840                 :          0 :                 nfs4_slot_sequence_acked(slot, slot->seq_nr);
     841                 :            :                 goto out_retry;
     842                 :            :         case -NFS4ERR_RETRY_UNCACHED_REP:
     843                 :            :         case -NFS4ERR_SEQ_FALSE_RETRY:
     844                 :            :                 /*
     845                 :            :                  * The server thinks we tried to replay a request.
     846                 :            :                  * Retry the call after bumping the sequence ID.
     847                 :            :                  */
     848                 :          0 :                 nfs4_slot_sequence_acked(slot, slot->seq_nr);
     849                 :            :                 goto retry_new_seq;
     850                 :            :         case -NFS4ERR_BADSLOT:
     851                 :            :                 /*
     852                 :            :                  * The slot id we used was probably retired. Try again
     853                 :            :                  * using a different slot id.
     854                 :            :                  */
     855                 :          0 :                 if (slot->slot_nr < slot->table->target_highest_slotid)
     856                 :            :                         goto session_recover;
     857                 :            :                 goto retry_nowait;
     858                 :            :         case -NFS4ERR_SEQ_MISORDERED:
     859                 :          0 :                 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
     860                 :            :                 /*
     861                 :            :                  * Were one or more calls using this slot interrupted?
     862                 :            :                  * If the server never received the request, then our
     863                 :            :                  * transmitted slot sequence number may be too high. However,
     864                 :            :                  * if the server did receive the request then it might
     865                 :            :                  * accidentally give us a reply with a mismatched operation.
     866                 :            :                  * We can sort this out by sending a lone sequence operation
     867                 :            :                  * to the server on the same slot.
     868                 :            :                  */
     869                 :          0 :                 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
     870                 :          0 :                         slot->seq_nr--;
     871                 :          0 :                         if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
     872                 :          0 :                                 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
     873                 :          0 :                                 res->sr_slot = NULL;
     874                 :            :                         }
     875                 :            :                         goto retry_nowait;
     876                 :            :                 }
     877                 :            :                 /*
     878                 :            :                  * RFC5661:
     879                 :            :                  * A retry might be sent while the original request is
     880                 :            :                  * still in progress on the replier. The replier SHOULD
     881                 :            :                  * deal with the issue by returning NFS4ERR_DELAY as the
     882                 :            :                  * reply to SEQUENCE or CB_SEQUENCE operation, but
     883                 :            :                  * implementations MAY return NFS4ERR_SEQ_MISORDERED.
     884                 :            :                  *
     885                 :            :                  * Restart the search after a delay.
     886                 :            :                  */
     887                 :          0 :                 slot->seq_nr = slot->seq_nr_highest_sent;
     888                 :          0 :                 goto out_retry;
     889                 :            :         case -NFS4ERR_BADSESSION:
     890                 :            :         case -NFS4ERR_DEADSESSION:
     891                 :            :         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
     892                 :            :                 goto session_recover;
     893                 :            :         default:
     894                 :            :                 /* Just update the slot sequence no. */
     895                 :          0 :                 slot->seq_done = 1;
     896                 :            :         }
     897                 :            : out:
     898                 :            :         /* The session may be reset by one of the error handlers. */
     899                 :            :         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
     900                 :            : out_noaction:
     901                 :          0 :         return ret;
     902                 :            : session_recover:
     903                 :          0 :         nfs4_schedule_session_recovery(session, status);
     904                 :            :         dprintk("%s ERROR: %d Reset session\n", __func__, status);
     905                 :            :         nfs41_sequence_free_slot(res);
     906                 :            :         goto out;
     907                 :            : retry_new_seq:
     908                 :          0 :         ++slot->seq_nr;
     909                 :            : retry_nowait:
     910                 :          0 :         if (rpc_restart_call_prepare(task)) {
     911                 :            :                 nfs41_sequence_free_slot(res);
     912                 :          0 :                 task->tk_status = 0;
     913                 :            :                 ret = 0;
     914                 :            :         }
     915                 :            :         goto out;
     916                 :            : out_retry:
     917                 :          0 :         if (!rpc_restart_call(task))
     918                 :            :                 goto out;
     919                 :          0 :         rpc_delay(task, NFS4_POLL_RETRY_MAX);
     920                 :          0 :         return 0;
     921                 :            : }
     922                 :            : 
     923                 :          0 : int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
     924                 :            : {
     925                 :          0 :         if (!nfs41_sequence_process(task, res))
     926                 :            :                 return 0;
     927                 :          0 :         if (res->sr_slot != NULL)
     928                 :            :                 nfs41_sequence_free_slot(res);
     929                 :            :         return 1;
     930                 :            : 
     931                 :            : }
     932                 :            : EXPORT_SYMBOL_GPL(nfs41_sequence_done);
     933                 :            : 
     934                 :          0 : static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
     935                 :            : {
     936                 :          0 :         if (res->sr_slot == NULL)
     937                 :            :                 return 1;
     938                 :          0 :         if (res->sr_slot->table->session != NULL)
     939                 :          0 :                 return nfs41_sequence_process(task, res);
     940                 :            :         return nfs40_sequence_done(task, res);
     941                 :            : }
     942                 :            : 
     943                 :          0 : static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
     944                 :            : {
     945                 :          0 :         if (res->sr_slot != NULL) {
     946                 :          0 :                 if (res->sr_slot->table->session != NULL)
     947                 :            :                         nfs41_sequence_free_slot(res);
     948                 :            :                 else
     949                 :          0 :                         nfs40_sequence_free_slot(res);
     950                 :            :         }
     951                 :          0 : }
     952                 :            : 
     953                 :          0 : int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
     954                 :            : {
     955                 :          0 :         if (res->sr_slot == NULL)
     956                 :            :                 return 1;
     957                 :          0 :         if (!res->sr_slot->table->session)
     958                 :            :                 return nfs40_sequence_done(task, res);
     959                 :          0 :         return nfs41_sequence_done(task, res);
     960                 :            : }
     961                 :            : EXPORT_SYMBOL_GPL(nfs4_sequence_done);
     962                 :            : 
     963                 :          0 : static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
     964                 :            : {
     965                 :            :         struct nfs4_call_sync_data *data = calldata;
     966                 :            : 
     967                 :            :         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
     968                 :            : 
     969                 :          0 :         nfs4_setup_sequence(data->seq_server->nfs_client,
     970                 :            :                             data->seq_args, data->seq_res, task);
     971                 :          0 : }
     972                 :            : 
     973                 :          0 : static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
     974                 :            : {
     975                 :            :         struct nfs4_call_sync_data *data = calldata;
     976                 :            : 
     977                 :          0 :         nfs41_sequence_done(task, data->seq_res);
     978                 :          0 : }
     979                 :            : 
     980                 :            : static const struct rpc_call_ops nfs41_call_sync_ops = {
     981                 :            :         .rpc_call_prepare = nfs41_call_sync_prepare,
     982                 :            :         .rpc_call_done = nfs41_call_sync_done,
     983                 :            : };
     984                 :            : 
     985                 :            : #else   /* !CONFIG_NFS_V4_1 */
     986                 :            : 
     987                 :            : static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
     988                 :            : {
     989                 :            :         return nfs40_sequence_done(task, res);
     990                 :            : }
     991                 :            : 
     992                 :            : static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
     993                 :            : {
     994                 :            :         if (res->sr_slot != NULL)
     995                 :            :                 nfs40_sequence_free_slot(res);
     996                 :            : }
     997                 :            : 
     998                 :            : int nfs4_sequence_done(struct rpc_task *task,
     999                 :            :                        struct nfs4_sequence_res *res)
    1000                 :            : {
    1001                 :            :         return nfs40_sequence_done(task, res);
    1002                 :            : }
    1003                 :            : EXPORT_SYMBOL_GPL(nfs4_sequence_done);
    1004                 :            : 
    1005                 :            : #endif  /* !CONFIG_NFS_V4_1 */
    1006                 :            : 
    1007                 :            : static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
    1008                 :            : {
    1009                 :          0 :         res->sr_timestamp = jiffies;
    1010                 :          0 :         res->sr_status_flags = 0;
    1011                 :          0 :         res->sr_status = 1;
    1012                 :            : }
    1013                 :            : 
    1014                 :            : static
    1015                 :            : void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
    1016                 :            :                 struct nfs4_sequence_res *res,
    1017                 :            :                 struct nfs4_slot *slot)
    1018                 :            : {
    1019                 :          0 :         if (!slot)
    1020                 :            :                 return;
    1021                 :          0 :         slot->privileged = args->sa_privileged ? 1 : 0;
    1022                 :          0 :         args->sa_slot = slot;
    1023                 :            : 
    1024                 :          0 :         res->sr_slot = slot;
    1025                 :            : }
    1026                 :            : 
    1027                 :          0 : int nfs4_setup_sequence(struct nfs_client *client,
    1028                 :            :                         struct nfs4_sequence_args *args,
    1029                 :            :                         struct nfs4_sequence_res *res,
    1030                 :            :                         struct rpc_task *task)
    1031                 :            : {
    1032                 :            :         struct nfs4_session *session = nfs4_get_session(client);
    1033                 :          0 :         struct nfs4_slot_table *tbl  = client->cl_slot_tbl;
    1034                 :            :         struct nfs4_slot *slot;
    1035                 :            : 
    1036                 :            :         /* slot already allocated? */
    1037                 :          0 :         if (res->sr_slot != NULL)
    1038                 :            :                 goto out_start;
    1039                 :            : 
    1040                 :          0 :         if (session)
    1041                 :          0 :                 tbl = &session->fc_slot_table;
    1042                 :            : 
    1043                 :            :         spin_lock(&tbl->slot_tbl_lock);
    1044                 :            :         /* The state manager will wait until the slot table is empty */
    1045                 :          0 :         if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
    1046                 :            :                 goto out_sleep;
    1047                 :            : 
    1048                 :          0 :         slot = nfs4_alloc_slot(tbl);
    1049                 :          0 :         if (IS_ERR(slot)) {
    1050                 :          0 :                 if (slot == ERR_PTR(-ENOMEM))
    1051                 :            :                         goto out_sleep_timeout;
    1052                 :            :                 goto out_sleep;
    1053                 :            :         }
    1054                 :            :         spin_unlock(&tbl->slot_tbl_lock);
    1055                 :            : 
    1056                 :            :         nfs4_sequence_attach_slot(args, res, slot);
    1057                 :            : 
    1058                 :          0 :         trace_nfs4_setup_sequence(session, args);
    1059                 :            : out_start:
    1060                 :            :         nfs41_sequence_res_init(res);
    1061                 :          0 :         rpc_call_start(task);
    1062                 :          0 :         return 0;
    1063                 :            : out_sleep_timeout:
    1064                 :            :         /* Try again in 1/4 second */
    1065                 :          0 :         if (args->sa_privileged)
    1066                 :          0 :                 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
    1067                 :            :                                 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
    1068                 :            :         else
    1069                 :          0 :                 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
    1070                 :            :                                 NULL, jiffies + (HZ >> 2));
    1071                 :            :         spin_unlock(&tbl->slot_tbl_lock);
    1072                 :          0 :         return -EAGAIN;
    1073                 :            : out_sleep:
    1074                 :          0 :         if (args->sa_privileged)
    1075                 :          0 :                 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
    1076                 :            :                                 RPC_PRIORITY_PRIVILEGED);
    1077                 :            :         else
    1078                 :          0 :                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
    1079                 :            :         spin_unlock(&tbl->slot_tbl_lock);
    1080                 :          0 :         return -EAGAIN;
    1081                 :            : }
    1082                 :            : EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
    1083                 :            : 
    1084                 :          0 : static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
    1085                 :            : {
    1086                 :            :         struct nfs4_call_sync_data *data = calldata;
    1087                 :          0 :         nfs4_setup_sequence(data->seq_server->nfs_client,
    1088                 :            :                                 data->seq_args, data->seq_res, task);
    1089                 :          0 : }
    1090                 :            : 
    1091                 :          0 : static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
    1092                 :            : {
    1093                 :            :         struct nfs4_call_sync_data *data = calldata;
    1094                 :          0 :         nfs4_sequence_done(task, data->seq_res);
    1095                 :          0 : }
    1096                 :            : 
    1097                 :            : static const struct rpc_call_ops nfs40_call_sync_ops = {
    1098                 :            :         .rpc_call_prepare = nfs40_call_sync_prepare,
    1099                 :            :         .rpc_call_done = nfs40_call_sync_done,
    1100                 :            : };
    1101                 :            : 
    1102                 :          0 : static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
    1103                 :            : {
    1104                 :            :         int ret;
    1105                 :            :         struct rpc_task *task;
    1106                 :            : 
    1107                 :          0 :         task = rpc_run_task(task_setup);
    1108                 :          0 :         if (IS_ERR(task))
    1109                 :          0 :                 return PTR_ERR(task);
    1110                 :            : 
    1111                 :          0 :         ret = task->tk_status;
    1112                 :          0 :         rpc_put_task(task);
    1113                 :          0 :         return ret;
    1114                 :            : }
    1115                 :            : 
    1116                 :          0 : static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
    1117                 :            :                                    struct nfs_server *server,
    1118                 :            :                                    struct rpc_message *msg,
    1119                 :            :                                    struct nfs4_sequence_args *args,
    1120                 :            :                                    struct nfs4_sequence_res *res)
    1121                 :            : {
    1122                 :          0 :         struct nfs_client *clp = server->nfs_client;
    1123                 :          0 :         struct nfs4_call_sync_data data = {
    1124                 :            :                 .seq_server = server,
    1125                 :            :                 .seq_args = args,
    1126                 :            :                 .seq_res = res,
    1127                 :            :         };
    1128                 :          0 :         struct rpc_task_setup task_setup = {
    1129                 :            :                 .rpc_client = clnt,
    1130                 :            :                 .rpc_message = msg,
    1131                 :          0 :                 .callback_ops = clp->cl_mvops->call_sync_ops,
    1132                 :            :                 .callback_data = &data
    1133                 :            :         };
    1134                 :            : 
    1135                 :          0 :         return nfs4_call_sync_custom(&task_setup);
    1136                 :            : }
    1137                 :            : 
    1138                 :          0 : int nfs4_call_sync(struct rpc_clnt *clnt,
    1139                 :            :                    struct nfs_server *server,
    1140                 :            :                    struct rpc_message *msg,
    1141                 :            :                    struct nfs4_sequence_args *args,
    1142                 :            :                    struct nfs4_sequence_res *res,
    1143                 :            :                    int cache_reply)
    1144                 :            : {
    1145                 :            :         nfs4_init_sequence(args, res, cache_reply, 0);
    1146                 :          0 :         return nfs4_call_sync_sequence(clnt, server, msg, args, res);
    1147                 :            : }
    1148                 :            : 
    1149                 :            : static void
    1150                 :            : nfs4_inc_nlink_locked(struct inode *inode)
    1151                 :            : {
    1152                 :          0 :         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
    1153                 :          0 :         inc_nlink(inode);
    1154                 :            : }
    1155                 :            : 
    1156                 :            : static void
    1157                 :            : nfs4_dec_nlink_locked(struct inode *inode)
    1158                 :            : {
    1159                 :          0 :         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
    1160                 :          0 :         drop_nlink(inode);
    1161                 :            : }
    1162                 :            : 
    1163                 :            : static void
    1164                 :          0 : update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
    1165                 :            :                 unsigned long timestamp, unsigned long cache_validity)
    1166                 :            : {
    1167                 :            :         struct nfs_inode *nfsi = NFS_I(dir);
    1168                 :            : 
    1169                 :          0 :         nfsi->cache_validity |= NFS_INO_INVALID_CTIME
    1170                 :            :                 | NFS_INO_INVALID_MTIME
    1171                 :            :                 | NFS_INO_INVALID_DATA
    1172                 :            :                 | cache_validity;
    1173                 :          0 :         if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
    1174                 :          0 :                 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
    1175                 :          0 :                 nfsi->attrtimeo_timestamp = jiffies;
    1176                 :            :         } else {
    1177                 :          0 :                 nfs_force_lookup_revalidate(dir);
    1178                 :          0 :                 if (cinfo->before != inode_peek_iversion_raw(dir))
    1179                 :          0 :                         nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
    1180                 :            :                                 NFS_INO_INVALID_ACL;
    1181                 :            :         }
    1182                 :          0 :         inode_set_iversion_raw(dir, cinfo->after);
    1183                 :          0 :         nfsi->read_cache_jiffies = timestamp;
    1184                 :          0 :         nfsi->attr_gencount = nfs_inc_attr_generation_counter();
    1185                 :          0 :         nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
    1186                 :            :         nfs_fscache_invalidate(dir);
    1187                 :          0 : }
    1188                 :            : 
    1189                 :            : static void
    1190                 :          0 : update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
    1191                 :            :                 unsigned long timestamp, unsigned long cache_validity)
    1192                 :            : {
    1193                 :            :         spin_lock(&dir->i_lock);
    1194                 :          0 :         update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
    1195                 :            :         spin_unlock(&dir->i_lock);
    1196                 :          0 : }
    1197                 :            : 
    1198                 :            : struct nfs4_open_createattrs {
    1199                 :            :         struct nfs4_label *label;
    1200                 :            :         struct iattr *sattr;
    1201                 :            :         const __u32 verf[2];
    1202                 :            : };
    1203                 :            : 
    1204                 :            : static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
    1205                 :            :                 int err, struct nfs4_exception *exception)
    1206                 :            : {
    1207                 :          0 :         if (err != -EINVAL)
    1208                 :            :                 return false;
    1209                 :          0 :         if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
    1210                 :            :                 return false;
    1211                 :          0 :         server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
    1212                 :          0 :         exception->retry = 1;
    1213                 :            :         return true;
    1214                 :            : }
    1215                 :            : 
    1216                 :            : static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
    1217                 :            : {
    1218                 :          0 :          return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
    1219                 :            : }
    1220                 :            : 
    1221                 :            : static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
    1222                 :            : {
    1223                 :          0 :         fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
    1224                 :            : 
    1225                 :          0 :         return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
    1226                 :            : }
    1227                 :            : 
    1228                 :            : static u32
    1229                 :            : nfs4_map_atomic_open_share(struct nfs_server *server,
    1230                 :            :                 fmode_t fmode, int openflags)
    1231                 :            : {
    1232                 :            :         u32 res = 0;
    1233                 :            : 
    1234                 :          0 :         switch (fmode & (FMODE_READ | FMODE_WRITE)) {
    1235                 :            :         case FMODE_READ:
    1236                 :            :                 res = NFS4_SHARE_ACCESS_READ;
    1237                 :            :                 break;
    1238                 :            :         case FMODE_WRITE:
    1239                 :            :                 res = NFS4_SHARE_ACCESS_WRITE;
    1240                 :            :                 break;
    1241                 :            :         case FMODE_READ|FMODE_WRITE:
    1242                 :            :                 res = NFS4_SHARE_ACCESS_BOTH;
    1243                 :            :         }
    1244                 :          0 :         if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
    1245                 :            :                 goto out;
    1246                 :            :         /* Want no delegation if we're using O_DIRECT */
    1247                 :          0 :         if (openflags & O_DIRECT)
    1248                 :          0 :                 res |= NFS4_SHARE_WANT_NO_DELEG;
    1249                 :            : out:
    1250                 :            :         return res;
    1251                 :            : }
    1252                 :            : 
    1253                 :            : static enum open_claim_type4
    1254                 :            : nfs4_map_atomic_open_claim(struct nfs_server *server,
    1255                 :            :                 enum open_claim_type4 claim)
    1256                 :            : {
    1257                 :          0 :         if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
    1258                 :            :                 return claim;
    1259                 :          0 :         switch (claim) {
    1260                 :            :         default:
    1261                 :            :                 return claim;
    1262                 :            :         case NFS4_OPEN_CLAIM_FH:
    1263                 :            :                 return NFS4_OPEN_CLAIM_NULL;
    1264                 :            :         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
    1265                 :            :                 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
    1266                 :            :         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
    1267                 :            :                 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
    1268                 :            :         }
    1269                 :            : }
    1270                 :            : 
    1271                 :          0 : static void nfs4_init_opendata_res(struct nfs4_opendata *p)
    1272                 :            : {
    1273                 :          0 :         p->o_res.f_attr = &p->f_attr;
    1274                 :          0 :         p->o_res.f_label = p->f_label;
    1275                 :          0 :         p->o_res.seqid = p->o_arg.seqid;
    1276                 :          0 :         p->c_res.seqid = p->c_arg.seqid;
    1277                 :          0 :         p->o_res.server = p->o_arg.server;
    1278                 :          0 :         p->o_res.access_request = p->o_arg.access;
    1279                 :          0 :         nfs_fattr_init(&p->f_attr);
    1280                 :          0 :         nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
    1281                 :          0 : }
    1282                 :            : 
    1283                 :          0 : static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
    1284                 :            :                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
    1285                 :            :                 const struct nfs4_open_createattrs *c,
    1286                 :            :                 enum open_claim_type4 claim,
    1287                 :            :                 gfp_t gfp_mask)
    1288                 :            : {
    1289                 :          0 :         struct dentry *parent = dget_parent(dentry);
    1290                 :            :         struct inode *dir = d_inode(parent);
    1291                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    1292                 :            :         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
    1293                 :          0 :         struct nfs4_label *label = (c != NULL) ? c->label : NULL;
    1294                 :            :         struct nfs4_opendata *p;
    1295                 :            : 
    1296                 :          0 :         p = kzalloc(sizeof(*p), gfp_mask);
    1297                 :          0 :         if (p == NULL)
    1298                 :            :                 goto err;
    1299                 :            : 
    1300                 :          0 :         p->f_label = nfs4_label_alloc(server, gfp_mask);
    1301                 :          0 :         if (IS_ERR(p->f_label))
    1302                 :            :                 goto err_free_p;
    1303                 :            : 
    1304                 :          0 :         p->a_label = nfs4_label_alloc(server, gfp_mask);
    1305                 :          0 :         if (IS_ERR(p->a_label))
    1306                 :            :                 goto err_free_f;
    1307                 :            : 
    1308                 :          0 :         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
    1309                 :          0 :         p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
    1310                 :          0 :         if (IS_ERR(p->o_arg.seqid))
    1311                 :            :                 goto err_free_label;
    1312                 :          0 :         nfs_sb_active(dentry->d_sb);
    1313                 :          0 :         p->dentry = dget(dentry);
    1314                 :          0 :         p->dir = parent;
    1315                 :          0 :         p->owner = sp;
    1316                 :          0 :         atomic_inc(&sp->so_count);
    1317                 :          0 :         p->o_arg.open_flags = flags;
    1318                 :          0 :         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
    1319                 :          0 :         p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
    1320                 :          0 :         p->o_arg.share_access = nfs4_map_atomic_open_share(server,
    1321                 :            :                         fmode, flags);
    1322                 :          0 :         if (flags & O_CREAT) {
    1323                 :          0 :                 p->o_arg.umask = current_umask();
    1324                 :          0 :                 p->o_arg.label = nfs4_label_copy(p->a_label, label);
    1325                 :          0 :                 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
    1326                 :          0 :                         p->o_arg.u.attrs = &p->attrs;
    1327                 :          0 :                         memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
    1328                 :            : 
    1329                 :          0 :                         memcpy(p->o_arg.u.verifier.data, c->verf,
    1330                 :            :                                         sizeof(p->o_arg.u.verifier.data));
    1331                 :            :                 }
    1332                 :            :         }
    1333                 :            :         /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
    1334                 :            :          * will return permission denied for all bits until close */
    1335                 :          0 :         if (!(flags & O_EXCL)) {
    1336                 :            :                 /* ask server to check for all possible rights as results
    1337                 :            :                  * are cached */
    1338                 :          0 :                 switch (p->o_arg.claim) {
    1339                 :            :                 default:
    1340                 :            :                         break;
    1341                 :            :                 case NFS4_OPEN_CLAIM_NULL:
    1342                 :            :                 case NFS4_OPEN_CLAIM_FH:
    1343                 :          0 :                         p->o_arg.access = NFS4_ACCESS_READ |
    1344                 :            :                                 NFS4_ACCESS_MODIFY |
    1345                 :            :                                 NFS4_ACCESS_EXTEND |
    1346                 :            :                                 NFS4_ACCESS_EXECUTE;
    1347                 :            :                 }
    1348                 :            :         }
    1349                 :          0 :         p->o_arg.clientid = server->nfs_client->cl_clientid;
    1350                 :          0 :         p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
    1351                 :          0 :         p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
    1352                 :          0 :         p->o_arg.name = &dentry->d_name;
    1353                 :          0 :         p->o_arg.server = server;
    1354                 :          0 :         p->o_arg.bitmask = nfs4_bitmask(server, label);
    1355                 :          0 :         p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
    1356                 :          0 :         switch (p->o_arg.claim) {
    1357                 :            :         case NFS4_OPEN_CLAIM_NULL:
    1358                 :            :         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
    1359                 :            :         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
    1360                 :          0 :                 p->o_arg.fh = NFS_FH(dir);
    1361                 :          0 :                 break;
    1362                 :            :         case NFS4_OPEN_CLAIM_PREVIOUS:
    1363                 :            :         case NFS4_OPEN_CLAIM_FH:
    1364                 :            :         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
    1365                 :            :         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
    1366                 :          0 :                 p->o_arg.fh = NFS_FH(d_inode(dentry));
    1367                 :            :         }
    1368                 :          0 :         p->c_arg.fh = &p->o_res.fh;
    1369                 :          0 :         p->c_arg.stateid = &p->o_res.stateid;
    1370                 :          0 :         p->c_arg.seqid = p->o_arg.seqid;
    1371                 :          0 :         nfs4_init_opendata_res(p);
    1372                 :            :         kref_init(&p->kref);
    1373                 :          0 :         return p;
    1374                 :            : 
    1375                 :            : err_free_label:
    1376                 :          0 :         nfs4_label_free(p->a_label);
    1377                 :            : err_free_f:
    1378                 :          0 :         nfs4_label_free(p->f_label);
    1379                 :            : err_free_p:
    1380                 :          0 :         kfree(p);
    1381                 :            : err:
    1382                 :          0 :         dput(parent);
    1383                 :          0 :         return NULL;
    1384                 :            : }
    1385                 :            : 
    1386                 :          0 : static void nfs4_opendata_free(struct kref *kref)
    1387                 :            : {
    1388                 :            :         struct nfs4_opendata *p = container_of(kref,
    1389                 :            :                         struct nfs4_opendata, kref);
    1390                 :          0 :         struct super_block *sb = p->dentry->d_sb;
    1391                 :            : 
    1392                 :          0 :         nfs4_lgopen_release(p->lgp);
    1393                 :          0 :         nfs_free_seqid(p->o_arg.seqid);
    1394                 :          0 :         nfs4_sequence_free_slot(&p->o_res.seq_res);
    1395                 :          0 :         if (p->state != NULL)
    1396                 :          0 :                 nfs4_put_open_state(p->state);
    1397                 :          0 :         nfs4_put_state_owner(p->owner);
    1398                 :            : 
    1399                 :          0 :         nfs4_label_free(p->a_label);
    1400                 :          0 :         nfs4_label_free(p->f_label);
    1401                 :            : 
    1402                 :          0 :         dput(p->dir);
    1403                 :          0 :         dput(p->dentry);
    1404                 :          0 :         nfs_sb_deactive(sb);
    1405                 :          0 :         nfs_fattr_free_names(&p->f_attr);
    1406                 :          0 :         kfree(p->f_attr.mdsthreshold);
    1407                 :          0 :         kfree(p);
    1408                 :          0 : }
    1409                 :            : 
    1410                 :            : static void nfs4_opendata_put(struct nfs4_opendata *p)
    1411                 :            : {
    1412                 :          0 :         if (p != NULL)
    1413                 :          0 :                 kref_put(&p->kref, nfs4_opendata_free);
    1414                 :            : }
    1415                 :            : 
    1416                 :          0 : static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
    1417                 :            :                 fmode_t fmode)
    1418                 :            : {
    1419                 :          0 :         switch(fmode & (FMODE_READ|FMODE_WRITE)) {
    1420                 :            :         case FMODE_READ|FMODE_WRITE:
    1421                 :          0 :                 return state->n_rdwr != 0;
    1422                 :            :         case FMODE_WRITE:
    1423                 :          0 :                 return state->n_wronly != 0;
    1424                 :            :         case FMODE_READ:
    1425                 :          0 :                 return state->n_rdonly != 0;
    1426                 :            :         }
    1427                 :          0 :         WARN_ON_ONCE(1);
    1428                 :            :         return false;
    1429                 :            : }
    1430                 :            : 
    1431                 :          0 : static int can_open_cached(struct nfs4_state *state, fmode_t mode,
    1432                 :            :                 int open_mode, enum open_claim_type4 claim)
    1433                 :            : {
    1434                 :            :         int ret = 0;
    1435                 :            : 
    1436                 :          0 :         if (open_mode & (O_EXCL|O_TRUNC))
    1437                 :            :                 goto out;
    1438                 :          0 :         switch (claim) {
    1439                 :            :         case NFS4_OPEN_CLAIM_NULL:
    1440                 :            :         case NFS4_OPEN_CLAIM_FH:
    1441                 :            :                 goto out;
    1442                 :            :         default:
    1443                 :            :                 break;
    1444                 :            :         }
    1445                 :          0 :         switch (mode & (FMODE_READ|FMODE_WRITE)) {
    1446                 :            :                 case FMODE_READ:
    1447                 :            :                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
    1448                 :          0 :                                 && state->n_rdonly != 0;
    1449                 :          0 :                         break;
    1450                 :            :                 case FMODE_WRITE:
    1451                 :            :                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
    1452                 :          0 :                                 && state->n_wronly != 0;
    1453                 :          0 :                         break;
    1454                 :            :                 case FMODE_READ|FMODE_WRITE:
    1455                 :            :                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
    1456                 :          0 :                                 && state->n_rdwr != 0;
    1457                 :            :         }
    1458                 :            : out:
    1459                 :          0 :         return ret;
    1460                 :            : }
    1461                 :            : 
    1462                 :          0 : static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
    1463                 :            :                 enum open_claim_type4 claim)
    1464                 :            : {
    1465                 :          0 :         if (delegation == NULL)
    1466                 :            :                 return 0;
    1467                 :          0 :         if ((delegation->type & fmode) != fmode)
    1468                 :            :                 return 0;
    1469                 :          0 :         switch (claim) {
    1470                 :            :         case NFS4_OPEN_CLAIM_NULL:
    1471                 :            :         case NFS4_OPEN_CLAIM_FH:
    1472                 :            :                 break;
    1473                 :            :         case NFS4_OPEN_CLAIM_PREVIOUS:
    1474                 :          0 :                 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
    1475                 :            :                         break;
    1476                 :            :                 /* Fall through */
    1477                 :            :         default:
    1478                 :            :                 return 0;
    1479                 :            :         }
    1480                 :          0 :         nfs_mark_delegation_referenced(delegation);
    1481                 :          0 :         return 1;
    1482                 :            : }
    1483                 :            : 
    1484                 :          0 : static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
    1485                 :            : {
    1486                 :          0 :         switch (fmode) {
    1487                 :            :                 case FMODE_WRITE:
    1488                 :          0 :                         state->n_wronly++;
    1489                 :          0 :                         break;
    1490                 :            :                 case FMODE_READ:
    1491                 :          0 :                         state->n_rdonly++;
    1492                 :          0 :                         break;
    1493                 :            :                 case FMODE_READ|FMODE_WRITE:
    1494                 :          0 :                         state->n_rdwr++;
    1495                 :            :         }
    1496                 :          0 :         nfs4_state_set_mode_locked(state, state->state | fmode);
    1497                 :          0 : }
    1498                 :            : 
    1499                 :            : #ifdef CONFIG_NFS_V4_1
    1500                 :          0 : static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
    1501                 :            : {
    1502                 :          0 :         if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
    1503                 :            :                 return true;
    1504                 :          0 :         if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
    1505                 :            :                 return true;
    1506                 :          0 :         if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
    1507                 :            :                 return true;
    1508                 :            :         return false;
    1509                 :            : }
    1510                 :            : #endif /* CONFIG_NFS_V4_1 */
    1511                 :            : 
    1512                 :          0 : static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
    1513                 :            : {
    1514                 :          0 :         if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
    1515                 :          0 :                 wake_up_all(&state->waitq);
    1516                 :          0 : }
    1517                 :            : 
    1518                 :          0 : static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
    1519                 :            :                 const nfs4_stateid *stateid)
    1520                 :            : {
    1521                 :          0 :         u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
    1522                 :          0 :         u32 stateid_seqid = be32_to_cpu(stateid->seqid);
    1523                 :            : 
    1524                 :          0 :         if (stateid_seqid == state_seqid + 1U ||
    1525                 :          0 :             (stateid_seqid == 1U && state_seqid == 0xffffffffU))
    1526                 :          0 :                 nfs_state_log_update_open_stateid(state);
    1527                 :            :         else
    1528                 :          0 :                 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
    1529                 :          0 : }
    1530                 :            : 
    1531                 :          0 : static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
    1532                 :            : {
    1533                 :          0 :         struct nfs_client *clp = state->owner->so_server->nfs_client;
    1534                 :            :         bool need_recover = false;
    1535                 :            : 
    1536                 :          0 :         if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
    1537                 :            :                 need_recover = true;
    1538                 :          0 :         if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
    1539                 :            :                 need_recover = true;
    1540                 :          0 :         if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
    1541                 :            :                 need_recover = true;
    1542                 :          0 :         if (need_recover)
    1543                 :          0 :                 nfs4_state_mark_reclaim_nograce(clp, state);
    1544                 :          0 : }
    1545                 :            : 
    1546                 :            : /*
    1547                 :            :  * Check for whether or not the caller may update the open stateid
    1548                 :            :  * to the value passed in by stateid.
    1549                 :            :  *
    1550                 :            :  * Note: This function relies heavily on the server implementing
    1551                 :            :  * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
    1552                 :            :  * correctly.
    1553                 :            :  * i.e. The stateid seqids have to be initialised to 1, and
    1554                 :            :  * are then incremented on every state transition.
    1555                 :            :  */
    1556                 :          0 : static bool nfs_need_update_open_stateid(struct nfs4_state *state,
    1557                 :            :                 const nfs4_stateid *stateid)
    1558                 :            : {
    1559                 :          0 :         if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
    1560                 :            :             !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
    1561                 :          0 :                 if (stateid->seqid == cpu_to_be32(1))
    1562                 :          0 :                         nfs_state_log_update_open_stateid(state);
    1563                 :            :                 else
    1564                 :          0 :                         set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
    1565                 :            :                 return true;
    1566                 :            :         }
    1567                 :            : 
    1568                 :          0 :         if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
    1569                 :          0 :                 nfs_state_log_out_of_order_open_stateid(state, stateid);
    1570                 :          0 :                 return true;
    1571                 :            :         }
    1572                 :            :         return false;
    1573                 :            : }
    1574                 :            : 
    1575                 :          0 : static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
    1576                 :            : {
    1577                 :          0 :         if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
    1578                 :          0 :                 return;
    1579                 :          0 :         if (state->n_wronly)
    1580                 :          0 :                 set_bit(NFS_O_WRONLY_STATE, &state->flags);
    1581                 :          0 :         if (state->n_rdonly)
    1582                 :          0 :                 set_bit(NFS_O_RDONLY_STATE, &state->flags);
    1583                 :          0 :         if (state->n_rdwr)
    1584                 :          0 :                 set_bit(NFS_O_RDWR_STATE, &state->flags);
    1585                 :          0 :         set_bit(NFS_OPEN_STATE, &state->flags);
    1586                 :            : }
    1587                 :            : 
    1588                 :          0 : static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
    1589                 :            :                 nfs4_stateid *stateid, fmode_t fmode)
    1590                 :            : {
    1591                 :          0 :         clear_bit(NFS_O_RDWR_STATE, &state->flags);
    1592                 :          0 :         switch (fmode & (FMODE_READ|FMODE_WRITE)) {
    1593                 :            :         case FMODE_WRITE:
    1594                 :          0 :                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
    1595                 :          0 :                 break;
    1596                 :            :         case FMODE_READ:
    1597                 :          0 :                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
    1598                 :          0 :                 break;
    1599                 :            :         case 0:
    1600                 :          0 :                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
    1601                 :          0 :                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
    1602                 :          0 :                 clear_bit(NFS_OPEN_STATE, &state->flags);
    1603                 :            :         }
    1604                 :          0 :         if (stateid == NULL)
    1605                 :          0 :                 return;
    1606                 :            :         /* Handle OPEN+OPEN_DOWNGRADE races */
    1607                 :          0 :         if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
    1608                 :            :             !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
    1609                 :          0 :                 nfs_resync_open_stateid_locked(state);
    1610                 :          0 :                 goto out;
    1611                 :            :         }
    1612                 :          0 :         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
    1613                 :            :                 nfs4_stateid_copy(&state->stateid, stateid);
    1614                 :            :         nfs4_stateid_copy(&state->open_stateid, stateid);
    1615                 :          0 :         trace_nfs4_open_stateid_update(state->inode, stateid, 0);
    1616                 :            : out:
    1617                 :          0 :         nfs_state_log_update_open_stateid(state);
    1618                 :            : }
    1619                 :            : 
    1620                 :          0 : static void nfs_clear_open_stateid(struct nfs4_state *state,
    1621                 :            :         nfs4_stateid *arg_stateid,
    1622                 :            :         nfs4_stateid *stateid, fmode_t fmode)
    1623                 :            : {
    1624                 :            :         write_seqlock(&state->seqlock);
    1625                 :            :         /* Ignore, if the CLOSE argment doesn't match the current stateid */
    1626                 :          0 :         if (nfs4_state_match_open_stateid_other(state, arg_stateid))
    1627                 :          0 :                 nfs_clear_open_stateid_locked(state, stateid, fmode);
    1628                 :            :         write_sequnlock(&state->seqlock);
    1629                 :          0 :         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
    1630                 :          0 :                 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
    1631                 :          0 : }
    1632                 :            : 
    1633                 :          0 : static void nfs_set_open_stateid_locked(struct nfs4_state *state,
    1634                 :            :                 const nfs4_stateid *stateid, nfs4_stateid *freeme)
    1635                 :            :         __must_hold(&state->owner->so_lock)
    1636                 :            :         __must_hold(&state->seqlock)
    1637                 :            :         __must_hold(RCU)
    1638                 :            : 
    1639                 :            : {
    1640                 :          0 :         DEFINE_WAIT(wait);
    1641                 :            :         int status = 0;
    1642                 :            :         for (;;) {
    1643                 :            : 
    1644                 :          0 :                 if (!nfs_need_update_open_stateid(state, stateid))
    1645                 :          0 :                         return;
    1646                 :          0 :                 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
    1647                 :            :                         break;
    1648                 :          0 :                 if (status)
    1649                 :            :                         break;
    1650                 :            :                 /* Rely on seqids for serialisation with NFSv4.0 */
    1651                 :          0 :                 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
    1652                 :            :                         break;
    1653                 :            : 
    1654                 :          0 :                 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
    1655                 :            :                 /*
    1656                 :            :                  * Ensure we process the state changes in the same order
    1657                 :            :                  * in which the server processed them by delaying the
    1658                 :            :                  * update of the stateid until we are in sequence.
    1659                 :            :                  */
    1660                 :            :                 write_sequnlock(&state->seqlock);
    1661                 :          0 :                 spin_unlock(&state->owner->so_lock);
    1662                 :            :                 rcu_read_unlock();
    1663                 :          0 :                 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
    1664                 :          0 :                 if (!signal_pending(current)) {
    1665                 :          0 :                         if (schedule_timeout(5*HZ) == 0)
    1666                 :            :                                 status = -EAGAIN;
    1667                 :            :                         else
    1668                 :            :                                 status = 0;
    1669                 :            :                 } else
    1670                 :            :                         status = -EINTR;
    1671                 :          0 :                 finish_wait(&state->waitq, &wait);
    1672                 :            :                 rcu_read_lock();
    1673                 :          0 :                 spin_lock(&state->owner->so_lock);
    1674                 :            :                 write_seqlock(&state->seqlock);
    1675                 :            :         }
    1676                 :            : 
    1677                 :          0 :         if (test_bit(NFS_OPEN_STATE, &state->flags) &&
    1678                 :            :             !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
    1679                 :            :                 nfs4_stateid_copy(freeme, &state->open_stateid);
    1680                 :          0 :                 nfs_test_and_clear_all_open_stateid(state);
    1681                 :            :         }
    1682                 :            : 
    1683                 :          0 :         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
    1684                 :            :                 nfs4_stateid_copy(&state->stateid, stateid);
    1685                 :            :         nfs4_stateid_copy(&state->open_stateid, stateid);
    1686                 :          0 :         trace_nfs4_open_stateid_update(state->inode, stateid, status);
    1687                 :          0 :         nfs_state_log_update_open_stateid(state);
    1688                 :            : }
    1689                 :            : 
    1690                 :          0 : static void nfs_state_set_open_stateid(struct nfs4_state *state,
    1691                 :            :                 const nfs4_stateid *open_stateid,
    1692                 :            :                 fmode_t fmode,
    1693                 :            :                 nfs4_stateid *freeme)
    1694                 :            : {
    1695                 :            :         /*
    1696                 :            :          * Protect the call to nfs4_state_set_mode_locked and
    1697                 :            :          * serialise the stateid update
    1698                 :            :          */
    1699                 :            :         write_seqlock(&state->seqlock);
    1700                 :          0 :         nfs_set_open_stateid_locked(state, open_stateid, freeme);
    1701                 :          0 :         switch (fmode) {
    1702                 :            :         case FMODE_READ:
    1703                 :          0 :                 set_bit(NFS_O_RDONLY_STATE, &state->flags);
    1704                 :          0 :                 break;
    1705                 :            :         case FMODE_WRITE:
    1706                 :          0 :                 set_bit(NFS_O_WRONLY_STATE, &state->flags);
    1707                 :          0 :                 break;
    1708                 :            :         case FMODE_READ|FMODE_WRITE:
    1709                 :          0 :                 set_bit(NFS_O_RDWR_STATE, &state->flags);
    1710                 :            :         }
    1711                 :          0 :         set_bit(NFS_OPEN_STATE, &state->flags);
    1712                 :            :         write_sequnlock(&state->seqlock);
    1713                 :          0 : }
    1714                 :            : 
    1715                 :          0 : static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
    1716                 :            : {
    1717                 :          0 :         clear_bit(NFS_O_RDWR_STATE, &state->flags);
    1718                 :          0 :         clear_bit(NFS_O_WRONLY_STATE, &state->flags);
    1719                 :          0 :         clear_bit(NFS_O_RDONLY_STATE, &state->flags);
    1720                 :          0 :         clear_bit(NFS_OPEN_STATE, &state->flags);
    1721                 :          0 : }
    1722                 :            : 
    1723                 :          0 : static void nfs_state_set_delegation(struct nfs4_state *state,
    1724                 :            :                 const nfs4_stateid *deleg_stateid,
    1725                 :            :                 fmode_t fmode)
    1726                 :            : {
    1727                 :            :         /*
    1728                 :            :          * Protect the call to nfs4_state_set_mode_locked and
    1729                 :            :          * serialise the stateid update
    1730                 :            :          */
    1731                 :            :         write_seqlock(&state->seqlock);
    1732                 :            :         nfs4_stateid_copy(&state->stateid, deleg_stateid);
    1733                 :          0 :         set_bit(NFS_DELEGATED_STATE, &state->flags);
    1734                 :            :         write_sequnlock(&state->seqlock);
    1735                 :          0 : }
    1736                 :            : 
    1737                 :          0 : static void nfs_state_clear_delegation(struct nfs4_state *state)
    1738                 :            : {
    1739                 :            :         write_seqlock(&state->seqlock);
    1740                 :            :         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
    1741                 :          0 :         clear_bit(NFS_DELEGATED_STATE, &state->flags);
    1742                 :            :         write_sequnlock(&state->seqlock);
    1743                 :          0 : }
    1744                 :            : 
    1745                 :          0 : static int update_open_stateid(struct nfs4_state *state,
    1746                 :            :                 const nfs4_stateid *open_stateid,
    1747                 :            :                 const nfs4_stateid *delegation,
    1748                 :            :                 fmode_t fmode)
    1749                 :            : {
    1750                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    1751                 :          0 :         struct nfs_client *clp = server->nfs_client;
    1752                 :            :         struct nfs_inode *nfsi = NFS_I(state->inode);
    1753                 :            :         struct nfs_delegation *deleg_cur;
    1754                 :          0 :         nfs4_stateid freeme = { };
    1755                 :            :         int ret = 0;
    1756                 :            : 
    1757                 :          0 :         fmode &= (FMODE_READ|FMODE_WRITE);
    1758                 :            : 
    1759                 :            :         rcu_read_lock();
    1760                 :          0 :         spin_lock(&state->owner->so_lock);
    1761                 :          0 :         if (open_stateid != NULL) {
    1762                 :          0 :                 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
    1763                 :            :                 ret = 1;
    1764                 :            :         }
    1765                 :            : 
    1766                 :          0 :         deleg_cur = rcu_dereference(nfsi->delegation);
    1767                 :          0 :         if (deleg_cur == NULL)
    1768                 :            :                 goto no_delegation;
    1769                 :            : 
    1770                 :            :         spin_lock(&deleg_cur->lock);
    1771                 :          0 :         if (rcu_dereference(nfsi->delegation) != deleg_cur ||
    1772                 :          0 :            test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
    1773                 :          0 :             (deleg_cur->type & fmode) != fmode)
    1774                 :            :                 goto no_delegation_unlock;
    1775                 :            : 
    1776                 :          0 :         if (delegation == NULL)
    1777                 :            :                 delegation = &deleg_cur->stateid;
    1778                 :          0 :         else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
    1779                 :            :                 goto no_delegation_unlock;
    1780                 :            : 
    1781                 :          0 :         nfs_mark_delegation_referenced(deleg_cur);
    1782                 :          0 :         nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
    1783                 :            :         ret = 1;
    1784                 :            : no_delegation_unlock:
    1785                 :            :         spin_unlock(&deleg_cur->lock);
    1786                 :            : no_delegation:
    1787                 :          0 :         if (ret)
    1788                 :          0 :                 update_open_stateflags(state, fmode);
    1789                 :          0 :         spin_unlock(&state->owner->so_lock);
    1790                 :            :         rcu_read_unlock();
    1791                 :            : 
    1792                 :          0 :         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
    1793                 :          0 :                 nfs4_schedule_state_manager(clp);
    1794                 :          0 :         if (freeme.type != 0)
    1795                 :          0 :                 nfs4_test_and_free_stateid(server, &freeme,
    1796                 :          0 :                                 state->owner->so_cred);
    1797                 :            : 
    1798                 :          0 :         return ret;
    1799                 :            : }
    1800                 :            : 
    1801                 :          0 : static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
    1802                 :            :                 const nfs4_stateid *stateid)
    1803                 :            : {
    1804                 :          0 :         struct nfs4_state *state = lsp->ls_state;
    1805                 :            :         bool ret = false;
    1806                 :            : 
    1807                 :            :         spin_lock(&state->state_lock);
    1808                 :          0 :         if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
    1809                 :            :                 goto out_noupdate;
    1810                 :          0 :         if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
    1811                 :            :                 goto out_noupdate;
    1812                 :            :         nfs4_stateid_copy(&lsp->ls_stateid, stateid);
    1813                 :            :         ret = true;
    1814                 :            : out_noupdate:
    1815                 :            :         spin_unlock(&state->state_lock);
    1816                 :          0 :         return ret;
    1817                 :            : }
    1818                 :            : 
    1819                 :          0 : static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
    1820                 :            : {
    1821                 :            :         struct nfs_delegation *delegation;
    1822                 :            : 
    1823                 :          0 :         fmode &= FMODE_READ|FMODE_WRITE;
    1824                 :            :         rcu_read_lock();
    1825                 :          0 :         delegation = rcu_dereference(NFS_I(inode)->delegation);
    1826                 :          0 :         if (delegation == NULL || (delegation->type & fmode) == fmode) {
    1827                 :            :                 rcu_read_unlock();
    1828                 :          0 :                 return;
    1829                 :            :         }
    1830                 :            :         rcu_read_unlock();
    1831                 :          0 :         nfs4_inode_return_delegation(inode);
    1832                 :            : }
    1833                 :            : 
    1834                 :          0 : static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
    1835                 :            : {
    1836                 :          0 :         struct nfs4_state *state = opendata->state;
    1837                 :            :         struct nfs_delegation *delegation;
    1838                 :          0 :         int open_mode = opendata->o_arg.open_flags;
    1839                 :          0 :         fmode_t fmode = opendata->o_arg.fmode;
    1840                 :          0 :         enum open_claim_type4 claim = opendata->o_arg.claim;
    1841                 :            :         nfs4_stateid stateid;
    1842                 :            :         int ret = -EAGAIN;
    1843                 :            : 
    1844                 :            :         for (;;) {
    1845                 :          0 :                 spin_lock(&state->owner->so_lock);
    1846                 :          0 :                 if (can_open_cached(state, fmode, open_mode, claim)) {
    1847                 :          0 :                         update_open_stateflags(state, fmode);
    1848                 :          0 :                         spin_unlock(&state->owner->so_lock);
    1849                 :            :                         goto out_return_state;
    1850                 :            :                 }
    1851                 :          0 :                 spin_unlock(&state->owner->so_lock);
    1852                 :            :                 rcu_read_lock();
    1853                 :          0 :                 delegation = nfs4_get_valid_delegation(state->inode);
    1854                 :          0 :                 if (!can_open_delegated(delegation, fmode, claim)) {
    1855                 :            :                         rcu_read_unlock();
    1856                 :            :                         break;
    1857                 :            :                 }
    1858                 :            :                 /* Save the delegation */
    1859                 :            :                 nfs4_stateid_copy(&stateid, &delegation->stateid);
    1860                 :            :                 rcu_read_unlock();
    1861                 :          0 :                 nfs_release_seqid(opendata->o_arg.seqid);
    1862                 :          0 :                 if (!opendata->is_recover) {
    1863                 :          0 :                         ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
    1864                 :          0 :                         if (ret != 0)
    1865                 :            :                                 goto out;
    1866                 :            :                 }
    1867                 :            :                 ret = -EAGAIN;
    1868                 :            : 
    1869                 :            :                 /* Try to update the stateid using the delegation */
    1870                 :          0 :                 if (update_open_stateid(state, NULL, &stateid, fmode))
    1871                 :            :                         goto out_return_state;
    1872                 :            :         }
    1873                 :            : out:
    1874                 :          0 :         return ERR_PTR(ret);
    1875                 :            : out_return_state:
    1876                 :          0 :         refcount_inc(&state->count);
    1877                 :          0 :         return state;
    1878                 :            : }
    1879                 :            : 
    1880                 :            : static void
    1881                 :          0 : nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
    1882                 :            : {
    1883                 :          0 :         struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
    1884                 :            :         struct nfs_delegation *delegation;
    1885                 :            :         int delegation_flags = 0;
    1886                 :            : 
    1887                 :            :         rcu_read_lock();
    1888                 :          0 :         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
    1889                 :          0 :         if (delegation)
    1890                 :          0 :                 delegation_flags = delegation->flags;
    1891                 :            :         rcu_read_unlock();
    1892                 :          0 :         switch (data->o_arg.claim) {
    1893                 :            :         default:
    1894                 :            :                 break;
    1895                 :            :         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
    1896                 :            :         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
    1897                 :          0 :                 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
    1898                 :            :                                    "returning a delegation for "
    1899                 :            :                                    "OPEN(CLAIM_DELEGATE_CUR)\n",
    1900                 :            :                                    clp->cl_hostname);
    1901                 :          0 :                 return;
    1902                 :            :         }
    1903                 :          0 :         if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
    1904                 :          0 :                 nfs_inode_set_delegation(state->inode,
    1905                 :          0 :                                 data->owner->so_cred,
    1906                 :            :                                 data->o_res.delegation_type,
    1907                 :          0 :                                 &data->o_res.delegation,
    1908                 :            :                                 data->o_res.pagemod_limit);
    1909                 :            :         else
    1910                 :          0 :                 nfs_inode_reclaim_delegation(state->inode,
    1911                 :          0 :                                 data->owner->so_cred,
    1912                 :            :                                 data->o_res.delegation_type,
    1913                 :          0 :                                 &data->o_res.delegation,
    1914                 :            :                                 data->o_res.pagemod_limit);
    1915                 :            : 
    1916                 :          0 :         if (data->o_res.do_recall)
    1917                 :          0 :                 nfs_async_inode_return_delegation(state->inode,
    1918                 :          0 :                                                   &data->o_res.delegation);
    1919                 :            : }
    1920                 :            : 
    1921                 :            : /*
    1922                 :            :  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
    1923                 :            :  * and update the nfs4_state.
    1924                 :            :  */
    1925                 :            : static struct nfs4_state *
    1926                 :          0 : _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
    1927                 :            : {
    1928                 :          0 :         struct inode *inode = data->state->inode;
    1929                 :            :         struct nfs4_state *state = data->state;
    1930                 :            :         int ret;
    1931                 :            : 
    1932                 :          0 :         if (!data->rpc_done) {
    1933                 :          0 :                 if (data->rpc_status)
    1934                 :          0 :                         return ERR_PTR(data->rpc_status);
    1935                 :            :                 /* cached opens have already been processed */
    1936                 :            :                 goto update;
    1937                 :            :         }
    1938                 :            : 
    1939                 :          0 :         ret = nfs_refresh_inode(inode, &data->f_attr);
    1940                 :          0 :         if (ret)
    1941                 :          0 :                 return ERR_PTR(ret);
    1942                 :            : 
    1943                 :          0 :         if (data->o_res.delegation_type != 0)
    1944                 :          0 :                 nfs4_opendata_check_deleg(data, state);
    1945                 :            : update:
    1946                 :          0 :         if (!update_open_stateid(state, &data->o_res.stateid,
    1947                 :            :                                 NULL, data->o_arg.fmode))
    1948                 :            :                 return ERR_PTR(-EAGAIN);
    1949                 :          0 :         refcount_inc(&state->count);
    1950                 :            : 
    1951                 :          0 :         return state;
    1952                 :            : }
    1953                 :            : 
    1954                 :            : static struct inode *
    1955                 :          0 : nfs4_opendata_get_inode(struct nfs4_opendata *data)
    1956                 :            : {
    1957                 :            :         struct inode *inode;
    1958                 :            : 
    1959                 :          0 :         switch (data->o_arg.claim) {
    1960                 :            :         case NFS4_OPEN_CLAIM_NULL:
    1961                 :            :         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
    1962                 :            :         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
    1963                 :          0 :                 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
    1964                 :            :                         return ERR_PTR(-EAGAIN);
    1965                 :          0 :                 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
    1966                 :            :                                 &data->f_attr, data->f_label);
    1967                 :          0 :                 break;
    1968                 :            :         default:
    1969                 :          0 :                 inode = d_inode(data->dentry);
    1970                 :          0 :                 ihold(inode);
    1971                 :          0 :                 nfs_refresh_inode(inode, &data->f_attr);
    1972                 :            :         }
    1973                 :          0 :         return inode;
    1974                 :            : }
    1975                 :            : 
    1976                 :            : static struct nfs4_state *
    1977                 :          0 : nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
    1978                 :            : {
    1979                 :            :         struct nfs4_state *state;
    1980                 :            :         struct inode *inode;
    1981                 :            : 
    1982                 :          0 :         inode = nfs4_opendata_get_inode(data);
    1983                 :          0 :         if (IS_ERR(inode))
    1984                 :            :                 return ERR_CAST(inode);
    1985                 :          0 :         if (data->state != NULL && data->state->inode == inode) {
    1986                 :            :                 state = data->state;
    1987                 :          0 :                 refcount_inc(&state->count);
    1988                 :            :         } else
    1989                 :          0 :                 state = nfs4_get_open_state(inode, data->owner);
    1990                 :          0 :         iput(inode);
    1991                 :          0 :         if (state == NULL)
    1992                 :            :                 state = ERR_PTR(-ENOMEM);
    1993                 :          0 :         return state;
    1994                 :            : }
    1995                 :            : 
    1996                 :            : static struct nfs4_state *
    1997                 :          0 : _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
    1998                 :            : {
    1999                 :            :         struct nfs4_state *state;
    2000                 :            : 
    2001                 :          0 :         if (!data->rpc_done) {
    2002                 :          0 :                 state = nfs4_try_open_cached(data);
    2003                 :          0 :                 trace_nfs4_cached_open(data->state);
    2004                 :          0 :                 goto out;
    2005                 :            :         }
    2006                 :            : 
    2007                 :          0 :         state = nfs4_opendata_find_nfs4_state(data);
    2008                 :          0 :         if (IS_ERR(state))
    2009                 :            :                 goto out;
    2010                 :            : 
    2011                 :          0 :         if (data->o_res.delegation_type != 0)
    2012                 :          0 :                 nfs4_opendata_check_deleg(data, state);
    2013                 :          0 :         if (!update_open_stateid(state, &data->o_res.stateid,
    2014                 :            :                                 NULL, data->o_arg.fmode)) {
    2015                 :          0 :                 nfs4_put_open_state(state);
    2016                 :            :                 state = ERR_PTR(-EAGAIN);
    2017                 :            :         }
    2018                 :            : out:
    2019                 :          0 :         nfs_release_seqid(data->o_arg.seqid);
    2020                 :          0 :         return state;
    2021                 :            : }
    2022                 :            : 
    2023                 :            : static struct nfs4_state *
    2024                 :          0 : nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
    2025                 :            : {
    2026                 :            :         struct nfs4_state *ret;
    2027                 :            : 
    2028                 :          0 :         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
    2029                 :          0 :                 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
    2030                 :            :         else
    2031                 :          0 :                 ret = _nfs4_opendata_to_nfs4_state(data);
    2032                 :          0 :         nfs4_sequence_free_slot(&data->o_res.seq_res);
    2033                 :          0 :         return ret;
    2034                 :            : }
    2035                 :            : 
    2036                 :            : static struct nfs_open_context *
    2037                 :          0 : nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
    2038                 :            : {
    2039                 :          0 :         struct nfs_inode *nfsi = NFS_I(state->inode);
    2040                 :            :         struct nfs_open_context *ctx;
    2041                 :            : 
    2042                 :            :         rcu_read_lock();
    2043                 :          0 :         list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
    2044                 :          0 :                 if (ctx->state != state)
    2045                 :          0 :                         continue;
    2046                 :          0 :                 if ((ctx->mode & mode) != mode)
    2047                 :          0 :                         continue;
    2048                 :          0 :                 if (!get_nfs_open_context(ctx))
    2049                 :          0 :                         continue;
    2050                 :            :                 rcu_read_unlock();
    2051                 :          0 :                 return ctx;
    2052                 :            :         }
    2053                 :            :         rcu_read_unlock();
    2054                 :          0 :         return ERR_PTR(-ENOENT);
    2055                 :            : }
    2056                 :            : 
    2057                 :            : static struct nfs_open_context *
    2058                 :          0 : nfs4_state_find_open_context(struct nfs4_state *state)
    2059                 :            : {
    2060                 :            :         struct nfs_open_context *ctx;
    2061                 :            : 
    2062                 :          0 :         ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
    2063                 :          0 :         if (!IS_ERR(ctx))
    2064                 :            :                 return ctx;
    2065                 :          0 :         ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
    2066                 :          0 :         if (!IS_ERR(ctx))
    2067                 :            :                 return ctx;
    2068                 :          0 :         return nfs4_state_find_open_context_mode(state, FMODE_READ);
    2069                 :            : }
    2070                 :            : 
    2071                 :          0 : static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
    2072                 :            :                 struct nfs4_state *state, enum open_claim_type4 claim)
    2073                 :            : {
    2074                 :            :         struct nfs4_opendata *opendata;
    2075                 :            : 
    2076                 :          0 :         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
    2077                 :            :                         NULL, claim, GFP_NOFS);
    2078                 :          0 :         if (opendata == NULL)
    2079                 :            :                 return ERR_PTR(-ENOMEM);
    2080                 :          0 :         opendata->state = state;
    2081                 :          0 :         refcount_inc(&state->count);
    2082                 :          0 :         return opendata;
    2083                 :            : }
    2084                 :            : 
    2085                 :          0 : static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
    2086                 :            :                 fmode_t fmode)
    2087                 :            : {
    2088                 :            :         struct nfs4_state *newstate;
    2089                 :            :         int ret;
    2090                 :            : 
    2091                 :          0 :         if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
    2092                 :            :                 return 0;
    2093                 :          0 :         opendata->o_arg.open_flags = 0;
    2094                 :          0 :         opendata->o_arg.fmode = fmode;
    2095                 :          0 :         opendata->o_arg.share_access = nfs4_map_atomic_open_share(
    2096                 :            :                         NFS_SB(opendata->dentry->d_sb),
    2097                 :            :                         fmode, 0);
    2098                 :          0 :         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
    2099                 :          0 :         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
    2100                 :          0 :         nfs4_init_opendata_res(opendata);
    2101                 :          0 :         ret = _nfs4_recover_proc_open(opendata);
    2102                 :          0 :         if (ret != 0)
    2103                 :            :                 return ret; 
    2104                 :          0 :         newstate = nfs4_opendata_to_nfs4_state(opendata);
    2105                 :          0 :         if (IS_ERR(newstate))
    2106                 :          0 :                 return PTR_ERR(newstate);
    2107                 :          0 :         if (newstate != opendata->state)
    2108                 :            :                 ret = -ESTALE;
    2109                 :          0 :         nfs4_close_state(newstate, fmode);
    2110                 :          0 :         return ret;
    2111                 :            : }
    2112                 :            : 
    2113                 :          0 : static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
    2114                 :            : {
    2115                 :            :         int ret;
    2116                 :            : 
    2117                 :            :         /* memory barrier prior to reading state->n_* */
    2118                 :          0 :         smp_rmb();
    2119                 :          0 :         ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
    2120                 :          0 :         if (ret != 0)
    2121                 :            :                 return ret;
    2122                 :          0 :         ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
    2123                 :          0 :         if (ret != 0)
    2124                 :            :                 return ret;
    2125                 :          0 :         ret = nfs4_open_recover_helper(opendata, FMODE_READ);
    2126                 :          0 :         if (ret != 0)
    2127                 :            :                 return ret;
    2128                 :            :         /*
    2129                 :            :          * We may have performed cached opens for all three recoveries.
    2130                 :            :          * Check if we need to update the current stateid.
    2131                 :            :          */
    2132                 :          0 :         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
    2133                 :            :             !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
    2134                 :            :                 write_seqlock(&state->seqlock);
    2135                 :          0 :                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
    2136                 :            :                         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
    2137                 :            :                 write_sequnlock(&state->seqlock);
    2138                 :            :         }
    2139                 :            :         return 0;
    2140                 :            : }
    2141                 :            : 
    2142                 :            : /*
    2143                 :            :  * OPEN_RECLAIM:
    2144                 :            :  *      reclaim state on the server after a reboot.
    2145                 :            :  */
    2146                 :          0 : static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
    2147                 :            : {
    2148                 :            :         struct nfs_delegation *delegation;
    2149                 :            :         struct nfs4_opendata *opendata;
    2150                 :            :         fmode_t delegation_type = 0;
    2151                 :            :         int status;
    2152                 :            : 
    2153                 :          0 :         opendata = nfs4_open_recoverdata_alloc(ctx, state,
    2154                 :            :                         NFS4_OPEN_CLAIM_PREVIOUS);
    2155                 :          0 :         if (IS_ERR(opendata))
    2156                 :          0 :                 return PTR_ERR(opendata);
    2157                 :            :         rcu_read_lock();
    2158                 :          0 :         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
    2159                 :          0 :         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
    2160                 :          0 :                 delegation_type = delegation->type;
    2161                 :            :         rcu_read_unlock();
    2162                 :          0 :         opendata->o_arg.u.delegation_type = delegation_type;
    2163                 :          0 :         status = nfs4_open_recover(opendata, state);
    2164                 :            :         nfs4_opendata_put(opendata);
    2165                 :          0 :         return status;
    2166                 :            : }
    2167                 :            : 
    2168                 :          0 : static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
    2169                 :            : {
    2170                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    2171                 :          0 :         struct nfs4_exception exception = { };
    2172                 :            :         int err;
    2173                 :            :         do {
    2174                 :          0 :                 err = _nfs4_do_open_reclaim(ctx, state);
    2175                 :          0 :                 trace_nfs4_open_reclaim(ctx, 0, err);
    2176                 :          0 :                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
    2177                 :          0 :                         continue;
    2178                 :          0 :                 if (err != -NFS4ERR_DELAY)
    2179                 :            :                         break;
    2180                 :          0 :                 nfs4_handle_exception(server, err, &exception);
    2181                 :          0 :         } while (exception.retry);
    2182                 :          0 :         return err;
    2183                 :            : }
    2184                 :            : 
    2185                 :          0 : static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
    2186                 :            : {
    2187                 :            :         struct nfs_open_context *ctx;
    2188                 :            :         int ret;
    2189                 :            : 
    2190                 :          0 :         ctx = nfs4_state_find_open_context(state);
    2191                 :          0 :         if (IS_ERR(ctx))
    2192                 :            :                 return -EAGAIN;
    2193                 :          0 :         clear_bit(NFS_DELEGATED_STATE, &state->flags);
    2194                 :          0 :         nfs_state_clear_open_state_flags(state);
    2195                 :          0 :         ret = nfs4_do_open_reclaim(ctx, state);
    2196                 :          0 :         put_nfs_open_context(ctx);
    2197                 :          0 :         return ret;
    2198                 :            : }
    2199                 :            : 
    2200                 :          0 : static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
    2201                 :            : {
    2202                 :          0 :         switch (err) {
    2203                 :            :                 default:
    2204                 :          0 :                         printk(KERN_ERR "NFS: %s: unhandled error "
    2205                 :            :                                         "%d.\n", __func__, err);
    2206                 :            :                 case 0:
    2207                 :            :                 case -ENOENT:
    2208                 :            :                 case -EAGAIN:
    2209                 :            :                 case -ESTALE:
    2210                 :            :                 case -ETIMEDOUT:
    2211                 :            :                         break;
    2212                 :            :                 case -NFS4ERR_BADSESSION:
    2213                 :            :                 case -NFS4ERR_BADSLOT:
    2214                 :            :                 case -NFS4ERR_BAD_HIGH_SLOT:
    2215                 :            :                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
    2216                 :            :                 case -NFS4ERR_DEADSESSION:
    2217                 :            :                         return -EAGAIN;
    2218                 :            :                 case -NFS4ERR_STALE_CLIENTID:
    2219                 :            :                 case -NFS4ERR_STALE_STATEID:
    2220                 :            :                         /* Don't recall a delegation if it was lost */
    2221                 :          0 :                         nfs4_schedule_lease_recovery(server->nfs_client);
    2222                 :          0 :                         return -EAGAIN;
    2223                 :            :                 case -NFS4ERR_MOVED:
    2224                 :          0 :                         nfs4_schedule_migration_recovery(server);
    2225                 :          0 :                         return -EAGAIN;
    2226                 :            :                 case -NFS4ERR_LEASE_MOVED:
    2227                 :          0 :                         nfs4_schedule_lease_moved_recovery(server->nfs_client);
    2228                 :          0 :                         return -EAGAIN;
    2229                 :            :                 case -NFS4ERR_DELEG_REVOKED:
    2230                 :            :                 case -NFS4ERR_ADMIN_REVOKED:
    2231                 :            :                 case -NFS4ERR_EXPIRED:
    2232                 :            :                 case -NFS4ERR_BAD_STATEID:
    2233                 :            :                 case -NFS4ERR_OPENMODE:
    2234                 :          0 :                         nfs_inode_find_state_and_recover(state->inode,
    2235                 :            :                                         stateid);
    2236                 :          0 :                         nfs4_schedule_stateid_recovery(server, state);
    2237                 :          0 :                         return -EAGAIN;
    2238                 :            :                 case -NFS4ERR_DELAY:
    2239                 :            :                 case -NFS4ERR_GRACE:
    2240                 :            :                         ssleep(1);
    2241                 :          0 :                         return -EAGAIN;
    2242                 :            :                 case -ENOMEM:
    2243                 :            :                 case -NFS4ERR_DENIED:
    2244                 :          0 :                         if (fl) {
    2245                 :          0 :                                 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
    2246                 :          0 :                                 if (lsp)
    2247                 :          0 :                                         set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
    2248                 :            :                         }
    2249                 :            :                         return 0;
    2250                 :            :         }
    2251                 :          0 :         return err;
    2252                 :            : }
    2253                 :            : 
    2254                 :          0 : int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
    2255                 :            :                 struct nfs4_state *state, const nfs4_stateid *stateid)
    2256                 :            : {
    2257                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    2258                 :            :         struct nfs4_opendata *opendata;
    2259                 :            :         int err = 0;
    2260                 :            : 
    2261                 :          0 :         opendata = nfs4_open_recoverdata_alloc(ctx, state,
    2262                 :            :                         NFS4_OPEN_CLAIM_DELEG_CUR_FH);
    2263                 :          0 :         if (IS_ERR(opendata))
    2264                 :          0 :                 return PTR_ERR(opendata);
    2265                 :            :         nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
    2266                 :          0 :         if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
    2267                 :          0 :                 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
    2268                 :          0 :                 if (err)
    2269                 :            :                         goto out;
    2270                 :            :         }
    2271                 :          0 :         if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
    2272                 :          0 :                 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
    2273                 :          0 :                 if (err)
    2274                 :            :                         goto out;
    2275                 :            :         }
    2276                 :          0 :         if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
    2277                 :          0 :                 err = nfs4_open_recover_helper(opendata, FMODE_READ);
    2278                 :          0 :                 if (err)
    2279                 :            :                         goto out;
    2280                 :            :         }
    2281                 :          0 :         nfs_state_clear_delegation(state);
    2282                 :            : out:
    2283                 :            :         nfs4_opendata_put(opendata);
    2284                 :          0 :         return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
    2285                 :            : }
    2286                 :            : 
    2287                 :          0 : static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
    2288                 :            : {
    2289                 :            :         struct nfs4_opendata *data = calldata;
    2290                 :            : 
    2291                 :          0 :         nfs4_setup_sequence(data->o_arg.server->nfs_client,
    2292                 :            :                            &data->c_arg.seq_args, &data->c_res.seq_res, task);
    2293                 :          0 : }
    2294                 :            : 
    2295                 :          0 : static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
    2296                 :            : {
    2297                 :            :         struct nfs4_opendata *data = calldata;
    2298                 :            : 
    2299                 :          0 :         nfs40_sequence_done(task, &data->c_res.seq_res);
    2300                 :            : 
    2301                 :          0 :         data->rpc_status = task->tk_status;
    2302                 :          0 :         if (data->rpc_status == 0) {
    2303                 :            :                 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
    2304                 :          0 :                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
    2305                 :          0 :                 renew_lease(data->o_res.server, data->timestamp);
    2306                 :          0 :                 data->rpc_done = true;
    2307                 :            :         }
    2308                 :          0 : }
    2309                 :            : 
    2310                 :          0 : static void nfs4_open_confirm_release(void *calldata)
    2311                 :            : {
    2312                 :            :         struct nfs4_opendata *data = calldata;
    2313                 :            :         struct nfs4_state *state = NULL;
    2314                 :            : 
    2315                 :            :         /* If this request hasn't been cancelled, do nothing */
    2316                 :          0 :         if (!data->cancelled)
    2317                 :            :                 goto out_free;
    2318                 :            :         /* In case of error, no cleanup! */
    2319                 :          0 :         if (!data->rpc_done)
    2320                 :            :                 goto out_free;
    2321                 :          0 :         state = nfs4_opendata_to_nfs4_state(data);
    2322                 :          0 :         if (!IS_ERR(state))
    2323                 :          0 :                 nfs4_close_state(state, data->o_arg.fmode);
    2324                 :            : out_free:
    2325                 :            :         nfs4_opendata_put(data);
    2326                 :          0 : }
    2327                 :            : 
    2328                 :            : static const struct rpc_call_ops nfs4_open_confirm_ops = {
    2329                 :            :         .rpc_call_prepare = nfs4_open_confirm_prepare,
    2330                 :            :         .rpc_call_done = nfs4_open_confirm_done,
    2331                 :            :         .rpc_release = nfs4_open_confirm_release,
    2332                 :            : };
    2333                 :            : 
    2334                 :            : /*
    2335                 :            :  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
    2336                 :            :  */
    2337                 :          0 : static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
    2338                 :            : {
    2339                 :          0 :         struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
    2340                 :            :         struct rpc_task *task;
    2341                 :          0 :         struct  rpc_message msg = {
    2342                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
    2343                 :          0 :                 .rpc_argp = &data->c_arg,
    2344                 :          0 :                 .rpc_resp = &data->c_res,
    2345                 :          0 :                 .rpc_cred = data->owner->so_cred,
    2346                 :            :         };
    2347                 :          0 :         struct rpc_task_setup task_setup_data = {
    2348                 :          0 :                 .rpc_client = server->client,
    2349                 :            :                 .rpc_message = &msg,
    2350                 :            :                 .callback_ops = &nfs4_open_confirm_ops,
    2351                 :            :                 .callback_data = data,
    2352                 :            :                 .workqueue = nfsiod_workqueue,
    2353                 :            :                 .flags = RPC_TASK_ASYNC,
    2354                 :            :         };
    2355                 :            :         int status;
    2356                 :            : 
    2357                 :            :         nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
    2358                 :          0 :                                 data->is_recover);
    2359                 :            :         kref_get(&data->kref);
    2360                 :          0 :         data->rpc_done = false;
    2361                 :          0 :         data->rpc_status = 0;
    2362                 :          0 :         data->timestamp = jiffies;
    2363                 :          0 :         task = rpc_run_task(&task_setup_data);
    2364                 :          0 :         if (IS_ERR(task))
    2365                 :          0 :                 return PTR_ERR(task);
    2366                 :            :         status = rpc_wait_for_completion_task(task);
    2367                 :          0 :         if (status != 0) {
    2368                 :          0 :                 data->cancelled = true;
    2369                 :          0 :                 smp_wmb();
    2370                 :            :         } else
    2371                 :          0 :                 status = data->rpc_status;
    2372                 :          0 :         rpc_put_task(task);
    2373                 :          0 :         return status;
    2374                 :            : }
    2375                 :            : 
    2376                 :          0 : static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
    2377                 :            : {
    2378                 :            :         struct nfs4_opendata *data = calldata;
    2379                 :          0 :         struct nfs4_state_owner *sp = data->owner;
    2380                 :          0 :         struct nfs_client *clp = sp->so_server->nfs_client;
    2381                 :          0 :         enum open_claim_type4 claim = data->o_arg.claim;
    2382                 :            : 
    2383                 :          0 :         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
    2384                 :            :                 goto out_wait;
    2385                 :            :         /*
    2386                 :            :          * Check if we still need to send an OPEN call, or if we can use
    2387                 :            :          * a delegation instead.
    2388                 :            :          */
    2389                 :          0 :         if (data->state != NULL) {
    2390                 :            :                 struct nfs_delegation *delegation;
    2391                 :            : 
    2392                 :          0 :                 if (can_open_cached(data->state, data->o_arg.fmode,
    2393                 :            :                                         data->o_arg.open_flags, claim))
    2394                 :            :                         goto out_no_action;
    2395                 :            :                 rcu_read_lock();
    2396                 :          0 :                 delegation = nfs4_get_valid_delegation(data->state->inode);
    2397                 :          0 :                 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
    2398                 :            :                         goto unlock_no_action;
    2399                 :            :                 rcu_read_unlock();
    2400                 :            :         }
    2401                 :            :         /* Update client id. */
    2402                 :          0 :         data->o_arg.clientid = clp->cl_clientid;
    2403                 :          0 :         switch (claim) {
    2404                 :            :         default:
    2405                 :            :                 break;
    2406                 :            :         case NFS4_OPEN_CLAIM_PREVIOUS:
    2407                 :            :         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
    2408                 :            :         case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
    2409                 :          0 :                 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
    2410                 :            :                 /* Fall through */
    2411                 :            :         case NFS4_OPEN_CLAIM_FH:
    2412                 :          0 :                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
    2413                 :            :         }
    2414                 :          0 :         data->timestamp = jiffies;
    2415                 :          0 :         if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
    2416                 :            :                                 &data->o_arg.seq_args,
    2417                 :            :                                 &data->o_res.seq_res,
    2418                 :            :                                 task) != 0)
    2419                 :          0 :                 nfs_release_seqid(data->o_arg.seqid);
    2420                 :            : 
    2421                 :            :         /* Set the create mode (note dependency on the session type) */
    2422                 :          0 :         data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
    2423                 :          0 :         if (data->o_arg.open_flags & O_EXCL) {
    2424                 :          0 :                 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
    2425                 :          0 :                 if (nfs4_has_persistent_session(clp))
    2426                 :          0 :                         data->o_arg.createmode = NFS4_CREATE_GUARDED;
    2427                 :          0 :                 else if (clp->cl_mvops->minor_version > 0)
    2428                 :          0 :                         data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
    2429                 :            :         }
    2430                 :          0 :         return;
    2431                 :            : unlock_no_action:
    2432                 :          0 :         trace_nfs4_cached_open(data->state);
    2433                 :            :         rcu_read_unlock();
    2434                 :            : out_no_action:
    2435                 :          0 :         task->tk_action = NULL;
    2436                 :            : out_wait:
    2437                 :          0 :         nfs4_sequence_done(task, &data->o_res.seq_res);
    2438                 :            : }
    2439                 :            : 
    2440                 :          0 : static void nfs4_open_done(struct rpc_task *task, void *calldata)
    2441                 :            : {
    2442                 :            :         struct nfs4_opendata *data = calldata;
    2443                 :            : 
    2444                 :          0 :         data->rpc_status = task->tk_status;
    2445                 :            : 
    2446                 :          0 :         if (!nfs4_sequence_process(task, &data->o_res.seq_res))
    2447                 :          0 :                 return;
    2448                 :            : 
    2449                 :          0 :         if (task->tk_status == 0) {
    2450                 :          0 :                 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
    2451                 :          0 :                         switch (data->o_res.f_attr->mode & S_IFMT) {
    2452                 :            :                         case S_IFREG:
    2453                 :            :                                 break;
    2454                 :            :                         case S_IFLNK:
    2455                 :          0 :                                 data->rpc_status = -ELOOP;
    2456                 :          0 :                                 break;
    2457                 :            :                         case S_IFDIR:
    2458                 :          0 :                                 data->rpc_status = -EISDIR;
    2459                 :          0 :                                 break;
    2460                 :            :                         default:
    2461                 :          0 :                                 data->rpc_status = -ENOTDIR;
    2462                 :            :                         }
    2463                 :            :                 }
    2464                 :          0 :                 renew_lease(data->o_res.server, data->timestamp);
    2465                 :          0 :                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
    2466                 :          0 :                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
    2467                 :            :         }
    2468                 :          0 :         data->rpc_done = true;
    2469                 :            : }
    2470                 :            : 
    2471                 :          0 : static void nfs4_open_release(void *calldata)
    2472                 :            : {
    2473                 :            :         struct nfs4_opendata *data = calldata;
    2474                 :            :         struct nfs4_state *state = NULL;
    2475                 :            : 
    2476                 :            :         /* If this request hasn't been cancelled, do nothing */
    2477                 :          0 :         if (!data->cancelled)
    2478                 :            :                 goto out_free;
    2479                 :            :         /* In case of error, no cleanup! */
    2480                 :          0 :         if (data->rpc_status != 0 || !data->rpc_done)
    2481                 :            :                 goto out_free;
    2482                 :            :         /* In case we need an open_confirm, no cleanup! */
    2483                 :          0 :         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
    2484                 :            :                 goto out_free;
    2485                 :          0 :         state = nfs4_opendata_to_nfs4_state(data);
    2486                 :          0 :         if (!IS_ERR(state))
    2487                 :          0 :                 nfs4_close_state(state, data->o_arg.fmode);
    2488                 :            : out_free:
    2489                 :            :         nfs4_opendata_put(data);
    2490                 :          0 : }
    2491                 :            : 
    2492                 :            : static const struct rpc_call_ops nfs4_open_ops = {
    2493                 :            :         .rpc_call_prepare = nfs4_open_prepare,
    2494                 :            :         .rpc_call_done = nfs4_open_done,
    2495                 :            :         .rpc_release = nfs4_open_release,
    2496                 :            : };
    2497                 :            : 
    2498                 :          0 : static int nfs4_run_open_task(struct nfs4_opendata *data,
    2499                 :            :                               struct nfs_open_context *ctx)
    2500                 :            : {
    2501                 :          0 :         struct inode *dir = d_inode(data->dir);
    2502                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    2503                 :          0 :         struct nfs_openargs *o_arg = &data->o_arg;
    2504                 :          0 :         struct nfs_openres *o_res = &data->o_res;
    2505                 :            :         struct rpc_task *task;
    2506                 :          0 :         struct rpc_message msg = {
    2507                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
    2508                 :            :                 .rpc_argp = o_arg,
    2509                 :            :                 .rpc_resp = o_res,
    2510                 :          0 :                 .rpc_cred = data->owner->so_cred,
    2511                 :            :         };
    2512                 :          0 :         struct rpc_task_setup task_setup_data = {
    2513                 :          0 :                 .rpc_client = server->client,
    2514                 :            :                 .rpc_message = &msg,
    2515                 :            :                 .callback_ops = &nfs4_open_ops,
    2516                 :            :                 .callback_data = data,
    2517                 :            :                 .workqueue = nfsiod_workqueue,
    2518                 :            :                 .flags = RPC_TASK_ASYNC,
    2519                 :            :         };
    2520                 :            :         int status;
    2521                 :            : 
    2522                 :            :         kref_get(&data->kref);
    2523                 :          0 :         data->rpc_done = false;
    2524                 :          0 :         data->rpc_status = 0;
    2525                 :          0 :         data->cancelled = false;
    2526                 :          0 :         data->is_recover = false;
    2527                 :          0 :         if (!ctx) {
    2528                 :            :                 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
    2529                 :          0 :                 data->is_recover = true;
    2530                 :          0 :                 task_setup_data.flags |= RPC_TASK_TIMEOUT;
    2531                 :            :         } else {
    2532                 :            :                 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
    2533                 :          0 :                 pnfs_lgopen_prepare(data, ctx);
    2534                 :            :         }
    2535                 :          0 :         task = rpc_run_task(&task_setup_data);
    2536                 :          0 :         if (IS_ERR(task))
    2537                 :          0 :                 return PTR_ERR(task);
    2538                 :            :         status = rpc_wait_for_completion_task(task);
    2539                 :          0 :         if (status != 0) {
    2540                 :          0 :                 data->cancelled = true;
    2541                 :          0 :                 smp_wmb();
    2542                 :            :         } else
    2543                 :          0 :                 status = data->rpc_status;
    2544                 :          0 :         rpc_put_task(task);
    2545                 :            : 
    2546                 :          0 :         return status;
    2547                 :            : }
    2548                 :            : 
    2549                 :          0 : static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
    2550                 :            : {
    2551                 :          0 :         struct inode *dir = d_inode(data->dir);
    2552                 :            :         struct nfs_openres *o_res = &data->o_res;
    2553                 :            :         int status;
    2554                 :            : 
    2555                 :          0 :         status = nfs4_run_open_task(data, NULL);
    2556                 :          0 :         if (status != 0 || !data->rpc_done)
    2557                 :            :                 return status;
    2558                 :            : 
    2559                 :          0 :         nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
    2560                 :            : 
    2561                 :          0 :         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
    2562                 :          0 :                 status = _nfs4_proc_open_confirm(data);
    2563                 :            : 
    2564                 :          0 :         return status;
    2565                 :            : }
    2566                 :            : 
    2567                 :            : /*
    2568                 :            :  * Additional permission checks in order to distinguish between an
    2569                 :            :  * open for read, and an open for execute. This works around the
    2570                 :            :  * fact that NFSv4 OPEN treats read and execute permissions as being
    2571                 :            :  * the same.
    2572                 :            :  * Note that in the non-execute case, we want to turn off permission
    2573                 :            :  * checking if we just created a new file (POSIX open() semantics).
    2574                 :            :  */
    2575                 :          0 : static int nfs4_opendata_access(const struct cred *cred,
    2576                 :            :                                 struct nfs4_opendata *opendata,
    2577                 :            :                                 struct nfs4_state *state, fmode_t fmode,
    2578                 :            :                                 int openflags)
    2579                 :            : {
    2580                 :            :         struct nfs_access_entry cache;
    2581                 :            :         u32 mask, flags;
    2582                 :            : 
    2583                 :            :         /* access call failed or for some reason the server doesn't
    2584                 :            :          * support any access modes -- defer access call until later */
    2585                 :          0 :         if (opendata->o_res.access_supported == 0)
    2586                 :            :                 return 0;
    2587                 :            : 
    2588                 :            :         mask = 0;
    2589                 :            :         /*
    2590                 :            :          * Use openflags to check for exec, because fmode won't
    2591                 :            :          * always have FMODE_EXEC set when file open for exec.
    2592                 :            :          */
    2593                 :          0 :         if (openflags & __FMODE_EXEC) {
    2594                 :            :                 /* ONLY check for exec rights */
    2595                 :          0 :                 if (S_ISDIR(state->inode->i_mode))
    2596                 :            :                         mask = NFS4_ACCESS_LOOKUP;
    2597                 :            :                 else
    2598                 :            :                         mask = NFS4_ACCESS_EXECUTE;
    2599                 :          0 :         } else if ((fmode & FMODE_READ) && !opendata->file_created)
    2600                 :            :                 mask = NFS4_ACCESS_READ;
    2601                 :            : 
    2602                 :          0 :         cache.cred = cred;
    2603                 :          0 :         nfs_access_set_mask(&cache, opendata->o_res.access_result);
    2604                 :          0 :         nfs_access_add_cache(state->inode, &cache);
    2605                 :            : 
    2606                 :            :         flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
    2607                 :          0 :         if ((mask & ~cache.mask & flags) == 0)
    2608                 :            :                 return 0;
    2609                 :            : 
    2610                 :          0 :         return -EACCES;
    2611                 :            : }
    2612                 :            : 
    2613                 :            : /*
    2614                 :            :  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
    2615                 :            :  */
    2616                 :          0 : static int _nfs4_proc_open(struct nfs4_opendata *data,
    2617                 :            :                            struct nfs_open_context *ctx)
    2618                 :            : {
    2619                 :          0 :         struct inode *dir = d_inode(data->dir);
    2620                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    2621                 :            :         struct nfs_openargs *o_arg = &data->o_arg;
    2622                 :            :         struct nfs_openres *o_res = &data->o_res;
    2623                 :            :         int status;
    2624                 :            : 
    2625                 :          0 :         status = nfs4_run_open_task(data, ctx);
    2626                 :          0 :         if (!data->rpc_done)
    2627                 :            :                 return status;
    2628                 :          0 :         if (status != 0) {
    2629                 :          0 :                 if (status == -NFS4ERR_BADNAME &&
    2630                 :          0 :                                 !(o_arg->open_flags & O_CREAT))
    2631                 :            :                         return -ENOENT;
    2632                 :          0 :                 return status;
    2633                 :            :         }
    2634                 :            : 
    2635                 :          0 :         nfs_fattr_map_and_free_names(server, &data->f_attr);
    2636                 :            : 
    2637                 :          0 :         if (o_arg->open_flags & O_CREAT) {
    2638                 :          0 :                 if (o_arg->open_flags & O_EXCL)
    2639                 :          0 :                         data->file_created = true;
    2640                 :          0 :                 else if (o_res->cinfo.before != o_res->cinfo.after)
    2641                 :          0 :                         data->file_created = true;
    2642                 :          0 :                 if (data->file_created ||
    2643                 :          0 :                     inode_peek_iversion_raw(dir) != o_res->cinfo.after)
    2644                 :          0 :                         update_changeattr(dir, &o_res->cinfo,
    2645                 :          0 :                                         o_res->f_attr->time_start, 0);
    2646                 :            :         }
    2647                 :          0 :         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
    2648                 :          0 :                 server->caps &= ~NFS_CAP_POSIX_LOCK;
    2649                 :          0 :         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
    2650                 :          0 :                 status = _nfs4_proc_open_confirm(data);
    2651                 :          0 :                 if (status != 0)
    2652                 :            :                         return status;
    2653                 :            :         }
    2654                 :          0 :         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
    2655                 :          0 :                 nfs4_sequence_free_slot(&o_res->seq_res);
    2656                 :          0 :                 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
    2657                 :            :                                 o_res->f_label, NULL);
    2658                 :            :         }
    2659                 :            :         return 0;
    2660                 :            : }
    2661                 :            : 
    2662                 :            : /*
    2663                 :            :  * OPEN_EXPIRED:
    2664                 :            :  *      reclaim state on the server after a network partition.
    2665                 :            :  *      Assumes caller holds the appropriate lock
    2666                 :            :  */
    2667                 :          0 : static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
    2668                 :            : {
    2669                 :            :         struct nfs4_opendata *opendata;
    2670                 :            :         int ret;
    2671                 :            : 
    2672                 :          0 :         opendata = nfs4_open_recoverdata_alloc(ctx, state,
    2673                 :            :                         NFS4_OPEN_CLAIM_FH);
    2674                 :          0 :         if (IS_ERR(opendata))
    2675                 :          0 :                 return PTR_ERR(opendata);
    2676                 :          0 :         ret = nfs4_open_recover(opendata, state);
    2677                 :          0 :         if (ret == -ESTALE)
    2678                 :          0 :                 d_drop(ctx->dentry);
    2679                 :            :         nfs4_opendata_put(opendata);
    2680                 :          0 :         return ret;
    2681                 :            : }
    2682                 :            : 
    2683                 :          0 : static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
    2684                 :            : {
    2685                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    2686                 :          0 :         struct nfs4_exception exception = { };
    2687                 :            :         int err;
    2688                 :            : 
    2689                 :            :         do {
    2690                 :          0 :                 err = _nfs4_open_expired(ctx, state);
    2691                 :          0 :                 trace_nfs4_open_expired(ctx, 0, err);
    2692                 :          0 :                 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
    2693                 :          0 :                         continue;
    2694                 :          0 :                 switch (err) {
    2695                 :            :                 default:
    2696                 :            :                         goto out;
    2697                 :            :                 case -NFS4ERR_GRACE:
    2698                 :            :                 case -NFS4ERR_DELAY:
    2699                 :          0 :                         nfs4_handle_exception(server, err, &exception);
    2700                 :            :                         err = 0;
    2701                 :            :                 }
    2702                 :          0 :         } while (exception.retry);
    2703                 :            : out:
    2704                 :          0 :         return err;
    2705                 :            : }
    2706                 :            : 
    2707                 :          0 : static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
    2708                 :            : {
    2709                 :            :         struct nfs_open_context *ctx;
    2710                 :            :         int ret;
    2711                 :            : 
    2712                 :          0 :         ctx = nfs4_state_find_open_context(state);
    2713                 :          0 :         if (IS_ERR(ctx))
    2714                 :            :                 return -EAGAIN;
    2715                 :          0 :         ret = nfs4_do_open_expired(ctx, state);
    2716                 :          0 :         put_nfs_open_context(ctx);
    2717                 :          0 :         return ret;
    2718                 :            : }
    2719                 :            : 
    2720                 :            : static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
    2721                 :            :                 const nfs4_stateid *stateid)
    2722                 :            : {
    2723                 :          0 :         nfs_remove_bad_delegation(state->inode, stateid);
    2724                 :          0 :         nfs_state_clear_delegation(state);
    2725                 :            : }
    2726                 :            : 
    2727                 :          0 : static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
    2728                 :            : {
    2729                 :          0 :         if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
    2730                 :            :                 nfs_finish_clear_delegation_stateid(state, NULL);
    2731                 :          0 : }
    2732                 :            : 
    2733                 :          0 : static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
    2734                 :            : {
    2735                 :            :         /* NFSv4.0 doesn't allow for delegation recovery on open expire */
    2736                 :          0 :         nfs40_clear_delegation_stateid(state);
    2737                 :          0 :         nfs_state_clear_open_state_flags(state);
    2738                 :          0 :         return nfs4_open_expired(sp, state);
    2739                 :            : }
    2740                 :            : 
    2741                 :          0 : static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
    2742                 :            :                 nfs4_stateid *stateid,
    2743                 :            :                 const struct cred *cred)
    2744                 :            : {
    2745                 :          0 :         return -NFS4ERR_BAD_STATEID;
    2746                 :            : }
    2747                 :            : 
    2748                 :            : #if defined(CONFIG_NFS_V4_1)
    2749                 :          0 : static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
    2750                 :            :                 nfs4_stateid *stateid,
    2751                 :            :                 const struct cred *cred)
    2752                 :            : {
    2753                 :            :         int status;
    2754                 :            : 
    2755                 :          0 :         switch (stateid->type) {
    2756                 :            :         default:
    2757                 :            :                 break;
    2758                 :            :         case NFS4_INVALID_STATEID_TYPE:
    2759                 :            :         case NFS4_SPECIAL_STATEID_TYPE:
    2760                 :            :                 return -NFS4ERR_BAD_STATEID;
    2761                 :            :         case NFS4_REVOKED_STATEID_TYPE:
    2762                 :            :                 goto out_free;
    2763                 :            :         }
    2764                 :            : 
    2765                 :          0 :         status = nfs41_test_stateid(server, stateid, cred);
    2766                 :          0 :         switch (status) {
    2767                 :            :         case -NFS4ERR_EXPIRED:
    2768                 :            :         case -NFS4ERR_ADMIN_REVOKED:
    2769                 :            :         case -NFS4ERR_DELEG_REVOKED:
    2770                 :            :                 break;
    2771                 :            :         default:
    2772                 :            :                 return status;
    2773                 :            :         }
    2774                 :            : out_free:
    2775                 :            :         /* Ack the revoked state to the server */
    2776                 :          0 :         nfs41_free_stateid(server, stateid, cred, true);
    2777                 :          0 :         return -NFS4ERR_EXPIRED;
    2778                 :            : }
    2779                 :            : 
    2780                 :          0 : static int nfs41_check_delegation_stateid(struct nfs4_state *state)
    2781                 :            : {
    2782                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    2783                 :            :         nfs4_stateid stateid;
    2784                 :            :         struct nfs_delegation *delegation;
    2785                 :            :         const struct cred *cred = NULL;
    2786                 :            :         int status, ret = NFS_OK;
    2787                 :            : 
    2788                 :            :         /* Get the delegation credential for use by test/free_stateid */
    2789                 :            :         rcu_read_lock();
    2790                 :          0 :         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
    2791                 :          0 :         if (delegation == NULL) {
    2792                 :            :                 rcu_read_unlock();
    2793                 :          0 :                 nfs_state_clear_delegation(state);
    2794                 :          0 :                 return NFS_OK;
    2795                 :            :         }
    2796                 :            : 
    2797                 :            :         nfs4_stateid_copy(&stateid, &delegation->stateid);
    2798                 :            : 
    2799                 :          0 :         if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
    2800                 :            :                                 &delegation->flags)) {
    2801                 :            :                 rcu_read_unlock();
    2802                 :          0 :                 return NFS_OK;
    2803                 :            :         }
    2804                 :            : 
    2805                 :          0 :         if (delegation->cred)
    2806                 :          0 :                 cred = get_cred(delegation->cred);
    2807                 :            :         rcu_read_unlock();
    2808                 :          0 :         status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
    2809                 :          0 :         trace_nfs4_test_delegation_stateid(state, NULL, status);
    2810                 :          0 :         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
    2811                 :            :                 nfs_finish_clear_delegation_stateid(state, &stateid);
    2812                 :            :         else
    2813                 :            :                 ret = status;
    2814                 :            : 
    2815                 :          0 :         put_cred(cred);
    2816                 :          0 :         return ret;
    2817                 :            : }
    2818                 :            : 
    2819                 :          0 : static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
    2820                 :            : {
    2821                 :            :         nfs4_stateid tmp;
    2822                 :            : 
    2823                 :          0 :         if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
    2824                 :          0 :             nfs4_copy_delegation_stateid(state->inode, state->state,
    2825                 :          0 :                                 &tmp, NULL) &&
    2826                 :            :             nfs4_stateid_match_other(&state->stateid, &tmp))
    2827                 :          0 :                 nfs_state_set_delegation(state, &tmp, state->state);
    2828                 :            :         else
    2829                 :          0 :                 nfs_state_clear_delegation(state);
    2830                 :          0 : }
    2831                 :            : 
    2832                 :            : /**
    2833                 :            :  * nfs41_check_expired_locks - possibly free a lock stateid
    2834                 :            :  *
    2835                 :            :  * @state: NFSv4 state for an inode
    2836                 :            :  *
    2837                 :            :  * Returns NFS_OK if recovery for this stateid is now finished.
    2838                 :            :  * Otherwise a negative NFS4ERR value is returned.
    2839                 :            :  */
    2840                 :          0 : static int nfs41_check_expired_locks(struct nfs4_state *state)
    2841                 :            : {
    2842                 :            :         int status, ret = NFS_OK;
    2843                 :            :         struct nfs4_lock_state *lsp, *prev = NULL;
    2844                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    2845                 :            : 
    2846                 :          0 :         if (!test_bit(LK_STATE_IN_USE, &state->flags))
    2847                 :            :                 goto out;
    2848                 :            : 
    2849                 :            :         spin_lock(&state->state_lock);
    2850                 :          0 :         list_for_each_entry(lsp, &state->lock_states, ls_locks) {
    2851                 :          0 :                 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
    2852                 :          0 :                         const struct cred *cred = lsp->ls_state->owner->so_cred;
    2853                 :            : 
    2854                 :          0 :                         refcount_inc(&lsp->ls_count);
    2855                 :            :                         spin_unlock(&state->state_lock);
    2856                 :            : 
    2857                 :          0 :                         nfs4_put_lock_state(prev);
    2858                 :            :                         prev = lsp;
    2859                 :            : 
    2860                 :          0 :                         status = nfs41_test_and_free_expired_stateid(server,
    2861                 :            :                                         &lsp->ls_stateid,
    2862                 :            :                                         cred);
    2863                 :          0 :                         trace_nfs4_test_lock_stateid(state, lsp, status);
    2864                 :          0 :                         if (status == -NFS4ERR_EXPIRED ||
    2865                 :          0 :                             status == -NFS4ERR_BAD_STATEID) {
    2866                 :          0 :                                 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
    2867                 :          0 :                                 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
    2868                 :          0 :                                 if (!recover_lost_locks)
    2869                 :          0 :                                         set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
    2870                 :          0 :                         } else if (status != NFS_OK) {
    2871                 :          0 :                                 ret = status;
    2872                 :          0 :                                 nfs4_put_lock_state(prev);
    2873                 :          0 :                                 goto out;
    2874                 :            :                         }
    2875                 :            :                         spin_lock(&state->state_lock);
    2876                 :            :                 }
    2877                 :            :         }
    2878                 :            :         spin_unlock(&state->state_lock);
    2879                 :          0 :         nfs4_put_lock_state(prev);
    2880                 :            : out:
    2881                 :          0 :         return ret;
    2882                 :            : }
    2883                 :            : 
    2884                 :            : /**
    2885                 :            :  * nfs41_check_open_stateid - possibly free an open stateid
    2886                 :            :  *
    2887                 :            :  * @state: NFSv4 state for an inode
    2888                 :            :  *
    2889                 :            :  * Returns NFS_OK if recovery for this stateid is now finished.
    2890                 :            :  * Otherwise a negative NFS4ERR value is returned.
    2891                 :            :  */
    2892                 :          0 : static int nfs41_check_open_stateid(struct nfs4_state *state)
    2893                 :            : {
    2894                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    2895                 :          0 :         nfs4_stateid *stateid = &state->open_stateid;
    2896                 :          0 :         const struct cred *cred = state->owner->so_cred;
    2897                 :            :         int status;
    2898                 :            : 
    2899                 :          0 :         if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
    2900                 :            :                 return -NFS4ERR_BAD_STATEID;
    2901                 :          0 :         status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
    2902                 :          0 :         trace_nfs4_test_open_stateid(state, NULL, status);
    2903                 :          0 :         if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
    2904                 :          0 :                 nfs_state_clear_open_state_flags(state);
    2905                 :          0 :                 stateid->type = NFS4_INVALID_STATEID_TYPE;
    2906                 :          0 :                 return status;
    2907                 :            :         }
    2908                 :          0 :         if (nfs_open_stateid_recover_openmode(state))
    2909                 :            :                 return -NFS4ERR_OPENMODE;
    2910                 :          0 :         return NFS_OK;
    2911                 :            : }
    2912                 :            : 
    2913                 :          0 : static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
    2914                 :            : {
    2915                 :            :         int status;
    2916                 :            : 
    2917                 :          0 :         status = nfs41_check_delegation_stateid(state);
    2918                 :          0 :         if (status != NFS_OK)
    2919                 :            :                 return status;
    2920                 :          0 :         nfs41_delegation_recover_stateid(state);
    2921                 :            : 
    2922                 :          0 :         status = nfs41_check_expired_locks(state);
    2923                 :          0 :         if (status != NFS_OK)
    2924                 :            :                 return status;
    2925                 :          0 :         status = nfs41_check_open_stateid(state);
    2926                 :          0 :         if (status != NFS_OK)
    2927                 :          0 :                 status = nfs4_open_expired(sp, state);
    2928                 :          0 :         return status;
    2929                 :            : }
    2930                 :            : #endif
    2931                 :            : 
    2932                 :            : /*
    2933                 :            :  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
    2934                 :            :  * fields corresponding to attributes that were used to store the verifier.
    2935                 :            :  * Make sure we clobber those fields in the later setattr call
    2936                 :            :  */
    2937                 :          0 : static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
    2938                 :            :                                 struct iattr *sattr, struct nfs4_label **label)
    2939                 :            : {
    2940                 :          0 :         const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
    2941                 :            :         __u32 attrset[3];
    2942                 :            :         unsigned ret;
    2943                 :            :         unsigned i;
    2944                 :            : 
    2945                 :          0 :         for (i = 0; i < ARRAY_SIZE(attrset); i++) {
    2946                 :          0 :                 attrset[i] = opendata->o_res.attrset[i];
    2947                 :          0 :                 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
    2948                 :          0 :                         attrset[i] &= ~bitmask[i];
    2949                 :            :         }
    2950                 :            : 
    2951                 :          0 :         ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
    2952                 :          0 :                 sattr->ia_valid : 0;
    2953                 :            : 
    2954                 :          0 :         if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
    2955                 :          0 :                 if (sattr->ia_valid & ATTR_ATIME_SET)
    2956                 :          0 :                         ret |= ATTR_ATIME_SET;
    2957                 :            :                 else
    2958                 :          0 :                         ret |= ATTR_ATIME;
    2959                 :            :         }
    2960                 :            : 
    2961                 :          0 :         if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
    2962                 :          0 :                 if (sattr->ia_valid & ATTR_MTIME_SET)
    2963                 :          0 :                         ret |= ATTR_MTIME_SET;
    2964                 :            :                 else
    2965                 :          0 :                         ret |= ATTR_MTIME;
    2966                 :            :         }
    2967                 :            : 
    2968                 :          0 :         if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
    2969                 :          0 :                 *label = NULL;
    2970                 :          0 :         return ret;
    2971                 :            : }
    2972                 :            : 
    2973                 :          0 : static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
    2974                 :            :                 int flags, struct nfs_open_context *ctx)
    2975                 :            : {
    2976                 :          0 :         struct nfs4_state_owner *sp = opendata->owner;
    2977                 :          0 :         struct nfs_server *server = sp->so_server;
    2978                 :            :         struct dentry *dentry;
    2979                 :            :         struct nfs4_state *state;
    2980                 :            :         fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
    2981                 :          0 :         struct inode *dir = d_inode(opendata->dir);
    2982                 :            :         unsigned long dir_verifier;
    2983                 :            :         unsigned int seq;
    2984                 :            :         int ret;
    2985                 :            : 
    2986                 :            :         seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
    2987                 :            :         dir_verifier = nfs_save_change_attribute(dir);
    2988                 :            : 
    2989                 :          0 :         ret = _nfs4_proc_open(opendata, ctx);
    2990                 :          0 :         if (ret != 0)
    2991                 :            :                 goto out;
    2992                 :            : 
    2993                 :          0 :         state = _nfs4_opendata_to_nfs4_state(opendata);
    2994                 :            :         ret = PTR_ERR(state);
    2995                 :          0 :         if (IS_ERR(state))
    2996                 :            :                 goto out;
    2997                 :          0 :         ctx->state = state;
    2998                 :          0 :         if (server->caps & NFS_CAP_POSIX_LOCK)
    2999                 :          0 :                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
    3000                 :          0 :         if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
    3001                 :          0 :                 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
    3002                 :            : 
    3003                 :          0 :         dentry = opendata->dentry;
    3004                 :          0 :         if (d_really_is_negative(dentry)) {
    3005                 :            :                 struct dentry *alias;
    3006                 :          0 :                 d_drop(dentry);
    3007                 :          0 :                 alias = d_exact_alias(dentry, state->inode);
    3008                 :          0 :                 if (!alias)
    3009                 :          0 :                         alias = d_splice_alias(igrab(state->inode), dentry);
    3010                 :            :                 /* d_splice_alias() can't fail here - it's a non-directory */
    3011                 :          0 :                 if (alias) {
    3012                 :          0 :                         dput(ctx->dentry);
    3013                 :          0 :                         ctx->dentry = dentry = alias;
    3014                 :            :                 }
    3015                 :            :         }
    3016                 :            : 
    3017                 :          0 :         switch(opendata->o_arg.claim) {
    3018                 :            :         default:
    3019                 :            :                 break;
    3020                 :            :         case NFS4_OPEN_CLAIM_NULL:
    3021                 :            :         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
    3022                 :            :         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
    3023                 :          0 :                 if (!opendata->rpc_done)
    3024                 :            :                         break;
    3025                 :          0 :                 if (opendata->o_res.delegation_type != 0)
    3026                 :            :                         dir_verifier = nfs_save_change_attribute(dir);
    3027                 :            :                 nfs_set_verifier(dentry, dir_verifier);
    3028                 :            :         }
    3029                 :            : 
    3030                 :            :         /* Parse layoutget results before we check for access */
    3031                 :          0 :         pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
    3032                 :            : 
    3033                 :          0 :         ret = nfs4_opendata_access(sp->so_cred, opendata, state,
    3034                 :            :                         acc_mode, flags);
    3035                 :          0 :         if (ret != 0)
    3036                 :            :                 goto out;
    3037                 :            : 
    3038                 :          0 :         if (d_inode(dentry) == state->inode) {
    3039                 :          0 :                 nfs_inode_attach_open_context(ctx);
    3040                 :          0 :                 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
    3041                 :          0 :                         nfs4_schedule_stateid_recovery(server, state);
    3042                 :            :         }
    3043                 :            : 
    3044                 :            : out:
    3045                 :          0 :         if (!opendata->cancelled)
    3046                 :          0 :                 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
    3047                 :          0 :         return ret;
    3048                 :            : }
    3049                 :            : 
    3050                 :            : /*
    3051                 :            :  * Returns a referenced nfs4_state
    3052                 :            :  */
    3053                 :          0 : static int _nfs4_do_open(struct inode *dir,
    3054                 :            :                         struct nfs_open_context *ctx,
    3055                 :            :                         int flags,
    3056                 :            :                         const struct nfs4_open_createattrs *c,
    3057                 :            :                         int *opened)
    3058                 :            : {
    3059                 :            :         struct nfs4_state_owner  *sp;
    3060                 :            :         struct nfs4_state     *state = NULL;
    3061                 :            :         struct nfs_server       *server = NFS_SERVER(dir);
    3062                 :            :         struct nfs4_opendata *opendata;
    3063                 :          0 :         struct dentry *dentry = ctx->dentry;
    3064                 :          0 :         const struct cred *cred = ctx->cred;
    3065                 :          0 :         struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
    3066                 :            :         fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
    3067                 :            :         enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
    3068                 :          0 :         struct iattr *sattr = c->sattr;
    3069                 :          0 :         struct nfs4_label *label = c->label;
    3070                 :            :         struct nfs4_label *olabel = NULL;
    3071                 :            :         int status;
    3072                 :            : 
    3073                 :            :         /* Protect against reboot recovery conflicts */
    3074                 :            :         status = -ENOMEM;
    3075                 :          0 :         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
    3076                 :          0 :         if (sp == NULL) {
    3077                 :            :                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
    3078                 :            :                 goto out_err;
    3079                 :            :         }
    3080                 :          0 :         status = nfs4_client_recover_expired_lease(server->nfs_client);
    3081                 :          0 :         if (status != 0)
    3082                 :            :                 goto err_put_state_owner;
    3083                 :          0 :         if (d_really_is_positive(dentry))
    3084                 :          0 :                 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
    3085                 :            :         status = -ENOMEM;
    3086                 :          0 :         if (d_really_is_positive(dentry))
    3087                 :            :                 claim = NFS4_OPEN_CLAIM_FH;
    3088                 :          0 :         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
    3089                 :            :                         c, claim, GFP_KERNEL);
    3090                 :          0 :         if (opendata == NULL)
    3091                 :            :                 goto err_put_state_owner;
    3092                 :            : 
    3093                 :          0 :         if (label) {
    3094                 :          0 :                 olabel = nfs4_label_alloc(server, GFP_KERNEL);
    3095                 :          0 :                 if (IS_ERR(olabel)) {
    3096                 :            :                         status = PTR_ERR(olabel);
    3097                 :          0 :                         goto err_opendata_put;
    3098                 :            :                 }
    3099                 :            :         }
    3100                 :            : 
    3101                 :          0 :         if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
    3102                 :          0 :                 if (!opendata->f_attr.mdsthreshold) {
    3103                 :          0 :                         opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
    3104                 :          0 :                         if (!opendata->f_attr.mdsthreshold)
    3105                 :            :                                 goto err_free_label;
    3106                 :            :                 }
    3107                 :          0 :                 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
    3108                 :            :         }
    3109                 :          0 :         if (d_really_is_positive(dentry))
    3110                 :          0 :                 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
    3111                 :            : 
    3112                 :          0 :         status = _nfs4_open_and_get_state(opendata, flags, ctx);
    3113                 :          0 :         if (status != 0)
    3114                 :            :                 goto err_free_label;
    3115                 :          0 :         state = ctx->state;
    3116                 :            : 
    3117                 :          0 :         if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
    3118                 :          0 :             (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
    3119                 :          0 :                 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
    3120                 :            :                 /*
    3121                 :            :                  * send create attributes which was not set by open
    3122                 :            :                  * with an extra setattr.
    3123                 :            :                  */
    3124                 :          0 :                 if (attrs || label) {
    3125                 :          0 :                         unsigned ia_old = sattr->ia_valid;
    3126                 :            : 
    3127                 :          0 :                         sattr->ia_valid = attrs;
    3128                 :          0 :                         nfs_fattr_init(opendata->o_res.f_attr);
    3129                 :          0 :                         status = nfs4_do_setattr(state->inode, cred,
    3130                 :            :                                         opendata->o_res.f_attr, sattr,
    3131                 :            :                                         ctx, label, olabel);
    3132                 :          0 :                         if (status == 0) {
    3133                 :          0 :                                 nfs_setattr_update_inode(state->inode, sattr,
    3134                 :            :                                                 opendata->o_res.f_attr);
    3135                 :          0 :                                 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
    3136                 :            :                         }
    3137                 :          0 :                         sattr->ia_valid = ia_old;
    3138                 :            :                 }
    3139                 :            :         }
    3140                 :          0 :         if (opened && opendata->file_created)
    3141                 :          0 :                 *opened = 1;
    3142                 :            : 
    3143                 :          0 :         if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
    3144                 :          0 :                 *ctx_th = opendata->f_attr.mdsthreshold;
    3145                 :          0 :                 opendata->f_attr.mdsthreshold = NULL;
    3146                 :            :         }
    3147                 :            : 
    3148                 :          0 :         nfs4_label_free(olabel);
    3149                 :            : 
    3150                 :            :         nfs4_opendata_put(opendata);
    3151                 :          0 :         nfs4_put_state_owner(sp);
    3152                 :          0 :         return 0;
    3153                 :            : err_free_label:
    3154                 :          0 :         nfs4_label_free(olabel);
    3155                 :            : err_opendata_put:
    3156                 :            :         nfs4_opendata_put(opendata);
    3157                 :            : err_put_state_owner:
    3158                 :          0 :         nfs4_put_state_owner(sp);
    3159                 :            : out_err:
    3160                 :          0 :         return status;
    3161                 :            : }
    3162                 :            : 
    3163                 :            : 
    3164                 :          0 : static struct nfs4_state *nfs4_do_open(struct inode *dir,
    3165                 :            :                                         struct nfs_open_context *ctx,
    3166                 :            :                                         int flags,
    3167                 :            :                                         struct iattr *sattr,
    3168                 :            :                                         struct nfs4_label *label,
    3169                 :            :                                         int *opened)
    3170                 :            : {
    3171                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    3172                 :          0 :         struct nfs4_exception exception = {
    3173                 :            :                 .interruptible = true,
    3174                 :            :         };
    3175                 :            :         struct nfs4_state *res;
    3176                 :          0 :         struct nfs4_open_createattrs c = {
    3177                 :            :                 .label = label,
    3178                 :            :                 .sattr = sattr,
    3179                 :            :                 .verf = {
    3180                 :          0 :                         [0] = (__u32)jiffies,
    3181                 :          0 :                         [1] = (__u32)current->pid,
    3182                 :            :                 },
    3183                 :            :         };
    3184                 :            :         int status;
    3185                 :            : 
    3186                 :            :         do {
    3187                 :          0 :                 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
    3188                 :          0 :                 res = ctx->state;
    3189                 :          0 :                 trace_nfs4_open_file(ctx, flags, status);
    3190                 :          0 :                 if (status == 0)
    3191                 :            :                         break;
    3192                 :            :                 /* NOTE: BAD_SEQID means the server and client disagree about the
    3193                 :            :                  * book-keeping w.r.t. state-changing operations
    3194                 :            :                  * (OPEN/CLOSE/LOCK/LOCKU...)
    3195                 :            :                  * It is actually a sign of a bug on the client or on the server.
    3196                 :            :                  *
    3197                 :            :                  * If we receive a BAD_SEQID error in the particular case of
    3198                 :            :                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
    3199                 :            :                  * have unhashed the old state_owner for us, and that we can
    3200                 :            :                  * therefore safely retry using a new one. We should still warn
    3201                 :            :                  * the user though...
    3202                 :            :                  */
    3203                 :          0 :                 if (status == -NFS4ERR_BAD_SEQID) {
    3204                 :          0 :                         pr_warn_ratelimited("NFS: v4 server %s "
    3205                 :            :                                         " returned a bad sequence-id error!\n",
    3206                 :            :                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
    3207                 :          0 :                         exception.retry = 1;
    3208                 :          0 :                         continue;
    3209                 :            :                 }
    3210                 :            :                 /*
    3211                 :            :                  * BAD_STATEID on OPEN means that the server cancelled our
    3212                 :            :                  * state before it received the OPEN_CONFIRM.
    3213                 :            :                  * Recover by retrying the request as per the discussion
    3214                 :            :                  * on Page 181 of RFC3530.
    3215                 :            :                  */
    3216                 :          0 :                 if (status == -NFS4ERR_BAD_STATEID) {
    3217                 :          0 :                         exception.retry = 1;
    3218                 :          0 :                         continue;
    3219                 :            :                 }
    3220                 :          0 :                 if (status == -NFS4ERR_EXPIRED) {
    3221                 :          0 :                         nfs4_schedule_lease_recovery(server->nfs_client);
    3222                 :          0 :                         exception.retry = 1;
    3223                 :          0 :                         continue;
    3224                 :            :                 }
    3225                 :          0 :                 if (status == -EAGAIN) {
    3226                 :            :                         /* We must have found a delegation */
    3227                 :          0 :                         exception.retry = 1;
    3228                 :          0 :                         continue;
    3229                 :            :                 }
    3230                 :          0 :                 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
    3231                 :          0 :                         continue;
    3232                 :          0 :                 res = ERR_PTR(nfs4_handle_exception(server,
    3233                 :            :                                         status, &exception));
    3234                 :          0 :         } while (exception.retry);
    3235                 :          0 :         return res;
    3236                 :            : }
    3237                 :            : 
    3238                 :          0 : static int _nfs4_do_setattr(struct inode *inode,
    3239                 :            :                             struct nfs_setattrargs *arg,
    3240                 :            :                             struct nfs_setattrres *res,
    3241                 :            :                             const struct cred *cred,
    3242                 :            :                             struct nfs_open_context *ctx)
    3243                 :            : {
    3244                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    3245                 :          0 :         struct rpc_message msg = {
    3246                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
    3247                 :            :                 .rpc_argp       = arg,
    3248                 :            :                 .rpc_resp       = res,
    3249                 :            :                 .rpc_cred       = cred,
    3250                 :            :         };
    3251                 :          0 :         const struct cred *delegation_cred = NULL;
    3252                 :          0 :         unsigned long timestamp = jiffies;
    3253                 :            :         bool truncate;
    3254                 :            :         int status;
    3255                 :            : 
    3256                 :          0 :         nfs_fattr_init(res->fattr);
    3257                 :            : 
    3258                 :            :         /* Servers should only apply open mode checks for file size changes */
    3259                 :          0 :         truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
    3260                 :          0 :         if (!truncate)
    3261                 :            :                 goto zero_stateid;
    3262                 :            : 
    3263                 :          0 :         if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
    3264                 :            :                 /* Use that stateid */
    3265                 :          0 :         } else if (ctx != NULL && ctx->state) {
    3266                 :            :                 struct nfs_lock_context *l_ctx;
    3267                 :          0 :                 if (!nfs4_valid_open_stateid(ctx->state))
    3268                 :            :                         return -EBADF;
    3269                 :          0 :                 l_ctx = nfs_get_lock_context(ctx);
    3270                 :          0 :                 if (IS_ERR(l_ctx))
    3271                 :          0 :                         return PTR_ERR(l_ctx);
    3272                 :          0 :                 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
    3273                 :            :                                                 &arg->stateid, &delegation_cred);
    3274                 :          0 :                 nfs_put_lock_context(l_ctx);
    3275                 :          0 :                 if (status == -EIO)
    3276                 :            :                         return -EBADF;
    3277                 :            :         } else {
    3278                 :            : zero_stateid:
    3279                 :            :                 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
    3280                 :            :         }
    3281                 :          0 :         if (delegation_cred)
    3282                 :          0 :                 msg.rpc_cred = delegation_cred;
    3283                 :            : 
    3284                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
    3285                 :            : 
    3286                 :          0 :         put_cred(delegation_cred);
    3287                 :          0 :         if (status == 0 && ctx != NULL)
    3288                 :          0 :                 renew_lease(server, timestamp);
    3289                 :          0 :         trace_nfs4_setattr(inode, &arg->stateid, status);
    3290                 :          0 :         return status;
    3291                 :            : }
    3292                 :            : 
    3293                 :          0 : static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
    3294                 :            :                            struct nfs_fattr *fattr, struct iattr *sattr,
    3295                 :            :                            struct nfs_open_context *ctx, struct nfs4_label *ilabel,
    3296                 :            :                            struct nfs4_label *olabel)
    3297                 :            : {
    3298                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    3299                 :            :         __u32 bitmask[NFS4_BITMASK_SZ];
    3300                 :          0 :         struct nfs4_state *state = ctx ? ctx->state : NULL;
    3301                 :          0 :         struct nfs_setattrargs  arg = {
    3302                 :            :                 .fh             = NFS_FH(inode),
    3303                 :            :                 .iap            = sattr,
    3304                 :            :                 .server         = server,
    3305                 :            :                 .bitmask = bitmask,
    3306                 :            :                 .label          = ilabel,
    3307                 :            :         };
    3308                 :          0 :         struct nfs_setattrres  res = {
    3309                 :            :                 .fattr          = fattr,
    3310                 :            :                 .label          = olabel,
    3311                 :            :                 .server         = server,
    3312                 :            :         };
    3313                 :          0 :         struct nfs4_exception exception = {
    3314                 :            :                 .state = state,
    3315                 :            :                 .inode = inode,
    3316                 :            :                 .stateid = &arg.stateid,
    3317                 :            :         };
    3318                 :            :         int err;
    3319                 :            : 
    3320                 :            :         do {
    3321                 :            :                 nfs4_bitmap_copy_adjust_setattr(bitmask,
    3322                 :            :                                 nfs4_bitmask(server, olabel),
    3323                 :            :                                 inode);
    3324                 :            : 
    3325                 :          0 :                 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
    3326                 :          0 :                 switch (err) {
    3327                 :            :                 case -NFS4ERR_OPENMODE:
    3328                 :          0 :                         if (!(sattr->ia_valid & ATTR_SIZE)) {
    3329                 :          0 :                                 pr_warn_once("NFSv4: server %s is incorrectly "
    3330                 :            :                                                 "applying open mode checks to "
    3331                 :            :                                                 "a SETATTR that is not "
    3332                 :            :                                                 "changing file size.\n",
    3333                 :            :                                                 server->nfs_client->cl_hostname);
    3334                 :            :                         }
    3335                 :          0 :                         if (state && !(state->state & FMODE_WRITE)) {
    3336                 :            :                                 err = -EBADF;
    3337                 :          0 :                                 if (sattr->ia_valid & ATTR_OPEN)
    3338                 :            :                                         err = -EACCES;
    3339                 :            :                                 goto out;
    3340                 :            :                         }
    3341                 :            :                 }
    3342                 :          0 :                 err = nfs4_handle_exception(server, err, &exception);
    3343                 :          0 :         } while (exception.retry);
    3344                 :            : out:
    3345                 :          0 :         return err;
    3346                 :            : }
    3347                 :            : 
    3348                 :            : static bool
    3349                 :            : nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
    3350                 :            : {
    3351                 :          0 :         if (inode == NULL || !nfs_have_layout(inode))
    3352                 :            :                 return false;
    3353                 :            : 
    3354                 :          0 :         return pnfs_wait_on_layoutreturn(inode, task);
    3355                 :            : }
    3356                 :            : 
    3357                 :            : /*
    3358                 :            :  * Update the seqid of an open stateid
    3359                 :            :  */
    3360                 :          0 : static void nfs4_sync_open_stateid(nfs4_stateid *dst,
    3361                 :            :                 struct nfs4_state *state)
    3362                 :            : {
    3363                 :            :         __be32 seqid_open;
    3364                 :            :         u32 dst_seqid;
    3365                 :            :         int seq;
    3366                 :            : 
    3367                 :            :         for (;;) {
    3368                 :          0 :                 if (!nfs4_valid_open_stateid(state))
    3369                 :            :                         break;
    3370                 :            :                 seq = read_seqbegin(&state->seqlock);
    3371                 :          0 :                 if (!nfs4_state_match_open_stateid_other(state, dst)) {
    3372                 :            :                         nfs4_stateid_copy(dst, &state->open_stateid);
    3373                 :          0 :                         if (read_seqretry(&state->seqlock, seq))
    3374                 :          0 :                                 continue;
    3375                 :            :                         break;
    3376                 :            :                 }
    3377                 :          0 :                 seqid_open = state->open_stateid.seqid;
    3378                 :          0 :                 if (read_seqretry(&state->seqlock, seq))
    3379                 :          0 :                         continue;
    3380                 :            : 
    3381                 :          0 :                 dst_seqid = be32_to_cpu(dst->seqid);
    3382                 :          0 :                 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
    3383                 :          0 :                         dst->seqid = seqid_open;
    3384                 :            :                 break;
    3385                 :            :         }
    3386                 :          0 : }
    3387                 :            : 
    3388                 :            : /*
    3389                 :            :  * Update the seqid of an open stateid after receiving
    3390                 :            :  * NFS4ERR_OLD_STATEID
    3391                 :            :  */
    3392                 :          0 : static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
    3393                 :            :                 struct nfs4_state *state)
    3394                 :            : {
    3395                 :            :         __be32 seqid_open;
    3396                 :            :         u32 dst_seqid;
    3397                 :            :         bool ret;
    3398                 :            :         int seq;
    3399                 :            : 
    3400                 :            :         for (;;) {
    3401                 :            :                 ret = false;
    3402                 :          0 :                 if (!nfs4_valid_open_stateid(state))
    3403                 :            :                         break;
    3404                 :            :                 seq = read_seqbegin(&state->seqlock);
    3405                 :          0 :                 if (!nfs4_state_match_open_stateid_other(state, dst)) {
    3406                 :          0 :                         if (read_seqretry(&state->seqlock, seq))
    3407                 :          0 :                                 continue;
    3408                 :            :                         break;
    3409                 :            :                 }
    3410                 :          0 :                 seqid_open = state->open_stateid.seqid;
    3411                 :          0 :                 if (read_seqretry(&state->seqlock, seq))
    3412                 :          0 :                         continue;
    3413                 :            : 
    3414                 :          0 :                 dst_seqid = be32_to_cpu(dst->seqid);
    3415                 :          0 :                 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
    3416                 :          0 :                         dst->seqid = cpu_to_be32(dst_seqid + 1);
    3417                 :            :                 else
    3418                 :          0 :                         dst->seqid = seqid_open;
    3419                 :            :                 ret = true;
    3420                 :            :                 break;
    3421                 :            :         }
    3422                 :            : 
    3423                 :          0 :         return ret;
    3424                 :            : }
    3425                 :            : 
    3426                 :            : struct nfs4_closedata {
    3427                 :            :         struct inode *inode;
    3428                 :            :         struct nfs4_state *state;
    3429                 :            :         struct nfs_closeargs arg;
    3430                 :            :         struct nfs_closeres res;
    3431                 :            :         struct {
    3432                 :            :                 struct nfs4_layoutreturn_args arg;
    3433                 :            :                 struct nfs4_layoutreturn_res res;
    3434                 :            :                 struct nfs4_xdr_opaque_data ld_private;
    3435                 :            :                 u32 roc_barrier;
    3436                 :            :                 bool roc;
    3437                 :            :         } lr;
    3438                 :            :         struct nfs_fattr fattr;
    3439                 :            :         unsigned long timestamp;
    3440                 :            : };
    3441                 :            : 
    3442                 :          0 : static void nfs4_free_closedata(void *data)
    3443                 :            : {
    3444                 :            :         struct nfs4_closedata *calldata = data;
    3445                 :          0 :         struct nfs4_state_owner *sp = calldata->state->owner;
    3446                 :          0 :         struct super_block *sb = calldata->state->inode->i_sb;
    3447                 :            : 
    3448                 :          0 :         if (calldata->lr.roc)
    3449                 :          0 :                 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
    3450                 :            :                                 calldata->res.lr_ret);
    3451                 :          0 :         nfs4_put_open_state(calldata->state);
    3452                 :          0 :         nfs_free_seqid(calldata->arg.seqid);
    3453                 :          0 :         nfs4_put_state_owner(sp);
    3454                 :          0 :         nfs_sb_deactive(sb);
    3455                 :          0 :         kfree(calldata);
    3456                 :          0 : }
    3457                 :            : 
    3458                 :          0 : static void nfs4_close_done(struct rpc_task *task, void *data)
    3459                 :            : {
    3460                 :            :         struct nfs4_closedata *calldata = data;
    3461                 :          0 :         struct nfs4_state *state = calldata->state;
    3462                 :          0 :         struct nfs_server *server = NFS_SERVER(calldata->inode);
    3463                 :            :         nfs4_stateid *res_stateid = NULL;
    3464                 :          0 :         struct nfs4_exception exception = {
    3465                 :            :                 .state = state,
    3466                 :            :                 .inode = calldata->inode,
    3467                 :          0 :                 .stateid = &calldata->arg.stateid,
    3468                 :            :         };
    3469                 :            : 
    3470                 :            :         dprintk("%s: begin!\n", __func__);
    3471                 :          0 :         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
    3472                 :            :                 return;
    3473                 :          0 :         trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
    3474                 :            : 
    3475                 :            :         /* Handle Layoutreturn errors */
    3476                 :          0 :         if (pnfs_roc_done(task, calldata->inode,
    3477                 :            :                                 &calldata->arg.lr_args,
    3478                 :            :                                 &calldata->res.lr_res,
    3479                 :            :                                 &calldata->res.lr_ret) == -EAGAIN)
    3480                 :            :                 goto out_restart;
    3481                 :            : 
    3482                 :            :         /* hmm. we are done with the inode, and in the process of freeing
    3483                 :            :          * the state_owner. we keep this around to process errors
    3484                 :            :          */
    3485                 :          0 :         switch (task->tk_status) {
    3486                 :            :                 case 0:
    3487                 :          0 :                         res_stateid = &calldata->res.stateid;
    3488                 :          0 :                         renew_lease(server, calldata->timestamp);
    3489                 :          0 :                         break;
    3490                 :            :                 case -NFS4ERR_ACCESS:
    3491                 :          0 :                         if (calldata->arg.bitmask != NULL) {
    3492                 :          0 :                                 calldata->arg.bitmask = NULL;
    3493                 :          0 :                                 calldata->res.fattr = NULL;
    3494                 :          0 :                                 goto out_restart;
    3495                 :            : 
    3496                 :            :                         }
    3497                 :            :                         break;
    3498                 :            :                 case -NFS4ERR_OLD_STATEID:
    3499                 :            :                         /* Did we race with OPEN? */
    3500                 :          0 :                         if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
    3501                 :            :                                                 state))
    3502                 :            :                                 goto out_restart;
    3503                 :            :                         goto out_release;
    3504                 :            :                 case -NFS4ERR_ADMIN_REVOKED:
    3505                 :            :                 case -NFS4ERR_STALE_STATEID:
    3506                 :            :                 case -NFS4ERR_EXPIRED:
    3507                 :          0 :                         nfs4_free_revoked_stateid(server,
    3508                 :            :                                         &calldata->arg.stateid,
    3509                 :            :                                         task->tk_msg.rpc_cred);
    3510                 :            :                         /* Fallthrough */
    3511                 :            :                 case -NFS4ERR_BAD_STATEID:
    3512                 :          0 :                         if (calldata->arg.fmode == 0)
    3513                 :            :                                 break;
    3514                 :            :                         /* Fallthrough */
    3515                 :            :                 default:
    3516                 :          0 :                         task->tk_status = nfs4_async_handle_exception(task,
    3517                 :            :                                         server, task->tk_status, &exception);
    3518                 :          0 :                         if (exception.retry)
    3519                 :            :                                 goto out_restart;
    3520                 :            :         }
    3521                 :          0 :         nfs_clear_open_stateid(state, &calldata->arg.stateid,
    3522                 :            :                         res_stateid, calldata->arg.fmode);
    3523                 :            : out_release:
    3524                 :          0 :         task->tk_status = 0;
    3525                 :          0 :         nfs_release_seqid(calldata->arg.seqid);
    3526                 :          0 :         nfs_refresh_inode(calldata->inode, &calldata->fattr);
    3527                 :            :         dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
    3528                 :          0 :         return;
    3529                 :            : out_restart:
    3530                 :          0 :         task->tk_status = 0;
    3531                 :          0 :         rpc_restart_call_prepare(task);
    3532                 :          0 :         goto out_release;
    3533                 :            : }
    3534                 :            : 
    3535                 :          0 : static void nfs4_close_prepare(struct rpc_task *task, void *data)
    3536                 :            : {
    3537                 :            :         struct nfs4_closedata *calldata = data;
    3538                 :          0 :         struct nfs4_state *state = calldata->state;
    3539                 :          0 :         struct inode *inode = calldata->inode;
    3540                 :            :         struct pnfs_layout_hdr *lo;
    3541                 :            :         bool is_rdonly, is_wronly, is_rdwr;
    3542                 :            :         int call_close = 0;
    3543                 :            : 
    3544                 :            :         dprintk("%s: begin!\n", __func__);
    3545                 :          0 :         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
    3546                 :            :                 goto out_wait;
    3547                 :            : 
    3548                 :          0 :         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
    3549                 :          0 :         spin_lock(&state->owner->so_lock);
    3550                 :          0 :         is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
    3551                 :          0 :         is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
    3552                 :          0 :         is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
    3553                 :            :         /* Calculate the change in open mode */
    3554                 :          0 :         calldata->arg.fmode = 0;
    3555                 :          0 :         if (state->n_rdwr == 0) {
    3556                 :          0 :                 if (state->n_rdonly == 0)
    3557                 :          0 :                         call_close |= is_rdonly;
    3558                 :          0 :                 else if (is_rdonly)
    3559                 :          0 :                         calldata->arg.fmode |= FMODE_READ;
    3560                 :          0 :                 if (state->n_wronly == 0)
    3561                 :          0 :                         call_close |= is_wronly;
    3562                 :          0 :                 else if (is_wronly)
    3563                 :          0 :                         calldata->arg.fmode |= FMODE_WRITE;
    3564                 :          0 :                 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
    3565                 :          0 :                         call_close |= is_rdwr;
    3566                 :          0 :         } else if (is_rdwr)
    3567                 :          0 :                 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
    3568                 :            : 
    3569                 :          0 :         nfs4_sync_open_stateid(&calldata->arg.stateid, state);
    3570                 :          0 :         if (!nfs4_valid_open_stateid(state))
    3571                 :            :                 call_close = 0;
    3572                 :          0 :         spin_unlock(&state->owner->so_lock);
    3573                 :            : 
    3574                 :          0 :         if (!call_close) {
    3575                 :            :                 /* Note: exit _without_ calling nfs4_close_done */
    3576                 :            :                 goto out_no_action;
    3577                 :            :         }
    3578                 :            : 
    3579                 :          0 :         if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
    3580                 :          0 :                 nfs_release_seqid(calldata->arg.seqid);
    3581                 :          0 :                 goto out_wait;
    3582                 :            :         }
    3583                 :            : 
    3584                 :          0 :         lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
    3585                 :          0 :         if (lo && !pnfs_layout_is_valid(lo)) {
    3586                 :          0 :                 calldata->arg.lr_args = NULL;
    3587                 :          0 :                 calldata->res.lr_res = NULL;
    3588                 :            :         }
    3589                 :            : 
    3590                 :          0 :         if (calldata->arg.fmode == 0)
    3591                 :          0 :                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
    3592                 :            : 
    3593                 :          0 :         if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
    3594                 :            :                 /* Close-to-open cache consistency revalidation */
    3595                 :          0 :                 if (!nfs4_have_delegation(inode, FMODE_READ))
    3596                 :          0 :                         calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
    3597                 :            :                 else
    3598                 :          0 :                         calldata->arg.bitmask = NULL;
    3599                 :            :         }
    3600                 :            : 
    3601                 :          0 :         calldata->arg.share_access =
    3602                 :          0 :                 nfs4_map_atomic_open_share(NFS_SERVER(inode),
    3603                 :            :                                 calldata->arg.fmode, 0);
    3604                 :            : 
    3605                 :          0 :         if (calldata->res.fattr == NULL)
    3606                 :          0 :                 calldata->arg.bitmask = NULL;
    3607                 :          0 :         else if (calldata->arg.bitmask == NULL)
    3608                 :          0 :                 calldata->res.fattr = NULL;
    3609                 :          0 :         calldata->timestamp = jiffies;
    3610                 :          0 :         if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
    3611                 :            :                                 &calldata->arg.seq_args,
    3612                 :            :                                 &calldata->res.seq_res,
    3613                 :            :                                 task) != 0)
    3614                 :          0 :                 nfs_release_seqid(calldata->arg.seqid);
    3615                 :            :         dprintk("%s: done!\n", __func__);
    3616                 :          0 :         return;
    3617                 :            : out_no_action:
    3618                 :          0 :         task->tk_action = NULL;
    3619                 :            : out_wait:
    3620                 :          0 :         nfs4_sequence_done(task, &calldata->res.seq_res);
    3621                 :            : }
    3622                 :            : 
    3623                 :            : static const struct rpc_call_ops nfs4_close_ops = {
    3624                 :            :         .rpc_call_prepare = nfs4_close_prepare,
    3625                 :            :         .rpc_call_done = nfs4_close_done,
    3626                 :            :         .rpc_release = nfs4_free_closedata,
    3627                 :            : };
    3628                 :            : 
    3629                 :            : /* 
    3630                 :            :  * It is possible for data to be read/written from a mem-mapped file 
    3631                 :            :  * after the sys_close call (which hits the vfs layer as a flush).
    3632                 :            :  * This means that we can't safely call nfsv4 close on a file until 
    3633                 :            :  * the inode is cleared. This in turn means that we are not good
    3634                 :            :  * NFSv4 citizens - we do not indicate to the server to update the file's 
    3635                 :            :  * share state even when we are done with one of the three share 
    3636                 :            :  * stateid's in the inode.
    3637                 :            :  *
    3638                 :            :  * NOTE: Caller must be holding the sp->so_owner semaphore!
    3639                 :            :  */
    3640                 :          0 : int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
    3641                 :            : {
    3642                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    3643                 :            :         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
    3644                 :            :         struct nfs4_closedata *calldata;
    3645                 :          0 :         struct nfs4_state_owner *sp = state->owner;
    3646                 :            :         struct rpc_task *task;
    3647                 :          0 :         struct rpc_message msg = {
    3648                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
    3649                 :          0 :                 .rpc_cred = state->owner->so_cred,
    3650                 :            :         };
    3651                 :          0 :         struct rpc_task_setup task_setup_data = {
    3652                 :          0 :                 .rpc_client = server->client,
    3653                 :            :                 .rpc_message = &msg,
    3654                 :            :                 .callback_ops = &nfs4_close_ops,
    3655                 :            :                 .workqueue = nfsiod_workqueue,
    3656                 :            :                 .flags = RPC_TASK_ASYNC,
    3657                 :            :         };
    3658                 :            :         int status = -ENOMEM;
    3659                 :            : 
    3660                 :          0 :         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
    3661                 :            :                 &task_setup_data.rpc_client, &msg);
    3662                 :            : 
    3663                 :          0 :         calldata = kzalloc(sizeof(*calldata), gfp_mask);
    3664                 :          0 :         if (calldata == NULL)
    3665                 :            :                 goto out;
    3666                 :            :         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
    3667                 :          0 :         calldata->inode = state->inode;
    3668                 :          0 :         calldata->state = state;
    3669                 :          0 :         calldata->arg.fh = NFS_FH(state->inode);
    3670                 :          0 :         if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
    3671                 :            :                 goto out_free_calldata;
    3672                 :            :         /* Serialization for the sequence id */
    3673                 :          0 :         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
    3674                 :          0 :         calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
    3675                 :          0 :         if (IS_ERR(calldata->arg.seqid))
    3676                 :            :                 goto out_free_calldata;
    3677                 :          0 :         nfs_fattr_init(&calldata->fattr);
    3678                 :          0 :         calldata->arg.fmode = 0;
    3679                 :          0 :         calldata->lr.arg.ld_private = &calldata->lr.ld_private;
    3680                 :          0 :         calldata->res.fattr = &calldata->fattr;
    3681                 :          0 :         calldata->res.seqid = calldata->arg.seqid;
    3682                 :          0 :         calldata->res.server = server;
    3683                 :          0 :         calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
    3684                 :          0 :         calldata->lr.roc = pnfs_roc(state->inode,
    3685                 :            :                         &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
    3686                 :          0 :         if (calldata->lr.roc) {
    3687                 :          0 :                 calldata->arg.lr_args = &calldata->lr.arg;
    3688                 :          0 :                 calldata->res.lr_res = &calldata->lr.res;
    3689                 :            :         }
    3690                 :          0 :         nfs_sb_active(calldata->inode->i_sb);
    3691                 :            : 
    3692                 :          0 :         msg.rpc_argp = &calldata->arg;
    3693                 :          0 :         msg.rpc_resp = &calldata->res;
    3694                 :          0 :         task_setup_data.callback_data = calldata;
    3695                 :          0 :         task = rpc_run_task(&task_setup_data);
    3696                 :          0 :         if (IS_ERR(task))
    3697                 :          0 :                 return PTR_ERR(task);
    3698                 :            :         status = 0;
    3699                 :          0 :         if (wait)
    3700                 :            :                 status = rpc_wait_for_completion_task(task);
    3701                 :          0 :         rpc_put_task(task);
    3702                 :          0 :         return status;
    3703                 :            : out_free_calldata:
    3704                 :          0 :         kfree(calldata);
    3705                 :            : out:
    3706                 :          0 :         nfs4_put_open_state(state);
    3707                 :          0 :         nfs4_put_state_owner(sp);
    3708                 :          0 :         return status;
    3709                 :            : }
    3710                 :            : 
    3711                 :            : static struct inode *
    3712                 :          0 : nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
    3713                 :            :                 int open_flags, struct iattr *attr, int *opened)
    3714                 :            : {
    3715                 :            :         struct nfs4_state *state;
    3716                 :          0 :         struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
    3717                 :            : 
    3718                 :          0 :         label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
    3719                 :            : 
    3720                 :            :         /* Protect against concurrent sillydeletes */
    3721                 :          0 :         state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
    3722                 :            : 
    3723                 :            :         nfs4_label_release_security(label);
    3724                 :            : 
    3725                 :          0 :         if (IS_ERR(state))
    3726                 :            :                 return ERR_CAST(state);
    3727                 :          0 :         return state->inode;
    3728                 :            : }
    3729                 :            : 
    3730                 :          0 : static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
    3731                 :            : {
    3732                 :          0 :         if (ctx->state == NULL)
    3733                 :          0 :                 return;
    3734                 :          0 :         if (is_sync)
    3735                 :          0 :                 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
    3736                 :            :         else
    3737                 :          0 :                 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
    3738                 :            : }
    3739                 :            : 
    3740                 :            : #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
    3741                 :            : #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
    3742                 :            : #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
    3743                 :            : 
    3744                 :          0 : static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
    3745                 :            : {
    3746                 :          0 :         u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
    3747                 :          0 :         struct nfs4_server_caps_arg args = {
    3748                 :            :                 .fhandle = fhandle,
    3749                 :            :                 .bitmask = bitmask,
    3750                 :            :         };
    3751                 :          0 :         struct nfs4_server_caps_res res = {};
    3752                 :          0 :         struct rpc_message msg = {
    3753                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
    3754                 :            :                 .rpc_argp = &args,
    3755                 :            :                 .rpc_resp = &res,
    3756                 :            :         };
    3757                 :            :         int status;
    3758                 :            :         int i;
    3759                 :            : 
    3760                 :          0 :         bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
    3761                 :            :                      FATTR4_WORD0_FH_EXPIRE_TYPE |
    3762                 :            :                      FATTR4_WORD0_LINK_SUPPORT |
    3763                 :            :                      FATTR4_WORD0_SYMLINK_SUPPORT |
    3764                 :            :                      FATTR4_WORD0_ACLSUPPORT;
    3765                 :          0 :         if (minorversion)
    3766                 :          0 :                 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
    3767                 :            : 
    3768                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    3769                 :          0 :         if (status == 0) {
    3770                 :            :                 /* Sanity check the server answers */
    3771                 :          0 :                 switch (minorversion) {
    3772                 :            :                 case 0:
    3773                 :          0 :                         res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
    3774                 :          0 :                         res.attr_bitmask[2] = 0;
    3775                 :          0 :                         break;
    3776                 :            :                 case 1:
    3777                 :          0 :                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
    3778                 :          0 :                         break;
    3779                 :            :                 case 2:
    3780                 :          0 :                         res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
    3781                 :            :                 }
    3782                 :          0 :                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
    3783                 :          0 :                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
    3784                 :            :                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
    3785                 :            :                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
    3786                 :            :                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
    3787                 :            :                                 NFS_CAP_CTIME|NFS_CAP_MTIME|
    3788                 :            :                                 NFS_CAP_SECURITY_LABEL);
    3789                 :          0 :                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
    3790                 :          0 :                                 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
    3791                 :          0 :                         server->caps |= NFS_CAP_ACLS;
    3792                 :          0 :                 if (res.has_links != 0)
    3793                 :          0 :                         server->caps |= NFS_CAP_HARDLINKS;
    3794                 :          0 :                 if (res.has_symlinks != 0)
    3795                 :          0 :                         server->caps |= NFS_CAP_SYMLINKS;
    3796                 :          0 :                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
    3797                 :          0 :                         server->caps |= NFS_CAP_FILEID;
    3798                 :          0 :                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
    3799                 :          0 :                         server->caps |= NFS_CAP_MODE;
    3800                 :          0 :                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
    3801                 :          0 :                         server->caps |= NFS_CAP_NLINK;
    3802                 :          0 :                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
    3803                 :          0 :                         server->caps |= NFS_CAP_OWNER;
    3804                 :          0 :                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
    3805                 :          0 :                         server->caps |= NFS_CAP_OWNER_GROUP;
    3806                 :          0 :                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
    3807                 :          0 :                         server->caps |= NFS_CAP_ATIME;
    3808                 :          0 :                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
    3809                 :          0 :                         server->caps |= NFS_CAP_CTIME;
    3810                 :          0 :                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
    3811                 :          0 :                         server->caps |= NFS_CAP_MTIME;
    3812                 :            : #ifdef CONFIG_NFS_V4_SECURITY_LABEL
    3813                 :          0 :                 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
    3814                 :          0 :                         server->caps |= NFS_CAP_SECURITY_LABEL;
    3815                 :            : #endif
    3816                 :          0 :                 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
    3817                 :            :                                 sizeof(server->attr_bitmask));
    3818                 :          0 :                 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
    3819                 :            : 
    3820                 :          0 :                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
    3821                 :          0 :                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
    3822                 :          0 :                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
    3823                 :          0 :                 server->cache_consistency_bitmask[2] = 0;
    3824                 :            : 
    3825                 :            :                 /* Avoid a regression due to buggy server */
    3826                 :          0 :                 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
    3827                 :          0 :                         res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
    3828                 :          0 :                 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
    3829                 :            :                         sizeof(server->exclcreat_bitmask));
    3830                 :            : 
    3831                 :          0 :                 server->acl_bitmask = res.acl_bitmask;
    3832                 :          0 :                 server->fh_expire_type = res.fh_expire_type;
    3833                 :            :         }
    3834                 :            : 
    3835                 :          0 :         return status;
    3836                 :            : }
    3837                 :            : 
    3838                 :          0 : int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
    3839                 :            : {
    3840                 :          0 :         struct nfs4_exception exception = {
    3841                 :            :                 .interruptible = true,
    3842                 :            :         };
    3843                 :            :         int err;
    3844                 :            :         do {
    3845                 :          0 :                 err = nfs4_handle_exception(server,
    3846                 :            :                                 _nfs4_server_capabilities(server, fhandle),
    3847                 :            :                                 &exception);
    3848                 :          0 :         } while (exception.retry);
    3849                 :          0 :         return err;
    3850                 :            : }
    3851                 :            : 
    3852                 :          0 : static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
    3853                 :            :                 struct nfs_fsinfo *info)
    3854                 :            : {
    3855                 :            :         u32 bitmask[3];
    3856                 :          0 :         struct nfs4_lookup_root_arg args = {
    3857                 :            :                 .bitmask = bitmask,
    3858                 :            :         };
    3859                 :          0 :         struct nfs4_lookup_res res = {
    3860                 :            :                 .server = server,
    3861                 :          0 :                 .fattr = info->fattr,
    3862                 :            :                 .fh = fhandle,
    3863                 :            :         };
    3864                 :          0 :         struct rpc_message msg = {
    3865                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
    3866                 :            :                 .rpc_argp = &args,
    3867                 :            :                 .rpc_resp = &res,
    3868                 :            :         };
    3869                 :            : 
    3870                 :          0 :         bitmask[0] = nfs4_fattr_bitmap[0];
    3871                 :          0 :         bitmask[1] = nfs4_fattr_bitmap[1];
    3872                 :            :         /*
    3873                 :            :          * Process the label in the upcoming getfattr
    3874                 :            :          */
    3875                 :          0 :         bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
    3876                 :            : 
    3877                 :          0 :         nfs_fattr_init(info->fattr);
    3878                 :          0 :         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    3879                 :            : }
    3880                 :            : 
    3881                 :          0 : static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
    3882                 :            :                 struct nfs_fsinfo *info)
    3883                 :            : {
    3884                 :          0 :         struct nfs4_exception exception = {
    3885                 :            :                 .interruptible = true,
    3886                 :            :         };
    3887                 :            :         int err;
    3888                 :            :         do {
    3889                 :          0 :                 err = _nfs4_lookup_root(server, fhandle, info);
    3890                 :          0 :                 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
    3891                 :          0 :                 switch (err) {
    3892                 :            :                 case 0:
    3893                 :            :                 case -NFS4ERR_WRONGSEC:
    3894                 :            :                         goto out;
    3895                 :            :                 default:
    3896                 :          0 :                         err = nfs4_handle_exception(server, err, &exception);
    3897                 :            :                 }
    3898                 :          0 :         } while (exception.retry);
    3899                 :            : out:
    3900                 :          0 :         return err;
    3901                 :            : }
    3902                 :            : 
    3903                 :          0 : static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
    3904                 :            :                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
    3905                 :            : {
    3906                 :          0 :         struct rpc_auth_create_args auth_args = {
    3907                 :            :                 .pseudoflavor = flavor,
    3908                 :            :         };
    3909                 :            :         struct rpc_auth *auth;
    3910                 :            : 
    3911                 :          0 :         auth = rpcauth_create(&auth_args, server->client);
    3912                 :          0 :         if (IS_ERR(auth))
    3913                 :            :                 return -EACCES;
    3914                 :          0 :         return nfs4_lookup_root(server, fhandle, info);
    3915                 :            : }
    3916                 :            : 
    3917                 :            : /*
    3918                 :            :  * Retry pseudoroot lookup with various security flavors.  We do this when:
    3919                 :            :  *
    3920                 :            :  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
    3921                 :            :  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
    3922                 :            :  *
    3923                 :            :  * Returns zero on success, or a negative NFS4ERR value, or a
    3924                 :            :  * negative errno value.
    3925                 :            :  */
    3926                 :          0 : static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
    3927                 :            :                               struct nfs_fsinfo *info)
    3928                 :            : {
    3929                 :            :         /* Per 3530bis 15.33.5 */
    3930                 :            :         static const rpc_authflavor_t flav_array[] = {
    3931                 :            :                 RPC_AUTH_GSS_KRB5P,
    3932                 :            :                 RPC_AUTH_GSS_KRB5I,
    3933                 :            :                 RPC_AUTH_GSS_KRB5,
    3934                 :            :                 RPC_AUTH_UNIX,                  /* courtesy */
    3935                 :            :                 RPC_AUTH_NULL,
    3936                 :            :         };
    3937                 :            :         int status = -EPERM;
    3938                 :            :         size_t i;
    3939                 :            : 
    3940                 :          0 :         if (server->auth_info.flavor_len > 0) {
    3941                 :            :                 /* try each flavor specified by user */
    3942                 :          0 :                 for (i = 0; i < server->auth_info.flavor_len; i++) {
    3943                 :          0 :                         status = nfs4_lookup_root_sec(server, fhandle, info,
    3944                 :            :                                                 server->auth_info.flavors[i]);
    3945                 :          0 :                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
    3946                 :          0 :                                 continue;
    3947                 :            :                         break;
    3948                 :            :                 }
    3949                 :            :         } else {
    3950                 :            :                 /* no flavors specified by user, try default list */
    3951                 :          0 :                 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
    3952                 :          0 :                         status = nfs4_lookup_root_sec(server, fhandle, info,
    3953                 :            :                                                       flav_array[i]);
    3954                 :          0 :                         if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
    3955                 :          0 :                                 continue;
    3956                 :            :                         break;
    3957                 :            :                 }
    3958                 :            :         }
    3959                 :            : 
    3960                 :            :         /*
    3961                 :            :          * -EACCES could mean that the user doesn't have correct permissions
    3962                 :            :          * to access the mount.  It could also mean that we tried to mount
    3963                 :            :          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
    3964                 :            :          * existing mount programs don't handle -EACCES very well so it should
    3965                 :            :          * be mapped to -EPERM instead.
    3966                 :            :          */
    3967                 :          0 :         if (status == -EACCES)
    3968                 :            :                 status = -EPERM;
    3969                 :          0 :         return status;
    3970                 :            : }
    3971                 :            : 
    3972                 :            : /**
    3973                 :            :  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
    3974                 :            :  * @server: initialized nfs_server handle
    3975                 :            :  * @fhandle: we fill in the pseudo-fs root file handle
    3976                 :            :  * @info: we fill in an FSINFO struct
    3977                 :            :  * @auth_probe: probe the auth flavours
    3978                 :            :  *
    3979                 :            :  * Returns zero on success, or a negative errno.
    3980                 :            :  */
    3981                 :          0 : int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
    3982                 :            :                          struct nfs_fsinfo *info,
    3983                 :            :                          bool auth_probe)
    3984                 :            : {
    3985                 :            :         int status = 0;
    3986                 :            : 
    3987                 :          0 :         if (!auth_probe)
    3988                 :          0 :                 status = nfs4_lookup_root(server, fhandle, info);
    3989                 :            : 
    3990                 :          0 :         if (auth_probe || status == NFS4ERR_WRONGSEC)
    3991                 :          0 :                 status = server->nfs_client->cl_mvops->find_root_sec(server,
    3992                 :            :                                 fhandle, info);
    3993                 :            : 
    3994                 :          0 :         if (status == 0)
    3995                 :          0 :                 status = nfs4_server_capabilities(server, fhandle);
    3996                 :          0 :         if (status == 0)
    3997                 :          0 :                 status = nfs4_do_fsinfo(server, fhandle, info);
    3998                 :            : 
    3999                 :          0 :         return nfs4_map_errors(status);
    4000                 :            : }
    4001                 :            : 
    4002                 :          0 : static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
    4003                 :            :                               struct nfs_fsinfo *info)
    4004                 :            : {
    4005                 :            :         int error;
    4006                 :          0 :         struct nfs_fattr *fattr = info->fattr;
    4007                 :            :         struct nfs4_label *label = NULL;
    4008                 :            : 
    4009                 :          0 :         error = nfs4_server_capabilities(server, mntfh);
    4010                 :          0 :         if (error < 0) {
    4011                 :            :                 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
    4012                 :            :                 return error;
    4013                 :            :         }
    4014                 :            : 
    4015                 :          0 :         label = nfs4_label_alloc(server, GFP_KERNEL);
    4016                 :          0 :         if (IS_ERR(label))
    4017                 :          0 :                 return PTR_ERR(label);
    4018                 :            : 
    4019                 :          0 :         error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
    4020                 :          0 :         if (error < 0) {
    4021                 :            :                 dprintk("nfs4_get_root: getattr error = %d\n", -error);
    4022                 :            :                 goto err_free_label;
    4023                 :            :         }
    4024                 :            : 
    4025                 :          0 :         if (fattr->valid & NFS_ATTR_FATTR_FSID &&
    4026                 :            :             !nfs_fsid_equal(&server->fsid, &fattr->fsid))
    4027                 :          0 :                 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
    4028                 :            : 
    4029                 :            : err_free_label:
    4030                 :          0 :         nfs4_label_free(label);
    4031                 :            : 
    4032                 :          0 :         return error;
    4033                 :            : }
    4034                 :            : 
    4035                 :            : /*
    4036                 :            :  * Get locations and (maybe) other attributes of a referral.
    4037                 :            :  * Note that we'll actually follow the referral later when
    4038                 :            :  * we detect fsid mismatch in inode revalidation
    4039                 :            :  */
    4040                 :          0 : static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
    4041                 :            :                              const struct qstr *name, struct nfs_fattr *fattr,
    4042                 :            :                              struct nfs_fh *fhandle)
    4043                 :            : {
    4044                 :            :         int status = -ENOMEM;
    4045                 :            :         struct page *page = NULL;
    4046                 :            :         struct nfs4_fs_locations *locations = NULL;
    4047                 :            : 
    4048                 :            :         page = alloc_page(GFP_KERNEL);
    4049                 :          0 :         if (page == NULL)
    4050                 :            :                 goto out;
    4051                 :            :         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
    4052                 :          0 :         if (locations == NULL)
    4053                 :            :                 goto out;
    4054                 :            : 
    4055                 :          0 :         status = nfs4_proc_fs_locations(client, dir, name, locations, page);
    4056                 :          0 :         if (status != 0)
    4057                 :            :                 goto out;
    4058                 :            : 
    4059                 :            :         /*
    4060                 :            :          * If the fsid didn't change, this is a migration event, not a
    4061                 :            :          * referral.  Cause us to drop into the exception handler, which
    4062                 :            :          * will kick off migration recovery.
    4063                 :            :          */
    4064                 :          0 :         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
    4065                 :            :                 dprintk("%s: server did not return a different fsid for"
    4066                 :            :                         " a referral at %s\n", __func__, name->name);
    4067                 :            :                 status = -NFS4ERR_MOVED;
    4068                 :            :                 goto out;
    4069                 :            :         }
    4070                 :            :         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
    4071                 :            :         nfs_fixup_referral_attributes(&locations->fattr);
    4072                 :            : 
    4073                 :            :         /* replace the lookup nfs_fattr with the locations nfs_fattr */
    4074                 :          0 :         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
    4075                 :          0 :         memset(fhandle, 0, sizeof(struct nfs_fh));
    4076                 :            : out:
    4077                 :          0 :         if (page)
    4078                 :          0 :                 __free_page(page);
    4079                 :          0 :         kfree(locations);
    4080                 :          0 :         return status;
    4081                 :            : }
    4082                 :            : 
    4083                 :          0 : static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
    4084                 :            :                                 struct nfs_fattr *fattr, struct nfs4_label *label,
    4085                 :            :                                 struct inode *inode)
    4086                 :            : {
    4087                 :            :         __u32 bitmask[NFS4_BITMASK_SZ];
    4088                 :          0 :         struct nfs4_getattr_arg args = {
    4089                 :            :                 .fh = fhandle,
    4090                 :            :                 .bitmask = bitmask,
    4091                 :            :         };
    4092                 :          0 :         struct nfs4_getattr_res res = {
    4093                 :            :                 .fattr = fattr,
    4094                 :            :                 .label = label,
    4095                 :            :                 .server = server,
    4096                 :            :         };
    4097                 :          0 :         struct rpc_message msg = {
    4098                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
    4099                 :            :                 .rpc_argp = &args,
    4100                 :            :                 .rpc_resp = &res,
    4101                 :            :         };
    4102                 :            : 
    4103                 :          0 :         nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
    4104                 :            : 
    4105                 :          0 :         nfs_fattr_init(fattr);
    4106                 :          0 :         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    4107                 :            : }
    4108                 :            : 
    4109                 :          0 : static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
    4110                 :            :                                 struct nfs_fattr *fattr, struct nfs4_label *label,
    4111                 :            :                                 struct inode *inode)
    4112                 :            : {
    4113                 :          0 :         struct nfs4_exception exception = {
    4114                 :            :                 .interruptible = true,
    4115                 :            :         };
    4116                 :            :         int err;
    4117                 :            :         do {
    4118                 :          0 :                 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
    4119                 :          0 :                 trace_nfs4_getattr(server, fhandle, fattr, err);
    4120                 :          0 :                 err = nfs4_handle_exception(server, err,
    4121                 :            :                                 &exception);
    4122                 :          0 :         } while (exception.retry);
    4123                 :          0 :         return err;
    4124                 :            : }
    4125                 :            : 
    4126                 :            : /* 
    4127                 :            :  * The file is not closed if it is opened due to the a request to change
    4128                 :            :  * the size of the file. The open call will not be needed once the
    4129                 :            :  * VFS layer lookup-intents are implemented.
    4130                 :            :  *
    4131                 :            :  * Close is called when the inode is destroyed.
    4132                 :            :  * If we haven't opened the file for O_WRONLY, we
    4133                 :            :  * need to in the size_change case to obtain a stateid.
    4134                 :            :  *
    4135                 :            :  * Got race?
    4136                 :            :  * Because OPEN is always done by name in nfsv4, it is
    4137                 :            :  * possible that we opened a different file by the same
    4138                 :            :  * name.  We can recognize this race condition, but we
    4139                 :            :  * can't do anything about it besides returning an error.
    4140                 :            :  *
    4141                 :            :  * This will be fixed with VFS changes (lookup-intent).
    4142                 :            :  */
    4143                 :            : static int
    4144                 :          0 : nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
    4145                 :            :                   struct iattr *sattr)
    4146                 :            : {
    4147                 :            :         struct inode *inode = d_inode(dentry);
    4148                 :            :         const struct cred *cred = NULL;
    4149                 :            :         struct nfs_open_context *ctx = NULL;
    4150                 :            :         struct nfs4_label *label = NULL;
    4151                 :            :         int status;
    4152                 :            : 
    4153                 :          0 :         if (pnfs_ld_layoutret_on_setattr(inode) &&
    4154                 :          0 :             sattr->ia_valid & ATTR_SIZE &&
    4155                 :          0 :             sattr->ia_size < i_size_read(inode))
    4156                 :          0 :                 pnfs_commit_and_return_layout(inode);
    4157                 :            : 
    4158                 :          0 :         nfs_fattr_init(fattr);
    4159                 :            :         
    4160                 :            :         /* Deal with open(O_TRUNC) */
    4161                 :          0 :         if (sattr->ia_valid & ATTR_OPEN)
    4162                 :          0 :                 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
    4163                 :            : 
    4164                 :            :         /* Optimization: if the end result is no change, don't RPC */
    4165                 :          0 :         if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
    4166                 :            :                 return 0;
    4167                 :            : 
    4168                 :            :         /* Search for an existing open(O_WRITE) file */
    4169                 :          0 :         if (sattr->ia_valid & ATTR_FILE) {
    4170                 :            : 
    4171                 :          0 :                 ctx = nfs_file_open_context(sattr->ia_file);
    4172                 :          0 :                 if (ctx)
    4173                 :          0 :                         cred = ctx->cred;
    4174                 :            :         }
    4175                 :            : 
    4176                 :          0 :         label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
    4177                 :          0 :         if (IS_ERR(label))
    4178                 :          0 :                 return PTR_ERR(label);
    4179                 :            : 
    4180                 :            :         /* Return any delegations if we're going to change ACLs */
    4181                 :          0 :         if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
    4182                 :          0 :                 nfs4_inode_make_writeable(inode);
    4183                 :            : 
    4184                 :          0 :         status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
    4185                 :          0 :         if (status == 0) {
    4186                 :          0 :                 nfs_setattr_update_inode(inode, sattr, fattr);
    4187                 :          0 :                 nfs_setsecurity(inode, fattr, label);
    4188                 :            :         }
    4189                 :          0 :         nfs4_label_free(label);
    4190                 :          0 :         return status;
    4191                 :            : }
    4192                 :            : 
    4193                 :          0 : static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
    4194                 :            :                 const struct qstr *name, struct nfs_fh *fhandle,
    4195                 :            :                 struct nfs_fattr *fattr, struct nfs4_label *label)
    4196                 :            : {
    4197                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    4198                 :            :         int                    status;
    4199                 :          0 :         struct nfs4_lookup_arg args = {
    4200                 :          0 :                 .bitmask = server->attr_bitmask,
    4201                 :            :                 .dir_fh = NFS_FH(dir),
    4202                 :            :                 .name = name,
    4203                 :            :         };
    4204                 :          0 :         struct nfs4_lookup_res res = {
    4205                 :            :                 .server = server,
    4206                 :            :                 .fattr = fattr,
    4207                 :            :                 .label = label,
    4208                 :            :                 .fh = fhandle,
    4209                 :            :         };
    4210                 :          0 :         struct rpc_message msg = {
    4211                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
    4212                 :            :                 .rpc_argp = &args,
    4213                 :            :                 .rpc_resp = &res,
    4214                 :            :         };
    4215                 :            : 
    4216                 :          0 :         args.bitmask = nfs4_bitmask(server, label);
    4217                 :            : 
    4218                 :          0 :         nfs_fattr_init(fattr);
    4219                 :            : 
    4220                 :            :         dprintk("NFS call  lookup %s\n", name->name);
    4221                 :            :         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
    4222                 :            :         dprintk("NFS reply lookup: %d\n", status);
    4223                 :          0 :         return status;
    4224                 :            : }
    4225                 :            : 
    4226                 :            : static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
    4227                 :            : {
    4228                 :          0 :         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
    4229                 :            :                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
    4230                 :          0 :         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
    4231                 :          0 :         fattr->nlink = 2;
    4232                 :            : }
    4233                 :            : 
    4234                 :          0 : static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
    4235                 :            :                                    const struct qstr *name, struct nfs_fh *fhandle,
    4236                 :            :                                    struct nfs_fattr *fattr, struct nfs4_label *label)
    4237                 :            : {
    4238                 :          0 :         struct nfs4_exception exception = {
    4239                 :            :                 .interruptible = true,
    4240                 :            :         };
    4241                 :          0 :         struct rpc_clnt *client = *clnt;
    4242                 :            :         int err;
    4243                 :            :         do {
    4244                 :          0 :                 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
    4245                 :          0 :                 trace_nfs4_lookup(dir, name, err);
    4246                 :          0 :                 switch (err) {
    4247                 :            :                 case -NFS4ERR_BADNAME:
    4248                 :            :                         err = -ENOENT;
    4249                 :            :                         goto out;
    4250                 :            :                 case -NFS4ERR_MOVED:
    4251                 :          0 :                         err = nfs4_get_referral(client, dir, name, fattr, fhandle);
    4252                 :          0 :                         if (err == -NFS4ERR_MOVED)
    4253                 :          0 :                                 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
    4254                 :            :                         goto out;
    4255                 :            :                 case -NFS4ERR_WRONGSEC:
    4256                 :            :                         err = -EPERM;
    4257                 :          0 :                         if (client != *clnt)
    4258                 :            :                                 goto out;
    4259                 :          0 :                         client = nfs4_negotiate_security(client, dir, name);
    4260                 :          0 :                         if (IS_ERR(client))
    4261                 :          0 :                                 return PTR_ERR(client);
    4262                 :            : 
    4263                 :          0 :                         exception.retry = 1;
    4264                 :          0 :                         break;
    4265                 :            :                 default:
    4266                 :          0 :                         err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
    4267                 :            :                 }
    4268                 :          0 :         } while (exception.retry);
    4269                 :            : 
    4270                 :            : out:
    4271                 :          0 :         if (err == 0)
    4272                 :          0 :                 *clnt = client;
    4273                 :          0 :         else if (client != *clnt)
    4274                 :          0 :                 rpc_shutdown_client(client);
    4275                 :            : 
    4276                 :          0 :         return err;
    4277                 :            : }
    4278                 :            : 
    4279                 :          0 : static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
    4280                 :            :                             struct nfs_fh *fhandle, struct nfs_fattr *fattr,
    4281                 :            :                             struct nfs4_label *label)
    4282                 :            : {
    4283                 :            :         int status;
    4284                 :          0 :         struct rpc_clnt *client = NFS_CLIENT(dir);
    4285                 :            : 
    4286                 :          0 :         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
    4287                 :          0 :         if (client != NFS_CLIENT(dir)) {
    4288                 :          0 :                 rpc_shutdown_client(client);
    4289                 :            :                 nfs_fixup_secinfo_attributes(fattr);
    4290                 :            :         }
    4291                 :          0 :         return status;
    4292                 :            : }
    4293                 :            : 
    4294                 :            : struct rpc_clnt *
    4295                 :          0 : nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
    4296                 :            :                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
    4297                 :            : {
    4298                 :          0 :         struct rpc_clnt *client = NFS_CLIENT(dir);
    4299                 :            :         int status;
    4300                 :            : 
    4301                 :          0 :         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
    4302                 :          0 :         if (status < 0)
    4303                 :          0 :                 return ERR_PTR(status);
    4304                 :          0 :         return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
    4305                 :            : }
    4306                 :            : 
    4307                 :          0 : static int _nfs4_proc_lookupp(struct inode *inode,
    4308                 :            :                 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
    4309                 :            :                 struct nfs4_label *label)
    4310                 :            : {
    4311                 :            :         struct rpc_clnt *clnt = NFS_CLIENT(inode);
    4312                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    4313                 :            :         int                    status;
    4314                 :          0 :         struct nfs4_lookupp_arg args = {
    4315                 :          0 :                 .bitmask = server->attr_bitmask,
    4316                 :            :                 .fh = NFS_FH(inode),
    4317                 :            :         };
    4318                 :          0 :         struct nfs4_lookupp_res res = {
    4319                 :            :                 .server = server,
    4320                 :            :                 .fattr = fattr,
    4321                 :            :                 .label = label,
    4322                 :            :                 .fh = fhandle,
    4323                 :            :         };
    4324                 :          0 :         struct rpc_message msg = {
    4325                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
    4326                 :            :                 .rpc_argp = &args,
    4327                 :            :                 .rpc_resp = &res,
    4328                 :            :         };
    4329                 :            : 
    4330                 :          0 :         args.bitmask = nfs4_bitmask(server, label);
    4331                 :            : 
    4332                 :          0 :         nfs_fattr_init(fattr);
    4333                 :            : 
    4334                 :            :         dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
    4335                 :            :         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
    4336                 :            :                                 &res.seq_res, 0);
    4337                 :            :         dprintk("NFS reply lookupp: %d\n", status);
    4338                 :          0 :         return status;
    4339                 :            : }
    4340                 :            : 
    4341                 :          0 : static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
    4342                 :            :                              struct nfs_fattr *fattr, struct nfs4_label *label)
    4343                 :            : {
    4344                 :          0 :         struct nfs4_exception exception = {
    4345                 :            :                 .interruptible = true,
    4346                 :            :         };
    4347                 :            :         int err;
    4348                 :            :         do {
    4349                 :          0 :                 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
    4350                 :          0 :                 trace_nfs4_lookupp(inode, err);
    4351                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
    4352                 :            :                                 &exception);
    4353                 :          0 :         } while (exception.retry);
    4354                 :          0 :         return err;
    4355                 :            : }
    4356                 :            : 
    4357                 :          0 : static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
    4358                 :            : {
    4359                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    4360                 :          0 :         struct nfs4_accessargs args = {
    4361                 :            :                 .fh = NFS_FH(inode),
    4362                 :          0 :                 .access = entry->mask,
    4363                 :            :         };
    4364                 :          0 :         struct nfs4_accessres res = {
    4365                 :            :                 .server = server,
    4366                 :            :         };
    4367                 :          0 :         struct rpc_message msg = {
    4368                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
    4369                 :            :                 .rpc_argp = &args,
    4370                 :            :                 .rpc_resp = &res,
    4371                 :          0 :                 .rpc_cred = entry->cred,
    4372                 :            :         };
    4373                 :            :         int status = 0;
    4374                 :            : 
    4375                 :          0 :         if (!nfs4_have_delegation(inode, FMODE_READ)) {
    4376                 :          0 :                 res.fattr = nfs_alloc_fattr();
    4377                 :          0 :                 if (res.fattr == NULL)
    4378                 :            :                         return -ENOMEM;
    4379                 :          0 :                 args.bitmask = server->cache_consistency_bitmask;
    4380                 :            :         }
    4381                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    4382                 :          0 :         if (!status) {
    4383                 :          0 :                 nfs_access_set_mask(entry, res.access);
    4384                 :          0 :                 if (res.fattr)
    4385                 :          0 :                         nfs_refresh_inode(inode, res.fattr);
    4386                 :            :         }
    4387                 :          0 :         nfs_free_fattr(res.fattr);
    4388                 :          0 :         return status;
    4389                 :            : }
    4390                 :            : 
    4391                 :          0 : static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
    4392                 :            : {
    4393                 :          0 :         struct nfs4_exception exception = {
    4394                 :            :                 .interruptible = true,
    4395                 :            :         };
    4396                 :            :         int err;
    4397                 :            :         do {
    4398                 :          0 :                 err = _nfs4_proc_access(inode, entry);
    4399                 :          0 :                 trace_nfs4_access(inode, err);
    4400                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
    4401                 :            :                                 &exception);
    4402                 :          0 :         } while (exception.retry);
    4403                 :          0 :         return err;
    4404                 :            : }
    4405                 :            : 
    4406                 :            : /*
    4407                 :            :  * TODO: For the time being, we don't try to get any attributes
    4408                 :            :  * along with any of the zero-copy operations READ, READDIR,
    4409                 :            :  * READLINK, WRITE.
    4410                 :            :  *
    4411                 :            :  * In the case of the first three, we want to put the GETATTR
    4412                 :            :  * after the read-type operation -- this is because it is hard
    4413                 :            :  * to predict the length of a GETATTR response in v4, and thus
    4414                 :            :  * align the READ data correctly.  This means that the GETATTR
    4415                 :            :  * may end up partially falling into the page cache, and we should
    4416                 :            :  * shift it into the 'tail' of the xdr_buf before processing.
    4417                 :            :  * To do this efficiently, we need to know the total length
    4418                 :            :  * of data received, which doesn't seem to be available outside
    4419                 :            :  * of the RPC layer.
    4420                 :            :  *
    4421                 :            :  * In the case of WRITE, we also want to put the GETATTR after
    4422                 :            :  * the operation -- in this case because we want to make sure
    4423                 :            :  * we get the post-operation mtime and size.
    4424                 :            :  *
    4425                 :            :  * Both of these changes to the XDR layer would in fact be quite
    4426                 :            :  * minor, but I decided to leave them for a subsequent patch.
    4427                 :            :  */
    4428                 :          0 : static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
    4429                 :            :                 unsigned int pgbase, unsigned int pglen)
    4430                 :            : {
    4431                 :          0 :         struct nfs4_readlink args = {
    4432                 :            :                 .fh       = NFS_FH(inode),
    4433                 :            :                 .pgbase   = pgbase,
    4434                 :            :                 .pglen    = pglen,
    4435                 :            :                 .pages    = &page,
    4436                 :            :         };
    4437                 :            :         struct nfs4_readlink_res res;
    4438                 :          0 :         struct rpc_message msg = {
    4439                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
    4440                 :            :                 .rpc_argp = &args,
    4441                 :            :                 .rpc_resp = &res,
    4442                 :            :         };
    4443                 :            : 
    4444                 :          0 :         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
    4445                 :            : }
    4446                 :            : 
    4447                 :          0 : static int nfs4_proc_readlink(struct inode *inode, struct page *page,
    4448                 :            :                 unsigned int pgbase, unsigned int pglen)
    4449                 :            : {
    4450                 :          0 :         struct nfs4_exception exception = {
    4451                 :            :                 .interruptible = true,
    4452                 :            :         };
    4453                 :            :         int err;
    4454                 :            :         do {
    4455                 :          0 :                 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
    4456                 :          0 :                 trace_nfs4_readlink(inode, err);
    4457                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
    4458                 :            :                                 &exception);
    4459                 :          0 :         } while (exception.retry);
    4460                 :          0 :         return err;
    4461                 :            : }
    4462                 :            : 
    4463                 :            : /*
    4464                 :            :  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
    4465                 :            :  */
    4466                 :            : static int
    4467                 :          0 : nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
    4468                 :            :                  int flags)
    4469                 :            : {
    4470                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    4471                 :            :         struct nfs4_label l, *ilabel = NULL;
    4472                 :            :         struct nfs_open_context *ctx;
    4473                 :            :         struct nfs4_state *state;
    4474                 :            :         int status = 0;
    4475                 :            : 
    4476                 :          0 :         ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
    4477                 :          0 :         if (IS_ERR(ctx))
    4478                 :          0 :                 return PTR_ERR(ctx);
    4479                 :            : 
    4480                 :          0 :         ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
    4481                 :            : 
    4482                 :          0 :         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
    4483                 :          0 :                 sattr->ia_mode &= ~current_umask();
    4484                 :          0 :         state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
    4485                 :          0 :         if (IS_ERR(state)) {
    4486                 :            :                 status = PTR_ERR(state);
    4487                 :          0 :                 goto out;
    4488                 :            :         }
    4489                 :            : out:
    4490                 :            :         nfs4_label_release_security(ilabel);
    4491                 :          0 :         put_nfs_open_context(ctx);
    4492                 :          0 :         return status;
    4493                 :            : }
    4494                 :            : 
    4495                 :            : static int
    4496                 :          0 : _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
    4497                 :            : {
    4498                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    4499                 :          0 :         struct nfs_removeargs args = {
    4500                 :            :                 .fh = NFS_FH(dir),
    4501                 :            :                 .name = *name,
    4502                 :            :         };
    4503                 :          0 :         struct nfs_removeres res = {
    4504                 :            :                 .server = server,
    4505                 :            :         };
    4506                 :          0 :         struct rpc_message msg = {
    4507                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
    4508                 :            :                 .rpc_argp = &args,
    4509                 :            :                 .rpc_resp = &res,
    4510                 :            :         };
    4511                 :          0 :         unsigned long timestamp = jiffies;
    4512                 :            :         int status;
    4513                 :            : 
    4514                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
    4515                 :          0 :         if (status == 0) {
    4516                 :            :                 spin_lock(&dir->i_lock);
    4517                 :          0 :                 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
    4518                 :            :                 /* Removing a directory decrements nlink in the parent */
    4519                 :          0 :                 if (ftype == NF4DIR && dir->i_nlink > 2)
    4520                 :            :                         nfs4_dec_nlink_locked(dir);
    4521                 :            :                 spin_unlock(&dir->i_lock);
    4522                 :            :         }
    4523                 :          0 :         return status;
    4524                 :            : }
    4525                 :            : 
    4526                 :          0 : static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
    4527                 :            : {
    4528                 :          0 :         struct nfs4_exception exception = {
    4529                 :            :                 .interruptible = true,
    4530                 :            :         };
    4531                 :            :         struct inode *inode = d_inode(dentry);
    4532                 :            :         int err;
    4533                 :            : 
    4534                 :          0 :         if (inode) {
    4535                 :          0 :                 if (inode->i_nlink == 1)
    4536                 :          0 :                         nfs4_inode_return_delegation(inode);
    4537                 :            :                 else
    4538                 :          0 :                         nfs4_inode_make_writeable(inode);
    4539                 :            :         }
    4540                 :            :         do {
    4541                 :          0 :                 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
    4542                 :          0 :                 trace_nfs4_remove(dir, &dentry->d_name, err);
    4543                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
    4544                 :            :                                 &exception);
    4545                 :          0 :         } while (exception.retry);
    4546                 :          0 :         return err;
    4547                 :            : }
    4548                 :            : 
    4549                 :          0 : static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
    4550                 :            : {
    4551                 :          0 :         struct nfs4_exception exception = {
    4552                 :            :                 .interruptible = true,
    4553                 :            :         };
    4554                 :            :         int err;
    4555                 :            : 
    4556                 :            :         do {
    4557                 :          0 :                 err = _nfs4_proc_remove(dir, name, NF4DIR);
    4558                 :          0 :                 trace_nfs4_remove(dir, name, err);
    4559                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
    4560                 :            :                                 &exception);
    4561                 :          0 :         } while (exception.retry);
    4562                 :          0 :         return err;
    4563                 :            : }
    4564                 :            : 
    4565                 :          0 : static void nfs4_proc_unlink_setup(struct rpc_message *msg,
    4566                 :            :                 struct dentry *dentry,
    4567                 :            :                 struct inode *inode)
    4568                 :            : {
    4569                 :          0 :         struct nfs_removeargs *args = msg->rpc_argp;
    4570                 :          0 :         struct nfs_removeres *res = msg->rpc_resp;
    4571                 :            : 
    4572                 :          0 :         res->server = NFS_SB(dentry->d_sb);
    4573                 :          0 :         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
    4574                 :            :         nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
    4575                 :            : 
    4576                 :          0 :         nfs_fattr_init(res->dir_attr);
    4577                 :            : 
    4578                 :          0 :         if (inode)
    4579                 :          0 :                 nfs4_inode_return_delegation(inode);
    4580                 :          0 : }
    4581                 :            : 
    4582                 :          0 : static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
    4583                 :            : {
    4584                 :          0 :         nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
    4585                 :            :                         &data->args.seq_args,
    4586                 :            :                         &data->res.seq_res,
    4587                 :            :                         task);
    4588                 :          0 : }
    4589                 :            : 
    4590                 :          0 : static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
    4591                 :            : {
    4592                 :          0 :         struct nfs_unlinkdata *data = task->tk_calldata;
    4593                 :            :         struct nfs_removeres *res = &data->res;
    4594                 :            : 
    4595                 :          0 :         if (!nfs4_sequence_done(task, &res->seq_res))
    4596                 :            :                 return 0;
    4597                 :          0 :         if (nfs4_async_handle_error(task, res->server, NULL,
    4598                 :            :                                     &data->timeout) == -EAGAIN)
    4599                 :            :                 return 0;
    4600                 :          0 :         if (task->tk_status == 0)
    4601                 :          0 :                 update_changeattr(dir, &res->cinfo,
    4602                 :          0 :                                 res->dir_attr->time_start, 0);
    4603                 :            :         return 1;
    4604                 :            : }
    4605                 :            : 
    4606                 :          0 : static void nfs4_proc_rename_setup(struct rpc_message *msg,
    4607                 :            :                 struct dentry *old_dentry,
    4608                 :            :                 struct dentry *new_dentry)
    4609                 :            : {
    4610                 :          0 :         struct nfs_renameargs *arg = msg->rpc_argp;
    4611                 :          0 :         struct nfs_renameres *res = msg->rpc_resp;
    4612                 :            :         struct inode *old_inode = d_inode(old_dentry);
    4613                 :            :         struct inode *new_inode = d_inode(new_dentry);
    4614                 :            : 
    4615                 :          0 :         if (old_inode)
    4616                 :          0 :                 nfs4_inode_make_writeable(old_inode);
    4617                 :          0 :         if (new_inode)
    4618                 :          0 :                 nfs4_inode_return_delegation(new_inode);
    4619                 :          0 :         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
    4620                 :          0 :         res->server = NFS_SB(old_dentry->d_sb);
    4621                 :            :         nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
    4622                 :          0 : }
    4623                 :            : 
    4624                 :          0 : static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
    4625                 :            : {
    4626                 :          0 :         nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
    4627                 :            :                         &data->args.seq_args,
    4628                 :            :                         &data->res.seq_res,
    4629                 :            :                         task);
    4630                 :          0 : }
    4631                 :            : 
    4632                 :          0 : static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
    4633                 :            :                                  struct inode *new_dir)
    4634                 :            : {
    4635                 :          0 :         struct nfs_renamedata *data = task->tk_calldata;
    4636                 :            :         struct nfs_renameres *res = &data->res;
    4637                 :            : 
    4638                 :          0 :         if (!nfs4_sequence_done(task, &res->seq_res))
    4639                 :            :                 return 0;
    4640                 :          0 :         if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
    4641                 :            :                 return 0;
    4642                 :            : 
    4643                 :          0 :         if (task->tk_status == 0) {
    4644                 :          0 :                 if (new_dir != old_dir) {
    4645                 :            :                         /* Note: If we moved a directory, nlink will change */
    4646                 :          0 :                         update_changeattr(old_dir, &res->old_cinfo,
    4647                 :          0 :                                         res->old_fattr->time_start,
    4648                 :            :                                         NFS_INO_INVALID_OTHER);
    4649                 :          0 :                         update_changeattr(new_dir, &res->new_cinfo,
    4650                 :          0 :                                         res->new_fattr->time_start,
    4651                 :            :                                         NFS_INO_INVALID_OTHER);
    4652                 :            :                 } else
    4653                 :          0 :                         update_changeattr(old_dir, &res->old_cinfo,
    4654                 :          0 :                                         res->old_fattr->time_start,
    4655                 :            :                                         0);
    4656                 :            :         }
    4657                 :            :         return 1;
    4658                 :            : }
    4659                 :            : 
    4660                 :          0 : static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
    4661                 :            : {
    4662                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    4663                 :            :         __u32 bitmask[NFS4_BITMASK_SZ];
    4664                 :          0 :         struct nfs4_link_arg arg = {
    4665                 :            :                 .fh     = NFS_FH(inode),
    4666                 :            :                 .dir_fh = NFS_FH(dir),
    4667                 :            :                 .name   = name,
    4668                 :            :                 .bitmask = bitmask,
    4669                 :            :         };
    4670                 :          0 :         struct nfs4_link_res res = {
    4671                 :            :                 .server = server,
    4672                 :            :                 .label = NULL,
    4673                 :            :         };
    4674                 :          0 :         struct rpc_message msg = {
    4675                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
    4676                 :            :                 .rpc_argp = &arg,
    4677                 :            :                 .rpc_resp = &res,
    4678                 :            :         };
    4679                 :            :         int status = -ENOMEM;
    4680                 :            : 
    4681                 :          0 :         res.fattr = nfs_alloc_fattr();
    4682                 :          0 :         if (res.fattr == NULL)
    4683                 :            :                 goto out;
    4684                 :            : 
    4685                 :          0 :         res.label = nfs4_label_alloc(server, GFP_KERNEL);
    4686                 :          0 :         if (IS_ERR(res.label)) {
    4687                 :            :                 status = PTR_ERR(res.label);
    4688                 :          0 :                 goto out;
    4689                 :            :         }
    4690                 :            : 
    4691                 :          0 :         nfs4_inode_make_writeable(inode);
    4692                 :          0 :         nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
    4693                 :            : 
    4694                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
    4695                 :          0 :         if (!status) {
    4696                 :          0 :                 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
    4697                 :          0 :                 status = nfs_post_op_update_inode(inode, res.fattr);
    4698                 :          0 :                 if (!status)
    4699                 :          0 :                         nfs_setsecurity(inode, res.fattr, res.label);
    4700                 :            :         }
    4701                 :            : 
    4702                 :            : 
    4703                 :          0 :         nfs4_label_free(res.label);
    4704                 :            : 
    4705                 :            : out:
    4706                 :          0 :         nfs_free_fattr(res.fattr);
    4707                 :          0 :         return status;
    4708                 :            : }
    4709                 :            : 
    4710                 :          0 : static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
    4711                 :            : {
    4712                 :          0 :         struct nfs4_exception exception = {
    4713                 :            :                 .interruptible = true,
    4714                 :            :         };
    4715                 :            :         int err;
    4716                 :            :         do {
    4717                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(inode),
    4718                 :            :                                 _nfs4_proc_link(inode, dir, name),
    4719                 :            :                                 &exception);
    4720                 :          0 :         } while (exception.retry);
    4721                 :          0 :         return err;
    4722                 :            : }
    4723                 :            : 
    4724                 :            : struct nfs4_createdata {
    4725                 :            :         struct rpc_message msg;
    4726                 :            :         struct nfs4_create_arg arg;
    4727                 :            :         struct nfs4_create_res res;
    4728                 :            :         struct nfs_fh fh;
    4729                 :            :         struct nfs_fattr fattr;
    4730                 :            :         struct nfs4_label *label;
    4731                 :            : };
    4732                 :            : 
    4733                 :          0 : static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
    4734                 :            :                 const struct qstr *name, struct iattr *sattr, u32 ftype)
    4735                 :            : {
    4736                 :            :         struct nfs4_createdata *data;
    4737                 :            : 
    4738                 :          0 :         data = kzalloc(sizeof(*data), GFP_KERNEL);
    4739                 :          0 :         if (data != NULL) {
    4740                 :            :                 struct nfs_server *server = NFS_SERVER(dir);
    4741                 :            : 
    4742                 :          0 :                 data->label = nfs4_label_alloc(server, GFP_KERNEL);
    4743                 :          0 :                 if (IS_ERR(data->label))
    4744                 :            :                         goto out_free;
    4745                 :            : 
    4746                 :          0 :                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
    4747                 :          0 :                 data->msg.rpc_argp = &data->arg;
    4748                 :          0 :                 data->msg.rpc_resp = &data->res;
    4749                 :          0 :                 data->arg.dir_fh = NFS_FH(dir);
    4750                 :          0 :                 data->arg.server = server;
    4751                 :          0 :                 data->arg.name = name;
    4752                 :          0 :                 data->arg.attrs = sattr;
    4753                 :          0 :                 data->arg.ftype = ftype;
    4754                 :          0 :                 data->arg.bitmask = nfs4_bitmask(server, data->label);
    4755                 :          0 :                 data->arg.umask = current_umask();
    4756                 :          0 :                 data->res.server = server;
    4757                 :          0 :                 data->res.fh = &data->fh;
    4758                 :          0 :                 data->res.fattr = &data->fattr;
    4759                 :          0 :                 data->res.label = data->label;
    4760                 :          0 :                 nfs_fattr_init(data->res.fattr);
    4761                 :            :         }
    4762                 :          0 :         return data;
    4763                 :            : out_free:
    4764                 :          0 :         kfree(data);
    4765                 :          0 :         return NULL;
    4766                 :            : }
    4767                 :            : 
    4768                 :          0 : static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
    4769                 :            : {
    4770                 :          0 :         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
    4771                 :            :                                     &data->arg.seq_args, &data->res.seq_res, 1);
    4772                 :          0 :         if (status == 0) {
    4773                 :            :                 spin_lock(&dir->i_lock);
    4774                 :          0 :                 update_changeattr_locked(dir, &data->res.dir_cinfo,
    4775                 :          0 :                                 data->res.fattr->time_start, 0);
    4776                 :            :                 /* Creating a directory bumps nlink in the parent */
    4777                 :          0 :                 if (data->arg.ftype == NF4DIR)
    4778                 :            :                         nfs4_inc_nlink_locked(dir);
    4779                 :            :                 spin_unlock(&dir->i_lock);
    4780                 :          0 :                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
    4781                 :            :         }
    4782                 :          0 :         return status;
    4783                 :            : }
    4784                 :            : 
    4785                 :            : static void nfs4_free_createdata(struct nfs4_createdata *data)
    4786                 :            : {
    4787                 :          0 :         nfs4_label_free(data->label);
    4788                 :          0 :         kfree(data);
    4789                 :            : }
    4790                 :            : 
    4791                 :          0 : static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
    4792                 :            :                 struct page *page, unsigned int len, struct iattr *sattr,
    4793                 :            :                 struct nfs4_label *label)
    4794                 :            : {
    4795                 :            :         struct nfs4_createdata *data;
    4796                 :            :         int status = -ENAMETOOLONG;
    4797                 :            : 
    4798                 :          0 :         if (len > NFS4_MAXPATHLEN)
    4799                 :            :                 goto out;
    4800                 :            : 
    4801                 :            :         status = -ENOMEM;
    4802                 :          0 :         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
    4803                 :          0 :         if (data == NULL)
    4804                 :            :                 goto out;
    4805                 :            : 
    4806                 :          0 :         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
    4807                 :          0 :         data->arg.u.symlink.pages = &page;
    4808                 :          0 :         data->arg.u.symlink.len = len;
    4809                 :          0 :         data->arg.label = label;
    4810                 :            :         
    4811                 :          0 :         status = nfs4_do_create(dir, dentry, data);
    4812                 :            : 
    4813                 :            :         nfs4_free_createdata(data);
    4814                 :            : out:
    4815                 :          0 :         return status;
    4816                 :            : }
    4817                 :            : 
    4818                 :          0 : static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
    4819                 :            :                 struct page *page, unsigned int len, struct iattr *sattr)
    4820                 :            : {
    4821                 :          0 :         struct nfs4_exception exception = {
    4822                 :            :                 .interruptible = true,
    4823                 :            :         };
    4824                 :            :         struct nfs4_label l, *label = NULL;
    4825                 :            :         int err;
    4826                 :            : 
    4827                 :          0 :         label = nfs4_label_init_security(dir, dentry, sattr, &l);
    4828                 :            : 
    4829                 :            :         do {
    4830                 :          0 :                 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
    4831                 :          0 :                 trace_nfs4_symlink(dir, &dentry->d_name, err);
    4832                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
    4833                 :            :                                 &exception);
    4834                 :          0 :         } while (exception.retry);
    4835                 :            : 
    4836                 :            :         nfs4_label_release_security(label);
    4837                 :          0 :         return err;
    4838                 :            : }
    4839                 :            : 
    4840                 :          0 : static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
    4841                 :            :                 struct iattr *sattr, struct nfs4_label *label)
    4842                 :            : {
    4843                 :            :         struct nfs4_createdata *data;
    4844                 :            :         int status = -ENOMEM;
    4845                 :            : 
    4846                 :          0 :         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
    4847                 :          0 :         if (data == NULL)
    4848                 :            :                 goto out;
    4849                 :            : 
    4850                 :          0 :         data->arg.label = label;
    4851                 :          0 :         status = nfs4_do_create(dir, dentry, data);
    4852                 :            : 
    4853                 :            :         nfs4_free_createdata(data);
    4854                 :            : out:
    4855                 :          0 :         return status;
    4856                 :            : }
    4857                 :            : 
    4858                 :          0 : static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
    4859                 :            :                 struct iattr *sattr)
    4860                 :            : {
    4861                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    4862                 :          0 :         struct nfs4_exception exception = {
    4863                 :            :                 .interruptible = true,
    4864                 :            :         };
    4865                 :            :         struct nfs4_label l, *label = NULL;
    4866                 :            :         int err;
    4867                 :            : 
    4868                 :          0 :         label = nfs4_label_init_security(dir, dentry, sattr, &l);
    4869                 :            : 
    4870                 :          0 :         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
    4871                 :          0 :                 sattr->ia_mode &= ~current_umask();
    4872                 :            :         do {
    4873                 :          0 :                 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
    4874                 :          0 :                 trace_nfs4_mkdir(dir, &dentry->d_name, err);
    4875                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
    4876                 :            :                                 &exception);
    4877                 :          0 :         } while (exception.retry);
    4878                 :            :         nfs4_label_release_security(label);
    4879                 :            : 
    4880                 :          0 :         return err;
    4881                 :            : }
    4882                 :            : 
    4883                 :          0 : static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
    4884                 :            :                 u64 cookie, struct page **pages, unsigned int count, bool plus)
    4885                 :            : {
    4886                 :            :         struct inode            *dir = d_inode(dentry);
    4887                 :          0 :         struct nfs4_readdir_arg args = {
    4888                 :            :                 .fh = NFS_FH(dir),
    4889                 :            :                 .pages = pages,
    4890                 :            :                 .pgbase = 0,
    4891                 :            :                 .count = count,
    4892                 :          0 :                 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
    4893                 :            :                 .plus = plus,
    4894                 :            :         };
    4895                 :            :         struct nfs4_readdir_res res;
    4896                 :          0 :         struct rpc_message msg = {
    4897                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
    4898                 :            :                 .rpc_argp = &args,
    4899                 :            :                 .rpc_resp = &res,
    4900                 :            :                 .rpc_cred = cred,
    4901                 :            :         };
    4902                 :            :         int                     status;
    4903                 :            : 
    4904                 :            :         dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
    4905                 :            :                         dentry,
    4906                 :            :                         (unsigned long long)cookie);
    4907                 :          0 :         nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
    4908                 :          0 :         res.pgbase = args.pgbase;
    4909                 :          0 :         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
    4910                 :          0 :         if (status >= 0) {
    4911                 :          0 :                 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
    4912                 :          0 :                 status += args.pgbase;
    4913                 :            :         }
    4914                 :            : 
    4915                 :          0 :         nfs_invalidate_atime(dir);
    4916                 :            : 
    4917                 :            :         dprintk("%s: returns %d\n", __func__, status);
    4918                 :          0 :         return status;
    4919                 :            : }
    4920                 :            : 
    4921                 :          0 : static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
    4922                 :            :                 u64 cookie, struct page **pages, unsigned int count, bool plus)
    4923                 :            : {
    4924                 :          0 :         struct nfs4_exception exception = {
    4925                 :            :                 .interruptible = true,
    4926                 :            :         };
    4927                 :            :         int err;
    4928                 :            :         do {
    4929                 :          0 :                 err = _nfs4_proc_readdir(dentry, cred, cookie,
    4930                 :            :                                 pages, count, plus);
    4931                 :          0 :                 trace_nfs4_readdir(d_inode(dentry), err);
    4932                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
    4933                 :            :                                 &exception);
    4934                 :          0 :         } while (exception.retry);
    4935                 :          0 :         return err;
    4936                 :            : }
    4937                 :            : 
    4938                 :          0 : static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
    4939                 :            :                 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
    4940                 :            : {
    4941                 :            :         struct nfs4_createdata *data;
    4942                 :          0 :         int mode = sattr->ia_mode;
    4943                 :            :         int status = -ENOMEM;
    4944                 :            : 
    4945                 :          0 :         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
    4946                 :          0 :         if (data == NULL)
    4947                 :            :                 goto out;
    4948                 :            : 
    4949                 :          0 :         if (S_ISFIFO(mode))
    4950                 :          0 :                 data->arg.ftype = NF4FIFO;
    4951                 :          0 :         else if (S_ISBLK(mode)) {
    4952                 :          0 :                 data->arg.ftype = NF4BLK;
    4953                 :          0 :                 data->arg.u.device.specdata1 = MAJOR(rdev);
    4954                 :          0 :                 data->arg.u.device.specdata2 = MINOR(rdev);
    4955                 :            :         }
    4956                 :          0 :         else if (S_ISCHR(mode)) {
    4957                 :          0 :                 data->arg.ftype = NF4CHR;
    4958                 :          0 :                 data->arg.u.device.specdata1 = MAJOR(rdev);
    4959                 :          0 :                 data->arg.u.device.specdata2 = MINOR(rdev);
    4960                 :          0 :         } else if (!S_ISSOCK(mode)) {
    4961                 :            :                 status = -EINVAL;
    4962                 :            :                 goto out_free;
    4963                 :            :         }
    4964                 :            : 
    4965                 :          0 :         data->arg.label = label;
    4966                 :          0 :         status = nfs4_do_create(dir, dentry, data);
    4967                 :            : out_free:
    4968                 :            :         nfs4_free_createdata(data);
    4969                 :            : out:
    4970                 :          0 :         return status;
    4971                 :            : }
    4972                 :            : 
    4973                 :          0 : static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
    4974                 :            :                 struct iattr *sattr, dev_t rdev)
    4975                 :            : {
    4976                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    4977                 :          0 :         struct nfs4_exception exception = {
    4978                 :            :                 .interruptible = true,
    4979                 :            :         };
    4980                 :            :         struct nfs4_label l, *label = NULL;
    4981                 :            :         int err;
    4982                 :            : 
    4983                 :          0 :         label = nfs4_label_init_security(dir, dentry, sattr, &l);
    4984                 :            : 
    4985                 :          0 :         if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
    4986                 :          0 :                 sattr->ia_mode &= ~current_umask();
    4987                 :            :         do {
    4988                 :          0 :                 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
    4989                 :          0 :                 trace_nfs4_mknod(dir, &dentry->d_name, err);
    4990                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
    4991                 :            :                                 &exception);
    4992                 :          0 :         } while (exception.retry);
    4993                 :            : 
    4994                 :            :         nfs4_label_release_security(label);
    4995                 :            : 
    4996                 :          0 :         return err;
    4997                 :            : }
    4998                 :            : 
    4999                 :          0 : static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
    5000                 :            :                  struct nfs_fsstat *fsstat)
    5001                 :            : {
    5002                 :          0 :         struct nfs4_statfs_arg args = {
    5003                 :            :                 .fh = fhandle,
    5004                 :          0 :                 .bitmask = server->attr_bitmask,
    5005                 :            :         };
    5006                 :          0 :         struct nfs4_statfs_res res = {
    5007                 :            :                 .fsstat = fsstat,
    5008                 :            :         };
    5009                 :          0 :         struct rpc_message msg = {
    5010                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
    5011                 :            :                 .rpc_argp = &args,
    5012                 :            :                 .rpc_resp = &res,
    5013                 :            :         };
    5014                 :            : 
    5015                 :          0 :         nfs_fattr_init(fsstat->fattr);
    5016                 :          0 :         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    5017                 :            : }
    5018                 :            : 
    5019                 :          0 : static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
    5020                 :            : {
    5021                 :          0 :         struct nfs4_exception exception = {
    5022                 :            :                 .interruptible = true,
    5023                 :            :         };
    5024                 :            :         int err;
    5025                 :            :         do {
    5026                 :          0 :                 err = nfs4_handle_exception(server,
    5027                 :            :                                 _nfs4_proc_statfs(server, fhandle, fsstat),
    5028                 :            :                                 &exception);
    5029                 :          0 :         } while (exception.retry);
    5030                 :          0 :         return err;
    5031                 :            : }
    5032                 :            : 
    5033                 :          0 : static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
    5034                 :            :                 struct nfs_fsinfo *fsinfo)
    5035                 :            : {
    5036                 :          0 :         struct nfs4_fsinfo_arg args = {
    5037                 :            :                 .fh = fhandle,
    5038                 :          0 :                 .bitmask = server->attr_bitmask,
    5039                 :            :         };
    5040                 :          0 :         struct nfs4_fsinfo_res res = {
    5041                 :            :                 .fsinfo = fsinfo,
    5042                 :            :         };
    5043                 :          0 :         struct rpc_message msg = {
    5044                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
    5045                 :            :                 .rpc_argp = &args,
    5046                 :            :                 .rpc_resp = &res,
    5047                 :            :         };
    5048                 :            : 
    5049                 :          0 :         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    5050                 :            : }
    5051                 :            : 
    5052                 :          0 : static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
    5053                 :            : {
    5054                 :          0 :         struct nfs4_exception exception = {
    5055                 :            :                 .interruptible = true,
    5056                 :            :         };
    5057                 :            :         int err;
    5058                 :            : 
    5059                 :            :         do {
    5060                 :          0 :                 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
    5061                 :          0 :                 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
    5062                 :          0 :                 if (err == 0) {
    5063                 :          0 :                         nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
    5064                 :          0 :                         break;
    5065                 :            :                 }
    5066                 :          0 :                 err = nfs4_handle_exception(server, err, &exception);
    5067                 :          0 :         } while (exception.retry);
    5068                 :          0 :         return err;
    5069                 :            : }
    5070                 :            : 
    5071                 :          0 : static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
    5072                 :            : {
    5073                 :            :         int error;
    5074                 :            : 
    5075                 :          0 :         nfs_fattr_init(fsinfo->fattr);
    5076                 :          0 :         error = nfs4_do_fsinfo(server, fhandle, fsinfo);
    5077                 :          0 :         if (error == 0) {
    5078                 :            :                 /* block layout checks this! */
    5079                 :          0 :                 server->pnfs_blksize = fsinfo->blksize;
    5080                 :          0 :                 set_pnfs_layoutdriver(server, fhandle, fsinfo);
    5081                 :            :         }
    5082                 :            : 
    5083                 :          0 :         return error;
    5084                 :            : }
    5085                 :            : 
    5086                 :          0 : static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
    5087                 :            :                 struct nfs_pathconf *pathconf)
    5088                 :            : {
    5089                 :          0 :         struct nfs4_pathconf_arg args = {
    5090                 :            :                 .fh = fhandle,
    5091                 :          0 :                 .bitmask = server->attr_bitmask,
    5092                 :            :         };
    5093                 :          0 :         struct nfs4_pathconf_res res = {
    5094                 :            :                 .pathconf = pathconf,
    5095                 :            :         };
    5096                 :          0 :         struct rpc_message msg = {
    5097                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
    5098                 :            :                 .rpc_argp = &args,
    5099                 :            :                 .rpc_resp = &res,
    5100                 :            :         };
    5101                 :            : 
    5102                 :            :         /* None of the pathconf attributes are mandatory to implement */
    5103                 :          0 :         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
    5104                 :          0 :                 memset(pathconf, 0, sizeof(*pathconf));
    5105                 :          0 :                 return 0;
    5106                 :            :         }
    5107                 :            : 
    5108                 :          0 :         nfs_fattr_init(pathconf->fattr);
    5109                 :          0 :         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    5110                 :            : }
    5111                 :            : 
    5112                 :          0 : static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
    5113                 :            :                 struct nfs_pathconf *pathconf)
    5114                 :            : {
    5115                 :          0 :         struct nfs4_exception exception = {
    5116                 :            :                 .interruptible = true,
    5117                 :            :         };
    5118                 :            :         int err;
    5119                 :            : 
    5120                 :            :         do {
    5121                 :          0 :                 err = nfs4_handle_exception(server,
    5122                 :            :                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
    5123                 :            :                                 &exception);
    5124                 :          0 :         } while (exception.retry);
    5125                 :          0 :         return err;
    5126                 :            : }
    5127                 :            : 
    5128                 :          0 : int nfs4_set_rw_stateid(nfs4_stateid *stateid,
    5129                 :            :                 const struct nfs_open_context *ctx,
    5130                 :            :                 const struct nfs_lock_context *l_ctx,
    5131                 :            :                 fmode_t fmode)
    5132                 :            : {
    5133                 :          0 :         return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
    5134                 :            : }
    5135                 :            : EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
    5136                 :            : 
    5137                 :          0 : static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
    5138                 :            :                 const struct nfs_open_context *ctx,
    5139                 :            :                 const struct nfs_lock_context *l_ctx,
    5140                 :            :                 fmode_t fmode)
    5141                 :            : {
    5142                 :            :         nfs4_stateid current_stateid;
    5143                 :            : 
    5144                 :            :         /* If the current stateid represents a lost lock, then exit */
    5145                 :          0 :         if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
    5146                 :            :                 return true;
    5147                 :          0 :         return nfs4_stateid_match(stateid, &current_stateid);
    5148                 :            : }
    5149                 :            : 
    5150                 :            : static bool nfs4_error_stateid_expired(int err)
    5151                 :            : {
    5152                 :          0 :         switch (err) {
    5153                 :            :         case -NFS4ERR_DELEG_REVOKED:
    5154                 :            :         case -NFS4ERR_ADMIN_REVOKED:
    5155                 :            :         case -NFS4ERR_BAD_STATEID:
    5156                 :            :         case -NFS4ERR_STALE_STATEID:
    5157                 :            :         case -NFS4ERR_OLD_STATEID:
    5158                 :            :         case -NFS4ERR_OPENMODE:
    5159                 :            :         case -NFS4ERR_EXPIRED:
    5160                 :            :                 return true;
    5161                 :            :         }
    5162                 :            :         return false;
    5163                 :            : }
    5164                 :            : 
    5165                 :          0 : static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
    5166                 :            : {
    5167                 :          0 :         struct nfs_server *server = NFS_SERVER(hdr->inode);
    5168                 :            : 
    5169                 :          0 :         trace_nfs4_read(hdr, task->tk_status);
    5170                 :          0 :         if (task->tk_status < 0) {
    5171                 :          0 :                 struct nfs4_exception exception = {
    5172                 :          0 :                         .inode = hdr->inode,
    5173                 :          0 :                         .state = hdr->args.context->state,
    5174                 :          0 :                         .stateid = &hdr->args.stateid,
    5175                 :            :                 };
    5176                 :          0 :                 task->tk_status = nfs4_async_handle_exception(task,
    5177                 :            :                                 server, task->tk_status, &exception);
    5178                 :          0 :                 if (exception.retry) {
    5179                 :          0 :                         rpc_restart_call_prepare(task);
    5180                 :          0 :                         return -EAGAIN;
    5181                 :            :                 }
    5182                 :            :         }
    5183                 :            : 
    5184                 :          0 :         if (task->tk_status > 0)
    5185                 :          0 :                 renew_lease(server, hdr->timestamp);
    5186                 :            :         return 0;
    5187                 :            : }
    5188                 :            : 
    5189                 :          0 : static bool nfs4_read_stateid_changed(struct rpc_task *task,
    5190                 :            :                 struct nfs_pgio_args *args)
    5191                 :            : {
    5192                 :            : 
    5193                 :          0 :         if (!nfs4_error_stateid_expired(task->tk_status) ||
    5194                 :          0 :                 nfs4_stateid_is_current(&args->stateid,
    5195                 :          0 :                                 args->context,
    5196                 :          0 :                                 args->lock_context,
    5197                 :            :                                 FMODE_READ))
    5198                 :            :                 return false;
    5199                 :          0 :         rpc_restart_call_prepare(task);
    5200                 :          0 :         return true;
    5201                 :            : }
    5202                 :            : 
    5203                 :          0 : static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
    5204                 :            : {
    5205                 :            : 
    5206                 :            :         dprintk("--> %s\n", __func__);
    5207                 :            : 
    5208                 :          0 :         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
    5209                 :            :                 return -EAGAIN;
    5210                 :          0 :         if (nfs4_read_stateid_changed(task, &hdr->args))
    5211                 :            :                 return -EAGAIN;
    5212                 :          0 :         if (task->tk_status > 0)
    5213                 :          0 :                 nfs_invalidate_atime(hdr->inode);
    5214                 :          0 :         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
    5215                 :            :                                     nfs4_read_done_cb(task, hdr);
    5216                 :            : }
    5217                 :            : 
    5218                 :          0 : static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
    5219                 :            :                                  struct rpc_message *msg)
    5220                 :            : {
    5221                 :          0 :         hdr->timestamp   = jiffies;
    5222                 :          0 :         if (!hdr->pgio_done_cb)
    5223                 :          0 :                 hdr->pgio_done_cb = nfs4_read_done_cb;
    5224                 :          0 :         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
    5225                 :            :         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
    5226                 :          0 : }
    5227                 :            : 
    5228                 :          0 : static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
    5229                 :            :                                       struct nfs_pgio_header *hdr)
    5230                 :            : {
    5231                 :          0 :         if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
    5232                 :            :                         &hdr->args.seq_args,
    5233                 :            :                         &hdr->res.seq_res,
    5234                 :            :                         task))
    5235                 :            :                 return 0;
    5236                 :          0 :         if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
    5237                 :          0 :                                 hdr->args.lock_context,
    5238                 :            :                                 hdr->rw_mode) == -EIO)
    5239                 :            :                 return -EIO;
    5240                 :          0 :         if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
    5241                 :            :                 return -EIO;
    5242                 :          0 :         return 0;
    5243                 :            : }
    5244                 :            : 
    5245                 :          0 : static int nfs4_write_done_cb(struct rpc_task *task,
    5246                 :            :                               struct nfs_pgio_header *hdr)
    5247                 :            : {
    5248                 :          0 :         struct inode *inode = hdr->inode;
    5249                 :            : 
    5250                 :          0 :         trace_nfs4_write(hdr, task->tk_status);
    5251                 :          0 :         if (task->tk_status < 0) {
    5252                 :          0 :                 struct nfs4_exception exception = {
    5253                 :          0 :                         .inode = hdr->inode,
    5254                 :          0 :                         .state = hdr->args.context->state,
    5255                 :          0 :                         .stateid = &hdr->args.stateid,
    5256                 :            :                 };
    5257                 :          0 :                 task->tk_status = nfs4_async_handle_exception(task,
    5258                 :            :                                 NFS_SERVER(inode), task->tk_status,
    5259                 :            :                                 &exception);
    5260                 :          0 :                 if (exception.retry) {
    5261                 :          0 :                         rpc_restart_call_prepare(task);
    5262                 :          0 :                         return -EAGAIN;
    5263                 :            :                 }
    5264                 :            :         }
    5265                 :          0 :         if (task->tk_status >= 0) {
    5266                 :          0 :                 renew_lease(NFS_SERVER(inode), hdr->timestamp);
    5267                 :          0 :                 nfs_writeback_update_inode(hdr);
    5268                 :            :         }
    5269                 :            :         return 0;
    5270                 :            : }
    5271                 :            : 
    5272                 :          0 : static bool nfs4_write_stateid_changed(struct rpc_task *task,
    5273                 :            :                 struct nfs_pgio_args *args)
    5274                 :            : {
    5275                 :            : 
    5276                 :          0 :         if (!nfs4_error_stateid_expired(task->tk_status) ||
    5277                 :          0 :                 nfs4_stateid_is_current(&args->stateid,
    5278                 :          0 :                                 args->context,
    5279                 :          0 :                                 args->lock_context,
    5280                 :            :                                 FMODE_WRITE))
    5281                 :            :                 return false;
    5282                 :          0 :         rpc_restart_call_prepare(task);
    5283                 :          0 :         return true;
    5284                 :            : }
    5285                 :            : 
    5286                 :          0 : static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
    5287                 :            : {
    5288                 :          0 :         if (!nfs4_sequence_done(task, &hdr->res.seq_res))
    5289                 :            :                 return -EAGAIN;
    5290                 :          0 :         if (nfs4_write_stateid_changed(task, &hdr->args))
    5291                 :            :                 return -EAGAIN;
    5292                 :          0 :         return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
    5293                 :            :                 nfs4_write_done_cb(task, hdr);
    5294                 :            : }
    5295                 :            : 
    5296                 :            : static
    5297                 :          0 : bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
    5298                 :            : {
    5299                 :            :         /* Don't request attributes for pNFS or O_DIRECT writes */
    5300                 :          0 :         if (hdr->ds_clp != NULL || hdr->dreq != NULL)
    5301                 :            :                 return false;
    5302                 :            :         /* Otherwise, request attributes if and only if we don't hold
    5303                 :            :          * a delegation
    5304                 :            :          */
    5305                 :          0 :         return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
    5306                 :            : }
    5307                 :            : 
    5308                 :          0 : static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
    5309                 :            :                                   struct rpc_message *msg,
    5310                 :            :                                   struct rpc_clnt **clnt)
    5311                 :            : {
    5312                 :          0 :         struct nfs_server *server = NFS_SERVER(hdr->inode);
    5313                 :            : 
    5314                 :          0 :         if (!nfs4_write_need_cache_consistency_data(hdr)) {
    5315                 :          0 :                 hdr->args.bitmask = NULL;
    5316                 :          0 :                 hdr->res.fattr = NULL;
    5317                 :            :         } else
    5318                 :          0 :                 hdr->args.bitmask = server->cache_consistency_bitmask;
    5319                 :            : 
    5320                 :          0 :         if (!hdr->pgio_done_cb)
    5321                 :          0 :                 hdr->pgio_done_cb = nfs4_write_done_cb;
    5322                 :          0 :         hdr->res.server = server;
    5323                 :          0 :         hdr->timestamp   = jiffies;
    5324                 :            : 
    5325                 :          0 :         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
    5326                 :            :         nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
    5327                 :          0 :         nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
    5328                 :          0 : }
    5329                 :            : 
    5330                 :          0 : static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
    5331                 :            : {
    5332                 :          0 :         nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
    5333                 :            :                         &data->args.seq_args,
    5334                 :            :                         &data->res.seq_res,
    5335                 :            :                         task);
    5336                 :          0 : }
    5337                 :            : 
    5338                 :          0 : static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
    5339                 :            : {
    5340                 :          0 :         struct inode *inode = data->inode;
    5341                 :            : 
    5342                 :          0 :         trace_nfs4_commit(data, task->tk_status);
    5343                 :          0 :         if (nfs4_async_handle_error(task, NFS_SERVER(inode),
    5344                 :            :                                     NULL, NULL) == -EAGAIN) {
    5345                 :          0 :                 rpc_restart_call_prepare(task);
    5346                 :          0 :                 return -EAGAIN;
    5347                 :            :         }
    5348                 :            :         return 0;
    5349                 :            : }
    5350                 :            : 
    5351                 :          0 : static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
    5352                 :            : {
    5353                 :          0 :         if (!nfs4_sequence_done(task, &data->res.seq_res))
    5354                 :            :                 return -EAGAIN;
    5355                 :          0 :         return data->commit_done_cb(task, data);
    5356                 :            : }
    5357                 :            : 
    5358                 :          0 : static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
    5359                 :            :                                    struct rpc_clnt **clnt)
    5360                 :            : {
    5361                 :          0 :         struct nfs_server *server = NFS_SERVER(data->inode);
    5362                 :            : 
    5363                 :          0 :         if (data->commit_done_cb == NULL)
    5364                 :          0 :                 data->commit_done_cb = nfs4_commit_done_cb;
    5365                 :          0 :         data->res.server = server;
    5366                 :          0 :         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
    5367                 :            :         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
    5368                 :          0 :         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
    5369                 :          0 : }
    5370                 :            : 
    5371                 :          0 : static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
    5372                 :            :                                 struct nfs_commitres *res)
    5373                 :            : {
    5374                 :            :         struct inode *dst_inode = file_inode(dst);
    5375                 :            :         struct nfs_server *server = NFS_SERVER(dst_inode);
    5376                 :          0 :         struct rpc_message msg = {
    5377                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
    5378                 :            :                 .rpc_argp = args,
    5379                 :            :                 .rpc_resp = res,
    5380                 :            :         };
    5381                 :            : 
    5382                 :          0 :         args->fh = NFS_FH(dst_inode);
    5383                 :          0 :         return nfs4_call_sync(server->client, server, &msg,
    5384                 :            :                         &args->seq_args, &res->seq_res, 1);
    5385                 :            : }
    5386                 :            : 
    5387                 :          0 : int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
    5388                 :            : {
    5389                 :          0 :         struct nfs_commitargs args = {
    5390                 :            :                 .offset = offset,
    5391                 :            :                 .count = count,
    5392                 :            :         };
    5393                 :            :         struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
    5394                 :          0 :         struct nfs4_exception exception = { };
    5395                 :            :         int status;
    5396                 :            : 
    5397                 :            :         do {
    5398                 :          0 :                 status = _nfs4_proc_commit(dst, &args, res);
    5399                 :          0 :                 status = nfs4_handle_exception(dst_server, status, &exception);
    5400                 :          0 :         } while (exception.retry);
    5401                 :            : 
    5402                 :          0 :         return status;
    5403                 :            : }
    5404                 :            : 
    5405                 :            : struct nfs4_renewdata {
    5406                 :            :         struct nfs_client       *client;
    5407                 :            :         unsigned long           timestamp;
    5408                 :            : };
    5409                 :            : 
    5410                 :            : /*
    5411                 :            :  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
    5412                 :            :  * standalone procedure for queueing an asynchronous RENEW.
    5413                 :            :  */
    5414                 :          0 : static void nfs4_renew_release(void *calldata)
    5415                 :            : {
    5416                 :            :         struct nfs4_renewdata *data = calldata;
    5417                 :          0 :         struct nfs_client *clp = data->client;
    5418                 :            : 
    5419                 :          0 :         if (refcount_read(&clp->cl_count) > 1)
    5420                 :          0 :                 nfs4_schedule_state_renewal(clp);
    5421                 :          0 :         nfs_put_client(clp);
    5422                 :          0 :         kfree(data);
    5423                 :          0 : }
    5424                 :            : 
    5425                 :          0 : static void nfs4_renew_done(struct rpc_task *task, void *calldata)
    5426                 :            : {
    5427                 :            :         struct nfs4_renewdata *data = calldata;
    5428                 :          0 :         struct nfs_client *clp = data->client;
    5429                 :          0 :         unsigned long timestamp = data->timestamp;
    5430                 :            : 
    5431                 :          0 :         trace_nfs4_renew_async(clp, task->tk_status);
    5432                 :          0 :         switch (task->tk_status) {
    5433                 :            :         case 0:
    5434                 :            :                 break;
    5435                 :            :         case -NFS4ERR_LEASE_MOVED:
    5436                 :          0 :                 nfs4_schedule_lease_moved_recovery(clp);
    5437                 :          0 :                 break;
    5438                 :            :         default:
    5439                 :            :                 /* Unless we're shutting down, schedule state recovery! */
    5440                 :          0 :                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
    5441                 :            :                         return;
    5442                 :          0 :                 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
    5443                 :          0 :                         nfs4_schedule_lease_recovery(clp);
    5444                 :          0 :                         return;
    5445                 :            :                 }
    5446                 :          0 :                 nfs4_schedule_path_down_recovery(clp);
    5447                 :            :         }
    5448                 :          0 :         do_renew_lease(clp, timestamp);
    5449                 :            : }
    5450                 :            : 
    5451                 :            : static const struct rpc_call_ops nfs4_renew_ops = {
    5452                 :            :         .rpc_call_done = nfs4_renew_done,
    5453                 :            :         .rpc_release = nfs4_renew_release,
    5454                 :            : };
    5455                 :            : 
    5456                 :          0 : static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
    5457                 :            : {
    5458                 :          0 :         struct rpc_message msg = {
    5459                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
    5460                 :            :                 .rpc_argp       = clp,
    5461                 :            :                 .rpc_cred       = cred,
    5462                 :            :         };
    5463                 :            :         struct nfs4_renewdata *data;
    5464                 :            : 
    5465                 :          0 :         if (renew_flags == 0)
    5466                 :            :                 return 0;
    5467                 :          0 :         if (!refcount_inc_not_zero(&clp->cl_count))
    5468                 :            :                 return -EIO;
    5469                 :            :         data = kmalloc(sizeof(*data), GFP_NOFS);
    5470                 :          0 :         if (data == NULL) {
    5471                 :          0 :                 nfs_put_client(clp);
    5472                 :          0 :                 return -ENOMEM;
    5473                 :            :         }
    5474                 :          0 :         data->client = clp;
    5475                 :          0 :         data->timestamp = jiffies;
    5476                 :          0 :         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
    5477                 :            :                         &nfs4_renew_ops, data);
    5478                 :            : }
    5479                 :            : 
    5480                 :          0 : static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
    5481                 :            : {
    5482                 :          0 :         struct rpc_message msg = {
    5483                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
    5484                 :            :                 .rpc_argp       = clp,
    5485                 :            :                 .rpc_cred       = cred,
    5486                 :            :         };
    5487                 :          0 :         unsigned long now = jiffies;
    5488                 :            :         int status;
    5489                 :            : 
    5490                 :          0 :         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
    5491                 :          0 :         if (status < 0)
    5492                 :            :                 return status;
    5493                 :          0 :         do_renew_lease(clp, now);
    5494                 :          0 :         return 0;
    5495                 :            : }
    5496                 :            : 
    5497                 :            : static inline int nfs4_server_supports_acls(struct nfs_server *server)
    5498                 :            : {
    5499                 :          0 :         return server->caps & NFS_CAP_ACLS;
    5500                 :            : }
    5501                 :            : 
    5502                 :            : /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
    5503                 :            :  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
    5504                 :            :  * the stack.
    5505                 :            :  */
    5506                 :            : #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
    5507                 :            : 
    5508                 :          0 : static int buf_to_pages_noslab(const void *buf, size_t buflen,
    5509                 :            :                 struct page **pages)
    5510                 :            : {
    5511                 :            :         struct page *newpage, **spages;
    5512                 :            :         int rc = 0;
    5513                 :            :         size_t len;
    5514                 :            :         spages = pages;
    5515                 :            : 
    5516                 :            :         do {
    5517                 :          0 :                 len = min_t(size_t, PAGE_SIZE, buflen);
    5518                 :            :                 newpage = alloc_page(GFP_KERNEL);
    5519                 :            : 
    5520                 :          0 :                 if (newpage == NULL)
    5521                 :            :                         goto unwind;
    5522                 :          0 :                 memcpy(page_address(newpage), buf, len);
    5523                 :          0 :                 buf += len;
    5524                 :          0 :                 buflen -= len;
    5525                 :          0 :                 *pages++ = newpage;
    5526                 :          0 :                 rc++;
    5527                 :          0 :         } while (buflen != 0);
    5528                 :            : 
    5529                 :          0 :         return rc;
    5530                 :            : 
    5531                 :            : unwind:
    5532                 :          0 :         for(; rc > 0; rc--)
    5533                 :          0 :                 __free_page(spages[rc-1]);
    5534                 :            :         return -ENOMEM;
    5535                 :            : }
    5536                 :            : 
    5537                 :            : struct nfs4_cached_acl {
    5538                 :            :         int cached;
    5539                 :            :         size_t len;
    5540                 :            :         char data[0];
    5541                 :            : };
    5542                 :            : 
    5543                 :          0 : static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
    5544                 :            : {
    5545                 :            :         struct nfs_inode *nfsi = NFS_I(inode);
    5546                 :            : 
    5547                 :            :         spin_lock(&inode->i_lock);
    5548                 :          0 :         kfree(nfsi->nfs4_acl);
    5549                 :          0 :         nfsi->nfs4_acl = acl;
    5550                 :            :         spin_unlock(&inode->i_lock);
    5551                 :          0 : }
    5552                 :            : 
    5553                 :          0 : static void nfs4_zap_acl_attr(struct inode *inode)
    5554                 :            : {
    5555                 :          0 :         nfs4_set_cached_acl(inode, NULL);
    5556                 :          0 : }
    5557                 :            : 
    5558                 :          0 : static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
    5559                 :            : {
    5560                 :            :         struct nfs_inode *nfsi = NFS_I(inode);
    5561                 :            :         struct nfs4_cached_acl *acl;
    5562                 :            :         int ret = -ENOENT;
    5563                 :            : 
    5564                 :            :         spin_lock(&inode->i_lock);
    5565                 :          0 :         acl = nfsi->nfs4_acl;
    5566                 :          0 :         if (acl == NULL)
    5567                 :            :                 goto out;
    5568                 :          0 :         if (buf == NULL) /* user is just asking for length */
    5569                 :            :                 goto out_len;
    5570                 :          0 :         if (acl->cached == 0)
    5571                 :            :                 goto out;
    5572                 :            :         ret = -ERANGE; /* see getxattr(2) man page */
    5573                 :          0 :         if (acl->len > buflen)
    5574                 :            :                 goto out;
    5575                 :          0 :         memcpy(buf, acl->data, acl->len);
    5576                 :            : out_len:
    5577                 :          0 :         ret = acl->len;
    5578                 :            : out:
    5579                 :            :         spin_unlock(&inode->i_lock);
    5580                 :          0 :         return ret;
    5581                 :            : }
    5582                 :            : 
    5583                 :          0 : static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
    5584                 :            : {
    5585                 :            :         struct nfs4_cached_acl *acl;
    5586                 :          0 :         size_t buflen = sizeof(*acl) + acl_len;
    5587                 :            : 
    5588                 :          0 :         if (buflen <= PAGE_SIZE) {
    5589                 :            :                 acl = kmalloc(buflen, GFP_KERNEL);
    5590                 :          0 :                 if (acl == NULL)
    5591                 :            :                         goto out;
    5592                 :          0 :                 acl->cached = 1;
    5593                 :          0 :                 _copy_from_pages(acl->data, pages, pgbase, acl_len);
    5594                 :            :         } else {
    5595                 :            :                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
    5596                 :          0 :                 if (acl == NULL)
    5597                 :            :                         goto out;
    5598                 :          0 :                 acl->cached = 0;
    5599                 :            :         }
    5600                 :          0 :         acl->len = acl_len;
    5601                 :            : out:
    5602                 :          0 :         nfs4_set_cached_acl(inode, acl);
    5603                 :          0 : }
    5604                 :            : 
    5605                 :            : /*
    5606                 :            :  * The getxattr API returns the required buffer length when called with a
    5607                 :            :  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
    5608                 :            :  * the required buf.  On a NULL buf, we send a page of data to the server
    5609                 :            :  * guessing that the ACL request can be serviced by a page. If so, we cache
    5610                 :            :  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
    5611                 :            :  * the cache. If not so, we throw away the page, and cache the required
    5612                 :            :  * length. The next getxattr call will then produce another round trip to
    5613                 :            :  * the server, this time with the input buf of the required size.
    5614                 :            :  */
    5615                 :          0 : static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
    5616                 :            : {
    5617                 :          0 :         struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
    5618                 :          0 :         struct nfs_getaclargs args = {
    5619                 :            :                 .fh = NFS_FH(inode),
    5620                 :            :                 .acl_pages = pages,
    5621                 :            :                 .acl_len = buflen,
    5622                 :            :         };
    5623                 :          0 :         struct nfs_getaclres res = {
    5624                 :            :                 .acl_len = buflen,
    5625                 :            :         };
    5626                 :          0 :         struct rpc_message msg = {
    5627                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
    5628                 :            :                 .rpc_argp = &args,
    5629                 :            :                 .rpc_resp = &res,
    5630                 :            :         };
    5631                 :          0 :         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
    5632                 :            :         int ret = -ENOMEM, i;
    5633                 :            : 
    5634                 :          0 :         if (npages > ARRAY_SIZE(pages))
    5635                 :            :                 return -ERANGE;
    5636                 :            : 
    5637                 :          0 :         for (i = 0; i < npages; i++) {
    5638                 :          0 :                 pages[i] = alloc_page(GFP_KERNEL);
    5639                 :          0 :                 if (!pages[i])
    5640                 :            :                         goto out_free;
    5641                 :            :         }
    5642                 :            : 
    5643                 :            :         /* for decoding across pages */
    5644                 :          0 :         res.acl_scratch = alloc_page(GFP_KERNEL);
    5645                 :          0 :         if (!res.acl_scratch)
    5646                 :            :                 goto out_free;
    5647                 :            : 
    5648                 :          0 :         args.acl_len = npages * PAGE_SIZE;
    5649                 :            : 
    5650                 :            :         dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
    5651                 :            :                 __func__, buf, buflen, npages, args.acl_len);
    5652                 :          0 :         ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
    5653                 :            :                              &msg, &args.seq_args, &res.seq_res, 0);
    5654                 :          0 :         if (ret)
    5655                 :            :                 goto out_free;
    5656                 :            : 
    5657                 :            :         /* Handle the case where the passed-in buffer is too short */
    5658                 :          0 :         if (res.acl_flags & NFS4_ACL_TRUNC) {
    5659                 :            :                 /* Did the user only issue a request for the acl length? */
    5660                 :          0 :                 if (buf == NULL)
    5661                 :            :                         goto out_ok;
    5662                 :            :                 ret = -ERANGE;
    5663                 :            :                 goto out_free;
    5664                 :            :         }
    5665                 :          0 :         nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
    5666                 :          0 :         if (buf) {
    5667                 :          0 :                 if (res.acl_len > buflen) {
    5668                 :            :                         ret = -ERANGE;
    5669                 :            :                         goto out_free;
    5670                 :            :                 }
    5671                 :          0 :                 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
    5672                 :            :         }
    5673                 :            : out_ok:
    5674                 :          0 :         ret = res.acl_len;
    5675                 :            : out_free:
    5676                 :          0 :         for (i = 0; i < npages; i++)
    5677                 :          0 :                 if (pages[i])
    5678                 :          0 :                         __free_page(pages[i]);
    5679                 :          0 :         if (res.acl_scratch)
    5680                 :          0 :                 __free_page(res.acl_scratch);
    5681                 :          0 :         return ret;
    5682                 :            : }
    5683                 :            : 
    5684                 :          0 : static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
    5685                 :            : {
    5686                 :          0 :         struct nfs4_exception exception = {
    5687                 :            :                 .interruptible = true,
    5688                 :            :         };
    5689                 :            :         ssize_t ret;
    5690                 :            :         do {
    5691                 :          0 :                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
    5692                 :          0 :                 trace_nfs4_get_acl(inode, ret);
    5693                 :          0 :                 if (ret >= 0)
    5694                 :            :                         break;
    5695                 :          0 :                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
    5696                 :          0 :         } while (exception.retry);
    5697                 :          0 :         return ret;
    5698                 :            : }
    5699                 :            : 
    5700                 :          0 : static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
    5701                 :            : {
    5702                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    5703                 :            :         int ret;
    5704                 :            : 
    5705                 :          0 :         if (!nfs4_server_supports_acls(server))
    5706                 :            :                 return -EOPNOTSUPP;
    5707                 :          0 :         ret = nfs_revalidate_inode(server, inode);
    5708                 :          0 :         if (ret < 0)
    5709                 :            :                 return ret;
    5710                 :          0 :         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
    5711                 :          0 :                 nfs_zap_acl_cache(inode);
    5712                 :          0 :         ret = nfs4_read_cached_acl(inode, buf, buflen);
    5713                 :          0 :         if (ret != -ENOENT)
    5714                 :            :                 /* -ENOENT is returned if there is no ACL or if there is an ACL
    5715                 :            :                  * but no cached acl data, just the acl length */
    5716                 :            :                 return ret;
    5717                 :          0 :         return nfs4_get_acl_uncached(inode, buf, buflen);
    5718                 :            : }
    5719                 :            : 
    5720                 :          0 : static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
    5721                 :            : {
    5722                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    5723                 :            :         struct page *pages[NFS4ACL_MAXPAGES];
    5724                 :          0 :         struct nfs_setaclargs arg = {
    5725                 :            :                 .fh             = NFS_FH(inode),
    5726                 :            :                 .acl_pages      = pages,
    5727                 :            :                 .acl_len        = buflen,
    5728                 :            :         };
    5729                 :            :         struct nfs_setaclres res;
    5730                 :          0 :         struct rpc_message msg = {
    5731                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
    5732                 :            :                 .rpc_argp       = &arg,
    5733                 :            :                 .rpc_resp       = &res,
    5734                 :            :         };
    5735                 :          0 :         unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
    5736                 :            :         int ret, i;
    5737                 :            : 
    5738                 :          0 :         if (!nfs4_server_supports_acls(server))
    5739                 :            :                 return -EOPNOTSUPP;
    5740                 :          0 :         if (npages > ARRAY_SIZE(pages))
    5741                 :            :                 return -ERANGE;
    5742                 :          0 :         i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
    5743                 :          0 :         if (i < 0)
    5744                 :            :                 return i;
    5745                 :          0 :         nfs4_inode_make_writeable(inode);
    5746                 :          0 :         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
    5747                 :            : 
    5748                 :            :         /*
    5749                 :            :          * Free each page after tx, so the only ref left is
    5750                 :            :          * held by the network stack
    5751                 :            :          */
    5752                 :          0 :         for (; i > 0; i--)
    5753                 :          0 :                 put_page(pages[i-1]);
    5754                 :            : 
    5755                 :            :         /*
    5756                 :            :          * Acl update can result in inode attribute update.
    5757                 :            :          * so mark the attribute cache invalid.
    5758                 :            :          */
    5759                 :            :         spin_lock(&inode->i_lock);
    5760                 :          0 :         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
    5761                 :            :                 | NFS_INO_INVALID_CTIME
    5762                 :            :                 | NFS_INO_REVAL_FORCED;
    5763                 :            :         spin_unlock(&inode->i_lock);
    5764                 :          0 :         nfs_access_zap_cache(inode);
    5765                 :          0 :         nfs_zap_acl_cache(inode);
    5766                 :          0 :         return ret;
    5767                 :            : }
    5768                 :            : 
    5769                 :          0 : static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
    5770                 :            : {
    5771                 :          0 :         struct nfs4_exception exception = { };
    5772                 :            :         int err;
    5773                 :            :         do {
    5774                 :          0 :                 err = __nfs4_proc_set_acl(inode, buf, buflen);
    5775                 :          0 :                 trace_nfs4_set_acl(inode, err);
    5776                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
    5777                 :            :                                 &exception);
    5778                 :          0 :         } while (exception.retry);
    5779                 :          0 :         return err;
    5780                 :            : }
    5781                 :            : 
    5782                 :            : #ifdef CONFIG_NFS_V4_SECURITY_LABEL
    5783                 :          0 : static int _nfs4_get_security_label(struct inode *inode, void *buf,
    5784                 :            :                                         size_t buflen)
    5785                 :            : {
    5786                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    5787                 :            :         struct nfs_fattr fattr;
    5788                 :          0 :         struct nfs4_label label = {0, 0, buflen, buf};
    5789                 :            : 
    5790                 :          0 :         u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
    5791                 :          0 :         struct nfs4_getattr_arg arg = {
    5792                 :            :                 .fh             = NFS_FH(inode),
    5793                 :            :                 .bitmask        = bitmask,
    5794                 :            :         };
    5795                 :          0 :         struct nfs4_getattr_res res = {
    5796                 :            :                 .fattr          = &fattr,
    5797                 :            :                 .label          = &label,
    5798                 :            :                 .server         = server,
    5799                 :            :         };
    5800                 :          0 :         struct rpc_message msg = {
    5801                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
    5802                 :            :                 .rpc_argp       = &arg,
    5803                 :            :                 .rpc_resp       = &res,
    5804                 :            :         };
    5805                 :            :         int ret;
    5806                 :            : 
    5807                 :          0 :         nfs_fattr_init(&fattr);
    5808                 :            : 
    5809                 :          0 :         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
    5810                 :          0 :         if (ret)
    5811                 :            :                 return ret;
    5812                 :          0 :         if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
    5813                 :            :                 return -ENOENT;
    5814                 :          0 :         return 0;
    5815                 :            : }
    5816                 :            : 
    5817                 :          0 : static int nfs4_get_security_label(struct inode *inode, void *buf,
    5818                 :            :                                         size_t buflen)
    5819                 :            : {
    5820                 :          0 :         struct nfs4_exception exception = {
    5821                 :            :                 .interruptible = true,
    5822                 :            :         };
    5823                 :            :         int err;
    5824                 :            : 
    5825                 :          0 :         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
    5826                 :            :                 return -EOPNOTSUPP;
    5827                 :            : 
    5828                 :            :         do {
    5829                 :          0 :                 err = _nfs4_get_security_label(inode, buf, buflen);
    5830                 :          0 :                 trace_nfs4_get_security_label(inode, err);
    5831                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
    5832                 :            :                                 &exception);
    5833                 :          0 :         } while (exception.retry);
    5834                 :          0 :         return err;
    5835                 :            : }
    5836                 :            : 
    5837                 :          0 : static int _nfs4_do_set_security_label(struct inode *inode,
    5838                 :            :                 struct nfs4_label *ilabel,
    5839                 :            :                 struct nfs_fattr *fattr,
    5840                 :            :                 struct nfs4_label *olabel)
    5841                 :            : {
    5842                 :            : 
    5843                 :          0 :         struct iattr sattr = {0};
    5844                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    5845                 :          0 :         const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
    5846                 :          0 :         struct nfs_setattrargs arg = {
    5847                 :            :                 .fh             = NFS_FH(inode),
    5848                 :            :                 .iap            = &sattr,
    5849                 :            :                 .server         = server,
    5850                 :            :                 .bitmask        = bitmask,
    5851                 :            :                 .label          = ilabel,
    5852                 :            :         };
    5853                 :          0 :         struct nfs_setattrres res = {
    5854                 :            :                 .fattr          = fattr,
    5855                 :            :                 .label          = olabel,
    5856                 :            :                 .server         = server,
    5857                 :            :         };
    5858                 :          0 :         struct rpc_message msg = {
    5859                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
    5860                 :            :                 .rpc_argp       = &arg,
    5861                 :            :                 .rpc_resp       = &res,
    5862                 :            :         };
    5863                 :            :         int status;
    5864                 :            : 
    5865                 :            :         nfs4_stateid_copy(&arg.stateid, &zero_stateid);
    5866                 :            : 
    5867                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
    5868                 :            :         if (status)
    5869                 :            :                 dprintk("%s failed: %d\n", __func__, status);
    5870                 :            : 
    5871                 :          0 :         return status;
    5872                 :            : }
    5873                 :            : 
    5874                 :          0 : static int nfs4_do_set_security_label(struct inode *inode,
    5875                 :            :                 struct nfs4_label *ilabel,
    5876                 :            :                 struct nfs_fattr *fattr,
    5877                 :            :                 struct nfs4_label *olabel)
    5878                 :            : {
    5879                 :          0 :         struct nfs4_exception exception = { };
    5880                 :            :         int err;
    5881                 :            : 
    5882                 :            :         do {
    5883                 :          0 :                 err = _nfs4_do_set_security_label(inode, ilabel,
    5884                 :            :                                 fattr, olabel);
    5885                 :          0 :                 trace_nfs4_set_security_label(inode, err);
    5886                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(inode), err,
    5887                 :            :                                 &exception);
    5888                 :          0 :         } while (exception.retry);
    5889                 :          0 :         return err;
    5890                 :            : }
    5891                 :            : 
    5892                 :            : static int
    5893                 :          0 : nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
    5894                 :            : {
    5895                 :            :         struct nfs4_label ilabel, *olabel = NULL;
    5896                 :            :         struct nfs_fattr fattr;
    5897                 :            :         int status;
    5898                 :            : 
    5899                 :          0 :         if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
    5900                 :            :                 return -EOPNOTSUPP;
    5901                 :            : 
    5902                 :          0 :         nfs_fattr_init(&fattr);
    5903                 :            : 
    5904                 :          0 :         ilabel.pi = 0;
    5905                 :          0 :         ilabel.lfs = 0;
    5906                 :          0 :         ilabel.label = (char *)buf;
    5907                 :          0 :         ilabel.len = buflen;
    5908                 :            : 
    5909                 :          0 :         olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
    5910                 :          0 :         if (IS_ERR(olabel)) {
    5911                 :          0 :                 status = -PTR_ERR(olabel);
    5912                 :          0 :                 goto out;
    5913                 :            :         }
    5914                 :            : 
    5915                 :          0 :         status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
    5916                 :          0 :         if (status == 0)
    5917                 :          0 :                 nfs_setsecurity(inode, &fattr, olabel);
    5918                 :            : 
    5919                 :          0 :         nfs4_label_free(olabel);
    5920                 :            : out:
    5921                 :          0 :         return status;
    5922                 :            : }
    5923                 :            : #endif  /* CONFIG_NFS_V4_SECURITY_LABEL */
    5924                 :            : 
    5925                 :            : 
    5926                 :          0 : static void nfs4_init_boot_verifier(const struct nfs_client *clp,
    5927                 :            :                                     nfs4_verifier *bootverf)
    5928                 :            : {
    5929                 :            :         __be32 verf[2];
    5930                 :            : 
    5931                 :          0 :         if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
    5932                 :            :                 /* An impossible timestamp guarantees this value
    5933                 :            :                  * will never match a generated boot time. */
    5934                 :          0 :                 verf[0] = cpu_to_be32(U32_MAX);
    5935                 :          0 :                 verf[1] = cpu_to_be32(U32_MAX);
    5936                 :            :         } else {
    5937                 :          0 :                 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
    5938                 :          0 :                 u64 ns = ktime_to_ns(nn->boot_time);
    5939                 :            : 
    5940                 :          0 :                 verf[0] = cpu_to_be32(ns >> 32);
    5941                 :          0 :                 verf[1] = cpu_to_be32(ns);
    5942                 :            :         }
    5943                 :          0 :         memcpy(bootverf->data, verf, sizeof(bootverf->data));
    5944                 :          0 : }
    5945                 :            : 
    5946                 :            : static int
    5947                 :          0 : nfs4_init_nonuniform_client_string(struct nfs_client *clp)
    5948                 :            : {
    5949                 :            :         size_t len;
    5950                 :            :         char *str;
    5951                 :            : 
    5952                 :          0 :         if (clp->cl_owner_id != NULL)
    5953                 :            :                 return 0;
    5954                 :            : 
    5955                 :            :         rcu_read_lock();
    5956                 :          0 :         len = 14 +
    5957                 :          0 :                 strlen(clp->cl_rpcclient->cl_nodename) +
    5958                 :          0 :                 1 +
    5959                 :          0 :                 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
    5960                 :            :                 1;
    5961                 :            :         rcu_read_unlock();
    5962                 :          0 :         if (nfs4_client_id_uniquifier[0] != '\0')
    5963                 :          0 :                 len += strlen(nfs4_client_id_uniquifier) + 1;
    5964                 :          0 :         if (len > NFS4_OPAQUE_LIMIT + 1)
    5965                 :            :                 return -EINVAL;
    5966                 :            : 
    5967                 :            :         /*
    5968                 :            :          * Since this string is allocated at mount time, and held until the
    5969                 :            :          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
    5970                 :            :          * about a memory-reclaim deadlock.
    5971                 :            :          */
    5972                 :            :         str = kmalloc(len, GFP_KERNEL);
    5973                 :          0 :         if (!str)
    5974                 :            :                 return -ENOMEM;
    5975                 :            : 
    5976                 :            :         rcu_read_lock();
    5977                 :          0 :         if (nfs4_client_id_uniquifier[0] != '\0')
    5978                 :          0 :                 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
    5979                 :          0 :                           clp->cl_rpcclient->cl_nodename,
    5980                 :            :                           nfs4_client_id_uniquifier,
    5981                 :            :                           rpc_peeraddr2str(clp->cl_rpcclient,
    5982                 :            :                                            RPC_DISPLAY_ADDR));
    5983                 :            :         else
    5984                 :          0 :                 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
    5985                 :          0 :                           clp->cl_rpcclient->cl_nodename,
    5986                 :            :                           rpc_peeraddr2str(clp->cl_rpcclient,
    5987                 :            :                                            RPC_DISPLAY_ADDR));
    5988                 :            :         rcu_read_unlock();
    5989                 :            : 
    5990                 :          0 :         clp->cl_owner_id = str;
    5991                 :          0 :         return 0;
    5992                 :            : }
    5993                 :            : 
    5994                 :            : static int
    5995                 :          0 : nfs4_init_uniquifier_client_string(struct nfs_client *clp)
    5996                 :            : {
    5997                 :            :         size_t len;
    5998                 :            :         char *str;
    5999                 :            : 
    6000                 :          0 :         len = 10 + 10 + 1 + 10 + 1 +
    6001                 :          0 :                 strlen(nfs4_client_id_uniquifier) + 1 +
    6002                 :          0 :                 strlen(clp->cl_rpcclient->cl_nodename) + 1;
    6003                 :            : 
    6004                 :          0 :         if (len > NFS4_OPAQUE_LIMIT + 1)
    6005                 :            :                 return -EINVAL;
    6006                 :            : 
    6007                 :            :         /*
    6008                 :            :          * Since this string is allocated at mount time, and held until the
    6009                 :            :          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
    6010                 :            :          * about a memory-reclaim deadlock.
    6011                 :            :          */
    6012                 :            :         str = kmalloc(len, GFP_KERNEL);
    6013                 :          0 :         if (!str)
    6014                 :            :                 return -ENOMEM;
    6015                 :            : 
    6016                 :          0 :         scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
    6017                 :          0 :                         clp->rpc_ops->version, clp->cl_minorversion,
    6018                 :            :                         nfs4_client_id_uniquifier,
    6019                 :          0 :                         clp->cl_rpcclient->cl_nodename);
    6020                 :          0 :         clp->cl_owner_id = str;
    6021                 :          0 :         return 0;
    6022                 :            : }
    6023                 :            : 
    6024                 :            : static int
    6025                 :          0 : nfs4_init_uniform_client_string(struct nfs_client *clp)
    6026                 :            : {
    6027                 :            :         size_t len;
    6028                 :            :         char *str;
    6029                 :            : 
    6030                 :          0 :         if (clp->cl_owner_id != NULL)
    6031                 :            :                 return 0;
    6032                 :            : 
    6033                 :          0 :         if (nfs4_client_id_uniquifier[0] != '\0')
    6034                 :          0 :                 return nfs4_init_uniquifier_client_string(clp);
    6035                 :            : 
    6036                 :          0 :         len = 10 + 10 + 1 + 10 + 1 +
    6037                 :          0 :                 strlen(clp->cl_rpcclient->cl_nodename) + 1;
    6038                 :            : 
    6039                 :          0 :         if (len > NFS4_OPAQUE_LIMIT + 1)
    6040                 :            :                 return -EINVAL;
    6041                 :            : 
    6042                 :            :         /*
    6043                 :            :          * Since this string is allocated at mount time, and held until the
    6044                 :            :          * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
    6045                 :            :          * about a memory-reclaim deadlock.
    6046                 :            :          */
    6047                 :            :         str = kmalloc(len, GFP_KERNEL);
    6048                 :          0 :         if (!str)
    6049                 :            :                 return -ENOMEM;
    6050                 :            : 
    6051                 :          0 :         scnprintf(str, len, "Linux NFSv%u.%u %s",
    6052                 :          0 :                         clp->rpc_ops->version, clp->cl_minorversion,
    6053                 :          0 :                         clp->cl_rpcclient->cl_nodename);
    6054                 :          0 :         clp->cl_owner_id = str;
    6055                 :          0 :         return 0;
    6056                 :            : }
    6057                 :            : 
    6058                 :            : /*
    6059                 :            :  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
    6060                 :            :  * services.  Advertise one based on the address family of the
    6061                 :            :  * clientaddr.
    6062                 :            :  */
    6063                 :            : static unsigned int
    6064                 :          0 : nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
    6065                 :            : {
    6066                 :          0 :         if (strchr(clp->cl_ipaddr, ':') != NULL)
    6067                 :          0 :                 return scnprintf(buf, len, "tcp6");
    6068                 :            :         else
    6069                 :          0 :                 return scnprintf(buf, len, "tcp");
    6070                 :            : }
    6071                 :            : 
    6072                 :          0 : static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
    6073                 :            : {
    6074                 :            :         struct nfs4_setclientid *sc = calldata;
    6075                 :            : 
    6076                 :          0 :         if (task->tk_status == 0)
    6077                 :          0 :                 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
    6078                 :          0 : }
    6079                 :            : 
    6080                 :            : static const struct rpc_call_ops nfs4_setclientid_ops = {
    6081                 :            :         .rpc_call_done = nfs4_setclientid_done,
    6082                 :            : };
    6083                 :            : 
    6084                 :            : /**
    6085                 :            :  * nfs4_proc_setclientid - Negotiate client ID
    6086                 :            :  * @clp: state data structure
    6087                 :            :  * @program: RPC program for NFSv4 callback service
    6088                 :            :  * @port: IP port number for NFS4 callback service
    6089                 :            :  * @cred: credential to use for this call
    6090                 :            :  * @res: where to place the result
    6091                 :            :  *
    6092                 :            :  * Returns zero, a negative errno, or a negative NFS4ERR status code.
    6093                 :            :  */
    6094                 :          0 : int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
    6095                 :            :                 unsigned short port, const struct cred *cred,
    6096                 :            :                 struct nfs4_setclientid_res *res)
    6097                 :            : {
    6098                 :            :         nfs4_verifier sc_verifier;
    6099                 :          0 :         struct nfs4_setclientid setclientid = {
    6100                 :            :                 .sc_verifier = &sc_verifier,
    6101                 :            :                 .sc_prog = program,
    6102                 :            :                 .sc_clnt = clp,
    6103                 :            :         };
    6104                 :          0 :         struct rpc_message msg = {
    6105                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
    6106                 :            :                 .rpc_argp = &setclientid,
    6107                 :            :                 .rpc_resp = res,
    6108                 :            :                 .rpc_cred = cred,
    6109                 :            :         };
    6110                 :          0 :         struct rpc_task_setup task_setup_data = {
    6111                 :          0 :                 .rpc_client = clp->cl_rpcclient,
    6112                 :            :                 .rpc_message = &msg,
    6113                 :            :                 .callback_ops = &nfs4_setclientid_ops,
    6114                 :            :                 .callback_data = &setclientid,
    6115                 :            :                 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
    6116                 :            :         };
    6117                 :          0 :         unsigned long now = jiffies;
    6118                 :            :         int status;
    6119                 :            : 
    6120                 :            :         /* nfs_client_id4 */
    6121                 :          0 :         nfs4_init_boot_verifier(clp, &sc_verifier);
    6122                 :            : 
    6123                 :          0 :         if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
    6124                 :          0 :                 status = nfs4_init_uniform_client_string(clp);
    6125                 :            :         else
    6126                 :          0 :                 status = nfs4_init_nonuniform_client_string(clp);
    6127                 :            : 
    6128                 :          0 :         if (status)
    6129                 :            :                 goto out;
    6130                 :            : 
    6131                 :            :         /* cb_client4 */
    6132                 :          0 :         setclientid.sc_netid_len =
    6133                 :          0 :                                 nfs4_init_callback_netid(clp,
    6134                 :            :                                                 setclientid.sc_netid,
    6135                 :            :                                                 sizeof(setclientid.sc_netid));
    6136                 :          0 :         setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
    6137                 :            :                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
    6138                 :          0 :                                 clp->cl_ipaddr, port >> 8, port & 255);
    6139                 :            : 
    6140                 :            :         dprintk("NFS call  setclientid auth=%s, '%s'\n",
    6141                 :            :                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
    6142                 :            :                 clp->cl_owner_id);
    6143                 :            : 
    6144                 :          0 :         status = nfs4_call_sync_custom(&task_setup_data);
    6145                 :          0 :         if (setclientid.sc_cred) {
    6146                 :          0 :                 kfree(clp->cl_acceptor);
    6147                 :          0 :                 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
    6148                 :          0 :                 put_rpccred(setclientid.sc_cred);
    6149                 :            :         }
    6150                 :            : 
    6151                 :          0 :         if (status == 0)
    6152                 :          0 :                 do_renew_lease(clp, now);
    6153                 :            : out:
    6154                 :          0 :         trace_nfs4_setclientid(clp, status);
    6155                 :            :         dprintk("NFS reply setclientid: %d\n", status);
    6156                 :          0 :         return status;
    6157                 :            : }
    6158                 :            : 
    6159                 :            : /**
    6160                 :            :  * nfs4_proc_setclientid_confirm - Confirm client ID
    6161                 :            :  * @clp: state data structure
    6162                 :            :  * @arg: result of a previous SETCLIENTID
    6163                 :            :  * @cred: credential to use for this call
    6164                 :            :  *
    6165                 :            :  * Returns zero, a negative errno, or a negative NFS4ERR status code.
    6166                 :            :  */
    6167                 :          0 : int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
    6168                 :            :                 struct nfs4_setclientid_res *arg,
    6169                 :            :                 const struct cred *cred)
    6170                 :            : {
    6171                 :          0 :         struct rpc_message msg = {
    6172                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
    6173                 :            :                 .rpc_argp = arg,
    6174                 :            :                 .rpc_cred = cred,
    6175                 :            :         };
    6176                 :            :         int status;
    6177                 :            : 
    6178                 :            :         dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
    6179                 :            :                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
    6180                 :            :                 clp->cl_clientid);
    6181                 :          0 :         status = rpc_call_sync(clp->cl_rpcclient, &msg,
    6182                 :            :                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
    6183                 :          0 :         trace_nfs4_setclientid_confirm(clp, status);
    6184                 :            :         dprintk("NFS reply setclientid_confirm: %d\n", status);
    6185                 :          0 :         return status;
    6186                 :            : }
    6187                 :            : 
    6188                 :            : struct nfs4_delegreturndata {
    6189                 :            :         struct nfs4_delegreturnargs args;
    6190                 :            :         struct nfs4_delegreturnres res;
    6191                 :            :         struct nfs_fh fh;
    6192                 :            :         nfs4_stateid stateid;
    6193                 :            :         unsigned long timestamp;
    6194                 :            :         struct {
    6195                 :            :                 struct nfs4_layoutreturn_args arg;
    6196                 :            :                 struct nfs4_layoutreturn_res res;
    6197                 :            :                 struct nfs4_xdr_opaque_data ld_private;
    6198                 :            :                 u32 roc_barrier;
    6199                 :            :                 bool roc;
    6200                 :            :         } lr;
    6201                 :            :         struct nfs_fattr fattr;
    6202                 :            :         int rpc_status;
    6203                 :            :         struct inode *inode;
    6204                 :            : };
    6205                 :            : 
    6206                 :          0 : static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
    6207                 :            : {
    6208                 :            :         struct nfs4_delegreturndata *data = calldata;
    6209                 :          0 :         struct nfs4_exception exception = {
    6210                 :          0 :                 .inode = data->inode,
    6211                 :          0 :                 .stateid = &data->stateid,
    6212                 :            :         };
    6213                 :            : 
    6214                 :          0 :         if (!nfs4_sequence_done(task, &data->res.seq_res))
    6215                 :          0 :                 return;
    6216                 :            : 
    6217                 :          0 :         trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
    6218                 :            : 
    6219                 :            :         /* Handle Layoutreturn errors */
    6220                 :          0 :         if (pnfs_roc_done(task, data->inode,
    6221                 :            :                                 &data->args.lr_args,
    6222                 :            :                                 &data->res.lr_res,
    6223                 :            :                                 &data->res.lr_ret) == -EAGAIN)
    6224                 :            :                 goto out_restart;
    6225                 :            : 
    6226                 :          0 :         switch (task->tk_status) {
    6227                 :            :         case 0:
    6228                 :          0 :                 renew_lease(data->res.server, data->timestamp);
    6229                 :          0 :                 break;
    6230                 :            :         case -NFS4ERR_ADMIN_REVOKED:
    6231                 :            :         case -NFS4ERR_DELEG_REVOKED:
    6232                 :            :         case -NFS4ERR_EXPIRED:
    6233                 :          0 :                 nfs4_free_revoked_stateid(data->res.server,
    6234                 :            :                                 data->args.stateid,
    6235                 :            :                                 task->tk_msg.rpc_cred);
    6236                 :            :                 /* Fallthrough */
    6237                 :            :         case -NFS4ERR_BAD_STATEID:
    6238                 :            :         case -NFS4ERR_STALE_STATEID:
    6239                 :          0 :                 task->tk_status = 0;
    6240                 :          0 :                 break;
    6241                 :            :         case -NFS4ERR_OLD_STATEID:
    6242                 :          0 :                 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
    6243                 :            :                         goto out_restart;
    6244                 :          0 :                 task->tk_status = 0;
    6245                 :          0 :                 break;
    6246                 :            :         case -NFS4ERR_ACCESS:
    6247                 :          0 :                 if (data->args.bitmask) {
    6248                 :          0 :                         data->args.bitmask = NULL;
    6249                 :          0 :                         data->res.fattr = NULL;
    6250                 :          0 :                         goto out_restart;
    6251                 :            :                 }
    6252                 :            :                 /* Fallthrough */
    6253                 :            :         default:
    6254                 :          0 :                 task->tk_status = nfs4_async_handle_exception(task,
    6255                 :            :                                 data->res.server, task->tk_status,
    6256                 :            :                                 &exception);
    6257                 :          0 :                 if (exception.retry)
    6258                 :            :                         goto out_restart;
    6259                 :            :         }
    6260                 :          0 :         data->rpc_status = task->tk_status;
    6261                 :          0 :         return;
    6262                 :            : out_restart:
    6263                 :          0 :         task->tk_status = 0;
    6264                 :          0 :         rpc_restart_call_prepare(task);
    6265                 :            : }
    6266                 :            : 
    6267                 :          0 : static void nfs4_delegreturn_release(void *calldata)
    6268                 :            : {
    6269                 :            :         struct nfs4_delegreturndata *data = calldata;
    6270                 :          0 :         struct inode *inode = data->inode;
    6271                 :            : 
    6272                 :          0 :         if (inode) {
    6273                 :          0 :                 if (data->lr.roc)
    6274                 :          0 :                         pnfs_roc_release(&data->lr.arg, &data->lr.res,
    6275                 :            :                                         data->res.lr_ret);
    6276                 :          0 :                 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
    6277                 :          0 :                 nfs_iput_and_deactive(inode);
    6278                 :            :         }
    6279                 :          0 :         kfree(calldata);
    6280                 :          0 : }
    6281                 :            : 
    6282                 :          0 : static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
    6283                 :            : {
    6284                 :            :         struct nfs4_delegreturndata *d_data;
    6285                 :            :         struct pnfs_layout_hdr *lo;
    6286                 :            : 
    6287                 :            :         d_data = (struct nfs4_delegreturndata *)data;
    6288                 :            : 
    6289                 :          0 :         if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
    6290                 :          0 :                 nfs4_sequence_done(task, &d_data->res.seq_res);
    6291                 :          0 :                 return;
    6292                 :            :         }
    6293                 :            : 
    6294                 :          0 :         lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
    6295                 :          0 :         if (lo && !pnfs_layout_is_valid(lo)) {
    6296                 :          0 :                 d_data->args.lr_args = NULL;
    6297                 :          0 :                 d_data->res.lr_res = NULL;
    6298                 :            :         }
    6299                 :            : 
    6300                 :          0 :         nfs4_setup_sequence(d_data->res.server->nfs_client,
    6301                 :            :                         &d_data->args.seq_args,
    6302                 :            :                         &d_data->res.seq_res,
    6303                 :            :                         task);
    6304                 :            : }
    6305                 :            : 
    6306                 :            : static const struct rpc_call_ops nfs4_delegreturn_ops = {
    6307                 :            :         .rpc_call_prepare = nfs4_delegreturn_prepare,
    6308                 :            :         .rpc_call_done = nfs4_delegreturn_done,
    6309                 :            :         .rpc_release = nfs4_delegreturn_release,
    6310                 :            : };
    6311                 :            : 
    6312                 :          0 : static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
    6313                 :            : {
    6314                 :            :         struct nfs4_delegreturndata *data;
    6315                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    6316                 :            :         struct rpc_task *task;
    6317                 :          0 :         struct rpc_message msg = {
    6318                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
    6319                 :            :                 .rpc_cred = cred,
    6320                 :            :         };
    6321                 :          0 :         struct rpc_task_setup task_setup_data = {
    6322                 :          0 :                 .rpc_client = server->client,
    6323                 :            :                 .rpc_message = &msg,
    6324                 :            :                 .callback_ops = &nfs4_delegreturn_ops,
    6325                 :            :                 .flags = RPC_TASK_ASYNC,
    6326                 :            :         };
    6327                 :            :         int status = 0;
    6328                 :            : 
    6329                 :          0 :         data = kzalloc(sizeof(*data), GFP_NOFS);
    6330                 :          0 :         if (data == NULL)
    6331                 :            :                 return -ENOMEM;
    6332                 :            :         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
    6333                 :            : 
    6334                 :          0 :         nfs4_state_protect(server->nfs_client,
    6335                 :            :                         NFS_SP4_MACH_CRED_CLEANUP,
    6336                 :            :                         &task_setup_data.rpc_client, &msg);
    6337                 :            : 
    6338                 :          0 :         data->args.fhandle = &data->fh;
    6339                 :          0 :         data->args.stateid = &data->stateid;
    6340                 :          0 :         data->args.bitmask = server->cache_consistency_bitmask;
    6341                 :            :         nfs_copy_fh(&data->fh, NFS_FH(inode));
    6342                 :            :         nfs4_stateid_copy(&data->stateid, stateid);
    6343                 :          0 :         data->res.fattr = &data->fattr;
    6344                 :          0 :         data->res.server = server;
    6345                 :          0 :         data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
    6346                 :          0 :         data->lr.arg.ld_private = &data->lr.ld_private;
    6347                 :          0 :         nfs_fattr_init(data->res.fattr);
    6348                 :          0 :         data->timestamp = jiffies;
    6349                 :          0 :         data->rpc_status = 0;
    6350                 :          0 :         data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
    6351                 :          0 :         data->inode = nfs_igrab_and_active(inode);
    6352                 :          0 :         if (data->inode) {
    6353                 :          0 :                 if (data->lr.roc) {
    6354                 :          0 :                         data->args.lr_args = &data->lr.arg;
    6355                 :          0 :                         data->res.lr_res = &data->lr.res;
    6356                 :            :                 }
    6357                 :          0 :         } else if (data->lr.roc) {
    6358                 :          0 :                 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
    6359                 :          0 :                 data->lr.roc = false;
    6360                 :            :         }
    6361                 :            : 
    6362                 :          0 :         task_setup_data.callback_data = data;
    6363                 :          0 :         msg.rpc_argp = &data->args;
    6364                 :          0 :         msg.rpc_resp = &data->res;
    6365                 :          0 :         task = rpc_run_task(&task_setup_data);
    6366                 :          0 :         if (IS_ERR(task))
    6367                 :          0 :                 return PTR_ERR(task);
    6368                 :          0 :         if (!issync)
    6369                 :            :                 goto out;
    6370                 :            :         status = rpc_wait_for_completion_task(task);
    6371                 :          0 :         if (status != 0)
    6372                 :            :                 goto out;
    6373                 :          0 :         status = data->rpc_status;
    6374                 :            : out:
    6375                 :          0 :         rpc_put_task(task);
    6376                 :          0 :         return status;
    6377                 :            : }
    6378                 :            : 
    6379                 :          0 : int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
    6380                 :            : {
    6381                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    6382                 :          0 :         struct nfs4_exception exception = { };
    6383                 :            :         int err;
    6384                 :            :         do {
    6385                 :          0 :                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
    6386                 :          0 :                 trace_nfs4_delegreturn(inode, stateid, err);
    6387                 :          0 :                 switch (err) {
    6388                 :            :                         case -NFS4ERR_STALE_STATEID:
    6389                 :            :                         case -NFS4ERR_EXPIRED:
    6390                 :            :                         case 0:
    6391                 :            :                                 return 0;
    6392                 :            :                 }
    6393                 :          0 :                 err = nfs4_handle_exception(server, err, &exception);
    6394                 :          0 :         } while (exception.retry);
    6395                 :          0 :         return err;
    6396                 :            : }
    6397                 :            : 
    6398                 :          0 : static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
    6399                 :            : {
    6400                 :          0 :         struct inode *inode = state->inode;
    6401                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    6402                 :          0 :         struct nfs_client *clp = server->nfs_client;
    6403                 :          0 :         struct nfs_lockt_args arg = {
    6404                 :            :                 .fh = NFS_FH(inode),
    6405                 :            :                 .fl = request,
    6406                 :            :         };
    6407                 :          0 :         struct nfs_lockt_res res = {
    6408                 :            :                 .denied = request,
    6409                 :            :         };
    6410                 :          0 :         struct rpc_message msg = {
    6411                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
    6412                 :            :                 .rpc_argp       = &arg,
    6413                 :            :                 .rpc_resp       = &res,
    6414                 :          0 :                 .rpc_cred       = state->owner->so_cred,
    6415                 :            :         };
    6416                 :            :         struct nfs4_lock_state *lsp;
    6417                 :            :         int status;
    6418                 :            : 
    6419                 :          0 :         arg.lock_owner.clientid = clp->cl_clientid;
    6420                 :          0 :         status = nfs4_set_lock_state(state, request);
    6421                 :          0 :         if (status != 0)
    6422                 :            :                 goto out;
    6423                 :          0 :         lsp = request->fl_u.nfs4_fl.owner;
    6424                 :          0 :         arg.lock_owner.id = lsp->ls_seqid.owner_id;
    6425                 :          0 :         arg.lock_owner.s_dev = server->s_dev;
    6426                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
    6427                 :          0 :         switch (status) {
    6428                 :            :                 case 0:
    6429                 :          0 :                         request->fl_type = F_UNLCK;
    6430                 :          0 :                         break;
    6431                 :            :                 case -NFS4ERR_DENIED:
    6432                 :            :                         status = 0;
    6433                 :            :         }
    6434                 :          0 :         request->fl_ops->fl_release_private(request);
    6435                 :          0 :         request->fl_ops = NULL;
    6436                 :            : out:
    6437                 :          0 :         return status;
    6438                 :            : }
    6439                 :            : 
    6440                 :          0 : static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
    6441                 :            : {
    6442                 :          0 :         struct nfs4_exception exception = {
    6443                 :            :                 .interruptible = true,
    6444                 :            :         };
    6445                 :            :         int err;
    6446                 :            : 
    6447                 :            :         do {
    6448                 :          0 :                 err = _nfs4_proc_getlk(state, cmd, request);
    6449                 :          0 :                 trace_nfs4_get_lock(request, state, cmd, err);
    6450                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
    6451                 :            :                                 &exception);
    6452                 :          0 :         } while (exception.retry);
    6453                 :          0 :         return err;
    6454                 :            : }
    6455                 :            : 
    6456                 :            : /*
    6457                 :            :  * Update the seqid of a lock stateid after receiving
    6458                 :            :  * NFS4ERR_OLD_STATEID
    6459                 :            :  */
    6460                 :          0 : static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
    6461                 :            :                 struct nfs4_lock_state *lsp)
    6462                 :            : {
    6463                 :          0 :         struct nfs4_state *state = lsp->ls_state;
    6464                 :            :         bool ret = false;
    6465                 :            : 
    6466                 :            :         spin_lock(&state->state_lock);
    6467                 :          0 :         if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
    6468                 :            :                 goto out;
    6469                 :          0 :         if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
    6470                 :            :                 nfs4_stateid_seqid_inc(dst);
    6471                 :            :         else
    6472                 :          0 :                 dst->seqid = lsp->ls_stateid.seqid;
    6473                 :            :         ret = true;
    6474                 :            : out:
    6475                 :            :         spin_unlock(&state->state_lock);
    6476                 :          0 :         return ret;
    6477                 :            : }
    6478                 :            : 
    6479                 :          0 : static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
    6480                 :            :                 struct nfs4_lock_state *lsp)
    6481                 :            : {
    6482                 :          0 :         struct nfs4_state *state = lsp->ls_state;
    6483                 :            :         bool ret;
    6484                 :            : 
    6485                 :            :         spin_lock(&state->state_lock);
    6486                 :          0 :         ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
    6487                 :            :         nfs4_stateid_copy(dst, &lsp->ls_stateid);
    6488                 :            :         spin_unlock(&state->state_lock);
    6489                 :          0 :         return ret;
    6490                 :            : }
    6491                 :            : 
    6492                 :            : struct nfs4_unlockdata {
    6493                 :            :         struct nfs_locku_args arg;
    6494                 :            :         struct nfs_locku_res res;
    6495                 :            :         struct nfs4_lock_state *lsp;
    6496                 :            :         struct nfs_open_context *ctx;
    6497                 :            :         struct nfs_lock_context *l_ctx;
    6498                 :            :         struct file_lock fl;
    6499                 :            :         struct nfs_server *server;
    6500                 :            :         unsigned long timestamp;
    6501                 :            : };
    6502                 :            : 
    6503                 :          0 : static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
    6504                 :            :                 struct nfs_open_context *ctx,
    6505                 :            :                 struct nfs4_lock_state *lsp,
    6506                 :            :                 struct nfs_seqid *seqid)
    6507                 :            : {
    6508                 :            :         struct nfs4_unlockdata *p;
    6509                 :          0 :         struct nfs4_state *state = lsp->ls_state;
    6510                 :          0 :         struct inode *inode = state->inode;
    6511                 :            : 
    6512                 :          0 :         p = kzalloc(sizeof(*p), GFP_NOFS);
    6513                 :          0 :         if (p == NULL)
    6514                 :            :                 return NULL;
    6515                 :          0 :         p->arg.fh = NFS_FH(inode);
    6516                 :          0 :         p->arg.fl = &p->fl;
    6517                 :          0 :         p->arg.seqid = seqid;
    6518                 :          0 :         p->res.seqid = seqid;
    6519                 :          0 :         p->lsp = lsp;
    6520                 :            :         /* Ensure we don't close file until we're done freeing locks! */
    6521                 :          0 :         p->ctx = get_nfs_open_context(ctx);
    6522                 :          0 :         p->l_ctx = nfs_get_lock_context(ctx);
    6523                 :          0 :         locks_init_lock(&p->fl);
    6524                 :          0 :         locks_copy_lock(&p->fl, fl);
    6525                 :          0 :         p->server = NFS_SERVER(inode);
    6526                 :            :         spin_lock(&state->state_lock);
    6527                 :            :         nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
    6528                 :            :         spin_unlock(&state->state_lock);
    6529                 :          0 :         return p;
    6530                 :            : }
    6531                 :            : 
    6532                 :          0 : static void nfs4_locku_release_calldata(void *data)
    6533                 :            : {
    6534                 :            :         struct nfs4_unlockdata *calldata = data;
    6535                 :          0 :         nfs_free_seqid(calldata->arg.seqid);
    6536                 :          0 :         nfs4_put_lock_state(calldata->lsp);
    6537                 :          0 :         nfs_put_lock_context(calldata->l_ctx);
    6538                 :          0 :         put_nfs_open_context(calldata->ctx);
    6539                 :          0 :         kfree(calldata);
    6540                 :          0 : }
    6541                 :            : 
    6542                 :          0 : static void nfs4_locku_done(struct rpc_task *task, void *data)
    6543                 :            : {
    6544                 :            :         struct nfs4_unlockdata *calldata = data;
    6545                 :          0 :         struct nfs4_exception exception = {
    6546                 :          0 :                 .inode = calldata->lsp->ls_state->inode,
    6547                 :          0 :                 .stateid = &calldata->arg.stateid,
    6548                 :            :         };
    6549                 :            : 
    6550                 :          0 :         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
    6551                 :          0 :                 return;
    6552                 :          0 :         switch (task->tk_status) {
    6553                 :            :                 case 0:
    6554                 :          0 :                         renew_lease(calldata->server, calldata->timestamp);
    6555                 :          0 :                         locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
    6556                 :          0 :                         if (nfs4_update_lock_stateid(calldata->lsp,
    6557                 :          0 :                                         &calldata->res.stateid))
    6558                 :            :                                 break;
    6559                 :            :                         /* Fall through */
    6560                 :            :                 case -NFS4ERR_ADMIN_REVOKED:
    6561                 :            :                 case -NFS4ERR_EXPIRED:
    6562                 :          0 :                         nfs4_free_revoked_stateid(calldata->server,
    6563                 :            :                                         &calldata->arg.stateid,
    6564                 :            :                                         task->tk_msg.rpc_cred);
    6565                 :            :                         /* Fall through */
    6566                 :            :                 case -NFS4ERR_BAD_STATEID:
    6567                 :            :                 case -NFS4ERR_STALE_STATEID:
    6568                 :          0 :                         if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
    6569                 :            :                                                 calldata->lsp))
    6570                 :          0 :                                 rpc_restart_call_prepare(task);
    6571                 :            :                         break;
    6572                 :            :                 case -NFS4ERR_OLD_STATEID:
    6573                 :          0 :                         if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
    6574                 :            :                                                 calldata->lsp))
    6575                 :          0 :                                 rpc_restart_call_prepare(task);
    6576                 :            :                         break;
    6577                 :            :                 default:
    6578                 :          0 :                         task->tk_status = nfs4_async_handle_exception(task,
    6579                 :            :                                         calldata->server, task->tk_status,
    6580                 :            :                                         &exception);
    6581                 :          0 :                         if (exception.retry)
    6582                 :          0 :                                 rpc_restart_call_prepare(task);
    6583                 :            :         }
    6584                 :          0 :         nfs_release_seqid(calldata->arg.seqid);
    6585                 :            : }
    6586                 :            : 
    6587                 :          0 : static void nfs4_locku_prepare(struct rpc_task *task, void *data)
    6588                 :            : {
    6589                 :            :         struct nfs4_unlockdata *calldata = data;
    6590                 :            : 
    6591                 :          0 :         if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
    6592                 :          0 :                 nfs_async_iocounter_wait(task, calldata->l_ctx))
    6593                 :            :                 return;
    6594                 :            : 
    6595                 :          0 :         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
    6596                 :            :                 goto out_wait;
    6597                 :          0 :         if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
    6598                 :            :                 /* Note: exit _without_ running nfs4_locku_done */
    6599                 :            :                 goto out_no_action;
    6600                 :            :         }
    6601                 :          0 :         calldata->timestamp = jiffies;
    6602                 :          0 :         if (nfs4_setup_sequence(calldata->server->nfs_client,
    6603                 :            :                                 &calldata->arg.seq_args,
    6604                 :            :                                 &calldata->res.seq_res,
    6605                 :            :                                 task) != 0)
    6606                 :          0 :                 nfs_release_seqid(calldata->arg.seqid);
    6607                 :            :         return;
    6608                 :            : out_no_action:
    6609                 :          0 :         task->tk_action = NULL;
    6610                 :            : out_wait:
    6611                 :          0 :         nfs4_sequence_done(task, &calldata->res.seq_res);
    6612                 :            : }
    6613                 :            : 
    6614                 :            : static const struct rpc_call_ops nfs4_locku_ops = {
    6615                 :            :         .rpc_call_prepare = nfs4_locku_prepare,
    6616                 :            :         .rpc_call_done = nfs4_locku_done,
    6617                 :            :         .rpc_release = nfs4_locku_release_calldata,
    6618                 :            : };
    6619                 :            : 
    6620                 :          0 : static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
    6621                 :            :                 struct nfs_open_context *ctx,
    6622                 :            :                 struct nfs4_lock_state *lsp,
    6623                 :            :                 struct nfs_seqid *seqid)
    6624                 :            : {
    6625                 :            :         struct nfs4_unlockdata *data;
    6626                 :          0 :         struct rpc_message msg = {
    6627                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
    6628                 :          0 :                 .rpc_cred = ctx->cred,
    6629                 :            :         };
    6630                 :          0 :         struct rpc_task_setup task_setup_data = {
    6631                 :          0 :                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
    6632                 :            :                 .rpc_message = &msg,
    6633                 :            :                 .callback_ops = &nfs4_locku_ops,
    6634                 :            :                 .workqueue = nfsiod_workqueue,
    6635                 :            :                 .flags = RPC_TASK_ASYNC,
    6636                 :            :         };
    6637                 :            : 
    6638                 :          0 :         nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
    6639                 :            :                 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
    6640                 :            : 
    6641                 :            :         /* Ensure this is an unlock - when canceling a lock, the
    6642                 :            :          * canceled lock is passed in, and it won't be an unlock.
    6643                 :            :          */
    6644                 :          0 :         fl->fl_type = F_UNLCK;
    6645                 :          0 :         if (fl->fl_flags & FL_CLOSE)
    6646                 :          0 :                 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
    6647                 :            : 
    6648                 :          0 :         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
    6649                 :          0 :         if (data == NULL) {
    6650                 :          0 :                 nfs_free_seqid(seqid);
    6651                 :          0 :                 return ERR_PTR(-ENOMEM);
    6652                 :            :         }
    6653                 :            : 
    6654                 :            :         nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
    6655                 :          0 :         msg.rpc_argp = &data->arg;
    6656                 :          0 :         msg.rpc_resp = &data->res;
    6657                 :          0 :         task_setup_data.callback_data = data;
    6658                 :          0 :         return rpc_run_task(&task_setup_data);
    6659                 :            : }
    6660                 :            : 
    6661                 :          0 : static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
    6662                 :            : {
    6663                 :          0 :         struct inode *inode = state->inode;
    6664                 :          0 :         struct nfs4_state_owner *sp = state->owner;
    6665                 :            :         struct nfs_inode *nfsi = NFS_I(inode);
    6666                 :            :         struct nfs_seqid *seqid;
    6667                 :            :         struct nfs4_lock_state *lsp;
    6668                 :            :         struct rpc_task *task;
    6669                 :            :         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
    6670                 :            :         int status = 0;
    6671                 :          0 :         unsigned char fl_flags = request->fl_flags;
    6672                 :            : 
    6673                 :          0 :         status = nfs4_set_lock_state(state, request);
    6674                 :            :         /* Unlock _before_ we do the RPC call */
    6675                 :          0 :         request->fl_flags |= FL_EXISTS;
    6676                 :            :         /* Exclude nfs_delegation_claim_locks() */
    6677                 :          0 :         mutex_lock(&sp->so_delegreturn_mutex);
    6678                 :            :         /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
    6679                 :          0 :         down_read(&nfsi->rwsem);
    6680                 :          0 :         if (locks_lock_inode_wait(inode, request) == -ENOENT) {
    6681                 :          0 :                 up_read(&nfsi->rwsem);
    6682                 :          0 :                 mutex_unlock(&sp->so_delegreturn_mutex);
    6683                 :          0 :                 goto out;
    6684                 :            :         }
    6685                 :          0 :         up_read(&nfsi->rwsem);
    6686                 :          0 :         mutex_unlock(&sp->so_delegreturn_mutex);
    6687                 :          0 :         if (status != 0)
    6688                 :            :                 goto out;
    6689                 :            :         /* Is this a delegated lock? */
    6690                 :          0 :         lsp = request->fl_u.nfs4_fl.owner;
    6691                 :          0 :         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
    6692                 :            :                 goto out;
    6693                 :          0 :         alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
    6694                 :          0 :         seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
    6695                 :            :         status = -ENOMEM;
    6696                 :          0 :         if (IS_ERR(seqid))
    6697                 :            :                 goto out;
    6698                 :          0 :         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
    6699                 :            :         status = PTR_ERR(task);
    6700                 :          0 :         if (IS_ERR(task))
    6701                 :            :                 goto out;
    6702                 :            :         status = rpc_wait_for_completion_task(task);
    6703                 :          0 :         rpc_put_task(task);
    6704                 :            : out:
    6705                 :          0 :         request->fl_flags = fl_flags;
    6706                 :          0 :         trace_nfs4_unlock(request, state, F_SETLK, status);
    6707                 :          0 :         return status;
    6708                 :            : }
    6709                 :            : 
    6710                 :            : struct nfs4_lockdata {
    6711                 :            :         struct nfs_lock_args arg;
    6712                 :            :         struct nfs_lock_res res;
    6713                 :            :         struct nfs4_lock_state *lsp;
    6714                 :            :         struct nfs_open_context *ctx;
    6715                 :            :         struct file_lock fl;
    6716                 :            :         unsigned long timestamp;
    6717                 :            :         int rpc_status;
    6718                 :            :         int cancelled;
    6719                 :            :         struct nfs_server *server;
    6720                 :            : };
    6721                 :            : 
    6722                 :          0 : static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
    6723                 :            :                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
    6724                 :            :                 gfp_t gfp_mask)
    6725                 :            : {
    6726                 :            :         struct nfs4_lockdata *p;
    6727                 :          0 :         struct inode *inode = lsp->ls_state->inode;
    6728                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    6729                 :            :         struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
    6730                 :            : 
    6731                 :          0 :         p = kzalloc(sizeof(*p), gfp_mask);
    6732                 :          0 :         if (p == NULL)
    6733                 :            :                 return NULL;
    6734                 :            : 
    6735                 :          0 :         p->arg.fh = NFS_FH(inode);
    6736                 :          0 :         p->arg.fl = &p->fl;
    6737                 :          0 :         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
    6738                 :          0 :         if (IS_ERR(p->arg.open_seqid))
    6739                 :            :                 goto out_free;
    6740                 :          0 :         alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
    6741                 :          0 :         p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
    6742                 :          0 :         if (IS_ERR(p->arg.lock_seqid))
    6743                 :            :                 goto out_free_seqid;
    6744                 :          0 :         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
    6745                 :          0 :         p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
    6746                 :          0 :         p->arg.lock_owner.s_dev = server->s_dev;
    6747                 :          0 :         p->res.lock_seqid = p->arg.lock_seqid;
    6748                 :          0 :         p->lsp = lsp;
    6749                 :          0 :         p->server = server;
    6750                 :          0 :         p->ctx = get_nfs_open_context(ctx);
    6751                 :          0 :         locks_init_lock(&p->fl);
    6752                 :          0 :         locks_copy_lock(&p->fl, fl);
    6753                 :          0 :         return p;
    6754                 :            : out_free_seqid:
    6755                 :          0 :         nfs_free_seqid(p->arg.open_seqid);
    6756                 :            : out_free:
    6757                 :          0 :         kfree(p);
    6758                 :          0 :         return NULL;
    6759                 :            : }
    6760                 :            : 
    6761                 :          0 : static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
    6762                 :            : {
    6763                 :            :         struct nfs4_lockdata *data = calldata;
    6764                 :          0 :         struct nfs4_state *state = data->lsp->ls_state;
    6765                 :            : 
    6766                 :            :         dprintk("%s: begin!\n", __func__);
    6767                 :          0 :         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
    6768                 :            :                 goto out_wait;
    6769                 :            :         /* Do we need to do an open_to_lock_owner? */
    6770                 :          0 :         if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
    6771                 :          0 :                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
    6772                 :            :                         goto out_release_lock_seqid;
    6773                 :            :                 }
    6774                 :            :                 nfs4_stateid_copy(&data->arg.open_stateid,
    6775                 :            :                                 &state->open_stateid);
    6776                 :          0 :                 data->arg.new_lock_owner = 1;
    6777                 :          0 :                 data->res.open_seqid = data->arg.open_seqid;
    6778                 :            :         } else {
    6779                 :          0 :                 data->arg.new_lock_owner = 0;
    6780                 :            :                 nfs4_stateid_copy(&data->arg.lock_stateid,
    6781                 :            :                                 &data->lsp->ls_stateid);
    6782                 :            :         }
    6783                 :          0 :         if (!nfs4_valid_open_stateid(state)) {
    6784                 :          0 :                 data->rpc_status = -EBADF;
    6785                 :          0 :                 task->tk_action = NULL;
    6786                 :          0 :                 goto out_release_open_seqid;
    6787                 :            :         }
    6788                 :          0 :         data->timestamp = jiffies;
    6789                 :          0 :         if (nfs4_setup_sequence(data->server->nfs_client,
    6790                 :            :                                 &data->arg.seq_args,
    6791                 :            :                                 &data->res.seq_res,
    6792                 :            :                                 task) == 0)
    6793                 :          0 :                 return;
    6794                 :            : out_release_open_seqid:
    6795                 :          0 :         nfs_release_seqid(data->arg.open_seqid);
    6796                 :            : out_release_lock_seqid:
    6797                 :          0 :         nfs_release_seqid(data->arg.lock_seqid);
    6798                 :            : out_wait:
    6799                 :          0 :         nfs4_sequence_done(task, &data->res.seq_res);
    6800                 :            :         dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
    6801                 :            : }
    6802                 :            : 
    6803                 :          0 : static void nfs4_lock_done(struct rpc_task *task, void *calldata)
    6804                 :            : {
    6805                 :            :         struct nfs4_lockdata *data = calldata;
    6806                 :          0 :         struct nfs4_lock_state *lsp = data->lsp;
    6807                 :            : 
    6808                 :            :         dprintk("%s: begin!\n", __func__);
    6809                 :            : 
    6810                 :          0 :         if (!nfs4_sequence_done(task, &data->res.seq_res))
    6811                 :            :                 return;
    6812                 :            : 
    6813                 :          0 :         data->rpc_status = task->tk_status;
    6814                 :          0 :         switch (task->tk_status) {
    6815                 :            :         case 0:
    6816                 :          0 :                 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
    6817                 :            :                                 data->timestamp);
    6818                 :          0 :                 if (data->arg.new_lock && !data->cancelled) {
    6819                 :          0 :                         data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
    6820                 :          0 :                         if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
    6821                 :            :                                 goto out_restart;
    6822                 :            :                 }
    6823                 :          0 :                 if (data->arg.new_lock_owner != 0) {
    6824                 :          0 :                         nfs_confirm_seqid(&lsp->ls_seqid, 0);
    6825                 :            :                         nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
    6826                 :          0 :                         set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
    6827                 :          0 :                 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
    6828                 :            :                         goto out_restart;
    6829                 :            :                 break;
    6830                 :            :         case -NFS4ERR_BAD_STATEID:
    6831                 :            :         case -NFS4ERR_OLD_STATEID:
    6832                 :            :         case -NFS4ERR_STALE_STATEID:
    6833                 :            :         case -NFS4ERR_EXPIRED:
    6834                 :          0 :                 if (data->arg.new_lock_owner != 0) {
    6835                 :          0 :                         if (!nfs4_stateid_match(&data->arg.open_stateid,
    6836                 :          0 :                                                 &lsp->ls_state->open_stateid))
    6837                 :            :                                 goto out_restart;
    6838                 :          0 :                 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
    6839                 :            :                                                 &lsp->ls_stateid))
    6840                 :            :                                 goto out_restart;
    6841                 :            :         }
    6842                 :            : out_done:
    6843                 :            :         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
    6844                 :            :         return;
    6845                 :            : out_restart:
    6846                 :          0 :         if (!data->cancelled)
    6847                 :          0 :                 rpc_restart_call_prepare(task);
    6848                 :            :         goto out_done;
    6849                 :            : }
    6850                 :            : 
    6851                 :          0 : static void nfs4_lock_release(void *calldata)
    6852                 :            : {
    6853                 :            :         struct nfs4_lockdata *data = calldata;
    6854                 :            : 
    6855                 :            :         dprintk("%s: begin!\n", __func__);
    6856                 :          0 :         nfs_free_seqid(data->arg.open_seqid);
    6857                 :          0 :         if (data->cancelled && data->rpc_status == 0) {
    6858                 :            :                 struct rpc_task *task;
    6859                 :          0 :                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
    6860                 :            :                                 data->arg.lock_seqid);
    6861                 :          0 :                 if (!IS_ERR(task))
    6862                 :          0 :                         rpc_put_task_async(task);
    6863                 :            :                 dprintk("%s: cancelling lock!\n", __func__);
    6864                 :            :         } else
    6865                 :          0 :                 nfs_free_seqid(data->arg.lock_seqid);
    6866                 :          0 :         nfs4_put_lock_state(data->lsp);
    6867                 :          0 :         put_nfs_open_context(data->ctx);
    6868                 :          0 :         kfree(data);
    6869                 :            :         dprintk("%s: done!\n", __func__);
    6870                 :          0 : }
    6871                 :            : 
    6872                 :            : static const struct rpc_call_ops nfs4_lock_ops = {
    6873                 :            :         .rpc_call_prepare = nfs4_lock_prepare,
    6874                 :            :         .rpc_call_done = nfs4_lock_done,
    6875                 :            :         .rpc_release = nfs4_lock_release,
    6876                 :            : };
    6877                 :            : 
    6878                 :          0 : static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
    6879                 :            : {
    6880                 :          0 :         switch (error) {
    6881                 :            :         case -NFS4ERR_ADMIN_REVOKED:
    6882                 :            :         case -NFS4ERR_EXPIRED:
    6883                 :            :         case -NFS4ERR_BAD_STATEID:
    6884                 :          0 :                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
    6885                 :          0 :                 if (new_lock_owner != 0 ||
    6886                 :            :                    test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
    6887                 :          0 :                         nfs4_schedule_stateid_recovery(server, lsp->ls_state);
    6888                 :            :                 break;
    6889                 :            :         case -NFS4ERR_STALE_STATEID:
    6890                 :          0 :                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
    6891                 :          0 :                 nfs4_schedule_lease_recovery(server->nfs_client);
    6892                 :            :         };
    6893                 :          0 : }
    6894                 :            : 
    6895                 :          0 : static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
    6896                 :            : {
    6897                 :            :         struct nfs4_lockdata *data;
    6898                 :            :         struct rpc_task *task;
    6899                 :          0 :         struct rpc_message msg = {
    6900                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
    6901                 :          0 :                 .rpc_cred = state->owner->so_cred,
    6902                 :            :         };
    6903                 :          0 :         struct rpc_task_setup task_setup_data = {
    6904                 :          0 :                 .rpc_client = NFS_CLIENT(state->inode),
    6905                 :            :                 .rpc_message = &msg,
    6906                 :            :                 .callback_ops = &nfs4_lock_ops,
    6907                 :            :                 .workqueue = nfsiod_workqueue,
    6908                 :            :                 .flags = RPC_TASK_ASYNC,
    6909                 :            :         };
    6910                 :            :         int ret;
    6911                 :            : 
    6912                 :            :         dprintk("%s: begin!\n", __func__);
    6913                 :          0 :         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
    6914                 :            :                         fl->fl_u.nfs4_fl.owner,
    6915                 :            :                         recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
    6916                 :          0 :         if (data == NULL)
    6917                 :            :                 return -ENOMEM;
    6918                 :          0 :         if (IS_SETLKW(cmd))
    6919                 :          0 :                 data->arg.block = 1;
    6920                 :          0 :         nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
    6921                 :            :                                 recovery_type > NFS_LOCK_NEW);
    6922                 :          0 :         msg.rpc_argp = &data->arg;
    6923                 :          0 :         msg.rpc_resp = &data->res;
    6924                 :          0 :         task_setup_data.callback_data = data;
    6925                 :          0 :         if (recovery_type > NFS_LOCK_NEW) {
    6926                 :          0 :                 if (recovery_type == NFS_LOCK_RECLAIM)
    6927                 :          0 :                         data->arg.reclaim = NFS_LOCK_RECLAIM;
    6928                 :            :         } else
    6929                 :          0 :                 data->arg.new_lock = 1;
    6930                 :          0 :         task = rpc_run_task(&task_setup_data);
    6931                 :          0 :         if (IS_ERR(task))
    6932                 :          0 :                 return PTR_ERR(task);
    6933                 :            :         ret = rpc_wait_for_completion_task(task);
    6934                 :          0 :         if (ret == 0) {
    6935                 :          0 :                 ret = data->rpc_status;
    6936                 :          0 :                 if (ret)
    6937                 :          0 :                         nfs4_handle_setlk_error(data->server, data->lsp,
    6938                 :          0 :                                         data->arg.new_lock_owner, ret);
    6939                 :            :         } else
    6940                 :          0 :                 data->cancelled = true;
    6941                 :          0 :         rpc_put_task(task);
    6942                 :            :         dprintk("%s: done, ret = %d!\n", __func__, ret);
    6943                 :          0 :         trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
    6944                 :          0 :         return ret;
    6945                 :            : }
    6946                 :            : 
    6947                 :          0 : static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
    6948                 :            : {
    6949                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    6950                 :          0 :         struct nfs4_exception exception = {
    6951                 :            :                 .inode = state->inode,
    6952                 :            :         };
    6953                 :            :         int err;
    6954                 :            : 
    6955                 :            :         do {
    6956                 :            :                 /* Cache the lock if possible... */
    6957                 :          0 :                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
    6958                 :            :                         return 0;
    6959                 :          0 :                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
    6960                 :          0 :                 if (err != -NFS4ERR_DELAY)
    6961                 :            :                         break;
    6962                 :          0 :                 nfs4_handle_exception(server, err, &exception);
    6963                 :          0 :         } while (exception.retry);
    6964                 :          0 :         return err;
    6965                 :            : }
    6966                 :            : 
    6967                 :          0 : static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
    6968                 :            : {
    6969                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    6970                 :          0 :         struct nfs4_exception exception = {
    6971                 :            :                 .inode = state->inode,
    6972                 :            :         };
    6973                 :            :         int err;
    6974                 :            : 
    6975                 :          0 :         err = nfs4_set_lock_state(state, request);
    6976                 :          0 :         if (err != 0)
    6977                 :            :                 return err;
    6978                 :          0 :         if (!recover_lost_locks) {
    6979                 :          0 :                 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
    6980                 :          0 :                 return 0;
    6981                 :            :         }
    6982                 :            :         do {
    6983                 :          0 :                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
    6984                 :            :                         return 0;
    6985                 :          0 :                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
    6986                 :          0 :                 switch (err) {
    6987                 :            :                 default:
    6988                 :            :                         goto out;
    6989                 :            :                 case -NFS4ERR_GRACE:
    6990                 :            :                 case -NFS4ERR_DELAY:
    6991                 :          0 :                         nfs4_handle_exception(server, err, &exception);
    6992                 :            :                         err = 0;
    6993                 :            :                 }
    6994                 :          0 :         } while (exception.retry);
    6995                 :            : out:
    6996                 :          0 :         return err;
    6997                 :            : }
    6998                 :            : 
    6999                 :            : #if defined(CONFIG_NFS_V4_1)
    7000                 :          0 : static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
    7001                 :            : {
    7002                 :            :         struct nfs4_lock_state *lsp;
    7003                 :            :         int status;
    7004                 :            : 
    7005                 :          0 :         status = nfs4_set_lock_state(state, request);
    7006                 :          0 :         if (status != 0)
    7007                 :            :                 return status;
    7008                 :          0 :         lsp = request->fl_u.nfs4_fl.owner;
    7009                 :          0 :         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
    7010                 :            :             test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
    7011                 :            :                 return 0;
    7012                 :          0 :         return nfs4_lock_expired(state, request);
    7013                 :            : }
    7014                 :            : #endif
    7015                 :            : 
    7016                 :          0 : static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
    7017                 :            : {
    7018                 :          0 :         struct nfs_inode *nfsi = NFS_I(state->inode);
    7019                 :          0 :         struct nfs4_state_owner *sp = state->owner;
    7020                 :          0 :         unsigned char fl_flags = request->fl_flags;
    7021                 :            :         int status;
    7022                 :            : 
    7023                 :          0 :         request->fl_flags |= FL_ACCESS;
    7024                 :          0 :         status = locks_lock_inode_wait(state->inode, request);
    7025                 :          0 :         if (status < 0)
    7026                 :            :                 goto out;
    7027                 :          0 :         mutex_lock(&sp->so_delegreturn_mutex);
    7028                 :          0 :         down_read(&nfsi->rwsem);
    7029                 :          0 :         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
    7030                 :            :                 /* Yes: cache locks! */
    7031                 :            :                 /* ...but avoid races with delegation recall... */
    7032                 :          0 :                 request->fl_flags = fl_flags & ~FL_SLEEP;
    7033                 :          0 :                 status = locks_lock_inode_wait(state->inode, request);
    7034                 :          0 :                 up_read(&nfsi->rwsem);
    7035                 :          0 :                 mutex_unlock(&sp->so_delegreturn_mutex);
    7036                 :          0 :                 goto out;
    7037                 :            :         }
    7038                 :          0 :         up_read(&nfsi->rwsem);
    7039                 :          0 :         mutex_unlock(&sp->so_delegreturn_mutex);
    7040                 :          0 :         status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
    7041                 :            : out:
    7042                 :          0 :         request->fl_flags = fl_flags;
    7043                 :          0 :         return status;
    7044                 :            : }
    7045                 :            : 
    7046                 :          0 : static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
    7047                 :            : {
    7048                 :          0 :         struct nfs4_exception exception = {
    7049                 :            :                 .state = state,
    7050                 :          0 :                 .inode = state->inode,
    7051                 :            :                 .interruptible = true,
    7052                 :            :         };
    7053                 :            :         int err;
    7054                 :            : 
    7055                 :            :         do {
    7056                 :          0 :                 err = _nfs4_proc_setlk(state, cmd, request);
    7057                 :          0 :                 if (err == -NFS4ERR_DENIED)
    7058                 :            :                         err = -EAGAIN;
    7059                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
    7060                 :            :                                 err, &exception);
    7061                 :          0 :         } while (exception.retry);
    7062                 :          0 :         return err;
    7063                 :            : }
    7064                 :            : 
    7065                 :            : #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
    7066                 :            : #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
    7067                 :            : 
    7068                 :            : static int
    7069                 :          0 : nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
    7070                 :            :                         struct file_lock *request)
    7071                 :            : {
    7072                 :            :         int             status = -ERESTARTSYS;
    7073                 :            :         unsigned long   timeout = NFS4_LOCK_MINTIMEOUT;
    7074                 :            : 
    7075                 :          0 :         while(!signalled()) {
    7076                 :          0 :                 status = nfs4_proc_setlk(state, cmd, request);
    7077                 :          0 :                 if ((status != -EAGAIN) || IS_SETLK(cmd))
    7078                 :            :                         break;
    7079                 :          0 :                 freezable_schedule_timeout_interruptible(timeout);
    7080                 :          0 :                 timeout *= 2;
    7081                 :          0 :                 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
    7082                 :            :                 status = -ERESTARTSYS;
    7083                 :            :         }
    7084                 :          0 :         return status;
    7085                 :            : }
    7086                 :            : 
    7087                 :            : #ifdef CONFIG_NFS_V4_1
    7088                 :            : struct nfs4_lock_waiter {
    7089                 :            :         struct task_struct      *task;
    7090                 :            :         struct inode            *inode;
    7091                 :            :         struct nfs_lowner       *owner;
    7092                 :            : };
    7093                 :            : 
    7094                 :            : static int
    7095                 :          0 : nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
    7096                 :            : {
    7097                 :            :         int ret;
    7098                 :          0 :         struct nfs4_lock_waiter *waiter = wait->private;
    7099                 :            : 
    7100                 :            :         /* NULL key means to wake up everyone */
    7101                 :          0 :         if (key) {
    7102                 :            :                 struct cb_notify_lock_args      *cbnl = key;
    7103                 :            :                 struct nfs_lowner               *lowner = &cbnl->cbnl_owner,
    7104                 :          0 :                                                 *wowner = waiter->owner;
    7105                 :            : 
    7106                 :            :                 /* Only wake if the callback was for the same owner. */
    7107                 :          0 :                 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
    7108                 :            :                         return 0;
    7109                 :            : 
    7110                 :            :                 /* Make sure it's for the right inode */
    7111                 :          0 :                 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
    7112                 :            :                         return 0;
    7113                 :            :         }
    7114                 :            : 
    7115                 :            :         /* override "private" so we can use default_wake_function */
    7116                 :          0 :         wait->private = waiter->task;
    7117                 :          0 :         ret = woken_wake_function(wait, mode, flags, key);
    7118                 :          0 :         if (ret)
    7119                 :          0 :                 list_del_init(&wait->entry);
    7120                 :          0 :         wait->private = waiter;
    7121                 :          0 :         return ret;
    7122                 :            : }
    7123                 :            : 
    7124                 :            : static int
    7125                 :          0 : nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
    7126                 :            : {
    7127                 :            :         int status = -ERESTARTSYS;
    7128                 :          0 :         struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
    7129                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    7130                 :          0 :         struct nfs_client *clp = server->nfs_client;
    7131                 :          0 :         wait_queue_head_t *q = &clp->cl_lock_waitq;
    7132                 :          0 :         struct nfs_lowner owner = { .clientid = clp->cl_clientid,
    7133                 :          0 :                                     .id = lsp->ls_seqid.owner_id,
    7134                 :          0 :                                     .s_dev = server->s_dev };
    7135                 :          0 :         struct nfs4_lock_waiter waiter = { .task  = current,
    7136                 :            :                                            .inode = state->inode,
    7137                 :            :                                            .owner = &owner};
    7138                 :            :         wait_queue_entry_t wait;
    7139                 :            : 
    7140                 :            :         /* Don't bother with waitqueue if we don't expect a callback */
    7141                 :          0 :         if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
    7142                 :          0 :                 return nfs4_retry_setlk_simple(state, cmd, request);
    7143                 :            : 
    7144                 :          0 :         init_wait(&wait);
    7145                 :          0 :         wait.private = &waiter;
    7146                 :          0 :         wait.func = nfs4_wake_lock_waiter;
    7147                 :            : 
    7148                 :          0 :         while(!signalled()) {
    7149                 :          0 :                 add_wait_queue(q, &wait);
    7150                 :          0 :                 status = nfs4_proc_setlk(state, cmd, request);
    7151                 :          0 :                 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
    7152                 :          0 :                         finish_wait(q, &wait);
    7153                 :          0 :                         break;
    7154                 :            :                 }
    7155                 :            : 
    7156                 :            :                 status = -ERESTARTSYS;
    7157                 :            :                 freezer_do_not_count();
    7158                 :          0 :                 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
    7159                 :          0 :                 freezer_count();
    7160                 :          0 :                 finish_wait(q, &wait);
    7161                 :            :         }
    7162                 :            : 
    7163                 :          0 :         return status;
    7164                 :            : }
    7165                 :            : #else /* !CONFIG_NFS_V4_1 */
    7166                 :            : static inline int
    7167                 :            : nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
    7168                 :            : {
    7169                 :            :         return nfs4_retry_setlk_simple(state, cmd, request);
    7170                 :            : }
    7171                 :            : #endif
    7172                 :            : 
    7173                 :            : static int
    7174                 :          0 : nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
    7175                 :            : {
    7176                 :            :         struct nfs_open_context *ctx;
    7177                 :            :         struct nfs4_state *state;
    7178                 :            :         int status;
    7179                 :            : 
    7180                 :            :         /* verify open state */
    7181                 :            :         ctx = nfs_file_open_context(filp);
    7182                 :          0 :         state = ctx->state;
    7183                 :            : 
    7184                 :          0 :         if (IS_GETLK(cmd)) {
    7185                 :          0 :                 if (state != NULL)
    7186                 :          0 :                         return nfs4_proc_getlk(state, F_GETLK, request);
    7187                 :            :                 return 0;
    7188                 :            :         }
    7189                 :            : 
    7190                 :          0 :         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
    7191                 :            :                 return -EINVAL;
    7192                 :            : 
    7193                 :          0 :         if (request->fl_type == F_UNLCK) {
    7194                 :          0 :                 if (state != NULL)
    7195                 :          0 :                         return nfs4_proc_unlck(state, cmd, request);
    7196                 :            :                 return 0;
    7197                 :            :         }
    7198                 :            : 
    7199                 :          0 :         if (state == NULL)
    7200                 :            :                 return -ENOLCK;
    7201                 :            : 
    7202                 :          0 :         if ((request->fl_flags & FL_POSIX) &&
    7203                 :            :             !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
    7204                 :            :                 return -ENOLCK;
    7205                 :            : 
    7206                 :            :         /*
    7207                 :            :          * Don't rely on the VFS having checked the file open mode,
    7208                 :            :          * since it won't do this for flock() locks.
    7209                 :            :          */
    7210                 :          0 :         switch (request->fl_type) {
    7211                 :            :         case F_RDLCK:
    7212                 :          0 :                 if (!(filp->f_mode & FMODE_READ))
    7213                 :            :                         return -EBADF;
    7214                 :            :                 break;
    7215                 :            :         case F_WRLCK:
    7216                 :          0 :                 if (!(filp->f_mode & FMODE_WRITE))
    7217                 :            :                         return -EBADF;
    7218                 :            :         }
    7219                 :            : 
    7220                 :          0 :         status = nfs4_set_lock_state(state, request);
    7221                 :          0 :         if (status != 0)
    7222                 :            :                 return status;
    7223                 :            : 
    7224                 :          0 :         return nfs4_retry_setlk(state, cmd, request);
    7225                 :            : }
    7226                 :            : 
    7227                 :          0 : int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
    7228                 :            : {
    7229                 :          0 :         struct nfs_server *server = NFS_SERVER(state->inode);
    7230                 :            :         int err;
    7231                 :            : 
    7232                 :          0 :         err = nfs4_set_lock_state(state, fl);
    7233                 :          0 :         if (err != 0)
    7234                 :            :                 return err;
    7235                 :          0 :         err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
    7236                 :          0 :         return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
    7237                 :            : }
    7238                 :            : 
    7239                 :            : struct nfs_release_lockowner_data {
    7240                 :            :         struct nfs4_lock_state *lsp;
    7241                 :            :         struct nfs_server *server;
    7242                 :            :         struct nfs_release_lockowner_args args;
    7243                 :            :         struct nfs_release_lockowner_res res;
    7244                 :            :         unsigned long timestamp;
    7245                 :            : };
    7246                 :            : 
    7247                 :          0 : static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
    7248                 :            : {
    7249                 :            :         struct nfs_release_lockowner_data *data = calldata;
    7250                 :          0 :         struct nfs_server *server = data->server;
    7251                 :          0 :         nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
    7252                 :            :                            &data->res.seq_res, task);
    7253                 :          0 :         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
    7254                 :          0 :         data->timestamp = jiffies;
    7255                 :          0 : }
    7256                 :            : 
    7257                 :          0 : static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
    7258                 :            : {
    7259                 :            :         struct nfs_release_lockowner_data *data = calldata;
    7260                 :          0 :         struct nfs_server *server = data->server;
    7261                 :            : 
    7262                 :          0 :         nfs40_sequence_done(task, &data->res.seq_res);
    7263                 :            : 
    7264                 :          0 :         switch (task->tk_status) {
    7265                 :            :         case 0:
    7266                 :          0 :                 renew_lease(server, data->timestamp);
    7267                 :          0 :                 break;
    7268                 :            :         case -NFS4ERR_STALE_CLIENTID:
    7269                 :            :         case -NFS4ERR_EXPIRED:
    7270                 :          0 :                 nfs4_schedule_lease_recovery(server->nfs_client);
    7271                 :          0 :                 break;
    7272                 :            :         case -NFS4ERR_LEASE_MOVED:
    7273                 :            :         case -NFS4ERR_DELAY:
    7274                 :          0 :                 if (nfs4_async_handle_error(task, server,
    7275                 :            :                                             NULL, NULL) == -EAGAIN)
    7276                 :          0 :                         rpc_restart_call_prepare(task);
    7277                 :            :         }
    7278                 :          0 : }
    7279                 :            : 
    7280                 :          0 : static void nfs4_release_lockowner_release(void *calldata)
    7281                 :            : {
    7282                 :            :         struct nfs_release_lockowner_data *data = calldata;
    7283                 :          0 :         nfs4_free_lock_state(data->server, data->lsp);
    7284                 :          0 :         kfree(calldata);
    7285                 :          0 : }
    7286                 :            : 
    7287                 :            : static const struct rpc_call_ops nfs4_release_lockowner_ops = {
    7288                 :            :         .rpc_call_prepare = nfs4_release_lockowner_prepare,
    7289                 :            :         .rpc_call_done = nfs4_release_lockowner_done,
    7290                 :            :         .rpc_release = nfs4_release_lockowner_release,
    7291                 :            : };
    7292                 :            : 
    7293                 :            : static void
    7294                 :          0 : nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
    7295                 :            : {
    7296                 :            :         struct nfs_release_lockowner_data *data;
    7297                 :          0 :         struct rpc_message msg = {
    7298                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
    7299                 :            :         };
    7300                 :            : 
    7301                 :          0 :         if (server->nfs_client->cl_mvops->minor_version != 0)
    7302                 :          0 :                 return;
    7303                 :            : 
    7304                 :            :         data = kmalloc(sizeof(*data), GFP_NOFS);
    7305                 :          0 :         if (!data)
    7306                 :            :                 return;
    7307                 :          0 :         data->lsp = lsp;
    7308                 :          0 :         data->server = server;
    7309                 :          0 :         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
    7310                 :          0 :         data->args.lock_owner.id = lsp->ls_seqid.owner_id;
    7311                 :          0 :         data->args.lock_owner.s_dev = server->s_dev;
    7312                 :            : 
    7313                 :          0 :         msg.rpc_argp = &data->args;
    7314                 :          0 :         msg.rpc_resp = &data->res;
    7315                 :            :         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
    7316                 :          0 :         rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
    7317                 :            : }
    7318                 :            : 
    7319                 :            : #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
    7320                 :            : 
    7321                 :          0 : static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
    7322                 :            :                                    struct dentry *unused, struct inode *inode,
    7323                 :            :                                    const char *key, const void *buf,
    7324                 :            :                                    size_t buflen, int flags)
    7325                 :            : {
    7326                 :          0 :         return nfs4_proc_set_acl(inode, buf, buflen);
    7327                 :            : }
    7328                 :            : 
    7329                 :          0 : static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
    7330                 :            :                                    struct dentry *unused, struct inode *inode,
    7331                 :            :                                    const char *key, void *buf, size_t buflen)
    7332                 :            : {
    7333                 :          0 :         return nfs4_proc_get_acl(inode, buf, buflen);
    7334                 :            : }
    7335                 :            : 
    7336                 :          0 : static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
    7337                 :            : {
    7338                 :          0 :         return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
    7339                 :            : }
    7340                 :            : 
    7341                 :            : #ifdef CONFIG_NFS_V4_SECURITY_LABEL
    7342                 :            : 
    7343                 :          0 : static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
    7344                 :            :                                      struct dentry *unused, struct inode *inode,
    7345                 :            :                                      const char *key, const void *buf,
    7346                 :            :                                      size_t buflen, int flags)
    7347                 :            : {
    7348                 :          0 :         if (security_ismaclabel(key))
    7349                 :          0 :                 return nfs4_set_security_label(inode, buf, buflen);
    7350                 :            : 
    7351                 :            :         return -EOPNOTSUPP;
    7352                 :            : }
    7353                 :            : 
    7354                 :          0 : static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
    7355                 :            :                                      struct dentry *unused, struct inode *inode,
    7356                 :            :                                      const char *key, void *buf, size_t buflen)
    7357                 :            : {
    7358                 :          0 :         if (security_ismaclabel(key))
    7359                 :          0 :                 return nfs4_get_security_label(inode, buf, buflen);
    7360                 :            :         return -EOPNOTSUPP;
    7361                 :            : }
    7362                 :            : 
    7363                 :            : static ssize_t
    7364                 :          0 : nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
    7365                 :            : {
    7366                 :            :         int len = 0;
    7367                 :            : 
    7368                 :          0 :         if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
    7369                 :          0 :                 len = security_inode_listsecurity(inode, list, list_len);
    7370                 :          0 :                 if (list_len && len > list_len)
    7371                 :            :                         return -ERANGE;
    7372                 :            :         }
    7373                 :          0 :         return len;
    7374                 :            : }
    7375                 :            : 
    7376                 :            : static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
    7377                 :            :         .prefix = XATTR_SECURITY_PREFIX,
    7378                 :            :         .get    = nfs4_xattr_get_nfs4_label,
    7379                 :            :         .set    = nfs4_xattr_set_nfs4_label,
    7380                 :            : };
    7381                 :            : 
    7382                 :            : #else
    7383                 :            : 
    7384                 :            : static ssize_t
    7385                 :            : nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
    7386                 :            : {
    7387                 :            :         return 0;
    7388                 :            : }
    7389                 :            : 
    7390                 :            : #endif
    7391                 :            : 
    7392                 :            : /*
    7393                 :            :  * nfs_fhget will use either the mounted_on_fileid or the fileid
    7394                 :            :  */
    7395                 :            : static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
    7396                 :            : {
    7397                 :          0 :         if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
    7398                 :            :                (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
    7399                 :            :               (fattr->valid & NFS_ATTR_FATTR_FSID) &&
    7400                 :            :               (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
    7401                 :            :                 return;
    7402                 :            : 
    7403                 :          0 :         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
    7404                 :            :                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
    7405                 :          0 :         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
    7406                 :          0 :         fattr->nlink = 2;
    7407                 :            : }
    7408                 :            : 
    7409                 :          0 : static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
    7410                 :            :                                    const struct qstr *name,
    7411                 :            :                                    struct nfs4_fs_locations *fs_locations,
    7412                 :            :                                    struct page *page)
    7413                 :            : {
    7414                 :            :         struct nfs_server *server = NFS_SERVER(dir);
    7415                 :            :         u32 bitmask[3];
    7416                 :          0 :         struct nfs4_fs_locations_arg args = {
    7417                 :            :                 .dir_fh = NFS_FH(dir),
    7418                 :            :                 .name = name,
    7419                 :            :                 .page = page,
    7420                 :            :                 .bitmask = bitmask,
    7421                 :            :         };
    7422                 :          0 :         struct nfs4_fs_locations_res res = {
    7423                 :            :                 .fs_locations = fs_locations,
    7424                 :            :         };
    7425                 :          0 :         struct rpc_message msg = {
    7426                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
    7427                 :            :                 .rpc_argp = &args,
    7428                 :            :                 .rpc_resp = &res,
    7429                 :            :         };
    7430                 :            :         int status;
    7431                 :            : 
    7432                 :            :         dprintk("%s: start\n", __func__);
    7433                 :            : 
    7434                 :          0 :         bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
    7435                 :          0 :         bitmask[1] = nfs4_fattr_bitmap[1];
    7436                 :            : 
    7437                 :            :         /* Ask for the fileid of the absent filesystem if mounted_on_fileid
    7438                 :            :          * is not supported */
    7439                 :          0 :         if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
    7440                 :          0 :                 bitmask[0] &= ~FATTR4_WORD0_FILEID;
    7441                 :            :         else
    7442                 :          0 :                 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
    7443                 :            : 
    7444                 :          0 :         nfs_fattr_init(&fs_locations->fattr);
    7445                 :          0 :         fs_locations->server = server;
    7446                 :          0 :         fs_locations->nlocations = 0;
    7447                 :            :         status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
    7448                 :            :         dprintk("%s: returned status = %d\n", __func__, status);
    7449                 :          0 :         return status;
    7450                 :            : }
    7451                 :            : 
    7452                 :          0 : int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
    7453                 :            :                            const struct qstr *name,
    7454                 :            :                            struct nfs4_fs_locations *fs_locations,
    7455                 :            :                            struct page *page)
    7456                 :            : {
    7457                 :          0 :         struct nfs4_exception exception = {
    7458                 :            :                 .interruptible = true,
    7459                 :            :         };
    7460                 :            :         int err;
    7461                 :            :         do {
    7462                 :          0 :                 err = _nfs4_proc_fs_locations(client, dir, name,
    7463                 :            :                                 fs_locations, page);
    7464                 :          0 :                 trace_nfs4_get_fs_locations(dir, name, err);
    7465                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
    7466                 :            :                                 &exception);
    7467                 :          0 :         } while (exception.retry);
    7468                 :          0 :         return err;
    7469                 :            : }
    7470                 :            : 
    7471                 :            : /*
    7472                 :            :  * This operation also signals the server that this client is
    7473                 :            :  * performing migration recovery.  The server can stop returning
    7474                 :            :  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
    7475                 :            :  * appended to this compound to identify the client ID which is
    7476                 :            :  * performing recovery.
    7477                 :            :  */
    7478                 :          0 : static int _nfs40_proc_get_locations(struct inode *inode,
    7479                 :            :                                      struct nfs4_fs_locations *locations,
    7480                 :            :                                      struct page *page, const struct cred *cred)
    7481                 :            : {
    7482                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    7483                 :          0 :         struct rpc_clnt *clnt = server->client;
    7484                 :          0 :         u32 bitmask[2] = {
    7485                 :            :                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
    7486                 :            :         };
    7487                 :          0 :         struct nfs4_fs_locations_arg args = {
    7488                 :          0 :                 .clientid       = server->nfs_client->cl_clientid,
    7489                 :            :                 .fh             = NFS_FH(inode),
    7490                 :            :                 .page           = page,
    7491                 :            :                 .bitmask        = bitmask,
    7492                 :            :                 .migration      = 1,            /* skip LOOKUP */
    7493                 :            :                 .renew          = 1,            /* append RENEW */
    7494                 :            :         };
    7495                 :          0 :         struct nfs4_fs_locations_res res = {
    7496                 :            :                 .fs_locations   = locations,
    7497                 :            :                 .migration      = 1,
    7498                 :            :                 .renew          = 1,
    7499                 :            :         };
    7500                 :          0 :         struct rpc_message msg = {
    7501                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
    7502                 :            :                 .rpc_argp       = &args,
    7503                 :            :                 .rpc_resp       = &res,
    7504                 :            :                 .rpc_cred       = cred,
    7505                 :            :         };
    7506                 :          0 :         unsigned long now = jiffies;
    7507                 :            :         int status;
    7508                 :            : 
    7509                 :          0 :         nfs_fattr_init(&locations->fattr);
    7510                 :          0 :         locations->server = server;
    7511                 :          0 :         locations->nlocations = 0;
    7512                 :            : 
    7513                 :            :         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
    7514                 :          0 :         status = nfs4_call_sync_sequence(clnt, server, &msg,
    7515                 :            :                                         &args.seq_args, &res.seq_res);
    7516                 :          0 :         if (status)
    7517                 :            :                 return status;
    7518                 :            : 
    7519                 :          0 :         renew_lease(server, now);
    7520                 :          0 :         return 0;
    7521                 :            : }
    7522                 :            : 
    7523                 :            : #ifdef CONFIG_NFS_V4_1
    7524                 :            : 
    7525                 :            : /*
    7526                 :            :  * This operation also signals the server that this client is
    7527                 :            :  * performing migration recovery.  The server can stop asserting
    7528                 :            :  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
    7529                 :            :  * performing this operation is identified in the SEQUENCE
    7530                 :            :  * operation in this compound.
    7531                 :            :  *
    7532                 :            :  * When the client supports GETATTR(fs_locations_info), it can
    7533                 :            :  * be plumbed in here.
    7534                 :            :  */
    7535                 :          0 : static int _nfs41_proc_get_locations(struct inode *inode,
    7536                 :            :                                      struct nfs4_fs_locations *locations,
    7537                 :            :                                      struct page *page, const struct cred *cred)
    7538                 :            : {
    7539                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    7540                 :          0 :         struct rpc_clnt *clnt = server->client;
    7541                 :          0 :         u32 bitmask[2] = {
    7542                 :            :                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
    7543                 :            :         };
    7544                 :          0 :         struct nfs4_fs_locations_arg args = {
    7545                 :            :                 .fh             = NFS_FH(inode),
    7546                 :            :                 .page           = page,
    7547                 :            :                 .bitmask        = bitmask,
    7548                 :            :                 .migration      = 1,            /* skip LOOKUP */
    7549                 :            :         };
    7550                 :          0 :         struct nfs4_fs_locations_res res = {
    7551                 :            :                 .fs_locations   = locations,
    7552                 :            :                 .migration      = 1,
    7553                 :            :         };
    7554                 :          0 :         struct rpc_message msg = {
    7555                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
    7556                 :            :                 .rpc_argp       = &args,
    7557                 :            :                 .rpc_resp       = &res,
    7558                 :            :                 .rpc_cred       = cred,
    7559                 :            :         };
    7560                 :            :         int status;
    7561                 :            : 
    7562                 :          0 :         nfs_fattr_init(&locations->fattr);
    7563                 :          0 :         locations->server = server;
    7564                 :          0 :         locations->nlocations = 0;
    7565                 :            : 
    7566                 :            :         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
    7567                 :          0 :         status = nfs4_call_sync_sequence(clnt, server, &msg,
    7568                 :            :                                         &args.seq_args, &res.seq_res);
    7569                 :          0 :         if (status == NFS4_OK &&
    7570                 :          0 :             res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
    7571                 :            :                 status = -NFS4ERR_LEASE_MOVED;
    7572                 :          0 :         return status;
    7573                 :            : }
    7574                 :            : 
    7575                 :            : #endif  /* CONFIG_NFS_V4_1 */
    7576                 :            : 
    7577                 :            : /**
    7578                 :            :  * nfs4_proc_get_locations - discover locations for a migrated FSID
    7579                 :            :  * @inode: inode on FSID that is migrating
    7580                 :            :  * @locations: result of query
    7581                 :            :  * @page: buffer
    7582                 :            :  * @cred: credential to use for this operation
    7583                 :            :  *
    7584                 :            :  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
    7585                 :            :  * operation failed, or a negative errno if a local error occurred.
    7586                 :            :  *
    7587                 :            :  * On success, "locations" is filled in, but if the server has
    7588                 :            :  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
    7589                 :            :  * asserted.
    7590                 :            :  *
    7591                 :            :  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
    7592                 :            :  * from this client that require migration recovery.
    7593                 :            :  */
    7594                 :          0 : int nfs4_proc_get_locations(struct inode *inode,
    7595                 :            :                             struct nfs4_fs_locations *locations,
    7596                 :            :                             struct page *page, const struct cred *cred)
    7597                 :            : {
    7598                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    7599                 :          0 :         struct nfs_client *clp = server->nfs_client;
    7600                 :          0 :         const struct nfs4_mig_recovery_ops *ops =
    7601                 :          0 :                                         clp->cl_mvops->mig_recovery_ops;
    7602                 :          0 :         struct nfs4_exception exception = {
    7603                 :            :                 .interruptible = true,
    7604                 :            :         };
    7605                 :            :         int status;
    7606                 :            : 
    7607                 :            :         dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
    7608                 :            :                 (unsigned long long)server->fsid.major,
    7609                 :            :                 (unsigned long long)server->fsid.minor,
    7610                 :            :                 clp->cl_hostname);
    7611                 :            :         nfs_display_fhandle(NFS_FH(inode), __func__);
    7612                 :            : 
    7613                 :            :         do {
    7614                 :          0 :                 status = ops->get_locations(inode, locations, page, cred);
    7615                 :          0 :                 if (status != -NFS4ERR_DELAY)
    7616                 :            :                         break;
    7617                 :          0 :                 nfs4_handle_exception(server, status, &exception);
    7618                 :          0 :         } while (exception.retry);
    7619                 :          0 :         return status;
    7620                 :            : }
    7621                 :            : 
    7622                 :            : /*
    7623                 :            :  * This operation also signals the server that this client is
    7624                 :            :  * performing "lease moved" recovery.  The server can stop
    7625                 :            :  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
    7626                 :            :  * is appended to this compound to identify the client ID which is
    7627                 :            :  * performing recovery.
    7628                 :            :  */
    7629                 :          0 : static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
    7630                 :            : {
    7631                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    7632                 :          0 :         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
    7633                 :          0 :         struct rpc_clnt *clnt = server->client;
    7634                 :          0 :         struct nfs4_fsid_present_arg args = {
    7635                 :            :                 .fh             = NFS_FH(inode),
    7636                 :          0 :                 .clientid       = clp->cl_clientid,
    7637                 :            :                 .renew          = 1,            /* append RENEW */
    7638                 :            :         };
    7639                 :          0 :         struct nfs4_fsid_present_res res = {
    7640                 :            :                 .renew          = 1,
    7641                 :            :         };
    7642                 :          0 :         struct rpc_message msg = {
    7643                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
    7644                 :            :                 .rpc_argp       = &args,
    7645                 :            :                 .rpc_resp       = &res,
    7646                 :            :                 .rpc_cred       = cred,
    7647                 :            :         };
    7648                 :          0 :         unsigned long now = jiffies;
    7649                 :            :         int status;
    7650                 :            : 
    7651                 :          0 :         res.fh = nfs_alloc_fhandle();
    7652                 :          0 :         if (res.fh == NULL)
    7653                 :            :                 return -ENOMEM;
    7654                 :            : 
    7655                 :            :         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
    7656                 :          0 :         status = nfs4_call_sync_sequence(clnt, server, &msg,
    7657                 :            :                                                 &args.seq_args, &res.seq_res);
    7658                 :          0 :         nfs_free_fhandle(res.fh);
    7659                 :          0 :         if (status)
    7660                 :            :                 return status;
    7661                 :            : 
    7662                 :          0 :         do_renew_lease(clp, now);
    7663                 :          0 :         return 0;
    7664                 :            : }
    7665                 :            : 
    7666                 :            : #ifdef CONFIG_NFS_V4_1
    7667                 :            : 
    7668                 :            : /*
    7669                 :            :  * This operation also signals the server that this client is
    7670                 :            :  * performing "lease moved" recovery.  The server can stop asserting
    7671                 :            :  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
    7672                 :            :  * this operation is identified in the SEQUENCE operation in this
    7673                 :            :  * compound.
    7674                 :            :  */
    7675                 :          0 : static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
    7676                 :            : {
    7677                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    7678                 :          0 :         struct rpc_clnt *clnt = server->client;
    7679                 :          0 :         struct nfs4_fsid_present_arg args = {
    7680                 :            :                 .fh             = NFS_FH(inode),
    7681                 :            :         };
    7682                 :          0 :         struct nfs4_fsid_present_res res = {
    7683                 :            :         };
    7684                 :          0 :         struct rpc_message msg = {
    7685                 :            :                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
    7686                 :            :                 .rpc_argp       = &args,
    7687                 :            :                 .rpc_resp       = &res,
    7688                 :            :                 .rpc_cred       = cred,
    7689                 :            :         };
    7690                 :            :         int status;
    7691                 :            : 
    7692                 :          0 :         res.fh = nfs_alloc_fhandle();
    7693                 :          0 :         if (res.fh == NULL)
    7694                 :            :                 return -ENOMEM;
    7695                 :            : 
    7696                 :            :         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
    7697                 :          0 :         status = nfs4_call_sync_sequence(clnt, server, &msg,
    7698                 :            :                                                 &args.seq_args, &res.seq_res);
    7699                 :          0 :         nfs_free_fhandle(res.fh);
    7700                 :          0 :         if (status == NFS4_OK &&
    7701                 :          0 :             res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
    7702                 :            :                 status = -NFS4ERR_LEASE_MOVED;
    7703                 :          0 :         return status;
    7704                 :            : }
    7705                 :            : 
    7706                 :            : #endif  /* CONFIG_NFS_V4_1 */
    7707                 :            : 
    7708                 :            : /**
    7709                 :            :  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
    7710                 :            :  * @inode: inode on FSID to check
    7711                 :            :  * @cred: credential to use for this operation
    7712                 :            :  *
    7713                 :            :  * Server indicates whether the FSID is present, moved, or not
    7714                 :            :  * recognized.  This operation is necessary to clear a LEASE_MOVED
    7715                 :            :  * condition for this client ID.
    7716                 :            :  *
    7717                 :            :  * Returns NFS4_OK if the FSID is present on this server,
    7718                 :            :  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
    7719                 :            :  *  NFS4ERR code if some error occurred on the server, or a
    7720                 :            :  *  negative errno if a local failure occurred.
    7721                 :            :  */
    7722                 :          0 : int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
    7723                 :            : {
    7724                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    7725                 :          0 :         struct nfs_client *clp = server->nfs_client;
    7726                 :          0 :         const struct nfs4_mig_recovery_ops *ops =
    7727                 :          0 :                                         clp->cl_mvops->mig_recovery_ops;
    7728                 :          0 :         struct nfs4_exception exception = {
    7729                 :            :                 .interruptible = true,
    7730                 :            :         };
    7731                 :            :         int status;
    7732                 :            : 
    7733                 :            :         dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
    7734                 :            :                 (unsigned long long)server->fsid.major,
    7735                 :            :                 (unsigned long long)server->fsid.minor,
    7736                 :            :                 clp->cl_hostname);
    7737                 :            :         nfs_display_fhandle(NFS_FH(inode), __func__);
    7738                 :            : 
    7739                 :            :         do {
    7740                 :          0 :                 status = ops->fsid_present(inode, cred);
    7741                 :          0 :                 if (status != -NFS4ERR_DELAY)
    7742                 :            :                         break;
    7743                 :          0 :                 nfs4_handle_exception(server, status, &exception);
    7744                 :          0 :         } while (exception.retry);
    7745                 :          0 :         return status;
    7746                 :            : }
    7747                 :            : 
    7748                 :            : /*
    7749                 :            :  * If 'use_integrity' is true and the state managment nfs_client
    7750                 :            :  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
    7751                 :            :  * and the machine credential as per RFC3530bis and RFC5661 Security
    7752                 :            :  * Considerations sections. Otherwise, just use the user cred with the
    7753                 :            :  * filesystem's rpc_client.
    7754                 :            :  */
    7755                 :          0 : static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
    7756                 :            : {
    7757                 :            :         int status;
    7758                 :          0 :         struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
    7759                 :          0 :         struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
    7760                 :          0 :         struct nfs4_secinfo_arg args = {
    7761                 :            :                 .dir_fh = NFS_FH(dir),
    7762                 :            :                 .name   = name,
    7763                 :            :         };
    7764                 :          0 :         struct nfs4_secinfo_res res = {
    7765                 :            :                 .flavors     = flavors,
    7766                 :            :         };
    7767                 :          0 :         struct rpc_message msg = {
    7768                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
    7769                 :            :                 .rpc_argp = &args,
    7770                 :            :                 .rpc_resp = &res,
    7771                 :            :         };
    7772                 :          0 :         struct nfs4_call_sync_data data = {
    7773                 :            :                 .seq_server = NFS_SERVER(dir),
    7774                 :            :                 .seq_args = &args.seq_args,
    7775                 :            :                 .seq_res = &res.seq_res,
    7776                 :            :         };
    7777                 :          0 :         struct rpc_task_setup task_setup = {
    7778                 :            :                 .rpc_client = clnt,
    7779                 :            :                 .rpc_message = &msg,
    7780                 :          0 :                 .callback_ops = clp->cl_mvops->call_sync_ops,
    7781                 :            :                 .callback_data = &data,
    7782                 :            :                 .flags = RPC_TASK_NO_ROUND_ROBIN,
    7783                 :            :         };
    7784                 :            :         const struct cred *cred = NULL;
    7785                 :            : 
    7786                 :          0 :         if (use_integrity) {
    7787                 :          0 :                 clnt = clp->cl_rpcclient;
    7788                 :          0 :                 task_setup.rpc_client = clnt;
    7789                 :            : 
    7790                 :          0 :                 cred = nfs4_get_clid_cred(clp);
    7791                 :          0 :                 msg.rpc_cred = cred;
    7792                 :            :         }
    7793                 :            : 
    7794                 :            :         dprintk("NFS call  secinfo %s\n", name->name);
    7795                 :            : 
    7796                 :            :         nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
    7797                 :            :         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
    7798                 :          0 :         status = nfs4_call_sync_custom(&task_setup);
    7799                 :            : 
    7800                 :            :         dprintk("NFS reply  secinfo: %d\n", status);
    7801                 :            : 
    7802                 :          0 :         put_cred(cred);
    7803                 :          0 :         return status;
    7804                 :            : }
    7805                 :            : 
    7806                 :          0 : int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
    7807                 :            :                       struct nfs4_secinfo_flavors *flavors)
    7808                 :            : {
    7809                 :          0 :         struct nfs4_exception exception = {
    7810                 :            :                 .interruptible = true,
    7811                 :            :         };
    7812                 :            :         int err;
    7813                 :            :         do {
    7814                 :            :                 err = -NFS4ERR_WRONGSEC;
    7815                 :            : 
    7816                 :            :                 /* try to use integrity protection with machine cred */
    7817                 :          0 :                 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
    7818                 :          0 :                         err = _nfs4_proc_secinfo(dir, name, flavors, true);
    7819                 :            : 
    7820                 :            :                 /*
    7821                 :            :                  * if unable to use integrity protection, or SECINFO with
    7822                 :            :                  * integrity protection returns NFS4ERR_WRONGSEC (which is
    7823                 :            :                  * disallowed by spec, but exists in deployed servers) use
    7824                 :            :                  * the current filesystem's rpc_client and the user cred.
    7825                 :            :                  */
    7826                 :          0 :                 if (err == -NFS4ERR_WRONGSEC)
    7827                 :          0 :                         err = _nfs4_proc_secinfo(dir, name, flavors, false);
    7828                 :            : 
    7829                 :          0 :                 trace_nfs4_secinfo(dir, name, err);
    7830                 :          0 :                 err = nfs4_handle_exception(NFS_SERVER(dir), err,
    7831                 :            :                                 &exception);
    7832                 :          0 :         } while (exception.retry);
    7833                 :          0 :         return err;
    7834                 :            : }
    7835                 :            : 
    7836                 :            : #ifdef CONFIG_NFS_V4_1
    7837                 :            : /*
    7838                 :            :  * Check the exchange flags returned by the server for invalid flags, having
    7839                 :            :  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
    7840                 :            :  * DS flags set.
    7841                 :            :  */
    7842                 :            : static int nfs4_check_cl_exchange_flags(u32 flags)
    7843                 :            : {
    7844                 :          0 :         if (flags & ~EXCHGID4_FLAG_MASK_R)
    7845                 :            :                 goto out_inval;
    7846                 :          0 :         if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
    7847                 :            :             (flags & EXCHGID4_FLAG_USE_NON_PNFS))
    7848                 :            :                 goto out_inval;
    7849                 :          0 :         if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
    7850                 :            :                 goto out_inval;
    7851                 :            :         return NFS_OK;
    7852                 :            : out_inval:
    7853                 :            :         return -NFS4ERR_INVAL;
    7854                 :            : }
    7855                 :            : 
    7856                 :            : static bool
    7857                 :            : nfs41_same_server_scope(struct nfs41_server_scope *a,
    7858                 :            :                         struct nfs41_server_scope *b)
    7859                 :            : {
    7860                 :          0 :         if (a->server_scope_sz != b->server_scope_sz)
    7861                 :            :                 return false;
    7862                 :          0 :         return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
    7863                 :            : }
    7864                 :            : 
    7865                 :            : static void
    7866                 :          0 : nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
    7867                 :            : {
    7868                 :          0 :         struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
    7869                 :          0 :         struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
    7870                 :          0 :         struct nfs_client *clp = args->client;
    7871                 :            : 
    7872                 :          0 :         switch (task->tk_status) {
    7873                 :            :         case -NFS4ERR_BADSESSION:
    7874                 :            :         case -NFS4ERR_DEADSESSION:
    7875                 :          0 :                 nfs4_schedule_session_recovery(clp->cl_session,
    7876                 :            :                                 task->tk_status);
    7877                 :            :         }
    7878                 :          0 :         if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
    7879                 :          0 :                         res->dir != NFS4_CDFS4_BOTH) {
    7880                 :            :                 rpc_task_close_connection(task);
    7881                 :          0 :                 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
    7882                 :          0 :                         rpc_restart_call(task);
    7883                 :            :         }
    7884                 :          0 : }
    7885                 :            : 
    7886                 :            : static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
    7887                 :            :         .rpc_call_done =  nfs4_bind_one_conn_to_session_done,
    7888                 :            : };
    7889                 :            : 
    7890                 :            : /*
    7891                 :            :  * nfs4_proc_bind_one_conn_to_session()
    7892                 :            :  *
    7893                 :            :  * The 4.1 client currently uses the same TCP connection for the
    7894                 :            :  * fore and backchannel.
    7895                 :            :  */
    7896                 :            : static
    7897                 :          0 : int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
    7898                 :            :                 struct rpc_xprt *xprt,
    7899                 :            :                 struct nfs_client *clp,
    7900                 :            :                 const struct cred *cred)
    7901                 :            : {
    7902                 :            :         int status;
    7903                 :          0 :         struct nfs41_bind_conn_to_session_args args = {
    7904                 :            :                 .client = clp,
    7905                 :            :                 .dir = NFS4_CDFC4_FORE_OR_BOTH,
    7906                 :            :                 .retries = 0,
    7907                 :            :         };
    7908                 :            :         struct nfs41_bind_conn_to_session_res res;
    7909                 :          0 :         struct rpc_message msg = {
    7910                 :            :                 .rpc_proc =
    7911                 :            :                         &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
    7912                 :            :                 .rpc_argp = &args,
    7913                 :            :                 .rpc_resp = &res,
    7914                 :            :                 .rpc_cred = cred,
    7915                 :            :         };
    7916                 :          0 :         struct rpc_task_setup task_setup_data = {
    7917                 :            :                 .rpc_client = clnt,
    7918                 :            :                 .rpc_xprt = xprt,
    7919                 :            :                 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
    7920                 :            :                 .rpc_message = &msg,
    7921                 :            :                 .flags = RPC_TASK_TIMEOUT,
    7922                 :            :         };
    7923                 :            :         struct rpc_task *task;
    7924                 :            : 
    7925                 :          0 :         nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
    7926                 :          0 :         if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
    7927                 :          0 :                 args.dir = NFS4_CDFC4_FORE;
    7928                 :            : 
    7929                 :            :         /* Do not set the backchannel flag unless this is clnt->cl_xprt */
    7930                 :          0 :         if (xprt != rcu_access_pointer(clnt->cl_xprt))
    7931                 :          0 :                 args.dir = NFS4_CDFC4_FORE;
    7932                 :            : 
    7933                 :          0 :         task = rpc_run_task(&task_setup_data);
    7934                 :          0 :         if (!IS_ERR(task)) {
    7935                 :          0 :                 status = task->tk_status;
    7936                 :          0 :                 rpc_put_task(task);
    7937                 :            :         } else
    7938                 :            :                 status = PTR_ERR(task);
    7939                 :          0 :         trace_nfs4_bind_conn_to_session(clp, status);
    7940                 :          0 :         if (status == 0) {
    7941                 :          0 :                 if (memcmp(res.sessionid.data,
    7942                 :          0 :                     clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
    7943                 :            :                         dprintk("NFS: %s: Session ID mismatch\n", __func__);
    7944                 :            :                         return -EIO;
    7945                 :            :                 }
    7946                 :          0 :                 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
    7947                 :            :                         dprintk("NFS: %s: Unexpected direction from server\n",
    7948                 :            :                                 __func__);
    7949                 :            :                         return -EIO;
    7950                 :            :                 }
    7951                 :          0 :                 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
    7952                 :            :                         dprintk("NFS: %s: Server returned RDMA mode = true\n",
    7953                 :            :                                 __func__);
    7954                 :            :                         return -EIO;
    7955                 :            :                 }
    7956                 :            :         }
    7957                 :            : 
    7958                 :          0 :         return status;
    7959                 :            : }
    7960                 :            : 
    7961                 :            : struct rpc_bind_conn_calldata {
    7962                 :            :         struct nfs_client *clp;
    7963                 :            :         const struct cred *cred;
    7964                 :            : };
    7965                 :            : 
    7966                 :            : static int
    7967                 :          0 : nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
    7968                 :            :                 struct rpc_xprt *xprt,
    7969                 :            :                 void *calldata)
    7970                 :            : {
    7971                 :            :         struct rpc_bind_conn_calldata *p = calldata;
    7972                 :            : 
    7973                 :          0 :         return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
    7974                 :            : }
    7975                 :            : 
    7976                 :          0 : int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
    7977                 :            : {
    7978                 :          0 :         struct rpc_bind_conn_calldata data = {
    7979                 :            :                 .clp = clp,
    7980                 :            :                 .cred = cred,
    7981                 :            :         };
    7982                 :          0 :         return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
    7983                 :            :                         nfs4_proc_bind_conn_to_session_callback, &data);
    7984                 :            : }
    7985                 :            : 
    7986                 :            : /*
    7987                 :            :  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
    7988                 :            :  * and operations we'd like to see to enable certain features in the allow map
    7989                 :            :  */
    7990                 :            : static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
    7991                 :            :         .how = SP4_MACH_CRED,
    7992                 :            :         .enforce.u.words = {
    7993                 :            :                 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
    7994                 :            :                       1 << (OP_EXCHANGE_ID - 32) |
    7995                 :            :                       1 << (OP_CREATE_SESSION - 32) |
    7996                 :            :                       1 << (OP_DESTROY_SESSION - 32) |
    7997                 :            :                       1 << (OP_DESTROY_CLIENTID - 32)
    7998                 :            :         },
    7999                 :            :         .allow.u.words = {
    8000                 :            :                 [0] = 1 << (OP_CLOSE) |
    8001                 :            :                       1 << (OP_OPEN_DOWNGRADE) |
    8002                 :            :                       1 << (OP_LOCKU) |
    8003                 :            :                       1 << (OP_DELEGRETURN) |
    8004                 :            :                       1 << (OP_COMMIT),
    8005                 :            :                 [1] = 1 << (OP_SECINFO - 32) |
    8006                 :            :                       1 << (OP_SECINFO_NO_NAME - 32) |
    8007                 :            :                       1 << (OP_LAYOUTRETURN - 32) |
    8008                 :            :                       1 << (OP_TEST_STATEID - 32) |
    8009                 :            :                       1 << (OP_FREE_STATEID - 32) |
    8010                 :            :                       1 << (OP_WRITE - 32)
    8011                 :            :         }
    8012                 :            : };
    8013                 :            : 
    8014                 :            : /*
    8015                 :            :  * Select the state protection mode for client `clp' given the server results
    8016                 :            :  * from exchange_id in `sp'.
    8017                 :            :  *
    8018                 :            :  * Returns 0 on success, negative errno otherwise.
    8019                 :            :  */
    8020                 :          0 : static int nfs4_sp4_select_mode(struct nfs_client *clp,
    8021                 :            :                                  struct nfs41_state_protection *sp)
    8022                 :            : {
    8023                 :            :         static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
    8024                 :            :                 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
    8025                 :            :                       1 << (OP_EXCHANGE_ID - 32) |
    8026                 :            :                       1 << (OP_CREATE_SESSION - 32) |
    8027                 :            :                       1 << (OP_DESTROY_SESSION - 32) |
    8028                 :            :                       1 << (OP_DESTROY_CLIENTID - 32)
    8029                 :            :         };
    8030                 :            :         unsigned long flags = 0;
    8031                 :            :         unsigned int i;
    8032                 :            :         int ret = 0;
    8033                 :            : 
    8034                 :          0 :         if (sp->how == SP4_MACH_CRED) {
    8035                 :            :                 /* Print state protect result */
    8036                 :            :                 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
    8037                 :          0 :                 for (i = 0; i <= LAST_NFS4_OP; i++) {
    8038                 :          0 :                         if (test_bit(i, sp->enforce.u.longs))
    8039                 :            :                                 dfprintk(MOUNT, "  enforce op %d\n", i);
    8040                 :          0 :                         if (test_bit(i, sp->allow.u.longs))
    8041                 :            :                                 dfprintk(MOUNT, "  allow op %d\n", i);
    8042                 :            :                 }
    8043                 :            : 
    8044                 :            :                 /* make sure nothing is on enforce list that isn't supported */
    8045                 :          0 :                 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
    8046                 :          0 :                         if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
    8047                 :            :                                 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
    8048                 :            :                                 ret = -EINVAL;
    8049                 :            :                                 goto out;
    8050                 :            :                         }
    8051                 :            :                 }
    8052                 :            : 
    8053                 :            :                 /*
    8054                 :            :                  * Minimal mode - state operations are allowed to use machine
    8055                 :            :                  * credential.  Note this already happens by default, so the
    8056                 :            :                  * client doesn't have to do anything more than the negotiation.
    8057                 :            :                  *
    8058                 :            :                  * NOTE: we don't care if EXCHANGE_ID is in the list -
    8059                 :            :                  *       we're already using the machine cred for exchange_id
    8060                 :            :                  *       and will never use a different cred.
    8061                 :            :                  */
    8062                 :          0 :                 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
    8063                 :          0 :                     test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
    8064                 :          0 :                     test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
    8065                 :            :                     test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
    8066                 :            :                         dfprintk(MOUNT, "sp4_mach_cred:\n");
    8067                 :            :                         dfprintk(MOUNT, "  minimal mode enabled\n");
    8068                 :            :                         __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
    8069                 :            :                 } else {
    8070                 :            :                         dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
    8071                 :            :                         ret = -EINVAL;
    8072                 :            :                         goto out;
    8073                 :            :                 }
    8074                 :            : 
    8075                 :          0 :                 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
    8076                 :          0 :                     test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
    8077                 :          0 :                     test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
    8078                 :            :                     test_bit(OP_LOCKU, sp->allow.u.longs)) {
    8079                 :            :                         dfprintk(MOUNT, "  cleanup mode enabled\n");
    8080                 :            :                         __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
    8081                 :            :                 }
    8082                 :            : 
    8083                 :          0 :                 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
    8084                 :            :                         dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
    8085                 :            :                         __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
    8086                 :            :                 }
    8087                 :            : 
    8088                 :          0 :                 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
    8089                 :            :                     test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
    8090                 :            :                         dfprintk(MOUNT, "  secinfo mode enabled\n");
    8091                 :            :                         __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
    8092                 :            :                 }
    8093                 :            : 
    8094                 :          0 :                 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
    8095                 :            :                     test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
    8096                 :            :                         dfprintk(MOUNT, "  stateid mode enabled\n");
    8097                 :            :                         __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
    8098                 :            :                 }
    8099                 :            : 
    8100                 :          0 :                 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
    8101                 :            :                         dfprintk(MOUNT, "  write mode enabled\n");
    8102                 :            :                         __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
    8103                 :            :                 }
    8104                 :            : 
    8105                 :          0 :                 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
    8106                 :            :                         dfprintk(MOUNT, "  commit mode enabled\n");
    8107                 :            :                         __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
    8108                 :            :                 }
    8109                 :            :         }
    8110                 :            : out:
    8111                 :          0 :         clp->cl_sp4_flags = flags;
    8112                 :          0 :         return ret;
    8113                 :            : }
    8114                 :            : 
    8115                 :            : struct nfs41_exchange_id_data {
    8116                 :            :         struct nfs41_exchange_id_res res;
    8117                 :            :         struct nfs41_exchange_id_args args;
    8118                 :            : };
    8119                 :            : 
    8120                 :          0 : static void nfs4_exchange_id_release(void *data)
    8121                 :            : {
    8122                 :            :         struct nfs41_exchange_id_data *cdata =
    8123                 :            :                                         (struct nfs41_exchange_id_data *)data;
    8124                 :            : 
    8125                 :          0 :         nfs_put_client(cdata->args.client);
    8126                 :          0 :         kfree(cdata->res.impl_id);
    8127                 :          0 :         kfree(cdata->res.server_scope);
    8128                 :          0 :         kfree(cdata->res.server_owner);
    8129                 :          0 :         kfree(cdata);
    8130                 :          0 : }
    8131                 :            : 
    8132                 :            : static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
    8133                 :            :         .rpc_release = nfs4_exchange_id_release,
    8134                 :            : };
    8135                 :            : 
    8136                 :            : /*
    8137                 :            :  * _nfs4_proc_exchange_id()
    8138                 :            :  *
    8139                 :            :  * Wrapper for EXCHANGE_ID operation.
    8140                 :            :  */
    8141                 :            : static struct rpc_task *
    8142                 :          0 : nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
    8143                 :            :                         u32 sp4_how, struct rpc_xprt *xprt)
    8144                 :            : {
    8145                 :          0 :         struct rpc_message msg = {
    8146                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
    8147                 :            :                 .rpc_cred = cred,
    8148                 :            :         };
    8149                 :          0 :         struct rpc_task_setup task_setup_data = {
    8150                 :          0 :                 .rpc_client = clp->cl_rpcclient,
    8151                 :            :                 .callback_ops = &nfs4_exchange_id_call_ops,
    8152                 :            :                 .rpc_message = &msg,
    8153                 :            :                 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
    8154                 :            :         };
    8155                 :            :         struct nfs41_exchange_id_data *calldata;
    8156                 :            :         int status;
    8157                 :            : 
    8158                 :          0 :         if (!refcount_inc_not_zero(&clp->cl_count))
    8159                 :            :                 return ERR_PTR(-EIO);
    8160                 :            : 
    8161                 :            :         status = -ENOMEM;
    8162                 :          0 :         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
    8163                 :          0 :         if (!calldata)
    8164                 :            :                 goto out;
    8165                 :            : 
    8166                 :          0 :         nfs4_init_boot_verifier(clp, &calldata->args.verifier);
    8167                 :            : 
    8168                 :          0 :         status = nfs4_init_uniform_client_string(clp);
    8169                 :          0 :         if (status)
    8170                 :            :                 goto out_calldata;
    8171                 :            : 
    8172                 :          0 :         calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
    8173                 :            :                                                 GFP_NOFS);
    8174                 :            :         status = -ENOMEM;
    8175                 :          0 :         if (unlikely(calldata->res.server_owner == NULL))
    8176                 :            :                 goto out_calldata;
    8177                 :            : 
    8178                 :          0 :         calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
    8179                 :            :                                         GFP_NOFS);
    8180                 :          0 :         if (unlikely(calldata->res.server_scope == NULL))
    8181                 :            :                 goto out_server_owner;
    8182                 :            : 
    8183                 :          0 :         calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
    8184                 :          0 :         if (unlikely(calldata->res.impl_id == NULL))
    8185                 :            :                 goto out_server_scope;
    8186                 :            : 
    8187                 :          0 :         switch (sp4_how) {
    8188                 :            :         case SP4_NONE:
    8189                 :          0 :                 calldata->args.state_protect.how = SP4_NONE;
    8190                 :          0 :                 break;
    8191                 :            : 
    8192                 :            :         case SP4_MACH_CRED:
    8193                 :          0 :                 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
    8194                 :          0 :                 break;
    8195                 :            : 
    8196                 :            :         default:
    8197                 :            :                 /* unsupported! */
    8198                 :          0 :                 WARN_ON_ONCE(1);
    8199                 :            :                 status = -EINVAL;
    8200                 :            :                 goto out_impl_id;
    8201                 :            :         }
    8202                 :          0 :         if (xprt) {
    8203                 :          0 :                 task_setup_data.rpc_xprt = xprt;
    8204                 :          0 :                 task_setup_data.flags |= RPC_TASK_SOFTCONN;
    8205                 :          0 :                 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
    8206                 :            :                                 sizeof(calldata->args.verifier.data));
    8207                 :            :         }
    8208                 :          0 :         calldata->args.client = clp;
    8209                 :          0 :         calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
    8210                 :            :         EXCHGID4_FLAG_BIND_PRINC_STATEID;
    8211                 :            : #ifdef CONFIG_NFS_V4_1_MIGRATION
    8212                 :            :         calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
    8213                 :            : #endif
    8214                 :          0 :         msg.rpc_argp = &calldata->args;
    8215                 :          0 :         msg.rpc_resp = &calldata->res;
    8216                 :          0 :         task_setup_data.callback_data = calldata;
    8217                 :            : 
    8218                 :          0 :         return rpc_run_task(&task_setup_data);
    8219                 :            : 
    8220                 :            : out_impl_id:
    8221                 :          0 :         kfree(calldata->res.impl_id);
    8222                 :            : out_server_scope:
    8223                 :          0 :         kfree(calldata->res.server_scope);
    8224                 :            : out_server_owner:
    8225                 :          0 :         kfree(calldata->res.server_owner);
    8226                 :            : out_calldata:
    8227                 :          0 :         kfree(calldata);
    8228                 :            : out:
    8229                 :          0 :         nfs_put_client(clp);
    8230                 :          0 :         return ERR_PTR(status);
    8231                 :            : }
    8232                 :            : 
    8233                 :            : /*
    8234                 :            :  * _nfs4_proc_exchange_id()
    8235                 :            :  *
    8236                 :            :  * Wrapper for EXCHANGE_ID operation.
    8237                 :            :  */
    8238                 :          0 : static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
    8239                 :            :                         u32 sp4_how)
    8240                 :            : {
    8241                 :            :         struct rpc_task *task;
    8242                 :            :         struct nfs41_exchange_id_args *argp;
    8243                 :            :         struct nfs41_exchange_id_res *resp;
    8244                 :          0 :         unsigned long now = jiffies;
    8245                 :            :         int status;
    8246                 :            : 
    8247                 :          0 :         task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
    8248                 :          0 :         if (IS_ERR(task))
    8249                 :          0 :                 return PTR_ERR(task);
    8250                 :            : 
    8251                 :          0 :         argp = task->tk_msg.rpc_argp;
    8252                 :          0 :         resp = task->tk_msg.rpc_resp;
    8253                 :          0 :         status = task->tk_status;
    8254                 :          0 :         if (status  != 0)
    8255                 :            :                 goto out;
    8256                 :            : 
    8257                 :          0 :         status = nfs4_check_cl_exchange_flags(resp->flags);
    8258                 :          0 :         if (status  != 0)
    8259                 :            :                 goto out;
    8260                 :            : 
    8261                 :          0 :         status = nfs4_sp4_select_mode(clp, &resp->state_protect);
    8262                 :          0 :         if (status != 0)
    8263                 :            :                 goto out;
    8264                 :            : 
    8265                 :          0 :         do_renew_lease(clp, now);
    8266                 :            : 
    8267                 :          0 :         clp->cl_clientid = resp->clientid;
    8268                 :          0 :         clp->cl_exchange_flags = resp->flags;
    8269                 :          0 :         clp->cl_seqid = resp->seqid;
    8270                 :            :         /* Client ID is not confirmed */
    8271                 :          0 :         if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
    8272                 :          0 :                 clear_bit(NFS4_SESSION_ESTABLISHED,
    8273                 :            :                           &clp->cl_session->session_state);
    8274                 :            : 
    8275                 :          0 :         if (clp->cl_serverscope != NULL &&
    8276                 :          0 :             !nfs41_same_server_scope(clp->cl_serverscope,
    8277                 :            :                                 resp->server_scope)) {
    8278                 :            :                 dprintk("%s: server_scope mismatch detected\n",
    8279                 :            :                         __func__);
    8280                 :          0 :                 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
    8281                 :            :         }
    8282                 :            : 
    8283                 :          0 :         swap(clp->cl_serverowner, resp->server_owner);
    8284                 :          0 :         swap(clp->cl_serverscope, resp->server_scope);
    8285                 :          0 :         swap(clp->cl_implid, resp->impl_id);
    8286                 :            : 
    8287                 :            :         /* Save the EXCHANGE_ID verifier session trunk tests */
    8288                 :          0 :         memcpy(clp->cl_confirm.data, argp->verifier.data,
    8289                 :            :                sizeof(clp->cl_confirm.data));
    8290                 :            : out:
    8291                 :          0 :         trace_nfs4_exchange_id(clp, status);
    8292                 :          0 :         rpc_put_task(task);
    8293                 :          0 :         return status;
    8294                 :            : }
    8295                 :            : 
    8296                 :            : /*
    8297                 :            :  * nfs4_proc_exchange_id()
    8298                 :            :  *
    8299                 :            :  * Returns zero, a negative errno, or a negative NFS4ERR status code.
    8300                 :            :  *
    8301                 :            :  * Since the clientid has expired, all compounds using sessions
    8302                 :            :  * associated with the stale clientid will be returning
    8303                 :            :  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
    8304                 :            :  * be in some phase of session reset.
    8305                 :            :  *
    8306                 :            :  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
    8307                 :            :  */
    8308                 :          0 : int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
    8309                 :            : {
    8310                 :          0 :         rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
    8311                 :            :         int status;
    8312                 :            : 
    8313                 :            :         /* try SP4_MACH_CRED if krb5i/p */
    8314                 :          0 :         if (authflavor == RPC_AUTH_GSS_KRB5I ||
    8315                 :            :             authflavor == RPC_AUTH_GSS_KRB5P) {
    8316                 :          0 :                 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
    8317                 :          0 :                 if (!status)
    8318                 :            :                         return 0;
    8319                 :            :         }
    8320                 :            : 
    8321                 :            :         /* try SP4_NONE */
    8322                 :          0 :         return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
    8323                 :            : }
    8324                 :            : 
    8325                 :            : /**
    8326                 :            :  * nfs4_test_session_trunk
    8327                 :            :  *
    8328                 :            :  * This is an add_xprt_test() test function called from
    8329                 :            :  * rpc_clnt_setup_test_and_add_xprt.
    8330                 :            :  *
    8331                 :            :  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
    8332                 :            :  * and is dereferrenced in nfs4_exchange_id_release
    8333                 :            :  *
    8334                 :            :  * Upon success, add the new transport to the rpc_clnt
    8335                 :            :  *
    8336                 :            :  * @clnt: struct rpc_clnt to get new transport
    8337                 :            :  * @xprt: the rpc_xprt to test
    8338                 :            :  * @data: call data for _nfs4_proc_exchange_id.
    8339                 :            :  */
    8340                 :          0 : void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
    8341                 :            :                             void *data)
    8342                 :            : {
    8343                 :            :         struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
    8344                 :            :         struct rpc_task *task;
    8345                 :            :         int status;
    8346                 :            : 
    8347                 :            :         u32 sp4_how;
    8348                 :            : 
    8349                 :            :         dprintk("--> %s try %s\n", __func__,
    8350                 :            :                 xprt->address_strings[RPC_DISPLAY_ADDR]);
    8351                 :            : 
    8352                 :          0 :         sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
    8353                 :            : 
    8354                 :            :         /* Test connection for session trunking. Async exchange_id call */
    8355                 :          0 :         task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
    8356                 :          0 :         if (IS_ERR(task))
    8357                 :          0 :                 return;
    8358                 :            : 
    8359                 :          0 :         status = task->tk_status;
    8360                 :          0 :         if (status == 0)
    8361                 :          0 :                 status = nfs4_detect_session_trunking(adata->clp,
    8362                 :          0 :                                 task->tk_msg.rpc_resp, xprt);
    8363                 :            : 
    8364                 :          0 :         if (status == 0)
    8365                 :          0 :                 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
    8366                 :            : 
    8367                 :          0 :         rpc_put_task(task);
    8368                 :            : }
    8369                 :            : EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
    8370                 :            : 
    8371                 :          0 : static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
    8372                 :            :                 const struct cred *cred)
    8373                 :            : {
    8374                 :          0 :         struct rpc_message msg = {
    8375                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
    8376                 :            :                 .rpc_argp = clp,
    8377                 :            :                 .rpc_cred = cred,
    8378                 :            :         };
    8379                 :            :         int status;
    8380                 :            : 
    8381                 :          0 :         status = rpc_call_sync(clp->cl_rpcclient, &msg,
    8382                 :            :                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
    8383                 :          0 :         trace_nfs4_destroy_clientid(clp, status);
    8384                 :            :         if (status)
    8385                 :            :                 dprintk("NFS: Got error %d from the server %s on "
    8386                 :            :                         "DESTROY_CLIENTID.", status, clp->cl_hostname);
    8387                 :          0 :         return status;
    8388                 :            : }
    8389                 :            : 
    8390                 :          0 : static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
    8391                 :            :                 const struct cred *cred)
    8392                 :            : {
    8393                 :            :         unsigned int loop;
    8394                 :            :         int ret;
    8395                 :            : 
    8396                 :          0 :         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
    8397                 :          0 :                 ret = _nfs4_proc_destroy_clientid(clp, cred);
    8398                 :          0 :                 switch (ret) {
    8399                 :            :                 case -NFS4ERR_DELAY:
    8400                 :            :                 case -NFS4ERR_CLIENTID_BUSY:
    8401                 :            :                         ssleep(1);
    8402                 :            :                         break;
    8403                 :            :                 default:
    8404                 :          0 :                         return ret;
    8405                 :            :                 }
    8406                 :            :         }
    8407                 :            :         return 0;
    8408                 :            : }
    8409                 :            : 
    8410                 :          0 : int nfs4_destroy_clientid(struct nfs_client *clp)
    8411                 :            : {
    8412                 :            :         const struct cred *cred;
    8413                 :            :         int ret = 0;
    8414                 :            : 
    8415                 :          0 :         if (clp->cl_mvops->minor_version < 1)
    8416                 :            :                 goto out;
    8417                 :          0 :         if (clp->cl_exchange_flags == 0)
    8418                 :            :                 goto out;
    8419                 :          0 :         if (clp->cl_preserve_clid)
    8420                 :            :                 goto out;
    8421                 :          0 :         cred = nfs4_get_clid_cred(clp);
    8422                 :          0 :         ret = nfs4_proc_destroy_clientid(clp, cred);
    8423                 :          0 :         put_cred(cred);
    8424                 :          0 :         switch (ret) {
    8425                 :            :         case 0:
    8426                 :            :         case -NFS4ERR_STALE_CLIENTID:
    8427                 :          0 :                 clp->cl_exchange_flags = 0;
    8428                 :            :         }
    8429                 :            : out:
    8430                 :          0 :         return ret;
    8431                 :            : }
    8432                 :            : 
    8433                 :            : #endif /* CONFIG_NFS_V4_1 */
    8434                 :            : 
    8435                 :            : struct nfs4_get_lease_time_data {
    8436                 :            :         struct nfs4_get_lease_time_args *args;
    8437                 :            :         struct nfs4_get_lease_time_res *res;
    8438                 :            :         struct nfs_client *clp;
    8439                 :            : };
    8440                 :            : 
    8441                 :          0 : static void nfs4_get_lease_time_prepare(struct rpc_task *task,
    8442                 :            :                                         void *calldata)
    8443                 :            : {
    8444                 :            :         struct nfs4_get_lease_time_data *data =
    8445                 :            :                         (struct nfs4_get_lease_time_data *)calldata;
    8446                 :            : 
    8447                 :            :         dprintk("--> %s\n", __func__);
    8448                 :            :         /* just setup sequence, do not trigger session recovery
    8449                 :            :            since we're invoked within one */
    8450                 :          0 :         nfs4_setup_sequence(data->clp,
    8451                 :          0 :                         &data->args->la_seq_args,
    8452                 :          0 :                         &data->res->lr_seq_res,
    8453                 :            :                         task);
    8454                 :            :         dprintk("<-- %s\n", __func__);
    8455                 :          0 : }
    8456                 :            : 
    8457                 :            : /*
    8458                 :            :  * Called from nfs4_state_manager thread for session setup, so don't recover
    8459                 :            :  * from sequence operation or clientid errors.
    8460                 :            :  */
    8461                 :          0 : static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
    8462                 :            : {
    8463                 :            :         struct nfs4_get_lease_time_data *data =
    8464                 :            :                         (struct nfs4_get_lease_time_data *)calldata;
    8465                 :            : 
    8466                 :            :         dprintk("--> %s\n", __func__);
    8467                 :          0 :         if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
    8468                 :            :                 return;
    8469                 :          0 :         switch (task->tk_status) {
    8470                 :            :         case -NFS4ERR_DELAY:
    8471                 :            :         case -NFS4ERR_GRACE:
    8472                 :            :                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
    8473                 :          0 :                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
    8474                 :          0 :                 task->tk_status = 0;
    8475                 :            :                 /* fall through */
    8476                 :            :         case -NFS4ERR_RETRY_UNCACHED_REP:
    8477                 :          0 :                 rpc_restart_call_prepare(task);
    8478                 :          0 :                 return;
    8479                 :            :         }
    8480                 :            :         dprintk("<-- %s\n", __func__);
    8481                 :            : }
    8482                 :            : 
    8483                 :            : static const struct rpc_call_ops nfs4_get_lease_time_ops = {
    8484                 :            :         .rpc_call_prepare = nfs4_get_lease_time_prepare,
    8485                 :            :         .rpc_call_done = nfs4_get_lease_time_done,
    8486                 :            : };
    8487                 :            : 
    8488                 :          0 : int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
    8489                 :            : {
    8490                 :            :         struct nfs4_get_lease_time_args args;
    8491                 :          0 :         struct nfs4_get_lease_time_res res = {
    8492                 :            :                 .lr_fsinfo = fsinfo,
    8493                 :            :         };
    8494                 :          0 :         struct nfs4_get_lease_time_data data = {
    8495                 :            :                 .args = &args,
    8496                 :            :                 .res = &res,
    8497                 :            :                 .clp = clp,
    8498                 :            :         };
    8499                 :          0 :         struct rpc_message msg = {
    8500                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
    8501                 :            :                 .rpc_argp = &args,
    8502                 :            :                 .rpc_resp = &res,
    8503                 :            :         };
    8504                 :          0 :         struct rpc_task_setup task_setup = {
    8505                 :          0 :                 .rpc_client = clp->cl_rpcclient,
    8506                 :            :                 .rpc_message = &msg,
    8507                 :            :                 .callback_ops = &nfs4_get_lease_time_ops,
    8508                 :            :                 .callback_data = &data,
    8509                 :            :                 .flags = RPC_TASK_TIMEOUT,
    8510                 :            :         };
    8511                 :            : 
    8512                 :            :         nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
    8513                 :          0 :         return nfs4_call_sync_custom(&task_setup);
    8514                 :            : }
    8515                 :            : 
    8516                 :            : #ifdef CONFIG_NFS_V4_1
    8517                 :            : 
    8518                 :            : /*
    8519                 :            :  * Initialize the values to be used by the client in CREATE_SESSION
    8520                 :            :  * If nfs4_init_session set the fore channel request and response sizes,
    8521                 :            :  * use them.
    8522                 :            :  *
    8523                 :            :  * Set the back channel max_resp_sz_cached to zero to force the client to
    8524                 :            :  * always set csa_cachethis to FALSE because the current implementation
    8525                 :            :  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
    8526                 :            :  */
    8527                 :          0 : static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
    8528                 :            :                                     struct rpc_clnt *clnt)
    8529                 :            : {
    8530                 :            :         unsigned int max_rqst_sz, max_resp_sz;
    8531                 :          0 :         unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
    8532                 :          0 :         unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
    8533                 :            : 
    8534                 :          0 :         max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
    8535                 :          0 :         max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
    8536                 :            : 
    8537                 :            :         /* Fore channel attributes */
    8538                 :          0 :         args->fc_attrs.max_rqst_sz = max_rqst_sz;
    8539                 :          0 :         args->fc_attrs.max_resp_sz = max_resp_sz;
    8540                 :          0 :         args->fc_attrs.max_ops = NFS4_MAX_OPS;
    8541                 :          0 :         args->fc_attrs.max_reqs = max_session_slots;
    8542                 :            : 
    8543                 :            :         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
    8544                 :            :                 "max_ops=%u max_reqs=%u\n",
    8545                 :            :                 __func__,
    8546                 :            :                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
    8547                 :            :                 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
    8548                 :            : 
    8549                 :            :         /* Back channel attributes */
    8550                 :          0 :         args->bc_attrs.max_rqst_sz = max_bc_payload;
    8551                 :          0 :         args->bc_attrs.max_resp_sz = max_bc_payload;
    8552                 :          0 :         args->bc_attrs.max_resp_sz_cached = 0;
    8553                 :          0 :         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
    8554                 :          0 :         args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
    8555                 :          0 :         if (args->bc_attrs.max_reqs > max_bc_slots)
    8556                 :          0 :                 args->bc_attrs.max_reqs = max_bc_slots;
    8557                 :            : 
    8558                 :            :         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
    8559                 :            :                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
    8560                 :            :                 __func__,
    8561                 :            :                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
    8562                 :            :                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
    8563                 :            :                 args->bc_attrs.max_reqs);
    8564                 :          0 : }
    8565                 :            : 
    8566                 :            : static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
    8567                 :            :                 struct nfs41_create_session_res *res)
    8568                 :            : {
    8569                 :            :         struct nfs4_channel_attrs *sent = &args->fc_attrs;
    8570                 :            :         struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
    8571                 :            : 
    8572                 :          0 :         if (rcvd->max_resp_sz > sent->max_resp_sz)
    8573                 :            :                 return -EINVAL;
    8574                 :            :         /*
    8575                 :            :          * Our requested max_ops is the minimum we need; we're not
    8576                 :            :          * prepared to break up compounds into smaller pieces than that.
    8577                 :            :          * So, no point even trying to continue if the server won't
    8578                 :            :          * cooperate:
    8579                 :            :          */
    8580                 :          0 :         if (rcvd->max_ops < sent->max_ops)
    8581                 :            :                 return -EINVAL;
    8582                 :          0 :         if (rcvd->max_reqs == 0)
    8583                 :            :                 return -EINVAL;
    8584                 :          0 :         if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
    8585                 :          0 :                 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
    8586                 :            :         return 0;
    8587                 :            : }
    8588                 :            : 
    8589                 :          0 : static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
    8590                 :            :                 struct nfs41_create_session_res *res)
    8591                 :            : {
    8592                 :            :         struct nfs4_channel_attrs *sent = &args->bc_attrs;
    8593                 :            :         struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
    8594                 :            : 
    8595                 :          0 :         if (!(res->flags & SESSION4_BACK_CHAN))
    8596                 :            :                 goto out;
    8597                 :          0 :         if (rcvd->max_rqst_sz > sent->max_rqst_sz)
    8598                 :            :                 return -EINVAL;
    8599                 :          0 :         if (rcvd->max_resp_sz < sent->max_resp_sz)
    8600                 :            :                 return -EINVAL;
    8601                 :          0 :         if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
    8602                 :            :                 return -EINVAL;
    8603                 :          0 :         if (rcvd->max_ops > sent->max_ops)
    8604                 :            :                 return -EINVAL;
    8605                 :          0 :         if (rcvd->max_reqs > sent->max_reqs)
    8606                 :            :                 return -EINVAL;
    8607                 :            : out:
    8608                 :          0 :         return 0;
    8609                 :            : }
    8610                 :            : 
    8611                 :          0 : static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
    8612                 :            :                                      struct nfs41_create_session_res *res)
    8613                 :            : {
    8614                 :            :         int ret;
    8615                 :            : 
    8616                 :            :         ret = nfs4_verify_fore_channel_attrs(args, res);
    8617                 :          0 :         if (ret)
    8618                 :            :                 return ret;
    8619                 :          0 :         return nfs4_verify_back_channel_attrs(args, res);
    8620                 :            : }
    8621                 :            : 
    8622                 :          0 : static void nfs4_update_session(struct nfs4_session *session,
    8623                 :            :                 struct nfs41_create_session_res *res)
    8624                 :            : {
    8625                 :            :         nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
    8626                 :            :         /* Mark client id and session as being confirmed */
    8627                 :          0 :         session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
    8628                 :          0 :         set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
    8629                 :          0 :         session->flags = res->flags;
    8630                 :          0 :         memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
    8631                 :          0 :         if (res->flags & SESSION4_BACK_CHAN)
    8632                 :          0 :                 memcpy(&session->bc_attrs, &res->bc_attrs,
    8633                 :            :                                 sizeof(session->bc_attrs));
    8634                 :          0 : }
    8635                 :            : 
    8636                 :          0 : static int _nfs4_proc_create_session(struct nfs_client *clp,
    8637                 :            :                 const struct cred *cred)
    8638                 :            : {
    8639                 :          0 :         struct nfs4_session *session = clp->cl_session;
    8640                 :          0 :         struct nfs41_create_session_args args = {
    8641                 :            :                 .client = clp,
    8642                 :          0 :                 .clientid = clp->cl_clientid,
    8643                 :          0 :                 .seqid = clp->cl_seqid,
    8644                 :            :                 .cb_program = NFS4_CALLBACK,
    8645                 :            :         };
    8646                 :            :         struct nfs41_create_session_res res;
    8647                 :            : 
    8648                 :          0 :         struct rpc_message msg = {
    8649                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
    8650                 :            :                 .rpc_argp = &args,
    8651                 :            :                 .rpc_resp = &res,
    8652                 :            :                 .rpc_cred = cred,
    8653                 :            :         };
    8654                 :            :         int status;
    8655                 :            : 
    8656                 :          0 :         nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
    8657                 :          0 :         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
    8658                 :            : 
    8659                 :          0 :         status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
    8660                 :            :                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
    8661                 :          0 :         trace_nfs4_create_session(clp, status);
    8662                 :            : 
    8663                 :          0 :         switch (status) {
    8664                 :            :         case -NFS4ERR_STALE_CLIENTID:
    8665                 :            :         case -NFS4ERR_DELAY:
    8666                 :            :         case -ETIMEDOUT:
    8667                 :            :         case -EACCES:
    8668                 :            :         case -EAGAIN:
    8669                 :            :                 goto out;
    8670                 :            :         };
    8671                 :            : 
    8672                 :          0 :         clp->cl_seqid++;
    8673                 :          0 :         if (!status) {
    8674                 :            :                 /* Verify the session's negotiated channel_attrs values */
    8675                 :          0 :                 status = nfs4_verify_channel_attrs(&args, &res);
    8676                 :            :                 /* Increment the clientid slot sequence id */
    8677                 :          0 :                 if (status)
    8678                 :            :                         goto out;
    8679                 :          0 :                 nfs4_update_session(session, &res);
    8680                 :            :         }
    8681                 :            : out:
    8682                 :          0 :         return status;
    8683                 :            : }
    8684                 :            : 
    8685                 :            : /*
    8686                 :            :  * Issues a CREATE_SESSION operation to the server.
    8687                 :            :  * It is the responsibility of the caller to verify the session is
    8688                 :            :  * expired before calling this routine.
    8689                 :            :  */
    8690                 :          0 : int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
    8691                 :            : {
    8692                 :            :         int status;
    8693                 :            :         unsigned *ptr;
    8694                 :          0 :         struct nfs4_session *session = clp->cl_session;
    8695                 :            : 
    8696                 :            :         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
    8697                 :            : 
    8698                 :          0 :         status = _nfs4_proc_create_session(clp, cred);
    8699                 :          0 :         if (status)
    8700                 :            :                 goto out;
    8701                 :            : 
    8702                 :            :         /* Init or reset the session slot tables */
    8703                 :          0 :         status = nfs4_setup_session_slot_tables(session);
    8704                 :            :         dprintk("slot table setup returned %d\n", status);
    8705                 :            :         if (status)
    8706                 :            :                 goto out;
    8707                 :            : 
    8708                 :            :         ptr = (unsigned *)&session->sess_id.data[0];
    8709                 :            :         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
    8710                 :            :                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
    8711                 :            : out:
    8712                 :            :         dprintk("<-- %s\n", __func__);
    8713                 :          0 :         return status;
    8714                 :            : }
    8715                 :            : 
    8716                 :            : /*
    8717                 :            :  * Issue the over-the-wire RPC DESTROY_SESSION.
    8718                 :            :  * The caller must serialize access to this routine.
    8719                 :            :  */
    8720                 :          0 : int nfs4_proc_destroy_session(struct nfs4_session *session,
    8721                 :            :                 const struct cred *cred)
    8722                 :            : {
    8723                 :          0 :         struct rpc_message msg = {
    8724                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
    8725                 :            :                 .rpc_argp = session,
    8726                 :            :                 .rpc_cred = cred,
    8727                 :            :         };
    8728                 :            :         int status = 0;
    8729                 :            : 
    8730                 :            :         dprintk("--> nfs4_proc_destroy_session\n");
    8731                 :            : 
    8732                 :            :         /* session is still being setup */
    8733                 :          0 :         if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
    8734                 :            :                 return 0;
    8735                 :            : 
    8736                 :          0 :         status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
    8737                 :            :                                RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
    8738                 :          0 :         trace_nfs4_destroy_session(session->clp, status);
    8739                 :            : 
    8740                 :            :         if (status)
    8741                 :            :                 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
    8742                 :            :                         "Session has been destroyed regardless...\n", status);
    8743                 :            : 
    8744                 :            :         dprintk("<-- nfs4_proc_destroy_session\n");
    8745                 :          0 :         return status;
    8746                 :            : }
    8747                 :            : 
    8748                 :            : /*
    8749                 :            :  * Renew the cl_session lease.
    8750                 :            :  */
    8751                 :            : struct nfs4_sequence_data {
    8752                 :            :         struct nfs_client *clp;
    8753                 :            :         struct nfs4_sequence_args args;
    8754                 :            :         struct nfs4_sequence_res res;
    8755                 :            : };
    8756                 :            : 
    8757                 :          0 : static void nfs41_sequence_release(void *data)
    8758                 :            : {
    8759                 :            :         struct nfs4_sequence_data *calldata = data;
    8760                 :          0 :         struct nfs_client *clp = calldata->clp;
    8761                 :            : 
    8762                 :          0 :         if (refcount_read(&clp->cl_count) > 1)
    8763                 :          0 :                 nfs4_schedule_state_renewal(clp);
    8764                 :          0 :         nfs_put_client(clp);
    8765                 :          0 :         kfree(calldata);
    8766                 :          0 : }
    8767                 :            : 
    8768                 :          0 : static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
    8769                 :            : {
    8770                 :          0 :         switch(task->tk_status) {
    8771                 :            :         case -NFS4ERR_DELAY:
    8772                 :          0 :                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
    8773                 :          0 :                 return -EAGAIN;
    8774                 :            :         default:
    8775                 :          0 :                 nfs4_schedule_lease_recovery(clp);
    8776                 :            :         }
    8777                 :          0 :         return 0;
    8778                 :            : }
    8779                 :            : 
    8780                 :          0 : static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
    8781                 :            : {
    8782                 :            :         struct nfs4_sequence_data *calldata = data;
    8783                 :          0 :         struct nfs_client *clp = calldata->clp;
    8784                 :            : 
    8785                 :          0 :         if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
    8786                 :            :                 return;
    8787                 :            : 
    8788                 :          0 :         trace_nfs4_sequence(clp, task->tk_status);
    8789                 :          0 :         if (task->tk_status < 0) {
    8790                 :            :                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
    8791                 :          0 :                 if (refcount_read(&clp->cl_count) == 1)
    8792                 :            :                         goto out;
    8793                 :            : 
    8794                 :          0 :                 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
    8795                 :          0 :                         rpc_restart_call_prepare(task);
    8796                 :          0 :                         return;
    8797                 :            :                 }
    8798                 :            :         }
    8799                 :            :         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
    8800                 :            : out:
    8801                 :            :         dprintk("<-- %s\n", __func__);
    8802                 :            : }
    8803                 :            : 
    8804                 :          0 : static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
    8805                 :            : {
    8806                 :            :         struct nfs4_sequence_data *calldata = data;
    8807                 :          0 :         struct nfs_client *clp = calldata->clp;
    8808                 :            :         struct nfs4_sequence_args *args;
    8809                 :            :         struct nfs4_sequence_res *res;
    8810                 :            : 
    8811                 :          0 :         args = task->tk_msg.rpc_argp;
    8812                 :          0 :         res = task->tk_msg.rpc_resp;
    8813                 :            : 
    8814                 :          0 :         nfs4_setup_sequence(clp, args, res, task);
    8815                 :          0 : }
    8816                 :            : 
    8817                 :            : static const struct rpc_call_ops nfs41_sequence_ops = {
    8818                 :            :         .rpc_call_done = nfs41_sequence_call_done,
    8819                 :            :         .rpc_call_prepare = nfs41_sequence_prepare,
    8820                 :            :         .rpc_release = nfs41_sequence_release,
    8821                 :            : };
    8822                 :            : 
    8823                 :          0 : static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
    8824                 :            :                 const struct cred *cred,
    8825                 :            :                 struct nfs4_slot *slot,
    8826                 :            :                 bool is_privileged)
    8827                 :            : {
    8828                 :            :         struct nfs4_sequence_data *calldata;
    8829                 :          0 :         struct rpc_message msg = {
    8830                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
    8831                 :            :                 .rpc_cred = cred,
    8832                 :            :         };
    8833                 :          0 :         struct rpc_task_setup task_setup_data = {
    8834                 :          0 :                 .rpc_client = clp->cl_rpcclient,
    8835                 :            :                 .rpc_message = &msg,
    8836                 :            :                 .callback_ops = &nfs41_sequence_ops,
    8837                 :            :                 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
    8838                 :            :         };
    8839                 :            :         struct rpc_task *ret;
    8840                 :            : 
    8841                 :            :         ret = ERR_PTR(-EIO);
    8842                 :          0 :         if (!refcount_inc_not_zero(&clp->cl_count))
    8843                 :            :                 goto out_err;
    8844                 :            : 
    8845                 :            :         ret = ERR_PTR(-ENOMEM);
    8846                 :          0 :         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
    8847                 :          0 :         if (calldata == NULL)
    8848                 :            :                 goto out_put_clp;
    8849                 :            :         nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
    8850                 :            :         nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
    8851                 :          0 :         msg.rpc_argp = &calldata->args;
    8852                 :          0 :         msg.rpc_resp = &calldata->res;
    8853                 :          0 :         calldata->clp = clp;
    8854                 :          0 :         task_setup_data.callback_data = calldata;
    8855                 :            : 
    8856                 :          0 :         ret = rpc_run_task(&task_setup_data);
    8857                 :          0 :         if (IS_ERR(ret))
    8858                 :            :                 goto out_err;
    8859                 :            :         return ret;
    8860                 :            : out_put_clp:
    8861                 :          0 :         nfs_put_client(clp);
    8862                 :            : out_err:
    8863                 :          0 :         nfs41_release_slot(slot);
    8864                 :          0 :         return ret;
    8865                 :            : }
    8866                 :            : 
    8867                 :          0 : static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
    8868                 :            : {
    8869                 :            :         struct rpc_task *task;
    8870                 :            :         int ret = 0;
    8871                 :            : 
    8872                 :          0 :         if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
    8873                 :            :                 return -EAGAIN;
    8874                 :          0 :         task = _nfs41_proc_sequence(clp, cred, NULL, false);
    8875                 :          0 :         if (IS_ERR(task))
    8876                 :            :                 ret = PTR_ERR(task);
    8877                 :            :         else
    8878                 :          0 :                 rpc_put_task_async(task);
    8879                 :            :         dprintk("<-- %s status=%d\n", __func__, ret);
    8880                 :          0 :         return ret;
    8881                 :            : }
    8882                 :            : 
    8883                 :          0 : static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
    8884                 :            : {
    8885                 :            :         struct rpc_task *task;
    8886                 :            :         int ret;
    8887                 :            : 
    8888                 :          0 :         task = _nfs41_proc_sequence(clp, cred, NULL, true);
    8889                 :          0 :         if (IS_ERR(task)) {
    8890                 :            :                 ret = PTR_ERR(task);
    8891                 :          0 :                 goto out;
    8892                 :            :         }
    8893                 :            :         ret = rpc_wait_for_completion_task(task);
    8894                 :          0 :         if (!ret)
    8895                 :          0 :                 ret = task->tk_status;
    8896                 :          0 :         rpc_put_task(task);
    8897                 :            : out:
    8898                 :            :         dprintk("<-- %s status=%d\n", __func__, ret);
    8899                 :          0 :         return ret;
    8900                 :            : }
    8901                 :            : 
    8902                 :            : struct nfs4_reclaim_complete_data {
    8903                 :            :         struct nfs_client *clp;
    8904                 :            :         struct nfs41_reclaim_complete_args arg;
    8905                 :            :         struct nfs41_reclaim_complete_res res;
    8906                 :            : };
    8907                 :            : 
    8908                 :          0 : static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
    8909                 :            : {
    8910                 :            :         struct nfs4_reclaim_complete_data *calldata = data;
    8911                 :            : 
    8912                 :          0 :         nfs4_setup_sequence(calldata->clp,
    8913                 :            :                         &calldata->arg.seq_args,
    8914                 :            :                         &calldata->res.seq_res,
    8915                 :            :                         task);
    8916                 :          0 : }
    8917                 :            : 
    8918                 :          0 : static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
    8919                 :            : {
    8920                 :          0 :         switch(task->tk_status) {
    8921                 :            :         case 0:
    8922                 :          0 :                 wake_up_all(&clp->cl_lock_waitq);
    8923                 :            :                 /* Fallthrough */
    8924                 :            :         case -NFS4ERR_COMPLETE_ALREADY:
    8925                 :            :         case -NFS4ERR_WRONG_CRED: /* What to do here? */
    8926                 :            :                 break;
    8927                 :            :         case -NFS4ERR_DELAY:
    8928                 :          0 :                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
    8929                 :            :                 /* fall through */
    8930                 :            :         case -NFS4ERR_RETRY_UNCACHED_REP:
    8931                 :            :                 return -EAGAIN;
    8932                 :            :         case -NFS4ERR_BADSESSION:
    8933                 :            :         case -NFS4ERR_DEADSESSION:
    8934                 :            :         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
    8935                 :            :                 break;
    8936                 :            :         default:
    8937                 :          0 :                 nfs4_schedule_lease_recovery(clp);
    8938                 :            :         }
    8939                 :            :         return 0;
    8940                 :            : }
    8941                 :            : 
    8942                 :          0 : static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
    8943                 :            : {
    8944                 :            :         struct nfs4_reclaim_complete_data *calldata = data;
    8945                 :          0 :         struct nfs_client *clp = calldata->clp;
    8946                 :          0 :         struct nfs4_sequence_res *res = &calldata->res.seq_res;
    8947                 :            : 
    8948                 :            :         dprintk("--> %s\n", __func__);
    8949                 :          0 :         if (!nfs41_sequence_done(task, res))
    8950                 :            :                 return;
    8951                 :            : 
    8952                 :          0 :         trace_nfs4_reclaim_complete(clp, task->tk_status);
    8953                 :          0 :         if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
    8954                 :          0 :                 rpc_restart_call_prepare(task);
    8955                 :          0 :                 return;
    8956                 :            :         }
    8957                 :            :         dprintk("<-- %s\n", __func__);
    8958                 :            : }
    8959                 :            : 
    8960                 :          0 : static void nfs4_free_reclaim_complete_data(void *data)
    8961                 :            : {
    8962                 :            :         struct nfs4_reclaim_complete_data *calldata = data;
    8963                 :            : 
    8964                 :          0 :         kfree(calldata);
    8965                 :          0 : }
    8966                 :            : 
    8967                 :            : static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
    8968                 :            :         .rpc_call_prepare = nfs4_reclaim_complete_prepare,
    8969                 :            :         .rpc_call_done = nfs4_reclaim_complete_done,
    8970                 :            :         .rpc_release = nfs4_free_reclaim_complete_data,
    8971                 :            : };
    8972                 :            : 
    8973                 :            : /*
    8974                 :            :  * Issue a global reclaim complete.
    8975                 :            :  */
    8976                 :          0 : static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
    8977                 :            :                 const struct cred *cred)
    8978                 :            : {
    8979                 :            :         struct nfs4_reclaim_complete_data *calldata;
    8980                 :          0 :         struct rpc_message msg = {
    8981                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
    8982                 :            :                 .rpc_cred = cred,
    8983                 :            :         };
    8984                 :          0 :         struct rpc_task_setup task_setup_data = {
    8985                 :          0 :                 .rpc_client = clp->cl_rpcclient,
    8986                 :            :                 .rpc_message = &msg,
    8987                 :            :                 .callback_ops = &nfs4_reclaim_complete_call_ops,
    8988                 :            :                 .flags = RPC_TASK_NO_ROUND_ROBIN,
    8989                 :            :         };
    8990                 :            :         int status = -ENOMEM;
    8991                 :            : 
    8992                 :            :         dprintk("--> %s\n", __func__);
    8993                 :          0 :         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
    8994                 :          0 :         if (calldata == NULL)
    8995                 :            :                 goto out;
    8996                 :          0 :         calldata->clp = clp;
    8997                 :          0 :         calldata->arg.one_fs = 0;
    8998                 :            : 
    8999                 :            :         nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
    9000                 :          0 :         msg.rpc_argp = &calldata->arg;
    9001                 :          0 :         msg.rpc_resp = &calldata->res;
    9002                 :          0 :         task_setup_data.callback_data = calldata;
    9003                 :          0 :         status = nfs4_call_sync_custom(&task_setup_data);
    9004                 :            : out:
    9005                 :            :         dprintk("<-- %s status=%d\n", __func__, status);
    9006                 :          0 :         return status;
    9007                 :            : }
    9008                 :            : 
    9009                 :            : static void
    9010                 :          0 : nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
    9011                 :            : {
    9012                 :            :         struct nfs4_layoutget *lgp = calldata;
    9013                 :          0 :         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
    9014                 :            : 
    9015                 :            :         dprintk("--> %s\n", __func__);
    9016                 :          0 :         nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
    9017                 :            :                                 &lgp->res.seq_res, task);
    9018                 :            :         dprintk("<-- %s\n", __func__);
    9019                 :          0 : }
    9020                 :            : 
    9021                 :          0 : static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
    9022                 :            : {
    9023                 :            :         struct nfs4_layoutget *lgp = calldata;
    9024                 :            : 
    9025                 :            :         dprintk("--> %s\n", __func__);
    9026                 :          0 :         nfs41_sequence_process(task, &lgp->res.seq_res);
    9027                 :            :         dprintk("<-- %s\n", __func__);
    9028                 :          0 : }
    9029                 :            : 
    9030                 :            : static int
    9031                 :          0 : nfs4_layoutget_handle_exception(struct rpc_task *task,
    9032                 :            :                 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
    9033                 :            : {
    9034                 :          0 :         struct inode *inode = lgp->args.inode;
    9035                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    9036                 :            :         struct pnfs_layout_hdr *lo;
    9037                 :          0 :         int nfs4err = task->tk_status;
    9038                 :            :         int err, status = 0;
    9039                 :          0 :         LIST_HEAD(head);
    9040                 :            : 
    9041                 :            :         dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
    9042                 :            : 
    9043                 :          0 :         nfs4_sequence_free_slot(&lgp->res.seq_res);
    9044                 :            : 
    9045                 :          0 :         switch (nfs4err) {
    9046                 :            :         case 0:
    9047                 :            :                 goto out;
    9048                 :            : 
    9049                 :            :         /*
    9050                 :            :          * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
    9051                 :            :          * on the file. set tk_status to -ENODATA to tell upper layer to
    9052                 :            :          * retry go inband.
    9053                 :            :          */
    9054                 :            :         case -NFS4ERR_LAYOUTUNAVAILABLE:
    9055                 :            :                 status = -ENODATA;
    9056                 :          0 :                 goto out;
    9057                 :            :         /*
    9058                 :            :          * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
    9059                 :            :          * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
    9060                 :            :          */
    9061                 :            :         case -NFS4ERR_BADLAYOUT:
    9062                 :            :                 status = -EOVERFLOW;
    9063                 :          0 :                 goto out;
    9064                 :            :         /*
    9065                 :            :          * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
    9066                 :            :          * (or clients) writing to the same RAID stripe except when
    9067                 :            :          * the minlength argument is 0 (see RFC5661 section 18.43.3).
    9068                 :            :          *
    9069                 :            :          * Treat it like we would RECALLCONFLICT -- we retry for a little
    9070                 :            :          * while, and then eventually give up.
    9071                 :            :          */
    9072                 :            :         case -NFS4ERR_LAYOUTTRYLATER:
    9073                 :          0 :                 if (lgp->args.minlength == 0) {
    9074                 :            :                         status = -EOVERFLOW;
    9075                 :            :                         goto out;
    9076                 :            :                 }
    9077                 :            :                 status = -EBUSY;
    9078                 :            :                 break;
    9079                 :            :         case -NFS4ERR_RECALLCONFLICT:
    9080                 :            :                 status = -ERECALLCONFLICT;
    9081                 :          0 :                 break;
    9082                 :            :         case -NFS4ERR_DELEG_REVOKED:
    9083                 :            :         case -NFS4ERR_ADMIN_REVOKED:
    9084                 :            :         case -NFS4ERR_EXPIRED:
    9085                 :            :         case -NFS4ERR_BAD_STATEID:
    9086                 :          0 :                 exception->timeout = 0;
    9087                 :            :                 spin_lock(&inode->i_lock);
    9088                 :          0 :                 lo = NFS_I(inode)->layout;
    9089                 :            :                 /* If the open stateid was bad, then recover it. */
    9090                 :          0 :                 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
    9091                 :            :                     !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
    9092                 :            :                         spin_unlock(&inode->i_lock);
    9093                 :          0 :                         exception->state = lgp->args.ctx->state;
    9094                 :          0 :                         exception->stateid = &lgp->args.stateid;
    9095                 :          0 :                         break;
    9096                 :            :                 }
    9097                 :            : 
    9098                 :            :                 /*
    9099                 :            :                  * Mark the bad layout state as invalid, then retry
    9100                 :            :                  */
    9101                 :          0 :                 pnfs_mark_layout_stateid_invalid(lo, &head);
    9102                 :            :                 spin_unlock(&inode->i_lock);
    9103                 :          0 :                 nfs_commit_inode(inode, 0);
    9104                 :          0 :                 pnfs_free_lseg_list(&head);
    9105                 :            :                 status = -EAGAIN;
    9106                 :          0 :                 goto out;
    9107                 :            :         }
    9108                 :            : 
    9109                 :          0 :         err = nfs4_handle_exception(server, nfs4err, exception);
    9110                 :          0 :         if (!status) {
    9111                 :          0 :                 if (exception->retry)
    9112                 :            :                         status = -EAGAIN;
    9113                 :            :                 else
    9114                 :            :                         status = err;
    9115                 :            :         }
    9116                 :            : out:
    9117                 :            :         dprintk("<-- %s\n", __func__);
    9118                 :          0 :         return status;
    9119                 :            : }
    9120                 :            : 
    9121                 :          0 : size_t max_response_pages(struct nfs_server *server)
    9122                 :            : {
    9123                 :          0 :         u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
    9124                 :          0 :         return nfs_page_array_len(0, max_resp_sz);
    9125                 :            : }
    9126                 :            : 
    9127                 :          0 : static void nfs4_layoutget_release(void *calldata)
    9128                 :            : {
    9129                 :            :         struct nfs4_layoutget *lgp = calldata;
    9130                 :            : 
    9131                 :            :         dprintk("--> %s\n", __func__);
    9132                 :          0 :         nfs4_sequence_free_slot(&lgp->res.seq_res);
    9133                 :          0 :         pnfs_layoutget_free(lgp);
    9134                 :            :         dprintk("<-- %s\n", __func__);
    9135                 :          0 : }
    9136                 :            : 
    9137                 :            : static const struct rpc_call_ops nfs4_layoutget_call_ops = {
    9138                 :            :         .rpc_call_prepare = nfs4_layoutget_prepare,
    9139                 :            :         .rpc_call_done = nfs4_layoutget_done,
    9140                 :            :         .rpc_release = nfs4_layoutget_release,
    9141                 :            : };
    9142                 :            : 
    9143                 :            : struct pnfs_layout_segment *
    9144                 :          0 : nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
    9145                 :            : {
    9146                 :          0 :         struct inode *inode = lgp->args.inode;
    9147                 :            :         struct nfs_server *server = NFS_SERVER(inode);
    9148                 :            :         struct rpc_task *task;
    9149                 :          0 :         struct rpc_message msg = {
    9150                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
    9151                 :          0 :                 .rpc_argp = &lgp->args,
    9152                 :          0 :                 .rpc_resp = &lgp->res,
    9153                 :          0 :                 .rpc_cred = lgp->cred,
    9154                 :            :         };
    9155                 :          0 :         struct rpc_task_setup task_setup_data = {
    9156                 :          0 :                 .rpc_client = server->client,
    9157                 :            :                 .rpc_message = &msg,
    9158                 :            :                 .callback_ops = &nfs4_layoutget_call_ops,
    9159                 :            :                 .callback_data = lgp,
    9160                 :            :                 .flags = RPC_TASK_ASYNC,
    9161                 :            :         };
    9162                 :            :         struct pnfs_layout_segment *lseg = NULL;
    9163                 :          0 :         struct nfs4_exception exception = {
    9164                 :            :                 .inode = inode,
    9165                 :          0 :                 .timeout = *timeout,
    9166                 :            :         };
    9167                 :            :         int status = 0;
    9168                 :            : 
    9169                 :            :         dprintk("--> %s\n", __func__);
    9170                 :            : 
    9171                 :            :         /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
    9172                 :          0 :         pnfs_get_layout_hdr(NFS_I(inode)->layout);
    9173                 :            : 
    9174                 :            :         nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
    9175                 :            : 
    9176                 :          0 :         task = rpc_run_task(&task_setup_data);
    9177                 :          0 :         if (IS_ERR(task))
    9178                 :            :                 return ERR_CAST(task);
    9179                 :            :         status = rpc_wait_for_completion_task(task);
    9180                 :          0 :         if (status != 0)
    9181                 :            :                 goto out;
    9182                 :            : 
    9183                 :          0 :         if (task->tk_status < 0) {
    9184                 :          0 :                 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
    9185                 :          0 :                 *timeout = exception.timeout;
    9186                 :          0 :         } else if (lgp->res.layoutp->len == 0) {
    9187                 :            :                 status = -EAGAIN;
    9188                 :          0 :                 *timeout = nfs4_update_delay(&exception.timeout);
    9189                 :            :         } else
    9190                 :          0 :                 lseg = pnfs_layout_process(lgp);
    9191                 :            : out:
    9192                 :          0 :         trace_nfs4_layoutget(lgp->args.ctx,
    9193                 :          0 :                         &lgp->args.range,
    9194                 :          0 :                         &lgp->res.range,
    9195                 :          0 :                         &lgp->res.stateid,
    9196                 :            :                         status);
    9197                 :            : 
    9198                 :          0 :         rpc_put_task(task);
    9199                 :            :         dprintk("<-- %s status=%d\n", __func__, status);
    9200                 :          0 :         if (status)
    9201                 :          0 :                 return ERR_PTR(status);
    9202                 :            :         return lseg;
    9203                 :            : }
    9204                 :            : 
    9205                 :            : static void
    9206                 :          0 : nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
    9207                 :            : {
    9208                 :            :         struct nfs4_layoutreturn *lrp = calldata;
    9209                 :            : 
    9210                 :            :         dprintk("--> %s\n", __func__);
    9211                 :          0 :         nfs4_setup_sequence(lrp->clp,
    9212                 :            :                         &lrp->args.seq_args,
    9213                 :            :                         &lrp->res.seq_res,
    9214                 :            :                         task);
    9215                 :          0 :         if (!pnfs_layout_is_valid(lrp->args.layout))
    9216                 :          0 :                 rpc_exit(task, 0);
    9217                 :          0 : }
    9218                 :            : 
    9219                 :          0 : static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
    9220                 :            : {
    9221                 :            :         struct nfs4_layoutreturn *lrp = calldata;
    9222                 :            :         struct nfs_server *server;
    9223                 :            : 
    9224                 :            :         dprintk("--> %s\n", __func__);
    9225                 :            : 
    9226                 :          0 :         if (!nfs41_sequence_process(task, &lrp->res.seq_res))
    9227                 :            :                 return;
    9228                 :            : 
    9229                 :            :         /*
    9230                 :            :          * Was there an RPC level error? Assume the call succeeded,
    9231                 :            :          * and that we need to release the layout
    9232                 :            :          */
    9233                 :          0 :         if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
    9234                 :          0 :                 lrp->res.lrs_present = 0;
    9235                 :          0 :                 return;
    9236                 :            :         }
    9237                 :            : 
    9238                 :          0 :         server = NFS_SERVER(lrp->args.inode);
    9239                 :          0 :         switch (task->tk_status) {
    9240                 :            :         case -NFS4ERR_OLD_STATEID:
    9241                 :          0 :                 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
    9242                 :            :                                         &lrp->args.range,
    9243                 :            :                                         lrp->args.inode))
    9244                 :            :                         goto out_restart;
    9245                 :            :                 /* Fallthrough */
    9246                 :            :         default:
    9247                 :          0 :                 task->tk_status = 0;
    9248                 :            :                 /* Fallthrough */
    9249                 :            :         case 0:
    9250                 :            :                 break;
    9251                 :            :         case -NFS4ERR_DELAY:
    9252                 :          0 :                 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
    9253                 :            :                         break;
    9254                 :            :                 goto out_restart;
    9255                 :            :         }
    9256                 :            :         dprintk("<-- %s\n", __func__);
    9257                 :            :         return;
    9258                 :            : out_restart:
    9259                 :          0 :         task->tk_status = 0;
    9260                 :          0 :         nfs4_sequence_free_slot(&lrp->res.seq_res);
    9261                 :          0 :         rpc_restart_call_prepare(task);
    9262                 :            : }
    9263                 :            : 
    9264                 :          0 : static void nfs4_layoutreturn_release(void *calldata)
    9265                 :            : {
    9266                 :            :         struct nfs4_layoutreturn *lrp = calldata;
    9267                 :          0 :         struct pnfs_layout_hdr *lo = lrp->args.layout;
    9268                 :            : 
    9269                 :            :         dprintk("--> %s\n", __func__);
    9270                 :          0 :         pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
    9271                 :          0 :                         lrp->res.lrs_present ? &lrp->res.stateid : NULL);
    9272                 :          0 :         nfs4_sequence_free_slot(&lrp->res.seq_res);
    9273                 :          0 :         if (lrp->ld_private.ops && lrp->ld_private.ops->free)
    9274                 :          0 :                 lrp->ld_private.ops->free(&lrp->ld_private);
    9275                 :          0 :         pnfs_put_layout_hdr(lrp->args.layout);
    9276                 :          0 :         nfs_iput_and_deactive(lrp->inode);
    9277                 :          0 :         kfree(calldata);
    9278                 :            :         dprintk("<-- %s\n", __func__);
    9279                 :          0 : }
    9280                 :            : 
    9281                 :            : static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
    9282                 :            :         .rpc_call_prepare = nfs4_layoutreturn_prepare,
    9283                 :            :         .rpc_call_done = nfs4_layoutreturn_done,
    9284                 :            :         .rpc_release = nfs4_layoutreturn_release,
    9285                 :            : };
    9286                 :            : 
    9287                 :          0 : int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
    9288                 :            : {
    9289                 :            :         struct rpc_task *task;
    9290                 :          0 :         struct rpc_message msg = {
    9291                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
    9292                 :          0 :                 .rpc_argp = &lrp->args,
    9293                 :          0 :                 .rpc_resp = &lrp->res,
    9294                 :          0 :                 .rpc_cred = lrp->cred,
    9295                 :            :         };
    9296                 :          0 :         struct rpc_task_setup task_setup_data = {
    9297                 :          0 :                 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
    9298                 :            :                 .rpc_message = &msg,
    9299                 :            :                 .callback_ops = &nfs4_layoutreturn_call_ops,
    9300                 :            :                 .callback_data = lrp,
    9301                 :            :         };
    9302                 :            :         int status = 0;
    9303                 :            : 
    9304                 :          0 :         nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
    9305                 :            :                         NFS_SP4_MACH_CRED_PNFS_CLEANUP,
    9306                 :            :                         &task_setup_data.rpc_client, &msg);
    9307                 :            : 
    9308                 :            :         dprintk("--> %s\n", __func__);
    9309                 :          0 :         if (!sync) {
    9310                 :          0 :                 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
    9311                 :          0 :                 if (!lrp->inode) {
    9312                 :          0 :                         nfs4_layoutreturn_release(lrp);
    9313                 :          0 :                         return -EAGAIN;
    9314                 :            :                 }
    9315                 :          0 :                 task_setup_data.flags |= RPC_TASK_ASYNC;
    9316                 :            :         }
    9317                 :            :         nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
    9318                 :          0 :         task = rpc_run_task(&task_setup_data);
    9319                 :          0 :         if (IS_ERR(task))
    9320                 :          0 :                 return PTR_ERR(task);
    9321                 :          0 :         if (sync)
    9322                 :          0 :                 status = task->tk_status;
    9323                 :          0 :         trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
    9324                 :            :         dprintk("<-- %s status=%d\n", __func__, status);
    9325                 :          0 :         rpc_put_task(task);
    9326                 :          0 :         return status;
    9327                 :            : }
    9328                 :            : 
    9329                 :            : static int
    9330                 :          0 : _nfs4_proc_getdeviceinfo(struct nfs_server *server,
    9331                 :            :                 struct pnfs_device *pdev,
    9332                 :            :                 const struct cred *cred)
    9333                 :            : {
    9334                 :          0 :         struct nfs4_getdeviceinfo_args args = {
    9335                 :            :                 .pdev = pdev,
    9336                 :            :                 .notify_types = NOTIFY_DEVICEID4_CHANGE |
    9337                 :            :                         NOTIFY_DEVICEID4_DELETE,
    9338                 :            :         };
    9339                 :          0 :         struct nfs4_getdeviceinfo_res res = {
    9340                 :            :                 .pdev = pdev,
    9341                 :            :         };
    9342                 :          0 :         struct rpc_message msg = {
    9343                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
    9344                 :            :                 .rpc_argp = &args,
    9345                 :            :                 .rpc_resp = &res,
    9346                 :            :                 .rpc_cred = cred,
    9347                 :            :         };
    9348                 :            :         int status;
    9349                 :            : 
    9350                 :            :         dprintk("--> %s\n", __func__);
    9351                 :          0 :         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
    9352                 :            :         if (res.notification & ~args.notify_types)
    9353                 :            :                 dprintk("%s: unsupported notification\n", __func__);
    9354                 :          0 :         if (res.notification != args.notify_types)
    9355                 :          0 :                 pdev->nocache = 1;
    9356                 :            : 
    9357                 :            :         dprintk("<-- %s status=%d\n", __func__, status);
    9358                 :            : 
    9359                 :          0 :         return status;
    9360                 :            : }
    9361                 :            : 
    9362                 :          0 : int nfs4_proc_getdeviceinfo(struct nfs_server *server,
    9363                 :            :                 struct pnfs_device *pdev,
    9364                 :            :                 const struct cred *cred)
    9365                 :            : {
    9366                 :          0 :         struct nfs4_exception exception = { };
    9367                 :            :         int err;
    9368                 :            : 
    9369                 :            :         do {
    9370                 :          0 :                 err = nfs4_handle_exception(server,
    9371                 :            :                                         _nfs4_proc_getdeviceinfo(server, pdev, cred),
    9372                 :            :                                         &exception);
    9373                 :          0 :         } while (exception.retry);
    9374                 :          0 :         return err;
    9375                 :            : }
    9376                 :            : EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
    9377                 :            : 
    9378                 :          0 : static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
    9379                 :            : {
    9380                 :            :         struct nfs4_layoutcommit_data *data = calldata;
    9381                 :          0 :         struct nfs_server *server = NFS_SERVER(data->args.inode);
    9382                 :            : 
    9383                 :          0 :         nfs4_setup_sequence(server->nfs_client,
    9384                 :            :                         &data->args.seq_args,
    9385                 :            :                         &data->res.seq_res,
    9386                 :            :                         task);
    9387                 :          0 : }
    9388                 :            : 
    9389                 :            : static void
    9390                 :          0 : nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
    9391                 :            : {
    9392                 :            :         struct nfs4_layoutcommit_data *data = calldata;
    9393                 :          0 :         struct nfs_server *server = NFS_SERVER(data->args.inode);
    9394                 :            : 
    9395                 :          0 :         if (!nfs41_sequence_done(task, &data->res.seq_res))
    9396                 :            :                 return;
    9397                 :            : 
    9398                 :          0 :         switch (task->tk_status) { /* Just ignore these failures */
    9399                 :            :         case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
    9400                 :            :         case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
    9401                 :            :         case -NFS4ERR_BADLAYOUT:     /* no layout */
    9402                 :            :         case -NFS4ERR_GRACE:        /* loca_recalim always false */
    9403                 :          0 :                 task->tk_status = 0;
    9404                 :            :         case 0:
    9405                 :            :                 break;
    9406                 :            :         default:
    9407                 :          0 :                 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
    9408                 :          0 :                         rpc_restart_call_prepare(task);
    9409                 :          0 :                         return;
    9410                 :            :                 }
    9411                 :            :         }
    9412                 :            : }
    9413                 :            : 
    9414                 :          0 : static void nfs4_layoutcommit_release(void *calldata)
    9415                 :            : {
    9416                 :            :         struct nfs4_layoutcommit_data *data = calldata;
    9417                 :            : 
    9418                 :          0 :         pnfs_cleanup_layoutcommit(data);
    9419                 :          0 :         nfs_post_op_update_inode_force_wcc(data->args.inode,
    9420                 :            :                                            data->res.fattr);
    9421                 :          0 :         put_cred(data->cred);
    9422                 :          0 :         nfs_iput_and_deactive(data->inode);
    9423                 :          0 :         kfree(data);
    9424                 :          0 : }
    9425                 :            : 
    9426                 :            : static const struct rpc_call_ops nfs4_layoutcommit_ops = {
    9427                 :            :         .rpc_call_prepare = nfs4_layoutcommit_prepare,
    9428                 :            :         .rpc_call_done = nfs4_layoutcommit_done,
    9429                 :            :         .rpc_release = nfs4_layoutcommit_release,
    9430                 :            : };
    9431                 :            : 
    9432                 :            : int
    9433                 :          0 : nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
    9434                 :            : {
    9435                 :          0 :         struct rpc_message msg = {
    9436                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
    9437                 :          0 :                 .rpc_argp = &data->args,
    9438                 :          0 :                 .rpc_resp = &data->res,
    9439                 :          0 :                 .rpc_cred = data->cred,
    9440                 :            :         };
    9441                 :          0 :         struct rpc_task_setup task_setup_data = {
    9442                 :          0 :                 .task = &data->task,
    9443                 :          0 :                 .rpc_client = NFS_CLIENT(data->args.inode),
    9444                 :            :                 .rpc_message = &msg,
    9445                 :            :                 .callback_ops = &nfs4_layoutcommit_ops,
    9446                 :            :                 .callback_data = data,
    9447                 :            :         };
    9448                 :            :         struct rpc_task *task;
    9449                 :            :         int status = 0;
    9450                 :            : 
    9451                 :            :         dprintk("NFS: initiating layoutcommit call. sync %d "
    9452                 :            :                 "lbw: %llu inode %lu\n", sync,
    9453                 :            :                 data->args.lastbytewritten,
    9454                 :            :                 data->args.inode->i_ino);
    9455                 :            : 
    9456                 :          0 :         if (!sync) {
    9457                 :          0 :                 data->inode = nfs_igrab_and_active(data->args.inode);
    9458                 :          0 :                 if (data->inode == NULL) {
    9459                 :          0 :                         nfs4_layoutcommit_release(data);
    9460                 :          0 :                         return -EAGAIN;
    9461                 :            :                 }
    9462                 :          0 :                 task_setup_data.flags = RPC_TASK_ASYNC;
    9463                 :            :         }
    9464                 :            :         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
    9465                 :          0 :         task = rpc_run_task(&task_setup_data);
    9466                 :          0 :         if (IS_ERR(task))
    9467                 :          0 :                 return PTR_ERR(task);
    9468                 :          0 :         if (sync)
    9469                 :          0 :                 status = task->tk_status;
    9470                 :          0 :         trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
    9471                 :            :         dprintk("%s: status %d\n", __func__, status);
    9472                 :          0 :         rpc_put_task(task);
    9473                 :          0 :         return status;
    9474                 :            : }
    9475                 :            : 
    9476                 :            : /*
    9477                 :            :  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
    9478                 :            :  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
    9479                 :            :  */
    9480                 :            : static int
    9481                 :          0 : _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
    9482                 :            :                     struct nfs_fsinfo *info,
    9483                 :            :                     struct nfs4_secinfo_flavors *flavors, bool use_integrity)
    9484                 :            : {
    9485                 :          0 :         struct nfs41_secinfo_no_name_args args = {
    9486                 :            :                 .style = SECINFO_STYLE_CURRENT_FH,
    9487                 :            :         };
    9488                 :          0 :         struct nfs4_secinfo_res res = {
    9489                 :            :                 .flavors = flavors,
    9490                 :            :         };
    9491                 :          0 :         struct rpc_message msg = {
    9492                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
    9493                 :            :                 .rpc_argp = &args,
    9494                 :            :                 .rpc_resp = &res,
    9495                 :            :         };
    9496                 :            :         struct rpc_clnt *clnt = server->client;
    9497                 :          0 :         struct nfs4_call_sync_data data = {
    9498                 :            :                 .seq_server = server,
    9499                 :            :                 .seq_args = &args.seq_args,
    9500                 :            :                 .seq_res = &res.seq_res,
    9501                 :            :         };
    9502                 :          0 :         struct rpc_task_setup task_setup = {
    9503                 :          0 :                 .rpc_client = server->client,
    9504                 :            :                 .rpc_message = &msg,
    9505                 :          0 :                 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
    9506                 :            :                 .callback_data = &data,
    9507                 :            :                 .flags = RPC_TASK_NO_ROUND_ROBIN,
    9508                 :            :         };
    9509                 :            :         const struct cred *cred = NULL;
    9510                 :            :         int status;
    9511                 :            : 
    9512                 :          0 :         if (use_integrity) {
    9513                 :          0 :                 clnt = server->nfs_client->cl_rpcclient;
    9514                 :          0 :                 task_setup.rpc_client = clnt;
    9515                 :            : 
    9516                 :          0 :                 cred = nfs4_get_clid_cred(server->nfs_client);
    9517                 :          0 :                 msg.rpc_cred = cred;
    9518                 :            :         }
    9519                 :            : 
    9520                 :            :         dprintk("--> %s\n", __func__);
    9521                 :            :         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
    9522                 :          0 :         status = nfs4_call_sync_custom(&task_setup);
    9523                 :            :         dprintk("<-- %s status=%d\n", __func__, status);
    9524                 :            : 
    9525                 :          0 :         put_cred(cred);
    9526                 :            : 
    9527                 :          0 :         return status;
    9528                 :            : }
    9529                 :            : 
    9530                 :            : static int
    9531                 :          0 : nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
    9532                 :            :                            struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
    9533                 :            : {
    9534                 :          0 :         struct nfs4_exception exception = {
    9535                 :            :                 .interruptible = true,
    9536                 :            :         };
    9537                 :            :         int err;
    9538                 :            :         do {
    9539                 :            :                 /* first try using integrity protection */
    9540                 :            :                 err = -NFS4ERR_WRONGSEC;
    9541                 :            : 
    9542                 :            :                 /* try to use integrity protection with machine cred */
    9543                 :          0 :                 if (_nfs4_is_integrity_protected(server->nfs_client))
    9544                 :          0 :                         err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
    9545                 :            :                                                           flavors, true);
    9546                 :            : 
    9547                 :            :                 /*
    9548                 :            :                  * if unable to use integrity protection, or SECINFO with
    9549                 :            :                  * integrity protection returns NFS4ERR_WRONGSEC (which is
    9550                 :            :                  * disallowed by spec, but exists in deployed servers) use
    9551                 :            :                  * the current filesystem's rpc_client and the user cred.
    9552                 :            :                  */
    9553                 :          0 :                 if (err == -NFS4ERR_WRONGSEC)
    9554                 :          0 :                         err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
    9555                 :            :                                                           flavors, false);
    9556                 :            : 
    9557                 :          0 :                 switch (err) {
    9558                 :            :                 case 0:
    9559                 :            :                 case -NFS4ERR_WRONGSEC:
    9560                 :            :                 case -ENOTSUPP:
    9561                 :            :                         goto out;
    9562                 :            :                 default:
    9563                 :          0 :                         err = nfs4_handle_exception(server, err, &exception);
    9564                 :            :                 }
    9565                 :          0 :         } while (exception.retry);
    9566                 :            : out:
    9567                 :          0 :         return err;
    9568                 :            : }
    9569                 :            : 
    9570                 :            : static int
    9571                 :          0 : nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
    9572                 :            :                     struct nfs_fsinfo *info)
    9573                 :            : {
    9574                 :            :         int err;
    9575                 :            :         struct page *page;
    9576                 :            :         rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
    9577                 :            :         struct nfs4_secinfo_flavors *flavors;
    9578                 :            :         struct nfs4_secinfo4 *secinfo;
    9579                 :            :         int i;
    9580                 :            : 
    9581                 :            :         page = alloc_page(GFP_KERNEL);
    9582                 :          0 :         if (!page) {
    9583                 :            :                 err = -ENOMEM;
    9584                 :            :                 goto out;
    9585                 :            :         }
    9586                 :            : 
    9587                 :            :         flavors = page_address(page);
    9588                 :          0 :         err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
    9589                 :            : 
    9590                 :            :         /*
    9591                 :            :          * Fall back on "guess and check" method if
    9592                 :            :          * the server doesn't support SECINFO_NO_NAME
    9593                 :            :          */
    9594                 :          0 :         if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
    9595                 :          0 :                 err = nfs4_find_root_sec(server, fhandle, info);
    9596                 :          0 :                 goto out_freepage;
    9597                 :            :         }
    9598                 :          0 :         if (err)
    9599                 :            :                 goto out_freepage;
    9600                 :            : 
    9601                 :          0 :         for (i = 0; i < flavors->num_flavors; i++) {
    9602                 :            :                 secinfo = &flavors->flavors[i];
    9603                 :            : 
    9604                 :          0 :                 switch (secinfo->flavor) {
    9605                 :            :                 case RPC_AUTH_NULL:
    9606                 :            :                 case RPC_AUTH_UNIX:
    9607                 :            :                 case RPC_AUTH_GSS:
    9608                 :          0 :                         flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
    9609                 :            :                                         &secinfo->flavor_info);
    9610                 :          0 :                         break;
    9611                 :            :                 default:
    9612                 :            :                         flavor = RPC_AUTH_MAXFLAVOR;
    9613                 :            :                         break;
    9614                 :            :                 }
    9615                 :            : 
    9616                 :          0 :                 if (!nfs_auth_info_match(&server->auth_info, flavor))
    9617                 :            :                         flavor = RPC_AUTH_MAXFLAVOR;
    9618                 :            : 
    9619                 :          0 :                 if (flavor != RPC_AUTH_MAXFLAVOR) {
    9620                 :          0 :                         err = nfs4_lookup_root_sec(server, fhandle,
    9621                 :            :                                                    info, flavor);
    9622                 :          0 :                         if (!err)
    9623                 :            :                                 break;
    9624                 :            :                 }
    9625                 :            :         }
    9626                 :            : 
    9627                 :          0 :         if (flavor == RPC_AUTH_MAXFLAVOR)
    9628                 :            :                 err = -EPERM;
    9629                 :            : 
    9630                 :            : out_freepage:
    9631                 :          0 :         put_page(page);
    9632                 :          0 :         if (err == -EACCES)
    9633                 :            :                 return -EPERM;
    9634                 :            : out:
    9635                 :          0 :         return err;
    9636                 :            : }
    9637                 :            : 
    9638                 :          0 : static int _nfs41_test_stateid(struct nfs_server *server,
    9639                 :            :                 nfs4_stateid *stateid,
    9640                 :            :                 const struct cred *cred)
    9641                 :            : {
    9642                 :            :         int status;
    9643                 :          0 :         struct nfs41_test_stateid_args args = {
    9644                 :            :                 .stateid = stateid,
    9645                 :            :         };
    9646                 :            :         struct nfs41_test_stateid_res res;
    9647                 :          0 :         struct rpc_message msg = {
    9648                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
    9649                 :            :                 .rpc_argp = &args,
    9650                 :            :                 .rpc_resp = &res,
    9651                 :            :                 .rpc_cred = cred,
    9652                 :            :         };
    9653                 :          0 :         struct rpc_clnt *rpc_client = server->client;
    9654                 :            : 
    9655                 :          0 :         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
    9656                 :            :                 &rpc_client, &msg);
    9657                 :            : 
    9658                 :            :         dprintk("NFS call  test_stateid %p\n", stateid);
    9659                 :            :         nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
    9660                 :          0 :         status = nfs4_call_sync_sequence(rpc_client, server, &msg,
    9661                 :            :                         &args.seq_args, &res.seq_res);
    9662                 :          0 :         if (status != NFS_OK) {
    9663                 :            :                 dprintk("NFS reply test_stateid: failed, %d\n", status);
    9664                 :            :                 return status;
    9665                 :            :         }
    9666                 :            :         dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
    9667                 :          0 :         return -res.status;
    9668                 :            : }
    9669                 :            : 
    9670                 :          0 : static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
    9671                 :            :                 int err, struct nfs4_exception *exception)
    9672                 :            : {
    9673                 :          0 :         exception->retry = 0;
    9674                 :          0 :         switch(err) {
    9675                 :            :         case -NFS4ERR_DELAY:
    9676                 :            :         case -NFS4ERR_RETRY_UNCACHED_REP:
    9677                 :          0 :                 nfs4_handle_exception(server, err, exception);
    9678                 :          0 :                 break;
    9679                 :            :         case -NFS4ERR_BADSESSION:
    9680                 :            :         case -NFS4ERR_BADSLOT:
    9681                 :            :         case -NFS4ERR_BAD_HIGH_SLOT:
    9682                 :            :         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
    9683                 :            :         case -NFS4ERR_DEADSESSION:
    9684                 :          0 :                 nfs4_do_handle_exception(server, err, exception);
    9685                 :            :         }
    9686                 :          0 : }
    9687                 :            : 
    9688                 :            : /**
    9689                 :            :  * nfs41_test_stateid - perform a TEST_STATEID operation
    9690                 :            :  *
    9691                 :            :  * @server: server / transport on which to perform the operation
    9692                 :            :  * @stateid: state ID to test
    9693                 :            :  * @cred: credential
    9694                 :            :  *
    9695                 :            :  * Returns NFS_OK if the server recognizes that "stateid" is valid.
    9696                 :            :  * Otherwise a negative NFS4ERR value is returned if the operation
    9697                 :            :  * failed or the state ID is not currently valid.
    9698                 :            :  */
    9699                 :          0 : static int nfs41_test_stateid(struct nfs_server *server,
    9700                 :            :                 nfs4_stateid *stateid,
    9701                 :            :                 const struct cred *cred)
    9702                 :            : {
    9703                 :          0 :         struct nfs4_exception exception = {
    9704                 :            :                 .interruptible = true,
    9705                 :            :         };
    9706                 :            :         int err;
    9707                 :            :         do {
    9708                 :          0 :                 err = _nfs41_test_stateid(server, stateid, cred);
    9709                 :          0 :                 nfs4_handle_delay_or_session_error(server, err, &exception);
    9710                 :          0 :         } while (exception.retry);
    9711                 :          0 :         return err;
    9712                 :            : }
    9713                 :            : 
    9714                 :            : struct nfs_free_stateid_data {
    9715                 :            :         struct nfs_server *server;
    9716                 :            :         struct nfs41_free_stateid_args args;
    9717                 :            :         struct nfs41_free_stateid_res res;
    9718                 :            : };
    9719                 :            : 
    9720                 :          0 : static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
    9721                 :            : {
    9722                 :            :         struct nfs_free_stateid_data *data = calldata;
    9723                 :          0 :         nfs4_setup_sequence(data->server->nfs_client,
    9724                 :            :                         &data->args.seq_args,
    9725                 :            :                         &data->res.seq_res,
    9726                 :            :                         task);
    9727                 :          0 : }
    9728                 :            : 
    9729                 :          0 : static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
    9730                 :            : {
    9731                 :            :         struct nfs_free_stateid_data *data = calldata;
    9732                 :            : 
    9733                 :          0 :         nfs41_sequence_done(task, &data->res.seq_res);
    9734                 :            : 
    9735                 :          0 :         switch (task->tk_status) {
    9736                 :            :         case -NFS4ERR_DELAY:
    9737                 :          0 :                 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
    9738                 :          0 :                         rpc_restart_call_prepare(task);
    9739                 :            :         }
    9740                 :          0 : }
    9741                 :            : 
    9742                 :          0 : static void nfs41_free_stateid_release(void *calldata)
    9743                 :            : {
    9744                 :          0 :         kfree(calldata);
    9745                 :          0 : }
    9746                 :            : 
    9747                 :            : static const struct rpc_call_ops nfs41_free_stateid_ops = {
    9748                 :            :         .rpc_call_prepare = nfs41_free_stateid_prepare,
    9749                 :            :         .rpc_call_done = nfs41_free_stateid_done,
    9750                 :            :         .rpc_release = nfs41_free_stateid_release,
    9751                 :            : };
    9752                 :            : 
    9753                 :            : /**
    9754                 :            :  * nfs41_free_stateid - perform a FREE_STATEID operation
    9755                 :            :  *
    9756                 :            :  * @server: server / transport on which to perform the operation
    9757                 :            :  * @stateid: state ID to release
    9758                 :            :  * @cred: credential
    9759                 :            :  * @privileged: set to true if this call needs to be privileged
    9760                 :            :  *
    9761                 :            :  * Note: this function is always asynchronous.
    9762                 :            :  */
    9763                 :          0 : static int nfs41_free_stateid(struct nfs_server *server,
    9764                 :            :                 const nfs4_stateid *stateid,
    9765                 :            :                 const struct cred *cred,
    9766                 :            :                 bool privileged)
    9767                 :            : {
    9768                 :          0 :         struct rpc_message msg = {
    9769                 :            :                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
    9770                 :            :                 .rpc_cred = cred,
    9771                 :            :         };
    9772                 :          0 :         struct rpc_task_setup task_setup = {
    9773                 :          0 :                 .rpc_client = server->client,
    9774                 :            :                 .rpc_message = &msg,
    9775                 :            :                 .callback_ops = &nfs41_free_stateid_ops,
    9776                 :            :                 .flags = RPC_TASK_ASYNC,
    9777                 :            :         };
    9778                 :            :         struct nfs_free_stateid_data *data;
    9779                 :            :         struct rpc_task *task;
    9780                 :            : 
    9781                 :          0 :         nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
    9782                 :            :                 &task_setup.rpc_client, &msg);
    9783                 :            : 
    9784                 :            :         dprintk("NFS call  free_stateid %p\n", stateid);
    9785                 :            :         data = kmalloc(sizeof(*data), GFP_NOFS);
    9786                 :          0 :         if (!data)
    9787                 :            :                 return -ENOMEM;
    9788                 :          0 :         data->server = server;
    9789                 :            :         nfs4_stateid_copy(&data->args.stateid, stateid);
    9790                 :            : 
    9791                 :          0 :         task_setup.callback_data = data;
    9792                 :            : 
    9793                 :          0 :         msg.rpc_argp = &data->args;
    9794                 :          0 :         msg.rpc_resp = &data->res;
    9795                 :            :         nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
    9796                 :          0 :         task = rpc_run_task(&task_setup);
    9797                 :          0 :         if (IS_ERR(task))
    9798                 :          0 :                 return PTR_ERR(task);
    9799                 :          0 :         rpc_put_task(task);
    9800                 :          0 :         return 0;
    9801                 :            : }
    9802                 :            : 
    9803                 :            : static void
    9804                 :          0 : nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
    9805                 :            : {
    9806                 :          0 :         const struct cred *cred = lsp->ls_state->owner->so_cred;
    9807                 :            : 
    9808                 :          0 :         nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
    9809                 :          0 :         nfs4_free_lock_state(server, lsp);
    9810                 :          0 : }
    9811                 :            : 
    9812                 :          0 : static bool nfs41_match_stateid(const nfs4_stateid *s1,
    9813                 :            :                 const nfs4_stateid *s2)
    9814                 :            : {
    9815                 :          0 :         if (s1->type != s2->type)
    9816                 :            :                 return false;
    9817                 :            : 
    9818                 :          0 :         if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
    9819                 :            :                 return false;
    9820                 :            : 
    9821                 :          0 :         if (s1->seqid == s2->seqid)
    9822                 :            :                 return true;
    9823                 :            : 
    9824                 :          0 :         return s1->seqid == 0 || s2->seqid == 0;
    9825                 :            : }
    9826                 :            : 
    9827                 :            : #endif /* CONFIG_NFS_V4_1 */
    9828                 :            : 
    9829                 :          0 : static bool nfs4_match_stateid(const nfs4_stateid *s1,
    9830                 :            :                 const nfs4_stateid *s2)
    9831                 :            : {
    9832                 :          0 :         return nfs4_stateid_match(s1, s2);
    9833                 :            : }
    9834                 :            : 
    9835                 :            : 
    9836                 :            : static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
    9837                 :            :         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
    9838                 :            :         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
    9839                 :            :         .recover_open   = nfs4_open_reclaim,
    9840                 :            :         .recover_lock   = nfs4_lock_reclaim,
    9841                 :            :         .establish_clid = nfs4_init_clientid,
    9842                 :            :         .detect_trunking = nfs40_discover_server_trunking,
    9843                 :            : };
    9844                 :            : 
    9845                 :            : #if defined(CONFIG_NFS_V4_1)
    9846                 :            : static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
    9847                 :            :         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
    9848                 :            :         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
    9849                 :            :         .recover_open   = nfs4_open_reclaim,
    9850                 :            :         .recover_lock   = nfs4_lock_reclaim,
    9851                 :            :         .establish_clid = nfs41_init_clientid,
    9852                 :            :         .reclaim_complete = nfs41_proc_reclaim_complete,
    9853                 :            :         .detect_trunking = nfs41_discover_server_trunking,
    9854                 :            : };
    9855                 :            : #endif /* CONFIG_NFS_V4_1 */
    9856                 :            : 
    9857                 :            : static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
    9858                 :            :         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
    9859                 :            :         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
    9860                 :            :         .recover_open   = nfs40_open_expired,
    9861                 :            :         .recover_lock   = nfs4_lock_expired,
    9862                 :            :         .establish_clid = nfs4_init_clientid,
    9863                 :            : };
    9864                 :            : 
    9865                 :            : #if defined(CONFIG_NFS_V4_1)
    9866                 :            : static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
    9867                 :            :         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
    9868                 :            :         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
    9869                 :            :         .recover_open   = nfs41_open_expired,
    9870                 :            :         .recover_lock   = nfs41_lock_expired,
    9871                 :            :         .establish_clid = nfs41_init_clientid,
    9872                 :            : };
    9873                 :            : #endif /* CONFIG_NFS_V4_1 */
    9874                 :            : 
    9875                 :            : static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
    9876                 :            :         .sched_state_renewal = nfs4_proc_async_renew,
    9877                 :            :         .get_state_renewal_cred = nfs4_get_renew_cred,
    9878                 :            :         .renew_lease = nfs4_proc_renew,
    9879                 :            : };
    9880                 :            : 
    9881                 :            : #if defined(CONFIG_NFS_V4_1)
    9882                 :            : static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
    9883                 :            :         .sched_state_renewal = nfs41_proc_async_sequence,
    9884                 :            :         .get_state_renewal_cred = nfs4_get_machine_cred,
    9885                 :            :         .renew_lease = nfs4_proc_sequence,
    9886                 :            : };
    9887                 :            : #endif
    9888                 :            : 
    9889                 :            : static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
    9890                 :            :         .get_locations = _nfs40_proc_get_locations,
    9891                 :            :         .fsid_present = _nfs40_proc_fsid_present,
    9892                 :            : };
    9893                 :            : 
    9894                 :            : #if defined(CONFIG_NFS_V4_1)
    9895                 :            : static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
    9896                 :            :         .get_locations = _nfs41_proc_get_locations,
    9897                 :            :         .fsid_present = _nfs41_proc_fsid_present,
    9898                 :            : };
    9899                 :            : #endif  /* CONFIG_NFS_V4_1 */
    9900                 :            : 
    9901                 :            : static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
    9902                 :            :         .minor_version = 0,
    9903                 :            :         .init_caps = NFS_CAP_READDIRPLUS
    9904                 :            :                 | NFS_CAP_ATOMIC_OPEN
    9905                 :            :                 | NFS_CAP_POSIX_LOCK,
    9906                 :            :         .init_client = nfs40_init_client,
    9907                 :            :         .shutdown_client = nfs40_shutdown_client,
    9908                 :            :         .match_stateid = nfs4_match_stateid,
    9909                 :            :         .find_root_sec = nfs4_find_root_sec,
    9910                 :            :         .free_lock_state = nfs4_release_lockowner,
    9911                 :            :         .test_and_free_expired = nfs40_test_and_free_expired_stateid,
    9912                 :            :         .alloc_seqid = nfs_alloc_seqid,
    9913                 :            :         .call_sync_ops = &nfs40_call_sync_ops,
    9914                 :            :         .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
    9915                 :            :         .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
    9916                 :            :         .state_renewal_ops = &nfs40_state_renewal_ops,
    9917                 :            :         .mig_recovery_ops = &nfs40_mig_recovery_ops,
    9918                 :            : };
    9919                 :            : 
    9920                 :            : #if defined(CONFIG_NFS_V4_1)
    9921                 :            : static struct nfs_seqid *
    9922                 :          0 : nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
    9923                 :            : {
    9924                 :          0 :         return NULL;
    9925                 :            : }
    9926                 :            : 
    9927                 :            : static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
    9928                 :            :         .minor_version = 1,
    9929                 :            :         .init_caps = NFS_CAP_READDIRPLUS
    9930                 :            :                 | NFS_CAP_ATOMIC_OPEN
    9931                 :            :                 | NFS_CAP_POSIX_LOCK
    9932                 :            :                 | NFS_CAP_STATEID_NFSV41
    9933                 :            :                 | NFS_CAP_ATOMIC_OPEN_V1
    9934                 :            :                 | NFS_CAP_LGOPEN,
    9935                 :            :         .init_client = nfs41_init_client,
    9936                 :            :         .shutdown_client = nfs41_shutdown_client,
    9937                 :            :         .match_stateid = nfs41_match_stateid,
    9938                 :            :         .find_root_sec = nfs41_find_root_sec,
    9939                 :            :         .free_lock_state = nfs41_free_lock_state,
    9940                 :            :         .test_and_free_expired = nfs41_test_and_free_expired_stateid,
    9941                 :            :         .alloc_seqid = nfs_alloc_no_seqid,
    9942                 :            :         .session_trunk = nfs4_test_session_trunk,
    9943                 :            :         .call_sync_ops = &nfs41_call_sync_ops,
    9944                 :            :         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
    9945                 :            :         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
    9946                 :            :         .state_renewal_ops = &nfs41_state_renewal_ops,
    9947                 :            :         .mig_recovery_ops = &nfs41_mig_recovery_ops,
    9948                 :            : };
    9949                 :            : #endif
    9950                 :            : 
    9951                 :            : #if defined(CONFIG_NFS_V4_2)
    9952                 :            : static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
    9953                 :            :         .minor_version = 2,
    9954                 :            :         .init_caps = NFS_CAP_READDIRPLUS
    9955                 :            :                 | NFS_CAP_ATOMIC_OPEN
    9956                 :            :                 | NFS_CAP_POSIX_LOCK
    9957                 :            :                 | NFS_CAP_STATEID_NFSV41
    9958                 :            :                 | NFS_CAP_ATOMIC_OPEN_V1
    9959                 :            :                 | NFS_CAP_LGOPEN
    9960                 :            :                 | NFS_CAP_ALLOCATE
    9961                 :            :                 | NFS_CAP_COPY
    9962                 :            :                 | NFS_CAP_OFFLOAD_CANCEL
    9963                 :            :                 | NFS_CAP_DEALLOCATE
    9964                 :            :                 | NFS_CAP_SEEK
    9965                 :            :                 | NFS_CAP_LAYOUTSTATS
    9966                 :            :                 | NFS_CAP_CLONE
    9967                 :            :                 | NFS_CAP_LAYOUTERROR,
    9968                 :            :         .init_client = nfs41_init_client,
    9969                 :            :         .shutdown_client = nfs41_shutdown_client,
    9970                 :            :         .match_stateid = nfs41_match_stateid,
    9971                 :            :         .find_root_sec = nfs41_find_root_sec,
    9972                 :            :         .free_lock_state = nfs41_free_lock_state,
    9973                 :            :         .call_sync_ops = &nfs41_call_sync_ops,
    9974                 :            :         .test_and_free_expired = nfs41_test_and_free_expired_stateid,
    9975                 :            :         .alloc_seqid = nfs_alloc_no_seqid,
    9976                 :            :         .session_trunk = nfs4_test_session_trunk,
    9977                 :            :         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
    9978                 :            :         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
    9979                 :            :         .state_renewal_ops = &nfs41_state_renewal_ops,
    9980                 :            :         .mig_recovery_ops = &nfs41_mig_recovery_ops,
    9981                 :            : };
    9982                 :            : #endif
    9983                 :            : 
    9984                 :            : const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
    9985                 :            :         [0] = &nfs_v4_0_minor_ops,
    9986                 :            : #if defined(CONFIG_NFS_V4_1)
    9987                 :            :         [1] = &nfs_v4_1_minor_ops,
    9988                 :            : #endif
    9989                 :            : #if defined(CONFIG_NFS_V4_2)
    9990                 :            :         [2] = &nfs_v4_2_minor_ops,
    9991                 :            : #endif
    9992                 :            : };
    9993                 :            : 
    9994                 :          0 : static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
    9995                 :            : {
    9996                 :            :         ssize_t error, error2;
    9997                 :            : 
    9998                 :          0 :         error = generic_listxattr(dentry, list, size);
    9999                 :          0 :         if (error < 0)
   10000                 :            :                 return error;
   10001                 :          0 :         if (list) {
   10002                 :          0 :                 list += error;
   10003                 :          0 :                 size -= error;
   10004                 :            :         }
   10005                 :            : 
   10006                 :          0 :         error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
   10007                 :          0 :         if (error2 < 0)
   10008                 :            :                 return error2;
   10009                 :          0 :         return error + error2;
   10010                 :            : }
   10011                 :            : 
   10012                 :            : static const struct inode_operations nfs4_dir_inode_operations = {
   10013                 :            :         .create         = nfs_create,
   10014                 :            :         .lookup         = nfs_lookup,
   10015                 :            :         .atomic_open    = nfs_atomic_open,
   10016                 :            :         .link           = nfs_link,
   10017                 :            :         .unlink         = nfs_unlink,
   10018                 :            :         .symlink        = nfs_symlink,
   10019                 :            :         .mkdir          = nfs_mkdir,
   10020                 :            :         .rmdir          = nfs_rmdir,
   10021                 :            :         .mknod          = nfs_mknod,
   10022                 :            :         .rename         = nfs_rename,
   10023                 :            :         .permission     = nfs_permission,
   10024                 :            :         .getattr        = nfs_getattr,
   10025                 :            :         .setattr        = nfs_setattr,
   10026                 :            :         .listxattr      = nfs4_listxattr,
   10027                 :            : };
   10028                 :            : 
   10029                 :            : static const struct inode_operations nfs4_file_inode_operations = {
   10030                 :            :         .permission     = nfs_permission,
   10031                 :            :         .getattr        = nfs_getattr,
   10032                 :            :         .setattr        = nfs_setattr,
   10033                 :            :         .listxattr      = nfs4_listxattr,
   10034                 :            : };
   10035                 :            : 
   10036                 :            : const struct nfs_rpc_ops nfs_v4_clientops = {
   10037                 :            :         .version        = 4,                    /* protocol version */
   10038                 :            :         .dentry_ops     = &nfs4_dentry_operations,
   10039                 :            :         .dir_inode_ops  = &nfs4_dir_inode_operations,
   10040                 :            :         .file_inode_ops = &nfs4_file_inode_operations,
   10041                 :            :         .file_ops       = &nfs4_file_operations,
   10042                 :            :         .getroot        = nfs4_proc_get_root,
   10043                 :            :         .submount       = nfs4_submount,
   10044                 :            :         .try_mount      = nfs4_try_mount,
   10045                 :            :         .getattr        = nfs4_proc_getattr,
   10046                 :            :         .setattr        = nfs4_proc_setattr,
   10047                 :            :         .lookup         = nfs4_proc_lookup,
   10048                 :            :         .lookupp        = nfs4_proc_lookupp,
   10049                 :            :         .access         = nfs4_proc_access,
   10050                 :            :         .readlink       = nfs4_proc_readlink,
   10051                 :            :         .create         = nfs4_proc_create,
   10052                 :            :         .remove         = nfs4_proc_remove,
   10053                 :            :         .unlink_setup   = nfs4_proc_unlink_setup,
   10054                 :            :         .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
   10055                 :            :         .unlink_done    = nfs4_proc_unlink_done,
   10056                 :            :         .rename_setup   = nfs4_proc_rename_setup,
   10057                 :            :         .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
   10058                 :            :         .rename_done    = nfs4_proc_rename_done,
   10059                 :            :         .link           = nfs4_proc_link,
   10060                 :            :         .symlink        = nfs4_proc_symlink,
   10061                 :            :         .mkdir          = nfs4_proc_mkdir,
   10062                 :            :         .rmdir          = nfs4_proc_rmdir,
   10063                 :            :         .readdir        = nfs4_proc_readdir,
   10064                 :            :         .mknod          = nfs4_proc_mknod,
   10065                 :            :         .statfs         = nfs4_proc_statfs,
   10066                 :            :         .fsinfo         = nfs4_proc_fsinfo,
   10067                 :            :         .pathconf       = nfs4_proc_pathconf,
   10068                 :            :         .set_capabilities = nfs4_server_capabilities,
   10069                 :            :         .decode_dirent  = nfs4_decode_dirent,
   10070                 :            :         .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
   10071                 :            :         .read_setup     = nfs4_proc_read_setup,
   10072                 :            :         .read_done      = nfs4_read_done,
   10073                 :            :         .write_setup    = nfs4_proc_write_setup,
   10074                 :            :         .write_done     = nfs4_write_done,
   10075                 :            :         .commit_setup   = nfs4_proc_commit_setup,
   10076                 :            :         .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
   10077                 :            :         .commit_done    = nfs4_commit_done,
   10078                 :            :         .lock           = nfs4_proc_lock,
   10079                 :            :         .clear_acl_cache = nfs4_zap_acl_attr,
   10080                 :            :         .close_context  = nfs4_close_context,
   10081                 :            :         .open_context   = nfs4_atomic_open,
   10082                 :            :         .have_delegation = nfs4_have_delegation,
   10083                 :            :         .alloc_client   = nfs4_alloc_client,
   10084                 :            :         .init_client    = nfs4_init_client,
   10085                 :            :         .free_client    = nfs4_free_client,
   10086                 :            :         .create_server  = nfs4_create_server,
   10087                 :            :         .clone_server   = nfs_clone_server,
   10088                 :            : };
   10089                 :            : 
   10090                 :            : static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
   10091                 :            :         .name   = XATTR_NAME_NFSV4_ACL,
   10092                 :            :         .list   = nfs4_xattr_list_nfs4_acl,
   10093                 :            :         .get    = nfs4_xattr_get_nfs4_acl,
   10094                 :            :         .set    = nfs4_xattr_set_nfs4_acl,
   10095                 :            : };
   10096                 :            : 
   10097                 :            : const struct xattr_handler *nfs4_xattr_handlers[] = {
   10098                 :            :         &nfs4_xattr_nfs4_acl_handler,
   10099                 :            : #ifdef CONFIG_NFS_V4_SECURITY_LABEL
   10100                 :            :         &nfs4_xattr_nfs4_label_handler,
   10101                 :            : #endif
   10102                 :            :         NULL
   10103                 :            : };
   10104                 :            : 
   10105                 :            : /*
   10106                 :            :  * Local variables:
   10107                 :            :  *  c-basic-offset: 8
   10108                 :            :  * End:
   10109                 :            :  */
    

Generated by: LCOV version 1.14