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 request tag.
5805          */
5806         resp->tag.utf8string_len = args->tag.utf8string_len;
5807         if (args->tag.utf8string_len != 0) {
5808                 resp->tag.utf8string_val =
5809                     kmem_alloc(args->tag.utf8string_len, KM_SLEEP);
5810                 bcopy(args->tag.utf8string_val, resp->tag.utf8string_val,
5811                     resp->tag.utf8string_len);
5812         } else {
5813                 resp->tag.utf8string_val = NULL;
5814         }
5815 
5816         cs.statusp = &resp->status;
5817         cs.req = req;
5818         resp->array = NULL;
5819         resp->array_len = 0;
5820 
5821         /*
5822          * XXX for now, minorversion should be zero
5823          */
5824         if (args->minorversion != NFS4_MINORVERSION) {
5825                 DTRACE_NFSV4_2(compound__start, struct compound_state *,
5826                     &cs, COMPOUND4args *, args);
5827                 resp->status = NFS4ERR_MINOR_VERS_MISMATCH;
5828                 DTRACE_NFSV4_2(compound__done, struct compound_state *,
5829                     &cs, COMPOUND4res *, resp);
5830                 return;
5831         }
5832 
5833         if (args->array_len == 0) {
5834                 resp->status = NFS4_OK;
5835                 return;
5836         }
5837 
5838         ASSERT(exi == NULL);
5839         ASSERT(cr == NULL);
5840 
5841         cr = crget();
5842         ASSERT(cr != NULL);
5843 
5844         if (sec_svc_getcred(req, cr, &cs.principal, &cs.nfsflavor) == 0) {
5845                 DTRACE_NFSV4_2(compound__start, struct compound_state *,
5846                     &cs, COMPOUND4args *, args);
5847                 crfree(cr);
5848                 DTRACE_NFSV4_2(compound__done, struct compound_state *,
5849                     &cs, COMPOUND4res *, resp);
5850                 svcerr_badcred(req->rq_xprt);
5851                 if (rv != NULL)
5852                         *rv = 1;
5853                 return;
5854         }
5855         resp->array_len = args->array_len;
5856         resp->array = kmem_zalloc(args->array_len * sizeof (nfs_resop4),
5857             KM_SLEEP);
5858 
5859         cs.basecr = cr;
5860         nsrv4 = nfs4_get_srv();
5861 
5862         DTRACE_NFSV4_2(compound__start, struct compound_state *, &cs,
5863             COMPOUND4args *, args);
5864 
5865         /*
5866          * For now, NFS4 compound processing must be protected by
5867          * exported_lock because it can access more than one exportinfo
5868          * per compound and share/unshare can now change multiple
5869          * exinfo structs.  The NFS2/3 code only refs 1 exportinfo
5870          * per proc (excluding public exinfo), and exi_count design
5871          * is sufficient to protect concurrent execution of NFS2/3
5872          * ops along with unexport.  This lock will be removed as
5873          * part of the NFSv4 phase 2 namespace redesign work.
5874          */
5875         rw_enter(&ne->exported_lock, RW_READER);
5876 
5877         /*
5878          * If this is the first compound we've seen, we need to start all
5879          * new instances' grace periods.
5880          */
5881         if (nsrv4->seen_first_compound == 0) {
5882                 rfs4_grace_start_new(nsrv4);
5883                 /*
5884                  * This must be set after rfs4_grace_start_new(), otherwise
5885                  * another thread could proceed past here before the former
5886                  * is finished.
5887                  */
5888                 nsrv4->seen_first_compound = 1;
5889         }
5890 
5891         for (i = 0; i < args->array_len && cs.cont; i++) {
5892                 nfs_argop4 *argop;
5893                 nfs_resop4 *resop;
5894                 uint_t op;
5895 
5896                 argop = &args->array[i];
5897                 resop = &resp->array[i];
5898                 resop->resop = argop->argop;
5899                 op = (uint_t)resop->resop;
5900 
5901                 if (op < rfsv4disp_cnt) {
5902                         /*
5903                          * Count the individual ops here; NULL and COMPOUND
5904                          * are counted in common_dispatch()
5905                          */
5906                         rfsproccnt_v4_ptr[op].value.ui64++;
5907 
5908                         NFS4_DEBUG(rfs4_debug > 1,
5909                             (CE_NOTE, "Executing %s", rfs4_op_string[op]));
5910                         (*rfsv4disptab[op].dis_proc)(argop, resop, req, &cs);
5911                         NFS4_DEBUG(rfs4_debug > 1, (CE_NOTE, "%s returned %d",
5912                             rfs4_op_string[op], *cs.statusp));
5913                         if (*cs.statusp != NFS4_OK)
5914                                 cs.cont = FALSE;
5915                 } else {
5916                         /*
5917                          * This is effectively dead code since XDR code
5918                          * will have already returned BADXDR if op doesn't
5919                          * decode to legal value.  This only done for a
5920                          * day when XDR code doesn't verify v4 opcodes.
5921                          */
5922                         op = OP_ILLEGAL;
5923                         rfsproccnt_v4_ptr[OP_ILLEGAL_IDX].value.ui64++;
5924 
5925                         rfs4_op_illegal(argop, resop, req, &cs);
5926                         cs.cont = FALSE;
5927                 }
5928 
5929                 /*
5930                  * If not at last op, and if we are to stop, then
5931                  * compact the results array.
5932                  */
5933                 if ((i + 1) < args->array_len && !cs.cont) {
5934                         nfs_resop4 *new_res = kmem_alloc(
5935                             (i+1) * sizeof (nfs_resop4), KM_SLEEP);
5936                         bcopy(resp->array,
5937                             new_res, (i+1) * sizeof (nfs_resop4));
5938                         kmem_free(resp->array,
5939                             args->array_len * sizeof (nfs_resop4));
5940 
5941                         resp->array_len =  i + 1;
5942                         resp->array = new_res;
5943                 }
5944         }
5945 
5946         rw_exit(&ne->exported_lock);
5947 
5948         /*
5949          * clear exportinfo and vnode fields from compound_state before dtrace
5950          * probe, to avoid tracing residual values for path and share path.
5951          */
5952         if (cs.vp)
5953                 VN_RELE(cs.vp);
5954         if (cs.saved_vp)
5955                 VN_RELE(cs.saved_vp);
5956         cs.exi = cs.saved_exi = NULL;
5957         cs.vp = cs.saved_vp = NULL;
5958 
5959         DTRACE_NFSV4_2(compound__done, struct compound_state *, &cs,
5960             COMPOUND4res *, resp);
5961 
5962         if (cs.saved_fh.nfs_fh4_val)
5963                 kmem_free(cs.saved_fh.nfs_fh4_val, NFS4_FHSIZE);
5964 
5965         if (cs.basecr)
5966                 crfree(cs.basecr);
5967         if (cs.cr)
5968                 crfree(cs.cr);
5969         /*
5970          * done with this compound request, free the label
5971          */
5972 
5973         if (req->rq_label != NULL) {
5974                 kmem_free(req->rq_label, sizeof (bslabel_t));
5975                 req->rq_label = NULL;
5976         }
5977 }
5978 
5979 /*
5980  * XXX because of what appears to be duplicate calls to rfs4_compound_free
5981  * XXX zero out the tag and array values. Need to investigate why the
5982  * XXX calls occur, but at least prevent the panic for now.
5983  */
5984 void
5985 rfs4_compound_free(COMPOUND4res *resp)
5986 {
5987         uint_t i;
5988 
5989         if (resp->tag.utf8string_val) {
5990                 UTF8STRING_FREE(resp->tag)
5991         }
5992 
5993         for (i = 0; i < resp->array_len; i++) {
5994                 nfs_resop4 *resop;
5995                 uint_t op;
5996 
5997                 resop = &resp->array[i];
5998                 op = (uint_t)resop->resop;
5999                 if (op < rfsv4disp_cnt) {
6000                         (*rfsv4disptab[op].dis_resfree)(resop);
6001                 }
6002         }
6003         if (resp->array != NULL) {
6004                 kmem_free(resp->array, resp->array_len * sizeof (nfs_resop4));
6005         }
6006 }
6007 
6008 /*
6009  * Process the value of the compound request rpc flags, as a bit-AND
6010  * of the individual per-op flags (idempotent, allowork, publicfh_ok)
6011  */
6012 void
6013 rfs4_compound_flagproc(COMPOUND4args *args, int *flagp)
6014 {
6015         int i;
6016         int flag = RPC_ALL;
6017 
6018         for (i = 0; flag && i < args->array_len; i++) {
6019                 uint_t op;
6020 
6021                 op = (uint_t)args->array[i].argop;
6022 
6023                 if (op < rfsv4disp_cnt)
6024                         flag &= rfsv4disptab[op].dis_flags;
6025                 else
6026                         flag = 0;
6027         }
6028         *flagp = flag;
6029 }
6030 
6031 nfsstat4
6032 rfs4_client_sysid(rfs4_client_t *cp, sysid_t *sp)
6033 {
6034         nfsstat4 e;
6035 
6036         rfs4_dbe_lock(cp->rc_dbe);
6037 
6038         if (cp->rc_sysidt != LM_NOSYSID) {
6039                 *sp = cp->rc_sysidt;
6040                 e = NFS4_OK;
6041 
6042         } else if ((cp->rc_sysidt = lm_alloc_sysidt()) != LM_NOSYSID) {
6043                 *sp = cp->rc_sysidt;
6044                 e = NFS4_OK;
6045 
6046                 NFS4_DEBUG(rfs4_debug, (CE_NOTE,
6047                     "rfs4_client_sysid: allocated 0x%x\n", *sp));
6048         } else
6049                 e = NFS4ERR_DELAY;
6050 
6051         rfs4_dbe_unlock(cp->rc_dbe);
6052         return (e);
6053 }
6054 
6055 #if defined(DEBUG) && ! defined(lint)
6056 static void lock_print(char *str, int operation, struct flock64 *flk)
6057 {
6058         char *op, *type;
6059 
6060         switch (operation) {
6061         case F_GETLK: op = "F_GETLK";
6062                 break;
6063         case F_SETLK: op = "F_SETLK";
6064                 break;
6065         case F_SETLK_NBMAND: op = "F_SETLK_NBMAND";
6066                 break;
6067         default: op = "F_UNKNOWN";
6068                 break;
6069         }
6070         switch (flk->l_type) {
6071         case F_UNLCK: type = "F_UNLCK";
6072                 break;
6073         case F_RDLCK: type = "F_RDLCK";
6074                 break;
6075         case F_WRLCK: type = "F_WRLCK";
6076                 break;
6077         default: type = "F_UNKNOWN";
6078                 break;
6079         }
6080 
6081         ASSERT(flk->l_whence == 0);
6082         cmn_err(CE_NOTE, "%s:  %s, type = %s, off = %llx len = %llx pid = %d",
6083             str, op, type, (longlong_t)flk->l_start,
6084             flk->l_len ? (longlong_t)flk->l_len : ~0LL, flk->l_pid);
6085 }
6086 
6087 #define LOCK_PRINT(d, s, t, f) if (d) lock_print(s, t, f)
6088 #else
6089 #define LOCK_PRINT(d, s, t, f)
6090 #endif
6091 
6092 /*ARGSUSED*/
6093 static bool_t
6094 creds_ok(cred_set_t cr_set, struct svc_req *req, struct compound_state *cs)
6095 {
6096         return (TRUE);
6097 }
6098 
6099 /*
6100  * Look up the pathname using the vp in cs as the directory vnode.
6101  * cs->vp will be the vnode for the file on success
6102  */
6103 
6104 static nfsstat4
6105 rfs4_lookup(component4 *component, struct svc_req *req,
6106     struct compound_state *cs)
6107 {
6108         char *nm;
6109         uint32_t len;
6110         nfsstat4 status;
6111         struct sockaddr *ca;
6112         char *name;
6113 
6114         if (cs->vp == NULL) {
6115                 return (NFS4ERR_NOFILEHANDLE);
6116         }
6117         if (cs->vp->v_type != VDIR) {
6118                 return (NFS4ERR_NOTDIR);
6119         }
6120 
6121         status = utf8_dir_verify(component);
6122         if (status != NFS4_OK)
6123                 return (status);
6124 
6125         nm = utf8_to_fn(component, &len, NULL);
6126         if (nm == NULL) {
6127                 return (NFS4ERR_INVAL);
6128         }
6129 
6130         if (len > MAXNAMELEN) {
6131                 kmem_free(nm, len);
6132                 return (NFS4ERR_NAMETOOLONG);
6133         }
6134 
6135         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6136         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6137             MAXPATHLEN + 1);
6138 
6139         if (name == NULL) {
6140                 kmem_free(nm, len);
6141                 return (NFS4ERR_INVAL);
6142         }
6143 
6144         status = do_rfs4_op_lookup(name, req, cs);
6145 
6146         if (name != nm)
6147                 kmem_free(name, MAXPATHLEN + 1);
6148 
6149         kmem_free(nm, len);
6150 
6151         return (status);
6152 }
6153 
6154 static nfsstat4
6155 rfs4_lookupfile(component4 *component, struct svc_req *req,
6156     struct compound_state *cs, uint32_t access, change_info4 *cinfo)
6157 {
6158         nfsstat4 status;
6159         vnode_t *dvp = cs->vp;
6160         vattr_t bva, ava, fva;
6161         int error;
6162 
6163         /* Get "before" change value */
6164         bva.va_mask = AT_CTIME|AT_SEQ;
6165         error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6166         if (error)
6167                 return (puterrno4(error));
6168 
6169         /* rfs4_lookup may VN_RELE directory */
6170         VN_HOLD(dvp);
6171 
6172         status = rfs4_lookup(component, req, cs);
6173         if (status != NFS4_OK) {
6174                 VN_RELE(dvp);
6175                 return (status);
6176         }
6177 
6178         /*
6179          * Get "after" change value, if it fails, simply return the
6180          * before value.
6181          */
6182         ava.va_mask = AT_CTIME|AT_SEQ;
6183         if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6184                 ava.va_ctime = bva.va_ctime;
6185                 ava.va_seq = 0;
6186         }
6187         VN_RELE(dvp);
6188 
6189         /*
6190          * Validate the file is a file
6191          */
6192         fva.va_mask = AT_TYPE|AT_MODE;
6193         error = VOP_GETATTR(cs->vp, &fva, 0, cs->cr, NULL);
6194         if (error)
6195                 return (puterrno4(error));
6196 
6197         if (fva.va_type != VREG) {
6198                 if (fva.va_type == VDIR)
6199                         return (NFS4ERR_ISDIR);
6200                 if (fva.va_type == VLNK)
6201                         return (NFS4ERR_SYMLINK);
6202                 return (NFS4ERR_INVAL);
6203         }
6204 
6205         NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime);
6206         NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6207 
6208         /*
6209          * It is undefined if VOP_LOOKUP will change va_seq, so
6210          * cinfo.atomic = TRUE only if we have
6211          * non-zero va_seq's, and they have not changed.
6212          */
6213         if (bva.va_seq && ava.va_seq && ava.va_seq == bva.va_seq)
6214                 cinfo->atomic = TRUE;
6215         else
6216                 cinfo->atomic = FALSE;
6217 
6218         /* Check for mandatory locking */
6219         cs->mandlock = MANDLOCK(cs->vp, fva.va_mode);
6220         return (check_open_access(access, cs, req));
6221 }
6222 
6223 static nfsstat4
6224 create_vnode(vnode_t *dvp, char *nm,  vattr_t *vap, createmode4 mode,
6225     cred_t *cr, vnode_t **vpp, bool_t *created)
6226 {
6227         int error;
6228         nfsstat4 status = NFS4_OK;
6229         vattr_t va;
6230 
6231 tryagain:
6232 
6233         /*
6234          * The file open mode used is VWRITE.  If the client needs
6235          * some other semantic, then it should do the access checking
6236          * itself.  It would have been nice to have the file open mode
6237          * passed as part of the arguments.
6238          */
6239 
6240         *created = TRUE;
6241         error = VOP_CREATE(dvp, nm, vap, EXCL, VWRITE, vpp, cr, 0, NULL, NULL);
6242 
6243         if (error) {
6244                 *created = FALSE;
6245 
6246                 /*
6247                  * If we got something other than file already exists
6248                  * then just return this error.  Otherwise, we got
6249                  * EEXIST.  If we were doing a GUARDED create, then
6250                  * just return this error.  Otherwise, we need to
6251                  * make sure that this wasn't a duplicate of an
6252                  * exclusive create request.
6253                  *
6254                  * The assumption is made that a non-exclusive create
6255                  * request will never return EEXIST.
6256                  */
6257 
6258                 if (error != EEXIST || mode == GUARDED4) {
6259                         status = puterrno4(error);
6260                         return (status);
6261                 }
6262                 error = VOP_LOOKUP(dvp, nm, vpp, NULL, 0, NULL, cr,
6263                     NULL, NULL, NULL);
6264 
6265                 if (error) {
6266                         /*
6267                          * We couldn't find the file that we thought that
6268                          * we just created.  So, we'll just try creating
6269                          * it again.
6270                          */
6271                         if (error == ENOENT)
6272                                 goto tryagain;
6273 
6274                         status = puterrno4(error);
6275                         return (status);
6276                 }
6277 
6278                 if (mode == UNCHECKED4) {
6279                         /* existing object must be regular file */
6280                         if ((*vpp)->v_type != VREG) {
6281                                 if ((*vpp)->v_type == VDIR)
6282                                         status = NFS4ERR_ISDIR;
6283                                 else if ((*vpp)->v_type == VLNK)
6284                                         status = NFS4ERR_SYMLINK;
6285                                 else
6286                                         status = NFS4ERR_INVAL;
6287                                 VN_RELE(*vpp);
6288                                 return (status);
6289                         }
6290 
6291                         return (NFS4_OK);
6292                 }
6293 
6294                 /* Check for duplicate request */
6295                 va.va_mask = AT_MTIME;
6296                 error = VOP_GETATTR(*vpp, &va, 0, cr, NULL);
6297                 if (!error) {
6298                         /* We found the file */
6299                         const timestruc_t *mtime = &vap->va_mtime;
6300 
6301                         if (va.va_mtime.tv_sec != mtime->tv_sec ||
6302                             va.va_mtime.tv_nsec != mtime->tv_nsec) {
6303                                 /* but its not our creation */
6304                                 VN_RELE(*vpp);
6305                                 return (NFS4ERR_EXIST);
6306                         }
6307                         *created = TRUE; /* retrans of create == created */
6308                         return (NFS4_OK);
6309                 }
6310                 VN_RELE(*vpp);
6311                 return (NFS4ERR_EXIST);
6312         }
6313 
6314         return (NFS4_OK);
6315 }
6316 
6317 static nfsstat4
6318 check_open_access(uint32_t access, struct compound_state *cs,
6319     struct svc_req *req)
6320 {
6321         int error;
6322         vnode_t *vp;
6323         bool_t readonly;
6324         cred_t *cr = cs->cr;
6325 
6326         /* For now we don't allow mandatory locking as per V2/V3 */
6327         if (cs->access == CS_ACCESS_DENIED || cs->mandlock) {
6328                 return (NFS4ERR_ACCESS);
6329         }
6330 
6331         vp = cs->vp;
6332         ASSERT(cr != NULL && vp->v_type == VREG);
6333 
6334         /*
6335          * If the file system is exported read only and we are trying
6336          * to open for write, then return NFS4ERR_ROFS
6337          */
6338 
6339         readonly = rdonly4(req, cs);
6340 
6341         if ((access & OPEN4_SHARE_ACCESS_WRITE) && readonly)
6342                 return (NFS4ERR_ROFS);
6343 
6344         if (access & OPEN4_SHARE_ACCESS_READ) {
6345                 if ((VOP_ACCESS(vp, VREAD, 0, cr, NULL) != 0) &&
6346                     (VOP_ACCESS(vp, VEXEC, 0, cr, NULL) != 0)) {
6347                         return (NFS4ERR_ACCESS);
6348                 }
6349         }
6350 
6351         if (access & OPEN4_SHARE_ACCESS_WRITE) {
6352                 error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
6353                 if (error)
6354                         return (NFS4ERR_ACCESS);
6355         }
6356 
6357         return (NFS4_OK);
6358 }
6359 
6360 static nfsstat4
6361 rfs4_createfile(OPEN4args *args, struct svc_req *req, struct compound_state *cs,
6362     change_info4 *cinfo, bitmap4 *attrset, clientid4 clientid)
6363 {
6364         struct nfs4_svgetit_arg sarg;
6365         struct nfs4_ntov_table ntov;
6366 
6367         bool_t ntov_table_init = FALSE;
6368         struct statvfs64 sb;
6369         nfsstat4 status;
6370         vnode_t *vp;
6371         vattr_t bva, ava, iva, cva, *vap;
6372         vnode_t *dvp;
6373         timespec32_t *mtime;
6374         char *nm = NULL;
6375         uint_t buflen;
6376         bool_t created;
6377         bool_t setsize = FALSE;
6378         len_t reqsize;
6379         int error;
6380         bool_t trunc;
6381         caller_context_t ct;
6382         component4 *component;
6383         bslabel_t *clabel;
6384         struct sockaddr *ca;
6385         char *name = NULL;
6386 
6387         sarg.sbp = &sb;
6388         sarg.is_referral = B_FALSE;
6389 
6390         dvp = cs->vp;
6391 
6392         /* Check if the file system is read only */
6393         if (rdonly4(req, cs))
6394                 return (NFS4ERR_ROFS);
6395 
6396         /* check the label of including directory */
6397         if (is_system_labeled()) {
6398                 ASSERT(req->rq_label != NULL);
6399                 clabel = req->rq_label;
6400                 DTRACE_PROBE2(tx__rfs4__log__info__opremove__clabel, char *,
6401                     "got client label from request(1)",
6402                     struct svc_req *, req);
6403                 if (!blequal(&l_admin_low->tsl_label, clabel)) {
6404                         if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
6405                             cs->exi)) {
6406                                 return (NFS4ERR_ACCESS);
6407                         }
6408                 }
6409         }
6410 
6411         /*
6412          * Get the last component of path name in nm. cs will reference
6413          * the including directory on success.
6414          */
6415         component = &args->open_claim4_u.file;
6416         status = utf8_dir_verify(component);
6417         if (status != NFS4_OK)
6418                 return (status);
6419 
6420         nm = utf8_to_fn(component, &buflen, NULL);
6421 
6422         if (nm == NULL)
6423                 return (NFS4ERR_RESOURCE);
6424 
6425         if (buflen > MAXNAMELEN) {
6426                 kmem_free(nm, buflen);
6427                 return (NFS4ERR_NAMETOOLONG);
6428         }
6429 
6430         bva.va_mask = AT_TYPE|AT_CTIME|AT_SEQ;
6431         error = VOP_GETATTR(dvp, &bva, 0, cs->cr, NULL);
6432         if (error) {
6433                 kmem_free(nm, buflen);
6434                 return (puterrno4(error));
6435         }
6436 
6437         if (bva.va_type != VDIR) {
6438                 kmem_free(nm, buflen);
6439                 return (NFS4ERR_NOTDIR);
6440         }
6441 
6442         NFS4_SET_FATTR4_CHANGE(cinfo->before, bva.va_ctime)
6443 
6444         switch (args->mode) {
6445         case GUARDED4:
6446                 /*FALLTHROUGH*/
6447         case UNCHECKED4:
6448                 nfs4_ntov_table_init(&ntov);
6449                 ntov_table_init = TRUE;
6450 
6451                 *attrset = 0;
6452                 status = do_rfs4_set_attrs(attrset,
6453                     &args->createhow4_u.createattrs,
6454                     cs, &sarg, &ntov, NFS4ATTR_SETIT);
6455 
6456                 if (status == NFS4_OK && (sarg.vap->va_mask & AT_TYPE) &&
6457                     sarg.vap->va_type != VREG) {
6458                         if (sarg.vap->va_type == VDIR)
6459                                 status = NFS4ERR_ISDIR;
6460                         else if (sarg.vap->va_type == VLNK)
6461                                 status = NFS4ERR_SYMLINK;
6462                         else
6463                                 status = NFS4ERR_INVAL;
6464                 }
6465 
6466                 if (status != NFS4_OK) {
6467                         kmem_free(nm, buflen);
6468                         nfs4_ntov_table_free(&ntov, &sarg);
6469                         *attrset = 0;
6470                         return (status);
6471                 }
6472 
6473                 vap = sarg.vap;
6474                 vap->va_type = VREG;
6475                 vap->va_mask |= AT_TYPE;
6476 
6477                 if ((vap->va_mask & AT_MODE) == 0) {
6478                         vap->va_mask |= AT_MODE;
6479                         vap->va_mode = (mode_t)0600;
6480                 }
6481 
6482                 if (vap->va_mask & AT_SIZE) {
6483 
6484                         /* Disallow create with a non-zero size */
6485 
6486                         if ((reqsize = sarg.vap->va_size) != 0) {
6487                                 kmem_free(nm, buflen);
6488                                 nfs4_ntov_table_free(&ntov, &sarg);
6489                                 *attrset = 0;
6490                                 return (NFS4ERR_INVAL);
6491                         }
6492                         setsize = TRUE;
6493                 }
6494                 break;
6495 
6496         case EXCLUSIVE4:
6497                 /* prohibit EXCL create of named attributes */
6498                 if (dvp->v_flag & V_XATTRDIR) {
6499                         kmem_free(nm, buflen);
6500                         *attrset = 0;
6501                         return (NFS4ERR_INVAL);
6502                 }
6503 
6504                 cva.va_mask = AT_TYPE | AT_MTIME | AT_MODE;
6505                 cva.va_type = VREG;
6506                 /*
6507                  * Ensure no time overflows. Assumes underlying
6508                  * filesystem supports at least 32 bits.
6509                  * Truncate nsec to usec resolution to allow valid
6510                  * compares even if the underlying filesystem truncates.
6511                  */
6512                 mtime = (timespec32_t *)&args->createhow4_u.createverf;
6513                 cva.va_mtime.tv_sec = mtime->tv_sec % TIME32_MAX;
6514                 cva.va_mtime.tv_nsec = (mtime->tv_nsec / 1000) * 1000;
6515                 cva.va_mode = (mode_t)0;
6516                 vap = &cva;
6517 
6518                 /*
6519                  * For EXCL create, attrset is set to the server attr
6520                  * used to cache the client's verifier.
6521                  */
6522                 *attrset = FATTR4_TIME_MODIFY_MASK;
6523                 break;
6524         }
6525 
6526         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
6527         name = nfscmd_convname(ca, cs->exi, nm, NFSCMD_CONV_INBOUND,
6528             MAXPATHLEN  + 1);
6529 
6530         if (name == NULL) {
6531                 kmem_free(nm, buflen);
6532                 return (NFS4ERR_SERVERFAULT);
6533         }
6534 
6535         status = create_vnode(dvp, name, vap, args->mode,
6536             cs->cr, &vp, &created);
6537         if (nm != name)
6538                 kmem_free(name, MAXPATHLEN + 1);
6539         kmem_free(nm, buflen);
6540 
6541         if (status != NFS4_OK) {
6542                 if (ntov_table_init)
6543                         nfs4_ntov_table_free(&ntov, &sarg);
6544                 *attrset = 0;
6545                 return (status);
6546         }
6547 
6548         trunc = (setsize && !created);
6549 
6550         if (args->mode != EXCLUSIVE4) {
6551                 bitmap4 createmask = args->createhow4_u.createattrs.attrmask;
6552 
6553                 /*
6554                  * True verification that object was created with correct
6555                  * attrs is impossible.  The attrs could have been changed
6556                  * immediately after object creation.  If attributes did
6557                  * not verify, the only recourse for the server is to
6558                  * destroy the object.  Maybe if some attrs (like gid)
6559                  * are set incorrectly, the object should be destroyed;
6560                  * however, seems bad as a default policy.  Do we really
6561                  * want to destroy an object over one of the times not
6562                  * verifying correctly?  For these reasons, the server
6563                  * currently sets bits in attrset for createattrs
6564                  * that were set; however, no verification is done.
6565                  *
6566                  * vmask_to_nmask accounts for vattr bits set on create
6567                  *      [do_rfs4_set_attrs() only sets resp bits for
6568                  *       non-vattr/vfs bits.]
6569                  * Mask off any bits we set by default so as not to return
6570                  * more attrset bits than were requested in createattrs
6571                  */
6572                 if (created) {
6573                         nfs4_vmask_to_nmask(sarg.vap->va_mask, attrset);
6574                         *attrset &= createmask;
6575                 } else {
6576                         /*
6577                          * We did not create the vnode (we tried but it
6578                          * already existed).  In this case, the only createattr
6579                          * that the spec allows the server to set is size,
6580                          * and even then, it can only be set if it is 0.
6581                          */
6582                         *attrset = 0;
6583                         if (trunc)
6584                                 *attrset = FATTR4_SIZE_MASK;
6585                 }
6586         }
6587         if (ntov_table_init)
6588                 nfs4_ntov_table_free(&ntov, &sarg);
6589 
6590         /*
6591          * Get the initial "after" sequence number, if it fails,
6592          * set to zero, time to before.
6593          */
6594         iva.va_mask = AT_CTIME|AT_SEQ;
6595         if (VOP_GETATTR(dvp, &iva, 0, cs->cr, NULL)) {
6596                 iva.va_seq = 0;
6597                 iva.va_ctime = bva.va_ctime;
6598         }
6599 
6600         /*
6601          * create_vnode attempts to create the file exclusive,
6602          * if it already exists the VOP_CREATE will fail and
6603          * may not increase va_seq. It is atomic if
6604          * we haven't changed the directory, but if it has changed
6605          * we don't know what changed it.
6606          */
6607         if (!created) {
6608                 if (bva.va_seq && iva.va_seq &&
6609                     bva.va_seq == iva.va_seq)
6610                         cinfo->atomic = TRUE;
6611                 else
6612                         cinfo->atomic = FALSE;
6613                 NFS4_SET_FATTR4_CHANGE(cinfo->after, iva.va_ctime);
6614         } else {
6615                 /*
6616                  * The entry was created, we need to sync the
6617                  * directory metadata.
6618                  */
6619                 (void) VOP_FSYNC(dvp, 0, cs->cr, NULL);
6620 
6621                 /*
6622                  * Get "after" change value, if it fails, simply return the
6623                  * before value.
6624                  */
6625                 ava.va_mask = AT_CTIME|AT_SEQ;
6626                 if (VOP_GETATTR(dvp, &ava, 0, cs->cr, NULL)) {
6627                         ava.va_ctime = bva.va_ctime;
6628                         ava.va_seq = 0;
6629                 }
6630 
6631                 NFS4_SET_FATTR4_CHANGE(cinfo->after, ava.va_ctime);
6632 
6633                 /*
6634                  * The cinfo->atomic = TRUE only if we have
6635                  * non-zero va_seq's, and it has incremented by exactly one
6636                  * during the create_vnode and it didn't
6637                  * change during the VOP_FSYNC.
6638                  */
6639                 if (bva.va_seq && iva.va_seq && ava.va_seq &&
6640                     iva.va_seq == (bva.va_seq + 1) && iva.va_seq == ava.va_seq)
6641                         cinfo->atomic = TRUE;
6642                 else
6643                         cinfo->atomic = FALSE;
6644         }
6645 
6646         /* Check for mandatory locking and that the size gets set. */
6647         cva.va_mask = AT_MODE;
6648         if (setsize)
6649                 cva.va_mask |= AT_SIZE;
6650 
6651         /* Assume the worst */
6652         cs->mandlock = TRUE;
6653 
6654         if (VOP_GETATTR(vp, &cva, 0, cs->cr, NULL) == 0) {
6655                 cs->mandlock = MANDLOCK(cs->vp, cva.va_mode);
6656 
6657                 /*
6658                  * Truncate the file if necessary; this would be
6659                  * the case for create over an existing file.
6660                  */
6661 
6662                 if (trunc) {
6663                         int in_crit = 0;
6664                         rfs4_file_t *fp;
6665                         nfs4_srv_t *nsrv4;
6666                         bool_t create = FALSE;
6667 
6668                         /*
6669                          * We are writing over an existing file.
6670                          * Check to see if we need to recall a delegation.
6671                          */
6672                         nsrv4 = nfs4_get_srv();
6673                         rfs4_hold_deleg_policy(nsrv4);
6674                         if ((fp = rfs4_findfile(vp, NULL, &create)) != NULL) {
6675                                 if (rfs4_check_delegated_byfp(FWRITE, fp,
6676                                     (reqsize == 0), FALSE, FALSE, &clientid)) {
6677                                         rfs4_file_rele(fp);
6678                                         rfs4_rele_deleg_policy(nsrv4);
6679                                         VN_RELE(vp);
6680                                         *attrset = 0;
6681                                         return (NFS4ERR_DELAY);
6682                                 }
6683                                 rfs4_file_rele(fp);
6684                         }
6685                         rfs4_rele_deleg_policy(nsrv4);
6686 
6687                         if (nbl_need_check(vp)) {
6688                                 in_crit = 1;
6689 
6690                                 ASSERT(reqsize == 0);
6691 
6692                                 nbl_start_crit(vp, RW_READER);
6693                                 if (nbl_conflict(vp, NBL_WRITE, 0,
6694                                     cva.va_size, 0, NULL)) {
6695                                         in_crit = 0;
6696                                         nbl_end_crit(vp);
6697                                         VN_RELE(vp);
6698                                         *attrset = 0;
6699                                         return (NFS4ERR_ACCESS);
6700                                 }
6701                         }
6702                         ct.cc_sysid = 0;
6703                         ct.cc_pid = 0;
6704                         ct.cc_caller_id = nfs4_srv_caller_id;
6705                         ct.cc_flags = CC_DONTBLOCK;
6706 
6707                         cva.va_mask = AT_SIZE;
6708                         cva.va_size = reqsize;
6709                         (void) VOP_SETATTR(vp, &cva, 0, cs->cr, &ct);
6710                         if (in_crit)
6711                                 nbl_end_crit(vp);
6712                 }
6713         }
6714 
6715         error = makefh4(&cs->fh, vp, cs->exi);
6716 
6717         /*
6718          * Force modified data and metadata out to stable storage.
6719          */
6720         (void) VOP_FSYNC(vp, FNODSYNC, cs->cr, NULL);
6721 
6722         if (error) {
6723                 VN_RELE(vp);
6724                 *attrset = 0;
6725                 return (puterrno4(error));
6726         }
6727 
6728         /* if parent dir is attrdir, set namedattr fh flag */
6729         if (dvp->v_flag & V_XATTRDIR)
6730                 set_fh4_flag(&cs->fh, FH4_NAMEDATTR);
6731 
6732         if (cs->vp)
6733                 VN_RELE(cs->vp);
6734 
6735         cs->vp = vp;
6736 
6737         /*
6738          * if we did not create the file, we will need to check
6739          * the access bits on the file
6740          */
6741 
6742         if (!created) {
6743                 if (setsize)
6744                         args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
6745                 status = check_open_access(args->share_access, cs, req);
6746                 if (status != NFS4_OK)
6747                         *attrset = 0;
6748         }
6749         return (status);
6750 }
6751 
6752 /*ARGSUSED*/
6753 static void
6754 rfs4_do_open(struct compound_state *cs, struct svc_req *req,
6755     rfs4_openowner_t *oo, delegreq_t deleg,
6756     uint32_t access, uint32_t deny,
6757     OPEN4res *resp, int deleg_cur)
6758 {
6759         /* XXX Currently not using req  */
6760         rfs4_state_t *sp;
6761         rfs4_file_t *fp;
6762         bool_t screate = TRUE;
6763         bool_t fcreate = TRUE;
6764         uint32_t open_a, share_a;
6765         uint32_t open_d, share_d;
6766         rfs4_deleg_state_t *dsp;
6767         sysid_t sysid;
6768         nfsstat4 status;
6769         caller_context_t ct;
6770         int fflags = 0;
6771         int recall = 0;
6772         int err;
6773         int first_open;
6774 
6775         /* get the file struct and hold a lock on it during initial open */
6776         fp = rfs4_findfile_withlock(cs->vp, &cs->fh, &fcreate);
6777         if (fp == NULL) {
6778                 resp->status = NFS4ERR_RESOURCE;
6779                 DTRACE_PROBE1(nfss__e__do__open1, nfsstat4, resp->status);
6780                 return;
6781         }
6782 
6783         sp = rfs4_findstate_by_owner_file(oo, fp, &screate);
6784         if (sp == NULL) {
6785                 resp->status = NFS4ERR_RESOURCE;
6786                 DTRACE_PROBE1(nfss__e__do__open2, nfsstat4, resp->status);
6787                 /* No need to keep any reference */
6788                 rw_exit(&fp->rf_file_rwlock);
6789                 rfs4_file_rele(fp);
6790                 return;
6791         }
6792 
6793         /* try to get the sysid before continuing */
6794         if ((status = rfs4_client_sysid(oo->ro_client, &sysid)) != NFS4_OK) {
6795                 resp->status = status;
6796                 rfs4_file_rele(fp);
6797                 /* Not a fully formed open; "close" it */
6798                 if (screate == TRUE)
6799                         rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6800                 rfs4_state_rele(sp);
6801                 return;
6802         }
6803 
6804         /* Calculate the fflags for this OPEN. */
6805         if (access & OPEN4_SHARE_ACCESS_READ)
6806                 fflags |= FREAD;
6807         if (access & OPEN4_SHARE_ACCESS_WRITE)
6808                 fflags |= FWRITE;
6809 
6810         rfs4_dbe_lock(sp->rs_dbe);
6811 
6812         /*
6813          * Calculate the new deny and access mode that this open is adding to
6814          * the file for this open owner;
6815          */
6816         open_d = (deny & ~sp->rs_open_deny);
6817         open_a = (access & ~sp->rs_open_access);
6818 
6819         /*
6820          * Calculate the new share access and share deny modes that this open
6821          * is adding to the file for this open owner;
6822          */
6823         share_a = (access & ~sp->rs_share_access);
6824         share_d = (deny & ~sp->rs_share_deny);
6825 
6826         first_open = (sp->rs_open_access & OPEN4_SHARE_ACCESS_BOTH) == 0;
6827 
6828         /*
6829          * Check to see the client has already sent an open for this
6830          * open owner on this file with the same share/deny modes.
6831          * If so, we don't need to check for a conflict and we don't
6832          * need to add another shrlock.  If not, then we need to
6833          * check for conflicts in deny and access before checking for
6834          * conflicts in delegation.  We don't want to recall a
6835          * delegation based on an open that will eventually fail based
6836          * on shares modes.
6837          */
6838 
6839         if (share_a || share_d) {
6840                 if ((err = rfs4_share(sp, access, deny)) != 0) {
6841                         rfs4_dbe_unlock(sp->rs_dbe);
6842                         resp->status = err;
6843 
6844                         rfs4_file_rele(fp);
6845                         /* Not a fully formed open; "close" it */
6846                         if (screate == TRUE)
6847                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6848                         rfs4_state_rele(sp);
6849                         return;
6850                 }
6851         }
6852 
6853         rfs4_dbe_lock(fp->rf_dbe);
6854 
6855         /*
6856          * Check to see if this file is delegated and if so, if a
6857          * recall needs to be done.
6858          */
6859         if (rfs4_check_recall(sp, access)) {
6860                 rfs4_dbe_unlock(fp->rf_dbe);
6861                 rfs4_dbe_unlock(sp->rs_dbe);
6862                 rfs4_recall_deleg(fp, FALSE, sp->rs_owner->ro_client);
6863                 delay(NFS4_DELEGATION_CONFLICT_DELAY);
6864                 rfs4_dbe_lock(sp->rs_dbe);
6865 
6866                 /* if state closed while lock was dropped */
6867                 if (sp->rs_closed) {
6868                         if (share_a || share_d)
6869                                 (void) rfs4_unshare(sp);
6870                         rfs4_dbe_unlock(sp->rs_dbe);
6871                         rfs4_file_rele(fp);
6872                         /* Not a fully formed open; "close" it */
6873                         if (screate == TRUE)
6874                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6875                         rfs4_state_rele(sp);
6876                         resp->status = NFS4ERR_OLD_STATEID;
6877                         return;
6878                 }
6879 
6880                 rfs4_dbe_lock(fp->rf_dbe);
6881                 /* Let's see if the delegation was returned */
6882                 if (rfs4_check_recall(sp, access)) {
6883                         rfs4_dbe_unlock(fp->rf_dbe);
6884                         if (share_a || share_d)
6885                                 (void) rfs4_unshare(sp);
6886                         rfs4_dbe_unlock(sp->rs_dbe);
6887                         rfs4_file_rele(fp);
6888                         rfs4_update_lease(sp->rs_owner->ro_client);
6889 
6890                         /* Not a fully formed open; "close" it */
6891                         if (screate == TRUE)
6892                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6893                         rfs4_state_rele(sp);
6894                         resp->status = NFS4ERR_DELAY;
6895                         return;
6896                 }
6897         }
6898         /*
6899          * the share check passed and any delegation conflict has been
6900          * taken care of, now call vop_open.
6901          * if this is the first open then call vop_open with fflags.
6902          * if not, call vn_open_upgrade with just the upgrade flags.
6903          *
6904          * if the file has been opened already, it will have the current
6905          * access mode in the state struct.  if it has no share access, then
6906          * this is a new open.
6907          *
6908          * However, if this is open with CLAIM_DLEGATE_CUR, then don't
6909          * call VOP_OPEN(), just do the open upgrade.
6910          */
6911         if (first_open && !deleg_cur) {
6912                 ct.cc_sysid = sysid;
6913                 ct.cc_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
6914                 ct.cc_caller_id = nfs4_srv_caller_id;
6915                 ct.cc_flags = CC_DONTBLOCK;
6916                 err = VOP_OPEN(&cs->vp, fflags, cs->cr, &ct);
6917                 if (err) {
6918                         rfs4_dbe_unlock(fp->rf_dbe);
6919                         if (share_a || share_d)
6920                                 (void) rfs4_unshare(sp);
6921                         rfs4_dbe_unlock(sp->rs_dbe);
6922                         rfs4_file_rele(fp);
6923 
6924                         /* Not a fully formed open; "close" it */
6925                         if (screate == TRUE)
6926                                 rfs4_state_close(sp, FALSE, FALSE, cs->cr);
6927                         rfs4_state_rele(sp);
6928                         /* check if a monitor detected a delegation conflict */
6929                         if (err == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK))
6930                                 resp->status = NFS4ERR_DELAY;
6931                         else
6932                                 resp->status = NFS4ERR_SERVERFAULT;
6933                         return;
6934                 }
6935         } else { /* open upgrade */
6936                 /*
6937                  * calculate the fflags for the new mode that is being added
6938                  * by this upgrade.
6939                  */
6940                 fflags = 0;
6941                 if (open_a & OPEN4_SHARE_ACCESS_READ)
6942                         fflags |= FREAD;
6943                 if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6944                         fflags |= FWRITE;
6945                 vn_open_upgrade(cs->vp, fflags);
6946         }
6947         sp->rs_open_access |= access;
6948         sp->rs_open_deny |= deny;
6949 
6950         if (open_d & OPEN4_SHARE_DENY_READ)
6951                 fp->rf_deny_read++;
6952         if (open_d & OPEN4_SHARE_DENY_WRITE)
6953                 fp->rf_deny_write++;
6954         fp->rf_share_deny |= deny;
6955 
6956         if (open_a & OPEN4_SHARE_ACCESS_READ)
6957                 fp->rf_access_read++;
6958         if (open_a & OPEN4_SHARE_ACCESS_WRITE)
6959                 fp->rf_access_write++;
6960         fp->rf_share_access |= access;
6961 
6962         /*
6963          * Check for delegation here. if the deleg argument is not
6964          * DELEG_ANY, then this is a reclaim from a client and
6965          * we must honor the delegation requested. If necessary we can
6966          * set the recall flag.
6967          */
6968 
6969         dsp = rfs4_grant_delegation(deleg, sp, &recall);
6970 
6971         cs->deleg = (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE);
6972 
6973         next_stateid(&sp->rs_stateid);
6974 
6975         resp->stateid = sp->rs_stateid.stateid;
6976 
6977         rfs4_dbe_unlock(fp->rf_dbe);
6978         rfs4_dbe_unlock(sp->rs_dbe);
6979 
6980         if (dsp) {
6981                 rfs4_set_deleg_response(dsp, &resp->delegation, NULL, recall);
6982                 rfs4_deleg_state_rele(dsp);
6983         }
6984 
6985         rfs4_file_rele(fp);
6986         rfs4_state_rele(sp);
6987 
6988         resp->status = NFS4_OK;
6989 }
6990 
6991 /*ARGSUSED*/
6992 static void
6993 rfs4_do_opennull(struct compound_state *cs, struct svc_req *req,
6994     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
6995 {
6996         change_info4 *cinfo = &resp->cinfo;
6997         bitmap4 *attrset = &resp->attrset;
6998 
6999         if (args->opentype == OPEN4_NOCREATE)
7000                 resp->status = rfs4_lookupfile(&args->open_claim4_u.file,
7001                     req, cs, args->share_access, cinfo);
7002         else {
7003                 /* inhibit delegation grants during exclusive create */
7004 
7005                 if (args->mode == EXCLUSIVE4)
7006                         rfs4_disable_delegation();
7007 
7008                 resp->status = rfs4_createfile(args, req, cs, cinfo, attrset,
7009                     oo->ro_client->rc_clientid);
7010         }
7011 
7012         if (resp->status == NFS4_OK) {
7013 
7014                 /* cs->vp cs->fh now reference the desired file */
7015 
7016                 rfs4_do_open(cs, req, oo,
7017                     oo->ro_need_confirm ? DELEG_NONE : DELEG_ANY,
7018                     args->share_access, args->share_deny, resp, 0);
7019 
7020                 /*
7021                  * If rfs4_createfile set attrset, we must
7022                  * clear this attrset before the response is copied.
7023                  */
7024                 if (resp->status != NFS4_OK && resp->attrset) {
7025                         resp->attrset = 0;
7026                 }
7027         }
7028         else
7029                 *cs->statusp = resp->status;
7030 
7031         if (args->mode == EXCLUSIVE4)
7032                 rfs4_enable_delegation();
7033 }
7034 
7035 /*ARGSUSED*/
7036 static void
7037 rfs4_do_openprev(struct compound_state *cs, struct svc_req *req,
7038     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7039 {
7040         change_info4 *cinfo = &resp->cinfo;
7041         vattr_t va;
7042         vtype_t v_type = cs->vp->v_type;
7043         int error = 0;
7044 
7045         /* Verify that we have a regular file */
7046         if (v_type != VREG) {
7047                 if (v_type == VDIR)
7048                         resp->status = NFS4ERR_ISDIR;
7049                 else if (v_type == VLNK)
7050                         resp->status = NFS4ERR_SYMLINK;
7051                 else
7052                         resp->status = NFS4ERR_INVAL;
7053                 return;
7054         }
7055 
7056         va.va_mask = AT_MODE|AT_UID;
7057         error = VOP_GETATTR(cs->vp, &va, 0, cs->cr, NULL);
7058         if (error) {
7059                 resp->status = puterrno4(error);
7060                 return;
7061         }
7062 
7063         cs->mandlock = MANDLOCK(cs->vp, va.va_mode);
7064 
7065         /*
7066          * Check if we have access to the file, Note the the file
7067          * could have originally been open UNCHECKED or GUARDED
7068          * with mode bits that will now fail, but there is nothing
7069          * we can really do about that except in the case that the
7070          * owner of the file is the one requesting the open.
7071          */
7072         if (crgetuid(cs->cr) != va.va_uid) {
7073                 resp->status = check_open_access(args->share_access, cs, req);
7074                 if (resp->status != NFS4_OK) {
7075                         return;
7076                 }
7077         }
7078 
7079         /*
7080          * cinfo on a CLAIM_PREVIOUS is undefined, initialize to zero
7081          */
7082         cinfo->before = 0;
7083         cinfo->after = 0;
7084         cinfo->atomic = FALSE;
7085 
7086         rfs4_do_open(cs, req, oo,
7087             NFS4_DELEG4TYPE2REQTYPE(args->open_claim4_u.delegate_type),
7088             args->share_access, args->share_deny, resp, 0);
7089 }
7090 
7091 static void
7092 rfs4_do_opendelcur(struct compound_state *cs, struct svc_req *req,
7093     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7094 {
7095         int error;
7096         nfsstat4 status;
7097         stateid4 stateid =
7098             args->open_claim4_u.delegate_cur_info.delegate_stateid;
7099         rfs4_deleg_state_t *dsp;
7100 
7101         /*
7102          * Find the state info from the stateid and confirm that the
7103          * file is delegated.  If the state openowner is the same as
7104          * the supplied openowner we're done. If not, get the file
7105          * info from the found state info. Use that file info to
7106          * create the state for this lock owner. Note solaris doen't
7107          * really need the pathname to find the file. We may want to
7108          * lookup the pathname and make sure that the vp exist and
7109          * matches the vp in the file structure. However it is
7110          * possible that the pathname nolonger exists (local process
7111          * unlinks the file), so this may not be that useful.
7112          */
7113 
7114         status = rfs4_get_deleg_state(&stateid, &dsp);
7115         if (status != NFS4_OK) {
7116                 resp->status = status;
7117                 return;
7118         }
7119 
7120         ASSERT(dsp->rds_finfo->rf_dinfo.rd_dtype != OPEN_DELEGATE_NONE);
7121 
7122         /*
7123          * New lock owner, create state. Since this was probably called
7124          * in response to a CB_RECALL we set deleg to DELEG_NONE
7125          */
7126 
7127         ASSERT(cs->vp != NULL);
7128         VN_RELE(cs->vp);
7129         VN_HOLD(dsp->rds_finfo->rf_vp);
7130         cs->vp = dsp->rds_finfo->rf_vp;
7131 
7132         if (error = makefh4(&cs->fh, cs->vp, cs->exi)) {
7133                 rfs4_deleg_state_rele(dsp);
7134                 *cs->statusp = resp->status = puterrno4(error);
7135                 return;
7136         }
7137 
7138         /* Mark progress for delegation returns */
7139         dsp->rds_finfo->rf_dinfo.rd_time_lastwrite = gethrestime_sec();
7140         rfs4_deleg_state_rele(dsp);
7141         rfs4_do_open(cs, req, oo, DELEG_NONE,
7142             args->share_access, args->share_deny, resp, 1);
7143 }
7144 
7145 /*ARGSUSED*/
7146 static void
7147 rfs4_do_opendelprev(struct compound_state *cs, struct svc_req *req,
7148     OPEN4args *args, rfs4_openowner_t *oo, OPEN4res *resp)
7149 {
7150         /*
7151          * Lookup the pathname, it must already exist since this file
7152          * was delegated.
7153          *
7154          * Find the file and state info for this vp and open owner pair.
7155          *      check that they are in fact delegated.
7156          *      check that the state access and deny modes are the same.
7157          *
7158          * Return the delgation possibly seting the recall flag.
7159          */
7160         rfs4_file_t *fp;
7161         rfs4_state_t *sp;
7162         bool_t create = FALSE;
7163         bool_t dcreate = FALSE;
7164         rfs4_deleg_state_t *dsp;
7165         nfsace4 *ace;
7166 
7167         /* Note we ignore oflags */
7168         resp->status = rfs4_lookupfile(&args->open_claim4_u.file_delegate_prev,
7169             req, cs, args->share_access, &resp->cinfo);
7170 
7171         if (resp->status != NFS4_OK) {
7172                 return;
7173         }
7174 
7175         /* get the file struct and hold a lock on it during initial open */
7176         fp = rfs4_findfile_withlock(cs->vp, NULL, &create);
7177         if (fp == NULL) {
7178                 resp->status = NFS4ERR_RESOURCE;
7179                 DTRACE_PROBE1(nfss__e__do_opendelprev1, nfsstat4, resp->status);
7180                 return;
7181         }
7182 
7183         sp = rfs4_findstate_by_owner_file(oo, fp, &create);
7184         if (sp == NULL) {
7185                 resp->status = NFS4ERR_SERVERFAULT;
7186                 DTRACE_PROBE1(nfss__e__do_opendelprev2, nfsstat4, resp->status);
7187                 rw_exit(&fp->rf_file_rwlock);
7188                 rfs4_file_rele(fp);
7189                 return;
7190         }
7191 
7192         rfs4_dbe_lock(sp->rs_dbe);
7193         rfs4_dbe_lock(fp->rf_dbe);
7194         if (args->share_access != sp->rs_share_access ||
7195             args->share_deny != sp->rs_share_deny ||
7196             sp->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
7197                 NFS4_DEBUG(rfs4_debug,
7198                     (CE_NOTE, "rfs4_do_opendelprev: state mixup"));
7199                 rfs4_dbe_unlock(fp->rf_dbe);
7200                 rfs4_dbe_unlock(sp->rs_dbe);
7201                 rfs4_file_rele(fp);
7202                 rfs4_state_rele(sp);
7203                 resp->status = NFS4ERR_SERVERFAULT;
7204                 return;
7205         }
7206         rfs4_dbe_unlock(fp->rf_dbe);
7207         rfs4_dbe_unlock(sp->rs_dbe);
7208 
7209         dsp = rfs4_finddeleg(sp, &dcreate);
7210         if (dsp == NULL) {
7211                 rfs4_state_rele(sp);
7212                 rfs4_file_rele(fp);
7213                 resp->status = NFS4ERR_SERVERFAULT;
7214                 return;
7215         }
7216 
7217         next_stateid(&sp->rs_stateid);
7218 
7219         resp->stateid = sp->rs_stateid.stateid;
7220 
7221         resp->delegation.delegation_type = dsp->rds_dtype;
7222 
7223         if (dsp->rds_dtype == OPEN_DELEGATE_READ) {
7224                 open_read_delegation4 *rv =
7225                     &resp->delegation.open_delegation4_u.read;
7226 
7227                 rv->stateid = dsp->rds_delegid.stateid;
7228                 rv->recall = FALSE; /* no policy in place to set to TRUE */
7229                 ace = &rv->permissions;
7230         } else {
7231                 open_write_delegation4 *rv =
7232                     &resp->delegation.open_delegation4_u.write;
7233 
7234                 rv->stateid = dsp->rds_delegid.stateid;
7235                 rv->recall = FALSE;  /* no policy in place to set to TRUE */
7236                 ace = &rv->permissions;
7237                 rv->space_limit.limitby = NFS_LIMIT_SIZE;
7238                 rv->space_limit.nfs_space_limit4_u.filesize = UINT64_MAX;
7239         }
7240 
7241         /* XXX For now */
7242         ace->type = ACE4_ACCESS_ALLOWED_ACE_TYPE;
7243         ace->flag = 0;
7244         ace->access_mask = 0;
7245         ace->who.utf8string_len = 0;
7246         ace->who.utf8string_val = 0;
7247 
7248         rfs4_deleg_state_rele(dsp);
7249         rfs4_state_rele(sp);
7250         rfs4_file_rele(fp);
7251 }
7252 
7253 typedef enum {
7254         NFS4_CHKSEQ_OKAY = 0,
7255         NFS4_CHKSEQ_REPLAY = 1,
7256         NFS4_CHKSEQ_BAD = 2
7257 } rfs4_chkseq_t;
7258 
7259 /*
7260  * Generic function for sequence number checks.
7261  */
7262 static rfs4_chkseq_t
7263 rfs4_check_seqid(seqid4 seqid, nfs_resop4 *lastop,
7264     seqid4 rqst_seq, nfs_resop4 *resop, bool_t copyres)
7265 {
7266         /* Same sequence ids and matching operations? */
7267         if (seqid == rqst_seq && resop->resop == lastop->resop) {
7268                 if (copyres == TRUE) {
7269                         rfs4_free_reply(resop);
7270                         rfs4_copy_reply(resop, lastop);
7271                 }
7272                 NFS4_DEBUG(rfs4_debug, (CE_NOTE,
7273                     "Replayed SEQID %d\n", seqid));
7274                 return (NFS4_CHKSEQ_REPLAY);
7275         }
7276 
7277         /* If the incoming sequence is not the next expected then it is bad */
7278         if (rqst_seq != seqid + 1) {
7279                 if (rqst_seq == seqid) {
7280                         NFS4_DEBUG(rfs4_debug,
7281                             (CE_NOTE, "BAD SEQID: Replayed sequence id "
7282                             "but last op was %d current op is %d\n",
7283                             lastop->resop, resop->resop));
7284                         return (NFS4_CHKSEQ_BAD);
7285                 }
7286                 NFS4_DEBUG(rfs4_debug,
7287                     (CE_NOTE, "BAD SEQID: got %u expecting %u\n",
7288                     rqst_seq, seqid));
7289                 return (NFS4_CHKSEQ_BAD);
7290         }
7291 
7292         /* Everything okay -- next expected */
7293         return (NFS4_CHKSEQ_OKAY);
7294 }
7295 
7296 
7297 static rfs4_chkseq_t
7298 rfs4_check_open_seqid(seqid4 seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7299 {
7300         rfs4_chkseq_t rc;
7301 
7302         rfs4_dbe_lock(op->ro_dbe);
7303         rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply, seqid, resop,
7304             TRUE);
7305         rfs4_dbe_unlock(op->ro_dbe);
7306 
7307         if (rc == NFS4_CHKSEQ_OKAY)
7308                 rfs4_update_lease(op->ro_client);
7309 
7310         return (rc);
7311 }
7312 
7313 static rfs4_chkseq_t
7314 rfs4_check_olo_seqid(seqid4 olo_seqid, rfs4_openowner_t *op, nfs_resop4 *resop)
7315 {
7316         rfs4_chkseq_t rc;
7317 
7318         rfs4_dbe_lock(op->ro_dbe);
7319         rc = rfs4_check_seqid(op->ro_open_seqid, &op->ro_reply,
7320             olo_seqid, resop, FALSE);
7321         rfs4_dbe_unlock(op->ro_dbe);
7322 
7323         return (rc);
7324 }
7325 
7326 static rfs4_chkseq_t
7327 rfs4_check_lock_seqid(seqid4 seqid, rfs4_lo_state_t *lsp, nfs_resop4 *resop)
7328 {
7329         rfs4_chkseq_t rc = NFS4_CHKSEQ_OKAY;
7330 
7331         rfs4_dbe_lock(lsp->rls_dbe);
7332         if (!lsp->rls_skip_seqid_check)
7333                 rc = rfs4_check_seqid(lsp->rls_seqid, &lsp->rls_reply, seqid,
7334                     resop, TRUE);
7335         rfs4_dbe_unlock(lsp->rls_dbe);
7336 
7337         return (rc);
7338 }
7339 
7340 static void
7341 rfs4_op_open(nfs_argop4 *argop, nfs_resop4 *resop,
7342     struct svc_req *req, struct compound_state *cs)
7343 {
7344         OPEN4args *args = &argop->nfs_argop4_u.opopen;
7345         OPEN4res *resp = &resop->nfs_resop4_u.opopen;
7346         open_owner4 *owner = &args->owner;
7347         open_claim_type4 claim = args->claim;
7348         rfs4_client_t *cp;
7349         rfs4_openowner_t *oo;
7350         bool_t create;
7351         bool_t replay = FALSE;
7352         int can_reclaim;
7353 
7354         DTRACE_NFSV4_2(op__open__start, struct compound_state *, cs,
7355             OPEN4args *, args);
7356 
7357         if (cs->vp == NULL) {
7358                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7359                 goto end;
7360         }
7361 
7362         /*
7363          * Need to check clientid and lease expiration first based on
7364          * error ordering and incrementing sequence id.
7365          */
7366         cp = rfs4_findclient_by_id(owner->clientid, FALSE);
7367         if (cp == NULL) {
7368                 *cs->statusp = resp->status =
7369                     rfs4_check_clientid(&owner->clientid, 0);
7370                 goto end;
7371         }
7372 
7373         if (rfs4_lease_expired(cp)) {
7374                 rfs4_client_close(cp);
7375                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
7376                 goto end;
7377         }
7378         can_reclaim = cp->rc_can_reclaim;
7379 
7380         /*
7381          * Find the open_owner for use from this point forward.  Take
7382          * care in updating the sequence id based on the type of error
7383          * being returned.
7384          */
7385 retry:
7386         create = TRUE;
7387         oo = rfs4_findopenowner(owner, &create, args->seqid);
7388         if (oo == NULL) {
7389                 *cs->statusp = resp->status = NFS4ERR_RESOURCE;
7390                 rfs4_client_rele(cp);
7391                 goto end;
7392         }
7393 
7394         /* Hold off access to the sequence space while the open is done */
7395         rfs4_sw_enter(&oo->ro_sw);
7396 
7397         /*
7398          * If the open_owner existed before at the server, then check
7399          * the sequence id.
7400          */
7401         if (!create && !oo->ro_postpone_confirm) {
7402                 switch (rfs4_check_open_seqid(args->seqid, oo, resop)) {
7403                 case NFS4_CHKSEQ_BAD:
7404                         if ((args->seqid > oo->ro_open_seqid) &&
7405                             oo->ro_need_confirm) {
7406                                 rfs4_free_opens(oo, TRUE, FALSE);
7407                                 rfs4_sw_exit(&oo->ro_sw);
7408                                 rfs4_openowner_rele(oo);
7409                                 goto retry;
7410                         }
7411                         resp->status = NFS4ERR_BAD_SEQID;
7412                         goto out;
7413                 case NFS4_CHKSEQ_REPLAY: /* replay of previous request */
7414                         replay = TRUE;
7415                         goto out;
7416                 default:
7417                         break;
7418                 }
7419 
7420                 /*
7421                  * Sequence was ok and open owner exists
7422                  * check to see if we have yet to see an
7423                  * open_confirm.
7424                  */
7425                 if (oo->ro_need_confirm) {
7426                         rfs4_free_opens(oo, TRUE, FALSE);
7427                         rfs4_sw_exit(&oo->ro_sw);
7428                         rfs4_openowner_rele(oo);
7429                         goto retry;
7430                 }
7431         }
7432         /* Grace only applies to regular-type OPENs */
7433         if (rfs4_clnt_in_grace(cp) &&
7434             (claim == CLAIM_NULL || claim == CLAIM_DELEGATE_CUR)) {
7435                 *cs->statusp = resp->status = NFS4ERR_GRACE;
7436                 goto out;
7437         }
7438 
7439         /*
7440          * If previous state at the server existed then can_reclaim
7441          * will be set. If not reply NFS4ERR_NO_GRACE to the
7442          * client.
7443          */
7444         if (rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS && !can_reclaim) {
7445                 *cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7446                 goto out;
7447         }
7448 
7449 
7450         /*
7451          * Reject the open if the client has missed the grace period
7452          */
7453         if (!rfs4_clnt_in_grace(cp) && claim == CLAIM_PREVIOUS) {
7454                 *cs->statusp = resp->status = NFS4ERR_NO_GRACE;
7455                 goto out;
7456         }
7457 
7458         /* Couple of up-front bookkeeping items */
7459         if (oo->ro_need_confirm) {
7460                 /*
7461                  * If this is a reclaim OPEN then we should not ask
7462                  * for a confirmation of the open_owner per the
7463                  * protocol specification.
7464                  */
7465                 if (claim == CLAIM_PREVIOUS)
7466                         oo->ro_need_confirm = FALSE;
7467                 else
7468                         resp->rflags |= OPEN4_RESULT_CONFIRM;
7469         }
7470         resp->rflags |= OPEN4_RESULT_LOCKTYPE_POSIX;
7471 
7472         /*
7473          * If there is an unshared filesystem mounted on this vnode,
7474          * do not allow to open/create in this directory.
7475          */
7476         if (vn_ismntpt(cs->vp)) {
7477                 *cs->statusp = resp->status = NFS4ERR_ACCESS;
7478                 goto out;
7479         }
7480 
7481         /*
7482          * access must READ, WRITE, or BOTH.  No access is invalid.
7483          * deny can be READ, WRITE, BOTH, or NONE.
7484          * bits not defined for access/deny are invalid.
7485          */
7486         if (! (args->share_access & OPEN4_SHARE_ACCESS_BOTH) ||
7487             (args->share_access & ~OPEN4_SHARE_ACCESS_BOTH) ||
7488             (args->share_deny & ~OPEN4_SHARE_DENY_BOTH)) {
7489                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7490                 goto out;
7491         }
7492 
7493 
7494         /*
7495          * make sure attrset is zero before response is built.
7496          */
7497         resp->attrset = 0;
7498 
7499         switch (claim) {
7500         case CLAIM_NULL:
7501                 rfs4_do_opennull(cs, req, args, oo, resp);
7502                 break;
7503         case CLAIM_PREVIOUS:
7504                 rfs4_do_openprev(cs, req, args, oo, resp);
7505                 break;
7506         case CLAIM_DELEGATE_CUR:
7507                 rfs4_do_opendelcur(cs, req, args, oo, resp);
7508                 break;
7509         case CLAIM_DELEGATE_PREV:
7510                 rfs4_do_opendelprev(cs, req, args, oo, resp);
7511                 break;
7512         default:
7513                 resp->status = NFS4ERR_INVAL;
7514                 break;
7515         }
7516 
7517 out:
7518         rfs4_client_rele(cp);
7519 
7520         /* Catch sequence id handling here to make it a little easier */
7521         switch (resp->status) {
7522         case NFS4ERR_BADXDR:
7523         case NFS4ERR_BAD_SEQID:
7524         case NFS4ERR_BAD_STATEID:
7525         case NFS4ERR_NOFILEHANDLE:
7526         case NFS4ERR_RESOURCE:
7527         case NFS4ERR_STALE_CLIENTID:
7528         case NFS4ERR_STALE_STATEID:
7529                 /*
7530                  * The protocol states that if any of these errors are
7531                  * being returned, the sequence id should not be
7532                  * incremented.  Any other return requires an
7533                  * increment.
7534                  */
7535                 break;
7536         default:
7537                 /* Always update the lease in this case */
7538                 rfs4_update_lease(oo->ro_client);
7539 
7540                 /* Regular response - copy the result */
7541                 if (!replay)
7542                         rfs4_update_open_resp(oo, resop, &cs->fh);
7543 
7544                 /*
7545                  * REPLAY case: Only if the previous response was OK
7546                  * do we copy the filehandle.  If not OK, no
7547                  * filehandle to copy.
7548                  */
7549                 if (replay == TRUE &&
7550                     resp->status == NFS4_OK &&
7551                     oo->ro_reply_fh.nfs_fh4_val) {
7552                         /*
7553                          * If this is a replay, we must restore the
7554                          * current filehandle/vp to that of what was
7555                          * returned originally.  Try our best to do
7556                          * it.
7557                          */
7558                         nfs_fh4_fmt_t *fh_fmtp =
7559                             (nfs_fh4_fmt_t *)oo->ro_reply_fh.nfs_fh4_val;
7560 
7561                         cs->exi = checkexport4(&fh_fmtp->fh4_fsid,
7562                             (fid_t *)&fh_fmtp->fh4_xlen, NULL);
7563 
7564                         if (cs->exi == NULL) {
7565                                 resp->status = NFS4ERR_STALE;
7566                                 goto finish;
7567                         }
7568 
7569                         VN_RELE(cs->vp);
7570 
7571                         cs->vp = nfs4_fhtovp(&oo->ro_reply_fh, cs->exi,
7572                             &resp->status);
7573 
7574                         if (cs->vp == NULL)
7575                                 goto finish;
7576 
7577                         nfs_fh4_copy(&oo->ro_reply_fh, &cs->fh);
7578                 }
7579 
7580                 /*
7581                  * If this was a replay, no need to update the
7582                  * sequence id. If the open_owner was not created on
7583                  * this pass, then update.  The first use of an
7584                  * open_owner will not bump the sequence id.
7585                  */
7586                 if (replay == FALSE && !create)
7587                         rfs4_update_open_sequence(oo);
7588                 /*
7589                  * If the client is receiving an error and the
7590                  * open_owner needs to be confirmed, there is no way
7591                  * to notify the client of this fact ignoring the fact
7592                  * that the server has no method of returning a
7593                  * stateid to confirm.  Therefore, the server needs to
7594                  * mark this open_owner in a way as to avoid the
7595                  * sequence id checking the next time the client uses
7596                  * this open_owner.
7597                  */
7598                 if (resp->status != NFS4_OK && oo->ro_need_confirm)
7599                         oo->ro_postpone_confirm = TRUE;
7600                 /*
7601                  * If OK response then clear the postpone flag and
7602                  * reset the sequence id to keep in sync with the
7603                  * client.
7604                  */
7605                 if (resp->status == NFS4_OK && oo->ro_postpone_confirm) {
7606                         oo->ro_postpone_confirm = FALSE;
7607                         oo->ro_open_seqid = args->seqid;
7608                 }
7609                 break;
7610         }
7611 
7612 finish:
7613         *cs->statusp = resp->status;
7614 
7615         rfs4_sw_exit(&oo->ro_sw);
7616         rfs4_openowner_rele(oo);
7617 
7618 end:
7619         DTRACE_NFSV4_2(op__open__done, struct compound_state *, cs,
7620             OPEN4res *, resp);
7621 }
7622 
7623 /*ARGSUSED*/
7624 void
7625 rfs4_op_open_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
7626     struct svc_req *req, struct compound_state *cs)
7627 {
7628         OPEN_CONFIRM4args *args = &argop->nfs_argop4_u.opopen_confirm;
7629         OPEN_CONFIRM4res *resp = &resop->nfs_resop4_u.opopen_confirm;
7630         rfs4_state_t *sp;
7631         nfsstat4 status;
7632 
7633         DTRACE_NFSV4_2(op__open__confirm__start, struct compound_state *, cs,
7634             OPEN_CONFIRM4args *, args);
7635 
7636         if (cs->vp == NULL) {
7637                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7638                 goto out;
7639         }
7640 
7641         if (cs->vp->v_type != VREG) {
7642                 *cs->statusp = resp->status =
7643                     cs->vp->v_type == VDIR ? NFS4ERR_ISDIR : NFS4ERR_INVAL;
7644                 return;
7645         }
7646 
7647         status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7648         if (status != NFS4_OK) {
7649                 *cs->statusp = resp->status = status;
7650                 goto out;
7651         }
7652 
7653         /* Ensure specified filehandle matches */
7654         if (cs->vp != sp->rs_finfo->rf_vp) {
7655                 rfs4_state_rele(sp);
7656                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7657                 goto out;
7658         }
7659 
7660         /* hold off other access to open_owner while we tinker */
7661         rfs4_sw_enter(&sp->rs_owner->ro_sw);
7662 
7663         switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7664         case NFS4_CHECK_STATEID_OKAY:
7665                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7666                     resop) != 0) {
7667                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7668                         break;
7669                 }
7670                 /*
7671                  * If it is the appropriate stateid and determined to
7672                  * be "OKAY" then this means that the stateid does not
7673                  * need to be confirmed and the client is in error for
7674                  * sending an OPEN_CONFIRM.
7675                  */
7676                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7677                 break;
7678         case NFS4_CHECK_STATEID_OLD:
7679                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7680                 break;
7681         case NFS4_CHECK_STATEID_BAD:
7682                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7683                 break;
7684         case NFS4_CHECK_STATEID_EXPIRED:
7685                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
7686                 break;
7687         case NFS4_CHECK_STATEID_CLOSED:
7688                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7689                 break;
7690         case NFS4_CHECK_STATEID_REPLAY:
7691                 switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7692                     resop)) {
7693                 case NFS4_CHKSEQ_OKAY:
7694                         /*
7695                          * This is replayed stateid; if seqid matches
7696                          * next expected, then client is using wrong seqid.
7697                          */
7698                         /* fall through */
7699                 case NFS4_CHKSEQ_BAD:
7700                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7701                         break;
7702                 case NFS4_CHKSEQ_REPLAY:
7703                         /*
7704                          * Note this case is the duplicate case so
7705                          * resp->status is already set.
7706                          */
7707                         *cs->statusp = resp->status;
7708                         rfs4_update_lease(sp->rs_owner->ro_client);
7709                         break;
7710                 }
7711                 break;
7712         case NFS4_CHECK_STATEID_UNCONFIRMED:
7713                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7714                     resop) != NFS4_CHKSEQ_OKAY) {
7715                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7716                         break;
7717                 }
7718                 *cs->statusp = resp->status = NFS4_OK;
7719 
7720                 next_stateid(&sp->rs_stateid);
7721                 resp->open_stateid = sp->rs_stateid.stateid;
7722                 sp->rs_owner->ro_need_confirm = FALSE;
7723                 rfs4_update_lease(sp->rs_owner->ro_client);
7724                 rfs4_update_open_sequence(sp->rs_owner);
7725                 rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7726                 break;
7727         default:
7728                 ASSERT(FALSE);
7729                 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7730                 break;
7731         }
7732         rfs4_sw_exit(&sp->rs_owner->ro_sw);
7733         rfs4_state_rele(sp);
7734 
7735 out:
7736         DTRACE_NFSV4_2(op__open__confirm__done, struct compound_state *, cs,
7737             OPEN_CONFIRM4res *, resp);
7738 }
7739 
7740 /*ARGSUSED*/
7741 void
7742 rfs4_op_open_downgrade(nfs_argop4 *argop, nfs_resop4 *resop,
7743     struct svc_req *req, struct compound_state *cs)
7744 {
7745         OPEN_DOWNGRADE4args *args = &argop->nfs_argop4_u.opopen_downgrade;
7746         OPEN_DOWNGRADE4res *resp = &resop->nfs_resop4_u.opopen_downgrade;
7747         uint32_t access = args->share_access;
7748         uint32_t deny = args->share_deny;
7749         nfsstat4 status;
7750         rfs4_state_t *sp;
7751         rfs4_file_t *fp;
7752         int fflags = 0;
7753 
7754         DTRACE_NFSV4_2(op__open__downgrade__start, struct compound_state *, cs,
7755             OPEN_DOWNGRADE4args *, args);
7756 
7757         if (cs->vp == NULL) {
7758                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
7759                 goto out;
7760         }
7761 
7762         if (cs->vp->v_type != VREG) {
7763                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7764                 return;
7765         }
7766 
7767         status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_VALID);
7768         if (status != NFS4_OK) {
7769                 *cs->statusp = resp->status = status;
7770                 goto out;
7771         }
7772 
7773         /* Ensure specified filehandle matches */
7774         if (cs->vp != sp->rs_finfo->rf_vp) {
7775                 rfs4_state_rele(sp);
7776                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7777                 goto out;
7778         }
7779 
7780         /* hold off other access to open_owner while we tinker */
7781         rfs4_sw_enter(&sp->rs_owner->ro_sw);
7782 
7783         switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
7784         case NFS4_CHECK_STATEID_OKAY:
7785                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7786                     resop) != NFS4_CHKSEQ_OKAY) {
7787                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7788                         goto end;
7789                 }
7790                 break;
7791         case NFS4_CHECK_STATEID_OLD:
7792                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7793                 goto end;
7794         case NFS4_CHECK_STATEID_BAD:
7795                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7796                 goto end;
7797         case NFS4_CHECK_STATEID_EXPIRED:
7798                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
7799                 goto end;
7800         case NFS4_CHECK_STATEID_CLOSED:
7801                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
7802                 goto end;
7803         case NFS4_CHECK_STATEID_UNCONFIRMED:
7804                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
7805                 goto end;
7806         case NFS4_CHECK_STATEID_REPLAY:
7807                 /* Check the sequence id for the open owner */
7808                 switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
7809                     resop)) {
7810                 case NFS4_CHKSEQ_OKAY:
7811                         /*
7812                          * This is replayed stateid; if seqid matches
7813                          * next expected, then client is using wrong seqid.
7814                          */
7815                         /* fall through */
7816                 case NFS4_CHKSEQ_BAD:
7817                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
7818                         goto end;
7819                 case NFS4_CHKSEQ_REPLAY:
7820                         /*
7821                          * Note this case is the duplicate case so
7822                          * resp->status is already set.
7823                          */
7824                         *cs->statusp = resp->status;
7825                         rfs4_update_lease(sp->rs_owner->ro_client);
7826                         goto end;
7827                 }
7828                 break;
7829         default:
7830                 ASSERT(FALSE);
7831                 break;
7832         }
7833 
7834         rfs4_dbe_lock(sp->rs_dbe);
7835         /*
7836          * Check that the new access modes and deny modes are valid.
7837          * Check that no invalid bits are set.
7838          */
7839         if ((access & ~(OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) ||
7840             (deny & ~(OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE))) {
7841                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7842                 rfs4_update_open_sequence(sp->rs_owner);
7843                 rfs4_dbe_unlock(sp->rs_dbe);
7844                 goto end;
7845         }
7846 
7847         /*
7848          * The new modes must be a subset of the current modes and
7849          * the access must specify at least one mode. To test that
7850          * the new mode is a subset of the current modes we bitwise
7851          * AND them together and check that the result equals the new
7852          * mode. For example:
7853          * New mode, access == R and current mode, sp->rs_open_access  == RW
7854          * access & sp->rs_open_access == R == access, so the new access mode
7855          * is valid. Consider access == RW, sp->rs_open_access = R
7856          * access & sp->rs_open_access == R != access, so the new access mode
7857          * is invalid.
7858          */
7859         if ((access & sp->rs_open_access) != access ||
7860             (deny & sp->rs_open_deny) != deny ||
7861             (access &
7862             (OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WRITE)) == 0) {
7863                 *cs->statusp = resp->status = NFS4ERR_INVAL;
7864                 rfs4_update_open_sequence(sp->rs_owner);
7865                 rfs4_dbe_unlock(sp->rs_dbe);
7866                 goto end;
7867         }
7868 
7869         /*
7870          * Release any share locks associated with this stateID.
7871          * Strictly speaking, this violates the spec because the
7872          * spec effectively requires that open downgrade be atomic.
7873          * At present, fs_shrlock does not have this capability.
7874          */
7875         (void) rfs4_unshare(sp);
7876 
7877         status = rfs4_share(sp, access, deny);
7878         if (status != NFS4_OK) {
7879                 *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
7880                 rfs4_update_open_sequence(sp->rs_owner);
7881                 rfs4_dbe_unlock(sp->rs_dbe);
7882                 goto end;
7883         }
7884 
7885         fp = sp->rs_finfo;
7886         rfs4_dbe_lock(fp->rf_dbe);
7887 
7888         /*
7889          * If the current mode has deny read and the new mode
7890          * does not, decrement the number of deny read mode bits
7891          * and if it goes to zero turn off the deny read bit
7892          * on the file.
7893          */
7894         if ((sp->rs_open_deny & OPEN4_SHARE_DENY_READ) &&
7895             (deny & OPEN4_SHARE_DENY_READ) == 0) {
7896                 fp->rf_deny_read--;
7897                 if (fp->rf_deny_read == 0)
7898                         fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
7899         }
7900 
7901         /*
7902          * If the current mode has deny write and the new mode
7903          * does not, decrement the number of deny write mode bits
7904          * and if it goes to zero turn off the deny write bit
7905          * on the file.
7906          */
7907         if ((sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) &&
7908             (deny & OPEN4_SHARE_DENY_WRITE) == 0) {
7909                 fp->rf_deny_write--;
7910                 if (fp->rf_deny_write == 0)
7911                         fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
7912         }
7913 
7914         /*
7915          * If the current mode has access read and the new mode
7916          * does not, decrement the number of access read mode bits
7917          * and if it goes to zero turn off the access read bit
7918          * on the file.  set fflags to FREAD for the call to
7919          * vn_open_downgrade().
7920          */
7921         if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) &&
7922             (access & OPEN4_SHARE_ACCESS_READ) == 0) {
7923                 fp->rf_access_read--;
7924                 if (fp->rf_access_read == 0)
7925                         fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
7926                 fflags |= FREAD;
7927         }
7928 
7929         /*
7930          * If the current mode has access write and the new mode
7931          * does not, decrement the number of access write mode bits
7932          * and if it goes to zero turn off the access write bit
7933          * on the file.  set fflags to FWRITE for the call to
7934          * vn_open_downgrade().
7935          */
7936         if ((sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) &&
7937             (access & OPEN4_SHARE_ACCESS_WRITE) == 0) {
7938                 fp->rf_access_write--;
7939                 if (fp->rf_access_write == 0)
7940                         fp->rf_share_deny &= ~OPEN4_SHARE_ACCESS_WRITE;
7941                 fflags |= FWRITE;
7942         }
7943 
7944         /* Check that the file is still accessible */
7945         ASSERT(fp->rf_share_access);
7946 
7947         rfs4_dbe_unlock(fp->rf_dbe);
7948 
7949         /* now set the new open access and deny modes */
7950         sp->rs_open_access = access;
7951         sp->rs_open_deny = deny;
7952 
7953         /*
7954          * we successfully downgraded the share lock, now we need to downgrade
7955          * the open. it is possible that the downgrade was only for a deny
7956          * mode and we have nothing else to do.
7957          */
7958         if ((fflags & (FREAD|FWRITE)) != 0)
7959                 vn_open_downgrade(cs->vp, fflags);
7960 
7961         /* Update the stateid */
7962         next_stateid(&sp->rs_stateid);
7963         resp->open_stateid = sp->rs_stateid.stateid;
7964 
7965         rfs4_dbe_unlock(sp->rs_dbe);
7966 
7967         *cs->statusp = resp->status = NFS4_OK;
7968         /* Update the lease */
7969         rfs4_update_lease(sp->rs_owner->ro_client);
7970         /* And the sequence */
7971         rfs4_update_open_sequence(sp->rs_owner);
7972         rfs4_update_open_resp(sp->rs_owner, resop, NULL);
7973 
7974 end:
7975         rfs4_sw_exit(&sp->rs_owner->ro_sw);
7976         rfs4_state_rele(sp);
7977 out:
7978         DTRACE_NFSV4_2(op__open__downgrade__done, struct compound_state *, cs,
7979             OPEN_DOWNGRADE4res *, resp);
7980 }
7981 
7982 static void *
7983 memstr(const void *s1, const char *s2, size_t n)
7984 {
7985         size_t l = strlen(s2);
7986         char *p = (char *)s1;
7987 
7988         while (n >= l) {
7989                 if (bcmp(p, s2, l) == 0)
7990                         return (p);
7991                 p++;
7992                 n--;
7993         }
7994 
7995         return (NULL);
7996 }
7997 
7998 /*
7999  * The logic behind this function is detailed in the NFSv4 RFC in the
8000  * SETCLIENTID operation description under IMPLEMENTATION.  Refer to
8001  * that section for explicit guidance to server behavior for
8002  * SETCLIENTID.
8003  */
8004 void
8005 rfs4_op_setclientid(nfs_argop4 *argop, nfs_resop4 *resop,
8006     struct svc_req *req, struct compound_state *cs)
8007 {
8008         SETCLIENTID4args *args = &argop->nfs_argop4_u.opsetclientid;
8009         SETCLIENTID4res *res = &resop->nfs_resop4_u.opsetclientid;
8010         rfs4_client_t *cp, *newcp, *cp_confirmed, *cp_unconfirmed;
8011         rfs4_clntip_t *ci;
8012         bool_t create;
8013         char *addr, *netid;
8014         int len;
8015 
8016         DTRACE_NFSV4_2(op__setclientid__start, struct compound_state *, cs,
8017             SETCLIENTID4args *, args);
8018 retry:
8019         newcp = cp_confirmed = cp_unconfirmed = NULL;
8020 
8021         /*
8022          * Save the caller's IP address
8023          */
8024         args->client.cl_addr =
8025             (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
8026 
8027         /*
8028          * Record if it is a Solaris client that cannot handle referrals.
8029          */
8030         if (memstr(args->client.id_val, "Solaris", args->client.id_len) &&
8031             !memstr(args->client.id_val, "+referrals", args->client.id_len)) {
8032                 /* Add a "yes, it's downrev" record */
8033                 create = TRUE;
8034                 ci = rfs4_find_clntip(args->client.cl_addr, &create);
8035                 ASSERT(ci != NULL);
8036                 rfs4_dbe_rele(ci->ri_dbe);
8037         } else {
8038                 /* Remove any previous record */
8039                 rfs4_invalidate_clntip(args->client.cl_addr);
8040         }
8041 
8042         /*
8043          * In search of an EXISTING client matching the incoming
8044          * request to establish a new client identifier at the server
8045          */
8046         create = TRUE;
8047         cp = rfs4_findclient(&args->client, &create, NULL);
8048 
8049         /* Should never happen */
8050         ASSERT(cp != NULL);
8051 
8052         if (cp == NULL) {
8053                 *cs->statusp = res->status = NFS4ERR_SERVERFAULT;
8054                 goto out;
8055         }
8056 
8057         /*
8058          * Easiest case. Client identifier is newly created and is
8059          * unconfirmed.  Also note that for this case, no other
8060          * entries exist for the client identifier.  Nothing else to
8061          * check.  Just setup the response and respond.
8062          */
8063         if (create) {
8064                 *cs->statusp = res->status = NFS4_OK;
8065                 res->SETCLIENTID4res_u.resok4.clientid = cp->rc_clientid;
8066                 res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8067                     cp->rc_confirm_verf;
8068                 /* Setup callback information; CB_NULL confirmation later */
8069                 rfs4_client_setcb(cp, &args->callback, args->callback_ident);
8070 
8071                 rfs4_client_rele(cp);
8072                 goto out;
8073         }
8074 
8075         /*
8076          * An existing, confirmed client may exist but it may not have
8077          * been active for at least one lease period.  If so, then
8078          * "close" the client and create a new client identifier
8079          */
8080         if (rfs4_lease_expired(cp)) {
8081                 rfs4_client_close(cp);
8082                 goto retry;
8083         }
8084 
8085         if (cp->rc_need_confirm == TRUE)
8086                 cp_unconfirmed = cp;
8087         else
8088                 cp_confirmed = cp;
8089 
8090         cp = NULL;
8091 
8092         /*
8093          * We have a confirmed client, now check for an
8094          * unconfimred entry
8095          */
8096         if (cp_confirmed) {
8097                 /* If creds don't match then client identifier is inuse */
8098                 if (!creds_ok(cp_confirmed->rc_cr_set, req, cs)) {
8099                         rfs4_cbinfo_t *cbp;
8100                         /*
8101                          * Some one else has established this client
8102                          * id. Try and say * who they are. We will use
8103                          * the call back address supplied by * the
8104                          * first client.
8105                          */
8106                         *cs->statusp = res->status = NFS4ERR_CLID_INUSE;
8107 
8108                         addr = netid = NULL;
8109 
8110                         cbp = &cp_confirmed->rc_cbinfo;
8111                         if (cbp->cb_callback.cb_location.r_addr &&
8112                             cbp->cb_callback.cb_location.r_netid) {
8113                                 cb_client4 *cbcp = &cbp->cb_callback;
8114 
8115                                 len = strlen(cbcp->cb_location.r_addr)+1;
8116                                 addr = kmem_alloc(len, KM_SLEEP);
8117                                 bcopy(cbcp->cb_location.r_addr, addr, len);
8118                                 len = strlen(cbcp->cb_location.r_netid)+1;
8119                                 netid = kmem_alloc(len, KM_SLEEP);
8120                                 bcopy(cbcp->cb_location.r_netid, netid, len);
8121                         }
8122 
8123                         res->SETCLIENTID4res_u.client_using.r_addr = addr;
8124                         res->SETCLIENTID4res_u.client_using.r_netid = netid;
8125 
8126                         rfs4_client_rele(cp_confirmed);
8127                 }
8128 
8129                 /*
8130                  * Confirmed, creds match, and verifier matches; must
8131                  * be an update of the callback info
8132                  */
8133                 if (cp_confirmed->rc_nfs_client.verifier ==
8134                     args->client.verifier) {
8135                         /* Setup callback information */
8136                         rfs4_client_setcb(cp_confirmed, &args->callback,
8137                             args->callback_ident);
8138 
8139                         /* everything okay -- move ahead */
8140                         *cs->statusp = res->status = NFS4_OK;
8141                         res->SETCLIENTID4res_u.resok4.clientid =
8142                             cp_confirmed->rc_clientid;
8143 
8144                         /* update the confirm_verifier and return it */
8145                         rfs4_client_scv_next(cp_confirmed);
8146                         res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8147                             cp_confirmed->rc_confirm_verf;
8148 
8149                         rfs4_client_rele(cp_confirmed);
8150                         goto out;
8151                 }
8152 
8153                 /*
8154                  * Creds match but the verifier doesn't.  Must search
8155                  * for an unconfirmed client that would be replaced by
8156                  * this request.
8157                  */
8158                 create = FALSE;
8159                 cp_unconfirmed = rfs4_findclient(&args->client, &create,
8160                     cp_confirmed);
8161         }
8162 
8163         /*
8164          * At this point, we have taken care of the brand new client
8165          * struct, INUSE case, update of an existing, and confirmed
8166          * client struct.
8167          */
8168 
8169         /*
8170          * check to see if things have changed while we originally
8171          * picked up the client struct.  If they have, then return and
8172          * retry the processing of this SETCLIENTID request.
8173          */
8174         if (cp_unconfirmed) {
8175                 rfs4_dbe_lock(cp_unconfirmed->rc_dbe);
8176                 if (!cp_unconfirmed->rc_need_confirm) {
8177                         rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
8178                         rfs4_client_rele(cp_unconfirmed);
8179                         if (cp_confirmed)
8180                                 rfs4_client_rele(cp_confirmed);
8181                         goto retry;
8182                 }
8183                 /* do away with the old unconfirmed one */
8184                 rfs4_dbe_invalidate(cp_unconfirmed->rc_dbe);
8185                 rfs4_dbe_unlock(cp_unconfirmed->rc_dbe);
8186                 rfs4_client_rele(cp_unconfirmed);
8187                 cp_unconfirmed = NULL;
8188         }
8189 
8190         /*
8191          * This search will temporarily hide the confirmed client
8192          * struct while a new client struct is created as the
8193          * unconfirmed one.
8194          */
8195         create = TRUE;
8196         newcp = rfs4_findclient(&args->client, &create, cp_confirmed);
8197 
8198         ASSERT(newcp != NULL);
8199 
8200         if (newcp == NULL) {
8201                 *cs->statusp = res->status = NFS4ERR_SERVERFAULT;
8202                 rfs4_client_rele(cp_confirmed);
8203                 goto out;
8204         }
8205 
8206         /*
8207          * If one was not created, then a similar request must be in
8208          * process so release and start over with this one
8209          */
8210         if (create != TRUE) {
8211                 rfs4_client_rele(newcp);
8212                 if (cp_confirmed)
8213                         rfs4_client_rele(cp_confirmed);
8214                 goto retry;
8215         }
8216 
8217         *cs->statusp = res->status = NFS4_OK;
8218         res->SETCLIENTID4res_u.resok4.clientid = newcp->rc_clientid;
8219         res->SETCLIENTID4res_u.resok4.setclientid_confirm =
8220             newcp->rc_confirm_verf;
8221         /* Setup callback information; CB_NULL confirmation later */
8222         rfs4_client_setcb(newcp, &args->callback, args->callback_ident);
8223 
8224         newcp->rc_cp_confirmed = cp_confirmed;
8225 
8226         rfs4_client_rele(newcp);
8227 
8228 out:
8229         DTRACE_NFSV4_2(op__setclientid__done, struct compound_state *, cs,
8230             SETCLIENTID4res *, res);
8231 }
8232 
8233 /*ARGSUSED*/
8234 void
8235 rfs4_op_setclientid_confirm(nfs_argop4 *argop, nfs_resop4 *resop,
8236     struct svc_req *req, struct compound_state *cs)
8237 {
8238         SETCLIENTID_CONFIRM4args *args =
8239             &argop->nfs_argop4_u.opsetclientid_confirm;
8240         SETCLIENTID_CONFIRM4res *res =
8241             &resop->nfs_resop4_u.opsetclientid_confirm;
8242         rfs4_client_t *cp, *cptoclose = NULL;
8243         nfs4_srv_t *nsrv4;
8244 
8245         DTRACE_NFSV4_2(op__setclientid__confirm__start,
8246             struct compound_state *, cs,
8247             SETCLIENTID_CONFIRM4args *, args);
8248 
8249         nsrv4 = nfs4_get_srv();
8250         *cs->statusp = res->status = NFS4_OK;
8251 
8252         cp = rfs4_findclient_by_id(args->clientid, TRUE);
8253 
8254         if (cp == NULL) {
8255                 *cs->statusp = res->status =
8256                     rfs4_check_clientid(&args->clientid, 1);
8257                 goto out;
8258         }
8259 
8260         if (!creds_ok(cp, req, cs)) {
8261                 *cs->statusp = res->status = NFS4ERR_CLID_INUSE;
8262                 rfs4_client_rele(cp);
8263                 goto out;
8264         }
8265 
8266         /* If the verifier doesn't match, the record doesn't match */
8267         if (cp->rc_confirm_verf != args->setclientid_confirm) {
8268                 *cs->statusp = res->status = NFS4ERR_STALE_CLIENTID;
8269                 rfs4_client_rele(cp);
8270                 goto out;
8271         }
8272 
8273         rfs4_dbe_lock(cp->rc_dbe);
8274         cp->rc_need_confirm = FALSE;
8275         if (cp->rc_cp_confirmed) {
8276                 cptoclose = cp->rc_cp_confirmed;
8277                 cptoclose->rc_ss_remove = 1;
8278                 cp->rc_cp_confirmed = NULL;
8279         }
8280 
8281         /*
8282          * Update the client's associated server instance, if it's changed
8283          * since the client was created.
8284          */
8285         if (rfs4_servinst(cp) != nsrv4->nfs4_cur_servinst)
8286                 rfs4_servinst_assign(nsrv4, cp, nsrv4->nfs4_cur_servinst);
8287 
8288         /*
8289          * Record clientid in stable storage.
8290          * Must be done after server instance has been assigned.
8291          */
8292         rfs4_ss_clid(nsrv4, cp);
8293 
8294         rfs4_dbe_unlock(cp->rc_dbe);
8295 
8296         if (cptoclose)
8297                 /* don't need to rele, client_close does it */
8298                 rfs4_client_close(cptoclose);
8299 
8300         /* If needed, initiate CB_NULL call for callback path */
8301         rfs4_deleg_cb_check(cp);
8302         rfs4_update_lease(cp);
8303 
8304         /*
8305          * Check to see if client can perform reclaims
8306          */
8307         rfs4_ss_chkclid(nsrv4, cp);
8308 
8309         rfs4_client_rele(cp);
8310 
8311 out:
8312         DTRACE_NFSV4_2(op__setclientid__confirm__done,
8313             struct compound_state *, cs,
8314             SETCLIENTID_CONFIRM4 *, res);
8315 }
8316 
8317 
8318 /*ARGSUSED*/
8319 void
8320 rfs4_op_close(nfs_argop4 *argop, nfs_resop4 *resop,
8321     struct svc_req *req, struct compound_state *cs)
8322 {
8323         CLOSE4args *args = &argop->nfs_argop4_u.opclose;
8324         CLOSE4res *resp = &resop->nfs_resop4_u.opclose;
8325         rfs4_state_t *sp;
8326         nfsstat4 status;
8327 
8328         DTRACE_NFSV4_2(op__close__start, struct compound_state *, cs,
8329             CLOSE4args *, args);
8330 
8331         if (cs->vp == NULL) {
8332                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8333                 goto out;
8334         }
8335 
8336         status = rfs4_get_state(&args->open_stateid, &sp, RFS4_DBS_INVALID);
8337         if (status != NFS4_OK) {
8338                 *cs->statusp = resp->status = status;
8339                 goto out;
8340         }
8341 
8342         /* Ensure specified filehandle matches */
8343         if (cs->vp != sp->rs_finfo->rf_vp) {
8344                 rfs4_state_rele(sp);
8345                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8346                 goto out;
8347         }
8348 
8349         /* hold off other access to open_owner while we tinker */
8350         rfs4_sw_enter(&sp->rs_owner->ro_sw);
8351 
8352         switch (rfs4_check_stateid_seqid(sp, &args->open_stateid)) {
8353         case NFS4_CHECK_STATEID_OKAY:
8354                 if (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8355                     resop) != NFS4_CHKSEQ_OKAY) {
8356                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8357                         goto end;
8358                 }
8359                 break;
8360         case NFS4_CHECK_STATEID_OLD:
8361                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8362                 goto end;
8363         case NFS4_CHECK_STATEID_BAD:
8364                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8365                 goto end;
8366         case NFS4_CHECK_STATEID_EXPIRED:
8367                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
8368                 goto end;
8369         case NFS4_CHECK_STATEID_CLOSED:
8370                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8371                 goto end;
8372         case NFS4_CHECK_STATEID_UNCONFIRMED:
8373                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8374                 goto end;
8375         case NFS4_CHECK_STATEID_REPLAY:
8376                 /* Check the sequence id for the open owner */
8377                 switch (rfs4_check_open_seqid(args->seqid, sp->rs_owner,
8378                     resop)) {
8379                 case NFS4_CHKSEQ_OKAY:
8380                         /*
8381                          * This is replayed stateid; if seqid matches
8382                          * next expected, then client is using wrong seqid.
8383                          */
8384                         /* FALL THROUGH */
8385                 case NFS4_CHKSEQ_BAD:
8386                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8387                         goto end;
8388                 case NFS4_CHKSEQ_REPLAY:
8389                         /*
8390                          * Note this case is the duplicate case so
8391                          * resp->status is already set.
8392                          */
8393                         *cs->statusp = resp->status;
8394                         rfs4_update_lease(sp->rs_owner->ro_client);
8395                         goto end;
8396                 }
8397                 break;
8398         default:
8399                 ASSERT(FALSE);
8400                 break;
8401         }
8402 
8403         rfs4_dbe_lock(sp->rs_dbe);
8404 
8405         /* Update the stateid. */
8406         next_stateid(&sp->rs_stateid);
8407         resp->open_stateid = sp->rs_stateid.stateid;
8408 
8409         rfs4_dbe_unlock(sp->rs_dbe);
8410 
8411         rfs4_update_lease(sp->rs_owner->ro_client);
8412         rfs4_update_open_sequence(sp->rs_owner);
8413         rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8414 
8415         rfs4_state_close(sp, FALSE, FALSE, cs->cr);
8416 
8417         *cs->statusp = resp->status = status;
8418 
8419 end:
8420         rfs4_sw_exit(&sp->rs_owner->ro_sw);
8421         rfs4_state_rele(sp);
8422 out:
8423         DTRACE_NFSV4_2(op__close__done, struct compound_state *, cs,
8424             CLOSE4res *, resp);
8425 }
8426 
8427 /*
8428  * Manage the counts on the file struct and close all file locks
8429  */
8430 /*ARGSUSED*/
8431 void
8432 rfs4_release_share_lock_state(rfs4_state_t *sp, cred_t *cr,
8433     bool_t close_of_client)
8434 {
8435         rfs4_file_t *fp = sp->rs_finfo;
8436         rfs4_lo_state_t *lsp;
8437         int fflags = 0;
8438 
8439         /*
8440          * If this call is part of the larger closing down of client
8441          * state then it is just easier to release all locks
8442          * associated with this client instead of going through each
8443          * individual file and cleaning locks there.
8444          */
8445         if (close_of_client) {
8446                 if (sp->rs_owner->ro_client->rc_unlksys_completed == FALSE &&
8447                     !list_is_empty(&sp->rs_lostatelist) &&
8448                     sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID) {
8449                         /* Is the PxFS kernel module loaded? */
8450                         if (lm_remove_file_locks != NULL) {
8451                                 int new_sysid;
8452 
8453                                 /* Encode the cluster nodeid in new sysid */
8454                                 new_sysid = sp->rs_owner->ro_client->rc_sysidt;
8455                                 lm_set_nlmid_flk(&new_sysid);
8456 
8457                                 /*
8458                                  * This PxFS routine removes file locks for a
8459                                  * client over all nodes of a cluster.
8460                                  */
8461                                 NFS4_DEBUG(rfs4_debug, (CE_NOTE,
8462                                     "lm_remove_file_locks(sysid=0x%x)\n",
8463                                     new_sysid));
8464                                 (*lm_remove_file_locks)(new_sysid);
8465                         } else {
8466                                 struct flock64 flk;
8467 
8468                                 /* Release all locks for this client */
8469                                 flk.l_type = F_UNLKSYS;
8470                                 flk.l_whence = 0;
8471                                 flk.l_start = 0;
8472                                 flk.l_len = 0;
8473                                 flk.l_sysid =
8474                                     sp->rs_owner->ro_client->rc_sysidt;
8475                                 flk.l_pid = 0;
8476                                 (void) VOP_FRLOCK(sp->rs_finfo->rf_vp, F_SETLK,
8477                                     &flk, F_REMOTELOCK | FREAD | FWRITE,
8478                                     (u_offset_t)0, NULL, CRED(), NULL);
8479                         }
8480 
8481                         sp->rs_owner->ro_client->rc_unlksys_completed = TRUE;
8482                 }
8483         }
8484 
8485         /*
8486          * Release all locks on this file by this lock owner or at
8487          * least mark the locks as having been released
8488          */
8489         for (lsp = list_head(&sp->rs_lostatelist); lsp != NULL;
8490             lsp = list_next(&sp->rs_lostatelist, lsp)) {
8491                 lsp->rls_locks_cleaned = TRUE;
8492 
8493                 /* Was this already taken care of above? */
8494                 if (!close_of_client &&
8495                     sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8496                         (void) cleanlocks(sp->rs_finfo->rf_vp,
8497                             lsp->rls_locker->rl_pid,
8498                             lsp->rls_locker->rl_client->rc_sysidt);
8499         }
8500 
8501         /*
8502          * Release any shrlocks associated with this open state ID.
8503          * This must be done before the rfs4_state gets marked closed.
8504          */
8505         if (sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID)
8506                 (void) rfs4_unshare(sp);
8507 
8508         if (sp->rs_open_access) {
8509                 rfs4_dbe_lock(fp->rf_dbe);
8510 
8511                 /*
8512                  * Decrement the count for each access and deny bit that this
8513                  * state has contributed to the file.
8514                  * If the file counts go to zero
8515                  * clear the appropriate bit in the appropriate mask.
8516                  */
8517                 if (sp->rs_open_access & OPEN4_SHARE_ACCESS_READ) {
8518                         fp->rf_access_read--;
8519                         fflags |= FREAD;
8520                         if (fp->rf_access_read == 0)
8521                                 fp->rf_share_access &= ~OPEN4_SHARE_ACCESS_READ;
8522                 }
8523                 if (sp->rs_open_access & OPEN4_SHARE_ACCESS_WRITE) {
8524                         fp->rf_access_write--;
8525                         fflags |= FWRITE;
8526                         if (fp->rf_access_write == 0)
8527                                 fp->rf_share_access &=
8528                                     ~OPEN4_SHARE_ACCESS_WRITE;
8529                 }
8530                 if (sp->rs_open_deny & OPEN4_SHARE_DENY_READ) {
8531                         fp->rf_deny_read--;
8532                         if (fp->rf_deny_read == 0)
8533                                 fp->rf_share_deny &= ~OPEN4_SHARE_DENY_READ;
8534                 }
8535                 if (sp->rs_open_deny & OPEN4_SHARE_DENY_WRITE) {
8536                         fp->rf_deny_write--;
8537                         if (fp->rf_deny_write == 0)
8538                                 fp->rf_share_deny &= ~OPEN4_SHARE_DENY_WRITE;
8539                 }
8540 
8541                 (void) VOP_CLOSE(fp->rf_vp, fflags, 1, (offset_t)0, cr, NULL);
8542 
8543                 rfs4_dbe_unlock(fp->rf_dbe);
8544 
8545                 sp->rs_open_access = 0;
8546                 sp->rs_open_deny = 0;
8547         }
8548 }
8549 
8550 /*
8551  * lock_denied: Fill in a LOCK4deneid structure given an flock64 structure.
8552  */
8553 static nfsstat4
8554 lock_denied(LOCK4denied *dp, struct flock64 *flk)
8555 {
8556         rfs4_lockowner_t *lo;
8557         rfs4_client_t *cp;
8558         uint32_t len;
8559 
8560         lo = rfs4_findlockowner_by_pid(flk->l_pid);
8561         if (lo != NULL) {
8562                 cp = lo->rl_client;
8563                 if (rfs4_lease_expired(cp)) {
8564                         rfs4_lockowner_rele(lo);
8565                         rfs4_dbe_hold(cp->rc_dbe);
8566                         rfs4_client_close(cp);
8567                         return (NFS4ERR_EXPIRED);
8568                 }
8569                 dp->owner.clientid = lo->rl_owner.clientid;
8570                 len = lo->rl_owner.owner_len;
8571                 dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8572                 bcopy(lo->rl_owner.owner_val, dp->owner.owner_val, len);
8573                 dp->owner.owner_len = len;
8574                 rfs4_lockowner_rele(lo);
8575                 goto finish;
8576         }
8577 
8578         /*
8579          * Its not a NFS4 lock. We take advantage that the upper 32 bits
8580          * of the client id contain the boot time for a NFS4 lock. So we
8581          * fabricate and identity by setting clientid to the sysid, and
8582          * the lock owner to the pid.
8583          */
8584         dp->owner.clientid = flk->l_sysid;
8585         len = sizeof (pid_t);
8586         dp->owner.owner_len = len;
8587         dp->owner.owner_val = kmem_alloc(len, KM_SLEEP);
8588         bcopy(&flk->l_pid, dp->owner.owner_val, len);
8589 finish:
8590         dp->offset = flk->l_start;
8591         dp->length = flk->l_len;
8592 
8593         if (flk->l_type == F_RDLCK)
8594                 dp->locktype = READ_LT;
8595         else if (flk->l_type == F_WRLCK)
8596                 dp->locktype = WRITE_LT;
8597         else
8598                 return (NFS4ERR_INVAL); /* no mapping from POSIX ltype to v4 */
8599 
8600         return (NFS4_OK);
8601 }
8602 
8603 /*
8604  * The NFSv4.0 LOCK operation does not support the blocking lock (at the
8605  * NFSv4.0 protocol level) so the client needs to resend the LOCK request in a
8606  * case the lock is denied by the NFSv4.0 server.  NFSv4.0 clients are prepared
8607  * for that (obviously); they are sending the LOCK requests with some delays
8608  * between the attempts.  See nfs4frlock() and nfs4_block_and_wait() for the
8609  * locking and delay implementation at the client side.
8610  *
8611  * To make the life of the clients easier, the NFSv4.0 server tries to do some
8612  * fast retries on its own (the for loop below) in a hope the lock will be
8613  * available soon.  And if not, the client won't need to resend the LOCK
8614  * requests so fast to check the lock availability.  This basically saves some
8615  * network traffic and tries to make sure the client gets the lock ASAP.
8616  */
8617 static int
8618 setlock(vnode_t *vp, struct flock64 *flock, int flag, cred_t *cred)
8619 {
8620         int error;
8621         struct flock64 flk;
8622         int i;
8623         clock_t delaytime;
8624         int cmd;
8625         int spin_cnt = 0;
8626 
8627         cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
8628 retry:
8629         delaytime = MSEC_TO_TICK_ROUNDUP(rfs4_lock_delay);
8630 
8631         for (i = 0; i < rfs4_maxlock_tries; i++) {
8632                 LOCK_PRINT(rfs4_debug, "setlock", cmd, flock);
8633                 error = VOP_FRLOCK(vp, cmd,
8634                     flock, flag, (u_offset_t)0, NULL, cred, NULL);
8635 
8636                 if (error != EAGAIN && error != EACCES)
8637                         break;
8638 
8639                 if (i < rfs4_maxlock_tries - 1) {
8640                         delay(delaytime);
8641                         delaytime *= 2;
8642                 }
8643         }
8644 
8645         if (error == EAGAIN || error == EACCES) {
8646                 /* Get the owner of the lock */
8647                 flk = *flock;
8648                 LOCK_PRINT(rfs4_debug, "setlock", F_GETLK, &flk);
8649                 if (VOP_FRLOCK(vp, F_GETLK, &flk, flag, 0, NULL, cred,
8650                     NULL) == 0) {
8651                         /*
8652                          * There's a race inherent in the current VOP_FRLOCK
8653                          * design where:
8654                          * a: "other guy" takes a lock that conflicts with a
8655                          * lock we want
8656                          * b: we attempt to take our lock (non-blocking) and
8657                          * the attempt fails.
8658                          * c: "other guy" releases the conflicting lock
8659                          * d: we ask what lock conflicts with the lock we want,
8660                          * getting F_UNLCK (no lock blocks us)
8661                          *
8662                          * If we retry the non-blocking lock attempt in this
8663                          * case (restart at step 'b') there's some possibility
8664                          * that many such attempts might fail.  However a test
8665                          * designed to actually provoke this race shows that
8666                          * the vast majority of cases require no retry, and
8667                          * only a few took as many as three retries.  Here's
8668                          * the test outcome:
8669                          *
8670                          *         number of retries    how many times we needed
8671                          *                              that many retries
8672                          *         0                    79461
8673                          *         1                      862
8674                          *         2                       49
8675                          *         3                        5
8676                          *
8677                          * Given those empirical results, we arbitrarily limit
8678                          * the retry count to ten.
8679                          *
8680                          * If we actually make to ten retries and give up,
8681                          * nothing catastrophic happens, but we're unable to
8682                          * return the information about the conflicting lock to
8683                          * the NFS client.  That's an acceptable trade off vs.
8684                          * letting this retry loop run forever.
8685                          */
8686                         if (flk.l_type == F_UNLCK) {
8687                                 if (spin_cnt++ < 10) {
8688                                         /* No longer locked, retry */
8689                                         goto retry;
8690                                 }
8691                         } else {
8692                                 *flock = flk;
8693                                 LOCK_PRINT(rfs4_debug, "setlock(blocking lock)",
8694                                     F_GETLK, &flk);
8695                         }
8696                 }
8697         }
8698 
8699         return (error);
8700 }
8701 
8702 /*ARGSUSED*/
8703 static nfsstat4
8704 rfs4_do_lock(rfs4_lo_state_t *lsp, nfs_lock_type4 locktype,
8705     offset4 offset, length4 length, cred_t *cred, nfs_resop4 *resop)
8706 {
8707         nfsstat4 status;
8708         rfs4_lockowner_t *lo = lsp->rls_locker;
8709         rfs4_state_t *sp = lsp->rls_state;
8710         struct flock64 flock;
8711         int16_t ltype;
8712         int flag;
8713         int error;
8714         sysid_t sysid;
8715         LOCK4res *lres;
8716         vnode_t *vp;
8717 
8718         if (rfs4_lease_expired(lo->rl_client)) {
8719                 return (NFS4ERR_EXPIRED);
8720         }
8721 
8722         if ((status = rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
8723                 return (status);
8724 
8725         /* Check for zero length. To lock to end of file use all ones for V4 */
8726         if (length == 0)
8727                 return (NFS4ERR_INVAL);
8728         else if (length == (length4)(~0))
8729                 length = 0;             /* Posix to end of file  */
8730 
8731 retry:
8732         rfs4_dbe_lock(sp->rs_dbe);
8733         if (sp->rs_closed == TRUE) {
8734                 rfs4_dbe_unlock(sp->rs_dbe);
8735                 return (NFS4ERR_OLD_STATEID);
8736         }
8737 
8738         if (resop->resop != OP_LOCKU) {
8739                 switch (locktype) {
8740                 case READ_LT:
8741                 case READW_LT:
8742                         if ((sp->rs_share_access
8743                             & OPEN4_SHARE_ACCESS_READ) == 0) {
8744                                 rfs4_dbe_unlock(sp->rs_dbe);
8745 
8746                                 return (NFS4ERR_OPENMODE);
8747                         }
8748                         ltype = F_RDLCK;
8749                         break;
8750                 case WRITE_LT:
8751                 case WRITEW_LT:
8752                         if ((sp->rs_share_access
8753                             & OPEN4_SHARE_ACCESS_WRITE) == 0) {
8754                                 rfs4_dbe_unlock(sp->rs_dbe);
8755 
8756                                 return (NFS4ERR_OPENMODE);
8757                         }
8758                         ltype = F_WRLCK;
8759                         break;
8760                 }
8761         } else
8762                 ltype = F_UNLCK;
8763 
8764         flock.l_type = ltype;
8765         flock.l_whence = 0;             /* SEEK_SET */
8766         flock.l_start = offset;
8767         flock.l_len = length;
8768         flock.l_sysid = sysid;
8769         flock.l_pid = lsp->rls_locker->rl_pid;
8770 
8771         /* Note that length4 is uint64_t but l_len and l_start are off64_t */
8772         if (flock.l_len < 0 || flock.l_start < 0) {
8773                 rfs4_dbe_unlock(sp->rs_dbe);
8774                 return (NFS4ERR_INVAL);
8775         }
8776 
8777         /*
8778          * N.B. FREAD has the same value as OPEN4_SHARE_ACCESS_READ and
8779          * FWRITE has the same value as OPEN4_SHARE_ACCESS_WRITE.
8780          */
8781         flag = (int)sp->rs_share_access | F_REMOTELOCK;
8782 
8783         vp = sp->rs_finfo->rf_vp;
8784         VN_HOLD(vp);
8785 
8786         /*
8787          * We need to unlock sp before we call the underlying filesystem to
8788          * acquire the file lock.
8789          */
8790         rfs4_dbe_unlock(sp->rs_dbe);
8791 
8792         error = setlock(vp, &flock, flag, cred);
8793 
8794         /*
8795          * Make sure the file is still open.  In a case the file was closed in
8796          * the meantime, clean the lock we acquired using the setlock() call
8797          * above, and return the appropriate error.
8798          */
8799         rfs4_dbe_lock(sp->rs_dbe);
8800         if (sp->rs_closed == TRUE) {
8801                 cleanlocks(vp, lsp->rls_locker->rl_pid, sysid);
8802                 rfs4_dbe_unlock(sp->rs_dbe);
8803 
8804                 VN_RELE(vp);
8805 
8806                 return (NFS4ERR_OLD_STATEID);
8807         }
8808         rfs4_dbe_unlock(sp->rs_dbe);
8809 
8810         VN_RELE(vp);
8811 
8812         if (error == 0) {
8813                 rfs4_dbe_lock(lsp->rls_dbe);
8814                 next_stateid(&lsp->rls_lockid);
8815                 rfs4_dbe_unlock(lsp->rls_dbe);
8816         }
8817 
8818         /*
8819          * N.B. We map error values to nfsv4 errors. This is differrent
8820          * than puterrno4 routine.
8821          */
8822         switch (error) {
8823         case 0:
8824                 status = NFS4_OK;
8825                 break;
8826         case EAGAIN:
8827         case EACCES:            /* Old value */
8828                 /* Can only get here if op is OP_LOCK */
8829                 ASSERT(resop->resop == OP_LOCK);
8830                 lres = &resop->nfs_resop4_u.oplock;
8831                 status = NFS4ERR_DENIED;
8832                 if (lock_denied(&lres->LOCK4res_u.denied, &flock)
8833                     == NFS4ERR_EXPIRED)
8834                         goto retry;
8835                 break;
8836         case ENOLCK:
8837                 status = NFS4ERR_DELAY;
8838                 break;
8839         case EOVERFLOW:
8840                 status = NFS4ERR_INVAL;
8841                 break;
8842         case EINVAL:
8843                 status = NFS4ERR_NOTSUPP;
8844                 break;
8845         default:
8846                 status = NFS4ERR_SERVERFAULT;
8847                 break;
8848         }
8849 
8850         return (status);
8851 }
8852 
8853 /*ARGSUSED*/
8854 void
8855 rfs4_op_lock(nfs_argop4 *argop, nfs_resop4 *resop,
8856     struct svc_req *req, struct compound_state *cs)
8857 {
8858         LOCK4args *args = &argop->nfs_argop4_u.oplock;
8859         LOCK4res *resp = &resop->nfs_resop4_u.oplock;
8860         nfsstat4 status;
8861         stateid4 *stateid;
8862         rfs4_lockowner_t *lo;
8863         rfs4_client_t *cp;
8864         rfs4_state_t *sp = NULL;
8865         rfs4_lo_state_t *lsp = NULL;
8866         bool_t ls_sw_held = FALSE;
8867         bool_t create = TRUE;
8868         bool_t lcreate = TRUE;
8869         bool_t dup_lock = FALSE;
8870         int rc;
8871 
8872         DTRACE_NFSV4_2(op__lock__start, struct compound_state *, cs,
8873             LOCK4args *, args);
8874 
8875         if (cs->vp == NULL) {
8876                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
8877                 DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8878                     cs, LOCK4res *, resp);
8879                 return;
8880         }
8881 
8882         if (args->locker.new_lock_owner) {
8883                 /* Create a new lockowner for this instance */
8884                 open_to_lock_owner4 *olo = &args->locker.locker4_u.open_owner;
8885 
8886                 NFS4_DEBUG(rfs4_debug, (CE_NOTE, "Creating new lock owner"));
8887 
8888                 stateid = &olo->open_stateid;
8889                 status = rfs4_get_state(stateid, &sp, RFS4_DBS_VALID);
8890                 if (status != NFS4_OK) {
8891                         NFS4_DEBUG(rfs4_debug,
8892                             (CE_NOTE, "Get state failed in lock %d", status));
8893                         *cs->statusp = resp->status = status;
8894                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8895                             cs, LOCK4res *, resp);
8896                         return;
8897                 }
8898 
8899                 /* Ensure specified filehandle matches */
8900                 if (cs->vp != sp->rs_finfo->rf_vp) {
8901                         rfs4_state_rele(sp);
8902                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8903                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
8904                             cs, LOCK4res *, resp);
8905                         return;
8906                 }
8907 
8908                 /* hold off other access to open_owner while we tinker */
8909                 rfs4_sw_enter(&sp->rs_owner->ro_sw);
8910 
8911                 switch (rc = rfs4_check_stateid_seqid(sp, stateid)) {
8912                 case NFS4_CHECK_STATEID_OLD:
8913                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8914                         goto end;
8915                 case NFS4_CHECK_STATEID_BAD:
8916                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8917                         goto end;
8918                 case NFS4_CHECK_STATEID_EXPIRED:
8919                         *cs->statusp = resp->status = NFS4ERR_EXPIRED;
8920                         goto end;
8921                 case NFS4_CHECK_STATEID_UNCONFIRMED:
8922                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
8923                         goto end;
8924                 case NFS4_CHECK_STATEID_CLOSED:
8925                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
8926                         goto end;
8927                 case NFS4_CHECK_STATEID_OKAY:
8928                 case NFS4_CHECK_STATEID_REPLAY:
8929                         switch (rfs4_check_olo_seqid(olo->open_seqid,
8930                             sp->rs_owner, resop)) {
8931                         case NFS4_CHKSEQ_OKAY:
8932                                 if (rc == NFS4_CHECK_STATEID_OKAY)
8933                                         break;
8934                                 /*
8935                                  * This is replayed stateid; if seqid
8936                                  * matches next expected, then client
8937                                  * is using wrong seqid.
8938                                  */
8939                                 /* FALLTHROUGH */
8940                         case NFS4_CHKSEQ_BAD:
8941                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
8942                                 goto end;
8943                         case NFS4_CHKSEQ_REPLAY:
8944                                 /* This is a duplicate LOCK request */
8945                                 dup_lock = TRUE;
8946 
8947                                 /*
8948                                  * For a duplicate we do not want to
8949                                  * create a new lockowner as it should
8950                                  * already exist.
8951                                  * Turn off the lockowner create flag.
8952                                  */
8953                                 lcreate = FALSE;
8954                         }
8955                         break;
8956                 }
8957 
8958                 lo = rfs4_findlockowner(&olo->lock_owner, &lcreate);
8959                 if (lo == NULL) {
8960                         NFS4_DEBUG(rfs4_debug,
8961                             (CE_NOTE, "rfs4_op_lock: no lock owner"));
8962                         *cs->statusp = resp->status = NFS4ERR_RESOURCE;
8963                         goto end;
8964                 }
8965 
8966                 lsp = rfs4_findlo_state_by_owner(lo, sp, &create);
8967                 if (lsp == NULL) {
8968                         rfs4_update_lease(sp->rs_owner->ro_client);
8969                         /*
8970                          * Only update theh open_seqid if this is not
8971                          * a duplicate request
8972                          */
8973                         if (dup_lock == FALSE) {
8974                                 rfs4_update_open_sequence(sp->rs_owner);
8975                         }
8976 
8977                         NFS4_DEBUG(rfs4_debug,
8978                             (CE_NOTE, "rfs4_op_lock: no state"));
8979                         *cs->statusp = resp->status = NFS4ERR_SERVERFAULT;
8980                         rfs4_update_open_resp(sp->rs_owner, resop, NULL);
8981                         rfs4_lockowner_rele(lo);
8982                         goto end;
8983                 }
8984 
8985                 /*
8986                  * This is the new_lock_owner branch and the client is
8987                  * supposed to be associating a new lock_owner with
8988                  * the open file at this point.  If we find that a
8989                  * lock_owner/state association already exists and a
8990                  * successful LOCK request was returned to the client,
8991                  * an error is returned to the client since this is
8992                  * not appropriate.  The client should be using the
8993                  * existing lock_owner branch.
8994                  */
8995                 if (dup_lock == FALSE && create == FALSE) {
8996                         if (lsp->rls_lock_completed == TRUE) {
8997                                 *cs->statusp =
8998                                     resp->status = NFS4ERR_BAD_SEQID;
8999                                 rfs4_lockowner_rele(lo);
9000                                 goto end;
9001                         }
9002                 }
9003 
9004                 rfs4_update_lease(sp->rs_owner->ro_client);
9005 
9006                 /*
9007                  * Only update theh open_seqid if this is not
9008                  * a duplicate request
9009                  */
9010                 if (dup_lock == FALSE) {
9011                         rfs4_update_open_sequence(sp->rs_owner);
9012                 }
9013 
9014                 /*
9015                  * If this is a duplicate lock request, just copy the
9016                  * previously saved reply and return.
9017                  */
9018                 if (dup_lock == TRUE) {
9019                         /* verify that lock_seqid's match */
9020                         if (lsp->rls_seqid != olo->lock_seqid) {
9021                                 NFS4_DEBUG(rfs4_debug,
9022                                     (CE_NOTE, "rfs4_op_lock: Dup-Lock seqid bad"
9023                                     "lsp->seqid=%d old->seqid=%d",
9024                                     lsp->rls_seqid, olo->lock_seqid));
9025                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9026                         } else {
9027                                 rfs4_copy_reply(resop, &lsp->rls_reply);
9028                                 /*
9029                                  * Make sure to copy the just
9030                                  * retrieved reply status into the
9031                                  * overall compound status
9032                                  */
9033                                 *cs->statusp = resp->status;
9034                         }
9035                         rfs4_lockowner_rele(lo);
9036                         goto end;
9037                 }
9038 
9039                 rfs4_dbe_lock(lsp->rls_dbe);
9040 
9041                 /* Make sure to update the lock sequence id */
9042                 lsp->rls_seqid = olo->lock_seqid;
9043 
9044                 NFS4_DEBUG(rfs4_debug,
9045                     (CE_NOTE, "Lock seqid established as %d", lsp->rls_seqid));
9046 
9047                 /*
9048                  * This is used to signify the newly created lockowner
9049                  * stateid and its sequence number.  The checks for
9050                  * sequence number and increment don't occur on the
9051                  * very first lock request for a lockowner.
9052                  */
9053                 lsp->rls_skip_seqid_check = TRUE;
9054 
9055                 /* hold off other access to lsp while we tinker */
9056                 rfs4_sw_enter(&lsp->rls_sw);
9057                 ls_sw_held = TRUE;
9058 
9059                 rfs4_dbe_unlock(lsp->rls_dbe);
9060 
9061                 rfs4_lockowner_rele(lo);
9062         } else {
9063                 stateid = &args->locker.locker4_u.lock_owner.lock_stateid;
9064                 /* get lsp and hold the lock on the underlying file struct */
9065                 if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE))
9066                     != NFS4_OK) {
9067                         *cs->statusp = resp->status = status;
9068                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9069                             cs, LOCK4res *, resp);
9070                         return;
9071                 }
9072                 create = FALSE; /* We didn't create lsp */
9073 
9074                 /* Ensure specified filehandle matches */
9075                 if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
9076                         rfs4_lo_state_rele(lsp, TRUE);
9077                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9078                         DTRACE_NFSV4_2(op__lock__done, struct compound_state *,
9079                             cs, LOCK4res *, resp);
9080                         return;
9081                 }
9082 
9083                 /* hold off other access to lsp while we tinker */
9084                 rfs4_sw_enter(&lsp->rls_sw);
9085                 ls_sw_held = TRUE;
9086 
9087                 switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
9088                 /*
9089                  * The stateid looks like it was okay (expected to be
9090                  * the next one)
9091                  */
9092                 case NFS4_CHECK_STATEID_OKAY:
9093                         /*
9094                          * The sequence id is now checked.  Determine
9095                          * if this is a replay or if it is in the
9096                          * expected (next) sequence.  In the case of a
9097                          * replay, there are two replay conditions
9098                          * that may occur.  The first is the normal
9099                          * condition where a LOCK is done with a
9100                          * NFS4_OK response and the stateid is
9101                          * updated.  That case is handled below when
9102                          * the stateid is identified as a REPLAY.  The
9103                          * second is the case where an error is
9104                          * returned, like NFS4ERR_DENIED, and the
9105                          * sequence number is updated but the stateid
9106                          * is not updated.  This second case is dealt
9107                          * with here.  So it may seem odd that the
9108                          * stateid is okay but the sequence id is a
9109                          * replay but it is okay.
9110                          */
9111                         switch (rfs4_check_lock_seqid(
9112                             args->locker.locker4_u.lock_owner.lock_seqid,
9113                             lsp, resop)) {
9114                         case NFS4_CHKSEQ_REPLAY:
9115                                 if (resp->status != NFS4_OK) {
9116                                         /*
9117                                          * Here is our replay and need
9118                                          * to verify that the last
9119                                          * response was an error.
9120                                          */
9121                                         *cs->statusp = resp->status;
9122                                         goto end;
9123                                 }
9124                                 /*
9125                                  * This is done since the sequence id
9126                                  * looked like a replay but it didn't
9127                                  * pass our check so a BAD_SEQID is
9128                                  * returned as a result.
9129                                  */
9130                                 /*FALLTHROUGH*/
9131                         case NFS4_CHKSEQ_BAD:
9132                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9133                                 goto end;
9134                         case NFS4_CHKSEQ_OKAY:
9135                                 /* Everything looks okay move ahead */
9136                                 break;
9137                         }
9138                         break;
9139                 case NFS4_CHECK_STATEID_OLD:
9140                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9141                         goto end;
9142                 case NFS4_CHECK_STATEID_BAD:
9143                         *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9144                         goto end;
9145                 case NFS4_CHECK_STATEID_EXPIRED:
9146                         *cs->statusp = resp->status = NFS4ERR_EXPIRED;
9147                         goto end;
9148                 case NFS4_CHECK_STATEID_CLOSED:
9149                         *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9150                         goto end;
9151                 case NFS4_CHECK_STATEID_REPLAY:
9152                         switch (rfs4_check_lock_seqid(
9153                             args->locker.locker4_u.lock_owner.lock_seqid,
9154                             lsp, resop)) {
9155                         case NFS4_CHKSEQ_OKAY:
9156                                 /*
9157                                  * This is a replayed stateid; if
9158                                  * seqid matches the next expected,
9159                                  * then client is using wrong seqid.
9160                                  */
9161                         case NFS4_CHKSEQ_BAD:
9162                                 *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9163                                 goto end;
9164                         case NFS4_CHKSEQ_REPLAY:
9165                                 rfs4_update_lease(lsp->rls_locker->rl_client);
9166                                 *cs->statusp = status = resp->status;
9167                                 goto end;
9168                         }
9169                         break;
9170                 default:
9171                         ASSERT(FALSE);
9172                         break;
9173                 }
9174 
9175                 rfs4_update_lock_sequence(lsp);
9176                 rfs4_update_lease(lsp->rls_locker->rl_client);
9177         }
9178 
9179         /*
9180          * NFS4 only allows locking on regular files, so
9181          * verify type of object.
9182          */
9183         if (cs->vp->v_type != VREG) {
9184                 if (cs->vp->v_type == VDIR)
9185                         status = NFS4ERR_ISDIR;
9186                 else
9187                         status = NFS4ERR_INVAL;
9188                 goto out;
9189         }
9190 
9191         cp = lsp->rls_state->rs_owner->ro_client;
9192 
9193         if (rfs4_clnt_in_grace(cp) && !args->reclaim) {
9194                 status = NFS4ERR_GRACE;
9195                 goto out;
9196         }
9197 
9198         if (rfs4_clnt_in_grace(cp) && args->reclaim && !cp->rc_can_reclaim) {
9199                 status = NFS4ERR_NO_GRACE;
9200                 goto out;
9201         }
9202 
9203         if (!rfs4_clnt_in_grace(cp) && args->reclaim) {
9204                 status = NFS4ERR_NO_GRACE;
9205                 goto out;
9206         }
9207 
9208         if (lsp->rls_state->rs_finfo->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE)
9209                 cs->deleg = TRUE;
9210 
9211         status = rfs4_do_lock(lsp, args->locktype,
9212             args->offset, args->length, cs->cr, resop);
9213 
9214 out:
9215         lsp->rls_skip_seqid_check = FALSE;
9216 
9217         *cs->statusp = resp->status = status;
9218 
9219         if (status == NFS4_OK) {
9220                 resp->LOCK4res_u.lock_stateid = lsp->rls_lockid.stateid;
9221                 lsp->rls_lock_completed = TRUE;
9222         }
9223         /*
9224          * Only update the "OPEN" response here if this was a new
9225          * lock_owner
9226          */
9227         if (sp)
9228                 rfs4_update_open_resp(sp->rs_owner, resop, NULL);
9229 
9230         rfs4_update_lock_resp(lsp, resop);
9231 
9232 end:
9233         if (lsp) {
9234                 if (ls_sw_held)
9235                         rfs4_sw_exit(&lsp->rls_sw);
9236                 /*
9237                  * If an sp obtained, then the lsp does not represent
9238                  * a lock on the file struct.
9239                  */
9240                 if (sp != NULL)
9241                         rfs4_lo_state_rele(lsp, FALSE);
9242                 else
9243                         rfs4_lo_state_rele(lsp, TRUE);
9244         }
9245         if (sp) {
9246                 rfs4_sw_exit(&sp->rs_owner->ro_sw);
9247                 rfs4_state_rele(sp);
9248         }
9249 
9250         DTRACE_NFSV4_2(op__lock__done, struct compound_state *, cs,
9251             LOCK4res *, resp);
9252 }
9253 
9254 /* free function for LOCK/LOCKT */
9255 static void
9256 lock_denied_free(nfs_resop4 *resop)
9257 {
9258         LOCK4denied *dp = NULL;
9259 
9260         switch (resop->resop) {
9261         case OP_LOCK:
9262                 if (resop->nfs_resop4_u.oplock.status == NFS4ERR_DENIED)
9263                         dp = &resop->nfs_resop4_u.oplock.LOCK4res_u.denied;
9264                 break;
9265         case OP_LOCKT:
9266                 if (resop->nfs_resop4_u.oplockt.status == NFS4ERR_DENIED)
9267                         dp = &resop->nfs_resop4_u.oplockt.denied;
9268                 break;
9269         default:
9270                 break;
9271         }
9272 
9273         if (dp)
9274                 kmem_free(dp->owner.owner_val, dp->owner.owner_len);
9275 }
9276 
9277 /*ARGSUSED*/
9278 void
9279 rfs4_op_locku(nfs_argop4 *argop, nfs_resop4 *resop,
9280     struct svc_req *req, struct compound_state *cs)
9281 {
9282         LOCKU4args *args = &argop->nfs_argop4_u.oplocku;
9283         LOCKU4res *resp = &resop->nfs_resop4_u.oplocku;
9284         nfsstat4 status;
9285         stateid4 *stateid = &args->lock_stateid;
9286         rfs4_lo_state_t *lsp;
9287 
9288         DTRACE_NFSV4_2(op__locku__start, struct compound_state *, cs,
9289             LOCKU4args *, args);
9290 
9291         if (cs->vp == NULL) {
9292                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9293                 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9294                     LOCKU4res *, resp);
9295                 return;
9296         }
9297 
9298         if ((status = rfs4_get_lo_state(stateid, &lsp, TRUE)) != NFS4_OK) {
9299                 *cs->statusp = resp->status = status;
9300                 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9301                     LOCKU4res *, resp);
9302                 return;
9303         }
9304 
9305         /* Ensure specified filehandle matches */
9306         if (cs->vp != lsp->rls_state->rs_finfo->rf_vp) {
9307                 rfs4_lo_state_rele(lsp, TRUE);
9308                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9309                 DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9310                     LOCKU4res *, resp);
9311                 return;
9312         }
9313 
9314         /* hold off other access to lsp while we tinker */
9315         rfs4_sw_enter(&lsp->rls_sw);
9316 
9317         switch (rfs4_check_lo_stateid_seqid(lsp, stateid)) {
9318         case NFS4_CHECK_STATEID_OKAY:
9319                 if (rfs4_check_lock_seqid(args->seqid, lsp, resop)
9320                     != NFS4_CHKSEQ_OKAY) {
9321                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9322                         goto end;
9323                 }
9324                 break;
9325         case NFS4_CHECK_STATEID_OLD:
9326                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9327                 goto end;
9328         case NFS4_CHECK_STATEID_BAD:
9329                 *cs->statusp = resp->status = NFS4ERR_BAD_STATEID;
9330                 goto end;
9331         case NFS4_CHECK_STATEID_EXPIRED:
9332                 *cs->statusp = resp->status = NFS4ERR_EXPIRED;
9333                 goto end;
9334         case NFS4_CHECK_STATEID_CLOSED:
9335                 *cs->statusp = resp->status = NFS4ERR_OLD_STATEID;
9336                 goto end;
9337         case NFS4_CHECK_STATEID_REPLAY:
9338                 switch (rfs4_check_lock_seqid(args->seqid, lsp, resop)) {
9339                 case NFS4_CHKSEQ_OKAY:
9340                                 /*
9341                                  * This is a replayed stateid; if
9342                                  * seqid matches the next expected,
9343                                  * then client is using wrong seqid.
9344                                  */
9345                 case NFS4_CHKSEQ_BAD:
9346                         *cs->statusp = resp->status = NFS4ERR_BAD_SEQID;
9347                         goto end;
9348                 case NFS4_CHKSEQ_REPLAY:
9349                         rfs4_update_lease(lsp->rls_locker->rl_client);
9350                         *cs->statusp = status = resp->status;
9351                         goto end;
9352                 }
9353                 break;
9354         default:
9355                 ASSERT(FALSE);
9356                 break;
9357         }
9358 
9359         rfs4_update_lock_sequence(lsp);
9360         rfs4_update_lease(lsp->rls_locker->rl_client);
9361 
9362         /*
9363          * NFS4 only allows locking on regular files, so
9364          * verify type of object.
9365          */
9366         if (cs->vp->v_type != VREG) {
9367                 if (cs->vp->v_type == VDIR)
9368                         status = NFS4ERR_ISDIR;
9369                 else
9370                         status = NFS4ERR_INVAL;
9371                 goto out;
9372         }
9373 
9374         if (rfs4_clnt_in_grace(lsp->rls_state->rs_owner->ro_client)) {
9375                 status = NFS4ERR_GRACE;
9376                 goto out;
9377         }
9378 
9379         status = rfs4_do_lock(lsp, args->locktype,
9380             args->offset, args->length, cs->cr, resop);
9381 
9382 out:
9383         *cs->statusp = resp->status = status;
9384 
9385         if (status == NFS4_OK)
9386                 resp->lock_stateid = lsp->rls_lockid.stateid;
9387 
9388         rfs4_update_lock_resp(lsp, resop);
9389 
9390 end:
9391         rfs4_sw_exit(&lsp->rls_sw);
9392         rfs4_lo_state_rele(lsp, TRUE);
9393 
9394         DTRACE_NFSV4_2(op__locku__done, struct compound_state *, cs,
9395             LOCKU4res *, resp);
9396 }
9397 
9398 /*
9399  * LOCKT is a best effort routine, the client can not be guaranteed that
9400  * the status return is still in effect by the time the reply is received.
9401  * They are numerous race conditions in this routine, but we are not required
9402  * and can not be accurate.
9403  */
9404 /*ARGSUSED*/
9405 void
9406 rfs4_op_lockt(nfs_argop4 *argop, nfs_resop4 *resop,
9407     struct svc_req *req, struct compound_state *cs)
9408 {
9409         LOCKT4args *args = &argop->nfs_argop4_u.oplockt;
9410         LOCKT4res *resp = &resop->nfs_resop4_u.oplockt;
9411         rfs4_lockowner_t *lo;
9412         rfs4_client_t *cp;
9413         bool_t create = FALSE;
9414         struct flock64 flk;
9415         int error;
9416         int flag = FREAD | FWRITE;
9417         int ltype;
9418         length4 posix_length;
9419         sysid_t sysid;
9420         pid_t pid;
9421 
9422         DTRACE_NFSV4_2(op__lockt__start, struct compound_state *, cs,
9423             LOCKT4args *, args);
9424 
9425         if (cs->vp == NULL) {
9426                 *cs->statusp = resp->status = NFS4ERR_NOFILEHANDLE;
9427                 goto out;
9428         }
9429 
9430         /*
9431          * NFS4 only allows locking on regular files, so
9432          * verify type of object.
9433          */
9434         if (cs->vp->v_type != VREG) {
9435                 if (cs->vp->v_type == VDIR)
9436                         *cs->statusp = resp->status = NFS4ERR_ISDIR;
9437                 else
9438                         *cs->statusp = resp->status =  NFS4ERR_INVAL;
9439                 goto out;
9440         }
9441 
9442         /*
9443          * Check out the clientid to ensure the server knows about it
9444          * so that we correctly inform the client of a server reboot.
9445          */
9446         if ((cp = rfs4_findclient_by_id(args->owner.clientid, FALSE))
9447             == NULL) {
9448                 *cs->statusp = resp->status =
9449                     rfs4_check_clientid(&args->owner.clientid, 0);
9450                 goto out;
9451         }
9452         if (rfs4_lease_expired(cp)) {
9453                 rfs4_client_close(cp);
9454                 /*
9455                  * Protocol doesn't allow returning NFS4ERR_STALE as
9456                  * other operations do on this check so STALE_CLIENTID
9457                  * is returned instead
9458                  */
9459                 *cs->statusp = resp->status = NFS4ERR_STALE_CLIENTID;
9460                 goto out;
9461         }
9462 
9463         if (rfs4_clnt_in_grace(cp) && !(cp->rc_can_reclaim)) {
9464                 *cs->statusp = resp->status = NFS4ERR_GRACE;
9465                 rfs4_client_rele(cp);
9466                 goto out;
9467         }
9468         rfs4_client_rele(cp);
9469 
9470         resp->status = NFS4_OK;
9471 
9472         switch (args->locktype) {
9473         case READ_LT:
9474         case READW_LT:
9475                 ltype = F_RDLCK;
9476                 break;
9477         case WRITE_LT:
9478         case WRITEW_LT:
9479                 ltype = F_WRLCK;
9480                 break;
9481         }
9482 
9483         posix_length = args->length;
9484         /* Check for zero length. To lock to end of file use all ones for V4 */
9485         if (posix_length == 0) {
9486                 *cs->statusp = resp->status = NFS4ERR_INVAL;
9487                 goto out;
9488         } else if (posix_length == (length4)(~0)) {
9489                 posix_length = 0;       /* Posix to end of file  */
9490         }
9491 
9492         /* Find or create a lockowner */
9493         lo = rfs4_findlockowner(&args->owner, &create);
9494 
9495         if (lo) {
9496                 pid = lo->rl_pid;
9497                 if ((resp->status =
9498                     rfs4_client_sysid(lo->rl_client, &sysid)) != NFS4_OK)
9499                         goto err;
9500         } else {
9501                 pid = 0;
9502                 sysid = lockt_sysid;
9503         }
9504 retry:
9505         flk.l_type = ltype;
9506         flk.l_whence = 0;               /* SEEK_SET */
9507         flk.l_start = args->offset;
9508         flk.l_len = posix_length;
9509         flk.l_sysid = sysid;
9510         flk.l_pid = pid;
9511         flag |= F_REMOTELOCK;
9512 
9513         LOCK_PRINT(rfs4_debug, "rfs4_op_lockt", F_GETLK, &flk);
9514 
9515         /* Note that length4 is uint64_t but l_len and l_start are off64_t */
9516         if (flk.l_len < 0 || flk.l_start < 0) {
9517                 resp->status = NFS4ERR_INVAL;
9518                 goto err;
9519         }
9520         error = VOP_FRLOCK(cs->vp, F_GETLK, &flk, flag, (u_offset_t)0,
9521             NULL, cs->cr, NULL);
9522 
9523         /*
9524          * N.B. We map error values to nfsv4 errors. This is differrent
9525          * than puterrno4 routine.
9526          */
9527         switch (error) {
9528         case 0:
9529                 if (flk.l_type == F_UNLCK)
9530                         resp->status = NFS4_OK;
9531                 else {
9532                         if (lock_denied(&resp->denied, &flk) == NFS4ERR_EXPIRED)
9533                                 goto retry;
9534                         resp->status = NFS4ERR_DENIED;
9535                 }
9536                 break;
9537         case EOVERFLOW:
9538                 resp->status = NFS4ERR_INVAL;
9539                 break;
9540         case EINVAL:
9541                 resp->status = NFS4ERR_NOTSUPP;
9542                 break;
9543         default:
9544                 cmn_err(CE_WARN, "rfs4_op_lockt: unexpected errno (%d)",
9545                     error);
9546                 resp->status = NFS4ERR_SERVERFAULT;
9547                 break;
9548         }
9549 
9550 err:
9551         if (lo)
9552                 rfs4_lockowner_rele(lo);
9553         *cs->statusp = resp->status;
9554 out:
9555         DTRACE_NFSV4_2(op__lockt__done, struct compound_state *, cs,
9556             LOCKT4res *, resp);
9557 }
9558 
9559 int
9560 rfs4_share(rfs4_state_t *sp, uint32_t access, uint32_t deny)
9561 {
9562         int err;
9563         int cmd;
9564         vnode_t *vp;
9565         struct shrlock shr;
9566         struct shr_locowner shr_loco;
9567         int fflags = 0;
9568 
9569         ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9570         ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9571 
9572         if (sp->rs_closed)
9573                 return (NFS4ERR_OLD_STATEID);
9574 
9575         vp = sp->rs_finfo->rf_vp;
9576         ASSERT(vp);
9577 
9578         shr.s_access = shr.s_deny = 0;
9579 
9580         if (access & OPEN4_SHARE_ACCESS_READ) {
9581                 fflags |= FREAD;
9582                 shr.s_access |= F_RDACC;
9583         }
9584         if (access & OPEN4_SHARE_ACCESS_WRITE) {
9585                 fflags |= FWRITE;
9586                 shr.s_access |= F_WRACC;
9587         }
9588         ASSERT(shr.s_access);
9589 
9590         if (deny & OPEN4_SHARE_DENY_READ)
9591                 shr.s_deny |= F_RDDNY;
9592         if (deny & OPEN4_SHARE_DENY_WRITE)
9593                 shr.s_deny |= F_WRDNY;
9594 
9595         shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9596         shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9597         shr_loco.sl_pid = shr.s_pid;
9598         shr_loco.sl_id = shr.s_sysid;
9599         shr.s_owner = (caddr_t)&shr_loco;
9600         shr.s_own_len = sizeof (shr_loco);
9601 
9602         cmd = nbl_need_check(vp) ? F_SHARE_NBMAND : F_SHARE;
9603 
9604         err = VOP_SHRLOCK(vp, cmd, &shr, fflags, CRED(), NULL);
9605         if (err != 0) {
9606                 if (err == EAGAIN)
9607                         err = NFS4ERR_SHARE_DENIED;
9608                 else
9609                         err = puterrno4(err);
9610                 return (err);
9611         }
9612 
9613         sp->rs_share_access |= access;
9614         sp->rs_share_deny |= deny;
9615 
9616         return (0);
9617 }
9618 
9619 int
9620 rfs4_unshare(rfs4_state_t *sp)
9621 {
9622         int err;
9623         struct shrlock shr;
9624         struct shr_locowner shr_loco;
9625 
9626         ASSERT(rfs4_dbe_islocked(sp->rs_dbe));
9627 
9628         if (sp->rs_closed || sp->rs_share_access == 0)
9629                 return (0);
9630 
9631         ASSERT(sp->rs_owner->ro_client->rc_sysidt != LM_NOSYSID);
9632         ASSERT(sp->rs_finfo->rf_vp);
9633 
9634         shr.s_access = shr.s_deny = 0;
9635         shr.s_pid = rfs4_dbe_getid(sp->rs_owner->ro_dbe);
9636         shr.s_sysid = sp->rs_owner->ro_client->rc_sysidt;
9637         shr_loco.sl_pid = shr.s_pid;
9638         shr_loco.sl_id = shr.s_sysid;
9639         shr.s_owner = (caddr_t)&shr_loco;
9640         shr.s_own_len = sizeof (shr_loco);
9641 
9642         err = VOP_SHRLOCK(sp->rs_finfo->rf_vp, F_UNSHARE, &shr, 0, CRED(),
9643             NULL);
9644         if (err != 0) {
9645                 err = puterrno4(err);
9646                 return (err);
9647         }
9648 
9649         sp->rs_share_access = 0;
9650         sp->rs_share_deny = 0;
9651 
9652         return (0);
9653 
9654 }
9655 
9656 static int
9657 rdma_setup_read_data4(READ4args *args, READ4res *rok)
9658 {
9659         struct clist    *wcl;
9660         count4          count = rok->data_len;
9661         int             wlist_len;
9662 
9663         wcl = args->wlist;
9664         if (rdma_setup_read_chunks(wcl, count, &wlist_len) == FALSE) {
9665                 return (FALSE);
9666         }
9667         wcl = args->wlist;
9668         rok->wlist_len = wlist_len;
9669         rok->wlist = wcl;
9670         return (TRUE);
9671 }
9672 
9673 /* tunable to disable server referrals */
9674 int rfs4_no_referrals = 0;
9675 
9676 /*
9677  * Find an NFS record in reparse point data.
9678  * Returns 0 for success and <0 or an errno value on failure.
9679  */
9680 int
9681 vn_find_nfs_record(vnode_t *vp, nvlist_t **nvlp, char **svcp, char **datap)
9682 {
9683         int err;
9684         char *stype, *val;
9685         nvlist_t *nvl;
9686         nvpair_t *curr;
9687 
9688         if ((nvl = reparse_init()) == NULL)
9689                 return (-1);
9690 
9691         if ((err = reparse_vnode_parse(vp, nvl)) != 0) {
9692                 reparse_free(nvl);
9693                 return (err);
9694         }
9695 
9696         curr = NULL;
9697         while ((curr = nvlist_next_nvpair(nvl, curr)) != NULL) {
9698                 if ((stype = nvpair_name(curr)) == NULL) {
9699                         reparse_free(nvl);
9700                         return (-2);
9701                 }
9702                 if (strncasecmp(stype, "NFS", 3) == 0)
9703                         break;
9704         }
9705 
9706         if ((curr == NULL) ||
9707             (nvpair_value_string(curr, &val))) {
9708                 reparse_free(nvl);
9709                 return (-3);
9710         }
9711         *nvlp = nvl;
9712         *svcp = stype;
9713         *datap = val;
9714         return (0);
9715 }
9716 
9717 int
9718 vn_is_nfs_reparse(vnode_t *vp, cred_t *cr)
9719 {
9720         nvlist_t *nvl;
9721         char *s, *d;
9722 
9723         if (rfs4_no_referrals != 0)
9724                 return (B_FALSE);
9725 
9726         if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
9727                 return (B_FALSE);
9728 
9729         if (vn_find_nfs_record(vp, &nvl, &s, &d) != 0)
9730                 return (B_FALSE);
9731 
9732         reparse_free(nvl);
9733 
9734         return (B_TRUE);
9735 }
9736 
9737 /*
9738  * There is a user-level copy of this routine in ref_subr.c.
9739  * Changes should be kept in sync.
9740  */
9741 static int
9742 nfs4_create_components(char *path, component4 *comp4)
9743 {
9744         int slen, plen, ncomp;
9745         char *ori_path, *nxtc, buf[MAXNAMELEN];
9746 
9747         if (path == NULL)
9748                 return (0);
9749 
9750         plen = strlen(path) + 1;        /* include the terminator */
9751         ori_path = path;
9752         ncomp = 0;
9753 
9754         /* count number of components in the path */
9755         for (nxtc = path; nxtc < ori_path + plen; nxtc++) {
9756                 if (*nxtc == '/' || *nxtc == '\0' || *nxtc == '\n') {
9757                         if ((slen = nxtc - path) == 0) {
9758                                 path = nxtc + 1;
9759                                 continue;
9760                         }
9761 
9762                         if (comp4 != NULL) {
9763                                 bcopy(path, buf, slen);
9764                                 buf[slen] = '\0';
9765                                 (void) str_to_utf8(buf, &comp4[ncomp]);
9766                         }
9767 
9768                         ncomp++;        /* 1 valid component */
9769                         path = nxtc + 1;
9770                 }
9771                 if (*nxtc == '\0' || *nxtc == '\n')
9772                         break;
9773         }
9774 
9775         return (ncomp);
9776 }
9777 
9778 /*
9779  * There is a user-level copy of this routine in ref_subr.c.
9780  * Changes should be kept in sync.
9781  */
9782 static int
9783 make_pathname4(char *path, pathname4 *pathname)
9784 {
9785         int ncomp;
9786         component4 *comp4;
9787 
9788         if (pathname == NULL)
9789                 return (0);
9790 
9791         if (path == NULL) {
9792                 pathname->pathname4_val = NULL;
9793                 pathname->pathname4_len = 0;
9794                 return (0);
9795         }
9796 
9797         /* count number of components to alloc buffer */
9798         if ((ncomp = nfs4_create_components(path, NULL)) == 0) {
9799                 pathname->pathname4_val = NULL;
9800                 pathname->pathname4_len = 0;
9801                 return (0);
9802         }
9803         comp4 = kmem_zalloc(ncomp * sizeof (component4), KM_SLEEP);
9804 
9805         /* copy components into allocated buffer */
9806         ncomp = nfs4_create_components(path, comp4);
9807 
9808         pathname->pathname4_val = comp4;
9809         pathname->pathname4_len = ncomp;
9810 
9811         return (ncomp);
9812 }
9813 
9814 #define xdr_fs_locations4 xdr_fattr4_fs_locations
9815 
9816 fs_locations4 *
9817 fetch_referral(vnode_t *vp, cred_t *cr)
9818 {
9819         nvlist_t *nvl;
9820         char *stype, *sdata;
9821         fs_locations4 *result;
9822         char buf[1024];
9823         size_t bufsize;
9824         XDR xdr;
9825         int err;
9826 
9827         /*
9828          * Check attrs to ensure it's a reparse point
9829          */
9830         if (vn_is_reparse(vp, cr, NULL) == B_FALSE)
9831                 return (NULL);
9832 
9833         /*
9834          * Look for an NFS record and get the type and data
9835          */
9836         if (vn_find_nfs_record(vp, &nvl, &stype, &sdata) != 0)
9837                 return (NULL);
9838 
9839         /*
9840          * With the type and data, upcall to get the referral
9841          */
9842         bufsize = sizeof (buf);
9843         bzero(buf, sizeof (buf));
9844         err = reparse_kderef((const char *)stype, (const char *)sdata,
9845             buf, &bufsize);
9846         reparse_free(nvl);
9847 
9848         DTRACE_PROBE4(nfs4serv__func__referral__upcall,
9849             char *, stype, char *, sdata, char *, buf, int, err);
9850         if (err) {
9851                 cmn_err(CE_NOTE,
9852                     "reparsed daemon not running: unable to get referral (%d)",
9853                     err);
9854                 return (NULL);
9855         }
9856 
9857         /*
9858          * We get an XDR'ed record back from the kderef call
9859          */
9860         xdrmem_create(&xdr, buf, bufsize, XDR_DECODE);
9861         result = kmem_alloc(sizeof (fs_locations4), KM_SLEEP);
9862         err = xdr_fs_locations4(&xdr, result);
9863         XDR_DESTROY(&xdr);
9864         if (err != TRUE) {
9865                 DTRACE_PROBE1(nfs4serv__func__referral__upcall__xdrfail,
9866                     int, err);
9867                 return (NULL);
9868         }
9869 
9870         /*
9871          * Look at path to recover fs_root, ignoring the leading '/'
9872          */
9873         (void) make_pathname4(vp->v_path, &result->fs_root);
9874 
9875         return (result);
9876 }
9877 
9878 char *
9879 build_symlink(vnode_t *vp, cred_t *cr, size_t *strsz)
9880 {
9881         fs_locations4 *fsl;
9882         fs_location4 *fs;
9883         char *server, *path, *symbuf;
9884         static char *prefix = "/net/";
9885         int i, size, npaths;
9886         uint_t len;
9887 
9888         /* Get the referral */
9889         if ((fsl = fetch_referral(vp, cr)) == NULL)
9890                 return (NULL);
9891 
9892         /* Deal with only the first location and first server */
9893         fs = &fsl->locations_val[0];
9894         server = utf8_to_str(&fs->server_val[0], &len, NULL);
9895         if (server == NULL) {
9896                 rfs4_free_fs_locations4(fsl);
9897                 kmem_free(fsl, sizeof (fs_locations4));
9898                 return (NULL);
9899         }
9900 
9901         /* Figure out size for "/net/" + host + /path/path/path + NULL */
9902         size = strlen(prefix) + len;
9903         for (i = 0; i < fs->rootpath.pathname4_len; i++)
9904                 size += fs->rootpath.pathname4_val[i].utf8string_len + 1;
9905 
9906         /* Allocate the symlink buffer and fill it */
9907         symbuf = kmem_zalloc(size, KM_SLEEP);
9908         (void) strcat(symbuf, prefix);
9909         (void) strcat(symbuf, server);
9910         kmem_free(server, len);
9911 
9912         npaths = 0;
9913         for (i = 0; i < fs->rootpath.pathname4_len; i++) {
9914                 path = utf8_to_str(&fs->rootpath.pathname4_val[i], &len, NULL);
9915                 if (path == NULL)
9916                         continue;
9917                 (void) strcat(symbuf, "/");
9918                 (void) strcat(symbuf, path);
9919                 npaths++;
9920                 kmem_free(path, len);
9921         }
9922 
9923         rfs4_free_fs_locations4(fsl);
9924         kmem_free(fsl, sizeof (fs_locations4));
9925 
9926         if (strsz != NULL)
9927                 *strsz = size;
9928         return (symbuf);
9929 }
9930 
9931 /*
9932  * Check to see if we have a downrev Solaris client, so that we
9933  * can send it a symlink instead of a referral.
9934  */
9935 int
9936 client_is_downrev(struct svc_req *req)
9937 {
9938         struct sockaddr *ca;
9939         rfs4_clntip_t *ci;
9940         bool_t create = FALSE;
9941         int is_downrev;
9942 
9943         ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
9944         ASSERT(ca);
9945         ci = rfs4_find_clntip(ca, &create);
9946         if (ci == NULL)
9947                 return (0);
9948         is_downrev = ci->ri_no_referrals;
9949         rfs4_dbe_rele(ci->ri_dbe);
9950         return (is_downrev);
9951 }
9952 
9953 /*
9954  * Do the main work of handling HA-NFSv4 Resource Group failover on
9955  * Sun Cluster.
9956  * We need to detect whether any RG admin paths have been added or removed,
9957  * and adjust resources accordingly.
9958  * Currently we're using a very inefficient algorithm, ~ 2 * O(n**2). In
9959  * order to scale, the list and array of paths need to be held in more
9960  * suitable data structures.
9961  */
9962 static void
9963 hanfsv4_failover(nfs4_srv_t *nsrv4)
9964 {
9965         int i, start_grace, numadded_paths = 0;
9966         char **added_paths = NULL;
9967         rfs4_dss_path_t *dss_path;
9968 
9969         /*
9970          * Note: currently, dss_pathlist cannot be NULL, since
9971          * it will always include an entry for NFS4_DSS_VAR_DIR. If we
9972          * make the latter dynamically specified too, the following will
9973          * need to be adjusted.
9974          */
9975 
9976         /*
9977          * First, look for removed paths: RGs that have been failed-over
9978          * away from this node.
9979          * Walk the "currently-serving" dss_pathlist and, for each
9980          * path, check if it is on the "passed-in" rfs4_dss_newpaths array
9981          * from nfsd. If not, that RG path has been removed.
9982          *
9983          * Note that nfsd has sorted rfs4_dss_newpaths for us, and removed
9984          * any duplicates.
9985          */
9986         dss_path = nsrv4->dss_pathlist;
9987         do {
9988                 int found = 0;
9989                 char *path = dss_path->path;
9990 
9991                 /* used only for non-HA so may not be removed */
9992                 if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
9993                         dss_path = dss_path->next;
9994                         continue;
9995                 }
9996 
9997                 for (i = 0; i < rfs4_dss_numnewpaths; i++) {
9998                         int cmpret;
9999                         char *newpath = rfs4_dss_newpaths[i];
10000 
10001                         /*
10002                          * Since nfsd has sorted rfs4_dss_newpaths for us,
10003                          * once the return from strcmp is negative we know
10004                          * we've passed the point where "path" should be,
10005                          * and can stop searching: "path" has been removed.
10006                          */
10007                         cmpret = strcmp(path, newpath);
10008                         if (cmpret < 0)
10009                                 break;
10010                         if (cmpret == 0) {
10011                                 found = 1;
10012                                 break;
10013                         }
10014                 }
10015 
10016                 if (found == 0) {
10017                         unsigned index = dss_path->index;
10018                         rfs4_servinst_t *sip = dss_path->sip;
10019                         rfs4_dss_path_t *path_next = dss_path->next;
10020 
10021                         /*
10022                          * This path has been removed.
10023                          * We must clear out the servinst reference to
10024                          * it, since it's now owned by another
10025                          * node: we should not attempt to touch it.
10026                          */
10027                         ASSERT(dss_path == sip->dss_paths[index]);
10028                         sip->dss_paths[index] = NULL;
10029 
10030                         /* remove from "currently-serving" list, and destroy */
10031                         remque(dss_path);
10032                         /* allow for NUL */
10033                         kmem_free(dss_path->path, strlen(dss_path->path) + 1);
10034                         kmem_free(dss_path, sizeof (rfs4_dss_path_t));
10035 
10036                         dss_path = path_next;
10037                 } else {
10038                         /* path was found; not removed */
10039                         dss_path = dss_path->next;
10040                 }
10041         } while (dss_path != nsrv4->dss_pathlist);
10042 
10043         /*
10044          * Now, look for added paths: RGs that have been failed-over
10045          * to this node.
10046          * Walk the "passed-in" rfs4_dss_newpaths array from nfsd and,
10047          * for each path, check if it is on the "currently-serving"
10048          * dss_pathlist. If not, that RG path has been added.
10049          *
10050          * Note: we don't do duplicate detection here; nfsd does that for us.
10051          *
10052          * Note: numadded_paths <= rfs4_dss_numnewpaths, which gives us
10053          * an upper bound for the size needed for added_paths[numadded_paths].
10054          */
10055 
10056         /* probably more space than we need, but guaranteed to be enough */
10057         if (rfs4_dss_numnewpaths > 0) {
10058                 size_t sz = rfs4_dss_numnewpaths * sizeof (char *);
10059                 added_paths = kmem_zalloc(sz, KM_SLEEP);
10060         }
10061 
10062         /* walk the "passed-in" rfs4_dss_newpaths array from nfsd */
10063         for (i = 0; i < rfs4_dss_numnewpaths; i++) {
10064                 int found = 0;
10065                 char *newpath = rfs4_dss_newpaths[i];
10066 
10067                 dss_path = nsrv4->dss_pathlist;
10068                 do {
10069                         char *path = dss_path->path;
10070 
10071                         /* used only for non-HA */
10072                         if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
10073                                 dss_path = dss_path->next;
10074                                 continue;
10075                         }
10076 
10077                         if (strncmp(path, newpath, strlen(path)) == 0) {
10078                                 found = 1;
10079                                 break;
10080                         }
10081 
10082                         dss_path = dss_path->next;
10083                 } while (dss_path != nsrv4->dss_pathlist);
10084 
10085                 if (found == 0) {
10086                         added_paths[numadded_paths] = newpath;
10087                         numadded_paths++;
10088                 }
10089         }
10090 
10091         /* did we find any added paths? */
10092         if (numadded_paths > 0) {
10093 
10094                 /* create a new server instance, and start its grace period */
10095                 start_grace = 1;
10096                 /* CSTYLED */
10097                 rfs4_servinst_create(nsrv4, start_grace, numadded_paths, added_paths);
10098 
10099                 /* read in the stable storage state from these paths */
10100                 rfs4_dss_readstate(nsrv4, numadded_paths, added_paths);
10101 
10102                 /*
10103                  * Multiple failovers during a grace period will cause
10104                  * clients of the same resource group to be partitioned
10105                  * into different server instances, with different
10106                  * grace periods.  Since clients of the same resource
10107                  * group must be subject to the same grace period,
10108                  * we need to reset all currently active grace periods.
10109                  */
10110                 rfs4_grace_reset_all(nsrv4);
10111         }
10112 
10113         if (rfs4_dss_numnewpaths > 0)
10114                 kmem_free(added_paths, rfs4_dss_numnewpaths * sizeof (char *));
10115 }