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 2018 Nexenta Systems, Inc.
  28  * Copyright 2019 Nexenta by DDN, Inc.
  29  */
  30 
  31 #include <sys/systm.h>
  32 #include <sys/kmem.h>
  33 #include <sys/cmn_err.h>
  34 #include <sys/atomic.h>
  35 #include <sys/clconf.h>
  36 #include <sys/cladm.h>
  37 #include <sys/flock.h>
  38 #include <nfs/export.h>
  39 #include <nfs/nfs.h>
  40 #include <nfs/nfs4.h>
  41 #include <nfs/nfssys.h>
  42 #include <nfs/lm.h>
  43 #include <sys/pathname.h>
  44 #include <sys/sdt.h>
  45 #include <sys/nvpair.h>
  46 
  47 extern u_longlong_t nfs4_srv_caller_id;
  48 
  49 extern uint_t nfs4_srv_vkey;
  50 
  51 stateid4 special0 = {
  52         0,
  53         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  54 };
  55 
  56 stateid4 special1 = {
  57         0xffffffff,
  58         {
  59                 (char)0xff, (char)0xff, (char)0xff, (char)0xff,
  60                 (char)0xff, (char)0xff, (char)0xff, (char)0xff,
  61                 (char)0xff, (char)0xff, (char)0xff, (char)0xff
  62         }
  63 };
  64 
  65 
  66 #define ISSPECIAL(id)  (stateid4_cmp(id, &special0) || \
  67                         stateid4_cmp(id, &special1))
  68 
  69 /* For embedding the cluster nodeid into our clientid */
  70 #define CLUSTER_NODEID_SHIFT    24
  71 #define CLUSTER_MAX_NODEID      255
  72 
  73 #ifdef DEBUG
  74 int rfs4_debug;
  75 #endif
  76 
  77 static uint32_t rfs4_database_debug = 0x00;
  78 
  79 /* CSTYLED */
  80 static void rfs4_ss_clid_write(nfs4_srv_t *nsrv4, rfs4_client_t *cp, char *leaf);
  81 static void rfs4_ss_clid_write_one(rfs4_client_t *cp, char *dir, char *leaf);
  82 static void rfs4_dss_clear_oldstate(rfs4_servinst_t *sip);
  83 static void rfs4_ss_chkclid_sip(rfs4_client_t *cp, rfs4_servinst_t *sip);
  84 
  85 /*
  86  * Couple of simple init/destroy functions for a general waiter
  87  */
  88 void
  89 rfs4_sw_init(rfs4_state_wait_t *swp)
  90 {
  91         mutex_init(swp->sw_cv_lock, NULL, MUTEX_DEFAULT, NULL);
  92         cv_init(swp->sw_cv, NULL, CV_DEFAULT, NULL);
  93         swp->sw_active = FALSE;
  94         swp->sw_wait_count = 0;
  95 }
  96 
  97 void
  98 rfs4_sw_destroy(rfs4_state_wait_t *swp)
  99 {
 100         mutex_destroy(swp->sw_cv_lock);
 101         cv_destroy(swp->sw_cv);
 102 }
 103 
 104 void
 105 rfs4_sw_enter(rfs4_state_wait_t *swp)
 106 {
 107         mutex_enter(swp->sw_cv_lock);
 108         while (swp->sw_active) {
 109                 swp->sw_wait_count++;
 110                 cv_wait(swp->sw_cv, swp->sw_cv_lock);
 111                 swp->sw_wait_count--;
 112         }
 113         ASSERT(swp->sw_active == FALSE);
 114         swp->sw_active = TRUE;
 115         mutex_exit(swp->sw_cv_lock);
 116 }
 117 
 118 void
 119 rfs4_sw_exit(rfs4_state_wait_t *swp)
 120 {
 121         mutex_enter(swp->sw_cv_lock);
 122         ASSERT(swp->sw_active == TRUE);
 123         swp->sw_active = FALSE;
 124         if (swp->sw_wait_count != 0)
 125                 cv_broadcast(swp->sw_cv);
 126         mutex_exit(swp->sw_cv_lock);
 127 }
 128 
 129 static void
 130 deep_lock_copy(LOCK4res *dres, LOCK4res *sres)
 131 {
 132         lock_owner4 *slo = &sres->LOCK4res_u.denied.owner;
 133         lock_owner4 *dlo = &dres->LOCK4res_u.denied.owner;
 134 
 135         if (sres->status == NFS4ERR_DENIED) {
 136                 dlo->owner_val = kmem_alloc(slo->owner_len, KM_SLEEP);
 137                 bcopy(slo->owner_val, dlo->owner_val, slo->owner_len);
 138         }
 139 }
 140 
 141 /*
 142  * CPR callback id -- not related to v4 callbacks
 143  */
 144 static callb_id_t cpr_id = 0;
 145 
 146 static void
 147 deep_lock_free(LOCK4res *res)
 148 {
 149         lock_owner4 *lo = &res->LOCK4res_u.denied.owner;
 150 
 151         if (res->status == NFS4ERR_DENIED)
 152                 kmem_free(lo->owner_val, lo->owner_len);
 153 }
 154 
 155 static void
 156 deep_open_copy(OPEN4res *dres, OPEN4res *sres)
 157 {
 158         nfsace4 *sacep, *dacep;
 159 
 160         if (sres->status != NFS4_OK) {
 161                 return;
 162         }
 163 
 164         dres->attrset = sres->attrset;
 165 
 166         switch (sres->delegation.delegation_type) {
 167         case OPEN_DELEGATE_NONE:
 168                 return;
 169         case OPEN_DELEGATE_READ:
 170                 sacep = &sres->delegation.open_delegation4_u.read.permissions;
 171                 dacep = &dres->delegation.open_delegation4_u.read.permissions;
 172                 break;
 173         case OPEN_DELEGATE_WRITE:
 174                 sacep = &sres->delegation.open_delegation4_u.write.permissions;
 175                 dacep = &dres->delegation.open_delegation4_u.write.permissions;
 176                 break;
 177         }
 178         dacep->who.utf8string_val =
 179             kmem_alloc(sacep->who.utf8string_len, KM_SLEEP);
 180         bcopy(sacep->who.utf8string_val, dacep->who.utf8string_val,
 181             sacep->who.utf8string_len);
 182 }
 183 
 184 static void
 185 deep_open_free(OPEN4res *res)
 186 {
 187         nfsace4 *acep;
 188         if (res->status != NFS4_OK)
 189                 return;
 190 
 191         switch (res->delegation.delegation_type) {
 192         case OPEN_DELEGATE_NONE:
 193                 return;
 194         case OPEN_DELEGATE_READ:
 195                 acep = &res->delegation.open_delegation4_u.read.permissions;
 196                 break;
 197         case OPEN_DELEGATE_WRITE:
 198                 acep = &res->delegation.open_delegation4_u.write.permissions;
 199                 break;
 200         }
 201 
 202         if (acep->who.utf8string_val) {
 203                 kmem_free(acep->who.utf8string_val, acep->who.utf8string_len);
 204                 acep->who.utf8string_val = NULL;
 205         }
 206 }
 207 
 208 void
 209 rfs4_free_reply(nfs_resop4 *rp)
 210 {
 211         switch (rp->resop) {
 212         case OP_LOCK:
 213                 deep_lock_free(&rp->nfs_resop4_u.oplock);
 214                 break;
 215         case OP_OPEN:
 216                 deep_open_free(&rp->nfs_resop4_u.opopen);
 217         default:
 218                 break;
 219         }
 220 }
 221 
 222 void
 223 rfs4_copy_reply(nfs_resop4 *dst, nfs_resop4 *src)
 224 {
 225         *dst = *src;
 226 
 227         /* Handle responses that need deep copy */
 228         switch (src->resop) {
 229         case OP_LOCK:
 230                 deep_lock_copy(&dst->nfs_resop4_u.oplock,
 231                     &src->nfs_resop4_u.oplock);
 232                 break;
 233         case OP_OPEN:
 234                 deep_open_copy(&dst->nfs_resop4_u.opopen,
 235                     &src->nfs_resop4_u.opopen);
 236                 break;
 237         default:
 238                 break;
 239         };
 240 }
 241 
 242 /*
 243  * This is the implementation of the underlying state engine. The
 244  * public interface to this engine is described by
 245  * nfs4_state.h. Callers to the engine should hold no state engine
 246  * locks when they call in to it. If the protocol needs to lock data
 247  * structures it should do so after acquiring all references to them
 248  * first and then follow the following lock order:
 249  *
 250  *      client > openowner > state > lo_state > lockowner > file.
 251  *
 252  * Internally we only allow a thread to hold one hash bucket lock at a
 253  * time and the lock is higher in the lock order (must be acquired
 254  * first) than the data structure that is on that hash list.
 255  *
 256  * If a new reference was acquired by the caller, that reference needs
 257  * to be released after releasing all acquired locks with the
 258  * corresponding rfs4_*_rele routine.
 259  */
 260 
 261 /*
 262  * This code is some what prototypical for now. Its purpose currently is to
 263  * implement the interfaces sufficiently to finish the higher protocol
 264  * elements. This will be replaced by a dynamically resizeable tables
 265  * backed by kmem_cache allocator. However synchronization is handled
 266  * correctly (I hope) and will not change by much.  The mutexes for
 267  * the hash buckets that can be used to create new instances of data
 268  * structures  might be good candidates to evolve into reader writer
 269  * locks. If it has to do a creation, it would be holding the
 270  * mutex across a kmem_alloc with KM_SLEEP specified.
 271  */
 272 
 273 #ifdef DEBUG
 274 #define TABSIZE 17
 275 #else
 276 #define TABSIZE 2047
 277 #endif
 278 
 279 #define ADDRHASH(key) ((unsigned long)(key) >> 3)
 280 
 281 #define MAXTABSZ 1024*1024
 282 
 283 /* The values below are rfs4_lease_time units */
 284 
 285 #ifdef DEBUG
 286 #define CLIENT_CACHE_TIME 1
 287 #define OPENOWNER_CACHE_TIME 1
 288 #define STATE_CACHE_TIME 1
 289 #define LO_STATE_CACHE_TIME 1
 290 #define LOCKOWNER_CACHE_TIME 1
 291 #define FILE_CACHE_TIME 3
 292 #define DELEG_STATE_CACHE_TIME 1
 293 #else
 294 #define CLIENT_CACHE_TIME 10
 295 #define OPENOWNER_CACHE_TIME 5
 296 #define STATE_CACHE_TIME 1
 297 #define LO_STATE_CACHE_TIME 1
 298 #define LOCKOWNER_CACHE_TIME 3
 299 #define FILE_CACHE_TIME 40
 300 #define DELEG_STATE_CACHE_TIME 1
 301 #endif
 302 
 303 /*
 304  * NFSv4 server state databases
 305  *
 306  * Initilized when the module is loaded and used by NFSv4 state tables.
 307  * These kmem_cache databases are global, the tables that make use of these
 308  * are per zone.
 309  */
 310 kmem_cache_t *rfs4_client_mem_cache;
 311 kmem_cache_t *rfs4_clntIP_mem_cache;
 312 kmem_cache_t *rfs4_openown_mem_cache;
 313 kmem_cache_t *rfs4_openstID_mem_cache;
 314 kmem_cache_t *rfs4_lockstID_mem_cache;
 315 kmem_cache_t *rfs4_lockown_mem_cache;
 316 kmem_cache_t *rfs4_file_mem_cache;
 317 kmem_cache_t *rfs4_delegstID_mem_cache;
 318 
 319 /*
 320  * NFSv4 state table functions
 321  */
 322 static bool_t rfs4_client_create(rfs4_entry_t, void *);
 323 static void rfs4_dss_remove_cpleaf(rfs4_client_t *);
 324 static void rfs4_dss_remove_leaf(rfs4_servinst_t *, char *, char *);
 325 static void rfs4_client_destroy(rfs4_entry_t);
 326 static bool_t rfs4_client_expiry(rfs4_entry_t);
 327 static uint32_t clientid_hash(void *);
 328 static bool_t clientid_compare(rfs4_entry_t, void *);
 329 static void *clientid_mkkey(rfs4_entry_t);
 330 static uint32_t nfsclnt_hash(void *);
 331 static bool_t nfsclnt_compare(rfs4_entry_t, void *);
 332 static void *nfsclnt_mkkey(rfs4_entry_t);
 333 static bool_t rfs4_clntip_expiry(rfs4_entry_t);
 334 static void rfs4_clntip_destroy(rfs4_entry_t);
 335 static bool_t rfs4_clntip_create(rfs4_entry_t, void *);
 336 static uint32_t clntip_hash(void *);
 337 static bool_t clntip_compare(rfs4_entry_t, void *);
 338 static void *clntip_mkkey(rfs4_entry_t);
 339 static bool_t rfs4_openowner_create(rfs4_entry_t, void *);
 340 static void rfs4_openowner_destroy(rfs4_entry_t);
 341 static bool_t rfs4_openowner_expiry(rfs4_entry_t);
 342 static uint32_t openowner_hash(void *);
 343 static bool_t openowner_compare(rfs4_entry_t, void *);
 344 static void *openowner_mkkey(rfs4_entry_t);
 345 static bool_t rfs4_state_create(rfs4_entry_t, void *);
 346 static void rfs4_state_destroy(rfs4_entry_t);
 347 static bool_t rfs4_state_expiry(rfs4_entry_t);
 348 static uint32_t state_hash(void *);
 349 static bool_t state_compare(rfs4_entry_t, void *);
 350 static void *state_mkkey(rfs4_entry_t);
 351 static uint32_t state_owner_file_hash(void *);
 352 static bool_t state_owner_file_compare(rfs4_entry_t, void *);
 353 static void *state_owner_file_mkkey(rfs4_entry_t);
 354 static uint32_t state_file_hash(void *);
 355 static bool_t state_file_compare(rfs4_entry_t, void *);
 356 static void *state_file_mkkey(rfs4_entry_t);
 357 static bool_t rfs4_lo_state_create(rfs4_entry_t, void *);
 358 static void rfs4_lo_state_destroy(rfs4_entry_t);
 359 static bool_t rfs4_lo_state_expiry(rfs4_entry_t);
 360 static uint32_t lo_state_hash(void *);
 361 static bool_t lo_state_compare(rfs4_entry_t, void *);
 362 static void *lo_state_mkkey(rfs4_entry_t);
 363 static uint32_t lo_state_lo_hash(void *);
 364 static bool_t lo_state_lo_compare(rfs4_entry_t, void *);
 365 static void *lo_state_lo_mkkey(rfs4_entry_t);
 366 static bool_t rfs4_lockowner_create(rfs4_entry_t, void *);
 367 static void rfs4_lockowner_destroy(rfs4_entry_t);
 368 static bool_t rfs4_lockowner_expiry(rfs4_entry_t);
 369 static uint32_t lockowner_hash(void *);
 370 static bool_t lockowner_compare(rfs4_entry_t, void *);
 371 static void *lockowner_mkkey(rfs4_entry_t);
 372 static uint32_t pid_hash(void *);
 373 static bool_t pid_compare(rfs4_entry_t, void *);
 374 static void *pid_mkkey(rfs4_entry_t);
 375 static bool_t rfs4_file_create(rfs4_entry_t, void *);
 376 static void rfs4_file_destroy(rfs4_entry_t);
 377 static uint32_t file_hash(void *);
 378 static bool_t file_compare(rfs4_entry_t, void *);
 379 static void *file_mkkey(rfs4_entry_t);
 380 static bool_t rfs4_deleg_state_create(rfs4_entry_t, void *);
 381 static void rfs4_deleg_state_destroy(rfs4_entry_t);
 382 static bool_t rfs4_deleg_state_expiry(rfs4_entry_t);
 383 static uint32_t deleg_hash(void *);
 384 static bool_t deleg_compare(rfs4_entry_t, void *);
 385 static void *deleg_mkkey(rfs4_entry_t);
 386 static uint32_t deleg_state_hash(void *);
 387 static bool_t deleg_state_compare(rfs4_entry_t, void *);
 388 static void *deleg_state_mkkey(rfs4_entry_t);
 389 
 390 static void rfs4_state_rele_nounlock(rfs4_state_t *);
 391 
 392 static int rfs4_ss_enabled = 0;
 393 
 394 extern void (*rfs4_client_clrst)(struct nfs4clrst_args *);
 395 
 396 void
 397 rfs4_ss_pnfree(rfs4_ss_pn_t *ss_pn)
 398 {
 399         kmem_free(ss_pn, sizeof (rfs4_ss_pn_t));
 400 }
 401 
 402 static rfs4_ss_pn_t *
 403 rfs4_ss_pnalloc(char *dir, char *leaf)
 404 {
 405         rfs4_ss_pn_t *ss_pn;
 406         int     dir_len, leaf_len;
 407 
 408         /*
 409          * validate we have a resonable path
 410          * (account for the '/' and trailing null)
 411          */
 412         if ((dir_len = strlen(dir)) > MAXPATHLEN ||
 413             (leaf_len = strlen(leaf)) > MAXNAMELEN ||
 414             (dir_len + leaf_len + 2) > MAXPATHLEN) {
 415                 return (NULL);
 416         }
 417 
 418         ss_pn = kmem_alloc(sizeof (rfs4_ss_pn_t), KM_SLEEP);
 419 
 420         (void) snprintf(ss_pn->pn, MAXPATHLEN, "%s/%s", dir, leaf);
 421         /* Handy pointer to just the leaf name */
 422         ss_pn->leaf = ss_pn->pn + dir_len + 1;
 423         return (ss_pn);
 424 }
 425 
 426 
 427 /*
 428  * Move the "leaf" filename from "sdir" directory
 429  * to the "ddir" directory. Return the pathname of
 430  * the destination unless the rename fails in which
 431  * case we need to return the source pathname.
 432  */
 433 static rfs4_ss_pn_t *
 434 rfs4_ss_movestate(char *sdir, char *ddir, char *leaf)
 435 {
 436         rfs4_ss_pn_t *src, *dst;
 437 
 438         if ((src = rfs4_ss_pnalloc(sdir, leaf)) == NULL)
 439                 return (NULL);
 440 
 441         if ((dst = rfs4_ss_pnalloc(ddir, leaf)) == NULL) {
 442                 rfs4_ss_pnfree(src);
 443                 return (NULL);
 444         }
 445 
 446         /*
 447          * If the rename fails we shall return the src
 448          * pathname and free the dst. Otherwise we need
 449          * to free the src and return the dst pathanme.
 450          */
 451         if (vn_rename(src->pn, dst->pn, UIO_SYSSPACE)) {
 452                 rfs4_ss_pnfree(dst);
 453                 return (src);
 454         }
 455         rfs4_ss_pnfree(src);
 456         return (dst);
 457 }
 458 
 459 
 460 static rfs4_oldstate_t *
 461 rfs4_ss_getstate(vnode_t *dvp, rfs4_ss_pn_t *ss_pn)
 462 {
 463         struct uio uio;
 464         struct iovec iov[3];
 465 
 466         rfs4_oldstate_t *cl_ss = NULL;
 467         vnode_t *vp;
 468         vattr_t va;
 469         uint_t id_len;
 470         int err, kill_file, file_vers;
 471 
 472         if (ss_pn == NULL)
 473                 return (NULL);
 474 
 475         /*
 476          * open the state file.
 477          */
 478         if (vn_open(ss_pn->pn, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0) != 0) {
 479                 return (NULL);
 480         }
 481 
 482         if (vp->v_type != VREG) {
 483                 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
 484                 VN_RELE(vp);
 485                 return (NULL);
 486         }
 487 
 488         err = VOP_ACCESS(vp, VREAD, 0, CRED(), NULL);
 489         if (err) {
 490                 /*
 491                  * We don't have read access? better get the heck out.
 492                  */
 493                 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
 494                 VN_RELE(vp);
 495                 return (NULL);
 496         }
 497 
 498         (void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
 499         /*
 500          * get the file size to do some basic validation
 501          */
 502         va.va_mask = AT_SIZE;
 503         err = VOP_GETATTR(vp, &va, 0, CRED(), NULL);
 504 
 505         kill_file = (va.va_size == 0 || va.va_size <
 506             (NFS4_VERIFIER_SIZE + sizeof (uint_t)+1));
 507 
 508         if (err || kill_file) {
 509                 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
 510                 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
 511                 VN_RELE(vp);
 512                 if (kill_file) {
 513                         (void) VOP_REMOVE(dvp, ss_pn->leaf, CRED(), NULL, 0);
 514                 }
 515                 return (NULL);
 516         }
 517 
 518         cl_ss = kmem_alloc(sizeof (rfs4_oldstate_t), KM_SLEEP);
 519 
 520         /*
 521          * build iovecs to read in the file_version, verifier and id_len
 522          */
 523         iov[0].iov_base = (caddr_t)&file_vers;
 524         iov[0].iov_len = sizeof (int);
 525         iov[1].iov_base = (caddr_t)&cl_ss->cl_id4.verifier;
 526         iov[1].iov_len = NFS4_VERIFIER_SIZE;
 527         iov[2].iov_base = (caddr_t)&id_len;
 528         iov[2].iov_len = sizeof (uint_t);
 529 
 530         uio.uio_iov = iov;
 531         uio.uio_iovcnt = 3;
 532         uio.uio_segflg = UIO_SYSSPACE;
 533         uio.uio_loffset = 0;
 534         uio.uio_resid = sizeof (int) + NFS4_VERIFIER_SIZE + sizeof (uint_t);
 535 
 536         if (err = VOP_READ(vp, &uio, FREAD, CRED(), NULL)) {
 537                 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
 538                 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
 539                 VN_RELE(vp);
 540                 kmem_free(cl_ss, sizeof (rfs4_oldstate_t));
 541                 return (NULL);
 542         }
 543 
 544         /*
 545          * if the file_version doesn't match or if the
 546          * id_len is zero or the combination of the verifier,
 547          * id_len and id_val is bigger than the file we have
 548          * a problem. If so ditch the file.
 549          */
 550         kill_file = (file_vers != NFS4_SS_VERSION || id_len == 0 ||
 551             (id_len + NFS4_VERIFIER_SIZE + sizeof (uint_t)) > va.va_size);
 552 
 553         if (err || kill_file) {
 554                 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
 555                 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
 556                 VN_RELE(vp);
 557                 kmem_free(cl_ss, sizeof (rfs4_oldstate_t));
 558                 if (kill_file) {
 559                         (void) VOP_REMOVE(dvp, ss_pn->leaf, CRED(), NULL, 0);
 560                 }
 561                 return (NULL);
 562         }
 563 
 564         /*
 565          * now get the client id value
 566          */
 567         cl_ss->cl_id4.id_val = kmem_alloc(id_len, KM_SLEEP);
 568         iov[0].iov_base = cl_ss->cl_id4.id_val;
 569         iov[0].iov_len = id_len;
 570 
 571         uio.uio_iov = iov;
 572         uio.uio_iovcnt = 1;
 573         uio.uio_segflg = UIO_SYSSPACE;
 574         uio.uio_resid = cl_ss->cl_id4.id_len = id_len;
 575 
 576         if (err = VOP_READ(vp, &uio, FREAD, CRED(), NULL)) {
 577                 VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
 578                 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
 579                 VN_RELE(vp);
 580                 kmem_free(cl_ss->cl_id4.id_val, id_len);
 581                 kmem_free(cl_ss, sizeof (rfs4_oldstate_t));
 582                 return (NULL);
 583         }
 584 
 585         VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
 586         (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
 587         VN_RELE(vp);
 588         return (cl_ss);
 589 }
 590 
 591 #ifdef  nextdp
 592 #undef nextdp
 593 #endif
 594 #define nextdp(dp)      ((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
 595 
 596 /*
 597  * Add entries from statedir to supplied oldstate list.
 598  * Optionally, move all entries from statedir -> destdir.
 599  */
 600 void
 601 rfs4_ss_oldstate(rfs4_oldstate_t *oldstate, char *statedir, char *destdir)
 602 {
 603         rfs4_ss_pn_t *ss_pn;
 604         rfs4_oldstate_t *cl_ss = NULL;
 605         char    *dirt = NULL;
 606         int     err, dir_eof = 0, size = 0;
 607         vnode_t *dvp;
 608         struct iovec iov;
 609         struct uio uio;
 610         struct dirent64 *dep;
 611         offset_t dirchunk_offset = 0;
 612 
 613         /*
 614          * open the state directory
 615          */
 616         if (vn_open(statedir, UIO_SYSSPACE, FREAD, 0, &dvp, 0, 0))
 617                 return;
 618 
 619         if (dvp->v_type != VDIR || VOP_ACCESS(dvp, VREAD, 0, CRED(), NULL))
 620                 goto out;
 621 
 622         dirt = kmem_alloc(RFS4_SS_DIRSIZE, KM_SLEEP);
 623 
 624         /*
 625          * Get and process the directory entries
 626          */
 627         while (!dir_eof) {
 628                 (void) VOP_RWLOCK(dvp, V_WRITELOCK_FALSE, NULL);
 629                 iov.iov_base = dirt;
 630                 iov.iov_len = RFS4_SS_DIRSIZE;
 631                 uio.uio_iov = &iov;
 632                 uio.uio_iovcnt = 1;
 633                 uio.uio_segflg = UIO_SYSSPACE;
 634                 uio.uio_loffset = dirchunk_offset;
 635                 uio.uio_resid = RFS4_SS_DIRSIZE;
 636 
 637                 err = VOP_READDIR(dvp, &uio, CRED(), &dir_eof, NULL, 0);
 638                 VOP_RWUNLOCK(dvp, V_WRITELOCK_FALSE, NULL);
 639                 if (err)
 640                         goto out;
 641 
 642                 size = RFS4_SS_DIRSIZE - uio.uio_resid;
 643 
 644                 /*
 645                  * Process all the directory entries in this
 646                  * readdir chunk
 647                  */
 648                 for (dep = (struct dirent64 *)dirt; size > 0;
 649                     dep = nextdp(dep)) {
 650 
 651                         size -= dep->d_reclen;
 652                         dirchunk_offset = dep->d_off;
 653 
 654                         /*
 655                          * Skip '.' and '..'
 656                          */
 657                         if (NFS_IS_DOTNAME(dep->d_name))
 658                                 continue;
 659 
 660                         ss_pn = rfs4_ss_pnalloc(statedir, dep->d_name);
 661                         if (ss_pn == NULL)
 662                                 continue;
 663 
 664                         if (cl_ss = rfs4_ss_getstate(dvp, ss_pn)) {
 665                                 if (destdir != NULL) {
 666                                         rfs4_ss_pnfree(ss_pn);
 667                                         cl_ss->ss_pn = rfs4_ss_movestate(
 668                                             statedir, destdir, dep->d_name);
 669                                 } else {
 670                                         cl_ss->ss_pn = ss_pn;
 671                                 }
 672                                 insque(cl_ss, oldstate);
 673                         } else {
 674                                 rfs4_ss_pnfree(ss_pn);
 675                         }
 676                 }
 677         }
 678 
 679 out:
 680         (void) VOP_CLOSE(dvp, FREAD, 1, (offset_t)0, CRED(), NULL);
 681         VN_RELE(dvp);
 682         if (dirt)
 683                 kmem_free((caddr_t)dirt, RFS4_SS_DIRSIZE);
 684 }
 685 
 686 static void
 687 rfs4_ss_init(nfs4_srv_t *nsrv4)
 688 {
 689         int npaths = 1;
 690         char *default_dss_path = NFS4_DSS_VAR_DIR;
 691 
 692         /* read the default stable storage state */
 693         rfs4_dss_readstate(nsrv4, npaths, &default_dss_path);
 694 
 695         rfs4_ss_enabled = 1;
 696 }
 697 
 698 static void
 699 rfs4_ss_fini(nfs4_srv_t *nsrv4)
 700 {
 701         rfs4_servinst_t *sip;
 702 
 703         mutex_enter(&nsrv4->servinst_lock);
 704         sip = nsrv4->nfs4_cur_servinst;
 705         while (sip != NULL) {
 706                 rfs4_dss_clear_oldstate(sip);
 707                 sip = sip->next;
 708         }
 709         mutex_exit(&nsrv4->servinst_lock);
 710 }
 711 
 712 /*
 713  * Remove all oldstate files referenced by this servinst.
 714  */
 715 static void
 716 rfs4_dss_clear_oldstate(rfs4_servinst_t *sip)
 717 {
 718         rfs4_oldstate_t *os_head, *osp;
 719 
 720         rw_enter(&sip->oldstate_lock, RW_WRITER);
 721         os_head = sip->oldstate;
 722 
 723         if (os_head == NULL) {
 724                 rw_exit(&sip->oldstate_lock);
 725                 return;
 726         }
 727 
 728         /* skip dummy entry */
 729         osp = os_head->next;
 730         while (osp != os_head) {
 731                 char *leaf = osp->ss_pn->leaf;
 732                 rfs4_oldstate_t *os_next;
 733 
 734                 rfs4_dss_remove_leaf(sip, NFS4_DSS_OLDSTATE_LEAF, leaf);
 735 
 736                 if (osp->cl_id4.id_val)
 737                         kmem_free(osp->cl_id4.id_val, osp->cl_id4.id_len);
 738                 rfs4_ss_pnfree(osp->ss_pn);
 739 
 740                 os_next = osp->next;
 741                 remque(osp);
 742                 kmem_free(osp, sizeof (rfs4_oldstate_t));
 743                 osp = os_next;
 744         }
 745 
 746         rw_exit(&sip->oldstate_lock);
 747 }
 748 
 749 /*
 750  * Form the state and oldstate paths, and read in the stable storage files.
 751  */
 752 void
 753 rfs4_dss_readstate(nfs4_srv_t *nsrv4, int npaths, char **paths)
 754 {
 755         int i;
 756         char *state, *oldstate;
 757 
 758         state = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 759         oldstate = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 760 
 761         for (i = 0; i < npaths; i++) {
 762                 char *path = paths[i];
 763 
 764                 (void) sprintf(state, "%s/%s", path, NFS4_DSS_STATE_LEAF);
 765                 (void) sprintf(oldstate, "%s/%s", path, NFS4_DSS_OLDSTATE_LEAF);
 766 
 767                 /*
 768                  * Populate the current server instance's oldstate list.
 769                  *
 770                  * 1. Read stable storage data from old state directory,
 771                  *    leaving its contents alone.
 772                  *
 773                  * 2. Read stable storage data from state directory,
 774                  *    and move the latter's contents to old state
 775                  *    directory.
 776                  */
 777                 /* CSTYLED */
 778                 rfs4_ss_oldstate(nsrv4->nfs4_cur_servinst->oldstate, oldstate, NULL);
 779                 /* CSTYLED */
 780                 rfs4_ss_oldstate(nsrv4->nfs4_cur_servinst->oldstate, state, oldstate);
 781         }
 782 
 783         kmem_free(state, MAXPATHLEN);
 784         kmem_free(oldstate, MAXPATHLEN);
 785 }
 786 
 787 
 788 /*
 789  * Check if we are still in grace and if the client can be
 790  * granted permission to perform reclaims.
 791  */
 792 void
 793 rfs4_ss_chkclid(nfs4_srv_t *nsrv4, rfs4_client_t *cp)
 794 {
 795         rfs4_servinst_t *sip;
 796 
 797         /*
 798          * It should be sufficient to check the oldstate data for just
 799          * this client's instance. However, since our per-instance
 800          * client grouping is solely temporal, HA-NFSv4 RG failover
 801          * might result in clients of the same RG being partitioned into
 802          * separate instances.
 803          *
 804          * Until the client grouping is improved, we must check the
 805          * oldstate data for all instances with an active grace period.
 806          *
 807          * This also serves as the mechanism to remove stale oldstate data.
 808          * The first time we check an instance after its grace period has
 809          * expired, the oldstate data should be cleared.
 810          *
 811          * Start at the current instance, and walk the list backwards
 812          * to the first.
 813          */
 814         mutex_enter(&nsrv4->servinst_lock);
 815         for (sip = nsrv4->nfs4_cur_servinst; sip != NULL; sip = sip->prev) {
 816                 rfs4_ss_chkclid_sip(cp, sip);
 817 
 818                 /* if the above check found this client, we're done */
 819                 if (cp->rc_can_reclaim)
 820                         break;
 821         }
 822         mutex_exit(&nsrv4->servinst_lock);
 823 }
 824 
 825 static void
 826 rfs4_ss_chkclid_sip(rfs4_client_t *cp, rfs4_servinst_t *sip)
 827 {
 828         rfs4_oldstate_t *osp, *os_head;
 829 
 830         /* short circuit everything if this server instance has no oldstate */
 831         rw_enter(&sip->oldstate_lock, RW_READER);
 832         os_head = sip->oldstate;
 833         rw_exit(&sip->oldstate_lock);
 834         if (os_head == NULL)
 835                 return;
 836 
 837         /*
 838          * If this server instance is no longer in a grace period then
 839          * the client won't be able to reclaim. No further need for this
 840          * instance's oldstate data, so it can be cleared.
 841          */
 842         if (!rfs4_servinst_in_grace(sip))
 843                 return;
 844 
 845         /* this instance is still in grace; search for the clientid */
 846 
 847         rw_enter(&sip->oldstate_lock, RW_READER);
 848 
 849         os_head = sip->oldstate;
 850         /* skip dummy entry */
 851         osp = os_head->next;
 852         while (osp != os_head) {
 853                 if (osp->cl_id4.id_len == cp->rc_nfs_client.id_len) {
 854                         if (bcmp(osp->cl_id4.id_val, cp->rc_nfs_client.id_val,
 855                             osp->cl_id4.id_len) == 0) {
 856                                 cp->rc_can_reclaim = 1;
 857                                 break;
 858                         }
 859                 }
 860                 osp = osp->next;
 861         }
 862 
 863         rw_exit(&sip->oldstate_lock);
 864 }
 865 
 866 /*
 867  * Place client information into stable storage: 1/3.
 868  * First, generate the leaf filename, from the client's IP address and
 869  * the server-generated short-hand clientid.
 870  */
 871 void
 872 rfs4_ss_clid(nfs4_srv_t *nsrv4, rfs4_client_t *cp)
 873 {
 874         const char *kinet_ntop6(uchar_t *, char *, size_t);
 875         char leaf[MAXNAMELEN], buf[INET6_ADDRSTRLEN];
 876         struct sockaddr *ca;
 877         uchar_t *b;
 878 
 879         if (rfs4_ss_enabled == 0) {
 880                 return;
 881         }
 882 
 883         buf[0] = 0;
 884 
 885         ca = (struct sockaddr *)&cp->rc_addr;
 886 
 887         /*
 888          * Convert the caller's IP address to a dotted string
 889          */
 890         if (ca->sa_family == AF_INET) {
 891                 b = (uchar_t *)&((struct sockaddr_in *)ca)->sin_addr;
 892                 (void) sprintf(buf, "%03d.%03d.%03d.%03d", b[0] & 0xFF,
 893                     b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
 894         } else if (ca->sa_family == AF_INET6) {
 895                 struct sockaddr_in6 *sin6;
 896 
 897                 sin6 = (struct sockaddr_in6 *)ca;
 898                 (void) kinet_ntop6((uchar_t *)&sin6->sin6_addr,
 899                     buf, INET6_ADDRSTRLEN);
 900         }
 901 
 902         (void) snprintf(leaf, MAXNAMELEN, "%s-%llx", buf,
 903             (longlong_t)cp->rc_clientid);
 904         rfs4_ss_clid_write(nsrv4, cp, leaf);
 905 }
 906 
 907 /*
 908  * Place client information into stable storage: 2/3.
 909  * DSS: distributed stable storage: the file may need to be written to
 910  * multiple directories.
 911  */
 912 static void
 913 rfs4_ss_clid_write(nfs4_srv_t *nsrv4, rfs4_client_t *cp, char *leaf)
 914 {
 915         rfs4_servinst_t *sip;
 916 
 917         /*
 918          * It should be sufficient to write the leaf file to (all) DSS paths
 919          * associated with just this client's instance. However, since our
 920          * per-instance client grouping is solely temporal, HA-NFSv4 RG
 921          * failover might result in us losing DSS data.
 922          *
 923          * Until the client grouping is improved, we must write the DSS data
 924          * to all instances' paths. Start at the current instance, and
 925          * walk the list backwards to the first.
 926          */
 927         mutex_enter(&nsrv4->servinst_lock);
 928         for (sip = nsrv4->nfs4_cur_servinst; sip != NULL; sip = sip->prev) {
 929                 int i, npaths = sip->dss_npaths;
 930 
 931                 /* write the leaf file to all DSS paths */
 932                 for (i = 0; i < npaths; i++) {
 933                         rfs4_dss_path_t *dss_path = sip->dss_paths[i];
 934 
 935                         /* HA-NFSv4 path might have been failed-away from us */
 936                         if (dss_path == NULL)
 937                                 continue;
 938 
 939                         rfs4_ss_clid_write_one(cp, dss_path->path, leaf);
 940                 }
 941         }
 942         mutex_exit(&nsrv4->servinst_lock);
 943 }
 944 
 945 /*
 946  * Place client information into stable storage: 3/3.
 947  * Write the stable storage data to the requested file.
 948  */
 949 static void
 950 rfs4_ss_clid_write_one(rfs4_client_t *cp, char *dss_path, char *leaf)
 951 {
 952         int ioflag;
 953         int file_vers = NFS4_SS_VERSION;
 954         size_t dirlen;
 955         struct uio uio;
 956         struct iovec iov[4];
 957         char *dir;
 958         rfs4_ss_pn_t *ss_pn;
 959         vnode_t *vp;
 960         nfs_client_id4 *cl_id4 = &(cp->rc_nfs_client);
 961 
 962         /* allow 2 extra bytes for '/' & NUL */
 963         dirlen = strlen(dss_path) + strlen(NFS4_DSS_STATE_LEAF) + 2;
 964         dir = kmem_alloc(dirlen, KM_SLEEP);
 965         (void) sprintf(dir, "%s/%s", dss_path, NFS4_DSS_STATE_LEAF);
 966 
 967         ss_pn = rfs4_ss_pnalloc(dir, leaf);
 968         /* rfs4_ss_pnalloc takes its own copy */
 969         kmem_free(dir, dirlen);
 970         if (ss_pn == NULL)
 971                 return;
 972 
 973         if (vn_open(ss_pn->pn, UIO_SYSSPACE, FCREAT|FWRITE, 0600, &vp,
 974             CRCREAT, 0)) {
 975                 rfs4_ss_pnfree(ss_pn);
 976                 return;
 977         }
 978 
 979         /*
 980          * We need to record leaf - i.e. the filename - so that we know
 981          * what to remove, in the future. However, the dir part of cp->ss_pn
 982          * should never be referenced directly, since it's potentially only
 983          * one of several paths with this leaf in it.
 984          */
 985         if (cp->rc_ss_pn != NULL) {
 986                 if (strcmp(cp->rc_ss_pn->leaf, leaf) == 0) {
 987                         /* we've already recorded *this* leaf */
 988                         rfs4_ss_pnfree(ss_pn);
 989                 } else {
 990                         /* replace with this leaf */
 991                         rfs4_ss_pnfree(cp->rc_ss_pn);
 992                         cp->rc_ss_pn = ss_pn;
 993                 }
 994         } else {
 995                 cp->rc_ss_pn = ss_pn;
 996         }
 997 
 998         /*
 999          * Build a scatter list that points to the nfs_client_id4
1000          */
1001         iov[0].iov_base = (caddr_t)&file_vers;
1002         iov[0].iov_len = sizeof (int);
1003         iov[1].iov_base = (caddr_t)&(cl_id4->verifier);
1004         iov[1].iov_len = NFS4_VERIFIER_SIZE;
1005         iov[2].iov_base = (caddr_t)&(cl_id4->id_len);
1006         iov[2].iov_len = sizeof (uint_t);
1007         iov[3].iov_base = (caddr_t)cl_id4->id_val;
1008         iov[3].iov_len = cl_id4->id_len;
1009 
1010         uio.uio_iov = iov;
1011         uio.uio_iovcnt = 4;
1012         uio.uio_loffset = 0;
1013         uio.uio_segflg = UIO_SYSSPACE;
1014         uio.uio_llimit = (rlim64_t)MAXOFFSET_T;
1015         uio.uio_resid = cl_id4->id_len + sizeof (int) +
1016             NFS4_VERIFIER_SIZE + sizeof (uint_t);
1017 
1018         ioflag = uio.uio_fmode = (FWRITE|FSYNC);
1019         uio.uio_extflg = UIO_COPY_DEFAULT;
1020 
1021         (void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
1022         /* write the full client id to the file. */
1023         (void) VOP_WRITE(vp, &uio, ioflag, CRED(), NULL);
1024         VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
1025 
1026         (void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED(), NULL);
1027         VN_RELE(vp);
1028 }
1029 
1030 /*
1031  * DSS: distributed stable storage.
1032  * Unpack the list of paths passed by nfsd.
1033  * Use nvlist_alloc(9F) to manage the data.
1034  * The caller is responsible for allocating and freeing the buffer.
1035  */
1036 int
1037 rfs4_dss_setpaths(char *buf, size_t buflen)
1038 {
1039         int error;
1040 
1041         /*
1042          * If this is a "warm start", i.e. we previously had DSS paths,
1043          * preserve the old paths.
1044          */
1045         if (rfs4_dss_paths != NULL) {
1046                 /*
1047                  * Before we lose the ptr, destroy the nvlist and pathnames
1048                  * array from the warm start before this one.
1049                  */
1050                 nvlist_free(rfs4_dss_oldpaths);
1051                 rfs4_dss_oldpaths = rfs4_dss_paths;
1052         }
1053 
1054         /* unpack the buffer into a searchable nvlist */
1055         error = nvlist_unpack(buf, buflen, &rfs4_dss_paths, KM_SLEEP);
1056         if (error)
1057                 return (error);
1058 
1059         /*
1060          * Search the nvlist for the pathnames nvpair (which is the only nvpair
1061          * in the list, and record its location.
1062          */
1063         error = nvlist_lookup_string_array(rfs4_dss_paths, NFS4_DSS_NVPAIR_NAME,
1064             &rfs4_dss_newpaths, &rfs4_dss_numnewpaths);
1065         return (error);
1066 }
1067 
1068 /*
1069  * Ultimately the nfssys() call NFS4_CLR_STATE endsup here
1070  * to find and mark the client for forced expire.
1071  */
1072 static void
1073 rfs4_client_scrub(rfs4_entry_t ent, void *arg)
1074 {
1075         rfs4_client_t *cp = (rfs4_client_t *)ent;
1076         struct nfs4clrst_args *clr = arg;
1077         struct sockaddr_in6 *ent_sin6;
1078         struct in6_addr  clr_in6;
1079         struct sockaddr_in  *ent_sin;
1080         struct in_addr   clr_in;
1081 
1082         if (clr->addr_type != cp->rc_addr.ss_family) {
1083                 return;
1084         }
1085 
1086         switch (clr->addr_type) {
1087 
1088         case AF_INET6:
1089                 /* copyin the address from user space */
1090                 if (copyin(clr->ap, &clr_in6, sizeof (clr_in6))) {
1091                         break;
1092                 }
1093 
1094                 ent_sin6 = (struct sockaddr_in6 *)&cp->rc_addr;
1095 
1096                 /*
1097                  * now compare, and if equivalent mark entry
1098                  * for forced expiration
1099                  */
1100                 if (IN6_ARE_ADDR_EQUAL(&ent_sin6->sin6_addr, &clr_in6)) {
1101                         cp->rc_forced_expire = 1;
1102                 }
1103                 break;
1104 
1105         case AF_INET:
1106                 /* copyin the address from user space */
1107                 if (copyin(clr->ap, &clr_in, sizeof (clr_in))) {
1108                         break;
1109                 }
1110 
1111                 ent_sin = (struct sockaddr_in *)&cp->rc_addr;
1112 
1113                 /*
1114                  * now compare, and if equivalent mark entry
1115                  * for forced expiration
1116                  */
1117                 if (ent_sin->sin_addr.s_addr == clr_in.s_addr) {
1118                         cp->rc_forced_expire = 1;
1119                 }
1120                 break;
1121 
1122         default:
1123                 /* force this assert to fail */
1124                 ASSERT(clr->addr_type != clr->addr_type);
1125         }
1126 }
1127 
1128 /*
1129  * This is called from nfssys() in order to clear server state
1130  * for the specified client IP Address.
1131  */
1132 void
1133 rfs4_clear_client_state(struct nfs4clrst_args *clr)
1134 {
1135         nfs4_srv_t *nsrv4;
1136         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
1137         (void) rfs4_dbe_walk(nsrv4->rfs4_client_tab, rfs4_client_scrub, clr);
1138 }
1139 
1140 /*
1141  * Used to initialize the NFSv4 server's state or database.  All of
1142  * the tables are created and timers are set.
1143  */
1144 void
1145 rfs4_state_g_init()
1146 {
1147         extern boolean_t rfs4_cpr_callb(void *, int);
1148         /*
1149          * Add a CPR callback so that we can update client
1150          * access times to extend the lease after a suspend
1151          * and resume (using the same class as rpcmod/connmgr)
1152          */
1153         cpr_id = callb_add(rfs4_cpr_callb, 0, CB_CL_CPR_RPC, "rfs4");
1154 
1155         /*
1156          * NFSv4 server state databases
1157          *
1158          * Initilized when the module is loaded and used by NFSv4 state tables.
1159          * These kmem_cache free pools are used globally, the NFSv4 state
1160          * tables which make use of these kmem_cache free pools are per zone.
1161          *
1162          * initialize the global kmem_cache free pools which will be used by
1163          * the NFSv4 state tables.
1164          */
1165         /* CSTYLED */
1166         rfs4_client_mem_cache = nfs4_init_mem_cache("Client_entry_cache", 2, sizeof (rfs4_client_t), 0);
1167         /* CSTYLED */
1168         rfs4_clntIP_mem_cache = nfs4_init_mem_cache("ClntIP_entry_cache", 1, sizeof (rfs4_clntip_t), 1);
1169         /* CSTYLED */
1170         rfs4_openown_mem_cache = nfs4_init_mem_cache("OpenOwner_entry_cache", 1, sizeof (rfs4_openowner_t), 2);
1171         /* CSTYLED */
1172         rfs4_openstID_mem_cache = nfs4_init_mem_cache("OpenStateID_entry_cache", 3, sizeof (rfs4_state_t), 3);
1173         /* CSTYLED */
1174         rfs4_lockstID_mem_cache = nfs4_init_mem_cache("LockStateID_entry_cache", 3, sizeof (rfs4_lo_state_t), 4);
1175         /* CSTYLED */
1176         rfs4_lockown_mem_cache = nfs4_init_mem_cache("Lockowner_entry_cache", 2, sizeof (rfs4_lockowner_t), 5);
1177         /* CSTYLED */
1178         rfs4_file_mem_cache = nfs4_init_mem_cache("File_entry_cache", 1, sizeof (rfs4_file_t), 6);
1179         /* CSTYLED */
1180         rfs4_delegstID_mem_cache = nfs4_init_mem_cache("DelegStateID_entry_cache", 2, sizeof (rfs4_deleg_state_t), 7);
1181 
1182         rfs4_client_clrst = rfs4_clear_client_state;
1183 }
1184 
1185 
1186 /*
1187  * Used at server shutdown to cleanup all of the NFSv4 server's structures
1188  * and other state.
1189  */
1190 void
1191 rfs4_state_g_fini()
1192 {
1193         int i;
1194         /*
1195          * Cleanup the CPR callback.
1196          */
1197         if (cpr_id)
1198                 (void) callb_delete(cpr_id);
1199 
1200         rfs4_client_clrst = NULL;
1201 
1202         /* free the NFSv4 state databases */
1203         for (i = 0; i < RFS4_DB_MEM_CACHE_NUM; i++) {
1204                 kmem_cache_destroy(rfs4_db_mem_cache_table[i].r_db_mem_cache);
1205                 rfs4_db_mem_cache_table[i].r_db_mem_cache = NULL;
1206         }
1207 
1208         rfs4_client_mem_cache = NULL;
1209         rfs4_clntIP_mem_cache = NULL;
1210         rfs4_openown_mem_cache = NULL;
1211         rfs4_openstID_mem_cache = NULL;
1212         rfs4_lockstID_mem_cache = NULL;
1213         rfs4_lockown_mem_cache = NULL;
1214         rfs4_file_mem_cache = NULL;
1215         rfs4_delegstID_mem_cache = NULL;
1216 
1217         /* DSS: distributed stable storage */
1218         nvlist_free(rfs4_dss_oldpaths);
1219         nvlist_free(rfs4_dss_paths);
1220         rfs4_dss_paths = rfs4_dss_oldpaths = NULL;
1221 }
1222 
1223 /*
1224  * Used to initialize the per zone NFSv4 server's state
1225  */
1226 void
1227 rfs4_state_zone_init(nfs4_srv_t *nsrv4)
1228 {
1229         time_t start_time;
1230         int start_grace;
1231         char *dss_path = NFS4_DSS_VAR_DIR;
1232 
1233         /* DSS: distributed stable storage: initialise served paths list */
1234         nsrv4->dss_pathlist = NULL;
1235 
1236         /*
1237          * Set the boot time.  If the server
1238          * has been restarted quickly and has had the opportunity to
1239          * service clients, then the start_time needs to be bumped
1240          * regardless.  A small window but it exists...
1241          */
1242         start_time = gethrestime_sec();
1243         if (nsrv4->rfs4_start_time < start_time)
1244                 nsrv4->rfs4_start_time = start_time;
1245         else
1246                 nsrv4->rfs4_start_time++;
1247 
1248         /*
1249          * Create the first server instance, or a new one if the server has
1250          * been restarted; see above comments on rfs4_start_time. Don't
1251          * start its grace period; that will be done later, to maximise the
1252          * clients' recovery window.
1253          */
1254         start_grace = 0;
1255         if (curzone == global_zone && rfs4_dss_numnewpaths > 0) {
1256                 int i;
1257                 char **dss_allpaths = NULL;
1258                 dss_allpaths = kmem_alloc(sizeof (char *) * (rfs4_dss_numnewpaths + 1), KM_SLEEP);
1259                 /*
1260                  * Add the default path into the list of paths for saving
1261                  * state informantion.
1262                  */
1263                 dss_allpaths[0] = dss_path;
1264                 for ( i = 0; i < rfs4_dss_numnewpaths; i++) {
1265                         dss_allpaths[i + 1] = rfs4_dss_newpaths[i];
1266                 }
1267                 rfs4_servinst_create(nsrv4, start_grace, (rfs4_dss_numnewpaths + 1), dss_allpaths);
1268                 kmem_free(dss_allpaths, (sizeof (char *) * (rfs4_dss_numnewpaths + 1)));
1269         } else {
1270                 rfs4_servinst_create(nsrv4, start_grace, 1, &dss_path);
1271         }
1272 
1273         /* reset the "first NFSv4 request" status */
1274         nsrv4->seen_first_compound = 0;
1275 
1276         mutex_enter(&nsrv4->state_lock);
1277 
1278         /*
1279          * If the server state database has already been initialized,
1280          * skip it
1281          */
1282         if (nsrv4->nfs4_server_state != NULL) {
1283                 mutex_exit(&nsrv4->state_lock);
1284                 return;
1285         }
1286 
1287         rw_init(&nsrv4->rfs4_findclient_lock, NULL, RW_DEFAULT, NULL);
1288 
1289         /* set the various cache timers for table creation */
1290         if (nsrv4->rfs4_client_cache_time == 0)
1291                 nsrv4->rfs4_client_cache_time = CLIENT_CACHE_TIME;
1292         if (nsrv4->rfs4_openowner_cache_time == 0)
1293                 nsrv4->rfs4_openowner_cache_time = OPENOWNER_CACHE_TIME;
1294         if (nsrv4->rfs4_state_cache_time == 0)
1295                 nsrv4->rfs4_state_cache_time = STATE_CACHE_TIME;
1296         if (nsrv4->rfs4_lo_state_cache_time == 0)
1297                 nsrv4->rfs4_lo_state_cache_time = LO_STATE_CACHE_TIME;
1298         if (nsrv4->rfs4_lockowner_cache_time == 0)
1299                 nsrv4->rfs4_lockowner_cache_time = LOCKOWNER_CACHE_TIME;
1300         if (nsrv4->rfs4_file_cache_time == 0)
1301                 nsrv4->rfs4_file_cache_time = FILE_CACHE_TIME;
1302         if (nsrv4->rfs4_deleg_state_cache_time == 0)
1303                 nsrv4->rfs4_deleg_state_cache_time = DELEG_STATE_CACHE_TIME;
1304 
1305         /* Create the overall database to hold all server state */
1306         nsrv4->nfs4_server_state = rfs4_database_create(rfs4_database_debug);
1307 
1308         /* Now create the individual tables */
1309         nsrv4->rfs4_client_cache_time *= rfs4_lease_time;
1310         nsrv4->rfs4_client_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1311             "Client",
1312             nsrv4->rfs4_client_cache_time,
1313             2,
1314             rfs4_client_create,
1315             rfs4_client_destroy,
1316             rfs4_client_expiry,
1317             sizeof (rfs4_client_t),
1318             TABSIZE,
1319             MAXTABSZ/8, 100);
1320         nsrv4->rfs4_nfsclnt_idx = rfs4_index_create(nsrv4->rfs4_client_tab,
1321             "nfs_client_id4", nfsclnt_hash,
1322             nfsclnt_compare, nfsclnt_mkkey,
1323             TRUE);
1324         nsrv4->rfs4_clientid_idx = rfs4_index_create(nsrv4->rfs4_client_tab,
1325             "client_id", clientid_hash,
1326             clientid_compare, clientid_mkkey,
1327             FALSE);
1328 
1329         nsrv4->rfs4_clntip_cache_time = 86400 * 365; /* about a year */
1330         nsrv4->rfs4_clntip_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1331             "ClntIP",
1332             nsrv4->rfs4_clntip_cache_time,
1333             1,
1334             rfs4_clntip_create,
1335             rfs4_clntip_destroy,
1336             rfs4_clntip_expiry,
1337             sizeof (rfs4_clntip_t),
1338             TABSIZE,
1339             MAXTABSZ, 100);
1340         nsrv4->rfs4_clntip_idx = rfs4_index_create(nsrv4->rfs4_clntip_tab,
1341             "client_ip", clntip_hash,
1342             clntip_compare, clntip_mkkey,
1343             TRUE);
1344 
1345         nsrv4->rfs4_openowner_cache_time *= rfs4_lease_time;
1346         nsrv4->rfs4_openowner_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1347             "OpenOwner",
1348             nsrv4->rfs4_openowner_cache_time,
1349             1,
1350             rfs4_openowner_create,
1351             rfs4_openowner_destroy,
1352             rfs4_openowner_expiry,
1353             sizeof (rfs4_openowner_t),
1354             TABSIZE,
1355             MAXTABSZ, 100);
1356         nsrv4->rfs4_openowner_idx = rfs4_index_create(nsrv4->rfs4_openowner_tab,
1357             "open_owner4", openowner_hash,
1358             openowner_compare,
1359             openowner_mkkey, TRUE);
1360 
1361         nsrv4->rfs4_state_cache_time *= rfs4_lease_time;
1362         nsrv4->rfs4_state_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1363             "OpenStateID",
1364             nsrv4->rfs4_state_cache_time,
1365             3,
1366             rfs4_state_create,
1367             rfs4_state_destroy,
1368             rfs4_state_expiry,
1369             sizeof (rfs4_state_t),
1370             TABSIZE,
1371             MAXTABSZ, 100);
1372 
1373         /* CSTYLED */
1374         nsrv4->rfs4_state_owner_file_idx = rfs4_index_create(nsrv4->rfs4_state_tab,
1375             "Openowner-File",
1376             state_owner_file_hash,
1377             state_owner_file_compare,
1378             state_owner_file_mkkey, TRUE);
1379 
1380         nsrv4->rfs4_state_idx = rfs4_index_create(nsrv4->rfs4_state_tab,
1381             "State-id", state_hash,
1382             state_compare, state_mkkey, FALSE);
1383 
1384         nsrv4->rfs4_state_file_idx = rfs4_index_create(nsrv4->rfs4_state_tab,
1385             "File", state_file_hash,
1386             state_file_compare, state_file_mkkey,
1387             FALSE);
1388 
1389         nsrv4->rfs4_lo_state_cache_time *= rfs4_lease_time;
1390         nsrv4->rfs4_lo_state_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1391             "LockStateID",
1392             nsrv4->rfs4_lo_state_cache_time,
1393             2,
1394             rfs4_lo_state_create,
1395             rfs4_lo_state_destroy,
1396             rfs4_lo_state_expiry,
1397             sizeof (rfs4_lo_state_t),
1398             TABSIZE,
1399             MAXTABSZ, 100);
1400 
1401         /* CSTYLED */
1402         nsrv4->rfs4_lo_state_owner_idx = rfs4_index_create(nsrv4->rfs4_lo_state_tab,
1403             "lockownerxstate",
1404             lo_state_lo_hash,
1405             lo_state_lo_compare,
1406             lo_state_lo_mkkey, TRUE);
1407 
1408         nsrv4->rfs4_lo_state_idx = rfs4_index_create(nsrv4->rfs4_lo_state_tab,
1409             "State-id",
1410             lo_state_hash, lo_state_compare,
1411             lo_state_mkkey, FALSE);
1412 
1413         nsrv4->rfs4_lockowner_cache_time *= rfs4_lease_time;
1414 
1415         nsrv4->rfs4_lockowner_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1416             "Lockowner",
1417             nsrv4->rfs4_lockowner_cache_time,
1418             2,
1419             rfs4_lockowner_create,
1420             rfs4_lockowner_destroy,
1421             rfs4_lockowner_expiry,
1422             sizeof (rfs4_lockowner_t),
1423             TABSIZE,
1424             MAXTABSZ, 100);
1425 
1426         nsrv4->rfs4_lockowner_idx = rfs4_index_create(nsrv4->rfs4_lockowner_tab,
1427             "lock_owner4", lockowner_hash,
1428             lockowner_compare,
1429             lockowner_mkkey, TRUE);
1430 
1431         /* CSTYLED */
1432         nsrv4->rfs4_lockowner_pid_idx = rfs4_index_create(nsrv4->rfs4_lockowner_tab,
1433             "pid", pid_hash,
1434             pid_compare, pid_mkkey,
1435             FALSE);
1436 
1437         nsrv4->rfs4_file_cache_time *= rfs4_lease_time;
1438         nsrv4->rfs4_file_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1439             "File",
1440             nsrv4->rfs4_file_cache_time,
1441             1,
1442             rfs4_file_create,
1443             rfs4_file_destroy,
1444             NULL,
1445             sizeof (rfs4_file_t),
1446             TABSIZE,
1447             MAXTABSZ, -1);
1448 
1449         nsrv4->rfs4_file_idx = rfs4_index_create(nsrv4->rfs4_file_tab,
1450             "Filehandle", file_hash,
1451             file_compare, file_mkkey, TRUE);
1452 
1453         nsrv4->rfs4_deleg_state_cache_time *= rfs4_lease_time;
1454         /* CSTYLED */
1455         nsrv4->rfs4_deleg_state_tab = rfs4_table_create(nsrv4->nfs4_server_state,
1456             "DelegStateID",
1457             nsrv4->rfs4_deleg_state_cache_time,
1458             2,
1459             rfs4_deleg_state_create,
1460             rfs4_deleg_state_destroy,
1461             rfs4_deleg_state_expiry,
1462             sizeof (rfs4_deleg_state_t),
1463             TABSIZE,
1464             MAXTABSZ, 100);
1465         nsrv4->rfs4_deleg_idx = rfs4_index_create(nsrv4->rfs4_deleg_state_tab,
1466             "DelegByFileClient",
1467             deleg_hash,
1468             deleg_compare,
1469             deleg_mkkey, TRUE);
1470 
1471         /* CSTYLED */
1472         nsrv4->rfs4_deleg_state_idx = rfs4_index_create(nsrv4->rfs4_deleg_state_tab,
1473             "DelegState",
1474             deleg_state_hash,
1475             deleg_state_compare,
1476             deleg_state_mkkey, FALSE);
1477 
1478         mutex_exit(&nsrv4->state_lock);
1479 
1480         /*
1481          * Init the stable storage.
1482          */
1483         rfs4_ss_init(nsrv4);
1484 }
1485 
1486 /*
1487  * Used at server shutdown to cleanup all of NFSv4 server's zone structures
1488  * and state.
1489  */
1490 void
1491 rfs4_state_zone_fini()
1492 {
1493         rfs4_database_t *dbp;
1494         nfs4_srv_t *nsrv4;
1495         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
1496 
1497         rfs4_set_deleg_policy(nsrv4, SRV_NEVER_DELEGATE);
1498 
1499         mutex_enter(&nsrv4->state_lock);
1500 
1501         if (nsrv4->nfs4_server_state == NULL) {
1502                 mutex_exit(&nsrv4->state_lock);
1503                 return;
1504         }
1505 
1506         /* destroy server instances and current instance ptr */
1507         rfs4_servinst_destroy_all(nsrv4);
1508 
1509         /* reset the "first NFSv4 request" status */
1510         nsrv4->seen_first_compound = 0;
1511 
1512         dbp = nsrv4->nfs4_server_state;
1513         nsrv4->nfs4_server_state = NULL;
1514 
1515         rw_destroy(&nsrv4->rfs4_findclient_lock);
1516 
1517         /* First stop all of the reaper threads in the database */
1518         rfs4_database_shutdown(dbp);
1519         /*
1520          * WARNING: There may be consumers of the rfs4 database still
1521          * active as we destroy these.  IF that's the case, consider putting
1522          * some of their _zone_fini()-like functions into the zsd key as
1523          * ~~SHUTDOWN~~ functions instead of ~~DESTROY~~ functions.  We can
1524          * maintain some ordering guarantees better that way.
1525          */
1526         /* Now destroy/release the database tables */
1527         rfs4_database_destroy(dbp);
1528 
1529         /* Reset the cache timers for next time */
1530         nsrv4->rfs4_client_cache_time = 0;
1531         nsrv4->rfs4_openowner_cache_time = 0;
1532         nsrv4->rfs4_state_cache_time = 0;
1533         nsrv4->rfs4_lo_state_cache_time = 0;
1534         nsrv4->rfs4_lockowner_cache_time = 0;
1535         nsrv4->rfs4_file_cache_time = 0;
1536         nsrv4->rfs4_deleg_state_cache_time = 0;
1537 
1538         mutex_exit(&nsrv4->state_lock);
1539 
1540         /* clean up any dangling stable storage structures */
1541         rfs4_ss_fini(nsrv4);
1542 }
1543 
1544 typedef union {
1545         struct {
1546                 uint32_t start_time;
1547                 uint32_t c_id;
1548         } impl_id;
1549         clientid4 id4;
1550 } cid;
1551 
1552 static int foreign_stateid(stateid_t *id);
1553 static int foreign_clientid(cid *cidp);
1554 static void embed_nodeid(cid *cidp);
1555 
1556 typedef union {
1557         struct {
1558                 uint32_t c_id;
1559                 uint32_t gen_num;
1560         } cv_impl;
1561         verifier4       confirm_verf;
1562 } scid_confirm_verf;
1563 
1564 static uint32_t
1565 clientid_hash(void *key)
1566 {
1567         cid *idp = key;
1568 
1569         return (idp->impl_id.c_id);
1570 }
1571 
1572 static bool_t
1573 clientid_compare(rfs4_entry_t entry, void *key)
1574 {
1575         rfs4_client_t *cp = (rfs4_client_t *)entry;
1576         clientid4 *idp = key;
1577 
1578         return (*idp == cp->rc_clientid);
1579 }
1580 
1581 static void *
1582 clientid_mkkey(rfs4_entry_t entry)
1583 {
1584         rfs4_client_t *cp = (rfs4_client_t *)entry;
1585 
1586         return (&cp->rc_clientid);
1587 }
1588 
1589 static uint32_t
1590 nfsclnt_hash(void *key)
1591 {
1592         nfs_client_id4 *client = key;
1593         int i;
1594         uint32_t hash = 0;
1595 
1596         for (i = 0; i < client->id_len; i++) {
1597                 hash <<= 1;
1598                 hash += (uint_t)client->id_val[i];
1599         }
1600         return (hash);
1601 }
1602 
1603 
1604 static bool_t
1605 nfsclnt_compare(rfs4_entry_t entry, void *key)
1606 {
1607         rfs4_client_t *cp = (rfs4_client_t *)entry;
1608         nfs_client_id4 *nfs_client = key;
1609 
1610         if (cp->rc_nfs_client.id_len != nfs_client->id_len)
1611                 return (FALSE);
1612 
1613         return (bcmp(cp->rc_nfs_client.id_val, nfs_client->id_val,
1614             nfs_client->id_len) == 0);
1615 }
1616 
1617 static void *
1618 nfsclnt_mkkey(rfs4_entry_t entry)
1619 {
1620         rfs4_client_t *cp = (rfs4_client_t *)entry;
1621 
1622         return (&cp->rc_nfs_client);
1623 }
1624 
1625 static bool_t
1626 rfs4_client_expiry(rfs4_entry_t u_entry)
1627 {
1628         rfs4_client_t *cp = (rfs4_client_t *)u_entry;
1629         bool_t cp_expired;
1630 
1631         if (rfs4_dbe_is_invalid(cp->rc_dbe)) {
1632                 cp->rc_ss_remove = 1;
1633                 return (TRUE);
1634         }
1635         /*
1636          * If the sysadmin has used clear_locks for this
1637          * entry then forced_expire will be set and we
1638          * want this entry to be reaped. Or the entry
1639          * has exceeded its lease period.
1640          */
1641         cp_expired = (cp->rc_forced_expire ||
1642             (gethrestime_sec() - cp->rc_last_access
1643             > rfs4_lease_time));
1644 
1645         if (!cp->rc_ss_remove && cp_expired)
1646                 cp->rc_ss_remove = 1;
1647         return (cp_expired);
1648 }
1649 
1650 /*
1651  * Remove the leaf file from all distributed stable storage paths.
1652  */
1653 static void
1654 rfs4_dss_remove_cpleaf(rfs4_client_t *cp)
1655 {
1656         nfs4_srv_t *nsrv4;
1657         rfs4_servinst_t *sip;
1658         char *leaf = cp->rc_ss_pn->leaf;
1659 
1660         /*
1661          * since the state files are written to all DSS
1662          * paths we must remove this leaf file instance
1663          * from all server instances.
1664          */
1665 
1666         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
1667         mutex_enter(&nsrv4->servinst_lock);
1668         for (sip = nsrv4->nfs4_cur_servinst; sip != NULL; sip = sip->prev) {
1669                 /* remove the leaf file associated with this server instance */
1670                 rfs4_dss_remove_leaf(sip, NFS4_DSS_STATE_LEAF, leaf);
1671         }
1672         mutex_exit(&nsrv4->servinst_lock);
1673 }
1674 
1675 static void
1676 rfs4_dss_remove_leaf(rfs4_servinst_t *sip, char *dir_leaf, char *leaf)
1677 {
1678         int i, npaths = sip->dss_npaths;
1679 
1680         for (i = 0; i < npaths; i++) {
1681                 rfs4_dss_path_t *dss_path = sip->dss_paths[i];
1682                 char *path, *dir;
1683                 size_t pathlen;
1684 
1685                 /* the HA-NFSv4 path might have been failed-over away from us */
1686                 if (dss_path == NULL)
1687                         continue;
1688 
1689                 dir = dss_path->path;
1690 
1691                 /* allow 3 extra bytes for two '/' & a NUL */
1692                 pathlen = strlen(dir) + strlen(dir_leaf) + strlen(leaf) + 3;
1693                 path = kmem_alloc(pathlen, KM_SLEEP);
1694                 (void) sprintf(path, "%s/%s/%s", dir, dir_leaf, leaf);
1695 
1696                 (void) vn_remove(path, UIO_SYSSPACE, RMFILE);
1697 
1698                 kmem_free(path, pathlen);
1699         }
1700 }
1701 
1702 static void
1703 rfs4_client_destroy(rfs4_entry_t u_entry)
1704 {
1705         rfs4_client_t *cp = (rfs4_client_t *)u_entry;
1706 
1707         mutex_destroy(cp->rc_cbinfo.cb_lock);
1708         cv_destroy(cp->rc_cbinfo.cb_cv);
1709         cv_destroy(cp->rc_cbinfo.cb_cv_nullcaller);
1710         list_destroy(&cp->rc_openownerlist);
1711 
1712         /* free callback info */
1713         rfs4_cbinfo_free(&cp->rc_cbinfo);
1714 
1715         if (cp->rc_cp_confirmed)
1716                 rfs4_client_rele(cp->rc_cp_confirmed);
1717 
1718         if (cp->rc_ss_pn) {
1719                 /* check if the stable storage files need to be removed */
1720                 if (cp->rc_ss_remove)
1721                         rfs4_dss_remove_cpleaf(cp);
1722                 rfs4_ss_pnfree(cp->rc_ss_pn);
1723         }
1724 
1725         /* Free the client supplied client id */
1726         kmem_free(cp->rc_nfs_client.id_val, cp->rc_nfs_client.id_len);
1727 
1728         if (cp->rc_sysidt != LM_NOSYSID)
1729                 lm_free_sysidt(cp->rc_sysidt);
1730 }
1731 
1732 static bool_t
1733 rfs4_client_create(rfs4_entry_t u_entry, void *arg)
1734 {
1735         rfs4_client_t *cp = (rfs4_client_t *)u_entry;
1736         nfs_client_id4 *client = (nfs_client_id4 *)arg;
1737         struct sockaddr *ca;
1738         cid *cidp;
1739         scid_confirm_verf *scvp;
1740         nfs4_srv_t *nsrv4;
1741 
1742         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
1743 
1744         /* Get a clientid to give to the client */
1745         cidp = (cid *)&cp->rc_clientid;
1746         cidp->impl_id.start_time = nsrv4->rfs4_start_time;
1747         cidp->impl_id.c_id = (uint32_t)rfs4_dbe_getid(cp->rc_dbe);
1748 
1749         /* If we are booted as a cluster node, embed our nodeid */
1750         if (cluster_bootflags & CLUSTER_BOOTED)
1751                 embed_nodeid(cidp);
1752 
1753         /* Allocate and copy client's client id value */
1754         cp->rc_nfs_client.id_val = kmem_alloc(client->id_len, KM_SLEEP);
1755         cp->rc_nfs_client.id_len = client->id_len;
1756         bcopy(client->id_val, cp->rc_nfs_client.id_val, client->id_len);
1757         cp->rc_nfs_client.verifier = client->verifier;
1758 
1759         /* Copy client's IP address */
1760         ca = client->cl_addr;
1761         if (ca->sa_family == AF_INET)
1762                 bcopy(ca, &cp->rc_addr, sizeof (struct sockaddr_in));
1763         else if (ca->sa_family == AF_INET6)
1764                 bcopy(ca, &cp->rc_addr, sizeof (struct sockaddr_in6));
1765         cp->rc_nfs_client.cl_addr = (struct sockaddr *)&cp->rc_addr;
1766 
1767         /* Init the value for the SETCLIENTID_CONFIRM verifier */
1768         scvp = (scid_confirm_verf *)&cp->rc_confirm_verf;
1769         scvp->cv_impl.c_id = cidp->impl_id.c_id;
1770         scvp->cv_impl.gen_num = 0;
1771 
1772         /* An F_UNLKSYS has been done for this client */
1773         cp->rc_unlksys_completed = FALSE;
1774 
1775         /* We need the client to ack us */
1776         cp->rc_need_confirm = TRUE;
1777         cp->rc_cp_confirmed = NULL;
1778 
1779         /* TRUE all the time until the callback path actually fails */
1780         cp->rc_cbinfo.cb_notified_of_cb_path_down = TRUE;
1781 
1782         /* Initialize the access time to now */
1783         cp->rc_last_access = gethrestime_sec();
1784 
1785         cp->rc_cr_set = NULL;
1786 
1787         cp->rc_sysidt = LM_NOSYSID;
1788 
1789         list_create(&cp->rc_openownerlist, sizeof (rfs4_openowner_t),
1790             offsetof(rfs4_openowner_t, ro_node));
1791 
1792         /* set up the callback control structure */
1793         cp->rc_cbinfo.cb_state = CB_UNINIT;
1794         mutex_init(cp->rc_cbinfo.cb_lock, NULL, MUTEX_DEFAULT, NULL);
1795         cv_init(cp->rc_cbinfo.cb_cv, NULL, CV_DEFAULT, NULL);
1796         cv_init(cp->rc_cbinfo.cb_cv_nullcaller, NULL, CV_DEFAULT, NULL);
1797 
1798         /*
1799          * Associate the client_t with the current server instance.
1800          * The hold is solely to satisfy the calling requirement of
1801          * rfs4_servinst_assign(). In this case it's not strictly necessary.
1802          */
1803         rfs4_dbe_hold(cp->rc_dbe);
1804         rfs4_servinst_assign(nsrv4, cp, nsrv4->nfs4_cur_servinst);
1805         rfs4_dbe_rele(cp->rc_dbe);
1806 
1807         return (TRUE);
1808 }
1809 
1810 /*
1811  * Caller wants to generate/update the setclientid_confirm verifier
1812  * associated with a client.  This is done during the SETCLIENTID
1813  * processing.
1814  */
1815 void
1816 rfs4_client_scv_next(rfs4_client_t *cp)
1817 {
1818         scid_confirm_verf *scvp;
1819 
1820         /* Init the value for the SETCLIENTID_CONFIRM verifier */
1821         scvp = (scid_confirm_verf *)&cp->rc_confirm_verf;
1822         scvp->cv_impl.gen_num++;
1823 }
1824 
1825 void
1826 rfs4_client_rele(rfs4_client_t *cp)
1827 {
1828         rfs4_dbe_rele(cp->rc_dbe);
1829 }
1830 
1831 rfs4_client_t *
1832 rfs4_findclient(nfs_client_id4 *client, bool_t *create, rfs4_client_t *oldcp)
1833 {
1834         rfs4_client_t *cp;
1835         nfs4_srv_t *nsrv4;
1836         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
1837 
1838 
1839         if (oldcp) {
1840                 rw_enter(&nsrv4->rfs4_findclient_lock, RW_WRITER);
1841                 rfs4_dbe_hide(oldcp->rc_dbe);
1842         } else {
1843                 rw_enter(&nsrv4->rfs4_findclient_lock, RW_READER);
1844         }
1845 
1846         cp = (rfs4_client_t *)rfs4_dbsearch(nsrv4->rfs4_nfsclnt_idx, client,
1847             create, (void *)client, RFS4_DBS_VALID);
1848 
1849         if (oldcp)
1850                 rfs4_dbe_unhide(oldcp->rc_dbe);
1851 
1852         rw_exit(&nsrv4->rfs4_findclient_lock);
1853 
1854         return (cp);
1855 }
1856 
1857 rfs4_client_t *
1858 rfs4_findclient_by_id(clientid4 clientid, bool_t find_unconfirmed)
1859 {
1860         rfs4_client_t *cp;
1861         bool_t create = FALSE;
1862         cid *cidp = (cid *)&clientid;
1863         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
1864 
1865         /* If we're a cluster and the nodeid isn't right, short-circuit */
1866         if (cluster_bootflags & CLUSTER_BOOTED && foreign_clientid(cidp))
1867                 return (NULL);
1868 
1869         rw_enter(&nsrv4->rfs4_findclient_lock, RW_READER);
1870 
1871         cp = (rfs4_client_t *)rfs4_dbsearch(nsrv4->rfs4_clientid_idx, &clientid,
1872             &create, NULL, RFS4_DBS_VALID);
1873 
1874         rw_exit(&nsrv4->rfs4_findclient_lock);
1875 
1876         if (cp && cp->rc_need_confirm && find_unconfirmed == FALSE) {
1877                 rfs4_client_rele(cp);
1878                 return (NULL);
1879         } else {
1880                 return (cp);
1881         }
1882 }
1883 
1884 static uint32_t
1885 clntip_hash(void *key)
1886 {
1887         struct sockaddr *addr = key;
1888         int i, len = 0;
1889         uint32_t hash = 0;
1890         char *ptr;
1891 
1892         if (addr->sa_family == AF_INET) {
1893                 struct sockaddr_in *a = (struct sockaddr_in *)addr;
1894                 len = sizeof (struct in_addr);
1895                 ptr = (char *)&a->sin_addr;
1896         } else if (addr->sa_family == AF_INET6) {
1897                 struct sockaddr_in6 *a = (struct sockaddr_in6 *)addr;
1898                 len = sizeof (struct in6_addr);
1899                 ptr = (char *)&a->sin6_addr;
1900         } else
1901                 return (0);
1902 
1903         for (i = 0; i < len; i++) {
1904                 hash <<= 1;
1905                 hash += (uint_t)ptr[i];
1906         }
1907         return (hash);
1908 }
1909 
1910 static bool_t
1911 clntip_compare(rfs4_entry_t entry, void *key)
1912 {
1913         rfs4_clntip_t *cp = (rfs4_clntip_t *)entry;
1914         struct sockaddr *addr = key;
1915         int len = 0;
1916         char *p1, *p2;
1917 
1918         if (addr->sa_family == AF_INET) {
1919                 struct sockaddr_in *a1 = (struct sockaddr_in *)&cp->ri_addr;
1920                 struct sockaddr_in *a2 = (struct sockaddr_in *)addr;
1921                 len = sizeof (struct in_addr);
1922                 p1 = (char *)&a1->sin_addr;
1923                 p2 = (char *)&a2->sin_addr;
1924         } else if (addr->sa_family == AF_INET6) {
1925                 struct sockaddr_in6 *a1 = (struct sockaddr_in6 *)&cp->ri_addr;
1926                 struct sockaddr_in6 *a2 = (struct sockaddr_in6 *)addr;
1927                 len = sizeof (struct in6_addr);
1928                 p1 = (char *)&a1->sin6_addr;
1929                 p2 = (char *)&a2->sin6_addr;
1930         } else
1931                 return (0);
1932 
1933         return (bcmp(p1, p2, len) == 0);
1934 }
1935 
1936 static void *
1937 clntip_mkkey(rfs4_entry_t entry)
1938 {
1939         rfs4_clntip_t *cp = (rfs4_clntip_t *)entry;
1940 
1941         return (&cp->ri_addr);
1942 }
1943 
1944 static bool_t
1945 rfs4_clntip_expiry(rfs4_entry_t u_entry)
1946 {
1947         rfs4_clntip_t *cp = (rfs4_clntip_t *)u_entry;
1948 
1949         if (rfs4_dbe_is_invalid(cp->ri_dbe))
1950                 return (TRUE);
1951         return (FALSE);
1952 }
1953 
1954 /* ARGSUSED */
1955 static void
1956 rfs4_clntip_destroy(rfs4_entry_t u_entry)
1957 {
1958 }
1959 
1960 static bool_t
1961 rfs4_clntip_create(rfs4_entry_t u_entry, void *arg)
1962 {
1963         rfs4_clntip_t *cp = (rfs4_clntip_t *)u_entry;
1964         struct sockaddr *ca = (struct sockaddr *)arg;
1965 
1966         /* Copy client's IP address */
1967         if (ca->sa_family == AF_INET)
1968                 bcopy(ca, &cp->ri_addr, sizeof (struct sockaddr_in));
1969         else if (ca->sa_family == AF_INET6)
1970                 bcopy(ca, &cp->ri_addr, sizeof (struct sockaddr_in6));
1971         else
1972                 return (FALSE);
1973         cp->ri_no_referrals = 1;
1974 
1975         return (TRUE);
1976 }
1977 
1978 rfs4_clntip_t *
1979 rfs4_find_clntip(struct sockaddr *addr, bool_t *create)
1980 {
1981         rfs4_clntip_t *cp;
1982         nfs4_srv_t *nsrv4;
1983 
1984         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
1985 
1986         rw_enter(&nsrv4->rfs4_findclient_lock, RW_READER);
1987 
1988         cp = (rfs4_clntip_t *)rfs4_dbsearch(nsrv4->rfs4_clntip_idx, addr,
1989             create, addr, RFS4_DBS_VALID);
1990 
1991         rw_exit(&nsrv4->rfs4_findclient_lock);
1992 
1993         return (cp);
1994 }
1995 
1996 void
1997 rfs4_invalidate_clntip(struct sockaddr *addr)
1998 {
1999         rfs4_clntip_t *cp;
2000         bool_t create = FALSE;
2001         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2002 
2003         rw_enter(&nsrv4->rfs4_findclient_lock, RW_READER);
2004 
2005         cp = (rfs4_clntip_t *)rfs4_dbsearch(nsrv4->rfs4_clntip_idx, addr,
2006             &create, NULL, RFS4_DBS_VALID);
2007         if (cp == NULL) {
2008                 rw_exit(&nsrv4->rfs4_findclient_lock);
2009                 return;
2010         }
2011         rfs4_dbe_invalidate(cp->ri_dbe);
2012         rfs4_dbe_rele(cp->ri_dbe);
2013 
2014         rw_exit(&nsrv4->rfs4_findclient_lock);
2015 }
2016 
2017 bool_t
2018 rfs4_lease_expired(rfs4_client_t *cp)
2019 {
2020         bool_t rc;
2021 
2022         rfs4_dbe_lock(cp->rc_dbe);
2023 
2024         /*
2025          * If the admin has executed clear_locks for this
2026          * client id, force expire will be set, so no need
2027          * to calculate anything because it's "outa here".
2028          */
2029         if (cp->rc_forced_expire) {
2030                 rc = TRUE;
2031         } else {
2032                 rc = (gethrestime_sec() - cp->rc_last_access > rfs4_lease_time);
2033         }
2034 
2035         /*
2036          * If the lease has expired we will also want
2037          * to remove any stable storage state data. So
2038          * mark the client id accordingly.
2039          */
2040         if (!cp->rc_ss_remove)
2041                 cp->rc_ss_remove = (rc == TRUE);
2042 
2043         rfs4_dbe_unlock(cp->rc_dbe);
2044 
2045         return (rc);
2046 }
2047 
2048 void
2049 rfs4_update_lease(rfs4_client_t *cp)
2050 {
2051         rfs4_dbe_lock(cp->rc_dbe);
2052         if (!cp->rc_forced_expire)
2053                 cp->rc_last_access = gethrestime_sec();
2054         rfs4_dbe_unlock(cp->rc_dbe);
2055 }
2056 
2057 
2058 static bool_t
2059 EQOPENOWNER(open_owner4 *a, open_owner4 *b)
2060 {
2061         bool_t rc;
2062 
2063         if (a->clientid != b->clientid)
2064                 return (FALSE);
2065 
2066         if (a->owner_len != b->owner_len)
2067                 return (FALSE);
2068 
2069         rc = (bcmp(a->owner_val, b->owner_val, a->owner_len) == 0);
2070 
2071         return (rc);
2072 }
2073 
2074 static uint_t
2075 openowner_hash(void *key)
2076 {
2077         int i;
2078         open_owner4 *openowner = key;
2079         uint_t hash = 0;
2080 
2081         for (i = 0; i < openowner->owner_len; i++) {
2082                 hash <<= 4;
2083                 hash += (uint_t)openowner->owner_val[i];
2084         }
2085         hash += (uint_t)openowner->clientid;
2086         hash |= (openowner->clientid >> 32);
2087 
2088         return (hash);
2089 }
2090 
2091 static bool_t
2092 openowner_compare(rfs4_entry_t u_entry, void *key)
2093 {
2094         rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
2095         open_owner4 *arg = key;
2096 
2097         return (EQOPENOWNER(&oo->ro_owner, arg));
2098 }
2099 
2100 void *
2101 openowner_mkkey(rfs4_entry_t u_entry)
2102 {
2103         rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
2104 
2105         return (&oo->ro_owner);
2106 }
2107 
2108 /* ARGSUSED */
2109 static bool_t
2110 rfs4_openowner_expiry(rfs4_entry_t u_entry)
2111 {
2112         /* openstateid held us and did all needed delay */
2113         return (TRUE);
2114 }
2115 
2116 static void
2117 rfs4_openowner_destroy(rfs4_entry_t u_entry)
2118 {
2119         rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
2120 
2121         /* Remove open owner from client's lists of open owners */
2122         rfs4_dbe_lock(oo->ro_client->rc_dbe);
2123         list_remove(&oo->ro_client->rc_openownerlist, oo);
2124         rfs4_dbe_unlock(oo->ro_client->rc_dbe);
2125 
2126         /* One less reference to the client */
2127         rfs4_client_rele(oo->ro_client);
2128         oo->ro_client = NULL;
2129 
2130         /* Free the last reply for this lock owner */
2131         rfs4_free_reply(&oo->ro_reply);
2132 
2133         if (oo->ro_reply_fh.nfs_fh4_val) {
2134                 kmem_free(oo->ro_reply_fh.nfs_fh4_val,
2135                     oo->ro_reply_fh.nfs_fh4_len);
2136                 oo->ro_reply_fh.nfs_fh4_val = NULL;
2137                 oo->ro_reply_fh.nfs_fh4_len = 0;
2138         }
2139 
2140         rfs4_sw_destroy(&oo->ro_sw);
2141         list_destroy(&oo->ro_statelist);
2142 
2143         /* Free the lock owner id */
2144         kmem_free(oo->ro_owner.owner_val, oo->ro_owner.owner_len);
2145 }
2146 
2147 void
2148 rfs4_openowner_rele(rfs4_openowner_t *oo)
2149 {
2150         rfs4_dbe_rele(oo->ro_dbe);
2151 }
2152 
2153 static bool_t
2154 rfs4_openowner_create(rfs4_entry_t u_entry, void *arg)
2155 {
2156         rfs4_openowner_t *oo = (rfs4_openowner_t *)u_entry;
2157         rfs4_openowner_t *argp = (rfs4_openowner_t *)arg;
2158         open_owner4 *openowner = &argp->ro_owner;
2159         seqid4 seqid = argp->ro_open_seqid;
2160         rfs4_client_t *cp;
2161         bool_t create = FALSE;
2162         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2163 
2164         rw_enter(&nsrv4->rfs4_findclient_lock, RW_READER);
2165 
2166         cp = (rfs4_client_t *)rfs4_dbsearch(nsrv4->rfs4_clientid_idx,
2167             &openowner->clientid,
2168             &create, NULL, RFS4_DBS_VALID);
2169 
2170         rw_exit(&nsrv4->rfs4_findclient_lock);
2171 
2172         if (cp == NULL)
2173                 return (FALSE);
2174 
2175         oo->ro_reply_fh.nfs_fh4_len = 0;
2176         oo->ro_reply_fh.nfs_fh4_val = NULL;
2177 
2178         oo->ro_owner.clientid = openowner->clientid;
2179         oo->ro_owner.owner_val =
2180             kmem_alloc(openowner->owner_len, KM_SLEEP);
2181 
2182         bcopy(openowner->owner_val,
2183             oo->ro_owner.owner_val, openowner->owner_len);
2184 
2185         oo->ro_owner.owner_len = openowner->owner_len;
2186 
2187         oo->ro_need_confirm = TRUE;
2188 
2189         rfs4_sw_init(&oo->ro_sw);
2190 
2191         oo->ro_open_seqid = seqid;
2192         bzero(&oo->ro_reply, sizeof (nfs_resop4));
2193         oo->ro_client = cp;
2194         oo->ro_cr_set = NULL;
2195 
2196         list_create(&oo->ro_statelist, sizeof (rfs4_state_t),
2197             offsetof(rfs4_state_t, rs_node));
2198 
2199         /* Insert openowner into client's open owner list */
2200         rfs4_dbe_lock(cp->rc_dbe);
2201         list_insert_tail(&cp->rc_openownerlist, oo);
2202         rfs4_dbe_unlock(cp->rc_dbe);
2203 
2204         return (TRUE);
2205 }
2206 
2207 rfs4_openowner_t *
2208 rfs4_findopenowner(open_owner4 *openowner, bool_t *create, seqid4 seqid)
2209 {
2210         rfs4_openowner_t *oo;
2211         rfs4_openowner_t arg;
2212         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2213 
2214         arg.ro_owner = *openowner;
2215         arg.ro_open_seqid = seqid;
2216         /* CSTYLED */
2217         oo = (rfs4_openowner_t *)rfs4_dbsearch(nsrv4->rfs4_openowner_idx, openowner,
2218             create, &arg, RFS4_DBS_VALID);
2219 
2220         return (oo);
2221 }
2222 
2223 void
2224 rfs4_update_open_sequence(rfs4_openowner_t *oo)
2225 {
2226 
2227         rfs4_dbe_lock(oo->ro_dbe);
2228 
2229         oo->ro_open_seqid++;
2230 
2231         rfs4_dbe_unlock(oo->ro_dbe);
2232 }
2233 
2234 void
2235 rfs4_update_open_resp(rfs4_openowner_t *oo, nfs_resop4 *resp, nfs_fh4 *fh)
2236 {
2237 
2238         rfs4_dbe_lock(oo->ro_dbe);
2239 
2240         rfs4_free_reply(&oo->ro_reply);
2241 
2242         rfs4_copy_reply(&oo->ro_reply, resp);
2243 
2244         /* Save the filehandle if provided and free if not used */
2245         if (resp->nfs_resop4_u.opopen.status == NFS4_OK &&
2246             fh && fh->nfs_fh4_len) {
2247                 if (oo->ro_reply_fh.nfs_fh4_val == NULL)
2248                         oo->ro_reply_fh.nfs_fh4_val =
2249                             kmem_alloc(fh->nfs_fh4_len, KM_SLEEP);
2250                 nfs_fh4_copy(fh, &oo->ro_reply_fh);
2251         } else {
2252                 if (oo->ro_reply_fh.nfs_fh4_val) {
2253                         kmem_free(oo->ro_reply_fh.nfs_fh4_val,
2254                             oo->ro_reply_fh.nfs_fh4_len);
2255                         oo->ro_reply_fh.nfs_fh4_val = NULL;
2256                         oo->ro_reply_fh.nfs_fh4_len = 0;
2257                 }
2258         }
2259 
2260         rfs4_dbe_unlock(oo->ro_dbe);
2261 }
2262 
2263 static bool_t
2264 lockowner_compare(rfs4_entry_t u_entry, void *key)
2265 {
2266         rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2267         lock_owner4 *b = (lock_owner4 *)key;
2268 
2269         if (lo->rl_owner.clientid != b->clientid)
2270                 return (FALSE);
2271 
2272         if (lo->rl_owner.owner_len != b->owner_len)
2273                 return (FALSE);
2274 
2275         return (bcmp(lo->rl_owner.owner_val, b->owner_val,
2276             lo->rl_owner.owner_len) == 0);
2277 }
2278 
2279 void *
2280 lockowner_mkkey(rfs4_entry_t u_entry)
2281 {
2282         rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2283 
2284         return (&lo->rl_owner);
2285 }
2286 
2287 static uint32_t
2288 lockowner_hash(void *key)
2289 {
2290         int i;
2291         lock_owner4 *lockowner = key;
2292         uint_t hash = 0;
2293 
2294         for (i = 0; i < lockowner->owner_len; i++) {
2295                 hash <<= 4;
2296                 hash += (uint_t)lockowner->owner_val[i];
2297         }
2298         hash += (uint_t)lockowner->clientid;
2299         hash |= (lockowner->clientid >> 32);
2300 
2301         return (hash);
2302 }
2303 
2304 static uint32_t
2305 pid_hash(void *key)
2306 {
2307         return ((uint32_t)(uintptr_t)key);
2308 }
2309 
2310 static void *
2311 pid_mkkey(rfs4_entry_t u_entry)
2312 {
2313         rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2314 
2315         return ((void *)(uintptr_t)lo->rl_pid);
2316 }
2317 
2318 static bool_t
2319 pid_compare(rfs4_entry_t u_entry, void *key)
2320 {
2321         rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2322 
2323         return (lo->rl_pid == (pid_t)(uintptr_t)key);
2324 }
2325 
2326 static void
2327 rfs4_lockowner_destroy(rfs4_entry_t u_entry)
2328 {
2329         rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2330 
2331         /* Free the lock owner id */
2332         kmem_free(lo->rl_owner.owner_val, lo->rl_owner.owner_len);
2333         rfs4_client_rele(lo->rl_client);
2334 }
2335 
2336 void
2337 rfs4_lockowner_rele(rfs4_lockowner_t *lo)
2338 {
2339         rfs4_dbe_rele(lo->rl_dbe);
2340 }
2341 
2342 /* ARGSUSED */
2343 static bool_t
2344 rfs4_lockowner_expiry(rfs4_entry_t u_entry)
2345 {
2346         /*
2347          * Since expiry is called with no other references on
2348          * this struct, go ahead and have it removed.
2349          */
2350         return (TRUE);
2351 }
2352 
2353 static bool_t
2354 rfs4_lockowner_create(rfs4_entry_t u_entry, void *arg)
2355 {
2356         rfs4_lockowner_t *lo = (rfs4_lockowner_t *)u_entry;
2357         lock_owner4 *lockowner = (lock_owner4 *)arg;
2358         rfs4_client_t *cp;
2359         bool_t create = FALSE;
2360         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2361 
2362         rw_enter(&nsrv4->rfs4_findclient_lock, RW_READER);
2363 
2364         cp = (rfs4_client_t *)rfs4_dbsearch(nsrv4->rfs4_clientid_idx,
2365             &lockowner->clientid,
2366             &create, NULL, RFS4_DBS_VALID);
2367 
2368         rw_exit(&nsrv4->rfs4_findclient_lock);
2369 
2370         if (cp == NULL)
2371                 return (FALSE);
2372 
2373         /* Reference client */
2374         lo->rl_client = cp;
2375         lo->rl_owner.clientid = lockowner->clientid;
2376         lo->rl_owner.owner_val = kmem_alloc(lockowner->owner_len, KM_SLEEP);
2377         bcopy(lockowner->owner_val, lo->rl_owner.owner_val,
2378             lockowner->owner_len);
2379         lo->rl_owner.owner_len = lockowner->owner_len;
2380         lo->rl_pid = rfs4_dbe_getid(lo->rl_dbe);
2381 
2382         return (TRUE);
2383 }
2384 
2385 rfs4_lockowner_t *
2386 rfs4_findlockowner(lock_owner4 *lockowner, bool_t *create)
2387 {
2388         rfs4_lockowner_t *lo;
2389         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2390 
2391         /* CSTYLED */
2392         lo = (rfs4_lockowner_t *)rfs4_dbsearch(nsrv4->rfs4_lockowner_idx, lockowner,
2393             create, lockowner, RFS4_DBS_VALID);
2394 
2395         return (lo);
2396 }
2397 
2398 rfs4_lockowner_t *
2399 rfs4_findlockowner_by_pid(pid_t pid)
2400 {
2401         rfs4_lockowner_t *lo;
2402         bool_t create = FALSE;
2403         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2404 
2405         lo = (rfs4_lockowner_t *)rfs4_dbsearch(nsrv4->rfs4_lockowner_pid_idx,
2406             (void *)(uintptr_t)pid, &create, NULL, RFS4_DBS_VALID);
2407 
2408         return (lo);
2409 }
2410 
2411 
2412 static uint32_t
2413 file_hash(void *key)
2414 {
2415         return (ADDRHASH(key));
2416 }
2417 
2418 static void *
2419 file_mkkey(rfs4_entry_t u_entry)
2420 {
2421         rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2422 
2423         return (fp->rf_vp);
2424 }
2425 
2426 static bool_t
2427 file_compare(rfs4_entry_t u_entry, void *key)
2428 {
2429         rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2430 
2431         return (fp->rf_vp == (vnode_t *)key);
2432 }
2433 
2434 static void
2435 rfs4_file_destroy(rfs4_entry_t u_entry)
2436 {
2437         rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2438 
2439         list_destroy(&fp->rf_delegstatelist);
2440 
2441         if (fp->rf_filehandle.nfs_fh4_val)
2442                 kmem_free(fp->rf_filehandle.nfs_fh4_val,
2443                     fp->rf_filehandle.nfs_fh4_len);
2444         cv_destroy(fp->rf_dinfo.rd_recall_cv);
2445         if (fp->rf_vp) {
2446                 vnode_t *vp = fp->rf_vp;
2447 
2448                 mutex_enter(&vp->v_vsd_lock);
2449                 (void) vsd_set(vp, nfs4_srv_vkey, NULL);
2450                 mutex_exit(&vp->v_vsd_lock);
2451                 VN_RELE(vp);
2452                 fp->rf_vp = NULL;
2453         }
2454         rw_destroy(&fp->rf_file_rwlock);
2455 }
2456 
2457 /*
2458  * Used to unlock the underlying dbe struct only
2459  */
2460 void
2461 rfs4_file_rele(rfs4_file_t *fp)
2462 {
2463         rfs4_dbe_rele(fp->rf_dbe);
2464 }
2465 
2466 typedef struct {
2467     vnode_t *vp;
2468     nfs_fh4 *fh;
2469 } rfs4_fcreate_arg;
2470 
2471 static bool_t
2472 rfs4_file_create(rfs4_entry_t u_entry, void *arg)
2473 {
2474         rfs4_file_t *fp = (rfs4_file_t *)u_entry;
2475         rfs4_fcreate_arg *ap = (rfs4_fcreate_arg *)arg;
2476         vnode_t *vp = ap->vp;
2477         nfs_fh4 *fh = ap->fh;
2478 
2479         VN_HOLD(vp);
2480 
2481         fp->rf_filehandle.nfs_fh4_len = 0;
2482         fp->rf_filehandle.nfs_fh4_val = NULL;
2483         ASSERT(fh && fh->nfs_fh4_len);
2484         if (fh && fh->nfs_fh4_len) {
2485                 fp->rf_filehandle.nfs_fh4_val =
2486                     kmem_alloc(fh->nfs_fh4_len, KM_SLEEP);
2487                 nfs_fh4_copy(fh, &fp->rf_filehandle);
2488         }
2489         fp->rf_vp = vp;
2490 
2491         list_create(&fp->rf_delegstatelist, sizeof (rfs4_deleg_state_t),
2492             offsetof(rfs4_deleg_state_t, rds_node));
2493 
2494         fp->rf_share_deny = fp->rf_share_access = fp->rf_access_read = 0;
2495         fp->rf_access_write = fp->rf_deny_read = fp->rf_deny_write = 0;
2496 
2497         mutex_init(fp->rf_dinfo.rd_recall_lock, NULL, MUTEX_DEFAULT, NULL);
2498         cv_init(fp->rf_dinfo.rd_recall_cv, NULL, CV_DEFAULT, NULL);
2499 
2500         fp->rf_dinfo.rd_dtype = OPEN_DELEGATE_NONE;
2501 
2502         rw_init(&fp->rf_file_rwlock, NULL, RW_DEFAULT, NULL);
2503 
2504         mutex_enter(&vp->v_vsd_lock);
2505         VERIFY(vsd_set(vp, nfs4_srv_vkey, (void *)fp) == 0);
2506         mutex_exit(&vp->v_vsd_lock);
2507 
2508         return (TRUE);
2509 }
2510 
2511 rfs4_file_t *
2512 rfs4_findfile(vnode_t *vp, nfs_fh4 *fh, bool_t *create)
2513 {
2514         rfs4_file_t *fp;
2515         rfs4_fcreate_arg arg;
2516         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2517 
2518         arg.vp = vp;
2519         arg.fh = fh;
2520 
2521         if (*create == TRUE)
2522                 /* CSTYLED */
2523                 fp = (rfs4_file_t *)rfs4_dbsearch(nsrv4->rfs4_file_idx, vp, create,
2524                     &arg, RFS4_DBS_VALID);
2525         else {
2526                 mutex_enter(&vp->v_vsd_lock);
2527                 fp = (rfs4_file_t *)vsd_get(vp, nfs4_srv_vkey);
2528                 if (fp) {
2529                         rfs4_dbe_lock(fp->rf_dbe);
2530                         if (rfs4_dbe_is_invalid(fp->rf_dbe) ||
2531                             (rfs4_dbe_refcnt(fp->rf_dbe) == 0)) {
2532                                 rfs4_dbe_unlock(fp->rf_dbe);
2533                                 fp = NULL;
2534                         } else {
2535                                 rfs4_dbe_hold(fp->rf_dbe);
2536                                 rfs4_dbe_unlock(fp->rf_dbe);
2537                         }
2538                 }
2539                 mutex_exit(&vp->v_vsd_lock);
2540         }
2541         return (fp);
2542 }
2543 
2544 /*
2545  * Find a file in the db and once it is located, take the rw lock.
2546  * Need to check the vnode pointer and if it does not exist (it was
2547  * removed between the db location and check) redo the find.  This
2548  * assumes that a file struct that has a NULL vnode pointer is marked
2549  * at 'invalid' and will not be found in the db the second time
2550  * around.
2551  */
2552 rfs4_file_t *
2553 rfs4_findfile_withlock(vnode_t *vp, nfs_fh4 *fh, bool_t *create)
2554 {
2555         rfs4_file_t *fp;
2556         rfs4_fcreate_arg arg;
2557         bool_t screate = *create;
2558         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2559 
2560         if (screate == FALSE) {
2561                 mutex_enter(&vp->v_vsd_lock);
2562                 fp = (rfs4_file_t *)vsd_get(vp, nfs4_srv_vkey);
2563                 if (fp) {
2564                         rfs4_dbe_lock(fp->rf_dbe);
2565                         if (rfs4_dbe_is_invalid(fp->rf_dbe) ||
2566                             (rfs4_dbe_refcnt(fp->rf_dbe) == 0)) {
2567                                 rfs4_dbe_unlock(fp->rf_dbe);
2568                                 mutex_exit(&vp->v_vsd_lock);
2569                                 fp = NULL;
2570                         } else {
2571                                 rfs4_dbe_hold(fp->rf_dbe);
2572                                 rfs4_dbe_unlock(fp->rf_dbe);
2573                                 mutex_exit(&vp->v_vsd_lock);
2574                                 rw_enter(&fp->rf_file_rwlock, RW_WRITER);
2575                                 if (fp->rf_vp == NULL) {
2576                                         rw_exit(&fp->rf_file_rwlock);
2577                                         rfs4_file_rele(fp);
2578                                         fp = NULL;
2579                                 }
2580                         }
2581                 } else {
2582                         mutex_exit(&vp->v_vsd_lock);
2583                 }
2584         } else {
2585 retry:
2586                 arg.vp = vp;
2587                 arg.fh = fh;
2588 
2589                 fp = (rfs4_file_t *)rfs4_dbsearch(nsrv4->rfs4_file_idx, vp,
2590                     create, &arg, RFS4_DBS_VALID);
2591                 if (fp != NULL) {
2592                         rw_enter(&fp->rf_file_rwlock, RW_WRITER);
2593                         if (fp->rf_vp == NULL) {
2594                                 rw_exit(&fp->rf_file_rwlock);
2595                                 rfs4_file_rele(fp);
2596                                 *create = screate;
2597                                 goto retry;
2598                         }
2599                 }
2600         }
2601 
2602         return (fp);
2603 }
2604 
2605 static uint32_t
2606 lo_state_hash(void *key)
2607 {
2608         stateid_t *id = key;
2609 
2610         return (id->bits.ident+id->bits.pid);
2611 }
2612 
2613 static bool_t
2614 lo_state_compare(rfs4_entry_t u_entry, void *key)
2615 {
2616         rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2617         stateid_t *id = key;
2618         bool_t rc;
2619 
2620         rc = (lsp->rls_lockid.bits.boottime == id->bits.boottime &&
2621             lsp->rls_lockid.bits.type == id->bits.type &&
2622             lsp->rls_lockid.bits.ident == id->bits.ident &&
2623             lsp->rls_lockid.bits.pid == id->bits.pid);
2624 
2625         return (rc);
2626 }
2627 
2628 static void *
2629 lo_state_mkkey(rfs4_entry_t u_entry)
2630 {
2631         rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2632 
2633         return (&lsp->rls_lockid);
2634 }
2635 
2636 static bool_t
2637 rfs4_lo_state_expiry(rfs4_entry_t u_entry)
2638 {
2639         rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2640 
2641         if (rfs4_dbe_is_invalid(lsp->rls_dbe))
2642                 return (TRUE);
2643         if (lsp->rls_state->rs_closed)
2644                 return (TRUE);
2645         return ((gethrestime_sec() -
2646             lsp->rls_state->rs_owner->ro_client->rc_last_access
2647             > rfs4_lease_time));
2648 }
2649 
2650 static void
2651 rfs4_lo_state_destroy(rfs4_entry_t u_entry)
2652 {
2653         rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2654 
2655         rfs4_dbe_lock(lsp->rls_state->rs_dbe);
2656         list_remove(&lsp->rls_state->rs_lostatelist, lsp);
2657         rfs4_dbe_unlock(lsp->rls_state->rs_dbe);
2658 
2659         rfs4_sw_destroy(&lsp->rls_sw);
2660 
2661         /* Make sure to release the file locks */
2662         if (lsp->rls_locks_cleaned == FALSE) {
2663                 lsp->rls_locks_cleaned = TRUE;
2664                 if (lsp->rls_locker->rl_client->rc_sysidt != LM_NOSYSID) {
2665                         /* Is the PxFS kernel module loaded? */
2666                         if (lm_remove_file_locks != NULL) {
2667                                 int new_sysid;
2668 
2669                                 /* Encode the cluster nodeid in new sysid */
2670                                 new_sysid =
2671                                     lsp->rls_locker->rl_client->rc_sysidt;
2672                                 lm_set_nlmid_flk(&new_sysid);
2673 
2674                                 /*
2675                                  * This PxFS routine removes file locks for a
2676                                  * client over all nodes of a cluster.
2677                                  */
2678                                 DTRACE_PROBE1(nfss_i_clust_rm_lck,
2679                                     int, new_sysid);
2680                                 (*lm_remove_file_locks)(new_sysid);
2681                         } else {
2682                                 (void) cleanlocks(
2683                                     lsp->rls_state->rs_finfo->rf_vp,
2684                                     lsp->rls_locker->rl_pid,
2685                                     lsp->rls_locker->rl_client->rc_sysidt);
2686                         }
2687                 }
2688         }
2689 
2690         /* Free the last reply for this state */
2691         rfs4_free_reply(&lsp->rls_reply);
2692 
2693         rfs4_lockowner_rele(lsp->rls_locker);
2694         lsp->rls_locker = NULL;
2695 
2696         rfs4_state_rele_nounlock(lsp->rls_state);
2697         lsp->rls_state = NULL;
2698 }
2699 
2700 static bool_t
2701 rfs4_lo_state_create(rfs4_entry_t u_entry, void *arg)
2702 {
2703         rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2704         rfs4_lo_state_t *argp = (rfs4_lo_state_t *)arg;
2705         rfs4_lockowner_t *lo = argp->rls_locker;
2706         rfs4_state_t *sp = argp->rls_state;
2707 
2708         lsp->rls_state = sp;
2709 
2710         lsp->rls_lockid = sp->rs_stateid;
2711         lsp->rls_lockid.bits.type = LOCKID;
2712         lsp->rls_lockid.bits.chgseq = 0;
2713         lsp->rls_lockid.bits.pid = lo->rl_pid;
2714 
2715         lsp->rls_locks_cleaned = FALSE;
2716         lsp->rls_lock_completed = FALSE;
2717 
2718         rfs4_sw_init(&lsp->rls_sw);
2719 
2720         /* Attached the supplied lock owner */
2721         rfs4_dbe_hold(lo->rl_dbe);
2722         lsp->rls_locker = lo;
2723 
2724         rfs4_dbe_lock(sp->rs_dbe);
2725         list_insert_tail(&sp->rs_lostatelist, lsp);
2726         rfs4_dbe_hold(sp->rs_dbe);
2727         rfs4_dbe_unlock(sp->rs_dbe);
2728 
2729         return (TRUE);
2730 }
2731 
2732 void
2733 rfs4_lo_state_rele(rfs4_lo_state_t *lsp, bool_t unlock_fp)
2734 {
2735         if (unlock_fp == TRUE)
2736                 rw_exit(&lsp->rls_state->rs_finfo->rf_file_rwlock);
2737         rfs4_dbe_rele(lsp->rls_dbe);
2738 }
2739 
2740 static rfs4_lo_state_t *
2741 rfs4_findlo_state(stateid_t *id, bool_t lock_fp)
2742 {
2743         rfs4_lo_state_t *lsp;
2744         bool_t create = FALSE;
2745         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2746 
2747         lsp = (rfs4_lo_state_t *)rfs4_dbsearch(nsrv4->rfs4_lo_state_idx, id,
2748             &create, NULL, RFS4_DBS_VALID);
2749         if (lock_fp == TRUE && lsp != NULL)
2750                 rw_enter(&lsp->rls_state->rs_finfo->rf_file_rwlock, RW_READER);
2751 
2752         return (lsp);
2753 }
2754 
2755 
2756 static uint32_t
2757 lo_state_lo_hash(void *key)
2758 {
2759         rfs4_lo_state_t *lsp = key;
2760 
2761         return (ADDRHASH(lsp->rls_locker) ^ ADDRHASH(lsp->rls_state));
2762 }
2763 
2764 static bool_t
2765 lo_state_lo_compare(rfs4_entry_t u_entry, void *key)
2766 {
2767         rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
2768         rfs4_lo_state_t *keyp = key;
2769 
2770         return (keyp->rls_locker == lsp->rls_locker &&
2771             keyp->rls_state == lsp->rls_state);
2772 }
2773 
2774 static void *
2775 lo_state_lo_mkkey(rfs4_entry_t u_entry)
2776 {
2777         return (u_entry);
2778 }
2779 
2780 rfs4_lo_state_t *
2781 rfs4_findlo_state_by_owner(rfs4_lockowner_t *lo, rfs4_state_t *sp,
2782     bool_t *create)
2783 {
2784         rfs4_lo_state_t *lsp;
2785         rfs4_lo_state_t arg;
2786         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2787 
2788         arg.rls_locker = lo;
2789         arg.rls_state = sp;
2790 
2791         lsp = (rfs4_lo_state_t *)rfs4_dbsearch(nsrv4->rfs4_lo_state_owner_idx,
2792             &arg, create, &arg, RFS4_DBS_VALID);
2793 
2794         return (lsp);
2795 }
2796 
2797 static stateid_t
2798 get_stateid(id_t eid)
2799 {
2800         stateid_t id;
2801         nfs4_srv_t *nsrv4;
2802 
2803         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
2804 
2805         id.bits.boottime = nsrv4->rfs4_start_time;
2806         id.bits.ident = eid;
2807         id.bits.chgseq = 0;
2808         id.bits.type = 0;
2809         id.bits.pid = 0;
2810 
2811         /*
2812          * If we are booted as a cluster node, embed our nodeid.
2813          * We've already done sanity checks in rfs4_client_create() so no
2814          * need to repeat them here.
2815          */
2816         id.bits.clnodeid = (cluster_bootflags & CLUSTER_BOOTED) ?
2817             clconf_get_nodeid() : 0;
2818 
2819         return (id);
2820 }
2821 
2822 /*
2823  * For use only when booted as a cluster node.
2824  * Returns TRUE if the embedded nodeid indicates that this stateid was
2825  * generated on another node.
2826  */
2827 static int
2828 foreign_stateid(stateid_t *id)
2829 {
2830         ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2831         return (id->bits.clnodeid != (uint32_t)clconf_get_nodeid());
2832 }
2833 
2834 /*
2835  * For use only when booted as a cluster node.
2836  * Returns TRUE if the embedded nodeid indicates that this clientid was
2837  * generated on another node.
2838  */
2839 static int
2840 foreign_clientid(cid *cidp)
2841 {
2842         ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2843         return (cidp->impl_id.c_id >> CLUSTER_NODEID_SHIFT !=
2844             (uint32_t)clconf_get_nodeid());
2845 }
2846 
2847 /*
2848  * For use only when booted as a cluster node.
2849  * Embed our cluster nodeid into the clientid.
2850  */
2851 static void
2852 embed_nodeid(cid *cidp)
2853 {
2854         int clnodeid;
2855         /*
2856          * Currently, our state tables are small enough that their
2857          * ids will leave enough bits free for the nodeid. If the
2858          * tables become larger, we mustn't overwrite the id.
2859          * Equally, we only have room for so many bits of nodeid, so
2860          * must check that too.
2861          */
2862         ASSERT(cluster_bootflags & CLUSTER_BOOTED);
2863         ASSERT(cidp->impl_id.c_id >> CLUSTER_NODEID_SHIFT == 0);
2864         clnodeid = clconf_get_nodeid();
2865         ASSERT(clnodeid <= CLUSTER_MAX_NODEID);
2866         ASSERT(clnodeid != NODEID_UNKNOWN);
2867         cidp->impl_id.c_id |= (clnodeid << CLUSTER_NODEID_SHIFT);
2868 }
2869 
2870 static uint32_t
2871 state_hash(void *key)
2872 {
2873         stateid_t *ip = (stateid_t *)key;
2874 
2875         return (ip->bits.ident);
2876 }
2877 
2878 static bool_t
2879 state_compare(rfs4_entry_t u_entry, void *key)
2880 {
2881         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2882         stateid_t *id = (stateid_t *)key;
2883         bool_t rc;
2884 
2885         rc = (sp->rs_stateid.bits.boottime == id->bits.boottime &&
2886             sp->rs_stateid.bits.ident == id->bits.ident);
2887 
2888         return (rc);
2889 }
2890 
2891 static void *
2892 state_mkkey(rfs4_entry_t u_entry)
2893 {
2894         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2895 
2896         return (&sp->rs_stateid);
2897 }
2898 
2899 static void
2900 rfs4_state_destroy(rfs4_entry_t u_entry)
2901 {
2902         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
2903 
2904         /* remove from openowner list */
2905         rfs4_dbe_lock(sp->rs_owner->ro_dbe);
2906         list_remove(&sp->rs_owner->ro_statelist, sp);
2907         rfs4_dbe_unlock(sp->rs_owner->ro_dbe);
2908 
2909         list_destroy(&sp->rs_lostatelist);
2910 
2911         /* release any share locks for this stateid if it's still open */
2912         if (!sp->rs_closed) {
2913                 rfs4_dbe_lock(sp->rs_dbe);
2914                 (void) rfs4_unshare(sp);
2915                 rfs4_dbe_unlock(sp->rs_dbe);
2916         }
2917 
2918         /* Were done with the file */
2919         rfs4_file_rele(sp->rs_finfo);
2920         sp->rs_finfo = NULL;
2921 
2922         /* And now with the openowner */
2923         rfs4_openowner_rele(sp->rs_owner);
2924         sp->rs_owner = NULL;
2925 }
2926 
2927 static void
2928 rfs4_state_rele_nounlock(rfs4_state_t *sp)
2929 {
2930         rfs4_dbe_rele(sp->rs_dbe);
2931 }
2932 
2933 void
2934 rfs4_state_rele(rfs4_state_t *sp)
2935 {
2936         rw_exit(&sp->rs_finfo->rf_file_rwlock);
2937         rfs4_dbe_rele(sp->rs_dbe);
2938 }
2939 
2940 static uint32_t
2941 deleg_hash(void *key)
2942 {
2943         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)key;
2944 
2945         return (ADDRHASH(dsp->rds_client) ^ ADDRHASH(dsp->rds_finfo));
2946 }
2947 
2948 static bool_t
2949 deleg_compare(rfs4_entry_t u_entry, void *key)
2950 {
2951         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2952         rfs4_deleg_state_t *kdsp = (rfs4_deleg_state_t *)key;
2953 
2954         return (dsp->rds_client == kdsp->rds_client &&
2955             dsp->rds_finfo == kdsp->rds_finfo);
2956 }
2957 
2958 static void *
2959 deleg_mkkey(rfs4_entry_t u_entry)
2960 {
2961         return (u_entry);
2962 }
2963 
2964 static uint32_t
2965 deleg_state_hash(void *key)
2966 {
2967         stateid_t *ip = (stateid_t *)key;
2968 
2969         return (ip->bits.ident);
2970 }
2971 
2972 static bool_t
2973 deleg_state_compare(rfs4_entry_t u_entry, void *key)
2974 {
2975         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2976         stateid_t *id = (stateid_t *)key;
2977         bool_t rc;
2978 
2979         if (id->bits.type != DELEGID)
2980                 return (FALSE);
2981 
2982         rc = (dsp->rds_delegid.bits.boottime == id->bits.boottime &&
2983             dsp->rds_delegid.bits.ident == id->bits.ident);
2984 
2985         return (rc);
2986 }
2987 
2988 static void *
2989 deleg_state_mkkey(rfs4_entry_t u_entry)
2990 {
2991         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
2992 
2993         return (&dsp->rds_delegid);
2994 }
2995 
2996 static bool_t
2997 rfs4_deleg_state_expiry(rfs4_entry_t u_entry)
2998 {
2999         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
3000 
3001         if (rfs4_dbe_is_invalid(dsp->rds_dbe))
3002                 return (TRUE);
3003 
3004         if (dsp->rds_dtype == OPEN_DELEGATE_NONE)
3005                 return (TRUE);
3006 
3007         if ((gethrestime_sec() - dsp->rds_client->rc_last_access
3008             > rfs4_lease_time)) {
3009                 rfs4_dbe_invalidate(dsp->rds_dbe);
3010                 return (TRUE);
3011         }
3012 
3013         return (FALSE);
3014 }
3015 
3016 static bool_t
3017 rfs4_deleg_state_create(rfs4_entry_t u_entry, void *argp)
3018 {
3019         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
3020         rfs4_file_t *fp = ((rfs4_deleg_state_t *)argp)->rds_finfo;
3021         rfs4_client_t *cp = ((rfs4_deleg_state_t *)argp)->rds_client;
3022 
3023         rfs4_dbe_hold(fp->rf_dbe);
3024         rfs4_dbe_hold(cp->rc_dbe);
3025 
3026         dsp->rds_delegid = get_stateid(rfs4_dbe_getid(dsp->rds_dbe));
3027         dsp->rds_delegid.bits.type = DELEGID;
3028         dsp->rds_finfo = fp;
3029         dsp->rds_client = cp;
3030         dsp->rds_dtype = OPEN_DELEGATE_NONE;
3031 
3032         dsp->rds_time_granted = gethrestime_sec();   /* observability */
3033         dsp->rds_time_revoked = 0;
3034 
3035         list_link_init(&dsp->rds_node);
3036 
3037         return (TRUE);
3038 }
3039 
3040 static void
3041 rfs4_deleg_state_destroy(rfs4_entry_t u_entry)
3042 {
3043         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
3044 
3045         /* return delegation if necessary */
3046         rfs4_return_deleg(dsp, FALSE);
3047 
3048         /* Were done with the file */
3049         rfs4_file_rele(dsp->rds_finfo);
3050         dsp->rds_finfo = NULL;
3051 
3052         /* And now with the openowner */
3053         rfs4_client_rele(dsp->rds_client);
3054         dsp->rds_client = NULL;
3055 }
3056 
3057 rfs4_deleg_state_t *
3058 rfs4_finddeleg(rfs4_state_t *sp, bool_t *create)
3059 {
3060         rfs4_deleg_state_t ds, *dsp;
3061         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
3062 
3063         ds.rds_client = sp->rs_owner->ro_client;
3064         ds.rds_finfo = sp->rs_finfo;
3065 
3066         dsp = (rfs4_deleg_state_t *)rfs4_dbsearch(nsrv4->rfs4_deleg_idx, &ds,
3067             create, &ds, RFS4_DBS_VALID);
3068 
3069         return (dsp);
3070 }
3071 
3072 rfs4_deleg_state_t *
3073 rfs4_finddelegstate(stateid_t *id)
3074 {
3075         rfs4_deleg_state_t *dsp;
3076         bool_t create = FALSE;
3077         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
3078 
3079         dsp = (rfs4_deleg_state_t *)rfs4_dbsearch(nsrv4->rfs4_deleg_state_idx,
3080             id, &create, NULL, RFS4_DBS_VALID);
3081 
3082         return (dsp);
3083 }
3084 
3085 void
3086 rfs4_deleg_state_rele(rfs4_deleg_state_t *dsp)
3087 {
3088         rfs4_dbe_rele(dsp->rds_dbe);
3089 }
3090 
3091 void
3092 rfs4_update_lock_sequence(rfs4_lo_state_t *lsp)
3093 {
3094 
3095         rfs4_dbe_lock(lsp->rls_dbe);
3096 
3097         /*
3098          * If we are skipping sequence id checking, this means that
3099          * this is the first lock request and therefore the sequence
3100          * id does not need to be updated.  This only happens on the
3101          * first lock request for a lockowner
3102          */
3103         if (!lsp->rls_skip_seqid_check)
3104                 lsp->rls_seqid++;
3105 
3106         rfs4_dbe_unlock(lsp->rls_dbe);
3107 }
3108 
3109 void
3110 rfs4_update_lock_resp(rfs4_lo_state_t *lsp, nfs_resop4 *resp)
3111 {
3112 
3113         rfs4_dbe_lock(lsp->rls_dbe);
3114 
3115         rfs4_free_reply(&lsp->rls_reply);
3116 
3117         rfs4_copy_reply(&lsp->rls_reply, resp);
3118 
3119         rfs4_dbe_unlock(lsp->rls_dbe);
3120 }
3121 
3122 void
3123 rfs4_free_opens(rfs4_openowner_t *oo, bool_t invalidate,
3124     bool_t close_of_client)
3125 {
3126         rfs4_state_t *sp;
3127 
3128         rfs4_dbe_lock(oo->ro_dbe);
3129 
3130         for (sp = list_head(&oo->ro_statelist); sp != NULL;
3131             sp = list_next(&oo->ro_statelist, sp)) {
3132                 rfs4_state_close(sp, FALSE, close_of_client, CRED());
3133                 if (invalidate == TRUE)
3134                         rfs4_dbe_invalidate(sp->rs_dbe);
3135         }
3136 
3137         rfs4_dbe_invalidate(oo->ro_dbe);
3138         rfs4_dbe_unlock(oo->ro_dbe);
3139 }
3140 
3141 static uint32_t
3142 state_owner_file_hash(void *key)
3143 {
3144         rfs4_state_t *sp = key;
3145 
3146         return (ADDRHASH(sp->rs_owner) ^ ADDRHASH(sp->rs_finfo));
3147 }
3148 
3149 static bool_t
3150 state_owner_file_compare(rfs4_entry_t u_entry, void *key)
3151 {
3152         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3153         rfs4_state_t *arg = key;
3154 
3155         if (sp->rs_closed == TRUE)
3156                 return (FALSE);
3157 
3158         return (arg->rs_owner == sp->rs_owner && arg->rs_finfo == sp->rs_finfo);
3159 }
3160 
3161 static void *
3162 state_owner_file_mkkey(rfs4_entry_t u_entry)
3163 {
3164         return (u_entry);
3165 }
3166 
3167 static uint32_t
3168 state_file_hash(void *key)
3169 {
3170         return (ADDRHASH(key));
3171 }
3172 
3173 static bool_t
3174 state_file_compare(rfs4_entry_t u_entry, void *key)
3175 {
3176         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3177         rfs4_file_t *fp = key;
3178 
3179         if (sp->rs_closed == TRUE)
3180                 return (FALSE);
3181 
3182         return (fp == sp->rs_finfo);
3183 }
3184 
3185 static void *
3186 state_file_mkkey(rfs4_entry_t u_entry)
3187 {
3188         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3189 
3190         return (sp->rs_finfo);
3191 }
3192 
3193 rfs4_state_t *
3194 rfs4_findstate_by_owner_file(rfs4_openowner_t *oo, rfs4_file_t *fp,
3195     bool_t *create)
3196 {
3197         rfs4_state_t *sp;
3198         rfs4_state_t key;
3199         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
3200 
3201         key.rs_owner = oo;
3202         key.rs_finfo = fp;
3203 
3204         sp = (rfs4_state_t *)rfs4_dbsearch(nsrv4->rfs4_state_owner_file_idx,
3205             &key, create, &key, RFS4_DBS_VALID);
3206 
3207         return (sp);
3208 }
3209 
3210 /* This returns ANY state struct that refers to this file */
3211 static rfs4_state_t *
3212 rfs4_findstate_by_file(rfs4_file_t *fp)
3213 {
3214         bool_t create = FALSE;
3215         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
3216 
3217         return ((rfs4_state_t *)rfs4_dbsearch(nsrv4->rfs4_state_file_idx, fp,
3218             &create, fp, RFS4_DBS_VALID));
3219 }
3220 
3221 static bool_t
3222 rfs4_state_expiry(rfs4_entry_t u_entry)
3223 {
3224         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3225 
3226         if (rfs4_dbe_is_invalid(sp->rs_dbe))
3227                 return (TRUE);
3228 
3229         if (sp->rs_closed == TRUE &&
3230             ((gethrestime_sec() - rfs4_dbe_get_timerele(sp->rs_dbe))
3231             > rfs4_lease_time))
3232                 return (TRUE);
3233 
3234         return ((gethrestime_sec() - sp->rs_owner->ro_client->rc_last_access
3235             > rfs4_lease_time));
3236 }
3237 
3238 static bool_t
3239 rfs4_state_create(rfs4_entry_t u_entry, void *argp)
3240 {
3241         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3242         rfs4_file_t *fp = ((rfs4_state_t *)argp)->rs_finfo;
3243         rfs4_openowner_t *oo = ((rfs4_state_t *)argp)->rs_owner;
3244 
3245         rfs4_dbe_hold(fp->rf_dbe);
3246         rfs4_dbe_hold(oo->ro_dbe);
3247         sp->rs_stateid = get_stateid(rfs4_dbe_getid(sp->rs_dbe));
3248         sp->rs_stateid.bits.type = OPENID;
3249         sp->rs_owner = oo;
3250         sp->rs_finfo = fp;
3251 
3252         list_create(&sp->rs_lostatelist, sizeof (rfs4_lo_state_t),
3253             offsetof(rfs4_lo_state_t, rls_node));
3254 
3255         /* Insert state on per open owner's list */
3256         rfs4_dbe_lock(oo->ro_dbe);
3257         list_insert_tail(&oo->ro_statelist, sp);
3258         rfs4_dbe_unlock(oo->ro_dbe);
3259 
3260         return (TRUE);
3261 }
3262 
3263 static rfs4_state_t *
3264 rfs4_findstate(stateid_t *id, rfs4_dbsearch_type_t find_invalid, bool_t lock_fp)
3265 {
3266         rfs4_state_t *sp;
3267         bool_t create = FALSE;
3268         nfs4_srv_t *nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
3269 
3270         sp = (rfs4_state_t *)rfs4_dbsearch(nsrv4->rfs4_state_idx, id,
3271             &create, NULL, find_invalid);
3272         if (lock_fp == TRUE && sp != NULL)
3273                 rw_enter(&sp->rs_finfo->rf_file_rwlock, RW_READER);
3274 
3275         return (sp);
3276 }
3277 
3278 void
3279 rfs4_state_close(rfs4_state_t *sp, bool_t lock_held, bool_t close_of_client,
3280     cred_t *cr)
3281 {
3282         /* Remove the associated lo_state owners */
3283         if (!lock_held)
3284                 rfs4_dbe_lock(sp->rs_dbe);
3285 
3286         /*
3287          * If refcnt == 0, the dbe is about to be destroyed.
3288          * lock state will be released by the reaper thread.
3289          */
3290 
3291         if (rfs4_dbe_refcnt(sp->rs_dbe) > 0) {
3292                 if (sp->rs_closed == FALSE) {
3293                         rfs4_release_share_lock_state(sp, cr, close_of_client);
3294                         sp->rs_closed = TRUE;
3295                 }
3296         }
3297 
3298         if (!lock_held)
3299                 rfs4_dbe_unlock(sp->rs_dbe);
3300 }
3301 
3302 /*
3303  * Remove all state associated with the given client.
3304  */
3305 void
3306 rfs4_client_state_remove(rfs4_client_t *cp)
3307 {
3308         rfs4_openowner_t *oo;
3309 
3310         rfs4_dbe_lock(cp->rc_dbe);
3311 
3312         for (oo = list_head(&cp->rc_openownerlist); oo != NULL;
3313             oo = list_next(&cp->rc_openownerlist, oo)) {
3314                 rfs4_free_opens(oo, TRUE, TRUE);
3315         }
3316 
3317         rfs4_dbe_unlock(cp->rc_dbe);
3318 }
3319 
3320 void
3321 rfs4_client_close(rfs4_client_t *cp)
3322 {
3323         /* Mark client as going away. */
3324         rfs4_dbe_lock(cp->rc_dbe);
3325         rfs4_dbe_invalidate(cp->rc_dbe);
3326         rfs4_dbe_unlock(cp->rc_dbe);
3327 
3328         rfs4_client_state_remove(cp);
3329 
3330         /* Release the client */
3331         rfs4_client_rele(cp);
3332 }
3333 
3334 nfsstat4
3335 rfs4_check_clientid(clientid4 *cp, int setclid_confirm)
3336 {
3337         cid *cidp = (cid *) cp;
3338         nfs4_srv_t *nsrv4;
3339 
3340         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
3341 
3342         /*
3343          * If we are booted as a cluster node, check the embedded nodeid.
3344          * If it indicates that this clientid was generated on another node,
3345          * inform the client accordingly.
3346          */
3347         if (cluster_bootflags & CLUSTER_BOOTED && foreign_clientid(cidp))
3348                 return (NFS4ERR_STALE_CLIENTID);
3349 
3350         /*
3351          * If the server start time matches the time provided
3352          * by the client (via the clientid) and this is NOT a
3353          * setclientid_confirm then return EXPIRED.
3354          */
3355         if (!setclid_confirm &&
3356             cidp->impl_id.start_time == nsrv4->rfs4_start_time)
3357                 return (NFS4ERR_EXPIRED);
3358 
3359         return (NFS4ERR_STALE_CLIENTID);
3360 }
3361 
3362 /*
3363  * This is used when a stateid has not been found amongst the
3364  * current server's state.  Check the stateid to see if it
3365  * was from this server instantiation or not.
3366  */
3367 static nfsstat4
3368 what_stateid_error(stateid_t *id, stateid_type_t type)
3369 {
3370         nfs4_srv_t *nsrv4;
3371 
3372         nsrv4 = zone_getspecific(rfs4_zone_key, curzone);
3373 
3374         /* If we are booted as a cluster node, was stateid locally generated? */
3375         if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3376                 return (NFS4ERR_STALE_STATEID);
3377 
3378         /* If types don't match then no use checking further */
3379         if (type != id->bits.type)
3380                 return (NFS4ERR_BAD_STATEID);
3381 
3382         /* From a different server instantiation, return STALE */
3383         if (id->bits.boottime != nsrv4->rfs4_start_time)
3384                 return (NFS4ERR_STALE_STATEID);
3385 
3386         /*
3387          * From this server but the state is most likely beyond lease
3388          * timeout: return NFS4ERR_EXPIRED.  However, there is the
3389          * case of a delegation stateid.  For delegations, there is a
3390          * case where the state can be removed without the client's
3391          * knowledge/consent: revocation.  In the case of delegation
3392          * revocation, the delegation state will be removed and will
3393          * not be found.  If the client does something like a
3394          * DELEGRETURN or even a READ/WRITE with a delegatoin stateid
3395          * that has been revoked, the server should return BAD_STATEID
3396          * instead of the more common EXPIRED error.
3397          */
3398         if (id->bits.boottime == nsrv4->rfs4_start_time) {
3399                 if (type == DELEGID)
3400                         return (NFS4ERR_BAD_STATEID);
3401                 else
3402                         return (NFS4ERR_EXPIRED);
3403         }
3404 
3405         return (NFS4ERR_BAD_STATEID);
3406 }
3407 
3408 /*
3409  * Used later on to find the various state structs.  When called from
3410  * rfs4_check_stateid()->rfs4_get_all_state(), no file struct lock is
3411  * taken (it is not needed) and helps on the read/write path with
3412  * respect to performance.
3413  */
3414 static nfsstat4
3415 rfs4_get_state_lockit(stateid4 *stateid, rfs4_state_t **spp,
3416     rfs4_dbsearch_type_t find_invalid, bool_t lock_fp)
3417 {
3418         stateid_t *id = (stateid_t *)stateid;
3419         rfs4_state_t *sp;
3420 
3421         *spp = NULL;
3422 
3423         /* If we are booted as a cluster node, was stateid locally generated? */
3424         if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3425                 return (NFS4ERR_STALE_STATEID);
3426 
3427         sp = rfs4_findstate(id, find_invalid, lock_fp);
3428         if (sp == NULL) {
3429                 return (what_stateid_error(id, OPENID));
3430         }
3431 
3432         if (rfs4_lease_expired(sp->rs_owner->ro_client)) {
3433                 if (lock_fp == TRUE)
3434                         rfs4_state_rele(sp);
3435                 else
3436                         rfs4_state_rele_nounlock(sp);
3437                 return (NFS4ERR_EXPIRED);
3438         }
3439 
3440         *spp = sp;
3441 
3442         return (NFS4_OK);
3443 }
3444 
3445 nfsstat4
3446 rfs4_get_state(stateid4 *stateid, rfs4_state_t **spp,
3447     rfs4_dbsearch_type_t find_invalid)
3448 {
3449         return (rfs4_get_state_lockit(stateid, spp, find_invalid, TRUE));
3450 }
3451 
3452 int
3453 rfs4_check_stateid_seqid(rfs4_state_t *sp, stateid4 *stateid)
3454 {
3455         stateid_t *id = (stateid_t *)stateid;
3456 
3457         if (rfs4_lease_expired(sp->rs_owner->ro_client))
3458                 return (NFS4_CHECK_STATEID_EXPIRED);
3459 
3460         /* Stateid is some time in the future - that's bad */
3461         if (sp->rs_stateid.bits.chgseq < id->bits.chgseq)
3462                 return (NFS4_CHECK_STATEID_BAD);
3463 
3464         if (sp->rs_stateid.bits.chgseq == id->bits.chgseq + 1)
3465                 return (NFS4_CHECK_STATEID_REPLAY);
3466 
3467         /* Stateid is some time in the past - that's old */
3468         if (sp->rs_stateid.bits.chgseq > id->bits.chgseq)
3469                 return (NFS4_CHECK_STATEID_OLD);
3470 
3471         /* Caller needs to know about confirmation before closure */
3472         if (sp->rs_owner->ro_need_confirm)
3473                 return (NFS4_CHECK_STATEID_UNCONFIRMED);
3474 
3475         if (sp->rs_closed == TRUE)
3476                 return (NFS4_CHECK_STATEID_CLOSED);
3477 
3478         return (NFS4_CHECK_STATEID_OKAY);
3479 }
3480 
3481 int
3482 rfs4_check_lo_stateid_seqid(rfs4_lo_state_t *lsp, stateid4 *stateid)
3483 {
3484         stateid_t *id = (stateid_t *)stateid;
3485 
3486         if (rfs4_lease_expired(lsp->rls_state->rs_owner->ro_client))
3487                 return (NFS4_CHECK_STATEID_EXPIRED);
3488 
3489         /* Stateid is some time in the future - that's bad */
3490         if (lsp->rls_lockid.bits.chgseq < id->bits.chgseq)
3491                 return (NFS4_CHECK_STATEID_BAD);
3492 
3493         if (lsp->rls_lockid.bits.chgseq == id->bits.chgseq + 1)
3494                 return (NFS4_CHECK_STATEID_REPLAY);
3495 
3496         /* Stateid is some time in the past - that's old */
3497         if (lsp->rls_lockid.bits.chgseq > id->bits.chgseq)
3498                 return (NFS4_CHECK_STATEID_OLD);
3499 
3500         if (lsp->rls_state->rs_closed == TRUE)
3501                 return (NFS4_CHECK_STATEID_CLOSED);
3502 
3503         return (NFS4_CHECK_STATEID_OKAY);
3504 }
3505 
3506 nfsstat4
3507 rfs4_get_deleg_state(stateid4 *stateid, rfs4_deleg_state_t **dspp)
3508 {
3509         stateid_t *id = (stateid_t *)stateid;
3510         rfs4_deleg_state_t *dsp;
3511 
3512         *dspp = NULL;
3513 
3514         /* If we are booted as a cluster node, was stateid locally generated? */
3515         if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3516                 return (NFS4ERR_STALE_STATEID);
3517 
3518         dsp = rfs4_finddelegstate(id);
3519         if (dsp == NULL) {
3520                 return (what_stateid_error(id, DELEGID));
3521         }
3522 
3523         if (rfs4_lease_expired(dsp->rds_client)) {
3524                 rfs4_deleg_state_rele(dsp);
3525                 return (NFS4ERR_EXPIRED);
3526         }
3527 
3528         *dspp = dsp;
3529 
3530         return (NFS4_OK);
3531 }
3532 
3533 nfsstat4
3534 rfs4_get_lo_state(stateid4 *stateid, rfs4_lo_state_t **lspp, bool_t lock_fp)
3535 {
3536         stateid_t *id = (stateid_t *)stateid;
3537         rfs4_lo_state_t *lsp;
3538 
3539         *lspp = NULL;
3540 
3541         /* If we are booted as a cluster node, was stateid locally generated? */
3542         if ((cluster_bootflags & CLUSTER_BOOTED) && foreign_stateid(id))
3543                 return (NFS4ERR_STALE_STATEID);
3544 
3545         lsp = rfs4_findlo_state(id, lock_fp);
3546         if (lsp == NULL) {
3547                 return (what_stateid_error(id, LOCKID));
3548         }
3549 
3550         if (rfs4_lease_expired(lsp->rls_state->rs_owner->ro_client)) {
3551                 rfs4_lo_state_rele(lsp, lock_fp);
3552                 return (NFS4ERR_EXPIRED);
3553         }
3554 
3555         *lspp = lsp;
3556 
3557         return (NFS4_OK);
3558 }
3559 
3560 static nfsstat4
3561 rfs4_get_all_state(stateid4 *sid, rfs4_state_t **spp,
3562     rfs4_deleg_state_t **dspp, rfs4_lo_state_t **lspp)
3563 {
3564         rfs4_state_t *sp = NULL;
3565         rfs4_deleg_state_t *dsp = NULL;
3566         rfs4_lo_state_t *lsp = NULL;
3567         stateid_t *id;
3568         nfsstat4 status;
3569 
3570         *spp = NULL; *dspp = NULL; *lspp = NULL;
3571 
3572         id = (stateid_t *)sid;
3573         switch (id->bits.type) {
3574         case OPENID:
3575                 status = rfs4_get_state_lockit(sid, &sp, FALSE, FALSE);
3576                 break;
3577         case DELEGID:
3578                 status = rfs4_get_deleg_state(sid, &dsp);
3579                 break;
3580         case LOCKID:
3581                 status = rfs4_get_lo_state(sid, &lsp, FALSE);
3582                 if (status == NFS4_OK) {
3583                         sp = lsp->rls_state;
3584                         rfs4_dbe_hold(sp->rs_dbe);
3585                 }
3586                 break;
3587         default:
3588                 status = NFS4ERR_BAD_STATEID;
3589         }
3590 
3591         if (status == NFS4_OK) {
3592                 *spp = sp;
3593                 *dspp = dsp;
3594                 *lspp = lsp;
3595         }
3596 
3597         return (status);
3598 }
3599 
3600 /*
3601  * Given the I/O mode (FREAD or FWRITE), this checks whether the
3602  * rfs4_state_t struct has access to do this operation and if so
3603  * return NFS4_OK; otherwise the proper NFSv4 error is returned.
3604  */
3605 nfsstat4
3606 rfs4_state_has_access(rfs4_state_t *sp, int mode, vnode_t *vp)
3607 {
3608         nfsstat4 stat = NFS4_OK;
3609         rfs4_file_t *fp;
3610         bool_t create = FALSE;
3611 
3612         rfs4_dbe_lock(sp->rs_dbe);
3613         if (mode == FWRITE) {
3614                 if (!(sp->rs_share_access & OPEN4_SHARE_ACCESS_WRITE)) {
3615                         stat = NFS4ERR_OPENMODE;
3616                 }
3617         } else if (mode == FREAD) {
3618                 if (!(sp->rs_share_access & OPEN4_SHARE_ACCESS_READ)) {
3619                         /*
3620                          * If we have OPENed the file with DENYing access
3621                          * to both READ and WRITE then no one else could
3622                          * have OPENed the file, hence no conflicting READ
3623                          * deny.  This check is merely an optimization.
3624                          */
3625                         if (sp->rs_share_deny == OPEN4_SHARE_DENY_BOTH)
3626                                 goto out;
3627 
3628                         /* Check against file struct's DENY mode */
3629                         fp = rfs4_findfile(vp, NULL, &create);
3630                         if (fp != NULL) {
3631                                 int deny_read = 0;
3632                                 rfs4_dbe_lock(fp->rf_dbe);
3633                                 /*
3634                                  * Check if any other open owner has the file
3635                                  * OPENed with deny READ.
3636                                  */
3637                                 if (sp->rs_share_deny & OPEN4_SHARE_DENY_READ)
3638                                         deny_read = 1;
3639                                 ASSERT(fp->rf_deny_read >= deny_read);
3640                                 if (fp->rf_deny_read > deny_read)
3641                                         stat = NFS4ERR_OPENMODE;
3642                                 rfs4_dbe_unlock(fp->rf_dbe);
3643                                 rfs4_file_rele(fp);
3644                         }
3645                 }
3646         } else {
3647                 /* Illegal I/O mode */
3648                 stat = NFS4ERR_INVAL;
3649         }
3650 out:
3651         rfs4_dbe_unlock(sp->rs_dbe);
3652         return (stat);
3653 }
3654 
3655 /*
3656  * Given the I/O mode (FREAD or FWRITE), the vnode, the stateid and whether
3657  * the file is being truncated, return NFS4_OK if allowed or appropriate
3658  * V4 error if not. Note NFS4ERR_DELAY will be returned and a recall on
3659  * the associated file will be done if the I/O is not consistent with any
3660  * delegation in effect on the file. Should be holding VOP_RWLOCK, either
3661  * as reader or writer as appropriate. rfs4_op_open will acquire the
3662  * VOP_RWLOCK as writer when setting up delegation. If the stateid is bad
3663  * this routine will return NFS4ERR_BAD_STATEID. In addition, through the
3664  * deleg parameter, we will return whether a write delegation is held by
3665  * the client associated with this stateid.
3666  * If the server instance associated with the relevant client is in its
3667  * grace period, return NFS4ERR_GRACE.
3668  */
3669 
3670 nfsstat4
3671 rfs4_check_stateid(int mode, vnode_t *vp,
3672     stateid4 *stateid, bool_t trunc, bool_t *deleg,
3673     bool_t do_access, caller_context_t *ct)
3674 {
3675         rfs4_file_t *fp;
3676         bool_t create = FALSE;
3677         rfs4_state_t *sp;
3678         rfs4_deleg_state_t *dsp;
3679         rfs4_lo_state_t *lsp;
3680         stateid_t *id = (stateid_t *)stateid;
3681         nfsstat4 stat = NFS4_OK;
3682 
3683         if (ct != NULL) {
3684                 ct->cc_sysid = 0;
3685                 ct->cc_pid = 0;
3686                 ct->cc_caller_id = nfs4_srv_caller_id;
3687                 ct->cc_flags = CC_DONTBLOCK;
3688         }
3689 
3690         if (ISSPECIAL(stateid)) {
3691                 fp = rfs4_findfile(vp, NULL, &create);
3692                 if (fp == NULL)
3693                         return (NFS4_OK);
3694                 if (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_NONE) {
3695                         rfs4_file_rele(fp);
3696                         return (NFS4_OK);
3697                 }
3698                 if (mode == FWRITE ||
3699                     fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_WRITE) {
3700                         rfs4_recall_deleg(fp, trunc, NULL);
3701                         rfs4_file_rele(fp);
3702                         return (NFS4ERR_DELAY);
3703                 }
3704                 rfs4_file_rele(fp);
3705                 return (NFS4_OK);
3706         } else {
3707                 stat = rfs4_get_all_state(stateid, &sp, &dsp, &lsp);
3708                 if (stat != NFS4_OK)
3709                         return (stat);
3710                 if (lsp != NULL) {
3711                         /* Is associated server instance in its grace period? */
3712                         if (rfs4_clnt_in_grace(lsp->rls_locker->rl_client)) {
3713                                 rfs4_lo_state_rele(lsp, FALSE);
3714                                 if (sp != NULL)
3715                                         rfs4_state_rele_nounlock(sp);
3716                                 return (NFS4ERR_GRACE);
3717                         }
3718                         if (id->bits.type == LOCKID) {
3719                                 /* Seqid in the future? - that's bad */
3720                                 if (lsp->rls_lockid.bits.chgseq <
3721                                     id->bits.chgseq) {
3722                                         rfs4_lo_state_rele(lsp, FALSE);
3723                                         if (sp != NULL)
3724                                                 rfs4_state_rele_nounlock(sp);
3725                                         return (NFS4ERR_BAD_STATEID);
3726                                 }
3727                                 /* Seqid in the past? - that's old */
3728                                 if (lsp->rls_lockid.bits.chgseq >
3729                                     id->bits.chgseq) {
3730                                         rfs4_lo_state_rele(lsp, FALSE);
3731                                         if (sp != NULL)
3732                                                 rfs4_state_rele_nounlock(sp);
3733                                         return (NFS4ERR_OLD_STATEID);
3734                                 }
3735                                 /* Ensure specified filehandle matches */
3736                                 if (lsp->rls_state->rs_finfo->rf_vp != vp) {
3737                                         rfs4_lo_state_rele(lsp, FALSE);
3738                                         if (sp != NULL)
3739                                                 rfs4_state_rele_nounlock(sp);
3740                                         return (NFS4ERR_BAD_STATEID);
3741                                 }
3742                         }
3743                         if (ct != NULL) {
3744                                 ct->cc_sysid =
3745                                     lsp->rls_locker->rl_client->rc_sysidt;
3746                                 ct->cc_pid = lsp->rls_locker->rl_pid;
3747                         }
3748                         rfs4_lo_state_rele(lsp, FALSE);
3749                 }
3750 
3751                 /* Stateid provided was an "open" stateid */
3752                 if (sp != NULL) {
3753                         /* Is associated server instance in its grace period? */
3754                         if (rfs4_clnt_in_grace(sp->rs_owner->ro_client)) {
3755                                 rfs4_state_rele_nounlock(sp);
3756                                 return (NFS4ERR_GRACE);
3757                         }
3758                         if (id->bits.type == OPENID) {
3759                                 /* Seqid in the future? - that's bad */
3760                                 if (sp->rs_stateid.bits.chgseq <
3761                                     id->bits.chgseq) {
3762                                         rfs4_state_rele_nounlock(sp);
3763                                         return (NFS4ERR_BAD_STATEID);
3764                                 }
3765                                 /* Seqid in the past - that's old */
3766                                 if (sp->rs_stateid.bits.chgseq >
3767                                     id->bits.chgseq) {
3768                                         rfs4_state_rele_nounlock(sp);
3769                                         return (NFS4ERR_OLD_STATEID);
3770                                 }
3771                         }
3772                         /* Ensure specified filehandle matches */
3773                         if (sp->rs_finfo->rf_vp != vp) {
3774                                 rfs4_state_rele_nounlock(sp);
3775                                 return (NFS4ERR_BAD_STATEID);
3776                         }
3777 
3778                         if (sp->rs_owner->ro_need_confirm) {
3779                                 rfs4_state_rele_nounlock(sp);
3780                                 return (NFS4ERR_BAD_STATEID);
3781                         }
3782 
3783                         if (sp->rs_closed == TRUE) {
3784                                 rfs4_state_rele_nounlock(sp);
3785                                 return (NFS4ERR_OLD_STATEID);
3786                         }
3787 
3788                         if (do_access)
3789                                 stat = rfs4_state_has_access(sp, mode, vp);
3790                         else
3791                                 stat = NFS4_OK;
3792 
3793                         /*
3794                          * Return whether this state has write
3795                          * delegation if desired
3796                          */
3797                         if (deleg && (sp->rs_finfo->rf_dinfo.rd_dtype ==
3798                             OPEN_DELEGATE_WRITE))
3799                                 *deleg = TRUE;
3800 
3801                         /*
3802                          * We got a valid stateid, so we update the
3803                          * lease on the client. Ideally we would like
3804                          * to do this after the calling op succeeds,
3805                          * but for now this will be good
3806                          * enough. Callers of this routine are
3807                          * currently insulated from the state stuff.
3808                          */
3809                         rfs4_update_lease(sp->rs_owner->ro_client);
3810 
3811                         /*
3812                          * If a delegation is present on this file and
3813                          * this is a WRITE, then update the lastwrite
3814                          * time to indicate that activity is present.
3815                          */
3816                         if (sp->rs_finfo->rf_dinfo.rd_dtype ==
3817                             OPEN_DELEGATE_WRITE &&
3818                             mode == FWRITE) {
3819                                 sp->rs_finfo->rf_dinfo.rd_time_lastwrite =
3820                                     gethrestime_sec();
3821                         }
3822 
3823                         rfs4_state_rele_nounlock(sp);
3824 
3825                         return (stat);
3826                 }
3827 
3828                 if (dsp != NULL) {
3829                         /* Is associated server instance in its grace period? */
3830                         if (rfs4_clnt_in_grace(dsp->rds_client)) {
3831                                 rfs4_deleg_state_rele(dsp);
3832                                 return (NFS4ERR_GRACE);
3833                         }
3834                         if (dsp->rds_delegid.bits.chgseq != id->bits.chgseq) {
3835                                 rfs4_deleg_state_rele(dsp);
3836                                 return (NFS4ERR_BAD_STATEID);
3837                         }
3838 
3839                         /* Ensure specified filehandle matches */
3840                         if (dsp->rds_finfo->rf_vp != vp) {
3841                                 rfs4_deleg_state_rele(dsp);
3842                                 return (NFS4ERR_BAD_STATEID);
3843                         }
3844                         /*
3845                          * Return whether this state has write
3846                          * delegation if desired
3847                          */
3848                         if (deleg && (dsp->rds_finfo->rf_dinfo.rd_dtype ==
3849                             OPEN_DELEGATE_WRITE))
3850                                 *deleg = TRUE;
3851 
3852                         rfs4_update_lease(dsp->rds_client);
3853 
3854                         /*
3855                          * If a delegation is present on this file and
3856                          * this is a WRITE, then update the lastwrite
3857                          * time to indicate that activity is present.
3858                          */
3859                         if (dsp->rds_finfo->rf_dinfo.rd_dtype ==
3860                             OPEN_DELEGATE_WRITE && mode == FWRITE) {
3861                                 dsp->rds_finfo->rf_dinfo.rd_time_lastwrite =
3862                                     gethrestime_sec();
3863                         }
3864 
3865                         /*
3866                          * XXX - what happens if this is a WRITE and the
3867                          * delegation type of for READ.
3868                          */
3869                         rfs4_deleg_state_rele(dsp);
3870 
3871                         return (stat);
3872                 }
3873                 /*
3874                  * If we got this far, something bad happened
3875                  */
3876                 return (NFS4ERR_BAD_STATEID);
3877         }
3878 }
3879 
3880 
3881 /*
3882  * This is a special function in that for the file struct provided the
3883  * server wants to remove/close all current state associated with the
3884  * file.  The prime use of this would be with OP_REMOVE to force the
3885  * release of state and particularly of file locks.
3886  *
3887  * There is an assumption that there is no delegations outstanding on
3888  * this file at this point.  The caller should have waited for those
3889  * to be returned or revoked.
3890  */
3891 void
3892 rfs4_close_all_state(rfs4_file_t *fp)
3893 {
3894         rfs4_state_t *sp;
3895 
3896         rfs4_dbe_lock(fp->rf_dbe);
3897 
3898 #ifdef DEBUG
3899         /* only applies when server is handing out delegations */
3900         if (nfs4_get_deleg_policy() != SRV_NEVER_DELEGATE)
3901                 ASSERT(fp->rf_dinfo.rd_hold_grant > 0);
3902 #endif
3903 
3904         /* No delegations for this file */
3905         ASSERT(list_is_empty(&fp->rf_delegstatelist));
3906 
3907         /* Make sure that it can not be found */
3908         rfs4_dbe_invalidate(fp->rf_dbe);
3909 
3910         if (fp->rf_vp == NULL) {
3911                 rfs4_dbe_unlock(fp->rf_dbe);
3912                 return;
3913         }
3914         rfs4_dbe_unlock(fp->rf_dbe);
3915 
3916         /*
3917          * Hold as writer to prevent other server threads from
3918          * processing requests related to the file while all state is
3919          * being removed.
3920          */
3921         rw_enter(&fp->rf_file_rwlock, RW_WRITER);
3922 
3923         /* Remove ALL state from the file */
3924         while (sp = rfs4_findstate_by_file(fp)) {
3925                 rfs4_state_close(sp, FALSE, FALSE, CRED());
3926                 rfs4_state_rele_nounlock(sp);
3927         }
3928 
3929         /*
3930          * This is only safe since there are no further references to
3931          * the file.
3932          */
3933         rfs4_dbe_lock(fp->rf_dbe);
3934         if (fp->rf_vp) {
3935                 vnode_t *vp = fp->rf_vp;
3936 
3937                 mutex_enter(&vp->v_vsd_lock);
3938                 (void) vsd_set(vp, nfs4_srv_vkey, NULL);
3939                 mutex_exit(&vp->v_vsd_lock);
3940                 VN_RELE(vp);
3941                 fp->rf_vp = NULL;
3942         }
3943         rfs4_dbe_unlock(fp->rf_dbe);
3944 
3945         /* Finally let other references to proceed */
3946         rw_exit(&fp->rf_file_rwlock);
3947 }
3948 
3949 /*
3950  * This function is used as a target for the rfs4_dbe_walk() call
3951  * below.  The purpose of this function is to see if the
3952  * lockowner_state refers to a file that resides within the exportinfo
3953  * export.  If so, then remove the lock_owner state (file locks and
3954  * share "locks") for this object since the intent is the server is
3955  * unexporting the specified directory.  Be sure to invalidate the
3956  * object after the state has been released
3957  */
3958 static void
3959 rfs4_lo_state_walk_callout(rfs4_entry_t u_entry, void *e)
3960 {
3961         rfs4_lo_state_t *lsp = (rfs4_lo_state_t *)u_entry;
3962         struct exportinfo *exi = (struct exportinfo *)e;
3963         nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3964         fhandle_t *efhp;
3965 
3966         efhp = (fhandle_t *)&exi->exi_fh;
3967         exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
3968 
3969         FH_TO_FMT4(efhp, exi_fhp);
3970 
3971         finfo_fhp = (nfs_fh4_fmt_t *)lsp->rls_state->rs_finfo->
3972             rf_filehandle.nfs_fh4_val;
3973 
3974         if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
3975             bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
3976             exi_fhp->fh4_xlen) == 0) {
3977                 rfs4_state_close(lsp->rls_state, FALSE, FALSE, CRED());
3978                 rfs4_dbe_invalidate(lsp->rls_dbe);
3979                 rfs4_dbe_invalidate(lsp->rls_state->rs_dbe);
3980         }
3981 }
3982 
3983 /*
3984  * This function is used as a target for the rfs4_dbe_walk() call
3985  * below.  The purpose of this function is to see if the state refers
3986  * to a file that resides within the exportinfo export.  If so, then
3987  * remove the open state for this object since the intent is the
3988  * server is unexporting the specified directory.  The main result for
3989  * this type of entry is to invalidate it such it will not be found in
3990  * the future.
3991  */
3992 static void
3993 rfs4_state_walk_callout(rfs4_entry_t u_entry, void *e)
3994 {
3995         rfs4_state_t *sp = (rfs4_state_t *)u_entry;
3996         struct exportinfo *exi = (struct exportinfo *)e;
3997         nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
3998         fhandle_t *efhp;
3999 
4000         efhp = (fhandle_t *)&exi->exi_fh;
4001         exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
4002 
4003         FH_TO_FMT4(efhp, exi_fhp);
4004 
4005         finfo_fhp =
4006             (nfs_fh4_fmt_t *)sp->rs_finfo->rf_filehandle.nfs_fh4_val;
4007 
4008         if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
4009             bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
4010             exi_fhp->fh4_xlen) == 0) {
4011                 rfs4_state_close(sp, TRUE, FALSE, CRED());
4012                 rfs4_dbe_invalidate(sp->rs_dbe);
4013         }
4014 }
4015 
4016 /*
4017  * This function is used as a target for the rfs4_dbe_walk() call
4018  * below.  The purpose of this function is to see if the state refers
4019  * to a file that resides within the exportinfo export.  If so, then
4020  * remove the deleg state for this object since the intent is the
4021  * server is unexporting the specified directory.  The main result for
4022  * this type of entry is to invalidate it such it will not be found in
4023  * the future.
4024  */
4025 static void
4026 rfs4_deleg_state_walk_callout(rfs4_entry_t u_entry, void *e)
4027 {
4028         rfs4_deleg_state_t *dsp = (rfs4_deleg_state_t *)u_entry;
4029         struct exportinfo *exi = (struct exportinfo *)e;
4030         nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
4031         fhandle_t *efhp;
4032 
4033         efhp = (fhandle_t *)&exi->exi_fh;
4034         exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
4035 
4036         FH_TO_FMT4(efhp, exi_fhp);
4037 
4038         finfo_fhp =
4039             (nfs_fh4_fmt_t *)dsp->rds_finfo->rf_filehandle.nfs_fh4_val;
4040 
4041         if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
4042             bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
4043             exi_fhp->fh4_xlen) == 0) {
4044                 rfs4_dbe_invalidate(dsp->rds_dbe);
4045         }
4046 }
4047 
4048 /*
4049  * This function is used as a target for the rfs4_dbe_walk() call
4050  * below.  The purpose of this function is to see if the state refers
4051  * to a file that resides within the exportinfo export.  If so, then
4052  * release vnode hold for this object since the intent is the server
4053  * is unexporting the specified directory.  Invalidation will prevent
4054  * this struct from being found in the future.
4055  */
4056 static void
4057 rfs4_file_walk_callout(rfs4_entry_t u_entry, void *e)
4058 {
4059         rfs4_file_t *fp = (rfs4_file_t *)u_entry;
4060         struct exportinfo *exi = (struct exportinfo *)e;
4061         nfs_fh4_fmt_t   fhfmt4, *exi_fhp, *finfo_fhp;
4062         fhandle_t *efhp;
4063 
4064         efhp = (fhandle_t *)&exi->exi_fh;
4065         exi_fhp = (nfs_fh4_fmt_t *)&fhfmt4;
4066 
4067         FH_TO_FMT4(efhp, exi_fhp);
4068 
4069         finfo_fhp = (nfs_fh4_fmt_t *)fp->rf_filehandle.nfs_fh4_val;
4070 
4071         if (EQFSID(&finfo_fhp->fh4_fsid, &exi_fhp->fh4_fsid) &&
4072             bcmp(&finfo_fhp->fh4_xdata, &exi_fhp->fh4_xdata,
4073             exi_fhp->fh4_xlen) == 0) {
4074                 if (fp->rf_vp) {
4075                         vnode_t *vp = fp->rf_vp;
4076 
4077                         /*
4078                          * don't leak monitors and remove the reference
4079                          * put on the vnode when the delegation was granted.
4080                          */
4081                         if (fp->rf_dinfo.rd_dtype == OPEN_DELEGATE_READ) {
4082                                 (void) fem_uninstall(vp, deleg_rdops,
4083                                     (void *)fp);
4084                                 vn_open_downgrade(vp, FREAD);
4085                         } else if (fp->rf_dinfo.rd_dtype ==
4086                             OPEN_DELEGATE_WRITE) {
4087                                 (void) fem_uninstall(vp, deleg_wrops,
4088                                     (void *)fp);
4089                                 vn_open_downgrade(vp, FREAD|FWRITE);
4090                         }
4091                         mutex_enter(&vp->v_vsd_lock);
4092                         (void) vsd_set(vp, nfs4_srv_vkey, NULL);
4093                         mutex_exit(&vp->v_vsd_lock);
4094                         VN_RELE(vp);
4095                         fp->rf_vp = NULL;
4096                 }
4097                 rfs4_dbe_invalidate(fp->rf_dbe);
4098         }
4099 }
4100 
4101 /*
4102  * Given a directory that is being unexported, cleanup/release all
4103  * state in the server that refers to objects residing underneath this
4104  * particular export.  The ordering of the release is important.
4105  * Lock_owner, then state and then file.
4106  */
4107 void
4108 rfs4_clean_state_exi(struct exportinfo *exi)
4109 {
4110         nfs4_srv_t *nsrv4;
4111 
4112         /* curzone mightn't be exi_zone, so use exi_zone instead. */
4113         ASSERT(exi->exi_zone == curzone || curzone == global_zone);
4114         nsrv4 = zone_getspecific(rfs4_zone_key, exi->exi_zone);
4115         if (nsrv4 == NULL) /* NOTE: NFSv4 cleanup MAY have already happened. */
4116                 return;
4117         mutex_enter(&nsrv4->state_lock);
4118 
4119         if (nsrv4->nfs4_server_state == NULL) {
4120                 mutex_exit(&nsrv4->state_lock);
4121                 return;
4122         }
4123 
4124         /* CSTYLED */
4125         rfs4_dbe_walk(nsrv4->rfs4_lo_state_tab, rfs4_lo_state_walk_callout, exi);
4126         rfs4_dbe_walk(nsrv4->rfs4_state_tab, rfs4_state_walk_callout, exi);
4127         /* CSTYLED */
4128         rfs4_dbe_walk(nsrv4->rfs4_deleg_state_tab, rfs4_deleg_state_walk_callout, exi);
4129         rfs4_dbe_walk(nsrv4->rfs4_file_tab, rfs4_file_walk_callout, exi);
4130 
4131         mutex_exit(&nsrv4->state_lock);
4132 }