]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzfs/libzfs_changelist.c
Add linux user disk support
[mirror_zfs.git] / lib / libzfs / libzfs_changelist.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
24 * Use is subject to license terms.
25 *
26 * Portions Copyright 2007 Ramprakash Jelari
27 */
28
34dc7c2f
BB
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 */
63typedef struct prop_changenode {
64 zfs_handle_t *cn_handle;
65 int cn_shared;
66 int cn_mounted;
67 int cn_zoned;
68 boolean_t cn_needpost; /* is postfix() needed? */
69 uu_list_node_t cn_listnode;
70} prop_changenode_t;
71
72struct prop_changelist {
73 zfs_prop_t cl_prop;
74 zfs_prop_t cl_realprop;
75 zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */
76 uu_list_pool_t *cl_pool;
77 uu_list_t *cl_list;
78 boolean_t cl_waslegacy;
79 boolean_t cl_allchildren;
80 boolean_t cl_alldependents;
b128c09f
BB
81 int cl_mflags; /* Mount flags */
82 int cl_gflags; /* Gather request flags */
34dc7c2f
BB
83 boolean_t cl_haszonedchild;
84 boolean_t cl_sorted;
85};
86
87/*
88 * If the property is 'mountpoint', go through and unmount filesystems as
89 * necessary. We don't do the same for 'sharenfs', because we can just re-share
90 * with different options without interrupting service. We do handle 'sharesmb'
91 * since there may be old resource names that need to be removed.
92 */
93int
94changelist_prefix(prop_changelist_t *clp)
95{
d603ed6c 96#ifdef HAVE_ZPL
34dc7c2f
BB
97 prop_changenode_t *cn;
98 int ret = 0;
99
100 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
101 clp->cl_prop != ZFS_PROP_SHARESMB)
102 return (0);
103
104 for (cn = uu_list_first(clp->cl_list); cn != NULL;
105 cn = uu_list_next(clp->cl_list, cn)) {
106
107 /* if a previous loop failed, set the remaining to false */
108 if (ret == -1) {
109 cn->cn_needpost = B_FALSE;
110 continue;
111 }
112
113 /*
114 * If we are in the global zone, but this dataset is exported
115 * to a local zone, do nothing.
116 */
117 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
118 continue;
119
428870ff 120 if (!ZFS_IS_VOLUME(cn->cn_handle)) {
34dc7c2f
BB
121 /*
122 * Do the property specific processing.
123 */
124 switch (clp->cl_prop) {
125 case ZFS_PROP_MOUNTPOINT:
126 if (zfs_unmount(cn->cn_handle, NULL,
b128c09f 127 clp->cl_mflags) != 0) {
34dc7c2f
BB
128 ret = -1;
129 cn->cn_needpost = B_FALSE;
130 }
131 break;
132 case ZFS_PROP_SHARESMB:
133 (void) zfs_unshare_smb(cn->cn_handle, NULL);
134 break;
e75c13c3
BB
135 default:
136 break;
34dc7c2f
BB
137 }
138 }
139 }
140
141 if (ret == -1)
142 (void) changelist_postfix(clp);
143
144 return (ret);
d603ed6c
BB
145#else
146 return 0;
147#endif /* HAVE_ZPL */
34dc7c2f
BB
148}
149
150/*
151 * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
152 * reshare the filesystems as necessary. In changelist_gather() we recorded
153 * whether the filesystem was previously shared or mounted. The action we take
154 * depends on the previous state, and whether the value was previously 'legacy'.
155 * For non-legacy properties, we only remount/reshare the filesystem if it was
156 * previously mounted/shared. Otherwise, we always remount/reshare the
157 * filesystem.
158 */
159int
160changelist_postfix(prop_changelist_t *clp)
161{
d603ed6c 162#ifdef HAVE_ZPL
34dc7c2f
BB
163 prop_changenode_t *cn;
164 char shareopts[ZFS_MAXPROPLEN];
165 int errors = 0;
166 libzfs_handle_t *hdl;
167
168 /*
169 * If we're changing the mountpoint, attempt to destroy the underlying
170 * mountpoint. All other datasets will have inherited from this dataset
171 * (in which case their mountpoints exist in the filesystem in the new
172 * location), or have explicit mountpoints set (in which case they won't
173 * be in the changelist).
174 */
175 if ((cn = uu_list_last(clp->cl_list)) == NULL)
176 return (0);
177
178 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
179 remove_mountpoint(cn->cn_handle);
180
181 /*
182 * It is possible that the changelist_prefix() used libshare
183 * to unshare some entries. Since libshare caches data, an
184 * attempt to reshare during postfix can fail unless libshare
185 * is uninitialized here so that it will reinitialize later.
186 */
187 if (cn->cn_handle != NULL) {
188 hdl = cn->cn_handle->zfs_hdl;
189 assert(hdl != NULL);
190 zfs_uninit_libshare(hdl);
191 }
192
193 /*
194 * We walk the datasets in reverse, because we want to mount any parent
195 * datasets before mounting the children. We walk all datasets even if
196 * there are errors.
197 */
198 for (cn = uu_list_last(clp->cl_list); cn != NULL;
199 cn = uu_list_prev(clp->cl_list, cn)) {
200
201 boolean_t sharenfs;
202 boolean_t sharesmb;
9babb374 203 boolean_t mounted;
34dc7c2f
BB
204
205 /*
206 * If we are in the global zone, but this dataset is exported
207 * to a local zone, do nothing.
208 */
209 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
210 continue;
211
212 /* Only do post-processing if it's required */
213 if (!cn->cn_needpost)
214 continue;
215 cn->cn_needpost = B_FALSE;
216
217 zfs_refresh_properties(cn->cn_handle);
218
428870ff 219 if (ZFS_IS_VOLUME(cn->cn_handle))
34dc7c2f 220 continue;
34dc7c2f
BB
221
222 /*
223 * Remount if previously mounted or mountpoint was legacy,
224 * or sharenfs or sharesmb property is set.
225 */
226 sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
227 shareopts, sizeof (shareopts), NULL, NULL, 0,
228 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
229
230 sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
231 shareopts, sizeof (shareopts), NULL, NULL, 0,
232 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
233
9babb374
BB
234 mounted = zfs_is_mounted(cn->cn_handle, NULL);
235
236 if (!mounted && (cn->cn_mounted ||
237 ((sharenfs || sharesmb || clp->cl_waslegacy) &&
238 (zfs_prop_get_int(cn->cn_handle,
239 ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON)))) {
240
241 if (zfs_mount(cn->cn_handle, NULL, 0) != 0)
242 errors++;
243 else
244 mounted = TRUE;
245 }
34dc7c2f
BB
246
247 /*
9babb374
BB
248 * If the file system is mounted we always re-share even
249 * if the filesystem is currently shared, so that we can
250 * adopt any new options.
34dc7c2f 251 */
9babb374 252 if (sharenfs && mounted)
34dc7c2f
BB
253 errors += zfs_share_nfs(cn->cn_handle);
254 else if (cn->cn_shared || clp->cl_waslegacy)
255 errors += zfs_unshare_nfs(cn->cn_handle, NULL);
9babb374 256 if (sharesmb && mounted)
34dc7c2f
BB
257 errors += zfs_share_smb(cn->cn_handle);
258 else if (cn->cn_shared || clp->cl_waslegacy)
259 errors += zfs_unshare_smb(cn->cn_handle, NULL);
260 }
261
262 return (errors ? -1 : 0);
d603ed6c
BB
263#else
264 return 0;
265#endif /* HAVE_ZPL */
34dc7c2f
BB
266}
267
268/*
269 * Is this "dataset" a child of "parent"?
270 */
271boolean_t
272isa_child_of(const char *dataset, const char *parent)
273{
274 int len;
275
276 len = strlen(parent);
277
278 if (strncmp(dataset, parent, len) == 0 &&
279 (dataset[len] == '@' || dataset[len] == '/' ||
280 dataset[len] == '\0'))
281 return (B_TRUE);
282 else
283 return (B_FALSE);
284
285}
286
287/*
288 * If we rename a filesystem, child filesystem handles are no longer valid
289 * since we identify each dataset by its name in the ZFS namespace. As a
290 * result, we have to go through and fix up all the names appropriately. We
291 * could do this automatically if libzfs kept track of all open handles, but
292 * this is a lot less work.
293 */
294void
295changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
296{
297 prop_changenode_t *cn;
298 char newname[ZFS_MAXNAMELEN];
299
300 for (cn = uu_list_first(clp->cl_list); cn != NULL;
301 cn = uu_list_next(clp->cl_list, cn)) {
302 /*
303 * Do not rename a clone that's not in the source hierarchy.
304 */
305 if (!isa_child_of(cn->cn_handle->zfs_name, src))
306 continue;
307
308 /*
309 * Destroy the previous mountpoint if needed.
310 */
311 remove_mountpoint(cn->cn_handle);
312
313 (void) strlcpy(newname, dst, sizeof (newname));
314 (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
315
316 (void) strlcpy(cn->cn_handle->zfs_name, newname,
317 sizeof (cn->cn_handle->zfs_name));
318 }
319}
320
321/*
322 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
323 * unshare all the datasets in the list.
324 */
325int
326changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
327{
d603ed6c 328#ifdef HAVE_ZPL
34dc7c2f
BB
329 prop_changenode_t *cn;
330 int ret = 0;
331
332 if (clp->cl_prop != ZFS_PROP_SHARENFS &&
333 clp->cl_prop != ZFS_PROP_SHARESMB)
334 return (0);
335
336 for (cn = uu_list_first(clp->cl_list); cn != NULL;
337 cn = uu_list_next(clp->cl_list, cn)) {
338 if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
339 ret = -1;
340 }
341
342 return (ret);
d603ed6c
BB
343#else
344 return 0;
345#endif
34dc7c2f
BB
346}
347
348/*
349 * Check if there is any child exported to a local zone in a given changelist.
350 * This information has already been recorded while gathering the changelist
351 * via changelist_gather().
352 */
353int
354changelist_haszonedchild(prop_changelist_t *clp)
355{
356 return (clp->cl_haszonedchild);
357}
358
359/*
360 * Remove a node from a gathered list.
361 */
362void
363changelist_remove(prop_changelist_t *clp, const char *name)
364{
365 prop_changenode_t *cn;
366
367 for (cn = uu_list_first(clp->cl_list); cn != NULL;
368 cn = uu_list_next(clp->cl_list, cn)) {
369
370 if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
371 uu_list_remove(clp->cl_list, cn);
372 zfs_close(cn->cn_handle);
373 free(cn);
374 return;
375 }
376 }
377}
378
379/*
380 * Release any memory associated with a changelist.
381 */
382void
383changelist_free(prop_changelist_t *clp)
384{
385 prop_changenode_t *cn;
386 void *cookie;
387
388 if (clp->cl_list) {
389 cookie = NULL;
390 while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
391 zfs_close(cn->cn_handle);
392 free(cn);
393 }
394
395 uu_list_destroy(clp->cl_list);
396 }
397 if (clp->cl_pool)
398 uu_list_pool_destroy(clp->cl_pool);
399
400 free(clp);
401}
402
403static int
404change_one(zfs_handle_t *zhp, void *data)
405{
406 prop_changelist_t *clp = data;
407 char property[ZFS_MAXPROPLEN];
408 char where[64];
409 prop_changenode_t *cn;
410 zprop_source_t sourcetype;
411 zprop_source_t share_sourcetype;
412
413 /*
414 * We only want to unmount/unshare those filesystems that may inherit
415 * from the target filesystem. If we find any filesystem with a
416 * locally set mountpoint, we ignore any children since changing the
417 * property will not affect them. If this is a rename, we iterate
418 * over all children regardless, since we need them unmounted in
419 * order to do the rename. Also, if this is a volume and we're doing
420 * a rename, then always add it to the changelist.
421 */
422
423 if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
424 zfs_prop_get(zhp, clp->cl_prop, property,
425 sizeof (property), &sourcetype, where, sizeof (where),
426 B_FALSE) != 0) {
427 zfs_close(zhp);
428 return (0);
429 }
430
431 /*
432 * If we are "watching" sharenfs or sharesmb
433 * then check out the companion property which is tracked
434 * in cl_shareprop
435 */
436 if (clp->cl_shareprop != ZPROP_INVAL &&
437 zfs_prop_get(zhp, clp->cl_shareprop, property,
438 sizeof (property), &share_sourcetype, where, sizeof (where),
439 B_FALSE) != 0) {
440 zfs_close(zhp);
441 return (0);
442 }
443
444 if (clp->cl_alldependents || clp->cl_allchildren ||
445 sourcetype == ZPROP_SRC_DEFAULT ||
446 sourcetype == ZPROP_SRC_INHERITED ||
447 (clp->cl_shareprop != ZPROP_INVAL &&
448 (share_sourcetype == ZPROP_SRC_DEFAULT ||
449 share_sourcetype == ZPROP_SRC_INHERITED))) {
450 if ((cn = zfs_alloc(zfs_get_handle(zhp),
451 sizeof (prop_changenode_t))) == NULL) {
452 zfs_close(zhp);
453 return (-1);
454 }
455
456 cn->cn_handle = zhp;
b128c09f
BB
457 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
458 zfs_is_mounted(zhp, NULL);
34dc7c2f
BB
459 cn->cn_shared = zfs_is_shared(zhp);
460 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
461 cn->cn_needpost = B_TRUE;
462
463 /* Indicate if any child is exported to a local zone. */
464 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
465 clp->cl_haszonedchild = B_TRUE;
466
467 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
468
469 if (clp->cl_sorted) {
470 uu_list_index_t idx;
471
472 (void) uu_list_find(clp->cl_list, cn, NULL,
473 &idx);
474 uu_list_insert(clp->cl_list, cn, idx);
475 } else {
45d1cae3
BB
476 /*
477 * Add this child to beginning of the list. Children
478 * below this one in the hierarchy will get added above
479 * this one in the list. This produces a list in
480 * reverse dataset name order.
481 * This is necessary when the original mountpoint
482 * is legacy or none.
483 */
34dc7c2f
BB
484 ASSERT(!clp->cl_alldependents);
485 verify(uu_list_insert_before(clp->cl_list,
486 uu_list_first(clp->cl_list), cn) == 0);
487 }
488
489 if (!clp->cl_alldependents)
490 return (zfs_iter_children(zhp, change_one, data));
491 } else {
492 zfs_close(zhp);
493 }
494
495 return (0);
496}
497
498/*ARGSUSED*/
499static int
500compare_mountpoints(const void *a, const void *b, void *unused)
501{
502 const prop_changenode_t *ca = a;
503 const prop_changenode_t *cb = b;
504
505 char mounta[MAXPATHLEN];
506 char mountb[MAXPATHLEN];
507
508 boolean_t hasmounta, hasmountb;
509
510 /*
511 * When unsharing or unmounting filesystems, we need to do it in
512 * mountpoint order. This allows the user to have a mountpoint
513 * hierarchy that is different from the dataset hierarchy, and still
514 * allow it to be changed. However, if either dataset doesn't have a
515 * mountpoint (because it is a volume or a snapshot), we place it at the
516 * end of the list, because it doesn't affect our change at all.
517 */
518 hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
519 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
520 hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
521 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
522
523 if (!hasmounta && hasmountb)
524 return (-1);
525 else if (hasmounta && !hasmountb)
526 return (1);
527 else if (!hasmounta && !hasmountb)
528 return (0);
529 else
530 return (strcmp(mountb, mounta));
531}
532
533/*
534 * Given a ZFS handle and a property, construct a complete list of datasets
535 * that need to be modified as part of this process. For anything but the
536 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
537 * Otherwise, we iterate over all children and look for any datasets that
538 * inherit the property. For each such dataset, we add it to the list and
539 * mark whether it was shared beforehand.
540 */
541prop_changelist_t *
b128c09f
BB
542changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
543 int mnt_flags)
34dc7c2f
BB
544{
545 prop_changelist_t *clp;
546 prop_changenode_t *cn;
547 zfs_handle_t *temp;
548 char property[ZFS_MAXPROPLEN];
549 uu_compare_fn_t *compare = NULL;
45d1cae3 550 boolean_t legacy = B_FALSE;
34dc7c2f
BB
551
552 if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
553 return (NULL);
554
555 /*
556 * For mountpoint-related tasks, we want to sort everything by
557 * mountpoint, so that we mount and unmount them in the appropriate
558 * order, regardless of their position in the hierarchy.
559 */
560 if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
561 prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
562 prop == ZFS_PROP_SHARESMB) {
45d1cae3
BB
563
564 if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
565 property, sizeof (property),
566 NULL, NULL, 0, B_FALSE) == 0 &&
567 (strcmp(property, "legacy") == 0 ||
568 strcmp(property, "none") == 0)) {
569
570 legacy = B_TRUE;
571 }
572 if (!legacy) {
573 compare = compare_mountpoints;
574 clp->cl_sorted = B_TRUE;
575 }
34dc7c2f
BB
576 }
577
578 clp->cl_pool = uu_list_pool_create("changelist_pool",
579 sizeof (prop_changenode_t),
580 offsetof(prop_changenode_t, cn_listnode),
581 compare, 0);
582 if (clp->cl_pool == NULL) {
583 assert(uu_error() == UU_ERROR_NO_MEMORY);
584 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
585 changelist_free(clp);
586 return (NULL);
587 }
588
589 clp->cl_list = uu_list_create(clp->cl_pool, NULL,
590 clp->cl_sorted ? UU_LIST_SORTED : 0);
b128c09f
BB
591 clp->cl_gflags = gather_flags;
592 clp->cl_mflags = mnt_flags;
34dc7c2f
BB
593
594 if (clp->cl_list == NULL) {
595 assert(uu_error() == UU_ERROR_NO_MEMORY);
596 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
597 changelist_free(clp);
598 return (NULL);
599 }
600
601 /*
602 * If this is a rename or the 'zoned' property, we pretend we're
603 * changing the mountpoint and flag it so we can catch all children in
604 * change_one().
605 *
606 * Flag cl_alldependents to catch all children plus the dependents
607 * (clones) that are not in the hierarchy.
608 */
609 if (prop == ZFS_PROP_NAME) {
610 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
611 clp->cl_alldependents = B_TRUE;
612 } else if (prop == ZFS_PROP_ZONED) {
613 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
614 clp->cl_allchildren = B_TRUE;
615 } else if (prop == ZFS_PROP_CANMOUNT) {
616 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
617 } else if (prop == ZFS_PROP_VOLSIZE) {
618 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
34dc7c2f
BB
619 } else {
620 clp->cl_prop = prop;
621 }
622 clp->cl_realprop = prop;
623
624 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
625 clp->cl_prop != ZFS_PROP_SHARENFS &&
428870ff 626 clp->cl_prop != ZFS_PROP_SHARESMB)
34dc7c2f
BB
627 return (clp);
628
629 /*
630 * If watching SHARENFS or SHARESMB then
631 * also watch its companion property.
632 */
633 if (clp->cl_prop == ZFS_PROP_SHARENFS)
634 clp->cl_shareprop = ZFS_PROP_SHARESMB;
635 else if (clp->cl_prop == ZFS_PROP_SHARESMB)
636 clp->cl_shareprop = ZFS_PROP_SHARENFS;
637
638 if (clp->cl_alldependents) {
639 if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
640 changelist_free(clp);
641 return (NULL);
642 }
643 } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
644 changelist_free(clp);
645 return (NULL);
646 }
647
648 /*
649 * We have to re-open ourselves because we auto-close all the handles
650 * and can't tell the difference.
651 */
652 if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
653 ZFS_TYPE_DATASET)) == NULL) {
654 changelist_free(clp);
655 return (NULL);
656 }
657
658 /*
659 * Always add ourself to the list. We add ourselves to the end so that
660 * we're the last to be unmounted.
661 */
662 if ((cn = zfs_alloc(zhp->zfs_hdl,
663 sizeof (prop_changenode_t))) == NULL) {
664 zfs_close(temp);
665 changelist_free(clp);
666 return (NULL);
667 }
668
669 cn->cn_handle = temp;
b128c09f
BB
670 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
671 zfs_is_mounted(temp, NULL);
34dc7c2f
BB
672 cn->cn_shared = zfs_is_shared(temp);
673 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
674 cn->cn_needpost = B_TRUE;
675
676 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
677 if (clp->cl_sorted) {
678 uu_list_index_t idx;
679 (void) uu_list_find(clp->cl_list, cn, NULL, &idx);
680 uu_list_insert(clp->cl_list, cn, idx);
681 } else {
45d1cae3
BB
682 /*
683 * Add the target dataset to the end of the list.
684 * The list is not really unsorted. The list will be
685 * in reverse dataset name order. This is necessary
686 * when the original mountpoint is legacy or none.
687 */
34dc7c2f
BB
688 verify(uu_list_insert_after(clp->cl_list,
689 uu_list_last(clp->cl_list), cn) == 0);
690 }
691
692 /*
693 * If the mountpoint property was previously 'legacy', or 'none',
694 * record it as the behavior of changelist_postfix() will be different.
695 */
45d1cae3 696 if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && legacy) {
34dc7c2f
BB
697 /*
698 * do not automatically mount ex-legacy datasets if
699 * we specifically set canmount to noauto
700 */
701 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
702 ZFS_CANMOUNT_NOAUTO)
703 clp->cl_waslegacy = B_TRUE;
704 }
705
706 return (clp);
707}