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