10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51610Sthurlow * Common Development and Distribution License (the "License").
61610Sthurlow * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate *
2112134SPavel.Filipensky@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
220Sstevel@tonic-gate */
230Sstevel@tonic-gate
240Sstevel@tonic-gate #include <sys/systm.h>
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <nfs/nfs.h>
270Sstevel@tonic-gate #include <nfs/export.h>
280Sstevel@tonic-gate #include <sys/cmn_err.h>
290Sstevel@tonic-gate
304871Sjasmith #define PSEUDOFS_SUFFIX " (pseudo)"
314871Sjasmith
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * A version of VOP_FID that deals with a remote VOP_FID for nfs.
340Sstevel@tonic-gate * If vp is an nfs node, nfs4_fid() returns EREMOTE, nfs3_fid() and nfs_fid()
350Sstevel@tonic-gate * returns the filehandle of vp as its fid. When nfs uses fid to set the
360Sstevel@tonic-gate * exportinfo filehandle template, a remote nfs filehandle would be too big for
370Sstevel@tonic-gate * the fid of the exported directory. This routine remaps the value of the
380Sstevel@tonic-gate * attribute va_nodeid of vp to be the fid of vp, so that the fid can fit.
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * We need this fid mainly for setting up NFSv4 server namespace where an
410Sstevel@tonic-gate * nfs filesystem is also part of it. Thus, need to be able to setup a pseudo
420Sstevel@tonic-gate * exportinfo for an nfs node.
430Sstevel@tonic-gate *
444871Sjasmith * e.g. mount a filesystem on top of a nfs dir, and then share the new mount
454871Sjasmith * (like exporting a local disk from a "diskless" client)
460Sstevel@tonic-gate */
470Sstevel@tonic-gate int
vop_fid_pseudo(vnode_t * vp,fid_t * fidp)480Sstevel@tonic-gate vop_fid_pseudo(vnode_t *vp, fid_t *fidp)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate struct vattr va;
510Sstevel@tonic-gate int error;
520Sstevel@tonic-gate
535331Samw error = VOP_FID(vp, fidp, NULL);
540Sstevel@tonic-gate
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate * XXX nfs4_fid() does nothing and returns EREMOTE.
570Sstevel@tonic-gate * XXX nfs3_fid()/nfs_fid() returns nfs filehandle as its fid
580Sstevel@tonic-gate * which has a bigger length than local fid.
591610Sthurlow * NFS_FH4MAXDATA is the size of
601610Sthurlow * fhandle4_t.fh_xdata[NFS_FH4MAXDATA].
61806Sek110237 *
62806Sek110237 * Note: nfs[2,3,4]_fid() only gets called for diskless clients.
630Sstevel@tonic-gate */
64806Sek110237 if (error == EREMOTE ||
651610Sthurlow (error == 0 && fidp->fid_len > NFS_FH4MAXDATA)) {
660Sstevel@tonic-gate
670Sstevel@tonic-gate va.va_mask = AT_NODEID;
685331Samw error = VOP_GETATTR(vp, &va, 0, CRED(), NULL);
690Sstevel@tonic-gate if (error)
700Sstevel@tonic-gate return (error);
710Sstevel@tonic-gate
720Sstevel@tonic-gate fidp->fid_len = sizeof (va.va_nodeid);
730Sstevel@tonic-gate bcopy(&va.va_nodeid, fidp->fid_data, fidp->fid_len);
740Sstevel@tonic-gate return (0);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate return (error);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Get an nfsv4 vnode of the given fid from the visible list of an
820Sstevel@tonic-gate * nfs filesystem or get the exi_vp if it is the root node.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate int
nfs4_vget_pseudo(struct exportinfo * exi,vnode_t ** vpp,fid_t * fidp)850Sstevel@tonic-gate nfs4_vget_pseudo(struct exportinfo *exi, vnode_t **vpp, fid_t *fidp)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate fid_t exp_fid;
880Sstevel@tonic-gate struct exp_visible *visp;
890Sstevel@tonic-gate int error;
900Sstevel@tonic-gate
910Sstevel@tonic-gate /* check if the given fid is in the visible list */
920Sstevel@tonic-gate
930Sstevel@tonic-gate for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
940Sstevel@tonic-gate if (EQFID(fidp, &visp->vis_fid)) {
950Sstevel@tonic-gate VN_HOLD(visp->vis_vp);
960Sstevel@tonic-gate *vpp = visp->vis_vp;
970Sstevel@tonic-gate return (0);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /* check if the given fid is the same as the exported node */
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate bzero(&exp_fid, sizeof (exp_fid));
1040Sstevel@tonic-gate exp_fid.fid_len = MAXFIDSZ;
1050Sstevel@tonic-gate error = vop_fid_pseudo(exi->exi_vp, &exp_fid);
1060Sstevel@tonic-gate if (error)
1070Sstevel@tonic-gate return (error);
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate if (EQFID(fidp, &exp_fid)) {
1100Sstevel@tonic-gate VN_HOLD(exi->exi_vp);
1110Sstevel@tonic-gate *vpp = exi->exi_vp;
1120Sstevel@tonic-gate return (0);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate return (ENOENT);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * Create a pseudo export entry
1200Sstevel@tonic-gate *
1210Sstevel@tonic-gate * This is an export entry that's created as the
1220Sstevel@tonic-gate * side-effect of a "real" export. As a part of
1230Sstevel@tonic-gate * a real export, the pathname to the export is
1240Sstevel@tonic-gate * checked to see if all the directory components
1250Sstevel@tonic-gate * are accessible via an NFSv4 client, i.e. are
1264871Sjasmith * exported. If treeclimb_export() finds an unexported
1270Sstevel@tonic-gate * mountpoint along the path, then it calls this
1280Sstevel@tonic-gate * function to export it.
1290Sstevel@tonic-gate *
1304871Sjasmith * This pseudo export differs from a real export in that
1314871Sjasmith * it only allows read-only access. A "visible" list of
1324871Sjasmith * directories is added to filter lookup and readdir results
1334871Sjasmith * to only contain dirnames which lead to descendant shares.
1340Sstevel@tonic-gate *
1354871Sjasmith * A visible list has a per-file-system scope. Any exportinfo
1364871Sjasmith * struct (real or pseudo) can have a visible list as long as
1374871Sjasmith * a) its export root is VROOT
1384871Sjasmith * b) a descendant of the export root is shared
1390Sstevel@tonic-gate */
140*12679SPavel.Filipensky@Sun.COM struct exportinfo *
pseudo_exportfs(vnode_t * vp,fid_t * fid,struct exp_visible * vis_head,struct exportdata * exdata)141*12679SPavel.Filipensky@Sun.COM pseudo_exportfs(vnode_t *vp, fid_t *fid, struct exp_visible *vis_head,
142*12679SPavel.Filipensky@Sun.COM struct exportdata *exdata)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate struct exportinfo *exi;
1450Sstevel@tonic-gate struct exportdata *kex;
1460Sstevel@tonic-gate fsid_t fsid;
147*12679SPavel.Filipensky@Sun.COM int vpathlen;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&exported_lock));
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate fsid = vp->v_vfsp->vfs_fsid;
1520Sstevel@tonic-gate exi = kmem_zalloc(sizeof (*exi), KM_SLEEP);
1530Sstevel@tonic-gate exi->exi_fsid = fsid;
154*12679SPavel.Filipensky@Sun.COM exi->exi_fid = *fid;
1550Sstevel@tonic-gate exi->exi_vp = vp;
1560Sstevel@tonic-gate VN_HOLD(exi->exi_vp);
1570Sstevel@tonic-gate exi->exi_visible = vis_head;
1580Sstevel@tonic-gate exi->exi_count = 1;
1590Sstevel@tonic-gate exi->exi_volatile_dev = (vfssw[vp->v_vfsp->vfs_fstype].vsw_flag &
1604871Sjasmith VSW_VOLATILEDEV) ? 1 : 0;
1610Sstevel@tonic-gate mutex_init(&exi->exi_lock, NULL, MUTEX_DEFAULT, NULL);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate * Build up the template fhandle
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate exi->exi_fh.fh_fsid = fsid;
1670Sstevel@tonic-gate ASSERT(exi->exi_fid.fid_len <= sizeof (exi->exi_fh.fh_xdata));
1680Sstevel@tonic-gate exi->exi_fh.fh_xlen = exi->exi_fid.fid_len;
1690Sstevel@tonic-gate bcopy(exi->exi_fid.fid_data, exi->exi_fh.fh_xdata,
1700Sstevel@tonic-gate exi->exi_fid.fid_len);
1710Sstevel@tonic-gate exi->exi_fh.fh_len = sizeof (exi->exi_fh.fh_data);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate kex = &exi->exi_export;
1740Sstevel@tonic-gate kex->ex_flags = EX_PSEUDO;
1750Sstevel@tonic-gate
1764871Sjasmith vpathlen = vp->v_path ? strlen(vp->v_path) : 0;
1774871Sjasmith kex->ex_pathlen = vpathlen + strlen(PSEUDOFS_SUFFIX);
1784871Sjasmith kex->ex_path = kmem_alloc(kex->ex_pathlen + 1, KM_SLEEP);
1790Sstevel@tonic-gate
1804871Sjasmith if (vpathlen)
1814871Sjasmith (void) strcpy(kex->ex_path, vp->v_path);
1824871Sjasmith (void) strcpy(kex->ex_path + vpathlen, PSEUDOFS_SUFFIX);
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /* Transfer the secinfo data from exdata to this new pseudo node */
1850Sstevel@tonic-gate if (exdata)
1860Sstevel@tonic-gate srv_secinfo_exp2pseu(&exi->exi_export, exdata);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate * Initialize auth cache lock
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate rw_init(&exi->exi_cache_lock, NULL, RW_DEFAULT, NULL);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * Insert the new entry at the front of the export list
1950Sstevel@tonic-gate */
1960Sstevel@tonic-gate export_link(exi);
1970Sstevel@tonic-gate
198*12679SPavel.Filipensky@Sun.COM return (exi);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * Free a list of visible directories
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate void
free_visible(struct exp_visible * head)2050Sstevel@tonic-gate free_visible(struct exp_visible *head)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate struct exp_visible *visp, *next;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate for (visp = head; visp; visp = next) {
2100Sstevel@tonic-gate if (visp->vis_vp != NULL)
2110Sstevel@tonic-gate VN_RELE(visp->vis_vp);
2124871Sjasmith
2130Sstevel@tonic-gate next = visp->vis_next;
2144871Sjasmith srv_secinfo_list_free(visp->vis_secinfo, visp->vis_seccnt);
2150Sstevel@tonic-gate kmem_free(visp, sizeof (*visp));
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate /*
2206236Spf199842 * Connects newchild (or subtree with newchild in head)
2216236Spf199842 * to the parent node. We always add it to the beginning
2226236Spf199842 * of sibling list.
2236236Spf199842 */
2246236Spf199842 static void
tree_add_child(treenode_t * parent,treenode_t * newchild)2256236Spf199842 tree_add_child(treenode_t *parent, treenode_t *newchild)
2266236Spf199842 {
2276236Spf199842 newchild->tree_parent = parent;
2286236Spf199842 newchild->tree_sibling = parent->tree_child_first;
2296236Spf199842 parent->tree_child_first = newchild;
2306236Spf199842 }
2316236Spf199842
23212134SPavel.Filipensky@Sun.COM /* Look up among direct children a node with the exact tree_vis pointer */
23312134SPavel.Filipensky@Sun.COM static treenode_t *
tree_find_child_by_vis(treenode_t * t,exp_visible_t * vis)23412134SPavel.Filipensky@Sun.COM tree_find_child_by_vis(treenode_t *t, exp_visible_t *vis)
23512134SPavel.Filipensky@Sun.COM {
23612134SPavel.Filipensky@Sun.COM for (t = t->tree_child_first; t; t = t->tree_sibling)
23712134SPavel.Filipensky@Sun.COM if (t->tree_vis == vis)
23812134SPavel.Filipensky@Sun.COM return (t);
23912134SPavel.Filipensky@Sun.COM return (NULL);
24012134SPavel.Filipensky@Sun.COM }
24112134SPavel.Filipensky@Sun.COM
2426236Spf199842 /*
2436236Spf199842 * Add new node to the head of subtree pointed by 'n'. n can be NULL.
2446236Spf199842 * Interconnects the new treenode with exp_visible and exportinfo
2456236Spf199842 * if needed.
2466236Spf199842 */
2476236Spf199842 static treenode_t *
tree_prepend_node(treenode_t * n,exp_visible_t * v,exportinfo_t * e)2486236Spf199842 tree_prepend_node(treenode_t *n, exp_visible_t *v, exportinfo_t *e)
2496236Spf199842 {
2506236Spf199842 treenode_t *tnode = kmem_zalloc(sizeof (*tnode), KM_SLEEP);
2516236Spf199842
2526236Spf199842 if (n) {
2536236Spf199842 tnode->tree_child_first = n;
2546236Spf199842 n->tree_parent = tnode;
2556236Spf199842 }
2566236Spf199842 if (v) {
2576236Spf199842 tnode->tree_vis = v;
2586236Spf199842 }
2596236Spf199842 if (e) {
2606236Spf199842 tnode->tree_exi = e;
2616236Spf199842 e->exi_tree = tnode;
2626236Spf199842 }
2636236Spf199842 return (tnode);
2646236Spf199842 }
2656236Spf199842
2666236Spf199842 /*
2676236Spf199842 * Removes node from the tree and frees the treenode struct.
2686236Spf199842 * Does not free structures pointed by tree_exi and tree_vis,
2696236Spf199842 * they should be already freed.
2706236Spf199842 */
2716236Spf199842 static void
tree_remove_node(treenode_t * node)2726236Spf199842 tree_remove_node(treenode_t *node)
2736236Spf199842 {
2746236Spf199842 treenode_t *parent = node->tree_parent;
2756236Spf199842 treenode_t *s; /* s for sibling */
2766236Spf199842
2776236Spf199842 if (parent == NULL) {
2786236Spf199842 kmem_free(node, sizeof (*node));
2796236Spf199842 ns_root = NULL;
2806236Spf199842 return;
2816236Spf199842 }
2826236Spf199842 /* This node is first child */
2836236Spf199842 if (parent->tree_child_first == node) {
2846236Spf199842 parent->tree_child_first = node->tree_sibling;
2856236Spf199842 /* This node is not first child */
2866236Spf199842 } else {
2876236Spf199842 s = parent->tree_child_first;
2886236Spf199842 while (s->tree_sibling != node)
2896236Spf199842 s = s->tree_sibling;
2906236Spf199842 s->tree_sibling = s->tree_sibling->tree_sibling;
2916236Spf199842 }
2926236Spf199842 kmem_free(node, sizeof (*node));
2936236Spf199842 }
2946236Spf199842
2956236Spf199842 /*
2960Sstevel@tonic-gate * When we export a new directory we need to add a new
2970Sstevel@tonic-gate * path segment through the pseudofs to reach the new
2980Sstevel@tonic-gate * directory. This new path is reflected in a list of
2990Sstevel@tonic-gate * directories added to the "visible" list.
3000Sstevel@tonic-gate *
3010Sstevel@tonic-gate * Here there are two lists of visible fids: one hanging off the
3020Sstevel@tonic-gate * pseudo exportinfo, and the one we want to add. It's possible
3030Sstevel@tonic-gate * that the two lists share a common path segment
3040Sstevel@tonic-gate * and have some common directories. We need to combine
3050Sstevel@tonic-gate * the lists so there's no duplicate entries. Where a common
3060Sstevel@tonic-gate * path component is found, the vis_count field is bumped.
3070Sstevel@tonic-gate *
30811796SPavel.Filipensky@Sun.COM * This example shows that the treenode chain (tree_head) and
30911796SPavel.Filipensky@Sun.COM * exp_visible chain (vis_head) can differ in length. The latter
31011796SPavel.Filipensky@Sun.COM * can be shorter. The outer loop must loop over the vis_head chain.
31111796SPavel.Filipensky@Sun.COM *
31211796SPavel.Filipensky@Sun.COM * share /x/a
31311796SPavel.Filipensky@Sun.COM * mount -F ufs /dev/dsk/... /x/y
31411796SPavel.Filipensky@Sun.COM * mkdir -p /x/y/a/b
31511796SPavel.Filipensky@Sun.COM * share /x/y/a/b
31611796SPavel.Filipensky@Sun.COM *
31711796SPavel.Filipensky@Sun.COM * When more_visible() is called during the second share,
318*12679SPavel.Filipensky@Sun.COM * the existing namespace is following:
31911796SPavel.Filipensky@Sun.COM * exp_visible_t
32011796SPavel.Filipensky@Sun.COM * treenode_t exportinfo_t v0 v1
32111796SPavel.Filipensky@Sun.COM * ns_root+---+ +------------+ +---+ +---+
32211796SPavel.Filipensky@Sun.COM * t0| / |........| E0 pseudo |->| x |->| a |
32311796SPavel.Filipensky@Sun.COM * +---+ +------------+ +---+ +---+
32411796SPavel.Filipensky@Sun.COM * | / /
32511796SPavel.Filipensky@Sun.COM * +---+ / /
32611796SPavel.Filipensky@Sun.COM * t1| x |------------------------ /
32711796SPavel.Filipensky@Sun.COM * +---+ /
32811796SPavel.Filipensky@Sun.COM * | /
32911796SPavel.Filipensky@Sun.COM * +---+ /
33011796SPavel.Filipensky@Sun.COM * t2| a |-------------------------
33111796SPavel.Filipensky@Sun.COM * +---+........+------------+
33211796SPavel.Filipensky@Sun.COM * | E1 real |
33311796SPavel.Filipensky@Sun.COM * +------------+
33411796SPavel.Filipensky@Sun.COM *
33511796SPavel.Filipensky@Sun.COM * This is being added:
33611796SPavel.Filipensky@Sun.COM *
33711796SPavel.Filipensky@Sun.COM * tree_head vis_head
33811796SPavel.Filipensky@Sun.COM * +---+ +---+
33911796SPavel.Filipensky@Sun.COM * t3| x |->| x |v2
34011796SPavel.Filipensky@Sun.COM * +---+ +---+
34111796SPavel.Filipensky@Sun.COM * | |
34211796SPavel.Filipensky@Sun.COM * +---+ +---+ v4 v5
34311796SPavel.Filipensky@Sun.COM * t4| y |->| y |v3 +------------+ +---+ +---+
34411796SPavel.Filipensky@Sun.COM * +---+\ +---+ | E2 pseudo |->| a |->| b |
34511796SPavel.Filipensky@Sun.COM * | \....... >+------------+ +---+ +---+
34611796SPavel.Filipensky@Sun.COM * +---+ / /
34711796SPavel.Filipensky@Sun.COM * t5| a |--------------------------- /
34811796SPavel.Filipensky@Sun.COM * +---+ /
34911796SPavel.Filipensky@Sun.COM * | /
35011796SPavel.Filipensky@Sun.COM * +---+-------------------------------
35111796SPavel.Filipensky@Sun.COM * t6| b | +------------+
35211796SPavel.Filipensky@Sun.COM * +---+..........>| E3 real |
35311796SPavel.Filipensky@Sun.COM * +------------+
35411796SPavel.Filipensky@Sun.COM *
35511796SPavel.Filipensky@Sun.COM * more_visible() will:
35612134SPavel.Filipensky@Sun.COM * - kmem_free() t3 and v2
35712134SPavel.Filipensky@Sun.COM * - add t4, t5, t6 as a child of t1 (t4 will become sibling of t2)
35811796SPavel.Filipensky@Sun.COM * - add v3 to the end of E0->exi_visible
35911796SPavel.Filipensky@Sun.COM *
360*12679SPavel.Filipensky@Sun.COM * Note that v4 and v5 were already processed in pseudo_exportfs() and
36111796SPavel.Filipensky@Sun.COM * added to E2. The outer loop of more_visible() will loop only over v2
36211796SPavel.Filipensky@Sun.COM * and v3. The inner loop of more_visible() always loops over v0 and v1.
36312134SPavel.Filipensky@Sun.COM *
36412134SPavel.Filipensky@Sun.COM * Illustration for this scenario:
36512134SPavel.Filipensky@Sun.COM *
36612134SPavel.Filipensky@Sun.COM * mkdir -p /v/a/b/c
36712134SPavel.Filipensky@Sun.COM * share /v/a/b/c
36812134SPavel.Filipensky@Sun.COM * mkdir /v/a/b/c1
36912134SPavel.Filipensky@Sun.COM * mkdir -p /v/a1
37012134SPavel.Filipensky@Sun.COM * mv /v/a/b /v/a1
37112134SPavel.Filipensky@Sun.COM * share /v/a1/b/c1
37212134SPavel.Filipensky@Sun.COM *
37312134SPavel.Filipensky@Sun.COM * EXISTING
37412134SPavel.Filipensky@Sun.COM * treenode
37512134SPavel.Filipensky@Sun.COM * namespace: +-----------+ visibles
37612134SPavel.Filipensky@Sun.COM * |exportinfo |-->v->a->b->c
37712134SPavel.Filipensky@Sun.COM * connect_point->+---+--->+-----------+
37812134SPavel.Filipensky@Sun.COM * | / |T0
37912134SPavel.Filipensky@Sun.COM * +---+
38012134SPavel.Filipensky@Sun.COM * | NEW treenode chain:
38112134SPavel.Filipensky@Sun.COM * child->+---+
38212134SPavel.Filipensky@Sun.COM * | v |T1 +---+<-curr
38312134SPavel.Filipensky@Sun.COM * +---+ N1| v |
38412134SPavel.Filipensky@Sun.COM * | +---+
38512134SPavel.Filipensky@Sun.COM * +---+ |
38612134SPavel.Filipensky@Sun.COM * | a |T2 +---+<-tree_head
38712134SPavel.Filipensky@Sun.COM * +---+ N2| a1|
38812134SPavel.Filipensky@Sun.COM * | +---+
38912134SPavel.Filipensky@Sun.COM * +---+ |
39012134SPavel.Filipensky@Sun.COM * | b |T3 +---+
39112134SPavel.Filipensky@Sun.COM * +---+ N3| b |
39212134SPavel.Filipensky@Sun.COM * | +---+
39312134SPavel.Filipensky@Sun.COM * +---+ |
39412134SPavel.Filipensky@Sun.COM * | c |T4 +---+
39512134SPavel.Filipensky@Sun.COM * +---+ N4| c1|
39612134SPavel.Filipensky@Sun.COM * +---+
39712134SPavel.Filipensky@Sun.COM *
39812134SPavel.Filipensky@Sun.COM * The picture above illustrates the position of following pointers after line
39912134SPavel.Filipensky@Sun.COM * 'child = tree_find_child_by_vis(connect_point, curr->tree_vis);'
40012134SPavel.Filipensky@Sun.COM * was executed for the first time in the outer 'for' loop:
40112134SPavel.Filipensky@Sun.COM *
40212134SPavel.Filipensky@Sun.COM * connect_point..parent treenode in the EXISTING namespace to which the 'curr'
40312134SPavel.Filipensky@Sun.COM * should be connected. If 'connect_point' already has a child
40412134SPavel.Filipensky@Sun.COM * with the same value of tree_vis as the curr->tree_vis is,
40512134SPavel.Filipensky@Sun.COM * the 'curr' will not be added, but kmem_free()d.
40612134SPavel.Filipensky@Sun.COM * child..........the result of tree_find_child_by_vis()
40712134SPavel.Filipensky@Sun.COM * curr...........currently processed treenode from the NEW treenode chain
40812134SPavel.Filipensky@Sun.COM * tree_head......current head of the NEW treenode chain, in this case it was
40912134SPavel.Filipensky@Sun.COM * already moved down to its child - preparation for another loop
41012134SPavel.Filipensky@Sun.COM *
41112134SPavel.Filipensky@Sun.COM * What will happen to NEW treenodes N1, N2, N3, N4 in more_visible() later:
41212134SPavel.Filipensky@Sun.COM *
41312134SPavel.Filipensky@Sun.COM * N1: is merged - i.e. N1 is kmem_free()d. T0 has a child T1 with the same
41412134SPavel.Filipensky@Sun.COM * tree_vis as N1
41512134SPavel.Filipensky@Sun.COM * N2: is added as a new child of T1
41612134SPavel.Filipensky@Sun.COM * Note: not just N2, but the whole chain N2->N3->N4 is added
41712134SPavel.Filipensky@Sun.COM * N3: not processed separately (it was added together with N2)
41812134SPavel.Filipensky@Sun.COM * Even that N3 and T3 have same tree_vis, they are NOT merged, but will
41912134SPavel.Filipensky@Sun.COM * become duplicates.
42012134SPavel.Filipensky@Sun.COM * N4: not processed separately
4210Sstevel@tonic-gate */
4220Sstevel@tonic-gate static void
more_visible(struct exportinfo * exi,treenode_t * tree_head)42311796SPavel.Filipensky@Sun.COM more_visible(struct exportinfo *exi, treenode_t *tree_head)
4240Sstevel@tonic-gate {
42511796SPavel.Filipensky@Sun.COM struct exp_visible *vp1, *vp2, *vis_head, *tail, *next;
4260Sstevel@tonic-gate int found;
42712134SPavel.Filipensky@Sun.COM treenode_t *child, *curr, *connect_point;
4280Sstevel@tonic-gate
42911796SPavel.Filipensky@Sun.COM vis_head = tree_head->tree_vis;
43012134SPavel.Filipensky@Sun.COM connect_point = exi->exi_tree;
4316236Spf199842
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate * If exportinfo doesn't already have a visible
4340Sstevel@tonic-gate * list just assign the entire supplied list.
4350Sstevel@tonic-gate */
4360Sstevel@tonic-gate if (exi->exi_visible == NULL) {
43712134SPavel.Filipensky@Sun.COM tree_add_child(exi->exi_tree, tree_head);
4380Sstevel@tonic-gate exi->exi_visible = vis_head;
4390Sstevel@tonic-gate return;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate
44211796SPavel.Filipensky@Sun.COM /* The outer loop traverses the supplied list. */
44311796SPavel.Filipensky@Sun.COM for (vp1 = vis_head; vp1; vp1 = next) {
44411796SPavel.Filipensky@Sun.COM found = 0;
44511796SPavel.Filipensky@Sun.COM next = vp1->vis_next;
4460Sstevel@tonic-gate
44711796SPavel.Filipensky@Sun.COM /* The inner loop searches the exportinfo visible list. */
4480Sstevel@tonic-gate for (vp2 = exi->exi_visible; vp2; vp2 = vp2->vis_next) {
4490Sstevel@tonic-gate tail = vp2;
4500Sstevel@tonic-gate if (EQFID(&vp1->vis_fid, &vp2->vis_fid)) {
4510Sstevel@tonic-gate found = 1;
4520Sstevel@tonic-gate vp2->vis_count++;
4530Sstevel@tonic-gate VN_RELE(vp1->vis_vp);
45412134SPavel.Filipensky@Sun.COM /* Transfer vis_exported from vp1 to vp2. */
4550Sstevel@tonic-gate if (vp1->vis_exported && !vp2->vis_exported)
4560Sstevel@tonic-gate vp2->vis_exported = 1;
45711796SPavel.Filipensky@Sun.COM kmem_free(vp1, sizeof (*vp1));
45811796SPavel.Filipensky@Sun.COM tree_head->tree_vis = vp2;
4590Sstevel@tonic-gate break;
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate /* If not found - add to the end of the list */
4640Sstevel@tonic-gate if (! found) {
46511796SPavel.Filipensky@Sun.COM tail->vis_next = vp1;
46611796SPavel.Filipensky@Sun.COM vp1->vis_next = NULL;
4670Sstevel@tonic-gate }
46812134SPavel.Filipensky@Sun.COM
46912134SPavel.Filipensky@Sun.COM curr = tree_head;
47011796SPavel.Filipensky@Sun.COM tree_head = tree_head->tree_child_first;
47112134SPavel.Filipensky@Sun.COM
47212134SPavel.Filipensky@Sun.COM if (! connect_point) /* No longer merging */
47312134SPavel.Filipensky@Sun.COM continue;
47412134SPavel.Filipensky@Sun.COM /*
47512134SPavel.Filipensky@Sun.COM * The inner loop could set curr->tree_vis to the EXISTING
47612134SPavel.Filipensky@Sun.COM * exp_visible vp2, so we can search among the children of
47712134SPavel.Filipensky@Sun.COM * connect_point for the curr->tree_vis. No need for EQFID.
47812134SPavel.Filipensky@Sun.COM */
47912134SPavel.Filipensky@Sun.COM child = tree_find_child_by_vis(connect_point, curr->tree_vis);
48012628SPavel.Filipensky@Sun.COM
48112628SPavel.Filipensky@Sun.COM /*
48212628SPavel.Filipensky@Sun.COM * Merging cannot be done if a valid child->tree_exi would
48312628SPavel.Filipensky@Sun.COM * be overwritten by a new curr->tree_exi.
48412628SPavel.Filipensky@Sun.COM */
48512628SPavel.Filipensky@Sun.COM if (child &&
48612628SPavel.Filipensky@Sun.COM (child->tree_exi == NULL || curr->tree_exi == NULL)) {
48712134SPavel.Filipensky@Sun.COM if (curr->tree_exi) { /* Transfer the exportinfo */
48812134SPavel.Filipensky@Sun.COM child->tree_exi = curr->tree_exi;
48912134SPavel.Filipensky@Sun.COM child->tree_exi->exi_tree = child;
49012134SPavel.Filipensky@Sun.COM }
49112134SPavel.Filipensky@Sun.COM kmem_free(curr, sizeof (treenode_t));
49212628SPavel.Filipensky@Sun.COM connect_point = child;
49312134SPavel.Filipensky@Sun.COM } else { /* Branching */
49412134SPavel.Filipensky@Sun.COM tree_add_child(connect_point, curr);
49512628SPavel.Filipensky@Sun.COM connect_point = NULL;
49612134SPavel.Filipensky@Sun.COM }
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate /*
5016236Spf199842 * Remove one visible entry from the pseudo exportfs.
5020Sstevel@tonic-gate *
5030Sstevel@tonic-gate * When we unexport a directory, we have to remove path
5040Sstevel@tonic-gate * components from the visible list in the pseudo exportfs
5056236Spf199842 * entry. The supplied visible contains one fid of one path
5066236Spf199842 * component. The visible list of the export
5076236Spf199842 * is checked against provided visible, matching fid has its
5080Sstevel@tonic-gate * reference count decremented. If a reference count drops to
5090Sstevel@tonic-gate * zero, then it means no paths now use this directory, so its
5100Sstevel@tonic-gate * fid can be removed from the visible list.
5110Sstevel@tonic-gate *
5120Sstevel@tonic-gate * When the last path is removed, the visible list will be null.
5130Sstevel@tonic-gate */
5140Sstevel@tonic-gate static void
less_visible(struct exportinfo * exi,struct exp_visible * vp1)5156236Spf199842 less_visible(struct exportinfo *exi, struct exp_visible *vp1)
5160Sstevel@tonic-gate {
5176236Spf199842 struct exp_visible *vp2;
5180Sstevel@tonic-gate struct exp_visible *prev, *next;
5190Sstevel@tonic-gate
5206236Spf199842 for (vp2 = exi->exi_visible, prev = NULL; vp2; vp2 = next) {
5210Sstevel@tonic-gate
5226236Spf199842 next = vp2->vis_next;
5230Sstevel@tonic-gate
52412134SPavel.Filipensky@Sun.COM if (vp1 == vp2) {
5256236Spf199842 /*
5266236Spf199842 * Decrement the ref count.
5276236Spf199842 * Remove the entry if it's zero.
5286236Spf199842 */
5296236Spf199842 if (--vp2->vis_count <= 0) {
5306236Spf199842 if (prev == NULL)
5316236Spf199842 exi->exi_visible = next;
5326236Spf199842 else
5336236Spf199842 prev->vis_next = next;
5346236Spf199842 VN_RELE(vp2->vis_vp);
5356236Spf199842 srv_secinfo_list_free(vp2->vis_secinfo,
5366236Spf199842 vp2->vis_seccnt);
5376236Spf199842 kmem_free(vp2, sizeof (*vp1));
5380Sstevel@tonic-gate }
5396236Spf199842 break;
5400Sstevel@tonic-gate }
5416236Spf199842 prev = vp2;
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * This function checks the path to a new export to
5470Sstevel@tonic-gate * check whether all the pathname components are
5480Sstevel@tonic-gate * exported. It works by climbing the file tree one
5490Sstevel@tonic-gate * component at a time via "..", crossing mountpoints
5500Sstevel@tonic-gate * if necessary until an export entry is found, or the
5510Sstevel@tonic-gate * system root is reached.
5520Sstevel@tonic-gate *
5530Sstevel@tonic-gate * If an unexported mountpoint is found, then
5540Sstevel@tonic-gate * a new pseudo export is added and the pathname from
5550Sstevel@tonic-gate * the mountpoint down to the export is added to the
5560Sstevel@tonic-gate * visible list for the new pseudo export. If an existing
5570Sstevel@tonic-gate * pseudo export is found, then the pathname is added
5580Sstevel@tonic-gate * to its visible list.
5590Sstevel@tonic-gate *
5600Sstevel@tonic-gate * Note that there's some tests for exportdir.
5610Sstevel@tonic-gate * The exportinfo entry that's passed as a parameter
5620Sstevel@tonic-gate * is that of the real export and exportdir is set
5630Sstevel@tonic-gate * for this case.
5640Sstevel@tonic-gate *
5650Sstevel@tonic-gate * Here is an example of a possible setup:
5660Sstevel@tonic-gate *
5670Sstevel@tonic-gate * () - a new fs; fs mount point
5680Sstevel@tonic-gate * EXPORT - a real exported node
5690Sstevel@tonic-gate * PSEUDO - a pseudo node
5700Sstevel@tonic-gate * vis - visible list
5710Sstevel@tonic-gate * f# - security flavor#
5725331Samw * (f#) - security flavor# propagated from its descendents
5730Sstevel@tonic-gate * "" - covered vnode
5740Sstevel@tonic-gate *
5750Sstevel@tonic-gate *
5760Sstevel@tonic-gate * /
5770Sstevel@tonic-gate * |
5780Sstevel@tonic-gate * (a) PSEUDO (f1,f2)
5790Sstevel@tonic-gate * | vis: b,b,"c","n"
5800Sstevel@tonic-gate * |
5810Sstevel@tonic-gate * b
5820Sstevel@tonic-gate * ---------|------------------
5830Sstevel@tonic-gate * | |
5840Sstevel@tonic-gate * (c) EXPORT,f1(f2) (n) PSEUDO (f1,f2)
5850Sstevel@tonic-gate * | vis: "e","d" | vis: m,m,,p,q,"o"
5860Sstevel@tonic-gate * | |
5870Sstevel@tonic-gate * ------------------ -------------------
5880Sstevel@tonic-gate * | | | | |
5890Sstevel@tonic-gate * (d) (e) f m EXPORT,f1(f2) p
5900Sstevel@tonic-gate * EXPORT EXPORT | |
5910Sstevel@tonic-gate * f1 f2 | |
5920Sstevel@tonic-gate * | | |
5930Sstevel@tonic-gate * j (o) EXPORT,f2 q EXPORT f2
5940Sstevel@tonic-gate *
5950Sstevel@tonic-gate */
5960Sstevel@tonic-gate int
treeclimb_export(struct exportinfo * exip)5970Sstevel@tonic-gate treeclimb_export(struct exportinfo *exip)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate vnode_t *dvp, *vp;
6000Sstevel@tonic-gate fid_t fid;
6010Sstevel@tonic-gate int error;
6020Sstevel@tonic-gate int exportdir;
6030Sstevel@tonic-gate struct exportinfo *exi = NULL;
6046236Spf199842 struct exportinfo *new_exi = exip;
6050Sstevel@tonic-gate struct exp_visible *visp;
6060Sstevel@tonic-gate struct exp_visible *vis_head = NULL;
6070Sstevel@tonic-gate struct vattr va;
6086236Spf199842 treenode_t *tree_head = NULL;
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&exported_lock));
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate vp = exip->exi_vp;
6130Sstevel@tonic-gate VN_HOLD(vp);
6140Sstevel@tonic-gate exportdir = 1;
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate for (;;) {
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate bzero(&fid, sizeof (fid));
6190Sstevel@tonic-gate fid.fid_len = MAXFIDSZ;
6200Sstevel@tonic-gate error = vop_fid_pseudo(vp, &fid);
6210Sstevel@tonic-gate if (error)
6220Sstevel@tonic-gate break;
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate if (! exportdir) {
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate * Check if this exportroot is a VROOT dir. If so,
6270Sstevel@tonic-gate * then attach the pseudonodes. If not, then
6280Sstevel@tonic-gate * continue .. traversal until we hit a VROOT
6290Sstevel@tonic-gate * export (pseudo or real).
6300Sstevel@tonic-gate */
6310Sstevel@tonic-gate exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid, vp);
6320Sstevel@tonic-gate if (exi != NULL && vp->v_flag & VROOT) {
6330Sstevel@tonic-gate /*
6340Sstevel@tonic-gate * Found an export info
6350Sstevel@tonic-gate *
6360Sstevel@tonic-gate * Extend the list of visible
6370Sstevel@tonic-gate * directories whether it's a pseudo
6380Sstevel@tonic-gate * or a real export.
6390Sstevel@tonic-gate */
64011796SPavel.Filipensky@Sun.COM more_visible(exi, tree_head);
6410Sstevel@tonic-gate break; /* and climb no further */
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate * If at the root of the filesystem, need
6470Sstevel@tonic-gate * to traverse across the mountpoint
6480Sstevel@tonic-gate * and continue the climb on the mounted-on
6490Sstevel@tonic-gate * filesystem.
6500Sstevel@tonic-gate */
6510Sstevel@tonic-gate if (vp->v_flag & VROOT) {
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate if (! exportdir) {
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate * Found the root directory of a filesystem
6560Sstevel@tonic-gate * that isn't exported. Need to export
6570Sstevel@tonic-gate * this as a pseudo export so that an NFS v4
6580Sstevel@tonic-gate * client can do lookups in it.
6590Sstevel@tonic-gate */
660*12679SPavel.Filipensky@Sun.COM new_exi = pseudo_exportfs(vp, &fid, vis_head,
661*12679SPavel.Filipensky@Sun.COM NULL);
6620Sstevel@tonic-gate vis_head = NULL;
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate if (VN_CMP(vp, rootdir)) {
6660Sstevel@tonic-gate /* at system root */
6676236Spf199842 /*
6686236Spf199842 * If sharing "/", new_exi is shared exportinfo
6696236Spf199842 * (exip). Otherwise, new_exi is exportinfo
6706236Spf199842 * created in pseudo_exportfs() above.
6716236Spf199842 */
6726236Spf199842 ns_root = tree_prepend_node(tree_head, 0,
6736236Spf199842 new_exi);
6740Sstevel@tonic-gate break;
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate vp = untraverse(vp);
6780Sstevel@tonic-gate exportdir = 0;
6790Sstevel@tonic-gate continue;
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate /*
6830Sstevel@tonic-gate * Do a getattr to obtain the nodeid (inode num)
6840Sstevel@tonic-gate * for this vnode.
6850Sstevel@tonic-gate */
6860Sstevel@tonic-gate va.va_mask = AT_NODEID;
6875331Samw error = VOP_GETATTR(vp, &va, 0, CRED(), NULL);
6889654SPavel.Filipensky@Sun.COM if (error)
6890Sstevel@tonic-gate break;
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate /*
6920Sstevel@tonic-gate * Add this directory fid to visible list
6930Sstevel@tonic-gate */
6940Sstevel@tonic-gate visp = kmem_alloc(sizeof (*visp), KM_SLEEP);
6950Sstevel@tonic-gate VN_HOLD(vp);
6960Sstevel@tonic-gate visp->vis_vp = vp;
6970Sstevel@tonic-gate visp->vis_fid = fid; /* structure copy */
6980Sstevel@tonic-gate visp->vis_ino = va.va_nodeid;
6990Sstevel@tonic-gate visp->vis_count = 1;
7000Sstevel@tonic-gate visp->vis_exported = exportdir;
7014871Sjasmith visp->vis_secinfo = NULL;
7024871Sjasmith visp->vis_seccnt = 0;
7030Sstevel@tonic-gate visp->vis_next = vis_head;
7040Sstevel@tonic-gate vis_head = visp;
7050Sstevel@tonic-gate
7066236Spf199842
7076236Spf199842 /*
7086236Spf199842 * Will set treenode's pointer to exportinfo to
7096236Spf199842 * 1. shared exportinfo (exip) - if first visit here
7106236Spf199842 * 2. freshly allocated pseudo export (if any)
7116236Spf199842 * 3. null otherwise
7126236Spf199842 */
7136236Spf199842 tree_head = tree_prepend_node(tree_head, visp, new_exi);
7146236Spf199842 new_exi = NULL;
7156236Spf199842
7160Sstevel@tonic-gate /*
7170Sstevel@tonic-gate * Now, do a ".." to find parent dir of vp.
7180Sstevel@tonic-gate */
7195331Samw error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, CRED(),
7205331Samw NULL, NULL, NULL);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate if (error == ENOTDIR && exportdir) {
7230Sstevel@tonic-gate dvp = exip->exi_dvp;
7240Sstevel@tonic-gate ASSERT(dvp != NULL);
7250Sstevel@tonic-gate VN_HOLD(dvp);
7260Sstevel@tonic-gate error = 0;
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate if (error)
7300Sstevel@tonic-gate break;
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate exportdir = 0;
7330Sstevel@tonic-gate VN_RELE(vp);
7340Sstevel@tonic-gate vp = dvp;
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate VN_RELE(vp);
7386236Spf199842
7396236Spf199842 /*
7406236Spf199842 * We can have set error due to error in:
7416236Spf199842 * 1. vop_fid_pseudo()
742*12679SPavel.Filipensky@Sun.COM * 2. VOP_GETATTR()
743*12679SPavel.Filipensky@Sun.COM * 3. VOP_LOOKUP()
7449654SPavel.Filipensky@Sun.COM * We must free pseudo exportinfos, visibles and treenodes.
7459654SPavel.Filipensky@Sun.COM * Visibles are referenced from treenode_t::tree_vis and
7469654SPavel.Filipensky@Sun.COM * exportinfo_t::exi_visible. To avoid double freeing, only
7479654SPavel.Filipensky@Sun.COM * exi_visible pointer is used, via exi_rele(), for the clean-up.
7486236Spf199842 */
7496236Spf199842 if (error) {
7509654SPavel.Filipensky@Sun.COM /* Free unconnected visibles, if there are any. */
7519654SPavel.Filipensky@Sun.COM if (vis_head)
7529654SPavel.Filipensky@Sun.COM free_visible(vis_head);
7539654SPavel.Filipensky@Sun.COM
7549654SPavel.Filipensky@Sun.COM /* Connect unconnected exportinfo, if there is any. */
7559654SPavel.Filipensky@Sun.COM if (new_exi && new_exi != exip)
7569654SPavel.Filipensky@Sun.COM tree_head = tree_prepend_node(tree_head, 0, new_exi);
7579654SPavel.Filipensky@Sun.COM
7586236Spf199842 while (tree_head) {
7596236Spf199842 treenode_t *t2 = tree_head;
7606236Spf199842 exportinfo_t *e = tree_head->tree_exi;
7616236Spf199842 /* exip will be freed in exportfs() */
7626236Spf199842 if (e && e != exip) {
763*12679SPavel.Filipensky@Sun.COM export_unlink(e);
7646236Spf199842 exi_rele(e);
7656236Spf199842 }
7666236Spf199842 tree_head = tree_head->tree_child_first;
7676236Spf199842 kmem_free(t2, sizeof (*t2));
7686236Spf199842 }
7696236Spf199842 }
7706236Spf199842
7710Sstevel@tonic-gate return (error);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate
7740Sstevel@tonic-gate /*
7756236Spf199842 * Walk up the tree and:
7766236Spf199842 * 1. release pseudo exportinfo if it has no child
7776236Spf199842 * 2. release visible in parent's exportinfo
7786236Spf199842 * 3. delete non-exported leaf nodes from tree
7790Sstevel@tonic-gate *
7806236Spf199842 * Deleting of nodes will start only if the unshared
7816236Spf199842 * node was a leaf node.
7826236Spf199842 * Deleting of nodes will finish when we reach a node which
7836236Spf199842 * has children or is a real export, then we might still need
7846236Spf199842 * to continue releasing visibles, until we reach VROOT node.
7850Sstevel@tonic-gate */
7866236Spf199842 void
treeclimb_unexport(struct exportinfo * exip)7870Sstevel@tonic-gate treeclimb_unexport(struct exportinfo *exip)
7880Sstevel@tonic-gate {
7896236Spf199842 treenode_t *tnode, *old_nd;
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&exported_lock));
7920Sstevel@tonic-gate
7936236Spf199842 tnode = exip->exi_tree;
7946236Spf199842 /*
7956236Spf199842 * The unshared exportinfo was unlinked in unexport().
7966236Spf199842 * Zeroing tree_exi ensures that we will skip it.
7976236Spf199842 */
7986236Spf199842 tnode->tree_exi = NULL;
7990Sstevel@tonic-gate
80012134SPavel.Filipensky@Sun.COM if (tnode->tree_vis) /* system root has tree_vis == NULL */
80112134SPavel.Filipensky@Sun.COM tnode->tree_vis->vis_exported = 0;
80212134SPavel.Filipensky@Sun.COM
8036236Spf199842 while (tnode) {
8040Sstevel@tonic-gate
8056236Spf199842 /* Stop at VROOT node which is exported or has child */
8066236Spf199842 if (TREE_ROOT(tnode) &&
8076236Spf199842 (TREE_EXPORTED(tnode) || tnode->tree_child_first))
8080Sstevel@tonic-gate break;
8090Sstevel@tonic-gate
8106236Spf199842 /* Release pseudo export if it has no child */
8116236Spf199842 if (TREE_ROOT(tnode) && !TREE_EXPORTED(tnode) &&
8126236Spf199842 tnode->tree_child_first == 0) {
813*12679SPavel.Filipensky@Sun.COM export_unlink(tnode->tree_exi);
8146236Spf199842 exi_rele(tnode->tree_exi);
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate
8176236Spf199842 /* Release visible in parent's exportinfo */
818*12679SPavel.Filipensky@Sun.COM if (tnode->tree_vis)
819*12679SPavel.Filipensky@Sun.COM less_visible(vis2exi(tnode), tnode->tree_vis);
8200Sstevel@tonic-gate
8216236Spf199842 /* Continue with parent */
8226236Spf199842 old_nd = tnode;
8236236Spf199842 tnode = tnode->tree_parent;
8240Sstevel@tonic-gate
8256236Spf199842 /* Remove itself, if this is a leaf and non-exported node */
8266236Spf199842 if (old_nd->tree_child_first == NULL && !TREE_EXPORTED(old_nd))
8276236Spf199842 tree_remove_node(old_nd);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate * Traverse backward across mountpoint from the
8330Sstevel@tonic-gate * root vnode of a filesystem to its mounted-on
8340Sstevel@tonic-gate * vnode.
8350Sstevel@tonic-gate */
8360Sstevel@tonic-gate vnode_t *
untraverse(vnode_t * vp)8370Sstevel@tonic-gate untraverse(vnode_t *vp)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate vnode_t *tvp, *nextvp;
8400Sstevel@tonic-gate
8410Sstevel@tonic-gate tvp = vp;
8420Sstevel@tonic-gate for (;;) {
8430Sstevel@tonic-gate if (! (tvp->v_flag & VROOT))
8440Sstevel@tonic-gate break;
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate /* lock vfs to prevent unmount of this vfs */
8470Sstevel@tonic-gate vfs_lock_wait(tvp->v_vfsp);
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate if ((nextvp = tvp->v_vfsp->vfs_vnodecovered) == NULL) {
8500Sstevel@tonic-gate vfs_unlock(tvp->v_vfsp);
8510Sstevel@tonic-gate break;
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate /*
8550Sstevel@tonic-gate * Hold nextvp to prevent unmount. After unlock vfs and
8560Sstevel@tonic-gate * rele tvp, any number of overlays could be unmounted.
8570Sstevel@tonic-gate * Putting a hold on vfs_vnodecovered will only allow
8580Sstevel@tonic-gate * tvp's vfs to be unmounted. Of course if caller placed
8590Sstevel@tonic-gate * extra hold on vp before calling untraverse, the following
8600Sstevel@tonic-gate * hold would not be needed. Since prev actions of caller
8610Sstevel@tonic-gate * are unknown, we need to hold here just to be safe.
8620Sstevel@tonic-gate */
8630Sstevel@tonic-gate VN_HOLD(nextvp);
8640Sstevel@tonic-gate vfs_unlock(tvp->v_vfsp);
8650Sstevel@tonic-gate VN_RELE(tvp);
8660Sstevel@tonic-gate tvp = nextvp;
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate return (tvp);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate /*
8730Sstevel@tonic-gate * Given an exportinfo, climb up to find the exportinfo for the VROOT
8740Sstevel@tonic-gate * of the filesystem.
8750Sstevel@tonic-gate *
8760Sstevel@tonic-gate * e.g. /
8770Sstevel@tonic-gate * |
8780Sstevel@tonic-gate * a (VROOT) pseudo-exportinfo
8790Sstevel@tonic-gate * |
8800Sstevel@tonic-gate * b
8810Sstevel@tonic-gate * |
8820Sstevel@tonic-gate * c #share /a/b/c
8830Sstevel@tonic-gate * |
8840Sstevel@tonic-gate * d
8850Sstevel@tonic-gate *
8860Sstevel@tonic-gate * where c is in the same filesystem as a.
8870Sstevel@tonic-gate * So, get_root_export(*exportinfo_for_c) returns exportinfo_for_a
8880Sstevel@tonic-gate *
8890Sstevel@tonic-gate * If d is shared, then c will be put into a's visible list.
8900Sstevel@tonic-gate * Note: visible list is per filesystem and is attached to the
8910Sstevel@tonic-gate * VROOT exportinfo.
8920Sstevel@tonic-gate */
8930Sstevel@tonic-gate struct exportinfo *
get_root_export(struct exportinfo * exip)8940Sstevel@tonic-gate get_root_export(struct exportinfo *exip)
8950Sstevel@tonic-gate {
89612134SPavel.Filipensky@Sun.COM treenode_t *tnode = exip->exi_tree;
89712134SPavel.Filipensky@Sun.COM exportinfo_t *exi = NULL;
8980Sstevel@tonic-gate
89912134SPavel.Filipensky@Sun.COM while (tnode) {
90012134SPavel.Filipensky@Sun.COM if (TREE_ROOT(tnode)) {
90112134SPavel.Filipensky@Sun.COM exi = tnode->tree_exi;
9020Sstevel@tonic-gate break;
9030Sstevel@tonic-gate }
90412134SPavel.Filipensky@Sun.COM tnode = tnode->tree_parent;
9050Sstevel@tonic-gate }
90612134SPavel.Filipensky@Sun.COM ASSERT(exi);
9070Sstevel@tonic-gate return (exi);
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate /*
9110Sstevel@tonic-gate * Return true if the supplied vnode has a sub-directory exported.
9120Sstevel@tonic-gate */
9130Sstevel@tonic-gate int
has_visible(struct exportinfo * exi,vnode_t * vp)9140Sstevel@tonic-gate has_visible(struct exportinfo *exi, vnode_t *vp)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate struct exp_visible *visp;
9170Sstevel@tonic-gate fid_t fid;
9180Sstevel@tonic-gate bool_t vp_is_exported;
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate vp_is_exported = VN_CMP(vp, exi->exi_vp);
9210Sstevel@tonic-gate
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate * An exported root vnode has a sub-dir shared if it has a visible list.
9240Sstevel@tonic-gate * i.e. if it does not have a visible list, then there is no node in
9250Sstevel@tonic-gate * this filesystem leads to any other shared node.
9260Sstevel@tonic-gate */
9270Sstevel@tonic-gate if (vp_is_exported && (vp->v_flag & VROOT))
9280Sstevel@tonic-gate return (exi->exi_visible ? 1 : 0);
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate /*
9310Sstevel@tonic-gate * Only the exportinfo of a fs root node may have a visible list.
9320Sstevel@tonic-gate * Either it is a pseudo root node, or a real exported root node.
9330Sstevel@tonic-gate */
93412134SPavel.Filipensky@Sun.COM exi = get_root_export(exi);
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate if (!exi->exi_visible)
9370Sstevel@tonic-gate return (0);
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate /* Get the fid of the vnode */
9400Sstevel@tonic-gate bzero(&fid, sizeof (fid));
9410Sstevel@tonic-gate fid.fid_len = MAXFIDSZ;
9420Sstevel@tonic-gate if (vop_fid_pseudo(vp, &fid) != 0) {
9430Sstevel@tonic-gate return (0);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate /*
9470Sstevel@tonic-gate * See if vp is in the visible list of the root node exportinfo.
9480Sstevel@tonic-gate */
9490Sstevel@tonic-gate for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
9500Sstevel@tonic-gate if (EQFID(&fid, &visp->vis_fid)) {
9510Sstevel@tonic-gate /*
9520Sstevel@tonic-gate * If vp is an exported non-root node with only 1 path
9530Sstevel@tonic-gate * count (for itself), it indicates no sub-dir shared
9540Sstevel@tonic-gate * using this vp as a path.
9550Sstevel@tonic-gate */
9560Sstevel@tonic-gate if (vp_is_exported && visp->vis_count < 2)
9570Sstevel@tonic-gate break;
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate return (1);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate return (0);
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate
9660Sstevel@tonic-gate /*
9670Sstevel@tonic-gate * Returns true if the supplied vnode is visible
9680Sstevel@tonic-gate * in this export. If vnode is visible, return
9690Sstevel@tonic-gate * vis_exported in expseudo.
9700Sstevel@tonic-gate */
9710Sstevel@tonic-gate int
nfs_visible(struct exportinfo * exi,vnode_t * vp,int * expseudo)9720Sstevel@tonic-gate nfs_visible(struct exportinfo *exi, vnode_t *vp, int *expseudo)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate struct exp_visible *visp;
9750Sstevel@tonic-gate fid_t fid;
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate * First check to see if vp is export root.
9790Sstevel@tonic-gate *
9800Sstevel@tonic-gate * A pseudo export root can never be exported
9810Sstevel@tonic-gate * (it would be a real export then); however,
9820Sstevel@tonic-gate * it is always visible. If a pseudo root object
9830Sstevel@tonic-gate * was exported by server admin, then the entire
9840Sstevel@tonic-gate * pseudo exportinfo (and all visible entries) would
9850Sstevel@tonic-gate * be destroyed. A pseudo exportinfo only exists
9860Sstevel@tonic-gate * to provide access to real (descendant) export(s).
9870Sstevel@tonic-gate *
9880Sstevel@tonic-gate * Previously, rootdir was special cased here; however,
9890Sstevel@tonic-gate * the export root special case handles the rootdir
9900Sstevel@tonic-gate * case also.
9910Sstevel@tonic-gate */
9920Sstevel@tonic-gate if (VN_CMP(vp, exi->exi_vp)) {
9930Sstevel@tonic-gate *expseudo = 0;
9940Sstevel@tonic-gate return (1);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate
9970Sstevel@tonic-gate /*
9980Sstevel@tonic-gate * Only a PSEUDO node has a visible list or an exported VROOT
9990Sstevel@tonic-gate * node may have a visible list.
10000Sstevel@tonic-gate */
100112134SPavel.Filipensky@Sun.COM if (! PSEUDO(exi))
100212134SPavel.Filipensky@Sun.COM exi = get_root_export(exi);
10030Sstevel@tonic-gate
10040Sstevel@tonic-gate /* Get the fid of the vnode */
10050Sstevel@tonic-gate
10060Sstevel@tonic-gate bzero(&fid, sizeof (fid));
10070Sstevel@tonic-gate fid.fid_len = MAXFIDSZ;
10080Sstevel@tonic-gate if (vop_fid_pseudo(vp, &fid) != 0) {
10090Sstevel@tonic-gate *expseudo = 0;
10100Sstevel@tonic-gate return (0);
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate /*
10140Sstevel@tonic-gate * We can't trust VN_CMP() above because of LOFS.
10150Sstevel@tonic-gate * Even though VOP_CMP will do the right thing for LOFS
10160Sstevel@tonic-gate * objects, VN_CMP will short circuit out early when the
10170Sstevel@tonic-gate * vnode ops ptrs are different. Just in case we're dealing
10180Sstevel@tonic-gate * with LOFS, compare exi_fid/fsid here.
10190Sstevel@tonic-gate *
10200Sstevel@tonic-gate * expseudo is not set because this is not an export
10210Sstevel@tonic-gate */
10220Sstevel@tonic-gate if (EQFID(&exi->exi_fid, &fid) &&
10230Sstevel@tonic-gate EQFSID(&exi->exi_fsid, &vp->v_vfsp->vfs_fsid)) {
10240Sstevel@tonic-gate *expseudo = 0;
10250Sstevel@tonic-gate return (1);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate /* See if it matches any fid in the visible list */
10300Sstevel@tonic-gate
10310Sstevel@tonic-gate for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
10320Sstevel@tonic-gate if (EQFID(&fid, &visp->vis_fid)) {
10330Sstevel@tonic-gate *expseudo = visp->vis_exported;
10340Sstevel@tonic-gate return (1);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate *expseudo = 0;
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate return (0);
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate
10430Sstevel@tonic-gate /*
10440Sstevel@tonic-gate * Returns true if the supplied vnode is the
10450Sstevel@tonic-gate * directory of an export point.
10460Sstevel@tonic-gate */
10470Sstevel@tonic-gate int
nfs_exported(struct exportinfo * exi,vnode_t * vp)10480Sstevel@tonic-gate nfs_exported(struct exportinfo *exi, vnode_t *vp)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate struct exp_visible *visp;
10510Sstevel@tonic-gate fid_t fid;
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate /*
10540Sstevel@tonic-gate * First check to see if vp is the export root
10550Sstevel@tonic-gate * This check required for the case of lookup ..
10560Sstevel@tonic-gate * where .. is a V_ROOT vnode and a pseudo exportroot.
10570Sstevel@tonic-gate * Pseudo export root objects do not have an entry
10580Sstevel@tonic-gate * in the visible list even though every V_ROOT
10590Sstevel@tonic-gate * pseudonode is visible. It is safe to compare
10600Sstevel@tonic-gate * vp here because pseudo_exportfs put a hold on
10610Sstevel@tonic-gate * it when exi_vp was initialized.
10620Sstevel@tonic-gate *
10630Sstevel@tonic-gate * Note: VN_CMP() won't match for LOFS shares, but they're
10640Sstevel@tonic-gate * handled below w/EQFID/EQFSID.
10650Sstevel@tonic-gate */
10660Sstevel@tonic-gate if (VN_CMP(vp, exi->exi_vp))
10670Sstevel@tonic-gate return (1);
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate /* Get the fid of the vnode */
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate bzero(&fid, sizeof (fid));
10720Sstevel@tonic-gate fid.fid_len = MAXFIDSZ;
10730Sstevel@tonic-gate if (vop_fid_pseudo(vp, &fid) != 0)
10740Sstevel@tonic-gate return (0);
10750Sstevel@tonic-gate
10760Sstevel@tonic-gate if (EQFID(&fid, &exi->exi_fid) &&
10770Sstevel@tonic-gate EQFSID(&vp->v_vfsp->vfs_fsid, &exi->exi_fsid)) {
10780Sstevel@tonic-gate return (1);
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate
10810Sstevel@tonic-gate /* See if it matches any fid in the visible list */
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
10840Sstevel@tonic-gate if (EQFID(&fid, &visp->vis_fid))
10850Sstevel@tonic-gate return (visp->vis_exported);
10860Sstevel@tonic-gate }
10870Sstevel@tonic-gate
10880Sstevel@tonic-gate return (0);
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate
10910Sstevel@tonic-gate /*
10920Sstevel@tonic-gate * Returns true if the supplied inode is visible
10930Sstevel@tonic-gate * in this export. This function is used by
10940Sstevel@tonic-gate * readdir which uses inode numbers from the
10950Sstevel@tonic-gate * directory.
10960Sstevel@tonic-gate *
10970Sstevel@tonic-gate * NOTE: this code does not match inode number for ".",
10980Sstevel@tonic-gate * but it isn't required because NFS4 server rddir
10990Sstevel@tonic-gate * skips . and .. entries.
11000Sstevel@tonic-gate */
11010Sstevel@tonic-gate int
nfs_visible_inode(struct exportinfo * exi,ino64_t ino,int * expseudo)11020Sstevel@tonic-gate nfs_visible_inode(struct exportinfo *exi, ino64_t ino, int *expseudo)
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate struct exp_visible *visp;
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate /*
11070Sstevel@tonic-gate * Only a PSEUDO node has a visible list or an exported VROOT
11080Sstevel@tonic-gate * node may have a visible list.
11090Sstevel@tonic-gate */
111012134SPavel.Filipensky@Sun.COM if (! PSEUDO(exi))
111112134SPavel.Filipensky@Sun.COM exi = get_root_export(exi);
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate for (visp = exi->exi_visible; visp; visp = visp->vis_next)
11140Sstevel@tonic-gate if ((u_longlong_t)ino == visp->vis_ino) {
11150Sstevel@tonic-gate *expseudo = visp->vis_exported;
11160Sstevel@tonic-gate return (1);
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate *expseudo = 0;
11200Sstevel@tonic-gate return (0);
11210Sstevel@tonic-gate }
1122