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