xref: /illumos-gate/usr/src/cmd/zoneadm/zfs.c (revision 9adfa60d)
10b5de56dSgjelinek /*
20b5de56dSgjelinek  * CDDL HEADER START
30b5de56dSgjelinek  *
40b5de56dSgjelinek  * The contents of this file are subject to the terms of the
50b5de56dSgjelinek  * Common Development and Distribution License (the "License").
60b5de56dSgjelinek  * You may not use this file except in compliance with the License.
70b5de56dSgjelinek  *
80b5de56dSgjelinek  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90b5de56dSgjelinek  * or http://www.opensolaris.org/os/licensing.
100b5de56dSgjelinek  * See the License for the specific language governing permissions
110b5de56dSgjelinek  * and limitations under the License.
120b5de56dSgjelinek  *
130b5de56dSgjelinek  * When distributing Covered Code, include this CDDL HEADER in each
140b5de56dSgjelinek  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150b5de56dSgjelinek  * If applicable, add the following below this CDDL HEADER, with the
160b5de56dSgjelinek  * fields enclosed by brackets "[]" replaced with your own identifying
170b5de56dSgjelinek  * information: Portions Copyright [yyyy] [name of copyright owner]
180b5de56dSgjelinek  *
190b5de56dSgjelinek  * CDDL HEADER END
200b5de56dSgjelinek  */
220b5de56dSgjelinek /*
232b6c28b8Sbatschul  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24*9adfa60dSMatthew Ahrens  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25dd9c3b29SJerry Jelinek  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
260d8fa8f8SMartin Matuska  * Copyright (c) 2016 Martin Matuska. All rights reserved.
270b5de56dSgjelinek  */
290b5de56dSgjelinek /*
300b5de56dSgjelinek  * This file contains the functions used to support the ZFS integration
310b5de56dSgjelinek  * with zones.  This includes validation (e.g. zonecfg dataset), cloning,
320b5de56dSgjelinek  * file system creation and destruction.
330b5de56dSgjelinek  */
350b5de56dSgjelinek #include <stdio.h>
360b5de56dSgjelinek #include <errno.h>
370b5de56dSgjelinek #include <unistd.h>
380b5de56dSgjelinek #include <string.h>
390b5de56dSgjelinek #include <locale.h>
400b5de56dSgjelinek #include <libintl.h>
410b5de56dSgjelinek #include <sys/stat.h>
420b5de56dSgjelinek #include <sys/statvfs.h>
430b5de56dSgjelinek #include <libgen.h>
440b5de56dSgjelinek #include <libzonecfg.h>
450b5de56dSgjelinek #include <sys/mnttab.h>
460b5de56dSgjelinek #include <libzfs.h>
4711506c41Sgjelinek #include <sys/mntent.h>
48286822ddS #include <values.h>
490094b373Sjv #include <strings.h>
500094b373Sjv #include <assert.h>
520b5de56dSgjelinek #include "zoneadm.h"
5499653d4eSeschrock libzfs_handle_t *g_zfs;
560b5de56dSgjelinek typedef struct zfs_mount_data {
570b5de56dSgjelinek 	char		*match_name;
580b5de56dSgjelinek 	zfs_handle_t	*match_handle;
590b5de56dSgjelinek } zfs_mount_data_t;
610b5de56dSgjelinek typedef struct zfs_snapshot_data {
62286822ddS 	char	*match_name;	/* zonename@SUNWzone */
63286822ddS 	int	len;		/* strlen of match_name */
64286822ddS 	int	max;		/* highest digit appended to snap name */
65286822ddS 	int	num;		/* number of snapshots to rename */
66286822ddS 	int	cntr;		/* counter for renaming snapshots */
670b5de56dSgjelinek } zfs_snapshot_data_t;
69286822ddS typedef struct clone_data {
70286822ddS 	zfs_handle_t	*clone_zhp;	/* clone dataset to promote */
71286822ddS 	time_t		origin_creation; /* snapshot creation time of clone */
72286822ddS 	const char	*snapshot;	/* snapshot of dataset being demoted */
73286822ddS } clone_data_t;
750b5de56dSgjelinek /*
760b5de56dSgjelinek  * A ZFS file system iterator call-back function which returns the
770b5de56dSgjelinek  * zfs_handle_t for a ZFS file system on the specified mount point.
780b5de56dSgjelinek  */
790b5de56dSgjelinek static int
match_mountpoint(zfs_handle_t * zhp,void * data)800b5de56dSgjelinek match_mountpoint(zfs_handle_t *zhp, void *data)
810b5de56dSgjelinek {
820b5de56dSgjelinek 	int			res;
830b5de56dSgjelinek 	zfs_mount_data_t	*cbp;
840b5de56dSgjelinek 	char			mp[ZFS_MAXPROPLEN];
860b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
870b5de56dSgjelinek 		zfs_close(zhp);
880b5de56dSgjelinek 		return (0);
890b5de56dSgjelinek 	}
9111506c41Sgjelinek 	/* First check if the dataset is mounted. */
9211506c41Sgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTED, mp, sizeof (mp), NULL, NULL,
9311506c41Sgjelinek 	    0, B_FALSE) != 0 || strcmp(mp, "no") == 0) {
9411506c41Sgjelinek 		zfs_close(zhp);
9511506c41Sgjelinek 		return (0);
9611506c41Sgjelinek 	}
9811506c41Sgjelinek 	/* Now check mount point. */
990b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
10011506c41Sgjelinek 	    0, B_FALSE) != 0) {
10111506c41Sgjelinek 		zfs_close(zhp);
10211506c41Sgjelinek 		return (0);
10311506c41Sgjelinek 	}
10511506c41Sgjelinek 	cbp = (zfs_mount_data_t *)data;
10711506c41Sgjelinek 	if (strcmp(mp, "legacy") == 0) {
10811506c41Sgjelinek 		/* If legacy, must look in mnttab for mountpoint. */
10911506c41Sgjelinek 		FILE		*fp;
11011506c41Sgjelinek 		struct mnttab	entry;
11111506c41Sgjelinek 		const char	*nm;
11311506c41Sgjelinek 		nm = zfs_get_name(zhp);
11411506c41Sgjelinek 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
11511506c41Sgjelinek 			zfs_close(zhp);
11611506c41Sgjelinek 			return (0);
11711506c41Sgjelinek 		}
11911506c41Sgjelinek 		while (getmntent(fp, &entry) == 0) {
12011506c41Sgjelinek 			if (strcmp(nm, entry.mnt_special) == 0) {
12111506c41Sgjelinek 				if (strcmp(entry.mnt_mountp, cbp->match_name)
12211506c41Sgjelinek 				    == 0) {
12311506c41Sgjelinek 					(void) fclose(fp);
12411506c41Sgjelinek 					cbp->match_handle = zhp;
12511506c41Sgjelinek 					return (1);
12611506c41Sgjelinek 				}
12711506c41Sgjelinek 				break;
12811506c41Sgjelinek 			}
12911506c41Sgjelinek 		}
13011506c41Sgjelinek 		(void) fclose(fp);
13211506c41Sgjelinek 	} else if (strcmp(mp, cbp->match_name) == 0) {
1330b5de56dSgjelinek 		cbp->match_handle = zhp;
1340b5de56dSgjelinek 		return (1);
1350b5de56dSgjelinek 	}
13711506c41Sgjelinek 	/* Iterate over any nested datasets. */
1380b5de56dSgjelinek 	res = zfs_iter_filesystems(zhp, match_mountpoint, data);
1390b5de56dSgjelinek 	zfs_close(zhp);
1400b5de56dSgjelinek 	return (res);
1410b5de56dSgjelinek }
1430b5de56dSgjelinek /*
1440b5de56dSgjelinek  * Get ZFS handle for the specified mount point.
1450b5de56dSgjelinek  */
1460b5de56dSgjelinek static zfs_handle_t *
mount2zhandle(char * mountpoint)1470b5de56dSgjelinek mount2zhandle(char *mountpoint)
1480b5de56dSgjelinek {
1490b5de56dSgjelinek 	zfs_mount_data_t	cb;
1510b5de56dSgjelinek 	cb.match_name = mountpoint;
1520b5de56dSgjelinek 	cb.match_handle = NULL;
15399653d4eSeschrock 	(void) zfs_iter_root(g_zfs, match_mountpoint, &cb);
1540b5de56dSgjelinek 	return (cb.match_handle);
1550b5de56dSgjelinek }
1570b5de56dSgjelinek /*
1580b5de56dSgjelinek  * Check if there is already a file system (zfs or any other type) mounted on
1590b5de56dSgjelinek  * path.
1600b5de56dSgjelinek  */
1610b5de56dSgjelinek static boolean_t
is_mountpnt(char * path)1620b5de56dSgjelinek is_mountpnt(char *path)
1630b5de56dSgjelinek {
1640b5de56dSgjelinek 	FILE		*fp;
1650b5de56dSgjelinek 	struct mnttab	entry;
16711506c41Sgjelinek 	if ((fp = fopen(MNTTAB, "r")) == NULL)
1680b5de56dSgjelinek 		return (B_FALSE);
1700b5de56dSgjelinek 	while (getmntent(fp, &entry) == 0) {
1710b5de56dSgjelinek 		if (strcmp(path, entry.mnt_mountp) == 0) {
1720b5de56dSgjelinek 			(void) fclose(fp);
1730b5de56dSgjelinek 			return (B_TRUE);
1740b5de56dSgjelinek 		}
1750b5de56dSgjelinek 	}
1770b5de56dSgjelinek 	(void) fclose(fp);
1780b5de56dSgjelinek 	return (B_FALSE);
1790b5de56dSgjelinek }
1810b5de56dSgjelinek /*
182ff17c8bfSgjelinek  * Run the brand's pre-snapshot hook before we take a ZFS snapshot of the zone.
1830b5de56dSgjelinek  */
1840b5de56dSgjelinek static int
pre_snapshot(char * presnapbuf)185ff17c8bfSgjelinek pre_snapshot(char *presnapbuf)
1860b5de56dSgjelinek {
187ff17c8bfSgjelinek 	int status;
189ff17c8bfSgjelinek 	/* No brand-specific handler */
190ff17c8bfSgjelinek 	if (presnapbuf[0] == '\0')
191ff17c8bfSgjelinek 		return (Z_OK);
193ff17c8bfSgjelinek 	/* Run the hook */
194c75cc341S 	status = do_subproc(presnapbuf);
195ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific presnapshot"),
196ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
1970b5de56dSgjelinek 		return (Z_ERR);
1990b5de56dSgjelinek 	return (Z_OK);
2000b5de56dSgjelinek }
2020b5de56dSgjelinek /*
203ff17c8bfSgjelinek  * Run the brand's post-snapshot hook after we take a ZFS snapshot of the zone.
2040b5de56dSgjelinek  */
2050b5de56dSgjelinek static int
post_snapshot(char * postsnapbuf)206ff17c8bfSgjelinek post_snapshot(char *postsnapbuf)
2070b5de56dSgjelinek {
208ff17c8bfSgjelinek 	int status;
210ff17c8bfSgjelinek 	/* No brand-specific handler */
211ff17c8bfSgjelinek 	if (postsnapbuf[0] == '\0')
212ff17c8bfSgjelinek 		return (Z_OK);
214ff17c8bfSgjelinek 	/* Run the hook */
215c75cc341S 	status = do_subproc(postsnapbuf);
216ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific postsnapshot"),
217ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
2180b5de56dSgjelinek 		return (Z_ERR);
2200b5de56dSgjelinek 	return (Z_OK);
2210b5de56dSgjelinek }
2230b5de56dSgjelinek /*
2240b5de56dSgjelinek  * This is a ZFS snapshot iterator call-back function which returns the
2250b5de56dSgjelinek  * highest number of SUNWzone snapshots that have been taken.
2260b5de56dSgjelinek  */
2270b5de56dSgjelinek static int
get_snap_max(zfs_handle_t * zhp,void * data)2280b5de56dSgjelinek get_snap_max(zfs_handle_t *zhp, void *data)
2290b5de56dSgjelinek {
2300b5de56dSgjelinek 	int			res;
2310b5de56dSgjelinek 	zfs_snapshot_data_t	*cbp;
2330b5de56dSgjelinek 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
2340b5de56dSgjelinek 		zfs_close(zhp);
2350b5de56dSgjelinek 		return (0);
2360b5de56dSgjelinek 	}
2380b5de56dSgjelinek 	cbp = (zfs_snapshot_data_t *)data;
2400b5de56dSgjelinek 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) == 0) {
2410b5de56dSgjelinek 		char	*nump;
2420b5de56dSgjelinek 		int	num;
244286822ddS 		cbp->num++;
2450b5de56dSgjelinek 		nump = (char *)(zfs_get_name(zhp) + cbp->len);
2460b5de56dSgjelinek 		num = atoi(nump);
2470b5de56dSgjelinek 		if (num > cbp->max)
2480b5de56dSgjelinek 			cbp->max = num;
2490b5de56dSgjelinek 	}
2510d8fa8f8SMartin Matuska 	res = zfs_iter_snapshots(zhp, B_FALSE, get_snap_max, data);
2520b5de56dSgjelinek 	zfs_close(zhp);
2530b5de56dSgjelinek 	return (res);
2540b5de56dSgjelinek }
2560b5de56dSgjelinek /*
2570b5de56dSgjelinek  * Take a ZFS snapshot to be used for cloning the zone.
2580b5de56dSgjelinek  */
2590b5de56dSgjelinek static int
take_snapshot(zfs_handle_t * zhp,char * snapshot_name,int snap_size,char * presnapbuf,char * postsnapbuf)260ff17c8bfSgjelinek take_snapshot(zfs_handle_t *zhp, char *snapshot_name, int snap_size,
261ff17c8bfSgjelinek     char *presnapbuf, char *postsnapbuf)
2620b5de56dSgjelinek {
2630b5de56dSgjelinek 	int			res;
264*9adfa60dSMatthew Ahrens 	char			template[ZFS_MAX_DATASET_NAME_LEN];
2650b5de56dSgjelinek 	zfs_snapshot_data_t	cb;
2670b5de56dSgjelinek 	/*
2680b5de56dSgjelinek 	 * First we need to figure out the next available name for the
2690b5de56dSgjelinek 	 * zone snapshot.  Look through the list of zones snapshots for
2700b5de56dSgjelinek 	 * this file system to determine the maximum snapshot name.
2710b5de56dSgjelinek 	 */
2720b5de56dSgjelinek 	if (snprintf(template, sizeof (template), "%s@SUNWzone",
2730b5de56dSgjelinek 	    zfs_get_name(zhp)) >=  sizeof (template))
2740b5de56dSgjelinek 		return (Z_ERR);
2760b5de56dSgjelinek 	cb.match_name = template;
2770b5de56dSgjelinek 	cb.len = strlen(template);
2780b5de56dSgjelinek 	cb.max = 0;
2800d8fa8f8SMartin Matuska 	if (zfs_iter_snapshots(zhp, B_FALSE, get_snap_max, &cb) != 0)
2810b5de56dSgjelinek 		return (Z_ERR);
2830b5de56dSgjelinek 	cb.max++;
2850b5de56dSgjelinek 	if (snprintf(snapshot_name, snap_size, "%s@SUNWzone%d",
2860b5de56dSgjelinek 	    zfs_get_name(zhp), cb.max) >= snap_size)
2870b5de56dSgjelinek 		return (Z_ERR);
289ff17c8bfSgjelinek 	if (pre_snapshot(presnapbuf) != Z_OK)
2900b5de56dSgjelinek 		return (Z_ERR);
291bb0ade09Sahrens 	res = zfs_snapshot(g_zfs, snapshot_name, B_FALSE, NULL);
292ff17c8bfSgjelinek 	if (post_snapshot(postsnapbuf) != Z_OK)
2930b5de56dSgjelinek 		return (Z_ERR);
2950b5de56dSgjelinek 	if (res != 0)
2960b5de56dSgjelinek 		return (Z_ERR);
2970b5de56dSgjelinek 	return (Z_OK);
2980b5de56dSgjelinek }
3000b5de56dSgjelinek /*
3010b5de56dSgjelinek  * We are using an explicit snapshot from some earlier point in time so
302ff17c8bfSgjelinek  * we need to validate it.  Run the brand specific hook.
3030b5de56dSgjelinek  */
3040b5de56dSgjelinek static int
validate_snapshot(char * snapshot_name,char * snap_path,char * validsnapbuf)305ff17c8bfSgjelinek validate_snapshot(char *snapshot_name, char *snap_path, char *validsnapbuf)
3060b5de56dSgjelinek {
307ff17c8bfSgjelinek 	int status;
308ff17c8bfSgjelinek 	char cmdbuf[MAXPATHLEN];
310ff17c8bfSgjelinek 	/* No brand-specific handler */
311ff17c8bfSgjelinek 	if (validsnapbuf[0] == '\0')
312ff17c8bfSgjelinek 		return (Z_OK);
314ff17c8bfSgjelinek 	/* pass args - snapshot_name & snap_path */
315ff17c8bfSgjelinek 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s %s %s", validsnapbuf,
316ff17c8bfSgjelinek 	    snapshot_name, snap_path) >= sizeof (cmdbuf)) {
317ff17c8bfSgjelinek 		zerror("Command line too long");
3180b5de56dSgjelinek 		return (Z_ERR);
3190b5de56dSgjelinek 	}
321ff17c8bfSgjelinek 	/* Run the hook */
322c75cc341S 	status = do_subproc(cmdbuf);
323ff17c8bfSgjelinek 	if ((status = subproc_status(gettext("brand-specific validatesnapshot"),
324ff17c8bfSgjelinek 	    status, B_FALSE)) != ZONE_SUBPROC_OK)
325ff17c8bfSgjelinek 		return (Z_ERR);
327ff17c8bfSgjelinek 	return (Z_OK);
3280b5de56dSgjelinek }
3300b5de56dSgjelinek /*
3310b5de56dSgjelinek  * Remove the sw inventory file from inside this zonepath that we picked up out
3320b5de56dSgjelinek  * of the snapshot.
3330b5de56dSgjelinek  */
3340b5de56dSgjelinek static int
clean_out_clone()3350b5de56dSgjelinek clean_out_clone()
3360b5de56dSgjelinek {
3370b5de56dSgjelinek 	int err;
3380b5de56dSgjelinek 	zone_dochandle_t handle;
3400b5de56dSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
3410b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3420b5de56dSgjelinek 		return (Z_ERR);
3430b5de56dSgjelinek 	}
3450b5de56dSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
3460b5de56dSgjelinek 		errno = err;
3470b5de56dSgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3480b5de56dSgjelinek 		zonecfg_fini_handle(handle);
3490b5de56dSgjelinek 		return (Z_ERR);
3500b5de56dSgjelinek 	}
3520b5de56dSgjelinek 	zonecfg_rm_detached(handle, B_FALSE);
3530b5de56dSgjelinek 	zonecfg_fini_handle(handle);
3550b5de56dSgjelinek 	return (Z_OK);
3560b5de56dSgjelinek }
3580b5de56dSgjelinek /*
3590b5de56dSgjelinek  * Make a ZFS clone on zonepath from snapshot_name.
3600b5de56dSgjelinek  */
3610b5de56dSgjelinek static int
clone_snap(char * snapshot_name,char * zonepath)3620b5de56dSgjelinek clone_snap(char *snapshot_name, char *zonepath)
3630b5de56dSgjelinek {
3640b5de56dSgjelinek 	int		res = Z_OK;
3650b5de56dSgjelinek 	int		err;
3660b5de56dSgjelinek 	zfs_handle_t	*zhp;
3670b5de56dSgjelinek 	zfs_handle_t	*clone;
368e9dbad6fSeschrock 	nvlist_t	*props = NULL;
37099653d4eSeschrock 	if ((zhp = zfs_open(g_zfs, snapshot_name, ZFS_TYPE_SNAPSHOT)) == NULL)
3710b5de56dSgjelinek 		return (Z_NO_ENTRY);
3730b5de56dSgjelinek 	(void) printf(gettext("Cloning snapshot %s\n"), snapshot_name);
3752b6c28b8Sbatschul 	/*
3762b6c28b8Sbatschul 	 * We turn off zfs SHARENFS and SHARESMB properties on the
3772b6c28b8Sbatschul 	 * zoneroot dataset in order to prevent the GZ from sharing
3782b6c28b8Sbatschul 	 * NGZ data by accident.
3792b6c28b8Sbatschul 	 */
3802b6c28b8Sbatschul 	if ((nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) ||
3812b6c28b8Sbatschul 	    (nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARENFS),
3822b6c28b8Sbatschul 	    "off") != 0) ||
3832b6c28b8Sbatschul 	    (nvlist_add_string(props, zfs_prop_to_name(ZFS_PROP_SHARESMB),
3842b6c28b8Sbatschul 	    "off") != 0)) {
385aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(props);
386e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not create ZFS clone "
387e9dbad6fSeschrock 		    "%s: out of memory\n"), zonepath);
388e9dbad6fSeschrock 		return (Z_ERR);
389e9dbad6fSeschrock 	}
391e9dbad6fSeschrock 	err = zfs_clone(zhp, zonepath, props);
3920b5de56dSgjelinek 	zfs_close(zhp);
394e9dbad6fSeschrock 	nvlist_free(props);
3960b5de56dSgjelinek 	if (err != 0)
3970b5de56dSgjelinek 		return (Z_ERR);
3990b5de56dSgjelinek 	/* create the mountpoint if necessary */
400990b4856Slling 	if ((clone = zfs_open(g_zfs, zonepath, ZFS_TYPE_DATASET)) == NULL)
4010b5de56dSgjelinek 		return (Z_ERR);
4030b5de56dSgjelinek 	/*
4040b5de56dSgjelinek 	 * The clone has been created so we need to print a diagnostic
4050b5de56dSgjelinek 	 * message if one of the following steps fails for some reason.
4060b5de56dSgjelinek 	 */
4070b5de56dSgjelinek 	if (zfs_mount(clone, NULL, 0) != 0) {
4080b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not mount ZFS clone "
4090b5de56dSgjelinek 		    "%s\n"), zfs_get_name(clone));
4100b5de56dSgjelinek 		res = Z_ERR;
412e9dbad6fSeschrock 	} else if (clean_out_clone() != Z_OK) {
413e9dbad6fSeschrock 		(void) fprintf(stderr, gettext("could not remove the "
414e9dbad6fSeschrock 		    "software inventory from ZFS clone %s\n"),
415e9dbad6fSeschrock 		    zfs_get_name(clone));
416e9dbad6fSeschrock 		res = Z_ERR;
4170b5de56dSgjelinek 	}
4190b5de56dSgjelinek 	zfs_close(clone);
4200b5de56dSgjelinek 	return (res);
4210b5de56dSgjelinek }
4230b5de56dSgjelinek /*
4240b5de56dSgjelinek  * This function takes a zonepath and attempts to determine what the ZFS
4250b5de56dSgjelinek  * file system name (not mountpoint) should be for that path.  We do not
4260b5de56dSgjelinek  * assume that zonepath is an existing directory or ZFS fs since we use
4270b5de56dSgjelinek  * this function as part of the process of creating a new ZFS fs or clone.
4280b5de56dSgjelinek  *
4290b5de56dSgjelinek  * The way this works is that we look at the parent directory of the zonepath
4300b5de56dSgjelinek  * to see if it is a ZFS fs.  If it is, we get the name of that ZFS fs and
4310b5de56dSgjelinek  * append the last component of the zonepath to generate the ZFS name for the
4320b5de56dSgjelinek  * zonepath.  This matches the algorithm that ZFS uses for automatically
4330b5de56dSgjelinek  * mounting a new fs after it is created.
4340b5de56dSgjelinek  *
4350b5de56dSgjelinek  * Although a ZFS fs can be mounted anywhere, we don't worry about handling
4360b5de56dSgjelinek  * all of the complexity that a user could possibly configure with arbitrary
4370b5de56dSgjelinek  * mounts since there is no way to generate a ZFS name from a random path in
4380b5de56dSgjelinek  * the file system.  We only try to handle the automatic mounts that ZFS does
4390b5de56dSgjelinek  * for each file system.  ZFS restricts this so that a new fs must be created
4400b5de56dSgjelinek  * in an existing parent ZFS fs.  It then automatically mounts the new fs
4410b5de56dSgjelinek  * directly under the mountpoint for the parent fs using the last component
4420b5de56dSgjelinek  * of the name as the mountpoint directory.
4430b5de56dSgjelinek  *
4440b5de56dSgjelinek  * For example:
4450b5de56dSgjelinek  *    Name			Mountpoint
4460b5de56dSgjelinek  *    space/eng/dev/test/zone1	/project1/eng/dev/test/zone1
4470b5de56dSgjelinek  *
4480b5de56dSgjelinek  * Return Z_OK if the path mapped to a ZFS file system name, otherwise return
4490b5de56dSgjelinek  * Z_ERR.
4500b5de56dSgjelinek  */
4510b5de56dSgjelinek static int
path2name(char * zonepath,char * zfs_name,int len)4520b5de56dSgjelinek path2name(char *zonepath, char *zfs_name, int len)
4530b5de56dSgjelinek {
4540b5de56dSgjelinek 	int		res;
45511506c41Sgjelinek 	char		*bnm, *dnm, *dname, *bname;
4560b5de56dSgjelinek 	zfs_handle_t	*zhp;
45711506c41Sgjelinek 	struct stat	stbuf;
45911506c41Sgjelinek 	/*
46011506c41Sgjelinek 	 * We need two tmp strings to handle paths directly in / (e.g. /foo)
46111506c41Sgjelinek 	 * since dirname will overwrite the first char after "/" in this case.
46211506c41Sgjelinek 	 */
46311506c41Sgjelinek 	if ((bnm = strdup(zonepath)) == NULL)
46411506c41Sgjelinek 		return (Z_ERR);
46611506c41Sgjelinek 	if ((dnm = strdup(zonepath)) == NULL) {
46711506c41Sgjelinek 		free(bnm);
4680b5de56dSgjelinek 		return (Z_ERR);
46911506c41Sgjelinek 	}
47111506c41Sgjelinek 	bname = basename(bnm);
47211506c41Sgjelinek 	dname = dirname(dnm);
4740b5de56dSgjelinek 	/*
47511506c41Sgjelinek 	 * This is a quick test to save iterating over all of the zfs datasets
47611506c41Sgjelinek 	 * on the system (which can be a lot).  If the parent dir is not in a
47711506c41Sgjelinek 	 * ZFS fs, then we're done.
4780b5de56dSgjelinek 	 */
47911506c41Sgjelinek 	if (stat(dname, &stbuf) != 0 || !S_ISDIR(stbuf.st_mode) ||
48011506c41Sgjelinek 	    strcmp(stbuf.st_fstype, MNTTYPE_ZFS) != 0) {
48111506c41Sgjelinek 		free(bnm);
48211506c41Sgjelinek 		free(dnm);
4830b5de56dSgjelinek 		return (Z_ERR);
48411506c41Sgjelinek 	}
48611506c41Sgjelinek 	/* See if the parent directory is its own ZFS dataset. */
48711506c41Sgjelinek 	if ((zhp = mount2zhandle(dname)) == NULL) {
48811506c41Sgjelinek 		/*
48911506c41Sgjelinek 		 * The parent is not a ZFS dataset so we can't automatically
49011506c41Sgjelinek 		 * create a dataset on the given path.
49111506c41Sgjelinek 		 */
49211506c41Sgjelinek 		free(bnm);
49311506c41Sgjelinek 		free(dnm);
49411506c41Sgjelinek 		return (Z_ERR);
49511506c41Sgjelinek 	}
49711506c41Sgjelinek 	res = snprintf(zfs_name, len, "%s/%s", zfs_get_name(zhp), bname);
49911506c41Sgjelinek 	free(bnm);
50011506c41Sgjelinek 	free(dnm);
5010b5de56dSgjelinek 	zfs_close(zhp);
5020b5de56dSgjelinek 	if (res >= len)
5030b5de56dSgjelinek 		return (Z_ERR);
5050b5de56dSgjelinek 	return (Z_OK);
5060b5de56dSgjelinek }
5080b5de56dSgjelinek /*
5090b5de56dSgjelinek  * A ZFS file system iterator call-back function used to determine if the
5100b5de56dSgjelinek  * file system has dependents (snapshots & clones).
5110b5de56dSgjelinek  */
5120b5de56dSgjelinek /* ARGSUSED */
5130b5de56dSgjelinek static int
has_dependent(zfs_handle_t * zhp,void * data)5140b5de56dSgjelinek has_dependent(zfs_handle_t *zhp, void *data)
5150b5de56dSgjelinek {
5160b5de56dSgjelinek 	zfs_close(zhp);
5170b5de56dSgjelinek 	return (1);
5180b5de56dSgjelinek }
5200b5de56dSgjelinek /*
5210b5de56dSgjelinek  * Given a snapshot name, get the file system path where the snapshot lives.
5220b5de56dSgjelinek  * A snapshot name is of the form fs_name@snap_name.  For example, snapshot
5230b5de56dSgjelinek  * pl/zones/z1@SUNWzone1 would have a path of
5240b5de56dSgjelinek  * /pl/zones/z1/.zfs/snapshot/SUNWzone1.
5250b5de56dSgjelinek  */
5260b5de56dSgjelinek static int
snap2path(char * snap_name,char * path,int len)5270b5de56dSgjelinek snap2path(char *snap_name, char *path, int len)
5280b5de56dSgjelinek {
5290b5de56dSgjelinek 	char		*p;
5300b5de56dSgjelinek 	zfs_handle_t	*zhp;
5310b5de56dSgjelinek 	char		mp[ZFS_MAXPROPLEN];
5330b5de56dSgjelinek 	if ((p = strrchr(snap_name, '@')) == NULL)
5340b5de56dSgjelinek 		return (Z_ERR);
5360b5de56dSgjelinek 	/* Get the file system name from the snap_name. */
5370b5de56dSgjelinek 	*p = '\0';
538990b4856Slling 	zhp = zfs_open(g_zfs, snap_name, ZFS_TYPE_DATASET);
5390b5de56dSgjelinek 	*p = '@';
5400b5de56dSgjelinek 	if (zhp == NULL)
5410b5de56dSgjelinek 		return (Z_ERR);
5430b5de56dSgjelinek 	/* Get the file system mount point. */
5440b5de56dSgjelinek 	if (zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mp, sizeof (mp), NULL, NULL,
54599653d4eSeschrock 	    0, B_FALSE) != 0) {
5460b5de56dSgjelinek 		zfs_close(zhp);
5470b5de56dSgjelinek 		return (Z_ERR);
5480b5de56dSgjelinek 	}
5490b5de56dSgjelinek 	zfs_close(zhp);
5510b5de56dSgjelinek 	p++;
5520b5de56dSgjelinek 	if (snprintf(path, len, "%s/.zfs/snapshot/%s", mp, p) >= len)
5530b5de56dSgjelinek 		return (Z_ERR);
5550b5de56dSgjelinek 	return (Z_OK);
5560b5de56dSgjelinek }
558286822ddS /*
559286822ddS  * This callback function is used to iterate through a snapshot's dependencies
560286822ddS  * to find a filesystem that is a direct clone of the snapshot being iterated.
561286822ddS  */
562286822ddS static int
get_direct_clone(zfs_handle_t * zhp,void * data)563286822ddS get_direct_clone(zfs_handle_t *zhp, void *data)
564286822ddS {
565286822ddS 	clone_data_t	*cd = data;
566*9adfa60dSMatthew Ahrens 	char		origin[ZFS_MAX_DATASET_NAME_LEN];
567*9adfa60dSMatthew Ahrens 	char		ds_path[ZFS_MAX_DATASET_NAME_LEN];
569286822ddS 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
570286822ddS 		zfs_close(zhp);
571286822ddS 		return (0);
572286822ddS 	}
574286822ddS 	(void) strlcpy(ds_path, zfs_get_name(zhp), sizeof (ds_path));
576286822ddS 	/* Make sure this is a direct clone of the snapshot we're iterating. */
577286822ddS 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, origin, sizeof (origin), NULL,
578286822ddS 	    NULL, 0, B_FALSE) != 0 || strcmp(origin, cd->snapshot) != 0) {
579286822ddS 		zfs_close(zhp);
580286822ddS 		return (0);
581286822ddS 	}
583286822ddS 	if (cd->clone_zhp != NULL)
584286822ddS 		zfs_close(cd->clone_zhp);
586286822ddS 	cd->clone_zhp = zhp;
587286822ddS 	return (1);
588286822ddS }
590286822ddS /*
591286822ddS  * A ZFS file system iterator call-back function used to determine the clone
592286822ddS  * to promote.  This function finds the youngest (i.e. last one taken) snapshot
593286822ddS  * that has a clone.  If found, it returns a reference to that clone in the
594286822ddS  * callback data.
595286822ddS  */
596286822ddS static int
find_clone(zfs_handle_t * zhp,void * data)597286822ddS find_clone(zfs_handle_t *zhp, void *data)
598286822ddS {
599286822ddS 	clone_data_t	*cd = data;
600286822ddS 	time_t		snap_creation;
601286822ddS 	int		zret = 0;
603286822ddS 	/* If snapshot has no clones, skip it */
604286822ddS 	if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) == 0) {
605286822ddS 		zfs_close(zhp);
606286822ddS 		return (0);
607286822ddS 	}
609286822ddS 	cd->snapshot = zfs_get_name(zhp);
611286822ddS 	/* Get the creation time of this snapshot */
612286822ddS 	snap_creation = (time_t)zfs_prop_get_int(zhp, ZFS_PROP_CREATION);
614286822ddS 	/*
615286822ddS 	 * If this snapshot's creation time is greater than (i.e. younger than)
616286822ddS 	 * the current youngest snapshot found, iterate this snapshot to
617286822ddS 	 * get the right clone.
618286822ddS 	 */
619286822ddS 	if (snap_creation >= cd->origin_creation) {
620286822ddS 		/*
621286822ddS 		 * Iterate the dependents of this snapshot to find a clone
622286822ddS 		 * that's a direct dependent.
623286822ddS 		 */
624286822ddS 		if ((zret = zfs_iter_dependents(zhp, B_FALSE, get_direct_clone,
625286822ddS 		    cd)) == -1) {
626286822ddS 			zfs_close(zhp);
627286822ddS 			return (1);
628286822ddS 		} else if (zret == 1) {
629286822ddS 			/*
630286822ddS 			 * Found a clone, update the origin_creation time
631286822ddS 			 * in the callback data.
632286822ddS 			 */
633286822ddS 			cd->origin_creation = snap_creation;
634286822ddS 		}
635286822ddS 	}
637286822ddS 	zfs_close(zhp);
638286822ddS 	return (0);
639286822ddS }
641286822ddS /*
642286822ddS  * A ZFS file system iterator call-back function used to remove standalone
643286822ddS  * snapshots.
644286822ddS  */
645286822ddS /* ARGSUSED */
646286822ddS static int
rm_snap(zfs_handle_t * zhp,void * data)647286822ddS rm_snap(zfs_handle_t *zhp, void *data)
648286822ddS {
649286822ddS 	/* If snapshot has clones, something is wrong */
650286822ddS 	if (zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES) != 0) {
651286822ddS 		zfs_close(zhp);
652286822ddS 		return (1);
653286822ddS 	}
655286822ddS 	if (zfs_unmount(zhp, NULL, 0) == 0) {
656842727c2SChris Kirby 		(void) zfs_destroy(zhp, B_FALSE);
657286822ddS 	}
659286822ddS 	zfs_close(zhp);
660286822ddS 	return (0);
661286822ddS }
663286822ddS /*
664286822ddS  * A ZFS snapshot iterator call-back function which renames snapshots.
665286822ddS  */
666286822ddS static int
rename_snap(zfs_handle_t * zhp,void * data)667286822ddS rename_snap(zfs_handle_t *zhp, void *data)
668286822ddS {
669286822ddS 	int			res;
670286822ddS 	zfs_snapshot_data_t	*cbp;
671*9adfa60dSMatthew Ahrens 	char			template[ZFS_MAX_DATASET_NAME_LEN];
673286822ddS 	cbp = (zfs_snapshot_data_t *)data;
675286822ddS 	/*
676286822ddS 	 * When renaming snapshots with the iterator, the iterator can see
677286822ddS 	 * the same snapshot after we've renamed up in the namespace.  To
678286822ddS 	 * prevent this we check the count for the number of snapshots we have
679286822ddS 	 * to rename and stop at that point.
680286822ddS 	 */
681286822ddS 	if (cbp->cntr >= cbp->num) {
682286822ddS 		zfs_close(zhp);
683286822ddS 		return (0);
684286822ddS 	}
686286822ddS 	if (zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) {
687286822ddS 		zfs_close(zhp);
688286822ddS 		return (0);
689286822ddS 	}
691286822ddS 	/* Only rename the snapshots we automatically generate when we clone. */
692286822ddS 	if (strncmp(zfs_get_name(zhp), cbp->match_name, cbp->len) != 0) {
693286822ddS 		zfs_close(zhp);
694286822ddS 		return (0);
695286822ddS 	}
697286822ddS 	(void) snprintf(template, sizeof (template), "%s%d", cbp->match_name,
698286822ddS 	    cbp->max++);
7006a9cb0eaSEric Schrock 	res = (zfs_rename(zhp, template, B_FALSE, B_FALSE) != 0);
701286822ddS 	if (res != 0)
702286822ddS 		(void) fprintf(stderr, gettext("failed to rename snapshot %s "
703286822ddS 		    "to %s: %s\n"), zfs_get_name(zhp), template,
704286822ddS 		    libzfs_error_description(g_zfs));
706286822ddS 	cbp->cntr++;
708286822ddS 	zfs_close(zhp);
709286822ddS 	return (res);
710286822ddS }
712286822ddS /*
713286822ddS  * Rename the source dataset's snapshots that are automatically generated when
714286822ddS  * we clone a zone so that there won't be a name collision when we promote the
715286822ddS  * cloned dataset.  Once the snapshots have been renamed, then promote the
716286822ddS  * clone.
717286822ddS  *
718286822ddS  * The snapshot rename process gets the highest number on the snapshot names
719286822ddS  * (the format is zonename@SUNWzoneXX where XX are digits) on both the source
720286822ddS  * and clone datasets, then renames the source dataset snapshots starting at
721286822ddS  * the next number.
722286822ddS  */
723286822ddS static int
promote_clone(zfs_handle_t * src_zhp,zfs_handle_t * cln_zhp)724286822ddS promote_clone(zfs_handle_t *src_zhp, zfs_handle_t *cln_zhp)
725286822ddS {
726286822ddS 	zfs_snapshot_data_t	sd;
727*9adfa60dSMatthew Ahrens 	char			nm[ZFS_MAX_DATASET_NAME_LEN];
728*9adfa60dSMatthew Ahrens 	char			template[ZFS_MAX_DATASET_NAME_LEN];
730286822ddS 	(void) strlcpy(nm, zfs_get_name(cln_zhp), sizeof (nm));
731286822ddS 	/*
732286822ddS 	 * Start by getting the clone's snapshot max which we use
733286822ddS 	 * during the rename of the original dataset's snapshots.
734286822ddS 	 */
735286822ddS 	(void) snprintf(template, sizeof (template), "%s@SUNWzone", nm);
736286822ddS 	sd.match_name = template;
737286822ddS 	sd.len = strlen(template);
738286822ddS 	sd.max = 0;
7400d8fa8f8SMartin Matuska 	if (zfs_iter_snapshots(cln_zhp, B_FALSE, get_snap_max, &sd) != 0)
741286822ddS 		return (Z_ERR);
743286822ddS 	/*
744286822ddS 	 * Now make sure the source's snapshot max is at least as high as
745286822ddS 	 * the clone's snapshot max.
746286822ddS 	 */
747286822ddS 	(void) snprintf(template, sizeof (template), "%s@SUNWzone",
748286822ddS 	    zfs_get_name(src_zhp));
749286822ddS 	sd.match_name = template;
750286822ddS 	sd.len = strlen(template);
751286822ddS 	sd.num = 0;
7530d8fa8f8SMartin Matuska 	if (zfs_iter_snapshots(src_zhp, B_FALSE, get_snap_max, &sd) != 0)
754286822ddS 		return (Z_ERR);
756286822ddS 	/*
757286822ddS 	 * Now rename the source dataset's snapshots so there's no
758286822ddS 	 * conflict when we promote the clone.
759286822ddS 	 */
760286822ddS 	sd.max++;
761286822ddS 	sd.cntr = 0;
7620d8fa8f8SMartin Matuska 	if (zfs_iter_snapshots(src_zhp, B_FALSE, rename_snap, &sd) != 0)
763286822ddS 		return (Z_ERR);