17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
527242a7cSthurlow  * Common Development and Distribution License (the "License").
627242a7cSthurlow  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2071da0c32SMarcel Telka  */
2171da0c32SMarcel Telka 
2271da0c32SMarcel Telka /*
2308214031SPavel Filipensky  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24*0dfe541eSEvan Layton  */
25*0dfe541eSEvan Layton 
26*0dfe541eSEvan Layton /*
27*0dfe541eSEvan Layton  * Copyright 2018 Nexenta Systems, Inc.
28e2fc3408SPatrick Mooney  * Copyright (c) 2015, Joyent, Inc.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
347c478bd9Sstevel@tonic-gate #include <nfs/export.h>
357c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
3671da0c32SMarcel Telka #include <sys/avl.h>
377c478bd9Sstevel@tonic-gate 
38af9f9769Sjasmith #define	PSEUDOFS_SUFFIX		" (pseudo)"
39af9f9769Sjasmith 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * A version of VOP_FID that deals with a remote VOP_FID for nfs.
427c478bd9Sstevel@tonic-gate  * If vp is an nfs node, nfs4_fid() returns EREMOTE, nfs3_fid() and nfs_fid()
437c478bd9Sstevel@tonic-gate  * returns the filehandle of vp as its fid. When nfs uses fid to set the
447c478bd9Sstevel@tonic-gate  * exportinfo filehandle template, a remote nfs filehandle would be too big for
457c478bd9Sstevel@tonic-gate  * the fid of the exported directory. This routine remaps the value of the
467c478bd9Sstevel@tonic-gate  * attribute va_nodeid of vp to be the fid of vp, so that the fid can fit.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * We need this fid mainly for setting up NFSv4 server namespace where an
497c478bd9Sstevel@tonic-gate  * nfs filesystem is also part of it. Thus, need to be able to setup a pseudo
507c478bd9Sstevel@tonic-gate  * exportinfo for an nfs node.
517c478bd9Sstevel@tonic-gate  *
52af9f9769Sjasmith  * e.g. mount a filesystem on top of a nfs dir, and then share the new mount
53af9f9769Sjasmith  *      (like exporting a local disk from a "diskless" client)
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate int
vop_fid_pseudo(vnode_t * vp,fid_t * fidp)567c478bd9Sstevel@tonic-gate vop_fid_pseudo(vnode_t *vp, fid_t *fidp)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	struct vattr va;
597c478bd9Sstevel@tonic-gate 	int error;
607c478bd9Sstevel@tonic-gate 
61da6c28aaSamw 	error = VOP_FID(vp, fidp, NULL);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * XXX nfs4_fid() does nothing and returns EREMOTE.
657c478bd9Sstevel@tonic-gate 	 * XXX nfs3_fid()/nfs_fid() returns nfs filehandle as its fid
667c478bd9Sstevel@tonic-gate 	 * which has a bigger length than local fid.
6727242a7cSthurlow 	 * NFS_FH4MAXDATA is the size of
6827242a7cSthurlow 	 * fhandle4_t.fh_xdata[NFS_FH4MAXDATA].
697c46fb7fSek 	 *
707c46fb7fSek 	 * Note: nfs[2,3,4]_fid() only gets called for diskless clients.
717c478bd9Sstevel@tonic-gate 	 */
727c46fb7fSek 	if (error == EREMOTE ||
7327242a7cSthurlow 	    (error == 0 && fidp->fid_len > NFS_FH4MAXDATA)) {
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		va.va_mask = AT_NODEID;
76da6c28aaSamw 		error = VOP_GETATTR(vp, &va, 0, CRED(), NULL);
777c478bd9Sstevel@tonic-gate 		if (error)
787c478bd9Sstevel@tonic-gate 			return (error);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		fidp->fid_len = sizeof (va.va_nodeid);
817c478bd9Sstevel@tonic-gate 		bcopy(&va.va_nodeid, fidp->fid_data, fidp->fid_len);
827c478bd9Sstevel@tonic-gate 		return (0);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	return (error);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Get an nfsv4 vnode of the given fid from the visible list of an
907c478bd9Sstevel@tonic-gate  * nfs filesystem or get the exi_vp if it is the root node.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int
nfs4_vget_pseudo(struct exportinfo * exi,vnode_t ** vpp,fid_t * fidp)937c478bd9Sstevel@tonic-gate nfs4_vget_pseudo(struct exportinfo *exi, vnode_t **vpp, fid_t *fidp)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	fid_t exp_fid;
967c478bd9Sstevel@tonic-gate 	struct exp_visible *visp;
977c478bd9Sstevel@tonic-gate 	int error;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	/* check if the given fid is in the visible list */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
1027c478bd9Sstevel@tonic-gate 		if (EQFID(fidp, &visp->vis_fid)) {
1037c478bd9Sstevel@tonic-gate 			VN_HOLD(visp->vis_vp);
1047c478bd9Sstevel@tonic-gate 			*vpp = visp->vis_vp;
1057c478bd9Sstevel@tonic-gate 			return (0);
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* check if the given fid is the same as the exported node */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	bzero(&exp_fid, sizeof (exp_fid));
1127c478bd9Sstevel@tonic-gate 	exp_fid.fid_len = MAXFIDSZ;
1137c478bd9Sstevel@tonic-gate 	error = vop_fid_pseudo(exi->exi_vp, &exp_fid);
1147c478bd9Sstevel@tonic-gate 	if (error)
1157c478bd9Sstevel@tonic-gate 		return (error);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (EQFID(fidp, &exp_fid)) {
1187c478bd9Sstevel@tonic-gate 		VN_HOLD(exi->exi_vp);
1197c478bd9Sstevel@tonic-gate 		*vpp = exi->exi_vp;
1207c478bd9Sstevel@tonic-gate 		return (0);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	return (ENOENT);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Create a pseudo export entry
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * This is an export entry that's created as the
1307c478bd9Sstevel@tonic-gate  * side-effect of a "real" export.  As a part of
1317c478bd9Sstevel@tonic-gate  * a real export, the pathname to the export is
1327c478bd9Sstevel@tonic-gate  * checked to see if all the directory components
1337c478bd9Sstevel@tonic-gate  * are accessible via an NFSv4 client, i.e. are
134af9f9769Sjasmith  * exported.  If treeclimb_export() finds an unexported
1357c478bd9Sstevel@tonic-gate  * mountpoint along the path, then it calls this
1367c478bd9Sstevel@tonic-gate  * function to export it.
1377c478bd9Sstevel@tonic-gate  *
138af9f9769Sjasmith  * This pseudo export differs from a real export in that
139af9f9769Sjasmith  * it only allows read-only access.  A "visible" list of
140af9f9769Sjasmith  * directories is added to filter lookup and readdir results
141af9f9769Sjasmith  * to only contain dirnames which lead to descendant shares.
1427c478bd9Sstevel@tonic-gate  *
143af9f9769Sjasmith  * A visible list has a per-file-system scope.  Any exportinfo
144af9f9769Sjasmith  * struct (real or pseudo) can have a visible list as long as
145*0dfe541eSEvan Layton  * a) its export root is VROOT, or is the zone's root for in-zone NFS service
146af9f9769Sjasmith  * b) a descendant of the export root is shared
1477c478bd9Sstevel@tonic-gate  */
1480616fd7fSPavel Filipensky struct exportinfo *
pseudo_exportfs(nfs_export_t * ne,vnode_t * vp,fid_t * fid,struct exp_visible * vis_head,struct exportdata * exdata)149*0dfe541eSEvan Layton pseudo_exportfs(nfs_export_t *ne, vnode_t *vp, fid_t *fid,
150*0dfe541eSEvan Layton     struct exp_visible *vis_head, struct exportdata *exdata)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	struct exportinfo *exi;
1537c478bd9Sstevel@tonic-gate 	struct exportdata *kex;
1547c478bd9Sstevel@tonic-gate 	fsid_t fsid;
1550616fd7fSPavel Filipensky 	int vpathlen;
15671da0c32SMarcel Telka 	int i;
1577c478bd9Sstevel@tonic-gate 
158*0dfe541eSEvan Layton 	ASSERT(RW_WRITE_HELD(&ne->exported_lock));
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	fsid = vp->v_vfsp->vfs_fsid;
1617c478bd9Sstevel@tonic-gate 	exi = kmem_zalloc(sizeof (*exi), KM_SLEEP);
1627c478bd9Sstevel@tonic-gate 	exi->exi_fsid = fsid;
1630616fd7fSPavel Filipensky 	exi->exi_fid = *fid;
1647c478bd9Sstevel@tonic-gate 	exi->exi_vp = vp;
1657c478bd9Sstevel@tonic-gate 	VN_HOLD(exi->exi_vp);
1667c478bd9Sstevel@tonic-gate 	exi->exi_visible = vis_head;
1677c478bd9Sstevel@tonic-gate 	exi->exi_count = 1;
168*0dfe541eSEvan Layton 	exi->exi_zoneid = ne->ne_globals->nfs_zoneid;
1697c478bd9Sstevel@tonic-gate 	exi->exi_volatile_dev = (vfssw[vp->v_vfsp->vfs_fstype].vsw_flag &
170af9f9769Sjasmith 	    VSW_VOLATILEDEV) ? 1 : 0;
1717c478bd9Sstevel@tonic-gate 	mutex_init(&exi->exi_lock, NULL, MUTEX_DEFAULT, NULL);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Build up the template fhandle
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	exi->exi_fh.fh_fsid = fsid;
1777c478bd9Sstevel@tonic-gate 	ASSERT(exi->exi_fid.fid_len <= sizeof (exi->exi_fh.fh_xdata));
1787c478bd9Sstevel@tonic-gate 	exi->exi_fh.fh_xlen = exi->exi_fid.fid_len;
1797c478bd9Sstevel@tonic-gate 	bcopy(exi->exi_fid.fid_data, exi->exi_fh.fh_xdata,
1807c478bd9Sstevel@tonic-gate 	    exi->exi_fid.fid_len);
1817c478bd9Sstevel@tonic-gate 	exi->exi_fh.fh_len = sizeof (exi->exi_fh.fh_data);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	kex = &exi->exi_export;
1847c478bd9Sstevel@tonic-gate 	kex->ex_flags = EX_PSEUDO;
1857c478bd9Sstevel@tonic-gate 
186e2fc3408SPatrick Mooney 	vpathlen = strlen(vp->v_path);
187af9f9769Sjasmith 	kex->ex_pathlen = vpathlen + strlen(PSEUDOFS_SUFFIX);
1887c478bd9Sstevel@tonic-gate 	kex->ex_path = kmem_alloc(kex->ex_pathlen + 1, KM_SLEEP);
189af9f9769Sjasmith 
190af9f9769Sjasmith 	if (vpathlen)
191e2fc3408SPatrick Mooney 		(void) strncpy(kex->ex_path, vp->v_path, vpathlen);
192af9f9769Sjasmith 	(void) strcpy(kex->ex_path + vpathlen, PSEUDOFS_SUFFIX);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* Transfer the secinfo data from exdata to this new pseudo node */
1957c478bd9Sstevel@tonic-gate 	if (exdata)
1967c478bd9Sstevel@tonic-gate 		srv_secinfo_exp2pseu(&exi->exi_export, exdata);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/*
19971da0c32SMarcel Telka 	 * Initialize auth cache and auth cache lock
2007c478bd9Sstevel@tonic-gate 	 */
20171da0c32SMarcel Telka 	for (i = 0; i < AUTH_TABLESIZE; i++) {
20271da0c32SMarcel Telka 		exi->exi_cache[i] = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
20371da0c32SMarcel Telka 		avl_create(exi->exi_cache[i], nfsauth_cache_clnt_compar,
20471da0c32SMarcel Telka 		    sizeof (struct auth_cache_clnt),
20571da0c32SMarcel Telka 		    offsetof(struct auth_cache_clnt, authc_link));
20671da0c32SMarcel Telka 	}
2077c478bd9Sstevel@tonic-gate 	rw_init(&exi->exi_cache_lock, NULL, RW_DEFAULT, NULL);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Insert the new entry at the front of the export list
2117c478bd9Sstevel@tonic-gate 	 */
212*0dfe541eSEvan Layton 	export_link(ne, exi);
213*0dfe541eSEvan Layton 
214*0dfe541eSEvan Layton 	/*
215*0dfe541eSEvan Layton 	 * Initialize exi_id and exi_kstats
216*0dfe541eSEvan Layton 	 */
217*0dfe541eSEvan Layton 	mutex_enter(&nfs_exi_id_lock);
218*0dfe541eSEvan Layton 	exi->exi_id = exi_id_get_next();
219*0dfe541eSEvan Layton 	avl_add(&exi_id_tree, exi);
220*0dfe541eSEvan Layton 	mutex_exit(&nfs_exi_id_lock);
2217c478bd9Sstevel@tonic-gate 
2220616fd7fSPavel Filipensky 	return (exi);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Free a list of visible directories
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate void
free_visible(struct exp_visible * head)2297c478bd9Sstevel@tonic-gate free_visible(struct exp_visible *head)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	struct exp_visible *visp, *next;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	for (visp = head; visp; visp = next) {
2347c478bd9Sstevel@tonic-gate 		if (visp->vis_vp != NULL)
2357c478bd9Sstevel@tonic-gate 			VN_RELE(visp->vis_vp);
236af9f9769Sjasmith 
2377c478bd9Sstevel@tonic-gate 		next = visp->vis_next;
238af9f9769Sjasmith 		srv_secinfo_list_free(visp->vis_secinfo, visp->vis_seccnt);
2397c478bd9Sstevel@tonic-gate 		kmem_free(visp, sizeof (*visp));
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
243992d2facSpf /*
244992d2facSpf  * Connects newchild (or subtree with newchild in head)
245992d2facSpf  * to the parent node. We always add it to the beginning
246992d2facSpf  * of sibling list.
247992d2facSpf  */
248992d2facSpf static void
tree_add_child(treenode_t * parent,treenode_t * newchild)249992d2facSpf tree_add_child(treenode_t *parent, treenode_t *newchild)
250992d2facSpf {
251992d2facSpf 	newchild->tree_parent = parent;
252992d2facSpf 	newchild->tree_sibling = parent->tree_child_first;
253992d2facSpf 	parent->tree_child_first = newchild;
254992d2facSpf }
255992d2facSpf 
25608214031SPavel Filipensky /* Look up among direct children a node with the exact tree_vis pointer */
25708214031SPavel Filipensky static treenode_t *
tree_find_child_by_vis(treenode_t * t,exp_visible_t * vis)25808214031SPavel Filipensky tree_find_child_by_vis(treenode_t *t, exp_visible_t *vis)
25908214031SPavel Filipensky {
26008214031SPavel Filipensky 	for (t = t->tree_child_first; t; t = t->tree_sibling)
26108214031SPavel Filipensky 		if (t->tree_vis == vis)
26208214031SPavel Filipensky 			return (t);
26308214031SPavel Filipensky 	return (NULL);
26408214031SPavel Filipensky }
26508214031SPavel Filipensky 
266992d2facSpf /*
267992d2facSpf  * Add new node to the head of subtree pointed by 'n'. n can be NULL.
268992d2facSpf  * Interconnects the new treenode with exp_visible and exportinfo
269992d2facSpf  * if needed.
270992d2facSpf  */
271992d2facSpf static treenode_t *
tree_prepend_node(treenode_t * n,exp_visible_t * v,exportinfo_t * e)272992d2facSpf tree_prepend_node(treenode_t *n, exp_visible_t *v, exportinfo_t *e)
273992d2facSpf {
274992d2facSpf 	treenode_t *tnode = kmem_zalloc(sizeof (*tnode), KM_SLEEP);
275992d2facSpf 
276992d2facSpf 	if (n) {
277992d2facSpf 		tnode->tree_child_first = n;
278992d2facSpf 		n->tree_parent = tnode;
279992d2facSpf 	}
280992d2facSpf 	if (v) {
281992d2facSpf 		tnode->tree_vis = v;
282992d2facSpf 	}
283992d2facSpf 	if (e) {
284992d2facSpf 		tnode->tree_exi = e;
285992d2facSpf 		e->exi_tree = tnode;
286992d2facSpf 	}
287992d2facSpf 	return (tnode);
288992d2facSpf }
289992d2facSpf 
290992d2facSpf /*
291992d2facSpf  * Removes node from the tree and frees the treenode struct.
292992d2facSpf  * Does not free structures pointed by tree_exi and tree_vis,
293992d2facSpf  * they should be already freed.
294992d2facSpf  */
295992d2facSpf static void
tree_remove_node(nfs_export_t * ne,treenode_t * node)296*0dfe541eSEvan Layton tree_remove_node(nfs_export_t *ne, treenode_t *node)
297992d2facSpf {
298992d2facSpf 	treenode_t *parent = node->tree_parent;
299992d2facSpf 	treenode_t *s; /* s for sibling */
300992d2facSpf 
301992d2facSpf 	if (parent == NULL) {
302992d2facSpf 		kmem_free(node, sizeof (*node));
303*0dfe541eSEvan Layton 		ne->ns_root = NULL;
304992d2facSpf 		return;
305992d2facSpf 	}
306992d2facSpf 	/* This node is first child */
307992d2facSpf 	if (parent->tree_child_first == node) {
308992d2facSpf 		parent->tree_child_first = node->tree_sibling;
309992d2facSpf 	/* This node is not first child */
310992d2facSpf 	} else {
311992d2facSpf 		s = parent->tree_child_first;
312992d2facSpf 		while (s->tree_sibling != node)
313992d2facSpf 			s = s->tree_sibling;
314992d2facSpf 		s->tree_sibling = s->tree_sibling->tree_sibling;
315992d2facSpf 	}
316992d2facSpf 	kmem_free(node, sizeof (*node));
317992d2facSpf }
318992d2facSpf 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * When we export a new directory we need to add a new
3217c478bd9Sstevel@tonic-gate  * path segment through the pseudofs to reach the new
3227c478bd9Sstevel@tonic-gate  * directory. This new path is reflected in a list of
3237c478bd9Sstevel@tonic-gate  * directories added to the "visible" list.
3247c478bd9Sstevel@tonic-gate  *
3257c478bd9Sstevel@tonic-gate  * Here there are two lists of visible fids: one hanging off the
3267c478bd9Sstevel@tonic-gate  * pseudo exportinfo, and the one we want to add.  It's possible
3277c478bd9Sstevel@tonic-gate  * that the two lists share a common path segment
3287c478bd9Sstevel@tonic-gate  * and have some common directories.  We need to combine
3297c478bd9Sstevel@tonic-gate  * the lists so there's no duplicate entries. Where a common
3307c478bd9Sstevel@tonic-gate  * path component is found, the vis_count field is bumped.
3317c478bd9Sstevel@tonic-gate  *
3323f238301SPavel Filipensky  * This example shows that the treenode chain (tree_head) and
3333f238301SPavel Filipensky  * exp_visible chain (vis_head) can differ in length. The latter
3343f238301SPavel Filipensky  * can be shorter. The outer loop must loop over the vis_head chain.
3353f238301SPavel Filipensky  *
3363f238301SPavel Filipensky  * share /x/a
3373f238301SPavel Filipensky  * mount -F ufs /dev/dsk/... /x/y
3383f238301SPavel Filipensky  * mkdir -p /x/y/a/b
3393f238301SPavel Filipensky  * share  /x/y/a/b
3403f238301SPavel Filipensky  *
3413f238301SPavel Filipensky  * When more_visible() is called during the second share,
3420616fd7fSPavel Filipensky  * the existing namespace is following:
3433f238301SPavel Filipensky  *                                   exp_visible_t
3443f238301SPavel Filipensky  *   treenode_t       exportinfo_t      v0     v1
3453f238301SPavel Filipensky  * ns_root+---+        +------------+  +---+  +---+
3463f238301SPavel Filipensky  *      t0| / |........| E0 pseudo  |->| x |->| a |
3473f238301SPavel Filipensky  *        +---+        +------------+  +---+  +---+
3483f238301SPavel Filipensky  *          |                           /    /
3493f238301SPavel Filipensky  *        +---+                        /    /
3503f238301SPavel Filipensky  *      t1| x |------------------------    /
3513f238301SPavel Filipensky  *        +---+                           /
3523f238301SPavel Filipensky  *          |                            /
3533f238301SPavel Filipensky  *        +---+                         /
3543f238301SPavel Filipensky  *      t2| a |-------------------------
3553f238301SPavel Filipensky  *        +---+........+------------+
3563f238301SPavel Filipensky  *                     | E1 real    |
3573f238301SPavel Filipensky  *                     +------------+
3583f238301SPavel Filipensky  *
3593f238301SPavel Filipensky  * This is being added:
3603f238301SPavel Filipensky  *
3613f238301SPavel Filipensky  *    tree_head  vis_head
3623f238301SPavel Filipensky  *        +---+  +---+
3633f238301SPavel Filipensky  *      t3| x |->| x |v2
3643f238301SPavel Filipensky  *        +---+  +---+
3653f238301SPavel Filipensky  *          |      |
3663f238301SPavel Filipensky  *        +---+  +---+                     v4     v5
3673f238301SPavel Filipensky  *      t4| y |->| y |v3  +------------+  +---+  +---+
3683f238301SPavel Filipensky  *        +---+\ +---+    | E2 pseudo  |->| a |->| b |
3693f238301SPavel Filipensky  *          |   \....... >+------------+  +---+  +---+
3703f238301SPavel Filipensky  *        +---+                           /      /
3713f238301SPavel Filipensky  *      t5| a |---------------------------      /
3723f238301SPavel Filipensky  *        +---+                                /
3733f238301SPavel Filipensky  *          |                                 /
3743f238301SPavel Filipensky  *        +---+-------------------------------
3753f238301SPavel Filipensky  *      t6| b |           +------------+
3763f238301SPavel Filipensky  *        +---+..........>| E3 real    |
3773f238301SPavel Filipensky  *                        +------------+
3783f238301SPavel Filipensky  *
3793f238301SPavel Filipensky  * more_visible() will:
38008214031SPavel Filipensky  * - kmem_free() t3 and v2
38108214031SPavel Filipensky  * - add t4, t5, t6 as a child of t1 (t4 will become sibling of t2)
3823f238301SPavel Filipensky  * - add v3 to the end of E0->exi_visible
3833f238301SPavel Filipensky  *
3840616fd7fSPavel Filipensky  * Note that v4 and v5 were already processed in pseudo_exportfs() and
3853f238301SPavel Filipensky  * added to E2. The outer loop of more_visible() will loop only over v2
3863f238301SPavel Filipensky  * and v3. The inner loop of more_visible() always loops over v0 and v1.
38708214031SPavel Filipensky  *
38808214031SPavel Filipensky  * Illustration for this scenario:
38908214031SPavel Filipensky  *
39008214031SPavel Filipensky  * mkdir -p /v/a/b/c
39108214031SPavel Filipensky  * share /v/a/b/c
39208214031SPavel Filipensky  * mkdir /v/a/b/c1
39308214031SPavel Filipensky  * mkdir -p /v/a1
39408214031SPavel Filipensky  * mv /v/a/b /v/a1
39508214031SPavel Filipensky  * share /v/a1/b/c1
39608214031SPavel Filipensky  *
39708214031SPavel Filipensky  *           EXISTING
39808214031SPavel Filipensky  *           treenode
39908214031SPavel Filipensky  *           namespace:    +-----------+   visibles
40008214031SPavel Filipensky  *                         |exportinfo |-->v->a->b->c
40108214031SPavel Filipensky  * connect_point->+---+--->+-----------+
40208214031SPavel Filipensky  *                | / |T0
40308214031SPavel Filipensky  *                +---+
40408214031SPavel Filipensky  *                  |                            NEW treenode chain:
40508214031SPavel Filipensky  *         child->+---+
40608214031SPavel Filipensky  *                | v |T1                          +---+<-curr
40708214031SPavel Filipensky  *                +---+                          N1| v |
40808214031SPavel Filipensky  *                  |                              +---+
40908214031SPavel Filipensky  *                +---+                              |
41008214031SPavel Filipensky  *                | a |T2                          +---+<-tree_head
41108214031SPavel Filipensky  *                +---+                          N2| a1|
41208214031SPavel Filipensky  *                  |                              +---+
41308214031SPavel Filipensky  *                +---+                              |
41408214031SPavel Filipensky  *                | b |T3                          +---+
41508214031SPavel Filipensky  *                +---+                          N3| b |
41608214031SPavel Filipensky  *                  |                              +---+
41708214031SPavel Filipensky  *                +---+                              |
41808214031SPavel Filipensky  *                | c |T4                          +---+
41908214031SPavel Filipensky  *                +---+                          N4| c1|
42008214031SPavel Filipensky  *                                                 +---+
42108214031SPavel Filipensky  *
42208214031SPavel Filipensky  * The picture above illustrates the position of following pointers after line
42308214031SPavel Filipensky  * 'child = tree_find_child_by_vis(connect_point, curr->tree_vis);'
42408214031SPavel Filipensky  * was executed for the first time in the outer 'for' loop:
42508214031SPavel Filipensky  *
42608214031SPavel Filipensky  * connect_point..parent treenode in the EXISTING namespace to which the 'curr'
42708214031SPavel Filipensky  *                should be connected. If 'connect_point' already has a child
42808214031SPavel Filipensky  *                with the same value of tree_vis as the curr->tree_vis is,
42908214031SPavel Filipensky  *                the 'curr' will not be added, but kmem_free()d.
43008214031SPavel Filipensky  * child..........the result of tree_find_child_by_vis()
43108214031SPavel Filipensky  * curr...........currently processed treenode from the NEW treenode chain
43208214031SPavel Filipensky  * tree_head......current head of the NEW treenode chain, in this case it was
43308214031SPavel Filipensky  *                already moved down to its child - preparation for another loop
43408214031SPavel Filipensky  *
43508214031SPavel Filipensky  * What will happen to NEW treenodes N1, N2, N3, N4 in more_visible() later:
43608214031SPavel Filipensky  *
43708214031SPavel Filipensky  * N1: is merged - i.e. N1 is kmem_free()d. T0 has a child T1 with the same
43808214031SPavel Filipensky  *     tree_vis as N1
43908214031SPavel Filipensky  * N2: is added as a new child of T1
44008214031SPavel Filipensky  *     Note: not just N2, but the whole chain N2->N3->N4 is added
44108214031SPavel Filipensky  * N3: not processed separately (it was added together with N2)
44208214031SPavel Filipensky  *     Even that N3 and T3 have same tree_vis, they are NOT merged, but will
44308214031SPavel Filipensky  *     become duplicates.
44408214031SPavel Filipensky  * N4: not processed separately
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate static void
more_visible(struct exportinfo * exi,treenode_t * tree_head)4473f238301SPavel Filipensky more_visible(struct exportinfo *exi, treenode_t *tree_head)
4487c478bd9Sstevel@tonic-gate {
4493f238301SPavel Filipensky 	struct exp_visible *vp1, *vp2, *vis_head, *tail, *next;
4507c478bd9Sstevel@tonic-gate 	int found;
45108214031SPavel Filipensky 	treenode_t *child, *curr, *connect_point;
452*0dfe541eSEvan Layton 	nfs_export_t *ne = nfs_get_export();
4537c478bd9Sstevel@tonic-gate 
4543f238301SPavel Filipensky 	vis_head = tree_head->tree_vis;
45508214031SPavel Filipensky 	connect_point = exi->exi_tree;
456992d2facSpf 
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * If exportinfo doesn't already have a visible
4597c478bd9Sstevel@tonic-gate 	 * list just assign the entire supplied list.
4607c478bd9Sstevel@tonic-gate 	 */
4617c478bd9Sstevel@tonic-gate 	if (exi->exi_visible == NULL) {
4624a695956SMarcel Telka 		tree_add_child(connect_point, tree_head);
4637c478bd9Sstevel@tonic-gate 		exi->exi_visible = vis_head;
4644a695956SMarcel Telka 
4654a695956SMarcel Telka 		/* Update the change timestamp */
466*0dfe541eSEvan Layton 		tree_update_change(ne, connect_point, &vis_head->vis_change);
4674a695956SMarcel Telka 
4687c478bd9Sstevel@tonic-gate 		return;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4713f238301SPavel Filipensky 	/* The outer loop traverses the supplied list. */
4723f238301SPavel Filipensky 	for (vp1 = vis_head; vp1; vp1 = next) {
4737c478bd9Sstevel@tonic-gate 		found = 0;
4743f238301SPavel Filipensky 		next = vp1->vis_next;
4757c478bd9Sstevel@tonic-gate 
4763f238301SPavel Filipensky 		/* The inner loop searches the exportinfo visible list. */
4777c478bd9Sstevel@tonic-gate 		for (vp2 = exi->exi_visible; vp2; vp2 = vp2->vis_next) {
4787c478bd9Sstevel@tonic-gate 			tail = vp2;
4797c478bd9Sstevel@tonic-gate 			if (EQFID(&vp1->vis_fid, &vp2->vis_fid)) {
4807c478bd9Sstevel@tonic-gate 				found = 1;
4817c478bd9Sstevel@tonic-gate 				vp2->vis_count++;
4827c478bd9Sstevel@tonic-gate 				VN_RELE(vp1->vis_vp);
48308214031SPavel Filipensky 				/* Transfer vis_exported from vp1 to vp2. */
4847c478bd9Sstevel@tonic-gate 				if (vp1->vis_exported && !vp2->vis_exported)
4857c478bd9Sstevel@tonic-gate 					vp2->vis_exported = 1;
4863f238301SPavel Filipensky 				kmem_free(vp1, sizeof (*vp1));
4873f238301SPavel Filipensky 				tree_head->tree_vis = vp2;
4887c478bd9Sstevel@tonic-gate 				break;
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		/* If not found - add to the end of the list */
4937c478bd9Sstevel@tonic-gate 		if (! found) {
4943f238301SPavel Filipensky 			tail->vis_next = vp1;
4953f238301SPavel Filipensky 			vp1->vis_next = NULL;
4967c478bd9Sstevel@tonic-gate 		}
49708214031SPavel Filipensky 
49808214031SPavel Filipensky 		curr = tree_head;
4993f238301SPavel Filipensky 		tree_head = tree_head->tree_child_first;
50008214031SPavel Filipensky 
50108214031SPavel Filipensky 		if (! connect_point) /* No longer merging */
50208214031SPavel Filipensky 			continue;
50308214031SPavel Filipensky 		/*
50408214031SPavel Filipensky 		 * The inner loop could set curr->tree_vis to the EXISTING
50508214031SPavel Filipensky 		 * exp_visible vp2, so we can search among the children of
50608214031SPavel Filipensky 		 * connect_point for the curr->tree_vis. No need for EQFID.
50708214031SPavel Filipensky 		 */
50808214031SPavel Filipensky 		child = tree_find_child_by_vis(connect_point, curr->tree_vis);
5094e240297SPavel Filipensky 
5104e240297SPavel Filipensky 		/*
5114e240297SPavel Filipensky 		 * Merging cannot be done if a valid child->tree_exi would
5124e240297SPavel Filipensky 		 * be overwritten by a new curr->tree_exi.
5134e240297SPavel Filipensky 		 */
5144e240297SPavel Filipensky 		if (child &&
5154e240297SPavel Filipensky 		    (child->tree_exi == NULL || curr->tree_exi == NULL)) {
51608214031SPavel Filipensky 			if (curr->tree_exi) { /* Transfer the exportinfo */
51708214031SPavel Filipensky 				child->tree_exi = curr->tree_exi;
51808214031SPavel Filipensky 				child->tree_exi->exi_tree = child;
51908214031SPavel Filipensky 			}
52008214031SPavel Filipensky 			kmem_free(curr, sizeof (treenode_t));
5214e240297SPavel Filipensky 			connect_point = child;
52208214031SPavel Filipensky 		} else { /* Branching */
52308214031SPavel Filipensky 			tree_add_child(connect_point, curr);
5244a695956SMarcel Telka 
5254a695956SMarcel Telka 			/* Update the change timestamp */
526*0dfe541eSEvan Layton 			tree_update_change(ne, connect_point,
5274a695956SMarcel Telka 			    &curr->tree_vis->vis_change);
5284a695956SMarcel Telka 
5294e240297SPavel Filipensky 			connect_point = NULL;
53008214031SPavel Filipensky 		}
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*
535992d2facSpf  * Remove one visible entry from the pseudo exportfs.
5367c478bd9Sstevel@tonic-gate  *
5377c478bd9Sstevel@tonic-gate  * When we unexport a directory, we have to remove path
5387c478bd9Sstevel@tonic-gate  * components from the visible list in the pseudo exportfs
539992d2facSpf  * entry. The supplied visible contains one fid of one path
540992d2facSpf  * component. The visible list of the export
541992d2facSpf  * is checked against provided visible, matching fid has its
5427c478bd9Sstevel@tonic-gate  * reference count decremented.  If a reference count drops to
5437c478bd9Sstevel@tonic-gate  * zero, then it means no paths now use this directory, so its
5447c478bd9Sstevel@tonic-gate  * fid can be removed from the visible list.
5457c478bd9Sstevel@tonic-gate  *
5467c478bd9Sstevel@tonic-gate  * When the last path is removed, the visible list will be null.
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate static void
less_visible(struct exportinfo * exi,struct exp_visible * vp1)549992d2facSpf less_visible(struct exportinfo *exi, struct exp_visible *vp1)
5507c478bd9Sstevel@tonic-gate {
551992d2facSpf 	struct exp_visible *vp2;
5527c478bd9Sstevel@tonic-gate 	struct exp_visible *prev, *next;
5537c478bd9Sstevel@tonic-gate 
554992d2facSpf 	for (vp2 = exi->exi_visible, prev = NULL; vp2; vp2 = next) {
5557c478bd9Sstevel@tonic-gate 
556992d2facSpf 		next = vp2->vis_next;
5577c478bd9Sstevel@tonic-gate 
55808214031SPavel Filipensky 		if (vp1 == vp2) {
559992d2facSpf 			/*
560992d2facSpf 			 * Decrement the ref count.
561992d2facSpf 			 * Remove the entry if it's zero.
562992d2facSpf 			 */
563992d2facSpf 			if (--vp2->vis_count <= 0) {
564992d2facSpf 				if (prev == NULL)
565992d2facSpf 					exi->exi_visible = next;
566992d2facSpf 				else
567992d2facSpf 					prev->vis_next = next;
568992d2facSpf 				VN_RELE(vp2->vis_vp);
569992d2facSpf 				srv_secinfo_list_free(vp2->vis_secinfo,
570992d2facSpf 				    vp2->vis_seccnt);
571992d2facSpf 				kmem_free(vp2, sizeof (*vp1));
5727c478bd9Sstevel@tonic-gate 			}
573992d2facSpf 			break;
5747c478bd9Sstevel@tonic-gate 		}
575992d2facSpf 		prev = vp2;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * This function checks the path to a new export to
5817c478bd9Sstevel@tonic-gate  * check whether all the pathname components are
5827c478bd9Sstevel@tonic-gate  * exported. It works by climbing the file tree one
5837c478bd9Sstevel@tonic-gate  * component at a time via "..", crossing mountpoints
5847c478bd9Sstevel@tonic-gate  * if necessary until an export entry is found, or the
5857c478bd9Sstevel@tonic-gate  * system root is reached.
5867c478bd9Sstevel@tonic-gate  *
5877c478bd9Sstevel@tonic-gate  * If an unexported mountpoint is found, then
5887c478bd9Sstevel@tonic-gate  * a new pseudo export is added and the pathname from
5897c478bd9Sstevel@tonic-gate  * the mountpoint down to the export is added to the
5907c478bd9Sstevel@tonic-gate  * visible list for the new pseudo export.  If an existing
5917c478bd9Sstevel@tonic-gate  * pseudo export is found, then the pathname is added
5927c478bd9Sstevel@tonic-gate  * to its visible list.
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * Note that there's some tests for exportdir.
5957c478bd9Sstevel@tonic-gate  * The exportinfo entry that's passed as a parameter
5967c478bd9Sstevel@tonic-gate  * is that of the real export and exportdir is set
5977c478bd9Sstevel@tonic-gate  * for this case.
5987c478bd9Sstevel@tonic-gate  *
5997c478bd9Sstevel@tonic-gate  * Here is an example of a possible setup:
6007c478bd9Sstevel@tonic-gate  *
6017c478bd9Sstevel@tonic-gate  * () - a new fs; fs mount point
6027c478bd9Sstevel@tonic-gate  * EXPORT - a real exported node
6037c478bd9Sstevel@tonic-gate  * PSEUDO - a pseudo node
6047c478bd9Sstevel@tonic-gate  * vis - visible list
6057c478bd9Sstevel@tonic-gate  * f# - security flavor#
606da6c28aaSamw  * (f#) - security flavor# propagated from its descendents
6077c478bd9Sstevel@tonic-gate  * "" - covered vnode
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  *
6107c478bd9Sstevel@tonic-gate  *                 /
6117c478bd9Sstevel@tonic-gate  *                 |
6127c478bd9Sstevel@tonic-gate  *                 (a) PSEUDO (f1,f2)
6137c478bd9Sstevel@tonic-gate  *                 |   vis: b,b,"c","n"
6147c478bd9Sstevel@tonic-gate  *                 |
6157c478bd9Sstevel@tonic-gate  *                 b
6167c478bd9Sstevel@tonic-gate  *        ---------|------------------
6177c478bd9Sstevel@tonic-gate  *        |                          |
6187c478bd9Sstevel@tonic-gate  *        (c) EXPORT,f1(f2)          (n) PSEUDO (f1,f2)
6197c478bd9Sstevel@tonic-gate  *        |   vis: "e","d"           |   vis: m,m,,p,q,"o"
6207c478bd9Sstevel@tonic-gate  *        |                          |
6217c478bd9Sstevel@tonic-gate  *  ------------------          -------------------
6227c478bd9Sstevel@tonic-gate  *  |        |        |         |                  |
6237c478bd9Sstevel@tonic-gate  *  (d)      (e)      f         m EXPORT,f1(f2)    p
6247c478bd9Sstevel@tonic-gate  *  EXPORT   EXPORT             |                  |
6257c478bd9Sstevel@tonic-gate  *  f1       f2                 |                  |
6267c478bd9Sstevel@tonic-gate  *           |                  |                  |
6277c478bd9Sstevel@tonic-gate  *           j                 (o) EXPORT,f2       q EXPORT f2
6287c478bd9Sstevel@tonic-gate  *
6297c478bd9Sstevel@tonic-gate  */
6307c478bd9Sstevel@tonic-gate int
treeclimb_export(struct exportinfo * exip)6317c478bd9Sstevel@tonic-gate treeclimb_export(struct exportinfo *exip)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	vnode_t *dvp, *vp;
6347c478bd9Sstevel@tonic-gate 	fid_t fid;
6357c478bd9Sstevel@tonic-gate 	int error;
6367c478bd9Sstevel@tonic-gate 	int exportdir;
637992d2facSpf 	struct exportinfo *new_exi = exip;
6387c478bd9Sstevel@tonic-gate 	struct exp_visible *visp;
6397c478bd9Sstevel@tonic-gate 	struct exp_visible *vis_head = NULL;
6407c478bd9Sstevel@tonic-gate 	struct vattr va;
641992d2facSpf 	treenode_t *tree_head = NULL;
6424a695956SMarcel Telka 	timespec_t now;
643*0dfe541eSEvan Layton 	nfs_export_t *ne;
6447c478bd9Sstevel@tonic-gate 
645*0dfe541eSEvan Layton 	ne = exip->exi_ne;
646*0dfe541eSEvan Layton 	ASSERT3P(ne, ==, nfs_get_export());	/* curzone reality check */
647*0dfe541eSEvan Layton 	ASSERT(RW_WRITE_HELD(&ne->exported_lock));
6487c478bd9Sstevel@tonic-gate 
6494a695956SMarcel Telka 	gethrestime(&now);
6504a695956SMarcel Telka 
6517c478bd9Sstevel@tonic-gate 	vp = exip->exi_vp;
6527c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
6537c478bd9Sstevel@tonic-gate 	exportdir = 1;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	for (;;) {
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		bzero(&fid, sizeof (fid));
6587c478bd9Sstevel@tonic-gate 		fid.fid_len = MAXFIDSZ;
6597c478bd9Sstevel@tonic-gate 		error = vop_fid_pseudo(vp, &fid);
6607c478bd9Sstevel@tonic-gate 		if (error)
6617c478bd9Sstevel@tonic-gate 			break;
6627c478bd9Sstevel@tonic-gate 
663*0dfe541eSEvan Layton 		ASSERT3U(exip->exi_zoneid, ==, curzone->zone_id);
6647c478bd9Sstevel@tonic-gate 		/*
665*0dfe541eSEvan Layton 		 * The root of the file system, or the zone's root for
666*0dfe541eSEvan Layton 		 * in-zone NFS service needs special handling
6677c478bd9Sstevel@tonic-gate 		 */
668*0dfe541eSEvan Layton 		if (vp->v_flag & VROOT || vp == EXI_TO_ZONEROOTVP(exip)) {
669*0dfe541eSEvan Layton 			if (!exportdir) {
6704a695956SMarcel Telka 				struct exportinfo *exi;
6714a695956SMarcel Telka 
6724a695956SMarcel Telka 				/*
6734a695956SMarcel Telka 				 * Check if this VROOT dir is already exported.
6744a695956SMarcel Telka 				 * If so, then attach the pseudonodes.  If not,
6754a695956SMarcel Telka 				 * then continue .. traversal until we hit a
6764a695956SMarcel Telka 				 * VROOT export (pseudo or real).
6774a695956SMarcel Telka 				 */
6784a695956SMarcel Telka 				exi = checkexport4(&vp->v_vfsp->vfs_fsid, &fid,
6794a695956SMarcel Telka 				    vp);
6804a695956SMarcel Telka 				if (exi != NULL) {
6814a695956SMarcel Telka 					/*
6824a695956SMarcel Telka 					 * Found an export info
6834a695956SMarcel Telka 					 *
6844a695956SMarcel Telka 					 * Extend the list of visible
6854a695956SMarcel Telka 					 * directories whether it's a pseudo
6864a695956SMarcel Telka 					 * or a real export.
6874a695956SMarcel Telka 					 */
6884a695956SMarcel Telka 					more_visible(exi, tree_head);
6894a695956SMarcel Telka 					break;	/* and climb no further */
6904a695956SMarcel Telka 				}
6914a695956SMarcel Telka 
6927c478bd9Sstevel@tonic-gate 				/*
6937c478bd9Sstevel@tonic-gate 				 * Found the root directory of a filesystem
6947c478bd9Sstevel@tonic-gate 				 * that isn't exported.  Need to export
6957c478bd9Sstevel@tonic-gate 				 * this as a pseudo export so that an NFS v4
6967c478bd9Sstevel@tonic-gate 				 * client can do lookups in it.
6977c478bd9Sstevel@tonic-gate 				 */
698*0dfe541eSEvan Layton 				new_exi = pseudo_exportfs(ne, vp, &fid,
699*0dfe541eSEvan Layton 				    vis_head, NULL);
7007c478bd9Sstevel@tonic-gate 				vis_head = NULL;
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 
703*0dfe541eSEvan Layton 			if (VN_IS_CURZONEROOT(vp)) {
7047c478bd9Sstevel@tonic-gate 				/* at system root */
705992d2facSpf 				/*
706992d2facSpf 				 * If sharing "/", new_exi is shared exportinfo
707992d2facSpf 				 * (exip). Otherwise, new_exi is exportinfo
7084a695956SMarcel Telka 				 * created by pseudo_exportfs() above.
709992d2facSpf 				 */
710*0dfe541eSEvan Layton 				ne->ns_root = tree_prepend_node(tree_head, NULL,
711992d2facSpf 				    new_exi);
7124a695956SMarcel Telka 
7134a695956SMarcel Telka 				/* Update the change timestamp */
714*0dfe541eSEvan Layton 				tree_update_change(ne, ne->ns_root, &now);
7154a695956SMarcel Telka 
7167c478bd9Sstevel@tonic-gate 				break;
7177c478bd9Sstevel@tonic-gate 			}
7187c478bd9Sstevel@tonic-gate 
7194a695956SMarcel Telka 			/*
7204a695956SMarcel Telka 			 * Traverse across the mountpoint and continue the
7214a695956SMarcel Telka 			 * climb on the mounted-on filesystem.
7224a695956SMarcel Telka 			 */
723*0dfe541eSEvan Layton 			vp = untraverse(vp, ne->exi_root->exi_vp);
7247c478bd9Sstevel@tonic-gate 			exportdir = 0;
7257c478bd9Sstevel@tonic-gate 			continue;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * Do a getattr to obtain the nodeid (inode num)
7307c478bd9Sstevel@tonic-gate 		 * for this vnode.
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 		va.va_mask = AT_NODEID;
733da6c28aaSamw 		error = VOP_GETATTR(vp, &va, 0, CRED(), NULL);
734c076b414SPavel Filipensky 		if (error)
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		/*
7387c478bd9Sstevel@tonic-gate 		 *  Add this directory fid to visible list
7397c478bd9Sstevel@tonic-gate 		 */
7407c478bd9Sstevel@tonic-gate 		visp = kmem_alloc(sizeof (*visp), KM_SLEEP);
7417c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
7427c478bd9Sstevel@tonic-gate 		visp->vis_vp = vp;
7437c478bd9Sstevel@tonic-gate 		visp->vis_fid = fid;		/* structure copy */
7447c478bd9Sstevel@tonic-gate 		visp->vis_ino = va.va_nodeid;
7457c478bd9Sstevel@tonic-gate 		visp->vis_count = 1;
7467c478bd9Sstevel@tonic-gate 		visp->vis_exported = exportdir;
747af9f9769Sjasmith 		visp->vis_secinfo = NULL;
748af9f9769Sjasmith 		visp->vis_seccnt = 0;
7494a695956SMarcel Telka 		visp->vis_change = now;		/* structure copy */
7507c478bd9Sstevel@tonic-gate 		visp->vis_next = vis_head;
7517c478bd9Sstevel@tonic-gate 		vis_head = visp;
7527c478bd9Sstevel@tonic-gate 
753992d2facSpf 		/*
754992d2facSpf 		 * Will set treenode's pointer to exportinfo to
755992d2facSpf 		 * 1. shared exportinfo (exip) - if first visit here
756992d2facSpf 		 * 2. freshly allocated pseudo export (if any)
757992d2facSpf 		 * 3. null otherwise
758992d2facSpf 		 */
759992d2facSpf 		tree_head = tree_prepend_node(tree_head, visp, new_exi);
760992d2facSpf 		new_exi = NULL;
761992d2facSpf 
7627c478bd9Sstevel@tonic-gate 		/*
7637c478bd9Sstevel@tonic-gate 		 * Now, do a ".." to find parent dir of vp.
7647c478bd9Sstevel@tonic-gate 		 */
765da6c28aaSamw 		error = VOP_LOOKUP(vp, "..", &dvp, NULL, 0, NULL, CRED(),
766da6c28aaSamw 		    NULL, NULL, NULL);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		if (error == ENOTDIR && exportdir) {
7697c478bd9Sstevel@tonic-gate 			dvp = exip->exi_dvp;
7707c478bd9Sstevel@tonic-gate 			ASSERT(dvp != NULL);
7717c478bd9Sstevel@tonic-gate 			VN_HOLD(dvp);
7727c478bd9Sstevel@tonic-gate 			error = 0;
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		if (error)
7767c478bd9Sstevel@tonic-gate 			break;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		exportdir = 0;
7797c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
7807c478bd9Sstevel@tonic-gate 		vp = dvp;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
784992d2facSpf 
785992d2facSpf 	/*
786992d2facSpf 	 * We can have set error due to error in:
787992d2facSpf 	 * 1. vop_fid_pseudo()
7880616fd7fSPavel Filipensky 	 * 2. VOP_GETATTR()
7890616fd7fSPavel Filipensky 	 * 3. VOP_LOOKUP()
790c076b414SPavel Filipensky 	 * We must free pseudo exportinfos, visibles and treenodes.
791c076b414SPavel Filipensky 	 * Visibles are referenced from treenode_t::tree_vis and
792c076b414SPavel Filipensky 	 * exportinfo_t::exi_visible. To avoid double freeing, only
793c076b414SPavel Filipensky 	 * exi_visible pointer is used, via exi_rele(), for the clean-up.
794992d2facSpf 	 */
795992d2facSpf 	if (error) {
796c076b414SPavel Filipensky 		/* Free unconnected visibles, if there are any. */
797c076b414SPavel Filipensky 		if (vis_head)
798c076b414SPavel Filipensky 			free_visible(vis_head);
799c076b414SPavel Filipensky 
800c076b414SPavel Filipensky 		/* Connect unconnected exportinfo, if there is any. */
801c076b414SPavel Filipensky 		if (new_exi && new_exi != exip)
8024a695956SMarcel Telka 			tree_head = tree_prepend_node(tree_head, NULL, new_exi);
803c076b414SPavel Filipensky 
804992d2facSpf 		while (tree_head) {
805992d2facSpf 			treenode_t *t2 = tree_head;
806992d2facSpf 			exportinfo_t *e  = tree_head->tree_exi;
807992d2facSpf 			/* exip will be freed in exportfs() */
808992d2facSpf 			if (e && e != exip) {
809*0dfe541eSEvan Layton 				mutex_enter(&nfs_exi_id_lock);
810*0dfe541eSEvan Layton 				avl_remove(&exi_id_tree, e);
811*0dfe541eSEvan Layton 				mutex_exit(&nfs_exi_id_lock);
812*0dfe541eSEvan Layton 				export_unlink(ne, e);
813992d2facSpf 				exi_rele(e);
814992d2facSpf 			}
815992d2facSpf 			tree_head = tree_head->tree_child_first;
816992d2facSpf 			kmem_free(t2, sizeof (*t2));
817992d2facSpf 		}
818992d2facSpf 	}
819992d2facSpf 
8207c478bd9Sstevel@tonic-gate 	return (error);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate /*
824992d2facSpf  * Walk up the tree and:
825992d2facSpf  * 1. release pseudo exportinfo if it has no child
826992d2facSpf  * 2. release visible in parent's exportinfo
827992d2facSpf  * 3. delete non-exported leaf nodes from tree
8287c478bd9Sstevel@tonic-gate  *
829992d2facSpf  * Deleting of nodes will start only if the unshared
830992d2facSpf  * node was a leaf node.
831992d2facSpf  * Deleting of nodes will finish when we reach a node which
832992d2facSpf  * has children or is a real export, then we might still need
833*0dfe541eSEvan Layton  * to continue releasing visibles, until we reach VROOT or zone's root node.
8347c478bd9Sstevel@tonic-gate  */
835992d2facSpf void
treeclimb_unexport(nfs_export_t * ne,struct exportinfo * exip)836*0dfe541eSEvan Layton treeclimb_unexport(nfs_export_t *ne, struct exportinfo *exip)
8377c478bd9Sstevel@tonic-gate {
838992d2facSpf 	treenode_t *tnode, *old_nd;
8394a695956SMarcel Telka 	treenode_t *connect_point = NULL;
8407c478bd9Sstevel@tonic-gate 
841*0dfe541eSEvan Layton 	ASSERT(RW_WRITE_HELD(&ne->exported_lock));
842*0dfe541eSEvan Layton 	ASSERT(curzone->zone_id == exip->exi_zoneid ||
843*0dfe541eSEvan Layton 	    curzone->zone_id == global_zone->zone_id);
8447c478bd9Sstevel@tonic-gate 
845*0dfe541eSEvan Layton 	/*
846*0dfe541eSEvan Layton 	 * exi_tree can be null for the zone root
847*0dfe541eSEvan Layton 	 * which means we're already at the "top"
848*0dfe541eSEvan Layton 	 * and there's nothing more to "climb".
849*0dfe541eSEvan Layton 	 */
850992d2facSpf 	tnode = exip->exi_tree;
851*0dfe541eSEvan Layton 	if (tnode == NULL) {
852*0dfe541eSEvan Layton 		/* Should only happen for... */
853*0dfe541eSEvan Layton 		ASSERT(exip == ne->exi_root);
854*0dfe541eSEvan Layton 		return;
855*0dfe541eSEvan Layton 	}
856*0dfe541eSEvan Layton 
857992d2facSpf 	/*
858992d2facSpf 	 * The unshared exportinfo was unlinked in unexport().
859992d2facSpf 	 * Zeroing tree_exi ensures that we will skip it.
860992d2facSpf 	 */
861992d2facSpf 	tnode->tree_exi = NULL;
8627c478bd9Sstevel@tonic-gate 
8634a695956SMarcel Telka 	if (tnode->tree_vis != NULL) /* system root has tree_vis == NULL */
86408214031SPavel Filipensky 		tnode->tree_vis->vis_exported = 0;
86508214031SPavel Filipensky 
8664a695956SMarcel Telka 	while (tnode != NULL) {
8677c478bd9Sstevel@tonic-gate 
868*0dfe541eSEvan Layton 		/*
869*0dfe541eSEvan Layton 		 * Stop at VROOT (or zone root) node which is exported or has
870*0dfe541eSEvan Layton 		 * child.
871*0dfe541eSEvan Layton 		 */
872992d2facSpf 		if (TREE_ROOT(tnode) &&
8734a695956SMarcel Telka 		    (TREE_EXPORTED(tnode) || tnode->tree_child_first != NULL))
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 
876992d2facSpf 		/* Release pseudo export if it has no child */
877992d2facSpf 		if (TREE_ROOT(tnode) && !TREE_EXPORTED(tnode) &&
8784a695956SMarcel Telka 		    tnode->tree_child_first == NULL) {
879*0dfe541eSEvan Layton 			mutex_enter(&nfs_exi_id_lock);
880*0dfe541eSEvan Layton 			avl_remove(&exi_id_tree, tnode->tree_exi);
881*0dfe541eSEvan Layton 			mutex_exit(&nfs_exi_id_lock);
882*0dfe541eSEvan Layton 			export_unlink(ne, tnode->tree_exi);
883992d2facSpf 			exi_rele(tnode->tree_exi);
884*0dfe541eSEvan Layton 			tnode->tree_exi = NULL;
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 
887992d2facSpf 		/* Release visible in parent's exportinfo */
8884a695956SMarcel Telka 		if (tnode->tree_vis != NULL)
8890616fd7fSPavel Filipensky 			less_visible(vis2exi(tnode), tnode->tree_vis);
8907c478bd9Sstevel@tonic-gate 
891992d2facSpf 		/* Continue with parent */
892992d2facSpf 		old_nd = tnode;
893992d2facSpf 		tnode = tnode->tree_parent;
8947c478bd9Sstevel@tonic-gate 
895992d2facSpf 		/* Remove itself, if this is a leaf and non-exported node */
8964a695956SMarcel Telka 		if (old_nd->tree_child_first == NULL &&
8974a695956SMarcel Telka 		    !TREE_EXPORTED(old_nd)) {
898*0dfe541eSEvan Layton 			tree_remove_node(ne, old_nd);
8994a695956SMarcel Telka 			connect_point = tnode;
9004a695956SMarcel Telka 		}
9017c478bd9Sstevel@tonic-gate 	}
9024a695956SMarcel Telka 
9034a695956SMarcel Telka 	/* Update the change timestamp */
9044a695956SMarcel Telka 	if (connect_point != NULL)
905*0dfe541eSEvan Layton 		tree_update_change(ne, connect_point, NULL);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * Traverse backward across mountpoint from the
9107c478bd9Sstevel@tonic-gate  * root vnode of a filesystem to its mounted-on
9117c478bd9Sstevel@tonic-gate  * vnode.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate vnode_t *
untraverse(vnode_t * vp,vnode_t * zone_rootvp)914*0dfe541eSEvan Layton untraverse(vnode_t *vp, vnode_t *zone_rootvp)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	vnode_t *tvp, *nextvp;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	tvp = vp;
9197c478bd9Sstevel@tonic-gate 	for (;;) {
920*0dfe541eSEvan Layton 		if (!(tvp->v_flag & VROOT) && !VN_CMP(tvp, zone_rootvp))
9217c478bd9Sstevel@tonic-gate 			break;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 		/* lock vfs to prevent unmount of this vfs */
9247c478bd9Sstevel@tonic-gate 		vfs_lock_wait(tvp->v_vfsp);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		if ((nextvp = tvp->v_vfsp->vfs_vnodecovered) == NULL) {
9277c478bd9Sstevel@tonic-gate 			vfs_unlock(tvp->v_vfsp);
9287c478bd9Sstevel@tonic-gate 			break;
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		/*
9327c478bd9Sstevel@tonic-gate 		 * Hold nextvp to prevent unmount.  After unlock vfs and
9337c478bd9Sstevel@tonic-gate 		 * rele tvp, any number of overlays could be unmounted.
9347c478bd9Sstevel@tonic-gate 		 * Putting a hold on vfs_vnodecovered will only allow
9357c478bd9Sstevel@tonic-gate 		 * tvp's vfs to be unmounted. Of course if caller placed
9367c478bd9Sstevel@tonic-gate 		 * extra hold on vp before calling untraverse, the following
9377c478bd9Sstevel@tonic-gate 		 * hold would not be needed.  Since prev actions of caller
9387c478bd9Sstevel@tonic-gate 		 * are unknown, we need to hold here just to be safe.
9397c478bd9Sstevel@tonic-gate 		 */
9407c478bd9Sstevel@tonic-gate 		VN_HOLD(nextvp);
9417c478bd9Sstevel@tonic-gate 		vfs_unlock(tvp->v_vfsp);
9427c478bd9Sstevel@tonic-gate 		VN_RELE(tvp);
9437c478bd9Sstevel@tonic-gate 		tvp = nextvp;
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	return (tvp);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * Given an exportinfo, climb up to find the exportinfo for the VROOT
951*0dfe541eSEvan Layton  * (or zone root) of the filesystem.
9527c478bd9Sstevel@tonic-gate  *
9537c478bd9Sstevel@tonic-gate  * e.g.         /
9547c478bd9Sstevel@tonic-gate  *              |
9557c478bd9Sstevel@tonic-gate  *              a (VROOT) pseudo-exportinfo
9567c478bd9Sstevel@tonic-gate  *		|
9577c478bd9Sstevel@tonic-gate  *		b
9587c478bd9Sstevel@tonic-gate  *		|
9597c478bd9Sstevel@tonic-gate  *		c  #share /a/b/c
9607c478bd9Sstevel@tonic-gate  *		|
9617c478bd9Sstevel@tonic-gate  *		d
9627c478bd9Sstevel@tonic-gate  *
9637c478bd9Sstevel@tonic-gate  * where c is in the same filesystem as a.
9647c478bd9Sstevel@tonic-gate  * So, get_root_export(*exportinfo_for_c) returns exportinfo_for_a
9657c478bd9Sstevel@tonic-gate  *
9667c478bd9Sstevel@tonic-gate  * If d is shared, then c will be put into a's visible list.
9677c478bd9Sstevel@tonic-gate  * Note: visible list is per filesystem and is attached to the
968*0dfe541eSEvan Layton  * VROOT exportinfo.  Returned exi does NOT have a new hold.
9697c478bd9Sstevel@tonic-gate  */
9707c478bd9Sstevel@tonic-gate struct exportinfo *
get_root_export(struct exportinfo * exip)9717c478bd9Sstevel@tonic-gate get_root_export(struct exportinfo *exip)
9727c478bd9Sstevel@tonic-gate {
97308214031SPavel Filipensky 	treenode_t *tnode = exip->exi_tree;
97408214031SPavel Filipensky 	exportinfo_t *exi = NULL;
9757c478bd9Sstevel@tonic-gate 
97608214031SPavel Filipensky 	while (tnode) {
97708214031SPavel Filipensky 		if (TREE_ROOT(tnode)) {
97808214031SPavel Filipensky 			exi = tnode->tree_exi;
9797c478bd9Sstevel@tonic-gate 			break;
9807c478bd9Sstevel@tonic-gate 		}
98108214031SPavel Filipensky 		tnode = tnode->tree_parent;
9827c478bd9Sstevel@tonic-gate 	}
98308214031SPavel Filipensky 	ASSERT(exi);
9847c478bd9Sstevel@tonic-gate 	return (exi);
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate /*
9887c478bd9Sstevel@tonic-gate  * Return true if the supplied vnode has a sub-directory exported.
9897c478bd9Sstevel@tonic-gate  */
9907c478bd9Sstevel@tonic-gate int
has_visible(struct exportinfo * exi,vnode_t * vp)9917c478bd9Sstevel@tonic-gate has_visible(struct exportinfo *exi, vnode_t *vp)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	struct exp_visible *visp;
9947c478bd9Sstevel@tonic-gate 	fid_t fid;
9957c478bd9Sstevel@tonic-gate 	bool_t vp_is_exported;
9967c478bd9Sstevel@tonic-gate 
9974a695956SMarcel Telka 	vp_is_exported = VN_CMP(vp, exi->exi_vp);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/*
1000*0dfe541eSEvan Layton 	 * An exported root vnode has a sub-dir shared if it has a visible
1001*0dfe541eSEvan Layton 	 * list.  i.e. if it does not have a visible list, then there is no
1002*0dfe541eSEvan Layton 	 * node in this filesystem leads to any other shared node.
10037c478bd9Sstevel@tonic-gate 	 */
1004*0dfe541eSEvan Layton 	ASSERT3P(curzone->zone_id, ==, exi->exi_zoneid);
1005*0dfe541eSEvan Layton 	if (vp_is_exported &&
1006*0dfe541eSEvan Layton 	    ((vp->v_flag & VROOT) || VN_IS_CURZONEROOT(vp))) {
10077c478bd9Sstevel@tonic-gate 		return (exi->exi_visible ? 1 : 0);
1008*0dfe541eSEvan Layton 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Only the exportinfo of a fs root node may have a visible list.
10127c478bd9Sstevel@tonic-gate 	 * Either it is a pseudo root node, or a real exported root node.
10137c478bd9Sstevel@tonic-gate 	 */
101408214031SPavel Filipensky 	exi = get_root_export(exi);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	if (!exi->exi_visible)
10177c478bd9Sstevel@tonic-gate 		return (0);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/* Get the fid of the vnode */
10207c478bd9Sstevel@tonic-gate 	bzero(&fid, sizeof (fid));
10217c478bd9Sstevel@tonic-gate 	fid.fid_len = MAXFIDSZ;
10227c478bd9Sstevel@tonic-gate 	if (vop_fid_pseudo(vp, &fid) != 0) {
10237c478bd9Sstevel@tonic-gate 		return (0);
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/*
10277c478bd9Sstevel@tonic-gate 	 * See if vp is in the visible list of the root node exportinfo.
10287c478bd9Sstevel@tonic-gate 	 */
10297c478bd9Sstevel@tonic-gate 	for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
10307c478bd9Sstevel@tonic-gate 		if (EQFID(&fid, &visp->vis_fid)) {
10317c478bd9Sstevel@tonic-gate 			/*
10327c478bd9Sstevel@tonic-gate 			 * If vp is an exported non-root node with only 1 path
10337c478bd9Sstevel@tonic-gate 			 * count (for itself), it indicates no sub-dir shared
10347c478bd9Sstevel@tonic-gate 			 * using this vp as a path.
10357c478bd9Sstevel@tonic-gate 			 */
10367c478bd9Sstevel@tonic-gate 			if (vp_is_exported && visp->vis_count < 2)
10377c478bd9Sstevel@tonic-gate 				break;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 			return (1);
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	return (0);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate  * Returns true if the supplied vnode is visible
10487c478bd9Sstevel@tonic-gate  * in this export.  If vnode is visible, return
10497c478bd9Sstevel@tonic-gate  * vis_exported in expseudo.
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate int
nfs_visible(struct exportinfo * exi,vnode_t * vp,int * expseudo)10527c478bd9Sstevel@tonic-gate nfs_visible(struct exportinfo *exi, vnode_t *vp, int *expseudo)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	struct exp_visible *visp;
10557c478bd9Sstevel@tonic-gate 	fid_t fid;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	/*
10587c478bd9Sstevel@tonic-gate 	 * First check to see if vp is export root.
10597c478bd9Sstevel@tonic-gate 	 *
10607c478bd9Sstevel@tonic-gate 	 * A pseudo export root can never be exported
10617c478bd9Sstevel@tonic-gate 	 * (it would be a real export then); however,
10627c478bd9Sstevel@tonic-gate 	 * it is always visible.  If a pseudo root object
10637c478bd9Sstevel@tonic-gate 	 * was exported by server admin, then the entire
10647c478bd9Sstevel@tonic-gate 	 * pseudo exportinfo (and all visible entries) would
10657c478bd9Sstevel@tonic-gate 	 * be destroyed.  A pseudo exportinfo only exists
10667c478bd9Sstevel@tonic-gate 	 * to provide access to real (descendant) export(s).
10677c478bd9Sstevel@tonic-gate 	 *
10687c478bd9Sstevel@tonic-gate 	 * Previously, rootdir was special cased here; however,
10697c478bd9Sstevel@tonic-gate 	 * the export root special case handles the rootdir
10707c478bd9Sstevel@tonic-gate 	 * case also.
10717c478bd9Sstevel@tonic-gate 	 */
10727c478bd9Sstevel@tonic-gate 	if (VN_CMP(vp, exi->exi_vp)) {
10737c478bd9Sstevel@tonic-gate 		*expseudo = 0;
10747c478bd9Sstevel@tonic-gate 		return (1);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	/*
10787c478bd9Sstevel@tonic-gate 	 * Only a PSEUDO node has a visible list or an exported VROOT
10797c478bd9Sstevel@tonic-gate 	 * node may have a visible list.
10807c478bd9Sstevel@tonic-gate 	 */
1081*0dfe541eSEvan Layton 	if (!PSEUDO(exi))
108208214031SPavel Filipensky 		exi = get_root_export(exi);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/* Get the fid of the vnode */
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	bzero(&fid, sizeof (fid));
10877c478bd9Sstevel@tonic-gate 	fid.fid_len = MAXFIDSZ;
10887c478bd9Sstevel@tonic-gate 	if (vop_fid_pseudo(vp, &fid) != 0) {
10897c478bd9Sstevel@tonic-gate 		*expseudo = 0;
10907c478bd9Sstevel@tonic-gate 		return (0);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/*
10947c478bd9Sstevel@tonic-gate 	 * We can't trust VN_CMP() above because of LOFS.
10957c478bd9Sstevel@tonic-gate 	 * Even though VOP_CMP will do the right thing for LOFS
10967c478bd9Sstevel@tonic-gate 	 * objects, VN_CMP will short circuit out early when the
10977c478bd9Sstevel@tonic-gate 	 * vnode ops ptrs are different.  Just in case we're dealing
10987c478bd9Sstevel@tonic-gate 	 * with LOFS, compare exi_fid/fsid here.
10997c478bd9Sstevel@tonic-gate 	 *
11007c478bd9Sstevel@tonic-gate 	 * expseudo is not set because this is not an export
11017c478bd9Sstevel@tonic-gate 	 */
11027c478bd9Sstevel@tonic-gate 	if (EQFID(&exi->exi_fid, &fid) &&
11037c478bd9Sstevel@tonic-gate 	    EQFSID(&exi->exi_fsid, &vp->v_vfsp->vfs_fsid)) {
11047c478bd9Sstevel@tonic-gate 		*expseudo = 0;
11057c478bd9Sstevel@tonic-gate 		return (1);
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	/* See if it matches any fid in the visible list */
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
11127c478bd9Sstevel@tonic-gate 		if (EQFID(&fid, &visp->vis_fid)) {
11137c478bd9Sstevel@tonic-gate 			*expseudo = visp->vis_exported;
11147c478bd9Sstevel@tonic-gate 			return (1);
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	*expseudo = 0;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	return (0);
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate /*
11247c478bd9Sstevel@tonic-gate  * Returns true if the supplied vnode is the
11257c478bd9Sstevel@tonic-gate  * directory of an export point.
11267c478bd9Sstevel@tonic-gate  */
11277c478bd9Sstevel@tonic-gate int
nfs_exported(struct exportinfo * exi,vnode_t * vp)11287c478bd9Sstevel@tonic-gate nfs_exported(struct exportinfo *exi, vnode_t *vp)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	struct exp_visible *visp;
11317c478bd9Sstevel@tonic-gate 	fid_t fid;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	/*
11347c478bd9Sstevel@tonic-gate 	 * First check to see if vp is the export root
11357c478bd9Sstevel@tonic-gate 	 * This check required for the case of lookup ..
11367c478bd9Sstevel@tonic-gate 	 * where .. is a V_ROOT vnode and a pseudo exportroot.
11377c478bd9Sstevel@tonic-gate 	 * Pseudo export root objects do not have an entry
11387c478bd9Sstevel@tonic-gate 	 * in the visible list even though every V_ROOT
11397c478bd9Sstevel@tonic-gate 	 * pseudonode is visible.  It is safe to compare
11407c478bd9Sstevel@tonic-gate 	 * vp here because pseudo_exportfs put a hold on
11417c478bd9Sstevel@tonic-gate 	 * it when exi_vp was initialized.
11427c478bd9Sstevel@tonic-gate 	 *
11437c478bd9Sstevel@tonic-gate 	 * Note: VN_CMP() won't match for LOFS shares, but they're
11447c478bd9Sstevel@tonic-gate 	 * handled below w/EQFID/EQFSID.
11457c478bd9Sstevel@tonic-gate 	 */
11467c478bd9Sstevel@tonic-gate 	if (VN_CMP(vp, exi->exi_vp))
11477c478bd9Sstevel@tonic-gate 		return (1);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/* Get the fid of the vnode */
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	bzero(&fid, sizeof (fid));
11527c478bd9Sstevel@tonic-gate 	fid.fid_len = MAXFIDSZ;
11537c478bd9Sstevel@tonic-gate 	if (vop_fid_pseudo(vp, &fid) != 0)
11547c478bd9Sstevel@tonic-gate 		return (0);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	if (EQFID(&fid, &exi->exi_fid) &&
11577c478bd9Sstevel@tonic-gate 	    EQFSID(&vp->v_vfsp->vfs_fsid, &exi->exi_fsid)) {
11587c478bd9Sstevel@tonic-gate 		return (1);
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	/* See if it matches any fid in the visible list */
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
11647c478bd9Sstevel@tonic-gate 		if (EQFID(&fid, &visp->vis_fid))
11657c478bd9Sstevel@tonic-gate 			return (visp->vis_exported);
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	return (0);
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate /*
11727c478bd9Sstevel@tonic-gate  * Returns true if the supplied inode is visible
11737c478bd9Sstevel@tonic-gate  * in this export.  This function is used by
11747c478bd9Sstevel@tonic-gate  * readdir which uses inode numbers from the
11757c478bd9Sstevel@tonic-gate  * directory.
11767c478bd9Sstevel@tonic-gate  *
11777c478bd9Sstevel@tonic-gate  * NOTE: this code does not match inode number for ".",
11787c478bd9Sstevel@tonic-gate  * but it isn't required because NFS4 server rddir
11797c478bd9Sstevel@tonic-gate  * skips . and .. entries.
11807c478bd9Sstevel@tonic-gate  */
11817c478bd9Sstevel@tonic-gate int
nfs_visible_inode(struct exportinfo * exi,ino64_t ino,struct exp_visible ** visp)11824a695956SMarcel Telka nfs_visible_inode(struct exportinfo *exi, ino64_t ino,
11834a695956SMarcel Telka     struct exp_visible **visp)
11847c478bd9Sstevel@tonic-gate {
11857c478bd9Sstevel@tonic-gate 	/*
11867c478bd9Sstevel@tonic-gate 	 * Only a PSEUDO node has a visible list or an exported VROOT
11877c478bd9Sstevel@tonic-gate 	 * node may have a visible list.
11887c478bd9Sstevel@tonic-gate 	 */
1189*0dfe541eSEvan Layton 	if (!PSEUDO(exi))
119008214031SPavel Filipensky 		exi = get_root_export(exi);
11917c478bd9Sstevel@tonic-gate 
11924a695956SMarcel Telka 	for (*visp = exi->exi_visible; *visp != NULL; *visp = (*visp)->vis_next)
11934a695956SMarcel Telka 		if ((u_longlong_t)ino == (*visp)->vis_ino) {
11947c478bd9Sstevel@tonic-gate 			return (1);
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	return (0);
11987c478bd9Sstevel@tonic-gate }
11994a695956SMarcel Telka 
12004a695956SMarcel Telka /*
12014a695956SMarcel Telka  * Get the change attribute from visible and returns TRUE.
12024a695956SMarcel Telka  * If the change value is not available returns FALSE.
12034a695956SMarcel Telka  */
12044a695956SMarcel Telka bool_t
nfs_visible_change(struct exportinfo * exi,vnode_t * vp,timespec_t * change)12054a695956SMarcel Telka nfs_visible_change(struct exportinfo *exi, vnode_t *vp, timespec_t *change)
12064a695956SMarcel Telka {
12074a695956SMarcel Telka 	struct exp_visible *visp;
12084a695956SMarcel Telka 	fid_t fid;
12094a695956SMarcel Telka 	treenode_t *node;
1210*0dfe541eSEvan Layton 	nfs_export_t *ne = nfs_get_export();
12114a695956SMarcel Telka 
12124a695956SMarcel Telka 	/*
12134a695956SMarcel Telka 	 * First check to see if vp is export root.
12144a695956SMarcel Telka 	 */
12154a695956SMarcel Telka 	if (VN_CMP(vp, exi->exi_vp))
12164a695956SMarcel Telka 		goto exproot;
12174a695956SMarcel Telka 
12184a695956SMarcel Telka 	/*
12194a695956SMarcel Telka 	 * Only a PSEUDO node has a visible list or an exported VROOT
12204a695956SMarcel Telka 	 * node may have a visible list.
12214a695956SMarcel Telka 	 */
12224a695956SMarcel Telka 	if (!PSEUDO(exi))
12234a695956SMarcel Telka 		exi = get_root_export(exi);
12244a695956SMarcel Telka 
12254a695956SMarcel Telka 	/* Get the fid of the vnode */
12264a695956SMarcel Telka 	bzero(&fid, sizeof (fid));
12274a695956SMarcel Telka 	fid.fid_len = MAXFIDSZ;
12284a695956SMarcel Telka 	if (vop_fid_pseudo(vp, &fid) != 0)
12294a695956SMarcel Telka 		return (FALSE);
12304a695956SMarcel Telka 
12314a695956SMarcel Telka 	/*
12324a695956SMarcel Telka 	 * We can't trust VN_CMP() above because of LOFS.
12334a695956SMarcel Telka 	 * Even though VOP_CMP will do the right thing for LOFS
12344a695956SMarcel Telka 	 * objects, VN_CMP will short circuit out early when the
12354a695956SMarcel Telka 	 * vnode ops ptrs are different.  Just in case we're dealing
12364a695956SMarcel Telka 	 * with LOFS, compare exi_fid/fsid here.
12374a695956SMarcel Telka 	 */
12384a695956SMarcel Telka 	if (EQFID(&exi->exi_fid, &fid) &&
12394a695956SMarcel Telka 	    EQFSID(&exi->exi_fsid, &vp->v_vfsp->vfs_fsid))
12404a695956SMarcel Telka 		goto exproot;
12414a695956SMarcel Telka 
12424a695956SMarcel Telka 	/* See if it matches any fid in the visible list */
12434a695956SMarcel Telka 	for (visp = exi->exi_visible; visp; visp = visp->vis_next) {
12444a695956SMarcel Telka 		if (EQFID(&fid, &visp->vis_fid)) {
12454a695956SMarcel Telka 			*change = visp->vis_change;
12464a695956SMarcel Telka 			return (TRUE);
12474a695956SMarcel Telka 		}
12484a695956SMarcel Telka 	}
12494a695956SMarcel Telka 
12504a695956SMarcel Telka 	return (FALSE);
12514a695956SMarcel Telka 
12524a695956SMarcel Telka exproot:
12534a695956SMarcel Telka 	/* The VROOT export have its visible available through treenode */
12544a695956SMarcel Telka 	node = exi->exi_tree;
1255*0dfe541eSEvan Layton 	if (node != ne->ns_root) {
12564a695956SMarcel Telka 		ASSERT(node->tree_vis != NULL);
12574a695956SMarcel Telka 		*change = node->tree_vis->vis_change;
12584a695956SMarcel Telka 	} else {
12594a695956SMarcel Telka 		ASSERT(node->tree_vis == NULL);
1260*0dfe541eSEvan Layton 		*change = ne->ns_root_change;
12614a695956SMarcel Telka 	}
12624a695956SMarcel Telka 	return (TRUE);
12634a695956SMarcel Telka }
12644a695956SMarcel Telka 
12654a695956SMarcel Telka /*
12664a695956SMarcel Telka  * Update the change attribute value for a particular treenode.  The change
12674a695956SMarcel Telka  * attribute value is stored in the visible attached to the treenode, or in the
12684a695956SMarcel Telka  * ns_root_change.
12694a695956SMarcel Telka  *
12704a695956SMarcel Telka  * If the change value is not supplied, the current time is used.
12714a695956SMarcel Telka  */
12724a695956SMarcel Telka void
tree_update_change(nfs_export_t * ne,treenode_t * tnode,timespec_t * change)1273*0dfe541eSEvan Layton tree_update_change(nfs_export_t *ne, treenode_t *tnode, timespec_t *change)
12744a695956SMarcel Telka {
12754a695956SMarcel Telka 	timespec_t *vis_change;
12764a695956SMarcel Telka 
12774a695956SMarcel Telka 	ASSERT(tnode != NULL);
1278*0dfe541eSEvan Layton 	ASSERT((tnode != ne->ns_root && tnode->tree_vis != NULL) ||
1279*0dfe541eSEvan Layton 	    (tnode == ne->ns_root && tnode->tree_vis == NULL));
12804a695956SMarcel Telka 
1281*0dfe541eSEvan Layton 	vis_change = tnode == ne->ns_root ? &ne->ns_root_change
12824a695956SMarcel Telka 	    : &tnode->tree_vis->vis_change;
12834a695956SMarcel Telka 
12844a695956SMarcel Telka 	if (change != NULL)
12854a695956SMarcel Telka 		*vis_change = *change;
12864a695956SMarcel Telka 	else
12874a695956SMarcel Telka 		gethrestime(vis_change);
12884a695956SMarcel Telka }
1289