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