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