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