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