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