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