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