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