1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*
  27  *      Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
  28  *      All Rights Reserved
  29  */
  30 
  31 /*
  32  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
  33  * Copyright 2019 Nexenta Systems, Inc.
  34  * Copyright 2019 Nexenta by DDN, Inc.
  35  */
  36 
  37 #include <sys/param.h>
  38 #include <sys/types.h>
  39 #include <sys/systm.h>
  40 #include <sys/cred.h>
  41 #include <sys/buf.h>
  42 #include <sys/vfs.h>
  43 #include <sys/vfs_opreg.h>
  44 #include <sys/vnode.h>
  45 #include <sys/uio.h>
  46 #include <sys/errno.h>
  47 #include <sys/sysmacros.h>
  48 #include <sys/statvfs.h>
  49 #include <sys/kmem.h>
  50 #include <sys/dirent.h>
  51 #include <sys/cmn_err.h>
  52 #include <sys/debug.h>
  53 #include <sys/systeminfo.h>
  54 #include <sys/flock.h>
  55 #include <sys/pathname.h>
  56 #include <sys/nbmlock.h>
  57 #include <sys/share.h>
  58 #include <sys/atomic.h>
  59 #include <sys/policy.h>
  60 #include <sys/fem.h>
  61 #include <sys/sdt.h>
  62 #include <sys/ddi.h>
  63 #include <sys/zone.h>
  64 
  65 #include <fs/fs_reparse.h>
  66 
  67 #include <rpc/types.h>
  68 #include <rpc/auth.h>
  69 #include <rpc/rpcsec_gss.h>
  70 #include <rpc/svc.h>
  71 
  72 #include <nfs/nfs.h>
  73 #include <nfs/nfssys.h>
  74 #include <nfs/export.h>
  75 #include <nfs/nfs_cmd.h>
  76 #include <nfs/lm.h>
  77 #include <nfs/nfs4.h>
  78 #include <nfs/nfs4_drc.h>
  79 
  80 #include <sys/strsubr.h>
  81 #include <sys/strsun.h>
  82 
  83 #include <inet/common.h>
  84 #include <inet/ip.h>
  85 #include <inet/ip6.h>
  86 
  87 #include <sys/tsol/label.h>
  88 #include <sys/tsol/tndb.h>
  89 
  90 #define RFS4_MAXLOCK_TRIES 4    /* Try to get the lock this many times */
  91 static int rfs4_maxlock_tries = RFS4_MAXLOCK_TRIES;
  92 #define RFS4_LOCK_DELAY 10      /* Milliseconds */
  93 static clock_t  rfs4_lock_delay = RFS4_LOCK_DELAY;
  94 extern struct svc_ops rdma_svc_ops;
  95 extern int nfs_loaned_buffers;
  96 /* End of Tunables */
  97 
  98 static int rdma_setup_read_data4(READ4args *, READ4res *);
  99 
 100 /*
 101  * Used to bump the stateid4.seqid value and show changes in the stateid
 102  */
 103 #define next_stateid(sp) (++(sp)->bits.chgseq)
 104 
 105 /*
 106  * RFS4_MINLEN_ENTRY4: XDR-encoded size of smallest possible dirent.
 107  *      This is used to return NFS4ERR_TOOSMALL when clients specify
 108  *      maxcount that isn't large enough to hold the smallest possible
 109  *      XDR encoded dirent.
 110  *
 111  *          sizeof cookie (8 bytes) +
 112  *          sizeof name_len (4 bytes) +
 113  *          sizeof smallest (padded) name (4 bytes) +
 114  *          sizeof bitmap4_len (12 bytes) +   NOTE: we always encode len=2 bm4
 115  *          sizeof attrlist4_len (4 bytes) +
 116  *          sizeof next boolean (4 bytes)
 117  *
 118  * RFS4_MINLEN_RDDIR4: XDR-encoded size of READDIR op reply containing
 119  * the smallest possible entry4 (assumes no attrs requested).
 120  *      sizeof nfsstat4 (4 bytes) +
 121  *      sizeof verifier4 (8 bytes) +
 122  *      sizeof entry4list bool (4 bytes) +
 123  *      sizeof entry4   (36 bytes) +
 124  *      sizeof eof bool  (4 bytes)
 125  *
 126  * RFS4_MINLEN_RDDIR_BUF: minimum length of buffer server will provide to
 127  *      VOP_READDIR.  Its value is the size of the maximum possible dirent
 128  *      for solaris.  The DIRENT64_RECLEN macro returns the size of dirent
 129  *      required for a given name length.  MAXNAMELEN is the maximum
 130  *      filename length allowed in Solaris.  The first two DIRENT64_RECLEN()
 131  *      macros are to allow for . and .. entries -- just a minor tweak to try
 132  *      and guarantee that buffer we give to VOP_READDIR will be large enough
 133  *      to hold ., .., and the largest possible solaris dirent64.
 134  */
 135 #define RFS4_MINLEN_ENTRY4 36
 136 #define RFS4_MINLEN_RDDIR4 (4 + NFS4_VERIFIER_SIZE + 4 + RFS4_MINLEN_ENTRY4 + 4)
 137 #define RFS4_MINLEN_RDDIR_BUF \
 138         (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2) + DIRENT64_RECLEN(MAXNAMELEN))
 139 
 140 /*
 141  * It would be better to pad to 4 bytes since that's what XDR would do,
 142  * but the dirents UFS gives us are already padded to 8, so just take
 143  * what we're given.  Dircount is only a hint anyway.  Currently the
 144  * solaris kernel is ASCII only, so there's no point in calling the
 145  * UTF8 functions.
 146  *
 147  * dirent64: named padded to provide 8 byte struct alignment
 148  *      d_ino(8) + d_off(8) + d_reclen(2) + d_name(namelen + null(1) + pad)
 149  *
 150  * cookie: uint64_t   +  utf8namelen: uint_t  +   utf8name padded to 8 bytes
 151  *
 152  */
 153 #define DIRENT64_TO_DIRCOUNT(dp) \
 154         (3 * BYTES_PER_XDR_UNIT + DIRENT64_NAMELEN((dp)->d_reclen))
 155 
 156 
 157 static sysid_t          lockt_sysid;    /* dummy sysid for all LOCKT calls */
 158 
 159 u_longlong_t    nfs4_srv_caller_id;
 160 uint_t          nfs4_srv_vkey = 0;
 161 
 162 void    rfs4_init_compound_state(struct compound_state *);
 163 
 164 static void     nullfree(caddr_t);
 165 static void     rfs4_op_inval(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 166                     struct compound_state *);
 167 static void     rfs4_op_access(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 168                     struct compound_state *);
 169 static void     rfs4_op_close(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 170                     struct compound_state *);
 171 static void     rfs4_op_commit(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 172                     struct compound_state *);
 173 static void     rfs4_op_create(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 174                     struct compound_state *);
 175 static void     rfs4_op_create_free(nfs_resop4 *resop);
 176 static void     rfs4_op_delegreturn(nfs_argop4 *, nfs_resop4 *,
 177                     struct svc_req *, struct compound_state *);
 178 static void     rfs4_op_delegpurge(nfs_argop4 *, nfs_resop4 *,
 179                     struct svc_req *, struct compound_state *);
 180 static void     rfs4_op_getattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 181                     struct compound_state *);
 182 static void     rfs4_op_getattr_free(nfs_resop4 *);
 183 static void     rfs4_op_getfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 184                     struct compound_state *);
 185 static void     rfs4_op_getfh_free(nfs_resop4 *);
 186 static void     rfs4_op_illegal(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 187                     struct compound_state *);
 188 static void     rfs4_op_link(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 189                     struct compound_state *);
 190 static void     rfs4_op_lock(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 191                     struct compound_state *);
 192 static void     lock_denied_free(nfs_resop4 *);
 193 static void     rfs4_op_locku(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 194                     struct compound_state *);
 195 static void     rfs4_op_lockt(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 196                     struct compound_state *);
 197 static void     rfs4_op_lookup(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 198                     struct compound_state *);
 199 static void     rfs4_op_lookupp(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 200                     struct compound_state *);
 201 static void     rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop,
 202                     struct svc_req *req, struct compound_state *cs);
 203 static void     rfs4_op_nverify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 204                     struct compound_state *);
 205 static void     rfs4_op_open(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 206                     struct compound_state *);
 207 static void     rfs4_op_open_confirm(nfs_argop4 *, nfs_resop4 *,
 208                     struct svc_req *, struct compound_state *);
 209 static void     rfs4_op_open_downgrade(nfs_argop4 *, nfs_resop4 *,
 210                     struct svc_req *, struct compound_state *);
 211 static void     rfs4_op_putfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 212                     struct compound_state *);
 213 static void     rfs4_op_putpubfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 214                     struct compound_state *);
 215 static void     rfs4_op_putrootfh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 216                     struct compound_state *);
 217 static void     rfs4_op_read(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 218                     struct compound_state *);
 219 static void     rfs4_op_read_free(nfs_resop4 *);
 220 static void     rfs4_op_readdir_free(nfs_resop4 *resop);
 221 static void     rfs4_op_readlink(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 222                     struct compound_state *);
 223 static void     rfs4_op_readlink_free(nfs_resop4 *);
 224 static void     rfs4_op_release_lockowner(nfs_argop4 *, nfs_resop4 *,
 225                     struct svc_req *, struct compound_state *);
 226 static void     rfs4_op_remove(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 227                     struct compound_state *);
 228 static void     rfs4_op_rename(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 229                     struct compound_state *);
 230 static void     rfs4_op_renew(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 231                     struct compound_state *);
 232 static void     rfs4_op_restorefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 233                     struct compound_state *);
 234 static void     rfs4_op_savefh(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 235                     struct compound_state *);
 236 static void     rfs4_op_setattr(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 237                     struct compound_state *);
 238 static void     rfs4_op_verify(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 239                     struct compound_state *);
 240 static void     rfs4_op_write(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 241                     struct compound_state *);
 242 static void     rfs4_op_setclientid(nfs_argop4 *, nfs_resop4 *,
 243                     struct svc_req *, struct compound_state *);
 244 static void     rfs4_op_setclientid_confirm(nfs_argop4 *, nfs_resop4 *,
 245                     struct svc_req *req, struct compound_state *);
 246 static void     rfs4_op_secinfo(nfs_argop4 *, nfs_resop4 *, struct svc_req *,
 247                     struct compound_state *);
 248 static void     rfs4_op_secinfo_free(nfs_resop4 *);
 249 
 250 static nfsstat4 check_open_access(uint32_t, struct compound_state *,
 251                     struct svc_req *);
 252 nfsstat4        rfs4_client_sysid(rfs4_client_t *, sysid_t *);
 253 void            rfs4_ss_clid(nfs4_srv_t *, rfs4_client_t *);
 254 
 255 
 256 /*
 257  * translation table for attrs
 258  */
 259 struct nfs4_ntov_table {
 260         union nfs4_attr_u *na;
 261         uint8_t amap[NFS4_MAXNUM_ATTRS];
 262         int attrcnt;
 263         bool_t vfsstat;
 264 };
 265 
 266 static void     nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp);
 267 static void     nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
 268                     struct nfs4_svgetit_arg *sargp);
 269 
 270 static nfsstat4 do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp,
 271                     struct compound_state *cs, struct nfs4_svgetit_arg *sargp,
 272                     struct nfs4_ntov_table *ntovp, nfs4_attr_cmd_t cmd);
 273 
 274 static void     hanfsv4_failover(nfs4_srv_t *);
 275 
 276 fem_t           *deleg_rdops;
 277 fem_t           *deleg_wrops;
 278 
 279 /*
 280  * NFS4 op dispatch table
 281  */
 282 
 283 struct rfsv4disp {
 284         void    (*dis_proc)();          /* proc to call */
 285         void    (*dis_resfree)();       /* frees space allocated by proc */
 286         int     dis_flags;              /* RPC_IDEMPOTENT, etc... */
 287 };
 288 
 289 static struct rfsv4disp rfsv4disptab[] = {
 290         /*
 291          * NFS VERSION 4
 292          */
 293 
 294         /* RFS_NULL = 0 */
 295         {rfs4_op_illegal, nullfree, 0},
 296 
 297         /* UNUSED = 1 */
 298         {rfs4_op_illegal, nullfree, 0},
 299 
 300         /* UNUSED = 2 */
 301         {rfs4_op_illegal, nullfree, 0},
 302 
 303         /* OP_ACCESS = 3 */
 304         {rfs4_op_access, nullfree, RPC_IDEMPOTENT},
 305 
 306         /* OP_CLOSE = 4 */
 307         {rfs4_op_close, nullfree, 0},
 308 
 309         /* OP_COMMIT = 5 */
 310         {rfs4_op_commit, nullfree, RPC_IDEMPOTENT},
 311 
 312         /* OP_CREATE = 6 */
 313         {rfs4_op_create, nullfree, 0},
 314 
 315         /* OP_DELEGPURGE = 7 */
 316         {rfs4_op_delegpurge, nullfree, 0},
 317 
 318         /* OP_DELEGRETURN = 8 */
 319         {rfs4_op_delegreturn, nullfree, 0},
 320 
 321         /* OP_GETATTR = 9 */
 322         {rfs4_op_getattr, rfs4_op_getattr_free, RPC_IDEMPOTENT},
 323 
 324         /* OP_GETFH = 10 */
 325         {rfs4_op_getfh, rfs4_op_getfh_free, RPC_ALL},
 326 
 327         /* OP_LINK = 11 */
 328         {rfs4_op_link, nullfree, 0},
 329 
 330         /* OP_LOCK = 12 */
 331         {rfs4_op_lock, lock_denied_free, 0},
 332 
 333         /* OP_LOCKT = 13 */
 334         {rfs4_op_lockt, lock_denied_free, 0},
 335 
 336         /* OP_LOCKU = 14 */
 337         {rfs4_op_locku, nullfree, 0},
 338 
 339         /* OP_LOOKUP = 15 */
 340         {rfs4_op_lookup, nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK)},
 341 
 342         /* OP_LOOKUPP = 16 */
 343         {rfs4_op_lookupp, nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK)},
 344 
 345         /* OP_NVERIFY = 17 */
 346         {rfs4_op_nverify, nullfree, RPC_IDEMPOTENT},
 347 
 348         /* OP_OPEN = 18 */
 349         {rfs4_op_open, rfs4_free_reply, 0},
 350 
 351         /* OP_OPENATTR = 19 */
 352         {rfs4_op_openattr, nullfree, 0},
 353 
 354         /* OP_OPEN_CONFIRM = 20 */
 355         {rfs4_op_open_confirm, nullfree, 0},
 356 
 357         /* OP_OPEN_DOWNGRADE = 21 */
 358         {rfs4_op_open_downgrade, nullfree, 0},
 359 
 360         /* OP_OPEN_PUTFH = 22 */
 361         {rfs4_op_putfh, nullfree, RPC_ALL},
 362 
 363         /* OP_PUTPUBFH = 23 */
 364         {rfs4_op_putpubfh, nullfree, RPC_ALL},
 365 
 366         /* OP_PUTROOTFH = 24 */
 367         {rfs4_op_putrootfh, nullfree, RPC_ALL},
 368 
 369         /* OP_READ = 25 */
 370         {rfs4_op_read, rfs4_op_read_free, RPC_IDEMPOTENT},
 371 
 372         /* OP_READDIR = 26 */
 373         {rfs4_op_readdir, rfs4_op_readdir_free, RPC_IDEMPOTENT},
 374 
 375         /* OP_READLINK = 27 */
 376         {rfs4_op_readlink, rfs4_op_readlink_free, RPC_IDEMPOTENT},
 377 
 378         /* OP_REMOVE = 28 */
 379         {rfs4_op_remove, nullfree, 0},
 380 
 381         /* OP_RENAME = 29 */
 382         {rfs4_op_rename, nullfree, 0},
 383 
 384         /* OP_RENEW = 30 */
 385         {rfs4_op_renew, nullfree, 0},
 386 
 387         /* OP_RESTOREFH = 31 */
 388         {rfs4_op_restorefh, nullfree, RPC_ALL},
 389 
 390         /* OP_SAVEFH = 32 */
 391         {rfs4_op_savefh, nullfree, RPC_ALL},
 392 
 393         /* OP_SECINFO = 33 */
 394         {rfs4_op_secinfo, rfs4_op_secinfo_free, 0},
 395 
 396         /* OP_SETATTR = 34 */
 397         {rfs4_op_setattr, nullfree, 0},
 398 
 399         /* OP_SETCLIENTID = 35 */
 400         {rfs4_op_setclientid, nullfree, 0},
 401 
 402         /* OP_SETCLIENTID_CONFIRM = 36 */
 403         {rfs4_op_setclientid_confirm, nullfree, 0},
 404 
 405         /* OP_VERIFY = 37 */
 406         {rfs4_op_verify, nullfree, RPC_IDEMPOTENT},
 407 
 408         /* OP_WRITE = 38 */
 409         {rfs4_op_write, nullfree, 0},
 410 
 411         /* OP_RELEASE_LOCKOWNER = 39 */
 412         {rfs4_op_release_lockowner, nullfree, 0},
 413 };
 414 
 415 static uint_t rfsv4disp_cnt = sizeof (rfsv4disptab) / sizeof (rfsv4disptab[0]);
 416 
 417 #define OP_ILLEGAL_IDX (rfsv4disp_cnt)
 418 
 419 #ifdef DEBUG
 420 
 421 int             rfs4_fillone_debug = 0;
 422 int             rfs4_no_stub_access = 1;
 423 int             rfs4_rddir_debug = 0;
 424 
 425 static char    *rfs4_op_string[] = {
 426         "rfs4_op_null",
 427         "rfs4_op_1 unused",
 428         "rfs4_op_2 unused",
 429         "rfs4_op_access",
 430         "rfs4_op_close",
 431         "rfs4_op_commit",
 432         "rfs4_op_create",
 433         "rfs4_op_delegpurge",
 434         "rfs4_op_delegreturn",
 435         "rfs4_op_getattr",
 436         "rfs4_op_getfh",
 437         "rfs4_op_link",
 438         "rfs4_op_lock",
 439         "rfs4_op_lockt",
 440         "rfs4_op_locku",
 441         "rfs4_op_lookup",
 442         "rfs4_op_lookupp",
 443         "rfs4_op_nverify",
 444         "rfs4_op_open",
 445         "rfs4_op_openattr",
 446         "rfs4_op_open_confirm",
 447         "rfs4_op_open_downgrade",
 448         "rfs4_op_putfh",
 449         "rfs4_op_putpubfh",
 450         "rfs4_op_putrootfh",
 451         "rfs4_op_read",
 452         "rfs4_op_readdir",
 453         "rfs4_op_readlink",
 454         "rfs4_op_remove",
 455         "rfs4_op_rename",
 456         "rfs4_op_renew",
 457         "rfs4_op_restorefh",
 458         "rfs4_op_savefh",
 459         "rfs4_op_secinfo",
 460         "rfs4_op_setattr",
 461         "rfs4_op_setclientid",
 462         "rfs4_op_setclient_confirm",
 463         "rfs4_op_verify",
 464         "rfs4_op_write",
 465         "rfs4_op_release_lockowner",
 466         "rfs4_op_illegal"
 467 };
 468 #endif
 469 
 470 void    rfs4_ss_chkclid(nfs4_srv_t *, rfs4_client_t *);
 471 
 472 extern size_t   strlcpy(char *dst, const char *src, size_t dstsize);
 473 
 474 extern void     rfs4_free_fs_locations4(fs_locations4 *);
 475 
 476 #ifdef  nextdp
 477 #undef nextdp
 478 #endif
 479 #define nextdp(dp)      ((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
 480 
 481 static const fs_operation_def_t nfs4_rd_deleg_tmpl[] = {
 482         VOPNAME_OPEN,           { .femop_open = deleg_rd_open },
 483         VOPNAME_WRITE,          { .femop_write = deleg_rd_write },
 484         VOPNAME_SETATTR,        { .femop_setattr = deleg_rd_setattr },
 485         VOPNAME_RWLOCK,         { .femop_rwlock = deleg_rd_rwlock },
 486         VOPNAME_SPACE,          { .femop_space = deleg_rd_space },
 487         VOPNAME_SETSECATTR,     { .femop_setsecattr = deleg_rd_setsecattr },
 488         VOPNAME_VNEVENT,        { .femop_vnevent = deleg_rd_vnevent },
 489         NULL,                   NULL
 490 };
 491 static const fs_operation_def_t nfs4_wr_deleg_tmpl[] = {
 492         VOPNAME_OPEN,           { .femop_open = deleg_wr_open },
 493         VOPNAME_READ,           { .femop_read = deleg_wr_read },
 494         VOPNAME_WRITE,          { .femop_write = deleg_wr_write },
 495         VOPNAME_SETATTR,        { .femop_setattr = deleg_wr_setattr },
 496         VOPNAME_RWLOCK,         { .femop_rwlock = deleg_wr_rwlock },
 497         VOPNAME_SPACE,          { .femop_space = deleg_wr_space },
 498         VOPNAME_SETSECATTR,     { .femop_setsecattr = deleg_wr_setsecattr },
 499         VOPNAME_VNEVENT,        { .femop_vnevent = deleg_wr_vnevent },
 500         NULL,                   NULL
 501 };
 502 
 503 nfs4_srv_t *
 504 nfs4_get_srv(void)
 505 {
 506         nfs_globals_t *ng = zone_getspecific(nfssrv_zone_key, curzone);
 507         nfs4_srv_t *srv = ng->nfs4_srv;
 508         ASSERT(srv != NULL);
 509         return (srv);
 510 }
 511 
 512 void
 513 rfs4_srv_zone_init(nfs_globals_t *ng)
 514 {
 515         nfs4_srv_t *nsrv4;
 516         timespec32_t verf;
 517 
 518         nsrv4 = kmem_zalloc(sizeof (*nsrv4), KM_SLEEP);
 519 
 520         /*
 521          * The following algorithm attempts to find a unique verifier
 522          * to be used as the write verifier returned from the server
 523          * to the client.  It is important that this verifier change
 524          * whenever the server reboots.  Of secondary importance, it
 525          * is important for the verifier to be unique between two
 526          * different servers.
 527          *
 528          * Thus, an attempt is made to use the system hostid and the
 529          * current time in seconds when the nfssrv kernel module is
 530          * loaded.  It is assumed that an NFS server will not be able
 531          * to boot and then to reboot in less than a second.  If the
 532          * hostid has not been set, then the current high resolution
 533          * time is used.  This will ensure different verifiers each
 534          * time the server reboots and minimize the chances that two
 535          * different servers will have the same verifier.
 536          * XXX - this is broken on LP64 kernels.
 537          */
 538         verf.tv_sec = (time_t)zone_get_hostid(NULL);
 539         if (verf.tv_sec != 0) {
 540                 verf.tv_nsec = gethrestime_sec();
 541         } else {
 542                 timespec_t tverf;
 543 
 544                 gethrestime(&tverf);
 545                 verf.tv_sec = (time_t)tverf.tv_sec;
 546                 verf.tv_nsec = tverf.tv_nsec;
 547         }
 548         nsrv4->write4verf = *(uint64_t *)&verf;
 549 
 550         /* Used to manage create/destroy of server state */
 551         nsrv4->nfs4_server_state = NULL;
 552         nsrv4->nfs4_cur_servinst = NULL;
 553         nsrv4->nfs4_deleg_policy = SRV_NEVER_DELEGATE;
 554         mutex_init(&nsrv4->deleg_lock, NULL, MUTEX_DEFAULT, NULL);
 555         mutex_init(&nsrv4->state_lock, NULL, MUTEX_DEFAULT, NULL);
 556         mutex_init(&nsrv4->servinst_lock, NULL, MUTEX_DEFAULT, NULL);
 557         rw_init(&nsrv4->deleg_policy_lock, NULL, RW_DEFAULT, NULL);
 558 
 559         ng->nfs4_srv = nsrv4;
 560 }
 561 
 562 void
 563 rfs4_srv_zone_fini(nfs_globals_t *ng)
 564 {
 565         nfs4_srv_t *nsrv4 = ng->nfs4_srv;
 566 
 567         ng->nfs4_srv = NULL;
 568 
 569         mutex_destroy(&nsrv4->deleg_lock);
 570         mutex_destroy(&nsrv4->state_lock);
 571         mutex_destroy(&nsrv4->servinst_lock);
 572         rw_destroy(&nsrv4->deleg_policy_lock);
 573 
 574         kmem_free(nsrv4, sizeof (*nsrv4));
 575 }
 576 
 577 void
 578 rfs4_srvrinit(void)
 579 {
 580         extern void rfs4_attr_init();
 581 
 582         rfs4_attr_init();
 583 
 584         if (fem_create("deleg_rdops", nfs4_rd_deleg_tmpl, &deleg_rdops) != 0) {
 585                 rfs4_disable_delegation();
 586         } else if (fem_create("deleg_wrops", nfs4_wr_deleg_tmpl,
 587             &deleg_wrops) != 0) {
 588                 rfs4_disable_delegation();
 589                 fem_free(deleg_rdops);
 590         }
 591 
 592         nfs4_srv_caller_id = fs_new_caller_id();
 593         lockt_sysid = lm_alloc_sysidt();
 594         vsd_create(&nfs4_srv_vkey, NULL);
 595         rfs4_state_g_init();
 596 }
 597 
 598 void
 599 rfs4_srvrfini(void)
 600 {
 601         if (lockt_sysid != LM_NOSYSID) {
 602                 lm_free_sysidt(lockt_sysid);
 603                 lockt_sysid = LM_NOSYSID;
 604         }
 605 
 606         rfs4_state_g_fini();
 607 
 608         fem_free(deleg_rdops);
 609         fem_free(deleg_wrops);
 610 }
 611 
 612 void
 613 rfs4_do_server_start(int server_upordown,
 614     int srv_delegation, int cluster_booted)
 615 {
 616         nfs4_srv_t *nsrv4 = nfs4_get_srv();
 617 
 618         /* Is this a warm start? */
 619         if (server_upordown == NFS_SERVER_QUIESCED) {
 620                 cmn_err(CE_NOTE, "nfs4_srv: "
 621                     "server was previously quiesced; "
 622                     "existing NFSv4 state will be re-used");
 623 
 624                 /*
 625                  * HA-NFSv4: this is also the signal
 626                  * that a Resource Group failover has
 627                  * occurred.
 628                  */
 629                 if (cluster_booted)
 630                         hanfsv4_failover(nsrv4);
 631         } else {
 632                 /* Cold start */
 633                 nsrv4->rfs4_start_time = 0;
 634                 rfs4_state_zone_init(nsrv4);
 635                 nsrv4->nfs4_drc = rfs4_init_drc(nfs4_drc_max,
 636                     nfs4_drc_hash);
 637 
 638                 /*
 639                  * The nfsd service was started with the -s option
 640                  * we need to pull in any state from the paths indicated.
 641                  */
 642                 if (curzone == global_zone && rfs4_dss_numnewpaths > 0) {
 643                         /* read in the stable storage state from these paths */
 644                         rfs4_dss_readstate(nsrv4, rfs4_dss_numnewpaths,
 645                             rfs4_dss_newpaths);
 646                 }
 647         }
 648 
 649         /* Check if delegation is to be enabled */
 650         if (srv_delegation != FALSE)
 651                 rfs4_set_deleg_policy(nsrv4, SRV_NORMAL_DELEGATE);
 652 }
 653 
 654 void
 655 rfs4_init_compound_state(struct compound_state *cs)
 656 {
 657         bzero(cs, sizeof (*cs));
 658         cs->cont = TRUE;
 659         cs->access = CS_ACCESS_DENIED;
 660         cs->deleg = FALSE;
 661         cs->mandlock = FALSE;
 662         cs->fh.nfs_fh4_val = cs->fhbuf;
 663 }
 664 
 665 void
 666 rfs4_grace_start(rfs4_servinst_t *sip)
 667 {
 668         rw_enter(&sip->rwlock, RW_WRITER);
 669         sip->start_time = (time_t)TICK_TO_SEC(ddi_get_lbolt());
 670         sip->grace_period = rfs4_grace_period;
 671         rw_exit(&sip->rwlock);
 672 }
 673 
 674 /*
 675  * returns true if the instance's grace period has never been started
 676  */
 677 int
 678 rfs4_servinst_grace_new(rfs4_servinst_t *sip)
 679 {
 680         time_t start_time;
 681 
 682         rw_enter(&sip->rwlock, RW_READER);
 683         start_time = sip->start_time;
 684         rw_exit(&sip->rwlock);
 685 
 686         return (start_time == 0);
 687 }
 688 
 689 /*
 690  * Indicates if server instance is within the
 691  * grace period.
 692  */
 693 int
 694 rfs4_servinst_in_grace(rfs4_servinst_t *sip)
 695 {
 696         time_t grace_expiry;
 697 
 698         rw_enter(&sip->rwlock, RW_READER);
 699         grace_expiry = sip->start_time + sip->grace_period;
 700         rw_exit(&sip->rwlock);
 701 
 702         return (((time_t)TICK_TO_SEC(ddi_get_lbolt())) < grace_expiry);
 703 }
 704 
 705 int
 706 rfs4_clnt_in_grace(rfs4_client_t *cp)
 707 {
 708         ASSERT(rfs4_dbe_refcnt(cp->rc_dbe) > 0);
 709 
 710         return (rfs4_servinst_in_grace(cp->rc_server_instance));
 711 }
 712 
 713 /*
 714  * reset all currently active grace periods
 715  */
 716 void
 717 rfs4_grace_reset_all(nfs4_srv_t *nsrv4)
 718 {
 719         rfs4_servinst_t *sip;
 720 
 721         mutex_enter(&nsrv4->servinst_lock);
 722         for (sip = nsrv4->nfs4_cur_servinst; sip != NULL; sip = sip->prev)
 723                 if (rfs4_servinst_in_grace(sip))
 724                         rfs4_grace_start(sip);
 725         mutex_exit(&nsrv4->servinst_lock);
 726 }
 727 
 728 /*
 729  * start any new instances' grace periods
 730  */
 731 void
 732 rfs4_grace_start_new(nfs4_srv_t *nsrv4)
 733 {
 734         rfs4_servinst_t *sip;
 735 
 736         mutex_enter(&nsrv4->servinst_lock);
 737         for (sip = nsrv4->nfs4_cur_servinst; sip != NULL; sip = sip->prev)
 738                 if (rfs4_servinst_grace_new(sip))
 739                         rfs4_grace_start(sip);
 740         mutex_exit(&nsrv4->servinst_lock);
 741 }
 742 
 743 static rfs4_dss_path_t *
 744 rfs4_dss_newpath(nfs4_srv_t *nsrv4, rfs4_servinst_t *sip,
 745     char *path, unsigned index)
 746 {
 747         size_t len;
 748         rfs4_dss_path_t *dss_path;
 749 
 750         dss_path = kmem_alloc(sizeof (rfs4_dss_path_t), KM_SLEEP);
 751 
 752         /*
 753          * Take a copy of the string, since the original may be overwritten.
 754          * Sadly, no strdup() in the kernel.
 755          */
 756         /* allow for NUL */
 757         len = strlen(path) + 1;
 758         dss_path->path = kmem_alloc(len, KM_SLEEP);
 759         (void) strlcpy(dss_path->path, path, len);
 760 
 761         /* associate with servinst */
 762         dss_path->sip = sip;
 763         dss_path->index = index;
 764 
 765         /*
 766          * Add to list of served paths.
 767          * No locking required, as we're only ever called at startup.
 768          */
 769         if (nsrv4->dss_pathlist == NULL) {
 770                 /* this is the first dss_path_t */
 771 
 772                 /* needed for insque/remque */
 773                 dss_path->next = dss_path->prev = dss_path;
 774 
 775                 nsrv4->dss_pathlist = dss_path;
 776         } else {
 777                 insque(dss_path, nsrv4->dss_pathlist);
 778         }
 779 
 780         return (dss_path);
 781 }
 782 
 783 /*
 784  * Create a new server instance, and make it the currently active instance.
 785  * Note that starting the grace period too early will reduce the clients'
 786  * recovery window.
 787  */
 788 void
 789 rfs4_servinst_create(nfs4_srv_t *nsrv4, int start_grace,
 790     int dss_npaths, char **dss_paths)
 791 {
 792         unsigned i;
 793         rfs4_servinst_t *sip;
 794         rfs4_oldstate_t *oldstate;
 795 
 796         sip = kmem_alloc(sizeof (rfs4_servinst_t), KM_SLEEP);
 797         rw_init(&sip->rwlock, NULL, RW_DEFAULT, NULL);
 798 
 799         sip->start_time = (time_t)0;
 800         sip->grace_period = (time_t)0;
 801         sip->next = NULL;
 802         sip->prev = NULL;
 803 
 804         rw_init(&sip->oldstate_lock, NULL, RW_DEFAULT, NULL);
 805         /*
 806          * This initial dummy entry is required to setup for insque/remque.
 807          * It must be skipped over whenever the list is traversed.
 808          */
 809         oldstate = kmem_alloc(sizeof (rfs4_oldstate_t), KM_SLEEP);
 810         /* insque/remque require initial list entry to be self-terminated */
 811         oldstate->next = oldstate;
 812         oldstate->prev = oldstate;
 813         sip->oldstate = oldstate;
 814 
 815 
 816         sip->dss_npaths = dss_npaths;
 817         sip->dss_paths = kmem_alloc(dss_npaths *
 818             sizeof (rfs4_dss_path_t *), KM_SLEEP);
 819 
 820         for (i = 0; i < dss_npaths; i++) {
 821                 sip->dss_paths[i] =
 822                     rfs4_dss_newpath(nsrv4, sip, dss_paths[i], i);
 823         }
 824 
 825         mutex_enter(&nsrv4->servinst_lock);
 826         if (nsrv4->nfs4_cur_servinst != NULL) {
 827                 /* add to linked list */
 828                 sip->prev = nsrv4->nfs4_cur_servinst;
 829                 nsrv4->nfs4_cur_servinst->next = sip;
 830         }
 831         if (start_grace)
 832                 rfs4_grace_start(sip);
 833         /* make the new instance "current" */
 834         nsrv4->nfs4_cur_servinst = sip;
 835 
 836         mutex_exit(&nsrv4->servinst_lock);
 837 }
 838 
 839 /*
 840  * In future, we might add a rfs4_servinst_destroy(sip) but, for now, destroy
 841  * all instances directly.
 842  */
 843 void
 844 rfs4_servinst_destroy_all(nfs4_srv_t *nsrv4)
 845 {
 846         rfs4_servinst_t *sip, *prev, *current;
 847 #ifdef DEBUG
 848         int n = 0;
 849 #endif
 850 
 851         mutex_enter(&nsrv4->servinst_lock);
 852         ASSERT(nsrv4->nfs4_cur_servinst != NULL);
 853         current = nsrv4->nfs4_cur_servinst;
 854         nsrv4->nfs4_cur_servinst = NULL;
 855         for (sip = current; sip != NULL; sip = prev) {
 856                 prev = sip->prev;
 857                 rw_destroy(&sip->rwlock);
 858                 if (sip->oldstate)
 859                         kmem_free(sip->oldstate, sizeof (rfs4_oldstate_t));
 860                 if (sip->dss_paths) {
 861                         int i = sip->dss_npaths;
 862 
 863                         while (i > 0) {
 864                                 i--;
 865                                 if (sip->dss_paths[i] != NULL) {
 866                                         char *path = sip->dss_paths[i]->path;
 867 
 868                                         if (path != NULL) {
 869                                                 kmem_free(path,
 870                                                     strlen(path) + 1);
 871                                         }
 872                                         kmem_free(sip->dss_paths[i],
 873                                             sizeof (rfs4_dss_path_t));
 874                                 }
 875                         }
 876                         kmem_free(sip->dss_paths,
 877                             sip->dss_npaths * sizeof (rfs4_dss_path_t *));
 878                 }
 879                 kmem_free(sip, sizeof (rfs4_servinst_t));
 880 #ifdef DEBUG
 881                 n++;
 882 #endif
 883         }
 884         mutex_exit(&nsrv4->servinst_lock);
 885 }
 886 
 887 /*
 888  * Assign the current server instance to a client_t.
 889  * Should be called with cp->rc_dbe held.
 890  */
 891 void
 892 rfs4_servinst_assign(nfs4_srv_t *nsrv4, rfs4_client_t *cp,
 893     rfs4_servinst_t *sip)
 894 {
 895         ASSERT(rfs4_dbe_refcnt(cp->rc_dbe) > 0);
 896 
 897         /*
 898          * The lock ensures that if the current instance is in the process
 899          * of changing, we will see the new one.
 900          */
 901         mutex_enter(&nsrv4->servinst_lock);
 902         cp->rc_server_instance = sip;
 903         mutex_exit(&nsrv4->servinst_lock);
 904 }
 905 
 906 rfs4_servinst_t *
 907 rfs4_servinst(rfs4_client_t *cp)
 908 {
 909         ASSERT(rfs4_dbe_refcnt(cp->rc_dbe) > 0);
 910 
 911         return (cp->rc_server_instance);
 912 }
 913 
 914 /* ARGSUSED */
 915 static void
 916 nullfree(caddr_t resop)
 917 {
 918 }
 919 
 920 /*
 921  * This is a fall-through for invalid or not implemented (yet) ops
 922  */
 923 /* ARGSUSED */
 924 static void
 925 rfs4_op_inval(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
 926     struct compound_state *cs)
 927 {
 928         *cs->statusp = *((nfsstat4 *)&(resop)->nfs_resop4_u) = NFS4ERR_INVAL;
 929 }
 930 
 931 /*
 932  * Check if the security flavor, nfsnum, is in the flavor_list.
 933  */
 934 bool_t
 935 in_flavor_list(int nfsnum, int *flavor_list, int count)
 936 {
 937         int i;
 938 
 939         for (i = 0; i < count; i++) {
 940                 if (nfsnum == flavor_list[i])
 941                         return (TRUE);
 942         }
 943         return (FALSE);
 944 }
 945 
 946 /*
 947  * Used by rfs4_op_secinfo to get the security information from the
 948  * export structure associated with the component.
 949  */
 950 /* ARGSUSED */
 951 static nfsstat4
 952 do_rfs4_op_secinfo(struct compound_state *cs, char *nm, SECINFO4res *resp)
 953 {
 954         int error, different_export = 0;
 955         vnode_t *dvp, *vp;
 956         struct exportinfo *exi;
 957         fid_t fid;
 958         uint_t count, i;
 959         secinfo4 *resok_val;
 960         struct secinfo *secp;
 961         seconfig_t *si;
 962         bool_t did_traverse = FALSE;
 963         int dotdot, walk;
 964         nfs_export_t *ne = nfs_get_export();
 965 
 966         dvp = cs->vp;
 967         exi = cs->exi;
 968         ASSERT(exi != NULL);
 969         dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
 970 
 971         /*
 972          * If dotdotting, then need to check whether it's above the
 973          * root of a filesystem, or above an export point.
 974          */
 975         if (dotdot) {
 976                 vnode_t *zone_rootvp = ne->exi_root->exi_vp;
 977 
 978                 ASSERT3U(exi->exi_zoneid, ==, ne->exi_root->exi_zoneid);
 979                 /*
 980                  * If dotdotting at the root of a filesystem, then
 981                  * need to traverse back to the mounted-on filesystem
 982                  * and do the dotdot lookup there.
 983                  */
 984                 if ((dvp->v_flag & VROOT) || VN_CMP(dvp, zone_rootvp)) {
 985 
 986                         /*
 987                          * If at the system root, then can
 988                          * go up no further.
 989                          */
 990                         if (VN_CMP(dvp, zone_rootvp))
 991                                 return (puterrno4(ENOENT));
 992 
 993                         /*
 994                          * Traverse back to the mounted-on filesystem
 995                          */
 996                         dvp = untraverse(dvp, zone_rootvp);
 997 
 998                         /*
 999                          * Set the different_export flag so we remember
1000                          * to pick up a new exportinfo entry for
1001                          * this new filesystem.
1002                          */
1003                         different_export = 1;
1004                 } else {
1005 
1006                         /*
1007                          * If dotdotting above an export point then set
1008                          * the different_export to get new export info.
1009                          */
1010                         different_export = nfs_exported(exi, dvp);
1011                 }
1012         }
1013 
1014         /*
1015          * Get the vnode for the component "nm".
1016          */
1017         error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cs->cr,
1018             NULL, NULL, NULL);
1019         if (error)
1020                 return (puterrno4(error));
1021 
1022         /*
1023          * If the vnode is in a pseudo filesystem, or if the security flavor
1024          * used in the request is valid but not an explicitly shared flavor,
1025          * or the access bit indicates that this is a limited access,
1026          * check whether this vnode is visible.
1027          */
1028         if (!different_export &&
1029             (PSEUDO(exi) || !is_exported_sec(cs->nfsflavor, exi) ||
1030             cs->access & CS_ACCESS_LIMITED)) {
1031                 if (! nfs_visible(exi, vp, &different_export)) {
1032                         VN_RELE(vp);
1033                         return (puterrno4(ENOENT));
1034                 }
1035         }
1036 
1037         /*
1038          * If it's a mountpoint, then traverse it.
1039          */
1040         if (vn_ismntpt(vp)) {
1041                 if ((error = traverse(&vp)) != 0) {
1042                         VN_RELE(vp);
1043                         return (puterrno4(error));
1044                 }
1045                 /* remember that we had to traverse mountpoint */
1046                 did_traverse = TRUE;
1047                 different_export = 1;
1048         } else if (vp->v_vfsp != dvp->v_vfsp) {
1049                 /*
1050                  * If vp isn't a mountpoint and the vfs ptrs aren't the same,
1051                  * then vp is probably an LOFS object.  We don't need the
1052                  * realvp, we just need to know that we might have crossed
1053                  * a server fs boundary and need to call checkexport4.
1054                  * (LOFS lookup hides server fs mountpoints, and actually calls
1055                  * traverse)
1056                  */
1057                 different_export = 1;
1058         }
1059 
1060         /*
1061          * Get the export information for it.
1062          */
1063         if (different_export) {
1064 
1065                 bzero(&fid, sizeof (fid));
1066                 fid.fid_len = MAXFIDSZ;
1067                 error = vop_fid_pseudo(vp, &fid);
1068                 if (error) {
1069                         VN_RELE(vp);
1070                         return (puterrno4(error));
1071                 }
1072 
1073                 /* We'll need to reassign "exi". */
1074                 if (dotdot)
1075                         exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
1076                 else
1077                         exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
1078 
1079                 if (exi == NULL) {
1080                         if (did_traverse == TRUE) {
1081                                 /*
1082                                  * If this vnode is a mounted-on vnode,
1083                                  * but the mounted-on file system is not
1084                                  * exported, send back the secinfo for
1085                                  * the exported node that the mounted-on
1086                                  * vnode lives in.
1087                                  */
1088                                 exi = cs->exi;
1089                         } else {
1090                                 VN_RELE(vp);
1091                                 return (puterrno4(EACCES));
1092                         }
1093                 }
1094         }
1095         ASSERT(exi != NULL);
1096 
1097 
1098         /*
1099          * Create the secinfo result based on the security information
1100          * from the exportinfo structure (exi).
1101          *
1102          * Return all flavors for a pseudo node.
1103          * For a real export node, return the flavor that the client
1104          * has access with.
1105          */
1106         ASSERT(RW_LOCK_HELD(&ne->exported_lock));
1107         if (PSEUDO(exi)) {
1108                 count = exi->exi_export.ex_seccnt; /* total sec count */
1109                 resok_val = kmem_alloc(count * sizeof (secinfo4), KM_SLEEP);
1110                 secp = exi->exi_export.ex_secinfo;
1111 
1112                 for (i = 0; i < count; i++) {
1113                         si = &secp[i].s_secinfo;
1114                         resok_val[i].flavor = si->sc_rpcnum;
1115                         if (resok_val[i].flavor == RPCSEC_GSS) {
1116                                 rpcsec_gss_info *info;
1117 
1118                                 info = &resok_val[i].flavor_info;
1119                                 info->qop = si->sc_qop;
1120                                 info->service = (rpc_gss_svc_t)si->sc_service;
1121 
1122                                 /* get oid opaque data */
1123                                 info->oid.sec_oid4_len =
1124                                     si->sc_gss_mech_type->length;
1125                                 info->oid.sec_oid4_val = kmem_alloc(
1126                                     si->sc_gss_mech_type->length, KM_SLEEP);
1127                                 bcopy(
1128                                     si->sc_gss_mech_type->elements,
1129                                     info->oid.sec_oid4_val,
1130                                     info->oid.sec_oid4_len);
1131                         }
1132                 }
1133                 resp->SECINFO4resok_len = count;
1134                 resp->SECINFO4resok_val = resok_val;
1135         } else {
1136                 int ret_cnt = 0, k = 0;
1137                 int *flavor_list;
1138 
1139                 count = exi->exi_export.ex_seccnt; /* total sec count */
1140                 secp = exi->exi_export.ex_secinfo;
1141 
1142                 flavor_list = kmem_alloc(count * sizeof (int), KM_SLEEP);
1143                 /* find out which flavors to return */
1144                 for (i = 0; i < count; i ++) {
1145                         int access, flavor, perm;
1146 
1147                         flavor = secp[i].s_secinfo.sc_nfsnum;
1148                         perm = secp[i].s_flags;
1149 
1150                         access = nfsauth4_secinfo_access(exi, cs->req,
1151                             flavor, perm, cs->basecr);
1152 
1153                         if (! (access & NFSAUTH_DENIED) &&
1154                             ! (access & NFSAUTH_WRONGSEC)) {
1155                                 flavor_list[ret_cnt] = flavor;
1156                                 ret_cnt++;
1157                         }
1158                 }
1159 
1160                 /* Create the returning SECINFO value */
1161                 resok_val = kmem_alloc(ret_cnt * sizeof (secinfo4), KM_SLEEP);
1162 
1163                 for (i = 0; i < count; i++) {
1164                         /*
1165                          * If the flavor is in the flavor list,
1166                          * fill in resok_val.
1167                          */
1168                         si = &secp[i].s_secinfo;
1169                         if (in_flavor_list(si->sc_nfsnum,
1170                             flavor_list, ret_cnt)) {
1171                                 resok_val[k].flavor = si->sc_rpcnum;
1172                                 if (resok_val[k].flavor == RPCSEC_GSS) {
1173                                         rpcsec_gss_info *info;
1174 
1175                                         info = &resok_val[k].flavor_info;
1176                                         info->qop = si->sc_qop;
1177                                         info->service = (rpc_gss_svc_t)
1178                                             si->sc_service;
1179 
1180                                         /* get oid opaque data */
1181                                         info->oid.sec_oid4_len =
1182                                             si->sc_gss_mech_type->length;
1183                                         info->oid.sec_oid4_val = kmem_alloc(
1184                                             si->sc_gss_mech_type->length,
1185                                             KM_SLEEP);
1186                                         bcopy(si->sc_gss_mech_type->elements,
1187                                             info->oid.sec_oid4_val,
1188                                             info->oid.sec_oid4_len);
1189                                 }
1190                                 k++;
1191                         }
1192                         if (k >= ret_cnt)
1193                                 break;
1194                 }
1195                 resp->SECINFO4resok_len = ret_cnt;
1196                 resp->SECINFO4resok_val = resok_val;
1197                 kmem_free(flavor_list, count * sizeof (int));
1198         }
1199 
1200         VN_RELE(vp);
1201         return (NFS4_OK);
1202 }
1203 
1204 /*
1205  * SECINFO (Operation 33): Obtain required security information on
1206  * the component name in the format of (security-mechanism-oid, qop, service)
1207  * triplets.
1208  */
1209 /* ARGSUSED */
1210 static void
1211 rfs4_op_secinfo(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1212     struct compound_state *cs)
1213 {
1214         SECINFO4args *args = &argop->nfs_argop4_u.opsecinfo;
1215         SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
1216         utf8string *utfnm = &args->name;
1217         uint_t len;
1218         char *nm;
1219         struct sockaddr *ca;
1220         char *name = NULL;
1221         nfsstat4 status = NFS4_OK;
1222 
1223         DTRACE_NFSV4_2(op__secinfo__start, struct compound_state *, cs,
1224             SECINFO4args *, args);
1225 
1226         /*
1227          * Current file handle (cfh) should have been set before getting
1228          * into this function. If not, return error.
1229          */
1230         if (cs->vp == NULL) {
1231                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1232                 goto out;
1233         }
1234 
1235         if (cs->vp->v_type != VDIR) {
1236                 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
1237                 goto out;
1238         }
1239 
1240         /*
1241          * Verify the component name. If failed, error out, but
1242          * do not error out if the component name is a "..".
1243          * SECINFO will return its parents secinfo data for SECINFO "..".
1244          */
1245         status = utf8_dir_verify(utfnm);
1246         if (status != NFS4_OK) {
1247                 if (utfnm->utf8string_len != 2 ||
1248                     utfnm->utf8string_val[0] != '.' ||
1249                     utfnm->utf8string_val[1] != '.') {
1250                         *cs->statusp = resp->status = status;
1251                         goto out;
1252                 }
1253         }
1254 
1255         nm = utf8_to_str(utfnm, &len, NULL);
1256         if (nm == NULL) {
1257                 *cs->statusp = resp->status = NFS4ERR_INVAL;
1258                 goto out;
1259         }
1260 
1261         if (len > MAXNAMELEN) {
1262                 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
1263                 kmem_free(nm, len);
1264                 goto out;
1265         }
1266 
1267         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
1268         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
1269             MAXPATHLEN  + 1);
1270 
1271         if (name == NULL) {
1272                 *cs->statusp = resp->status = NFS4ERR_INVAL;
1273                 kmem_free(nm, len);
1274                 goto out;
1275         }
1276 
1277 
1278         *cs->statusp = resp->status = do_rfs4_op_secinfo(cs, name, resp);
1279 
1280         if (name != nm)
1281                 kmem_free(name, MAXPATHLEN + 1);
1282         kmem_free(nm, len);
1283 
1284 out:
1285         DTRACE_NFSV4_2(op__secinfo__done, struct compound_state *, cs,
1286             SECINFO4res *, resp);
1287 }
1288 
1289 /*
1290  * Free SECINFO result.
1291  */
1292 /* ARGSUSED */
1293 static void
1294 rfs4_op_secinfo_free(nfs_resop4 *resop)
1295 {
1296         SECINFO4res *resp = &resop->nfs_resop4_u.opsecinfo;
1297         int count, i;
1298         secinfo4 *resok_val;
1299 
1300         /* If this is not an Ok result, nothing to free. */
1301         if (resp->status != NFS4_OK) {
1302                 return;
1303         }
1304 
1305         count = resp->SECINFO4resok_len;
1306         resok_val = resp->SECINFO4resok_val;
1307 
1308         for (i = 0; i < count; i++) {
1309                 if (resok_val[i].flavor == RPCSEC_GSS) {
1310                         rpcsec_gss_info *info;
1311 
1312                         info = &resok_val[i].flavor_info;
1313                         kmem_free(info->oid.sec_oid4_val,
1314                             info->oid.sec_oid4_len);
1315                 }
1316         }
1317         kmem_free(resok_val, count * sizeof (secinfo4));
1318         resp->SECINFO4resok_len = 0;
1319         resp->SECINFO4resok_val = NULL;
1320 }
1321 
1322 /* ARGSUSED */
1323 static void
1324 rfs4_op_access(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1325     struct compound_state *cs)
1326 {
1327         ACCESS4args *args = &argop->nfs_argop4_u.opaccess;
1328         ACCESS4res *resp = &resop->nfs_resop4_u.opaccess;
1329         int error;
1330         vnode_t *vp;
1331         struct vattr va;
1332         int checkwriteperm;
1333         cred_t *cr = cs->cr;
1334         bslabel_t *clabel, *slabel;
1335         ts_label_t *tslabel;
1336         boolean_t admin_low_client;
1337 
1338         DTRACE_NFSV4_2(op__access__start, struct compound_state *, cs,
1339             ACCESS4args *, args);
1340 
1341 #if 0   /* XXX allow access even if !cs->access. Eventually only pseudo fs */
1342         if (cs->access == CS_ACCESS_DENIED) {
1343                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
1344                 goto out;
1345         }
1346 #endif
1347         if (cs->vp == NULL) {
1348                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1349                 goto out;
1350         }
1351 
1352         ASSERT(cr != NULL);
1353 
1354         vp = cs->vp;
1355 
1356         /*
1357          * If the file system is exported read only, it is not appropriate
1358          * to check write permissions for regular files and directories.
1359          * Special files are interpreted by the client, so the underlying
1360          * permissions are sent back to the client for interpretation.
1361          */
1362         if (rdonly4(req, cs) &&
1363             (vp->v_type == VREG || vp->v_type == VDIR))
1364                 checkwriteperm = 0;
1365         else
1366                 checkwriteperm = 1;
1367 
1368         /*
1369          * XXX
1370          * We need the mode so that we can correctly determine access
1371          * permissions relative to a mandatory lock file.  Access to
1372          * mandatory lock files is denied on the server, so it might
1373          * as well be reflected to the server during the open.
1374          */
1375         va.va_mask = AT_MODE;
1376         error = VOP_GETATTR(vp, &va, 0, cr, NULL);
1377         if (error) {
1378                 *cs->statusp = resp->status = puterrno4(error);
1379                 goto out;
1380         }
1381         resp->access = 0;
1382         resp->supported = 0;
1383 
1384         if (is_system_labeled()) {
1385                 ASSERT(req->rq_label != NULL);
1386                 clabel = req->rq_label;
1387                 DTRACE_PROBE2(tx__rfs4__log__info__opaccess__clabel, char *,
1388                     "got client label from request(1)",
1389                     struct svc_req *, req);
1390                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
1391                         if ((tslabel = nfs_getflabel(vp, cs->exi)) == NULL) {
1392                                 *cs->statusp = resp->status = puterrno4(EACCES);
1393                                 goto out;
1394                         }
1395                         slabel = label2bslabel(tslabel);
1396                         DTRACE_PROBE3(tx__rfs4__log__info__opaccess__slabel,
1397                             char *, "got server label(1) for vp(2)",
1398                             bslabel_t *, slabel, vnode_t *, vp);
1399 
1400                         admin_low_client = B_FALSE;
1401                 } else
1402                         admin_low_client = B_TRUE;
1403         }
1404 
1405         if (args->access & ACCESS4_READ) {
1406                 error = VOP_ACCESS(vp, VREAD, 0, cr, NULL);
1407                 if (!error && !MANDLOCK(vp, va.va_mode) &&
1408                     (!is_system_labeled() || admin_low_client ||
1409                     bldominates(clabel, slabel)))
1410                         resp->access |= ACCESS4_READ;
1411                 resp->supported |= ACCESS4_READ;
1412         }
1413         if ((args->access & ACCESS4_LOOKUP) && vp->v_type == VDIR) {
1414                 error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
1415                 if (!error && (!is_system_labeled() || admin_low_client ||
1416                     bldominates(clabel, slabel)))
1417                         resp->access |= ACCESS4_LOOKUP;
1418                 resp->supported |= ACCESS4_LOOKUP;
1419         }
1420         if (checkwriteperm &&
1421             (args->access & (ACCESS4_MODIFY|ACCESS4_EXTEND))) {
1422                 error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
1423                 if (!error && !MANDLOCK(vp, va.va_mode) &&
1424                     (!is_system_labeled() || admin_low_client ||
1425                     blequal(clabel, slabel)))
1426                         resp->access |=
1427                             (args->access & (ACCESS4_MODIFY | ACCESS4_EXTEND));
1428                 resp->supported |=
1429                     resp->access & (ACCESS4_MODIFY | ACCESS4_EXTEND);
1430         }
1431 
1432         if (checkwriteperm &&
1433             (args->access & ACCESS4_DELETE) && vp->v_type == VDIR) {
1434                 error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
1435                 if (!error && (!is_system_labeled() || admin_low_client ||
1436                     blequal(clabel, slabel)))
1437                         resp->access |= ACCESS4_DELETE;
1438                 resp->supported |= ACCESS4_DELETE;
1439         }
1440         if (args->access & ACCESS4_EXECUTE && vp->v_type != VDIR) {
1441                 error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
1442                 if (!error && !MANDLOCK(vp, va.va_mode) &&
1443                     (!is_system_labeled() || admin_low_client ||
1444                     bldominates(clabel, slabel)))
1445                         resp->access |= ACCESS4_EXECUTE;
1446                 resp->supported |= ACCESS4_EXECUTE;
1447         }
1448 
1449         if (is_system_labeled() && !admin_low_client)
1450                 label_rele(tslabel);
1451 
1452         *cs->statusp = resp->status = NFS4_OK;
1453 out:
1454         DTRACE_NFSV4_2(op__access__done, struct compound_state *, cs,
1455             ACCESS4res *, resp);
1456 }
1457 
1458 /* ARGSUSED */
1459 static void
1460 rfs4_op_commit(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1461     struct compound_state *cs)
1462 {
1463         COMMIT4args *args = &argop->nfs_argop4_u.opcommit;
1464         COMMIT4res *resp = &resop->nfs_resop4_u.opcommit;
1465         int error;
1466         vnode_t *vp = cs->vp;
1467         cred_t *cr = cs->cr;
1468         vattr_t va;
1469         nfs4_srv_t *nsrv4;
1470 
1471         DTRACE_NFSV4_2(op__commit__start, struct compound_state *, cs,
1472             COMMIT4args *, args);
1473 
1474         if (vp == NULL) {
1475                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1476                 goto out;
1477         }
1478         if (cs->access == CS_ACCESS_DENIED) {
1479                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
1480                 goto out;
1481         }
1482 
1483         if (args->offset + args->count < args->offset) {
1484                 *cs->statusp = resp->status = NFS4ERR_INVAL;
1485                 goto out;
1486         }
1487 
1488         va.va_mask = AT_UID;
1489         error = VOP_GETATTR(vp, &va, 0, cr, NULL);
1490 
1491         /*
1492          * If we can't get the attributes, then we can't do the
1493          * right access checking.  So, we'll fail the request.
1494          */
1495         if (error) {
1496                 *cs->statusp = resp->status = puterrno4(error);
1497                 goto out;
1498         }
1499         if (rdonly4(req, cs)) {
1500                 *cs->statusp = resp->status = NFS4ERR_ROFS;
1501                 goto out;
1502         }
1503 
1504         if (vp->v_type != VREG) {
1505                 if (vp->v_type == VDIR)
1506                         resp->status = NFS4ERR_ISDIR;
1507                 else
1508                         resp->status = NFS4ERR_INVAL;
1509                 *cs->statusp = resp->status;
1510                 goto out;
1511         }
1512 
1513         if (crgetuid(cr) != va.va_uid &&
1514             (error = VOP_ACCESS(vp, VWRITE, 0, cs->cr, NULL))) {
1515                 *cs->statusp = resp->status = puterrno4(error);
1516                 goto out;
1517         }
1518 
1519         error = VOP_FSYNC(vp, FSYNC, cr, NULL);
1520 
1521         if (error) {
1522                 *cs->statusp = resp->status = puterrno4(error);
1523                 goto out;
1524         }
1525 
1526         nsrv4 = nfs4_get_srv();
1527         *cs->statusp = resp->status = NFS4_OK;
1528         resp->writeverf = nsrv4->write4verf;
1529 out:
1530         DTRACE_NFSV4_2(op__commit__done, struct compound_state *, cs,
1531             COMMIT4res *, resp);
1532 }
1533 
1534 /*
1535  * rfs4_op_mknod is called from rfs4_op_create after all initial verification
1536  * was completed. It does the nfsv4 create for special files.
1537  */
1538 /* ARGSUSED */
1539 static vnode_t *
1540 do_rfs4_op_mknod(CREATE4args *args, CREATE4res *resp, struct svc_req *req,
1541     struct compound_state *cs, vattr_t *vap, char *nm)
1542 {
1543         int error;
1544         cred_t *cr = cs->cr;
1545         vnode_t *dvp = cs->vp;
1546         vnode_t *vp = NULL;
1547         int mode;
1548         enum vcexcl excl;
1549 
1550         switch (args->type) {
1551         case NF4CHR:
1552         case NF4BLK:
1553                 if (secpolicy_sys_devices(cr) != 0) {
1554                         *cs->statusp = resp->status = NFS4ERR_PERM;
1555                         return (NULL);
1556                 }
1557                 if (args->type == NF4CHR)
1558                         vap->va_type = VCHR;
1559                 else
1560                         vap->va_type = VBLK;
1561                 vap->va_rdev = makedevice(args->ftype4_u.devdata.specdata1,
1562                     args->ftype4_u.devdata.specdata2);
1563                 vap->va_mask |= AT_RDEV;
1564                 break;
1565         case NF4SOCK:
1566                 vap->va_type = VSOCK;
1567                 break;
1568         case NF4FIFO:
1569                 vap->va_type = VFIFO;
1570                 break;
1571         default:
1572                 *cs->statusp = resp->status = NFS4ERR_BADTYPE;
1573                 return (NULL);
1574         }
1575 
1576         /*
1577          * Must specify the mode.
1578          */
1579         if (!(vap->va_mask & AT_MODE)) {
1580                 *cs->statusp = resp->status = NFS4ERR_INVAL;
1581                 return (NULL);
1582         }
1583 
1584         excl = EXCL;
1585 
1586         mode = 0;
1587 
1588         error = VOP_CREATE(dvp, nm, vap, excl, mode, &vp, cr, 0, NULL, NULL);
1589         if (error) {
1590                 *cs->statusp = resp->status = puterrno4(error);
1591                 return (NULL);
1592         }
1593         return (vp);
1594 }
1595 
1596 /*
1597  * nfsv4 create is used to create non-regular files. For regular files,
1598  * use nfsv4 open.
1599  */
1600 /* ARGSUSED */
1601 static void
1602 rfs4_op_create(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1603     struct compound_state *cs)
1604 {
1605         CREATE4args *args = &argop->nfs_argop4_u.opcreate;
1606         CREATE4res *resp = &resop->nfs_resop4_u.opcreate;
1607         int error;
1608         struct vattr bva, iva, iva2, ava, *vap;
1609         cred_t *cr = cs->cr;
1610         vnode_t *dvp = cs->vp;
1611         vnode_t *vp = NULL;
1612         vnode_t *realvp;
1613         char *nm, *lnm;
1614         uint_t len, llen;
1615         int syncval = 0;
1616         struct nfs4_svgetit_arg sarg;
1617         struct nfs4_ntov_table ntov;
1618         struct statvfs64 sb;
1619         nfsstat4 status;
1620         struct sockaddr *ca;
1621         char *name = NULL;
1622         char *lname = NULL;
1623 
1624         DTRACE_NFSV4_2(op__create__start, struct compound_state *, cs,
1625             CREATE4args *, args);
1626 
1627         resp->attrset = 0;
1628 
1629         if (dvp == NULL) {
1630                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
1631                 goto out;
1632         }
1633 
1634         /*
1635          * If there is an unshared filesystem mounted on this vnode,
1636          * do not allow to create an object in this directory.
1637          */
1638         if (vn_ismntpt(dvp)) {
1639                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
1640                 goto out;
1641         }
1642 
1643         /* Verify that type is correct */
1644         switch (args->type) {
1645         case NF4LNK:
1646         case NF4BLK:
1647         case NF4CHR:
1648         case NF4SOCK:
1649         case NF4FIFO:
1650         case NF4DIR:
1651                 break;
1652         default:
1653                 *cs->statusp = resp->status = NFS4ERR_BADTYPE;
1654                 goto out;
1655         };
1656 
1657         if (cs->access == CS_ACCESS_DENIED) {
1658                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
1659                 goto out;
1660         }
1661         if (dvp->v_type != VDIR) {
1662                 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
1663                 goto out;
1664         }
1665         status = utf8_dir_verify(&args->objname);
1666         if (status != NFS4_OK) {
1667                 *cs->statusp = resp->status = status;
1668                 goto out;
1669         }
1670 
1671         if (rdonly4(req, cs)) {
1672                 *cs->statusp = resp->status = NFS4ERR_ROFS;
1673                 goto out;
1674         }
1675 
1676         /*
1677          * Name of newly created object
1678          */
1679         nm = utf8_to_fn(&args->objname, &len, NULL);
1680         if (nm == NULL) {
1681                 *cs->statusp = resp->status = NFS4ERR_INVAL;
1682                 goto out;
1683         }
1684 
1685         if (len > MAXNAMELEN) {
1686                 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
1687                 kmem_free(nm, len);
1688                 goto out;
1689         }
1690 
1691         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
1692         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
1693             MAXPATHLEN  + 1);
1694 
1695         if (name == NULL) {
1696                 *cs->statusp = resp->status = NFS4ERR_INVAL;
1697                 kmem_free(nm, len);
1698                 goto out;
1699         }
1700 
1701         resp->attrset = 0;
1702 
1703         sarg.sbp = &sb;
1704         sarg.is_referral = B_FALSE;
1705         nfs4_ntov_table_init(&ntov);
1706 
1707         status = do_rfs4_set_attrs(&resp->attrset,
1708             &args->createattrs, cs, &sarg, &ntov, NFS4ATTR_SETIT);
1709 
1710         if (sarg.vap->va_mask == 0 && status == NFS4_OK)
1711                 status = NFS4ERR_INVAL;
1712 
1713         if (status != NFS4_OK) {
1714                 *cs->statusp = resp->status = status;
1715                 if (name != nm)
1716                         kmem_free(name, MAXPATHLEN + 1);
1717                 kmem_free(nm, len);
1718                 nfs4_ntov_table_free(&ntov, &sarg);
1719                 resp->attrset = 0;
1720                 goto out;
1721         }
1722 
1723         /* Get "before" change value */
1724         bva.va_mask = AT_CTIME|AT_SEQ|AT_MODE;
1725         error = VOP_GETATTR(dvp, &bva, 0, cr, NULL);
1726         if (error) {
1727                 *cs->statusp = resp->status = puterrno4(error);
1728                 if (name != nm)
1729                         kmem_free(name, MAXPATHLEN + 1);
1730                 kmem_free(nm, len);
1731                 nfs4_ntov_table_free(&ntov, &sarg);
1732                 resp->attrset = 0;
1733                 goto out;
1734         }
1735         NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bva.va_ctime)
1736 
1737         vap = sarg.vap;
1738 
1739         /*
1740          * Set the default initial values for attributes when the parent
1741          * directory does not have the VSUID/VSGID bit set and they have
1742          * not been specified in createattrs.
1743          */
1744         if (!(bva.va_mode & VSUID) && (vap->va_mask & AT_UID) == 0) {
1745                 vap->va_uid = crgetuid(cr);
1746                 vap->va_mask |= AT_UID;
1747         }
1748         if (!(bva.va_mode & VSGID) && (vap->va_mask & AT_GID) == 0) {
1749                 vap->va_gid = crgetgid(cr);
1750                 vap->va_mask |= AT_GID;
1751         }
1752 
1753         vap->va_mask |= AT_TYPE;
1754         switch (args->type) {
1755         case NF4DIR:
1756                 vap->va_type = VDIR;
1757                 if ((vap->va_mask & AT_MODE) == 0) {
1758                         vap->va_mode = 0700; /* default: owner rwx only */
1759                         vap->va_mask |= AT_MODE;
1760                 }
1761                 error = VOP_MKDIR(dvp, name, vap, &vp, cr, NULL, 0, NULL);
1762                 if (error)
1763                         break;
1764 
1765                 /*
1766                  * Get the initial "after" sequence number, if it fails,
1767                  * set to zero
1768                  */
1769                 iva.va_mask = AT_SEQ;
1770                 if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
1771                         iva.va_seq = 0;
1772                 break;
1773         case NF4LNK:
1774                 vap->va_type = VLNK;
1775                 if ((vap->va_mask & AT_MODE) == 0) {
1776                         vap->va_mode = 0700; /* default: owner rwx only */
1777                         vap->va_mask |= AT_MODE;
1778                 }
1779 
1780                 /*
1781                  * symlink names must be treated as data
1782                  */
1783                 lnm = utf8_to_str((utf8string *)&args->ftype4_u.linkdata,
1784                     &llen, NULL);
1785 
1786                 if (lnm == NULL) {
1787                         *cs->statusp = resp->status = NFS4ERR_INVAL;
1788                         if (name != nm)
1789                                 kmem_free(name, MAXPATHLEN + 1);
1790                         kmem_free(nm, len);
1791                         nfs4_ntov_table_free(&ntov, &sarg);
1792                         resp->attrset = 0;
1793                         goto out;
1794                 }
1795 
1796                 if (llen > MAXPATHLEN) {
1797                         *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
1798                         if (name != nm)
1799                                 kmem_free(name, MAXPATHLEN + 1);
1800                         kmem_free(nm, len);
1801                         kmem_free(lnm, llen);
1802                         nfs4_ntov_table_free(&ntov, &sarg);
1803                         resp->attrset = 0;
1804                         goto out;
1805                 }
1806 
1807                 lname = nfscmd_convname(ca, cs->exi, lnm,
1808                     NFSCMD_CONV_INBOUND, MAXPATHLEN  + 1);
1809 
1810                 if (lname == NULL) {
1811                         *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
1812                         if (name != nm)
1813                                 kmem_free(name, MAXPATHLEN + 1);
1814                         kmem_free(nm, len);
1815                         kmem_free(lnm, llen);
1816                         nfs4_ntov_table_free(&ntov, &sarg);
1817                         resp->attrset = 0;
1818                         goto out;
1819                 }
1820 
1821                 error = VOP_SYMLINK(dvp, name, vap, lname, cr, NULL, 0);
1822                 if (lname != lnm)
1823                         kmem_free(lname, MAXPATHLEN + 1);
1824                 kmem_free(lnm, llen);
1825                 if (error)
1826                         break;
1827 
1828                 /*
1829                  * Get the initial "after" sequence number, if it fails,
1830                  * set to zero
1831                  */
1832                 iva.va_mask = AT_SEQ;
1833                 if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
1834                         iva.va_seq = 0;
1835 
1836                 error = VOP_LOOKUP(dvp, name, &vp, NULL, 0, NULL, cr,
1837                     NULL, NULL, NULL);
1838                 if (error)
1839                         break;
1840 
1841                 /*
1842                  * va_seq is not safe over VOP calls, check it again
1843                  * if it has changed zero out iva to force atomic = FALSE.
1844                  */
1845                 iva2.va_mask = AT_SEQ;
1846                 if (VOP_GETATTR(dvp, &iva2, 0, cs->cr, NULL) ||
1847                     iva2.va_seq != iva.va_seq)
1848                         iva.va_seq = 0;
1849                 break;
1850         default:
1851                 /*
1852                  * probably a special file.
1853                  */
1854                 if ((vap->va_mask & AT_MODE) == 0) {
1855                         vap->va_mode = 0600; /* default: owner rw only */
1856                         vap->va_mask |= AT_MODE;
1857                 }
1858                 syncval = FNODSYNC;
1859                 /*
1860                  * We know this will only generate one VOP call
1861                  */
1862                 vp = do_rfs4_op_mknod(args, resp, req, cs, vap, name);
1863 
1864                 if (vp == NULL) {
1865                         if (name != nm)
1866                                 kmem_free(name, MAXPATHLEN + 1);
1867                         kmem_free(nm, len);
1868                         nfs4_ntov_table_free(&ntov, &sarg);
1869                         resp->attrset = 0;
1870                         goto out;
1871                 }
1872 
1873                 /*
1874                  * Get the initial "after" sequence number, if it fails,
1875                  * set to zero
1876                  */
1877                 iva.va_mask = AT_SEQ;
1878                 if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL))
1879                         iva.va_seq = 0;
1880 
1881                 break;
1882         }
1883         if (name != nm)
1884                 kmem_free(name, MAXPATHLEN + 1);
1885         kmem_free(nm, len);
1886 
1887         if (error) {
1888                 *cs->statusp = resp->status = puterrno4(error);
1889         }
1890 
1891         /*
1892          * Force modified data and metadata out to stable storage.
1893          */
1894         (void) VOP_FSYNC(dvp, 0, cr, NULL);
1895 
1896         if (resp->status != NFS4_OK) {
1897                 if (vp != NULL)
1898                         VN_RELE(vp);
1899                 nfs4_ntov_table_free(&ntov, &sarg);
1900                 resp->attrset = 0;
1901                 goto out;
1902         }
1903 
1904         /*
1905          * Finish setup of cinfo response, "before" value already set.
1906          * Get "after" change value, if it fails, simply return the
1907          * before value.
1908          */
1909         ava.va_mask = AT_CTIME|AT_SEQ;
1910         if (VOP_GETATTR(dvp, &ava, 0, cr, NULL)) {
1911                 ava.va_ctime = bva.va_ctime;
1912                 ava.va_seq = 0;
1913         }
1914         NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, ava.va_ctime);
1915 
1916         /*
1917          * True verification that object was created with correct
1918          * attrs is impossible.  The attrs could have been changed
1919          * immediately after object creation.  If attributes did
1920          * not verify, the only recourse for the server is to
1921          * destroy the object.  Maybe if some attrs (like gid)
1922          * are set incorrectly, the object should be destroyed;
1923          * however, seems bad as a default policy.  Do we really
1924          * want to destroy an object over one of the times not
1925          * verifying correctly?  For these reasons, the server
1926          * currently sets bits in attrset for createattrs
1927          * that were set; however, no verification is done.
1928          *
1929          * vmask_to_nmask accounts for vattr bits set on create
1930          *      [do_rfs4_set_attrs() only sets resp bits for
1931          *       non-vattr/vfs bits.]
1932          * Mask off any bits set by default so as not to return
1933          * more attrset bits than were requested in createattrs
1934          */
1935         nfs4_vmask_to_nmask(sarg.vap->va_mask, &resp->attrset);
1936         resp->attrset &= args->createattrs.attrmask;
1937         nfs4_ntov_table_free(&ntov, &sarg);
1938 
1939         error = makefh4(&cs->fh, vp, cs->exi);
1940         if (error) {
1941                 *cs->statusp = resp->status = puterrno4(error);
1942         }
1943 
1944         /*
1945          * The cinfo.atomic = TRUE only if we got no errors, we have
1946          * non-zero va_seq's, and it has incremented by exactly one
1947          * during the creation and it didn't change during the VOP_LOOKUP
1948          * or VOP_FSYNC.
1949          */
1950         if (!error && bva.va_seq && iva.va_seq && ava.va_seq &&
1951             iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
1952                 resp->cinfo.atomic = TRUE;
1953         else
1954                 resp->cinfo.atomic = FALSE;
1955 
1956         /*
1957          * Force modified metadata out to stable storage.
1958          *
1959          * if a underlying vp exists, pass it to VOP_FSYNC
1960          */
1961         if (VOP_REALVP(vp, &realvp, NULL) == 0)
1962                 (void) VOP_FSYNC(realvp, syncval, cr, NULL);
1963         else
1964                 (void) VOP_FSYNC(vp, syncval, cr, NULL);
1965 
1966         if (resp->status != NFS4_OK) {
1967                 VN_RELE(vp);
1968                 goto out;
1969         }
1970         if (cs->vp)
1971                 VN_RELE(cs->vp);
1972 
1973         cs->vp = vp;
1974         *cs->statusp = resp->status = NFS4_OK;
1975 out:
1976         DTRACE_NFSV4_2(op__create__done, struct compound_state *, cs,
1977             CREATE4res *, resp);
1978 }
1979 
1980 /*ARGSUSED*/
1981 static void
1982 rfs4_op_delegpurge(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1983     struct compound_state *cs)
1984 {
1985         DTRACE_NFSV4_2(op__delegpurge__start, struct compound_state *, cs,
1986             DELEGPURGE4args *, &argop->nfs_argop4_u.opdelegpurge);
1987 
1988         rfs4_op_inval(argop, resop, req, cs);
1989 
1990         DTRACE_NFSV4_2(op__delegpurge__done, struct compound_state *, cs,
1991             DELEGPURGE4res *, &resop->nfs_resop4_u.opdelegpurge);
1992 }
1993 
1994 /*ARGSUSED*/
1995 static void
1996 rfs4_op_delegreturn(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
1997     struct compound_state *cs)
1998 {
1999         DELEGRETURN4args *args = &argop->nfs_argop4_u.opdelegreturn;
2000         DELEGRETURN4res *resp = &resop->nfs_resop4_u.opdelegreturn;
2001         rfs4_deleg_state_t *dsp;
2002         nfsstat4 status;
2003 
2004         DTRACE_NFSV4_2(op__delegreturn__start, struct compound_state *, cs,
2005             DELEGRETURN4args *, args);
2006 
2007         status = rfs4_get_deleg_state(&args->deleg_stateid, &dsp);
2008         resp->status = *cs->statusp = status;
2009         if (status != NFS4_OK)
2010                 goto out;
2011 
2012         /* Ensure specified filehandle matches */
2013         if (cs->vp != dsp->rds_finfo->rf_vp) {
2014                 resp->status = *cs->statusp = NFS4ERR_BAD_STATEID;
2015         } else
2016                 rfs4_return_deleg(dsp, FALSE);
2017 
2018         rfs4_update_lease(dsp->rds_client);
2019 
2020         rfs4_deleg_state_rele(dsp);
2021 out:
2022         DTRACE_NFSV4_2(op__delegreturn__done, struct compound_state *, cs,
2023             DELEGRETURN4res *, resp);
2024 }
2025 
2026 /*
2027  * Check to see if a given "flavor" is an explicitly shared flavor.
2028  * The assumption of this routine is the "flavor" is already a valid
2029  * flavor in the secinfo list of "exi".
2030  *
2031  *      e.g.
2032  *              # share -o sec=flavor1 /export
2033  *              # share -o sec=flavor2 /export/home
2034  *
2035  *              flavor2 is not an explicitly shared flavor for /export,
2036  *              however it is in the secinfo list for /export thru the
2037  *              server namespace setup.
2038  */
2039 int
2040 is_exported_sec(int flavor, struct exportinfo *exi)
2041 {
2042         int     i;
2043         struct secinfo *sp;
2044 
2045         sp = exi->exi_export.ex_secinfo;
2046         for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2047                 if (flavor == sp[i].s_secinfo.sc_nfsnum ||
2048                     sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
2049                         return (SEC_REF_EXPORTED(&sp[i]));
2050                 }
2051         }
2052 
2053         /* Should not reach this point based on the assumption */
2054         return (0);
2055 }
2056 
2057 /*
2058  * Check if the security flavor used in the request matches what is
2059  * required at the export point or at the root pseudo node (exi_root).
2060  *
2061  * returns 1 if there's a match or if exported with AUTH_NONE; 0 otherwise.
2062  *
2063  */
2064 static int
2065 secinfo_match_or_authnone(struct compound_state *cs)
2066 {
2067         int     i;
2068         struct secinfo *sp;
2069 
2070         /*
2071          * Check cs->nfsflavor (from the request) against
2072          * the current export data in cs->exi.
2073          */
2074         sp = cs->exi->exi_export.ex_secinfo;
2075         for (i = 0; i < cs->exi->exi_export.ex_seccnt; i++) {
2076                 if (cs->nfsflavor == sp[i].s_secinfo.sc_nfsnum ||
2077                     sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
2078                         return (1);
2079         }
2080 
2081         return (0);
2082 }
2083 
2084 /*
2085  * Check the access authority for the client and return the correct error.
2086  */
2087 nfsstat4
2088 call_checkauth4(struct compound_state *cs, struct svc_req *req)
2089 {
2090         int     authres;
2091 
2092         /*
2093          * First, check if the security flavor used in the request
2094          * are among the flavors set in the server namespace.
2095          */
2096         if (!secinfo_match_or_authnone(cs)) {
2097                 *cs->statusp = NFS4ERR_WRONGSEC;
2098                 return (*cs->statusp);
2099         }
2100 
2101         authres = checkauth4(cs, req);
2102 
2103         if (authres > 0) {
2104                 *cs->statusp = NFS4_OK;
2105                 if (! (cs->access & CS_ACCESS_LIMITED))
2106                         cs->access = CS_ACCESS_OK;
2107         } else if (authres == 0) {
2108                 *cs->statusp = NFS4ERR_ACCESS;
2109         } else if (authres == -2) {
2110                 *cs->statusp = NFS4ERR_WRONGSEC;
2111         } else {
2112                 *cs->statusp = NFS4ERR_DELAY;
2113         }
2114         return (*cs->statusp);
2115 }
2116 
2117 /*
2118  * bitmap4_to_attrmask is called by getattr and readdir.
2119  * It sets up the vattr mask and determines whether vfsstat call is needed
2120  * based on the input bitmap.
2121  * Returns nfsv4 status.
2122  */
2123 static nfsstat4
2124 bitmap4_to_attrmask(bitmap4 breq, struct nfs4_svgetit_arg *sargp)
2125 {
2126         int i;
2127         uint_t  va_mask;
2128         struct statvfs64 *sbp = sargp->sbp;
2129 
2130         sargp->sbp = NULL;
2131         sargp->flag = 0;
2132         sargp->rdattr_error = NFS4_OK;
2133         sargp->mntdfid_set = FALSE;
2134         if (sargp->cs->vp)
2135                 sargp->xattr = get_fh4_flag(&sargp->cs->fh,
2136                     FH4_ATTRDIR | FH4_NAMEDATTR);
2137         else
2138                 sargp->xattr = 0;
2139 
2140         /*
2141          * Set rdattr_error_req to true if return error per
2142          * failed entry rather than fail the readdir.
2143          */
2144         if (breq & FATTR4_RDATTR_ERROR_MASK)
2145                 sargp->rdattr_error_req = 1;
2146         else
2147                 sargp->rdattr_error_req = 0;
2148 
2149         /*
2150          * generate the va_mask
2151          * Handle the easy cases first
2152          */
2153         switch (breq) {
2154         case NFS4_NTOV_ATTR_MASK:
2155                 sargp->vap->va_mask = NFS4_NTOV_ATTR_AT_MASK;
2156                 return (NFS4_OK);
2157 
2158         case NFS4_FS_ATTR_MASK:
2159                 sargp->vap->va_mask = NFS4_FS_ATTR_AT_MASK;
2160                 sargp->sbp = sbp;
2161                 return (NFS4_OK);
2162 
2163         case NFS4_NTOV_ATTR_CACHE_MASK:
2164                 sargp->vap->va_mask = NFS4_NTOV_ATTR_CACHE_AT_MASK;
2165                 return (NFS4_OK);
2166 
2167         case FATTR4_LEASE_TIME_MASK:
2168                 sargp->vap->va_mask = 0;
2169                 return (NFS4_OK);
2170 
2171         default:
2172                 va_mask = 0;
2173                 for (i = 0; i < nfs4_ntov_map_size; i++) {
2174                         if ((breq & nfs4_ntov_map[i].fbit) &&
2175                             nfs4_ntov_map[i].vbit)
2176                                 va_mask |= nfs4_ntov_map[i].vbit;
2177                 }
2178 
2179                 /*
2180                  * Check is vfsstat is needed
2181                  */
2182                 if (breq & NFS4_FS_ATTR_MASK)
2183                         sargp->sbp = sbp;
2184 
2185                 sargp->vap->va_mask = va_mask;
2186                 return (NFS4_OK);
2187         }
2188         /* NOTREACHED */
2189 }
2190 
2191 /*
2192  * bitmap4_get_sysattrs is called by getattr and readdir.
2193  * It calls both VOP_GETATTR and VFS_STATVFS calls to get the attrs.
2194  * Returns nfsv4 status.
2195  */
2196 static nfsstat4
2197 bitmap4_get_sysattrs(struct nfs4_svgetit_arg *sargp)
2198 {
2199         int error;
2200         struct compound_state *cs = sargp->cs;
2201         vnode_t *vp = cs->vp;
2202 
2203         if (sargp->sbp != NULL) {
2204                 if (error = VFS_STATVFS(vp->v_vfsp, sargp->sbp)) {
2205                         sargp->sbp = NULL;   /* to identify error */
2206                         return (puterrno4(error));
2207                 }
2208         }
2209 
2210         return (rfs4_vop_getattr(vp, sargp->vap, 0, cs->cr));
2211 }
2212 
2213 static void
2214 nfs4_ntov_table_init(struct nfs4_ntov_table *ntovp)
2215 {
2216         ntovp->na = kmem_zalloc(sizeof (union nfs4_attr_u) * nfs4_ntov_map_size,
2217             KM_SLEEP);
2218         ntovp->attrcnt = 0;
2219         ntovp->vfsstat = FALSE;
2220 }
2221 
2222 static void
2223 nfs4_ntov_table_free(struct nfs4_ntov_table *ntovp,
2224     struct nfs4_svgetit_arg *sargp)
2225 {
2226         int i;
2227         union nfs4_attr_u *na;
2228         uint8_t *amap;
2229 
2230         /*
2231          * XXX Should do the same checks for whether the bit is set
2232          */
2233         for (i = 0, na = ntovp->na, amap = ntovp->amap;
2234             i < ntovp->attrcnt; i++, na++, amap++) {
2235                 (void) (*nfs4_ntov_map[*amap].sv_getit)(
2236                     NFS4ATTR_FREEIT, sargp, na);
2237         }
2238         if ((sargp->op == NFS4ATTR_SETIT) || (sargp->op == NFS4ATTR_VERIT)) {
2239                 /*
2240                  * xdr_free for getattr will be done later
2241                  */
2242                 for (i = 0, na = ntovp->na, amap = ntovp->amap;
2243                     i < ntovp->attrcnt; i++, na++, amap++) {
2244                         xdr_free(nfs4_ntov_map[*amap].xfunc, (caddr_t)na);
2245                 }
2246         }
2247         kmem_free(ntovp->na, sizeof (union nfs4_attr_u) * nfs4_ntov_map_size);
2248 }
2249 
2250 /*
2251  * do_rfs4_op_getattr gets the system attrs and converts into fattr4.
2252  */
2253 static nfsstat4
2254 do_rfs4_op_getattr(bitmap4 breq, fattr4 *fattrp,
2255     struct nfs4_svgetit_arg *sargp)
2256 {
2257         int error = 0;
2258         int i, k;
2259         struct nfs4_ntov_table ntov;
2260         XDR xdr;
2261         ulong_t xdr_size;
2262         char *xdr_attrs;
2263         nfsstat4 status = NFS4_OK;
2264         nfsstat4 prev_rdattr_error = sargp->rdattr_error;
2265         union nfs4_attr_u *na;
2266         uint8_t *amap;
2267 
2268         sargp->op = NFS4ATTR_GETIT;
2269         sargp->flag = 0;
2270 
2271         fattrp->attrmask = 0;
2272         /* if no bits requested, then return empty fattr4 */
2273         if (breq == 0) {
2274                 fattrp->attrlist4_len = 0;
2275                 fattrp->attrlist4 = NULL;
2276                 return (NFS4_OK);
2277         }
2278 
2279         /*
2280          * return NFS4ERR_INVAL when client requests write-only attrs
2281          */
2282         if (breq & (FATTR4_TIME_ACCESS_SET_MASK | FATTR4_TIME_MODIFY_SET_MASK))
2283                 return (NFS4ERR_INVAL);
2284 
2285         nfs4_ntov_table_init(&ntov);
2286         na = ntov.na;
2287         amap = ntov.amap;
2288 
2289         /*
2290          * Now loop to get or verify the attrs
2291          */
2292         for (i = 0; i < nfs4_ntov_map_size; i++) {
2293                 if (breq & nfs4_ntov_map[i].fbit) {
2294                         if ((*nfs4_ntov_map[i].sv_getit)(
2295                             NFS4ATTR_SUPPORTED, sargp, NULL) == 0) {
2296 
2297                                 error = (*nfs4_ntov_map[i].sv_getit)(
2298                                     NFS4ATTR_GETIT, sargp, na);
2299 
2300                                 /*
2301                                  * Possible error values:
2302                                  * >0 if sv_getit failed to
2303                                  * get the attr; 0 if succeeded;
2304                                  * <0 if rdattr_error and the
2305                                  * attribute cannot be returned.
2306                                  */
2307                                 if (error && !(sargp->rdattr_error_req))
2308                                         goto done;
2309                                 /*
2310                                  * If error then just for entry
2311                                  */
2312                                 if (error == 0) {
2313                                         fattrp->attrmask |=
2314                                             nfs4_ntov_map[i].fbit;
2315                                         *amap++ =
2316                                             (uint8_t)nfs4_ntov_map[i].nval;
2317                                         na++;
2318                                         (ntov.attrcnt)++;
2319                                 } else if ((error > 0) &&
2320                                     (sargp->rdattr_error == NFS4_OK)) {
2321                                         sargp->rdattr_error = puterrno4(error);
2322                                 }
2323                                 error = 0;
2324                         }
2325                 }
2326         }
2327 
2328         /*
2329          * If rdattr_error was set after the return value for it was assigned,
2330          * update it.
2331          */
2332         if (prev_rdattr_error != sargp->rdattr_error) {
2333                 na = ntov.na;
2334                 amap = ntov.amap;
2335                 for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2336                         k = *amap;
2337                         if (k < FATTR4_RDATTR_ERROR) {
2338                                 continue;
2339                         }
2340                         if ((k == FATTR4_RDATTR_ERROR) &&
2341                             ((*nfs4_ntov_map[k].sv_getit)(
2342                             NFS4ATTR_SUPPORTED, sargp, NULL) == 0)) {
2343 
2344                                 (void) (*nfs4_ntov_map[k].sv_getit)(
2345                                     NFS4ATTR_GETIT, sargp, na);
2346                         }
2347                         break;
2348                 }
2349         }
2350 
2351         xdr_size = 0;
2352         na = ntov.na;
2353         amap = ntov.amap;
2354         for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2355                 xdr_size += xdr_sizeof(nfs4_ntov_map[*amap].xfunc, na);
2356         }
2357 
2358         fattrp->attrlist4_len = xdr_size;
2359         if (xdr_size) {
2360                 /* freed by rfs4_op_getattr_free() */
2361                 fattrp->attrlist4 = xdr_attrs = kmem_zalloc(xdr_size, KM_SLEEP);
2362 
2363                 xdrmem_create(&xdr, xdr_attrs, xdr_size, XDR_ENCODE);
2364 
2365                 na = ntov.na;
2366                 amap = ntov.amap;
2367                 for (i = 0; i < ntov.attrcnt; i++, na++, amap++) {
2368                         if (!(*nfs4_ntov_map[*amap].xfunc)(&xdr, na)) {
2369                                 DTRACE_PROBE1(nfss__e__getattr4_encfail,
2370                                     int, *amap);
2371                                 status = NFS4ERR_SERVERFAULT;
2372                                 break;
2373                         }
2374                 }
2375                 /* xdrmem_destroy(&xdrs); */        /* NO-OP */
2376         } else {
2377                 fattrp->attrlist4 = NULL;
2378         }
2379 done:
2380 
2381         nfs4_ntov_table_free(&ntov, sargp);
2382 
2383         if (error != 0)
2384                 status = puterrno4(error);
2385 
2386         return (status);
2387 }
2388 
2389 /* ARGSUSED */
2390 static void
2391 rfs4_op_getattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2392     struct compound_state *cs)
2393 {
2394         GETATTR4args *args = &argop->nfs_argop4_u.opgetattr;
2395         GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
2396         struct nfs4_svgetit_arg sarg;
2397         struct statvfs64 sb;
2398         nfsstat4 status;
2399 
2400         DTRACE_NFSV4_2(op__getattr__start, struct compound_state *, cs,
2401             GETATTR4args *, args);
2402 
2403         if (cs->vp == NULL) {
2404                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2405                 goto out;
2406         }
2407 
2408         if (cs->access == CS_ACCESS_DENIED) {
2409                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
2410                 goto out;
2411         }
2412 
2413         sarg.sbp = &sb;
2414         sarg.cs = cs;
2415         sarg.is_referral = B_FALSE;
2416 
2417         status = bitmap4_to_attrmask(args->attr_request, &sarg);
2418         if (status == NFS4_OK) {
2419 
2420                 status = bitmap4_get_sysattrs(&sarg);
2421                 if (status == NFS4_OK) {
2422 
2423                         /* Is this a referral? */
2424                         if (vn_is_nfs_reparse(cs->vp, cs->cr)) {
2425                                 /* Older V4 Solaris client sees a link */
2426                                 if (client_is_downrev(req))
2427                                         sarg.vap->va_type = VLNK;
2428                                 else
2429                                         sarg.is_referral = B_TRUE;
2430                         }
2431 
2432                         status = do_rfs4_op_getattr(args->attr_request,
2433                             &resp->obj_attributes, &sarg);
2434                 }
2435         }
2436         *cs->statusp = resp->status = status;
2437 out:
2438         DTRACE_NFSV4_2(op__getattr__done, struct compound_state *, cs,
2439             GETATTR4res *, resp);
2440 }
2441 
2442 static void
2443 rfs4_op_getattr_free(nfs_resop4 *resop)
2444 {
2445         GETATTR4res *resp = &resop->nfs_resop4_u.opgetattr;
2446 
2447         nfs4_fattr4_free(&resp->obj_attributes);
2448 }
2449 
2450 /* ARGSUSED */
2451 static void
2452 rfs4_op_getfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2453     struct compound_state *cs)
2454 {
2455         GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
2456 
2457         DTRACE_NFSV4_1(op__getfh__start, struct compound_state *, cs);
2458 
2459         if (cs->vp == NULL) {
2460                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2461                 goto out;
2462         }
2463         if (cs->access == CS_ACCESS_DENIED) {
2464                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
2465                 goto out;
2466         }
2467 
2468         /* check for reparse point at the share point */
2469         if (cs->exi->exi_moved || vn_is_nfs_reparse(cs->exi->exi_vp, cs->cr)) {
2470                 /* it's all bad */
2471                 cs->exi->exi_moved = 1;
2472                 *cs->statusp = resp->status = NFS4ERR_MOVED;
2473                 DTRACE_PROBE2(nfs4serv__func__referral__shared__moved,
2474                     vnode_t *, cs->vp, char *, "rfs4_op_getfh");
2475                 return;
2476         }
2477 
2478         /* check for reparse point at vp */
2479         if (vn_is_nfs_reparse(cs->vp, cs->cr) && !client_is_downrev(req)) {
2480                 /* it's not all bad */
2481                 *cs->statusp = resp->status = NFS4ERR_MOVED;
2482                 DTRACE_PROBE2(nfs4serv__func__referral__moved,
2483                     vnode_t *, cs->vp, char *, "rfs4_op_getfh");
2484                 return;
2485         }
2486 
2487         resp->object.nfs_fh4_val =
2488             kmem_alloc(cs->fh.nfs_fh4_len, KM_SLEEP);
2489         nfs_fh4_copy(&cs->fh, &resp->object);
2490         *cs->statusp = resp->status = NFS4_OK;
2491 out:
2492         DTRACE_NFSV4_2(op__getfh__done, struct compound_state *, cs,
2493             GETFH4res *, resp);
2494 }
2495 
2496 static void
2497 rfs4_op_getfh_free(nfs_resop4 *resop)
2498 {
2499         GETFH4res *resp = &resop->nfs_resop4_u.opgetfh;
2500 
2501         if (resp->status == NFS4_OK &&
2502             resp->object.nfs_fh4_val != NULL) {
2503                 kmem_free(resp->object.nfs_fh4_val, resp->object.nfs_fh4_len);
2504                 resp->object.nfs_fh4_val = NULL;
2505                 resp->object.nfs_fh4_len = 0;
2506         }
2507 }
2508 
2509 /*
2510  * illegal: args: void
2511  *          res : status (NFS4ERR_OP_ILLEGAL)
2512  */
2513 /* ARGSUSED */
2514 static void
2515 rfs4_op_illegal(nfs_argop4 *argop, nfs_resop4 *resop,
2516     struct svc_req *req, struct compound_state *cs)
2517 {
2518         ILLEGAL4res *resp = &resop->nfs_resop4_u.opillegal;
2519 
2520         resop->resop = OP_ILLEGAL;
2521         *cs->statusp = resp->status = NFS4ERR_OP_ILLEGAL;
2522 }
2523 
2524 /*
2525  * link: args: SAVED_FH: file, CURRENT_FH: target directory
2526  *       res: status. If success - CURRENT_FH unchanged, return change_info
2527  */
2528 /* ARGSUSED */
2529 static void
2530 rfs4_op_link(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2531     struct compound_state *cs)
2532 {
2533         LINK4args *args = &argop->nfs_argop4_u.oplink;
2534         LINK4res *resp = &resop->nfs_resop4_u.oplink;
2535         int error;
2536         vnode_t *vp;
2537         vnode_t *dvp;
2538         struct vattr bdva, idva, adva;
2539         char *nm;
2540         uint_t  len;
2541         struct sockaddr *ca;
2542         char *name = NULL;
2543         nfsstat4 status;
2544 
2545         DTRACE_NFSV4_2(op__link__start, struct compound_state *, cs,
2546             LINK4args *, args);
2547 
2548         /* SAVED_FH: source object */
2549         vp = cs->saved_vp;
2550         if (vp == NULL) {
2551                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2552                 goto out;
2553         }
2554 
2555         /* CURRENT_FH: target directory */
2556         dvp = cs->vp;
2557         if (dvp == NULL) {
2558                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2559                 goto out;
2560         }
2561 
2562         /*
2563          * If there is a non-shared filesystem mounted on this vnode,
2564          * do not allow to link any file in this directory.
2565          */
2566         if (vn_ismntpt(dvp)) {
2567                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
2568                 goto out;
2569         }
2570 
2571         if (cs->access == CS_ACCESS_DENIED) {
2572                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
2573                 goto out;
2574         }
2575 
2576         /* Check source object's type validity */
2577         if (vp->v_type == VDIR) {
2578                 *cs->statusp = resp->status = NFS4ERR_ISDIR;
2579                 goto out;
2580         }
2581 
2582         /* Check target directory's type */
2583         if (dvp->v_type != VDIR) {
2584                 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
2585                 goto out;
2586         }
2587 
2588         if (cs->saved_exi != cs->exi) {
2589                 *cs->statusp = resp->status = NFS4ERR_XDEV;
2590                 goto out;
2591         }
2592 
2593         status = utf8_dir_verify(&args->newname);
2594         if (status != NFS4_OK) {
2595                 *cs->statusp = resp->status = status;
2596                 goto out;
2597         }
2598 
2599         nm = utf8_to_fn(&args->newname, &len, NULL);
2600         if (nm == NULL) {
2601                 *cs->statusp = resp->status = NFS4ERR_INVAL;
2602                 goto out;
2603         }
2604 
2605         if (len > MAXNAMELEN) {
2606                 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
2607                 kmem_free(nm, len);
2608                 goto out;
2609         }
2610 
2611         if (rdonly4(req, cs)) {
2612                 *cs->statusp = resp->status = NFS4ERR_ROFS;
2613                 kmem_free(nm, len);
2614                 goto out;
2615         }
2616 
2617         /* Get "before" change value */
2618         bdva.va_mask = AT_CTIME|AT_SEQ;
2619         error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
2620         if (error) {
2621                 *cs->statusp = resp->status = puterrno4(error);
2622                 kmem_free(nm, len);
2623                 goto out;
2624         }
2625 
2626         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2627         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
2628             MAXPATHLEN  + 1);
2629 
2630         if (name == NULL) {
2631                 *cs->statusp = resp->status = NFS4ERR_INVAL;
2632                 kmem_free(nm, len);
2633                 goto out;
2634         }
2635 
2636         NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
2637 
2638         error = VOP_LINK(dvp, vp, name, cs->cr, NULL, 0);
2639 
2640         if (nm != name)
2641                 kmem_free(name, MAXPATHLEN + 1);
2642         kmem_free(nm, len);
2643 
2644         /*
2645          * Get the initial "after" sequence number, if it fails, set to zero
2646          */
2647         idva.va_mask = AT_SEQ;
2648         if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
2649                 idva.va_seq = 0;
2650 
2651         /*
2652          * Force modified data and metadata out to stable storage.
2653          */
2654         (void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
2655         (void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
2656 
2657         if (error) {
2658                 *cs->statusp = resp->status = puterrno4(error);
2659                 goto out;
2660         }
2661 
2662         /*
2663          * Get "after" change value, if it fails, simply return the
2664          * before value.
2665          */
2666         adva.va_mask = AT_CTIME|AT_SEQ;
2667         if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
2668                 adva.va_ctime = bdva.va_ctime;
2669                 adva.va_seq = 0;
2670         }
2671 
2672         NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
2673 
2674         /*
2675          * The cinfo.atomic = TRUE only if we have
2676          * non-zero va_seq's, and it has incremented by exactly one
2677          * during the VOP_LINK and it didn't change during the VOP_FSYNC.
2678          */
2679         if (bdva.va_seq && idva.va_seq && adva.va_seq &&
2680             idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
2681                 resp->cinfo.atomic = TRUE;
2682         else
2683                 resp->cinfo.atomic = FALSE;
2684 
2685         *cs->statusp = resp->status = NFS4_OK;
2686 out:
2687         DTRACE_NFSV4_2(op__link__done, struct compound_state *, cs,
2688             LINK4res *, resp);
2689 }
2690 
2691 /*
2692  * Used by rfs4_op_lookup and rfs4_op_lookupp to do the actual work.
2693  */
2694 
2695 /* ARGSUSED */
2696 static nfsstat4
2697 do_rfs4_op_lookup(char *nm, struct svc_req *req, struct compound_state *cs)
2698 {
2699         int error;
2700         int different_export = 0;
2701         vnode_t *vp, *pre_tvp = NULL, *oldvp = NULL;
2702         struct exportinfo *exi = NULL, *pre_exi = NULL;
2703         nfsstat4 stat;
2704         fid_t fid;
2705         int attrdir, dotdot, walk;
2706         bool_t is_newvp = FALSE;
2707 
2708         if (cs->vp->v_flag & V_XATTRDIR) {
2709                 attrdir = 1;
2710                 ASSERT(get_fh4_flag(&cs->fh, FH4_ATTRDIR));
2711         } else {
2712                 attrdir = 0;
2713                 ASSERT(! get_fh4_flag(&cs->fh, FH4_ATTRDIR));
2714         }
2715 
2716         dotdot = (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0');
2717 
2718         /*
2719          * If dotdotting, then need to check whether it's
2720          * above the root of a filesystem, or above an
2721          * export point.
2722          */
2723         if (dotdot) {
2724                 vnode_t *zone_rootvp;
2725 
2726                 ASSERT(cs->exi != NULL);
2727                 zone_rootvp = cs->exi->exi_ne->exi_root->exi_vp;
2728                 /*
2729                  * If dotdotting at the root of a filesystem, then
2730                  * need to traverse back to the mounted-on filesystem
2731                  * and do the dotdot lookup there.
2732                  */
2733                 if ((cs->vp->v_flag & VROOT) || VN_CMP(cs->vp, zone_rootvp)) {
2734 
2735                         /*
2736                          * If at the system root, then can
2737                          * go up no further.
2738                          */
2739                         if (VN_CMP(cs->vp, zone_rootvp))
2740                                 return (puterrno4(ENOENT));
2741 
2742                         /*
2743                          * Traverse back to the mounted-on filesystem
2744                          */
2745                         cs->vp = untraverse(cs->vp, zone_rootvp);
2746 
2747                         /*
2748                          * Set the different_export flag so we remember
2749                          * to pick up a new exportinfo entry for
2750                          * this new filesystem.
2751                          */
2752                         different_export = 1;
2753                 } else {
2754 
2755                         /*
2756                          * If dotdotting above an export point then set
2757                          * the different_export to get new export info.
2758                          */
2759                         different_export = nfs_exported(cs->exi, cs->vp);
2760                 }
2761         }
2762 
2763         error = VOP_LOOKUP(cs->vp, nm, &vp, NULL, 0, NULL, cs->cr,
2764             NULL, NULL, NULL);
2765         if (error)
2766                 return (puterrno4(error));
2767 
2768         /*
2769          * If the vnode is in a pseudo filesystem, check whether it is visible.
2770          *
2771          * XXX if the vnode is a symlink and it is not visible in
2772          * a pseudo filesystem, return ENOENT (not following symlink).
2773          * V4 client can not mount such symlink. This is a regression
2774          * from V2/V3.
2775          *
2776          * In the same exported filesystem, if the security flavor used
2777          * is not an explicitly shared flavor, limit the view to the visible
2778          * list entries only. This is not a WRONGSEC case because it's already
2779          * checked via PUTROOTFH/PUTPUBFH or PUTFH.
2780          */
2781         if (!different_export &&
2782             (PSEUDO(cs->exi) || ! is_exported_sec(cs->nfsflavor, cs->exi) ||
2783             cs->access & CS_ACCESS_LIMITED)) {
2784                 if (! nfs_visible(cs->exi, vp, &different_export)) {
2785                         VN_RELE(vp);
2786                         return (puterrno4(ENOENT));
2787                 }
2788         }
2789 
2790         /*
2791          * If it's a mountpoint, then traverse it.
2792          */
2793         if (vn_ismntpt(vp)) {
2794                 pre_exi = cs->exi;   /* save pre-traversed exportinfo */
2795                 pre_tvp = vp;           /* save pre-traversed vnode     */
2796 
2797                 /*
2798                  * hold pre_tvp to counteract rele by traverse.  We will
2799                  * need pre_tvp below if checkexport4 fails
2800                  */
2801                 VN_HOLD(pre_tvp);
2802                 if ((error = traverse(&vp)) != 0) {
2803                         VN_RELE(vp);
2804                         VN_RELE(pre_tvp);
2805                         return (puterrno4(error));
2806                 }
2807                 different_export = 1;
2808         } else if (vp->v_vfsp != cs->vp->v_vfsp) {
2809                 /*
2810                  * The vfsp comparison is to handle the case where
2811                  * a LOFS mount is shared.  lo_lookup traverses mount points,
2812                  * and NFS is unaware of local fs transistions because
2813                  * v_vfsmountedhere isn't set.  For this special LOFS case,
2814                  * the dir and the obj returned by lookup will have different
2815                  * vfs ptrs.
2816                  */
2817                 different_export = 1;
2818         }
2819 
2820         if (different_export) {
2821 
2822                 bzero(&fid, sizeof (fid));
2823                 fid.fid_len = MAXFIDSZ;
2824                 error = vop_fid_pseudo(vp, &fid);
2825                 if (error) {
2826                         VN_RELE(vp);
2827                         if (pre_tvp)
2828                                 VN_RELE(pre_tvp);
2829                         return (puterrno4(error));
2830                 }
2831 
2832                 if (dotdot)
2833                         exi = nfs_vptoexi(NULL, vp, cs->cr, &walk, NULL, TRUE);
2834                 else
2835                         exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
2836 
2837                 if (exi == NULL) {
2838                         if (pre_tvp) {
2839                                 /*
2840                                  * If this vnode is a mounted-on vnode,
2841                                  * but the mounted-on file system is not
2842                                  * exported, send back the filehandle for
2843                                  * the mounted-on vnode, not the root of
2844                                  * the mounted-on file system.
2845                                  */
2846                                 VN_RELE(vp);
2847                                 vp = pre_tvp;
2848                                 exi = pre_exi;
2849                         } else {
2850                                 VN_RELE(vp);
2851                                 return (puterrno4(EACCES));
2852                         }
2853                 } else if (pre_tvp) {
2854                         /* we're done with pre_tvp now. release extra hold */
2855                         VN_RELE(pre_tvp);
2856                 }
2857 
2858                 cs->exi = exi;
2859 
2860                 /*
2861                  * Now we do a checkauth4. The reason is that
2862                  * this client/user may not have access to the new
2863                  * exported file system, and if they do,
2864                  * the client/user may be mapped to a different uid.
2865                  *
2866                  * We start with a new cr, because the checkauth4 done
2867                  * in the PUT*FH operation over wrote the cred's uid,
2868                  * gid, etc, and we want the real thing before calling
2869                  * checkauth4()
2870                  */
2871                 crfree(cs->cr);
2872                 cs->cr = crdup(cs->basecr);
2873 
2874                 oldvp = cs->vp;
2875                 cs->vp = vp;
2876                 is_newvp = TRUE;
2877 
2878                 stat = call_checkauth4(cs, req);
2879                 if (stat != NFS4_OK) {
2880                         VN_RELE(cs->vp);
2881                         cs->vp = oldvp;
2882                         return (stat);
2883                 }
2884         }
2885 
2886         /*
2887          * After various NFS checks, do a label check on the path
2888          * component. The label on this path should either be the
2889          * global zone's label or a zone's label. We are only
2890          * interested in the zone's label because exported files
2891          * in global zone is accessible (though read-only) to
2892          * clients. The exportability/visibility check is already
2893          * done before reaching this code.
2894          */
2895         if (is_system_labeled()) {
2896                 bslabel_t *clabel;
2897 
2898                 ASSERT(req->rq_label != NULL);
2899                 clabel = req->rq_label;
2900                 DTRACE_PROBE2(tx__rfs4__log__info__oplookup__clabel, char *,
2901                     "got client label from request(1)", struct svc_req *, req);
2902 
2903                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
2904                         if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
2905                             cs->exi)) {
2906                                 error = EACCES;
2907                                 goto err_out;
2908                         }
2909                 } else {
2910                         /*
2911                          * We grant access to admin_low label clients
2912                          * only if the client is trusted, i.e. also
2913                          * running Solaris Trusted Extension.
2914                          */
2915                         struct sockaddr *ca;
2916                         int             addr_type;
2917                         void            *ipaddr;
2918                         tsol_tpc_t      *tp;
2919 
2920                         ca = (struct sockaddr *)svc_getrpccaller(
2921                             req->rq_xprt)->buf;
2922                         if (ca->sa_family == AF_INET) {
2923                                 addr_type = IPV4_VERSION;
2924                                 ipaddr = &((struct sockaddr_in *)ca)->sin_addr;
2925                         } else if (ca->sa_family == AF_INET6) {
2926                                 addr_type = IPV6_VERSION;
2927                                 ipaddr = &((struct sockaddr_in6 *)
2928                                     ca)->sin6_addr;
2929                         }
2930                         tp = find_tpc(ipaddr, addr_type, B_FALSE);
2931                         if (tp == NULL || tp->tpc_tp.tp_doi !=
2932                             l_admin_low->tsl_doi || tp->tpc_tp.host_type !=
2933                             SUN_CIPSO) {
2934                                 if (tp != NULL)
2935                                         TPC_RELE(tp);
2936                                 error = EACCES;
2937                                 goto err_out;
2938                         }
2939                         TPC_RELE(tp);
2940                 }
2941         }
2942 
2943         error = makefh4(&cs->fh, vp, cs->exi);
2944 
2945 err_out:
2946         if (error) {
2947                 if (is_newvp) {
2948                         VN_RELE(cs->vp);
2949                         cs->vp = oldvp;
2950                 } else
2951                         VN_RELE(vp);
2952                 return (puterrno4(error));
2953         }
2954 
2955         if (!is_newvp) {
2956                 if (cs->vp)
2957                         VN_RELE(cs->vp);
2958                 cs->vp = vp;
2959         } else if (oldvp)
2960                 VN_RELE(oldvp);
2961 
2962         /*
2963          * if did lookup on attrdir and didn't lookup .., set named
2964          * attr fh flag
2965          */
2966         if (attrdir && ! dotdot)
2967                 set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
2968 
2969         /* Assume false for now, open proc will set this */
2970         cs->mandlock = FALSE;
2971 
2972         return (NFS4_OK);
2973 }
2974 
2975 /* ARGSUSED */
2976 static void
2977 rfs4_op_lookup(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
2978     struct compound_state *cs)
2979 {
2980         LOOKUP4args *args = &argop->nfs_argop4_u.oplookup;
2981         LOOKUP4res *resp = &resop->nfs_resop4_u.oplookup;
2982         char *nm;
2983         uint_t len;
2984         struct sockaddr *ca;
2985         char *name = NULL;
2986         nfsstat4 status;
2987 
2988         DTRACE_NFSV4_2(op__lookup__start, struct compound_state *, cs,
2989             LOOKUP4args *, args);
2990 
2991         if (cs->vp == NULL) {
2992                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
2993                 goto out;
2994         }
2995 
2996         if (cs->vp->v_type == VLNK) {
2997                 *cs->statusp = resp->status = NFS4ERR_SYMLINK;
2998                 goto out;
2999         }
3000 
3001         if (cs->vp->v_type != VDIR) {
3002                 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
3003                 goto out;
3004         }
3005 
3006         status = utf8_dir_verify(&args->objname);
3007         if (status != NFS4_OK) {
3008                 *cs->statusp = resp->status = status;
3009                 goto out;
3010         }
3011 
3012         nm = utf8_to_str(&args->objname, &len, NULL);
3013         if (nm == NULL) {
3014                 *cs->statusp = resp->status = NFS4ERR_INVAL;
3015                 goto out;
3016         }
3017 
3018         if (len > MAXNAMELEN) {
3019                 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
3020                 kmem_free(nm, len);
3021                 goto out;
3022         }
3023 
3024         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
3025         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
3026             MAXPATHLEN  + 1);
3027 
3028         if (name == NULL) {
3029                 *cs->statusp = resp->status = NFS4ERR_INVAL;
3030                 kmem_free(nm, len);
3031                 goto out;
3032         }
3033 
3034         *cs->statusp = resp->status = do_rfs4_op_lookup(name, req, cs);
3035 
3036         if (name != nm)
3037                 kmem_free(name, MAXPATHLEN + 1);
3038         kmem_free(nm, len);
3039 
3040 out:
3041         DTRACE_NFSV4_2(op__lookup__done, struct compound_state *, cs,
3042             LOOKUP4res *, resp);
3043 }
3044 
3045 /* ARGSUSED */
3046 static void
3047 rfs4_op_lookupp(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
3048     struct compound_state *cs)
3049 {
3050         LOOKUPP4res *resp = &resop->nfs_resop4_u.oplookupp;
3051 
3052         DTRACE_NFSV4_1(op__lookupp__start, struct compound_state *, cs);
3053 
3054         if (cs->vp == NULL) {
3055                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3056                 goto out;
3057         }
3058 
3059         if (cs->vp->v_type != VDIR) {
3060                 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
3061                 goto out;
3062         }
3063 
3064         *cs->statusp = resp->status = do_rfs4_op_lookup("..", req, cs);
3065 
3066         /*
3067          * From NFSV4 Specification, LOOKUPP should not check for
3068          * NFS4ERR_WRONGSEC. Retrun NFS4_OK instead.
3069          */
3070         if (resp->status == NFS4ERR_WRONGSEC) {
3071                 *cs->statusp = resp->status = NFS4_OK;
3072         }
3073 
3074 out:
3075         DTRACE_NFSV4_2(op__lookupp__done, struct compound_state *, cs,
3076             LOOKUPP4res *, resp);
3077 }
3078 
3079 
3080 /*ARGSUSED2*/
3081 static void
3082 rfs4_op_openattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3083     struct compound_state *cs)
3084 {
3085         OPENATTR4args   *args = &argop->nfs_argop4_u.opopenattr;
3086         OPENATTR4res    *resp = &resop->nfs_resop4_u.opopenattr;
3087         vnode_t         *avp = NULL;
3088         int             lookup_flags = LOOKUP_XATTR, error;
3089         int             exp_ro = 0;
3090 
3091         DTRACE_NFSV4_2(op__openattr__start, struct compound_state *, cs,
3092             OPENATTR4args *, args);
3093 
3094         if (cs->vp == NULL) {
3095                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3096                 goto out;
3097         }
3098 
3099         if ((cs->vp->v_vfsp->vfs_flag & VFS_XATTR) == 0 &&
3100             !vfs_has_feature(cs->vp->v_vfsp, VFSFT_SYSATTR_VIEWS)) {
3101                 *cs->statusp = resp->status = puterrno4(ENOTSUP);
3102                 goto out;
3103         }
3104 
3105         /*
3106          * If file system supports passing ACE mask to VOP_ACCESS then
3107          * check for ACE_READ_NAMED_ATTRS, otherwise do legacy checks
3108          */
3109 
3110         if (vfs_has_feature(cs->vp->v_vfsp, VFSFT_ACEMASKONACCESS))
3111                 error = VOP_ACCESS(cs->vp, ACE_READ_NAMED_ATTRS,
3112                     V_ACE_MASK, cs->cr, NULL);
3113         else
3114                 error = ((VOP_ACCESS(cs->vp, VREAD, 0, cs->cr, NULL) != 0) &&
3115                     (VOP_ACCESS(cs->vp, VWRITE, 0, cs->cr, NULL) != 0) &&
3116                     (VOP_ACCESS(cs->vp, VEXEC, 0, cs->cr, NULL) != 0));
3117 
3118         if (error) {
3119                 *cs->statusp = resp->status = puterrno4(EACCES);
3120                 goto out;
3121         }
3122 
3123         /*
3124          * The CREATE_XATTR_DIR VOP flag cannot be specified if
3125          * the file system is exported read-only -- regardless of
3126          * createdir flag.  Otherwise the attrdir would be created
3127          * (assuming server fs isn't mounted readonly locally).  If
3128          * VOP_LOOKUP returns ENOENT in this case, the error will
3129          * be translated into EROFS.  ENOSYS is mapped to ENOTSUP
3130          * because specfs has no VOP_LOOKUP op, so the macro would
3131          * return ENOSYS.  EINVAL is returned by all (current)
3132          * Solaris file system implementations when any of their
3133          * restrictions are violated (xattr(dir) can't have xattrdir).
3134          * Returning NOTSUPP is more appropriate in this case
3135          * because the object will never be able to have an attrdir.
3136          */
3137         if (args->createdir && ! (exp_ro = rdonly4(req, cs)))
3138                 lookup_flags |= CREATE_XATTR_DIR;
3139 
3140         error = VOP_LOOKUP(cs->vp, "", &avp, NULL, lookup_flags, NULL, cs->cr,
3141             NULL, NULL, NULL);
3142 
3143         if (error) {
3144                 if (error == ENOENT && args->createdir && exp_ro)
3145                         *cs->statusp = resp->status = puterrno4(EROFS);
3146                 else if (error == EINVAL || error == ENOSYS)
3147                         *cs->statusp = resp->status = puterrno4(ENOTSUP);
3148                 else
3149                         *cs->statusp = resp->status = puterrno4(error);
3150                 goto out;
3151         }
3152 
3153         ASSERT(avp->v_flag & V_XATTRDIR);
3154 
3155         error = makefh4(&cs->fh, avp, cs->exi);
3156 
3157         if (error) {
3158                 VN_RELE(avp);
3159                 *cs->statusp = resp->status = puterrno4(error);
3160                 goto out;
3161         }
3162 
3163         VN_RELE(cs->vp);
3164         cs->vp = avp;
3165 
3166         /*
3167          * There is no requirement for an attrdir fh flag
3168          * because the attrdir has a vnode flag to distinguish
3169          * it from regular (non-xattr) directories.  The
3170          * FH4_ATTRDIR flag is set for future sanity checks.
3171          */
3172         set_fh4_flag(&cs->fh, FH4_ATTRDIR);
3173         *cs->statusp = resp->status = NFS4_OK;
3174 
3175 out:
3176         DTRACE_NFSV4_2(op__openattr__done, struct compound_state *, cs,
3177             OPENATTR4res *, resp);
3178 }
3179 
3180 static int
3181 do_io(int direction, vnode_t *vp, struct uio *uio, int ioflag, cred_t *cred,
3182     caller_context_t *ct)
3183 {
3184         int error;
3185         int i;
3186         clock_t delaytime;
3187 
3188         delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
3189 
3190         /*
3191          * Don't block on mandatory locks. If this routine returns
3192          * EAGAIN, the caller should return NFS4ERR_LOCKED.
3193          */
3194         uio->uio_fmode = FNONBLOCK;
3195 
3196         for (i = 0; i < rfs4_maxlock_tries; i++) {
3197 
3198 
3199                 if (direction == FREAD) {
3200                         (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, ct);
3201                         error = VOP_READ(vp, uio, ioflag, cred, ct);
3202                         VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, ct);
3203                 } else {
3204                         (void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, ct);
3205                         error = VOP_WRITE(vp, uio, ioflag, cred, ct);
3206                         VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, ct);
3207                 }
3208 
3209                 if (error != EAGAIN)
3210                         break;
3211 
3212                 if (i < rfs4_maxlock_tries - 1) {
3213                         delay(delaytime);
3214                         delaytime *= 2;
3215                 }
3216         }
3217 
3218         return (error);
3219 }
3220 
3221 /* ARGSUSED */
3222 static void
3223 rfs4_op_read(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3224     struct compound_state *cs)
3225 {
3226         READ4args *args = &argop->nfs_argop4_u.opread;
3227         READ4res *resp = &resop->nfs_resop4_u.opread;
3228         int error;
3229         int verror;
3230         vnode_t *vp;
3231         struct vattr va;
3232         struct iovec iov, *iovp = NULL;
3233         int iovcnt;
3234         struct uio uio;
3235         u_offset_t offset;
3236         bool_t *deleg = &cs->deleg;
3237         nfsstat4 stat;
3238         int in_crit = 0;
3239         mblk_t *mp = NULL;
3240         int alloc_err = 0;
3241         int rdma_used = 0;
3242         int loaned_buffers;
3243         caller_context_t ct;
3244         struct uio *uiop;
3245 
3246         DTRACE_NFSV4_2(op__read__start, struct compound_state *, cs,
3247             READ4args, args);
3248 
3249         vp = cs->vp;
3250         if (vp == NULL) {
3251                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3252                 goto out;
3253         }
3254         if (cs->access == CS_ACCESS_DENIED) {
3255                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
3256                 goto out;
3257         }
3258 
3259         if ((stat = rfs4_check_stateid(FREAD, vp, &args->stateid, FALSE,
3260             deleg, TRUE, &ct)) != NFS4_OK) {
3261                 *cs->statusp = resp->status = stat;
3262                 goto out;
3263         }
3264 
3265         /*
3266          * Enter the critical region before calling VOP_RWLOCK
3267          * to avoid a deadlock with write requests.
3268          */
3269         if (nbl_need_check(vp)) {
3270                 nbl_start_crit(vp, RW_READER);
3271                 in_crit = 1;
3272                 if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0,
3273                     &ct)) {
3274                         *cs->statusp = resp->status = NFS4ERR_LOCKED;
3275                         goto out;
3276                 }
3277         }
3278 
3279         if (args->wlist) {
3280                 if (args->count > clist_len(args->wlist)) {
3281                         *cs->statusp = resp->status = NFS4ERR_INVAL;
3282                         goto out;
3283                 }
3284                 rdma_used = 1;
3285         }
3286 
3287         /* use loaned buffers for TCP */
3288         loaned_buffers = (nfs_loaned_buffers && !rdma_used) ? 1 : 0;
3289 
3290         va.va_mask = AT_MODE|AT_SIZE|AT_UID;
3291         verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3292 
3293         /*
3294          * If we can't get the attributes, then we can't do the
3295          * right access checking.  So, we'll fail the request.
3296          */
3297         if (verror) {
3298                 *cs->statusp = resp->status = puterrno4(verror);
3299                 goto out;
3300         }
3301 
3302         if (vp->v_type != VREG) {
3303                 *cs->statusp = resp->status =
3304                     ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
3305                 goto out;
3306         }
3307 
3308         if (crgetuid(cs->cr) != va.va_uid &&
3309             (error = VOP_ACCESS(vp, VREAD, 0, cs->cr, &ct)) &&
3310             (error = VOP_ACCESS(vp, VEXEC, 0, cs->cr, &ct))) {
3311                 *cs->statusp = resp->status = puterrno4(error);
3312                 goto out;
3313         }
3314 
3315         if (MANDLOCK(vp, va.va_mode)) { /* XXX - V4 supports mand locking */
3316                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
3317                 goto out;
3318         }
3319 
3320         offset = args->offset;
3321         if (offset >= va.va_size) {
3322                 *cs->statusp = resp->status = NFS4_OK;
3323                 resp->eof = TRUE;
3324                 resp->data_len = 0;
3325                 resp->data_val = NULL;
3326                 resp->mblk = NULL;
3327                 /* RDMA */
3328                 resp->wlist = args->wlist;
3329                 resp->wlist_len = resp->data_len;
3330                 *cs->statusp = resp->status = NFS4_OK;
3331                 if (resp->wlist)
3332                         clist_zero_len(resp->wlist);
3333                 goto out;
3334         }
3335 
3336         if (args->count == 0) {
3337                 *cs->statusp = resp->status = NFS4_OK;
3338                 resp->eof = FALSE;
3339                 resp->data_len = 0;
3340                 resp->data_val = NULL;
3341                 resp->mblk = NULL;
3342                 /* RDMA */
3343                 resp->wlist = args->wlist;
3344                 resp->wlist_len = resp->data_len;
3345                 if (resp->wlist)
3346                         clist_zero_len(resp->wlist);
3347                 goto out;
3348         }
3349 
3350         /*
3351          * Do not allocate memory more than maximum allowed
3352          * transfer size
3353          */
3354         if (args->count > rfs4_tsize(req))
3355                 args->count = rfs4_tsize(req);
3356 
3357         if (loaned_buffers) {
3358                 uiop = (uio_t *)rfs_setup_xuio(vp);
3359                 ASSERT(uiop != NULL);
3360                 uiop->uio_segflg = UIO_SYSSPACE;
3361                 uiop->uio_loffset = args->offset;
3362                 uiop->uio_resid = args->count;
3363 
3364                 /* Jump to do the read if successful */
3365                 if (!VOP_REQZCBUF(vp, UIO_READ, (xuio_t *)uiop, cs->cr, &ct)) {
3366                         /*
3367                          * Need to hold the vnode until after VOP_RETZCBUF()
3368                          * is called.
3369                          */
3370                         VN_HOLD(vp);
3371                         goto doio_read;
3372                 }
3373 
3374                 DTRACE_PROBE2(nfss__i__reqzcbuf_failed, int,
3375                     uiop->uio_loffset, int, uiop->uio_resid);
3376 
3377                 uiop->uio_extflg = 0;
3378 
3379                 /* failure to setup for zero copy */
3380                 rfs_free_xuio((void *)uiop);
3381                 loaned_buffers = 0;
3382         }
3383 
3384         /*
3385          * If returning data via RDMA Write, then grab the chunk list. If we
3386          * aren't returning READ data w/RDMA_WRITE, then grab a mblk.
3387          */
3388         if (rdma_used) {
3389                 mp = NULL;
3390                 (void) rdma_get_wchunk(req, &iov, args->wlist);
3391                 uio.uio_iov = &iov;
3392                 uio.uio_iovcnt = 1;
3393         } else {
3394                 /*
3395                  * mp will contain the data to be sent out in the read reply.
3396                  * It will be freed after the reply has been sent.
3397                  */
3398                 mp = rfs_read_alloc(args->count, &iovp, &iovcnt);
3399                 ASSERT(mp != NULL);
3400                 ASSERT(alloc_err == 0);
3401                 uio.uio_iov = iovp;
3402                 uio.uio_iovcnt = iovcnt;
3403         }
3404 
3405         uio.uio_segflg = UIO_SYSSPACE;
3406         uio.uio_extflg = UIO_COPY_CACHED;
3407         uio.uio_loffset = args->offset;
3408         uio.uio_resid = args->count;
3409         uiop = &uio;
3410 
3411 doio_read:
3412         error = do_io(FREAD, vp, uiop, 0, cs->cr, &ct);
3413 
3414         va.va_mask = AT_SIZE;
3415         verror = VOP_GETATTR(vp, &va, 0, cs->cr, &ct);
3416 
3417         if (error) {
3418                 if (mp)
3419                         freemsg(mp);
3420                 *cs->statusp = resp->status = puterrno4(error);
3421                 goto out;
3422         }
3423 
3424         /* make mblk using zc buffers */
3425         if (loaned_buffers) {
3426                 mp = uio_to_mblk(uiop);
3427                 ASSERT(mp != NULL);
3428         }
3429 
3430         *cs->statusp = resp->status = NFS4_OK;
3431 
3432         ASSERT(uiop->uio_resid >= 0);
3433         resp->data_len = args->count - uiop->uio_resid;
3434         if (mp) {
3435                 resp->data_val = (char *)mp->b_datap->db_base;
3436                 rfs_rndup_mblks(mp, resp->data_len, loaned_buffers);
3437         } else {
3438                 resp->data_val = (caddr_t)iov.iov_base;
3439         }
3440 
3441         resp->mblk = mp;
3442 
3443         if (!verror && offset + resp->data_len == va.va_size)
3444                 resp->eof = TRUE;
3445         else
3446                 resp->eof = FALSE;
3447 
3448         if (rdma_used) {
3449                 if (!rdma_setup_read_data4(args, resp)) {
3450                         *cs->statusp = resp->status = NFS4ERR_INVAL;
3451                 }
3452         } else {
3453                 resp->wlist = NULL;
3454         }
3455 
3456 out:
3457         if (in_crit)
3458                 nbl_end_crit(vp);
3459 
3460         if (iovp != NULL)
3461                 kmem_free(iovp, iovcnt * sizeof (struct iovec));
3462 
3463         DTRACE_NFSV4_2(op__read__done, struct compound_state *, cs,
3464             READ4res *, resp);
3465 }
3466 
3467 static void
3468 rfs4_op_read_free(nfs_resop4 *resop)
3469 {
3470         READ4res        *resp = &resop->nfs_resop4_u.opread;
3471 
3472         if (resp->status == NFS4_OK && resp->mblk != NULL) {
3473                 freemsg(resp->mblk);
3474                 resp->mblk = NULL;
3475                 resp->data_val = NULL;
3476                 resp->data_len = 0;
3477         }
3478 }
3479 
3480 static void
3481 rfs4_op_readdir_free(nfs_resop4 * resop)
3482 {
3483         READDIR4res    *resp = &resop->nfs_resop4_u.opreaddir;
3484 
3485         if (resp->status == NFS4_OK && resp->mblk != NULL) {
3486                 freeb(resp->mblk);
3487                 resp->mblk = NULL;
3488                 resp->data_len = 0;
3489         }
3490 }
3491 
3492 
3493 /* ARGSUSED */
3494 static void
3495 rfs4_op_putpubfh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
3496     struct compound_state *cs)
3497 {
3498         PUTPUBFH4res    *resp = &resop->nfs_resop4_u.opputpubfh;
3499         int             error;
3500         vnode_t         *vp;
3501         struct exportinfo *exi, *sav_exi;
3502         nfs_fh4_fmt_t   *fh_fmtp;
3503         nfs_export_t *ne = nfs_get_export();
3504 
3505         DTRACE_NFSV4_1(op__putpubfh__start, struct compound_state *, cs);
3506 
3507         if (cs->vp) {
3508                 VN_RELE(cs->vp);
3509                 cs->vp = NULL;
3510         }
3511 
3512         if (cs->cr)
3513                 crfree(cs->cr);
3514 
3515         cs->cr = crdup(cs->basecr);
3516 
3517         vp = ne->exi_public->exi_vp;
3518         if (vp == NULL) {
3519                 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3520                 goto out;
3521         }
3522 
3523         error = makefh4(&cs->fh, vp, ne->exi_public);
3524         if (error != 0) {
3525                 *cs->statusp = resp->status = puterrno4(error);
3526                 goto out;
3527         }
3528         sav_exi = cs->exi;
3529         if (ne->exi_public == ne->exi_root) {
3530                 /*
3531                  * No filesystem is actually shared public, so we default
3532                  * to exi_root. In this case, we must check whether root
3533                  * is exported.
3534                  */
3535                 fh_fmtp = (nfs_fh4_fmt_t *)cs->fh.nfs_fh4_val;
3536 
3537                 /*
3538                  * if root filesystem is exported, the exportinfo struct that we
3539                  * should use is what checkexport4 returns, because root_exi is
3540                  * actually a mostly empty struct.
3541                  */
3542                 exi = checkexport4(&fh_fmtp->fh4_fsid,
3543                     (fid_t *)&fh_fmtp->fh4_xlen, NULL);
3544                 cs->exi = ((exi != NULL) ? exi : ne->exi_public);
3545         } else {
3546                 /*
3547                  * it's a properly shared filesystem
3548                  */
3549                 cs->exi = ne->exi_public;
3550         }
3551 
3552         if (is_system_labeled()) {
3553                 bslabel_t *clabel;
3554 
3555                 ASSERT(req->rq_label != NULL);
3556                 clabel = req->rq_label;
3557                 DTRACE_PROBE2(tx__rfs4__log__info__opputpubfh__clabel, char *,
3558                     "got client label from request(1)",
3559                     struct svc_req *, req);
3560                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
3561                         if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
3562                             cs->exi)) {
3563                                 *cs->statusp = resp->status =
3564                                     NFS4ERR_SERVERFAULT;
3565                                 goto out;
3566                         }
3567                 }
3568         }
3569 
3570         VN_HOLD(vp);
3571         cs->vp = vp;
3572 
3573         if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3574                 VN_RELE(cs->vp);
3575                 cs->vp = NULL;
3576                 cs->exi = sav_exi;
3577                 goto out;
3578         }
3579 
3580         *cs->statusp = resp->status = NFS4_OK;
3581 out:
3582         DTRACE_NFSV4_2(op__putpubfh__done, struct compound_state *, cs,
3583             PUTPUBFH4res *, resp);
3584 }
3585 
3586 /*
3587  * XXX - issue with put*fh operations. Suppose /export/home is exported.
3588  * Suppose an NFS client goes to mount /export/home/joe. If /export, home,
3589  * or joe have restrictive search permissions, then we shouldn't let
3590  * the client get a file handle. This is easy to enforce. However, we
3591  * don't know what security flavor should be used until we resolve the
3592  * path name. Another complication is uid mapping. If root is
3593  * the user, then it will be mapped to the anonymous user by default,
3594  * but we won't know that till we've resolved the path name. And we won't
3595  * know what the anonymous user is.
3596  * Luckily, SECINFO is specified to take a full filename.
3597  * So what we will have to in rfs4_op_lookup is check that flavor of
3598  * the target object matches that of the request, and if root was the
3599  * caller, check for the root= and anon= options, and if necessary,
3600  * repeat the lookup using the right cred_t. But that's not done yet.
3601  */
3602 /* ARGSUSED */
3603 static void
3604 rfs4_op_putfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3605     struct compound_state *cs)
3606 {
3607         PUTFH4args *args = &argop->nfs_argop4_u.opputfh;
3608         PUTFH4res *resp = &resop->nfs_resop4_u.opputfh;
3609         nfs_fh4_fmt_t *fh_fmtp;
3610 
3611         DTRACE_NFSV4_2(op__putfh__start, struct compound_state *, cs,
3612             PUTFH4args *, args);
3613 
3614         if (cs->vp) {
3615                 VN_RELE(cs->vp);
3616                 cs->vp = NULL;
3617         }
3618 
3619         if (cs->cr) {
3620                 crfree(cs->cr);
3621                 cs->cr = NULL;
3622         }
3623 
3624 
3625         if (args->object.nfs_fh4_len < NFS_FH4_LEN) {
3626                 *cs->statusp = resp->status = NFS4ERR_BADHANDLE;
3627                 goto out;
3628         }
3629 
3630         fh_fmtp = (nfs_fh4_fmt_t *)args->object.nfs_fh4_val;
3631         cs->exi = checkexport4(&fh_fmtp->fh4_fsid, (fid_t *)&fh_fmtp->fh4_xlen,
3632             NULL);
3633 
3634         if (cs->exi == NULL) {
3635                 *cs->statusp = resp->status = NFS4ERR_STALE;
3636                 goto out;
3637         }
3638 
3639         cs->cr = crdup(cs->basecr);
3640 
3641         ASSERT(cs->cr != NULL);
3642 
3643         if (! (cs->vp = nfs4_fhtovp(&args->object, cs->exi, &resp->status))) {
3644                 *cs->statusp = resp->status;
3645                 goto out;
3646         }
3647 
3648         if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3649                 VN_RELE(cs->vp);
3650                 cs->vp = NULL;
3651                 goto out;
3652         }
3653 
3654         nfs_fh4_copy(&args->object, &cs->fh);
3655         *cs->statusp = resp->status = NFS4_OK;
3656         cs->deleg = FALSE;
3657 
3658 out:
3659         DTRACE_NFSV4_2(op__putfh__done, struct compound_state *, cs,
3660             PUTFH4res *, resp);
3661 }
3662 
3663 /* ARGSUSED */
3664 static void
3665 rfs4_op_putrootfh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3666     struct compound_state *cs)
3667 {
3668         PUTROOTFH4res *resp = &resop->nfs_resop4_u.opputrootfh;
3669         int error;
3670         fid_t fid;
3671         struct exportinfo *exi, *sav_exi;
3672 
3673         DTRACE_NFSV4_1(op__putrootfh__start, struct compound_state *, cs);
3674 
3675         if (cs->vp) {
3676                 VN_RELE(cs->vp);
3677                 cs->vp = NULL;
3678         }
3679 
3680         if (cs->cr)
3681                 crfree(cs->cr);
3682 
3683         cs->cr = crdup(cs->basecr);
3684 
3685         /*
3686          * Using rootdir, the system root vnode,
3687          * get its fid.
3688          */
3689         bzero(&fid, sizeof (fid));
3690         fid.fid_len = MAXFIDSZ;
3691         error = vop_fid_pseudo(ZONE_ROOTVP(), &fid);
3692         if (error != 0) {
3693                 *cs->statusp = resp->status = puterrno4(error);
3694                 goto out;
3695         }
3696 
3697         /*
3698          * Then use the root fsid & fid it to find out if it's exported
3699          *
3700          * If the server root isn't exported directly, then
3701          * it should at least be a pseudo export based on
3702          * one or more exports further down in the server's
3703          * file tree.
3704          */
3705         exi = checkexport4(&ZONE_ROOTVP()->v_vfsp->vfs_fsid, &fid, NULL);
3706         if (exi == NULL || exi->exi_export.ex_flags & EX_PUBLIC) {
3707                 NFS4_DEBUG(rfs4_debug,
3708                     (CE_WARN, "rfs4_op_putrootfh: export check failure"));
3709                 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
3710                 goto out;
3711         }
3712 
3713         /*
3714          * Now make a filehandle based on the root
3715          * export and root vnode.
3716          */
3717         error = makefh4(&cs->fh, ZONE_ROOTVP(), exi);
3718         if (error != 0) {
3719                 *cs->statusp = resp->status = puterrno4(error);
3720                 goto out;
3721         }
3722 
3723         sav_exi = cs->exi;
3724         cs->exi = exi;
3725 
3726         VN_HOLD(ZONE_ROOTVP());
3727         cs->vp = ZONE_ROOTVP();
3728 
3729         if ((resp->status = call_checkauth4(cs, req)) != NFS4_OK) {
3730                 VN_RELE(cs->vp);
3731                 cs->vp = NULL;
3732                 cs->exi = sav_exi;
3733                 goto out;
3734         }
3735 
3736         *cs->statusp = resp->status = NFS4_OK;
3737         cs->deleg = FALSE;
3738 out:
3739         DTRACE_NFSV4_2(op__putrootfh__done, struct compound_state *, cs,
3740             PUTROOTFH4res *, resp);
3741 }
3742 
3743 /*
3744  * readlink: args: CURRENT_FH.
3745  *      res: status. If success - CURRENT_FH unchanged, return linktext.
3746  */
3747 
3748 /* ARGSUSED */
3749 static void
3750 rfs4_op_readlink(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
3751     struct compound_state *cs)
3752 {
3753         READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3754         int error;
3755         vnode_t *vp;
3756         struct iovec iov;
3757         struct vattr va;
3758         struct uio uio;
3759         char *data;
3760         struct sockaddr *ca;
3761         char *name = NULL;
3762         int is_referral;
3763 
3764         DTRACE_NFSV4_1(op__readlink__start, struct compound_state *, cs);
3765 
3766         /* CURRENT_FH: directory */
3767         vp = cs->vp;
3768         if (vp == NULL) {
3769                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
3770                 goto out;
3771         }
3772 
3773         if (cs->access == CS_ACCESS_DENIED) {
3774                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
3775                 goto out;
3776         }
3777 
3778         /* Is it a referral? */
3779         if (vn_is_nfs_reparse(vp, cs->cr) && client_is_downrev(req)) {
3780 
3781                 is_referral = 1;
3782 
3783         } else {
3784 
3785                 is_referral = 0;
3786 
3787                 if (vp->v_type == VDIR) {
3788                         *cs->statusp = resp->status = NFS4ERR_ISDIR;
3789                         goto out;
3790                 }
3791 
3792                 if (vp->v_type != VLNK) {
3793                         *cs->statusp = resp->status = NFS4ERR_INVAL;
3794                         goto out;
3795                 }
3796 
3797         }
3798 
3799         va.va_mask = AT_MODE;
3800         error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
3801         if (error) {
3802                 *cs->statusp = resp->status = puterrno4(error);
3803                 goto out;
3804         }
3805 
3806         if (MANDLOCK(vp, va.va_mode)) {
3807                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
3808                 goto out;
3809         }
3810 
3811         data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
3812 
3813         if (is_referral) {
3814                 char *s;
3815                 size_t strsz;
3816 
3817                 /* Get an artificial symlink based on a referral */
3818                 s = build_symlink(vp, cs->cr, &strsz);
3819                 global_svstat_ptr[4][NFS_REFERLINKS].value.ui64++;
3820                 DTRACE_PROBE2(nfs4serv__func__referral__reflink,
3821                     vnode_t *, vp, char *, s);
3822                 if (s == NULL)
3823                         error = EINVAL;
3824                 else {
3825                         error = 0;
3826                         (void) strlcpy(data, s, MAXPATHLEN + 1);
3827                         kmem_free(s, strsz);
3828                 }
3829 
3830         } else {
3831 
3832                 iov.iov_base = data;
3833                 iov.iov_len = MAXPATHLEN;
3834                 uio.uio_iov = &iov;
3835                 uio.uio_iovcnt = 1;
3836                 uio.uio_segflg = UIO_SYSSPACE;
3837                 uio.uio_extflg = UIO_COPY_CACHED;
3838                 uio.uio_loffset = 0;
3839                 uio.uio_resid = MAXPATHLEN;
3840 
3841                 error = VOP_READLINK(vp, &uio, cs->cr, NULL);
3842 
3843                 if (!error)
3844                         *(data + MAXPATHLEN - uio.uio_resid) = '\0';
3845         }
3846 
3847         if (error) {
3848                 kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3849                 *cs->statusp = resp->status = puterrno4(error);
3850                 goto out;
3851         }
3852 
3853         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
3854         name = nfscmd_convname(ca, cs->exi, data, NFSCMD_CONV_OUTBOUND,
3855             MAXPATHLEN  + 1);
3856 
3857         if (name == NULL) {
3858                 /*
3859                  * Even though the conversion failed, we return
3860                  * something. We just don't translate it.
3861                  */
3862                 name = data;
3863         }
3864 
3865         /*
3866          * treat link name as data
3867          */
3868         (void) str_to_utf8(name, (utf8string *)&resp->link);
3869 
3870         if (name != data)
3871                 kmem_free(name, MAXPATHLEN + 1);
3872         kmem_free((caddr_t)data, (uint_t)MAXPATHLEN + 1);
3873         *cs->statusp = resp->status = NFS4_OK;
3874 
3875 out:
3876         DTRACE_NFSV4_2(op__readlink__done, struct compound_state *, cs,
3877             READLINK4res *, resp);
3878 }
3879 
3880 static void
3881 rfs4_op_readlink_free(nfs_resop4 *resop)
3882 {
3883         READLINK4res *resp = &resop->nfs_resop4_u.opreadlink;
3884         utf8string *symlink = (utf8string *)&resp->link;
3885 
3886         if (symlink->utf8string_val) {
3887                 UTF8STRING_FREE(*symlink)
3888         }
3889 }
3890 
3891 /*
3892  * release_lockowner:
3893  *      Release any state associated with the supplied
3894  *      lockowner. Note if any lo_state is holding locks we will not
3895  *      rele that lo_state and thus the lockowner will not be destroyed.
3896  *      A client using lock after the lock owner stateid has been released
3897  *      will suffer the consequence of NFS4ERR_BAD_STATEID and would have
3898  *      to reissue the lock with new_lock_owner set to TRUE.
3899  *      args: lock_owner
3900  *      res:  status
3901  */
3902 /* ARGSUSED */
3903 static void
3904 rfs4_op_release_lockowner(nfs_argop4 *argop, nfs_resop4 *resop,
3905     struct svc_req *req, struct compound_state *cs)
3906 {
3907         RELEASE_LOCKOWNER4args *ap = &argop->nfs_argop4_u.oprelease_lockowner;
3908         RELEASE_LOCKOWNER4res *resp = &resop->nfs_resop4_u.oprelease_lockowner;
3909         rfs4_lockowner_t *lo;
3910         rfs4_openowner_t *oo;
3911         rfs4_state_t *sp;
3912         rfs4_lo_state_t *lsp;
3913         rfs4_client_t *cp;
3914         bool_t create = FALSE;
3915         locklist_t *llist;
3916         sysid_t sysid;
3917 
3918         DTRACE_NFSV4_2(op__release__lockowner__start, struct compound_state *,
3919             cs, RELEASE_LOCKOWNER4args *, ap);
3920 
3921         /* Make sure there is a clientid around for this request */
3922         cp = rfs4_findclient_by_id(ap->lock_owner.clientid, FALSE);
3923 
3924         if (cp == NULL) {
3925                 *cs->statusp = resp->status =
3926                     rfs4_check_clientid(&ap->lock_owner.clientid, 0);
3927                 goto out;
3928         }
3929         rfs4_client_rele(cp);
3930 
3931         lo = rfs4_findlockowner(&ap->lock_owner, &create);
3932         if (lo == NULL) {
3933                 *cs->statusp = resp->status = NFS4_OK;
3934                 goto out;
3935         }
3936         ASSERT(lo->rl_client != NULL);
3937 
3938         /*
3939          * Check for EXPIRED client. If so will reap state with in a lease
3940          * period or on next set_clientid_confirm step
3941          */
3942         if (rfs4_lease_expired(lo->rl_client)) {
3943                 rfs4_lockowner_rele(lo);
3944                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
3945                 goto out;
3946         }
3947 
3948         /*
3949          * If no sysid has been assigned, then no locks exist; just return.
3950          */
3951         rfs4_dbe_lock(lo->rl_client->rc_dbe);
3952         if (lo->rl_client->rc_sysidt == LM_NOSYSID) {
3953                 rfs4_lockowner_rele(lo);
3954                 rfs4_dbe_unlock(lo->rl_client->rc_dbe);
3955                 goto out;
3956         }
3957 
3958         sysid = lo->rl_client->rc_sysidt;
3959         rfs4_dbe_unlock(lo->rl_client->rc_dbe);
3960 
3961         /*
3962          * Mark the lockowner invalid.
3963          */
3964         rfs4_dbe_hide(lo->rl_dbe);
3965 
3966         /*
3967          * sysid-pid pair should now not be used since the lockowner is
3968          * invalid. If the client were to instantiate the lockowner again
3969          * it would be assigned a new pid. Thus we can get the list of
3970          * current locks.
3971          */
3972 
3973         llist = flk_get_active_locks(sysid, lo->rl_pid);
3974         /* If we are still holding locks fail */
3975         if (llist != NULL) {
3976 
3977                 *cs->statusp = resp->status = NFS4ERR_LOCKS_HELD;
3978 
3979                 flk_free_locklist(llist);
3980                 /*
3981                  * We need to unhide the lockowner so the client can
3982                  * try it again. The bad thing here is if the client
3983                  * has a logic error that took it here in the first place
3984                  * they probably have lost accounting of the locks that it
3985                  * is holding. So we may have dangling state until the
3986                  * open owner state is reaped via close. One scenario
3987                  * that could possibly occur is that the client has
3988                  * sent the unlock request(s) in separate threads
3989                  * and has not waited for the replies before sending the
3990                  * RELEASE_LOCKOWNER request. Presumably, it would expect
3991                  * and deal appropriately with NFS4ERR_LOCKS_HELD, by
3992                  * reissuing the request.
3993                  */
3994                 rfs4_dbe_unhide(lo->rl_dbe);
3995                 rfs4_lockowner_rele(lo);
3996                 goto out;
3997         }
3998 
3999         /*
4000          * For the corresponding client we need to check each open
4001          * owner for any opens that have lockowner state associated
4002          * with this lockowner.
4003          */
4004 
4005         rfs4_dbe_lock(lo->rl_client->rc_dbe);
4006         for (oo = list_head(&lo->rl_client->rc_openownerlist); oo != NULL;
4007             oo = list_next(&lo->rl_client->rc_openownerlist, oo)) {
4008 
4009                 rfs4_dbe_lock(oo->ro_dbe);
4010                 for (sp = list_head(&oo->ro_statelist); sp != NULL;
4011                     sp = list_next(&oo->ro_statelist, sp)) {
4012 
4013                         rfs4_dbe_lock(sp->rs_dbe);
4014                         for (lsp = list_head(&sp->rs_lostatelist);
4015                             lsp != NULL;
4016                             lsp = list_next(&sp->rs_lostatelist, lsp)) {
4017                                 if (lsp->rls_locker == lo) {
4018                                         rfs4_dbe_lock(lsp->rls_dbe);
4019                                         rfs4_dbe_invalidate(lsp->rls_dbe);
4020                                         rfs4_dbe_unlock(lsp->rls_dbe);
4021                                 }
4022                         }
4023                         rfs4_dbe_unlock(sp->rs_dbe);
4024                 }
4025                 rfs4_dbe_unlock(oo->ro_dbe);
4026         }
4027         rfs4_dbe_unlock(lo->rl_client->rc_dbe);
4028 
4029         rfs4_lockowner_rele(lo);
4030 
4031         *cs->statusp = resp->status = NFS4_OK;
4032 
4033 out:
4034         DTRACE_NFSV4_2(op__release__lockowner__done, struct compound_state *,
4035             cs, RELEASE_LOCKOWNER4res *, resp);
4036 }
4037 
4038 /*
4039  * short utility function to lookup a file and recall the delegation
4040  */
4041 static rfs4_file_t *
4042 rfs4_lookup_and_findfile(vnode_t *dvp, char *nm, vnode_t **vpp,
4043     int *lkup_error, cred_t *cr)
4044 {
4045         vnode_t *vp;
4046         rfs4_file_t *fp = NULL;
4047         bool_t fcreate = FALSE;
4048         int error;
4049 
4050         if (vpp)
4051                 *vpp = NULL;
4052 
4053         if ((error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cr, NULL, NULL,
4054             NULL)) == 0) {
4055                 if (vp->v_type == VREG)
4056                         fp = rfs4_findfile(vp, NULL, &fcreate);
4057                 if (vpp)
4058                         *vpp = vp;
4059                 else
4060                         VN_RELE(vp);
4061         }
4062 
4063         if (lkup_error)
4064                 *lkup_error = error;
4065 
4066         return (fp);
4067 }
4068 
4069 /*
4070  * remove: args: CURRENT_FH: directory; name.
4071  *      res: status. If success - CURRENT_FH unchanged, return change_info
4072  *              for directory.
4073  */
4074 /* ARGSUSED */
4075 static void
4076 rfs4_op_remove(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4077     struct compound_state *cs)
4078 {
4079         REMOVE4args *args = &argop->nfs_argop4_u.opremove;
4080         REMOVE4res *resp = &resop->nfs_resop4_u.opremove;
4081         int error;
4082         vnode_t *dvp, *vp;
4083         struct vattr bdva, idva, adva;
4084         char *nm;
4085         uint_t len;
4086         rfs4_file_t *fp;
4087         int in_crit = 0;
4088         bslabel_t *clabel;
4089         struct sockaddr *ca;
4090         char *name = NULL;
4091         nfsstat4 status;
4092 
4093         DTRACE_NFSV4_2(op__remove__start, struct compound_state *, cs,
4094             REMOVE4args *, args);
4095 
4096         /* CURRENT_FH: directory */
4097         dvp = cs->vp;
4098         if (dvp == NULL) {
4099                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4100                 goto out;
4101         }
4102 
4103         if (cs->access == CS_ACCESS_DENIED) {
4104                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
4105                 goto out;
4106         }
4107 
4108         /*
4109          * If there is an unshared filesystem mounted on this vnode,
4110          * Do not allow to remove anything in this directory.
4111          */
4112         if (vn_ismntpt(dvp)) {
4113                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
4114                 goto out;
4115         }
4116 
4117         if (dvp->v_type != VDIR) {
4118                 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
4119                 goto out;
4120         }
4121 
4122         status = utf8_dir_verify(&args->target);
4123         if (status != NFS4_OK) {
4124                 *cs->statusp = resp->status = status;
4125                 goto out;
4126         }
4127 
4128         /*
4129          * Lookup the file so that we can check if it's a directory
4130          */
4131         nm = utf8_to_fn(&args->target, &len, NULL);
4132         if (nm == NULL) {
4133                 *cs->statusp = resp->status = NFS4ERR_INVAL;
4134                 goto out;
4135         }
4136 
4137         if (len > MAXNAMELEN) {
4138                 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
4139                 kmem_free(nm, len);
4140                 goto out;
4141         }
4142 
4143         if (rdonly4(req, cs)) {
4144                 *cs->statusp = resp->status = NFS4ERR_ROFS;
4145                 kmem_free(nm, len);
4146                 goto out;
4147         }
4148 
4149         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
4150         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
4151             MAXPATHLEN  + 1);
4152 
4153         if (name == NULL) {
4154                 *cs->statusp = resp->status = NFS4ERR_INVAL;
4155                 kmem_free(nm, len);
4156                 goto out;
4157         }
4158 
4159         /*
4160          * Lookup the file to determine type and while we are see if
4161          * there is a file struct around and check for delegation.
4162          * We don't need to acquire va_seq before this lookup, if
4163          * it causes an update, cinfo.before will not match, which will
4164          * trigger a cache flush even if atomic is TRUE.
4165          */
4166         if (fp = rfs4_lookup_and_findfile(dvp, name, &vp, &error, cs->cr)) {
4167                 if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
4168                     NULL)) {
4169                         VN_RELE(vp);
4170                         rfs4_file_rele(fp);
4171                         *cs->statusp = resp->status = NFS4ERR_DELAY;
4172                         if (nm != name)
4173                                 kmem_free(name, MAXPATHLEN + 1);
4174                         kmem_free(nm, len);
4175                         goto out;
4176                 }
4177         }
4178 
4179         /* Didn't find anything to remove */
4180         if (vp == NULL) {
4181                 *cs->statusp = resp->status = error;
4182                 if (nm != name)
4183                         kmem_free(name, MAXPATHLEN + 1);
4184                 kmem_free(nm, len);
4185                 goto out;
4186         }
4187 
4188         if (nbl_need_check(vp)) {
4189                 nbl_start_crit(vp, RW_READER);
4190                 in_crit = 1;
4191                 if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
4192                         *cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4193                         if (nm != name)
4194                                 kmem_free(name, MAXPATHLEN + 1);
4195                         kmem_free(nm, len);
4196                         nbl_end_crit(vp);
4197                         VN_RELE(vp);
4198                         if (fp) {
4199                                 rfs4_clear_dont_grant(fp);
4200                                 rfs4_file_rele(fp);
4201                         }
4202                         goto out;
4203                 }
4204         }
4205 
4206         /* check label before allowing removal */
4207         if (is_system_labeled()) {
4208                 ASSERT(req->rq_label != NULL);
4209                 clabel = req->rq_label;
4210                 DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
4211                     "got client label from request(1)",
4212                     struct svc_req *, req);
4213                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
4214                         if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK,
4215                             cs->exi)) {
4216                                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
4217                                 if (name != nm)
4218                                         kmem_free(name, MAXPATHLEN + 1);
4219                                 kmem_free(nm, len);
4220                                 if (in_crit)
4221                                         nbl_end_crit(vp);
4222                                 VN_RELE(vp);
4223                                 if (fp) {
4224                                         rfs4_clear_dont_grant(fp);
4225                                         rfs4_file_rele(fp);
4226                                 }
4227                                 goto out;
4228                         }
4229                 }
4230         }
4231 
4232         /* Get dir "before" change value */
4233         bdva.va_mask = AT_CTIME|AT_SEQ;
4234         error = VOP_GETATTR(dvp, &bdva, 0, cs->cr, NULL);
4235         if (error) {
4236                 *cs->statusp = resp->status = puterrno4(error);
4237                 if (nm != name)
4238                         kmem_free(name, MAXPATHLEN + 1);
4239                 kmem_free(nm, len);
4240                 if (in_crit)
4241                         nbl_end_crit(vp);
4242                 VN_RELE(vp);
4243                 if (fp) {
4244                         rfs4_clear_dont_grant(fp);
4245                         rfs4_file_rele(fp);
4246                 }
4247                 goto out;
4248         }
4249         NFS4_SET_FATTR4_CHANGE(resp->cinfo.before, bdva.va_ctime)
4250 
4251         /* Actually do the REMOVE operation */
4252         if (vp->v_type == VDIR) {
4253                 /*
4254                  * Can't remove a directory that has a mounted-on filesystem.
4255                  */
4256                 if (vn_ismntpt(vp)) {
4257                         error = EACCES;
4258                 } else {
4259                         /*
4260                          * System V defines rmdir to return EEXIST,
4261                          * not ENOTEMPTY, if the directory is not
4262                          * empty.  A System V NFS server needs to map
4263                          * NFS4ERR_EXIST to NFS4ERR_NOTEMPTY to
4264                          * transmit over the wire.
4265                          */
4266                         if ((error = VOP_RMDIR(dvp, name, ZONE_ROOTVP(), cs->cr,
4267                             NULL, 0)) == EEXIST)
4268                                 error = ENOTEMPTY;
4269                 }
4270         } else {
4271                 if ((error = VOP_REMOVE(dvp, name, cs->cr, NULL, 0)) == 0 &&
4272                     fp != NULL) {
4273                         struct vattr va;
4274                         vnode_t *tvp;
4275 
4276                         rfs4_dbe_lock(fp->rf_dbe);
4277                         tvp = fp->rf_vp;
4278                         if (tvp)
4279                                 VN_HOLD(tvp);
4280                         rfs4_dbe_unlock(fp->rf_dbe);
4281 
4282                         if (tvp) {
4283                                 /*
4284                                  * This is va_seq safe because we are not
4285                                  * manipulating dvp.
4286                                  */
4287                                 va.va_mask = AT_NLINK;
4288                                 if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4289                                     va.va_nlink == 0) {
4290                                         /* Remove state on file remove */
4291                                         if (in_crit) {
4292                                                 nbl_end_crit(vp);
4293                                                 in_crit = 0;
4294                                         }
4295                                         rfs4_close_all_state(fp);
4296                                 }
4297                                 VN_RELE(tvp);
4298                         }
4299                 }
4300         }
4301 
4302         if (in_crit)
4303                 nbl_end_crit(vp);
4304         VN_RELE(vp);
4305 
4306         if (fp) {
4307                 rfs4_clear_dont_grant(fp);
4308                 rfs4_file_rele(fp);
4309         }
4310         if (nm != name)
4311                 kmem_free(name, MAXPATHLEN + 1);
4312         kmem_free(nm, len);
4313 
4314         if (error) {
4315                 *cs->statusp = resp->status = puterrno4(error);
4316                 goto out;
4317         }
4318 
4319         /*
4320          * Get the initial "after" sequence number, if it fails, set to zero
4321          */
4322         idva.va_mask = AT_SEQ;
4323         if (VOP_GETATTR(dvp, &idva, 0, cs->cr, NULL))
4324                 idva.va_seq = 0;
4325 
4326         /*
4327          * Force modified data and metadata out to stable storage.
4328          */
4329         (void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
4330 
4331         /*
4332          * Get "after" change value, if it fails, simply return the
4333          * before value.
4334          */
4335         adva.va_mask = AT_CTIME|AT_SEQ;
4336         if (VOP_GETATTR(dvp, &adva, 0, cs->cr, NULL)) {
4337                 adva.va_ctime = bdva.va_ctime;
4338                 adva.va_seq = 0;
4339         }
4340 
4341         NFS4_SET_FATTR4_CHANGE(resp->cinfo.after, adva.va_ctime)
4342 
4343         /*
4344          * The cinfo.atomic = TRUE only if we have
4345          * non-zero va_seq's, and it has incremented by exactly one
4346          * during the VOP_REMOVE/RMDIR and it didn't change during
4347          * the VOP_FSYNC.
4348          */
4349         if (bdva.va_seq && idva.va_seq && adva.va_seq &&
4350             idva.va_seq == (bdva.va_seq + 1) && idva.va_seq == adva.va_seq)
4351                 resp->cinfo.atomic = TRUE;
4352         else
4353                 resp->cinfo.atomic = FALSE;
4354 
4355         *cs->statusp = resp->status = NFS4_OK;
4356 
4357 out:
4358         DTRACE_NFSV4_2(op__remove__done, struct compound_state *, cs,
4359             REMOVE4res *, resp);
4360 }
4361 
4362 /*
4363  * rename: args: SAVED_FH: from directory, CURRENT_FH: target directory,
4364  *              oldname and newname.
4365  *      res: status. If success - CURRENT_FH unchanged, return change_info
4366  *              for both from and target directories.
4367  */
4368 /* ARGSUSED */
4369 static void
4370 rfs4_op_rename(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4371     struct compound_state *cs)
4372 {
4373         RENAME4args *args = &argop->nfs_argop4_u.oprename;
4374         RENAME4res *resp = &resop->nfs_resop4_u.oprename;
4375         int error;
4376         vnode_t *odvp;
4377         vnode_t *ndvp;
4378         vnode_t *srcvp, *targvp, *tvp;
4379         struct vattr obdva, oidva, oadva;
4380         struct vattr nbdva, nidva, nadva;
4381         char *onm, *nnm;
4382         uint_t olen, nlen;
4383         rfs4_file_t *fp, *sfp;
4384         int in_crit_src, in_crit_targ;
4385         int fp_rele_grant_hold, sfp_rele_grant_hold;
4386         int unlinked;
4387         bslabel_t *clabel;
4388         struct sockaddr *ca;
4389         char *converted_onm = NULL;
4390         char *converted_nnm = NULL;
4391         nfsstat4 status;
4392 
4393         DTRACE_NFSV4_2(op__rename__start, struct compound_state *, cs,
4394             RENAME4args *, args);
4395 
4396         fp = sfp = NULL;
4397         srcvp = targvp = tvp = NULL;
4398         in_crit_src = in_crit_targ = 0;
4399         fp_rele_grant_hold = sfp_rele_grant_hold = 0;
4400         unlinked = 0;
4401 
4402         /* CURRENT_FH: target directory */
4403         ndvp = cs->vp;
4404         if (ndvp == NULL) {
4405                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4406                 goto out;
4407         }
4408 
4409         /* SAVED_FH: from directory */
4410         odvp = cs->saved_vp;
4411         if (odvp == NULL) {
4412                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4413                 goto out;
4414         }
4415 
4416         if (cs->access == CS_ACCESS_DENIED) {
4417                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
4418                 goto out;
4419         }
4420 
4421         /*
4422          * If there is an unshared filesystem mounted on this vnode,
4423          * do not allow to rename objects in this directory.
4424          */
4425         if (vn_ismntpt(odvp)) {
4426                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
4427                 goto out;
4428         }
4429 
4430         /*
4431          * If there is an unshared filesystem mounted on this vnode,
4432          * do not allow to rename to this directory.
4433          */
4434         if (vn_ismntpt(ndvp)) {
4435                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
4436                 goto out;
4437         }
4438 
4439         if (odvp->v_type != VDIR || ndvp->v_type != VDIR) {
4440                 *cs->statusp = resp->status = NFS4ERR_NOTDIR;
4441                 goto out;
4442         }
4443 
4444         if (cs->saved_exi != cs->exi) {
4445                 *cs->statusp = resp->status = NFS4ERR_XDEV;
4446                 goto out;
4447         }
4448 
4449         status = utf8_dir_verify(&args->oldname);
4450         if (status != NFS4_OK) {
4451                 *cs->statusp = resp->status = status;
4452                 goto out;
4453         }
4454 
4455         status = utf8_dir_verify(&args->newname);
4456         if (status != NFS4_OK) {
4457                 *cs->statusp = resp->status = status;
4458                 goto out;
4459         }
4460 
4461         onm = utf8_to_fn(&args->oldname, &olen, NULL);
4462         if (onm == NULL) {
4463                 *cs->statusp = resp->status = NFS4ERR_INVAL;
4464                 goto out;
4465         }
4466         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
4467         nlen = MAXPATHLEN + 1;
4468         converted_onm = nfscmd_convname(ca, cs->exi, onm, NFSCMD_CONV_INBOUND,
4469             nlen);
4470 
4471         if (converted_onm == NULL) {
4472                 *cs->statusp = resp->status = NFS4ERR_INVAL;
4473                 kmem_free(onm, olen);
4474                 goto out;
4475         }
4476 
4477         nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4478         if (nnm == NULL) {
4479                 *cs->statusp = resp->status = NFS4ERR_INVAL;
4480                 if (onm != converted_onm)
4481                         kmem_free(converted_onm, MAXPATHLEN + 1);
4482                 kmem_free(onm, olen);
4483                 goto out;
4484         }
4485         converted_nnm = nfscmd_convname(ca, cs->exi, nnm, NFSCMD_CONV_INBOUND,
4486             MAXPATHLEN  + 1);
4487 
4488         if (converted_nnm == NULL) {
4489                 *cs->statusp = resp->status = NFS4ERR_INVAL;
4490                 kmem_free(nnm, nlen);
4491                 nnm = NULL;
4492                 if (onm != converted_onm)
4493                         kmem_free(converted_onm, MAXPATHLEN + 1);
4494                 kmem_free(onm, olen);
4495                 goto out;
4496         }
4497 
4498 
4499         if (olen > MAXNAMELEN || nlen > MAXNAMELEN) {
4500                 *cs->statusp = resp->status = NFS4ERR_NAMETOOLONG;
4501                 kmem_free(onm, olen);
4502                 kmem_free(nnm, nlen);
4503                 goto out;
4504         }
4505 
4506 
4507         if (rdonly4(req, cs)) {
4508                 *cs->statusp = resp->status = NFS4ERR_ROFS;
4509                 if (onm != converted_onm)
4510                         kmem_free(converted_onm, MAXPATHLEN + 1);
4511                 kmem_free(onm, olen);
4512                 if (nnm != converted_nnm)
4513                         kmem_free(converted_nnm, MAXPATHLEN + 1);
4514                 kmem_free(nnm, nlen);
4515                 goto out;
4516         }
4517 
4518         /* check label of the target dir */
4519         if (is_system_labeled()) {
4520                 ASSERT(req->rq_label != NULL);
4521                 clabel = req->rq_label;
4522                 DTRACE_PROBE2(tx__rfs4__log__info__oprename__clabel, char *,
4523                     "got client label from request(1)",
4524                     struct svc_req *, req);
4525                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
4526                         if (!do_rfs_label_check(clabel, ndvp,
4527                             EQUALITY_CHECK, cs->exi)) {
4528                                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
4529                                 goto err_out;
4530                         }
4531                 }
4532         }
4533 
4534         /*
4535          * Is the source a file and have a delegation?
4536          * We don't need to acquire va_seq before these lookups, if
4537          * it causes an update, cinfo.before will not match, which will
4538          * trigger a cache flush even if atomic is TRUE.
4539          */
4540         if (sfp = rfs4_lookup_and_findfile(odvp, converted_onm, &srcvp,
4541             &error, cs->cr)) {
4542                 if (rfs4_check_delegated_byfp(FWRITE, sfp, TRUE, TRUE, TRUE,
4543                     NULL)) {
4544                         *cs->statusp = resp->status = NFS4ERR_DELAY;
4545                         goto err_out;
4546                 }
4547         }
4548 
4549         if (srcvp == NULL) {
4550                 *cs->statusp = resp->status = puterrno4(error);
4551                 if (onm != converted_onm)
4552                         kmem_free(converted_onm, MAXPATHLEN + 1);
4553                 kmem_free(onm, olen);
4554                 if (nnm != converted_nnm)
4555                         kmem_free(converted_nnm, MAXPATHLEN + 1);
4556                 kmem_free(nnm, nlen);
4557                 goto out;
4558         }
4559 
4560         sfp_rele_grant_hold = 1;
4561 
4562         /* Does the destination exist and a file and have a delegation? */
4563         if (fp = rfs4_lookup_and_findfile(ndvp, converted_nnm, &targvp,
4564             NULL, cs->cr)) {
4565                 if (rfs4_check_delegated_byfp(FWRITE, fp, TRUE, TRUE, TRUE,
4566                     NULL)) {
4567                         *cs->statusp = resp->status = NFS4ERR_DELAY;
4568                         goto err_out;
4569                 }
4570         }
4571         fp_rele_grant_hold = 1;
4572 
4573         /* Check for NBMAND lock on both source and target */
4574         if (nbl_need_check(srcvp)) {
4575                 nbl_start_crit(srcvp, RW_READER);
4576                 in_crit_src = 1;
4577                 if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0, NULL)) {
4578                         *cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4579                         goto err_out;
4580                 }
4581         }
4582 
4583         if (targvp && nbl_need_check(targvp)) {
4584                 nbl_start_crit(targvp, RW_READER);
4585                 in_crit_targ = 1;
4586                 if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
4587                         *cs->statusp = resp->status = NFS4ERR_FILE_OPEN;
4588                         goto err_out;
4589                 }
4590         }
4591 
4592         /* Get source "before" change value */
4593         obdva.va_mask = AT_CTIME|AT_SEQ;
4594         error = VOP_GETATTR(odvp, &obdva, 0, cs->cr, NULL);
4595         if (!error) {
4596                 nbdva.va_mask = AT_CTIME|AT_SEQ;
4597                 error = VOP_GETATTR(ndvp, &nbdva, 0, cs->cr, NULL);
4598         }
4599         if (error) {
4600                 *cs->statusp = resp->status = puterrno4(error);
4601                 goto err_out;
4602         }
4603 
4604         NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.before, obdva.va_ctime)
4605         NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.before, nbdva.va_ctime)
4606 
4607         error = VOP_RENAME(odvp, converted_onm, ndvp, converted_nnm, cs->cr,
4608             NULL, 0);
4609 
4610         /*
4611          * If target existed and was unlinked by VOP_RENAME, state will need
4612          * closed. To avoid deadlock, rfs4_close_all_state will be done after
4613          * any necessary nbl_end_crit on srcvp and tgtvp.
4614          */
4615         if (error == 0 && fp != NULL) {
4616                 rfs4_dbe_lock(fp->rf_dbe);
4617                 tvp = fp->rf_vp;
4618                 if (tvp)
4619                         VN_HOLD(tvp);
4620                 rfs4_dbe_unlock(fp->rf_dbe);
4621 
4622                 if (tvp) {
4623                         struct vattr va;
4624                         va.va_mask = AT_NLINK;
4625 
4626                         if (!VOP_GETATTR(tvp, &va, 0, cs->cr, NULL) &&
4627                             va.va_nlink == 0) {
4628                                 unlinked = 1;
4629 
4630                                 /* DEBUG data */
4631                                 if ((srcvp == targvp) || (tvp != targvp)) {
4632                                         cmn_err(CE_WARN, "rfs4_op_rename: "
4633                                             "srcvp %p, targvp: %p, tvp: %p",
4634                                             (void *)srcvp, (void *)targvp,
4635                                             (void *)tvp);
4636                                 }
4637                         } else {
4638                                 VN_RELE(tvp);
4639                         }
4640                 }
4641         }
4642         if (error == 0)
4643                 vn_renamepath(ndvp, srcvp, nnm, nlen - 1);
4644 
4645         if (in_crit_src)
4646                 nbl_end_crit(srcvp);
4647         if (srcvp)
4648                 VN_RELE(srcvp);
4649         if (in_crit_targ)
4650                 nbl_end_crit(targvp);
4651         if (targvp)
4652                 VN_RELE(targvp);
4653 
4654         if (unlinked) {
4655                 ASSERT(fp != NULL);
4656                 ASSERT(tvp != NULL);
4657 
4658                 /* DEBUG data */
4659                 if (RW_READ_HELD(&tvp->v_nbllock)) {
4660                         cmn_err(CE_WARN, "rfs4_op_rename: "
4661                             "RW_READ_HELD(%p)", (void *)tvp);
4662                 }
4663 
4664                 /* The file is gone and so should the state */
4665                 rfs4_close_all_state(fp);
4666                 VN_RELE(tvp);
4667         }
4668 
4669         if (sfp) {
4670                 rfs4_clear_dont_grant(sfp);
4671                 rfs4_file_rele(sfp);
4672         }
4673         if (fp) {
4674                 rfs4_clear_dont_grant(fp);
4675                 rfs4_file_rele(fp);
4676         }
4677 
4678         if (converted_onm != onm)
4679                 kmem_free(converted_onm, MAXPATHLEN + 1);
4680         kmem_free(onm, olen);
4681         if (converted_nnm != nnm)
4682                 kmem_free(converted_nnm, MAXPATHLEN + 1);
4683         kmem_free(nnm, nlen);
4684 
4685         /*
4686          * Get the initial "after" sequence number, if it fails, set to zero
4687          */
4688         oidva.va_mask = AT_SEQ;
4689         if (VOP_GETATTR(odvp, &oidva, 0, cs->cr, NULL))
4690                 oidva.va_seq = 0;
4691 
4692         nidva.va_mask = AT_SEQ;
4693         if (VOP_GETATTR(ndvp, &nidva, 0, cs->cr, NULL))
4694                 nidva.va_seq = 0;
4695 
4696         /*
4697          * Force modified data and metadata out to stable storage.
4698          */
4699         (void) VOP_FSYNC(odvp, 0, cs->cr, NULL);
4700         (void) VOP_FSYNC(ndvp, 0, cs->cr, NULL);
4701 
4702         if (error) {
4703                 *cs->statusp = resp->status = puterrno4(error);
4704                 goto out;
4705         }
4706 
4707         /*
4708          * Get "after" change values, if it fails, simply return the
4709          * before value.
4710          */
4711         oadva.va_mask = AT_CTIME|AT_SEQ;
4712         if (VOP_GETATTR(odvp, &oadva, 0, cs->cr, NULL)) {
4713                 oadva.va_ctime = obdva.va_ctime;
4714                 oadva.va_seq = 0;
4715         }
4716 
4717         nadva.va_mask = AT_CTIME|AT_SEQ;
4718         if (VOP_GETATTR(odvp, &nadva, 0, cs->cr, NULL)) {
4719                 nadva.va_ctime = nbdva.va_ctime;
4720                 nadva.va_seq = 0;
4721         }
4722 
4723         NFS4_SET_FATTR4_CHANGE(resp->source_cinfo.after, oadva.va_ctime)
4724         NFS4_SET_FATTR4_CHANGE(resp->target_cinfo.after, nadva.va_ctime)
4725 
4726         /*
4727          * The cinfo.atomic = TRUE only if we have
4728          * non-zero va_seq's, and it has incremented by exactly one
4729          * during the VOP_RENAME and it didn't change during the VOP_FSYNC.
4730          */
4731         if (obdva.va_seq && oidva.va_seq && oadva.va_seq &&
4732             oidva.va_seq == (obdva.va_seq + 1) && oidva.va_seq == oadva.va_seq)
4733                 resp->source_cinfo.atomic = TRUE;
4734         else
4735                 resp->source_cinfo.atomic = FALSE;
4736 
4737         if (nbdva.va_seq && nidva.va_seq && nadva.va_seq &&
4738             nidva.va_seq == (nbdva.va_seq + 1) && nidva.va_seq == nadva.va_seq)
4739                 resp->target_cinfo.atomic = TRUE;
4740         else
4741                 resp->target_cinfo.atomic = FALSE;
4742 
4743 #ifdef  VOLATILE_FH_TEST
4744         {
4745         extern void add_volrnm_fh(struct exportinfo *, vnode_t *);
4746 
4747         /*
4748          * Add the renamed file handle to the volatile rename list
4749          */
4750         if (cs->exi->exi_export.ex_flags & EX_VOLRNM) {
4751                 /* file handles may expire on rename */
4752                 vnode_t *vp;
4753 
4754                 nnm = utf8_to_fn(&args->newname, &nlen, NULL);
4755                 /*
4756                  * Already know that nnm will be a valid string
4757                  */
4758                 error = VOP_LOOKUP(ndvp, nnm, &vp, NULL, 0, NULL, cs->cr,
4759                     NULL, NULL, NULL);
4760                 kmem_free(nnm, nlen);
4761                 if (!error) {
4762                         add_volrnm_fh(cs->exi, vp);
4763                         VN_RELE(vp);
4764                 }
4765         }
4766         }
4767 #endif  /* VOLATILE_FH_TEST */
4768 
4769         *cs->statusp = resp->status = NFS4_OK;
4770 out:
4771         DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4772             RENAME4res *, resp);
4773         return;
4774 
4775 err_out:
4776         if (onm != converted_onm)
4777                 kmem_free(converted_onm, MAXPATHLEN + 1);
4778         if (onm != NULL)
4779                 kmem_free(onm, olen);
4780         if (nnm != converted_nnm)
4781                 kmem_free(converted_nnm, MAXPATHLEN + 1);
4782         if (nnm != NULL)
4783                 kmem_free(nnm, nlen);
4784 
4785         if (in_crit_src) nbl_end_crit(srcvp);
4786         if (in_crit_targ) nbl_end_crit(targvp);
4787         if (targvp) VN_RELE(targvp);
4788         if (srcvp) VN_RELE(srcvp);
4789         if (sfp) {
4790                 if (sfp_rele_grant_hold) rfs4_clear_dont_grant(sfp);
4791                 rfs4_file_rele(sfp);
4792         }
4793         if (fp) {
4794                 if (fp_rele_grant_hold) rfs4_clear_dont_grant(fp);
4795                 rfs4_file_rele(fp);
4796         }
4797 
4798         DTRACE_NFSV4_2(op__rename__done, struct compound_state *, cs,
4799             RENAME4res *, resp);
4800 }
4801 
4802 /* ARGSUSED */
4803 static void
4804 rfs4_op_renew(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4805     struct compound_state *cs)
4806 {
4807         RENEW4args *args = &argop->nfs_argop4_u.oprenew;
4808         RENEW4res *resp = &resop->nfs_resop4_u.oprenew;
4809         rfs4_client_t *cp;
4810 
4811         DTRACE_NFSV4_2(op__renew__start, struct compound_state *, cs,
4812             RENEW4args *, args);
4813 
4814         if ((cp = rfs4_findclient_by_id(args->clientid, FALSE)) == NULL) {
4815                 *cs->statusp = resp->status =
4816                     rfs4_check_clientid(&args->clientid, 0);
4817                 goto out;
4818         }
4819 
4820         if (rfs4_lease_expired(cp)) {
4821                 rfs4_client_rele(cp);
4822                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
4823                 goto out;
4824         }
4825 
4826         rfs4_update_lease(cp);
4827 
4828         mutex_enter(cp->rc_cbinfo.cb_lock);
4829         if (cp->rc_cbinfo.cb_notified_of_cb_path_down == FALSE) {
4830                 cp->rc_cbinfo.cb_notified_of_cb_path_down = TRUE;
4831                 *cs->statusp = resp->status = NFS4ERR_CB_PATH_DOWN;
4832         } else {
4833                 *cs->statusp = resp->status = NFS4_OK;
4834         }
4835         mutex_exit(cp->rc_cbinfo.cb_lock);
4836 
4837         rfs4_client_rele(cp);
4838 
4839 out:
4840         DTRACE_NFSV4_2(op__renew__done, struct compound_state *, cs,
4841             RENEW4res *, resp);
4842 }
4843 
4844 /* ARGSUSED */
4845 static void
4846 rfs4_op_restorefh(nfs_argop4 *args, nfs_resop4 *resop, struct svc_req *req,
4847     struct compound_state *cs)
4848 {
4849         RESTOREFH4res *resp = &resop->nfs_resop4_u.oprestorefh;
4850 
4851         DTRACE_NFSV4_1(op__restorefh__start, struct compound_state *, cs);
4852 
4853         /* No need to check cs->access - we are not accessing any object */
4854         if ((cs->saved_vp == NULL) || (cs->saved_fh.nfs_fh4_val == NULL)) {
4855                 *cs->statusp = resp->status = NFS4ERR_RESTOREFH;
4856                 goto out;
4857         }
4858         if (cs->vp != NULL) {
4859                 VN_RELE(cs->vp);
4860         }
4861         cs->vp = cs->saved_vp;
4862         cs->saved_vp = NULL;
4863         cs->exi = cs->saved_exi;
4864         nfs_fh4_copy(&cs->saved_fh, &cs->fh);
4865         *cs->statusp = resp->status = NFS4_OK;
4866         cs->deleg = FALSE;
4867 
4868 out:
4869         DTRACE_NFSV4_2(op__restorefh__done, struct compound_state *, cs,
4870             RESTOREFH4res *, resp);
4871 }
4872 
4873 /* ARGSUSED */
4874 static void
4875 rfs4_op_savefh(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
4876     struct compound_state *cs)
4877 {
4878         SAVEFH4res *resp = &resop->nfs_resop4_u.opsavefh;
4879 
4880         DTRACE_NFSV4_1(op__savefh__start, struct compound_state *, cs);
4881 
4882         /* No need to check cs->access - we are not accessing any object */
4883         if (cs->vp == NULL) {
4884                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
4885                 goto out;
4886         }
4887         if (cs->saved_vp != NULL) {
4888                 VN_RELE(cs->saved_vp);
4889         }
4890         cs->saved_vp = cs->vp;
4891         VN_HOLD(cs->saved_vp);
4892         cs->saved_exi = cs->exi;
4893         /*
4894          * since SAVEFH is fairly rare, don't alloc space for its fh
4895          * unless necessary.
4896          */
4897         if (cs->saved_fh.nfs_fh4_val == NULL) {
4898                 cs->saved_fh.nfs_fh4_val = kmem_alloc(NFS4_FHSIZE, KM_SLEEP);
4899         }
4900         nfs_fh4_copy(&cs->fh, &cs->saved_fh);
4901         *cs->statusp = resp->status = NFS4_OK;
4902 
4903 out:
4904         DTRACE_NFSV4_2(op__savefh__done, struct compound_state *, cs,
4905             SAVEFH4res *, resp);
4906 }
4907 
4908 /*
4909  * rfs4_verify_attr is called when nfsv4 Setattr failed, but we wish to
4910  * return the bitmap of attrs that were set successfully. It is also
4911  * called by Verify/Nverify to test the vattr/vfsstat attrs. It should
4912  * always be called only after rfs4_do_set_attrs().
4913  *
4914  * Verify that the attributes are same as the expected ones. sargp->vap
4915  * and sargp->sbp contain the input attributes as translated from fattr4.
4916  *
4917  * This function verifies only the attrs that correspond to a vattr or
4918  * vfsstat struct. That is because of the extra step needed to get the
4919  * corresponding system structs. Other attributes have already been set or
4920  * verified by do_rfs4_set_attrs.
4921  *
4922  * Return 0 if all attrs match, -1 if some don't, error if error processing.
4923  */
4924 static int
4925 rfs4_verify_attr(struct nfs4_svgetit_arg *sargp,
4926     bitmap4 *resp, struct nfs4_ntov_table *ntovp)
4927 {
4928         int error, ret_error = 0;
4929         int i, k;
4930         uint_t sva_mask = sargp->vap->va_mask;
4931         uint_t vbit;
4932         union nfs4_attr_u *na;
4933         uint8_t *amap;
4934         bool_t getsb = ntovp->vfsstat;
4935 
4936         if (sva_mask != 0) {
4937                 /*
4938                  * Okay to overwrite sargp->vap because we verify based
4939                  * on the incoming values.
4940                  */
4941                 ret_error = VOP_GETATTR(sargp->cs->vp, sargp->vap, 0,
4942                     sargp->cs->cr, NULL);
4943                 if (ret_error) {
4944                         if (resp == NULL)
4945                                 return (ret_error);
4946                         /*
4947                          * Must return bitmap of successful attrs
4948                          */
4949                         sva_mask = 0;   /* to prevent checking vap later */
4950                 } else {
4951                         /*
4952                          * Some file systems clobber va_mask. it is probably
4953                          * wrong of them to do so, nonethless we practice
4954                          * defensive coding.
4955                          * See bug id 4276830.
4956                          */
4957                         sargp->vap->va_mask = sva_mask;
4958                 }
4959         }
4960 
4961         if (getsb) {
4962                 /*
4963                  * Now get the superblock and loop on the bitmap, as there is
4964                  * no simple way of translating from superblock to bitmap4.
4965                  */
4966                 ret_error = VFS_STATVFS(sargp->cs->vp->v_vfsp, sargp->sbp);
4967                 if (ret_error) {
4968                         if (resp == NULL)
4969                                 goto errout;
4970                         getsb = FALSE;
4971                 }
4972         }
4973 
4974         /*
4975          * Now loop and verify each attribute which getattr returned
4976          * whether it's the same as the input.
4977          */
4978         if (resp == NULL && !getsb && (sva_mask == 0))
4979                 goto errout;
4980 
4981         na = ntovp->na;
4982         amap = ntovp->amap;
4983         k = 0;
4984         for (i = 0; i < ntovp->attrcnt; i++, na++, amap++) {
4985                 k = *amap;
4986                 ASSERT(nfs4_ntov_map[k].nval == k);
4987                 vbit = nfs4_ntov_map[k].vbit;
4988 
4989                 /*
4990                  * If vattr attribute but VOP_GETATTR failed, or it's
4991                  * superblock attribute but VFS_STATVFS failed, skip
4992                  */
4993                 if (vbit) {
4994                         if ((vbit & sva_mask) == 0)
4995                                 continue;
4996                 } else if (!(getsb && nfs4_ntov_map[k].vfsstat)) {
4997                         continue;
4998                 }
4999                 error = (*nfs4_ntov_map[k].sv_getit)(NFS4ATTR_VERIT, sargp, na);
5000                 if (resp != NULL) {
5001                         if (error)
5002                                 ret_error = -1; /* not all match */
5003                         else    /* update response bitmap */
5004                                 *resp |= nfs4_ntov_map[k].fbit;
5005                         continue;
5006                 }
5007                 if (error) {
5008                         ret_error = -1; /* not all match */
5009                         break;
5010                 }
5011         }
5012 errout:
5013         return (ret_error);
5014 }
5015 
5016 /*
5017  * Decode the attribute to be set/verified. If the attr requires a sys op
5018  * (VOP_GETATTR, VFS_VFSSTAT), and the request is to verify, then don't
5019  * call the sv_getit function for it, because the sys op hasn't yet been done.
5020  * Return 0 for success, error code if failed.
5021  *
5022  * Note: the decoded arg is not freed here but in nfs4_ntov_table_free.
5023  */
5024 static int
5025 decode_fattr4_attr(nfs4_attr_cmd_t cmd, struct nfs4_svgetit_arg *sargp,
5026     int k, XDR *xdrp, bitmap4 *resp_bval, union nfs4_attr_u *nap)
5027 {
5028         int error = 0;
5029         bool_t set_later;
5030 
5031         sargp->vap->va_mask |= nfs4_ntov_map[k].vbit;
5032 
5033         if ((*nfs4_ntov_map[k].xfunc)(xdrp, nap)) {
5034                 set_later = nfs4_ntov_map[k].vbit || nfs4_ntov_map[k].vfsstat;
5035                 /*
5036                  * don't verify yet if a vattr or sb dependent attr,
5037                  * because we don't have their sys values yet.
5038                  * Will be done later.
5039                  */
5040                 if (! (set_later && (cmd == NFS4ATTR_VERIT))) {
5041                         /*
5042                          * ACLs are a special case, since setting the MODE
5043                          * conflicts with setting the ACL.  We delay setting
5044                          * the ACL until all other attributes have been set.
5045                          * The ACL gets set in do_rfs4_op_setattr().
5046                          */
5047                         if (nfs4_ntov_map[k].fbit != FATTR4_ACL_MASK) {
5048                                 error = (*nfs4_ntov_map[k].sv_getit)(cmd,
5049                                     sargp, nap);
5050                                 if (error) {
5051                                         xdr_free(nfs4_ntov_map[k].xfunc,
5052                                             (caddr_t)nap);
5053                                 }
5054                         }
5055                 }
5056         } else {
5057 #ifdef  DEBUG
5058                 cmn_err(CE_NOTE, "decode_fattr4_attr: error "
5059                     "decoding attribute %d\n", k);
5060 #endif
5061                 error = EINVAL;
5062         }
5063         if (!error && resp_bval && !set_later) {
5064                 *resp_bval |= nfs4_ntov_map[k].fbit;
5065         }
5066 
5067         return (error);
5068 }
5069 
5070 /*
5071  * Set vattr based on incoming fattr4 attrs - used by setattr.
5072  * Set response mask. Ignore any values that are not writable vattr attrs.
5073  */
5074 static nfsstat4
5075 do_rfs4_set_attrs(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
5076     struct nfs4_svgetit_arg *sargp, struct nfs4_ntov_table *ntovp,
5077     nfs4_attr_cmd_t cmd)
5078 {
5079         int error = 0;
5080         int i;
5081         char *attrs = fattrp->attrlist4;
5082         uint32_t attrslen = fattrp->attrlist4_len;
5083         XDR xdr;
5084         nfsstat4 status = NFS4_OK;
5085         vnode_t *vp = cs->vp;
5086         union nfs4_attr_u *na;
5087         uint8_t *amap;
5088 
5089 #ifndef lint
5090         /*
5091          * Make sure that maximum attribute number can be expressed as an
5092          * 8 bit quantity.
5093          */
5094         ASSERT(NFS4_MAXNUM_ATTRS <= (UINT8_MAX + 1));
5095 #endif
5096 
5097         if (vp == NULL) {
5098                 if (resp)
5099                         *resp = 0;
5100                 return (NFS4ERR_NOFILEHANDLE);
5101         }
5102         if (cs->access == CS_ACCESS_DENIED) {
5103                 if (resp)
5104                         *resp = 0;
5105                 return (NFS4ERR_ACCESS);
5106         }
5107 
5108         sargp->op = cmd;
5109         sargp->cs = cs;
5110         sargp->flag = 0;     /* may be set later */
5111         sargp->vap->va_mask = 0;
5112         sargp->rdattr_error = NFS4_OK;
5113         sargp->rdattr_error_req = FALSE;
5114         /* sargp->sbp is set by the caller */
5115 
5116         xdrmem_create(&xdr, attrs, attrslen, XDR_DECODE);
5117 
5118         na = ntovp->na;
5119         amap = ntovp->amap;
5120 
5121         /*
5122          * The following loop iterates on the nfs4_ntov_map checking
5123          * if the fbit is set in the requested bitmap.
5124          * If set then we process the arguments using the
5125          * rfs4_fattr4 conversion functions to populate the setattr
5126          * vattr and va_mask. Any settable attrs that are not using vattr
5127          * will be set in this loop.
5128          */
5129         for (i = 0; i < nfs4_ntov_map_size; i++) {
5130                 if (!(fattrp->attrmask & nfs4_ntov_map[i].fbit)) {
5131                         continue;
5132                 }
5133                 /*
5134                  * If setattr, must be a writable attr.
5135                  * If verify/nverify, must be a readable attr.
5136                  */
5137                 if ((error = (*nfs4_ntov_map[i].sv_getit)(
5138                     NFS4ATTR_SUPPORTED, sargp, NULL)) != 0) {
5139                         /*
5140                          * Client tries to set/verify an
5141                          * unsupported attribute, tries to set
5142                          * a read only attr or verify a write
5143                          * only one - error!
5144                          */
5145                         break;
5146                 }
5147                 /*
5148                  * Decode the attribute to set/verify
5149                  */
5150                 error = decode_fattr4_attr(cmd, sargp, nfs4_ntov_map[i].nval,
5151                     &xdr, resp ? resp : NULL, na);
5152                 if (error)
5153                         break;
5154                 *amap++ = (uint8_t)nfs4_ntov_map[i].nval;
5155                 na++;
5156                 (ntovp->attrcnt)++;
5157                 if (nfs4_ntov_map[i].vfsstat)
5158                         ntovp->vfsstat = TRUE;
5159         }
5160 
5161         if (error != 0)
5162                 status = (error == ENOTSUP ? NFS4ERR_ATTRNOTSUPP :
5163                     puterrno4(error));
5164         /* xdrmem_destroy(&xdrs); */        /* NO-OP */
5165         return (status);
5166 }
5167 
5168 static nfsstat4
5169 do_rfs4_op_setattr(bitmap4 *resp, fattr4 *fattrp, struct compound_state *cs,
5170     stateid4 *stateid)
5171 {
5172         int error = 0;
5173         struct nfs4_svgetit_arg sarg;
5174         bool_t trunc;
5175 
5176         nfsstat4 status = NFS4_OK;
5177         cred_t *cr = cs->cr;
5178         vnode_t *vp = cs->vp;
5179         struct nfs4_ntov_table ntov;
5180         struct statvfs64 sb;
5181         struct vattr bva;
5182         struct flock64 bf;
5183         int in_crit = 0;
5184         uint_t saved_mask = 0;
5185         caller_context_t ct;
5186 
5187         *resp = 0;
5188         sarg.sbp = &sb;
5189         sarg.is_referral = B_FALSE;
5190         nfs4_ntov_table_init(&ntov);
5191         status = do_rfs4_set_attrs(resp, fattrp, cs, &sarg, &ntov,
5192             NFS4ATTR_SETIT);
5193         if (status != NFS4_OK) {
5194                 /*
5195                  * failed set attrs
5196                  */
5197                 goto done;
5198         }
5199         if ((sarg.vap->va_mask == 0) &&
5200             (! (fattrp->attrmask & FATTR4_ACL_MASK))) {
5201                 /*
5202                  * no further work to be done
5203                  */
5204                 goto done;
5205         }
5206 
5207         /*
5208          * If we got a request to set the ACL and the MODE, only
5209          * allow changing VSUID, VSGID, and VSVTX.  Attempting
5210          * to change any other bits, along with setting an ACL,
5211          * gives NFS4ERR_INVAL.
5212          */
5213         if ((fattrp->attrmask & FATTR4_ACL_MASK) &&
5214             (fattrp->attrmask & FATTR4_MODE_MASK)) {
5215                 vattr_t va;
5216 
5217                 va.va_mask = AT_MODE;
5218                 error = VOP_GETATTR(vp, &va, 0, cs->cr, NULL);
5219                 if (error) {
5220                         status = puterrno4(error);
5221                         goto done;
5222                 }
5223                 if ((sarg.vap->va_mode ^ va.va_mode) &
5224                     ~(VSUID | VSGID | VSVTX)) {
5225                         status = NFS4ERR_INVAL;
5226                         goto done;
5227                 }
5228         }
5229 
5230         /* Check stateid only if size has been set */
5231         if (sarg.vap->va_mask & AT_SIZE) {
5232                 trunc = (sarg.vap->va_size == 0);
5233                 status = rfs4_check_stateid(FWRITE, cs->vp, stateid,
5234                     trunc, &cs->deleg, sarg.vap->va_mask & AT_SIZE, &ct);
5235                 if (status != NFS4_OK)
5236                         goto done;
5237         } else {
5238                 ct.cc_sysid = 0;
5239                 ct.cc_pid = 0;
5240                 ct.cc_caller_id = nfs4_srv_caller_id;
5241                 ct.cc_flags = CC_DONTBLOCK;
5242         }
5243 
5244         /* XXX start of possible race with delegations */
5245 
5246         /*
5247          * We need to specially handle size changes because it is
5248          * possible for the client to create a file with read-only
5249          * modes, but with the file opened for writing. If the client
5250          * then tries to set the file size, e.g. ftruncate(3C),
5251          * fcntl(F_FREESP), the normal access checking done in
5252          * VOP_SETATTR would prevent the client from doing it even though
5253          * it should be allowed to do so.  To get around this, we do the
5254          * access checking for ourselves and use VOP_SPACE which doesn't
5255          * do the access checking.
5256          * Also the client should not be allowed to change the file
5257          * size if there is a conflicting non-blocking mandatory lock in
5258          * the region of the change.
5259          */
5260         if (vp->v_type == VREG && (sarg.vap->va_mask & AT_SIZE)) {
5261                 u_offset_t offset;
5262                 ssize_t length;
5263 
5264                 /*
5265                  * ufs_setattr clears AT_SIZE from vap->va_mask, but
5266                  * before returning, sarg.vap->va_mask is used to
5267                  * generate the setattr reply bitmap.  We also clear
5268                  * AT_SIZE below before calling VOP_SPACE.  For both
5269                  * of these cases, the va_mask needs to be saved here
5270                  * and restored after calling VOP_SETATTR.
5271                  */
5272                 saved_mask = sarg.vap->va_mask;
5273 
5274                 /*
5275                  * Check any possible conflict due to NBMAND locks.
5276                  * Get into critical region before VOP_GETATTR, so the
5277                  * size attribute is valid when checking conflicts.
5278                  */
5279                 if (nbl_need_check(vp)) {
5280                         nbl_start_crit(vp, RW_READER);
5281                         in_crit = 1;
5282                 }
5283 
5284                 bva.va_mask = AT_UID|AT_SIZE;
5285                 if (error = VOP_GETATTR(vp, &bva, 0, cr, &ct)) {
5286                         status = puterrno4(error);
5287                         goto done;
5288                 }
5289 
5290                 if (in_crit) {
5291                         if (sarg.vap->va_size < bva.va_size) {
5292                                 offset = sarg.vap->va_size;
5293                                 length = bva.va_size - sarg.vap->va_size;
5294                         } else {
5295                                 offset = bva.va_size;
5296                                 length = sarg.vap->va_size - bva.va_size;
5297                         }
5298                         if (nbl_conflict(vp, NBL_WRITE, offset, length, 0,
5299                             &ct)) {
5300                                 status = NFS4ERR_LOCKED;
5301                                 goto done;
5302                         }
5303                 }
5304 
5305                 if (crgetuid(cr) == bva.va_uid) {
5306                         sarg.vap->va_mask &= ~AT_SIZE;
5307                         bf.l_type = F_WRLCK;
5308                         bf.l_whence = 0;
5309                         bf.l_start = (off64_t)sarg.vap->va_size;
5310                         bf.l_len = 0;
5311                         bf.l_sysid = 0;
5312                         bf.l_pid = 0;
5313                         error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
5314                             (offset_t)sarg.vap->va_size, cr, &ct);
5315                 }
5316         }
5317 
5318         if (!error && sarg.vap->va_mask != 0)
5319                 error = VOP_SETATTR(vp, sarg.vap, sarg.flag, cr, &ct);
5320 
5321         /* restore va_mask -- ufs_setattr clears AT_SIZE */
5322         if (saved_mask & AT_SIZE)
5323                 sarg.vap->va_mask |= AT_SIZE;
5324 
5325         /*
5326          * If an ACL was being set, it has been delayed until now,
5327          * in order to set the mode (via the VOP_SETATTR() above) first.
5328          */
5329         if ((! error) && (fattrp->attrmask & FATTR4_ACL_MASK)) {
5330                 int i;
5331 
5332                 for (i = 0; i < NFS4_MAXNUM_ATTRS; i++)
5333                         if (ntov.amap[i] == FATTR4_ACL)
5334                                 break;
5335                 if (i < NFS4_MAXNUM_ATTRS) {
5336                         error = (*nfs4_ntov_map[FATTR4_ACL].sv_getit)(
5337                             NFS4ATTR_SETIT, &sarg, &ntov.na[i]);
5338                         if (error == 0) {
5339                                 *resp |= FATTR4_ACL_MASK;
5340                         } else if (error == ENOTSUP) {
5341                                 (void) rfs4_verify_attr(&sarg, resp, &ntov);
5342                                 status = NFS4ERR_ATTRNOTSUPP;
5343                                 goto done;
5344                         }
5345                 } else {
5346                         NFS4_DEBUG(rfs4_debug,
5347                             (CE_NOTE, "do_rfs4_op_setattr: "
5348                             "unable to find ACL in fattr4"));
5349                         error = EINVAL;
5350                 }
5351         }
5352 
5353         if (error) {
5354                 /* check if a monitor detected a delegation conflict */
5355                 if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
5356                         status = NFS4ERR_DELAY;
5357                 else
5358                         status = puterrno4(error);
5359 
5360                 /*
5361                  * Set the response bitmap when setattr failed.
5362                  * If VOP_SETATTR partially succeeded, test by doing a
5363                  * VOP_GETATTR on the object and comparing the data
5364                  * to the setattr arguments.
5365                  */
5366                 (void) rfs4_verify_attr(&sarg, resp, &ntov);
5367         } else {
5368                 /*
5369                  * Force modified metadata out to stable storage.
5370                  */
5371                 (void) VOP_FSYNC(vp, FNODSYNC, cr, &ct);
5372                 /*
5373                  * Set response bitmap
5374                  */
5375                 nfs4_vmask_to_nmask_set(sarg.vap->va_mask, resp);
5376         }
5377 
5378 /* Return early and already have a NFSv4 error */
5379 done:
5380         /*
5381          * Except for nfs4_vmask_to_nmask_set(), vattr --> fattr
5382          * conversion sets both readable and writeable NFS4 attrs
5383          * for AT_MTIME and AT_ATIME.  The line below masks out
5384          * unrequested attrs from the setattr result bitmap.  This
5385          * is placed after the done: label to catch the ATTRNOTSUP
5386          * case.
5387          */
5388         *resp &= fattrp->attrmask;
5389 
5390         if (in_crit)
5391                 nbl_end_crit(vp);
5392 
5393         nfs4_ntov_table_free(&ntov, &sarg);
5394 
5395         return (status);
5396 }
5397 
5398 /* ARGSUSED */
5399 static void
5400 rfs4_op_setattr(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5401     struct compound_state *cs)
5402 {
5403         SETATTR4args *args = &argop->nfs_argop4_u.opsetattr;
5404         SETATTR4res *resp = &resop->nfs_resop4_u.opsetattr;
5405         bslabel_t *clabel;
5406 
5407         DTRACE_NFSV4_2(op__setattr__start, struct compound_state *, cs,
5408             SETATTR4args *, args);
5409 
5410         if (cs->vp == NULL) {
5411                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5412                 goto out;
5413         }
5414 
5415         /*
5416          * If there is an unshared filesystem mounted on this vnode,
5417          * do not allow to setattr on this vnode.
5418          */
5419         if (vn_ismntpt(cs->vp)) {
5420                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
5421                 goto out;
5422         }
5423 
5424         resp->attrsset = 0;
5425 
5426         if (rdonly4(req, cs)) {
5427                 *cs->statusp = resp->status = NFS4ERR_ROFS;
5428                 goto out;
5429         }
5430 
5431         /* check label before setting attributes */
5432         if (is_system_labeled()) {
5433                 ASSERT(req->rq_label != NULL);
5434                 clabel = req->rq_label;
5435                 DTRACE_PROBE2(tx__rfs4__log__info__opsetattr__clabel, char *,
5436                     "got client label from request(1)",
5437                     struct svc_req *, req);
5438                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
5439                         if (!do_rfs_label_check(clabel, cs->vp,
5440                             EQUALITY_CHECK, cs->exi)) {
5441                                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
5442                                 goto out;
5443                         }
5444                 }
5445         }
5446 
5447         *cs->statusp = resp->status =
5448             do_rfs4_op_setattr(&resp->attrsset, &args->obj_attributes, cs,
5449             &args->stateid);
5450 
5451 out:
5452         DTRACE_NFSV4_2(op__setattr__done, struct compound_state *, cs,
5453             SETATTR4res *, resp);
5454 }
5455 
5456 /* ARGSUSED */
5457 static void
5458 rfs4_op_verify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5459     struct compound_state *cs)
5460 {
5461         /*
5462          * verify and nverify are exactly the same, except that nverify
5463          * succeeds when some argument changed, and verify succeeds when
5464          * when none changed.
5465          */
5466 
5467         VERIFY4args  *args = &argop->nfs_argop4_u.opverify;
5468         VERIFY4res *resp = &resop->nfs_resop4_u.opverify;
5469 
5470         int error;
5471         struct nfs4_svgetit_arg sarg;
5472         struct statvfs64 sb;
5473         struct nfs4_ntov_table ntov;
5474 
5475         DTRACE_NFSV4_2(op__verify__start, struct compound_state *, cs,
5476             VERIFY4args *, args);
5477 
5478         if (cs->vp == NULL) {
5479                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5480                 goto out;
5481         }
5482 
5483         sarg.sbp = &sb;
5484         sarg.is_referral = B_FALSE;
5485         nfs4_ntov_table_init(&ntov);
5486         resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5487             &sarg, &ntov, NFS4ATTR_VERIT);
5488         if (resp->status != NFS4_OK) {
5489                 /*
5490                  * do_rfs4_set_attrs will try to verify systemwide attrs,
5491                  * so could return -1 for "no match".
5492                  */
5493                 if (resp->status == -1)
5494                         resp->status = NFS4ERR_NOT_SAME;
5495                 goto done;
5496         }
5497         error = rfs4_verify_attr(&sarg, NULL, &ntov);
5498         switch (error) {
5499         case 0:
5500                 resp->status = NFS4_OK;
5501                 break;
5502         case -1:
5503                 resp->status = NFS4ERR_NOT_SAME;
5504                 break;
5505         default:
5506                 resp->status = puterrno4(error);
5507                 break;
5508         }
5509 done:
5510         *cs->statusp = resp->status;
5511         nfs4_ntov_table_free(&ntov, &sarg);
5512 out:
5513         DTRACE_NFSV4_2(op__verify__done, struct compound_state *, cs,
5514             VERIFY4res *, resp);
5515 }
5516 
5517 /* ARGSUSED */
5518 static void
5519 rfs4_op_nverify(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5520     struct compound_state *cs)
5521 {
5522         /*
5523          * verify and nverify are exactly the same, except that nverify
5524          * succeeds when some argument changed, and verify succeeds when
5525          * when none changed.
5526          */
5527 
5528         NVERIFY4args  *args = &argop->nfs_argop4_u.opnverify;
5529         NVERIFY4res *resp = &resop->nfs_resop4_u.opnverify;
5530 
5531         int error;
5532         struct nfs4_svgetit_arg sarg;
5533         struct statvfs64 sb;
5534         struct nfs4_ntov_table ntov;
5535 
5536         DTRACE_NFSV4_2(op__nverify__start, struct compound_state *, cs,
5537             NVERIFY4args *, args);
5538 
5539         if (cs->vp == NULL) {
5540                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5541                 DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5542                     NVERIFY4res *, resp);
5543                 return;
5544         }
5545         sarg.sbp = &sb;
5546         sarg.is_referral = B_FALSE;
5547         nfs4_ntov_table_init(&ntov);
5548         resp->status = do_rfs4_set_attrs(NULL, &args->obj_attributes, cs,
5549             &sarg, &ntov, NFS4ATTR_VERIT);
5550         if (resp->status != NFS4_OK) {
5551                 /*
5552                  * do_rfs4_set_attrs will try to verify systemwide attrs,
5553                  * so could return -1 for "no match".
5554                  */
5555                 if (resp->status == -1)
5556                         resp->status = NFS4_OK;
5557                 goto done;
5558         }
5559         error = rfs4_verify_attr(&sarg, NULL, &ntov);
5560         switch (error) {
5561         case 0:
5562                 resp->status = NFS4ERR_SAME;
5563                 break;
5564         case -1:
5565                 resp->status = NFS4_OK;
5566                 break;
5567         default:
5568                 resp->status = puterrno4(error);
5569                 break;
5570         }
5571 done:
5572         *cs->statusp = resp->status;
5573         nfs4_ntov_table_free(&ntov, &sarg);
5574 
5575         DTRACE_NFSV4_2(op__nverify__done, struct compound_state *, cs,
5576             NVERIFY4res *, resp);
5577 }
5578 
5579 /*
5580  * XXX - This should live in an NFS header file.
5581  */
5582 #define MAX_IOVECS      12
5583 
5584 /* ARGSUSED */
5585 static void
5586 rfs4_op_write(nfs_argop4 *argop, nfs_resop4 *resop, struct svc_req *req,
5587     struct compound_state *cs)
5588 {
5589         WRITE4args *args = &argop->nfs_argop4_u.opwrite;
5590         WRITE4res *resp = &resop->nfs_resop4_u.opwrite;
5591         int error;
5592         vnode_t *vp;
5593         struct vattr bva;
5594         u_offset_t rlimit;
5595         struct uio uio;
5596         struct iovec iov[MAX_IOVECS];
5597         struct iovec *iovp;
5598         int iovcnt;
5599         int ioflag;
5600         cred_t *savecred, *cr;
5601         bool_t *deleg = &cs->deleg;
5602         nfsstat4 stat;
5603         int in_crit = 0;
5604         caller_context_t ct;
5605         nfs4_srv_t *nsrv4;
5606 
5607         DTRACE_NFSV4_2(op__write__start, struct compound_state *, cs,
5608             WRITE4args *, args);
5609 
5610         vp = cs->vp;
5611         if (vp == NULL) {
5612                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
5613                 goto out;
5614         }
5615         if (cs->access == CS_ACCESS_DENIED) {
5616                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
5617                 goto out;
5618         }
5619 
5620         cr = cs->cr;
5621 
5622         if ((stat = rfs4_check_stateid(FWRITE, vp, &args->stateid, FALSE,
5623             deleg, TRUE, &ct)) != NFS4_OK) {
5624                 *cs->statusp = resp->status = stat;
5625                 goto out;
5626         }
5627 
5628         /*
5629          * We have to enter the critical region before calling VOP_RWLOCK
5630          * to avoid a deadlock with ufs.
5631          */
5632         if (nbl_need_check(vp)) {
5633                 nbl_start_crit(vp, RW_READER);
5634                 in_crit = 1;
5635                 if (nbl_conflict(vp, NBL_WRITE,
5636                     args->offset, args->data_len, 0, &ct)) {
5637                         *cs->statusp = resp->status = NFS4ERR_LOCKED;
5638                         goto out;
5639                 }
5640         }
5641 
5642         bva.va_mask = AT_MODE | AT_UID;
5643         error = VOP_GETATTR(vp, &bva, 0, cr, &ct);
5644 
5645         /*
5646          * If we can't get the attributes, then we can't do the
5647          * right access checking.  So, we'll fail the request.
5648          */
5649         if (error) {
5650                 *cs->statusp = resp->status = puterrno4(error);
5651                 goto out;
5652         }
5653 
5654         if (rdonly4(req, cs)) {
5655                 *cs->statusp = resp->status = NFS4ERR_ROFS;
5656                 goto out;
5657         }
5658 
5659         if (vp->v_type != VREG) {
5660                 *cs->statusp = resp->status =
5661                     ((vp->v_type == VDIR) ? NFS4ERR_ISDIR : NFS4ERR_INVAL);
5662                 goto out;
5663         }
5664 
5665         if (crgetuid(cr) != bva.va_uid &&
5666             (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct))) {
5667                 *cs->statusp = resp->status = puterrno4(error);
5668                 goto out;
5669         }
5670 
5671         if (MANDLOCK(vp, bva.va_mode)) {
5672                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
5673                 goto out;
5674         }
5675 
5676         nsrv4 = nfs4_get_srv();
5677         if (args->data_len == 0) {
5678                 *cs->statusp = resp->status = NFS4_OK;
5679                 resp->count = 0;
5680                 resp->committed = args->stable;
5681                 resp->writeverf = nsrv4->write4verf;
5682                 goto out;
5683         }
5684 
5685         if (args->mblk != NULL) {
5686                 mblk_t *m;
5687                 uint_t bytes, round_len;
5688 
5689                 iovcnt = 0;
5690                 bytes = 0;
5691                 round_len = roundup(args->data_len, BYTES_PER_XDR_UNIT);
5692                 for (m = args->mblk;
5693                     m != NULL && bytes < round_len;
5694                     m = m->b_cont) {
5695                         iovcnt++;
5696                         bytes += MBLKL(m);
5697                 }
5698 #ifdef DEBUG
5699                 /* should have ended on an mblk boundary */
5700                 if (bytes != round_len) {
5701                         printf("bytes=0x%x, round_len=0x%x, req len=0x%x\n",
5702                             bytes, round_len, args->data_len);
5703                         printf("args=%p, args->mblk=%p, m=%p", (void *)args,
5704                             (void *)args->mblk, (void *)m);
5705                         ASSERT(bytes == round_len);
5706                 }
5707 #endif
5708                 if (iovcnt <= MAX_IOVECS) {
5709                         iovp = iov;
5710                 } else {
5711                         iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
5712                 }
5713                 mblk_to_iov(args->mblk, iovcnt, iovp);
5714         } else if (args->rlist != NULL) {
5715                 iovcnt = 1;
5716                 iovp = iov;
5717                 iovp->iov_base = (char *)((args->rlist)->u.c_daddr3);
5718                 iovp->iov_len = args->data_len;
5719         } else {
5720                 iovcnt = 1;
5721                 iovp = iov;
5722                 iovp->iov_base = args->data_val;
5723                 iovp->iov_len = args->data_len;
5724         }
5725 
5726         uio.uio_iov = iovp;
5727         uio.uio_iovcnt = iovcnt;
5728 
5729         uio.uio_segflg = UIO_SYSSPACE;
5730         uio.uio_extflg = UIO_COPY_DEFAULT;
5731         uio.uio_loffset = args->offset;
5732         uio.uio_resid = args->data_len;
5733         uio.uio_llimit = curproc->p_fsz_ctl;
5734         rlimit = uio.uio_llimit - args->offset;
5735         if (rlimit < (u_offset_t)uio.uio_resid)
5736                 uio.uio_resid = (int)rlimit;
5737 
5738         if (args->stable == UNSTABLE4)
5739                 ioflag = 0;
5740         else if (args->stable == FILE_SYNC4)
5741                 ioflag = FSYNC;
5742         else if (args->stable == DATA_SYNC4)
5743                 ioflag = FDSYNC;
5744         else {
5745                 if (iovp != iov)
5746                         kmem_free(iovp, sizeof (*iovp) * iovcnt);
5747                 *cs->statusp = resp->status = NFS4ERR_INVAL;
5748                 goto out;
5749         }
5750 
5751         /*
5752          * We're changing creds because VM may fault and we need
5753          * the cred of the current thread to be used if quota
5754          * checking is enabled.
5755          */
5756         savecred = curthread->t_cred;
5757         curthread->t_cred = cr;
5758         error = do_io(FWRITE, vp, &uio, ioflag, cr, &ct);
5759         curthread->t_cred = savecred;
5760 
5761         if (iovp != iov)
5762                 kmem_free(iovp, sizeof (*iovp) * iovcnt);
5763 
5764         if (error) {
5765                 *cs->statusp = resp->status = puterrno4(error);
5766                 goto out;
5767         }
5768 
5769         *cs->statusp = resp->status = NFS4_OK;
5770         resp->count = args->data_len - uio.uio_resid;
5771 
5772         if (ioflag == 0)
5773                 resp->committed = UNSTABLE4;
5774         else
5775                 resp->committed = FILE_SYNC4;
5776 
5777         resp->writeverf = nsrv4->write4verf;
5778 
5779 out:
5780         if (in_crit)
5781                 nbl_end_crit(vp);
5782 
5783         DTRACE_NFSV4_2(op__write__done, struct compound_state *, cs,
5784             WRITE4res *, resp);
5785 }
5786 
5787 
5788 /* XXX put in a header file */
5789 extern int      sec_svc_getcred(struct svc_req *, cred_t *,  caddr_t *, int *);
5790 
5791 void
5792 rfs4_compound(COMPOUND4args *args, COMPOUND4res *resp, struct exportinfo *exi,
5793     struct svc_req *req, cred_t *cr, int *rv)
5794 {
5795         uint_t i;
5796         struct compound_state cs;
5797         nfs4_srv_t *nsrv4;
5798         nfs_export_t *ne = nfs_get_export();
5799 
5800         if (rv != NULL)
5801                 *rv = 0;
5802         rfs4_init_compound_state(&cs);
5803         /*
5804          * Form a reply tag by copying over the reqeuest tag.
5805          */
5806         resp->tag.utf8string_val =
5807             kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
5808         resp->tag.utf8string_len = args->tag.utf8string_len;
5809         bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
5810             resp->tag.utf8string_len);
5811 
5812         cs.statusp = &resp->status;
5813         cs.req = req;
5814         resp->array = NULL;
5815         resp->array_len = 0;
5816 
5817         /*
5818          * XXX for now, minorversion should be zero
5819          */
5820         if (args->minorversion != NFS4_MINORVERSION) {
5821                 DTRACE_NFSV4_2(compound__start, struct compound_state *,
5822                     &cs, COMPOUND4args *, args);
5823                 resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5824                 DTRACE_NFSV4_2(compound__done, struct compound_state *,
5825                     &cs, COMPOUND4res *, resp);
5826                 return;
5827         }
5828 
5829         if (args->array_len == 0) {
5830                 resp->status = NFS4_OK;
5831                 return;
5832         }
5833 
5834         ASSERT(exi == NULL);
5835         ASSERT(cr == NULL);
5836 
5837         cr = crget();
5838         ASSERT(cr != NULL);
5839 
5840         if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
5841                 DTRACE_NFSV4_2(compound__start, struct compound_state *,
5842                     &cs, COMPOUND4args *, args);
5843                 crfree(cr);
5844                 DTRACE_NFSV4_2(compound__done, struct compound_state *,
5845                     &cs, COMPOUND4res *, resp);
5846                 svcerr_badcred(req->rq_xprt);
5847                 if (rv != NULL)
5848                         *rv = 1;
5849                 return;
5850         }
5851         resp->array_len = args->array_len;
5852         resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
5853             KM_SLEEP);
5854 
5855         cs.basecr = cr;
5856         nsrv4 = nfs4_get_srv();
5857 
5858         DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs,
5859             COMPOUND4args *, args);
5860 
5861         /*
5862          * For now, NFS4 compound processing must be protected by
5863          * exported_lock because it can access more than one exportinfo
5864          * per compound and share/unshare can now change multiple
5865          * exinfo structs.  The NFS2/3 code only refs 1 exportinfo
5866          * per proc (excluding public exinfo), and exi_count design
5867          * is sufficient to protect concurrent execution of NFS2/3
5868          * ops along with unexport.  This lock will be removed as
5869          * part of the NFSv4 phase 2 namespace redesign work.
5870          */
5871         rw_enter(&ne->exported_lock, RW_READER);
5872 
5873         /*
5874          * If this is the first compound we've seen, we need to start all
5875          * new instances' grace periods.
5876          */
5877         if (nsrv4->seen_first_compound == 0) {
5878                 rfs4_grace_start_new(nsrv4);
5879                 /*
5880                  * This must be set after rfs4_grace_start_new(), otherwise
5881                  * another thread could proceed past here before the former
5882                  * is finished.
5883                  */
5884                 nsrv4->seen_first_compound = 1;
5885         }
5886 
5887         for (i = 0; i < args->array_len && cs.cont; i++) {
5888                 nfs_argop4 *argop;
5889                 nfs_resop4 *resop;
5890                 uint_t op;
5891 
5892                 argop = &args->array[i];
5893                 resop = &resp->array[i];
5894                 resop->resop = argop->argop;
5895                 op = (uint_t)resop->resop;
5896 
5897                 if (op < rfsv4disp_cnt) {
5898                         /*
5899                          * Count the individual ops here; NULL and COMPOUND
5900                          * are counted in common_dispatch()
5901                          */
5902                         rfsproccnt_v4_ptr[op].value.ui64++;
5903 
5904                         NFS4_DEBUG(rfs4_debug > 1,
5905                             (CE_NOTE, "Executing %s", rfs4_op_string[op]));
5906                         (*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
5907                         NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d",
5908                             rfs4_op_string[op], *cs.statusp));
5909                         if (*cs.statusp != NFS4_OK)
5910                                 cs.cont = FALSE;
5911                 } else {
5912                         /*
5913                          * This is effectively dead code since XDR code
5914                          * will have already returned BADXDR if op doesn't
5915                          * decode to legal value.  This only done for a
5916                          * day when XDR code doesn't verify v4 opcodes.
5917                          */
5918                         op = OP_ILLEGAL;
5919                         rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++;
5920 
5921                         rfs4_op_illegal(argop, resop, req, &cs);
5922                         cs.cont = FALSE;
5923                 }
5924 
5925                 /*
5926                  * If not at last op, and if we are to stop, then
5927                  * compact the results array.
5928                  */
5929                 if ((i + 1) < args->array_len && !cs.cont) {
5930                         nfs_resop4 *new_res = kmem_alloc(
5931                             (i+1) * sizeof (nfs_resop4), KM_SLEEP);
5932                         bcopy(resp->array,
5933                             new_res, (i+1) * sizeof (nfs_resop4));
5934                         kmem_free(resp->array,
5935                             args->array_len * sizeof (nfs_resop4));
5936 
5937                         resp->array_len =  i + 1;
5938                         resp->array = new_res;
5939                 }
5940         }
5941 
5942         rw_exit(&ne->exported_lock);
5943 
5944         /*
5945          * clear exportinfo and vnode fields from compound_state before dtrace
5946          * probe, to avoid tracing residual values for path and share path.
5947          */
5948         if (cs.vp)
5949                 VN_RELE(cs.vp);
5950         if (cs.saved_vp)
5951                 VN_RELE(cs.saved_vp);
5952         cs.exi = cs.saved_exi = NULL;
5953         cs.vp = cs.saved_vp = NULL;
5954 
5955         DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs,
5956             COMPOUND4res *, resp);
5957 
5958         if (cs.saved_fh.nfs_fh4_val)
5959                 kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
5960 
5961         if (cs.basecr)
5962                 crfree(cs.basecr);
5963         if (cs.cr)
5964                 crfree(cs.cr);
5965         /*
5966          * done with this compound request, free the label
5967          */
5968 
5969         if (req->rq_label != NULL) {
5970                 kmem_free(req->rq_label, sizeof (bslabel_t));
5971                 req->rq_label = NULL;
5972         }
5973 }
5974 
5975 /*
5976  * XXX because of what appears to be duplicate calls to rfs4_compound_free
5977  * XXX zero out the tag and array values. Need to investigate why the
5978  * XXX calls occur, but at least prevent the panic for now.
5979  */
5980 void
5981 rfs4_compound_free(COMPOUND4res *resp)
5982 {
5983         uint_t i;
5984 
5985         if (resp->tag.utf8string_val) {
5986                 UTF8STRING_FREE(resp->tag)
5987         }
5988 
5989         for (i = 0; i < resp->array_len; i++) {
5990                 nfs_resop4 *resop;
5991                 uint_t op;
5992 
5993                 resop = &resp->array[i];
5994                 op = (uint_t)resop->resop;
5995                 if (op < rfsv4disp_cnt) {
5996                         (*rfsv4disptab[op].dis_resfree)(resop);
5997                 }
5998         }
5999         if (resp->array != NULL) {
6000                 kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
6001         }
6002 }
6003 
6004 /*
6005  * Process the value of the compound request rpc flags, as a bit-AND
6006  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
6007  */
6008 void
6009 rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
6010 {
6011         int i;
6012         int flag = RPC_ALL;
6013 
6014         for (i = 0; flag && i < args->array_len; i++) {
6015                 uint_t op;
6016 
6017                 op = (uint_t)args->array[i].argop;
6018 
6019                 if (op < rfsv4disp_cnt)
6020                         flag &= rfsv4disptab[op].dis_flags;
6021                 else
6022                         flag = 0;
6023         }
6024         *flagp = flag;
6025 }
6026 
6027 nfsstat4
6028 rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
6029 {
6030         nfsstat4 e;
6031 
6032         rfs4_dbe_lock(cp->rc_dbe);
6033 
6034         if (cp->rc_sysidt != LM_NOSYSID) {
6035                 *sp = cp->rc_sysidt;
6036                 e = NFS4_OK;
6037 
6038         } else if ((cp->rc_sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
6039                 *sp = cp->rc_sysidt;
6040                 e = NFS4_OK;
6041 
6042                 NFS4_DEBUG(rfs4_debug, (CE_NOTE,
6043                     "rfs4_client_sysid: allocated 0x%x\n", *sp));
6044         } else
6045                 e = NFS4ERR_DELAY;
6046 
6047         rfs4_dbe_unlock(cp->rc_dbe);
6048         return (e);
6049 }
6050 
6051 #if defined(DEBUG) && ! defined(lint)
6052 static void lock_print(char *str, int operation, struct flock64 *flk)
6053 {
6054         char *op, *type;
6055 
6056         switch (operation) {
6057         case F_GETLK: op = "F_GETLK";
6058                 break;
6059         case F_SETLK: op = "F_SETLK";
6060                 break;
6061         case F_SETLK_NBMAND: op = "F_SETLK_NBMAND";
6062                 break;
6063         default: op = "F_UNKNOWN";
6064                 break;
6065         }
6066         switch (flk->l_type) {
6067         case F_UNLCK: type = "F_UNLCK";
6068                 break;
6069         case F_RDLCK: type = "F_RDLCK";
6070                 break;
6071         case F_WRLCK: type = "F_WRLCK";
6072                 break;
6073         default: type = "F_UNKNOWN";
6074                 break;
6075         }
6076 
6077         ASSERT(flk->l_whence == 0);
6078         cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
6079             str, op, type, (longlong_t)flk->l_start,
6080             flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid);
6081 }
6082 
6083 #define LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
6084 #else
6085 #define LOCK_PRINT(d, s, t, f)
6086 #endif
6087 
6088 /*ARGSUSED*/
6089 static bool_t
6090 creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
6091 {
6092         return (TRUE);
6093 }
6094 
6095 /*
6096  * Look up the pathname using the vp in cs as the directory vnode.
6097  * cs->vp will be the vnode for the file on success
6098  */
6099 
6100 static nfsstat4
6101 rfs4_lookup(component4 *component, struct svc_req *req,
6102     struct compound_state *cs)
6103 {
6104         char *nm;
6105         uint32_t len;
6106         nfsstat4 status;
6107         struct sockaddr *ca;
6108         char *name;
6109 
6110         if (cs->vp == NULL) {
6111                 return (NFS4ERR_NOFILEHANDLE);
6112         }
6113         if (cs->vp->v_type != VDIR) {
6114                 return (NFS4ERR_NOTDIR);
6115         }
6116 
6117         status = utf8_dir_verify(component);
6118         if (status != NFS4_OK)
6119                 return (status);
6120 
6121         nm = utf8_to_fn(component, &len, NULL);
6122         if (nm == NULL) {
6123                 return (NFS4ERR_INVAL);
6124         }
6125 
6126         if (len > MAXNAMELEN) {
6127                 kmem_free(nm, len);
6128                 return (NFS4ERR_NAMETOOLONG);
6129         }
6130 
6131         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6132         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6133             MAXPATHLEN + 1);
6134 
6135         if (name == NULL) {
6136                 kmem_free(nm, len);
6137                 return (NFS4ERR_INVAL);
6138         }
6139 
6140         status = do_rfs4_op_lookup(name, req, cs);
6141 
6142         if (name != nm)
6143                 kmem_free(name, MAXPATHLEN + 1);
6144 
6145         kmem_free(nm, len);
6146 
6147         return (status);
6148 }
6149 
6150 static nfsstat4
6151 rfs4_lookupfile(component4 *component, struct svc_req *req,
6152     struct compound_state *cs, uint32_t access, change_info4 *cinfo)
6153 {
6154         nfsstat4 status;
6155         vnode_t *dvp = cs->vp;
6156         vattr_t bva, ava, fva;
6157         int error;
6158 
6159         /* Get "before" change value */
6160         bva.va_mask = AT_CTIME|AT_SEQ;
6161         error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6162         if (error)
6163                 return (puterrno4(error));
6164 
6165         /* rfs4_lookup may VN_RELE directory */
6166         VN_HOLD(dvp);
6167 
6168         status = rfs4_lookup(component, req, cs);
6169         if (status != NFS4_OK) {
6170                 VN_RELE(dvp);
6171                 return (status);
6172         }
6173 
6174         /*
6175          * Get "after" change value, if it fails, simply return the
6176          * before value.
6177          */
6178         ava.va_mask = AT_CTIME|AT_SEQ;
6179         if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6180                 ava.va_ctime = bva.va_ctime;
6181                 ava.va_seq = 0;
6182         }
6183         VN_RELE(dvp);
6184 
6185         /*
6186          * Validate the file is a file
6187          */
6188         fva.va_mask = AT_TYPE|AT_MODE;
6189         error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL);
6190         if (error)
6191                 return (puterrno4(error));
6192 
6193         if (fva.va_type != VREG) {
6194                 if (fva.va_type == VDIR)
6195                         return (NFS4ERR_ISDIR);
6196                 if (fva.va_type == VLNK)
6197                         return (NFS4ERR_SYMLINK);
6198                 return (NFS4ERR_INVAL);
6199         }
6200 
6201         NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
6202         NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6203 
6204         /*
6205          * It is undefined if VOP_LOOKUP will change va_seq, so
6206          * cinfo.atomic = TRUE only if we have
6207          * non-zero va_seq's, and they have not changed.
6208          */
6209         if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
6210                 cinfo->atomic = TRUE;
6211         else
6212                 cinfo->atomic = FALSE;
6213 
6214         /* Check for mandatory locking */
6215         cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
6216         return (check_open_access(access, cs, req));
6217 }
6218 
6219 static nfsstat4
6220 create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
6221     cred_t *cr, vnode_t **vpp, bool_t *created)
6222 {
6223         int error;
6224         nfsstat4 status = NFS4_OK;
6225         vattr_t va;
6226 
6227 tryagain:
6228 
6229         /*
6230          * The file open mode used is VWRITE.  If the client needs
6231          * some other semantic, then it should do the access checking
6232          * itself.  It would have been nice to have the file open mode
6233          * passed as part of the arguments.
6234          */
6235 
6236         *created = TRUE;
6237         error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL);
6238 
6239         if (error) {
6240                 *created = FALSE;
6241 
6242                 /*
6243                  * If we got something other than file already exists
6244                  * then just return this error.  Otherwise, we got
6245                  * EEXIST.  If we were doing a GUARDED create, then
6246                  * just return this error.  Otherwise, we need to
6247                  * make sure that this wasn't a duplicate of an
6248                  * exclusive create request.
6249                  *
6250                  * The assumption is made that a non-exclusive create
6251                  * request will never return EEXIST.
6252                  */
6253 
6254                 if (error != EEXIST || mode == GUARDED4) {
6255                         status = puterrno4(error);
6256                         return (status);
6257                 }
6258                 error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr,
6259                     NULL, NULL, NULL);
6260 
6261                 if (error) {
6262                         /*
6263                          * We couldn't find the file that we thought that
6264                          * we just created.  So, we'll just try creating
6265                          * it again.
6266                          */
6267                         if (error == ENOENT)
6268                                 goto tryagain;
6269 
6270                         status = puterrno4(error);
6271                         return (status);
6272                 }
6273 
6274                 if (mode == UNCHECKED4) {
6275                         /* existing object must be regular file */
6276                         if ((*vpp)->v_type != VREG) {
6277                                 if ((*vpp)->v_type == VDIR)
6278                                         status = NFS4ERR_ISDIR;
6279                                 else if ((*vpp)->v_type == VLNK)
6280                                         status = NFS4ERR_SYMLINK;
6281                                 else
6282                                         status = NFS4ERR_INVAL;
6283                                 VN_RELE(*vpp);
6284                                 return (status);
6285                         }
6286 
6287                         return (NFS4_OK);
6288                 }
6289 
6290                 /* Check for duplicate request */
6291                 va.va_mask = AT_MTIME;
6292                 error = VOP_GETATTR(*vpp, &va, 0, cr, NULL);
6293                 if (!error) {
6294                         /* We found the file */
6295                         const timestruc_t *mtime = &vap->va_mtime;
6296 
6297                         if (va.va_mtime.tv_sec != mtime->tv_sec ||
6298                             va.va_mtime.tv_nsec != mtime->tv_nsec) {
6299                                 /* but its not our creation */
6300                                 VN_RELE(*vpp);
6301                                 return (NFS4ERR_EXIST);
6302                         }
6303                         *created = TRUE; /* retrans of create == created */
6304                         return (NFS4_OK);
6305                 }
6306                 VN_RELE(*vpp);
6307                 return (NFS4ERR_EXIST);
6308         }
6309 
6310         return (NFS4_OK);
6311 }
6312 
6313 static nfsstat4
6314 check_open_access(uint32_t access, struct compound_state *cs,
6315     struct svc_req *req)
6316 {
6317         int error;
6318         vnode_t *vp;
6319         bool_t readonly;
6320         cred_t *cr = cs->cr;
6321 
6322         /* For now we don't allow mandatory locking as per V2/V3 */
6323         if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
6324                 return (NFS4ERR_ACCESS);
6325         }
6326 
6327         vp = cs->vp;
6328         ASSERT(cr != NULL && vp->v_type == VREG);
6329 
6330         /*
6331          * If the file system is exported read only and we are trying
6332          * to open for write, then return NFS4ERR_ROFS
6333          */
6334 
6335         readonly = rdonly4(req, cs);
6336 
6337         if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
6338                 return (NFS4ERR_ROFS);
6339 
6340         if (access & OPEN4_SHARE_ACCESS_READ) {
6341                 if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) &&
6342                     (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) {
6343                         return (NFS4ERR_ACCESS);
6344                 }
6345         }
6346 
6347         if (access & OPEN4_SHARE_ACCESS_WRITE) {
6348                 error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
6349                 if (error)
6350                         return (NFS4ERR_ACCESS);
6351         }
6352 
6353         return (NFS4_OK);
6354 }
6355 
6356 static nfsstat4
6357 rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
6358     change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
6359 {
6360         struct nfs4_svgetit_arg sarg;
6361         struct nfs4_ntov_table ntov;
6362 
6363         bool_t ntov_table_init = FALSE;
6364         struct statvfs64 sb;
6365         nfsstat4 status;
6366         vnode_t *vp;
6367         vattr_t bva, ava, iva, cva, *vap;
6368         vnode_t *dvp;
6369         timespec32_t *mtime;
6370         char *nm = NULL;
6371         uint_t buflen;
6372         bool_t created;
6373         bool_t setsize = FALSE;
6374         len_t reqsize;
6375         int error;
6376         bool_t trunc;
6377         caller_context_t ct;
6378         component4 *component;
6379         bslabel_t *clabel;
6380         struct sockaddr *ca;
6381         char *name = NULL;
6382 
6383         sarg.sbp = &sb;
6384         sarg.is_referral = B_FALSE;
6385 
6386         dvp = cs->vp;
6387 
6388         /* Check if the file system is read only */
6389         if (rdonly4(req, cs))
6390                 return (NFS4ERR_ROFS);
6391 
6392         /* check the label of including directory */
6393         if (is_system_labeled()) {
6394                 ASSERT(req->rq_label != NULL);
6395                 clabel = req->rq_label;
6396                 DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
6397                     "got client label from request(1)",
6398                     struct svc_req *, req);
6399                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
6400                         if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
6401                             cs->exi)) {
6402                                 return (NFS4ERR_ACCESS);
6403                         }
6404                 }
6405         }
6406 
6407         /*
6408          * Get the last component of path name in nm. cs will reference
6409          * the including directory on success.
6410          */
6411         component = &args->open_claim4_u.file;
6412         status = utf8_dir_verify(component);
6413         if (status != NFS4_OK)
6414                 return (status);
6415 
6416         nm = utf8_to_fn(component, &buflen, NULL);
6417 
6418         if (nm == NULL)
6419                 return (NFS4ERR_RESOURCE);
6420 
6421         if (buflen > MAXNAMELEN) {
6422                 kmem_free(nm, buflen);
6423                 return (NFS4ERR_NAMETOOLONG);
6424         }
6425 
6426         bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
6427         error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6428         if (error) {
6429                 kmem_free(nm, buflen);
6430                 return (puterrno4(error));
6431         }
6432 
6433         if (bva.va_type != VDIR) {
6434                 kmem_free(nm, buflen);
6435                 return (NFS4ERR_NOTDIR);
6436         }
6437 
6438         NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
6439 
6440         switch (args->mode) {
6441         case GUARDED4:
6442                 /*FALLTHROUGH*/
6443         case UNCHECKED4:
6444                 nfs4_ntov_table_init(&ntov);
6445                 ntov_table_init = TRUE;
6446 
6447                 *attrset = 0;
6448                 status = do_rfs4_set_attrs(attrset,
6449                     &args->createhow4_u.createattrs,
6450                     cs, &sarg, &ntov, NFS4ATTR_SETIT);
6451 
6452                 if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
6453                     sarg.vap->va_type != VREG) {
6454                         if (sarg.vap->va_type == VDIR)
6455                                 status = NFS4ERR_ISDIR;
6456                         else if (sarg.vap->va_type == VLNK)
6457                                 status = NFS4ERR_SYMLINK;
6458                         else
6459                                 status = NFS4ERR_INVAL;
6460                 }
6461 
6462                 if (status != NFS4_OK) {
6463                         kmem_free(nm, buflen);
6464                         nfs4_ntov_table_free(&ntov, &sarg);
6465                         *attrset = 0;
6466                         return (status);
6467                 }
6468 
6469                 vap = sarg.vap;
6470                 vap->va_type = VREG;
6471                 vap->va_mask |= AT_TYPE;
6472 
6473                 if ((vap->va_mask & AT_MODE) == 0) {
6474                         vap->va_mask |= AT_MODE;
6475                         vap->va_mode = (mode_t)0600;
6476                 }
6477 
6478                 if (vap->va_mask & AT_SIZE) {
6479 
6480                         /* Disallow create with a non-zero size */
6481 
6482                         if ((reqsize = sarg.vap->va_size) != 0) {
6483                                 kmem_free(nm, buflen);
6484                                 nfs4_ntov_table_free(&ntov, &sarg);
6485                                 *attrset = 0;
6486                                 return (NFS4ERR_INVAL);
6487                         }
6488                         setsize = TRUE;
6489                 }
6490                 break;
6491 
6492         case EXCLUSIVE4:
6493                 /* prohibit EXCL create of named attributes */
6494                 if (dvp->v_flag & V_XATTRDIR) {
6495                         kmem_free(nm, buflen);
6496                         *attrset = 0;
6497                         return (NFS4ERR_INVAL);
6498                 }
6499 
6500                 cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
6501                 cva.va_type = VREG;
6502                 /*
6503                  * Ensure no time overflows. Assumes underlying
6504                  * filesystem supports at least 32 bits.
6505                  * Truncate nsec to usec resolution to allow valid
6506                  * compares even if the underlying filesystem truncates.
6507                  */
6508                 mtime = (timespec32_t *)&args->createhow4_u.createverf;
6509                 cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
6510                 cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
6511                 cva.va_mode = (mode_t)0;
6512                 vap = &cva;
6513 
6514                 /*
6515                  * For EXCL create, attrset is set to the server attr
6516                  * used to cache the client's verifier.
6517                  */
6518                 *attrset = FATTR4_TIME_MODIFY_MASK;
6519                 break;
6520         }
6521 
6522         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6523         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6524             MAXPATHLEN  + 1);
6525 
6526         if (name == NULL) {
6527                 kmem_free(nm, buflen);
6528                 return (NFS4ERR_SERVERFAULT);
6529         }
6530 
6531         status = create_vnode(dvp, name, vap, args->mode,
6532             cs->cr, &vp, &created);
6533         if (nm != name)
6534                 kmem_free(name, MAXPATHLEN + 1);
6535         kmem_free(nm, buflen);
6536 
6537         if (status != NFS4_OK) {
6538                 if (ntov_table_init)
6539                         nfs4_ntov_table_free(&ntov, &sarg);
6540                 *attrset = 0;
6541                 return (status);
6542         }
6543 
6544         trunc = (setsize && !created);
6545 
6546         if (args->mode != EXCLUSIVE4) {
6547                 bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
6548 
6549                 /*
6550                  * True verification that object was created with correct
6551                  * attrs is impossible.  The attrs could have been changed
6552                  * immediately after object creation.  If attributes did
6553                  * not verify, the only recourse for the server is to
6554                  * destroy the object.  Maybe if some attrs (like gid)
6555                  * are set incorrectly, the object should be destroyed;
6556                  * however, seems bad as a default policy.  Do we really
6557                  * want to destroy an object over one of the times not
6558                  * verifying correctly?  For these reasons, the server
6559                  * currently sets bits in attrset for createattrs
6560                  * that were set; however, no verification is done.
6561                  *
6562                  * vmask_to_nmask accounts for vattr bits set on create
6563                  *      [do_rfs4_set_attrs() only sets resp bits for
6564                  *       non-vattr/vfs bits.]
6565                  * Mask off any bits we set by default so as not to return
6566                  * more attrset bits than were requested in createattrs
6567                  */
6568                 if (created) {
6569                         nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
6570                         *attrset &= createmask;
6571                 } else {
6572                         /*
6573                          * We did not create the vnode (we tried but it
6574                          * already existed).  In this case, the only createattr
6575                          * that the spec allows the server to set is size,
6576                          * and even then, it can only be set if it is 0.
6577                          */
6578                         *attrset = 0;
6579                         if (trunc)
6580                                 *attrset = FATTR4_SIZE_MASK;
6581                 }
6582         }
6583         if (ntov_table_init)
6584                 nfs4_ntov_table_free(&ntov, &sarg);
6585 
6586         /*
6587          * Get the initial "after" sequence number, if it fails,
6588          * set to zero, time to before.
6589          */
6590         iva.va_mask = AT_CTIME|AT_SEQ;
6591         if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) {
6592                 iva.va_seq = 0;
6593                 iva.va_ctime = bva.va_ctime;
6594         }
6595 
6596         /*
6597          * create_vnode attempts to create the file exclusive,
6598          * if it already exists the VOP_CREATE will fail and
6599          * may not increase va_seq. It is atomic if
6600          * we haven't changed the directory, but if it has changed
6601          * we don't know what changed it.
6602          */
6603         if (!created) {
6604                 if (bva.va_seq && iva.va_seq &&
6605                     bva.va_seq == iva.va_seq)
6606                         cinfo->atomic = TRUE;
6607                 else
6608                         cinfo->atomic = FALSE;
6609                 NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
6610         } else {
6611                 /*
6612                  * The entry was created, we need to sync the
6613                  * directory metadata.
6614                  */
6615                 (void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
6616 
6617                 /*
6618                  * Get "after" change value, if it fails, simply return the
6619                  * before value.
6620                  */
6621                 ava.va_mask = AT_CTIME|AT_SEQ;
6622                 if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6623                         ava.va_ctime = bva.va_ctime;
6624                         ava.va_seq = 0;
6625                 }
6626 
6627                 NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6628 
6629                 /*
6630                  * The cinfo->atomic = TRUE only if we have
6631                  * non-zero va_seq's, and it has incremented by exactly one
6632                  * during the create_vnode and it didn't
6633                  * change during the VOP_FSYNC.
6634                  */
6635                 if (bva.va_seq && iva.va_seq && ava.va_seq &&
6636                     iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
6637                         cinfo->atomic = TRUE;
6638                 else
6639                         cinfo->atomic = FALSE;
6640         }
6641 
6642         /* Check for mandatory locking and that the size gets set. */
6643         cva.va_mask = AT_MODE;
6644         if (setsize)
6645                 cva.va_mask |= AT_SIZE;
6646 
6647         /* Assume the worst */
6648         cs->mandlock = TRUE;
6649 
6650         if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) {
6651                 cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
6652 
6653                 /*
6654                  * Truncate the file if necessary; this would be
6655                  * the case for create over an existing file.
6656                  */
6657 
6658                 if (trunc) {
6659                         int in_crit = 0;
6660                         rfs4_file_t *fp;
6661                         nfs4_srv_t *nsrv4;
6662                         bool_t create = FALSE;
6663 
6664                         /*
6665                          * We are writing over an existing file.
6666                          * Check to see if we need to recall a delegation.
6667                          */
6668                         nsrv4 = nfs4_get_srv();
6669                         rfs4_hold_deleg_policy(nsrv4);
6670                         if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
6671                                 if (rfs4_check_delegated_byfp(FWRITE, fp,
6672                                     (reqsize == 0), FALSE, FALSE, &clientid)) {
6673                                         rfs4_file_rele(fp);
6674                                         rfs4_rele_deleg_policy(nsrv4);
6675                                         VN_RELE(vp);
6676                                         *attrset = 0;
6677                                         return (NFS4ERR_DELAY);
6678                                 }
6679                                 rfs4_file_rele(fp);
6680                         }
6681                         rfs4_rele_deleg_policy(nsrv4);
6682 
6683                         if (nbl_need_check(vp)) {
6684                                 in_crit = 1;
6685 
6686                                 ASSERT(reqsize == 0);
6687 
6688                                 nbl_start_crit(vp, RW_READER);
6689                                 if (nbl_conflict(vp, NBL_WRITE, 0,
6690                                     cva.va_size, 0, NULL)) {
6691                                         in_crit = 0;
6692                                         nbl_end_crit(vp);
6693                                         VN_RELE(vp);
6694                                         *attrset = 0;
6695                                         return (NFS4ERR_ACCESS);
6696                                 }
6697                         }
6698                         ct.cc_sysid = 0;
6699                         ct.cc_pid = 0;
6700                         ct.cc_caller_id = nfs4_srv_caller_id;
6701                         ct.cc_flags = CC_DONTBLOCK;
6702 
6703                         cva.va_mask = AT_SIZE;
6704                         cva.va_size = reqsize;
6705                         (void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
6706                         if (in_crit)
6707                                 nbl_end_crit(vp);
6708                 }
6709         }
6710 
6711         error = makefh4(&cs->fh, vp, cs->exi);
6712 
6713         /*
6714          * Force modified data and metadata out to stable storage.
6715          */
6716         (void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
6717 
6718         if (error) {
6719                 VN_RELE(vp);
6720                 *attrset = 0;
6721                 return (puterrno4(error));
6722         }
6723 
6724         /* if parent dir is attrdir, set namedattr fh flag */
6725         if (dvp->v_flag & V_XATTRDIR)
6726                 set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
6727 
6728         if (cs->vp)
6729                 VN_RELE(cs->vp);
6730 
6731         cs->vp = vp;
6732 
6733         /*
6734          * if we did not create the file, we will need to check
6735          * the access bits on the file
6736          */
6737 
6738         if (!created) {
6739                 if (setsize)
6740                         args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
6741                 status = check_open_access(args->share_access, cs, req);
6742                 if (status != NFS4_OK)
6743                         *attrset = 0;
6744         }
6745         return (status);
6746 }
6747 
6748 /*ARGSUSED*/
6749 static void
6750 rfs4_do_open(struct compound_state *cs, struct svc_req *req,
6751     rfs4_openowner_t *oo, delegreq_t deleg,
6752     uint32_t access, uint32_t deny,
6753     OPEN4res *resp, int deleg_cur)
6754 {
6755         /* XXX Currently not using req  */
6756         rfs4_state_t *sp;
6757         rfs4_file_t *fp;
6758         bool_t screate = TRUE;
6759         bool_t fcreate = TRUE;
6760         uint32_t open_a, share_a;
6761         uint32_t open_d, share_d;
6762         rfs4_deleg_state_t *dsp;
6763         sysid_t sysid;
6764         nfsstat4 status;
6765         caller_context_t ct;
6766         int fflags = 0;
6767         int recall = 0;
6768         int err;
6769         int first_open;
6770 
6771         /* get the file struct and hold a lock on it during initial open */
6772         fp = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
6773         if (fp == NULL) {
6774                 resp->status = NFS4ERR_RESOURCE;
6775                 DTRACE_PROBE1(nfss__e__do__open1, nfsstat4, resp->status);
6776                 return;
6777         }
6778 
6779         sp = rfs4_findstate_by_owner_file(oo, fp, &screate);
6780         if (sp == NULL) {
6781                 resp->status = NFS4ERR_RESOURCE;
6782                 DTRACE_PROBE1(nfss__e__do__open2, nfsstat4, resp->status);
6783                 /* No need to keep any reference */
6784                 rw_exit(&fp->rf_file_rwlock);
6785                 rfs4_file_rele(fp);
6786                 return;
6787         }
6788 
6789         /* try to get the sysid before continuing */
6790         if ((status = rfs4_client_sysid(oo->ro_client, &sysid)) != NFS4_OK) {
6791                 resp->status = status;
6792                 rfs4_file_rele(fp);
6793                 /* Not a fully formed open; "close" it */
6794                 if (screate == TRUE)
6795                         rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6796                 rfs4_state_rele(sp);
6797                 return;
6798         }
6799 
6800         /* Calculate the fflags for this OPEN. */
6801         if (access & OPEN4_SHARE_ACCESS_READ)
6802                 fflags |= FREAD;
6803         if (access & OPEN4_SHARE_ACCESS_WRITE)
6804                 fflags |= FWRITE;
6805 
6806         rfs4_dbe_lock(sp->rs_dbe);
6807 
6808         /*
6809          * Calculate the new deny and access mode that this open is adding to
6810          * the file for this open owner;
6811          */
6812         open_d = (deny & ~sp->rs_open_deny);
6813         open_a = (access & ~sp->rs_open_access);
6814 
6815         /*
6816          * Calculate the new share access and share deny modes that this open
6817          * is adding to the file for this open owner;
6818          */
6819         share_a = (access & ~sp->rs_share_access);
6820         share_d = (deny & ~sp->rs_share_deny);
6821 
6822         first_open = (sp->rs_open_access & OPEN4_SHARE_ACCESS_BOTH) == 0;
6823 
6824         /*
6825          * Check to see the client has already sent an open for this
6826          * open owner on this file with the same share/deny modes.
6827          * If so, we don't need to check for a conflict and we don't
6828          * need to add another shrlock.  If not, then we need to
6829          * check for conflicts in deny and access before checking for
6830          * conflicts in delegation.  We don't want to recall a
6831          * delegation based on an open that will eventually fail based
6832          * on shares modes.
6833          */
6834 
6835         if (share_a || share_d) {
6836                 if ((err = rfs4_share(sp, access, deny)) != 0) {
6837                         rfs4_dbe_unlock(sp->rs_dbe);
6838                         resp->status = err;
6839 
6840                         rfs4_file_rele(fp);
6841                         /* Not a fully formed open; "close" it */
6842                         if (screate == TRUE)
6843                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6844                         rfs4_state_rele(sp);
6845                         return;
6846                 }
6847         }
6848 
6849         rfs4_dbe_lock(fp->rf_dbe);
6850 
6851         /*
6852          * Check to see if this file is delegated and if so, if a
6853          * recall needs to be done.
6854          */
6855         if (rfs4_check_recall(sp, access)) {
6856                 rfs4_dbe_unlock(fp->rf_dbe);
6857                 rfs4_dbe_unlock(sp->rs_dbe);
6858                 rfs4_recall_deleg(fp, FALSE, sp->rs_owner->ro_client);
6859                 delay(NFS4_DELEGATION_CONFLICT_DELAY);
6860                 rfs4_dbe_lock(sp->rs_dbe);
6861 
6862                 /* if state closed while lock was dropped */
6863                 if (sp->rs_closed) {
6864                         if (share_a || share_d)
6865                                 (void) rfs4_unshare(sp);
6866                         rfs4_dbe_unlock(sp->rs_dbe);
6867                         rfs4_file_rele(fp);
6868                         /* Not a fully formed open; "close" it */
6869                         if (screate == TRUE)
6870                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6871                         rfs4_state_rele(sp);
6872                         resp->status = NFS4ERR_OLD_STATEID;
6873                         return;
6874                 }
6875 
6876                 rfs4_dbe_lock(fp->rf_dbe);
6877                 /* Let's see if the delegation was returned */
6878                 if (rfs4_check_recall(sp, access)) {
6879                         rfs4_dbe_unlock(fp->rf_dbe);
6880                         if (share_a || share_d)
6881                                 (void) rfs4_unshare(sp);
6882                         rfs4_dbe_unlock(sp->rs_dbe);
6883                         rfs4_file_rele(fp);
6884                         rfs4_update_lease(sp->rs_owner->ro_client);
6885 
6886                         /* Not a fully formed open; "close" it */
6887                         if (screate == TRUE)
6888                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6889                         rfs4_state_rele(sp);
6890                         resp->status = NFS4ERR_DELAY;
6891                         return;
6892                 }
6893         }
6894         /*
6895          * the share check passed and any delegation conflict has been
6896          * taken care of, now call vop_open.
6897          * if this is the first open then call vop_open with fflags.
6898          * if not, call vn_open_upgrade with just the upgrade flags.
6899          *
6900          * if the file has been opened already, it will have the current
6901          * access mode in the state struct.  if it has no share access, then
6902          * this is a new open.
6903          *
6904          * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6905          * call VOP_OPEN(), just do the open upgrade.
6906          */
6907         if (first_open && !deleg_cur) {
6908                 ct.cc_sysid = sysid;
6909                 ct.cc_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
6910                 ct.cc_caller_id = nfs4_srv_caller_id;
6911                 ct.cc_flags = CC_DONTBLOCK;
6912                 err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6913                 if (err) {
6914                         rfs4_dbe_unlock(fp->rf_dbe);
6915                         if (share_a || share_d)
6916                                 (void) rfs4_unshare(sp);
6917                         rfs4_dbe_unlock(sp->rs_dbe);
6918                         rfs4_file_rele(fp);
6919 
6920                         /* Not a fully formed open; "close" it */
6921                         if (screate == TRUE)
6922                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6923                         rfs4_state_rele(sp);
6924                         /* check if a monitor detected a delegation conflict */
6925                         if (err == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
6926                                 resp->status = NFS4ERR_DELAY;
6927                         else
6928                                 resp->status = NFS4ERR_SERVERFAULT;
6929                         return;
6930                 }
6931         } else { /* open upgrade */
6932                 /*
6933                  * calculate the fflags for the new mode that is being added
6934                  * by this upgrade.
6935                  */
6936                 fflags = 0;
6937                 if (open_a & OPEN4_SHARE_ACCESS_READ)
6938                         fflags |= FREAD;
6939                 if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6940                         fflags |= FWRITE;
6941                 vn_open_upgrade(cs->vp, fflags);
6942         }
6943         sp->rs_open_access |= access;
6944         sp->rs_open_deny |= deny;
6945 
6946         if (open_d & OPEN4_SHARE_DENY_READ)
6947                 fp->rf_deny_read++;
6948         if (open_d & OPEN4_SHARE_DENY_WRITE)
6949                 fp->rf_deny_write++;
6950         fp->rf_share_deny |= deny;
6951 
6952         if (open_a & OPEN4_SHARE_ACCESS_READ)
6953                 fp->rf_access_read++;
6954         if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6955                 fp->rf_access_write++;
6956         fp->rf_share_access |= access;
6957 
6958         /*
6959          * Check for delegation here. if the deleg argument is not
6960          * DELEG_ANY, then this is a reclaim from a client and
6961          * we must honor the delegation requested. If necessary we can
6962          * set the recall flag.
6963          */
6964 
6965         dsp = rfs4_grant_delegation(deleg, sp, &recall);
6966 
6967         cs->deleg = (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE);
6968 
6969         next_stateid(&sp->rs_stateid);
6970 
6971         resp->stateid = sp->rs_stateid.stateid;
6972 
6973         rfs4_dbe_unlock(fp->rf_dbe);
6974         rfs4_dbe_unlock(sp->rs_dbe);
6975 
6976         if (dsp) {
6977                 rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
6978                 rfs4_deleg_state_rele(dsp);
6979         }
6980 
6981         rfs4_file_rele(fp);
6982         rfs4_state_rele(sp);
6983 
6984         resp->status = NFS4_OK;
6985 }
6986 
6987 /*ARGSUSED*/
6988 static void
6989 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
6990     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6991 {
6992         change_info4 *cinfo = &resp->cinfo;
6993         bitmap4 *attrset = &resp->attrset;
6994 
6995         if (args->opentype == OPEN4_NOCREATE)
6996                 resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
6997                     req, cs, args->share_access, cinfo);
6998         else {
6999                 /* inhibit delegation grants during exclusive create */
7000 
7001                 if (args->mode == EXCLUSIVE4)
7002                         rfs4_disable_delegation();
7003 
7004                 resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
7005                     oo->ro_client->rc_clientid);
7006         }
7007 
7008         if (resp->status == NFS4_OK) {
7009 
7010                 /* cs->vp cs->fh now reference the desired file */
7011 
7012                 rfs4_do_open(cs, req, oo,
7013                     oo->ro_need_confirm ? DELEG_NONE : DELEG_ANY,
7014                     args->share_access, args->share_deny, resp, 0);
7015 
7016                 /*
7017                  * If rfs4_createfile set attrset, we must
7018                  * clear this attrset before the response is copied.
7019                  */
7020                 if (resp->status != NFS4_OK && resp->attrset) {
7021                         resp->attrset = 0;
7022                 }
7023         }
7024         else
7025                 *cs->statusp = resp->status;
7026 
7027         if (args->mode == EXCLUSIVE4)
7028                 rfs4_enable_delegation();
7029 }
7030 
7031 /*ARGSUSED*/
7032 static void
7033 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
7034     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7035 {
7036         change_info4 *cinfo = &resp->cinfo;
7037         vattr_t va;
7038         vtype_t v_type = cs->vp->v_type;
7039         int error = 0;
7040 
7041         /* Verify that we have a regular file */
7042         if (v_type != VREG) {
7043                 if (v_type == VDIR)
7044                         resp->status = NFS4ERR_ISDIR;
7045                 else if (v_type == VLNK)
7046                         resp->status = NFS4ERR_SYMLINK;
7047                 else
7048                         resp->status = NFS4ERR_INVAL;
7049                 return;
7050         }
7051 
7052         va.va_mask = AT_MODE|AT_UID;
7053         error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
7054         if (error) {
7055                 resp->status = puterrno4(error);
7056                 return;
7057         }
7058 
7059         cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
7060 
7061         /*
7062          * Check if we have access to the file, Note the the file
7063          * could have originally been open UNCHECKED or GUARDED
7064          * with mode bits that will now fail, but there is nothing
7065          * we can really do about that except in the case that the
7066          * owner of the file is the one requesting the open.
7067          */
7068         if (crgetuid(cs->cr) != va.va_uid) {
7069                 resp->status = check_open_access(args->share_access, cs, req);
7070                 if (resp->status != NFS4_OK) {
7071                         return;
7072                 }
7073         }
7074 
7075         /*
7076          * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
7077          */
7078         cinfo->before = 0;
7079         cinfo->after = 0;
7080         cinfo->atomic = FALSE;
7081 
7082         rfs4_do_open(cs, req, oo,
7083             NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
7084             args->share_access, args->share_deny, resp, 0);
7085 }
7086 
7087 static void
7088 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
7089     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7090 {
7091         int error;
7092         nfsstat4 status;
7093         stateid4 stateid =
7094             args->open_claim4_u.delegate_cur_info.delegate_stateid;
7095         rfs4_deleg_state_t *dsp;
7096 
7097         /*
7098          * Find the state info from the stateid and confirm that the
7099          * file is delegated.  If the state openowner is the same as
7100          * the supplied openowner we're done. If not, get the file
7101          * info from the found state info. Use that file info to
7102          * create the state for this lock owner. Note solaris doen't
7103          * really need the pathname to find the file. We may want to
7104          * lookup the pathname and make sure that the vp exist and
7105          * matches the vp in the file structure. However it is
7106          * possible that the pathname nolonger exists (local process
7107          * unlinks the file), so this may not be that useful.
7108          */
7109 
7110         status = rfs4_get_deleg_state(&stateid, &dsp);
7111         if (status != NFS4_OK) {
7112                 resp->status = status;
7113                 return;
7114         }
7115 
7116         ASSERT(dsp->rds_finfo->rf_dinfo.rd_dtype != OPEN_DELEGATE_NONE);
7117 
7118         /*
7119          * New lock owner, create state. Since this was probably called
7120          * in response to a CB_RECALL we set deleg to DELEG_NONE
7121          */
7122 
7123         ASSERT(cs->vp != NULL);
7124         VN_RELE(cs->vp);
7125         VN_HOLD(dsp->rds_finfo->rf_vp);
7126         cs->vp = dsp->rds_finfo->rf_vp;
7127 
7128         if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
7129                 rfs4_deleg_state_rele(dsp);
7130                 *cs->statusp = resp->status = puterrno4(error);
7131                 return;
7132         }
7133 
7134         /* Mark progress for delegation returns */
7135         dsp->rds_finfo->rf_dinfo.rd_time_lastwrite = gethrestime_sec();
7136         rfs4_deleg_state_rele(dsp);
7137         rfs4_do_open(cs, req, oo, DELEG_NONE,
7138             args->share_access, args->share_deny, resp, 1);
7139 }
7140 
7141 /*ARGSUSED*/
7142 static void
7143 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
7144     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7145 {
7146         /*
7147          * Lookup the pathname, it must already exist since this file
7148          * was delegated.
7149          *
7150          * Find the file and state info for this vp and open owner pair.
7151          *      check that they are in fact delegated.
7152          *      check that the state access and deny modes are the same.
7153          *
7154          * Return the delgation possibly seting the recall flag.
7155          */
7156         rfs4_file_t *fp;
7157         rfs4_state_t *sp;
7158         bool_t create = FALSE;
7159         bool_t dcreate = FALSE;
7160         rfs4_deleg_state_t *dsp;
7161         nfsace4 *ace;
7162 
7163         /* Note we ignore oflags */
7164         resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
7165             req, cs, args->share_access, &resp->cinfo);
7166 
7167         if (resp->status != NFS4_OK) {
7168                 return;
7169         }
7170 
7171         /* get the file struct and hold a lock on it during initial open */
7172         fp = rfs4_findfile_withlock(cs->vp, NULL, &create);
7173         if (fp == NULL) {
7174                 resp->status = NFS4ERR_RESOURCE;
7175                 DTRACE_PROBE1(nfss__e__do_opendelprev1, nfsstat4, resp->status);
7176                 return;
7177         }
7178 
7179         sp = rfs4_findstate_by_owner_file(oo, fp, &create);
7180         if (sp == NULL) {
7181                 resp->status = NFS4ERR_SERVERFAULT;
7182                 DTRACE_PROBE1(nfss__e__do_opendelprev2, nfsstat4, resp->status);
7183                 rw_exit(&fp->rf_file_rwlock);
7184                 rfs4_file_rele(fp);
7185                 return;
7186         }
7187 
7188         rfs4_dbe_lock(sp->rs_dbe);
7189         rfs4_dbe_lock(fp->rf_dbe);
7190         if (args->share_access != sp->rs_share_access ||
7191             args->share_deny != sp->rs_share_deny ||
7192             sp->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
7193                 NFS4_DEBUG(rfs4_debug,
7194                     (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
7195                 rfs4_dbe_unlock(fp->rf_dbe);
7196                 rfs4_dbe_unlock(sp->rs_dbe);
7197                 rfs4_file_rele(fp);
7198                 rfs4_state_rele(sp);
7199                 resp->status = NFS4ERR_SERVERFAULT;
7200                 return;
7201         }
7202         rfs4_dbe_unlock(fp->rf_dbe);
7203         rfs4_dbe_unlock(sp->rs_dbe);
7204 
7205         dsp = rfs4_finddeleg(sp, &dcreate);
7206         if (dsp == NULL) {
7207                 rfs4_state_rele(sp);
7208                 rfs4_file_rele(fp);
7209                 resp->status = NFS4ERR_SERVERFAULT;
7210                 return;
7211         }
7212 
7213         next_stateid(&sp->rs_stateid);
7214 
7215         resp->stateid = sp->rs_stateid.stateid;
7216 
7217         resp->delegation.delegation_type = dsp->rds_dtype;
7218 
7219         if (dsp->rds_dtype == OPEN_DELEGATE_READ) {
7220                 open_read_delegation4 *rv =
7221                     &resp->delegation.open_delegation4_u.read;
7222 
7223                 rv->stateid = dsp->rds_delegid.stateid;
7224                 rv->recall = FALSE; /* no policy in place to set to TRUE */
7225                 ace = &rv->permissions;
7226         } else {
7227                 open_write_delegation4 *rv =
7228                     &resp->delegation.open_delegation4_u.write;
7229 
7230                 rv->stateid = dsp->rds_delegid.stateid;
7231                 rv->recall = FALSE;  /* no policy in place to set to TRUE */
7232                 ace = &rv->permissions;
7233                 rv->space_limit.limitby = NFS_LIMIT_SIZE;
7234                 rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
7235         }
7236 
7237         /* XXX For now */
7238         ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
7239         ace->flag = 0;
7240         ace->access_mask = 0;
7241         ace->who.utf8string_len = 0;
7242         ace->who.utf8string_val = 0;
7243 
7244         rfs4_deleg_state_rele(dsp);
7245         rfs4_state_rele(sp);
7246         rfs4_file_rele(fp);
7247 }
7248 
7249 typedef enum {
7250         NFS4_CHKSEQ_OKAY = 0,
7251         NFS4_CHKSEQ_REPLAY = 1,
7252         NFS4_CHKSEQ_BAD = 2
7253 } rfs4_chkseq_t;
7254 
7255 /*
7256  * Generic function for sequence number checks.
7257  */
7258 static rfs4_chkseq_t
7259 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
7260     seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
7261 {
7262         /* Same sequence ids and matching operations? */
7263         if (seqid == rqst_seq && resop->resop == lastop->resop) {
7264                 if (copyres == TRUE) {
7265                         rfs4_free_reply(resop);
7266                         rfs4_copy_reply(resop, lastop);
7267                 }
7268                 NFS4_DEBUG(rfs4_debug, (CE_NOTE,
7269                     "Replayed SEQID %d\n", seqid));
7270                 return (NFS4_CHKSEQ_REPLAY);
7271         }
7272 
7273         /* If the incoming sequence is not the next expected then it is bad */
7274         if (rqst_seq != seqid + 1) {
7275                 if (rqst_seq == seqid) {
7276                         NFS4_DEBUG(rfs4_debug,
7277                             (CE_NOTE, "BAD SEQID: Replayed sequence id "
7278                             "but last op was %d current op is %d\n",
7279                             lastop->resop, resop->resop));
7280                         return (NFS4_CHKSEQ_BAD);
7281                 }
7282                 NFS4_DEBUG(rfs4_debug,
7283                     (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
7284                     rqst_seq, seqid));
7285                 return (NFS4_CHKSEQ_BAD);
7286         }
7287 
7288         /* Everything okay -- next expected */
7289         return (NFS4_CHKSEQ_OKAY);
7290 }
7291 
7292 
7293 static rfs4_chkseq_t
7294 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7295 {
7296         rfs4_chkseq_t rc;
7297 
7298         rfs4_dbe_lock(op->ro_dbe);
7299         rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply, seqid, resop,
7300             TRUE);
7301         rfs4_dbe_unlock(op->ro_dbe);
7302 
7303         if (rc == NFS4_CHKSEQ_OKAY)
7304                 rfs4_update_lease(op->ro_client);
7305 
7306         return (rc);
7307 }
7308 
7309 static rfs4_chkseq_t
7310 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7311 {
7312         rfs4_chkseq_t rc;
7313 
7314         rfs4_dbe_lock(op->ro_dbe);
7315         rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply,
7316             olo_seqid, resop, FALSE);
7317         rfs4_dbe_unlock(op->ro_dbe);
7318 
7319         return (rc);
7320 }
7321 
7322 static rfs4_chkseq_t
7323 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lsp, nfs_resop4 *resop)
7324 {
7325         rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
7326 
7327         rfs4_dbe_lock(lsp->rls_dbe);
7328         if (!lsp->rls_skip_seqid_check)
7329                 rc = rfs4_check_seqid(lsp->rls_seqid, &lsp->rls_reply, seqid,
7330                     resop, TRUE);
7331         rfs4_dbe_unlock(lsp->rls_dbe);
7332 
7333         return (rc);
7334 }
7335 
7336 static void
7337 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
7338     struct svc_req *req, struct compound_state *cs)
7339 {
7340         OPEN4args *args = &argop->nfs_argop4_u.opopen;
7341         OPEN4res *resp = &resop->nfs_resop4_u.opopen;
7342         open_owner4 *owner = &args->owner;
7343         open_claim_type4 claim = args->claim;
7344         rfs4_client_t *cp;
7345         rfs4_openowner_t *oo;
7346         bool_t create;
7347         bool_t replay = FALSE;
7348         int can_reclaim;
7349 
7350         DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
7351             OPEN4args *, args);
7352 
7353         if (cs->vp == NULL) {
7354                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7355                 goto end;
7356         }
7357 
7358         /*
7359          * Need to check clientid and lease expiration first based on
7360          * error ordering and incrementing sequence id.
7361          */
7362         cp = rfs4_findclient_by_id(owner->clientid, FALSE);
7363         if (cp == NULL) {
7364                 *cs->statusp = resp->status =
7365                     rfs4_check_clientid(&owner->clientid, 0);
7366                 goto end;
7367         }
7368 
7369         if (rfs4_lease_expired(cp)) {
7370                 rfs4_client_close(cp);
7371                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
7372                 goto end;
7373         }
7374         can_reclaim = cp->rc_can_reclaim;
7375 
7376         /*
7377          * Find the open_owner for use from this point forward.  Take
7378          * care in updating the sequence id based on the type of error
7379          * being returned.
7380          */
7381 retry:
7382         create = TRUE;
7383         oo = rfs4_findopenowner(owner, &create, args->seqid);
7384         if (oo == NULL) {
7385                 *cs->statusp = resp->status = NFS4ERR_RESOURCE;
7386                 rfs4_client_rele(cp);
7387                 goto end;
7388         }
7389 
7390         /* Hold off access to the sequence space while the open is done */
7391         rfs4_sw_enter(&oo->ro_sw);
7392 
7393         /*
7394          * If the open_owner existed before at the server, then check
7395          * the sequence id.
7396          */
7397         if (!create && !oo->ro_postpone_confirm) {
7398                 switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
7399                 case NFS4_CHKSEQ_BAD:
7400                         if ((args->seqid > oo->ro_open_seqid) &&
7401                             oo->ro_need_confirm) {
7402                                 rfs4_free_opens(oo, TRUE, FALSE);
7403                                 rfs4_sw_exit(&oo->ro_sw);
7404                                 rfs4_openowner_rele(oo);
7405                                 goto retry;
7406                         }
7407                         resp->status = NFS4ERR_BAD_SEQID;
7408                         goto out;
7409                 case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
7410                         replay = TRUE;
7411                         goto out;
7412                 default:
7413                         break;
7414                 }
7415 
7416                 /*
7417                  * Sequence was ok and open owner exists
7418                  * check to see if we have yet to see an
7419                  * open_confirm.
7420                  */
7421                 if (oo->ro_need_confirm) {
7422                         rfs4_free_opens(oo, TRUE, FALSE);
7423                         rfs4_sw_exit(&oo->ro_sw);
7424                         rfs4_openowner_rele(oo);
7425                         goto retry;
7426                 }
7427         }
7428         /* Grace only applies to regular-type OPENs */
7429         if (rfs4_clnt_in_grace(cp) &&
7430             (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
7431                 *cs->statusp = resp->status = NFS4ERR_GRACE;
7432                 goto out;
7433         }
7434 
7435         /*
7436          * If previous state at the server existed then can_reclaim
7437          * will be set. If not reply NFS4ERR_NO_GRACE to the
7438          * client.
7439          */
7440         if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
7441                 *cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7442                 goto out;
7443         }
7444 
7445 
7446         /*
7447          * Reject the open if the client has missed the grace period
7448          */
7449         if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
7450                 *cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7451                 goto out;
7452         }
7453 
7454         /* Couple of up-front bookkeeping items */
7455         if (oo->ro_need_confirm) {
7456                 /*
7457                  * If this is a reclaim OPEN then we should not ask
7458                  * for a confirmation of the open_owner per the
7459                  * protocol specification.
7460                  */
7461                 if (claim == CLAIM_PREVIOUS)
7462                         oo->ro_need_confirm = FALSE;
7463                 else
7464                         resp->rflags |= OPEN4_RESULT_CONFIRM;
7465         }
7466         resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
7467 
7468         /*
7469          * If there is an unshared filesystem mounted on this vnode,
7470          * do not allow to open/create in this directory.
7471          */
7472         if (vn_ismntpt(cs->vp)) {
7473                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
7474                 goto out;
7475         }
7476 
7477         /*
7478          * access must READ, WRITE, or BOTH.  No access is invalid.
7479          * deny can be READ, WRITE, BOTH, or NONE.
7480          * bits not defined for access/deny are invalid.
7481          */
7482         if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
7483             (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
7484             (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
7485                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7486                 goto out;
7487         }
7488 
7489 
7490         /*
7491          * make sure attrset is zero before response is built.
7492          */
7493         resp->attrset = 0;
7494 
7495         switch (claim) {
7496         case CLAIM_NULL:
7497                 rfs4_do_opennull(cs, req, args, oo, resp);
7498                 break;
7499         case CLAIM_PREVIOUS:
7500                 rfs4_do_openprev(cs, req, args, oo, resp);
7501                 break;
7502         case CLAIM_DELEGATE_CUR:
7503                 rfs4_do_opendelcur(cs, req, args, oo, resp);
7504                 break;
7505         case CLAIM_DELEGATE_PREV:
7506                 rfs4_do_opendelprev(cs, req, args, oo, resp);
7507                 break;
7508         default:
7509                 resp->status = NFS4ERR_INVAL;
7510                 break;
7511         }
7512 
7513 out:
7514         rfs4_client_rele(cp);
7515 
7516         /* Catch sequence id handling here to make it a little easier */
7517         switch (resp->status) {
7518         case NFS4ERR_BADXDR:
7519         case NFS4ERR_BAD_SEQID:
7520         case NFS4ERR_BAD_STATEID:
7521         case NFS4ERR_NOFILEHANDLE:
7522         case NFS4ERR_RESOURCE:
7523         case NFS4ERR_STALE_CLIENTID:
7524         case NFS4ERR_STALE_STATEID:
7525                 /*
7526                  * The protocol states that if any of these errors are
7527                  * being returned, the sequence id should not be
7528                  * incremented.  Any other return requires an
7529                  * increment.
7530                  */
7531                 break;
7532         default:
7533                 /* Always update the lease in this case */
7534                 rfs4_update_lease(oo->ro_client);
7535 
7536                 /* Regular response - copy the result */
7537                 if (!replay)
7538                         rfs4_update_open_resp(oo, resop, &cs->fh);
7539 
7540                 /*
7541                  * REPLAY case: Only if the previous response was OK
7542                  * do we copy the filehandle.  If not OK, no
7543                  * filehandle to copy.
7544                  */
7545                 if (replay == TRUE &&
7546                     resp->status == NFS4_OK &&
7547                     oo->ro_reply_fh.nfs_fh4_val) {
7548                         /*
7549                          * If this is a replay, we must restore the
7550                          * current filehandle/vp to that of what was
7551                          * returned originally.  Try our best to do
7552                          * it.
7553                          */
7554                         nfs_fh4_fmt_t *fh_fmtp =
7555                             (nfs_fh4_fmt_t *)oo->ro_reply_fh.nfs_fh4_val;
7556 
7557                         cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
7558                             (fid_t *)&fh_fmtp->fh4_xlen, NULL);
7559 
7560                         if (cs->exi == NULL) {
7561                                 resp->status = NFS4ERR_STALE;
7562                                 goto finish;
7563                         }
7564 
7565                         VN_RELE(cs->vp);
7566 
7567                         cs->vp = nfs4_fhtovp(&oo->ro_reply_fh, cs->exi,
7568                             &resp->status);
7569 
7570                         if (cs->vp == NULL)
7571                                 goto finish;
7572 
7573                         nfs_fh4_copy(&oo->ro_reply_fh, &cs->fh);
7574                 }
7575 
7576                 /*
7577                  * If this was a replay, no need to update the
7578                  * sequence id. If the open_owner was not created on
7579                  * this pass, then update.  The first use of an
7580                  * open_owner will not bump the sequence id.
7581                  */
7582                 if (replay == FALSE && !create)
7583                         rfs4_update_open_sequence(oo);
7584                 /*
7585                  * If the client is receiving an error and the
7586                  * open_owner needs to be confirmed, there is no way
7587                  * to notify the client of this fact ignoring the fact
7588                  * that the server has no method of returning a
7589                  * stateid to confirm.  Therefore, the server needs to
7590                  * mark this open_owner in a way as to avoid the
7591                  * sequence id checking the next time the client uses
7592                  * this open_owner.
7593                  */
7594                 if (resp->status != NFS4_OK && oo->ro_need_confirm)
7595                         oo->ro_postpone_confirm = TRUE;
7596                 /*
7597                  * If OK response then clear the postpone flag and
7598                  * reset the sequence id to keep in sync with the
7599                  * client.
7600                  */
7601                 if (resp->status == NFS4_OK && oo->ro_postpone_confirm) {
7602                         oo->ro_postpone_confirm = FALSE;
7603                         oo->ro_open_seqid = args->seqid;
7604                 }
7605                 break;
7606         }
7607 
7608 finish:
7609         *cs->statusp = resp->status;
7610 
7611         rfs4_sw_exit(&oo->ro_sw);
7612         rfs4_openowner_rele(oo);
7613 
7614 end:
7615         DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7616             OPEN4res *, resp);
7617 }
7618 
7619 /*ARGSUSED*/
7620 void
7621 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7622     struct svc_req *req, struct compound_state *cs)
7623 {
7624         OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
7625         OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
7626         rfs4_state_t *sp;
7627         nfsstat4 status;
7628 
7629         DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7630             OPEN_CONFIRM4args *, args);
7631 
7632         if (cs->vp == NULL) {
7633                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7634                 goto out;
7635         }
7636 
7637         if (cs->vp->v_type != VREG) {
7638                 *cs->statusp = resp->status =
7639                     cs->vp->v_type == VDIR ? NFS4ERR_ISDIR : NFS4ERR_INVAL;
7640                 return;
7641         }
7642 
7643         status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7644         if (status != NFS4_OK) {
7645                 *cs->statusp = resp->status = status;
7646                 goto out;
7647         }
7648 
7649         /* Ensure specified filehandle matches */
7650         if (cs->vp != sp->rs_finfo->rf_vp) {
7651                 rfs4_state_rele(sp);
7652                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7653                 goto out;
7654         }
7655 
7656         /* hold off other access to open_owner while we tinker */
7657         rfs4_sw_enter(&sp->rs_owner->ro_sw);
7658 
7659         switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7660         case NFS4_CHECK_STATEID_OKAY:
7661                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7662                     resop) != 0) {
7663                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7664                         break;
7665                 }
7666                 /*
7667                  * If it is the appropriate stateid and determined to
7668                  * be "OKAY" then this means that the stateid does not
7669                  * need to be confirmed and the client is in error for
7670                  * sending an OPEN_CONFIRM.
7671                  */
7672                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7673                 break;
7674         case NFS4_CHECK_STATEID_OLD:
7675                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7676                 break;
7677         case NFS4_CHECK_STATEID_BAD:
7678                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7679                 break;
7680         case NFS4_CHECK_STATEID_EXPIRED:
7681                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
7682                 break;
7683         case NFS4_CHECK_STATEID_CLOSED:
7684                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7685                 break;
7686         case NFS4_CHECK_STATEID_REPLAY:
7687                 switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7688                     resop)) {
7689                 case NFS4_CHKSEQ_OKAY:
7690                         /*
7691                          * This is replayed stateid; if seqid matches
7692                          * next expected, then client is using wrong seqid.
7693                          */
7694                         /* fall through */
7695                 case NFS4_CHKSEQ_BAD:
7696                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7697                         break;
7698                 case NFS4_CHKSEQ_REPLAY:
7699                         /*
7700                          * Note this case is the duplicate case so
7701                          * resp->status is already set.
7702                          */
7703                         *cs->statusp = resp->status;
7704                         rfs4_update_lease(sp->rs_owner->ro_client);
7705                         break;
7706                 }
7707                 break;
7708         case NFS4_CHECK_STATEID_UNCONFIRMED:
7709                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7710                     resop) != NFS4_CHKSEQ_OKAY) {
7711                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7712                         break;
7713                 }
7714                 *cs->statusp = resp->status = NFS4_OK;
7715 
7716                 next_stateid(&sp->rs_stateid);
7717                 resp->open_stateid = sp->rs_stateid.stateid;
7718                 sp->rs_owner->ro_need_confirm = FALSE;
7719                 rfs4_update_lease(sp->rs_owner->ro_client);
7720                 rfs4_update_open_sequence(sp->rs_owner);
7721                 rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7722                 break;
7723         default:
7724                 ASSERT(FALSE);
7725                 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7726                 break;
7727         }
7728         rfs4_sw_exit(&sp->rs_owner->ro_sw);
7729         rfs4_state_rele(sp);
7730 
7731 out:
7732         DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7733             OPEN_CONFIRM4res *, resp);
7734 }
7735 
7736 /*ARGSUSED*/
7737 void
7738 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
7739     struct svc_req *req, struct compound_state *cs)
7740 {
7741         OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
7742         OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
7743         uint32_t access = args->share_access;
7744         uint32_t deny = args->share_deny;
7745         nfsstat4 status;
7746         rfs4_state_t *sp;
7747         rfs4_file_t *fp;
7748         int fflags = 0;
7749 
7750         DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7751             OPEN_DOWNGRADE4args *, args);
7752 
7753         if (cs->vp == NULL) {
7754                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7755                 goto out;
7756         }
7757 
7758         if (cs->vp->v_type != VREG) {
7759                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7760                 return;
7761         }
7762 
7763         status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7764         if (status != NFS4_OK) {
7765                 *cs->statusp = resp->status = status;
7766                 goto out;
7767         }
7768 
7769         /* Ensure specified filehandle matches */
7770         if (cs->vp != sp->rs_finfo->rf_vp) {
7771                 rfs4_state_rele(sp);
7772                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7773                 goto out;
7774         }
7775 
7776         /* hold off other access to open_owner while we tinker */
7777         rfs4_sw_enter(&sp->rs_owner->ro_sw);
7778 
7779         switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7780         case NFS4_CHECK_STATEID_OKAY:
7781                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7782                     resop) != NFS4_CHKSEQ_OKAY) {
7783                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7784                         goto end;
7785                 }
7786                 break;
7787         case NFS4_CHECK_STATEID_OLD:
7788                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7789                 goto end;
7790         case NFS4_CHECK_STATEID_BAD:
7791                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7792                 goto end;
7793         case NFS4_CHECK_STATEID_EXPIRED:
7794                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
7795                 goto end;
7796         case NFS4_CHECK_STATEID_CLOSED:
7797                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7798                 goto end;
7799         case NFS4_CHECK_STATEID_UNCONFIRMED:
7800                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7801                 goto end;
7802         case NFS4_CHECK_STATEID_REPLAY:
7803                 /* Check the sequence id for the open owner */
7804                 switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7805                     resop)) {
7806                 case NFS4_CHKSEQ_OKAY:
7807                         /*
7808                          * This is replayed stateid; if seqid matches
7809                          * next expected, then client is using wrong seqid.
7810                          */
7811                         /* fall through */
7812                 case NFS4_CHKSEQ_BAD:
7813                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7814                         goto end;
7815                 case NFS4_CHKSEQ_REPLAY:
7816                         /*
7817                          * Note this case is the duplicate case so
7818                          * resp->status is already set.
7819                          */
7820                         *cs->statusp = resp->status;
7821                         rfs4_update_lease(sp->rs_owner->ro_client);
7822                         goto end;
7823                 }
7824                 break;
7825         default:
7826                 ASSERT(FALSE);
7827                 break;
7828         }
7829 
7830         rfs4_dbe_lock(sp->rs_dbe);
7831         /*
7832          * Check that the new access modes and deny modes are valid.
7833          * Check that no invalid bits are set.
7834          */
7835         if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7836             (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
7837                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7838                 rfs4_update_open_sequence(sp->rs_owner);
7839                 rfs4_dbe_unlock(sp->rs_dbe);
7840                 goto end;
7841         }
7842 
7843         /*
7844          * The new modes must be a subset of the current modes and
7845          * the access must specify at least one mode. To test that
7846          * the new mode is a subset of the current modes we bitwise
7847          * AND them together and check that the result equals the new
7848          * mode. For example:
7849          * New mode, access == R and current mode, sp->rs_open_access  == RW
7850          * access & sp->rs_open_access == R == access, so the new access mode
7851          * is valid. Consider access == RW, sp->rs_open_access = R
7852          * access & sp->rs_open_access == R != access, so the new access mode
7853          * is invalid.
7854          */
7855         if ((access & sp->rs_open_access) != access ||
7856             (deny & sp->rs_open_deny) != deny ||
7857             (access &
7858             (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
7859                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7860                 rfs4_update_open_sequence(sp->rs_owner);
7861                 rfs4_dbe_unlock(sp->rs_dbe);
7862                 goto end;
7863         }
7864 
7865         /*
7866          * Release any share locks associated with this stateID.
7867          * Strictly speaking, this violates the spec because the
7868          * spec effectively requires that open downgrade be atomic.
7869          * At present, fs_shrlock does not have this capability.
7870          */
7871         (void) rfs4_unshare(sp);
7872 
7873         status = rfs4_share(sp, access, deny);
7874         if (status != NFS4_OK) {
7875                 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7876                 rfs4_update_open_sequence(sp->rs_owner);
7877                 rfs4_dbe_unlock(sp->rs_dbe);
7878                 goto end;
7879         }
7880 
7881         fp = sp->rs_finfo;
7882         rfs4_dbe_lock(fp->rf_dbe);
7883 
7884         /*
7885          * If the current mode has deny read and the new mode
7886          * does not, decrement the number of deny read mode bits
7887          * and if it goes to zero turn off the deny read bit
7888          * on the file.
7889          */
7890         if ((sp->rs_open_deny & OPEN4_SHARE_DENY_READ) &&
7891             (deny & OPEN4_SHARE_DENY_READ) == 0) {
7892                 fp->rf_deny_read--;
7893                 if (fp->rf_deny_read == 0)
7894                         fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
7895         }
7896 
7897         /*
7898          * If the current mode has deny write and the new mode
7899          * does not, decrement the number of deny write mode bits
7900          * and if it goes to zero turn off the deny write bit
7901          * on the file.
7902          */
7903         if ((sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) &&
7904             (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7905                 fp->rf_deny_write--;
7906                 if (fp->rf_deny_write == 0)
7907                         fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7908         }
7909 
7910         /*
7911          * If the current mode has access read and the new mode
7912          * does not, decrement the number of access read mode bits
7913          * and if it goes to zero turn off the access read bit
7914          * on the file.  set fflags to FREAD for the call to
7915          * vn_open_downgrade().
7916          */
7917         if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) &&
7918             (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7919                 fp->rf_access_read--;
7920                 if (fp->rf_access_read == 0)
7921                         fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
7922                 fflags |= FREAD;
7923         }
7924 
7925         /*
7926          * If the current mode has access write and the new mode
7927          * does not, decrement the number of access write mode bits
7928          * and if it goes to zero turn off the access write bit
7929          * on the file.  set fflags to FWRITE for the call to
7930          * vn_open_downgrade().
7931          */
7932         if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) &&
7933             (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7934                 fp->rf_access_write--;
7935                 if (fp->rf_access_write == 0)
7936                         fp->rf_share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7937                 fflags |= FWRITE;
7938         }
7939 
7940         /* Check that the file is still accessible */
7941         ASSERT(fp->rf_share_access);
7942 
7943         rfs4_dbe_unlock(fp->rf_dbe);
7944 
7945         /* now set the new open access and deny modes */
7946         sp->rs_open_access = access;
7947         sp->rs_open_deny = deny;
7948 
7949         /*
7950          * we successfully downgraded the share lock, now we need to downgrade
7951          * the open. it is possible that the downgrade was only for a deny
7952          * mode and we have nothing else to do.
7953          */
7954         if ((fflags & (FREAD|FWRITE)) != 0)
7955                 vn_open_downgrade(cs->vp, fflags);
7956 
7957         /* Update the stateid */
7958         next_stateid(&sp->rs_stateid);
7959         resp->open_stateid = sp->rs_stateid.stateid;
7960 
7961         rfs4_dbe_unlock(sp->rs_dbe);
7962 
7963         *cs->statusp = resp->status = NFS4_OK;
7964         /* Update the lease */
7965         rfs4_update_lease(sp->rs_owner->ro_client);
7966         /* And the sequence */
7967         rfs4_update_open_sequence(sp->rs_owner);
7968         rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7969 
7970 end:
7971         rfs4_sw_exit(&sp->rs_owner->ro_sw);
7972         rfs4_state_rele(sp);
7973 out:
7974         DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7975             OPEN_DOWNGRADE4res *, resp);
7976 }
7977 
7978 static void *
7979 memstr(const void *s1, const char *s2, size_t n)
7980 {
7981         size_t l = strlen(s2);
7982         char *p = (char *)s1;
7983 
7984         while (n >= l) {
7985                 if (bcmp(p, s2, l) == 0)
7986                         return (p);
7987                 p++;
7988                 n--;
7989         }
7990 
7991         return (NULL);
7992 }
7993 
7994 /*
7995  * The logic behind this function is detailed in the NFSv4 RFC in the
7996  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
7997  * that section for explicit guidance to server behavior for
7998  * SETCLIENTID.
7999  */
8000 void
8001 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
8002     struct svc_req *req, struct compound_state *cs)
8003 {
8004         SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
8005         SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
8006         rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
8007         rfs4_clntip_t *ci;
8008         bool_t create;
8009         char *addr, *netid;
8010         int len;
8011 
8012         DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
8013             SETCLIENTID4args *, args);
8014 retry:
8015         newcp = cp_confirmed = cp_unconfirmed = NULL;
8016 
8017         /*
8018          * Save the caller's IP address
8019          */
8020         args->client.cl_addr =
8021             (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
8022 
8023         /*
8024          * Record if it is a Solaris client that cannot handle referrals.
8025          */
8026         if (memstr(args->client.id_val, "Solaris", args->client.id_len) &&
8027             !memstr(args->client.id_val, "+referrals", args->client.id_len)) {
8028                 /* Add a "yes, it's downrev" record */
8029                 create = TRUE;
8030                 ci = rfs4_find_clntip(args->client.cl_addr, &create);
8031                 ASSERT(ci != NULL);
8032                 rfs4_dbe_rele(ci->ri_dbe);
8033         } else {
8034                 /* Remove any previous record */
8035                 rfs4_invalidate_clntip(args->client.cl_addr);
8036         }
8037 
8038         /*
8039          * In search of an EXISTING client matching the incoming
8040          * request to establish a new client identifier at the server
8041          */
8042         create = TRUE;
8043         cp = rfs4_findclient(&args->client, &create, NULL);
8044 
8045         /* Should never happen */
8046         ASSERT(cp != NULL);
8047 
8048         if (cp == NULL) {
8049                 *cs->statusp = res->status = NFS4ERR_SERVERFAULT;
8050                 goto out;
8051         }
8052 
8053         /*
8054          * Easiest case. Client identifier is newly created and is
8055          * unconfirmed.  Also note that for this case, no other
8056          * entries exist for the client identifier.  Nothing else to
8057          * check.  Just setup the response and respond.
8058          */
8059         if (create) {
8060                 *cs->statusp = res->status = NFS4_OK;
8061                 res->SETCLIENTID4res_u.resok4.clientid = cp->rc_clientid;
8062                 res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8063                     cp->rc_confirm_verf;
8064                 /* Setup callback information; CB_NULL confirmation later */
8065                 rfs4_client_setcb(cp, &args->callback, args->callback_ident);
8066 
8067                 rfs4_client_rele(cp);
8068                 goto out;
8069         }
8070 
8071         /*
8072          * An existing, confirmed client may exist but it may not have
8073          * been active for at least one lease period.  If so, then
8074          * "close" the client and create a new client identifier
8075          */
8076         if (rfs4_lease_expired(cp)) {
8077                 rfs4_client_close(cp);
8078                 goto retry;
8079         }
8080 
8081         if (cp->rc_need_confirm == TRUE)
8082                 cp_unconfirmed = cp;
8083         else
8084                 cp_confirmed = cp;
8085 
8086         cp = NULL;
8087 
8088         /*
8089          * We have a confirmed client, now check for an
8090          * unconfimred entry
8091          */
8092         if (cp_confirmed) {
8093                 /* If creds don't match then client identifier is inuse */
8094                 if (!creds_ok(cp_confirmed->rc_cr_set, req, cs)) {
8095                         rfs4_cbinfo_t *cbp;
8096                         /*
8097                          * Some one else has established this client
8098                          * id. Try and say * who they are. We will use
8099                          * the call back address supplied by * the
8100                          * first client.
8101                          */
8102                         *cs->statusp = res->status = NFS4ERR_CLID_INUSE;
8103 
8104                         addr = netid = NULL;
8105 
8106                         cbp = &cp_confirmed->rc_cbinfo;
8107                         if (cbp->cb_callback.cb_location.r_addr &&
8108                             cbp->cb_callback.cb_location.r_netid) {
8109                                 cb_client4 *cbcp = &cbp->cb_callback;
8110 
8111                                 len = strlen(cbcp->cb_location.r_addr)+1;
8112                                 addr = kmem_alloc(len, KM_SLEEP);
8113                                 bcopy(cbcp->cb_location.r_addr, addr, len);
8114                                 len = strlen(cbcp->cb_location.r_netid)+1;
8115                                 netid = kmem_alloc(len, KM_SLEEP);
8116                                 bcopy(cbcp->cb_location.r_netid, netid, len);
8117                         }
8118 
8119                         res->SETCLIENTID4res_u.client_using.r_addr = addr;
8120                         res->SETCLIENTID4res_u.client_using.r_netid = netid;
8121 
8122                         rfs4_client_rele(cp_confirmed);
8123                 }
8124 
8125                 /*
8126                  * Confirmed, creds match, and verifier matches; must
8127                  * be an update of the callback info
8128                  */
8129                 if (cp_confirmed->rc_nfs_client.verifier ==
8130                     args->client.verifier) {
8131                         /* Setup callback information */
8132                         rfs4_client_setcb(cp_confirmed, &args->callback,
8133                             args->callback_ident);
8134 
8135                         /* everything okay -- move ahead */
8136                         *cs->statusp = res->status = NFS4_OK;
8137                         res->SETCLIENTID4res_u.resok4.clientid =
8138                             cp_confirmed->rc_clientid;
8139 
8140                         /* update the confirm_verifier and return it */
8141                         rfs4_client_scv_next(cp_confirmed);
8142                         res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8143                             cp_confirmed->rc_confirm_verf;
8144 
8145                         rfs4_client_rele(cp_confirmed);
8146                         goto out;
8147                 }
8148 
8149                 /*
8150                  * Creds match but the verifier doesn't.  Must search
8151                  * for an unconfirmed client that would be replaced by
8152                  * this request.
8153                  */
8154                 create = FALSE;
8155                 cp_unconfirmed = rfs4_findclient(&args->client, &create,
8156                     cp_confirmed);
8157         }
8158 
8159         /*
8160          * At this point, we have taken care of the brand new client
8161          * struct, INUSE case, update of an existing, and confirmed
8162          * client struct.
8163          */
8164 
8165         /*
8166          * check to see if things have changed while we originally
8167          * picked up the client struct.  If they have, then return and
8168          * retry the processing of this SETCLIENTID request.
8169          */
8170         if (cp_unconfirmed) {
8171                 rfs4_dbe_lock(cp_unconfirmed->rc_dbe);
8172                 if (!cp_unconfirmed->rc_need_confirm) {
8173                         rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
8174                         rfs4_client_rele(cp_unconfirmed);
8175                         if (cp_confirmed)
8176                                 rfs4_client_rele(cp_confirmed);
8177                         goto retry;
8178                 }
8179                 /* do away with the old unconfirmed one */
8180                 rfs4_dbe_invalidate(cp_unconfirmed->rc_dbe);
8181                 rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
8182                 rfs4_client_rele(cp_unconfirmed);
8183                 cp_unconfirmed = NULL;
8184         }
8185 
8186         /*
8187          * This search will temporarily hide the confirmed client
8188          * struct while a new client struct is created as the
8189          * unconfirmed one.
8190          */
8191         create = TRUE;
8192         newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
8193 
8194         ASSERT(newcp != NULL);
8195 
8196         if (newcp == NULL) {
8197                 *cs->statusp = res->status = NFS4ERR_SERVERFAULT;
8198                 rfs4_client_rele(cp_confirmed);
8199                 goto out;
8200         }
8201 
8202         /*
8203          * If one was not created, then a similar request must be in
8204          * process so release and start over with this one
8205          */
8206         if (create != TRUE) {
8207                 rfs4_client_rele(newcp);
8208                 if (cp_confirmed)
8209                         rfs4_client_rele(cp_confirmed);
8210                 goto retry;
8211         }
8212 
8213         *cs->statusp = res->status = NFS4_OK;
8214         res->SETCLIENTID4res_u.resok4.clientid = newcp->rc_clientid;
8215         res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8216             newcp->rc_confirm_verf;
8217         /* Setup callback information; CB_NULL confirmation later */
8218         rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
8219 
8220         newcp->rc_cp_confirmed = cp_confirmed;
8221 
8222         rfs4_client_rele(newcp);
8223 
8224 out:
8225         DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
8226             SETCLIENTID4res *, res);
8227 }
8228 
8229 /*ARGSUSED*/
8230 void
8231 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
8232     struct svc_req *req, struct compound_state *cs)
8233 {
8234         SETCLIENTID_CONFIRM4args *args =
8235             &argop->nfs_argop4_u.opsetclientid_confirm;
8236         SETCLIENTID_CONFIRM4res *res =
8237             &resop->nfs_resop4_u.opsetclientid_confirm;
8238         rfs4_client_t *cp, *cptoclose = NULL;
8239         nfs4_srv_t *nsrv4;
8240 
8241         DTRACE_NFSV4_2(op__setclientid__confirm__start,
8242             struct compound_state *, cs,
8243             SETCLIENTID_CONFIRM4args *, args);
8244 
8245         nsrv4 = nfs4_get_srv();
8246         *cs->statusp = res->status = NFS4_OK;
8247 
8248         cp = rfs4_findclient_by_id(args->clientid, TRUE);
8249 
8250         if (cp == NULL) {
8251                 *cs->statusp = res->status =
8252                     rfs4_check_clientid(&args->clientid, 1);
8253                 goto out;
8254         }
8255 
8256         if (!creds_ok(cp, req, cs)) {
8257                 *cs->statusp = res->status = NFS4ERR_CLID_INUSE;
8258                 rfs4_client_rele(cp);
8259                 goto out;
8260         }
8261 
8262         /* If the verifier doesn't match, the record doesn't match */
8263         if (cp->rc_confirm_verf != args->setclientid_confirm) {
8264                 *cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
8265                 rfs4_client_rele(cp);
8266                 goto out;
8267         }
8268 
8269         rfs4_dbe_lock(cp->rc_dbe);
8270         cp->rc_need_confirm = FALSE;
8271         if (cp->rc_cp_confirmed) {
8272                 cptoclose = cp->rc_cp_confirmed;
8273                 cptoclose->rc_ss_remove = 1;
8274                 cp->rc_cp_confirmed = NULL;
8275         }
8276 
8277         /*
8278          * Update the client's associated server instance, if it's changed
8279          * since the client was created.
8280          */
8281         if (rfs4_servinst(cp) != nsrv4->nfs4_cur_servinst)
8282                 rfs4_servinst_assign(nsrv4, cp, nsrv4->nfs4_cur_servinst);
8283 
8284         /*
8285          * Record clientid in stable storage.
8286          * Must be done after server instance has been assigned.
8287          */
8288         rfs4_ss_clid(nsrv4, cp);
8289 
8290         rfs4_dbe_unlock(cp->rc_dbe);
8291 
8292         if (cptoclose)
8293                 /* don't need to rele, client_close does it */
8294                 rfs4_client_close(cptoclose);
8295 
8296         /* If needed, initiate CB_NULL call for callback path */
8297         rfs4_deleg_cb_check(cp);
8298         rfs4_update_lease(cp);
8299 
8300         /*
8301          * Check to see if client can perform reclaims
8302          */
8303         rfs4_ss_chkclid(nsrv4, cp);
8304 
8305         rfs4_client_rele(cp);
8306 
8307 out:
8308         DTRACE_NFSV4_2(op__setclientid__confirm__done,
8309             struct compound_state *, cs,
8310             SETCLIENTID_CONFIRM4 *, res);
8311 }
8312 
8313 
8314 /*ARGSUSED*/
8315 void
8316 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
8317     struct svc_req *req, struct compound_state *cs)
8318 {
8319         CLOSE4args *args = &argop->nfs_argop4_u.opclose;
8320         CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
8321         rfs4_state_t *sp;
8322         nfsstat4 status;
8323 
8324         DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
8325             CLOSE4args *, args);
8326 
8327         if (cs->vp == NULL) {
8328                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8329                 goto out;
8330         }
8331 
8332         status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
8333         if (status != NFS4_OK) {
8334                 *cs->statusp = resp->status = status;
8335                 goto out;
8336         }
8337 
8338         /* Ensure specified filehandle matches */
8339         if (cs->vp != sp->rs_finfo->rf_vp) {
8340                 rfs4_state_rele(sp);
8341                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8342                 goto out;
8343         }
8344 
8345         /* hold off other access to open_owner while we tinker */
8346         rfs4_sw_enter(&sp->rs_owner->ro_sw);
8347 
8348         switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
8349         case NFS4_CHECK_STATEID_OKAY:
8350                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8351                     resop) != NFS4_CHKSEQ_OKAY) {
8352                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8353                         goto end;
8354                 }
8355                 break;
8356         case NFS4_CHECK_STATEID_OLD:
8357                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8358                 goto end;
8359         case NFS4_CHECK_STATEID_BAD:
8360                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8361                 goto end;
8362         case NFS4_CHECK_STATEID_EXPIRED:
8363                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
8364                 goto end;
8365         case NFS4_CHECK_STATEID_CLOSED:
8366                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8367                 goto end;
8368         case NFS4_CHECK_STATEID_UNCONFIRMED:
8369                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8370                 goto end;
8371         case NFS4_CHECK_STATEID_REPLAY:
8372                 /* Check the sequence id for the open owner */
8373                 switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8374                     resop)) {
8375                 case NFS4_CHKSEQ_OKAY:
8376                         /*
8377                          * This is replayed stateid; if seqid matches
8378                          * next expected, then client is using wrong seqid.
8379                          */
8380                         /* FALL THROUGH */
8381                 case NFS4_CHKSEQ_BAD:
8382                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8383                         goto end;
8384                 case NFS4_CHKSEQ_REPLAY:
8385                         /*
8386                          * Note this case is the duplicate case so
8387                          * resp->status is already set.
8388                          */
8389                         *cs->statusp = resp->status;
8390                         rfs4_update_lease(sp->rs_owner->ro_client);
8391                         goto end;
8392                 }
8393                 break;
8394         default:
8395                 ASSERT(FALSE);
8396                 break;
8397         }
8398 
8399         rfs4_dbe_lock(sp->rs_dbe);
8400 
8401         /* Update the stateid. */
8402         next_stateid(&sp->rs_stateid);
8403         resp->open_stateid = sp->rs_stateid.stateid;
8404 
8405         rfs4_dbe_unlock(sp->rs_dbe);
8406 
8407         rfs4_update_lease(sp->rs_owner->ro_client);
8408         rfs4_update_open_sequence(sp->rs_owner);
8409         rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8410 
8411         rfs4_state_close(sp, FALSE, FALSE, cs->cr);
8412 
8413         *cs->statusp = resp->status = status;
8414 
8415 end:
8416         rfs4_sw_exit(&sp->rs_owner->ro_sw);
8417         rfs4_state_rele(sp);
8418 out:
8419         DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
8420             CLOSE4res *, resp);
8421 }
8422 
8423 /*
8424  * Manage the counts on the file struct and close all file locks
8425  */
8426 /*ARGSUSED*/
8427 void
8428 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
8429     bool_t close_of_client)
8430 {
8431         rfs4_file_t *fp = sp->rs_finfo;
8432         rfs4_lo_state_t *lsp;
8433         int fflags = 0;
8434 
8435         /*
8436          * If this call is part of the larger closing down of client
8437          * state then it is just easier to release all locks
8438          * associated with this client instead of going through each
8439          * individual file and cleaning locks there.
8440          */
8441         if (close_of_client) {
8442                 if (sp->rs_owner->ro_client->rc_unlksys_completed == FALSE &&
8443                     !list_is_empty(&sp->rs_lostatelist) &&
8444                     sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID) {
8445                         /* Is the PxFS kernel module loaded? */
8446                         if (lm_remove_file_locks != NULL) {
8447                                 int new_sysid;
8448 
8449                                 /* Encode the cluster nodeid in new sysid */
8450                                 new_sysid = sp->rs_owner->ro_client->rc_sysidt;
8451                                 lm_set_nlmid_flk(&new_sysid);
8452 
8453                                 /*
8454                                  * This PxFS routine removes file locks for a
8455                                  * client over all nodes of a cluster.
8456                                  */
8457                                 NFS4_DEBUG(rfs4_debug, (CE_NOTE,
8458                                     "lm_remove_file_locks(sysid=0x%x)\n",
8459                                     new_sysid));
8460                                 (*lm_remove_file_locks)(new_sysid);
8461                         } else {
8462                                 struct flock64 flk;
8463 
8464                                 /* Release all locks for this client */
8465                                 flk.l_type = F_UNLKSYS;
8466                                 flk.l_whence = 0;
8467                                 flk.l_start = 0;
8468                                 flk.l_len = 0;
8469                                 flk.l_sysid =
8470                                     sp->rs_owner->ro_client->rc_sysidt;
8471                                 flk.l_pid = 0;
8472                                 (void) VOP_FRLOCK(sp->rs_finfo->rf_vp, F_SETLK,
8473                                     &flk, F_REMOTELOCK | FREAD | FWRITE,
8474                                     (u_offset_t)0, NULL, CRED(), NULL);
8475                         }
8476 
8477                         sp->rs_owner->ro_client->rc_unlksys_completed = TRUE;
8478                 }
8479         }
8480 
8481         /*
8482          * Release all locks on this file by this lock owner or at
8483          * least mark the locks as having been released
8484          */
8485         for (lsp = list_head(&sp->rs_lostatelist); lsp != NULL;
8486             lsp = list_next(&sp->rs_lostatelist, lsp)) {
8487                 lsp->rls_locks_cleaned = TRUE;
8488 
8489                 /* Was this already taken care of above? */
8490                 if (!close_of_client &&
8491                     sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8492                         (void) cleanlocks(sp->rs_finfo->rf_vp,
8493                             lsp->rls_locker->rl_pid,
8494                             lsp->rls_locker->rl_client->rc_sysidt);
8495         }
8496 
8497         /*
8498          * Release any shrlocks associated with this open state ID.
8499          * This must be done before the rfs4_state gets marked closed.
8500          */
8501         if (sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8502                 (void) rfs4_unshare(sp);
8503 
8504         if (sp->rs_open_access) {
8505                 rfs4_dbe_lock(fp->rf_dbe);
8506 
8507                 /*
8508                  * Decrement the count for each access and deny bit that this
8509                  * state has contributed to the file.
8510                  * If the file counts go to zero
8511                  * clear the appropriate bit in the appropriate mask.
8512                  */
8513                 if (sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) {
8514                         fp->rf_access_read--;
8515                         fflags |= FREAD;
8516                         if (fp->rf_access_read == 0)
8517                                 fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
8518                 }
8519                 if (sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) {
8520                         fp->rf_access_write--;
8521                         fflags |= FWRITE;
8522                         if (fp->rf_access_write == 0)
8523                                 fp->rf_share_access &=
8524                                     ~OPEN4_SHARE_ACCESS_WRITE;
8525                 }
8526                 if (sp->rs_open_deny & OPEN4_SHARE_DENY_READ) {
8527                         fp->rf_deny_read--;
8528                         if (fp->rf_deny_read == 0)
8529                                 fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
8530                 }
8531                 if (sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) {
8532                         fp->rf_deny_write--;
8533                         if (fp->rf_deny_write == 0)
8534                                 fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
8535                 }
8536 
8537                 (void) VOP_CLOSE(fp->rf_vp, fflags, 1, (offset_t)0, cr, NULL);
8538 
8539                 rfs4_dbe_unlock(fp->rf_dbe);
8540 
8541                 sp->rs_open_access = 0;
8542                 sp->rs_open_deny = 0;
8543         }
8544 }
8545 
8546 /*
8547  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
8548  */
8549 static nfsstat4
8550 lock_denied(LOCK4denied *dp, struct flock64 *flk)
8551 {
8552         rfs4_lockowner_t *lo;
8553         rfs4_client_t *cp;
8554         uint32_t len;
8555 
8556         lo = rfs4_findlockowner_by_pid(flk->l_pid);
8557         if (lo != NULL) {
8558                 cp = lo->rl_client;
8559                 if (rfs4_lease_expired(cp)) {
8560                         rfs4_lockowner_rele(lo);
8561                         rfs4_dbe_hold(cp->rc_dbe);
8562                         rfs4_client_close(cp);
8563                         return (NFS4ERR_EXPIRED);
8564                 }
8565                 dp->owner.clientid = lo->rl_owner.clientid;
8566                 len = lo->rl_owner.owner_len;
8567                 dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8568                 bcopy(lo->rl_owner.owner_val, dp->owner.owner_val, len);
8569                 dp->owner.owner_len = len;
8570                 rfs4_lockowner_rele(lo);
8571                 goto finish;
8572         }
8573 
8574         /*
8575          * Its not a NFS4 lock. We take advantage that the upper 32 bits
8576          * of the client id contain the boot time for a NFS4 lock. So we
8577          * fabricate and identity by setting clientid to the sysid, and
8578          * the lock owner to the pid.
8579          */
8580         dp->owner.clientid = flk->l_sysid;
8581         len = sizeof (pid_t);
8582         dp->owner.owner_len = len;
8583         dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8584         bcopy(&flk->l_pid, dp->owner.owner_val, len);
8585 finish:
8586         dp->offset = flk->l_start;
8587         dp->length = flk->l_len;
8588 
8589         if (flk->l_type == F_RDLCK)
8590                 dp->locktype = READ_LT;
8591         else if (flk->l_type == F_WRLCK)
8592                 dp->locktype = WRITE_LT;
8593         else
8594                 return (NFS4ERR_INVAL); /* no mapping from POSIX ltype to v4 */
8595 
8596         return (NFS4_OK);
8597 }
8598 
8599 /*
8600  * The NFSv4.0 LOCK operation does not support the blocking lock (at the
8601  * NFSv4.0 protocol level) so the client needs to resend the LOCK request in a
8602  * case the lock is denied by the NFSv4.0 server.  NFSv4.0 clients are prepared
8603  * for that (obviously); they are sending the LOCK requests with some delays
8604  * between the attempts.  See nfs4frlock() and nfs4_block_and_wait() for the
8605  * locking and delay implementation at the client side.
8606  *
8607  * To make the life of the clients easier, the NFSv4.0 server tries to do some
8608  * fast retries on its own (the for loop below) in a hope the lock will be
8609  * available soon.  And if not, the client won't need to resend the LOCK
8610  * requests so fast to check the lock availability.  This basically saves some
8611  * network traffic and tries to make sure the client gets the lock ASAP.
8612  */
8613 static int
8614 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
8615 {
8616         int error;
8617         struct flock64 flk;
8618         int i;
8619         clock_t delaytime;
8620         int cmd;
8621         int spin_cnt = 0;
8622 
8623         cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
8624 retry:
8625         delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
8626 
8627         for (i = 0; i < rfs4_maxlock_tries; i++) {
8628                 LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8629                 error = VOP_FRLOCK(vp, cmd,
8630                     flock, flag, (u_offset_t)0, NULL, cred, NULL);
8631 
8632                 if (error != EAGAIN && error != EACCES)
8633                         break;
8634 
8635                 if (i < rfs4_maxlock_tries - 1) {
8636                         delay(delaytime);
8637                         delaytime *= 2;
8638                 }
8639         }
8640 
8641         if (error == EAGAIN || error == EACCES) {
8642                 /* Get the owner of the lock */
8643                 flk = *flock;
8644                 LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
8645                 if (VOP_FRLOCK(vp, F_GETLK, &flk, flag, 0, NULL, cred,
8646                     NULL) == 0) {
8647                         /*
8648                          * There's a race inherent in the current VOP_FRLOCK
8649                          * design where:
8650                          * a: "other guy" takes a lock that conflicts with a
8651                          * lock we want
8652                          * b: we attempt to take our lock (non-blocking) and
8653                          * the attempt fails.
8654                          * c: "other guy" releases the conflicting lock
8655                          * d: we ask what lock conflicts with the lock we want,
8656                          * getting F_UNLCK (no lock blocks us)
8657                          *
8658                          * If we retry the non-blocking lock attempt in this
8659                          * case (restart at step 'b') there's some possibility
8660                          * that many such attempts might fail.  However a test
8661                          * designed to actually provoke this race shows that
8662                          * the vast majority of cases require no retry, and
8663                          * only a few took as many as three retries.  Here's
8664                          * the test outcome:
8665                          *
8666                          *         number of retries    how many times we needed
8667                          *                              that many retries
8668                          *         0                    79461
8669                          *         1                      862
8670                          *         2                       49
8671                          *         3                        5
8672                          *
8673                          * Given those empirical results, we arbitrarily limit
8674                          * the retry count to ten.
8675                          *
8676                          * If we actually make to ten retries and give up,
8677                          * nothing catastrophic happens, but we're unable to
8678                          * return the information about the conflicting lock to
8679                          * the NFS client.  That's an acceptable trade off vs.
8680                          * letting this retry loop run forever.
8681                          */
8682                         if (flk.l_type == F_UNLCK) {
8683                                 if (spin_cnt++ < 10) {
8684                                         /* No longer locked, retry */
8685                                         goto retry;
8686                                 }
8687                         } else {
8688                                 *flock = flk;
8689                                 LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
8690                                     F_GETLK, &flk);
8691                         }
8692                 }
8693         }
8694 
8695         return (error);
8696 }
8697 
8698 /*ARGSUSED*/
8699 static nfsstat4
8700 rfs4_do_lock(rfs4_lo_state_t *lsp, nfs_lock_type4 locktype,
8701     offset4 offset, length4 length, cred_t *cred, nfs_resop4 *resop)
8702 {
8703         nfsstat4 status;
8704         rfs4_lockowner_t *lo = lsp->rls_locker;
8705         rfs4_state_t *sp = lsp->rls_state;
8706         struct flock64 flock;
8707         int16_t ltype;
8708         int flag;
8709         int error;
8710         sysid_t sysid;
8711         LOCK4res *lres;
8712         vnode_t *vp;
8713 
8714         if (rfs4_lease_expired(lo->rl_client)) {
8715                 return (NFS4ERR_EXPIRED);
8716         }
8717 
8718         if ((status = rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
8719                 return (status);
8720 
8721         /* Check for zero length. To lock to end of file use all ones for V4 */
8722         if (length == 0)
8723                 return (NFS4ERR_INVAL);
8724         else if (length == (length4)(~0))
8725                 length = 0;             /* Posix to end of file  */
8726 
8727 retry:
8728         rfs4_dbe_lock(sp->rs_dbe);
8729         if (sp->rs_closed == TRUE) {
8730                 rfs4_dbe_unlock(sp->rs_dbe);
8731                 return (NFS4ERR_OLD_STATEID);
8732         }
8733 
8734         if (resop->resop != OP_LOCKU) {
8735                 switch (locktype) {
8736                 case READ_LT:
8737                 case READW_LT:
8738                         if ((sp->rs_share_access
8739                             & OPEN4_SHARE_ACCESS_READ) == 0) {
8740                                 rfs4_dbe_unlock(sp->rs_dbe);
8741 
8742                                 return (NFS4ERR_OPENMODE);
8743                         }
8744                         ltype = F_RDLCK;
8745                         break;
8746                 case WRITE_LT:
8747                 case WRITEW_LT:
8748                         if ((sp->rs_share_access
8749                             & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8750                                 rfs4_dbe_unlock(sp->rs_dbe);
8751 
8752                                 return (NFS4ERR_OPENMODE);
8753                         }
8754                         ltype = F_WRLCK;
8755                         break;
8756                 }
8757         } else
8758                 ltype = F_UNLCK;
8759 
8760         flock.l_type = ltype;
8761         flock.l_whence = 0;             /* SEEK_SET */
8762         flock.l_start = offset;
8763         flock.l_len = length;
8764         flock.l_sysid = sysid;
8765         flock.l_pid = lsp->rls_locker->rl_pid;
8766 
8767         /* Note that length4 is uint64_t but l_len and l_start are off64_t */
8768         if (flock.l_len < 0 || flock.l_start < 0) {
8769                 rfs4_dbe_unlock(sp->rs_dbe);
8770                 return (NFS4ERR_INVAL);
8771         }
8772 
8773         /*
8774          * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
8775          * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
8776          */
8777         flag = (int)sp->rs_share_access | F_REMOTELOCK;
8778 
8779         vp = sp->rs_finfo->rf_vp;
8780         VN_HOLD(vp);
8781 
8782         /*
8783          * We need to unlock sp before we call the underlying filesystem to
8784          * acquire the file lock.
8785          */
8786         rfs4_dbe_unlock(sp->rs_dbe);
8787 
8788         error = setlock(vp, &flock, flag, cred);
8789 
8790         /*
8791          * Make sure the file is still open.  In a case the file was closed in
8792          * the meantime, clean the lock we acquired using the setlock() call
8793          * above, and return the appropriate error.
8794          */
8795         rfs4_dbe_lock(sp->rs_dbe);
8796         if (sp->rs_closed == TRUE) {
8797                 cleanlocks(vp, lsp->rls_locker->rl_pid, sysid);
8798                 rfs4_dbe_unlock(sp->rs_dbe);
8799 
8800                 VN_RELE(vp);
8801 
8802                 return (NFS4ERR_OLD_STATEID);
8803         }
8804         rfs4_dbe_unlock(sp->rs_dbe);
8805 
8806         VN_RELE(vp);
8807 
8808         if (error == 0) {
8809                 rfs4_dbe_lock(lsp->rls_dbe);
8810                 next_stateid(&lsp->rls_lockid);
8811                 rfs4_dbe_unlock(lsp->rls_dbe);
8812         }
8813 
8814         /*
8815          * N.B. We map error values to nfsv4 errors. This is differrent
8816          * than puterrno4 routine.
8817          */
8818         switch (error) {
8819         case 0:
8820                 status = NFS4_OK;
8821                 break;
8822         case EAGAIN:
8823         case EACCES:            /* Old value */
8824                 /* Can only get here if op is OP_LOCK */
8825                 ASSERT(resop->resop == OP_LOCK);
8826                 lres = &resop->nfs_resop4_u.oplock;
8827                 status = NFS4ERR_DENIED;
8828                 if (lock_denied(&lres->LOCK4res_u.denied, &flock)
8829                     == NFS4ERR_EXPIRED)
8830                         goto retry;
8831                 break;
8832         case ENOLCK:
8833                 status = NFS4ERR_DELAY;
8834                 break;
8835         case EOVERFLOW:
8836                 status = NFS4ERR_INVAL;
8837                 break;
8838         case EINVAL:
8839                 status = NFS4ERR_NOTSUPP;
8840                 break;
8841         default:
8842                 status = NFS4ERR_SERVERFAULT;
8843                 break;
8844         }
8845 
8846         return (status);
8847 }
8848 
8849 /*ARGSUSED*/
8850 void
8851 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
8852     struct svc_req *req, struct compound_state *cs)
8853 {
8854         LOCK4args *args = &argop->nfs_argop4_u.oplock;
8855         LOCK4res *resp = &resop->nfs_resop4_u.oplock;
8856         nfsstat4 status;
8857         stateid4 *stateid;
8858         rfs4_lockowner_t *lo;
8859         rfs4_client_t *cp;
8860         rfs4_state_t *sp = NULL;
8861         rfs4_lo_state_t *lsp = NULL;
8862         bool_t ls_sw_held = FALSE;
8863         bool_t create = TRUE;
8864         bool_t lcreate = TRUE;
8865         bool_t dup_lock = FALSE;
8866         int rc;
8867 
8868         DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8869             LOCK4args *, args);
8870 
8871         if (cs->vp == NULL) {
8872                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8873                 DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8874                     cs, LOCK4res *, resp);
8875                 return;
8876         }
8877 
8878         if (args->locker.new_lock_owner) {
8879                 /* Create a new lockowner for this instance */
8880                 open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
8881 
8882                 NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
8883 
8884                 stateid = &olo->open_stateid;
8885                 status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
8886                 if (status != NFS4_OK) {
8887                         NFS4_DEBUG(rfs4_debug,
8888                             (CE_NOTE, "Get state failed in lock %d", status));
8889                         *cs->statusp = resp->status = status;
8890                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8891                             cs, LOCK4res *, resp);
8892                         return;
8893                 }
8894 
8895                 /* Ensure specified filehandle matches */
8896                 if (cs->vp != sp->rs_finfo->rf_vp) {
8897                         rfs4_state_rele(sp);
8898                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8899                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8900                             cs, LOCK4res *, resp);
8901                         return;
8902                 }
8903 
8904                 /* hold off other access to open_owner while we tinker */
8905                 rfs4_sw_enter(&sp->rs_owner->ro_sw);
8906 
8907                 switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
8908                 case NFS4_CHECK_STATEID_OLD:
8909                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8910                         goto end;
8911                 case NFS4_CHECK_STATEID_BAD:
8912                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8913                         goto end;
8914                 case NFS4_CHECK_STATEID_EXPIRED:
8915                         *cs->statusp = resp->status = NFS4ERR_EXPIRED;
8916                         goto end;
8917                 case NFS4_CHECK_STATEID_UNCONFIRMED:
8918                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8919                         goto end;
8920                 case NFS4_CHECK_STATEID_CLOSED:
8921                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8922                         goto end;
8923                 case NFS4_CHECK_STATEID_OKAY:
8924                 case NFS4_CHECK_STATEID_REPLAY:
8925                         switch (rfs4_check_olo_seqid(olo->open_seqid,
8926                             sp->rs_owner, resop)) {
8927                         case NFS4_CHKSEQ_OKAY:
8928                                 if (rc == NFS4_CHECK_STATEID_OKAY)
8929                                         break;
8930                                 /*
8931                                  * This is replayed stateid; if seqid
8932                                  * matches next expected, then client
8933                                  * is using wrong seqid.
8934                                  */
8935                                 /* FALLTHROUGH */
8936                         case NFS4_CHKSEQ_BAD:
8937                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8938                                 goto end;
8939                         case NFS4_CHKSEQ_REPLAY:
8940                                 /* This is a duplicate LOCK request */
8941                                 dup_lock = TRUE;
8942 
8943                                 /*
8944                                  * For a duplicate we do not want to
8945                                  * create a new lockowner as it should
8946                                  * already exist.
8947                                  * Turn off the lockowner create flag.
8948                                  */
8949                                 lcreate = FALSE;
8950                         }
8951                         break;
8952                 }
8953 
8954                 lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
8955                 if (lo == NULL) {
8956                         NFS4_DEBUG(rfs4_debug,
8957                             (CE_NOTE, "rfs4_op_lock: no lock owner"));
8958                         *cs->statusp = resp->status = NFS4ERR_RESOURCE;
8959                         goto end;
8960                 }
8961 
8962                 lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
8963                 if (lsp == NULL) {
8964                         rfs4_update_lease(sp->rs_owner->ro_client);
8965                         /*
8966                          * Only update theh open_seqid if this is not
8967                          * a duplicate request
8968                          */
8969                         if (dup_lock == FALSE) {
8970                                 rfs4_update_open_sequence(sp->rs_owner);
8971                         }
8972 
8973                         NFS4_DEBUG(rfs4_debug,
8974                             (CE_NOTE, "rfs4_op_lock: no state"));
8975                         *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8976                         rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8977                         rfs4_lockowner_rele(lo);
8978                         goto end;
8979                 }
8980 
8981                 /*
8982                  * This is the new_lock_owner branch and the client is
8983                  * supposed to be associating a new lock_owner with
8984                  * the open file at this point.  If we find that a
8985                  * lock_owner/state association already exists and a
8986                  * successful LOCK request was returned to the client,
8987                  * an error is returned to the client since this is
8988                  * not appropriate.  The client should be using the
8989                  * existing lock_owner branch.
8990                  */
8991                 if (dup_lock == FALSE && create == FALSE) {
8992                         if (lsp->rls_lock_completed == TRUE) {
8993                                 *cs->statusp =
8994                                     resp->status = NFS4ERR_BAD_SEQID;
8995                                 rfs4_lockowner_rele(lo);
8996                                 goto end;
8997                         }
8998                 }
8999 
9000                 rfs4_update_lease(sp->rs_owner->ro_client);
9001 
9002                 /*
9003                  * Only update theh open_seqid if this is not
9004                  * a duplicate request
9005                  */
9006                 if (dup_lock == FALSE) {
9007                         rfs4_update_open_sequence(sp->rs_owner);
9008                 }
9009 
9010                 /*
9011                  * If this is a duplicate lock request, just copy the
9012                  * previously saved reply and return.
9013                  */
9014                 if (dup_lock == TRUE) {
9015                         /* verify that lock_seqid's match */
9016                         if (lsp->rls_seqid != olo->lock_seqid) {
9017                                 NFS4_DEBUG(rfs4_debug,
9018                                     (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
9019                                     "lsp->seqid=%d old->seqid=%d",
9020                                     lsp->rls_seqid, olo->lock_seqid));
9021                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9022                         } else {
9023                                 rfs4_copy_reply(resop, &lsp->rls_reply);
9024                                 /*
9025                                  * Make sure to copy the just
9026                                  * retrieved reply status into the
9027                                  * overall compound status
9028                                  */
9029                                 *cs->statusp = resp->status;
9030                         }
9031                         rfs4_lockowner_rele(lo);
9032                         goto end;
9033                 }
9034 
9035                 rfs4_dbe_lock(lsp->rls_dbe);
9036 
9037                 /* Make sure to update the lock sequence id */
9038                 lsp->rls_seqid = olo->lock_seqid;
9039 
9040                 NFS4_DEBUG(rfs4_debug,
9041                     (CE_NOTE, "Lock seqid established as %d", lsp->rls_seqid));
9042 
9043                 /*
9044                  * This is used to signify the newly created lockowner
9045                  * stateid and its sequence number.  The checks for
9046                  * sequence number and increment don't occur on the
9047                  * very first lock request for a lockowner.
9048                  */
9049                 lsp->rls_skip_seqid_check = TRUE;
9050 
9051                 /* hold off other access to lsp while we tinker */
9052                 rfs4_sw_enter(&lsp->rls_sw);
9053                 ls_sw_held = TRUE;
9054 
9055                 rfs4_dbe_unlock(lsp->rls_dbe);
9056 
9057                 rfs4_lockowner_rele(lo);
9058         } else {
9059                 stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
9060                 /* get lsp and hold the lock on the underlying file struct */
9061                 if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
9062                     != NFS4_OK) {
9063                         *cs->statusp = resp->status = status;
9064                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9065                             cs, LOCK4res *, resp);
9066                         return;
9067                 }
9068                 create = FALSE; /* We didn't create lsp */
9069 
9070                 /* Ensure specified filehandle matches */
9071                 if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
9072                         rfs4_lo_state_rele(lsp, TRUE);
9073                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9074                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9075                             cs, LOCK4res *, resp);
9076                         return;
9077                 }
9078 
9079                 /* hold off other access to lsp while we tinker */
9080                 rfs4_sw_enter(&lsp->rls_sw);
9081                 ls_sw_held = TRUE;
9082 
9083                 switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
9084                 /*
9085                  * The stateid looks like it was okay (expected to be
9086                  * the next one)
9087                  */
9088                 case NFS4_CHECK_STATEID_OKAY:
9089                         /*
9090                          * The sequence id is now checked.  Determine
9091                          * if this is a replay or if it is in the
9092                          * expected (next) sequence.  In the case of a
9093                          * replay, there are two replay conditions
9094                          * that may occur.  The first is the normal
9095                          * condition where a LOCK is done with a
9096                          * NFS4_OK response and the stateid is
9097                          * updated.  That case is handled below when
9098                          * the stateid is identified as a REPLAY.  The
9099                          * second is the case where an error is
9100                          * returned, like NFS4ERR_DENIED, and the
9101                          * sequence number is updated but the stateid
9102                          * is not updated.  This second case is dealt
9103                          * with here.  So it may seem odd that the
9104                          * stateid is okay but the sequence id is a
9105                          * replay but it is okay.
9106                          */
9107                         switch (rfs4_check_lock_seqid(
9108                             args->locker.locker4_u.lock_owner.lock_seqid,
9109                             lsp, resop)) {
9110                         case NFS4_CHKSEQ_REPLAY:
9111                                 if (resp->status != NFS4_OK) {
9112                                         /*
9113                                          * Here is our replay and need
9114                                          * to verify that the last
9115                                          * response was an error.
9116                                          */
9117                                         *cs->statusp = resp->status;
9118                                         goto end;
9119                                 }
9120                                 /*
9121                                  * This is done since the sequence id
9122                                  * looked like a replay but it didn't
9123                                  * pass our check so a BAD_SEQID is
9124                                  * returned as a result.
9125                                  */
9126                                 /*FALLTHROUGH*/
9127                         case NFS4_CHKSEQ_BAD:
9128                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9129                                 goto end;
9130                         case NFS4_CHKSEQ_OKAY:
9131                                 /* Everything looks okay move ahead */
9132                                 break;
9133                         }
9134                         break;
9135                 case NFS4_CHECK_STATEID_OLD:
9136                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9137                         goto end;
9138                 case NFS4_CHECK_STATEID_BAD:
9139                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9140                         goto end;
9141                 case NFS4_CHECK_STATEID_EXPIRED:
9142                         *cs->statusp = resp->status = NFS4ERR_EXPIRED;
9143                         goto end;
9144                 case NFS4_CHECK_STATEID_CLOSED:
9145                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9146                         goto end;
9147                 case NFS4_CHECK_STATEID_REPLAY:
9148                         switch (rfs4_check_lock_seqid(
9149                             args->locker.locker4_u.lock_owner.lock_seqid,
9150                             lsp, resop)) {
9151                         case NFS4_CHKSEQ_OKAY:
9152                                 /*
9153                                  * This is a replayed stateid; if
9154                                  * seqid matches the next expected,
9155                                  * then client is using wrong seqid.
9156                                  */
9157                         case NFS4_CHKSEQ_BAD:
9158                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9159                                 goto end;
9160                         case NFS4_CHKSEQ_REPLAY:
9161                                 rfs4_update_lease(lsp->rls_locker->rl_client);
9162                                 *cs->statusp = status = resp->status;
9163                                 goto end;
9164                         }
9165                         break;
9166                 default:
9167                         ASSERT(FALSE);
9168                         break;
9169                 }
9170 
9171                 rfs4_update_lock_sequence(lsp);
9172                 rfs4_update_lease(lsp->rls_locker->rl_client);
9173         }
9174 
9175         /*
9176          * NFS4 only allows locking on regular files, so
9177          * verify type of object.
9178          */
9179         if (cs->vp->v_type != VREG) {
9180                 if (cs->vp->v_type == VDIR)
9181                         status = NFS4ERR_ISDIR;
9182                 else
9183                         status = NFS4ERR_INVAL;
9184                 goto out;
9185         }
9186 
9187         cp = lsp->rls_state->rs_owner->ro_client;
9188 
9189         if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
9190                 status = NFS4ERR_GRACE;
9191                 goto out;
9192         }
9193 
9194         if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->rc_can_reclaim) {
9195                 status = NFS4ERR_NO_GRACE;
9196                 goto out;
9197         }
9198 
9199         if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
9200                 status = NFS4ERR_NO_GRACE;
9201                 goto out;
9202         }
9203 
9204         if (lsp->rls_state->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE)
9205                 cs->deleg = TRUE;
9206 
9207         status = rfs4_do_lock(lsp, args->locktype,
9208             args->offset, args->length, cs->cr, resop);
9209 
9210 out:
9211         lsp->rls_skip_seqid_check = FALSE;
9212 
9213         *cs->statusp = resp->status = status;
9214 
9215         if (status == NFS4_OK) {
9216                 resp->LOCK4res_u.lock_stateid = lsp->rls_lockid.stateid;
9217                 lsp->rls_lock_completed = TRUE;
9218         }
9219         /*
9220          * Only update the "OPEN" response here if this was a new
9221          * lock_owner
9222          */
9223         if (sp)
9224                 rfs4_update_open_resp(sp->rs_owner, resop, NULL);
9225 
9226         rfs4_update_lock_resp(lsp, resop);
9227 
9228 end:
9229         if (lsp) {
9230                 if (ls_sw_held)
9231                         rfs4_sw_exit(&lsp->rls_sw);
9232                 /*
9233                  * If an sp obtained, then the lsp does not represent
9234                  * a lock on the file struct.
9235                  */
9236                 if (sp != NULL)
9237                         rfs4_lo_state_rele(lsp, FALSE);
9238                 else
9239                         rfs4_lo_state_rele(lsp, TRUE);
9240         }
9241         if (sp) {
9242                 rfs4_sw_exit(&sp->rs_owner->ro_sw);
9243                 rfs4_state_rele(sp);
9244         }
9245 
9246         DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
9247             LOCK4res *, resp);
9248 }
9249 
9250 /* free function for LOCK/LOCKT */
9251 static void
9252 lock_denied_free(nfs_resop4 *resop)
9253 {
9254         LOCK4denied *dp = NULL;
9255 
9256         switch (resop->resop) {
9257         case OP_LOCK:
9258                 if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
9259                         dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
9260                 break;
9261         case OP_LOCKT:
9262                 if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
9263                         dp = &resop->nfs_resop4_u.oplockt.denied;
9264                 break;
9265         default:
9266                 break;
9267         }
9268 
9269         if (dp)
9270                 kmem_free(dp->owner.owner_val, dp->owner.owner_len);
9271 }
9272 
9273 /*ARGSUSED*/
9274 void
9275 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
9276     struct svc_req *req, struct compound_state *cs)
9277 {
9278         LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
9279         LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
9280         nfsstat4 status;
9281         stateid4 *stateid = &args->lock_stateid;
9282         rfs4_lo_state_t *lsp;
9283 
9284         DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
9285             LOCKU4args *, args);
9286 
9287         if (cs->vp == NULL) {
9288                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9289                 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9290                     LOCKU4res *, resp);
9291                 return;
9292         }
9293 
9294         if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
9295                 *cs->statusp = resp->status = status;
9296                 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9297                     LOCKU4res *, resp);
9298                 return;
9299         }
9300 
9301         /* Ensure specified filehandle matches */
9302         if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
9303                 rfs4_lo_state_rele(lsp, TRUE);
9304                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9305                 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9306                     LOCKU4res *, resp);
9307                 return;
9308         }
9309 
9310         /* hold off other access to lsp while we tinker */
9311         rfs4_sw_enter(&lsp->rls_sw);
9312 
9313         switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
9314         case NFS4_CHECK_STATEID_OKAY:
9315                 if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
9316                     != NFS4_CHKSEQ_OKAY) {
9317                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9318                         goto end;
9319                 }
9320                 break;
9321         case NFS4_CHECK_STATEID_OLD:
9322                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9323                 goto end;
9324         case NFS4_CHECK_STATEID_BAD:
9325                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9326                 goto end;
9327         case NFS4_CHECK_STATEID_EXPIRED:
9328                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
9329                 goto end;
9330         case NFS4_CHECK_STATEID_CLOSED:
9331                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9332                 goto end;
9333         case NFS4_CHECK_STATEID_REPLAY:
9334                 switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
9335                 case NFS4_CHKSEQ_OKAY:
9336                                 /*
9337                                  * This is a replayed stateid; if
9338                                  * seqid matches the next expected,
9339                                  * then client is using wrong seqid.
9340                                  */
9341                 case NFS4_CHKSEQ_BAD:
9342                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9343                         goto end;
9344                 case NFS4_CHKSEQ_REPLAY:
9345                         rfs4_update_lease(lsp->rls_locker->rl_client);
9346                         *cs->statusp = status = resp->status;
9347                         goto end;
9348                 }
9349                 break;
9350         default:
9351                 ASSERT(FALSE);
9352                 break;
9353         }
9354 
9355         rfs4_update_lock_sequence(lsp);
9356         rfs4_update_lease(lsp->rls_locker->rl_client);
9357 
9358         /*
9359          * NFS4 only allows locking on regular files, so
9360          * verify type of object.
9361          */
9362         if (cs->vp->v_type != VREG) {
9363                 if (cs->vp->v_type == VDIR)
9364                         status = NFS4ERR_ISDIR;
9365                 else
9366                         status = NFS4ERR_INVAL;
9367                 goto out;
9368         }
9369 
9370         if (rfs4_clnt_in_grace(lsp->rls_state->rs_owner->ro_client)) {
9371                 status = NFS4ERR_GRACE;
9372                 goto out;
9373         }
9374 
9375         status = rfs4_do_lock(lsp, args->locktype,
9376             args->offset, args->length, cs->cr, resop);
9377 
9378 out:
9379         *cs->statusp = resp->status = status;
9380 
9381         if (status == NFS4_OK)
9382                 resp->lock_stateid = lsp->rls_lockid.stateid;
9383 
9384         rfs4_update_lock_resp(lsp, resop);
9385 
9386 end:
9387         rfs4_sw_exit(&lsp->rls_sw);
9388         rfs4_lo_state_rele(lsp, TRUE);
9389 
9390         DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9391             LOCKU4res *, resp);
9392 }
9393 
9394 /*
9395  * LOCKT is a best effort routine, the client can not be guaranteed that
9396  * the status return is still in effect by the time the reply is received.
9397  * They are numerous race conditions in this routine, but we are not required
9398  * and can not be accurate.
9399  */
9400 /*ARGSUSED*/
9401 void
9402 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
9403     struct svc_req *req, struct compound_state *cs)
9404 {
9405         LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
9406         LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
9407         rfs4_lockowner_t *lo;
9408         rfs4_client_t *cp;
9409         bool_t create = FALSE;
9410         struct flock64 flk;
9411         int error;
9412         int flag = FREAD | FWRITE;
9413         int ltype;
9414         length4 posix_length;
9415         sysid_t sysid;
9416         pid_t pid;
9417 
9418         DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
9419             LOCKT4args *, args);
9420 
9421         if (cs->vp == NULL) {
9422                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9423                 goto out;
9424         }
9425 
9426         /*
9427          * NFS4 only allows locking on regular files, so
9428          * verify type of object.
9429          */
9430         if (cs->vp->v_type != VREG) {
9431                 if (cs->vp->v_type == VDIR)
9432                         *cs->statusp = resp->status = NFS4ERR_ISDIR;
9433                 else
9434                         *cs->statusp = resp->status =  NFS4ERR_INVAL;
9435                 goto out;
9436         }
9437 
9438         /*
9439          * Check out the clientid to ensure the server knows about it
9440          * so that we correctly inform the client of a server reboot.
9441          */
9442         if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
9443             == NULL) {
9444                 *cs->statusp = resp->status =
9445                     rfs4_check_clientid(&args->owner.clientid, 0);
9446                 goto out;
9447         }
9448         if (rfs4_lease_expired(cp)) {
9449                 rfs4_client_close(cp);
9450                 /*
9451                  * Protocol doesn't allow returning NFS4ERR_STALE as
9452                  * other operations do on this check so STALE_CLIENTID
9453                  * is returned instead
9454                  */
9455                 *cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
9456                 goto out;
9457         }
9458 
9459         if (rfs4_clnt_in_grace(cp) && !(cp->rc_can_reclaim)) {
9460                 *cs->statusp = resp->status = NFS4ERR_GRACE;
9461                 rfs4_client_rele(cp);
9462                 goto out;
9463         }
9464         rfs4_client_rele(cp);
9465 
9466         resp->status = NFS4_OK;
9467 
9468         switch (args->locktype) {
9469         case READ_LT:
9470         case READW_LT:
9471                 ltype = F_RDLCK;
9472                 break;
9473         case WRITE_LT:
9474         case WRITEW_LT:
9475                 ltype = F_WRLCK;
9476                 break;
9477         }
9478 
9479         posix_length = args->length;
9480         /* Check for zero length. To lock to end of file use all ones for V4 */
9481         if (posix_length == 0) {
9482                 *cs->statusp = resp->status = NFS4ERR_INVAL;
9483                 goto out;
9484         } else if (posix_length == (length4)(~0)) {
9485                 posix_length = 0;       /* Posix to end of file  */
9486         }
9487 
9488         /* Find or create a lockowner */
9489         lo = rfs4_findlockowner(&args->owner, &create);
9490 
9491         if (lo) {
9492                 pid = lo->rl_pid;
9493                 if ((resp->status =
9494                     rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
9495                         goto err;
9496         } else {
9497                 pid = 0;
9498                 sysid = lockt_sysid;
9499         }
9500 retry:
9501         flk.l_type = ltype;
9502         flk.l_whence = 0;               /* SEEK_SET */
9503         flk.l_start = args->offset;
9504         flk.l_len = posix_length;
9505         flk.l_sysid = sysid;
9506         flk.l_pid = pid;
9507         flag |= F_REMOTELOCK;
9508 
9509         LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
9510 
9511         /* Note that length4 is uint64_t but l_len and l_start are off64_t */
9512         if (flk.l_len < 0 || flk.l_start < 0) {
9513                 resp->status = NFS4ERR_INVAL;
9514                 goto err;
9515         }
9516         error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
9517             NULL, cs->cr, NULL);
9518 
9519         /*
9520          * N.B. We map error values to nfsv4 errors. This is differrent
9521          * than puterrno4 routine.
9522          */
9523         switch (error) {
9524         case 0:
9525                 if (flk.l_type == F_UNLCK)
9526                         resp->status = NFS4_OK;
9527                 else {
9528                         if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
9529                                 goto retry;
9530                         resp->status = NFS4ERR_DENIED;
9531                 }
9532                 break;
9533         case EOVERFLOW:
9534                 resp->status = NFS4ERR_INVAL;
9535                 break;
9536         case EINVAL:
9537                 resp->status = NFS4ERR_NOTSUPP;
9538                 break;
9539         default:
9540                 cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
9541                     error);
9542                 resp->status = NFS4ERR_SERVERFAULT;
9543                 break;
9544         }
9545 
9546 err:
9547         if (lo)
9548                 rfs4_lockowner_rele(lo);
9549         *cs->statusp = resp->status;
9550 out:
9551         DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
9552             LOCKT4res *, resp);
9553 }
9554 
9555 int
9556 rfs4_share(rfs4_state_t *sp, uint32_t access, uint32_t deny)
9557 {
9558         int err;
9559         int cmd;
9560         vnode_t *vp;
9561         struct shrlock shr;
9562         struct shr_locowner shr_loco;
9563         int fflags = 0;
9564 
9565         ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9566         ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9567 
9568         if (sp->rs_closed)
9569                 return (NFS4ERR_OLD_STATEID);
9570 
9571         vp = sp->rs_finfo->rf_vp;
9572         ASSERT(vp);
9573 
9574         shr.s_access = shr.s_deny = 0;
9575 
9576         if (access & OPEN4_SHARE_ACCESS_READ) {
9577                 fflags |= FREAD;
9578                 shr.s_access |= F_RDACC;
9579         }
9580         if (access & OPEN4_SHARE_ACCESS_WRITE) {
9581                 fflags |= FWRITE;
9582                 shr.s_access |= F_WRACC;
9583         }
9584         ASSERT(shr.s_access);
9585 
9586         if (deny & OPEN4_SHARE_DENY_READ)
9587                 shr.s_deny |= F_RDDNY;
9588         if (deny & OPEN4_SHARE_DENY_WRITE)
9589                 shr.s_deny |= F_WRDNY;
9590 
9591         shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9592         shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9593         shr_loco.sl_pid = shr.s_pid;
9594         shr_loco.sl_id = shr.s_sysid;
9595         shr.s_owner = (caddr_t)&shr_loco;
9596         shr.s_own_len = sizeof (shr_loco);
9597 
9598         cmd = nbl_need_check(vp) ? F_SHARE_NBMAND : F_SHARE;
9599 
9600         err = VOP_SHRLOCK(vp, cmd, &shr, fflags, CRED(), NULL);
9601         if (err != 0) {
9602                 if (err == EAGAIN)
9603                         err = NFS4ERR_SHARE_DENIED;
9604                 else
9605                         err = puterrno4(err);
9606                 return (err);
9607         }
9608 
9609         sp->rs_share_access |= access;
9610         sp->rs_share_deny |= deny;
9611 
9612         return (0);
9613 }
9614 
9615 int
9616 rfs4_unshare(rfs4_state_t *sp)
9617 {
9618         int err;
9619         struct shrlock shr;
9620         struct shr_locowner shr_loco;
9621 
9622         ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9623 
9624         if (sp->rs_closed || sp->rs_share_access == 0)
9625                 return (0);
9626 
9627         ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9628         ASSERT(sp->rs_finfo->rf_vp);
9629 
9630         shr.s_access = shr.s_deny = 0;
9631         shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9632         shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9633         shr_loco.sl_pid = shr.s_pid;
9634         shr_loco.sl_id = shr.s_sysid;
9635         shr.s_owner = (caddr_t)&shr_loco;
9636         shr.s_own_len = sizeof (shr_loco);
9637 
9638         err = VOP_SHRLOCK(sp->rs_finfo->rf_vp, F_UNSHARE, &shr, 0, CRED(),
9639             NULL);
9640         if (err != 0) {
9641                 err = puterrno4(err);
9642                 return (err);
9643         }
9644 
9645         sp->rs_share_access = 0;
9646         sp->rs_share_deny = 0;
9647 
9648         return (0);
9649 
9650 }
9651 
9652 static int
9653 rdma_setup_read_data4(READ4args *args, READ4res *rok)
9654 {
9655         struct clist    *wcl;
9656         count4          count = rok->data_len;
9657         int             wlist_len;
9658 
9659         wcl = args->wlist;
9660         if (rdma_setup_read_chunks(wcl, count, &wlist_len) == FALSE) {
9661                 return (FALSE);
9662         }
9663         wcl = args->wlist;
9664         rok->wlist_len = wlist_len;
9665         rok->wlist = wcl;
9666         return (TRUE);
9667 }
9668 
9669 /* tunable to disable server referrals */
9670 int rfs4_no_referrals = 0;
9671 
9672 /*
9673  * Find an NFS record in reparse point data.
9674  * Returns 0 for success and <0 or an errno value on failure.
9675  */
9676 int
9677 vn_find_nfs_record(vnode_t *vp, nvlist_t **nvlp, char **svcp, char **datap)
9678 {
9679         int err;
9680         char *stype, *val;
9681         nvlist_t *nvl;
9682         nvpair_t *curr;
9683 
9684         if ((nvl = reparse_init()) == NULL)
9685                 return (-1);
9686 
9687         if ((err = reparse_vnode_parse(vp, nvl)) != 0) {
9688                 reparse_free(nvl);
9689                 return (err);
9690         }
9691 
9692         curr = NULL;
9693         while ((curr = nvlist_next_nvpair(nvl, curr)) != NULL) {
9694                 if ((stype = nvpair_name(curr)) == NULL) {
9695                         reparse_free(nvl);
9696                         return (-2);
9697                 }
9698                 if (strncasecmp(stype, "NFS", 3) == 0)
9699                         break;
9700         }
9701 
9702         if ((curr == NULL) ||
9703             (nvpair_value_string(curr, &val))) {
9704                 reparse_free(nvl);
9705                 return (-3);
9706         }
9707         *nvlp = nvl;
9708         *svcp = stype;
9709         *datap = val;
9710         return (0);
9711 }
9712 
9713 int
9714 vn_is_nfs_reparse(vnode_t *vp, cred_t *cr)
9715 {
9716         nvlist_t *nvl;
9717         char *s, *d;
9718 
9719         if (rfs4_no_referrals != 0)
9720                 return (B_FALSE);
9721 
9722         if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
9723                 return (B_FALSE);
9724 
9725         if (vn_find_nfs_record(vp, &nvl, &s, &d) != 0)
9726                 return (B_FALSE);
9727 
9728         reparse_free(nvl);
9729 
9730         return (B_TRUE);
9731 }
9732 
9733 /*
9734  * There is a user-level copy of this routine in ref_subr.c.
9735  * Changes should be kept in sync.
9736  */
9737 static int
9738 nfs4_create_components(char *path, component4 *comp4)
9739 {
9740         int slen, plen, ncomp;
9741         char *ori_path, *nxtc, buf[MAXNAMELEN];
9742 
9743         if (path == NULL)
9744                 return (0);
9745 
9746         plen = strlen(path) + 1;        /* include the terminator */
9747         ori_path = path;
9748         ncomp = 0;
9749 
9750         /* count number of components in the path */
9751         for (nxtc = path; nxtc < ori_path + plen; nxtc++) {
9752                 if (*nxtc == '/' || *nxtc == '\0' || *nxtc == '\n') {
9753                         if ((slen = nxtc - path) == 0) {
9754                                 path = nxtc + 1;
9755                                 continue;
9756                         }
9757 
9758                         if (comp4 != NULL) {
9759                                 bcopy(path, buf, slen);
9760                                 buf[slen] = '\0';
9761                                 (void) str_to_utf8(buf, &comp4[ncomp]);
9762                         }
9763 
9764                         ncomp++;        /* 1 valid component */
9765                         path = nxtc + 1;
9766                 }
9767                 if (*nxtc == '\0' || *nxtc == '\n')
9768                         break;
9769         }
9770 
9771         return (ncomp);
9772 }
9773 
9774 /*
9775  * There is a user-level copy of this routine in ref_subr.c.
9776  * Changes should be kept in sync.
9777  */
9778 static int
9779 make_pathname4(char *path, pathname4 *pathname)
9780 {
9781         int ncomp;
9782         component4 *comp4;
9783 
9784         if (pathname == NULL)
9785                 return (0);
9786 
9787         if (path == NULL) {
9788                 pathname->pathname4_val = NULL;
9789                 pathname->pathname4_len = 0;
9790                 return (0);
9791         }
9792 
9793         /* count number of components to alloc buffer */
9794         if ((ncomp = nfs4_create_components(path, NULL)) == 0) {
9795                 pathname->pathname4_val = NULL;
9796                 pathname->pathname4_len = 0;
9797                 return (0);
9798         }
9799         comp4 = kmem_zalloc(ncomp * sizeof (component4), KM_SLEEP);
9800 
9801         /* copy components into allocated buffer */
9802         ncomp = nfs4_create_components(path, comp4);
9803 
9804         pathname->pathname4_val = comp4;
9805         pathname->pathname4_len = ncomp;
9806 
9807         return (ncomp);
9808 }
9809 
9810 #define xdr_fs_locations4 xdr_fattr4_fs_locations
9811 
9812 fs_locations4 *
9813 fetch_referral(vnode_t *vp, cred_t *cr)
9814 {
9815         nvlist_t *nvl;
9816         char *stype, *sdata;
9817         fs_locations4 *result;
9818         char buf[1024];
9819         size_t bufsize;
9820         XDR xdr;
9821         int err;
9822 
9823         /*
9824          * Check attrs to ensure it's a reparse point
9825          */
9826         if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
9827                 return (NULL);
9828 
9829         /*
9830          * Look for an NFS record and get the type and data
9831          */
9832         if (vn_find_nfs_record(vp, &nvl, &stype, &sdata) != 0)
9833                 return (NULL);
9834 
9835         /*
9836          * With the type and data, upcall to get the referral
9837          */
9838         bufsize = sizeof (buf);
9839         bzero(buf, sizeof (buf));
9840         err = reparse_kderef((const char *)stype, (const char *)sdata,
9841             buf, &bufsize);
9842         reparse_free(nvl);
9843 
9844         DTRACE_PROBE4(nfs4serv__func__referral__upcall,
9845             char *, stype, char *, sdata, char *, buf, int, err);
9846         if (err) {
9847                 cmn_err(CE_NOTE,
9848                     "reparsed daemon not running: unable to get referral (%d)",
9849                     err);
9850                 return (NULL);
9851         }
9852 
9853         /*
9854          * We get an XDR'ed record back from the kderef call
9855          */
9856         xdrmem_create(&xdr, buf, bufsize, XDR_DECODE);
9857         result = kmem_alloc(sizeof (fs_locations4), KM_SLEEP);
9858         err = xdr_fs_locations4(&xdr, result);
9859         XDR_DESTROY(&xdr);
9860         if (err != TRUE) {
9861                 DTRACE_PROBE1(nfs4serv__func__referral__upcall__xdrfail,
9862                     int, err);
9863                 return (NULL);
9864         }
9865 
9866         /*
9867          * Look at path to recover fs_root, ignoring the leading '/'
9868          */
9869         (void) make_pathname4(vp->v_path, &result->fs_root);
9870 
9871         return (result);
9872 }
9873 
9874 char *
9875 build_symlink(vnode_t *vp, cred_t *cr, size_t *strsz)
9876 {
9877         fs_locations4 *fsl;
9878         fs_location4 *fs;
9879         char *server, *path, *symbuf;
9880         static char *prefix = "/net/";
9881         int i, size, npaths;
9882         uint_t len;
9883 
9884         /* Get the referral */
9885         if ((fsl = fetch_referral(vp, cr)) == NULL)
9886                 return (NULL);
9887 
9888         /* Deal with only the first location and first server */
9889         fs = &fsl->locations_val[0];
9890         server = utf8_to_str(&fs->server_val[0], &len, NULL);
9891         if (server == NULL) {
9892                 rfs4_free_fs_locations4(fsl);
9893                 kmem_free(fsl, sizeof (fs_locations4));
9894                 return (NULL);
9895         }
9896 
9897         /* Figure out size for "/net/" + host + /path/path/path + NULL */
9898         size = strlen(prefix) + len;
9899         for (i = 0; i < fs->rootpath.pathname4_len; i++)
9900                 size += fs->rootpath.pathname4_val[i].utf8string_len + 1;
9901 
9902         /* Allocate the symlink buffer and fill it */
9903         symbuf = kmem_zalloc(size, KM_SLEEP);
9904         (void) strcat(symbuf, prefix);
9905         (void) strcat(symbuf, server);
9906         kmem_free(server, len);
9907 
9908         npaths = 0;
9909         for (i = 0; i < fs->rootpath.pathname4_len; i++) {
9910                 path = utf8_to_str(&fs->rootpath.pathname4_val[i], &len, NULL);
9911                 if (path == NULL)
9912                         continue;
9913                 (void) strcat(symbuf, "/");
9914                 (void) strcat(symbuf, path);
9915                 npaths++;
9916                 kmem_free(path, len);
9917         }
9918 
9919         rfs4_free_fs_locations4(fsl);
9920         kmem_free(fsl, sizeof (fs_locations4));
9921 
9922         if (strsz != NULL)
9923                 *strsz = size;
9924         return (symbuf);
9925 }
9926 
9927 /*
9928  * Check to see if we have a downrev Solaris client, so that we
9929  * can send it a symlink instead of a referral.
9930  */
9931 int
9932 client_is_downrev(struct svc_req *req)
9933 {
9934         struct sockaddr *ca;
9935         rfs4_clntip_t *ci;
9936         bool_t create = FALSE;
9937         int is_downrev;
9938 
9939         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
9940         ASSERT(ca);
9941         ci = rfs4_find_clntip(ca, &create);
9942         if (ci == NULL)
9943                 return (0);
9944         is_downrev = ci->ri_no_referrals;
9945         rfs4_dbe_rele(ci->ri_dbe);
9946         return (is_downrev);
9947 }
9948 
9949 /*
9950  * Do the main work of handling HA-NFSv4 Resource Group failover on
9951  * Sun Cluster.
9952  * We need to detect whether any RG admin paths have been added or removed,
9953  * and adjust resources accordingly.
9954  * Currently we're using a very inefficient algorithm, ~ 2 * O(n**2). In
9955  * order to scale, the list and array of paths need to be held in more
9956  * suitable data structures.
9957  */
9958 static void
9959 hanfsv4_failover(nfs4_srv_t *nsrv4)
9960 {
9961         int i, start_grace, numadded_paths = 0;
9962         char **added_paths = NULL;
9963         rfs4_dss_path_t *dss_path;
9964 
9965         /*
9966          * Note: currently, dss_pathlist cannot be NULL, since
9967          * it will always include an entry for NFS4_DSS_VAR_DIR. If we
9968          * make the latter dynamically specified too, the following will
9969          * need to be adjusted.
9970          */
9971 
9972         /*
9973          * First, look for removed paths: RGs that have been failed-over
9974          * away from this node.
9975          * Walk the "currently-serving" dss_pathlist and, for each
9976          * path, check if it is on the "passed-in" rfs4_dss_newpaths array
9977          * from nfsd. If not, that RG path has been removed.
9978          *
9979          * Note that nfsd has sorted rfs4_dss_newpaths for us, and removed
9980          * any duplicates.
9981          */
9982         dss_path = nsrv4->dss_pathlist;
9983         do {
9984                 int found = 0;
9985                 char *path = dss_path->path;
9986 
9987                 /* used only for non-HA so may not be removed */
9988                 if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
9989                         dss_path = dss_path->next;
9990                         continue;
9991                 }
9992 
9993                 for (i = 0; i < rfs4_dss_numnewpaths; i++) {
9994                         int cmpret;
9995                         char *newpath = rfs4_dss_newpaths[i];
9996 
9997                         /*
9998                          * Since nfsd has sorted rfs4_dss_newpaths for us,
9999                          * once the return from strcmp is negative we know
10000                          * we've passed the point where "path" should be,
10001                          * and can stop searching: "path" has been removed.
10002                          */
10003                         cmpret = strcmp(path, newpath);
10004                         if (cmpret < 0)
10005                                 break;
10006                         if (cmpret == 0) {
10007                                 found = 1;
10008                                 break;
10009                         }
10010                 }
10011 
10012                 if (found == 0) {
10013                         unsigned index = dss_path->index;
10014                         rfs4_servinst_t *sip = dss_path->sip;
10015                         rfs4_dss_path_t *path_next = dss_path->next;
10016 
10017                         /*
10018                          * This path has been removed.
10019                          * We must clear out the servinst reference to
10020                          * it, since it's now owned by another
10021                          * node: we should not attempt to touch it.
10022                          */
10023                         ASSERT(dss_path == sip->dss_paths[index]);
10024                         sip->dss_paths[index] = NULL;
10025 
10026                         /* remove from "currently-serving" list, and destroy */
10027                         remque(dss_path);
10028                         /* allow for NUL */
10029                         kmem_free(dss_path->path, strlen(dss_path->path) + 1);
10030                         kmem_free(dss_path, sizeof (rfs4_dss_path_t));
10031 
10032                         dss_path = path_next;
10033                 } else {
10034                         /* path was found; not removed */
10035                         dss_path = dss_path->next;
10036                 }
10037         } while (dss_path != nsrv4->dss_pathlist);
10038 
10039         /*
10040          * Now, look for added paths: RGs that have been failed-over
10041          * to this node.
10042          * Walk the "passed-in" rfs4_dss_newpaths array from nfsd and,
10043          * for each path, check if it is on the "currently-serving"
10044          * dss_pathlist. If not, that RG path has been added.
10045          *
10046          * Note: we don't do duplicate detection here; nfsd does that for us.
10047          *
10048          * Note: numadded_paths <= rfs4_dss_numnewpaths, which gives us
10049          * an upper bound for the size needed for added_paths[numadded_paths].
10050          */
10051 
10052         /* probably more space than we need, but guaranteed to be enough */
10053         if (rfs4_dss_numnewpaths > 0) {
10054                 size_t sz = rfs4_dss_numnewpaths * sizeof (char *);
10055                 added_paths = kmem_zalloc(sz, KM_SLEEP);
10056         }
10057 
10058         /* walk the "passed-in" rfs4_dss_newpaths array from nfsd */
10059         for (i = 0; i < rfs4_dss_numnewpaths; i++) {
10060                 int found = 0;
10061                 char *newpath = rfs4_dss_newpaths[i];
10062 
10063                 dss_path = nsrv4->dss_pathlist;
10064                 do {
10065                         char *path = dss_path->path;
10066 
10067                         /* used only for non-HA */
10068                         if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
10069                                 dss_path = dss_path->next;
10070                                 continue;
10071                         }
10072 
10073                         if (strncmp(path, newpath, strlen(path)) == 0) {
10074                                 found = 1;
10075                                 break;
10076                         }
10077 
10078                         dss_path = dss_path->next;
10079                 } while (dss_path != nsrv4->dss_pathlist);
10080 
10081                 if (found == 0) {
10082                         added_paths[numadded_paths] = newpath;
10083                         numadded_paths++;
10084                 }
10085         }
10086 
10087         /* did we find any added paths? */
10088         if (numadded_paths > 0) {
10089 
10090                 /* create a new server instance, and start its grace period */
10091                 start_grace = 1;
10092                 /* CSTYLED */
10093                 rfs4_servinst_create(nsrv4, start_grace, numadded_paths, added_paths);
10094 
10095                 /* read in the stable storage state from these paths */
10096                 rfs4_dss_readstate(nsrv4, numadded_paths, added_paths);
10097 
10098                 /*
10099                  * Multiple failovers during a grace period will cause
10100                  * clients of the same resource group to be partitioned
10101                  * into different server instances, with different
10102                  * grace periods.  Since clients of the same resource
10103                  * group must be subject to the same grace period,
10104                  * we need to reset all currently active grace periods.
10105                  */
10106                 rfs4_grace_reset_all(nsrv4);
10107         }
10108 
10109         if (rfs4_dss_numnewpaths > 0)
10110                 kmem_free(added_paths, rfs4_dss_numnewpaths * sizeof (char *));
10111 }