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