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