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