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