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