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