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