1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
52082Seschrock * Common Development and Distribution License (the "License").
62082Seschrock * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
213126Sahl
22789Sahrens /*
23*11876SJames.Dunham@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24789Sahrens * Use is subject to license terms.
255542Smmusante *
265542Smmusante * Portions Copyright 2007 Ramprakash Jelari
27789Sahrens */
28789Sahrens
29789Sahrens #include <libintl.h>
30789Sahrens #include <libuutil.h>
31789Sahrens #include <stddef.h>
32789Sahrens #include <stdlib.h>
33789Sahrens #include <string.h>
34789Sahrens #include <unistd.h>
35789Sahrens #include <zone.h>
36789Sahrens
37789Sahrens #include <libzfs.h>
38789Sahrens
39789Sahrens #include "libzfs_impl.h"
40789Sahrens
41789Sahrens /*
42789Sahrens * Structure to keep track of dataset state. Before changing the 'sharenfs' or
43789Sahrens * 'mountpoint' property, we record whether the filesystem was previously
44789Sahrens * mounted/shared. This prior state dictates whether we remount/reshare the
45789Sahrens * dataset after the property has been changed.
46789Sahrens *
47789Sahrens * The interface consists of the following sequence of functions:
48789Sahrens *
49789Sahrens * changelist_gather()
50789Sahrens * changelist_prefix()
51789Sahrens * < change property >
52789Sahrens * changelist_postfix()
53789Sahrens * changelist_free()
54789Sahrens *
55789Sahrens * Other interfaces:
56789Sahrens *
571294Slling * changelist_remove() - remove a node from a gathered list
58789Sahrens * changelist_rename() - renames all datasets appropriately when doing a rename
59789Sahrens * changelist_unshare() - unshares all the nodes in a given changelist
60789Sahrens * changelist_haszonedchild() - check if there is any child exported to
61789Sahrens * a local zone
62789Sahrens */
63789Sahrens typedef struct prop_changenode {
64789Sahrens zfs_handle_t *cn_handle;
65789Sahrens int cn_shared;
66789Sahrens int cn_mounted;
67789Sahrens int cn_zoned;
685542Smmusante boolean_t cn_needpost; /* is postfix() needed? */
69789Sahrens uu_list_node_t cn_listnode;
70789Sahrens } prop_changenode_t;
71789Sahrens
72789Sahrens struct prop_changelist {
73789Sahrens zfs_prop_t cl_prop;
74789Sahrens zfs_prop_t cl_realprop;
755331Samw zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */
76789Sahrens uu_list_pool_t *cl_pool;
77789Sahrens uu_list_t *cl_list;
782082Seschrock boolean_t cl_waslegacy;
792082Seschrock boolean_t cl_allchildren;
802082Seschrock boolean_t cl_alldependents;
817366STim.Haley@Sun.COM int cl_mflags; /* Mount flags */
827366STim.Haley@Sun.COM int cl_gflags; /* Gather request flags */
832082Seschrock boolean_t cl_haszonedchild;
842474Seschrock boolean_t cl_sorted;
85789Sahrens };
86789Sahrens
87789Sahrens /*
88789Sahrens * If the property is 'mountpoint', go through and unmount filesystems as
89789Sahrens * necessary. We don't do the same for 'sharenfs', because we can just re-share
906113Sdougm * with different options without interrupting service. We do handle 'sharesmb'
916113Sdougm * since there may be old resource names that need to be removed.
92789Sahrens */
93789Sahrens int
changelist_prefix(prop_changelist_t * clp)94789Sahrens changelist_prefix(prop_changelist_t *clp)
95789Sahrens {
96789Sahrens prop_changenode_t *cn;
97789Sahrens int ret = 0;
98789Sahrens
996113Sdougm if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
1006113Sdougm clp->cl_prop != ZFS_PROP_SHARESMB)
101789Sahrens return (0);
102789Sahrens
103789Sahrens for (cn = uu_list_first(clp->cl_list); cn != NULL;
104789Sahrens cn = uu_list_next(clp->cl_list, cn)) {
1055610Smmusante
1065610Smmusante /* if a previous loop failed, set the remaining to false */
1075610Smmusante if (ret == -1) {
1085610Smmusante cn->cn_needpost = B_FALSE;
1095610Smmusante continue;
1105610Smmusante }
1115610Smmusante
112789Sahrens /*
1133126Sahl * If we are in the global zone, but this dataset is exported
1143126Sahl * to a local zone, do nothing.
115789Sahrens */
1163126Sahl if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
117789Sahrens continue;
118789Sahrens
119*11876SJames.Dunham@Sun.COM if (!ZFS_IS_VOLUME(cn->cn_handle)) {
1206113Sdougm /*
1216113Sdougm * Do the property specific processing.
1226113Sdougm */
1236113Sdougm switch (clp->cl_prop) {
1246113Sdougm case ZFS_PROP_MOUNTPOINT:
1256113Sdougm if (zfs_unmount(cn->cn_handle, NULL,
1267366STim.Haley@Sun.COM clp->cl_mflags) != 0) {
1276113Sdougm ret = -1;
1286113Sdougm cn->cn_needpost = B_FALSE;
1296113Sdougm }
1306113Sdougm break;
1316113Sdougm case ZFS_PROP_SHARESMB:
1326113Sdougm (void) zfs_unshare_smb(cn->cn_handle, NULL);
1336113Sdougm break;
1346113Sdougm }
1354577Sahrens }
136789Sahrens }
137789Sahrens
1385542Smmusante if (ret == -1)
1395542Smmusante (void) changelist_postfix(clp);
1405542Smmusante
141789Sahrens return (ret);
142789Sahrens }
143789Sahrens
144789Sahrens /*
1453126Sahl * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or
146789Sahrens * reshare the filesystems as necessary. In changelist_gather() we recorded
147789Sahrens * whether the filesystem was previously shared or mounted. The action we take
148789Sahrens * depends on the previous state, and whether the value was previously 'legacy'.
149789Sahrens * For non-legacy properties, we only remount/reshare the filesystem if it was
150789Sahrens * previously mounted/shared. Otherwise, we always remount/reshare the
151789Sahrens * filesystem.
152789Sahrens */
153789Sahrens int
changelist_postfix(prop_changelist_t * clp)154789Sahrens changelist_postfix(prop_changelist_t *clp)
155789Sahrens {
156789Sahrens prop_changenode_t *cn;
1573126Sahl char shareopts[ZFS_MAXPROPLEN];
1585473Srm160521 int errors = 0;
1594180Sdougm libzfs_handle_t *hdl;
160789Sahrens
161789Sahrens /*
162789Sahrens * If we're changing the mountpoint, attempt to destroy the underlying
163789Sahrens * mountpoint. All other datasets will have inherited from this dataset
164789Sahrens * (in which case their mountpoints exist in the filesystem in the new
165789Sahrens * location), or have explicit mountpoints set (in which case they won't
166789Sahrens * be in the changelist).
167789Sahrens */
168789Sahrens if ((cn = uu_list_last(clp->cl_list)) == NULL)
169789Sahrens return (0);
170789Sahrens
171789Sahrens if (clp->cl_prop == ZFS_PROP_MOUNTPOINT)
172789Sahrens remove_mountpoint(cn->cn_handle);
173789Sahrens
174789Sahrens /*
1754180Sdougm * It is possible that the changelist_prefix() used libshare
1764180Sdougm * to unshare some entries. Since libshare caches data, an
1774180Sdougm * attempt to reshare during postfix can fail unless libshare
1784180Sdougm * is uninitialized here so that it will reinitialize later.
1794180Sdougm */
1804180Sdougm if (cn->cn_handle != NULL) {
1814302Sdougm hdl = cn->cn_handle->zfs_hdl;
1824302Sdougm assert(hdl != NULL);
1834302Sdougm zfs_uninit_libshare(hdl);
1844180Sdougm }
1854180Sdougm
1864180Sdougm /*
187789Sahrens * We walk the datasets in reverse, because we want to mount any parent
1885473Srm160521 * datasets before mounting the children. We walk all datasets even if
1895473Srm160521 * there are errors.
190789Sahrens */
191789Sahrens for (cn = uu_list_last(clp->cl_list); cn != NULL;
192789Sahrens cn = uu_list_prev(clp->cl_list, cn)) {
1934840Srm160521
1944840Srm160521 boolean_t sharenfs;
1955331Samw boolean_t sharesmb;
1969404Swilliam.gorrell@sun.com boolean_t mounted;
1974840Srm160521
198789Sahrens /*
1993126Sahl * If we are in the global zone, but this dataset is exported
2003126Sahl * to a local zone, do nothing.
201789Sahrens */
2023126Sahl if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
203789Sahrens continue;
204789Sahrens
2055610Smmusante /* Only do post-processing if it's required */
2065542Smmusante if (!cn->cn_needpost)
2075542Smmusante continue;
2085542Smmusante cn->cn_needpost = B_FALSE;
2095542Smmusante
210789Sahrens zfs_refresh_properties(cn->cn_handle);
211789Sahrens
212*11876SJames.Dunham@Sun.COM if (ZFS_IS_VOLUME(cn->cn_handle))
213789Sahrens continue;
214789Sahrens
2154840Srm160521 /*
2164840Srm160521 * Remount if previously mounted or mountpoint was legacy,
2175331Samw * or sharenfs or sharesmb property is set.
2184840Srm160521 */
2194840Srm160521 sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS,
2204840Srm160521 shareopts, sizeof (shareopts), NULL, NULL, 0,
2214840Srm160521 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
2224840Srm160521
2235331Samw sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB,
2245331Samw shareopts, sizeof (shareopts), NULL, NULL, 0,
2255331Samw B_FALSE) == 0) && (strcmp(shareopts, "off") != 0));
2265331Samw
2279404Swilliam.gorrell@sun.com mounted = zfs_is_mounted(cn->cn_handle, NULL);
2289404Swilliam.gorrell@sun.com
2299404Swilliam.gorrell@sun.com if (!mounted && (cn->cn_mounted ||
2309404Swilliam.gorrell@sun.com ((sharenfs || sharesmb || clp->cl_waslegacy) &&
2319404Swilliam.gorrell@sun.com (zfs_prop_get_int(cn->cn_handle,
2329404Swilliam.gorrell@sun.com ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON)))) {
2339404Swilliam.gorrell@sun.com
2349404Swilliam.gorrell@sun.com if (zfs_mount(cn->cn_handle, NULL, 0) != 0)
2359404Swilliam.gorrell@sun.com errors++;
2369404Swilliam.gorrell@sun.com else
2379404Swilliam.gorrell@sun.com mounted = TRUE;
2389404Swilliam.gorrell@sun.com }
239789Sahrens
240789Sahrens /*
2419404Swilliam.gorrell@sun.com * If the file system is mounted we always re-share even
2429404Swilliam.gorrell@sun.com * if the filesystem is currently shared, so that we can
2439404Swilliam.gorrell@sun.com * adopt any new options.
244789Sahrens */
2459404Swilliam.gorrell@sun.com if (sharenfs && mounted)
2465473Srm160521 errors += zfs_share_nfs(cn->cn_handle);
2475473Srm160521 else if (cn->cn_shared || clp->cl_waslegacy)
2485473Srm160521 errors += zfs_unshare_nfs(cn->cn_handle, NULL);
2499404Swilliam.gorrell@sun.com if (sharesmb && mounted)
2505473Srm160521 errors += zfs_share_smb(cn->cn_handle);
2515473Srm160521 else if (cn->cn_shared || clp->cl_waslegacy)
2525473Srm160521 errors += zfs_unshare_smb(cn->cn_handle, NULL);
253789Sahrens }
254789Sahrens
2555473Srm160521 return (errors ? -1 : 0);
256789Sahrens }
257789Sahrens
258789Sahrens /*
2591294Slling * Is this "dataset" a child of "parent"?
2601294Slling */
2616027Srm160521 boolean_t
isa_child_of(const char * dataset,const char * parent)2623841Smmusante isa_child_of(const char *dataset, const char *parent)
2631294Slling {
2641294Slling int len;
2651294Slling
2661294Slling len = strlen(parent);
2671294Slling
2681294Slling if (strncmp(dataset, parent, len) == 0 &&
2693841Smmusante (dataset[len] == '@' || dataset[len] == '/' ||
2703841Smmusante dataset[len] == '\0'))
2712082Seschrock return (B_TRUE);
2721294Slling else
2732082Seschrock return (B_FALSE);
2741294Slling
2751294Slling }
2761294Slling
2771294Slling /*
2783126Sahl * If we rename a filesystem, child filesystem handles are no longer valid
2793126Sahl * since we identify each dataset by its name in the ZFS namespace. As a
2803126Sahl * result, we have to go through and fix up all the names appropriately. We
2813126Sahl * could do this automatically if libzfs kept track of all open handles, but
2823126Sahl * this is a lot less work.
283789Sahrens */
284789Sahrens void
changelist_rename(prop_changelist_t * clp,const char * src,const char * dst)285789Sahrens changelist_rename(prop_changelist_t *clp, const char *src, const char *dst)
286789Sahrens {
287789Sahrens prop_changenode_t *cn;
288789Sahrens char newname[ZFS_MAXNAMELEN];
289789Sahrens
290789Sahrens for (cn = uu_list_first(clp->cl_list); cn != NULL;
291789Sahrens cn = uu_list_next(clp->cl_list, cn)) {
292789Sahrens /*
2931294Slling * Do not rename a clone that's not in the source hierarchy.
2941294Slling */
2951294Slling if (!isa_child_of(cn->cn_handle->zfs_name, src))
2961294Slling continue;
2971294Slling
2981294Slling /*
299789Sahrens * Destroy the previous mountpoint if needed.
300789Sahrens */
301789Sahrens remove_mountpoint(cn->cn_handle);
302789Sahrens
303789Sahrens (void) strlcpy(newname, dst, sizeof (newname));
304789Sahrens (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src));
305789Sahrens
306789Sahrens (void) strlcpy(cn->cn_handle->zfs_name, newname,
307789Sahrens sizeof (cn->cn_handle->zfs_name));
308789Sahrens }
309789Sahrens }
310789Sahrens
311789Sahrens /*
3125331Samw * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property,
3135331Samw * unshare all the datasets in the list.
314789Sahrens */
315789Sahrens int
changelist_unshare(prop_changelist_t * clp,zfs_share_proto_t * proto)3165331Samw changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto)
317789Sahrens {
318789Sahrens prop_changenode_t *cn;
319789Sahrens int ret = 0;
320789Sahrens
3215331Samw if (clp->cl_prop != ZFS_PROP_SHARENFS &&
3225331Samw clp->cl_prop != ZFS_PROP_SHARESMB)
323789Sahrens return (0);
324789Sahrens
325789Sahrens for (cn = uu_list_first(clp->cl_list); cn != NULL;
326789Sahrens cn = uu_list_next(clp->cl_list, cn)) {
3275331Samw if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0)
328789Sahrens ret = -1;
329789Sahrens }
330789Sahrens
331789Sahrens return (ret);
332789Sahrens }
333789Sahrens
334789Sahrens /*
3353126Sahl * Check if there is any child exported to a local zone in a given changelist.
3363126Sahl * This information has already been recorded while gathering the changelist
3373126Sahl * via changelist_gather().
338789Sahrens */
339789Sahrens int
changelist_haszonedchild(prop_changelist_t * clp)340789Sahrens changelist_haszonedchild(prop_changelist_t *clp)
341789Sahrens {
342789Sahrens return (clp->cl_haszonedchild);
343789Sahrens }
344789Sahrens
345789Sahrens /*
3461294Slling * Remove a node from a gathered list.
3471294Slling */
3481294Slling void
changelist_remove(prop_changelist_t * clp,const char * name)3495367Sahrens changelist_remove(prop_changelist_t *clp, const char *name)
3501294Slling {
3511294Slling prop_changenode_t *cn;
3521294Slling
3531294Slling for (cn = uu_list_first(clp->cl_list); cn != NULL;
3541294Slling cn = uu_list_next(clp->cl_list, cn)) {
3551294Slling
3565367Sahrens if (strcmp(cn->cn_handle->zfs_name, name) == 0) {
3571294Slling uu_list_remove(clp->cl_list, cn);
3581294Slling zfs_close(cn->cn_handle);
3591294Slling free(cn);
3601294Slling return;
3611294Slling }
3621294Slling }
3631294Slling }
3641294Slling
3651294Slling /*
366789Sahrens * Release any memory associated with a changelist.
367789Sahrens */
368789Sahrens void
changelist_free(prop_changelist_t * clp)369789Sahrens changelist_free(prop_changelist_t *clp)
370789Sahrens {
371789Sahrens prop_changenode_t *cn;
3724074Seschrock void *cookie;
373789Sahrens
3742142Seschrock if (clp->cl_list) {
3754074Seschrock cookie = NULL;
3764074Seschrock while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) {
3772142Seschrock zfs_close(cn->cn_handle);
3782142Seschrock free(cn);
3792142Seschrock }
380789Sahrens
3812142Seschrock uu_list_destroy(clp->cl_list);
3822142Seschrock }
3832142Seschrock if (clp->cl_pool)
3842142Seschrock uu_list_pool_destroy(clp->cl_pool);
385789Sahrens
386789Sahrens free(clp);
387789Sahrens }
388789Sahrens
389789Sahrens static int
change_one(zfs_handle_t * zhp,void * data)390789Sahrens change_one(zfs_handle_t *zhp, void *data)
391789Sahrens {
392789Sahrens prop_changelist_t *clp = data;
393789Sahrens char property[ZFS_MAXPROPLEN];
394789Sahrens char where[64];
395789Sahrens prop_changenode_t *cn;
3965094Slling zprop_source_t sourcetype;
3975331Samw zprop_source_t share_sourcetype;
398789Sahrens
399789Sahrens /*
4003126Sahl * We only want to unmount/unshare those filesystems that may inherit
4013126Sahl * from the target filesystem. If we find any filesystem with a
4023126Sahl * locally set mountpoint, we ignore any children since changing the
4033126Sahl * property will not affect them. If this is a rename, we iterate
4043126Sahl * over all children regardless, since we need them unmounted in
4053126Sahl * order to do the rename. Also, if this is a volume and we're doing
4063126Sahl * a rename, then always add it to the changelist.
407789Sahrens */
408789Sahrens
4092676Seschrock if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) &&
410789Sahrens zfs_prop_get(zhp, clp->cl_prop, property,
411789Sahrens sizeof (property), &sourcetype, where, sizeof (where),
4122082Seschrock B_FALSE) != 0) {
4132082Seschrock zfs_close(zhp);
414789Sahrens return (0);
4152082Seschrock }
416789Sahrens
4175331Samw /*
4185331Samw * If we are "watching" sharenfs or sharesmb
4195331Samw * then check out the companion property which is tracked
4205331Samw * in cl_shareprop
4215331Samw */
4225331Samw if (clp->cl_shareprop != ZPROP_INVAL &&
4235331Samw zfs_prop_get(zhp, clp->cl_shareprop, property,
4245331Samw sizeof (property), &share_sourcetype, where, sizeof (where),
4255331Samw B_FALSE) != 0) {
4265331Samw zfs_close(zhp);
4275331Samw return (0);
4285331Samw }
4295331Samw
4301294Slling if (clp->cl_alldependents || clp->cl_allchildren ||
4315094Slling sourcetype == ZPROP_SRC_DEFAULT ||
4325331Samw sourcetype == ZPROP_SRC_INHERITED ||
4335331Samw (clp->cl_shareprop != ZPROP_INVAL &&
4345331Samw (share_sourcetype == ZPROP_SRC_DEFAULT ||
4355331Samw share_sourcetype == ZPROP_SRC_INHERITED))) {
4362082Seschrock if ((cn = zfs_alloc(zfs_get_handle(zhp),
4372082Seschrock sizeof (prop_changenode_t))) == NULL) {
4382082Seschrock zfs_close(zhp);
4392082Seschrock return (-1);
4402082Seschrock }
441789Sahrens
442789Sahrens cn->cn_handle = zhp;
4437366STim.Haley@Sun.COM cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
4447366STim.Haley@Sun.COM zfs_is_mounted(zhp, NULL);
4453126Sahl cn->cn_shared = zfs_is_shared(zhp);
446789Sahrens cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4475610Smmusante cn->cn_needpost = B_TRUE;
448789Sahrens
4493126Sahl /* Indicate if any child is exported to a local zone. */
4503126Sahl if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned)
4512082Seschrock clp->cl_haszonedchild = B_TRUE;
452789Sahrens
453789Sahrens uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
454789Sahrens
4552474Seschrock if (clp->cl_sorted) {
4562474Seschrock uu_list_index_t idx;
4572474Seschrock
4582474Seschrock (void) uu_list_find(clp->cl_list, cn, NULL,
4592474Seschrock &idx);
4602474Seschrock uu_list_insert(clp->cl_list, cn, idx);
4611294Slling } else {
46210196Swilliam.gorrell@sun.com /*
46310196Swilliam.gorrell@sun.com * Add this child to beginning of the list. Children
46410196Swilliam.gorrell@sun.com * below this one in the hierarchy will get added above
46510196Swilliam.gorrell@sun.com * this one in the list. This produces a list in
46610196Swilliam.gorrell@sun.com * reverse dataset name order.
46710196Swilliam.gorrell@sun.com * This is necessary when the original mountpoint
46810196Swilliam.gorrell@sun.com * is legacy or none.
46910196Swilliam.gorrell@sun.com */
4702474Seschrock ASSERT(!clp->cl_alldependents);
4711294Slling verify(uu_list_insert_before(clp->cl_list,
4724074Seschrock uu_list_first(clp->cl_list), cn) == 0);
4732474Seschrock }
4742474Seschrock
4752474Seschrock if (!clp->cl_alldependents)
4761294Slling return (zfs_iter_children(zhp, change_one, data));
477789Sahrens } else {
478789Sahrens zfs_close(zhp);
479789Sahrens }
480789Sahrens
481789Sahrens return (0);
482789Sahrens }
483789Sahrens
4842474Seschrock /*ARGSUSED*/
4852474Seschrock static int
compare_mountpoints(const void * a,const void * b,void * unused)4862474Seschrock compare_mountpoints(const void *a, const void *b, void *unused)
4872474Seschrock {
4882474Seschrock const prop_changenode_t *ca = a;
4892474Seschrock const prop_changenode_t *cb = b;
4902474Seschrock
4912474Seschrock char mounta[MAXPATHLEN];
4922474Seschrock char mountb[MAXPATHLEN];
4932474Seschrock
4942474Seschrock boolean_t hasmounta, hasmountb;
4952474Seschrock
4962474Seschrock /*
4972474Seschrock * When unsharing or unmounting filesystems, we need to do it in
4982474Seschrock * mountpoint order. This allows the user to have a mountpoint
4992474Seschrock * hierarchy that is different from the dataset hierarchy, and still
5002474Seschrock * allow it to be changed. However, if either dataset doesn't have a
5012474Seschrock * mountpoint (because it is a volume or a snapshot), we place it at the
5022474Seschrock * end of the list, because it doesn't affect our change at all.
5032474Seschrock */
5042474Seschrock hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta,
5052474Seschrock sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
5062474Seschrock hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb,
5072474Seschrock sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
5082474Seschrock
5092474Seschrock if (!hasmounta && hasmountb)
5102474Seschrock return (-1);
5112474Seschrock else if (hasmounta && !hasmountb)
5122474Seschrock return (1);
5132474Seschrock else if (!hasmounta && !hasmountb)
5142474Seschrock return (0);
5152474Seschrock else
5162474Seschrock return (strcmp(mountb, mounta));
5172474Seschrock }
518789Sahrens
519789Sahrens /*
5203126Sahl * Given a ZFS handle and a property, construct a complete list of datasets
5213126Sahl * that need to be modified as part of this process. For anything but the
522789Sahrens * 'mountpoint' and 'sharenfs' properties, this just returns an empty list.
5233126Sahl * Otherwise, we iterate over all children and look for any datasets that
5243126Sahl * inherit the property. For each such dataset, we add it to the list and
5253126Sahl * mark whether it was shared beforehand.
526789Sahrens */
527789Sahrens prop_changelist_t *
changelist_gather(zfs_handle_t * zhp,zfs_prop_t prop,int gather_flags,int mnt_flags)5287366STim.Haley@Sun.COM changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int gather_flags,
5297366STim.Haley@Sun.COM int mnt_flags)
530789Sahrens {
5312082Seschrock prop_changelist_t *clp;
532789Sahrens prop_changenode_t *cn;
533789Sahrens zfs_handle_t *temp;
534789Sahrens char property[ZFS_MAXPROPLEN];
5352474Seschrock uu_compare_fn_t *compare = NULL;
53610196Swilliam.gorrell@sun.com boolean_t legacy = B_FALSE;
537789Sahrens
5382082Seschrock if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL)
5392082Seschrock return (NULL);
5402082Seschrock
5412474Seschrock /*
5422474Seschrock * For mountpoint-related tasks, we want to sort everything by
5432474Seschrock * mountpoint, so that we mount and unmount them in the appropriate
5442474Seschrock * order, regardless of their position in the hierarchy.
5452474Seschrock */
5462474Seschrock if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED ||
5475331Samw prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS ||
5485331Samw prop == ZFS_PROP_SHARESMB) {
54910196Swilliam.gorrell@sun.com
55010196Swilliam.gorrell@sun.com if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
55110196Swilliam.gorrell@sun.com property, sizeof (property),
55210196Swilliam.gorrell@sun.com NULL, NULL, 0, B_FALSE) == 0 &&
55310196Swilliam.gorrell@sun.com (strcmp(property, "legacy") == 0 ||
55410196Swilliam.gorrell@sun.com strcmp(property, "none") == 0)) {
55510196Swilliam.gorrell@sun.com
55610196Swilliam.gorrell@sun.com legacy = B_TRUE;
55710196Swilliam.gorrell@sun.com }
55810196Swilliam.gorrell@sun.com if (!legacy) {
55910196Swilliam.gorrell@sun.com compare = compare_mountpoints;
56010196Swilliam.gorrell@sun.com clp->cl_sorted = B_TRUE;
56110196Swilliam.gorrell@sun.com }
5622474Seschrock }
5632474Seschrock
564789Sahrens clp->cl_pool = uu_list_pool_create("changelist_pool",
565789Sahrens sizeof (prop_changenode_t),
566789Sahrens offsetof(prop_changenode_t, cn_listnode),
5672474Seschrock compare, 0);
5682142Seschrock if (clp->cl_pool == NULL) {
5692142Seschrock assert(uu_error() == UU_ERROR_NO_MEMORY);
5702142Seschrock (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
5712142Seschrock changelist_free(clp);
5722142Seschrock return (NULL);
5732142Seschrock }
574789Sahrens
5752474Seschrock clp->cl_list = uu_list_create(clp->cl_pool, NULL,
5762474Seschrock clp->cl_sorted ? UU_LIST_SORTED : 0);
5777366STim.Haley@Sun.COM clp->cl_gflags = gather_flags;
5787366STim.Haley@Sun.COM clp->cl_mflags = mnt_flags;
579789Sahrens
5802142Seschrock if (clp->cl_list == NULL) {
5812142Seschrock assert(uu_error() == UU_ERROR_NO_MEMORY);
5822142Seschrock (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error");
5832142Seschrock changelist_free(clp);
5842142Seschrock return (NULL);
5852142Seschrock }
5862142Seschrock
587789Sahrens /*
588789Sahrens * If this is a rename or the 'zoned' property, we pretend we're
589789Sahrens * changing the mountpoint and flag it so we can catch all children in
590789Sahrens * change_one().
5911294Slling *
5923126Sahl * Flag cl_alldependents to catch all children plus the dependents
5933126Sahl * (clones) that are not in the hierarchy.
594789Sahrens */
5951294Slling if (prop == ZFS_PROP_NAME) {
5961294Slling clp->cl_prop = ZFS_PROP_MOUNTPOINT;
5972082Seschrock clp->cl_alldependents = B_TRUE;
5981294Slling } else if (prop == ZFS_PROP_ZONED) {
599789Sahrens clp->cl_prop = ZFS_PROP_MOUNTPOINT;
6002082Seschrock clp->cl_allchildren = B_TRUE;
6012676Seschrock } else if (prop == ZFS_PROP_CANMOUNT) {
6022676Seschrock clp->cl_prop = ZFS_PROP_MOUNTPOINT;
6033126Sahl } else if (prop == ZFS_PROP_VOLSIZE) {
6043126Sahl clp->cl_prop = ZFS_PROP_MOUNTPOINT;
605789Sahrens } else {
606789Sahrens clp->cl_prop = prop;
607789Sahrens }
608789Sahrens clp->cl_realprop = prop;
609789Sahrens
610789Sahrens if (clp->cl_prop != ZFS_PROP_MOUNTPOINT &&
6113126Sahl clp->cl_prop != ZFS_PROP_SHARENFS &&
612*11876SJames.Dunham@Sun.COM clp->cl_prop != ZFS_PROP_SHARESMB)
613789Sahrens return (clp);
614789Sahrens
6155331Samw /*
6165331Samw * If watching SHARENFS or SHARESMB then
6175331Samw * also watch its companion property.
6185331Samw */
6195331Samw if (clp->cl_prop == ZFS_PROP_SHARENFS)
6205331Samw clp->cl_shareprop = ZFS_PROP_SHARESMB;
6215331Samw else if (clp->cl_prop == ZFS_PROP_SHARESMB)
6225331Samw clp->cl_shareprop = ZFS_PROP_SHARENFS;
6235331Samw
6241294Slling if (clp->cl_alldependents) {
6252474Seschrock if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) {
6261294Slling changelist_free(clp);
6271294Slling return (NULL);
6281294Slling }
6291294Slling } else if (zfs_iter_children(zhp, change_one, clp) != 0) {
630789Sahrens changelist_free(clp);
631789Sahrens return (NULL);
632789Sahrens }
633789Sahrens
634789Sahrens /*
635789Sahrens * We have to re-open ourselves because we auto-close all the handles
636789Sahrens * and can't tell the difference.
637789Sahrens */
6382082Seschrock if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp),
6395094Slling ZFS_TYPE_DATASET)) == NULL) {
6402082Seschrock changelist_free(clp);
641789Sahrens return (NULL);
642789Sahrens }
643789Sahrens
644789Sahrens /*
645789Sahrens * Always add ourself to the list. We add ourselves to the end so that
646789Sahrens * we're the last to be unmounted.
647789Sahrens */
6482082Seschrock if ((cn = zfs_alloc(zhp->zfs_hdl,
6492082Seschrock sizeof (prop_changenode_t))) == NULL) {
6502082Seschrock zfs_close(temp);
6512082Seschrock changelist_free(clp);
6522082Seschrock return (NULL);
6532082Seschrock }
6542082Seschrock
655789Sahrens cn->cn_handle = temp;
6567366STim.Haley@Sun.COM cn->cn_mounted = (clp->cl_gflags & CL_GATHER_MOUNT_ALWAYS) ||
6577366STim.Haley@Sun.COM zfs_is_mounted(temp, NULL);
6583126Sahl cn->cn_shared = zfs_is_shared(temp);
659789Sahrens cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6605610Smmusante cn->cn_needpost = B_TRUE;
661789Sahrens
662789Sahrens uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool);
6632474Seschrock if (clp->cl_sorted) {
6642474Seschrock uu_list_index_t idx;
6652474Seschrock (void) uu_list_find(clp->cl_list, cn, NULL, &idx);
6662474Seschrock uu_list_insert(clp->cl_list, cn, idx);
6672474Seschrock } else {
66810196Swilliam.gorrell@sun.com /*
66910196Swilliam.gorrell@sun.com * Add the target dataset to the end of the list.
67010196Swilliam.gorrell@sun.com * The list is not really unsorted. The list will be
67110196Swilliam.gorrell@sun.com * in reverse dataset name order. This is necessary
67210196Swilliam.gorrell@sun.com * when the original mountpoint is legacy or none.
67310196Swilliam.gorrell@sun.com */
6742474Seschrock verify(uu_list_insert_after(clp->cl_list,
6752474Seschrock uu_list_last(clp->cl_list), cn) == 0);
6762474Seschrock }
677789Sahrens
678789Sahrens /*
6794840Srm160521 * If the mountpoint property was previously 'legacy', or 'none',
6804840Srm160521 * record it as the behavior of changelist_postfix() will be different.
681789Sahrens */
68210196Swilliam.gorrell@sun.com if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && legacy) {
6836168Shs24103 /*
6846168Shs24103 * do not automatically mount ex-legacy datasets if
6856168Shs24103 * we specifically set canmount to noauto
6866168Shs24103 */
6876168Shs24103 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) !=
6886168Shs24103 ZFS_CANMOUNT_NOAUTO)
6896168Shs24103 clp->cl_waslegacy = B_TRUE;
6906168Shs24103 }
691789Sahrens
692789Sahrens return (clp);
693789Sahrens }
694