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 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 22 */ 23 24 #include <sys/systm.h> 25 26 #include <nfs/nfs.h> 27 #include <nfs/export.h> 28 #include <sys/cmn_err.h> 29 30 #define PSEUDOFS_SUFFIX " (pseudo)" 31 32 /* 33 * A version of VOP_FID that deals with a remote VOP_FID for nfs. 34 * If vp is an nfs node, nfs4_fid() returns EREMOTE, nfs3_fid() and nfs_fid() 35 * returns the filehandle of vp as its fid. When nfs uses fid to set the 36 * exportinfo filehandle template, a remote nfs filehandle would be too big for 37 * the fid of the exported directory. This routine remaps the value of the 38 * attribute va_nodeid of vp to be the fid of vp, so that the fid can fit. 39 * 40 * We need this fid mainly for setting up NFSv4 server namespace where an 41 * nfs filesystem is also part of it. Thus, need to be able to setup a pseudo 42 * exportinfo for an nfs node. 43 * 44 * e.g. mount a filesystem on top of a nfs dir, and then share the new mount 45 * (like exporting a local disk from a "diskless" client) 46 */ 47 int 48 vop_fid_pseudo(vnode_t *vp, fid_t *fidp) 49 { 50 struct vattr va; 51 int error; 52 53 error = VOP_FID(vp, fidp, NULL); 54 55 /* 56 * XXX nfs4_fid() does nothing and returns EREMOTE. 57 * XXX nfs3_fid()/nfs_fid() returns nfs filehandle as its fid 58 * which has a bigger length than local fid. 59 * NFS_FH4MAXDATA is the size of 60 * fhandle4_t.fh_xdata[NFS_FH4MAXDATA]. 61 * 62 * Note: nfs[2,3,4]_fid() only gets called for diskless clients. 63 */ 64 if (error == EREMOTE || 65 (error == 0 && fidp->fid_len > NFS_FH4MAXDATA)) { 66 67 va.va_mask = AT_NODEID; 68 error = VOP_GETATTR(vp, &va, 0, CRED(), NULL); 69 if (error) 70 return (error); 71 72 fidp->fid_len = sizeof (va.va_nodeid); 73 bcopy(&va.va_nodeid, fidp->fid_data, fidp->fid_len); 74 return (0); 75 } 76 77 return (error); 78 } 79 80 /* 81 * Get an nfsv4 vnode of the given fid from the visible list of an 82 * nfs filesystem or get the exi_vp if it is the root node. 83 */ 84 int 85 nfs4_vget_pseudo(struct exportinfo *exi, vnode_t **vpp, fid_t *fidp) 86 { 87 fid_t exp_fid; 88 struct exp_visible *visp; 89 int error; 90 91 /* check if the given fid is in the visible list */ 92 93 for (visp = exi->exi_visible; visp; visp = visp->vis_next) { 94 if (EQFID(fidp, &visp->vis_fid)) { 95 VN_HOLD(visp->vis_vp); 96 *vpp = visp->vis_vp; 97 return (0); 98 } 99 } 100 101 /* check if the given fid is the same as the exported node */ 102 103 bzero(&exp_fid, sizeof (exp_fid)); 104 exp_fid.fid_len = MAXFIDSZ; 105 error = vop_fid_pseudo(exi->exi_vp, &exp_fid); 106 if (error) 107 return (error); 108 109 if (EQFID(fidp, &exp_fid)) { 110 VN_HOLD(exi->exi_vp); 111 *vpp = exi->exi_vp; 112 return (0); 113 } 114 115 return (ENOENT); 116 } 117 118 /* 119 * Create a pseudo export entry 120 * 121 * This is an export entry that's created as the 122 * side-effect of a "real" export. As a part of 123 * a real export, the pathname to the export is 124 * checked to see if all the directory components 125 * are accessible via an NFSv4 client, i.e. are 126 * exported. If treeclimb_export() finds an unexported 127 * mountpoint along the path, then it calls this 128 * function to export it. 129 * 130 * This pseudo export differs from a real export in that 131 * it only allows read-only access. A "visible" list of 132 * directories is added to filter lookup and readdir results 133 * to only contain dirnames which lead to descendant shares. 134 * 135 * A visible list has a per-file-system scope. Any exportinfo 136 * struct (real or pseudo) can have a visible list as long as 137 * a) its export root is VROOT 138 * b) a descendant of the export root is shared 139 */ 140 int 141 pseudo_exportfs(vnode_t *vp, struct exp_visible *vis_head, 142 struct exportdata *exdata, struct exportinfo **exi_retp) 143 { 144 struct exportinfo *exi; 145 struct exportdata *kex; 146 fid_t fid; 147 fsid_t fsid; 148 int error, vpathlen; 149 150 ASSERT(RW_WRITE_HELD(&exported_lock)); 151 152 /* 153 * Get the vfs id 154 */ 155 bzero(&fid, sizeof (fid)); 156 fid.fid_len = MAXFIDSZ; 157 error = vop_fid_pseudo(vp, &fid); 158 if (error) { 159 /* 160 * If VOP_FID returns ENOSPC then the fid supplied 161 * is too small. For now we simply return EREMOTE. 162 */ 163 if (error == ENOSPC) 164 error = EREMOTE; 165 return (error); 166 } 167 168 fsid = vp->v_vfsp->vfs_fsid; 169 exi = kmem_zalloc(sizeof (*exi), KM_SLEEP); 170 exi->exi_fsid = fsid; 171 exi->exi_fid = fid; 172 exi->exi_vp = vp; 173 VN_HOLD(exi->exi_vp); 174 exi->exi_visible = vis_head; 175 exi->exi_count = 1; 176 exi->exi_volatile_dev = (vfssw[vp->v_vfsp->vfs_fstype].vsw_flag & 177 VSW_VOLATILEDEV) ? 1 : 0; 178 mutex_init(&exi->exi_lock, NULL, MUTEX_DEFAULT, NULL); 179 180 /* 181 * Build up the template fhandle 182 */ 183 exi->exi_fh.fh_fsid = fsid; 184 ASSERT(exi->exi_fid.fid_len <= sizeof (exi->exi_fh.fh_xdata)); 185 exi->exi_fh.fh_xlen = exi->exi_fid.fid_len; 186 bcopy(exi->exi_fid.fid_data, exi->exi_fh.fh_xdata, 187 exi->exi_fid.fid_len); 188 exi->exi_fh.fh_len = sizeof (exi->exi_fh.fh_data); 189 190 kex = &exi->exi_export; 191 kex->ex_flags = EX_PSEUDO; 192 193 vpathlen = vp->v_path ? strlen(vp->v_path) : 0; 194 kex->ex_pathlen = vpathlen + strlen(PSEUDOFS_SUFFIX); 195 kex->ex_path = kmem_alloc(kex->ex_pathlen + 1, KM_SLEEP); 196 197 if (vpathlen) 198 (void) strcpy(kex->ex_path, vp->v_path); 199 (void) strcpy(kex->ex_path + vpathlen, PSEUDOFS_SUFFIX); 200 201 /* Transfer the secinfo data from exdata to this new pseudo node */ 202 if (exdata) 203 srv_secinfo_exp2pseu(&exi->exi_export, exdata); 204 205 /* 206 * Initialize auth cache lock 207 */ 208 rw_init(&exi->exi_cache_lock, NULL, RW_DEFAULT, NULL); 209 210 /* 211 * Insert the new entry at the front of the export list 212 */ 213 export_link(exi); 214 215 /* 216 * If exi_retp is non-NULL return a pointer to the new 217 * exportinfo structure. 218 */ 219 if (exi_retp) 220 *exi_retp = exi; 221 222 return (0); 223 } 224 225 /* 226 * Free a list of visible directories 227 */ 228 void 229 free_visible(struct exp_visible *head) 230 { 231 struct exp_visible *visp, *next; 232 233 for (visp = head; visp; visp = next) { 234 if (visp->vis_vp != NULL) 235 VN_RELE(visp->vis_vp); 236 237 next = visp->vis_next; 238 srv_secinfo_list_free(visp->vis_secinfo, visp->vis_seccnt); 239 kmem_free(visp, sizeof (*visp)); 240 } 241 } 242 243 /* 244 * Connects newchild (or subtree with newchild in head) 245 * to the parent node. We always add it to the beginning 246 * of sibling list. 247 */ 248 static void 249 tree_add_child(treenode_t *parent, treenode_t *newchild) 250 { 251 newchild->tree_parent = parent; 252 newchild->tree_sibling = parent->tree_child_first; 253 parent->tree_child_first = newchild; 254 } 255 256 /* Look up among direct children a node with the exact tree_vis pointer */ 257 static treenode_t * 258 tree_find_child_by_vis(treenode_t *t, exp_visible_t *vis) 259 { 260 for (t = t->tree_child_first; t; t = t->tree_sibling) 261 if (t->tree_vis == vis) 262 return (t); 263 return (NULL); 264 } 265 266 /* 267 * Add new node to the head of subtree pointed by 'n'. n can be NULL. 268 * Interconnects the new treenode with exp_visible and exportinfo 269 * if needed. 270 */ 271 static treenode_t * 272 tree_prepend_node(treenode_t *n, exp_visible_t *v, exportinfo_t *e) 273 { 274 treenode_t *tnode = kmem_zalloc(sizeof (*tnode), KM_SLEEP); 275 276 if (n) { 277 tnode->tree_child_first = n; 278 n->tree_parent = tnode; 279 } 280 if (v) { 281 tnode->tree_vis = v; 282 } 283 if (e) { 284 tnode->tree_exi = e; 285 e->exi_tree = tnode; 286 } 287 return (tnode); 288 } 289 290 /* 291 * Removes node from the tree and frees the treenode struct. 292 * Does not free structures pointed by tree_exi and tree_vis, 293 * they should be already freed. 294 */ 295 static void 296 tree_remove_node(treenode_t *node) 297 { 298 treenode_t *parent = node->tree_parent; 299 treenode_t *s; /* s for sibling */ 300 301 if (parent == NULL) { 302 kmem_free(node, sizeof (*node)); 303 ns_root = NULL; 304 return; 305 } 306 /* This node is first child */ 307 if (parent->tree_child_first == node) { 308 parent->tree_child_first = node->tree_sibling; 309 /* This node is not first child */ 310 } else { 311 s = parent->tree_child_first; 312 while (s->tree_sibling != node) 313 s = s->tree_sibling; 314 s->tree_sibling = s->tree_sibling->tree_sibling; 315 } 316 kmem_free(node, sizeof (*node)); 317 } 318 319 /* 320 * When we export a new directory we need to add a new 321 * path segment through the pseudofs to reach the new 322 * directory. This new path is reflected in a list of 323 * directories added to the "visible" list. 324 * 325 * Here there are two lists of visible fids: one hanging off the 326 * pseudo exportinfo, and the one we want to add. It's possible 327 * that the two lists share a common path segment 328 * and have some common directories. We need to combine 329 * the lists so there's no duplicate entries. Where a common 330 * path component is found, the vis_count field is bumped. 331 * 332 * This example shows that the treenode chain (tree_head) and 333 * exp_visible chain (vis_head) can differ in length. The latter 334 * can be shorter. The outer loop must loop over the vis_head chain. 335 * 336 * share /x/a 337 * mount -F ufs /dev/dsk/... /x/y 338 * mkdir -p /x/y/a/b 339 * share /x/y/a/b 340 * 341 * When more_visible() is called during the second share, 342 * the existing namespace is folowing: 343 * exp_visible_t 344 * treenode_t exportinfo_t v0 v1 345 * ns_root+---+ +------------+ +---+ +---+ 346 * t0| / |........| E0 pseudo |->| x |->| a | 347 * +---+ +------------+ +---+ +---+ 348 * | / / 349 * +---+ / / 350 * t1| x |------------------------ / 351 * +---+ / 352 * | / 353 * +---+ / 354 * t2| a |------------------------- 355 * +---+........+------------+ 356 * | E1 real | 357 * +------------+ 358 * 359 * This is being added: 360 * 361 * tree_head vis_head 362 * +---+ +---+ 363 * t3| x |->| x |v2 364 * +---+ +---+ 365 * | | 366 * +---+ +---+ v4 v5 367 * t4| y |->| y |v3 +------------+ +---+ +---+ 368 * +---+\ +---+ | E2 pseudo |->| a |->| b | 369 * | \....... >+------------+ +---+ +---+ 370 * +---+ / / 371 * t5| a |--------------------------- / 372 * +---+ / 373 * | / 374 * +---+------------------------------- 375 * t6| b | +------------+ 376 * +---+..........>| E3 real | 377 * +------------+ 378 * 379 * more_visible() will: 380 * - kmem_free() t3 and v2 381 * - add t4, t5, t6 as a child of t1 (t4 will become sibling of t2) 382 * - add v3 to the end of E0->exi_visible 383 * 384 * Note that v4 and v5 were already proccesed in pseudo_exportfs() and 385 * added to E2. The outer loop of more_visible() will loop only over v2 386 * and v3. The inner loop of more_visible() always loops over v0 and v1. 387 * 388 * Illustration for this scenario: 389 * 390 * mkdir -p /v/a/b/c 391 * share /v/a/b/c 392 * mkdir /v/a/b/c1 393 * mkdir -p /v/a1 394 * mv /v/a/b /v/a1 395 * share /v/a1/b/c1 396 * 397 * EXISTING 398 * treenode 399 * namespace: +-----------+ visibles 400 * |exportinfo |-->v->a->b->c 401 * connect_point->+---+--->+-----------+ 402 * | / |T0 403 * +---+ 404 * | NEW treenode chain: 405 * child->+---+ 406 * | v |T1 +---+<-curr 407 * +---+ N1| v | 408 * | +---+ 409 * +---+ | 410 * | a |T2 +---+<-tree_head 411 * +---+ N2| a1| 412 * | +---+ 413 * +---+ | 414 * | b |T3 +---+ 415 * +---+ N3| b | 416 * | +---+ 417 * +---+ | 418 * | c |T4 +---+ 419 * +---+ N4| c1| 420 * +---+ 421 * 422 * The picture above illustrates the position of following pointers after line 423 * 'child = tree_find_child_by_vis(connect_point, curr->tree_vis);' 424 * was executed for the first time in the outer 'for' loop: 425 * 426 * connect_point..parent treenode in the EXISTING namespace to which the 'curr' 427 * should be connected. If 'connect_point' already has a child 428 * with the same value of tree_vis as the curr->tree_vis is, 429 * the 'curr' will not be added, but kmem_free()d. 430 * child..........the result of tree_find_child_by_vis() 431 * curr...........currently processed treenode from the NEW treenode chain 432 * tree_head......current head of the NEW treenode chain, in this case it was 433 * already moved down to its child - preparation for another loop 434 * 435 * What will happen to NEW treenodes N1, N2, N3, N4 in more_visible() later: 436 * 437 * N1: is merged - i.e. N1 is kmem_free()d. T0 has a child T1 with the same 438 * tree_vis as N1 439 * N2: is added as a new child of T1 440 * Note: not just N2, but the whole chain N2->N3->N4 is added 441 * N3: not processed separately (it was added together with N2) 442 * Even that N3 and T3 have same tree_vis, they are NOT merged, but will 443 * become duplicates. 444 * N4: not processed separately 445 */ 446 static void 447 more_visible(struct exportinfo *exi, treenode_t *tree_head) 448 { 449 struct exp_visible *vp1, *vp2, *vis_head, *tail, *next; 450 int found; 451 treenode_t *child, *curr, *connect_point; 452 453 vis_head = tree_head->tree_vis; 454 connect_point = exi->exi_tree; 455 456 /* 457 * If exportinfo doesn't already have a visible 458 * list just assign the entire supplied list. 459 */ 460 if (exi->exi_visible == NULL) { 461 tree_add_child(exi->exi_tree, tree_head); 462 exi->exi_visible = vis_head; 463 return; 464 } 465 466 /* The outer loop traverses the supplied list. */ 467 for (vp1 = vis_head; vp1; vp1 = next) { 468 found = 0; 469 next = vp1->vis_next; 470 471 /* The inner loop searches the exportinfo visible list. */ 472 for (vp2 = exi->exi_visible; vp2; vp2 = vp2->vis_next) { 473 tail = vp2; 474 if (EQFID(&vp1->vis_fid, &vp2->vis_fid)) { 475 found = 1; 476 vp2->vis_count++; 477 VN_RELE(vp1->vis_vp); 478 /* Transfer vis_exported from vp1 to vp2. */ 479 if (vp1->vis_exported && !vp2->vis_exported) 480 vp2->vis_exported = 1; 481 kmem_free(vp1, sizeof (*vp1)); 482 tree_head->tree_vis = vp2; 483 break; 484 } 485 } 486 487 /* If not found - add to the end of the list */ 488 if (! found) { 489 tail->vis_next = vp1; 490 vp1->vis_next = NULL; 491 } 492 493 curr = tree_head; 494 tree_head = tree_head->tree_child_first; 495 496 if (! connect_point) /* No longer merging */ 497 continue; 498 /* 499 * The inner loop could set curr->tree_vis to the EXISTING 500 * exp_visible vp2, so we can search among the children of 501 * connect_point for the curr->tree_vis. No need for EQFID. 502 */ 503 child = tree_find_child_by_vis(connect_point, curr->tree_vis); 504 505 /* 506 * Merging cannot be done if a valid child->tree_exi would 507 * be overwritten by a new curr->tree_exi. 508 */ 509 if (child && 510 (child->tree_exi == NULL || curr->tree_exi == NULL)) { 511 if (curr->tree_exi) { /* Transfer the exportinfo */ 512 child->tree_exi = curr->tree_exi; 513 child->tree_exi->exi_tree = child; 514 } 515 kmem_free(curr, sizeof (treenode_t)); 516 connect_point = child; 517 } else { /* Branching */ 518 tree_add_child(connect_point, curr); 519 connect_point = NULL; 520 } 521 } 522 } 523 524 /* 525 * Remove one visible entry from the pseudo exportfs. 526 * 527 * When we unexport a directory, we have to remove path 528 * components from the visible list in the pseudo exportfs 529 * entry. The supplied visible contains one fid of one path 530 * component. The visible list of the export 531 * is checked against provided visible, matching fid has its 532 * reference count decremented. If a reference count drops to 533 * zero, then it means no paths now use this directory, so its 534 * fid can be removed from the visible list. 535 * 536 * When the last path is removed, the visible list will be null. 537 */ 538 static void 539 less_visible(struct exportinfo *exi, struct exp_visible *vp1) 540 { 541 struct exp_visible *vp2; 542 struct exp_visible *prev, *next; 543 544 for (vp2 = exi->exi_visible, prev = NULL; vp2; vp2 = next) { 545 546 next = vp2->vis_next; 547 548 if (vp1 == vp2) { 549 /* 550 * Decrement the ref count. 551 * Remove the entry if it's zero. 552 */ 553 if (--vp2->vis_count <= 0) { 554 if (prev == NULL) 555 exi->exi_visible = next; 556 else 557 prev->vis_next = next; 558 VN_RELE(vp2->vis_vp); 559 srv_secinfo_list_free(vp2->vis_secinfo, 560 vp2->vis_seccnt); 561 kmem_free(vp2, sizeof (*vp1)); 562 } 563 break; 564 } 565 prev = vp2; 566 } 567 } 568 569 /* 570 * This function checks the path to a new export to 571 * check whether all the pathname components are 572 * exported. It works by climbing the file tree one 573 * component at a time via "..", crossing mountpoints 574 * if necessary until an export entry is found, or the 575 * system root is reached. 576 * 577 * If an unexported mountpoint is found, then 578 * a new pseudo export is added and the pathname from 579 * the mountpoint down to the export is added to the 580 * visible list for the new pseudo export. If an existing 581 * pseudo export is found, then the pathname is added 582 * to its visible list. 583 * 584 * Note that there's some tests for exportdir. 585 * The exportinfo entry that's passed as a parameter 586 * is that of the real export and exportdir is set 587 * for this case. 588 * 589 * Here is an example of a possible setup: 590 * 591 * () - a new fs; fs mount point 592 * EXPORT - a real exported node 593 * PSEUDO - a pseudo node 594 * vis - visible list 595 * f# - security flavor# 596 * (f#) - security flavor# propagated from its descendents 597 * "" - covered vnode 598 * 599 * 600 * / 601 * | 602 * (a) PSEUDO (f1,f2) 603 * | vis: b,b,"c","n" 604 * | 605 * b 606 * ---------|------------------ 607 * | | 608 * (c) EXPORT,f1(f2) (n) PSEUDO (f1,f2) 609 * | vis: "e","d" | vis: m,m,,p,q,"o" 610 * | | 611 * ------------------ ------------------- 612 * | | | | | 613 * (d) (e) f m EXPORT,f1(f2) p 614 * EXPORT EXPORT | | 615 * f1 f2 | | 616 * | | | 617 * j (o) EXPORT,f2 q EXPORT f2 618 * 619 */ 620 int 621 treeclimb_export(struct exportinfo *exip) 622 { 623 vnode_t *dvp, *vp; 624 fid_t fid; 625 int error; 626 int exportdir; 627 struct exportinfo *exi = NULL; 628 struct exportinfo *new_exi = exip; 629 struct exp_visible *visp; 630 struct exp_visible *vis_head = NULL; 631 struct vattr va; 632 treenode_t *tree_head = NULL; 633 634 ASSERT(RW_WRITE_HELD(&exported_lock)); 635 636 vp = exip->exi_vp; 637 VN_HOLD(vp); 638 exportdir = 1; 639 640 for (;;) { 641 642 bzero(&fid, sizeof (fid)); 643 fid.fid_len = MAXFIDSZ; 644 error = vop_fid_pseudo(vp, &fid); 645 if (error) 646 break; 647 648 if (! exportdir) { 649 /* 650 * Check if this exportroot is a VROOT dir. If so, 651 * then attach the pseudonodes. If not, then 652 * continue .. traversal until we hit a VROOT 653 * export (pseudo or real). 654 */ 655 exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp); 656 if (exi != NULL && vp->v_flag & VROOT) { 657 /* 658 * Found an export info 659 * 660 * Extend the list of visible 661 * directories whether it's a pseudo 662 * or a real export. 663 */ 664 more_visible(exi, tree_head); 665 break; /* and climb no further */ 666 } 667 } 668 669 /* 670 * If at the root of the filesystem, need 671 * to traverse across the mountpoint 672 * and continue the climb on the mounted-on 673 * filesystem. 674 */ 675 if (vp->v_flag & VROOT) { 676 677 if (! exportdir) { 678 /* 679 * Found the root directory of a filesystem 680 * that isn't exported. Need to export 681 * this as a pseudo export so that an NFS v4 682 * client can do lookups in it. 683 */ 684 error = pseudo_exportfs(vp, vis_head, NULL, 685 &new_exi); 686 if (error) 687 break; 688 vis_head = NULL; 689 } 690 691 if (VN_CMP(vp, rootdir)) { 692 /* at system root */ 693 /* 694 * If sharing "/", new_exi is shared exportinfo 695 * (exip). Otherwise, new_exi is exportinfo 696 * created in pseudo_exportfs() above. 697 */ 698 ns_root = tree_prepend_node(tree_head, 0, 699 new_exi); 700 break; 701 } 702 703 vp = untraverse(vp); 704 exportdir = 0; 705 continue; 706 } 707 708 /* 709 * Do a getattr to obtain the nodeid (inode num) 710 * for this vnode. 711 */ 712 va.va_mask = AT_NODEID; 713 error = VOP_GETATTR(vp, &va, 0, CRED(), NULL); 714 if (error) 715 break; 716 717 /* 718 * Add this directory fid to visible list 719 */ 720 visp = kmem_alloc(sizeof (*visp), KM_SLEEP); 721 VN_HOLD(vp); 722 visp->vis_vp = vp; 723 visp->vis_fid = fid; /* structure copy */ 724 visp->vis_ino = va.va_nodeid; 725 visp->vis_count = 1; 726 visp->vis_exported = exportdir; 727 visp->vis_secinfo = NULL; 728 visp->vis_seccnt = 0; 729 visp->vis_next = vis_head; 730 vis_head = visp; 731 732 733 /* 734 * Will set treenode's pointer to exportinfo to 735 * 1. shared exportinfo (exip) - if first visit here 736 * 2. freshly allocated pseudo export (if any) 737 * 3. null otherwise 738 */ 739 tree_head = tree_prepend_node(tree_head, visp, new_exi); 740 new_exi = NULL; 741 742 /* 743 * Now, do a ".." to find parent dir of vp. 744 */ 745 error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, CRED(), 746 NULL, NULL, NULL); 747 748 if (error == ENOTDIR && exportdir) { 749 dvp = exip->exi_dvp; 750 ASSERT(dvp != NULL); 751 VN_HOLD(dvp); 752 error = 0; 753 } 754 755 if (error) 756 break; 757 758 exportdir = 0; 759 VN_RELE(vp); 760 vp = dvp; 761 } 762 763 VN_RELE(vp); 764 765 /* 766 * We can have set error due to error in: 767 * 1. vop_fid_pseudo() 768 * 2. pseudo_exportfs() which can fail only in vop_fid_pseudo() 769 * 3. VOP_GETATTR() 770 * 4. VOP_LOOKUP() 771 * We must free pseudo exportinfos, visibles and treenodes. 772 * Visibles are referenced from treenode_t::tree_vis and 773 * exportinfo_t::exi_visible. To avoid double freeing, only 774 * exi_visible pointer is used, via exi_rele(), for the clean-up. 775 */ 776 if (error) { 777 /* Free unconnected visibles, if there are any. */ 778 if (vis_head) 779 free_visible(vis_head); 780 781 /* Connect unconnected exportinfo, if there is any. */ 782 if (new_exi && new_exi != exip) 783 tree_head = tree_prepend_node(tree_head, 0, new_exi); 784 785 while (tree_head) { 786 treenode_t *t2 = tree_head; 787 exportinfo_t *e = tree_head->tree_exi; 788 /* exip will be freed in exportfs() */ 789 if (e && e != exip) { 790 (void) export_unlink(&e->exi_fsid, &e->exi_fid, 791 e->exi_vp, NULL); 792 exi_rele(e); 793 } 794 tree_head = tree_head->tree_child_first; 795 kmem_free(t2, sizeof (*t2)); 796 } 797 } 798 799 return (error); 800 } 801 802 /* 803 * Walk up the tree and: 804 * 1. release pseudo exportinfo if it has no child 805 * 2. release visible in parent's exportinfo 806 * 3. delete non-exported leaf nodes from tree 807 * 808 * Deleting of nodes will start only if the unshared 809 * node was a leaf node. 810 * Deleting of nodes will finish when we reach a node which 811 * has children or is a real export, then we might still need 812 * to continue releasing visibles, until we reach VROOT node. 813 */ 814 void 815 treeclimb_unexport(struct exportinfo *exip) 816 { 817 struct exportinfo *exi; 818 treenode_t *tnode, *old_nd; 819 820 ASSERT(RW_WRITE_HELD(&exported_lock)); 821 822 tnode = exip->exi_tree; 823 /* 824 * The unshared exportinfo was unlinked in unexport(). 825 * Zeroing tree_exi ensures that we will skip it. 826 */ 827 tnode->tree_exi = NULL; 828 829 if (tnode->tree_vis) /* system root has tree_vis == NULL */ 830 tnode->tree_vis->vis_exported = 0; 831 832 while (tnode) { 833 834 /* Stop at VROOT node which is exported or has child */ 835 if (TREE_ROOT(tnode) && 836 (TREE_EXPORTED(tnode) || tnode->tree_child_first)) 837 break; 838 839 /* Release pseudo export if it has no child */ 840 if (TREE_ROOT(tnode) && !TREE_EXPORTED(tnode) && 841 tnode->tree_child_first == 0) { 842 exi = tnode->tree_exi; 843 (void) export_unlink(&exi->exi_fsid, &exi->exi_fid, 844 exi->exi_vp, NULL); 845 exi_rele(tnode->tree_exi); 846 } 847 848 /* Release visible in parent's exportinfo */ 849 if (tnode->tree_vis) { 850 exi = vis2exi(tnode); 851 less_visible(exi, tnode->tree_vis); 852 } 853 854 /* Continue with parent */ 855 old_nd = tnode; 856 tnode = tnode->tree_parent; 857 858 /* Remove itself, if this is a leaf and non-exported node */ 859 if (old_nd->tree_child_first == NULL && !TREE_EXPORTED(old_nd)) 860 tree_remove_node(old_nd); 861 } 862 } 863 864 /* 865 * Traverse backward across mountpoint from the 866 * root vnode of a filesystem to its mounted-on 867 * vnode. 868 */ 869 vnode_t * 870 untraverse(vnode_t *vp) 871 { 872 vnode_t *tvp, *nextvp; 873 874 tvp = vp; 875 for (;;) { 876 if (! (tvp->v_flag & VROOT)) 877 break; 878 879 /* lock vfs to prevent unmount of this vfs */ 880 vfs_lock_wait(tvp->v_vfsp); 881 882 if ((nextvp = tvp->v_vfsp->vfs_vnodecovered) == NULL) { 883 vfs_unlock(tvp->v_vfsp); 884 break; 885 } 886 887 /* 888 * Hold nextvp to prevent unmount. After unlock vfs and 889 * rele tvp, any number of overlays could be unmounted. 890 * Putting a hold on vfs_vnodecovered will only allow 891 * tvp's vfs to be unmounted. Of course if caller placed 892 * extra hold on vp before calling untraverse, the following 893 * hold would not be needed. Since prev actions of caller 894 * are unknown, we need to hold here just to be safe. 895 */ 896 VN_HOLD(nextvp); 897 vfs_unlock(tvp->v_vfsp); 898 VN_RELE(tvp); 899 tvp = nextvp; 900 } 901 902 return (tvp); 903 } 904 905 /* 906 * Given an exportinfo, climb up to find the exportinfo for the VROOT 907 * of the filesystem. 908 * 909 * e.g. / 910 * | 911 * a (VROOT) pseudo-exportinfo 912 * | 913 * b 914 * | 915 * c #share /a/b/c 916 * | 917 * d 918 * 919 * where c is in the same filesystem as a. 920 * So, get_root_export(*exportinfo_for_c) returns exportinfo_for_a 921 * 922 * If d is shared, then c will be put into a's visible list. 923 * Note: visible list is per filesystem and is attached to the 924 * VROOT exportinfo. 925 */ 926 struct exportinfo * 927 get_root_export(struct exportinfo *exip) 928 { 929 treenode_t *tnode = exip->exi_tree; 930 exportinfo_t *exi = NULL; 931 932 while (tnode) { 933 if (TREE_ROOT(tnode)) { 934 exi = tnode->tree_exi; 935 break; 936 } 937 tnode = tnode->tree_parent; 938 } 939 ASSERT(exi); 940 return (exi); 941 } 942 943 /* 944 * Return true if the supplied vnode has a sub-directory exported. 945 */ 946 int 947 has_visible(struct exportinfo *exi, vnode_t *vp) 948 { 949 struct exp_visible *visp; 950 fid_t fid; 951 bool_t vp_is_exported; 952 953 vp_is_exported = VN_CMP(vp, exi->exi_vp); 954 955 /* 956 * An exported root vnode has a sub-dir shared if it has a visible list. 957 * i.e. if it does not have a visible list, then there is no node in 958 * this filesystem leads to any other shared node. 959 */ 960 if (vp_is_exported && (vp->v_flag & VROOT)) 961 return (exi->exi_visible ? 1 : 0); 962 963 /* 964 * Only the exportinfo of a fs root node may have a visible list. 965 * Either it is a pseudo root node, or a real exported root node. 966 */ 967 exi = get_root_export(exi); 968 969 if (!exi->exi_visible) 970 return (0); 971 972 /* Get the fid of the vnode */ 973 bzero(&fid, sizeof (fid)); 974 fid.fid_len = MAXFIDSZ; 975 if (vop_fid_pseudo(vp, &fid) != 0) { 976 return (0); 977 } 978 979 /* 980 * See if vp is in the visible list of the root node exportinfo. 981 */ 982 for (visp = exi->exi_visible; visp; visp = visp->vis_next) { 983 if (EQFID(&fid, &visp->vis_fid)) { 984 /* 985 * If vp is an exported non-root node with only 1 path 986 * count (for itself), it indicates no sub-dir shared 987 * using this vp as a path. 988 */ 989 if (vp_is_exported && visp->vis_count < 2) 990 break; 991 992 return (1); 993 } 994 } 995 996 return (0); 997 } 998 999 /* 1000 * Returns true if the supplied vnode is visible 1001 * in this export. If vnode is visible, return 1002 * vis_exported in expseudo. 1003 */ 1004 int 1005 nfs_visible(struct exportinfo *exi, vnode_t *vp, int *expseudo) 1006 { 1007 struct exp_visible *visp; 1008 fid_t fid; 1009 1010 /* 1011 * First check to see if vp is export root. 1012 * 1013 * A pseudo export root can never be exported 1014 * (it would be a real export then); however, 1015 * it is always visible. If a pseudo root object 1016 * was exported by server admin, then the entire 1017 * pseudo exportinfo (and all visible entries) would 1018 * be destroyed. A pseudo exportinfo only exists 1019 * to provide access to real (descendant) export(s). 1020 * 1021 * Previously, rootdir was special cased here; however, 1022 * the export root special case handles the rootdir 1023 * case also. 1024 */ 1025 if (VN_CMP(vp, exi->exi_vp)) { 1026 *expseudo = 0; 1027 return (1); 1028 } 1029 1030 /* 1031 * Only a PSEUDO node has a visible list or an exported VROOT 1032 * node may have a visible list. 1033 */ 1034 if (! PSEUDO(exi)) 1035 exi = get_root_export(exi); 1036 1037 /* Get the fid of the vnode */ 1038 1039 bzero(&fid, sizeof (fid)); 1040 fid.fid_len = MAXFIDSZ; 1041 if (vop_fid_pseudo(vp, &fid) != 0) { 1042 *expseudo = 0; 1043 return (0); 1044 } 1045 1046 /* 1047 * We can't trust VN_CMP() above because of LOFS. 1048 * Even though VOP_CMP will do the right thing for LOFS 1049 * objects, VN_CMP will short circuit out early when the 1050 * vnode ops ptrs are different. Just in case we're dealing 1051 * with LOFS, compare exi_fid/fsid here. 1052 * 1053 * expseudo is not set because this is not an export 1054 */ 1055 if (EQFID(&exi->exi_fid, &fid) && 1056 EQFSID(&exi->exi_fsid, &vp->v_vfsp->vfs_fsid)) { 1057 *expseudo = 0; 1058 return (1); 1059 } 1060 1061 1062 /* See if it matches any fid in the visible list */ 1063 1064 for (visp = exi->exi_visible; visp; visp = visp->vis_next) { 1065 if (EQFID(&fid, &visp->vis_fid)) { 1066 *expseudo = visp->vis_exported; 1067 return (1); 1068 } 1069 } 1070 1071 *expseudo = 0; 1072 1073 return (0); 1074 } 1075 1076 /* 1077 * Returns true if the supplied vnode is the 1078 * directory of an export point. 1079 */ 1080 int 1081 nfs_exported(struct exportinfo *exi, vnode_t *vp) 1082 { 1083 struct exp_visible *visp; 1084 fid_t fid; 1085 1086 /* 1087 * First check to see if vp is the export root 1088 * This check required for the case of lookup .. 1089 * where .. is a V_ROOT vnode and a pseudo exportroot. 1090 * Pseudo export root objects do not have an entry 1091 * in the visible list even though every V_ROOT 1092 * pseudonode is visible. It is safe to compare 1093 * vp here because pseudo_exportfs put a hold on 1094 * it when exi_vp was initialized. 1095 * 1096 * Note: VN_CMP() won't match for LOFS shares, but they're 1097 * handled below w/EQFID/EQFSID. 1098 */ 1099 if (VN_CMP(vp, exi->exi_vp)) 1100 return (1); 1101 1102 /* Get the fid of the vnode */ 1103 1104 bzero(&fid, sizeof (fid)); 1105 fid.fid_len = MAXFIDSZ; 1106 if (vop_fid_pseudo(vp, &fid) != 0) 1107 return (0); 1108 1109 if (EQFID(&fid, &exi->exi_fid) && 1110 EQFSID(&vp->v_vfsp->vfs_fsid, &exi->exi_fsid)) { 1111 return (1); 1112 } 1113 1114 /* See if it matches any fid in the visible list */ 1115 1116 for (visp = exi->exi_visible; visp; visp = visp->vis_next) { 1117 if (EQFID(&fid, &visp->vis_fid)) 1118 return (visp->vis_exported); 1119 } 1120 1121 return (0); 1122 } 1123 1124 /* 1125 * Returns true if the supplied inode is visible 1126 * in this export. This function is used by 1127 * readdir which uses inode numbers from the 1128 * directory. 1129 * 1130 * NOTE: this code does not match inode number for ".", 1131 * but it isn't required because NFS4 server rddir 1132 * skips . and .. entries. 1133 */ 1134 int 1135 nfs_visible_inode(struct exportinfo *exi, ino64_t ino, int *expseudo) 1136 { 1137 struct exp_visible *visp; 1138 1139 /* 1140 * Only a PSEUDO node has a visible list or an exported VROOT 1141 * node may have a visible list. 1142 */ 1143 if (! PSEUDO(exi)) 1144 exi = get_root_export(exi); 1145 1146 for (visp = exi->exi_visible; visp; visp = visp->vis_next) 1147 if ((u_longlong_t)ino == visp->vis_ino) { 1148 *expseudo = visp->vis_exported; 1149 return (1); 1150 } 1151 1152 *expseudo = 0; 1153 return (0); 1154 } 1155