xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs4_srv_deleg.c (revision 982b4ad2dc6b5ed2a2c8c1670e94ecf1fe63fc56)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/systm.h>
27 #include <rpc/auth.h>
28 #include <rpc/clnt.h>
29 #include <nfs/nfs4_kprot.h>
30 #include <nfs/nfs4.h>
31 #include <nfs/lm.h>
32 #include <sys/cmn_err.h>
33 #include <sys/disp.h>
34 #include <sys/sdt.h>
35 
36 #include <sys/pathname.h>
37 
38 #include <sys/strsubr.h>
39 #include <sys/ddi.h>
40 
41 #include <sys/vnode.h>
42 #include <sys/sdt.h>
43 #include <inet/common.h>
44 #include <inet/ip.h>
45 #include <inet/ip6.h>
46 
47 #define	MAX_READ_DELEGATIONS 5
48 
49 krwlock_t rfs4_deleg_policy_lock;
50 srv_deleg_policy_t rfs4_deleg_policy = SRV_NEVER_DELEGATE;
51 static int rfs4_deleg_wlp = 5;
52 kmutex_t rfs4_deleg_lock;
53 static int rfs4_deleg_disabled;
54 static int rfs4_max_setup_cb_tries = 5;
55 
56 #ifdef DEBUG
57 
58 static int rfs4_test_cbgetattr_fail = 0;
59 int rfs4_cb_null;
60 int rfs4_cb_debug;
61 int rfs4_deleg_debug;
62 
63 #endif
64 
65 static void rfs4_recall_file(rfs4_file_t *,
66 			    void (*recall)(rfs4_deleg_state_t *, bool_t),
67 			    bool_t, rfs4_client_t *);
68 static	void		rfs4_revoke_deleg(rfs4_deleg_state_t *);
69 static	void		rfs4_revoke_file(rfs4_file_t *);
70 static	void		rfs4_cb_chflush(rfs4_cbinfo_t *);
71 static	CLIENT		*rfs4_cb_getch(rfs4_cbinfo_t *);
72 static	void		rfs4_cb_freech(rfs4_cbinfo_t *, CLIENT *, bool_t);
73 static rfs4_deleg_state_t *rfs4_deleg_state(rfs4_state_t *,
74 				open_delegation_type4, int *);
75 
76 /*
77  * Convert a universal address to an transport specific
78  * address using inet_pton.
79  */
80 static int
81 uaddr2sockaddr(int af, char *ua, void *ap, in_port_t *pp)
82 {
83 	int dots = 0, i, j, len, k;
84 	unsigned char c;
85 	in_port_t port = 0;
86 
87 	len = strlen(ua);
88 
89 	for (i = len-1; i >= 0; i--) {
90 
91 		if (ua[i] == '.')
92 			dots++;
93 
94 		if (dots == 2) {
95 
96 			ua[i] = '\0';
97 			/*
98 			 * We use k to remember were to stick '.' back, since
99 			 * ua was kmem_allocateded from the pool len+1.
100 			 */
101 			k = i;
102 			if (inet_pton(af, ua, ap) == 1) {
103 
104 				c = 0;
105 
106 				for (j = i+1; j < len; j++) {
107 					if (ua[j] == '.') {
108 						port = c << 8;
109 						c = 0;
110 					} else if (ua[j] >= '0' &&
111 					    ua[j] <= '9') {
112 						c *= 10;
113 						c += ua[j] - '0';
114 					} else {
115 						ua[k] = '.';
116 						return (EINVAL);
117 					}
118 				}
119 				port += c;
120 
121 
122 				/* reset to network order */
123 				if (af == AF_INET) {
124 					*(uint32_t *)ap =
125 					    htonl(*(uint32_t *)ap);
126 					*pp = htons(port);
127 				} else {
128 					int ix;
129 					uint16_t *sap;
130 
131 					for (sap = ap, ix = 0; ix <
132 					    sizeof (struct in6_addr) /
133 					    sizeof (uint16_t); ix++)
134 						sap[ix] = htons(sap[ix]);
135 
136 					*pp = htons(port);
137 				}
138 
139 				ua[k] = '.';
140 				return (0);
141 			} else {
142 				ua[k] = '.';
143 				return (EINVAL);
144 			}
145 		}
146 	}
147 
148 	return (EINVAL);
149 }
150 
151 /*
152  * Update the delegation policy with the
153  * value of "new_policy"
154  */
155 void
156 rfs4_set_deleg_policy(srv_deleg_policy_t new_policy)
157 {
158 	rw_enter(&rfs4_deleg_policy_lock, RW_WRITER);
159 	rfs4_deleg_policy = new_policy;
160 	rw_exit(&rfs4_deleg_policy_lock);
161 }
162 
163 void
164 rfs4_hold_deleg_policy(void)
165 {
166 	rw_enter(&rfs4_deleg_policy_lock, RW_READER);
167 }
168 
169 void
170 rfs4_rele_deleg_policy(void)
171 {
172 	rw_exit(&rfs4_deleg_policy_lock);
173 }
174 
175 
176 /*
177  * This free function is to be used when the client struct is being
178  * released and nothing at all is needed of the callback info any
179  * longer.
180  */
181 void
182 rfs4_cbinfo_free(rfs4_cbinfo_t *cbp)
183 {
184 	char *addr = cbp->cb_callback.cb_location.r_addr;
185 	char *netid = cbp->cb_callback.cb_location.r_netid;
186 
187 	/* Free old address if any */
188 
189 	if (addr)
190 		kmem_free(addr, strlen(addr) + 1);
191 	if (netid)
192 		kmem_free(netid, strlen(netid) + 1);
193 
194 	addr = cbp->cb_newer.cb_callback.cb_location.r_addr;
195 	netid = cbp->cb_newer.cb_callback.cb_location.r_netid;
196 
197 	if (addr)
198 		kmem_free(addr, strlen(addr) + 1);
199 	if (netid)
200 		kmem_free(netid, strlen(netid) + 1);
201 
202 	if (cbp->cb_chc_free) {
203 		rfs4_cb_chflush(cbp);
204 	}
205 }
206 
207 /*
208  * The server uses this to check the callback path supplied by the
209  * client.  The callback connection is marked "in progress" while this
210  * work is going on and then eventually marked either OK or FAILED.
211  * This work can be done as part of a separate thread and at the end
212  * of this the thread will exit or it may be done such that the caller
213  * will continue with other work.
214  */
215 static void
216 rfs4_do_cb_null(rfs4_client_t *cp)
217 {
218 	struct timeval tv;
219 	CLIENT *ch;
220 	rfs4_cbstate_t newstate;
221 	rfs4_cbinfo_t *cbp = &cp->cbinfo;
222 
223 	mutex_enter(cbp->cb_lock);
224 	/* If another thread is doing CB_NULL RPC then return */
225 	if (cbp->cb_nullcaller == TRUE) {
226 		mutex_exit(cbp->cb_lock);
227 		rfs4_client_rele(cp);
228 		return;
229 	}
230 
231 	/* Mark the cbinfo as having a thread in the NULL callback */
232 	cbp->cb_nullcaller = TRUE;
233 
234 	/*
235 	 * Are there other threads still using the cbinfo client
236 	 * handles?  If so, this thread must wait before going and
237 	 * mucking aroiund with the callback information
238 	 */
239 	while (cbp->cb_refcnt != 0)
240 		cv_wait(cbp->cb_cv_nullcaller, cbp->cb_lock);
241 
242 	/*
243 	 * This thread itself may find that new callback info has
244 	 * arrived and is set up to handle this case and redrive the
245 	 * call to the client's callback server.
246 	 */
247 retry:
248 	if (cbp->cb_newer.cb_new == TRUE &&
249 	    cbp->cb_newer.cb_confirmed == TRUE) {
250 		char *addr = cbp->cb_callback.cb_location.r_addr;
251 		char *netid = cbp->cb_callback.cb_location.r_netid;
252 
253 		/*
254 		 * Free the old stuff if it exists; may be the first
255 		 * time through this path
256 		 */
257 		if (addr)
258 			kmem_free(addr, strlen(addr) + 1);
259 		if (netid)
260 			kmem_free(netid, strlen(netid) + 1);
261 
262 		/* Move over the addr/netid */
263 		cbp->cb_callback.cb_location.r_addr =
264 		    cbp->cb_newer.cb_callback.cb_location.r_addr;
265 		cbp->cb_newer.cb_callback.cb_location.r_addr = NULL;
266 		cbp->cb_callback.cb_location.r_netid =
267 		    cbp->cb_newer.cb_callback.cb_location.r_netid;
268 		cbp->cb_newer.cb_callback.cb_location.r_netid = NULL;
269 
270 		/* Get the program number */
271 		cbp->cb_callback.cb_program =
272 		    cbp->cb_newer.cb_callback.cb_program;
273 		cbp->cb_newer.cb_callback.cb_program = 0;
274 
275 		/* Don't forget the protocol's "cb_ident" field */
276 		cbp->cb_ident = cbp->cb_newer.cb_ident;
277 		cbp->cb_newer.cb_ident = 0;
278 
279 		/* no longer new */
280 		cbp->cb_newer.cb_new = FALSE;
281 		cbp->cb_newer.cb_confirmed = FALSE;
282 
283 		/* get rid of the old client handles that may exist */
284 		rfs4_cb_chflush(cbp);
285 
286 		cbp->cb_state = CB_NONE;
287 		cbp->cb_timefailed = 0; /* reset the clock */
288 		cbp->cb_notified_of_cb_path_down = TRUE;
289 	}
290 
291 	if (cbp->cb_state != CB_NONE) {
292 		cv_broadcast(cbp->cb_cv);	/* let the others know */
293 		cbp->cb_nullcaller = FALSE;
294 		mutex_exit(cbp->cb_lock);
295 		rfs4_client_rele(cp);
296 		return;
297 	}
298 
299 	/* mark rfs4_client_t as CALLBACK NULL in progress */
300 	cbp->cb_state = CB_INPROG;
301 	mutex_exit(cbp->cb_lock);
302 
303 	/* get/generate a client handle */
304 	if ((ch = rfs4_cb_getch(cbp)) == NULL) {
305 		mutex_enter(cbp->cb_lock);
306 		cbp->cb_state = CB_BAD;
307 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
308 		goto retry;
309 	}
310 
311 
312 	tv.tv_sec = 30;
313 	tv.tv_usec = 0;
314 	if (clnt_call(ch, CB_NULL, xdr_void, NULL, xdr_void, NULL, tv) != 0) {
315 		newstate = CB_BAD;
316 	} else {
317 		newstate = CB_OK;
318 #ifdef	DEBUG
319 		rfs4_cb_null++;
320 #endif
321 	}
322 
323 	/* Check to see if the client has specified new callback info */
324 	mutex_enter(cbp->cb_lock);
325 	rfs4_cb_freech(cbp, ch, TRUE);
326 	if (cbp->cb_newer.cb_new == TRUE &&
327 	    cbp->cb_newer.cb_confirmed == TRUE) {
328 		goto retry;	/* give the CB_NULL another chance */
329 	}
330 
331 	cbp->cb_state = newstate;
332 	if (cbp->cb_state == CB_BAD)
333 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
334 
335 	cv_broadcast(cbp->cb_cv);	/* start up the other threads */
336 	cbp->cb_nullcaller = FALSE;
337 	mutex_exit(cbp->cb_lock);
338 
339 	rfs4_client_rele(cp);
340 }
341 
342 /*
343  * Given a client struct, inspect the callback info to see if the
344  * callback path is up and available.
345  *
346  * If new callback path is available and no one has set it up then
347  * try to set it up. If setup is not successful after 5 tries (5 secs)
348  * then gives up and returns NULL.
349  *
350  * If callback path is being initialized, then wait for the CB_NULL RPC
351  * call to occur.
352  */
353 static rfs4_cbinfo_t *
354 rfs4_cbinfo_hold(rfs4_client_t *cp)
355 {
356 	rfs4_cbinfo_t *cbp = &cp->cbinfo;
357 	int retries = 0;
358 
359 	mutex_enter(cbp->cb_lock);
360 
361 	while (cbp->cb_newer.cb_new == TRUE && cbp->cb_nullcaller == FALSE) {
362 		/*
363 		 * Looks like a new callback path may be available and
364 		 * noone has set it up.
365 		 */
366 		mutex_exit(cbp->cb_lock);
367 		rfs4_dbe_hold(cp->dbe);
368 		rfs4_do_cb_null(cp); /* caller will release client hold */
369 
370 		mutex_enter(cbp->cb_lock);
371 		/*
372 		 * If callback path is no longer new, or it's being setup
373 		 * then stop and wait for it to be done.
374 		 */
375 		if (cbp->cb_newer.cb_new == FALSE || cbp->cb_nullcaller == TRUE)
376 			break;
377 		mutex_exit(cbp->cb_lock);
378 
379 		if (++retries >= rfs4_max_setup_cb_tries)
380 			return (NULL);
381 		delay(hz);
382 		mutex_enter(cbp->cb_lock);
383 	}
384 
385 	/* Is there a thread working on doing the CB_NULL RPC? */
386 	if (cbp->cb_nullcaller == TRUE)
387 		cv_wait(cbp->cb_cv, cbp->cb_lock);  /* if so, wait on it */
388 
389 	/* If the callback path is not okay (up and running), just quit */
390 	if (cbp->cb_state != CB_OK) {
391 		mutex_exit(cbp->cb_lock);
392 		return (NULL);
393 	}
394 
395 	/* Let someone know we are using the current callback info */
396 	cbp->cb_refcnt++;
397 	mutex_exit(cbp->cb_lock);
398 	return (cbp);
399 }
400 
401 /*
402  * The caller is done with the callback info.  It may be that the
403  * caller's RPC failed and the NFSv4 client has actually provided new
404  * callback information.  If so, let the caller know so they can
405  * advantage of this and maybe retry the RPC that originally failed.
406  */
407 static int
408 rfs4_cbinfo_rele(rfs4_cbinfo_t *cbp, rfs4_cbstate_t newstate)
409 {
410 	int cb_new = FALSE;
411 
412 	mutex_enter(cbp->cb_lock);
413 
414 	/* The caller gets a chance to mark the callback info as bad */
415 	if (newstate != CB_NOCHANGE)
416 		cbp->cb_state = newstate;
417 	if (newstate == CB_FAILED) {
418 		cbp->cb_timefailed = gethrestime_sec(); /* observability */
419 		cbp->cb_notified_of_cb_path_down = FALSE;
420 	}
421 
422 	cbp->cb_refcnt--;	/* no longer using the information */
423 
424 	/*
425 	 * A thread may be waiting on this one to finish and if so,
426 	 * let it know that it is okay to do the CB_NULL to the
427 	 * client's callback server.
428 	 */
429 	if (cbp->cb_refcnt == 0 && cbp->cb_nullcaller)
430 		cv_broadcast(cbp->cb_cv_nullcaller);
431 
432 	/*
433 	 * If this is the last thread to use the callback info and
434 	 * there is new callback information to try and no thread is
435 	 * there ready to do the CB_NULL, then return true to teh
436 	 * caller so they can do the CB_NULL
437 	 */
438 	if (cbp->cb_refcnt == 0 &&
439 	    cbp->cb_nullcaller == FALSE &&
440 	    cbp->cb_newer.cb_new == TRUE &&
441 	    cbp->cb_newer.cb_confirmed == TRUE)
442 		cb_new = TRUE;
443 
444 	mutex_exit(cbp->cb_lock);
445 
446 	return (cb_new);
447 }
448 
449 /*
450  * Given the information in the callback info struct, create a client
451  * handle that can be used by the server for its callback path.
452  */
453 static CLIENT *
454 rfs4_cbch_init(rfs4_cbinfo_t *cbp)
455 {
456 	struct knetconfig knc;
457 	vnode_t *vp;
458 	struct sockaddr_in addr4;
459 	struct sockaddr_in6 addr6;
460 	void *addr, *taddr;
461 	in_port_t *pp;
462 	int af;
463 	char *devnam;
464 	struct netbuf nb;
465 	int size;
466 	CLIENT *ch = NULL;
467 	int useresvport = 0;
468 
469 	mutex_enter(cbp->cb_lock);
470 
471 	if (cbp->cb_callback.cb_location.r_netid == NULL ||
472 	    cbp->cb_callback.cb_location.r_addr == NULL) {
473 		goto cb_init_out;
474 	}
475 
476 	if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp") == 0) {
477 		knc.knc_semantics = NC_TPI_COTS;
478 		knc.knc_protofmly = "inet";
479 		knc.knc_proto = "tcp";
480 		devnam = "/dev/tcp";
481 		af = AF_INET;
482 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp")
483 	    == 0) {
484 		knc.knc_semantics = NC_TPI_CLTS;
485 		knc.knc_protofmly = "inet";
486 		knc.knc_proto = "udp";
487 		devnam = "/dev/udp";
488 		af = AF_INET;
489 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "tcp6")
490 	    == 0) {
491 		knc.knc_semantics = NC_TPI_COTS;
492 		knc.knc_protofmly = "inet6";
493 		knc.knc_proto = "tcp";
494 		devnam = "/dev/tcp6";
495 		af = AF_INET6;
496 	} else if (strcmp(cbp->cb_callback.cb_location.r_netid, "udp6")
497 	    == 0) {
498 		knc.knc_semantics = NC_TPI_CLTS;
499 		knc.knc_protofmly = "inet6";
500 		knc.knc_proto = "udp";
501 		devnam = "/dev/udp6";
502 		af = AF_INET6;
503 	} else {
504 		goto cb_init_out;
505 	}
506 
507 	if (lookupname(devnam, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp) != 0) {
508 
509 		goto cb_init_out;
510 	}
511 
512 	if (vp->v_type != VCHR) {
513 		VN_RELE(vp);
514 		goto cb_init_out;
515 	}
516 
517 	knc.knc_rdev = vp->v_rdev;
518 
519 	VN_RELE(vp);
520 
521 	if (af == AF_INET) {
522 		size = sizeof (addr4);
523 		bzero(&addr4, size);
524 		addr4.sin_family = (sa_family_t)af;
525 		addr = &addr4.sin_addr;
526 		pp = &addr4.sin_port;
527 		taddr = &addr4;
528 	} else /* AF_INET6 */ {
529 		size = sizeof (addr6);
530 		bzero(&addr6, size);
531 		addr6.sin6_family = (sa_family_t)af;
532 		addr = &addr6.sin6_addr;
533 		pp = &addr6.sin6_port;
534 		taddr = &addr6;
535 	}
536 
537 	if (uaddr2sockaddr(af,
538 	    cbp->cb_callback.cb_location.r_addr, addr, pp)) {
539 
540 		goto cb_init_out;
541 	}
542 
543 
544 	nb.maxlen = nb.len = size;
545 	nb.buf = (char *)taddr;
546 
547 	if (clnt_tli_kcreate(&knc, &nb, cbp->cb_callback.cb_program,
548 	    NFS_CB, 0, 0, curthread->t_cred, &ch)) {
549 
550 		ch = NULL;
551 	}
552 
553 	/* turn off reserved port usage */
554 	(void) CLNT_CONTROL(ch, CLSET_BINDRESVPORT, (char *)&useresvport);
555 
556 cb_init_out:
557 	mutex_exit(cbp->cb_lock);
558 	return (ch);
559 }
560 
561 /*
562  * Iterate over the client handle cache and
563  * destroy it.
564  */
565 static void
566 rfs4_cb_chflush(rfs4_cbinfo_t *cbp)
567 {
568 	CLIENT *ch;
569 
570 	while (cbp->cb_chc_free) {
571 		cbp->cb_chc_free--;
572 		ch = cbp->cb_chc[cbp->cb_chc_free];
573 		cbp->cb_chc[cbp->cb_chc_free] = NULL;
574 		if (ch) {
575 			if (ch->cl_auth)
576 				auth_destroy(ch->cl_auth);
577 			clnt_destroy(ch);
578 		}
579 	}
580 }
581 
582 /*
583  * Return a client handle, either from a the small
584  * rfs4_client_t cache or one that we just created.
585  */
586 static CLIENT *
587 rfs4_cb_getch(rfs4_cbinfo_t *cbp)
588 {
589 	CLIENT *cbch = NULL;
590 	uint32_t zilch = 0;
591 
592 	mutex_enter(cbp->cb_lock);
593 
594 	if (cbp->cb_chc_free) {
595 		cbp->cb_chc_free--;
596 		cbch = cbp->cb_chc[ cbp->cb_chc_free ];
597 		mutex_exit(cbp->cb_lock);
598 		(void) CLNT_CONTROL(cbch, CLSET_XID, (char *)&zilch);
599 		return (cbch);
600 	}
601 
602 	mutex_exit(cbp->cb_lock);
603 
604 	/* none free so make it now */
605 	cbch = rfs4_cbch_init(cbp);
606 
607 	return (cbch);
608 }
609 
610 /*
611  * Return the client handle to the small cache or
612  * destroy it.
613  */
614 static void
615 rfs4_cb_freech(rfs4_cbinfo_t *cbp, CLIENT *ch, bool_t lockheld)
616 {
617 	if (lockheld == FALSE)
618 		mutex_enter(cbp->cb_lock);
619 
620 	if (cbp->cb_chc_free < RFS4_CBCH_MAX) {
621 		cbp->cb_chc[ cbp->cb_chc_free++ ] = ch;
622 		if (lockheld == FALSE)
623 			mutex_exit(cbp->cb_lock);
624 		return;
625 	}
626 	if (lockheld == FALSE)
627 		mutex_exit(cbp->cb_lock);
628 
629 	/*
630 	 * cache maxed out of free entries, obliterate
631 	 * this client handle, destroy it, throw it away.
632 	 */
633 	if (ch->cl_auth)
634 		auth_destroy(ch->cl_auth);
635 	clnt_destroy(ch);
636 }
637 
638 /*
639  * With the supplied callback information - initialize the client
640  * callback data.  If there is a callback in progress, save the
641  * callback info so that a thread can pick it up in the future.
642  */
643 void
644 rfs4_client_setcb(rfs4_client_t *cp, cb_client4 *cb, uint32_t cb_ident)
645 {
646 	char *addr = NULL;
647 	char *netid = NULL;
648 	rfs4_cbinfo_t *cbp = &cp->cbinfo;
649 	size_t len;
650 
651 	/* Set the call back for the client */
652 	if (cb->cb_location.r_addr && cb->cb_location.r_addr[0] != '\0' &&
653 	    cb->cb_location.r_netid && cb->cb_location.r_netid[0] != '\0') {
654 		len = strlen(cb->cb_location.r_addr) + 1;
655 		addr = kmem_alloc(len, KM_SLEEP);
656 		bcopy(cb->cb_location.r_addr, addr, len);
657 		len = strlen(cb->cb_location.r_netid) + 1;
658 		netid = kmem_alloc(len, KM_SLEEP);
659 		bcopy(cb->cb_location.r_netid, netid, len);
660 	}
661 	/* ready to save the new information but first free old, if exists */
662 	mutex_enter(cbp->cb_lock);
663 
664 	cbp->cb_newer.cb_callback.cb_program = cb->cb_program;
665 
666 	if (cbp->cb_newer.cb_callback.cb_location.r_addr != NULL)
667 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_addr,
668 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_addr) + 1);
669 	cbp->cb_newer.cb_callback.cb_location.r_addr = addr;
670 
671 	if (cbp->cb_newer.cb_callback.cb_location.r_netid != NULL)
672 		kmem_free(cbp->cb_newer.cb_callback.cb_location.r_netid,
673 		    strlen(cbp->cb_newer.cb_callback.cb_location.r_netid) + 1);
674 	cbp->cb_newer.cb_callback.cb_location.r_netid = netid;
675 
676 	cbp->cb_newer.cb_ident = cb_ident;
677 
678 	if (addr && *addr && netid && *netid) {
679 		cbp->cb_newer.cb_new = TRUE;
680 		cbp->cb_newer.cb_confirmed = FALSE;
681 	} else {
682 		cbp->cb_newer.cb_new = FALSE;
683 		cbp->cb_newer.cb_confirmed = FALSE;
684 	}
685 
686 	mutex_exit(cbp->cb_lock);
687 }
688 
689 /*
690  * The server uses this when processing SETCLIENTID_CONFIRM.  Callback
691  * information may have been provided on SETCLIENTID and this call
692  * marks that information as confirmed and then starts a thread to
693  * test the callback path.
694  */
695 void
696 rfs4_deleg_cb_check(rfs4_client_t *cp)
697 {
698 	if (cp->cbinfo.cb_newer.cb_new == FALSE)
699 		return;
700 
701 	cp->cbinfo.cb_newer.cb_confirmed = TRUE;
702 
703 	rfs4_dbe_hold(cp->dbe); /* hold the client struct for thread */
704 
705 	(void) thread_create(NULL, 0, rfs4_do_cb_null, cp, 0, &p0, TS_RUN,
706 	    minclsyspri);
707 }
708 
709 static void
710 rfs4args_cb_recall_free(nfs_cb_argop4 *argop)
711 {
712 	CB_RECALL4args	*rec_argp;
713 
714 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
715 	if (rec_argp->fh.nfs_fh4_val)
716 		kmem_free(rec_argp->fh.nfs_fh4_val, rec_argp->fh.nfs_fh4_len);
717 }
718 
719 /* ARGSUSED */
720 static void
721 rfs4args_cb_getattr_free(nfs_cb_argop4 *argop)
722 {
723 	CB_GETATTR4args *argp;
724 
725 	argp = &argop->nfs_cb_argop4_u.opcbgetattr;
726 	if (argp->fh.nfs_fh4_val)
727 		kmem_free(argp->fh.nfs_fh4_val, argp->fh.nfs_fh4_len);
728 }
729 
730 static void
731 rfs4freeargres(CB_COMPOUND4args *args, CB_COMPOUND4res *resp)
732 {
733 	int i, arglen;
734 	nfs_cb_argop4 *argop;
735 
736 	/*
737 	 * First free any special args alloc'd for specific ops.
738 	 */
739 	arglen = args->array_len;
740 	argop = args->array;
741 	for (i = 0; i < arglen; i++, argop++) {
742 
743 		switch (argop->argop) {
744 		case OP_CB_RECALL:
745 			rfs4args_cb_recall_free(argop);
746 			break;
747 
748 		case OP_CB_GETATTR:
749 			rfs4args_cb_getattr_free(argop);
750 			break;
751 
752 		default:
753 			return;
754 		}
755 	}
756 
757 	if (args->tag.utf8string_len > 0)
758 		UTF8STRING_FREE(args->tag)
759 
760 	kmem_free(args->array, arglen * sizeof (nfs_cb_argop4));
761 	if (resp)
762 		(void) xdr_free(xdr_CB_COMPOUND4res, (caddr_t)resp);
763 }
764 
765 /*
766  * General callback routine for the server to the client.
767  */
768 static enum clnt_stat
769 rfs4_do_callback(rfs4_client_t	*cp, CB_COMPOUND4args *args,
770 		CB_COMPOUND4res *res, struct timeval timeout)
771 {
772 	rfs4_cbinfo_t *cbp;
773 	CLIENT *ch;
774 	/* start with this in case cb_getch() fails */
775 	enum clnt_stat	stat = RPC_FAILED;
776 
777 	res->tag.utf8string_val = NULL;
778 	res->array = NULL;
779 
780 retry:
781 	cbp = rfs4_cbinfo_hold(cp);
782 	if (cbp == NULL)
783 		return (stat);
784 
785 	/* get a client handle */
786 	if ((ch = rfs4_cb_getch(cbp)) != NULL) {
787 		/*
788 		 * reset the cb_ident since it may have changed in
789 		 * rfs4_cbinfo_hold()
790 		 */
791 		args->callback_ident = cbp->cb_ident;
792 
793 		stat = clnt_call(ch, CB_COMPOUND, xdr_CB_COMPOUND4args_srv,
794 		    (caddr_t)args, xdr_CB_COMPOUND4res,
795 		    (caddr_t)res, timeout);
796 
797 		/* free client handle */
798 		rfs4_cb_freech(cbp, ch, FALSE);
799 	}
800 
801 	/*
802 	 * If the rele says that there may be new callback info then
803 	 * retry this sequence and it may succeed as a result of the
804 	 * new callback path
805 	 */
806 	if (rfs4_cbinfo_rele(cbp,
807 	    (stat == RPC_SUCCESS ? CB_NOCHANGE : CB_FAILED)) == TRUE)
808 		goto retry;
809 
810 	return (stat);
811 }
812 
813 /*
814  * Used by the NFSv4 server to get attributes for a file while
815  * handling the case where a file has been write delegated.  For the
816  * time being, VOP_GETATTR() is called and CB_GETATTR processing is
817  * not undertaken.  This call site is maintained in case the server is
818  * updated in the future to handle write delegation space guarantees.
819  */
820 nfsstat4
821 rfs4_vop_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
822 {
823 
824 	int error;
825 
826 	error = VOP_GETATTR(vp, vap, flag, cr, NULL);
827 	return (puterrno4(error));
828 }
829 
830 /*
831  * This is used everywhere in the v2/v3 server to allow the
832  * integration of all NFS versions and the support of delegation.  For
833  * now, just call the VOP_GETATTR().  If the NFSv4 server is enhanced
834  * in the future to provide space guarantees for write delegations
835  * then this call site should be expanded to interact with the client.
836  */
837 int
838 rfs4_delegated_getattr(vnode_t *vp, vattr_t *vap, int flag, cred_t *cr)
839 {
840 	return (VOP_GETATTR(vp, vap, flag, cr, NULL));
841 }
842 
843 /*
844  * Place the actual cb_recall otw call to client.
845  */
846 static void
847 rfs4_do_cb_recall(rfs4_deleg_state_t *dsp, bool_t trunc)
848 {
849 	CB_COMPOUND4args	cb4_args;
850 	CB_COMPOUND4res		cb4_res;
851 	CB_RECALL4args		*rec_argp;
852 	CB_RECALL4res		*rec_resp;
853 	nfs_cb_argop4		*argop;
854 	int			numops;
855 	int			argoplist_size;
856 	struct timeval		timeout;
857 	nfs_fh4			*fhp;
858 	enum clnt_stat		call_stat;
859 
860 	/*
861 	 * set up the compound args
862 	 */
863 	numops = 1;	/* CB_RECALL only */
864 
865 	argoplist_size = numops * sizeof (nfs_cb_argop4);
866 	argop = kmem_zalloc(argoplist_size, KM_SLEEP);
867 	argop->argop = OP_CB_RECALL;
868 	rec_argp = &argop->nfs_cb_argop4_u.opcbrecall;
869 
870 	(void) str_to_utf8("cb_recall", &cb4_args.tag);
871 	cb4_args.minorversion = CB4_MINORVERSION;
872 	/* cb4_args.callback_ident is set in rfs4_do_callback() */
873 	cb4_args.array_len = numops;
874 	cb4_args.array = argop;
875 
876 	/*
877 	 * fill in the args struct
878 	 */
879 	bcopy(&dsp->delegid.stateid, &rec_argp->stateid, sizeof (stateid4));
880 	rec_argp->truncate = trunc;
881 
882 	fhp = &dsp->finfo->filehandle;
883 	rec_argp->fh.nfs_fh4_val = kmem_alloc(sizeof (char) *
884 	    fhp->nfs_fh4_len, KM_SLEEP);
885 	nfs_fh4_copy(fhp, &rec_argp->fh);
886 
887 	/* Keep track of when we did this for observability */
888 	dsp->time_recalled = gethrestime_sec();
889 
890 	/*
891 	 * Set up the timeout for the callback and make the actual call.
892 	 * Timeout will be 80% of the lease period for this server.
893 	 */
894 	timeout.tv_sec = (rfs4_lease_time * 80) / 100;
895 	timeout.tv_usec = 0;
896 
897 	DTRACE_NFSV4_3(cb__recall__start, rfs4_client_t *, dsp->client,
898 	    rfs4_deleg_state_t *, dsp, CB_RECALL4args *, rec_argp);
899 
900 	call_stat = rfs4_do_callback(dsp->client, &cb4_args, &cb4_res, timeout);
901 
902 	rec_resp = (cb4_res.array_len == 0) ? NULL :
903 	    &cb4_res.array[0].nfs_cb_resop4_u.opcbrecall;
904 
905 	DTRACE_NFSV4_3(cb__recall__done, rfs4_client_t *, dsp->client,
906 	    rfs4_deleg_state_t *, dsp, CB_RECALL4res *, rec_resp);
907 
908 	if (call_stat != RPC_SUCCESS || cb4_res.status != NFS4_OK) {
909 		rfs4_revoke_deleg(dsp);
910 	}
911 
912 	rfs4freeargres(&cb4_args, &cb4_res);
913 }
914 
915 struct recall_arg {
916 	rfs4_deleg_state_t *dsp;
917 	void (*recall)(rfs4_deleg_state_t *, bool_t trunc);
918 	bool_t trunc;
919 };
920 
921 static void
922 do_recall(struct recall_arg *arg)
923 {
924 	rfs4_deleg_state_t *dsp = arg->dsp;
925 	rfs4_file_t *fp = dsp->finfo;
926 	callb_cpr_t cpr_info;
927 	kmutex_t cpr_lock;
928 
929 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
930 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Recall");
931 
932 	/*
933 	 * It is possible that before this thread starts
934 	 * the client has send us a return_delegation, and
935 	 * if that is the case we do not need to send the
936 	 * recall callback.
937 	 */
938 	if (dsp->dtype != OPEN_DELEGATE_NONE) {
939 		DTRACE_PROBE3(nfss__i__recall,
940 		    struct recall_arg *, arg,
941 		    struct rfs4_deleg_state_t *, dsp,
942 		    struct rfs4_file_t *, fp);
943 
944 		if (arg->recall)
945 			(void) (*arg->recall)(dsp, arg->trunc);
946 	}
947 
948 	mutex_enter(fp->dinfo->recall_lock);
949 	/*
950 	 * Recall count may go negative if the parent thread that is
951 	 * creating the individual callback threads does not modify
952 	 * the recall_count field before the callback thread actually
953 	 * gets a response from the CB_RECALL
954 	 */
955 	fp->dinfo->recall_count--;
956 	if (fp->dinfo->recall_count == 0)
957 		cv_signal(fp->dinfo->recall_cv);
958 	mutex_exit(fp->dinfo->recall_lock);
959 
960 	mutex_enter(&cpr_lock);
961 	CALLB_CPR_EXIT(&cpr_info);
962 	mutex_destroy(&cpr_lock);
963 
964 	rfs4_deleg_state_rele(dsp); /* release the hold for this thread */
965 
966 	kmem_free(arg, sizeof (struct recall_arg));
967 }
968 
969 struct master_recall_args {
970     rfs4_file_t *fp;
971     void (*recall)(rfs4_deleg_state_t *, bool_t);
972     bool_t trunc;
973 };
974 
975 static void
976 do_recall_file(struct master_recall_args *map)
977 {
978 	rfs4_file_t *fp = map->fp;
979 	rfs4_deleg_state_t *dsp;
980 	struct recall_arg *arg;
981 	callb_cpr_t cpr_info;
982 	kmutex_t cpr_lock;
983 	int32_t recall_count;
984 
985 	rfs4_dbe_lock(fp->dbe);
986 
987 	/* Recall already in progress ? */
988 	mutex_enter(fp->dinfo->recall_lock);
989 	if (fp->dinfo->recall_count != 0) {
990 		mutex_exit(fp->dinfo->recall_lock);
991 		rfs4_dbe_rele_nolock(fp->dbe);
992 		rfs4_dbe_unlock(fp->dbe);
993 		kmem_free(map, sizeof (struct master_recall_args));
994 		return;
995 	}
996 
997 	mutex_exit(fp->dinfo->recall_lock);
998 
999 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
1000 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,	"v4RecallFile");
1001 
1002 	recall_count = 0;
1003 	for (dsp = fp->delegationlist.next->dsp; dsp != NULL;
1004 	    dsp = dsp->delegationlist.next->dsp) {
1005 
1006 		rfs4_dbe_lock(dsp->dbe);
1007 		/*
1008 		 * if this delegation state
1009 		 * is being reaped skip it
1010 		 */
1011 		if (rfs4_dbe_is_invalid(dsp->dbe)) {
1012 			rfs4_dbe_unlock(dsp->dbe);
1013 			continue;
1014 		}
1015 
1016 		/* hold for receiving thread */
1017 		rfs4_dbe_hold(dsp->dbe);
1018 		rfs4_dbe_unlock(dsp->dbe);
1019 
1020 		arg = kmem_alloc(sizeof (struct recall_arg), KM_SLEEP);
1021 		arg->recall = map->recall;
1022 		arg->trunc = map->trunc;
1023 		arg->dsp = dsp;
1024 
1025 		recall_count++;
1026 
1027 		(void) thread_create(NULL, 0, do_recall, arg, 0, &p0, TS_RUN,
1028 		    minclsyspri);
1029 	}
1030 
1031 	rfs4_dbe_unlock(fp->dbe);
1032 
1033 	mutex_enter(fp->dinfo->recall_lock);
1034 	/*
1035 	 * Recall count may go negative if the parent thread that is
1036 	 * creating the individual callback threads does not modify
1037 	 * the recall_count field before the callback thread actually
1038 	 * gets a response from the CB_RECALL
1039 	 */
1040 	fp->dinfo->recall_count += recall_count;
1041 	while (fp->dinfo->recall_count)
1042 		cv_wait(fp->dinfo->recall_cv, fp->dinfo->recall_lock);
1043 
1044 	mutex_exit(fp->dinfo->recall_lock);
1045 
1046 	DTRACE_PROBE1(nfss__i__recall_done, rfs4_file_t *, fp);
1047 	rfs4_file_rele(fp);
1048 	kmem_free(map, sizeof (struct master_recall_args));
1049 	mutex_enter(&cpr_lock);
1050 	CALLB_CPR_EXIT(&cpr_info);
1051 	mutex_destroy(&cpr_lock);
1052 }
1053 
1054 static void
1055 rfs4_recall_file(rfs4_file_t *fp,
1056 	void (*recall)(rfs4_deleg_state_t *, bool_t trunc),
1057 	bool_t trunc, rfs4_client_t *cp)
1058 {
1059 	struct master_recall_args *args;
1060 
1061 	rfs4_dbe_lock(fp->dbe);
1062 	if (fp->dinfo->dtype == OPEN_DELEGATE_NONE) {
1063 		rfs4_dbe_unlock(fp->dbe);
1064 		return;
1065 	}
1066 	rfs4_dbe_hold(fp->dbe);	/* hold for new thread */
1067 
1068 	/*
1069 	 * Mark the time we started the recall processing.
1070 	 * If it has been previously recalled, do not reset the
1071 	 * timer since this is used for the revocation decision.
1072 	 */
1073 	if (fp->dinfo->time_recalled == 0)
1074 		fp->dinfo->time_recalled = gethrestime_sec();
1075 	fp->dinfo->ever_recalled = TRUE; /* used for policy decision */
1076 	/* Client causing recall not always available */
1077 	if (cp)
1078 		fp->dinfo->conflicted_client = cp->clientid;
1079 
1080 	rfs4_dbe_unlock(fp->dbe);
1081 
1082 	args = kmem_alloc(sizeof (struct master_recall_args), KM_SLEEP);
1083 	args->fp = fp;
1084 	args->recall = recall;
1085 	args->trunc = trunc;
1086 
1087 	(void) thread_create(NULL, 0, do_recall_file, args, 0, &p0, TS_RUN,
1088 	    minclsyspri);
1089 }
1090 
1091 void
1092 rfs4_recall_deleg(rfs4_file_t *fp, bool_t trunc, rfs4_client_t *cp)
1093 {
1094 	time_t elapsed1, elapsed2;
1095 
1096 	if (fp->dinfo->time_recalled != 0) {
1097 		elapsed1 = gethrestime_sec() - fp->dinfo->time_recalled;
1098 		elapsed2 = gethrestime_sec() - fp->dinfo->time_lastwrite;
1099 		/* First check to see if a revocation should occur */
1100 		if (elapsed1 > rfs4_lease_time &&
1101 		    elapsed2 > rfs4_lease_time) {
1102 			rfs4_revoke_file(fp);
1103 			return;
1104 		}
1105 		/*
1106 		 * Next check to see if a recall should be done again
1107 		 * so quickly.
1108 		 */
1109 		if (elapsed1 <= ((rfs4_lease_time * 20) / 100))
1110 			return;
1111 	}
1112 	rfs4_recall_file(fp, rfs4_do_cb_recall, trunc, cp);
1113 }
1114 
1115 /*
1116  * rfs4_check_recall is called from rfs4_do_open to determine if the current
1117  * open conflicts with the delegation.
1118  * Return true if we need recall otherwise false.
1119  * Assumes entry locks for sp and sp->finfo are held.
1120  */
1121 bool_t
1122 rfs4_check_recall(rfs4_state_t *sp, uint32_t access)
1123 {
1124 	open_delegation_type4 dtype = sp->finfo->dinfo->dtype;
1125 
1126 	switch (dtype) {
1127 	case OPEN_DELEGATE_NONE:
1128 		/* Not currently delegated so there is nothing to do */
1129 		return (FALSE);
1130 	case OPEN_DELEGATE_READ:
1131 		/*
1132 		 * If the access is only asking for READ then there is
1133 		 * no conflict and nothing to do.  If it is asking
1134 		 * for write, then there will be conflict and the read
1135 		 * delegation should be recalled.
1136 		 */
1137 		if (access == OPEN4_SHARE_ACCESS_READ)
1138 			return (FALSE);
1139 		else
1140 			return (TRUE);
1141 	case OPEN_DELEGATE_WRITE:
1142 		/* Check to see if this client has the delegation */
1143 		return (rfs4_is_deleg(sp));
1144 	}
1145 
1146 	return (FALSE);
1147 }
1148 
1149 /*
1150  * Return the "best" allowable delegation available given the current
1151  * delegation type and the desired access and deny modes on the file.
1152  * At the point that this routine is called we know that the access and
1153  * deny modes are consistent with the file modes.
1154  */
1155 static open_delegation_type4
1156 rfs4_check_delegation(rfs4_state_t *sp, rfs4_file_t *fp)
1157 {
1158 	open_delegation_type4 dtype = fp->dinfo->dtype;
1159 	uint32_t access = sp->share_access;
1160 	uint32_t deny = sp->share_deny;
1161 	int readcnt = 0;
1162 	int writecnt = 0;
1163 
1164 	switch (dtype) {
1165 	case OPEN_DELEGATE_NONE:
1166 		/*
1167 		 * Determine if more than just this OPEN have the file
1168 		 * open and if so, no delegation may be provided to
1169 		 * the client.
1170 		 */
1171 		if (access & OPEN4_SHARE_ACCESS_WRITE)
1172 			writecnt++;
1173 		if (access & OPEN4_SHARE_ACCESS_READ)
1174 			readcnt++;
1175 
1176 		if (fp->access_read > readcnt || fp->access_write > writecnt)
1177 			return (OPEN_DELEGATE_NONE);
1178 
1179 		/*
1180 		 * If the client is going to write, or if the client
1181 		 * has exclusive access, return a write delegation.
1182 		 */
1183 		if ((access & OPEN4_SHARE_ACCESS_WRITE) ||
1184 		    (deny & (OPEN4_SHARE_DENY_READ | OPEN4_SHARE_DENY_WRITE)))
1185 			return (OPEN_DELEGATE_WRITE);
1186 		/*
1187 		 * If we don't want to write or we've haven't denied read
1188 		 * access to others, return a read delegation.
1189 		 */
1190 		if ((access & ~OPEN4_SHARE_ACCESS_WRITE) ||
1191 		    (deny & ~OPEN4_SHARE_DENY_READ))
1192 			return (OPEN_DELEGATE_READ);
1193 
1194 		/* Shouldn't get here */
1195 		return (OPEN_DELEGATE_NONE);
1196 
1197 	case OPEN_DELEGATE_READ:
1198 		/*
1199 		 * If the file is delegated for read but we wan't to
1200 		 * write or deny others to read then we can't delegate
1201 		 * the file. We shouldn't get here since the delegation should
1202 		 * have been recalled already.
1203 		 */
1204 		if ((access & OPEN4_SHARE_ACCESS_WRITE) ||
1205 		    (deny & OPEN4_SHARE_DENY_READ))
1206 			return (OPEN_DELEGATE_NONE);
1207 		return (OPEN_DELEGATE_READ);
1208 
1209 	case OPEN_DELEGATE_WRITE:
1210 		return (OPEN_DELEGATE_WRITE);
1211 	}
1212 
1213 	/* Shouldn't get here */
1214 	return (OPEN_DELEGATE_NONE);
1215 }
1216 
1217 /*
1218  * Given the desired delegation type and the "history" of the file
1219  * determine the actual delegation type to return.
1220  */
1221 static open_delegation_type4
1222 rfs4_delegation_policy(open_delegation_type4 dtype,
1223 	rfs4_dinfo_t *dinfo, clientid4 cid)
1224 {
1225 	time_t elapsed;
1226 
1227 	if (rfs4_deleg_policy != SRV_NORMAL_DELEGATE)
1228 		return (OPEN_DELEGATE_NONE);
1229 
1230 	/*
1231 	 * Has this file/delegation ever been recalled?  If not then
1232 	 * no furhter checks for a delegation race need to be done.
1233 	 * However if a recall has occurred, then check to see if a
1234 	 * client has caused its own delegation recall to occur.  If
1235 	 * not, then has a delegation for this file been returned
1236 	 * recently?  If so, then do not assign a new delegation to
1237 	 * avoid a "delegation race" between the original client and
1238 	 * the new/conflicting client.
1239 	 */
1240 	if (dinfo->ever_recalled == TRUE) {
1241 		if (dinfo->conflicted_client != cid) {
1242 			elapsed = gethrestime_sec() - dinfo->time_returned;
1243 			if (elapsed < rfs4_lease_time)
1244 				return (OPEN_DELEGATE_NONE);
1245 		}
1246 	}
1247 
1248 	/* Limit the number of read grants */
1249 	if (dtype == OPEN_DELEGATE_READ &&
1250 	    dinfo->rdgrants > MAX_READ_DELEGATIONS)
1251 		return (OPEN_DELEGATE_NONE);
1252 
1253 	/*
1254 	 * Should consider limiting total number of read/write
1255 	 * delegations the server will permit.
1256 	 */
1257 
1258 	return (dtype);
1259 }
1260 
1261 /*
1262  * Try and grant a delegation for an open give the state. The routine
1263  * returns the delegation type granted. This could be OPEN_DELEGATE_NONE.
1264  *
1265  * The state and associate file entry must be locked
1266  */
1267 rfs4_deleg_state_t *
1268 rfs4_grant_delegation(delegreq_t dreq, rfs4_state_t *sp, int *recall)
1269 {
1270 	rfs4_file_t *fp = sp->finfo;
1271 	open_delegation_type4 dtype;
1272 	int no_delegation;
1273 
1274 	ASSERT(rfs4_dbe_islocked(sp->dbe));
1275 	ASSERT(rfs4_dbe_islocked(fp->dbe));
1276 
1277 	/* Is the server even providing delegations? */
1278 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE || dreq == DELEG_NONE)
1279 		return (NULL);
1280 
1281 	/* Check to see if delegations have been temporarily disabled */
1282 	mutex_enter(&rfs4_deleg_lock);
1283 	no_delegation = rfs4_deleg_disabled;
1284 	mutex_exit(&rfs4_deleg_lock);
1285 
1286 	if (no_delegation)
1287 		return (NULL);
1288 
1289 	/* Don't grant a delegation if a deletion is impending. */
1290 	if (fp->dinfo->hold_grant > 0) {
1291 		return (NULL);
1292 	}
1293 
1294 	/*
1295 	 * Don't grant a delegation if there are any lock manager
1296 	 * (NFSv2/v3) locks for the file.  This is a bit of a hack (e.g.,
1297 	 * if there are only read locks we should be able to grant a
1298 	 * read-only delegation), but it's good enough for now.
1299 	 *
1300 	 * MT safety: the lock manager checks for conflicting delegations
1301 	 * before processing a lock request.  That check will block until
1302 	 * we are done here.  So if the lock manager acquires a lock after
1303 	 * we decide to grant the delegation, the delegation will get
1304 	 * immediately recalled (if there's a conflict), so we're safe.
1305 	 */
1306 	if (lm_vp_active(fp->vp)) {
1307 		return (NULL);
1308 	}
1309 
1310 	/*
1311 	 * Based on the type of delegation request passed in, take the
1312 	 * appropriate action (DELEG_NONE is handled above)
1313 	 */
1314 	switch (dreq) {
1315 
1316 	case DELEG_READ:
1317 	case DELEG_WRITE:
1318 		/*
1319 		 * The server "must" grant the delegation in this case.
1320 		 * Client is using open previous
1321 		 */
1322 		dtype = (open_delegation_type4)dreq;
1323 		*recall = 1;
1324 		break;
1325 	case DELEG_ANY:
1326 		/*
1327 		 * If a valid callback path does not exist, no delegation may
1328 		 * be granted.
1329 		 */
1330 		if (sp->owner->client->cbinfo.cb_state != CB_OK)
1331 			return (NULL);
1332 
1333 		/*
1334 		 * If the original operation which caused time_rm_delayed
1335 		 * to be set hasn't been retried and completed for one
1336 		 * full lease period, clear it and allow delegations to
1337 		 * get granted again.
1338 		 */
1339 		if (fp->dinfo->time_rm_delayed > 0 &&
1340 		    gethrestime_sec() >
1341 		    fp->dinfo->time_rm_delayed + rfs4_lease_time)
1342 			fp->dinfo->time_rm_delayed = 0;
1343 
1344 		/*
1345 		 * If we are waiting for a delegation to be returned then
1346 		 * don't delegate this file. We do this for correctness as
1347 		 * well as if the file is being recalled we would likely
1348 		 * recall this file again.
1349 		 */
1350 
1351 		if (fp->dinfo->time_recalled != 0 ||
1352 		    fp->dinfo->time_rm_delayed != 0)
1353 			return (NULL);
1354 
1355 		/* Get the "best" delegation candidate */
1356 		dtype = rfs4_check_delegation(sp, fp);
1357 
1358 		if (dtype == OPEN_DELEGATE_NONE)
1359 			return (NULL);
1360 
1361 		/*
1362 		 * Based on policy and the history of the file get the
1363 		 * actual delegation.
1364 		 */
1365 		dtype = rfs4_delegation_policy(dtype, fp->dinfo,
1366 		    sp->owner->client->clientid);
1367 
1368 		if (dtype == OPEN_DELEGATE_NONE)
1369 			return (NULL);
1370 		break;
1371 	default:
1372 		return (NULL);
1373 	}
1374 
1375 	/* set the delegation for the state */
1376 	return (rfs4_deleg_state(sp, dtype, recall));
1377 }
1378 
1379 void
1380 rfs4_set_deleg_response(rfs4_deleg_state_t *dsp, open_delegation4 *dp,
1381 			nfsace4 *ace,  int recall)
1382 {
1383 	open_write_delegation4 *wp;
1384 	open_read_delegation4 *rp;
1385 	nfs_space_limit4 *spl;
1386 	nfsace4 nace;
1387 
1388 	/*
1389 	 * We need to allocate a new copy of the who string.
1390 	 * this string will be freed by the rfs4_op_open dis_resfree
1391 	 * routine. We need to do this allocation since replays will
1392 	 * be allocated and rfs4_compound can't tell the difference from
1393 	 * a replay and an inital open. N.B. if an ace is passed in, it
1394 	 * the caller's responsibility to free it.
1395 	 */
1396 
1397 	if (ace == NULL) {
1398 		/*
1399 		 * Default is to deny all access, the client will have
1400 		 * to contact the server.  XXX Do we want to actually
1401 		 * set a deny for every one, or do we simply want to
1402 		 * construct an entity that will match no one?
1403 		 */
1404 		nace.type = ACE4_ACCESS_DENIED_ACE_TYPE;
1405 		nace.flag = 0;
1406 		nace.access_mask = ACE4_VALID_MASK_BITS;
1407 		(void) str_to_utf8(ACE4_WHO_EVERYONE, &nace.who);
1408 	} else {
1409 		nace.type = ace->type;
1410 		nace.flag = ace->flag;
1411 		nace.access_mask = ace->access_mask;
1412 		(void) utf8_copy(&ace->who, &nace.who);
1413 	}
1414 
1415 	dp->delegation_type = dsp->dtype;
1416 
1417 	switch (dsp->dtype) {
1418 	case OPEN_DELEGATE_NONE:
1419 		break;
1420 	case OPEN_DELEGATE_READ:
1421 		rp = &dp->open_delegation4_u.read;
1422 		rp->stateid = dsp->delegid.stateid;
1423 		rp->recall = (bool_t)recall;
1424 		rp->permissions = nace;
1425 		break;
1426 	case OPEN_DELEGATE_WRITE:
1427 		wp = &dp->open_delegation4_u.write;
1428 		wp->stateid = dsp->delegid.stateid;
1429 		wp->recall = (bool_t)recall;
1430 		spl = &wp->space_limit;
1431 		spl->limitby = NFS_LIMIT_SIZE;
1432 		spl->nfs_space_limit4_u.filesize = 0;
1433 		wp->permissions = nace;
1434 		break;
1435 	}
1436 }
1437 
1438 /*
1439  * Check if the file is delegated via the provided file struct.
1440  * Return TRUE if it is delegated.  This is intended for use by
1441  * the v4 server.  The v2/v3 server code should use rfs4_check_delegated().
1442  *
1443  * Note that if the file is found to have a delegation, it is
1444  * recalled, unless the clientid of the caller matches the clientid of the
1445  * delegation. If the caller has specified, there is a slight delay
1446  * inserted in the hopes that the delegation will be returned quickly.
1447  */
1448 bool_t
1449 rfs4_check_delegated_byfp(int mode, rfs4_file_t *fp,
1450 	bool_t trunc, bool_t do_delay, bool_t is_rm, clientid4 *cp)
1451 {
1452 	rfs4_deleg_state_t *dsp;
1453 
1454 	/* Is delegation enabled? */
1455 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE)
1456 		return (FALSE);
1457 
1458 	/* do we have a delegation on this file? */
1459 	rfs4_dbe_lock(fp->dbe);
1460 	if (fp->dinfo->dtype == OPEN_DELEGATE_NONE) {
1461 		if (is_rm)
1462 			fp->dinfo->hold_grant++;
1463 		rfs4_dbe_unlock(fp->dbe);
1464 		return (FALSE);
1465 	}
1466 	/*
1467 	 * do we have a write delegation on this file or are we
1468 	 * requesting write access to a file with any type of existing
1469 	 * delegation?
1470 	 */
1471 	if (mode == FWRITE || fp->dinfo->dtype == OPEN_DELEGATE_WRITE) {
1472 		if (cp != NULL) {
1473 			dsp = fp->delegationlist.next->dsp;
1474 			if (dsp == NULL) {
1475 				rfs4_dbe_unlock(fp->dbe);
1476 				return (FALSE);
1477 			}
1478 			/*
1479 			 * Does the requestor already own the delegation?
1480 			 */
1481 			if (dsp->client->clientid == *(cp)) {
1482 				rfs4_dbe_unlock(fp->dbe);
1483 				return (FALSE);
1484 			}
1485 		}
1486 
1487 		rfs4_dbe_unlock(fp->dbe);
1488 		rfs4_recall_deleg(fp, trunc, NULL);
1489 
1490 		if (!do_delay) {
1491 			rfs4_dbe_lock(fp->dbe);
1492 			fp->dinfo->time_rm_delayed = gethrestime_sec();
1493 			rfs4_dbe_unlock(fp->dbe);
1494 			return (TRUE);
1495 		}
1496 
1497 		delay(NFS4_DELEGATION_CONFLICT_DELAY);
1498 
1499 		rfs4_dbe_lock(fp->dbe);
1500 		if (fp->dinfo->dtype != OPEN_DELEGATE_NONE) {
1501 			fp->dinfo->time_rm_delayed = gethrestime_sec();
1502 			rfs4_dbe_unlock(fp->dbe);
1503 			return (TRUE);
1504 		}
1505 	}
1506 	if (is_rm)
1507 		fp->dinfo->hold_grant++;
1508 	rfs4_dbe_unlock(fp->dbe);
1509 	return (FALSE);
1510 }
1511 
1512 /*
1513  * Check if the file is delegated in the case of a v2 or v3 access.
1514  * Return TRUE if it is delegated which in turn means that v2 should
1515  * drop the request and in the case of v3 JUKEBOX should be returned.
1516  */
1517 bool_t
1518 rfs4_check_delegated(int mode, vnode_t *vp, bool_t trunc)
1519 {
1520 	rfs4_file_t *fp;
1521 	bool_t create = FALSE;
1522 	bool_t rc = FALSE;
1523 
1524 	rfs4_hold_deleg_policy();
1525 
1526 	/* Is delegation enabled? */
1527 	if (rfs4_deleg_policy != SRV_NEVER_DELEGATE) {
1528 		fp = rfs4_findfile(vp, NULL, &create);
1529 		if (fp != NULL) {
1530 			if (rfs4_check_delegated_byfp(mode, fp, trunc,
1531 			    TRUE, FALSE, NULL)) {
1532 				rc = TRUE;
1533 			}
1534 			rfs4_file_rele(fp);
1535 		}
1536 	}
1537 	rfs4_rele_deleg_policy();
1538 	return (rc);
1539 }
1540 
1541 /*
1542  * Release a hold on the hold_grant counter which
1543  * prevents delegation from being granted while a remove
1544  * or a rename is in progress.
1545  */
1546 void
1547 rfs4_clear_dont_grant(rfs4_file_t *fp)
1548 {
1549 	if (rfs4_deleg_policy == SRV_NEVER_DELEGATE)
1550 		return;
1551 	rfs4_dbe_lock(fp->dbe);
1552 	ASSERT(fp->dinfo->hold_grant > 0);
1553 	fp->dinfo->hold_grant--;
1554 	fp->dinfo->time_rm_delayed = 0;
1555 	rfs4_dbe_unlock(fp->dbe);
1556 }
1557 
1558 /*
1559  * State support for delegation.
1560  * Set the state delegation type for this state;
1561  * This routine is called from open via rfs4_grant_delegation and the entry
1562  * locks on sp and sp->finfo are assumed.
1563  */
1564 static rfs4_deleg_state_t *
1565 rfs4_deleg_state(rfs4_state_t *sp, open_delegation_type4 dtype, int *recall)
1566 {
1567 	rfs4_file_t *fp = sp->finfo;
1568 	bool_t create = TRUE;
1569 	rfs4_deleg_state_t *dsp;
1570 	vnode_t *vp;
1571 	int open_prev = *recall;
1572 	int ret;
1573 	int fflags = 0;
1574 
1575 	ASSERT(rfs4_dbe_islocked(sp->dbe));
1576 	ASSERT(rfs4_dbe_islocked(fp->dbe));
1577 
1578 	/* Shouldn't happen */
1579 	if (fp->dinfo->recall_count != 0 ||
1580 	    (fp->dinfo->dtype == OPEN_DELEGATE_READ &&
1581 	    dtype != OPEN_DELEGATE_READ)) {
1582 		return (NULL);
1583 	}
1584 
1585 	/* Unlock to avoid deadlock */
1586 	rfs4_dbe_unlock(fp->dbe);
1587 	rfs4_dbe_unlock(sp->dbe);
1588 
1589 	dsp = rfs4_finddeleg(sp, &create);
1590 
1591 	rfs4_dbe_lock(sp->dbe);
1592 	rfs4_dbe_lock(fp->dbe);
1593 
1594 	if (dsp == NULL)
1595 		return (NULL);
1596 
1597 	/*
1598 	 * It is possible that since we dropped the lock
1599 	 * in order to call finddeleg, the rfs4_file_t
1600 	 * was marked such that we should not grant a
1601 	 * delegation, if so bail out.
1602 	 */
1603 	if (fp->dinfo->hold_grant > 0) {
1604 		rfs4_deleg_state_rele(dsp);
1605 		return (NULL);
1606 	}
1607 
1608 	if (create == FALSE) {
1609 		if (sp->owner->client == dsp->client &&
1610 		    dsp->dtype == dtype) {
1611 			return (dsp);
1612 		} else {
1613 			rfs4_deleg_state_rele(dsp);
1614 			return (NULL);
1615 		}
1616 	}
1617 
1618 	/*
1619 	 * Check that this file has not been delegated to another
1620 	 * client
1621 	 */
1622 	if (fp->dinfo->recall_count != 0 ||
1623 	    fp->dinfo->dtype == OPEN_DELEGATE_WRITE ||
1624 	    (fp->dinfo->dtype == OPEN_DELEGATE_READ &&
1625 	    dtype != OPEN_DELEGATE_READ)) {
1626 		rfs4_deleg_state_rele(dsp);
1627 		return (NULL);
1628 	}
1629 
1630 	vp = fp->vp;
1631 	/* vnevent_support returns 0 if file system supports vnevents */
1632 	if (vnevent_support(vp, NULL)) {
1633 		rfs4_deleg_state_rele(dsp);
1634 		return (NULL);
1635 	}
1636 
1637 	/* Calculate the fflags for this OPEN. */
1638 	if (sp->share_access & OPEN4_SHARE_ACCESS_READ)
1639 		fflags |= FREAD;
1640 	if (sp->share_access & OPEN4_SHARE_ACCESS_WRITE)
1641 		fflags |= FWRITE;
1642 
1643 	*recall = 0;
1644 	/*
1645 	 * Before granting a delegation we need to know if anyone else has
1646 	 * opened the file in a conflicting mode.  However, first we need to
1647 	 * know how we opened the file to check the counts properly.
1648 	 */
1649 	if (dtype == OPEN_DELEGATE_READ) {
1650 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1651 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1652 		    vn_is_mapped(vp, V_WRITE)) {
1653 			if (open_prev) {
1654 				*recall = 1;
1655 			} else {
1656 				rfs4_deleg_state_rele(dsp);
1657 				return (NULL);
1658 			}
1659 		}
1660 		ret = fem_install(vp, deleg_rdops, (void *)fp, OPUNIQ,
1661 		    rfs4_mon_hold, rfs4_mon_rele);
1662 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1663 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1664 		    vn_is_mapped(vp, V_WRITE)) {
1665 			if (open_prev) {
1666 				*recall = 1;
1667 			} else {
1668 				(void) fem_uninstall(vp, deleg_rdops,
1669 				    (void *)fp);
1670 				rfs4_deleg_state_rele(dsp);
1671 				return (NULL);
1672 			}
1673 		}
1674 		/*
1675 		 * Because a client can hold onto a delegation after the
1676 		 * file has been closed, we need to keep track of the
1677 		 * access to this file.  Otherwise the CIFS server would
1678 		 * not know about the client accessing the file and could
1679 		 * inappropriately grant an OPLOCK.
1680 		 * fem_install() returns EBUSY when asked to install a
1681 		 * OPUNIQ monitor more than once.  Therefore, check the
1682 		 * return code because we only want this done once.
1683 		 */
1684 		if (ret == 0)
1685 			vn_open_upgrade(vp, FREAD);
1686 	} else { /* WRITE */
1687 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1688 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1689 		    ((fflags & FREAD) && vn_has_other_opens(vp, V_READ)) ||
1690 		    (((fflags & FREAD) == 0) && vn_is_opened(vp, V_READ)) ||
1691 		    vn_is_mapped(vp, V_RDORWR)) {
1692 			if (open_prev) {
1693 				*recall = 1;
1694 			} else {
1695 				rfs4_deleg_state_rele(dsp);
1696 				return (NULL);
1697 			}
1698 		}
1699 		ret = fem_install(vp, deleg_wrops, (void *)fp, OPUNIQ,
1700 		    rfs4_mon_hold, rfs4_mon_rele);
1701 		if (((fflags & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
1702 		    (((fflags & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
1703 		    ((fflags & FREAD) && vn_has_other_opens(vp, V_READ)) ||
1704 		    (((fflags & FREAD) == 0) && vn_is_opened(vp, V_READ)) ||
1705 		    vn_is_mapped(vp, V_RDORWR)) {
1706 			if (open_prev) {
1707 				*recall = 1;
1708 			} else {
1709 				(void) fem_uninstall(vp, deleg_wrops,
1710 				    (void *)fp);
1711 				rfs4_deleg_state_rele(dsp);
1712 				return (NULL);
1713 			}
1714 		}
1715 		/*
1716 		 * Because a client can hold onto a delegation after the
1717 		 * file has been closed, we need to keep track of the
1718 		 * access to this file.  Otherwise the CIFS server would
1719 		 * not know about the client accessing the file and could
1720 		 * inappropriately grant an OPLOCK.
1721 		 * fem_install() returns EBUSY when asked to install a
1722 		 * OPUNIQ monitor more than once.  Therefore, check the
1723 		 * return code because we only want this done once.
1724 		 */
1725 		if (ret == 0)
1726 			vn_open_upgrade(vp, FREAD|FWRITE);
1727 	}
1728 	/* Place on delegation list for file */
1729 	insque(&dsp->delegationlist, fp->delegationlist.prev);
1730 
1731 	dsp->dtype = fp->dinfo->dtype = dtype;
1732 
1733 	/* Update delegation stats for this file */
1734 	fp->dinfo->time_lastgrant = gethrestime_sec();
1735 
1736 	/* reset since this is a new delegation */
1737 	fp->dinfo->conflicted_client = 0;
1738 	fp->dinfo->ever_recalled = FALSE;
1739 
1740 	if (dtype == OPEN_DELEGATE_READ)
1741 		fp->dinfo->rdgrants++;
1742 	else
1743 		fp->dinfo->wrgrants++;
1744 
1745 	return (dsp);
1746 }
1747 
1748 /*
1749  * State routine for the server when a delegation is returned.
1750  */
1751 void
1752 rfs4_return_deleg(rfs4_deleg_state_t *dsp, bool_t revoked)
1753 {
1754 	rfs4_file_t *fp = dsp->finfo;
1755 	open_delegation_type4 dtypewas;
1756 
1757 	rfs4_dbe_lock(fp->dbe);
1758 	/* Remove state from recall list */
1759 
1760 	remque(&dsp->delegationlist);
1761 	dsp->delegationlist.next = dsp->delegationlist.prev =
1762 	    &dsp->delegationlist;
1763 
1764 	if (&fp->delegationlist == fp->delegationlist.next) {
1765 		dtypewas = fp->dinfo->dtype;
1766 		fp->dinfo->dtype = OPEN_DELEGATE_NONE;
1767 		rfs4_dbe_cv_broadcast(fp->dbe);
1768 
1769 		/* if file system was unshared, the vp will be NULL */
1770 		if (fp->vp != NULL) {
1771 			/*
1772 			 * Once a delegation is no longer held by any client,
1773 			 * the monitor is uninstalled.  At this point, the
1774 			 * client must send OPEN otw, so we don't need the
1775 			 * reference on the vnode anymore.  The open
1776 			 * downgrade removes the reference put on earlier.
1777 			 */
1778 			if (dtypewas == OPEN_DELEGATE_READ) {
1779 				(void) fem_uninstall(fp->vp, deleg_rdops,
1780 				    (void *)fp);
1781 				vn_open_downgrade(fp->vp, FREAD);
1782 			} else if (dtypewas == OPEN_DELEGATE_WRITE) {
1783 				(void) fem_uninstall(fp->vp, deleg_wrops,
1784 				    (void *)fp);
1785 				vn_open_downgrade(fp->vp, FREAD|FWRITE);
1786 			}
1787 		}
1788 	}
1789 
1790 	switch (dsp->dtype) {
1791 	case OPEN_DELEGATE_READ:
1792 		fp->dinfo->rdgrants--;
1793 		break;
1794 	case OPEN_DELEGATE_WRITE:
1795 		fp->dinfo->wrgrants--;
1796 		break;
1797 	default:
1798 		break;
1799 	}
1800 
1801 	/* used in the policy decision */
1802 	fp->dinfo->time_returned = gethrestime_sec();
1803 
1804 	/*
1805 	 * reset the time_recalled field so future delegations are not
1806 	 * accidentally revoked
1807 	 */
1808 	if ((fp->dinfo->rdgrants + fp->dinfo->wrgrants) == 0)
1809 		fp->dinfo->time_recalled = 0;
1810 
1811 	rfs4_dbe_unlock(fp->dbe);
1812 
1813 	rfs4_dbe_lock(dsp->dbe);
1814 
1815 	dsp->dtype = OPEN_DELEGATE_NONE;
1816 
1817 	if (revoked == TRUE)
1818 		dsp->time_revoked = gethrestime_sec();
1819 
1820 	rfs4_dbe_invalidate(dsp->dbe);
1821 
1822 	rfs4_dbe_unlock(dsp->dbe);
1823 
1824 	if (revoked == TRUE) {
1825 		rfs4_dbe_lock(dsp->client->dbe);
1826 		dsp->client->deleg_revoked++;	/* observability */
1827 		rfs4_dbe_unlock(dsp->client->dbe);
1828 	}
1829 }
1830 
1831 static void
1832 rfs4_revoke_deleg(rfs4_deleg_state_t *dsp)
1833 {
1834 	rfs4_return_deleg(dsp, TRUE);
1835 }
1836 
1837 static void
1838 rfs4_revoke_file(rfs4_file_t *fp)
1839 {
1840 	rfs4_deleg_state_t *dsp;
1841 
1842 	/*
1843 	 * The lock for rfs4_file_t must be held when traversing the
1844 	 * delegation list but that lock needs to be released to call
1845 	 * rfs4_revoke_deleg()
1846 	 * This for loop is set up to check the list for being empty,
1847 	 * and locking the rfs4_file_t struct on init and end
1848 	 */
1849 	for (rfs4_dbe_lock(fp->dbe);
1850 	    &fp->delegationlist != fp->delegationlist.next;
1851 	    rfs4_dbe_lock(fp->dbe)) {
1852 
1853 		dsp = fp->delegationlist.next->dsp;
1854 		rfs4_dbe_hold(dsp->dbe);
1855 		rfs4_dbe_unlock(fp->dbe);
1856 		rfs4_revoke_deleg(dsp);
1857 		rfs4_deleg_state_rele(dsp);
1858 	}
1859 	rfs4_dbe_unlock(fp->dbe);
1860 }
1861 
1862 /*
1863  * A delegation is assumed to be present on the file associated with
1864  * "state".  Check to see if the delegation matches is associated with
1865  * the same client as referenced by "state".  If it is not, TRUE is
1866  * returned.  If the delegation DOES match the client (or no
1867  * delegation is present), return FALSE.
1868  * Assume the state entry and file entry are locked.
1869  */
1870 bool_t
1871 rfs4_is_deleg(rfs4_state_t *state)
1872 {
1873 	rfs4_deleg_state_t *dsp;
1874 	rfs4_file_t *fp = state->finfo;
1875 	rfs4_client_t *cp = state->owner->client;
1876 
1877 	ASSERT(rfs4_dbe_islocked(fp->dbe));
1878 	for (dsp = fp->delegationlist.next->dsp; dsp != NULL;
1879 	    dsp = dsp->delegationlist.next->dsp) {
1880 		if (cp != dsp->client) {
1881 			return (TRUE);
1882 		}
1883 	}
1884 	return (FALSE);
1885 }
1886 
1887 void
1888 rfs4_disable_delegation(void)
1889 {
1890 	mutex_enter(&rfs4_deleg_lock);
1891 	rfs4_deleg_disabled++;
1892 	mutex_exit(&rfs4_deleg_lock);
1893 }
1894 
1895 void
1896 rfs4_enable_delegation(void)
1897 {
1898 	mutex_enter(&rfs4_deleg_lock);
1899 	ASSERT(rfs4_deleg_disabled > 0);
1900 	rfs4_deleg_disabled--;
1901 	mutex_exit(&rfs4_deleg_lock);
1902 }
1903 
1904 void
1905 rfs4_mon_hold(void *arg)
1906 {
1907 	rfs4_file_t *fp = arg;
1908 
1909 	rfs4_dbe_hold(fp->dbe);
1910 }
1911 
1912 void
1913 rfs4_mon_rele(void *arg)
1914 {
1915 	rfs4_file_t *fp = arg;
1916 
1917 	rfs4_dbe_rele_nolock(fp->dbe);
1918 }
1919