]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzfs/libzfs_changelist.c
Remove dependency on sharetab file and refactor sharing logic
[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
c15d36c6 27 * Copyright (c) 2014, 2020 by Delphix. All rights reserved.
23d70cde 28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
0c6d0936 29 * Copyright (c) 2018 Datto Inc.
34dc7c2f
BB
30 */
31
34dc7c2f
BB
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 */
66typedef 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? */
0c6d0936 72 uu_avl_node_t cn_treenode;
34dc7c2f
BB
73} prop_changenode_t;
74
75struct prop_changelist {
76 zfs_prop_t cl_prop;
77 zfs_prop_t cl_realprop;
78 zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */
0c6d0936
AP
79 uu_avl_pool_t *cl_pool;
80 uu_avl_t *cl_tree;
34dc7c2f
BB
81 boolean_t cl_waslegacy;
82 boolean_t cl_allchildren;
83 boolean_t cl_alldependents;
b128c09f
BB
84 int cl_mflags; /* Mount flags */
85 int cl_gflags; /* Gather request flags */
34dc7c2f 86 boolean_t cl_haszonedchild;
34dc7c2f
BB
87};
88
89/*
90 * If the property is 'mountpoint', go through and unmount filesystems as
91 * necessary. We don't do the same for 'sharenfs', because we can just re-share
92 * with different options without interrupting service. We do handle 'sharesmb'
93 * since there may be old resource names that need to be removed.
94 */
95int
96changelist_prefix(prop_changelist_t *clp)
97{
98 prop_changenode_t *cn;
0c6d0936 99 uu_avl_walk_t *walk;
34dc7c2f 100 int ret = 0;
c15d36c6 101 boolean_t commit_smb_shares = B_FALSE;
34dc7c2f
BB
102
103 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
104 clp->cl_prop != ZFS_PROP_SHARESMB)
105 return (0);
106
0c6d0936
AP
107 if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
108 return (-1);
109
110 while ((cn = uu_avl_walk_next(walk)) != NULL) {
34dc7c2f
BB
111
112 /* if a previous loop failed, set the remaining to false */
113 if (ret == -1) {
114 cn->cn_needpost = B_FALSE;
115 continue;
116 }
117
118 /*
119 * If we are in the global zone, but this dataset is exported
120 * to a local zone, do nothing.
121 */
122 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
123 continue;
124
428870ff 125 if (!ZFS_IS_VOLUME(cn->cn_handle)) {
34dc7c2f
BB
126 /*
127 * Do the property specific processing.
128 */
129 switch (clp->cl_prop) {
130 case ZFS_PROP_MOUNTPOINT:
131 if (zfs_unmount(cn->cn_handle, NULL,
b128c09f 132 clp->cl_mflags) != 0) {
34dc7c2f
BB
133 ret = -1;
134 cn->cn_needpost = B_FALSE;
135 }
136 break;
137 case ZFS_PROP_SHARESMB:
138 (void) zfs_unshare_smb(cn->cn_handle, NULL);
c15d36c6 139 commit_smb_shares = B_TRUE;
34dc7c2f 140 break;
23d70cde 141
e75c13c3
BB
142 default:
143 break;
34dc7c2f
BB
144 }
145 }
146 }
147
c15d36c6
GW
148 if (commit_smb_shares)
149 zfs_commit_smb_shares();
0c6d0936
AP
150 uu_avl_walk_end(walk);
151
34dc7c2f
BB
152 if (ret == -1)
153 (void) changelist_postfix(clp);
154
155 return (ret);
156}
157
158/*
159 * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
160 * reshare the filesystems as necessary. In changelist_gather() we recorded
161 * whether the filesystem was previously shared or mounted. The action we take
162 * depends on the previous state, and whether the value was previously 'legacy'.
163 * For non-legacy properties, we only remount/reshare the filesystem if it was
164 * previously mounted/shared. Otherwise, we always remount/reshare the
165 * filesystem.
166 */
167int
168changelist_postfix(prop_changelist_t *clp)
169{
170 prop_changenode_t *cn;
0c6d0936 171 uu_avl_walk_t *walk;
34dc7c2f
BB
172 char shareopts[ZFS_MAXPROPLEN];
173 int errors = 0;
c15d36c6
GW
174 boolean_t commit_smb_shares = B_FALSE;
175 boolean_t commit_nfs_shares = B_FALSE;
34dc7c2f
BB
176
177 /*
178 * If we're changing the mountpoint, attempt to destroy the underlying
179 * mountpoint. All other datasets will have inherited from this dataset
180 * (in which case their mountpoints exist in the filesystem in the new
181 * location), or have explicit mountpoints set (in which case they won't
182 * be in the changelist).
183 */
0c6d0936 184 if ((cn = uu_avl_last(clp->cl_tree)) == NULL)
34dc7c2f
BB
185 return (0);
186
187 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
188 remove_mountpoint(cn->cn_handle);
189
34dc7c2f
BB
190 /*
191 * We walk the datasets in reverse, because we want to mount any parent
192 * datasets before mounting the children. We walk all datasets even if
193 * there are errors.
194 */
0c6d0936
AP
195 if ((walk = uu_avl_walk_start(clp->cl_tree,
196 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
197 return (-1);
198
199 while ((cn = uu_avl_walk_next(walk)) != NULL) {
34dc7c2f
BB
200
201 boolean_t sharenfs;
202 boolean_t sharesmb;
9babb374 203 boolean_t mounted;
b5256303 204 boolean_t needs_key;
34dc7c2f
BB
205
206 /*
207 * If we are in the global zone, but this dataset is exported
208 * to a local zone, do nothing.
209 */
210 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
211 continue;
212
213 /* Only do post-processing if it's required */
214 if (!cn->cn_needpost)
215 continue;
216 cn->cn_needpost = B_FALSE;
217
218 zfs_refresh_properties(cn->cn_handle);
219
428870ff 220 if (ZFS_IS_VOLUME(cn->cn_handle))
34dc7c2f 221 continue;
34dc7c2f
BB
222
223 /*
224 * Remount if previously mounted or mountpoint was legacy,
225 * or sharenfs or sharesmb 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 sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
232 shareopts, sizeof (shareopts), NULL, NULL, 0,
233 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
234
b5256303
TC
235 needs_key = (zfs_prop_get_int(cn->cn_handle,
236 ZFS_PROP_KEYSTATUS) == ZFS_KEYSTATUS_UNAVAILABLE);
237
9babb374
BB
238 mounted = zfs_is_mounted(cn->cn_handle, NULL);
239
b5256303 240 if (!mounted && !needs_key && (cn->cn_mounted ||
9babb374
BB
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 }
34dc7c2f
BB
250
251 /*
9babb374
BB
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.
34dc7c2f 255 */
c15d36c6 256 if (sharenfs && mounted) {
34dc7c2f 257 errors += zfs_share_nfs(cn->cn_handle);
c15d36c6
GW
258 commit_nfs_shares = B_TRUE;
259 } else if (cn->cn_shared || clp->cl_waslegacy) {
34dc7c2f 260 errors += zfs_unshare_nfs(cn->cn_handle, NULL);
c15d36c6
GW
261 commit_nfs_shares = B_TRUE;
262 }
263 if (sharesmb && mounted) {
34dc7c2f 264 errors += zfs_share_smb(cn->cn_handle);
c15d36c6
GW
265 commit_smb_shares = B_TRUE;
266 } else if (cn->cn_shared || clp->cl_waslegacy) {
34dc7c2f 267 errors += zfs_unshare_smb(cn->cn_handle, NULL);
c15d36c6
GW
268 commit_smb_shares = B_TRUE;
269 }
34dc7c2f 270 }
c15d36c6
GW
271 if (commit_nfs_shares)
272 zfs_commit_nfs_shares();
273 if (commit_smb_shares)
274 zfs_commit_smb_shares();
0c6d0936
AP
275 uu_avl_walk_end(walk);
276
34dc7c2f
BB
277 return (errors ? -1 : 0);
278}
279
280/*
281 * Is this "dataset" a child of "parent"?
282 */
283boolean_t
284isa_child_of(const char *dataset, const char *parent)
285{
286 int len;
287
288 len = strlen(parent);
289
290 if (strncmp(dataset, parent, len) == 0 &&
291 (dataset[len] == '@' || dataset[len] == '/' ||
292 dataset[len] == '\0'))
293 return (B_TRUE);
294 else
295 return (B_FALSE);
296
297}
298
299/*
300 * If we rename a filesystem, child filesystem handles are no longer valid
301 * since we identify each dataset by its name in the ZFS namespace. As a
302 * result, we have to go through and fix up all the names appropriately. We
303 * could do this automatically if libzfs kept track of all open handles, but
304 * this is a lot less work.
305 */
306void
307changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
308{
309 prop_changenode_t *cn;
0c6d0936 310 uu_avl_walk_t *walk;
eca7b760 311 char newname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 312
0c6d0936
AP
313 if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
314 return;
315
316 while ((cn = uu_avl_walk_next(walk)) != NULL) {
34dc7c2f
BB
317 /*
318 * Do not rename a clone that's not in the source hierarchy.
319 */
ba6a2402 320 if (!isa_child_of(cn->cn_handle->zfs_name, src))
34dc7c2f
BB
321 continue;
322
323 /*
324 * Destroy the previous mountpoint if needed.
325 */
ba6a2402 326 remove_mountpoint(cn->cn_handle);
34dc7c2f
BB
327
328 (void) strlcpy(newname, dst, sizeof (newname));
0b78aeae
B
329 (void) strlcat(newname, cn->cn_handle->zfs_name + strlen(src),
330 sizeof (newname));
34dc7c2f 331
ba6a2402
BB
332 (void) strlcpy(cn->cn_handle->zfs_name, newname,
333 sizeof (cn->cn_handle->zfs_name));
34dc7c2f 334 }
0c6d0936
AP
335
336 uu_avl_walk_end(walk);
34dc7c2f
BB
337}
338
339/*
340 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
341 * unshare all the datasets in the list.
342 */
343int
344changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
345{
346 prop_changenode_t *cn;
0c6d0936 347 uu_avl_walk_t *walk;
34dc7c2f
BB
348 int ret = 0;
349
350 if (clp->cl_prop != ZFS_PROP_SHARENFS &&
351 clp->cl_prop != ZFS_PROP_SHARESMB)
352 return (0);
353
0c6d0936
AP
354 if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
355 return (-1);
356
357 while ((cn = uu_avl_walk_next(walk)) != NULL) {
34dc7c2f
BB
358 if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
359 ret = -1;
360 }
361
c15d36c6 362 zfs_commit_proto(proto);
0c6d0936
AP
363 uu_avl_walk_end(walk);
364
34dc7c2f
BB
365 return (ret);
366}
367
368/*
369 * Check if there is any child exported to a local zone in a given changelist.
370 * This information has already been recorded while gathering the changelist
371 * via changelist_gather().
372 */
373int
374changelist_haszonedchild(prop_changelist_t *clp)
375{
376 return (clp->cl_haszonedchild);
377}
378
379/*
380 * Remove a node from a gathered list.
381 */
382void
383changelist_remove(prop_changelist_t *clp, const char *name)
384{
385 prop_changenode_t *cn;
0c6d0936 386 uu_avl_walk_t *walk;
34dc7c2f 387
0c6d0936
AP
388 if ((walk = uu_avl_walk_start(clp->cl_tree, UU_WALK_ROBUST)) == NULL)
389 return;
34dc7c2f 390
0c6d0936 391 while ((cn = uu_avl_walk_next(walk)) != NULL) {
34dc7c2f 392 if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
0c6d0936 393 uu_avl_remove(clp->cl_tree, cn);
34dc7c2f
BB
394 zfs_close(cn->cn_handle);
395 free(cn);
0c6d0936 396 uu_avl_walk_end(walk);
34dc7c2f
BB
397 return;
398 }
399 }
0c6d0936
AP
400
401 uu_avl_walk_end(walk);
34dc7c2f
BB
402}
403
404/*
405 * Release any memory associated with a changelist.
406 */
407void
408changelist_free(prop_changelist_t *clp)
409{
410 prop_changenode_t *cn;
34dc7c2f 411
0c6d0936
AP
412 if (clp->cl_tree) {
413 uu_avl_walk_t *walk;
414
415 if ((walk = uu_avl_walk_start(clp->cl_tree,
416 UU_WALK_ROBUST)) == NULL)
417 return;
418
419 while ((cn = uu_avl_walk_next(walk)) != NULL) {
420 uu_avl_remove(clp->cl_tree, cn);
34dc7c2f
BB
421 zfs_close(cn->cn_handle);
422 free(cn);
423 }
424
0c6d0936
AP
425 uu_avl_walk_end(walk);
426 uu_avl_destroy(clp->cl_tree);
34dc7c2f
BB
427 }
428 if (clp->cl_pool)
0c6d0936 429 uu_avl_pool_destroy(clp->cl_pool);
34dc7c2f
BB
430
431 free(clp);
432}
433
0c6d0936
AP
434/*
435 * Add one dataset to changelist
436 */
437static int
438changelist_add_mounted(zfs_handle_t *zhp, void *data)
439{
440 prop_changelist_t *clp = data;
441 prop_changenode_t *cn;
442 uu_avl_index_t idx;
443
444 ASSERT3U(clp->cl_prop, ==, ZFS_PROP_MOUNTPOINT);
445
446 if ((cn = zfs_alloc(zfs_get_handle(zhp),
447 sizeof (prop_changenode_t))) == NULL) {
448 zfs_close(zhp);
449 return (ENOMEM);
450 }
451
452 cn->cn_handle = zhp;
453 cn->cn_mounted = zfs_is_mounted(zhp, NULL);
454 ASSERT3U(cn->cn_mounted, ==, B_TRUE);
455 cn->cn_shared = zfs_is_shared(zhp);
456 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
457 cn->cn_needpost = B_TRUE;
458
459 /* Indicate if any child is exported to a local zone. */
460 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
461 clp->cl_haszonedchild = B_TRUE;
462
463 uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
464
465 if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
466 uu_avl_insert(clp->cl_tree, cn, idx);
467 } else {
468 free(cn);
469 zfs_close(zhp);
470 }
471
472 return (0);
473}
474
34dc7c2f
BB
475static int
476change_one(zfs_handle_t *zhp, void *data)
477{
478 prop_changelist_t *clp = data;
479 char property[ZFS_MAXPROPLEN];
480 char where[64];
e6cebbf8 481 prop_changenode_t *cn = NULL;
45cb520b
G
482 zprop_source_t sourcetype = ZPROP_SRC_NONE;
483 zprop_source_t share_sourcetype = ZPROP_SRC_NONE;
e6cebbf8 484 int ret = 0;
34dc7c2f
BB
485
486 /*
487 * We only want to unmount/unshare those filesystems that may inherit
488 * from the target filesystem. If we find any filesystem with a
489 * locally set mountpoint, we ignore any children since changing the
490 * property will not affect them. If this is a rename, we iterate
491 * over all children regardless, since we need them unmounted in
492 * order to do the rename. Also, if this is a volume and we're doing
493 * a rename, then always add it to the changelist.
494 */
495
496 if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
497 zfs_prop_get(zhp, clp->cl_prop, property,
498 sizeof (property), &sourcetype, where, sizeof (where),
499 B_FALSE) != 0) {
e6cebbf8 500 goto out;
34dc7c2f
BB
501 }
502
503 /*
504 * If we are "watching" sharenfs or sharesmb
505 * then check out the companion property which is tracked
506 * in cl_shareprop
507 */
508 if (clp->cl_shareprop != ZPROP_INVAL &&
509 zfs_prop_get(zhp, clp->cl_shareprop, property,
510 sizeof (property), &share_sourcetype, where, sizeof (where),
511 B_FALSE) != 0) {
e6cebbf8 512 goto out;
34dc7c2f
BB
513 }
514
515 if (clp->cl_alldependents || clp->cl_allchildren ||
516 sourcetype == ZPROP_SRC_DEFAULT ||
517 sourcetype == ZPROP_SRC_INHERITED ||
518 (clp->cl_shareprop != ZPROP_INVAL &&
519 (share_sourcetype == ZPROP_SRC_DEFAULT ||
520 share_sourcetype == ZPROP_SRC_INHERITED))) {
521 if ((cn = zfs_alloc(zfs_get_handle(zhp),
522 sizeof (prop_changenode_t))) == NULL) {
e6cebbf8
PZ
523 ret = -1;
524 goto out;
34dc7c2f
BB
525 }
526
527 cn->cn_handle = zhp;
b128c09f
BB
528 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
529 zfs_is_mounted(zhp, NULL);
34dc7c2f
BB
530 cn->cn_shared = zfs_is_shared(zhp);
531 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
532 cn->cn_needpost = B_TRUE;
533
534 /* Indicate if any child is exported to a local zone. */
535 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
536 clp->cl_haszonedchild = B_TRUE;
537
0c6d0936 538 uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
34dc7c2f 539
0c6d0936 540 uu_avl_index_t idx;
34dc7c2f 541
0c6d0936
AP
542 if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
543 uu_avl_insert(clp->cl_tree, cn, idx);
34dc7c2f 544 } else {
0c6d0936 545 free(cn);
e6cebbf8 546 cn = NULL;
34dc7c2f
BB
547 }
548
549 if (!clp->cl_alldependents)
e6cebbf8
PZ
550 ret = zfs_iter_children(zhp, change_one, data);
551
552 /*
553 * If we added the handle to the changelist, we will re-use it
554 * later so return without closing it.
555 */
556 if (cn != NULL)
557 return (ret);
34dc7c2f
BB
558 }
559
e6cebbf8
PZ
560out:
561 zfs_close(zhp);
562 return (ret);
34dc7c2f
BB
563}
564
34dc7c2f 565static int
50a343d8 566compare_props(const void *a, const void *b, zfs_prop_t prop)
34dc7c2f
BB
567{
568 const prop_changenode_t *ca = a;
569 const prop_changenode_t *cb = b;
570
50a343d8
AP
571 char propa[MAXPATHLEN];
572 char propb[MAXPATHLEN];
573
574 boolean_t haspropa, haspropb;
34dc7c2f 575
50a343d8
AP
576 haspropa = (zfs_prop_get(ca->cn_handle, prop, propa, sizeof (propa),
577 NULL, NULL, 0, B_FALSE) == 0);
578 haspropb = (zfs_prop_get(cb->cn_handle, prop, propb, sizeof (propb),
579 NULL, NULL, 0, B_FALSE) == 0);
34dc7c2f 580
50a343d8
AP
581 if (!haspropa && haspropb)
582 return (-1);
583 else if (haspropa && !haspropb)
584 return (1);
585 else if (!haspropa && !haspropb)
586 return (0);
587 else
588 return (strcmp(propb, propa));
589}
590
591/*ARGSUSED*/
592static int
593compare_mountpoints(const void *a, const void *b, void *unused)
594{
34dc7c2f
BB
595 /*
596 * When unsharing or unmounting filesystems, we need to do it in
597 * mountpoint order. This allows the user to have a mountpoint
598 * hierarchy that is different from the dataset hierarchy, and still
50a343d8 599 * allow it to be changed.
34dc7c2f 600 */
50a343d8
AP
601 return (compare_props(a, b, ZFS_PROP_MOUNTPOINT));
602}
34dc7c2f 603
50a343d8
AP
604/*ARGSUSED*/
605static int
606compare_dataset_names(const void *a, const void *b, void *unused)
607{
608 return (compare_props(a, b, ZFS_PROP_NAME));
34dc7c2f
BB
609}
610
611/*
612 * Given a ZFS handle and a property, construct a complete list of datasets
613 * that need to be modified as part of this process. For anything but the
614 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
615 * Otherwise, we iterate over all children and look for any datasets that
616 * inherit the property. For each such dataset, we add it to the list and
617 * mark whether it was shared beforehand.
618 */
619prop_changelist_t *
b128c09f
BB
620changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
621 int mnt_flags)
34dc7c2f
BB
622{
623 prop_changelist_t *clp;
624 prop_changenode_t *cn;
625 zfs_handle_t *temp;
626 char property[ZFS_MAXPROPLEN];
45d1cae3 627 boolean_t legacy = B_FALSE;
34dc7c2f
BB
628
629 if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
630 return (NULL);
631
632 /*
633 * For mountpoint-related tasks, we want to sort everything by
634 * mountpoint, so that we mount and unmount them in the appropriate
635 * order, regardless of their position in the hierarchy.
636 */
637 if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
638 prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
639 prop == ZFS_PROP_SHARESMB) {
45d1cae3
BB
640
641 if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
642 property, sizeof (property),
643 NULL, NULL, 0, B_FALSE) == 0 &&
644 (strcmp(property, "legacy") == 0 ||
645 strcmp(property, "none") == 0)) {
45d1cae3
BB
646 legacy = B_TRUE;
647 }
34dc7c2f
BB
648 }
649
0c6d0936 650 clp->cl_pool = uu_avl_pool_create("changelist_pool",
34dc7c2f 651 sizeof (prop_changenode_t),
0c6d0936 652 offsetof(prop_changenode_t, cn_treenode),
50a343d8 653 legacy ? compare_dataset_names : compare_mountpoints, 0);
34dc7c2f
BB
654 if (clp->cl_pool == NULL) {
655 assert(uu_error() == UU_ERROR_NO_MEMORY);
656 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
657 changelist_free(clp);
658 return (NULL);
659 }
660
0c6d0936 661 clp->cl_tree = uu_avl_create(clp->cl_pool, NULL, UU_DEFAULT);
b128c09f
BB
662 clp->cl_gflags = gather_flags;
663 clp->cl_mflags = mnt_flags;
34dc7c2f 664
0c6d0936 665 if (clp->cl_tree == NULL) {
34dc7c2f
BB
666 assert(uu_error() == UU_ERROR_NO_MEMORY);
667 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
668 changelist_free(clp);
669 return (NULL);
670 }
671
672 /*
673 * If this is a rename or the 'zoned' property, we pretend we're
674 * changing the mountpoint and flag it so we can catch all children in
675 * change_one().
676 *
677 * Flag cl_alldependents to catch all children plus the dependents
678 * (clones) that are not in the hierarchy.
679 */
680 if (prop == ZFS_PROP_NAME) {
681 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
682 clp->cl_alldependents = B_TRUE;
683 } else if (prop == ZFS_PROP_ZONED) {
684 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
685 clp->cl_allchildren = B_TRUE;
686 } else if (prop == ZFS_PROP_CANMOUNT) {
687 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
688 } else if (prop == ZFS_PROP_VOLSIZE) {
689 clp->cl_prop = ZFS_PROP_MOUNTPOINT;
34dc7c2f
BB
690 } else {
691 clp->cl_prop = prop;
692 }
693 clp->cl_realprop = prop;
694
695 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
696 clp->cl_prop != ZFS_PROP_SHARENFS &&
428870ff 697 clp->cl_prop != ZFS_PROP_SHARESMB)
34dc7c2f
BB
698 return (clp);
699
700 /*
701 * If watching SHARENFS or SHARESMB then
702 * also watch its companion property.
703 */
704 if (clp->cl_prop == ZFS_PROP_SHARENFS)
705 clp->cl_shareprop = ZFS_PROP_SHARESMB;
706 else if (clp->cl_prop == ZFS_PROP_SHARESMB)
707 clp->cl_shareprop = ZFS_PROP_SHARENFS;
708
0c6d0936 709 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT &&
50a343d8 710 (clp->cl_gflags & CL_GATHER_ITER_MOUNTED)) {
0c6d0936
AP
711 /*
712 * Instead of iterating through all of the dataset children we
713 * gather mounted dataset children from MNTTAB
714 */
715 if (zfs_iter_mounted(zhp, changelist_add_mounted, clp) != 0) {
716 changelist_free(clp);
717 return (NULL);
718 }
719 } else if (clp->cl_alldependents) {
34dc7c2f
BB
720 if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
721 changelist_free(clp);
722 return (NULL);
723 }
724 } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
725 changelist_free(clp);
726 return (NULL);
727 }
728
729 /*
730 * We have to re-open ourselves because we auto-close all the handles
731 * and can't tell the difference.
732 */
733 if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
734 ZFS_TYPE_DATASET)) == NULL) {
735 changelist_free(clp);
736 return (NULL);
737 }
738
739 /*
740 * Always add ourself to the list. We add ourselves to the end so that
741 * we're the last to be unmounted.
742 */
743 if ((cn = zfs_alloc(zhp->zfs_hdl,
744 sizeof (prop_changenode_t))) == NULL) {
745 zfs_close(temp);
746 changelist_free(clp);
747 return (NULL);
748 }
749
750 cn->cn_handle = temp;
b128c09f
BB
751 cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
752 zfs_is_mounted(temp, NULL);
34dc7c2f
BB
753 cn->cn_shared = zfs_is_shared(temp);
754 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
755 cn->cn_needpost = B_TRUE;
756
0c6d0936
AP
757 uu_avl_node_init(cn, &cn->cn_treenode, clp->cl_pool);
758 uu_avl_index_t idx;
759 if (uu_avl_find(clp->cl_tree, cn, NULL, &idx) == NULL) {
760 uu_avl_insert(clp->cl_tree, cn, idx);
34dc7c2f 761 } else {
0c6d0936
AP
762 free(cn);
763 zfs_close(temp);
34dc7c2f
BB
764 }
765
766 /*
767 * If the mountpoint property was previously 'legacy', or 'none',
768 * record it as the behavior of changelist_postfix() will be different.
769 */
45d1cae3 770 if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && legacy) {
34dc7c2f
BB
771 /*
772 * do not automatically mount ex-legacy datasets if
773 * we specifically set canmount to noauto
774 */
775 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
776 ZFS_CANMOUNT_NOAUTO)
777 clp->cl_waslegacy = B_TRUE;
778 }
779
780 return (clp);
781}