1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
25 */
26
27 /*
28 * Copyright 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T.
29 * All rights reserved.
30 */
31
32
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <sys/vfs.h>
37 #include <sys/vnode.h>
38 #include <sys/socket.h>
39 #include <sys/errno.h>
40 #include <sys/uio.h>
41 #include <sys/proc.h>
42 #include <sys/user.h>
43 #include <sys/file.h>
44 #include <sys/tiuser.h>
45 #include <sys/kmem.h>
46 #include <sys/pathname.h>
47 #include <sys/debug.h>
48 #include <sys/vtrace.h>
49 #include <sys/cmn_err.h>
50 #include <sys/acl.h>
51 #include <sys/utsname.h>
52 #include <sys/sdt.h>
53 #include <netinet/in.h>
54 #include <sys/avl.h>
55
56 #include <rpc/types.h>
57 #include <rpc/auth.h>
58 #include <rpc/svc.h>
59
60 #include <nfs/nfs.h>
61 #include <nfs/export.h>
62 #include <nfs/nfssys.h>
63 #include <nfs/nfs_clnt.h>
64 #include <nfs/nfs_acl.h>
65 #include <nfs/nfs_log.h>
66 #include <nfs/lm.h>
67 #include <sys/sunddi.h>
68 #include <sys/pkp_hash.h>
69
70 treenode_t *ns_root;
71
72 struct exportinfo *exptable_path_hash[PKP_HASH_SIZE];
73 struct exportinfo *exptable[EXPTABLESIZE];
74
75 static int unexport(exportinfo_t *);
76 static void exportfree(exportinfo_t *);
77 static int loadindex(exportdata_t *);
78
79 extern void nfsauth_cache_free(exportinfo_t *);
80 extern int sec_svc_loadrootnames(int, int, caddr_t **, model_t);
81 extern void sec_svc_freerootnames(int, int, caddr_t *);
82
83 static int build_seclist_nodups(exportdata_t *, secinfo_t *, int);
84 static void srv_secinfo_add(secinfo_t **, int *, secinfo_t *, int, int);
85 static void srv_secinfo_remove(secinfo_t **, int *, secinfo_t *, int);
86 static void srv_secinfo_treeclimb(exportinfo_t *, secinfo_t *, int, bool_t);
87
88 #ifdef VOLATILE_FH_TEST
89 static struct ex_vol_rename *find_volrnm_fh(exportinfo_t *, nfs_fh4 *);
90 static uint32_t find_volrnm_fh_id(exportinfo_t *, nfs_fh4 *);
91 static void free_volrnm_list(exportinfo_t *);
92 #endif /* VOLATILE_FH_TEST */
93
94 /*
95 * exported_lock Read/Write lock that protects the exportinfo list.
96 * This lock must be held when searching or modifiying
97 * the exportinfo list.
98 */
99 krwlock_t exported_lock;
100
101 /*
102 * "public" and default (root) location for public filehandle
103 */
104 struct exportinfo *exi_public, *exi_root;
105
106 fid_t exi_rootfid; /* for checking the default public file handle */
107
108 fhandle_t nullfh2; /* for comparing V2 filehandles */
109
110 /*
111 * macro for static dtrace probes to trace server namespace ref count mods.
112 */
113 #define SECREF_TRACE(seclist, tag, flav, aftcnt) \
114 DTRACE_PROBE4(nfss__i__nmspc__secref, struct secinfo *, (seclist), \
115 char *, (tag), int, (int)(flav), int, (int)(aftcnt))
116
117
118 #define exptablehash(fsid, fid) (nfs_fhhash((fsid), (fid)) & (EXPTABLESIZE - 1))
119
120 static uint8_t
121 xor_hash(uint8_t *data, int len)
122 {
123 uint8_t h = 0;
124
125 while (len--)
126 h ^= *data++;
127
128 return (h);
129 }
130
131 /*
132 * File handle hash function, XOR over all bytes in fsid and fid.
133 */
134 static unsigned
135 nfs_fhhash(fsid_t *fsid, fid_t *fid)
136 {
137 int len;
138 uint8_t h;
139
140 h = xor_hash((uint8_t *)fsid, sizeof (fsid_t));
141
142 /*
143 * Sanity check the length before using it
144 * blindly in case the client trashed it.
145 */
146 len = fid->fid_len > NFS_FH4MAXDATA ? 0 : fid->fid_len;
147 h ^= xor_hash((uint8_t *)fid->fid_data, len);
148
149 return ((unsigned)h);
150 }
151
152 /*
153 * Free the memory allocated within a secinfo entry.
154 */
155 void
156 srv_secinfo_entry_free(struct secinfo *secp)
157 {
158 if (secp->s_rootcnt > 0 && secp->s_rootnames != NULL) {
159 sec_svc_freerootnames(secp->s_secinfo.sc_rpcnum,
160 secp->s_rootcnt, secp->s_rootnames);
161 secp->s_rootcnt = 0;
162 }
163
164 if ((secp->s_secinfo.sc_rpcnum == RPCSEC_GSS) &&
165 (secp->s_secinfo.sc_gss_mech_type)) {
166 kmem_free(secp->s_secinfo.sc_gss_mech_type->elements,
167 secp->s_secinfo.sc_gss_mech_type->length);
168 kmem_free(secp->s_secinfo.sc_gss_mech_type,
169 sizeof (rpc_gss_OID_desc));
170 secp->s_secinfo.sc_gss_mech_type = NULL;
171 }
172 }
173
174 /*
175 * Free a list of secinfo allocated in the exportdata structure.
176 */
177 void
178 srv_secinfo_list_free(struct secinfo *secinfo, int cnt)
179 {
180 int i;
181
182 if (cnt == 0)
183 return;
184
185 for (i = 0; i < cnt; i++)
186 srv_secinfo_entry_free(&secinfo[i]);
187
188 kmem_free(secinfo, cnt * sizeof (struct secinfo));
189 }
190
191 /*
192 * Allocate and copy a secinfo data from "from" to "to".
193 *
194 * This routine is used by srv_secinfo_add() to add a new flavor to an
195 * ancestor's export node. The rootnames are not copied because the
196 * allowable rootname access only applies to the explicit exported node,
197 * not its ancestor's.
198 *
199 * "to" should have already been allocated and zeroed before calling
200 * this routine.
201 *
202 * This routine is used under the protection of exported_lock (RW_WRITER).
203 */
204 void
205 srv_secinfo_copy(struct secinfo *from, struct secinfo *to)
206 {
207 to->s_secinfo.sc_nfsnum = from->s_secinfo.sc_nfsnum;
208 to->s_secinfo.sc_rpcnum = from->s_secinfo.sc_rpcnum;
209
210 if (from->s_secinfo.sc_rpcnum == RPCSEC_GSS) {
211 to->s_secinfo.sc_service = from->s_secinfo.sc_service;
212 bcopy(from->s_secinfo.sc_name, to->s_secinfo.sc_name,
213 strlen(from->s_secinfo.sc_name));
214 bcopy(from->s_secinfo.sc_gss_mech, to->s_secinfo.sc_gss_mech,
215 strlen(from->s_secinfo.sc_gss_mech));
216
217 /* copy mechanism oid */
218 to->s_secinfo.sc_gss_mech_type =
219 kmem_alloc(sizeof (rpc_gss_OID_desc), KM_SLEEP);
220 to->s_secinfo.sc_gss_mech_type->length =
221 from->s_secinfo.sc_gss_mech_type->length;
222 to->s_secinfo.sc_gss_mech_type->elements =
223 kmem_alloc(from->s_secinfo.sc_gss_mech_type->length,
224 KM_SLEEP);
225 bcopy(from->s_secinfo.sc_gss_mech_type->elements,
226 to->s_secinfo.sc_gss_mech_type->elements,
227 from->s_secinfo.sc_gss_mech_type->length);
228 }
229
230 to->s_refcnt = from->s_refcnt;
231 to->s_window = from->s_window;
232 /* no need to copy the mode bits - s_flags */
233 }
234
235 /*
236 * Create a secinfo array without duplicates. The condensed
237 * flavor list is used to propagate flavor ref counts to an
238 * export's ancestor pseudonodes.
239 */
240 static int
241 build_seclist_nodups(exportdata_t *exd, secinfo_t *nodups, int exponly)
242 {
243 int ccnt, c;
244 int ncnt, n;
245 struct secinfo *cursec;
246
247 ncnt = 0;
248 ccnt = exd->ex_seccnt;
249 cursec = exd->ex_secinfo;
250
251 for (c = 0; c < ccnt; c++) {
252
253 if (exponly && ! SEC_REF_EXPORTED(&cursec[c]))
254 continue;
255
256 for (n = 0; n < ncnt; n++) {
257 if (nodups[n].s_secinfo.sc_nfsnum ==
258 cursec[c].s_secinfo.sc_nfsnum)
259 break;
260 }
261
262 /*
263 * The structure copy below also copys ptrs embedded
264 * within struct secinfo. The ptrs are copied but
265 * they are never freed from the nodups array. If
266 * an ancestor's secinfo array doesn't contain one
267 * of the nodups flavors, then the entry is properly
268 * copied into the ancestor's secinfo array.
269 * (see srv_secinfo_copy)
270 */
271 if (n == ncnt) {
272 nodups[n] = cursec[c];
273 ncnt++;
274 }
275 }
276 return (ncnt);
277 }
278
279 /*
280 * Add the new security flavors from newdata to the current list, pcursec.
281 * Upon return, *pcursec has the newly merged secinfo list.
282 *
283 * There should be at least 1 secinfo entry in newsec.
284 *
285 * This routine is used under the protection of exported_lock (RW_WRITER).
286 */
287 static void
288 srv_secinfo_add(secinfo_t **pcursec, int *pcurcnt, secinfo_t *newsec,
289 int newcnt, int is_pseudo)
290 {
291 int ccnt, c; /* sec count in current data - curdata */
292 int n; /* index for newsec - newsecinfo */
293 int tcnt; /* total sec count after merge */
294 int mcnt; /* total sec count after merge */
295 struct secinfo *msec; /* merged secinfo list */
296 struct secinfo *cursec;
297
298 cursec = *pcursec;
299 ccnt = *pcurcnt;
300
301 ASSERT(newcnt > 0);
302 tcnt = ccnt + newcnt;
303
304 for (n = 0; n < newcnt; n++) {
305 for (c = 0; c < ccnt; c++) {
306 if (newsec[n].s_secinfo.sc_nfsnum ==
307 cursec[c].s_secinfo.sc_nfsnum) {
308 cursec[c].s_refcnt += newsec[n].s_refcnt;
309 SECREF_TRACE(cursec, "add_ref",
310 cursec[c].s_secinfo.sc_nfsnum,
311 cursec[c].s_refcnt);
312 tcnt--;
313 break;
314 }
315 }
316 }
317
318 if (tcnt == ccnt)
319 return; /* no change; no new flavors */
320
321 msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
322
323 /* move current secinfo list data to the new list */
324 for (c = 0; c < ccnt; c++)
325 msec[c] = cursec[c];
326
327 /* Add the flavor that's not in the current data */
328 mcnt = ccnt;
329 for (n = 0; n < newcnt; n++) {
330 for (c = 0; c < ccnt; c++) {
331 if (newsec[n].s_secinfo.sc_nfsnum ==
332 cursec[c].s_secinfo.sc_nfsnum)
333 break;
334 }
335
336 /* This is the one. Add it. */
337 if (c == ccnt) {
338 srv_secinfo_copy(&newsec[n], &msec[mcnt]);
339
340 if (is_pseudo)
341 msec[mcnt].s_flags = M_RO;
342
343 SECREF_TRACE(msec, "new_ref",
344 msec[mcnt].s_secinfo.sc_nfsnum,
345 msec[mcnt].s_refcnt);
346 mcnt++;
347 }
348 }
349
350 ASSERT(mcnt == tcnt);
351
352 /*
353 * Done. Update curdata. Free the old secinfo list in
354 * curdata and return the new sec array info
355 */
356 if (ccnt > 0)
357 kmem_free(cursec, ccnt * sizeof (struct secinfo));
358 *pcurcnt = tcnt;
359 *pcursec = msec;
360 }
361
362 /*
363 * For NFS V4.
364 * Remove the security data of the unexported node from its ancestors.
365 * Assume there is at least one flavor entry in the current sec list
366 * (pcursec).
367 *
368 * This routine is used under the protection of exported_lock (RW_WRITER).
369 *
370 * Every element of remsec is an explicitly exported flavor. If
371 * srv_secinfo_remove() is called fom an exportfs error path, then
372 * the flavor list was derived from the user's share cmdline,
373 * and all flavors are explicit. If it was called from the unshare path,
374 * build_seclist_nodups() was called with the exponly flag.
375 */
376 static void
377 srv_secinfo_remove(secinfo_t **pcursec, int *pcurcnt, secinfo_t *remsec,
378 int remcnt)
379 {
380 int ccnt, c; /* sec count in current data - cursec */
381 int r; /* sec count in removal data - remsec */
382 int tcnt, mcnt; /* total sec count after removing */
383 struct secinfo *msec; /* final secinfo list after removing */
384 struct secinfo *cursec;
385
386 cursec = *pcursec;
387 ccnt = *pcurcnt;
388 tcnt = ccnt;
389
390 for (r = 0; r < remcnt; r++) {
391 /*
392 * At unshare/reshare time, only explicitly shared flavor ref
393 * counts are decremented and propagated to ancestors.
394 * Implicit flavor refs came from shared descendants, and
395 * they must be kept.
396 */
397 if (! SEC_REF_EXPORTED(&remsec[r]))
398 continue;
399
400 for (c = 0; c < ccnt; c++) {
401 if (remsec[r].s_secinfo.sc_nfsnum ==
402 cursec[c].s_secinfo.sc_nfsnum) {
403
404 /*
405 * Decrement secinfo reference count by 1.
406 * If this entry is invalid after decrementing
407 * the count (i.e. count < 1), this entry will
408 * be removed.
409 */
410 cursec[c].s_refcnt--;
411
412 SECREF_TRACE(cursec, "del_ref",
413 cursec[c].s_secinfo.sc_nfsnum,
414 cursec[c].s_refcnt);
415
416 ASSERT(cursec[c].s_refcnt >= 0);
417
418 if (SEC_REF_INVALID(&cursec[c]))
419 tcnt--;
420 break;
421 }
422 }
423 }
424
425 ASSERT(tcnt >= 0);
426 if (tcnt == ccnt)
427 return; /* no change; no flavors to remove */
428
429 if (tcnt == 0) {
430 srv_secinfo_list_free(cursec, ccnt);
431 *pcurcnt = 0;
432 *pcursec = NULL;
433 return;
434 }
435
436 msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
437
438 /* walk thru the given secinfo list to remove the flavors */
439 mcnt = 0;
440 for (c = 0; c < ccnt; c++) {
441 if (SEC_REF_INVALID(&cursec[c])) {
442 srv_secinfo_entry_free(&cursec[c]);
443 } else {
444 msec[mcnt] = cursec[c];
445 mcnt++;
446 }
447 }
448
449 ASSERT(mcnt == tcnt);
450 /*
451 * Done. Update curdata.
452 * Free the existing secinfo list in curdata. All pointers
453 * within the list have either been moved to msec or freed
454 * if it's invalid.
455 */
456 kmem_free(*pcursec, ccnt * sizeof (struct secinfo));
457 *pcursec = msec;
458 *pcurcnt = tcnt;
459 }
460
461
462 /*
463 * For the reshare case, sec flavor accounting happens in 3 steps:
464 * 1) propagate addition of new flavor refs up the ancestor tree
465 * 2) transfer flavor refs of descendants to new/reshared exportdata
466 * 3) propagate removal of old flavor refs up the ancestor tree
467 *
468 * srv_secinfo_exp2exp() implements step 2 of a reshare. At this point,
469 * the new flavor list has already been propagated up through the
470 * ancestor tree via srv_secinfo_treeclimb().
471 *
472 * If there is more than 1 export reference to an old flavor (i.e. some
473 * of its children shared with this flavor), this flavor information
474 * needs to be transferred to the new exportdata struct. A flavor in
475 * the old exportdata has descendant refs when its s_refcnt > 1 or it
476 * is implicitly shared (M_SEC4_EXPORTED not set in s_flags).
477 *
478 * SEC_REF_EXPORTED() is only true when M_SEC4_EXPORTED is set
479 * SEC_REF_SELF() is only true when both M_SEC4_EXPORTED is set and s_refcnt==1
480 *
481 * Transferring descendant flavor refcnts happens in 2 passes:
482 * a) flavors used before (oldsecinfo) and after (curdata->ex_secinfo) reshare
483 * b) flavors used before but not after reshare
484 *
485 * This routine is used under the protection of exported_lock (RW_WRITER).
486 */
487 void
488 srv_secinfo_exp2exp(exportdata_t *curdata, secinfo_t *oldsecinfo, int ocnt)
489 {
490 int ccnt, c; /* sec count in current data - curdata */
491 int o; /* sec count in old data - oldsecinfo */
492 int tcnt, mcnt; /* total sec count after the transfer */
493 struct secinfo *msec; /* merged secinfo list */
494
495 ccnt = curdata->ex_seccnt;
496
497 ASSERT(ocnt > 0);
498 ASSERT(!(curdata->ex_flags & EX_PSEUDO));
499
500 /*
501 * If the oldsecinfo has flavors with more than 1 reference count
502 * and the flavor is specified in the reshare, transfer the flavor
503 * refs to the new seclist (curdata.ex_secinfo).
504 */
505 tcnt = ccnt + ocnt;
506
507 for (o = 0; o < ocnt; o++) {
508
509 if (SEC_REF_SELF(&oldsecinfo[o])) {
510 tcnt--;
511 continue;
512 }
513
514 for (c = 0; c < ccnt; c++) {
515 if (oldsecinfo[o].s_secinfo.sc_nfsnum ==
516 curdata->ex_secinfo[c].s_secinfo.sc_nfsnum) {
517
518 /*
519 * add old reference to the current
520 * secinfo count
521 */
522 curdata->ex_secinfo[c].s_refcnt +=
523 oldsecinfo[o].s_refcnt;
524
525 /*
526 * Delete the old export flavor
527 * reference. The initial reference
528 * was created during srv_secinfo_add,
529 * and the count is decremented below
530 * to account for the initial reference.
531 */
532 if (SEC_REF_EXPORTED(&oldsecinfo[o]))
533 curdata->ex_secinfo[c].s_refcnt--;
534
535 SECREF_TRACE(curdata->ex_path,
536 "reshare_xfer_common_child_refs",
537 curdata->ex_secinfo[c].s_secinfo.sc_nfsnum,
538 curdata->ex_secinfo[c].s_refcnt);
539
540 ASSERT(curdata->ex_secinfo[c].s_refcnt >= 0);
541
542 tcnt--;
543 break;
544 }
545 }
546 }
547
548 if (tcnt == ccnt)
549 return; /* no more transfer to do */
550
551 /*
552 * oldsecinfo has flavors referenced by its children that are not
553 * in the current (new) export flavor list. Add these flavors.
554 */
555 msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
556
557 /* move current secinfo list data to the new list */
558 for (c = 0; c < ccnt; c++)
559 msec[c] = curdata->ex_secinfo[c];
560
561 /*
562 * Add the flavor that's not in the new export, but still
563 * referenced by its children.
564 */
565 mcnt = ccnt;
566 for (o = 0; o < ocnt; o++) {
567 if (! SEC_REF_SELF(&oldsecinfo[o])) {
568 for (c = 0; c < ccnt; c++) {
569 if (oldsecinfo[o].s_secinfo.sc_nfsnum ==
570 curdata->ex_secinfo[c].s_secinfo.sc_nfsnum)
571 break;
572 }
573
574 /*
575 * This is the one. Add it. Decrement the ref count
576 * by 1 if the flavor is an explicitly shared flavor
577 * for the oldsecinfo export node.
578 */
579 if (c == ccnt) {
580 srv_secinfo_copy(&oldsecinfo[o], &msec[mcnt]);
581 if (SEC_REF_EXPORTED(&oldsecinfo[o]))
582 msec[mcnt].s_refcnt--;
583
584 SECREF_TRACE(curdata,
585 "reshare_xfer_implicit_child_refs",
586 msec[mcnt].s_secinfo.sc_nfsnum,
587 msec[mcnt].s_refcnt);
588
589 ASSERT(msec[mcnt].s_refcnt >= 0);
590 mcnt++;
591 }
592 }
593 }
594
595 ASSERT(mcnt == tcnt);
596 /*
597 * Done. Update curdata, free the existing secinfo list in
598 * curdata and set the new value.
599 */
600 if (ccnt > 0)
601 kmem_free(curdata->ex_secinfo, ccnt * sizeof (struct secinfo));
602 curdata->ex_seccnt = tcnt;
603 curdata->ex_secinfo = msec;
604 }
605
606 /*
607 * When unsharing an old export node and the old node becomes a pseudo node,
608 * if there is more than 1 export reference to an old flavor (i.e. some of
609 * its children shared with this flavor), this flavor information needs to
610 * be transferred to the new shared node.
611 *
612 * This routine is used under the protection of exported_lock (RW_WRITER).
613 */
614 void
615 srv_secinfo_exp2pseu(exportdata_t *curdata, exportdata_t *olddata)
616 {
617 int ocnt, o; /* sec count in transfer data - trandata */
618 int tcnt, mcnt; /* total sec count after transfer */
619 struct secinfo *msec; /* merged secinfo list */
620
621 ASSERT(curdata->ex_flags & EX_PSEUDO);
622 ASSERT(curdata->ex_seccnt == 0);
623
624 ocnt = olddata->ex_seccnt;
625
626 /*
627 * If the olddata has flavors with more than 1 reference count,
628 * transfer the information to the curdata.
629 */
630 tcnt = ocnt;
631
632 for (o = 0; o < ocnt; o++) {
633 if (SEC_REF_SELF(&olddata->ex_secinfo[o]))
634 tcnt--;
635 }
636
637 if (tcnt == 0)
638 return; /* no transfer to do */
639
640 msec = kmem_zalloc(tcnt * sizeof (struct secinfo), KM_SLEEP);
641
642 mcnt = 0;
643 for (o = 0; o < ocnt; o++) {
644 if (! SEC_REF_SELF(&olddata->ex_secinfo[o])) {
645
646 /*
647 * Decrement the reference count by 1 if the flavor is
648 * an explicitly shared flavor for the olddata export
649 * node.
650 */
651 srv_secinfo_copy(&olddata->ex_secinfo[o], &msec[mcnt]);
652 msec[mcnt].s_flags = M_RO;
653 if (SEC_REF_EXPORTED(&olddata->ex_secinfo[o]))
654 msec[mcnt].s_refcnt--;
655
656 SECREF_TRACE(curdata, "unshare_morph_pseudo",
657 msec[mcnt].s_secinfo.sc_nfsnum,
658 msec[mcnt].s_refcnt);
659
660 ASSERT(msec[mcnt].s_refcnt >= 0);
661 mcnt++;
662 }
663 }
664
665 ASSERT(mcnt == tcnt);
666 /*
667 * Done. Update curdata.
668 * Free up the existing secinfo list in curdata and
669 * set the new value.
670 */
671 curdata->ex_seccnt = tcnt;
672 curdata->ex_secinfo = msec;
673 }
674
675 /*
676 * Find for given treenode the exportinfo which has its
677 * exp_visible linked on its exi_visible list.
678 *
679 * Note: We could add new pointer either to treenode or
680 * to exp_visible, which will point there directly.
681 * This would buy some speed for some memory.
682 */
683 exportinfo_t *
684 vis2exi(treenode_t *tnode)
685 {
686 exportinfo_t *exi_ret = NULL;
687
688 for (;;) {
689 tnode = tnode->tree_parent;
690 if (TREE_ROOT(tnode)) {
691 exi_ret = tnode->tree_exi;
692 break;
693 }
694 }
695
696 ASSERT(exi_ret); /* Every visible should have its home exportinfo */
697 return (exi_ret);
698 }
699
700 /*
701 * For NFS V4.
702 * Add or remove the newly exported or unexported security flavors of the
703 * given exportinfo from its ancestors upto the system root.
704 */
705 void
706 srv_secinfo_treeclimb(exportinfo_t *exip, secinfo_t *sec, int seccnt,
707 bool_t isadd)
708 {
709 treenode_t *tnode = exip->exi_tree;
710
711 ASSERT(RW_WRITE_HELD(&exported_lock));
712 ASSERT(tnode != NULL);
713
714 if (seccnt == 0)
715 return;
716
717 /*
718 * If flavors are being added and the new export root isn't
719 * also VROOT, its implicitly allowed flavors are inherited from
720 * its pseudonode.
721 * Note - for VROOT exports the implicitly allowed flavors were
722 * transferred from the PSEUDO export in exportfs()
723 */
724 if (isadd && !(exip->exi_vp->v_flag & VROOT) &&
725 tnode->tree_vis->vis_seccnt > 0) {
726 srv_secinfo_add(&exip->exi_export.ex_secinfo,
727 &exip->exi_export.ex_seccnt, tnode->tree_vis->vis_secinfo,
728 tnode->tree_vis->vis_seccnt, FALSE);
729 }
730
731 /*
732 * Move to parent node and propagate sec flavor
733 * to exportinfo and to visible structures.
734 */
735 tnode = tnode->tree_parent;
736
737 while (tnode != NULL) {
738
739 /* If there is exportinfo, update it */
740 if (tnode->tree_exi != NULL) {
741 secinfo_t **pxsec =
742 &tnode->tree_exi->exi_export.ex_secinfo;
743 int *pxcnt = &tnode->tree_exi->exi_export.ex_seccnt;
744 int is_pseudo = PSEUDO(tnode->tree_exi);
745 if (isadd)
746 srv_secinfo_add(pxsec, pxcnt, sec, seccnt,
747 is_pseudo);
748 else
749 srv_secinfo_remove(pxsec, pxcnt, sec, seccnt);
750 }
751
752 /* Update every visible - only root node has no visible */
753 if (tnode->tree_vis != NULL) {
754 secinfo_t **pxsec = &tnode->tree_vis->vis_secinfo;
755 int *pxcnt = &tnode->tree_vis->vis_seccnt;
756 if (isadd)
757 srv_secinfo_add(pxsec, pxcnt, sec, seccnt,
758 FALSE);
759 else
760 srv_secinfo_remove(pxsec, pxcnt, sec, seccnt);
761 }
762 tnode = tnode->tree_parent;
763 }
764 }
765
766 /* hash_name is a text substitution for either fid_hash or path_hash */
767 #define exp_hash_unlink(exi, hash_name) \
768 if (*(exi)->hash_name.bckt == (exi)) \
769 *(exi)->hash_name.bckt = (exi)->hash_name.next; \
770 if ((exi)->hash_name.prev) \
771 (exi)->hash_name.prev->hash_name.next = (exi)->hash_name.next; \
772 if ((exi)->hash_name.next) \
773 (exi)->hash_name.next->hash_name.prev = (exi)->hash_name.prev; \
774 (exi)->hash_name.bckt = NULL;
775
776 #define exp_hash_link(exi, hash_name, bucket) \
777 (exi)->hash_name.bckt = (bucket); \
778 (exi)->hash_name.prev = NULL; \
779 (exi)->hash_name.next = *(bucket); \
780 if ((exi)->hash_name.next) \
781 (exi)->hash_name.next->hash_name.prev = (exi); \
782 *(bucket) = (exi);
783
784 void
785 export_link(exportinfo_t *exi)
786 {
787 exportinfo_t **bckt;
788
789 bckt = &exptable[exptablehash(&exi->exi_fsid, &exi->exi_fid)];
790 exp_hash_link(exi, fid_hash, bckt);
791
792 bckt = &exptable_path_hash[pkp_tab_hash(exi->exi_export.ex_path,
793 strlen(exi->exi_export.ex_path))];
794 exp_hash_link(exi, path_hash, bckt);
795 }
796
797 /*
798 * Initialization routine for export routines. Should only be called once.
799 */
800 int
801 nfs_exportinit(void)
802 {
803 int error;
804 int i;
805
806 rw_init(&exported_lock, NULL, RW_DEFAULT, NULL);
807
808 /*
809 * Allocate the place holder for the public file handle, which
810 * is all zeroes. It is initially set to the root filesystem.
811 */
812 exi_root = kmem_zalloc(sizeof (*exi_root), KM_SLEEP);
813 exi_public = exi_root;
814
815 exi_root->exi_export.ex_flags = EX_PUBLIC;
816 exi_root->exi_export.ex_pathlen = 1; /* length of "/" */
817 exi_root->exi_export.ex_path =
818 kmem_alloc(exi_root->exi_export.ex_pathlen + 1, KM_SLEEP);
819 exi_root->exi_export.ex_path[0] = '/';
820 exi_root->exi_export.ex_path[1] = '\0';
821
822 exi_root->exi_count = 1;
823 mutex_init(&exi_root->exi_lock, NULL, MUTEX_DEFAULT, NULL);
824
825 exi_root->exi_vp = rootdir;
826 exi_rootfid.fid_len = MAXFIDSZ;
827 error = vop_fid_pseudo(exi_root->exi_vp, &exi_rootfid);
828 if (error) {
829 mutex_destroy(&exi_root->exi_lock);
830 kmem_free(exi_root, sizeof (*exi_root));
831 return (error);
832 }
833
834 /*
835 * Initialize auth cache and auth cache lock
836 */
837 for (i = 0; i < AUTH_TABLESIZE; i++) {
838 exi_root->exi_cache[i] = kmem_alloc(sizeof (avl_tree_t),
839 KM_SLEEP);
840 avl_create(exi_root->exi_cache[i], nfsauth_cache_clnt_compar,
841 sizeof (struct auth_cache_clnt),
842 offsetof(struct auth_cache_clnt, authc_link));
843 }
844 rw_init(&exi_root->exi_cache_lock, NULL, RW_DEFAULT, NULL);
845
846 /* setup the fhandle template */
847 exi_root->exi_fh.fh_fsid = rootdir->v_vfsp->vfs_fsid;
848 exi_root->exi_fh.fh_xlen = exi_rootfid.fid_len;
849 bcopy(exi_rootfid.fid_data, exi_root->exi_fh.fh_xdata,
850 exi_rootfid.fid_len);
851 exi_root->exi_fh.fh_len = sizeof (exi_root->exi_fh.fh_data);
852
853 /*
854 * Publish the exportinfo in the hash table
855 */
856 export_link(exi_root);
857
858 nfslog_init();
859 ns_root = NULL;
860
861 return (0);
862 }
863
864 /*
865 * Finalization routine for export routines. Called to cleanup previously
866 * initialization work when the NFS server module could not be loaded correctly.
867 */
868 void
869 nfs_exportfini(void)
870 {
871 int i;
872
873 /*
874 * Deallocate the place holder for the public file handle.
875 */
876 srv_secinfo_list_free(exi_root->exi_export.ex_secinfo,
877 exi_root->exi_export.ex_seccnt);
878 mutex_destroy(&exi_root->exi_lock);
879 rw_destroy(&exi_root->exi_cache_lock);
880 for (i = 0; i < AUTH_TABLESIZE; i++) {
881 avl_destroy(exi_root->exi_cache[i]);
882 kmem_free(exi_root->exi_cache[i], sizeof (avl_tree_t));
883 }
884 kmem_free(exi_root, sizeof (*exi_root));
885
886 rw_destroy(&exported_lock);
887 }
888
889 /*
890 * Check if 2 gss mechanism identifiers are the same.
891 *
892 * return FALSE if not the same.
893 * return TRUE if the same.
894 */
895 static bool_t
896 nfs_mech_equal(rpc_gss_OID mech1, rpc_gss_OID mech2)
897 {
898 if ((mech1->length == 0) && (mech2->length == 0))
899 return (TRUE);
900
901 if (mech1->length != mech2->length)
902 return (FALSE);
903
904 return (bcmp(mech1->elements, mech2->elements, mech1->length) == 0);
905 }
906
907 /*
908 * This routine is used by rpc to map rpc security number
909 * to nfs specific security flavor number.
910 *
911 * The gss callback prototype is
912 * callback(struct svc_req *, gss_cred_id_t *, gss_ctx_id_t *,
913 * rpc_gss_lock_t *, void **),
914 * since nfs does not use the gss_cred_id_t/gss_ctx_id_t arguments
915 * we cast them to void.
916 */
917 /*ARGSUSED*/
918 bool_t
919 rfs_gsscallback(struct svc_req *req, gss_cred_id_t deleg, void *gss_context,
920 rpc_gss_lock_t *lock, void **cookie)
921 {
922 int i, j;
923 rpc_gss_rawcred_t *raw_cred;
924 struct exportinfo *exi;
925
926 /*
927 * We don't deal with delegated credentials.
928 */
929 if (deleg != GSS_C_NO_CREDENTIAL)
930 return (FALSE);
931
932 raw_cred = lock->raw_cred;
933 *cookie = NULL;
934
935 rw_enter(&exported_lock, RW_READER);
936 for (i = 0; i < EXPTABLESIZE; i++) {
937 exi = exptable[i];
938 while (exi) {
939 if (exi->exi_export.ex_seccnt > 0) {
940 struct secinfo *secp;
941 seconfig_t *se;
942 int seccnt;
943
944 secp = exi->exi_export.ex_secinfo;
945 seccnt = exi->exi_export.ex_seccnt;
946 for (j = 0; j < seccnt; j++) {
947 /*
948 * If there is a map of the triplet
949 * (mechanism, service, qop) between
950 * raw_cred and the exported flavor,
951 * get the psudo flavor number.
952 * Also qop should not be NULL, it
953 * should be "default" or something
954 * else.
955 */
956 se = &secp[j].s_secinfo;
957 if ((se->sc_rpcnum == RPCSEC_GSS) &&
958
959 (nfs_mech_equal(
960 se->sc_gss_mech_type,
961 raw_cred->mechanism)) &&
962
963 (se->sc_service ==
964 raw_cred->service) &&
965 (raw_cred->qop == se->sc_qop)) {
966
967 *cookie = (void *)(uintptr_t)
968 se->sc_nfsnum;
969 goto done;
970 }
971 }
972 }
973 exi = exi->fid_hash.next;
974 }
975 }
976 done:
977 rw_exit(&exported_lock);
978
979 /*
980 * If no nfs pseudo number mapping can be found in the export
981 * table, assign the nfsflavor to NFS_FLAVOR_NOMAP. In V4, we may
982 * recover the flavor mismatch from NFS layer (NFS4ERR_WRONGSEC).
983 *
984 * For example:
985 * server first shares with krb5i;
986 * client mounts with krb5i;
987 * server re-shares with krb5p;
988 * client tries with krb5i, but no mapping can be found;
989 * rpcsec_gss module calls this routine to do the mapping,
990 * if this routine fails, request is rejected from
991 * the rpc layer.
992 * What we need is to let the nfs layer rejects the request.
993 * For V4, we can reject with NFS4ERR_WRONGSEC and the client
994 * may recover from it by getting the new flavor via SECINFO.
995 *
996 * nfs pseudo number for RPCSEC_GSS mapping (see nfssec.conf)
997 * is owned by IANA (see RFC 2623).
998 *
999 * XXX NFS_FLAVOR_NOMAP is defined in Solaris to work around
1000 * the implementation issue. This number should not overlap with
1001 * any new IANA defined pseudo flavor numbers.
1002 */
1003 if (*cookie == NULL)
1004 *cookie = (void *)NFS_FLAVOR_NOMAP;
1005
1006 lock->locked = TRUE;
1007
1008 return (TRUE);
1009 }
1010
1011
1012 /*
1013 * Exportfs system call; credentials should be checked before
1014 * calling this function.
1015 */
1016 int
1017 exportfs(struct exportfs_args *args, model_t model, cred_t *cr)
1018 {
1019 vnode_t *vp;
1020 vnode_t *dvp;
1021 struct exportdata *kex;
1022 struct exportinfo *exi = NULL;
1023 struct exportinfo *ex, *ex1, *ex2;
1024 fid_t fid;
1025 fsid_t fsid;
1026 int error;
1027 size_t allocsize;
1028 struct secinfo *sp;
1029 struct secinfo *exs;
1030 rpc_gss_callback_t cb;
1031 char *pathbuf;
1032 char *log_buffer;
1033 char *tagbuf;
1034 int callback;
1035 int allocd_seccnt;
1036 STRUCT_HANDLE(exportfs_args, uap);
1037 STRUCT_DECL(exportdata, uexi);
1038 struct secinfo newsec[MAX_FLAVORS];
1039 int newcnt;
1040 struct secinfo oldsec[MAX_FLAVORS];
1041 int oldcnt;
1042 int i;
1043 struct pathname lookpn;
1044
1045 STRUCT_SET_HANDLE(uap, model, args);
1046
1047 /* Read in pathname from userspace */
1048 if (error = pn_get(STRUCT_FGETP(uap, dname), UIO_USERSPACE, &lookpn))
1049 return (error);
1050
1051 /* Walk the export list looking for that pathname */
1052 rw_enter(&exported_lock, RW_READER);
1053 DTRACE_PROBE(nfss__i__exported_lock1_start);
1054 for (ex1 = exptable_path_hash[pkp_tab_hash(lookpn.pn_path,
1055 strlen(lookpn.pn_path))]; ex1; ex1 = ex1->path_hash.next) {
1056 if (ex1 != exi_root && 0 ==
1057 strcmp(ex1->exi_export.ex_path, lookpn.pn_path)) {
1058 exi_hold(ex1);
1059 break;
1060 }
1061 }
1062 DTRACE_PROBE(nfss__i__exported_lock1_stop);
1063 rw_exit(&exported_lock);
1064
1065 /* Is this an unshare? */
1066 if (STRUCT_FGETP(uap, uex) == NULL) {
1067 pn_free(&lookpn);
1068 if (ex1 == NULL)
1069 return (EINVAL);
1070 error = unexport(ex1);
1071 exi_rele(ex1);
1072 return (error);
1073 }
1074
1075 /* It is a share or a re-share */
1076 error = lookupname(STRUCT_FGETP(uap, dname), UIO_USERSPACE,
1077 FOLLOW, &dvp, &vp);
1078 if (error == EINVAL) {
1079 /*
1080 * if fname resolves to / we get EINVAL error
1081 * since we wanted the parent vnode. Try again
1082 * with NULL dvp.
1083 */
1084 error = lookupname(STRUCT_FGETP(uap, dname), UIO_USERSPACE,
1085 FOLLOW, NULL, &vp);
1086 dvp = NULL;
1087 }
1088 if (!error && vp == NULL) {
1089 /* Last component of fname not found */
1090 if (dvp != NULL)
1091 VN_RELE(dvp);
1092 error = ENOENT;
1093 }
1094 if (error) {
1095 pn_free(&lookpn);
1096 if (ex1)
1097 exi_rele(ex1);
1098 return (error);
1099 }
1100
1101 /*
1102 * 'vp' may be an AUTOFS node, so we perform a
1103 * VOP_ACCESS() to trigger the mount of the
1104 * intended filesystem, so we can share the intended
1105 * filesystem instead of the AUTOFS filesystem.
1106 */
1107 (void) VOP_ACCESS(vp, 0, 0, cr, NULL);
1108
1109 /*
1110 * We're interested in the top most filesystem.
1111 * This is specially important when uap->dname is a trigger
1112 * AUTOFS node, since we're really interested in sharing the
1113 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
1114 * call not the AUTOFS node itself.
1115 */
1116 if (vn_mountedvfs(vp) != NULL) {
1117 if (error = traverse(&vp)) {
1118 VN_RELE(vp);
1119 if (dvp != NULL)
1120 VN_RELE(dvp);
1121 pn_free(&lookpn);
1122 if (ex1)
1123 exi_rele(ex1);
1124 return (error);
1125 }
1126 }
1127
1128 /* Do not allow sharing another vnode for already shared path */
1129 if (ex1 && !PSEUDO(ex1) && !VN_CMP(ex1->exi_vp, vp)) {
1130 VN_RELE(vp);
1131 if (dvp != NULL)
1132 VN_RELE(dvp);
1133 pn_free(&lookpn);
1134 exi_rele(ex1);
1135 return (EEXIST);
1136 }
1137 if (ex1)
1138 exi_rele(ex1);
1139
1140 /*
1141 * Get the vfs id
1142 */
1143 bzero(&fid, sizeof (fid));
1144 fid.fid_len = MAXFIDSZ;
1145 error = VOP_FID(vp, &fid, NULL);
1146 fsid = vp->v_vfsp->vfs_fsid;
1147
1148 if (error) {
1149 VN_RELE(vp);
1150 if (dvp != NULL)
1151 VN_RELE(dvp);
1152 /*
1153 * If VOP_FID returns ENOSPC then the fid supplied
1154 * is too small. For now we simply return EREMOTE.
1155 */
1156 if (error == ENOSPC)
1157 error = EREMOTE;
1158 pn_free(&lookpn);
1159 return (error);
1160 }
1161
1162 /*
1163 * Do not allow re-sharing a shared vnode under a different path
1164 * PSEUDO export has ex_path fabricated, e.g. "/tmp (pseudo)", skip it.
1165 */
1166 rw_enter(&exported_lock, RW_READER);
1167 DTRACE_PROBE(nfss__i__exported_lock2_start);
1168 for (ex2 = exptable[exptablehash(&fsid, &fid)]; ex2;
1169 ex2 = ex2->fid_hash.next) {
1170 if (ex2 != exi_root && !PSEUDO(ex2) &&
1171 VN_CMP(ex2->exi_vp, vp) &&
1172 strcmp(ex2->exi_export.ex_path, lookpn.pn_path) != 0) {
1173 DTRACE_PROBE(nfss__i__exported_lock2_stop);
1174 rw_exit(&exported_lock);
1175 VN_RELE(vp);
1176 if (dvp != NULL)
1177 VN_RELE(dvp);
1178 pn_free(&lookpn);
1179 return (EEXIST);
1180 }
1181 }
1182 DTRACE_PROBE(nfss__i__exported_lock2_stop);
1183 rw_exit(&exported_lock);
1184 pn_free(&lookpn);
1185
1186 exi = kmem_zalloc(sizeof (*exi), KM_SLEEP);
1187 exi->exi_fsid = fsid;
1188 exi->exi_fid = fid;
1189 exi->exi_vp = vp;
1190 exi->exi_count = 1;
1191 exi->exi_volatile_dev = (vfssw[vp->v_vfsp->vfs_fstype].vsw_flag &
1192 VSW_VOLATILEDEV) ? 1 : 0;
1193 mutex_init(&exi->exi_lock, NULL, MUTEX_DEFAULT, NULL);
1194 exi->exi_dvp = dvp;
1195
1196 /*
1197 * Initialize auth cache and auth cache lock
1198 */
1199 for (i = 0; i < AUTH_TABLESIZE; i++) {
1200 exi->exi_cache[i] = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
1201 avl_create(exi->exi_cache[i], nfsauth_cache_clnt_compar,
1202 sizeof (struct auth_cache_clnt),
1203 offsetof(struct auth_cache_clnt, authc_link));
1204 }
1205 rw_init(&exi->exi_cache_lock, NULL, RW_DEFAULT, NULL);
1206
1207 /*
1208 * Build up the template fhandle
1209 */
1210 exi->exi_fh.fh_fsid = fsid;
1211 if (exi->exi_fid.fid_len > sizeof (exi->exi_fh.fh_xdata)) {
1212 error = EREMOTE;
1213 goto out1;
1214 }
1215 exi->exi_fh.fh_xlen = exi->exi_fid.fid_len;
1216 bcopy(exi->exi_fid.fid_data, exi->exi_fh.fh_xdata,
1217 exi->exi_fid.fid_len);
1218
1219 exi->exi_fh.fh_len = sizeof (exi->exi_fh.fh_data);
1220
1221 kex = &exi->exi_export;
1222
1223 /*
1224 * Load in everything, and do sanity checking
1225 */
1226 STRUCT_INIT(uexi, model);
1227 if (copyin(STRUCT_FGETP(uap, uex), STRUCT_BUF(uexi),
1228 STRUCT_SIZE(uexi))) {
1229 error = EFAULT;
1230 goto out1;
1231 }
1232
1233 kex->ex_version = STRUCT_FGET(uexi, ex_version);
1234 if (kex->ex_version != EX_CURRENT_VERSION) {
1235 error = EINVAL;
1236 cmn_err(CE_WARN,
1237 "NFS: exportfs requires export struct version 2 - got %d\n",
1238 kex->ex_version);
1239 goto out1;
1240 }
1241
1242 /*
1243 * Must have at least one security entry
1244 */
1245 kex->ex_seccnt = STRUCT_FGET(uexi, ex_seccnt);
1246 if (kex->ex_seccnt < 1) {
1247 error = EINVAL;
1248 goto out1;
1249 }
1250
1251 kex->ex_path = STRUCT_FGETP(uexi, ex_path);
1252 kex->ex_pathlen = STRUCT_FGET(uexi, ex_pathlen);
1253 kex->ex_flags = STRUCT_FGET(uexi, ex_flags);
1254 kex->ex_anon = STRUCT_FGET(uexi, ex_anon);
1255 kex->ex_secinfo = STRUCT_FGETP(uexi, ex_secinfo);
1256 kex->ex_index = STRUCT_FGETP(uexi, ex_index);
1257 kex->ex_log_buffer = STRUCT_FGETP(uexi, ex_log_buffer);
1258 kex->ex_log_bufferlen = STRUCT_FGET(uexi, ex_log_bufferlen);
1259 kex->ex_tag = STRUCT_FGETP(uexi, ex_tag);
1260 kex->ex_taglen = STRUCT_FGET(uexi, ex_taglen);
1261
1262 /*
1263 * Copy the exported pathname into
1264 * an appropriately sized buffer.
1265 */
1266 pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1267 if (copyinstr(kex->ex_path, pathbuf, MAXPATHLEN, &kex->ex_pathlen)) {
1268 kmem_free(pathbuf, MAXPATHLEN);
1269 error = EFAULT;
1270 goto out1;
1271 }
1272 kex->ex_path = kmem_alloc(kex->ex_pathlen + 1, KM_SLEEP);
1273 bcopy(pathbuf, kex->ex_path, kex->ex_pathlen);
1274 kex->ex_path[kex->ex_pathlen] = '\0';
1275 kmem_free(pathbuf, MAXPATHLEN);
1276
1277 /*
1278 * Get the path to the logging buffer and the tag
1279 */
1280 if (kex->ex_flags & EX_LOG) {
1281 log_buffer = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1282 if (copyinstr(kex->ex_log_buffer, log_buffer, MAXPATHLEN,
1283 &kex->ex_log_bufferlen)) {
1284 kmem_free(log_buffer, MAXPATHLEN);
1285 error = EFAULT;
1286 goto out2;
1287 }
1288 kex->ex_log_buffer =
1289 kmem_alloc(kex->ex_log_bufferlen + 1, KM_SLEEP);
1290 bcopy(log_buffer, kex->ex_log_buffer, kex->ex_log_bufferlen);
1291 kex->ex_log_buffer[kex->ex_log_bufferlen] = '\0';
1292 kmem_free(log_buffer, MAXPATHLEN);
1293
1294 tagbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1295 if (copyinstr(kex->ex_tag, tagbuf, MAXPATHLEN,
1296 &kex->ex_taglen)) {
1297 kmem_free(tagbuf, MAXPATHLEN);
1298 error = EFAULT;
1299 goto out3;
1300 }
1301 kex->ex_tag = kmem_alloc(kex->ex_taglen + 1, KM_SLEEP);
1302 bcopy(tagbuf, kex->ex_tag, kex->ex_taglen);
1303 kex->ex_tag[kex->ex_taglen] = '\0';
1304 kmem_free(tagbuf, MAXPATHLEN);
1305 }
1306
1307 /*
1308 * Load the security information for each flavor
1309 */
1310 allocsize = kex->ex_seccnt * SIZEOF_STRUCT(secinfo, model);
1311 sp = kmem_zalloc(allocsize, KM_SLEEP);
1312 if (copyin(kex->ex_secinfo, sp, allocsize)) {
1313 kmem_free(sp, allocsize);
1314 error = EFAULT;
1315 goto out4;
1316 }
1317
1318 /*
1319 * All of these nested structures need to be converted to
1320 * the kernel native format.
1321 */
1322 if (model != DATAMODEL_NATIVE) {
1323 size_t allocsize2;
1324 struct secinfo *sp2;
1325
1326 allocsize2 = kex->ex_seccnt * sizeof (struct secinfo);
1327 sp2 = kmem_zalloc(allocsize2, KM_SLEEP);
1328
1329 for (i = 0; i < kex->ex_seccnt; i++) {
1330 STRUCT_HANDLE(secinfo, usi);
1331
1332 STRUCT_SET_HANDLE(usi, model,
1333 (struct secinfo *)((caddr_t)sp +
1334 (i * SIZEOF_STRUCT(secinfo, model))));
1335 bcopy(STRUCT_FGET(usi, s_secinfo.sc_name),
1336 sp2[i].s_secinfo.sc_name, MAX_NAME_LEN);
1337 sp2[i].s_secinfo.sc_nfsnum =
1338 STRUCT_FGET(usi, s_secinfo.sc_nfsnum);
1339 sp2[i].s_secinfo.sc_rpcnum =
1340 STRUCT_FGET(usi, s_secinfo.sc_rpcnum);
1341 bcopy(STRUCT_FGET(usi, s_secinfo.sc_gss_mech),
1342 sp2[i].s_secinfo.sc_gss_mech, MAX_NAME_LEN);
1343 sp2[i].s_secinfo.sc_gss_mech_type =
1344 STRUCT_FGETP(usi, s_secinfo.sc_gss_mech_type);
1345 sp2[i].s_secinfo.sc_qop =
1346 STRUCT_FGET(usi, s_secinfo.sc_qop);
1347 sp2[i].s_secinfo.sc_service =
1348 STRUCT_FGET(usi, s_secinfo.sc_service);
1349
1350 sp2[i].s_flags = STRUCT_FGET(usi, s_flags);
1351 sp2[i].s_window = STRUCT_FGET(usi, s_window);
1352 sp2[i].s_rootid = STRUCT_FGET(usi, s_rootid);
1353 sp2[i].s_rootcnt = STRUCT_FGET(usi, s_rootcnt);
1354 sp2[i].s_rootnames = STRUCT_FGETP(usi, s_rootnames);
1355 }
1356 kmem_free(sp, allocsize);
1357 sp = sp2;
1358 allocsize = allocsize2;
1359 }
1360
1361 kex->ex_secinfo = sp;
1362
1363 /*
1364 * And now copy rootnames for each individual secinfo.
1365 */
1366 callback = 0;
1367 allocd_seccnt = 0;
1368 while (allocd_seccnt < kex->ex_seccnt) {
1369
1370 exs = &sp[allocd_seccnt];
1371 if (exs->s_rootcnt > 0) {
1372 if (!sec_svc_loadrootnames(exs->s_secinfo.sc_rpcnum,
1373 exs->s_rootcnt, &exs->s_rootnames, model)) {
1374 error = EFAULT;
1375 goto out5;
1376 }
1377 }
1378
1379 if (exs->s_secinfo.sc_rpcnum == RPCSEC_GSS) {
1380 rpc_gss_OID mech_tmp;
1381 STRUCT_DECL(rpc_gss_OID_s, umech_tmp);
1382 caddr_t elements_tmp;
1383
1384 /* Copyin mechanism type */
1385 STRUCT_INIT(umech_tmp, model);
1386 mech_tmp = kmem_alloc(sizeof (*mech_tmp), KM_SLEEP);
1387 if (copyin(exs->s_secinfo.sc_gss_mech_type,
1388 STRUCT_BUF(umech_tmp), STRUCT_SIZE(umech_tmp))) {
1389 kmem_free(mech_tmp, sizeof (*mech_tmp));
1390 error = EFAULT;
1391 goto out5;
1392 }
1393 mech_tmp->length = STRUCT_FGET(umech_tmp, length);
1394 mech_tmp->elements = STRUCT_FGETP(umech_tmp, elements);
1395
1396 elements_tmp = kmem_alloc(mech_tmp->length, KM_SLEEP);
1397 if (copyin(mech_tmp->elements, elements_tmp,
1398 mech_tmp->length)) {
1399 kmem_free(elements_tmp, mech_tmp->length);
1400 kmem_free(mech_tmp, sizeof (*mech_tmp));
1401 error = EFAULT;
1402 goto out5;
1403 }
1404 mech_tmp->elements = elements_tmp;
1405 exs->s_secinfo.sc_gss_mech_type = mech_tmp;
1406 allocd_seccnt++;
1407
1408 callback = 1;
1409 } else
1410 allocd_seccnt++;
1411 }
1412
1413 /*
1414 * Init the secinfo reference count and mark these flavors
1415 * explicitly exported flavors.
1416 */
1417 for (i = 0; i < kex->ex_seccnt; i++) {
1418 kex->ex_secinfo[i].s_flags |= M_4SEC_EXPORTED;
1419 kex->ex_secinfo[i].s_refcnt = 1;
1420 }
1421
1422 /*
1423 * Set up rpcsec_gss callback routine entry if any.
1424 */
1425 if (callback) {
1426 cb.callback = rfs_gsscallback;
1427 cb.program = NFS_ACL_PROGRAM;
1428 for (cb.version = NFS_ACL_VERSMIN;
1429 cb.version <= NFS_ACL_VERSMAX; cb.version++) {
1430 (void) sec_svc_control(RPC_SVC_SET_GSS_CALLBACK,
1431 (void *)&cb);
1432 }
1433
1434 cb.program = NFS_PROGRAM;
1435 for (cb.version = NFS_VERSMIN;
1436 cb.version <= NFS_VERSMAX; cb.version++) {
1437 (void) sec_svc_control(RPC_SVC_SET_GSS_CALLBACK,
1438 (void *)&cb);
1439 }
1440 }
1441
1442 /*
1443 * Check the index flag. Do this here to avoid holding the
1444 * lock while dealing with the index option (as we do with
1445 * the public option).
1446 */
1447 if (kex->ex_flags & EX_INDEX) {
1448 if (!kex->ex_index) { /* sanity check */
1449 error = EINVAL;
1450 goto out5;
1451 }
1452 if (error = loadindex(kex))
1453 goto out5;
1454 }
1455
1456 if (kex->ex_flags & EX_LOG) {
1457 if (error = nfslog_setup(exi))
1458 goto out6;
1459 }
1460
1461 /*
1462 * Insert the new entry at the front of the export list
1463 */
1464 rw_enter(&exported_lock, RW_WRITER);
1465 DTRACE_PROBE(nfss__i__exported_lock3_start);
1466
1467 export_link(exi);
1468
1469 /*
1470 * Check the rest of the list for an old entry for the fs.
1471 * If one is found then unlink it, wait until this is the
1472 * only reference and then free it.
1473 */
1474 for (ex = exi->fid_hash.next; ex != NULL; ex = ex->fid_hash.next) {
1475 if (ex != exi_root && VN_CMP(ex->exi_vp, vp)) {
1476 export_unlink(ex);
1477 break;
1478 }
1479 }
1480
1481 /*
1482 * If the public filehandle is pointing at the
1483 * old entry, then point it back at the root.
1484 */
1485 if (ex != NULL && ex == exi_public)
1486 exi_public = exi_root;
1487
1488 /*
1489 * If the public flag is on, make the global exi_public
1490 * point to this entry and turn off the public bit so that
1491 * we can distinguish it from the place holder export.
1492 */
1493 if (kex->ex_flags & EX_PUBLIC) {
1494 exi_public = exi;
1495 kex->ex_flags &= ~EX_PUBLIC;
1496 }
1497
1498 #ifdef VOLATILE_FH_TEST
1499 /*
1500 * Set up the volatile_id value if volatile on share.
1501 * The list of volatile renamed filehandles is always destroyed,
1502 * if the fs was reshared.
1503 */
1504 if (kex->ex_flags & EX_VOLFH)
1505 exi->exi_volatile_id = gethrestime_sec();
1506
1507 mutex_init(&exi->exi_vol_rename_lock, NULL, MUTEX_DEFAULT, NULL);
1508 #endif /* VOLATILE_FH_TEST */
1509
1510 /*
1511 * If this is a new export, then climb up
1512 * the tree and check if any pseudo exports
1513 * need to be created to provide a path for
1514 * NFS v4 clients.
1515 */
1516 if (ex == NULL) {
1517 error = treeclimb_export(exi);
1518 if (error)
1519 goto out7;
1520 } else {
1521 /* If it's a re-export update namespace tree */
1522 exi->exi_tree = ex->exi_tree;
1523 exi->exi_tree->tree_exi = exi;
1524
1525 /* Update the change timestamp */
1526 tree_update_change(exi->exi_tree, NULL);
1527 }
1528
1529 /*
1530 * build a unique flavor list from the flavors specified
1531 * in the share cmd. unique means that each flavor only
1532 * appears once in the secinfo list -- no duplicates allowed.
1533 */
1534 newcnt = build_seclist_nodups(&exi->exi_export, newsec, FALSE);
1535
1536 srv_secinfo_treeclimb(exi, newsec, newcnt, TRUE);
1537
1538 /*
1539 * If re-sharing an old export entry, update the secinfo data
1540 * depending on if the old entry is a pseudo node or not.
1541 */
1542 if (ex != NULL) {
1543 oldcnt = build_seclist_nodups(&ex->exi_export, oldsec, FALSE);
1544 if (PSEUDO(ex)) {
1545 /*
1546 * The dir being shared is a pseudo export root (which
1547 * will be transformed into a real export root). The
1548 * flavor(s) of the new share were propagated to the
1549 * ancestors by srv_secinfo_treeclimb() above. Now
1550 * transfer the implicit flavor refs from the old
1551 * pseudo exprot root to the new (real) export root.
1552 */
1553 srv_secinfo_add(&exi->exi_export.ex_secinfo,
1554 &exi->exi_export.ex_seccnt, oldsec, oldcnt, TRUE);
1555 } else {
1556 /*
1557 * First transfer implicit flavor refs to new export.
1558 * Remove old flavor refs last.
1559 */
1560 srv_secinfo_exp2exp(&exi->exi_export, oldsec, oldcnt);
1561 srv_secinfo_treeclimb(ex, oldsec, oldcnt, FALSE);
1562 }
1563 }
1564
1565 /*
1566 * If it's a re-export and the old entry has a pseudonode list,
1567 * transfer it to the new export.
1568 */
1569 if (ex != NULL && (ex->exi_visible != NULL)) {
1570 exi->exi_visible = ex->exi_visible;
1571 ex->exi_visible = NULL;
1572 }
1573
1574 DTRACE_PROBE(nfss__i__exported_lock3_stop);
1575 rw_exit(&exported_lock);
1576
1577 if (exi_public == exi || kex->ex_flags & EX_LOG) {
1578 /*
1579 * Log share operation to this buffer only.
1580 */
1581 nfslog_share_record(exi, cr);
1582 }
1583
1584 if (ex != NULL)
1585 exi_rele(ex);
1586
1587 return (0);
1588
1589 out7:
1590 /* Unlink the new export in exptable. */
1591 export_unlink(exi);
1592 DTRACE_PROBE(nfss__i__exported_lock3_stop);
1593 rw_exit(&exported_lock);
1594 out6:
1595 if (kex->ex_flags & EX_INDEX)
1596 kmem_free(kex->ex_index, strlen(kex->ex_index) + 1);
1597 out5:
1598 /* free partially completed allocation */
1599 while (--allocd_seccnt >= 0) {
1600 exs = &kex->ex_secinfo[allocd_seccnt];
1601 srv_secinfo_entry_free(exs);
1602 }
1603
1604 if (kex->ex_secinfo) {
1605 kmem_free(kex->ex_secinfo,
1606 kex->ex_seccnt * sizeof (struct secinfo));
1607 }
1608
1609 out4:
1610 if ((kex->ex_flags & EX_LOG) && kex->ex_tag != NULL)
1611 kmem_free(kex->ex_tag, kex->ex_taglen + 1);
1612 out3:
1613 if ((kex->ex_flags & EX_LOG) && kex->ex_log_buffer != NULL)
1614 kmem_free(kex->ex_log_buffer, kex->ex_log_bufferlen + 1);
1615 out2:
1616 kmem_free(kex->ex_path, kex->ex_pathlen + 1);
1617 out1:
1618 VN_RELE(vp);
1619 if (dvp != NULL)
1620 VN_RELE(dvp);
1621 mutex_destroy(&exi->exi_lock);
1622 rw_destroy(&exi->exi_cache_lock);
1623 for (i = 0; i < AUTH_TABLESIZE; i++) {
1624 avl_destroy(exi->exi_cache[i]);
1625 kmem_free(exi->exi_cache[i], sizeof (avl_tree_t));
1626 }
1627
1628 kmem_free(exi, sizeof (*exi));
1629
1630 return (error);
1631 }
1632
1633 /*
1634 * Remove the exportinfo from the export list
1635 */
1636 void
1637 export_unlink(struct exportinfo *exi)
1638 {
1639 ASSERT(RW_WRITE_HELD(&exported_lock));
1640
1641 exp_hash_unlink(exi, fid_hash);
1642 exp_hash_unlink(exi, path_hash);
1643 }
1644
1645 /*
1646 * Unexport an exported filesystem
1647 */
1648 static int
1649 unexport(struct exportinfo *exi)
1650 {
1651 struct secinfo cursec[MAX_FLAVORS];
1652 int curcnt;
1653
1654 rw_enter(&exported_lock, RW_WRITER);
1655
1656 /* Check if exi is still linked in the export table */
1657 if (!EXP_LINKED(exi) || PSEUDO(exi)) {
1658 rw_exit(&exported_lock);
1659 return (EINVAL);
1660 }
1661
1662 export_unlink(exi);
1663
1664 /*
1665 * Remove security flavors before treeclimb_unexport() is called
1666 * because srv_secinfo_treeclimb needs the namespace tree
1667 */
1668 curcnt = build_seclist_nodups(&exi->exi_export, cursec, TRUE);
1669
1670 srv_secinfo_treeclimb(exi, cursec, curcnt, FALSE);
1671
1672 /*
1673 * If there's a visible list, then need to leave
1674 * a pseudo export here to retain the visible list
1675 * for paths to exports below.
1676 */
1677 if (exi->exi_visible != NULL) {
1678 struct exportinfo *newexi;
1679
1680 newexi = pseudo_exportfs(exi->exi_vp, &exi->exi_fid,
1681 exi->exi_visible, &exi->exi_export);
1682 exi->exi_visible = NULL;
1683
1684 /* interconnect the existing treenode with the new exportinfo */
1685 newexi->exi_tree = exi->exi_tree;
1686 newexi->exi_tree->tree_exi = newexi;
1687
1688 /* Update the change timestamp */
1689 tree_update_change(exi->exi_tree, NULL);
1690 } else {
1691 treeclimb_unexport(exi);
1692 }
1693
1694 rw_exit(&exported_lock);
1695
1696 /*
1697 * Need to call into the NFSv4 server and release all data
1698 * held on this particular export. This is important since
1699 * the v4 server may be holding file locks or vnodes under
1700 * this export.
1701 */
1702 rfs4_clean_state_exi(exi);
1703
1704 /*
1705 * Notify the lock manager that the filesystem is being
1706 * unexported.
1707 */
1708 lm_unexport(exi);
1709
1710 /*
1711 * If this was a public export, restore
1712 * the public filehandle to the root.
1713 */
1714 if (exi == exi_public) {
1715 exi_public = exi_root;
1716
1717 nfslog_share_record(exi_public, CRED());
1718 }
1719
1720 if (exi->exi_export.ex_flags & EX_LOG) {
1721 nfslog_unshare_record(exi, CRED());
1722 }
1723
1724 exi_rele(exi);
1725 return (0);
1726 }
1727
1728 /*
1729 * Get file handle system call.
1730 * Takes file name and returns a file handle for it.
1731 * Credentials must be verified before calling.
1732 */
1733 int
1734 nfs_getfh(struct nfs_getfh_args *args, model_t model, cred_t *cr)
1735 {
1736 nfs_fh3 fh;
1737 char buf[NFS3_MAXFHSIZE];
1738 char *logptr, logbuf[NFS3_MAXFHSIZE];
1739 int l = NFS3_MAXFHSIZE;
1740 vnode_t *vp;
1741 vnode_t *dvp;
1742 struct exportinfo *exi;
1743 int error;
1744 int vers;
1745 STRUCT_HANDLE(nfs_getfh_args, uap);
1746
1747 #ifdef lint
1748 model = model; /* STRUCT macros don't always use it */
1749 #endif
1750
1751 STRUCT_SET_HANDLE(uap, model, args);
1752
1753 error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1754 FOLLOW, &dvp, &vp);
1755 if (error == EINVAL) {
1756 /*
1757 * if fname resolves to / we get EINVAL error
1758 * since we wanted the parent vnode. Try again
1759 * with NULL dvp.
1760 */
1761 error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1762 FOLLOW, NULL, &vp);
1763 dvp = NULL;
1764 }
1765 if (!error && vp == NULL) {
1766 /*
1767 * Last component of fname not found
1768 */
1769 if (dvp != NULL) {
1770 VN_RELE(dvp);
1771 }
1772 error = ENOENT;
1773 }
1774 if (error)
1775 return (error);
1776
1777 /*
1778 * 'vp' may be an AUTOFS node, so we perform a
1779 * VOP_ACCESS() to trigger the mount of the
1780 * intended filesystem, so we can share the intended
1781 * filesystem instead of the AUTOFS filesystem.
1782 */
1783 (void) VOP_ACCESS(vp, 0, 0, cr, NULL);
1784
1785 /*
1786 * We're interested in the top most filesystem.
1787 * This is specially important when uap->dname is a trigger
1788 * AUTOFS node, since we're really interested in sharing the
1789 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
1790 * call not the AUTOFS node itself.
1791 */
1792 if (vn_mountedvfs(vp) != NULL) {
1793 if (error = traverse(&vp)) {
1794 VN_RELE(vp);
1795 if (dvp != NULL)
1796 VN_RELE(dvp);
1797 return (error);
1798 }
1799 }
1800
1801 vers = STRUCT_FGET(uap, vers);
1802 exi = nfs_vptoexi(dvp, vp, cr, NULL, &error, FALSE);
1803 if (!error) {
1804 if (vers == NFS_VERSION) {
1805 error = makefh((fhandle_t *)buf, vp, exi);
1806 l = NFS_FHSIZE;
1807 logptr = buf;
1808 } else if (vers == NFS_V3) {
1809 int i, sz, pad;
1810
1811 error = makefh3(&fh, vp, exi);
1812 l = RNDUP(fh.fh3_length);
1813 if (!error && (l > sizeof (fhandle3_t)))
1814 error = EREMOTE;
1815 logptr = logbuf;
1816 if (!error) {
1817 i = 0;
1818 sz = sizeof (fsid_t);
1819 bcopy(&fh.fh3_fsid, &buf[i], sz);
1820 i += sz;
1821
1822 /*
1823 * For backwards compatibility, the
1824 * fid length may be less than
1825 * NFS_FHMAXDATA, but it was always
1826 * encoded as NFS_FHMAXDATA bytes.
1827 */
1828
1829 sz = sizeof (ushort_t);
1830 bcopy(&fh.fh3_len, &buf[i], sz);
1831 i += sz;
1832 bcopy(fh.fh3_data, &buf[i], fh.fh3_len);
1833 i += fh.fh3_len;
1834 pad = (NFS_FHMAXDATA - fh.fh3_len);
1835 if (pad > 0) {
1836 bzero(&buf[i], pad);
1837 i += pad;
1838 l += pad;
1839 }
1840
1841 sz = sizeof (ushort_t);
1842 bcopy(&fh.fh3_xlen, &buf[i], sz);
1843 i += sz;
1844 bcopy(fh.fh3_xdata, &buf[i], fh.fh3_xlen);
1845 i += fh.fh3_xlen;
1846 pad = (NFS_FHMAXDATA - fh.fh3_xlen);
1847 if (pad > 0) {
1848 bzero(&buf[i], pad);
1849 i += pad;
1850 l += pad;
1851 }
1852 }
1853 /*
1854 * If we need to do NFS logging, the filehandle
1855 * must be downsized to 32 bytes.
1856 */
1857 if (!error && exi->exi_export.ex_flags & EX_LOG) {
1858 i = 0;
1859 sz = sizeof (fsid_t);
1860 bcopy(&fh.fh3_fsid, &logbuf[i], sz);
1861 i += sz;
1862 sz = sizeof (ushort_t);
1863 bcopy(&fh.fh3_len, &logbuf[i], sz);
1864 i += sz;
1865 sz = NFS_FHMAXDATA;
1866 bcopy(fh.fh3_data, &logbuf[i], sz);
1867 i += sz;
1868 sz = sizeof (ushort_t);
1869 bcopy(&fh.fh3_xlen, &logbuf[i], sz);
1870 i += sz;
1871 sz = NFS_FHMAXDATA;
1872 bcopy(fh.fh3_xdata, &logbuf[i], sz);
1873 i += sz;
1874 }
1875 }
1876 if (!error && exi->exi_export.ex_flags & EX_LOG) {
1877 nfslog_getfh(exi, (fhandle_t *)logptr,
1878 STRUCT_FGETP(uap, fname), UIO_USERSPACE, cr);
1879 }
1880 exi_rele(exi);
1881 if (!error) {
1882 if (copyout(&l, STRUCT_FGETP(uap, lenp), sizeof (int)))
1883 error = EFAULT;
1884 if (copyout(buf, STRUCT_FGETP(uap, fhp), l))
1885 error = EFAULT;
1886 }
1887 }
1888 VN_RELE(vp);
1889 if (dvp != NULL) {
1890 VN_RELE(dvp);
1891 }
1892 return (error);
1893 }
1894
1895 /*
1896 * Strategy: if vp is in the export list, then
1897 * return the associated file handle. Otherwise, ".."
1898 * once up the vp and try again, until the root of the
1899 * filesystem is reached.
1900 */
1901 struct exportinfo *
1902 nfs_vptoexi(vnode_t *dvp, vnode_t *vp, cred_t *cr, int *walk,
1903 int *err, bool_t v4srv)
1904 {
1905 fid_t fid;
1906 int error;
1907 struct exportinfo *exi;
1908
1909 ASSERT(vp);
1910 VN_HOLD(vp);
1911 if (dvp != NULL) {
1912 VN_HOLD(dvp);
1913 }
1914 if (walk != NULL)
1915 *walk = 0;
1916
1917 for (;;) {
1918 bzero(&fid, sizeof (fid));
1919 fid.fid_len = MAXFIDSZ;
1920 error = vop_fid_pseudo(vp, &fid);
1921 if (error) {
1922 /*
1923 * If vop_fid_pseudo returns ENOSPC then the fid
1924 * supplied is too small. For now we simply
1925 * return EREMOTE.
1926 */
1927 if (error == ENOSPC)
1928 error = EREMOTE;
1929 break;
1930 }
1931
1932 if (v4srv)
1933 exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
1934 else
1935 exi = checkexport(&vp->v_vfsp->vfs_fsid, &fid);
1936
1937 if (exi != NULL) {
1938 /*
1939 * Found the export info
1940 */
1941 break;
1942 }
1943
1944 /*
1945 * We have just failed finding a matching export.
1946 * If we're at the root of this filesystem, then
1947 * it's time to stop (with failure).
1948 */
1949 if (vp->v_flag & VROOT) {
1950 error = EINVAL;
1951 break;
1952 }
1953
1954 if (walk != NULL)
1955 (*walk)++;
1956
1957 /*
1958 * Now, do a ".." up vp. If dvp is supplied, use it,
1959 * otherwise, look it up.
1960 */
1961 if (dvp == NULL) {
1962 error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, cr,
1963 NULL, NULL, NULL);
1964 if (error)
1965 break;
1966 }
1967 VN_RELE(vp);
1968 vp = dvp;
1969 dvp = NULL;
1970 }
1971 VN_RELE(vp);
1972 if (dvp != NULL) {
1973 VN_RELE(dvp);
1974 }
1975 if (error != 0) {
1976 if (err != NULL)
1977 *err = error;
1978 return (NULL);
1979 }
1980 return (exi);
1981 }
1982
1983 int
1984 chk_clnt_sec(exportinfo_t *exi, struct svc_req *req)
1985 {
1986 int i, nfsflavor;
1987 struct secinfo *sp;
1988
1989 /*
1990 * Get the nfs flavor number from xprt.
1991 */
1992 nfsflavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
1993
1994 sp = exi->exi_export.ex_secinfo;
1995 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1996 if ((nfsflavor == sp[i].s_secinfo.sc_nfsnum) &&
1997 SEC_REF_EXPORTED(sp + i))
1998 return (TRUE);
1999 }
2000 return (FALSE);
2001 }
2002
2003 /*
2004 * Make an fhandle from a vnode
2005 */
2006 int
2007 makefh(fhandle_t *fh, vnode_t *vp, exportinfo_t *exi)
2008 {
2009 int error;
2010
2011 *fh = exi->exi_fh; /* struct copy */
2012
2013 error = VOP_FID(vp, (fid_t *)&fh->fh_len, NULL);
2014 if (error) {
2015 /*
2016 * Should be something other than EREMOTE
2017 */
2018 return (EREMOTE);
2019 }
2020 return (0);
2021 }
2022
2023 /*
2024 * This routine makes an overloaded V2 fhandle which contains
2025 * sec modes.
2026 *
2027 * Note that the first four octets contain the length octet,
2028 * the status octet, and two padded octets to make them XDR
2029 * four-octet aligned.
2030 *
2031 * 1 2 3 4 32
2032 * +---+---+---+---+---+---+---+---+ +---+---+---+---+ +---+
2033 * | l | s | | | sec_1 |...| sec_n |...| |
2034 * +---+---+---+---+---+---+---+---+ +---+---+---+---+ +---+
2035 *
2036 * where
2037 *
2038 * the status octet s indicates whether there are more security
2039 * flavors (1 means yes, 0 means no) that require the client to
2040 * perform another 0x81 LOOKUP to get them,
2041 *
2042 * the length octet l is the length describing the number of
2043 * valid octets that follow. (l = 4 * n, where n is the number
2044 * of security flavors sent in the current overloaded filehandle.)
2045 *
2046 * sec_index should always be in the inclusive range: [1 - ex_seccnt],
2047 * and it tells server where to start within the secinfo array.
2048 * Usually it will always be 1; however, if more flavors are used
2049 * for the public export than can be encoded in the overloaded FH
2050 * (7 for NFS2), subsequent SNEGO MCLs will have a larger index
2051 * so the server will pick up where it left off from the previous
2052 * MCL reply.
2053 *
2054 * With NFS4 support, implicitly allowed flavors are also in
2055 * the secinfo array; however, they should not be returned in
2056 * SNEGO MCL replies.
2057 */
2058 int
2059 makefh_ol(fhandle_t *fh, exportinfo_t *exi, uint_t sec_index)
2060 {
2061 secinfo_t sec[MAX_FLAVORS];
2062 int totalcnt, i, *ipt, cnt, seccnt, secidx, fh_max_cnt;
2063 char *c;
2064
2065 if (fh == NULL || exi == NULL || sec_index < 1)
2066 return (EREMOTE);
2067
2068 /*
2069 * WebNFS clients need to know the unique set of explicitly
2070 * shared flavors in used for the public export. When
2071 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2072 * shared flavors are included in the list.
2073 */
2074 seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2075 if (sec_index > seccnt)
2076 return (EREMOTE);
2077
2078 fh_max_cnt = (NFS_FHSIZE / sizeof (int)) - 1;
2079 totalcnt = seccnt - sec_index + 1;
2080 cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2081
2082 c = (char *)fh;
2083 /*
2084 * Encode the length octet representing the number of
2085 * security flavors (in bytes) in this overloaded fh.
2086 */
2087 *c = cnt * sizeof (int);
2088
2089 /*
2090 * Encode the status octet that indicates whether there
2091 * are more security flavors the client needs to get.
2092 */
2093 *(c + 1) = totalcnt > fh_max_cnt;
2094
2095 /*
2096 * put security flavors in the overloaded fh
2097 */
2098 ipt = (int *)(c + sizeof (int32_t));
2099 secidx = sec_index - 1;
2100 for (i = 0; i < cnt; i++) {
2101 ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2102 }
2103 return (0);
2104 }
2105
2106 /*
2107 * Make an nfs_fh3 from a vnode
2108 */
2109 int
2110 makefh3(nfs_fh3 *fh, vnode_t *vp, struct exportinfo *exi)
2111 {
2112 int error;
2113 fid_t fid;
2114
2115 bzero(&fid, sizeof (fid));
2116 fid.fid_len = sizeof (fh->fh3_data);
2117 error = VOP_FID(vp, &fid, NULL);
2118 if (error)
2119 return (EREMOTE);
2120
2121 bzero(fh, sizeof (nfs_fh3));
2122 fh->fh3_fsid = exi->exi_fsid;
2123 fh->fh3_len = fid.fid_len;
2124 bcopy(fid.fid_data, fh->fh3_data, fh->fh3_len);
2125
2126 fh->fh3_xlen = exi->exi_fid.fid_len;
2127 ASSERT(fh->fh3_xlen <= sizeof (fh->fh3_xdata));
2128 bcopy(exi->exi_fid.fid_data, fh->fh3_xdata, fh->fh3_xlen);
2129
2130 fh->fh3_length = sizeof (fh->fh3_fsid)
2131 + sizeof (fh->fh3_len) + fh->fh3_len
2132 + sizeof (fh->fh3_xlen) + fh->fh3_xlen;
2133 fh->fh3_flags = 0;
2134
2135 return (0);
2136 }
2137
2138 /*
2139 * This routine makes an overloaded V3 fhandle which contains
2140 * sec modes.
2141 *
2142 * 1 4
2143 * +--+--+--+--+
2144 * | len |
2145 * +--+--+--+--+
2146 * up to 64
2147 * +--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+
2148 * |s | | | | sec_1 | sec_2 | ... | sec_n |
2149 * +--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+
2150 *
2151 * len = 4 * (n+1), where n is the number of security flavors
2152 * sent in the current overloaded filehandle.
2153 *
2154 * the status octet s indicates whether there are more security
2155 * mechanisms (1 means yes, 0 means no) that require the client
2156 * to perform another 0x81 LOOKUP to get them.
2157 *
2158 * Three octets are padded after the status octet.
2159 */
2160 int
2161 makefh3_ol(nfs_fh3 *fh, struct exportinfo *exi, uint_t sec_index)
2162 {
2163 secinfo_t sec[MAX_FLAVORS];
2164 int totalcnt, cnt, *ipt, i, seccnt, fh_max_cnt, secidx;
2165 char *c;
2166
2167 if (fh == NULL || exi == NULL || sec_index < 1)
2168 return (EREMOTE);
2169
2170 /*
2171 * WebNFS clients need to know the unique set of explicitly
2172 * shared flavors in used for the public export. When
2173 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2174 * shared flavors are included in the list.
2175 */
2176 seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2177
2178 if (sec_index > seccnt)
2179 return (EREMOTE);
2180
2181 fh_max_cnt = (NFS3_FHSIZE / sizeof (int)) - 1;
2182 totalcnt = seccnt - sec_index + 1;
2183 cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2184
2185 /*
2186 * Place the length in fh3_length representing the number
2187 * of security flavors (in bytes) in this overloaded fh.
2188 */
2189 fh->fh3_flags = FH_WEBNFS;
2190 fh->fh3_length = (cnt+1) * sizeof (int32_t);
2191
2192 c = (char *)&fh->fh3_u.nfs_fh3_i.fh3_i;
2193 /*
2194 * Encode the status octet that indicates whether there
2195 * are more security flavors the client needs to get.
2196 */
2197 *c = totalcnt > fh_max_cnt;
2198
2199 /*
2200 * put security flavors in the overloaded fh
2201 */
2202 secidx = sec_index - 1;
2203 ipt = (int *)(c + sizeof (int32_t));
2204 for (i = 0; i < cnt; i++) {
2205 ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2206 }
2207 return (0);
2208 }
2209
2210 /*
2211 * Make an nfs_fh4 from a vnode
2212 */
2213 int
2214 makefh4(nfs_fh4 *fh, vnode_t *vp, struct exportinfo *exi)
2215 {
2216 int error;
2217 nfs_fh4_fmt_t *fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2218 fid_t fid;
2219
2220 bzero(&fid, sizeof (fid));
2221 fid.fid_len = MAXFIDSZ;
2222 /*
2223 * vop_fid_pseudo() is used to set up NFSv4 namespace, so
2224 * use vop_fid_pseudo() here to get the fid instead of VOP_FID.
2225 */
2226 error = vop_fid_pseudo(vp, &fid);
2227 if (error)
2228 return (error);
2229
2230 fh->nfs_fh4_len = NFS_FH4_LEN;
2231
2232 fh_fmtp->fh4_i.fhx_fsid = exi->exi_fh.fh_fsid;
2233 fh_fmtp->fh4_i.fhx_xlen = exi->exi_fh.fh_xlen;
2234
2235 bzero(fh_fmtp->fh4_i.fhx_data, sizeof (fh_fmtp->fh4_i.fhx_data));
2236 bzero(fh_fmtp->fh4_i.fhx_xdata, sizeof (fh_fmtp->fh4_i.fhx_xdata));
2237 ASSERT(exi->exi_fh.fh_xlen <= sizeof (fh_fmtp->fh4_i.fhx_xdata));
2238 bcopy(exi->exi_fh.fh_xdata, fh_fmtp->fh4_i.fhx_xdata,
2239 exi->exi_fh.fh_xlen);
2240
2241 fh_fmtp->fh4_len = fid.fid_len;
2242 ASSERT(fid.fid_len <= sizeof (fh_fmtp->fh4_data));
2243 bcopy(fid.fid_data, fh_fmtp->fh4_data, fid.fid_len);
2244 fh_fmtp->fh4_flag = 0;
2245
2246 #ifdef VOLATILE_FH_TEST
2247 /*
2248 * XXX (temporary?)
2249 * Use the rnode volatile_id value to add volatility to the fh.
2250 *
2251 * For testing purposes there are currently two scenarios, based
2252 * on whether the filesystem was shared with "volatile_fh"
2253 * or "expire_on_rename". In the first case, use the value of
2254 * export struct share_time as the volatile_id. In the second
2255 * case use the vnode volatile_id value (which is set to the
2256 * time in which the file was renamed).
2257 *
2258 * Note that the above are temporary constructs for testing only
2259 * XXX
2260 */
2261 if (exi->exi_export.ex_flags & EX_VOLRNM) {
2262 fh_fmtp->fh4_volatile_id = find_volrnm_fh_id(exi, fh);
2263 } else if (exi->exi_export.ex_flags & EX_VOLFH) {
2264 fh_fmtp->fh4_volatile_id = exi->exi_volatile_id;
2265 } else {
2266 fh_fmtp->fh4_volatile_id = 0;
2267 }
2268 #endif /* VOLATILE_FH_TEST */
2269
2270 return (0);
2271 }
2272
2273 /*
2274 * Convert an fhandle into a vnode.
2275 * Uses the file id (fh_len + fh_data) in the fhandle to get the vnode.
2276 * WARNING: users of this routine must do a VN_RELE on the vnode when they
2277 * are done with it.
2278 */
2279 vnode_t *
2280 nfs_fhtovp(fhandle_t *fh, struct exportinfo *exi)
2281 {
2282 vfs_t *vfsp;
2283 vnode_t *vp;
2284 int error;
2285 fid_t *fidp;
2286
2287 TRACE_0(TR_FAC_NFS, TR_FHTOVP_START,
2288 "fhtovp_start");
2289
2290 if (exi == NULL) {
2291 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2292 "fhtovp_end:(%S)", "exi NULL");
2293 return (NULL); /* not exported */
2294 }
2295
2296 ASSERT(exi->exi_vp != NULL);
2297
2298 if (PUBLIC_FH2(fh)) {
2299 if (exi->exi_export.ex_flags & EX_PUBLIC) {
2300 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2301 "fhtovp_end:(%S)", "root not exported");
2302 return (NULL);
2303 }
2304 vp = exi->exi_vp;
2305 VN_HOLD(vp);
2306 return (vp);
2307 }
2308
2309 vfsp = exi->exi_vp->v_vfsp;
2310 ASSERT(vfsp != NULL);
2311 fidp = (fid_t *)&fh->fh_len;
2312
2313 error = VFS_VGET(vfsp, &vp, fidp);
2314 if (error || vp == NULL) {
2315 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2316 "fhtovp_end:(%S)", "VFS_GET failed or vp NULL");
2317 return (NULL);
2318 }
2319 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2320 "fhtovp_end:(%S)", "end");
2321 return (vp);
2322 }
2323
2324 /*
2325 * Convert an nfs_fh3 into a vnode.
2326 * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2327 * WARNING: users of this routine must do a VN_RELE on the vnode when they
2328 * are done with it.
2329 */
2330 vnode_t *
2331 nfs3_fhtovp(nfs_fh3 *fh, struct exportinfo *exi)
2332 {
2333 vfs_t *vfsp;
2334 vnode_t *vp;
2335 int error;
2336 fid_t *fidp;
2337
2338 if (exi == NULL)
2339 return (NULL); /* not exported */
2340
2341 ASSERT(exi->exi_vp != NULL);
2342
2343 if (PUBLIC_FH3(fh)) {
2344 if (exi->exi_export.ex_flags & EX_PUBLIC)
2345 return (NULL);
2346 vp = exi->exi_vp;
2347 VN_HOLD(vp);
2348 return (vp);
2349 }
2350
2351 if (fh->fh3_length < NFS3_OLDFHSIZE ||
2352 fh->fh3_length > NFS3_MAXFHSIZE)
2353 return (NULL);
2354
2355 vfsp = exi->exi_vp->v_vfsp;
2356 ASSERT(vfsp != NULL);
2357 fidp = FH3TOFIDP(fh);
2358
2359 error = VFS_VGET(vfsp, &vp, fidp);
2360 if (error || vp == NULL)
2361 return (NULL);
2362
2363 return (vp);
2364 }
2365
2366 /*
2367 * Convert an nfs_fh4 into a vnode.
2368 * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2369 * WARNING: users of this routine must do a VN_RELE on the vnode when they
2370 * are done with it.
2371 */
2372 vnode_t *
2373 nfs4_fhtovp(nfs_fh4 *fh, struct exportinfo *exi, nfsstat4 *statp)
2374 {
2375 vfs_t *vfsp;
2376 vnode_t *vp = NULL;
2377 int error;
2378 fid_t *fidp;
2379 nfs_fh4_fmt_t *fh_fmtp;
2380 #ifdef VOLATILE_FH_TEST
2381 uint32_t volatile_id = 0;
2382 #endif /* VOLATILE_FH_TEST */
2383
2384 if (exi == NULL) {
2385 *statp = NFS4ERR_STALE;
2386 return (NULL); /* not exported */
2387 }
2388 ASSERT(exi->exi_vp != NULL);
2389
2390 /* caller should have checked this */
2391 ASSERT(fh->nfs_fh4_len >= NFS_FH4_LEN);
2392
2393 fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2394 vfsp = exi->exi_vp->v_vfsp;
2395 ASSERT(vfsp != NULL);
2396 fidp = (fid_t *)&fh_fmtp->fh4_len;
2397
2398 #ifdef VOLATILE_FH_TEST
2399 /* XXX check if volatile - should be changed later */
2400 if (exi->exi_export.ex_flags & (EX_VOLRNM | EX_VOLFH)) {
2401 /*
2402 * Filesystem is shared with volatile filehandles
2403 */
2404 if (exi->exi_export.ex_flags & EX_VOLRNM)
2405 volatile_id = find_volrnm_fh_id(exi, fh);
2406 else
2407 volatile_id = exi->exi_volatile_id;
2408
2409 if (fh_fmtp->fh4_volatile_id != volatile_id) {
2410 *statp = NFS4ERR_FHEXPIRED;
2411 return (NULL);
2412 }
2413 }
2414 /*
2415 * XXX even if test_volatile_fh false, the fh may contain a
2416 * volatile id if obtained when the test was set.
2417 */
2418 fh_fmtp->fh4_volatile_id = (uchar_t)0;
2419 #endif /* VOLATILE_FH_TEST */
2420
2421 error = VFS_VGET(vfsp, &vp, fidp);
2422 /*
2423 * If we can not get vp from VFS_VGET, perhaps this is
2424 * an nfs v2/v3/v4 node in an nfsv4 pseudo filesystem.
2425 * Check it out.
2426 */
2427 if (error && PSEUDO(exi))
2428 error = nfs4_vget_pseudo(exi, &vp, fidp);
2429
2430 if (error || vp == NULL) {
2431 *statp = NFS4ERR_STALE;
2432 return (NULL);
2433 }
2434 /* XXX - disgusting hack */
2435 if (vp->v_type == VNON && vp->v_flag & V_XATTRDIR)
2436 vp->v_type = VDIR;
2437 *statp = NFS4_OK;
2438 return (vp);
2439 }
2440
2441 /*
2442 * Find the export structure associated with the given filesystem.
2443 * If found, then increment the ref count (exi_count).
2444 */
2445 struct exportinfo *
2446 checkexport(fsid_t *fsid, fid_t *fid)
2447 {
2448 struct exportinfo *exi;
2449
2450 rw_enter(&exported_lock, RW_READER);
2451 for (exi = exptable[exptablehash(fsid, fid)];
2452 exi != NULL;
2453 exi = exi->fid_hash.next) {
2454 if (exportmatch(exi, fsid, fid)) {
2455 /*
2456 * If this is the place holder for the
2457 * public file handle, then return the
2458 * real export entry for the public file
2459 * handle.
2460 */
2461 if (exi->exi_export.ex_flags & EX_PUBLIC) {
2462 exi = exi_public;
2463 }
2464
2465 exi_hold(exi);
2466 rw_exit(&exported_lock);
2467 return (exi);
2468 }
2469 }
2470 rw_exit(&exported_lock);
2471 return (NULL);
2472 }
2473
2474
2475 /*
2476 * "old school" version of checkexport() for NFS4. NFS4
2477 * rfs4_compound holds exported_lock for duration of compound
2478 * processing. This version doesn't manipulate exi_count
2479 * since NFS4 breaks fundamental assumptions in the exi_count
2480 * design.
2481 */
2482 struct exportinfo *
2483 checkexport4(fsid_t *fsid, fid_t *fid, vnode_t *vp)
2484 {
2485 struct exportinfo *exi;
2486
2487 ASSERT(RW_LOCK_HELD(&exported_lock));
2488
2489 for (exi = exptable[exptablehash(fsid, fid)];
2490 exi != NULL;
2491 exi = exi->fid_hash.next) {
2492 if (exportmatch(exi, fsid, fid)) {
2493 /*
2494 * If this is the place holder for the
2495 * public file handle, then return the
2496 * real export entry for the public file
2497 * handle.
2498 */
2499 if (exi->exi_export.ex_flags & EX_PUBLIC) {
2500 exi = exi_public;
2501 }
2502
2503 /*
2504 * If vp is given, check if vp is the
2505 * same vnode as the exported node.
2506 *
2507 * Since VOP_FID of a lofs node returns the
2508 * fid of its real node (ufs), the exported
2509 * node for lofs and (pseudo) ufs may have
2510 * the same fsid and fid.
2511 */
2512 if (vp == NULL || vp == exi->exi_vp)
2513 return (exi);
2514 }
2515 }
2516
2517 return (NULL);
2518 }
2519
2520 /*
2521 * Free an entire export list node
2522 */
2523 void
2524 exportfree(struct exportinfo *exi)
2525 {
2526 struct exportdata *ex;
2527 struct charset_cache *cache;
2528 int i;
2529
2530 ex = &exi->exi_export;
2531
2532 ASSERT(exi->exi_vp != NULL && !(exi->exi_export.ex_flags & EX_PUBLIC));
2533 VN_RELE(exi->exi_vp);
2534 if (exi->exi_dvp != NULL)
2535 VN_RELE(exi->exi_dvp);
2536
2537 if (ex->ex_flags & EX_INDEX)
2538 kmem_free(ex->ex_index, strlen(ex->ex_index) + 1);
2539
2540 kmem_free(ex->ex_path, ex->ex_pathlen + 1);
2541 nfsauth_cache_free(exi);
2542
2543 /*
2544 * if there is a character set mapping cached, clean it up.
2545 */
2546 for (cache = exi->exi_charset; cache != NULL;
2547 cache = exi->exi_charset) {
2548 if (cache->inbound != (kiconv_t)-1)
2549 (void) kiconv_close(cache->inbound);
2550 if (cache->outbound != (kiconv_t)-1)
2551 (void) kiconv_close(cache->outbound);
2552 exi->exi_charset = cache->next;
2553 kmem_free(cache, sizeof (struct charset_cache));
2554 }
2555
2556 if (exi->exi_logbuffer != NULL)
2557 nfslog_disable(exi);
2558
2559 if (ex->ex_flags & EX_LOG) {
2560 kmem_free(ex->ex_log_buffer, ex->ex_log_bufferlen + 1);
2561 kmem_free(ex->ex_tag, ex->ex_taglen + 1);
2562 }
2563
2564 if (exi->exi_visible)
2565 free_visible(exi->exi_visible);
2566
2567 srv_secinfo_list_free(ex->ex_secinfo, ex->ex_seccnt);
2568
2569 #ifdef VOLATILE_FH_TEST
2570 free_volrnm_list(exi);
2571 mutex_destroy(&exi->exi_vol_rename_lock);
2572 #endif /* VOLATILE_FH_TEST */
2573
2574 mutex_destroy(&exi->exi_lock);
2575 rw_destroy(&exi->exi_cache_lock);
2576 /*
2577 * All nodes in the exi_cache AVL trees were removed and freed in the
2578 * nfsauth_cache_free() call above. We will just destroy and free the
2579 * empty AVL trees here.
2580 */
2581 for (i = 0; i < AUTH_TABLESIZE; i++) {
2582 avl_destroy(exi->exi_cache[i]);
2583 kmem_free(exi->exi_cache[i], sizeof (avl_tree_t));
2584 }
2585
2586 kmem_free(exi, sizeof (*exi));
2587 }
2588
2589 /*
2590 * load the index file from user space into kernel space.
2591 */
2592 static int
2593 loadindex(struct exportdata *kex)
2594 {
2595 int error;
2596 char index[MAXNAMELEN+1];
2597 size_t len;
2598
2599 /*
2600 * copyinstr copies the complete string including the NULL and
2601 * returns the len with the NULL byte included in the calculation
2602 * as long as the max length is not exceeded.
2603 */
2604 if (error = copyinstr(kex->ex_index, index, sizeof (index), &len))
2605 return (error);
2606
2607 kex->ex_index = kmem_alloc(len, KM_SLEEP);
2608 bcopy(index, kex->ex_index, len);
2609
2610 return (0);
2611 }
2612
2613 void
2614 exi_hold(struct exportinfo *exi)
2615 {
2616 mutex_enter(&exi->exi_lock);
2617 exi->exi_count++;
2618 mutex_exit(&exi->exi_lock);
2619 }
2620
2621 /*
2622 * When a thread completes using exi, it should call exi_rele().
2623 * exi_rele() decrements exi_count. It releases exi if exi_count == 0, i.e.
2624 * if this is the last user of exi and exi is not on exportinfo list anymore
2625 */
2626 void
2627 exi_rele(struct exportinfo *exi)
2628 {
2629 mutex_enter(&exi->exi_lock);
2630 exi->exi_count--;
2631 if (exi->exi_count == 0) {
2632 mutex_exit(&exi->exi_lock);
2633 exportfree(exi);
2634 } else
2635 mutex_exit(&exi->exi_lock);
2636 }
2637
2638 #ifdef VOLATILE_FH_TEST
2639 /*
2640 * Test for volatile fh's - add file handle to list and set its volatile id
2641 * to time it was renamed. If EX_VOLFH is also on and the fs is reshared,
2642 * the vol_rename queue is purged.
2643 *
2644 * XXX This code is for unit testing purposes only... To correctly use it, it
2645 * needs to tie a rename list to the export struct and (more
2646 * important), protect access to the exi rename list using a write lock.
2647 */
2648
2649 /*
2650 * get the fh vol record if it's in the volatile on rename list. Don't check
2651 * volatile_id in the file handle - compare only the file handles.
2652 */
2653 static struct ex_vol_rename *
2654 find_volrnm_fh(struct exportinfo *exi, nfs_fh4 *fh4p)
2655 {
2656 struct ex_vol_rename *p = NULL;
2657 fhandle4_t *fhp;
2658
2659 /* XXX shouldn't we assert &exported_lock held? */
2660 ASSERT(MUTEX_HELD(&exi->exi_vol_rename_lock));
2661
2662 if (fh4p->nfs_fh4_len != NFS_FH4_LEN) {
2663 return (NULL);
2664 }
2665 fhp = &((nfs_fh4_fmt_t *)fh4p->nfs_fh4_val)->fh4_i;
2666 for (p = exi->exi_vol_rename; p != NULL; p = p->vrn_next) {
2667 if (bcmp(fhp, &p->vrn_fh_fmt.fh4_i,
2668 sizeof (fhandle4_t)) == 0)
2669 break;
2670 }
2671 return (p);
2672 }
2673
2674 /*
2675 * get the volatile id for the fh (if there is - else return 0). Ignore the
2676 * volatile_id in the file handle - compare only the file handles.
2677 */
2678 static uint32_t
2679 find_volrnm_fh_id(struct exportinfo *exi, nfs_fh4 *fh4p)
2680 {
2681 struct ex_vol_rename *p;
2682 uint32_t volatile_id;
2683
2684 mutex_enter(&exi->exi_vol_rename_lock);
2685 p = find_volrnm_fh(exi, fh4p);
2686 volatile_id = (p ? p->vrn_fh_fmt.fh4_volatile_id :
2687 exi->exi_volatile_id);
2688 mutex_exit(&exi->exi_vol_rename_lock);
2689 return (volatile_id);
2690 }
2691
2692 /*
2693 * Free the volatile on rename list - will be called if a filesystem is
2694 * unshared or reshared without EX_VOLRNM
2695 */
2696 static void
2697 free_volrnm_list(struct exportinfo *exi)
2698 {
2699 struct ex_vol_rename *p, *pnext;
2700
2701 /* no need to hold mutex lock - this one is called from exportfree */
2702 for (p = exi->exi_vol_rename; p != NULL; p = pnext) {
2703 pnext = p->vrn_next;
2704 kmem_free(p, sizeof (*p));
2705 }
2706 exi->exi_vol_rename = NULL;
2707 }
2708
2709 /*
2710 * Add a file handle to the volatile on rename list.
2711 */
2712 void
2713 add_volrnm_fh(struct exportinfo *exi, vnode_t *vp)
2714 {
2715 struct ex_vol_rename *p;
2716 char fhbuf[NFS4_FHSIZE];
2717 nfs_fh4 fh4;
2718 int error;
2719
2720 fh4.nfs_fh4_val = fhbuf;
2721 error = makefh4(&fh4, vp, exi);
2722 if ((error) || (fh4.nfs_fh4_len != sizeof (p->vrn_fh_fmt))) {
2723 return;
2724 }
2725
2726 mutex_enter(&exi->exi_vol_rename_lock);
2727
2728 p = find_volrnm_fh(exi, &fh4);
2729
2730 if (p == NULL) {
2731 p = kmem_alloc(sizeof (*p), KM_SLEEP);
2732 bcopy(fh4.nfs_fh4_val, &p->vrn_fh_fmt, sizeof (p->vrn_fh_fmt));
2733 p->vrn_next = exi->exi_vol_rename;
2734 exi->exi_vol_rename = p;
2735 }
2736
2737 p->vrn_fh_fmt.fh4_volatile_id = gethrestime_sec();
2738 mutex_exit(&exi->exi_vol_rename_lock);
2739 }
2740
2741 #endif /* VOLATILE_FH_TEST */