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