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