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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <libintl.h>
30 #include <libuutil.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <zone.h>
36 
37 #include <libzfs.h>
38 
39 #include "libzfs_impl.h"
40 
41 /*
42  * Structure to keep track of dataset state.  Before changing the 'sharenfs' or
43  * 'mountpoint' property, we record whether the filesystem was previously
44  * mounted/shared.  This prior state dictates whether we remount/reshare the
45  * dataset after the property has been changed.
46  *
47  * The interface consists of the following sequence of functions:
48  *
49  * 	changelist_gather()
50  * 	changelist_prefix()
51  * 	< change property >
52  * 	changelist_postfix()
53  * 	changelist_free()
54  *
55  * Other interfaces:
56  *
57  * changelist_remove() - remove a node from a gathered list
58  * changelist_rename() - renames all datasets appropriately when doing a rename
59  * changelist_unshare() - unshares all the nodes in a given changelist
60  * changelist_haszonedchild() - check if there is any child exported to
61  *				a local zone
62  */
63 typedef struct prop_changenode {
64 	zfs_handle_t		*cn_handle;
65 	int			cn_shared;
66 	int			cn_mounted;
67 	int			cn_zoned;
68 	uu_list_node_t		cn_listnode;
69 } prop_changenode_t;
70 
71 struct prop_changelist {
72 	zfs_prop_t		cl_prop;
73 	zfs_prop_t		cl_realprop;
74 	uu_list_pool_t		*cl_pool;
75 	uu_list_t		*cl_list;
76 	boolean_t		cl_waslegacy;
77 	boolean_t		cl_allchildren;
78 	boolean_t		cl_alldependents;
79 	int			cl_flags;
80 	boolean_t		cl_haszonedchild;
81 	boolean_t		cl_sorted;
82 };
83 
84 /*
85  * If the property is 'mountpoint', go through and unmount filesystems as
86  * necessary.  We don't do the same for 'sharenfs', because we can just re-share
87  * with different options without interrupting service.
88  */
89 int
90 changelist_prefix(prop_changelist_t *clp)
91 {
92 	prop_changenode_t *cn;
93 	int ret = 0;
94 
95 	if (clp->cl_prop != ZFS_PROP_MOUNTPOINT)
96 		return (0);
97 
98 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
99 	    cn = uu_list_next(clp->cl_list, cn)) {
100 		/*
101 		 * If we are in the global zone, but this dataset is exported
102 		 * to a local zone, do nothing.
103 		 */
104 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
105 			continue;
106 
107 		if (ZFS_IS_VOLUME(cn->cn_handle)) {
108 			switch (clp->cl_realprop) {
109 			case ZFS_PROP_NAME:
110 				/*
111 				 * If this was a rename, unshare the zvol, and
112 				 * remove the /dev/zvol links.
113 				 */
114 				(void) zfs_unshare_iscsi(cn->cn_handle);
115 
116 				if (zvol_remove_link(cn->cn_handle->zfs_hdl,
117 				    cn->cn_handle->zfs_name) != 0)
118 					ret = -1;
119 				break;
120 
121 			case ZFS_PROP_VOLSIZE:
122 				/*
123 				 * If this was a change to the volume size, we
124 				 * need to unshare and reshare the volume.
125 				 */
126 				(void) zfs_unshare_iscsi(cn->cn_handle);
127 				break;
128 			}
129 		} else if (zfs_unmount(cn->cn_handle, NULL, clp->cl_flags) != 0)
130 			ret = -1;
131 	}
132 
133 	return (ret);
134 }
135 
136 /*
137  * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
138  * reshare the filesystems as necessary.  In changelist_gather() we recorded
139  * whether the filesystem was previously shared or mounted.  The action we take
140  * depends on the previous state, and whether the value was previously 'legacy'.
141  * For non-legacy properties, we only remount/reshare the filesystem if it was
142  * previously mounted/shared.  Otherwise, we always remount/reshare the
143  * filesystem.
144  */
145 int
146 changelist_postfix(prop_changelist_t *clp)
147 {
148 	prop_changenode_t *cn;
149 	char shareopts[ZFS_MAXPROPLEN];
150 	int ret = 0;
151 	libzfs_handle_t *hdl;
152 
153 	/*
154 	 * If we're changing the mountpoint, attempt to destroy the underlying
155 	 * mountpoint.  All other datasets will have inherited from this dataset
156 	 * (in which case their mountpoints exist in the filesystem in the new
157 	 * location), or have explicit mountpoints set (in which case they won't
158 	 * be in the changelist).
159 	 */
160 	if ((cn = uu_list_last(clp->cl_list)) == NULL)
161 		return (0);
162 
163 	if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
164 		remove_mountpoint(cn->cn_handle);
165 
166 	/*
167 	 * It is possible that the changelist_prefix() used libshare
168 	 * to unshare some entries. Since libshare caches data, an
169 	 * attempt to reshare during postfix can fail unless libshare
170 	 * is uninitialized here so that it will reinitialize later.
171 	 */
172 	if (cn->cn_handle != NULL) {
173 	    hdl = cn->cn_handle->zfs_hdl;
174 	    assert(hdl != NULL);
175 	    zfs_uninit_libshare(hdl);
176 	}
177 
178 	/*
179 	 * We walk the datasets in reverse, because we want to mount any parent
180 	 * datasets before mounting the children.
181 	 */
182 	for (cn = uu_list_last(clp->cl_list); cn != NULL;
183 	    cn = uu_list_prev(clp->cl_list, cn)) {
184 		/*
185 		 * If we are in the global zone, but this dataset is exported
186 		 * to a local zone, do nothing.
187 		 */
188 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
189 			continue;
190 
191 		zfs_refresh_properties(cn->cn_handle);
192 
193 		if (ZFS_IS_VOLUME(cn->cn_handle)) {
194 			/*
195 			 * If we're doing a rename, recreate the /dev/zvol
196 			 * links.
197 			 */
198 			if (clp->cl_realprop == ZFS_PROP_NAME &&
199 			    zvol_create_link(cn->cn_handle->zfs_hdl,
200 			    cn->cn_handle->zfs_name) != 0) {
201 				ret = -1;
202 			} else if (cn->cn_shared ||
203 			    clp->cl_prop == ZFS_PROP_SHAREISCSI) {
204 				if (zfs_prop_get(cn->cn_handle,
205 				    ZFS_PROP_SHAREISCSI, shareopts,
206 				    sizeof (shareopts), NULL, NULL, 0,
207 				    B_FALSE) == 0 &&
208 				    strcmp(shareopts, "off") == 0) {
209 					ret = zfs_unshare_iscsi(cn->cn_handle);
210 				} else {
211 					ret = zfs_share_iscsi(cn->cn_handle);
212 				}
213 			}
214 
215 			continue;
216 		}
217 
218 		if ((clp->cl_waslegacy || cn->cn_mounted) &&
219 		    !zfs_is_mounted(cn->cn_handle, NULL) &&
220 		    zfs_mount(cn->cn_handle, NULL, 0) != 0)
221 			ret = -1;
222 
223 		/*
224 		 * We always re-share even if the filesystem is currently
225 		 * shared, so that we can adopt any new options.
226 		 */
227 		if (cn->cn_shared ||
228 		    (clp->cl_prop == ZFS_PROP_SHARENFS && clp->cl_waslegacy)) {
229 			if (zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
230 			    shareopts, sizeof (shareopts), NULL, NULL, 0,
231 			    B_FALSE) == 0 && strcmp(shareopts, "off") == 0) {
232 				ret = zfs_unshare_nfs(cn->cn_handle, NULL);
233 			} else {
234 				ret = zfs_share_nfs(cn->cn_handle);
235 			}
236 		}
237 	}
238 
239 	return (ret);
240 }
241 
242 /*
243  * Is this "dataset" a child of "parent"?
244  */
245 static boolean_t
246 isa_child_of(const char *dataset, const char *parent)
247 {
248 	int len;
249 
250 	len = strlen(parent);
251 
252 	if (strncmp(dataset, parent, len) == 0 &&
253 	    (dataset[len] == '@' || dataset[len] == '/' ||
254 	    dataset[len] == '\0'))
255 		return (B_TRUE);
256 	else
257 		return (B_FALSE);
258 
259 }
260 
261 /*
262  * If we rename a filesystem, child filesystem handles are no longer valid
263  * since we identify each dataset by its name in the ZFS namespace.  As a
264  * result, we have to go through and fix up all the names appropriately.  We
265  * could do this automatically if libzfs kept track of all open handles, but
266  * this is a lot less work.
267  */
268 void
269 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
270 {
271 	prop_changenode_t *cn;
272 	char newname[ZFS_MAXNAMELEN];
273 
274 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
275 	    cn = uu_list_next(clp->cl_list, cn)) {
276 		/*
277 		 * Do not rename a clone that's not in the source hierarchy.
278 		 */
279 		if (!isa_child_of(cn->cn_handle->zfs_name, src))
280 			continue;
281 
282 		/*
283 		 * Destroy the previous mountpoint if needed.
284 		 */
285 		remove_mountpoint(cn->cn_handle);
286 
287 		(void) strlcpy(newname, dst, sizeof (newname));
288 		(void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
289 
290 		(void) strlcpy(cn->cn_handle->zfs_name, newname,
291 		    sizeof (cn->cn_handle->zfs_name));
292 	}
293 }
294 
295 /*
296  * Given a gathered changelist for the 'sharenfs' property, unshare all the
297  * datasets in the list.
298  */
299 int
300 changelist_unshare(prop_changelist_t *clp)
301 {
302 	prop_changenode_t *cn;
303 	int ret = 0;
304 
305 	if (clp->cl_prop != ZFS_PROP_SHARENFS)
306 		return (0);
307 
308 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
309 	    cn = uu_list_next(clp->cl_list, cn)) {
310 		if (zfs_unshare_nfs(cn->cn_handle, NULL) != 0)
311 			ret = -1;
312 	}
313 
314 	return (ret);
315 }
316 
317 /*
318  * Check if there is any child exported to a local zone in a given changelist.
319  * This information has already been recorded while gathering the changelist
320  * via changelist_gather().
321  */
322 int
323 changelist_haszonedchild(prop_changelist_t *clp)
324 {
325 	return (clp->cl_haszonedchild);
326 }
327 
328 /*
329  * Remove a node from a gathered list.
330  */
331 void
332 changelist_remove(zfs_handle_t *zhp, prop_changelist_t *clp)
333 {
334 	prop_changenode_t *cn;
335 
336 	for (cn = uu_list_first(clp->cl_list); cn != NULL;
337 	    cn = uu_list_next(clp->cl_list, cn)) {
338 
339 		if (strcmp(cn->cn_handle->zfs_name, zhp->zfs_name) == 0) {
340 			uu_list_remove(clp->cl_list, cn);
341 			zfs_close(cn->cn_handle);
342 			free(cn);
343 			return;
344 		}
345 	}
346 }
347 
348 /*
349  * Release any memory associated with a changelist.
350  */
351 void
352 changelist_free(prop_changelist_t *clp)
353 {
354 	prop_changenode_t *cn;
355 	void *cookie;
356 
357 	if (clp->cl_list) {
358 		cookie = NULL;
359 		while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
360 			zfs_close(cn->cn_handle);
361 			free(cn);
362 		}
363 
364 		uu_list_destroy(clp->cl_list);
365 	}
366 	if (clp->cl_pool)
367 		uu_list_pool_destroy(clp->cl_pool);
368 
369 	free(clp);
370 }
371 
372 static int
373 change_one(zfs_handle_t *zhp, void *data)
374 {
375 	prop_changelist_t *clp = data;
376 	char property[ZFS_MAXPROPLEN];
377 	char where[64];
378 	prop_changenode_t *cn;
379 	zfs_source_t sourcetype;
380 
381 	/*
382 	 * We only want to unmount/unshare those filesystems that may inherit
383 	 * from the target filesystem.  If we find any filesystem with a
384 	 * locally set mountpoint, we ignore any children since changing the
385 	 * property will not affect them.  If this is a rename, we iterate
386 	 * over all children regardless, since we need them unmounted in
387 	 * order to do the rename.  Also, if this is a volume and we're doing
388 	 * a rename, then always add it to the changelist.
389 	 */
390 
391 	if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
392 	    zfs_prop_get(zhp, clp->cl_prop, property,
393 	    sizeof (property), &sourcetype, where, sizeof (where),
394 	    B_FALSE) != 0) {
395 		zfs_close(zhp);
396 		return (0);
397 	}
398 
399 	if (clp->cl_alldependents || clp->cl_allchildren ||
400 	    sourcetype == ZFS_SRC_DEFAULT || sourcetype == ZFS_SRC_INHERITED) {
401 		if ((cn = zfs_alloc(zfs_get_handle(zhp),
402 		    sizeof (prop_changenode_t))) == NULL) {
403 			zfs_close(zhp);
404 			return (-1);
405 		}
406 
407 		cn->cn_handle = zhp;
408 		cn->cn_mounted = zfs_is_mounted(zhp, NULL);
409 		cn->cn_shared = zfs_is_shared(zhp);
410 		cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
411 
412 		/* Indicate if any child is exported to a local zone. */
413 		if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
414 			clp->cl_haszonedchild = B_TRUE;
415 
416 		uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
417 
418 		if (clp->cl_sorted) {
419 			uu_list_index_t idx;
420 
421 			(void) uu_list_find(clp->cl_list, cn, NULL,
422 			    &idx);
423 			uu_list_insert(clp->cl_list, cn, idx);
424 		} else {
425 			ASSERT(!clp->cl_alldependents);
426 			verify(uu_list_insert_before(clp->cl_list,
427 			    uu_list_first(clp->cl_list), cn) == 0);
428 		}
429 
430 		if (!clp->cl_alldependents)
431 			return (zfs_iter_children(zhp, change_one, data));
432 	} else {
433 		zfs_close(zhp);
434 	}
435 
436 	return (0);
437 }
438 
439 /*ARGSUSED*/
440 static int
441 compare_mountpoints(const void *a, const void *b, void *unused)
442 {
443 	const prop_changenode_t *ca = a;
444 	const prop_changenode_t *cb = b;
445 
446 	char mounta[MAXPATHLEN];
447 	char mountb[MAXPATHLEN];
448 
449 	boolean_t hasmounta, hasmountb;
450 
451 	/*
452 	 * When unsharing or unmounting filesystems, we need to do it in
453 	 * mountpoint order.  This allows the user to have a mountpoint
454 	 * hierarchy that is different from the dataset hierarchy, and still
455 	 * allow it to be changed.  However, if either dataset doesn't have a
456 	 * mountpoint (because it is a volume or a snapshot), we place it at the
457 	 * end of the list, because it doesn't affect our change at all.
458 	 */
459 	hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
460 	    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
461 	hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
462 	    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
463 
464 	if (!hasmounta && hasmountb)
465 		return (-1);
466 	else if (hasmounta && !hasmountb)
467 		return (1);
468 	else if (!hasmounta && !hasmountb)
469 		return (0);
470 	else
471 		return (strcmp(mountb, mounta));
472 }
473 
474 /*
475  * Given a ZFS handle and a property, construct a complete list of datasets
476  * that need to be modified as part of this process.  For anything but the
477  * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
478  * Otherwise, we iterate over all children and look for any datasets that
479  * inherit the property.  For each such dataset, we add it to the list and
480  * mark whether it was shared beforehand.
481  */
482 prop_changelist_t *
483 changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int flags)
484 {
485 	prop_changelist_t *clp;
486 	prop_changenode_t *cn;
487 	zfs_handle_t *temp;
488 	char property[ZFS_MAXPROPLEN];
489 	uu_compare_fn_t *compare = NULL;
490 
491 	if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
492 		return (NULL);
493 
494 	/*
495 	 * For mountpoint-related tasks, we want to sort everything by
496 	 * mountpoint, so that we mount and unmount them in the appropriate
497 	 * order, regardless of their position in the hierarchy.
498 	 */
499 	if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
500 	    prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS) {
501 		compare = compare_mountpoints;
502 		clp->cl_sorted = B_TRUE;
503 	}
504 
505 	clp->cl_pool = uu_list_pool_create("changelist_pool",
506 	    sizeof (prop_changenode_t),
507 	    offsetof(prop_changenode_t, cn_listnode),
508 	    compare, 0);
509 	if (clp->cl_pool == NULL) {
510 		assert(uu_error() == UU_ERROR_NO_MEMORY);
511 		(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
512 		changelist_free(clp);
513 		return (NULL);
514 	}
515 
516 	clp->cl_list = uu_list_create(clp->cl_pool, NULL,
517 	    clp->cl_sorted ? UU_LIST_SORTED : 0);
518 	clp->cl_flags = flags;
519 
520 	if (clp->cl_list == NULL) {
521 		assert(uu_error() == UU_ERROR_NO_MEMORY);
522 		(void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
523 		changelist_free(clp);
524 		return (NULL);
525 	}
526 
527 	/*
528 	 * If this is a rename or the 'zoned' property, we pretend we're
529 	 * changing the mountpoint and flag it so we can catch all children in
530 	 * change_one().
531 	 *
532 	 * Flag cl_alldependents to catch all children plus the dependents
533 	 * (clones) that are not in the hierarchy.
534 	 */
535 	if (prop == ZFS_PROP_NAME) {
536 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
537 		clp->cl_alldependents = B_TRUE;
538 	} else if (prop == ZFS_PROP_ZONED) {
539 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
540 		clp->cl_allchildren = B_TRUE;
541 	} else if (prop == ZFS_PROP_CANMOUNT) {
542 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
543 	} else if (prop == ZFS_PROP_VOLSIZE) {
544 		clp->cl_prop = ZFS_PROP_MOUNTPOINT;
545 	} else {
546 		clp->cl_prop = prop;
547 	}
548 	clp->cl_realprop = prop;
549 
550 	if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
551 	    clp->cl_prop != ZFS_PROP_SHARENFS &&
552 	    clp->cl_prop != ZFS_PROP_SHAREISCSI)
553 		return (clp);
554 
555 	if (clp->cl_alldependents) {
556 		if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
557 			changelist_free(clp);
558 			return (NULL);
559 		}
560 	} else if (zfs_iter_children(zhp, change_one, clp) != 0) {
561 		changelist_free(clp);
562 		return (NULL);
563 	}
564 
565 	/*
566 	 * We have to re-open ourselves because we auto-close all the handles
567 	 * and can't tell the difference.
568 	 */
569 	if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
570 	    ZFS_TYPE_ANY)) == NULL) {
571 		changelist_free(clp);
572 		return (NULL);
573 	}
574 
575 	/*
576 	 * Always add ourself to the list.  We add ourselves to the end so that
577 	 * we're the last to be unmounted.
578 	 */
579 	if ((cn = zfs_alloc(zhp->zfs_hdl,
580 	    sizeof (prop_changenode_t))) == NULL) {
581 		zfs_close(temp);
582 		changelist_free(clp);
583 		return (NULL);
584 	}
585 
586 	cn->cn_handle = temp;
587 	cn->cn_mounted = zfs_is_mounted(temp, NULL);
588 	cn->cn_shared = zfs_is_shared(temp);
589 	cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
590 
591 	uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
592 	if (clp->cl_sorted) {
593 		uu_list_index_t idx;
594 		(void) uu_list_find(clp->cl_list, cn, NULL, &idx);
595 		uu_list_insert(clp->cl_list, cn, idx);
596 	} else {
597 		verify(uu_list_insert_after(clp->cl_list,
598 		    uu_list_last(clp->cl_list), cn) == 0);
599 	}
600 
601 	/*
602 	 * If the property was previously 'legacy' or 'none', record this fact,
603 	 * as the behavior of changelist_postfix() will be different.
604 	 */
605 	if (zfs_prop_get(zhp, prop, property, sizeof (property),
606 	    NULL, NULL, 0, B_FALSE) == 0 &&
607 	    (strcmp(property, "legacy") == 0 || strcmp(property, "none") == 0 ||
608 	    strcmp(property, "off") == 0))
609 		clp->cl_waslegacy = B_TRUE;
610 
611 	return (clp);
612 }
613