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
xor_hash(uint8_t * data,int len)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
nfs_fhhash(fsid_t * fsid,fid_t * fid)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
srv_secinfo_entry_free(struct secinfo * secp)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
srv_secinfo_list_free(struct secinfo * secinfo,int cnt)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
srv_secinfo_copy(struct secinfo * from,struct secinfo * to)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
build_seclist_nodups(exportdata_t * exd,secinfo_t * nodups,int exponly)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
srv_secinfo_add(secinfo_t ** pcursec,int * pcurcnt,secinfo_t * newsec,int newcnt,int is_pseudo)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
srv_secinfo_remove(secinfo_t ** pcursec,int * pcurcnt,secinfo_t * remsec,int remcnt)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
srv_secinfo_exp2exp(exportdata_t * curdata,secinfo_t * oldsecinfo,int ocnt)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
srv_secinfo_exp2pseu(exportdata_t * curdata,exportdata_t * olddata)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 *
vis2exi(treenode_t * tnode)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
srv_secinfo_treeclimb(exportinfo_t * exip,secinfo_t * sec,int seccnt,bool_t isadd)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
export_link(exportinfo_t * exi)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
nfs_exportinit(void)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
nfs_exportfini(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
nfs_mech_equal(rpc_gss_OID mech1,rpc_gss_OID mech2)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
rfs_gsscallback(struct svc_req * req,gss_cred_id_t deleg,void * gss_context,rpc_gss_lock_t * lock,void ** cookie)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
exportfs(struct exportfs_args * args,model_t model,cred_t * cr)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 ex->exi_tree = NULL;
1564 }
1565
1566 /*
1567 * If it's a re-export and the old entry has a pseudonode list,
1568 * transfer it to the new export.
1569 */
1570 if (ex != NULL && (ex->exi_visible != NULL)) {
1571 exi->exi_visible = ex->exi_visible;
1572 ex->exi_visible = NULL;
1573 }
1574
1575 DTRACE_PROBE(nfss__i__exported_lock3_stop);
1576 rw_exit(&exported_lock);
1577
1578 if (exi_public == exi || kex->ex_flags & EX_LOG) {
1579 /*
1580 * Log share operation to this buffer only.
1581 */
1582 nfslog_share_record(exi, cr);
1583 }
1584
1585 if (ex != NULL)
1586 exi_rele(ex);
1587
1588 return (0);
1589
1590 out7:
1591 /* Unlink the new export in exptable. */
1592 export_unlink(exi);
1593 DTRACE_PROBE(nfss__i__exported_lock3_stop);
1594 rw_exit(&exported_lock);
1595 out6:
1596 if (kex->ex_flags & EX_INDEX)
1597 kmem_free(kex->ex_index, strlen(kex->ex_index) + 1);
1598 out5:
1599 /* free partially completed allocation */
1600 while (--allocd_seccnt >= 0) {
1601 exs = &kex->ex_secinfo[allocd_seccnt];
1602 srv_secinfo_entry_free(exs);
1603 }
1604
1605 if (kex->ex_secinfo) {
1606 kmem_free(kex->ex_secinfo,
1607 kex->ex_seccnt * sizeof (struct secinfo));
1608 }
1609
1610 out4:
1611 if ((kex->ex_flags & EX_LOG) && kex->ex_tag != NULL)
1612 kmem_free(kex->ex_tag, kex->ex_taglen + 1);
1613 out3:
1614 if ((kex->ex_flags & EX_LOG) && kex->ex_log_buffer != NULL)
1615 kmem_free(kex->ex_log_buffer, kex->ex_log_bufferlen + 1);
1616 out2:
1617 kmem_free(kex->ex_path, kex->ex_pathlen + 1);
1618 out1:
1619 VN_RELE(vp);
1620 if (dvp != NULL)
1621 VN_RELE(dvp);
1622 mutex_destroy(&exi->exi_lock);
1623 rw_destroy(&exi->exi_cache_lock);
1624 for (i = 0; i < AUTH_TABLESIZE; i++) {
1625 avl_destroy(exi->exi_cache[i]);
1626 kmem_free(exi->exi_cache[i], sizeof (avl_tree_t));
1627 }
1628
1629 kmem_free(exi, sizeof (*exi));
1630
1631 return (error);
1632 }
1633
1634 /*
1635 * Remove the exportinfo from the export list
1636 */
1637 void
export_unlink(struct exportinfo * exi)1638 export_unlink(struct exportinfo *exi)
1639 {
1640 ASSERT(RW_WRITE_HELD(&exported_lock));
1641
1642 exp_hash_unlink(exi, fid_hash);
1643 exp_hash_unlink(exi, path_hash);
1644 }
1645
1646 /*
1647 * Unexport an exported filesystem
1648 */
1649 static int
unexport(struct exportinfo * exi)1650 unexport(struct exportinfo *exi)
1651 {
1652 struct secinfo cursec[MAX_FLAVORS];
1653 int curcnt;
1654
1655 rw_enter(&exported_lock, RW_WRITER);
1656
1657 /* Check if exi is still linked in the export table */
1658 if (!EXP_LINKED(exi) || PSEUDO(exi)) {
1659 rw_exit(&exported_lock);
1660 return (EINVAL);
1661 }
1662
1663 export_unlink(exi);
1664
1665 /*
1666 * Remove security flavors before treeclimb_unexport() is called
1667 * because srv_secinfo_treeclimb needs the namespace tree
1668 */
1669 curcnt = build_seclist_nodups(&exi->exi_export, cursec, TRUE);
1670
1671 srv_secinfo_treeclimb(exi, cursec, curcnt, FALSE);
1672
1673 /*
1674 * If there's a visible list, then need to leave
1675 * a pseudo export here to retain the visible list
1676 * for paths to exports below.
1677 */
1678 if (exi->exi_visible != NULL) {
1679 struct exportinfo *newexi;
1680
1681 newexi = pseudo_exportfs(exi->exi_vp, &exi->exi_fid,
1682 exi->exi_visible, &exi->exi_export);
1683 exi->exi_visible = NULL;
1684
1685 /* interconnect the existing treenode with the new exportinfo */
1686 newexi->exi_tree = exi->exi_tree;
1687 newexi->exi_tree->tree_exi = newexi;
1688
1689 /* Update the change timestamp */
1690 tree_update_change(newexi->exi_tree, NULL);
1691 } else {
1692 treeclimb_unexport(exi);
1693 }
1694 exi->exi_tree = NULL;
1695
1696 rw_exit(&exported_lock);
1697
1698 /*
1699 * Need to call into the NFSv4 server and release all data
1700 * held on this particular export. This is important since
1701 * the v4 server may be holding file locks or vnodes under
1702 * this export.
1703 */
1704 rfs4_clean_state_exi(exi);
1705
1706 /*
1707 * Notify the lock manager that the filesystem is being
1708 * unexported.
1709 */
1710 lm_unexport(exi);
1711
1712 /*
1713 * If this was a public export, restore
1714 * the public filehandle to the root.
1715 */
1716 if (exi == exi_public) {
1717 exi_public = exi_root;
1718
1719 nfslog_share_record(exi_public, CRED());
1720 }
1721
1722 if (exi->exi_export.ex_flags & EX_LOG) {
1723 nfslog_unshare_record(exi, CRED());
1724 }
1725
1726 exi_rele(exi);
1727 return (0);
1728 }
1729
1730 /*
1731 * Get file handle system call.
1732 * Takes file name and returns a file handle for it.
1733 * Credentials must be verified before calling.
1734 */
1735 int
nfs_getfh(struct nfs_getfh_args * args,model_t model,cred_t * cr)1736 nfs_getfh(struct nfs_getfh_args *args, model_t model, cred_t *cr)
1737 {
1738 nfs_fh3 fh;
1739 char buf[NFS3_MAXFHSIZE];
1740 char *logptr, logbuf[NFS3_MAXFHSIZE];
1741 int l = NFS3_MAXFHSIZE;
1742 vnode_t *vp;
1743 vnode_t *dvp;
1744 struct exportinfo *exi;
1745 int error;
1746 int vers;
1747 STRUCT_HANDLE(nfs_getfh_args, uap);
1748
1749 #ifdef lint
1750 model = model; /* STRUCT macros don't always use it */
1751 #endif
1752
1753 STRUCT_SET_HANDLE(uap, model, args);
1754
1755 error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1756 FOLLOW, &dvp, &vp);
1757 if (error == EINVAL) {
1758 /*
1759 * if fname resolves to / we get EINVAL error
1760 * since we wanted the parent vnode. Try again
1761 * with NULL dvp.
1762 */
1763 error = lookupname(STRUCT_FGETP(uap, fname), UIO_USERSPACE,
1764 FOLLOW, NULL, &vp);
1765 dvp = NULL;
1766 }
1767 if (!error && vp == NULL) {
1768 /*
1769 * Last component of fname not found
1770 */
1771 if (dvp != NULL) {
1772 VN_RELE(dvp);
1773 }
1774 error = ENOENT;
1775 }
1776 if (error)
1777 return (error);
1778
1779 /*
1780 * 'vp' may be an AUTOFS node, so we perform a
1781 * VOP_ACCESS() to trigger the mount of the
1782 * intended filesystem, so we can share the intended
1783 * filesystem instead of the AUTOFS filesystem.
1784 */
1785 (void) VOP_ACCESS(vp, 0, 0, cr, NULL);
1786
1787 /*
1788 * We're interested in the top most filesystem.
1789 * This is specially important when uap->dname is a trigger
1790 * AUTOFS node, since we're really interested in sharing the
1791 * filesystem AUTOFS mounted as result of the VOP_ACCESS()
1792 * call not the AUTOFS node itself.
1793 */
1794 if (vn_mountedvfs(vp) != NULL) {
1795 if (error = traverse(&vp)) {
1796 VN_RELE(vp);
1797 if (dvp != NULL)
1798 VN_RELE(dvp);
1799 return (error);
1800 }
1801 }
1802
1803 vers = STRUCT_FGET(uap, vers);
1804 exi = nfs_vptoexi(dvp, vp, cr, NULL, &error, FALSE);
1805 if (!error) {
1806 if (vers == NFS_VERSION) {
1807 error = makefh((fhandle_t *)buf, vp, exi);
1808 l = NFS_FHSIZE;
1809 logptr = buf;
1810 } else if (vers == NFS_V3) {
1811 int i, sz, pad;
1812
1813 error = makefh3(&fh, vp, exi);
1814 l = RNDUP(fh.fh3_length);
1815 if (!error && (l > sizeof (fhandle3_t)))
1816 error = EREMOTE;
1817 logptr = logbuf;
1818 if (!error) {
1819 i = 0;
1820 sz = sizeof (fsid_t);
1821 bcopy(&fh.fh3_fsid, &buf[i], sz);
1822 i += sz;
1823
1824 /*
1825 * For backwards compatibility, the
1826 * fid length may be less than
1827 * NFS_FHMAXDATA, but it was always
1828 * encoded as NFS_FHMAXDATA bytes.
1829 */
1830
1831 sz = sizeof (ushort_t);
1832 bcopy(&fh.fh3_len, &buf[i], sz);
1833 i += sz;
1834 bcopy(fh.fh3_data, &buf[i], fh.fh3_len);
1835 i += fh.fh3_len;
1836 pad = (NFS_FHMAXDATA - fh.fh3_len);
1837 if (pad > 0) {
1838 bzero(&buf[i], pad);
1839 i += pad;
1840 l += pad;
1841 }
1842
1843 sz = sizeof (ushort_t);
1844 bcopy(&fh.fh3_xlen, &buf[i], sz);
1845 i += sz;
1846 bcopy(fh.fh3_xdata, &buf[i], fh.fh3_xlen);
1847 i += fh.fh3_xlen;
1848 pad = (NFS_FHMAXDATA - fh.fh3_xlen);
1849 if (pad > 0) {
1850 bzero(&buf[i], pad);
1851 i += pad;
1852 l += pad;
1853 }
1854 }
1855 /*
1856 * If we need to do NFS logging, the filehandle
1857 * must be downsized to 32 bytes.
1858 */
1859 if (!error && exi->exi_export.ex_flags & EX_LOG) {
1860 i = 0;
1861 sz = sizeof (fsid_t);
1862 bcopy(&fh.fh3_fsid, &logbuf[i], sz);
1863 i += sz;
1864 sz = sizeof (ushort_t);
1865 bcopy(&fh.fh3_len, &logbuf[i], sz);
1866 i += sz;
1867 sz = NFS_FHMAXDATA;
1868 bcopy(fh.fh3_data, &logbuf[i], sz);
1869 i += sz;
1870 sz = sizeof (ushort_t);
1871 bcopy(&fh.fh3_xlen, &logbuf[i], sz);
1872 i += sz;
1873 sz = NFS_FHMAXDATA;
1874 bcopy(fh.fh3_xdata, &logbuf[i], sz);
1875 i += sz;
1876 }
1877 }
1878 if (!error && exi->exi_export.ex_flags & EX_LOG) {
1879 nfslog_getfh(exi, (fhandle_t *)logptr,
1880 STRUCT_FGETP(uap, fname), UIO_USERSPACE, cr);
1881 }
1882 exi_rele(exi);
1883 if (!error) {
1884 if (copyout(&l, STRUCT_FGETP(uap, lenp), sizeof (int)))
1885 error = EFAULT;
1886 if (copyout(buf, STRUCT_FGETP(uap, fhp), l))
1887 error = EFAULT;
1888 }
1889 }
1890 VN_RELE(vp);
1891 if (dvp != NULL) {
1892 VN_RELE(dvp);
1893 }
1894 return (error);
1895 }
1896
1897 /*
1898 * Strategy: if vp is in the export list, then
1899 * return the associated file handle. Otherwise, ".."
1900 * once up the vp and try again, until the root of the
1901 * filesystem is reached.
1902 */
1903 struct exportinfo *
nfs_vptoexi(vnode_t * dvp,vnode_t * vp,cred_t * cr,int * walk,int * err,bool_t v4srv)1904 nfs_vptoexi(vnode_t *dvp, vnode_t *vp, cred_t *cr, int *walk,
1905 int *err, bool_t v4srv)
1906 {
1907 fid_t fid;
1908 int error;
1909 struct exportinfo *exi;
1910
1911 ASSERT(vp);
1912 VN_HOLD(vp);
1913 if (dvp != NULL) {
1914 VN_HOLD(dvp);
1915 }
1916 if (walk != NULL)
1917 *walk = 0;
1918
1919 for (;;) {
1920 bzero(&fid, sizeof (fid));
1921 fid.fid_len = MAXFIDSZ;
1922 error = vop_fid_pseudo(vp, &fid);
1923 if (error) {
1924 /*
1925 * If vop_fid_pseudo returns ENOSPC then the fid
1926 * supplied is too small. For now we simply
1927 * return EREMOTE.
1928 */
1929 if (error == ENOSPC)
1930 error = EREMOTE;
1931 break;
1932 }
1933
1934 exi = checkexport(&vp->v_vfsp->vfs_fsid, &fid,
1935 v4srv ? vp : NULL);
1936 if (exi != NULL) {
1937 /*
1938 * Found the export info
1939 */
1940 break;
1941 }
1942
1943 /*
1944 * We have just failed finding a matching export.
1945 * If we're at the root of this filesystem, then
1946 * it's time to stop (with failure).
1947 */
1948 if (vp->v_flag & VROOT) {
1949 error = EINVAL;
1950 break;
1951 }
1952
1953 if (walk != NULL)
1954 (*walk)++;
1955
1956 /*
1957 * Now, do a ".." up vp. If dvp is supplied, use it,
1958 * otherwise, look it up.
1959 */
1960 if (dvp == NULL) {
1961 error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, cr,
1962 NULL, NULL, NULL);
1963 if (error)
1964 break;
1965 }
1966 VN_RELE(vp);
1967 vp = dvp;
1968 dvp = NULL;
1969 }
1970 VN_RELE(vp);
1971 if (dvp != NULL) {
1972 VN_RELE(dvp);
1973 }
1974 if (error != 0) {
1975 if (err != NULL)
1976 *err = error;
1977 return (NULL);
1978 }
1979 return (exi);
1980 }
1981
1982 int
chk_clnt_sec(exportinfo_t * exi,struct svc_req * req)1983 chk_clnt_sec(exportinfo_t *exi, struct svc_req *req)
1984 {
1985 int i, nfsflavor;
1986 struct secinfo *sp;
1987
1988 /*
1989 * Get the nfs flavor number from xprt.
1990 */
1991 nfsflavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
1992
1993 sp = exi->exi_export.ex_secinfo;
1994 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1995 if ((nfsflavor == sp[i].s_secinfo.sc_nfsnum) &&
1996 SEC_REF_EXPORTED(sp + i))
1997 return (TRUE);
1998 }
1999 return (FALSE);
2000 }
2001
2002 /*
2003 * Make an fhandle from a vnode
2004 */
2005 int
makefh(fhandle_t * fh,vnode_t * vp,exportinfo_t * exi)2006 makefh(fhandle_t *fh, vnode_t *vp, exportinfo_t *exi)
2007 {
2008 int error;
2009
2010 *fh = exi->exi_fh; /* struct copy */
2011
2012 error = VOP_FID(vp, (fid_t *)&fh->fh_len, NULL);
2013 if (error) {
2014 /*
2015 * Should be something other than EREMOTE
2016 */
2017 return (EREMOTE);
2018 }
2019 return (0);
2020 }
2021
2022 /*
2023 * This routine makes an overloaded V2 fhandle which contains
2024 * sec modes.
2025 *
2026 * Note that the first four octets contain the length octet,
2027 * the status octet, and two padded octets to make them XDR
2028 * four-octet aligned.
2029 *
2030 * 1 2 3 4 32
2031 * +---+---+---+---+---+---+---+---+ +---+---+---+---+ +---+
2032 * | l | s | | | sec_1 |...| sec_n |...| |
2033 * +---+---+---+---+---+---+---+---+ +---+---+---+---+ +---+
2034 *
2035 * where
2036 *
2037 * the status octet s indicates whether there are more security
2038 * flavors (1 means yes, 0 means no) that require the client to
2039 * perform another 0x81 LOOKUP to get them,
2040 *
2041 * the length octet l is the length describing the number of
2042 * valid octets that follow. (l = 4 * n, where n is the number
2043 * of security flavors sent in the current overloaded filehandle.)
2044 *
2045 * sec_index should always be in the inclusive range: [1 - ex_seccnt],
2046 * and it tells server where to start within the secinfo array.
2047 * Usually it will always be 1; however, if more flavors are used
2048 * for the public export than can be encoded in the overloaded FH
2049 * (7 for NFS2), subsequent SNEGO MCLs will have a larger index
2050 * so the server will pick up where it left off from the previous
2051 * MCL reply.
2052 *
2053 * With NFS4 support, implicitly allowed flavors are also in
2054 * the secinfo array; however, they should not be returned in
2055 * SNEGO MCL replies.
2056 */
2057 int
makefh_ol(fhandle_t * fh,exportinfo_t * exi,uint_t sec_index)2058 makefh_ol(fhandle_t *fh, exportinfo_t *exi, uint_t sec_index)
2059 {
2060 secinfo_t sec[MAX_FLAVORS];
2061 int totalcnt, i, *ipt, cnt, seccnt, secidx, fh_max_cnt;
2062 char *c;
2063
2064 if (fh == NULL || exi == NULL || sec_index < 1)
2065 return (EREMOTE);
2066
2067 /*
2068 * WebNFS clients need to know the unique set of explicitly
2069 * shared flavors in used for the public export. When
2070 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2071 * shared flavors are included in the list.
2072 */
2073 seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2074 if (sec_index > seccnt)
2075 return (EREMOTE);
2076
2077 fh_max_cnt = (NFS_FHSIZE / sizeof (int)) - 1;
2078 totalcnt = seccnt - sec_index + 1;
2079 cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2080
2081 c = (char *)fh;
2082 /*
2083 * Encode the length octet representing the number of
2084 * security flavors (in bytes) in this overloaded fh.
2085 */
2086 *c = cnt * sizeof (int);
2087
2088 /*
2089 * Encode the status octet that indicates whether there
2090 * are more security flavors the client needs to get.
2091 */
2092 *(c + 1) = totalcnt > fh_max_cnt;
2093
2094 /*
2095 * put security flavors in the overloaded fh
2096 */
2097 ipt = (int *)(c + sizeof (int32_t));
2098 secidx = sec_index - 1;
2099 for (i = 0; i < cnt; i++) {
2100 ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2101 }
2102 return (0);
2103 }
2104
2105 /*
2106 * Make an nfs_fh3 from a vnode
2107 */
2108 int
makefh3(nfs_fh3 * fh,vnode_t * vp,struct exportinfo * exi)2109 makefh3(nfs_fh3 *fh, vnode_t *vp, struct exportinfo *exi)
2110 {
2111 int error;
2112 fid_t fid;
2113
2114 bzero(&fid, sizeof (fid));
2115 fid.fid_len = sizeof (fh->fh3_data);
2116 error = VOP_FID(vp, &fid, NULL);
2117 if (error)
2118 return (EREMOTE);
2119
2120 bzero(fh, sizeof (nfs_fh3));
2121 fh->fh3_fsid = exi->exi_fsid;
2122 fh->fh3_len = fid.fid_len;
2123 bcopy(fid.fid_data, fh->fh3_data, fh->fh3_len);
2124
2125 fh->fh3_xlen = exi->exi_fid.fid_len;
2126 ASSERT(fh->fh3_xlen <= sizeof (fh->fh3_xdata));
2127 bcopy(exi->exi_fid.fid_data, fh->fh3_xdata, fh->fh3_xlen);
2128
2129 fh->fh3_length = sizeof (fh->fh3_fsid)
2130 + sizeof (fh->fh3_len) + fh->fh3_len
2131 + sizeof (fh->fh3_xlen) + fh->fh3_xlen;
2132 fh->fh3_flags = 0;
2133
2134 return (0);
2135 }
2136
2137 /*
2138 * This routine makes an overloaded V3 fhandle which contains
2139 * sec modes.
2140 *
2141 * 1 4
2142 * +--+--+--+--+
2143 * | len |
2144 * +--+--+--+--+
2145 * up to 64
2146 * +--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+
2147 * |s | | | | sec_1 | sec_2 | ... | sec_n |
2148 * +--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+
2149 *
2150 * len = 4 * (n+1), where n is the number of security flavors
2151 * sent in the current overloaded filehandle.
2152 *
2153 * the status octet s indicates whether there are more security
2154 * mechanisms (1 means yes, 0 means no) that require the client
2155 * to perform another 0x81 LOOKUP to get them.
2156 *
2157 * Three octets are padded after the status octet.
2158 */
2159 int
makefh3_ol(nfs_fh3 * fh,struct exportinfo * exi,uint_t sec_index)2160 makefh3_ol(nfs_fh3 *fh, struct exportinfo *exi, uint_t sec_index)
2161 {
2162 secinfo_t sec[MAX_FLAVORS];
2163 int totalcnt, cnt, *ipt, i, seccnt, fh_max_cnt, secidx;
2164 char *c;
2165
2166 if (fh == NULL || exi == NULL || sec_index < 1)
2167 return (EREMOTE);
2168
2169 /*
2170 * WebNFS clients need to know the unique set of explicitly
2171 * shared flavors in used for the public export. When
2172 * "TRUE" is passed to build_seclist_nodups(), only explicitly
2173 * shared flavors are included in the list.
2174 */
2175 seccnt = build_seclist_nodups(&exi->exi_export, sec, TRUE);
2176
2177 if (sec_index > seccnt)
2178 return (EREMOTE);
2179
2180 fh_max_cnt = (NFS3_FHSIZE / sizeof (int)) - 1;
2181 totalcnt = seccnt - sec_index + 1;
2182 cnt = totalcnt > fh_max_cnt ? fh_max_cnt : totalcnt;
2183
2184 /*
2185 * Place the length in fh3_length representing the number
2186 * of security flavors (in bytes) in this overloaded fh.
2187 */
2188 fh->fh3_flags = FH_WEBNFS;
2189 fh->fh3_length = (cnt+1) * sizeof (int32_t);
2190
2191 c = (char *)&fh->fh3_u.nfs_fh3_i.fh3_i;
2192 /*
2193 * Encode the status octet that indicates whether there
2194 * are more security flavors the client needs to get.
2195 */
2196 *c = totalcnt > fh_max_cnt;
2197
2198 /*
2199 * put security flavors in the overloaded fh
2200 */
2201 secidx = sec_index - 1;
2202 ipt = (int *)(c + sizeof (int32_t));
2203 for (i = 0; i < cnt; i++) {
2204 ipt[i] = htonl(sec[i + secidx].s_secinfo.sc_nfsnum);
2205 }
2206 return (0);
2207 }
2208
2209 /*
2210 * Make an nfs_fh4 from a vnode
2211 */
2212 int
makefh4(nfs_fh4 * fh,vnode_t * vp,struct exportinfo * exi)2213 makefh4(nfs_fh4 *fh, vnode_t *vp, struct exportinfo *exi)
2214 {
2215 int error;
2216 nfs_fh4_fmt_t *fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2217 fid_t fid;
2218
2219 bzero(&fid, sizeof (fid));
2220 fid.fid_len = MAXFIDSZ;
2221 /*
2222 * vop_fid_pseudo() is used to set up NFSv4 namespace, so
2223 * use vop_fid_pseudo() here to get the fid instead of VOP_FID.
2224 */
2225 error = vop_fid_pseudo(vp, &fid);
2226 if (error)
2227 return (error);
2228
2229 fh->nfs_fh4_len = NFS_FH4_LEN;
2230
2231 fh_fmtp->fh4_i.fhx_fsid = exi->exi_fh.fh_fsid;
2232 fh_fmtp->fh4_i.fhx_xlen = exi->exi_fh.fh_xlen;
2233
2234 bzero(fh_fmtp->fh4_i.fhx_data, sizeof (fh_fmtp->fh4_i.fhx_data));
2235 bzero(fh_fmtp->fh4_i.fhx_xdata, sizeof (fh_fmtp->fh4_i.fhx_xdata));
2236 ASSERT(exi->exi_fh.fh_xlen <= sizeof (fh_fmtp->fh4_i.fhx_xdata));
2237 bcopy(exi->exi_fh.fh_xdata, fh_fmtp->fh4_i.fhx_xdata,
2238 exi->exi_fh.fh_xlen);
2239
2240 fh_fmtp->fh4_len = fid.fid_len;
2241 ASSERT(fid.fid_len <= sizeof (fh_fmtp->fh4_data));
2242 bcopy(fid.fid_data, fh_fmtp->fh4_data, fid.fid_len);
2243 fh_fmtp->fh4_flag = 0;
2244
2245 #ifdef VOLATILE_FH_TEST
2246 /*
2247 * XXX (temporary?)
2248 * Use the rnode volatile_id value to add volatility to the fh.
2249 *
2250 * For testing purposes there are currently two scenarios, based
2251 * on whether the filesystem was shared with "volatile_fh"
2252 * or "expire_on_rename". In the first case, use the value of
2253 * export struct share_time as the volatile_id. In the second
2254 * case use the vnode volatile_id value (which is set to the
2255 * time in which the file was renamed).
2256 *
2257 * Note that the above are temporary constructs for testing only
2258 * XXX
2259 */
2260 if (exi->exi_export.ex_flags & EX_VOLRNM) {
2261 fh_fmtp->fh4_volatile_id = find_volrnm_fh_id(exi, fh);
2262 } else if (exi->exi_export.ex_flags & EX_VOLFH) {
2263 fh_fmtp->fh4_volatile_id = exi->exi_volatile_id;
2264 } else {
2265 fh_fmtp->fh4_volatile_id = 0;
2266 }
2267 #endif /* VOLATILE_FH_TEST */
2268
2269 return (0);
2270 }
2271
2272 /*
2273 * Convert an fhandle into a vnode.
2274 * Uses the file id (fh_len + fh_data) in the fhandle to get the vnode.
2275 * WARNING: users of this routine must do a VN_RELE on the vnode when they
2276 * are done with it.
2277 */
2278 vnode_t *
nfs_fhtovp(fhandle_t * fh,struct exportinfo * exi)2279 nfs_fhtovp(fhandle_t *fh, struct exportinfo *exi)
2280 {
2281 vfs_t *vfsp;
2282 vnode_t *vp;
2283 int error;
2284 fid_t *fidp;
2285
2286 TRACE_0(TR_FAC_NFS, TR_FHTOVP_START,
2287 "fhtovp_start");
2288
2289 if (exi == NULL) {
2290 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2291 "fhtovp_end:(%S)", "exi NULL");
2292 return (NULL); /* not exported */
2293 }
2294
2295 ASSERT(exi->exi_vp != NULL);
2296
2297 if (PUBLIC_FH2(fh)) {
2298 if (exi->exi_export.ex_flags & EX_PUBLIC) {
2299 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2300 "fhtovp_end:(%S)", "root not exported");
2301 return (NULL);
2302 }
2303 vp = exi->exi_vp;
2304 VN_HOLD(vp);
2305 return (vp);
2306 }
2307
2308 vfsp = exi->exi_vp->v_vfsp;
2309 ASSERT(vfsp != NULL);
2310 fidp = (fid_t *)&fh->fh_len;
2311
2312 error = VFS_VGET(vfsp, &vp, fidp);
2313 if (error || vp == NULL) {
2314 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2315 "fhtovp_end:(%S)", "VFS_GET failed or vp NULL");
2316 return (NULL);
2317 }
2318 TRACE_1(TR_FAC_NFS, TR_FHTOVP_END,
2319 "fhtovp_end:(%S)", "end");
2320 return (vp);
2321 }
2322
2323 /*
2324 * Convert an nfs_fh3 into a vnode.
2325 * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2326 * WARNING: users of this routine must do a VN_RELE on the vnode when they
2327 * are done with it.
2328 */
2329 vnode_t *
nfs3_fhtovp(nfs_fh3 * fh,struct exportinfo * exi)2330 nfs3_fhtovp(nfs_fh3 *fh, struct exportinfo *exi)
2331 {
2332 vfs_t *vfsp;
2333 vnode_t *vp;
2334 int error;
2335 fid_t *fidp;
2336
2337 if (exi == NULL)
2338 return (NULL); /* not exported */
2339
2340 ASSERT(exi->exi_vp != NULL);
2341
2342 if (PUBLIC_FH3(fh)) {
2343 if (exi->exi_export.ex_flags & EX_PUBLIC)
2344 return (NULL);
2345 vp = exi->exi_vp;
2346 VN_HOLD(vp);
2347 return (vp);
2348 }
2349
2350 if (fh->fh3_length < NFS3_OLDFHSIZE ||
2351 fh->fh3_length > NFS3_MAXFHSIZE)
2352 return (NULL);
2353
2354 vfsp = exi->exi_vp->v_vfsp;
2355 ASSERT(vfsp != NULL);
2356 fidp = FH3TOFIDP(fh);
2357
2358 error = VFS_VGET(vfsp, &vp, fidp);
2359 if (error || vp == NULL)
2360 return (NULL);
2361
2362 return (vp);
2363 }
2364
2365 /*
2366 * Convert an nfs_fh4 into a vnode.
2367 * Uses the file id (fh_len + fh_data) in the file handle to get the vnode.
2368 * WARNING: users of this routine must do a VN_RELE on the vnode when they
2369 * are done with it.
2370 */
2371 vnode_t *
nfs4_fhtovp(nfs_fh4 * fh,struct exportinfo * exi,nfsstat4 * statp)2372 nfs4_fhtovp(nfs_fh4 *fh, struct exportinfo *exi, nfsstat4 *statp)
2373 {
2374 vfs_t *vfsp;
2375 vnode_t *vp = NULL;
2376 int error;
2377 fid_t *fidp;
2378 nfs_fh4_fmt_t *fh_fmtp;
2379 #ifdef VOLATILE_FH_TEST
2380 uint32_t volatile_id = 0;
2381 #endif /* VOLATILE_FH_TEST */
2382
2383 if (exi == NULL) {
2384 *statp = NFS4ERR_STALE;
2385 return (NULL); /* not exported */
2386 }
2387 ASSERT(exi->exi_vp != NULL);
2388
2389 /* caller should have checked this */
2390 ASSERT(fh->nfs_fh4_len >= NFS_FH4_LEN);
2391
2392 fh_fmtp = (nfs_fh4_fmt_t *)fh->nfs_fh4_val;
2393 vfsp = exi->exi_vp->v_vfsp;
2394 ASSERT(vfsp != NULL);
2395 fidp = (fid_t *)&fh_fmtp->fh4_len;
2396
2397 #ifdef VOLATILE_FH_TEST
2398 /* XXX check if volatile - should be changed later */
2399 if (exi->exi_export.ex_flags & (EX_VOLRNM | EX_VOLFH)) {
2400 /*
2401 * Filesystem is shared with volatile filehandles
2402 */
2403 if (exi->exi_export.ex_flags & EX_VOLRNM)
2404 volatile_id = find_volrnm_fh_id(exi, fh);
2405 else
2406 volatile_id = exi->exi_volatile_id;
2407
2408 if (fh_fmtp->fh4_volatile_id != volatile_id) {
2409 *statp = NFS4ERR_FHEXPIRED;
2410 return (NULL);
2411 }
2412 }
2413 /*
2414 * XXX even if test_volatile_fh false, the fh may contain a
2415 * volatile id if obtained when the test was set.
2416 */
2417 fh_fmtp->fh4_volatile_id = (uchar_t)0;
2418 #endif /* VOLATILE_FH_TEST */
2419
2420 error = VFS_VGET(vfsp, &vp, fidp);
2421 /*
2422 * If we can not get vp from VFS_VGET, perhaps this is
2423 * an nfs v2/v3/v4 node in an nfsv4 pseudo filesystem.
2424 * Check it out.
2425 */
2426 if (error && PSEUDO(exi))
2427 error = nfs4_vget_pseudo(exi, &vp, fidp);
2428
2429 if (error || vp == NULL) {
2430 *statp = NFS4ERR_STALE;
2431 return (NULL);
2432 }
2433 /* XXX - disgusting hack */
2434 if (vp->v_type == VNON && vp->v_flag & V_XATTRDIR)
2435 vp->v_type = VDIR;
2436 *statp = NFS4_OK;
2437 return (vp);
2438 }
2439
2440 /*
2441 * Find the export structure associated with the given filesystem.
2442 * The exported_lock must be already held by caller.
2443 */
2444 struct exportinfo *
checkexport_nohold(fsid_t * fsid,fid_t * fid,vnode_t * vp)2445 checkexport_nohold(fsid_t *fsid, fid_t *fid, vnode_t *vp)
2446 {
2447 struct exportinfo *exi;
2448
2449 ASSERT(RW_LOCK_HELD(&exported_lock));
2450
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 /*
2466 * If vp is given, check if vp is the
2467 * same vnode as the exported node.
2468 *
2469 * Since VOP_FID of a lofs node returns the
2470 * fid of its real node (ufs), the exported
2471 * node for lofs and (pseudo) ufs may have
2472 * the same fsid and fid.
2473 */
2474 if (vp == NULL || vp == exi->exi_vp) {
2475 return (exi);
2476 }
2477 }
2478 }
2479 return (NULL);
2480 }
2481
2482 /*
2483 * Find the export structure associated with the given filesystem.
2484 * If found, then increment the ref count (exi_count).
2485 */
2486 struct exportinfo *
checkexport(fsid_t * fsid,fid_t * fid,vnode_t * vp)2487 checkexport(fsid_t *fsid, fid_t *fid, vnode_t *vp)
2488 {
2489 struct exportinfo *exi;
2490
2491 rw_enter(&exported_lock, RW_READER);
2492 exi = checkexport_nohold(fsid, fid, vp);
2493 if (exi != NULL)
2494 exi_hold(exi);
2495 rw_exit(&exported_lock);
2496
2497 return (exi);
2498 }
2499
2500 /*
2501 * Free an entire export list node
2502 */
2503 void
exportfree(struct exportinfo * exi)2504 exportfree(struct exportinfo *exi)
2505 {
2506 struct exportdata *ex;
2507 struct charset_cache *cache;
2508 int i;
2509
2510 ex = &exi->exi_export;
2511
2512 ASSERT(exi->exi_vp != NULL && !(exi->exi_export.ex_flags & EX_PUBLIC));
2513 VN_RELE(exi->exi_vp);
2514 if (exi->exi_dvp != NULL)
2515 VN_RELE(exi->exi_dvp);
2516
2517 if (ex->ex_flags & EX_INDEX)
2518 kmem_free(ex->ex_index, strlen(ex->ex_index) + 1);
2519
2520 kmem_free(ex->ex_path, ex->ex_pathlen + 1);
2521 nfsauth_cache_free(exi);
2522
2523 /*
2524 * if there is a character set mapping cached, clean it up.
2525 */
2526 for (cache = exi->exi_charset; cache != NULL;
2527 cache = exi->exi_charset) {
2528 if (cache->inbound != (kiconv_t)-1)
2529 (void) kiconv_close(cache->inbound);
2530 if (cache->outbound != (kiconv_t)-1)
2531 (void) kiconv_close(cache->outbound);
2532 exi->exi_charset = cache->next;
2533 kmem_free(cache, sizeof (struct charset_cache));
2534 }
2535
2536 if (exi->exi_logbuffer != NULL)
2537 nfslog_disable(exi);
2538
2539 if (ex->ex_flags & EX_LOG) {
2540 kmem_free(ex->ex_log_buffer, ex->ex_log_bufferlen + 1);
2541 kmem_free(ex->ex_tag, ex->ex_taglen + 1);
2542 }
2543
2544 if (exi->exi_visible)
2545 free_visible(exi->exi_visible);
2546
2547 srv_secinfo_list_free(ex->ex_secinfo, ex->ex_seccnt);
2548
2549 #ifdef VOLATILE_FH_TEST
2550 free_volrnm_list(exi);
2551 mutex_destroy(&exi->exi_vol_rename_lock);
2552 #endif /* VOLATILE_FH_TEST */
2553
2554 mutex_destroy(&exi->exi_lock);
2555 rw_destroy(&exi->exi_cache_lock);
2556 /*
2557 * All nodes in the exi_cache AVL trees were removed and freed in the
2558 * nfsauth_cache_free() call above. We will just destroy and free the
2559 * empty AVL trees here.
2560 */
2561 for (i = 0; i < AUTH_TABLESIZE; i++) {
2562 avl_destroy(exi->exi_cache[i]);
2563 kmem_free(exi->exi_cache[i], sizeof (avl_tree_t));
2564 }
2565
2566 kmem_free(exi, sizeof (*exi));
2567 }
2568
2569 /*
2570 * load the index file from user space into kernel space.
2571 */
2572 static int
loadindex(struct exportdata * kex)2573 loadindex(struct exportdata *kex)
2574 {
2575 int error;
2576 char index[MAXNAMELEN+1];
2577 size_t len;
2578
2579 /*
2580 * copyinstr copies the complete string including the NULL and
2581 * returns the len with the NULL byte included in the calculation
2582 * as long as the max length is not exceeded.
2583 */
2584 if (error = copyinstr(kex->ex_index, index, sizeof (index), &len))
2585 return (error);
2586
2587 kex->ex_index = kmem_alloc(len, KM_SLEEP);
2588 bcopy(index, kex->ex_index, len);
2589
2590 return (0);
2591 }
2592
2593 void
exi_hold(struct exportinfo * exi)2594 exi_hold(struct exportinfo *exi)
2595 {
2596 mutex_enter(&exi->exi_lock);
2597 exi->exi_count++;
2598 mutex_exit(&exi->exi_lock);
2599 }
2600
2601 /*
2602 * When a thread completes using exi, it should call exi_rele().
2603 * exi_rele() decrements exi_count. It releases exi if exi_count == 0, i.e.
2604 * if this is the last user of exi and exi is not on exportinfo list anymore
2605 */
2606 void
exi_rele(struct exportinfo * exi)2607 exi_rele(struct exportinfo *exi)
2608 {
2609 mutex_enter(&exi->exi_lock);
2610 exi->exi_count--;
2611 if (exi->exi_count == 0) {
2612 mutex_exit(&exi->exi_lock);
2613 exportfree(exi);
2614 } else
2615 mutex_exit(&exi->exi_lock);
2616 }
2617
2618 #ifdef VOLATILE_FH_TEST
2619 /*
2620 * Test for volatile fh's - add file handle to list and set its volatile id
2621 * to time it was renamed. If EX_VOLFH is also on and the fs is reshared,
2622 * the vol_rename queue is purged.
2623 *
2624 * XXX This code is for unit testing purposes only... To correctly use it, it
2625 * needs to tie a rename list to the export struct and (more
2626 * important), protect access to the exi rename list using a write lock.
2627 */
2628
2629 /*
2630 * get the fh vol record if it's in the volatile on rename list. Don't check
2631 * volatile_id in the file handle - compare only the file handles.
2632 */
2633 static struct ex_vol_rename *
find_volrnm_fh(struct exportinfo * exi,nfs_fh4 * fh4p)2634 find_volrnm_fh(struct exportinfo *exi, nfs_fh4 *fh4p)
2635 {
2636 struct ex_vol_rename *p = NULL;
2637 fhandle4_t *fhp;
2638
2639 /* XXX shouldn't we assert &exported_lock held? */
2640 ASSERT(MUTEX_HELD(&exi->exi_vol_rename_lock));
2641
2642 if (fh4p->nfs_fh4_len != NFS_FH4_LEN) {
2643 return (NULL);
2644 }
2645 fhp = &((nfs_fh4_fmt_t *)fh4p->nfs_fh4_val)->fh4_i;
2646 for (p = exi->exi_vol_rename; p != NULL; p = p->vrn_next) {
2647 if (bcmp(fhp, &p->vrn_fh_fmt.fh4_i,
2648 sizeof (fhandle4_t)) == 0)
2649 break;
2650 }
2651 return (p);
2652 }
2653
2654 /*
2655 * get the volatile id for the fh (if there is - else return 0). Ignore the
2656 * volatile_id in the file handle - compare only the file handles.
2657 */
2658 static uint32_t
find_volrnm_fh_id(struct exportinfo * exi,nfs_fh4 * fh4p)2659 find_volrnm_fh_id(struct exportinfo *exi, nfs_fh4 *fh4p)
2660 {
2661 struct ex_vol_rename *p;
2662 uint32_t volatile_id;
2663
2664 mutex_enter(&exi->exi_vol_rename_lock);
2665 p = find_volrnm_fh(exi, fh4p);
2666 volatile_id = (p ? p->vrn_fh_fmt.fh4_volatile_id :
2667 exi->exi_volatile_id);
2668 mutex_exit(&exi->exi_vol_rename_lock);
2669 return (volatile_id);
2670 }
2671
2672 /*
2673 * Free the volatile on rename list - will be called if a filesystem is
2674 * unshared or reshared without EX_VOLRNM
2675 */
2676 static void
free_volrnm_list(struct exportinfo * exi)2677 free_volrnm_list(struct exportinfo *exi)
2678 {
2679 struct ex_vol_rename *p, *pnext;
2680
2681 /* no need to hold mutex lock - this one is called from exportfree */
2682 for (p = exi->exi_vol_rename; p != NULL; p = pnext) {
2683 pnext = p->vrn_next;
2684 kmem_free(p, sizeof (*p));
2685 }
2686 exi->exi_vol_rename = NULL;
2687 }
2688
2689 /*
2690 * Add a file handle to the volatile on rename list.
2691 */
2692 void
add_volrnm_fh(struct exportinfo * exi,vnode_t * vp)2693 add_volrnm_fh(struct exportinfo *exi, vnode_t *vp)
2694 {
2695 struct ex_vol_rename *p;
2696 char fhbuf[NFS4_FHSIZE];
2697 nfs_fh4 fh4;
2698 int error;
2699
2700 fh4.nfs_fh4_val = fhbuf;
2701 error = makefh4(&fh4, vp, exi);
2702 if ((error) || (fh4.nfs_fh4_len != sizeof (p->vrn_fh_fmt))) {
2703 return;
2704 }
2705
2706 mutex_enter(&exi->exi_vol_rename_lock);
2707
2708 p = find_volrnm_fh(exi, &fh4);
2709
2710 if (p == NULL) {
2711 p = kmem_alloc(sizeof (*p), KM_SLEEP);
2712 bcopy(fh4.nfs_fh4_val, &p->vrn_fh_fmt, sizeof (p->vrn_fh_fmt));
2713 p->vrn_next = exi->exi_vol_rename;
2714 exi->exi_vol_rename = p;
2715 }
2716
2717 p->vrn_fh_fmt.fh4_volatile_id = gethrestime_sec();
2718 mutex_exit(&exi->exi_vol_rename_lock);
2719 }
2720
2721 #endif /* VOLATILE_FH_TEST */
2722