usr/src/lib/libzfs/common/libzfs_dataset.c
author marks
Tue, 26 Jun 2007 07:44:24 -0700
changeset 4543 12bb2876a62e
parent 4490 abf035049f7f
child 4577 ed36b0e652bc
permissions -rw-r--r--
PSARC/2006/465 ZFS Delegated Administration PSARC/2006/577 zpool property to disable delegation PSARC/2006/625 Enhancements to zpool history PSARC/2007/228 ZFS delegation amendments PSARC/2007/295 ZFS Delegated Administration Addendum 6280676 restore "owner" property 6349470 investigate non-root restore/backup 6572465 'zpool set bootfs=...' records history as 'zfs set bootfs=...'
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1407
diff changeset
     5
 * Common Development and Distribution License (the "License").
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1407
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
    21
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
3363
03fa67b9c65f 6508883 libzfs should not use VERIFY()
gw25295
parents: 3265
diff changeset
    23
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <assert.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <ctype.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <libdevinfo.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <libintl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <math.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <stdio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <stdlib.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <strings.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <unistd.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <zone.h>
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
    40
#include <fcntl.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include <sys/mntent.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#include <sys/mnttab.h>
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
    43
#include <sys/mount.h>
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    44
#include <sys/avl.h>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    45
#include <priv.h>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    46
#include <pwd.h>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    47
#include <grp.h>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    48
#include <stddef.h>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    49
#include <ucred.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
#include <sys/zio.h>
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
    53
#include <sys/zap.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
#include <libzfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
#include "zfs_namecheck.h"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
#include "zfs_prop.h"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
#include "libzfs_impl.h"
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    59
#include "zfs_deleg.h"
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
    61
static int create_parents(libzfs_handle_t *, char *, int);
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
    62
static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
    63
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
 * Given a single type (not a mask of types), return the type in a human
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
 * readable form.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
zfs_type_to_name(zfs_type_t type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
	switch (type) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
	case ZFS_TYPE_FILESYSTEM:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
		return (dgettext(TEXT_DOMAIN, "filesystem"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
	case ZFS_TYPE_SNAPSHOT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
		return (dgettext(TEXT_DOMAIN, "snapshot"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
	case ZFS_TYPE_VOLUME:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
		return (dgettext(TEXT_DOMAIN, "volume"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
	return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
 * Given a path and mask of ZFS types, return a string describing this dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
 * This is used when we fail to open a dataset and we cannot get an exact type.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
 * We guess what the type would have been based on the path and the mask of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
 * acceptable types.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
static const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
path_to_str(const char *path, int types)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
	 * When given a single type, always report the exact type.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
	if (types == ZFS_TYPE_SNAPSHOT)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
		return (dgettext(TEXT_DOMAIN, "snapshot"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
	if (types == ZFS_TYPE_FILESYSTEM)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
		return (dgettext(TEXT_DOMAIN, "filesystem"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
	if (types == ZFS_TYPE_VOLUME)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
		return (dgettext(TEXT_DOMAIN, "volume"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
	 * The user is requesting more than one type of dataset.  If this is the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
	 * case, consult the path itself.  If we're looking for a snapshot, and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
	 * a '@' is found, then report it as "snapshot".  Otherwise, remove the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
	 * snapshot attribute and try again.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
	if (types & ZFS_TYPE_SNAPSHOT) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
		if (strchr(path, '@') != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
			return (dgettext(TEXT_DOMAIN, "snapshot"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
		return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
	 * The user has requested either filesystems or volumes.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
	 * We have no way of knowing a priori what type this would be, so always
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
	 * report it as "filesystem" or "volume", our two primitive types.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
	if (types & ZFS_TYPE_FILESYSTEM)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
		return (dgettext(TEXT_DOMAIN, "filesystem"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
	assert(types & ZFS_TYPE_VOLUME);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
	return (dgettext(TEXT_DOMAIN, "volume"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
 * Validate a ZFS path.  This is used even before trying to open the dataset, to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
 * provide a more meaningful error message.  We place a more useful message in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
 * 'buf' detailing exactly why the name was not valid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
static int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   133
zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
	namecheck_err_t why;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
	char what;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
	if (dataset_namecheck(path, &why, &what) != 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   139
		if (hdl != NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
			switch (why) {
1003
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 905
diff changeset
   141
			case NAME_ERR_TOOLONG:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   142
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   143
				    "name is too long"));
1003
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 905
diff changeset
   144
				break;
ce99098d6a9b 6350417 long name will cause rename a dataset panic
lling
parents: 905
diff changeset
   145
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
			case NAME_ERR_LEADING_SLASH:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   147
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   148
				    "leading slash in name"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
			case NAME_ERR_EMPTY_COMPONENT:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   152
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   153
				    "empty component in name"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
			case NAME_ERR_TRAILING_SLASH:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   157
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   158
				    "trailing slash in name"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
			case NAME_ERR_INVALCHAR:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   162
				zfs_error_aux(hdl,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   163
				    dgettext(TEXT_DOMAIN, "invalid character "
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   164
				    "'%c' in name"), what);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
			case NAME_ERR_MULTIPLE_AT:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   168
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   169
				    "multiple '@' delimiters in name"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
				break;
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   171
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   172
			case NAME_ERR_NOLETTER:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   173
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   174
				    "pool doesn't begin with a letter"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   175
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   176
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   177
			case NAME_ERR_RESERVED:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   178
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   179
				    "name is reserved"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   180
				break;
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   181
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   182
			case NAME_ERR_DISKLIKE:
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   183
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   184
				    "reserved disk name"));
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
   185
				break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   190
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   191
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   192
	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   193
		if (hdl != NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   194
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   195
			    "snapshot delimiter '@' in filesystem name"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   196
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   197
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
   199
	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
   200
		if (hdl != NULL)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
   201
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3413
f4cbe0204ab7 6471255 Some ZFS error messages contain spelling errors
mmusante
parents: 3377
diff changeset
   202
			    "missing '@' delimiter in snapshot name"));
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
   203
		return (0);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
   204
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
   205
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   206
	return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   207
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   208
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   209
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   210
zfs_name_valid(const char *name, zfs_type_t type)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   211
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   212
	return (zfs_validate_name(NULL, name, type));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   213
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
/*
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   216
 * This function takes the raw DSL properties, and filters out the user-defined
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   217
 * properties into a separate nvlist.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   218
 */
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   219
static nvlist_t *
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   220
process_user_props(zfs_handle_t *zhp, nvlist_t *props)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   221
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   222
	libzfs_handle_t *hdl = zhp->zfs_hdl;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   223
	nvpair_t *elem;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   224
	nvlist_t *propval;
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   225
	nvlist_t *nvl;
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   226
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   227
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   228
		(void) no_memory(hdl);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   229
		return (NULL);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   230
	}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   231
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   232
	elem = NULL;
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   233
	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   234
		if (!zfs_prop_user(nvpair_name(elem)))
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   235
			continue;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   236
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   237
		verify(nvpair_value_nvlist(elem, &propval) == 0);
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   238
		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   239
			nvlist_free(nvl);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   240
			(void) no_memory(hdl);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   241
			return (NULL);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   242
		}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   243
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   244
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   245
	return (nvl);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   246
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   247
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   248
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
 * Utility function to gather stats (objset and zpl) for the given object.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
get_stats(zfs_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
	zfs_cmd_t zc = { 0 };
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   255
	libzfs_handle_t *hdl = zhp->zfs_hdl;
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   256
	nvlist_t *allprops, *userprops;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   260
	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   261
		return (-1);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
   262
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   263
	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
   264
		if (errno == ENOMEM) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   265
			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   266
				zcmd_free_nvlists(&zc);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   267
				return (-1);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   268
			}
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
   269
		} else {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   270
			zcmd_free_nvlists(&zc);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
   271
			return (-1);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
   272
		}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
   273
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   275
	zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   277
	(void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1407
diff changeset
   278
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   279
	if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   280
		zcmd_free_nvlists(&zc);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   281
		return (-1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   282
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   284
	zcmd_free_nvlists(&zc);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   285
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   286
	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   287
		nvlist_free(allprops);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   288
		return (-1);
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   289
	}
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   290
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   291
	nvlist_free(zhp->zfs_props);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   292
	nvlist_free(zhp->zfs_user_props);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   293
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   294
	zhp->zfs_props = allprops;
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   295
	zhp->zfs_user_props = userprops;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   296
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
 * Refresh the properties currently stored in the handle.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
zfs_refresh_properties(zfs_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
	(void) get_stats(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
 * Makes a handle from the given dataset name.  Used by zfs_open() and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
 * zfs_iter_* to create child handles on the fly.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
zfs_handle_t *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   314
make_dataset_handle(libzfs_handle_t *hdl, const char *path)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   316
	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   317
	char *logstr;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   318
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   319
	if (zhp == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   320
		return (NULL);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   321
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   322
	zhp->zfs_hdl = hdl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   324
	/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   325
	 * Preserve history log string.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   326
	 * any changes performed here will be
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   327
	 * logged as an internal event.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   328
	 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   329
	logstr = zhp->zfs_hdl->libzfs_log_str;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   330
	zhp->zfs_hdl->libzfs_log_str = NULL;
1758
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   331
top:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
	if (get_stats(zhp) != 0) {
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   335
		zhp->zfs_hdl->libzfs_log_str = logstr;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
		free(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
1758
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   340
	if (zhp->zfs_dmustats.dds_inconsistent) {
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   341
		zfs_cmd_t zc = { 0 };
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   342
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   343
		/*
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   344
		 * If it is dds_inconsistent, then we've caught it in
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   345
		 * the middle of a 'zfs receive' or 'zfs destroy', and
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   346
		 * it is inconsistent from the ZPL's point of view, so
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   347
		 * can't be mounted.  However, it could also be that we
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   348
		 * have crashed in the middle of one of those
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   349
		 * operations, in which case we need to get rid of the
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   350
		 * inconsistent state.  We do that by either rolling
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   351
		 * back to the previous snapshot (which will fail if
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   352
		 * there is none), or destroying the filesystem.  Note
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   353
		 * that if we are still in the middle of an active
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   354
		 * 'receive' or 'destroy', then the rollback and destroy
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   355
		 * will fail with EBUSY and we will drive on as usual.
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   356
		 */
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   357
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   358
		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   359
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   360
		if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   361
			(void) zvol_remove_link(hdl, zhp->zfs_name);
1758
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   362
			zc.zc_objset_type = DMU_OST_ZVOL;
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   363
		} else {
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   364
			zc.zc_objset_type = DMU_OST_ZFS;
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   365
		}
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   366
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   367
		/* If we can successfully roll it back, reget the stats */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   368
		if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0)
1758
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   369
			goto top;
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   370
		/*
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   371
		 * If we can sucessfully destroy it, pretend that it
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   372
		 * never existed.
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   373
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   374
		if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) {
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   375
			zhp->zfs_hdl->libzfs_log_str = logstr;
1758
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   376
			free(zhp);
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   377
			errno = ENOENT;
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   378
			return (NULL);
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   379
		}
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   380
	}
d0750a16db04 6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents: 1749
diff changeset
   381
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
	 * We've managed to open the dataset and gather statistics.  Determine
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
	 * the high-level type.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
	 */
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   386
	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   387
		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   388
	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   389
		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   390
	else
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   391
		abort();
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   392
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
	if (zhp->zfs_dmustats.dds_is_snapshot)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
		zhp->zfs_type = ZFS_TYPE_VOLUME;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
	else
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   400
		abort();	/* we should never see any other types */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   402
	zhp->zfs_hdl->libzfs_log_str = logstr;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
	return (zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
 * Opens the given snapshot, filesystem, or volume.   The 'types'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
 * argument is a mask of acceptable types.  The function will print an
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
 * appropriate error message and return NULL if it can't be opened.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
zfs_handle_t *
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   412
zfs_open(libzfs_handle_t *hdl, const char *path, int types)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
	zfs_handle_t *zhp;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   415
	char errbuf[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   416
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   417
	(void) snprintf(errbuf, sizeof (errbuf),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   418
	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
	/*
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   421
	 * Validate the name before we even try to open it.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   423
	if (!zfs_validate_name(hdl, path, ZFS_TYPE_ANY)) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   424
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   425
		    "invalid dataset name"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   426
		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
	 * Try to get stats for the dataset, which will tell us if it exists.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
	errno = 0;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   434
	if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
   435
		(void) zfs_standard_error(hdl, errno, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
	if (!(types & zhp->zfs_type)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   440
		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2142
f6e0487aa9a3 6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents: 2082
diff changeset
   441
		zfs_close(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
		return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
	return (zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
 * Release a ZFS handle.  Nothing to do but free the associated memory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
zfs_close(zfs_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
	if (zhp->zfs_mntopts)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
		free(zhp->zfs_mntopts);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   456
	nvlist_free(zhp->zfs_props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   457
	nvlist_free(zhp->zfs_user_props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
	free(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
 * Given a numeric suffix, convert the value into a number of bits that the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
 * resulting value must be shifted.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
static int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   466
str2shift(libzfs_handle_t *hdl, const char *buf)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
	const char *ends = "BKMGTPEZ";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   469
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
	if (buf[0] == '\0')
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   472
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   473
	for (i = 0; i < strlen(ends); i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
		if (toupper(buf[0]) == ends[i])
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   475
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   477
	if (i == strlen(ends)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   478
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   479
		    "invalid numeric suffix '%s'"), buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   481
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   482
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
	 * We want to allow trailing 'b' characters for 'GB' or 'Mb'.  But don't
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
	 * allow 'BB' - that's just weird.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' &&
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   488
	    toupper(buf[0]) != 'B'))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
		return (10*i);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   490
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   491
	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   492
	    "invalid numeric suffix '%s'"), buf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
	return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
 * Convert a string of the form '100G' into a real number.  Used when setting
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
 * properties or creating a volume.  'buf' is used to place an extended error
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
 * message for the caller to use.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
static int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   502
nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
	char *end;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
	int shift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
	*num = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
	/* Check to see if this looks like a number.  */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   511
		if (hdl)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   512
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   513
			    "bad numeric value '%s'"), value);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
	/* Rely on stroll() to process the numeric portion.  */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
	errno = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
	*num = strtoll(value, &end, 10);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
	 * Check for ERANGE, which indicates that the value is too large to fit
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
	 * in a 64-bit value.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
	if (errno == ERANGE) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   526
		if (hdl)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   527
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   528
			    "numeric value is too large"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
	 * If we have a decimal value, then do the computation with floating
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
	 * point arithmetic.  Otherwise, use standard arithmetic.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
	if (*end == '.') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
		double fval = strtod(value, &end);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   539
		if ((shift = str2shift(hdl, end)) == -1)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
		fval *= pow(2, shift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
		if (fval > UINT64_MAX) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   545
			if (hdl)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   546
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   547
				    "numeric value is too large"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
		*num = (uint64_t)fval;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
	} else {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   553
		if ((shift = str2shift(hdl, end)) == -1)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
		/* Check for overflow */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
		if (shift >= 64 || (*num << shift) >> shift != *num) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   558
			if (hdl)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   559
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   560
				    "numeric value is too large"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
		*num <<= shift;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
int
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   571
zfs_nicestrtonum(libzfs_handle_t *hdl, const char *str, uint64_t *val)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   572
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   573
	return (nicestrtonum(hdl, str, val));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   574
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   575
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   576
/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   577
 * The prop_parse_*() functions are designed to allow flexibility in callers
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   578
 * when setting properties.  At the DSL layer, all properties are either 64-bit
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   579
 * numbers or strings.  We want the user to be able to ignore this fact and
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   580
 * specify properties as native values (boolean, for example) or as strings (to
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   581
 * simplify command line utilities).  This also handles converting index types
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   582
 * (compression, checksum, etc) from strings to their on-disk index.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   583
 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   584
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   585
static int
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   586
prop_parse_boolean(libzfs_handle_t *hdl, nvpair_t *elem, uint64_t *val)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   588
	uint64_t ret;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   589
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   590
	switch (nvpair_type(elem)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   591
	case DATA_TYPE_STRING:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   592
		{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   593
			char *value;
3363
03fa67b9c65f 6508883 libzfs should not use VERIFY()
gw25295
parents: 3265
diff changeset
   594
			verify(nvpair_value_string(elem, &value) == 0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   595
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   596
			if (strcmp(value, "on") == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   597
				ret = 1;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   598
			} else if (strcmp(value, "off") == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   599
				ret = 0;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   600
			} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   601
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   602
				    "property '%s' must be 'on' or 'off'"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   603
				    nvpair_name(elem));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   604
				return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   605
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   606
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   607
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   608
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   609
	case DATA_TYPE_UINT64:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   610
		{
3363
03fa67b9c65f 6508883 libzfs should not use VERIFY()
gw25295
parents: 3265
diff changeset
   611
			verify(nvpair_value_uint64(elem, &ret) == 0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   612
			if (ret > 1) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   613
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   614
				    "'%s' must be a boolean value"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   615
				    nvpair_name(elem));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   616
				return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   617
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   618
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   619
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   620
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   621
	case DATA_TYPE_BOOLEAN_VALUE:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   622
		{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   623
			boolean_t value;
3363
03fa67b9c65f 6508883 libzfs should not use VERIFY()
gw25295
parents: 3265
diff changeset
   624
			verify(nvpair_value_boolean_value(elem, &value) == 0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   625
			ret = value;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   626
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   627
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   628
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   629
	default:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   630
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   631
		    "'%s' must be a boolean value"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   632
		    nvpair_name(elem));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   633
		return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   634
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   635
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   636
	*val = ret;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   637
	return (0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   638
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   639
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   640
static int
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   641
prop_parse_number(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   642
    uint64_t *val)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   643
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   644
	uint64_t ret;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   645
	boolean_t isnone = B_FALSE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   646
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   647
	switch (nvpair_type(elem)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   648
	case DATA_TYPE_STRING:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   649
		{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   650
			char *value;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   651
			(void) nvpair_value_string(elem, &value);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   652
			if (strcmp(value, "none") == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   653
				isnone = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   654
				ret = 0;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   655
			} else if (nicestrtonum(hdl, value, &ret) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   656
				return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   657
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   658
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   659
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   660
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   661
	case DATA_TYPE_UINT64:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   662
		(void) nvpair_value_uint64(elem, &ret);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   663
		break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   664
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   665
	default:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   666
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   667
		    "'%s' must be a number"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   668
		    nvpair_name(elem));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   669
		return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   670
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   671
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   672
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   673
	 * Quota special: force 'none' and don't allow 0.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   674
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   675
	if (ret == 0 && !isnone && prop == ZFS_PROP_QUOTA) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   676
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   677
		    "use 'none' to disable quota"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   678
		return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   679
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   680
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   681
	*val = ret;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   682
	return (0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   683
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   684
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   685
static int
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   686
prop_parse_index(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   687
    uint64_t *val)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   688
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   689
	char *propname = nvpair_name(elem);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   690
	char *value;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   691
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   692
	if (nvpair_type(elem) != DATA_TYPE_STRING) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   693
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   694
		    "'%s' must be a string"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   695
		return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   696
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   697
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   698
	(void) nvpair_value_string(elem, &value);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   699
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   700
	if (zfs_prop_string_to_index(prop, value, val) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   701
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   702
		    "'%s' must be one of '%s'"), propname,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   703
		    zfs_prop_values(prop));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   704
		return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   705
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   706
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   707
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
/*
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   711
 * Check if the bootfs name has the same pool name as it is set to.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   712
 * Assuming bootfs is a valid dataset name.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   713
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   714
static boolean_t
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   715
bootfs_poolname_valid(char *pool, char *bootfs)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   716
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   717
	char ch, *pname;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   718
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   719
	/* get the pool name from the bootfs name */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   720
	pname = bootfs;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   721
	while (*bootfs && !isspace(*bootfs) && *bootfs != '/')
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   722
		bootfs++;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   723
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   724
	ch = *bootfs;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   725
	*bootfs = 0;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   726
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   727
	if (strcmp(pool, pname) == 0) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   728
		*bootfs = ch;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   729
		return (B_TRUE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   730
	}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   731
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   732
	*bootfs = ch;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   733
	return (B_FALSE);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   734
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   735
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   736
/*
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   737
 * Given an nvlist of properties to set, validates that they are correct, and
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   738
 * parses any numeric properties (index, boolean, etc) if they are specified as
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   739
 * strings.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
 */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   741
nvlist_t *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   742
zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, char *pool_name,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   743
    nvlist_t *nvl, uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   744
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   745
	nvpair_t *elem;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   746
	const char *propname;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   747
	zfs_prop_t prop;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   748
	uint64_t intval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   749
	char *strval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   750
	nvlist_t *ret;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   751
	int isuser;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   752
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   753
	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   754
		(void) no_memory(hdl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   755
		return (NULL);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   756
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   757
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   758
	if (type == ZFS_TYPE_SNAPSHOT) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   759
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3413
f4cbe0204ab7 6471255 Some ZFS error messages contain spelling errors
mmusante
parents: 3377
diff changeset
   760
		    "snapshot properties cannot be modified"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   761
		(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   762
		goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   765
	elem = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   766
	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   767
		propname = nvpair_name(elem);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   768
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   769
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   770
		 * Make sure this property is valid and applies to this type.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   771
		 */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   772
		if ((prop = zfs_name_to_prop_common(propname, type))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   773
		    == ZFS_PROP_INVAL) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   774
			isuser = zfs_prop_user(propname);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
   775
			if (!isuser || (isuser && (type & ZFS_TYPE_POOL))) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   776
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   777
				    "invalid property '%s'"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   778
				    propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   779
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   780
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   781
			} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   782
				/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   783
				 * If this is a user property, make sure it's a
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   784
				 * string, and that it's less than
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   785
				 * ZAP_MAXNAMELEN.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   786
				 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   787
				if (nvpair_type(elem) != DATA_TYPE_STRING) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   788
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   789
					    "'%s' must be a string"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   790
					    propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   791
					(void) zfs_error(hdl, EZFS_BADPROP,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   792
					    errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   793
					goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   794
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   795
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   796
				if (strlen(nvpair_name(elem)) >=
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   797
				    ZAP_MAXNAMELEN) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   798
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   799
					    "property name '%s' is too long"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   800
					    propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   801
					(void) zfs_error(hdl, EZFS_BADPROP,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   802
					    errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   803
					goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   804
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   805
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   806
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   807
			(void) nvpair_value_string(elem, &strval);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   808
			if (nvlist_add_string(ret, propname, strval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   809
				(void) no_memory(hdl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   810
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   811
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   812
			continue;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
		}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   814
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   815
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   816
		 * Normalize the name, to get rid of shorthand abbrevations.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   817
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   818
		propname = zfs_prop_to_name(prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   819
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   820
		if (!zfs_prop_valid_for_type(prop, type)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   821
			zfs_error_aux(hdl,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   822
			    dgettext(TEXT_DOMAIN, "'%s' does not "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   823
			    "apply to datasets of this type"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   824
			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   825
			goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   826
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   827
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   828
		if (zfs_prop_readonly(prop) &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   829
		    (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   830
			zfs_error_aux(hdl,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   831
			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   832
			    propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   833
			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   834
			goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   835
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   836
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   837
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   838
		 * Convert any properties to the internal DSL value types.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   839
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   840
		strval = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   841
		switch (zfs_prop_get_type(prop)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   842
		case prop_type_boolean:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   843
			if (prop_parse_boolean(hdl, elem, &intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   844
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   845
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   846
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
			break;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   848
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   849
		case prop_type_string:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   850
			if (nvpair_type(elem) != DATA_TYPE_STRING) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   851
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   852
				    "'%s' must be a string"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   853
				    propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   854
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   855
				goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
			}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   857
			(void) nvpair_value_string(elem, &strval);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   858
			if (strlen(strval) >= ZFS_MAXPROPLEN) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   859
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   860
				    "'%s' is too long"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   861
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   862
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   863
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   864
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   865
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   866
		case prop_type_number:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   867
			if (prop_parse_number(hdl, elem, prop, &intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   868
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   869
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   870
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   871
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   872
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   873
		case prop_type_index:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   874
			if (prop_parse_index(hdl, elem, prop, &intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   875
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   876
				goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   878
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   879
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   880
		default:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   881
			abort();
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   882
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   883
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   884
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   885
		 * Add the result to our return set of properties.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   886
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   887
		if (strval) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   888
			if (nvlist_add_string(ret, propname, strval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   889
				(void) no_memory(hdl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   890
				goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
			}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   892
		} else if (nvlist_add_uint64(ret, propname, intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   893
			(void) no_memory(hdl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   894
			goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   895
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   896
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   897
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   898
		 * Perform some additional checks for specific properties.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   899
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   900
		switch (prop) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   901
		case ZFS_PROP_RECORDSIZE:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   902
		case ZFS_PROP_VOLBLOCKSIZE:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   903
			/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   904
			if (intval < SPA_MINBLOCKSIZE ||
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   905
			    intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   906
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   907
				    "'%s' must be power of 2 from %u "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   908
				    "to %uk"), propname,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   909
				    (uint_t)SPA_MINBLOCKSIZE,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   910
				    (uint_t)SPA_MAXBLOCKSIZE >> 10);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   911
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   912
				goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   913
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   914
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   915
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   916
		case ZFS_PROP_SHAREISCSI:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   917
			if (strcmp(strval, "off") != 0 &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   918
			    strcmp(strval, "on") != 0 &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   919
			    strcmp(strval, "type=disk") != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   920
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   921
				    "'%s' must be 'on', 'off', or 'type=disk'"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   922
				    propname);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   923
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   924
				goto error;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   925
			}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   926
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   927
			break;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   928
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   929
		case ZFS_PROP_MOUNTPOINT:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   930
			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   931
			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   932
				break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   933
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   934
			if (strval[0] != '/') {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
   935
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   936
				    "'%s' must be an absolute path, "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   937
				    "'none', or 'legacy'"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   938
				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   939
				goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
			}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   941
			/*FALLTHRU*/
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   942
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   943
		case ZFS_PROP_SHARENFS:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   944
			/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   945
			 * For the mountpoint and sharenfs properties, check if
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   946
			 * it can be set in a global/non-global zone based on
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   947
			 * the zoned property value:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   948
			 *
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   949
			 *		global zone	    non-global zone
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   950
			 * --------------------------------------------------
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   951
			 * zoned=on	mountpoint (no)	    mountpoint (yes)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   952
			 *		sharenfs (no)	    sharenfs (no)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   953
			 *
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   954
			 * zoned=off	mountpoint (yes)	N/A
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   955
			 *		sharenfs (yes)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   956
			 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   957
			if (zoned) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   958
				if (getzoneid() == GLOBAL_ZONEID) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   959
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   960
					    "'%s' cannot be set on "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   961
					    "dataset in a non-global zone"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   962
					    propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   963
					(void) zfs_error(hdl, EZFS_ZONED,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   964
					    errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   965
					goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   966
				} else if (prop == ZFS_PROP_SHARENFS) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   967
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   968
					    "'%s' cannot be set in "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   969
					    "a non-global zone"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   970
					(void) zfs_error(hdl, EZFS_ZONED,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   971
					    errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   972
					goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   973
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   974
			} else if (getzoneid() != GLOBAL_ZONEID) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   975
				/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   976
				 * If zoned property is 'off', this must be in
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   977
				 * a globle zone. If not, something is wrong.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   978
				 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   979
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   980
				    "'%s' cannot be set while dataset "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   981
				    "'zoned' property is set"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   982
				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   983
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   984
			}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
   985
4180
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   986
			/*
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   987
			 * At this point, it is legitimate to set the
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   988
			 * property. Now we want to make sure that the
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   989
			 * property value is valid if it is sharenfs.
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   990
			 */
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   991
			if (prop == ZFS_PROP_SHARENFS &&
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   992
			    strcmp(strval, "on") != 0 &&
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
   993
			    strcmp(strval, "off") != 0) {
4180
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   994
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   995
				/*
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   996
				 * Must be an NFS option string so
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   997
				 * init the libshare in order to
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   998
				 * enable the parser and then parse
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
   999
				 * the options. We use the control API
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1000
				 * since we don't care about the
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1001
				 * current configuration and don't
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1002
				 * want the overhead of loading it
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1003
				 * until we actually do something.
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1004
				 */
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1005
4217
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1006
				if (zfs_init_libshare(hdl,
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1007
				    SA_INIT_CONTROL_API) != SA_OK) {
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1008
					/*
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1009
					 * An error occurred so we can't do
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1010
					 * anything
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1011
					 */
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1012
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1013
					    "'%s' cannot be set: problem "
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1014
					    "in share initialization"),
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1015
					    propname);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1016
					(void) zfs_error(hdl, EZFS_BADPROP,
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1017
					    errbuf);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1018
					goto error;
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1019
				}
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1020
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1021
				if (zfs_parse_options(strval, "nfs") != SA_OK) {
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1022
					/*
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1023
					 * There was an error in parsing so
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1024
					 * deal with it by issuing an error
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1025
					 * message and leaving after
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1026
					 * uninitializing the the libshare
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1027
					 * interface.
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1028
					 */
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1029
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1030
					    "'%s' cannot be set to invalid "
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1031
					    "options"), propname);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1032
					(void) zfs_error(hdl, EZFS_BADPROP,
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1033
					    errbuf);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1034
					zfs_uninit_libshare(hdl);
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1035
					goto error;
3411a82f178f 6552521 truss dumps core with multibyte characters in syscall args
eschrock
parents: 4183
diff changeset
  1036
				}
4180
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1037
				zfs_uninit_libshare(hdl);
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1038
			}
30b15f0bd3c9 6491973 sharemgr: zfs set sharenfs=on is slow with lots of zfs filesystems
dougm
parents: 4007
diff changeset
  1039
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
  1040
			break;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1041
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4217
diff changeset
  1042
		case ZPOOL_PROP_BOOTFS:
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1043
			/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1044
			 * bootfs property value has to be a dataset name and
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1045
			 * the dataset has to be in the same pool as it sets to.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1046
			 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1047
			if (strval[0] != '\0' && (!zfs_name_valid(strval,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1048
			    ZFS_TYPE_FILESYSTEM) || !bootfs_poolname_valid(
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1049
			    pool_name, strval))) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1050
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1051
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1052
				    "is an invalid name"), strval);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1053
				(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1054
				goto error;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1055
			}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1056
			break;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1057
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1058
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1059
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1060
		 * For changes to existing volumes, we have some additional
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1061
		 * checks to enforce.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1062
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1063
		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1064
			uint64_t volsize = zfs_prop_get_int(zhp,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1065
			    ZFS_PROP_VOLSIZE);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1066
			uint64_t blocksize = zfs_prop_get_int(zhp,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1067
			    ZFS_PROP_VOLBLOCKSIZE);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1068
			char buf[64];
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1069
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1070
			switch (prop) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1071
			case ZFS_PROP_RESERVATION:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1072
				if (intval > volsize) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1073
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1074
					    "'%s' is greater than current "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1075
					    "volume size"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1076
					(void) zfs_error(hdl, EZFS_BADPROP,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1077
					    errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1078
					goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1079
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1080
				break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1081
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1082
			case ZFS_PROP_VOLSIZE:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1083
				if (intval % blocksize != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1084
					zfs_nicenum(blocksize, buf,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1085
					    sizeof (buf));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1086
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1087
					    "'%s' must be a multiple of "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1088
					    "volume block size (%s)"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1089
					    propname, buf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1090
					(void) zfs_error(hdl, EZFS_BADPROP,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1091
					    errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1092
					goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1093
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1094
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1095
				if (intval == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1096
					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1097
					    "'%s' cannot be zero"),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1098
					    propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1099
					(void) zfs_error(hdl, EZFS_BADPROP,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1100
					    errbuf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1101
					goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1102
				}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
  1103
				break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1106
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1107
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1108
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1109
	 * If this is an existing volume, and someone is setting the volsize,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1110
	 * make sure that it matches the reservation, or add it if necessary.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1111
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1112
	if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1113
	    nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1114
	    &intval) == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1115
		uint64_t old_volsize = zfs_prop_get_int(zhp,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1116
		    ZFS_PROP_VOLSIZE);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1117
		uint64_t old_reservation = zfs_prop_get_int(zhp,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1118
		    ZFS_PROP_RESERVATION);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1119
		uint64_t new_reservation;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1120
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1121
		if (old_volsize == old_reservation &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1122
		    nvlist_lookup_uint64(ret,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1123
		    zfs_prop_to_name(ZFS_PROP_RESERVATION),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1124
		    &new_reservation) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1125
			if (nvlist_add_uint64(ret,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1126
			    zfs_prop_to_name(ZFS_PROP_RESERVATION),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1127
			    intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1128
				(void) no_memory(hdl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1129
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1130
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1131
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1132
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1133
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1134
	return (ret);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1135
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1136
error:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1137
	nvlist_free(ret);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1138
	return (NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1141
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1142
zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1143
    uint64_t *ret_who)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1144
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1145
	struct passwd *pwd;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1146
	struct group *grp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1147
	uid_t id;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1148
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1149
	if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE ||
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1150
	    *who_type == ZFS_DELEG_NAMED_SET) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1151
		*ret_who = -1;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1152
		return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1153
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1154
	if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE))
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1155
		return (EZFS_BADWHO);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1156
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1157
	if (*who_type == ZFS_DELEG_WHO_UNKNOWN &&
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1158
	    strcmp(who, "everyone") == 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1159
		*ret_who = -1;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1160
		*who_type = ZFS_DELEG_EVERYONE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1161
		return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1162
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1163
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1164
	pwd = getpwnam(who);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1165
	grp = getgrnam(who);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1166
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1167
	if ((*who_type == ZFS_DELEG_USER) && pwd) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1168
		*ret_who = pwd->pw_uid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1169
	} else if ((*who_type == ZFS_DELEG_GROUP) && grp) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1170
		*ret_who = grp->gr_gid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1171
	} else if (pwd) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1172
		*ret_who = pwd->pw_uid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1173
		*who_type = ZFS_DELEG_USER;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1174
	} else if (grp) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1175
		*ret_who = grp->gr_gid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1176
		*who_type = ZFS_DELEG_GROUP;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1177
	} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1178
		char *end;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1179
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1180
		id = strtol(who, &end, 10);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1181
		if (errno != 0 || *end != '\0') {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1182
			return (EZFS_BADWHO);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1183
		} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1184
			*ret_who = id;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1185
			if (*who_type == ZFS_DELEG_WHO_UNKNOWN)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1186
				*who_type = ZFS_DELEG_USER;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1187
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1188
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1189
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1190
	return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1191
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1192
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1193
static void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1194
zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1195
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1196
	if (perms_nvp != NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1197
		verify(nvlist_add_nvlist(who_nvp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1198
		    name, perms_nvp) == 0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1199
	} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1200
		verify(nvlist_add_boolean(who_nvp, name) == 0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1201
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1202
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1203
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1204
static void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1205
helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1206
    zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1207
    nvlist_t *sets_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1208
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1209
	boolean_t do_perms, do_sets;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1210
	char name[ZFS_MAX_DELEG_NAME];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1211
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1212
	do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1213
	do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1214
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1215
	if (!do_perms && !do_sets)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1216
		do_perms = do_sets = B_TRUE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1217
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1218
	if (do_perms) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1219
		zfs_deleg_whokey(name, who_type, inherit,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1220
		    (who_type == ZFS_DELEG_NAMED_SET) ?
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1221
		    whostr : (void *)&whoid);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1222
		zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1223
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1224
	if (do_sets) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1225
		zfs_deleg_whokey(name, toupper(who_type), inherit,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1226
		    (who_type == ZFS_DELEG_NAMED_SET) ?
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1227
		    whostr : (void *)&whoid);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1228
		zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1229
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1230
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1231
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1232
static void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1233
zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1234
    nvlist_t *perms_nvp, nvlist_t *sets_nvp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1235
    zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1236
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1237
	if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1238
		helper(who_type, whoid, whostr, 0,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1239
		    who_nvp, perms_nvp, sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1240
	} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1241
		if (inherit & ZFS_DELEG_PERM_LOCAL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1242
			helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1243
			    who_nvp, perms_nvp, sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1244
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1245
		if (inherit & ZFS_DELEG_PERM_DESCENDENT) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1246
			helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1247
			    who_nvp, perms_nvp, sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1248
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1249
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1250
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1251
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1252
/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1253
 * Construct nvlist to pass down to kernel for setting/removing permissions.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1254
 *
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1255
 * The nvlist is constructed as a series of nvpairs with an optional embedded
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1256
 * nvlist of permissions to remove or set.  The topmost nvpairs are the actual
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1257
 * base attribute named stored in the dsl.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1258
 * Arguments:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1259
 *
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1260
 * whostr:   is a comma separated list of users, groups, or a single set name.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1261
 *           whostr may be null for everyone or create perms.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1262
 * who_type: is the type of entry in whostr.  Typically this will be
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1263
 *           ZFS_DELEG_WHO_UNKNOWN.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1264
 * perms:    comman separated list of permissions.  May be null if user
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1265
 *           is requested to remove permissions by who.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1266
 * inherit:  Specifies the inheritance of the permissions.  Will be either
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1267
 *           ZFS_DELEG_PERM_LOCAL and/or  ZFS_DELEG_PERM_DESCENDENT.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1268
 * nvp       The constructed nvlist to pass to zfs_perm_set().
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1269
 *           The output nvp will look something like this.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1270
 *              ul$1234 -> {create ; destroy }
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1271
 *              Ul$1234 -> { @myset }
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1272
 *              s-$@myset - { snapshot; checksum; compression }
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1273
 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1274
int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1275
zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1276
    zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1277
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1278
	nvlist_t *who_nvp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1279
	nvlist_t *perms_nvp = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1280
	nvlist_t *sets_nvp = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1281
	char errbuf[1024];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1282
	char *who_tok;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1283
	int error;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1284
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1285
	*nvp = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1286
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1287
	if (perms) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1288
		/* Make sure permission string doesn't have an '=' sign in it */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1289
		if (strchr(perms, '=') != NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1290
			(void) snprintf(errbuf, sizeof (errbuf),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1291
			    dgettext(TEXT_DOMAIN,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1292
			    "permissions can't contain equal sign : '%s'"),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1293
			    perms);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1294
			return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, errbuf));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1295
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1296
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1297
		if ((error = nvlist_alloc(&perms_nvp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1298
		    NV_UNIQUE_NAME, 0)) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1299
			return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1300
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1301
		if ((error = nvlist_alloc(&sets_nvp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1302
		    NV_UNIQUE_NAME, 0)) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1303
			nvlist_free(perms_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1304
			return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1305
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1306
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1307
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1308
	if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1309
		if (perms_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1310
			nvlist_free(perms_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1311
		if (sets_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1312
			nvlist_free(sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1313
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1314
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1315
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1316
	if (who_type == ZFS_DELEG_NAMED_SET) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1317
		namecheck_err_t why;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1318
		char what;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1319
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1320
		if ((error = permset_namecheck(whostr, &why, &what)) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1321
			switch (why) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1322
			case NAME_ERR_NO_AT:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1323
				zfs_error_aux(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1324
				    dgettext(TEXT_DOMAIN,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1325
				    "set definition must begin with an '@' "
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1326
				    "character"));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1327
			}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1328
			return (zfs_error(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1329
			    EZFS_BADPERMSET, whostr));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1330
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1331
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1332
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1333
	/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1334
	 * Build up nvlist(s) of permissions.  Two nvlists are maintained.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1335
	 * The first nvlist perms_nvp will have normal permissions and the
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1336
	 * other sets_nvp will have only permssion set names in it.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1337
	 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1338
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1339
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1340
	while (perms && *perms != '\0') {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1341
		char *value;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1342
		char *perm_name;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1343
		nvlist_t *update_nvp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1344
		int  perm_num;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1345
		char canonical_name[64];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1346
		char *canonicalp = canonical_name;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1347
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1348
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1349
		update_nvp = perms_nvp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1350
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1351
		perm_num = getsubopt(&perms, zfs_deleg_perm_tab, &value);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1352
		if (perm_num == -1) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1353
			zfs_prop_t prop;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1354
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1355
			prop = zfs_name_to_prop(value);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1356
			if (prop != ZFS_PROP_INVAL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1357
				(void) snprintf(canonical_name,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1358
				    sizeof (canonical_name), "%s",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1359
				    zfs_prop_to_name(prop));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1360
				perm_num = getsubopt(&canonicalp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1361
				    zfs_deleg_perm_tab, &value);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1362
			}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1363
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1364
		if (perm_num != -1) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1365
			perm_name = zfs_deleg_perm_tab[perm_num];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1366
		} else {  /* check and see if permission is a named set */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1367
			if (value[0] == '@') {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1368
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1369
				/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1370
				 * make sure permssion set isn't defined
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1371
				 * in terms of itself. ie.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1372
				 * @set1 = create,destroy,@set1
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1373
				 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1374
				if (who_type == ZFS_DELEG_NAMED_SET &&
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1375
				    strcmp(value, whostr) == 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1376
					nvlist_free(who_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1377
					nvlist_free(perms_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1378
					if (sets_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1379
						nvlist_free(sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1380
					(void) snprintf(errbuf,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1381
					    sizeof (errbuf),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1382
					    dgettext(TEXT_DOMAIN,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1383
					    "Invalid permission %s"), value);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1384
					return (zfs_error(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1385
					    EZFS_PERMSET_CIRCULAR, errbuf));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1386
				}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1387
				update_nvp = sets_nvp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1388
				perm_name = value;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1389
			} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1390
				nvlist_free(who_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1391
				nvlist_free(perms_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1392
				if (sets_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1393
					nvlist_free(sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1394
				return (zfs_error(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1395
				    EZFS_BADPERM, value));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1396
			}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1397
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1398
		verify(nvlist_add_boolean(update_nvp, perm_name) == 0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1399
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1400
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1401
	if (whostr && who_type != ZFS_DELEG_CREATE) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1402
		who_tok = strtok(whostr, ",");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1403
		if (who_tok == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1404
			nvlist_free(who_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1405
			nvlist_free(perms_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1406
			if (sets_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1407
				nvlist_free(sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1408
			(void) snprintf(errbuf, sizeof (errbuf),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1409
			    dgettext(TEXT_DOMAIN, "Who string is NULL"),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1410
			    whostr);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1411
			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1412
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1413
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1414
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1415
	/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1416
	 * Now create the nvlist(s)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1417
	 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1418
	do {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1419
		uint64_t who_id;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1420
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1421
		error = zfs_get_perm_who(who_tok, &who_type,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1422
		    &who_id);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1423
		if (error) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1424
			nvlist_free(who_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1425
			nvlist_free(perms_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1426
			if (sets_nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1427
				nvlist_free(sets_nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1428
			(void) snprintf(errbuf, sizeof (errbuf),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1429
			    dgettext(TEXT_DOMAIN,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1430
			    "Unable to determine uid/gid for "
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1431
			    "%s "), who_tok);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1432
			return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1433
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1434
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1435
		/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1436
		 * add entries for both local and descendent when required
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1437
		 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1438
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1439
		zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1440
		    perms_nvp, sets_nvp, who_type, inherit);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1441
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1442
	} while (who_tok = strtok(NULL, ","));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1443
	*nvp = who_nvp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1444
	return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1445
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1446
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1447
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1448
zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1449
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1450
	zfs_cmd_t zc = { 0 };
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1451
	int error;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1452
	size_t sz;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1453
	char errbuf[1024];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1454
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1455
	(void) snprintf(errbuf, sizeof (errbuf),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1456
	    dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1457
	    zhp->zfs_name);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1458
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1459
	if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp, &sz))
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1460
		return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1461
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1462
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1463
	zc.zc_perm_action = unset;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1464
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1465
	error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1466
	if (error && errno == ENOTSUP) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1467
		(void) snprintf(errbuf, sizeof (errbuf),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1468
		    gettext("Pool must be upgraded to use 'allow/unallow'"));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1469
		zcmd_free_nvlists(&zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1470
		return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1471
	} else if (error) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1472
		return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1473
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1474
	zcmd_free_nvlists(&zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1475
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1476
	return (error);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1477
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1478
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1479
int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1480
zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1481
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1482
	return (zfs_perm_set_common(zhp, nvp, B_FALSE));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1483
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1484
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1485
int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1486
zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1487
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1488
	return (zfs_perm_set_common(zhp, perms, B_TRUE));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1489
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1490
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1491
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1492
perm_compare(const void *arg1, const void *arg2)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1493
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1494
	const zfs_perm_node_t *node1 = arg1;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1495
	const zfs_perm_node_t *node2 = arg2;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1496
	int ret;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1497
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1498
	ret = strcmp(node1->z_pname, node2->z_pname);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1499
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1500
	if (ret > 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1501
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1502
	if (ret < 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1503
		return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1504
	else
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1505
		return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1506
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1507
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1508
static void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1509
zfs_destroy_perm_tree(avl_tree_t *tree)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1510
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1511
	zfs_perm_node_t *permnode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1512
	void *cookie;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1513
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1514
	cookie = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1515
	while ((permnode = avl_destroy_nodes(tree,  &cookie)) != NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1516
		avl_remove(tree, permnode);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1517
		free(permnode);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1518
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1519
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1520
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1521
static void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1522
zfs_destroy_tree(avl_tree_t *tree)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1523
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1524
	zfs_allow_node_t *allownode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1525
	void *cookie;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1526
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1527
	cookie = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1528
	while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1529
		zfs_destroy_perm_tree(&allownode->z_localdescend);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1530
		zfs_destroy_perm_tree(&allownode->z_local);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1531
		zfs_destroy_perm_tree(&allownode->z_descend);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1532
		avl_remove(tree, allownode);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1533
		free(allownode);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1534
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1535
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1536
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1537
void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1538
zfs_free_allows(zfs_allow_t *allow)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1539
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1540
	zfs_allow_t *allownext;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1541
	zfs_allow_t *freeallow;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1542
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1543
	allownext = allow;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1544
	while (allownext) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1545
		zfs_destroy_tree(&allownext->z_sets);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1546
		zfs_destroy_tree(&allownext->z_crperms);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1547
		zfs_destroy_tree(&allownext->z_user);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1548
		zfs_destroy_tree(&allownext->z_group);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1549
		zfs_destroy_tree(&allownext->z_everyone);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1550
		freeallow = allownext;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1551
		allownext = allownext->z_next;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1552
		free(freeallow);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1553
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1554
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1555
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1556
static zfs_allow_t *
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1557
zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1558
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1559
	zfs_allow_t *ptree;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1560
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1561
	if ((ptree = zfs_alloc(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1562
	    sizeof (zfs_allow_t))) == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1563
		return (NULL);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1564
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1565
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1566
	(void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1567
	avl_create(&ptree->z_sets,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1568
	    perm_compare, sizeof (zfs_allow_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1569
	    offsetof(zfs_allow_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1570
	avl_create(&ptree->z_crperms,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1571
	    perm_compare, sizeof (zfs_allow_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1572
	    offsetof(zfs_allow_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1573
	avl_create(&ptree->z_user,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1574
	    perm_compare, sizeof (zfs_allow_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1575
	    offsetof(zfs_allow_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1576
	avl_create(&ptree->z_group,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1577
	    perm_compare, sizeof (zfs_allow_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1578
	    offsetof(zfs_allow_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1579
	avl_create(&ptree->z_everyone,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1580
	    perm_compare, sizeof (zfs_allow_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1581
	    offsetof(zfs_allow_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1582
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1583
	if (prev)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1584
		prev->z_next = ptree;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1585
	ptree->z_next = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1586
	return (ptree);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1587
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1588
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1589
/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1590
 * Add permissions to the appropriate AVL permission tree.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1591
 * The appropriate tree may not be the requested tree.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1592
 * For example if ld indicates a local permission, but
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1593
 * same permission also exists as a descendent permission
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1594
 * then the permission will be removed from the descendent
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1595
 * tree and add the the local+descendent tree.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1596
 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1597
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1598
zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1599
    char *perm, char ld)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1600
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1601
	zfs_perm_node_t pnode, *permnode, *permnode2;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1602
	zfs_perm_node_t *newnode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1603
	avl_index_t where, where2;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1604
	avl_tree_t *tree, *altree;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1605
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1606
	(void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1607
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1608
	if (ld == ZFS_DELEG_NA) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1609
		tree =  &allownode->z_localdescend;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1610
		altree = &allownode->z_descend;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1611
	} else if (ld == ZFS_DELEG_LOCAL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1612
		tree = &allownode->z_local;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1613
		altree = &allownode->z_descend;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1614
	} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1615
		tree = &allownode->z_descend;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1616
		altree = &allownode->z_local;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1617
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1618
	permnode = avl_find(tree, &pnode, &where);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1619
	permnode2 = avl_find(altree, &pnode, &where2);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1620
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1621
	if (permnode2) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1622
		avl_remove(altree, permnode2);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1623
		free(permnode2);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1624
		if (permnode == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1625
			tree =  &allownode->z_localdescend;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1626
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1627
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1628
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1629
	/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1630
	 * Now insert new permission in either requested location
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1631
	 * local/descendent or into ld when perm will exist in both.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1632
	 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1633
	if (permnode == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1634
		if ((newnode = zfs_alloc(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1635
		    sizeof (zfs_perm_node_t))) == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1636
			return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1637
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1638
		*newnode = pnode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1639
		avl_add(tree, newnode);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1640
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1641
	return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1642
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1643
/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1644
 * Uggh, this is going to be a bit complicated.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1645
 * we have an nvlist coming out of the kernel that
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1646
 * will indicate where the permission is set and then
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1647
 * it will contain allow of the various "who's", and what
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1648
 * their permissions are.  To further complicate this
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1649
 * we will then have to coalesce the local,descendent
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1650
 * and local+descendent permissions where appropriate.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1651
 * The kernel only knows about a permission as being local
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1652
 * or descendent, but not both.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1653
 *
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1654
 * In order to make this easier for zfs_main to deal with
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1655
 * a series of AVL trees will be used to maintain
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1656
 * all of this, primarily for sorting purposes as well
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1657
 * as the ability to quickly locate a specific entry.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1658
 *
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1659
 * What we end up with are tree's for sets, create perms,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1660
 * user, groups and everyone.  With each of those trees
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1661
 * we have subtrees for local, descendent and local+descendent
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1662
 * permissions.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1663
 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1664
int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1665
zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1666
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1667
	zfs_cmd_t zc = { 0 };
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1668
	int error;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1669
	nvlist_t *nvlist;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1670
	nvlist_t *permnv, *sourcenv;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1671
	nvpair_t *who_pair, *source_pair;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1672
	nvpair_t *perm_pair;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1673
	char errbuf[1024];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1674
	zfs_allow_t *zallowp, *newallowp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1675
	char  ld;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1676
	char *nvpname;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1677
	uid_t	uid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1678
	gid_t	gid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1679
	avl_tree_t *tree;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1680
	avl_index_t where;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1681
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1682
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1683
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1684
	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1685
		return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1686
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1687
	while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1688
		if (errno == ENOMEM) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1689
			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1690
				zcmd_free_nvlists(&zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1691
				return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1692
			}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1693
		} else if (errno == ENOTSUP) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1694
			zcmd_free_nvlists(&zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1695
			(void) snprintf(errbuf, sizeof (errbuf),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1696
			    gettext("Pool must be upgraded to use 'allow'"));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1697
			return (zfs_error(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1698
			    EZFS_BADVERSION, errbuf));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1699
		} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1700
			zcmd_free_nvlists(&zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1701
			return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1702
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1703
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1704
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1705
	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1706
		zcmd_free_nvlists(&zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1707
		return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1708
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1709
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1710
	zcmd_free_nvlists(&zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1711
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1712
	source_pair = nvlist_next_nvpair(nvlist, NULL);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1713
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1714
	if (source_pair == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1715
		*zfs_perms = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1716
		return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1717
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1718
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1719
	*zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1720
	if (*zfs_perms == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1721
		return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1722
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1723
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1724
	zallowp = *zfs_perms;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1725
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1726
	for (;;) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1727
		struct passwd *pwd;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1728
		struct group *grp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1729
		zfs_allow_node_t *allownode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1730
		zfs_allow_node_t  findallownode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1731
		zfs_allow_node_t *newallownode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1732
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1733
		(void) strlcpy(zallowp->z_setpoint,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1734
		    nvpair_name(source_pair),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1735
		    sizeof (zallowp->z_setpoint));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1736
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1737
		if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1738
			goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1739
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1740
		/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1741
		 * Make sure nvlist is composed correctly
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1742
		 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1743
		if (zfs_deleg_verify_nvlist(sourcenv)) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1744
			goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1745
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1746
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1747
		who_pair = nvlist_next_nvpair(sourcenv, NULL);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1748
		if (who_pair == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1749
			goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1750
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1751
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1752
		do {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1753
			error = nvpair_value_nvlist(who_pair, &permnv);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1754
			if (error) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1755
				goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1756
			}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1757
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1758
			/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1759
			 * First build up the key to use
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1760
			 * for looking up in the various
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1761
			 * who trees.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1762
			 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1763
			ld = nvpair_name(who_pair)[1];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1764
			nvpname = nvpair_name(who_pair);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1765
			switch (nvpair_name(who_pair)[0]) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1766
			case ZFS_DELEG_USER:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1767
			case ZFS_DELEG_USER_SETS:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1768
				tree = &zallowp->z_user;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1769
				uid = atol(&nvpname[3]);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1770
				pwd = getpwuid(uid);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1771
				(void) snprintf(findallownode.z_key,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1772
				    sizeof (findallownode.z_key), "user %s",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1773
				    (pwd) ? pwd->pw_name :
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1774
				    &nvpair_name(who_pair)[3]);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1775
				break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1776
			case ZFS_DELEG_GROUP:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1777
			case ZFS_DELEG_GROUP_SETS:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1778
				tree = &zallowp->z_group;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1779
				gid = atol(&nvpname[3]);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1780
				grp = getgrgid(gid);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1781
				(void) snprintf(findallownode.z_key,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1782
				    sizeof (findallownode.z_key), "group %s",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1783
				    (grp) ? grp->gr_name :
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1784
				    &nvpair_name(who_pair)[3]);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1785
				break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1786
			case ZFS_DELEG_CREATE:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1787
			case ZFS_DELEG_CREATE_SETS:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1788
				tree = &zallowp->z_crperms;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1789
				(void) strlcpy(findallownode.z_key, "",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1790
				    sizeof (findallownode.z_key));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1791
				break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1792
			case ZFS_DELEG_EVERYONE:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1793
			case ZFS_DELEG_EVERYONE_SETS:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1794
				(void) snprintf(findallownode.z_key,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1795
				    sizeof (findallownode.z_key), "everyone");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1796
				tree = &zallowp->z_everyone;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1797
				break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1798
			case ZFS_DELEG_NAMED_SET:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1799
			case ZFS_DELEG_NAMED_SET_SETS:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1800
				(void) snprintf(findallownode.z_key,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1801
				    sizeof (findallownode.z_key), "%s",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1802
				    &nvpair_name(who_pair)[3]);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1803
				tree = &zallowp->z_sets;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1804
				break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1805
			}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1806
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1807
			/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1808
			 * Place who in tree
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1809
			 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1810
			allownode = avl_find(tree, &findallownode, &where);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1811
			if (allownode == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1812
				if ((newallownode = zfs_alloc(zhp->zfs_hdl,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1813
				    sizeof (zfs_allow_node_t))) == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1814
					goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1815
				}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1816
				avl_create(&newallownode->z_localdescend,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1817
				    perm_compare,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1818
				    sizeof (zfs_perm_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1819
				    offsetof(zfs_perm_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1820
				avl_create(&newallownode->z_local,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1821
				    perm_compare,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1822
				    sizeof (zfs_perm_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1823
				    offsetof(zfs_perm_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1824
				avl_create(&newallownode->z_descend,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1825
				    perm_compare,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1826
				    sizeof (zfs_perm_node_t),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1827
				    offsetof(zfs_perm_node_t, z_node));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1828
				(void) strlcpy(newallownode->z_key,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1829
				    findallownode.z_key,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1830
				    sizeof (findallownode.z_key));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1831
				avl_insert(tree, newallownode, where);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1832
				allownode = newallownode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1833
			}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1834
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1835
			/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1836
			 * Now iterate over the permissions and
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1837
			 * place them in the appropriate local,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1838
			 * descendent or local+descendent tree.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1839
			 *
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1840
			 * The permissions are added to the tree
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1841
			 * via zfs_coalesce_perm().
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1842
			 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1843
			perm_pair = nvlist_next_nvpair(permnv, NULL);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1844
			if (perm_pair == NULL)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1845
				goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1846
			do {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1847
				if (zfs_coalesce_perm(zhp, allownode,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1848
				    nvpair_name(perm_pair), ld) != 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1849
					goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1850
			} while (perm_pair = nvlist_next_nvpair(permnv,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1851
			    perm_pair));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1852
		} while (who_pair = nvlist_next_nvpair(sourcenv, who_pair));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1853
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1854
		source_pair = nvlist_next_nvpair(nvlist, source_pair);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1855
		if (source_pair == NULL)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1856
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1857
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1858
		/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1859
		 * allocate another node from the link list of
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1860
		 * zfs_allow_t structures
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1861
		 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1862
		newallowp = zfs_alloc_perm_tree(zhp, zallowp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1863
		    nvpair_name(source_pair));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1864
		if (newallowp == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1865
			goto abort;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1866
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1867
		zallowp = newallowp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1868
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1869
	nvlist_free(nvlist);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1870
	return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1871
abort:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1872
	zfs_free_allows(*zfs_perms);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1873
	nvlist_free(nvlist);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1874
	return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1875
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1876
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1877
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1878
 * Given a property name and value, set the property for the given dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1879
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1880
int
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1881
zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1882
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1883
	zfs_cmd_t zc = { 0 };
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1884
	int ret = -1;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1885
	prop_changelist_t *cl = NULL;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1886
	char errbuf[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1887
	libzfs_handle_t *hdl = zhp->zfs_hdl;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1888
	nvlist_t *nvl = NULL, *realprops;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1889
	zfs_prop_t prop;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1890
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1891
	(void) snprintf(errbuf, sizeof (errbuf),
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1892
	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1893
	    zhp->zfs_name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1894
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1895
	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1896
	    nvlist_add_string(nvl, propname, propval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1897
		(void) no_memory(hdl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1898
		goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1899
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1900
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  1901
	if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, NULL, nvl,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1902
	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1903
		goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1904
	nvlist_free(nvl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1905
	nvl = realprops;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1906
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1907
	prop = zfs_name_to_prop(propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1908
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1909
	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1910
		goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1911
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1912
	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1913
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1914
		    "child dataset with inherited mountpoint is used "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1915
		    "in a non-global zone"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1916
		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1917
		goto error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1918
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1919
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1920
	if ((ret = changelist_prefix(cl)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1921
		goto error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1922
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1923
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1924
	 * Execute the corresponding ioctl() to set this property.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1925
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1926
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1927
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1928
	if (zcmd_write_src_nvlist(hdl, &zc, nvl, NULL) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1929
		goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1930
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1931
	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1932
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
	if (ret != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1934
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1936
		case ENOSPC:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1938
			 * For quotas and reservations, ENOSPC indicates
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1939
			 * something different; setting a quota or reservation
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1940
			 * doesn't use any disk space.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1941
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1942
			switch (prop) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1943
			case ZFS_PROP_QUOTA:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1944
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1945
				    "size is less than current used or "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1946
				    "reserved space"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1947
				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1948
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1949
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1950
			case ZFS_PROP_RESERVATION:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1951
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1952
				    "size is greater than available space"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1953
				(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1954
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1955
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1956
			default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1957
				(void) zfs_standard_error(hdl, errno, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1958
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1959
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1960
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1961
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1962
		case EBUSY:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1963
			if (prop == ZFS_PROP_VOLBLOCKSIZE)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1964
				(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1965
			else
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1966
				(void) zfs_standard_error(hdl, EBUSY, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1967
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1968
1175
759d20c7e57b 6366265 attach/replace should allow a new device size at least the min of all devs in a mirror/raidz
lling
parents: 1133
diff changeset
  1969
		case EROFS:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1970
			(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1175
759d20c7e57b 6366265 attach/replace should allow a new device size at least the min of all devs in a mirror/raidz
lling
parents: 1133
diff changeset
  1971
			break;
759d20c7e57b 6366265 attach/replace should allow a new device size at least the min of all devs in a mirror/raidz
lling
parents: 1133
diff changeset
  1972
3886
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3835
diff changeset
  1973
		case ENOTSUP:
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3835
diff changeset
  1974
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3835
diff changeset
  1975
			    "pool must be upgraded to allow gzip compression"));
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3835
diff changeset
  1976
			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3835
diff changeset
  1977
			break;
3291401d66a6 6536602 add zlib compression support and upgrade to version 1.2.3
ahl
parents: 3835
diff changeset
  1978
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1979
		case EOVERFLOW:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1980
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1981
			 * This platform can't address a volume this big.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1982
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1983
#ifdef _ILP32
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1984
			if (prop == ZFS_PROP_VOLSIZE) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1985
				(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1986
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1987
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1988
#endif
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1989
			/* FALLTHROUGH */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1990
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  1991
			(void) zfs_standard_error(hdl, errno, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1992
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1993
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1994
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1995
		 * Refresh the statistics so the new property value
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1996
		 * is reflected.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1997
		 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1998
		if ((ret = changelist_postfix(cl)) == 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1999
			(void) get_stats(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2000
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2001
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2002
error:
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2003
	nvlist_free(nvl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2004
	zcmd_free_nvlists(&zc);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2005
	if (cl)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2006
		changelist_free(cl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2007
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2008
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2009
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2010
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2011
 * Given a property, inherit the value from the parent dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2012
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2013
int
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2014
zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2015
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2016
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2017
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2018
	prop_changelist_t *cl;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2019
	libzfs_handle_t *hdl = zhp->zfs_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2020
	char errbuf[1024];
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2021
	zfs_prop_t prop;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2022
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2023
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2024
	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2025
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2026
	if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2027
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2028
		 * For user properties, the amount of work we have to do is very
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2029
		 * small, so just do it here.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2030
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2031
		if (!zfs_prop_user(propname)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2032
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2033
			    "invalid property"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2034
			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2035
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2036
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2037
		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2038
		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2039
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2040
		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2041
			return (zfs_standard_error(hdl, errno, errbuf));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2042
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2043
		return (0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2044
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2045
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2046
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2047
	 * Verify that this property is inheritable.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2048
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2049
	if (zfs_prop_readonly(prop))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2050
		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2051
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2052
	if (!zfs_prop_inheritable(prop))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2053
		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2054
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2055
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2056
	 * Check to see if the value applies to this type
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2057
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2058
	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2059
		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2060
3443
e0e00ef6cee8 6494654 zfs inherit can't handle property short names
rm160521
parents: 3417
diff changeset
  2061
	/*
e0e00ef6cee8 6494654 zfs inherit can't handle property short names
rm160521
parents: 3417
diff changeset
  2062
	 * Normalize the name, to get rid of shorthand abbrevations.
e0e00ef6cee8 6494654 zfs inherit can't handle property short names
rm160521
parents: 3417
diff changeset
  2063
	 */
e0e00ef6cee8 6494654 zfs inherit can't handle property short names
rm160521
parents: 3417
diff changeset
  2064
	propname = zfs_prop_to_name(prop);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2065
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2066
	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2067
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2068
	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2069
	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2070
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2071
		    "dataset is used in a non-global zone"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2072
		return (zfs_error(hdl, EZFS_ZONED, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2073
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2074
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2075
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2076
	 * Determine datasets which will be affected by this change, if any.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2077
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2078
	if ((cl = changelist_gather(zhp, prop, 0)) == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2079
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2080
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2081
	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2082
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2083
		    "child dataset with inherited mountpoint is used "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2084
		    "in a non-global zone"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2085
		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2086
		goto error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2087
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2088
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2089
	if ((ret = changelist_prefix(cl)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2090
		goto error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2091
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2092
	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2093
		return (zfs_standard_error(hdl, errno, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2094
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2095
2169
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2166
diff changeset
  2096
		if ((ret = changelist_postfix(cl)) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2097
			goto error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2098
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2099
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2100
		 * Refresh the statistics so the new property is reflected.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2101
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2102
		(void) get_stats(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2103
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2104
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2105
error:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2106
	changelist_free(cl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2107
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2108
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2109
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2110
void
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2111
nicebool(int value, char *buf, size_t buflen)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2112
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2113
	if (value)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2114
		(void) strlcpy(buf, "on", buflen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2115
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2116
		(void) strlcpy(buf, "off", buflen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2117
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2118
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2119
/*
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2120
 * True DSL properties are stored in an nvlist.  The following two functions
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2121
 * extract them appropriately.
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2122
 */
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2123
static uint64_t
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2124
getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2125
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2126
	nvlist_t *nv;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2127
	uint64_t value;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2128
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2129
	*source = NULL;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2130
	if (nvlist_lookup_nvlist(zhp->zfs_props,
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2131
	    zfs_prop_to_name(prop), &nv) == 0) {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2132
		verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0);
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2133
		(void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2134
	} else {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2135
		value = zfs_prop_default_numeric(prop);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2136
		*source = "";
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2137
	}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2138
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2139
	return (value);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2140
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2141
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2142
static char *
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2143
getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2144
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2145
	nvlist_t *nv;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2146
	char *value;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2147
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2148
	*source = NULL;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2149
	if (nvlist_lookup_nvlist(zhp->zfs_props,
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2150
	    zfs_prop_to_name(prop), &nv) == 0) {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2151
		verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0);
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2152
		(void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2153
	} else {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2154
		if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2155
			value = "";
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2156
		*source = "";
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2157
	}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2158
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2159
	return (value);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2160
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2161
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2162
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2163
 * Internal function for getting a numeric property.  Both zfs_prop_get() and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2164
 * zfs_prop_get_int() are built using this interface.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2165
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2166
 * Certain properties can be overridden using 'mount -o'.  In this case, scan
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2167
 * the contents of the /etc/mnttab entry, searching for the appropriate options.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2168
 * If they differ from the on-disk values, report the current values and mark
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2169
 * the source "temporary".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2170
 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2171
static int
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2172
get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2173
    char **source, uint64_t *val)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2174
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2175
	struct mnttab mnt;
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2176
	char *mntopt_on = NULL;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2177
	char *mntopt_off = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2178
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2179
	*source = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2180
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2181
	switch (prop) {
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2182
	case ZFS_PROP_ATIME:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2183
		mntopt_on = MNTOPT_ATIME;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2184
		mntopt_off = MNTOPT_NOATIME;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2185
		break;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2186
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2187
	case ZFS_PROP_DEVICES:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2188
		mntopt_on = MNTOPT_DEVICES;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2189
		mntopt_off = MNTOPT_NODEVICES;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2190
		break;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2191
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2192
	case ZFS_PROP_EXEC:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2193
		mntopt_on = MNTOPT_EXEC;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2194
		mntopt_off = MNTOPT_NOEXEC;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2195
		break;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2196
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2197
	case ZFS_PROP_READONLY:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2198
		mntopt_on = MNTOPT_RO;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2199
		mntopt_off = MNTOPT_RW;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2200
		break;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2201
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2202
	case ZFS_PROP_SETUID:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2203
		mntopt_on = MNTOPT_SETUID;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2204
		mntopt_off = MNTOPT_NOSETUID;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2205
		break;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2206
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2207
	case ZFS_PROP_XATTR:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2208
		mntopt_on = MNTOPT_XATTR;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2209
		mntopt_off = MNTOPT_NOXATTR;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2210
		break;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2211
	}
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2212
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2213
	/*
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2214
	 * Because looking up the mount options is potentially expensive
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2215
	 * (iterating over all of /etc/mnttab), we defer its calculation until
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2216
	 * we're looking up a property which requires its presence.
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2217
	 */
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2218
	if (!zhp->zfs_mntcheck &&
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2219
	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2220
		struct mnttab entry, search = { 0 };
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2221
		FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab;
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2222
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2223
		search.mnt_special = (char *)zhp->zfs_name;
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2224
		search.mnt_fstype = MNTTYPE_ZFS;
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2225
		rewind(mnttab);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2226
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2227
		if (getmntany(mnttab, &entry, &search) == 0) {
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2228
			zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl,
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2229
			    entry.mnt_mntopts);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2230
			if (zhp->zfs_mntopts == NULL)
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2231
				return (-1);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2232
		}
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2233
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2234
		zhp->zfs_mntcheck = B_TRUE;
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2235
	}
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  2236
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2237
	if (zhp->zfs_mntopts == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2238
		mnt.mnt_mntopts = "";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2239
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2240
		mnt.mnt_mntopts = zhp->zfs_mntopts;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2241
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2242
	switch (prop) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2243
	case ZFS_PROP_ATIME:
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2244
	case ZFS_PROP_DEVICES:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2245
	case ZFS_PROP_EXEC:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2246
	case ZFS_PROP_READONLY:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2247
	case ZFS_PROP_SETUID:
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2248
	case ZFS_PROP_XATTR:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2249
		*val = getprop_uint64(zhp, prop, source);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2250
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2251
		if (hasmntopt(&mnt, mntopt_on) && !*val) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2252
			*val = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2253
			if (src)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2254
				*src = ZFS_SRC_TEMPORARY;
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2255
		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2256
			*val = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2257
			if (src)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2258
				*src = ZFS_SRC_TEMPORARY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2259
		}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2260
		break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2261
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2262
	case ZFS_PROP_RECORDSIZE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2263
	case ZFS_PROP_COMPRESSION:
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2264
	case ZFS_PROP_ZONED:
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2265
	case ZFS_PROP_CREATION:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2266
	case ZFS_PROP_COMPRESSRATIO:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2267
	case ZFS_PROP_REFERENCED:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2268
	case ZFS_PROP_USED:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2269
	case ZFS_PROP_CREATETXG:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2270
	case ZFS_PROP_AVAILABLE:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2271
	case ZFS_PROP_VOLSIZE:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2272
	case ZFS_PROP_VOLBLOCKSIZE:
3417
0630f537a7e5 6480849 source of canmount property needs improvement
rm160521
parents: 3413
diff changeset
  2273
		*val = getprop_uint64(zhp, prop, source);
0630f537a7e5 6480849 source of canmount property needs improvement
rm160521
parents: 3413
diff changeset
  2274
		break;
0630f537a7e5 6480849 source of canmount property needs improvement
rm160521
parents: 3413
diff changeset
  2275
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  2276
	case ZFS_PROP_CANMOUNT:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2277
		*val = getprop_uint64(zhp, prop, source);
3417
0630f537a7e5 6480849 source of canmount property needs improvement
rm160521
parents: 3413
diff changeset
  2278
		if (*val == 0)
0630f537a7e5 6480849 source of canmount property needs improvement
rm160521
parents: 3413
diff changeset
  2279
			*source = zhp->zfs_name;
0630f537a7e5 6480849 source of canmount property needs improvement
rm160521
parents: 3413
diff changeset
  2280
		else
0630f537a7e5 6480849 source of canmount property needs improvement
rm160521
parents: 3413
diff changeset
  2281
			*source = "";	/* default */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2282
		break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2283
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2284
	case ZFS_PROP_QUOTA:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2285
	case ZFS_PROP_RESERVATION:
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2286
		*val = getprop_uint64(zhp, prop, source);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2287
		if (*val == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2288
			*source = "";	/* default */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2289
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2290
			*source = zhp->zfs_name;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2291
		break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2292
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2293
	case ZFS_PROP_MOUNTED:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2294
		*val = (zhp->zfs_mntopts != NULL);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2295
		break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2296
3377
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3363
diff changeset
  2297
	case ZFS_PROP_NUMCLONES:
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3363
diff changeset
  2298
		*val = zhp->zfs_dmustats.dds_num_clones;
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3363
diff changeset
  2299
		break;
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3363
diff changeset
  2300
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2301
	default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2302
		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2303
		    "cannot get non-numeric property"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2304
		return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2305
		    dgettext(TEXT_DOMAIN, "internal error")));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2306
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2307
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2308
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2309
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2310
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2311
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2312
 * Calculate the source type, given the raw source string.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2313
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2314
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2315
get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2316
    char *statbuf, size_t statlen)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2317
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2318
	if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2319
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2320
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2321
	if (source == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2322
		*srctype = ZFS_SRC_NONE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2323
	} else if (source[0] == '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2324
		*srctype = ZFS_SRC_DEFAULT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2325
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2326
		if (strcmp(source, zhp->zfs_name) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2327
			*srctype = ZFS_SRC_LOCAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2328
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2329
			(void) strlcpy(statbuf, source, statlen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2330
			*srctype = ZFS_SRC_INHERITED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2331
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2332
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2333
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2334
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2335
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2336
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2337
 * Retrieve a property from the given object.  If 'literal' is specified, then
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2338
 * numbers are left as exact values.  Otherwise, numbers are converted to a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2339
 * human-readable form.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2340
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2341
 * Returns 0 on success, or -1 on error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2342
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2343
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2344
zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2345
    zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2346
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2347
	char *source = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2348
	uint64_t val;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2349
	char *str;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2350
	const char *root;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2351
	const char *strval;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2352
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2353
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2354
	 * Check to see if this property applies to our object
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2355
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2356
	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2357
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2358
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2359
	if (src)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2360
		*src = ZFS_SRC_NONE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2361
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2362
	switch (prop) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2363
	case ZFS_PROP_ATIME:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2364
	case ZFS_PROP_READONLY:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2365
	case ZFS_PROP_SETUID:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2366
	case ZFS_PROP_ZONED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2367
	case ZFS_PROP_DEVICES:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2368
	case ZFS_PROP_EXEC:
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2369
	case ZFS_PROP_CANMOUNT:
3234
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3126
diff changeset
  2370
	case ZFS_PROP_XATTR:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2371
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2372
		 * Basic boolean values are built on top of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2373
		 * get_numeric_property().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2374
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2375
		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2376
			return (-1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2377
		nicebool(val, propbuf, proplen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2378
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2379
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2381
	case ZFS_PROP_AVAILABLE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2382
	case ZFS_PROP_RECORDSIZE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2383
	case ZFS_PROP_CREATETXG:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2384
	case ZFS_PROP_REFERENCED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2385
	case ZFS_PROP_USED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2386
	case ZFS_PROP_VOLSIZE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2387
	case ZFS_PROP_VOLBLOCKSIZE:
3377
a2fa338530c1 6393525 vdev_reopen() should verify that it's still the same device
eschrock
parents: 3363
diff changeset
  2388
	case ZFS_PROP_NUMCLONES:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2389
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2390
		 * Basic numeric values are built on top of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2391
		 * get_numeric_property().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2392
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2393
		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2394
			return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2395
		if (literal)
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
  2396
			(void) snprintf(propbuf, proplen, "%llu",
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2397
			    (u_longlong_t)val);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2398
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2399
			zfs_nicenum(val, propbuf, proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2400
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2401
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2402
	case ZFS_PROP_COMPRESSION:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2403
	case ZFS_PROP_CHECKSUM:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2404
	case ZFS_PROP_SNAPDIR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2405
	case ZFS_PROP_ACLMODE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2406
	case ZFS_PROP_ACLINHERIT:
3835
063f0749804a PSARC/2007/121 zfs set copies
ahrens
parents: 3654
diff changeset
  2407
	case ZFS_PROP_COPIES:
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2408
		val = getprop_uint64(zhp, prop, &source);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2409
		verify(zfs_prop_index_to_string(prop, val, &strval) == 0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2410
		(void) strlcpy(propbuf, strval, proplen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2411
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2412
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2413
	case ZFS_PROP_CREATION:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2414
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2415
		 * 'creation' is a time_t stored in the statistics.  We convert
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2416
		 * this into a string unless 'literal' is specified.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2417
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2418
		{
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2419
			val = getprop_uint64(zhp, prop, &source);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2420
			time_t time = (time_t)val;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2421
			struct tm t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2422
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2423
			if (literal ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2424
			    localtime_r(&time, &t) == NULL ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2425
			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2426
			    &t) == 0)
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2427
				(void) snprintf(propbuf, proplen, "%llu", val);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2428
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2429
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2430
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2431
	case ZFS_PROP_MOUNTPOINT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2432
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2433
		 * Getting the precise mountpoint can be tricky.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2434
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2435
		 *  - for 'none' or 'legacy', return those values.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2436
		 *  - for default mountpoints, construct it as /zfs/<dataset>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2437
		 *  - for inherited mountpoints, we want to take everything
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2438
		 *    after our ancestor and append it to the inherited value.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2439
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2440
		 * If the pool has an alternate root, we want to prepend that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2441
		 * root to any values we return.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2442
		 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1407
diff changeset
  2443
		root = zhp->zfs_root;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2444
		str = getprop_string(zhp, prop, &source);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2445
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2446
		if (str[0] == '\0') {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2447
			(void) snprintf(propbuf, proplen, "%s/zfs/%s",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2448
			    root, zhp->zfs_name);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2449
		} else if (str[0] == '/') {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2450
			const char *relpath = zhp->zfs_name + strlen(source);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2451
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2452
			if (relpath[0] == '/')
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2453
				relpath++;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2454
			if (str[1] == '\0')
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2455
				str++;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2456
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2457
			if (relpath[0] == '\0')
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2458
				(void) snprintf(propbuf, proplen, "%s%s",
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2459
				    root, str);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2460
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2461
				(void) snprintf(propbuf, proplen, "%s%s%s%s",
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2462
				    root, str, relpath[0] == '@' ? "" : "/",
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2463
				    relpath);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2464
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2465
			/* 'legacy' or 'none' */
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2466
			(void) strlcpy(propbuf, str, proplen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2467
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2468
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2469
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2470
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2471
	case ZFS_PROP_SHARENFS:
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
  2472
	case ZFS_PROP_SHAREISCSI:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
  2473
	case ZFS_PROP_ISCSIOPTIONS:
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2474
		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2475
		    proplen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2476
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2477
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2478
	case ZFS_PROP_ORIGIN:
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2479
		(void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2480
		    proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2481
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2482
		 * If there is no parent at all, return failure to indicate that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2483
		 * it doesn't apply to this dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2484
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2485
		if (propbuf[0] == '\0')
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2486
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2487
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2488
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2489
	case ZFS_PROP_QUOTA:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2490
	case ZFS_PROP_RESERVATION:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2491
		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2492
			return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2493
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2494
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2495
		 * If quota or reservation is 0, we translate this into 'none'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2496
		 * (unless literal is set), and indicate that it's the default
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2497
		 * value.  Otherwise, we print the number nicely and indicate
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2498
		 * that its set locally.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2499
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2500
		if (val == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2501
			if (literal)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2502
				(void) strlcpy(propbuf, "0", proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2503
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2504
				(void) strlcpy(propbuf, "none", proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2505
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2506
			if (literal)
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
  2507
				(void) snprintf(propbuf, proplen, "%llu",
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2508
				    (u_longlong_t)val);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2509
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2510
				zfs_nicenum(val, propbuf, proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2511
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2512
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2513
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2514
	case ZFS_PROP_COMPRESSRATIO:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2515
		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2516
			return (-1);
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
  2517
		(void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2744
diff changeset
  2518
		    val / 100, (longlong_t)val % 100);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2519
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2520
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2521
	case ZFS_PROP_TYPE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2522
		switch (zhp->zfs_type) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2523
		case ZFS_TYPE_FILESYSTEM:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2524
			str = "filesystem";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2525
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2526
		case ZFS_TYPE_VOLUME:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2527
			str = "volume";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2528
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2529
		case ZFS_TYPE_SNAPSHOT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2530
			str = "snapshot";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2531
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2532
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2533
			abort();
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2534
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2535
		(void) snprintf(propbuf, proplen, "%s", str);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2536
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2537
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2538
	case ZFS_PROP_MOUNTED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2539
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2540
		 * The 'mounted' property is a pseudo-property that described
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2541
		 * whether the filesystem is currently mounted.  Even though
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2542
		 * it's a boolean value, the typical values of "on" and "off"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2543
		 * don't make sense, so we translate to "yes" and "no".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2544
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2545
		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2546
		    src, &source, &val) != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2547
			return (-1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2548
		if (val)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2549
			(void) strlcpy(propbuf, "yes", proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2550
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2551
			(void) strlcpy(propbuf, "no", proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2552
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2553
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2554
	case ZFS_PROP_NAME:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2555
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2556
		 * The 'name' property is a pseudo-property derived from the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2557
		 * dataset name.  It is presented as a real property to simplify
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2558
		 * consumers.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2559
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2560
		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2561
		break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2562
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2563
	default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2564
		abort();
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2565
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2566
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2567
	get_source(zhp, src, source, statbuf, statlen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2568
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2569
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2570
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2571
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2572
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2573
 * Utility function to get the given numeric property.  Does no validation that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2574
 * the given property is the appropriate type; should only be used with
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2575
 * hard-coded property types.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2576
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2577
uint64_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2578
zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2579
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2580
	char *source;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2581
	zfs_source_t sourcetype = ZFS_SRC_NONE;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2582
	uint64_t val;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2583
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2584
	(void) get_numeric_property(zhp, prop, &sourcetype, &source, &val);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2585
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2586
	return (val);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2587
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2588
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2589
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2590
 * Similar to zfs_prop_get(), but returns the value as an integer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2591
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2592
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2593
zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2594
    zfs_source_t *src, char *statbuf, size_t statlen)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2595
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2596
	char *source;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2597
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2598
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2599
	 * Check to see if this property applies to our object
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2600
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2601
	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  2602
		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2603
		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2604
		    zfs_prop_to_name(prop)));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2605
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2606
	if (src)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2607
		*src = ZFS_SRC_NONE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2608
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2609
	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2610
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2611
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2612
	get_source(zhp, src, source, statbuf, statlen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2613
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2614
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2615
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2616
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2617
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2618
 * Returns the name of the given zfs handle.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2619
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2620
const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2621
zfs_get_name(const zfs_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2622
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2623
	return (zhp->zfs_name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2624
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2625
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2626
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2627
 * Returns the type of the given zfs handle.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2628
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2629
zfs_type_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2630
zfs_get_type(const zfs_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2631
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2632
	return (zhp->zfs_type);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2633
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2634
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2635
/*
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2636
 * Iterate over all child filesystems
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2637
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2638
int
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2639
zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2640
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2641
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2642
	zfs_handle_t *nzhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2643
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2644
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2645
	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2646
	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2647
	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2648
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2649
		 * Ignore private dataset names.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2650
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2651
		if (dataset_name_hidden(zc.zc_name))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2652
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2653
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2654
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2655
		 * Silently ignore errors, as the only plausible explanation is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2656
		 * that the pool has since been removed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2657
		 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2658
		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2659
		    zc.zc_name)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2660
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2661
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2662
		if ((ret = func(nzhp, data)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2663
			return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2664
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2665
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2666
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2667
	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2668
	 * returned, then the underlying dataset has been removed since we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2669
	 * obtained the handle.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2670
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2671
	if (errno != ESRCH && errno != ENOENT)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2672
		return (zfs_standard_error(zhp->zfs_hdl, errno,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2673
		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2674
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2675
	return (0);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2676
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2677
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2678
/*
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2679
 * Iterate over all snapshots
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2680
 */
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2681
int
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2682
zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2683
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2684
	zfs_cmd_t zc = { 0 };
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2685
	zfs_handle_t *nzhp;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2686
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2687
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2688
	for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2689
	    ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2690
	    &zc) == 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2691
	    (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2692
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2693
		if ((nzhp = make_dataset_handle(zhp->zfs_hdl,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2694
		    zc.zc_name)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2695
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2696
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2697
		if ((ret = func(nzhp, data)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2698
			return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2699
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2700
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2701
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2702
	 * An errno value of ESRCH indicates normal completion.  If ENOENT is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2703
	 * returned, then the underlying dataset has been removed since we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2704
	 * obtained the handle.  Silently ignore this case, and return success.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2705
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2706
	if (errno != ESRCH && errno != ENOENT)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2707
		return (zfs_standard_error(zhp->zfs_hdl, errno,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2708
		    dgettext(TEXT_DOMAIN, "cannot iterate filesystems")));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2709
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2710
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2711
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2712
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2713
/*
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2714
 * Iterate over all children, snapshots and filesystems
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2715
 */
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2716
int
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2717
zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2718
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2719
	int ret;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2720
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2721
	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2722
		return (ret);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2723
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2724
	return (zfs_iter_snapshots(zhp, func, data));
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2725
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2726
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2727
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2728
 * Given a complete name, return just the portion that refers to the parent.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2729
 * Can return NULL if this is a pool.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2730
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2731
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2732
parent_name(const char *path, char *buf, size_t buflen)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2733
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2734
	char *loc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2735
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2736
	if ((loc = strrchr(path, '/')) == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2737
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2738
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2739
	(void) strncpy(buf, path, MIN(buflen, loc - path));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2740
	buf[loc - path] = '\0';
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2741
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2742
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2743
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2744
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2745
/*
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2746
 * If accept_ancestor is false, then check to make sure that the given path has
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2747
 * a parent, and that it exists.  If accept_ancestor is true, then find the
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2748
 * closest existing ancestor for the given path.  In prefixlen return the
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2749
 * length of already existing prefix of the given path.  We also fetch the
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2750
 * 'zoned' property, which is used to validate property settings when creating
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2751
 * new datasets.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2752
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2753
static int
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2754
check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2755
    boolean_t accept_ancestor, int *prefixlen)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2756
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2757
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2758
	char parent[ZFS_MAXNAMELEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2759
	char *slash;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2760
	zfs_handle_t *zhp;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2761
	char errbuf[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2762
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2763
	(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'",
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2764
	    path);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2765
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2766
	/* get parent, and check to see if this is just a pool */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2767
	if (parent_name(path, parent, sizeof (parent)) != 0) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2768
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2769
		    "missing dataset name"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2770
		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2771
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2772
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2773
	/* check to see if the pool exists */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2774
	if ((slash = strchr(parent, '/')) == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2775
		slash = parent + strlen(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2776
	(void) strncpy(zc.zc_name, parent, slash - parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2777
	zc.zc_name[slash - parent] = '\0';
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2778
	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2779
	    errno == ENOENT) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2780
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2781
		    "no such pool '%s'"), zc.zc_name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2782
		return (zfs_error(hdl, EZFS_NOENT, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2783
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2784
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2785
	/* check to see if the parent dataset exists */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2786
	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2787
		if (errno == ENOENT && accept_ancestor) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2788
			/*
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2789
			 * Go deeper to find an ancestor, give up on top level.
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2790
			 */
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2791
			if (parent_name(parent, parent, sizeof (parent)) != 0) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2792
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2793
				    "no such pool '%s'"), zc.zc_name);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2794
				return (zfs_error(hdl, EZFS_NOENT, errbuf));
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2795
			}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2796
		} else if (errno == ENOENT) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2797
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2798
			    "parent does not exist"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2799
			return (zfs_error(hdl, EZFS_NOENT, errbuf));
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2800
		} else
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2801
			return (zfs_standard_error(hdl, errno, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2802
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2803
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2804
	*zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2805
	/* we are in a non-global zone, but parent is in the global zone */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2806
	if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2807
		(void) zfs_standard_error(hdl, EPERM, errbuf);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2808
		zfs_close(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2809
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2810
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2811
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2812
	/* make sure parent is a filesystem */
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2813
	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2814
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2815
		    "parent is not a filesystem"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2816
		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2817
		zfs_close(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2818
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2819
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2820
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2821
	zfs_close(zhp);
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2822
	if (prefixlen != NULL)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2823
		*prefixlen = strlen(parent);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2824
	return (0);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2825
}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2826
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2827
/*
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2828
 * Finds whether the dataset of the given type(s) exists.
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2829
 */
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2830
boolean_t
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2831
zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2832
{
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2833
	zfs_handle_t *zhp;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2834
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2835
	if (!zfs_validate_name(hdl, path, types))
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2836
		return (B_FALSE);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2837
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2838
	/*
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2839
	 * Try to get stats for the dataset, which will tell us if it exists.
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2840
	 */
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2841
	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2842
		int ds_type = zhp->zfs_type;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2843
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2844
		zfs_close(zhp);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2845
		if (types & ds_type)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2846
			return (B_TRUE);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2847
	}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2848
	return (B_FALSE);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2849
}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2850
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2851
/*
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2852
 * Creates non-existing ancestors of the given path.
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2853
 */
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2854
int
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2855
zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2856
{
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2857
	int prefix;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2858
	uint64_t zoned;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2859
	char *path_copy;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2860
	int rc;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2861
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2862
	if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2863
		return (-1);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2864
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2865
	if ((path_copy = strdup(path)) != NULL) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2866
		rc = create_parents(hdl, path_copy, prefix);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2867
		free(path_copy);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2868
	}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2869
	if (path_copy == NULL || rc != 0)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2870
		return (-1);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2871
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2872
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2873
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2874
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2875
/*
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2876
 * Create a new filesystem or volume.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2877
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2878
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2879
zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2880
    nvlist_t *props)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2881
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2882
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2883
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2884
	uint64_t size = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2885
	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2886
	char errbuf[1024];
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2887
	uint64_t zoned;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2888
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2889
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2890
	    "cannot create '%s'"), path);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2891
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2892
	/* validate the path, taking care to note the extended error message */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2893
	if (!zfs_validate_name(hdl, path, type))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2894
		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2895
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2896
	/* validate parents exist */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2897
	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2898
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2899
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2900
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2901
	 * The failure modes when creating a dataset of a different type over
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2902
	 * one that already exists is a little strange.  In particular, if you
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2903
	 * try to create a dataset on top of an existing dataset, the ioctl()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2904
	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2905
	 * first try to see if the dataset exists.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2906
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2907
	(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  2908
	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2909
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2910
		    "dataset already exists"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2911
		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2912
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2913
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2914
	if (type == ZFS_TYPE_VOLUME)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2915
		zc.zc_objset_type = DMU_OST_ZVOL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2916
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2917
		zc.zc_objset_type = DMU_OST_ZFS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2918
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2919
	if (props && (props = zfs_validate_properties(hdl, type, NULL, props,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2920
	    zoned, NULL, errbuf)) == 0)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2921
		return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2922
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2923
	if (type == ZFS_TYPE_VOLUME) {
1133
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2924
		/*
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2925
		 * If we are creating a volume, the size and block size must
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2926
		 * satisfy a few restraints.  First, the blocksize must be a
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2927
		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2928
		 * volsize must be a multiple of the block size, and cannot be
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2929
		 * zero.
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2930
		 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2931
		if (props == NULL || nvlist_lookup_uint64(props,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2932
		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2933
			nvlist_free(props);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2934
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2935
			    "missing volume size"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2936
			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2937
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2938
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2939
		if ((ret = nvlist_lookup_uint64(props,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2940
		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2941
		    &blocksize)) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2942
			if (ret == ENOENT) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2943
				blocksize = zfs_prop_default_numeric(
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2944
				    ZFS_PROP_VOLBLOCKSIZE);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2945
			} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2946
				nvlist_free(props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2947
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2948
				    "missing volume block size"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2949
				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2950
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2951
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2952
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2953
		if (size == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2954
			nvlist_free(props);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2955
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2956
			    "volume size cannot be zero"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2957
			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1133
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2958
		}
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2959
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2960
		if (size % blocksize != 0) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2961
			nvlist_free(props);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2962
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2963
			    "volume size must be a multiple of volume block "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2964
			    "size"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2965
			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1133
335d069294d1 6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents: 1003
diff changeset
  2966
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2967
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2968
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2969
	if (props &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2970
	    zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2971
		return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2972
	nvlist_free(props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2973
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2974
	/* create the dataset */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2975
	ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2976
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2977
	if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2978
		ret = zvol_create_link(hdl, path);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2979
		if (ret) {
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2980
			(void) zfs_standard_error(hdl, errno,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2981
			    dgettext(TEXT_DOMAIN,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2982
			    "Volume successfully created, but device links "
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2983
			    "were not created"));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2984
			zcmd_free_nvlists(&zc);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2985
			return (-1);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2986
		}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  2987
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2988
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2989
	zcmd_free_nvlists(&zc);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2990
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2991
	/* check for failure */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2992
	if (ret != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2993
		char parent[ZFS_MAXNAMELEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2994
		(void) parent_name(path, parent, sizeof (parent));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2995
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2996
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2997
		case ENOENT:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2998
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  2999
			    "no such parent '%s'"), parent);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3000
			return (zfs_error(hdl, EZFS_NOENT, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3001
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3002
		case EINVAL:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3003
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3413
f4cbe0204ab7 6471255 Some ZFS error messages contain spelling errors
mmusante
parents: 3377
diff changeset
  3004
			    "parent '%s' is not a filesystem"), parent);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3005
			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3006
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3007
		case EDOM:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3008
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3009
			    "volume block size must be power of 2 from "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3010
			    "%u to %uk"),
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3011
			    (uint_t)SPA_MINBLOCKSIZE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3012
			    (uint_t)SPA_MAXBLOCKSIZE >> 10);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3013
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3014
			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3015
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3016
#ifdef _ILP32
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3017
		case EOVERFLOW:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3018
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3019
			 * This platform can't address a volume this big.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3020
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3021
			if (type == ZFS_TYPE_VOLUME)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3022
				return (zfs_error(hdl, EZFS_VOLTOOBIG,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3023
				    errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3024
#endif
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3025
			/* FALLTHROUGH */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3026
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3027
			return (zfs_standard_error(hdl, errno, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3028
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3029
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3030
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3031
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3032
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3033
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3034
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3035
 * Destroys the given dataset.  The caller must make sure that the filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3036
 * isn't mounted, and that there are no active dependents.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3037
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3038
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3039
zfs_destroy(zfs_handle_t *zhp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3040
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3041
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3042
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3043
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3044
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3045
	if (ZFS_IS_VOLUME(zhp)) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
  3046
		/*
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3047
		 * If user doesn't have permissions to unshare volume, then
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3048
		 * abort the request.  This would only happen for a
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3049
		 * non-privileged user.
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
  3050
		 */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3051
		if (zfs_unshare_iscsi(zhp) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3052
			return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3053
		}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2885
diff changeset
  3054
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3055
		if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3056
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3057
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3058
		zc.zc_objset_type = DMU_OST_ZVOL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3059
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3060
		zc.zc_objset_type = DMU_OST_ZFS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3061
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3062
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3063
	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  3064
		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3065
		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3066
		    zhp->zfs_name));
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3067
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3068
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3069
	remove_mountpoint(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3070
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3071
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3072
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3073
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3074
struct destroydata {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3075
	char *snapname;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3076
	boolean_t gotone;
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3077
	boolean_t closezhp;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3078
};
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3079
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3080
static int
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3081
zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3082
{
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3083
	struct destroydata *dd = arg;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3084
	zfs_handle_t *szhp;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3085
	char name[ZFS_MAXNAMELEN];
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3086
	boolean_t closezhp = dd->closezhp;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3087
	int rv;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3088
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3089
	(void) strlcpy(name, zhp->zfs_name, sizeof (name));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3090
	(void) strlcat(name, "@", sizeof (name));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3091
	(void) strlcat(name, dd->snapname, sizeof (name));
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3092
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3093
	szhp = make_dataset_handle(zhp->zfs_hdl, name);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3094
	if (szhp) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3095
		dd->gotone = B_TRUE;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3096
		zfs_close(szhp);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3097
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3098
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3099
	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3100
		(void) zvol_remove_link(zhp->zfs_hdl, name);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3101
		/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3102
		 * NB: this is simply a best-effort.  We don't want to
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3103
		 * return an error, because then we wouldn't visit all
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3104
		 * the volumes.
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3105
		 */
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3106
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3107
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3108
	dd->closezhp = B_TRUE;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3109
	rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3110
	if (closezhp)
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3111
		zfs_close(zhp);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3112
	return (rv);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3113
}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3114
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3115
/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3116
 * Destroys all snapshots with the given name in zhp & descendants.
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3117
 */
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3118
int
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3119
zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3120
{
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3121
	zfs_cmd_t zc = { 0 };
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3122
	int ret;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3123
	struct destroydata dd = { 0 };
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3124
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3125
	dd.snapname = snapname;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3126
	(void) zfs_remove_link_cb(zhp, &dd);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3127
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3128
	if (!dd.gotone) {
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  3129
		return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3130
		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3131
		    zhp->zfs_name, snapname));
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3132
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3133
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3134
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3135
	(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3136
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3137
	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3138
	if (ret != 0) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3139
		char errbuf[1024];
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3140
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3141
		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3142
		    "cannot destroy '%s@%s'"), zc.zc_name, snapname);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3143
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3144
		switch (errno) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3145
		case EEXIST:
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3146
			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3147
			    "snapshot is cloned"));
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3148
			return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3149
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3150
		default:
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3151
			return (zfs_standard_error(zhp->zfs_hdl, errno,
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3152
			    errbuf));
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3153
		}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3154
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3155
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3156
	return (0);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3157
}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3158
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3159
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3160
 * Clones the given dataset.  The target must be of the same type as the source.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3161
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3162
int
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3163
zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3164
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3165
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3166
	char parent[ZFS_MAXNAMELEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3167
	int ret;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3168
	char errbuf[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3169
	libzfs_handle_t *hdl = zhp->zfs_hdl;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3170
	zfs_type_t type;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3171
	uint64_t zoned;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3172
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3173
	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3174
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3175
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3176
	    "cannot create '%s'"), target);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3177
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3178
	/* validate the target name */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3179
	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3180
		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3181
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3182
	/* validate parents exist */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  3183
	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3184
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3185
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3186
	(void) parent_name(target, parent, sizeof (parent));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3187
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3188
	/* do the clone */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3189
	if (ZFS_IS_VOLUME(zhp)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3190
		zc.zc_objset_type = DMU_OST_ZVOL;
2744
0a6f00b6a209 6468554 zone install with a zfs zonepath could not create a dataset
nn35248
parents: 2676
diff changeset
  3191
		type = ZFS_TYPE_VOLUME;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3192
	} else {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3193
		zc.zc_objset_type = DMU_OST_ZFS;
2744
0a6f00b6a209 6468554 zone install with a zfs zonepath could not create a dataset
nn35248
parents: 2676
diff changeset
  3194
		type = ZFS_TYPE_FILESYSTEM;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3195
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3196
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3197
	if (props) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3198
		if ((props = zfs_validate_properties(hdl, type, NULL, props,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3199
		    zoned, zhp, errbuf)) == NULL)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3200
			return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3201
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3202
		if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3203
			nvlist_free(props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3204
			return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3205
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3206
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3207
		nvlist_free(props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3208
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3209
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3210
	(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3211
	(void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3212
	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3213
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3214
	zcmd_free_nvlists(&zc);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3215
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3216
	if (ret != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3217
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3218
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3219
		case ENOENT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3220
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3221
			 * The parent doesn't exist.  We should have caught this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3222
			 * above, but there may a race condition that has since
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3223
			 * destroyed the parent.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3224
			 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3225
			 * At this point, we don't know whether it's the source
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3226
			 * that doesn't exist anymore, or whether the target
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3227
			 * dataset doesn't exist.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3228
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3229
			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3230
			    "no such parent '%s'"), parent);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3231
			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3232
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3233
		case EXDEV:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3234
			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3235
			    "source and target pools differ"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3236
			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3237
			    errbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3238
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3239
		default:
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3240
			return (zfs_standard_error(zhp->zfs_hdl, errno,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3241
			    errbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3242
		}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3243
	} else if (ZFS_IS_VOLUME(zhp)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3244
		ret = zvol_create_link(zhp->zfs_hdl, target);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3245
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3246
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3247
	return (ret);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3248
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3249
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3250
typedef struct promote_data {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3251
	char cb_mountpoint[MAXPATHLEN];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3252
	const char *cb_target;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3253
	const char *cb_errbuf;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3254
	uint64_t cb_pivot_txg;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3255
} promote_data_t;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3256
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3257
static int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3258
promote_snap_cb(zfs_handle_t *zhp, void *data)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3259
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3260
	promote_data_t *pd = data;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3261
	zfs_handle_t *szhp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3262
	char snapname[MAXPATHLEN];
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3263
	int rv = 0;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3264
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3265
	/* We don't care about snapshots after the pivot point */
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3266
	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3267
		zfs_close(zhp);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3268
		return (0);
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3269
	}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3270
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3271
	/* Remove the device link if it's a zvol. */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3272
	if (ZFS_IS_VOLUME(zhp))
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3273
		(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3274
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3275
	/* Check for conflicting names */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3276
	(void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3277
	(void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3278
	szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3279
	if (szhp != NULL) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3280
		zfs_close(szhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3281
		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3282
		    "snapshot name '%s' from origin \n"
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3283
		    "conflicts with '%s' from target"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3284
		    zhp->zfs_name, snapname);
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3285
		rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3286
	}
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3287
	zfs_close(zhp);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3288
	return (rv);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3289
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3290
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3291
static int
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3292
promote_snap_done_cb(zfs_handle_t *zhp, void *data)
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3293
{
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3294
	promote_data_t *pd = data;
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3295
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3296
	/* We don't care about snapshots after the pivot point */
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3297
	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3298
		/* Create the device link if it's a zvol. */
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3299
		if (ZFS_IS_VOLUME(zhp))
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3300
			(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3301
	}
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3302
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3303
	zfs_close(zhp);
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3304
	return (0);
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3305
}
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3306
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3307
/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3308
 * Promotes the given clone fs to be the clone parent.
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3309
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3310
int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3311
zfs_promote(zfs_handle_t *zhp)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3312
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3313
	libzfs_handle_t *hdl = zhp->zfs_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3314
	zfs_cmd_t zc = { 0 };
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3315
	char parent[MAXPATHLEN];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3316
	char *cp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3317
	int ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3318
	zfs_handle_t *pzhp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3319
	promote_data_t pd;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3320
	char errbuf[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3321
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3322
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3323
	    "cannot promote '%s'"), zhp->zfs_name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3324
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3325
	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3326
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3327
		    "snapshots can not be promoted"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3328
		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3329
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3330
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3331
	(void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3332
	if (parent[0] == '\0') {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3333
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3334
		    "not a cloned filesystem"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3335
		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3336
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3337
	cp = strchr(parent, '@');
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3338
	*cp = '\0';
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3339
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3340
	/* Walk the snapshots we will be moving */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3341
	pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3342
	if (pzhp == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3343
		return (-1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3344
	pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3345
	zfs_close(pzhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3346
	pd.cb_target = zhp->zfs_name;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3347
	pd.cb_errbuf = errbuf;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3348
	pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3349
	if (pzhp == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3350
		return (-1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3351
	(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3352
	    sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3353
	ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3354
	if (ret != 0) {
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3355
		zfs_close(pzhp);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3356
		return (-1);
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3357
	}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3358
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3359
	/* issue the ioctl */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3360
	(void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3361
	    sizeof (zc.zc_value));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3362
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3363
	ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3364
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3365
	if (ret != 0) {
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3366
		int save_errno = errno;
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3367
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3368
		(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3369
		zfs_close(pzhp);
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3370
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3371
		switch (save_errno) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3372
		case EEXIST:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3373
			/*
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3374
			 * There is a conflicting snapshot name.  We
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3375
			 * should have caught this above, but they could
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3376
			 * have renamed something in the mean time.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3377
			 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3378
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3379
			    "conflicting snapshot name from parent '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3380
			    parent);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3381
			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3382
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3383
		default:
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3384
			return (zfs_standard_error(hdl, save_errno, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3385
		}
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3386
	} else {
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3387
		(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3388
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3389
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2199
diff changeset
  3390
	zfs_close(pzhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3391
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3392
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3393
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3394
struct createdata {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3395
	const char *cd_snapname;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3396
	int cd_ifexists;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3397
};
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3398
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3399
static int
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3400
zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3401
{
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3402
	struct createdata *cd = arg;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3403
	int ret;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3404
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3405
	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3406
		char name[MAXPATHLEN];
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3407
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3408
		(void) strlcpy(name, zhp->zfs_name, sizeof (name));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3409
		(void) strlcat(name, "@", sizeof (name));
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3410
		(void) strlcat(name, cd->cd_snapname, sizeof (name));
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3411
		(void) zvol_create_link_common(zhp->zfs_hdl, name,
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3412
		    cd->cd_ifexists);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3413
		/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3414
		 * NB: this is simply a best-effort.  We don't want to
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3415
		 * return an error, because then we wouldn't visit all
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3416
		 * the volumes.
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3417
		 */
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3418
	}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3419
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3420
	ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3421
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3422
	zfs_close(zhp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3423
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3424
	return (ret);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3425
}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3426
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3427
/*
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3428
 * Takes a snapshot of the given dataset.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3429
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3430
int
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3431
zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3432
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3433
	const char *delim;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3434
	char *parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3435
	zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3436
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3437
	int ret;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3438
	char errbuf[1024];
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3439
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3440
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3441
	    "cannot snapshot '%s'"), path);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3442
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3443
	/* validate the target name */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3444
	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT))
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3445
		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3446
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3447
	/* make sure the parent exists and is of the appropriate type */
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3448
	delim = strchr(path, '@');
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3449
	if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3450
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3451
	(void) strncpy(parent, path, delim - path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3452
	parent[delim - path] = '\0';
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3453
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3454
	if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3455
	    ZFS_TYPE_VOLUME)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3456
		free(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3457
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3458
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3459
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3460
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3461
	(void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3462
	if (ZFS_IS_VOLUME(zhp))
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3463
		zc.zc_objset_type = DMU_OST_ZVOL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3464
	else
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3465
		zc.zc_objset_type = DMU_OST_ZFS;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3466
	zc.zc_cookie = recursive;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3467
	ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3468
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3469
	/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3470
	 * if it was recursive, the one that actually failed will be in
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3471
	 * zc.zc_name.
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3472
	 */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3473
	if (ret != 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3474
		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3475
		    "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3476
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3477
	if (ret == 0 && recursive) {
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3478
		struct createdata cd;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3479
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3480
		cd.cd_snapname = delim + 1;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3481
		cd.cd_ifexists = B_FALSE;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  3482
		(void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3483
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3484
	if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3485
		ret = zvol_create_link(zhp->zfs_hdl, path);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3486
		if (ret != 0) {
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3487
			(void) zfs_standard_error(hdl, errno,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3488
			    dgettext(TEXT_DOMAIN,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3489
			    "Volume successfully snapshotted, but device links "
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3490
			    "were not created"));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3491
			free(parent);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3492
			zfs_close(zhp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3493
			return (-1);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2169
diff changeset
  3494
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3495
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3496
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3497
	if (ret != 0)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3498
		(void) zfs_standard_error(hdl, errno, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3499
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3500
	free(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3501
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3502
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3503
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3504
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3505
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3506
/*
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3507
 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3508
 * NULL) to the file descriptor specified by outfd.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3509
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3510
int
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3511
zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3512
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3513
	zfs_cmd_t zc = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3514
	char errbuf[1024];
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3515
	libzfs_handle_t *hdl = zhp->zfs_hdl;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3516
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3517
	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3518
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3519
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3520
	if (fromsnap)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3521
		(void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name));
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3522
	zc.zc_cookie = outfd;
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3523
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3524
	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) {
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3525
		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3526
		    "cannot send '%s'"), zhp->zfs_name);
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3527
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3528
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3529
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3530
		case EXDEV:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3531
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3413
f4cbe0204ab7 6471255 Some ZFS error messages contain spelling errors
mmusante
parents: 3377
diff changeset
  3532
			    "not an earlier snapshot from the same fs"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3533
			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3534
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3535
		case EDQUOT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3536
		case EFBIG:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3537
		case EIO:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3538
		case ENOLINK:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3539
		case ENOSPC:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3540
		case ENOSTR:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3541
		case ENXIO:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3542
		case EPIPE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3543
		case ERANGE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3544
		case EFAULT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3545
		case EROFS:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3546
			zfs_error_aux(hdl, strerror(errno));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3547
			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3548
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3549
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3550
			return (zfs_standard_error(hdl, errno, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3551
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3552
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3553
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3554
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3555
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3556
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3557
/*
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3558
 * Create ancestors of 'target', but not target itself, and not
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3559
 * ancestors whose names are shorter than prefixlen.  Die if
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3560
 * prefixlen-ancestor does not exist.
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3561
 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3562
static int
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3563
create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3564
{
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3565
	zfs_handle_t *h;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3566
	char *cp;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3567
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3568
	/* make sure prefix exists */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3569
	cp = strchr(target + prefixlen, '/');
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3570
	*cp = '\0';
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3571
	h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3572
	*cp = '/';
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3573
	if (h == NULL)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3574
		return (-1);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3575
	zfs_close(h);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3576
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3577
	/*
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3578
	 * Attempt to create, mount, and share any ancestor filesystems,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3579
	 * up to the prefixlen-long one.
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3580
	 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3581
	for (cp = target + prefixlen + 1;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3582
	    cp = strchr(cp, '/'); *cp = '/', cp++) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3583
		const char *opname;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3584
		char *logstr;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3585
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3586
		*cp = '\0';
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3587
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3588
		h = make_dataset_handle(hdl, target);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3589
		if (h) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3590
			/* it already exists, nothing to do here */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3591
			zfs_close(h);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3592
			continue;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3593
		}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3594
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3595
		opname = dgettext(TEXT_DOMAIN, "create");
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3596
		logstr = hdl->libzfs_log_str;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3597
		hdl->libzfs_log_str = NULL;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3598
		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3599
		    NULL) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3600
			hdl->libzfs_log_str = logstr;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3601
			goto ancestorerr;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3602
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3603
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3604
		hdl->libzfs_log_str = logstr;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3605
		opname = dgettext(TEXT_DOMAIN, "open");
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3606
		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3607
		if (h == NULL)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3608
			goto ancestorerr;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3609
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3610
		opname = dgettext(TEXT_DOMAIN, "mount");
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3611
		if (zfs_mount(h, NULL, 0) != 0)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3612
			goto ancestorerr;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3613
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3614
		opname = dgettext(TEXT_DOMAIN, "share");
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3615
		if (zfs_share(h) != 0)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3616
			goto ancestorerr;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3617
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3618
		zfs_close(h);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3619
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3620
		continue;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3621
ancestorerr:
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3622
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3623
		    "failed to %s ancestor '%s'"), opname, target);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3624
		return (-1);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3625
	}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3626
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3627
	return (0);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3628
}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3629
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3630
/*
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3631
 * Restores a backup of tosnap from the file descriptor specified by infd.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3632
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3633
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3634
zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix,
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3635
    int verbose, int dryrun, boolean_t force, int infd)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3636
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3637
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3638
	time_t begin_time;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3639
	int ioctl_err, err, bytes, size, choplen;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3640
	char *cp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3641
	dmu_replay_record_t drr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3642
	struct drr_begin *drrb = &zc.zc_begin_record;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3643
	char errbuf[1024];
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3644
	prop_changelist_t *clp;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3645
	char chopprefix[ZFS_MAXNAMELEN];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3646
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3647
	begin_time = time(NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3648
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3649
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3650
	    "cannot receive"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3651
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3652
	/* read in the BEGIN record */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3653
	cp = (char *)&drr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3654
	bytes = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3655
	do {
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3656
		size = read(infd, cp, sizeof (drr) - bytes);
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3657
		cp += size;
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3658
		bytes += size;
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3659
	} while (size > 0);
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3660
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3661
	if (size < 0 || bytes != sizeof (drr)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3662
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3663
		    "stream (failed to read first record)"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3664
		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3665
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3666
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3667
	zc.zc_begin_record = drr.drr_u.drr_begin;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3668
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3669
	if (drrb->drr_magic != DMU_BACKUP_MAGIC &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3670
	    drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3671
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3672
		    "stream (bad magic number)"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3673
		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3674
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3675
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3676
	if (drrb->drr_version != DMU_BACKUP_VERSION &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3677
	    drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3678
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3679
		    "0x%llx is supported (stream is version 0x%llx)"),
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3680
		    DMU_BACKUP_VERSION, drrb->drr_version);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3681
		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3682
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3683
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3684
	if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3685
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  3686
		    "stream (bad snapshot name)"));
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3687
		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3688
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3689
	/*
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3690
	 * Determine how much of the snapshot name stored in the stream
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3691
	 * we are going to tack on to the name they specified on the
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3692
	 * command line, and how much we are going to chop off.
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3693
	 *
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3694
	 * If they specified a snapshot, chop the entire name stored in
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3695
	 * the stream.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3696
	 */
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3697
	(void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3698
	if (isprefix) {
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3699
		/*
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3700
		 * They specified a fs with -d, we want to tack on
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3701
		 * everything but the pool name stored in the stream
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3702
		 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3703
		if (strchr(tosnap, '@')) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3704
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3705
			    "argument - snapshot not allowed with -d"));
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3706
			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3707
		}
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3708
		cp = strchr(chopprefix, '/');
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3709
		if (cp == NULL)
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3710
			cp = strchr(chopprefix, '@');
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3711
		*cp = '\0';
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3712
	} else if (strchr(tosnap, '@') == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3713
		/*
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3714
		 * If they specified a filesystem without -d, we want to
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3715
		 * tack on everything after the fs specified in the
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3716
		 * first name from the stream.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3717
		 */
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3718
		cp = strchr(chopprefix, '@');
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3719
		*cp = '\0';
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3720
	}
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3721
	choplen = strlen(chopprefix);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3722
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3723
	/*
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3724
	 * Determine name of destination snapshot, store in zc_value.
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3725
	 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3726
	(void) strcpy(zc.zc_value, tosnap);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3727
	(void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3728
	    sizeof (zc.zc_value));
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3729
	if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT))
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3237
diff changeset
  3730
		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3731
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3732
	(void) strcpy(zc.zc_name, zc.zc_value);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3733
	if (drrb->drr_fromguid) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3734
		/* incremental backup stream */
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3735
		zfs_handle_t *h;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3736
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3737
		/* do the recvbackup ioctl to the containing fs */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3738
		*strchr(zc.zc_name, '@') = '\0';
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3739
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3740
		/* make sure destination fs exists */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3741
		h = zfs_open(hdl, zc.zc_name,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3742
		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3743
		if (h == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3744
			return (-1);
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3745
		if (!dryrun) {
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3746
			/*
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3747
			 * We need to unmount all the dependents of the dataset
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3748
			 * and the dataset itself. If it's a volume
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3749
			 * then remove device link.
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3750
			 */
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3751
			if (h->zfs_type == ZFS_TYPE_FILESYSTEM) {
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3752
				clp = changelist_gather(h, ZFS_PROP_NAME, 0);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3753
				if (clp == NULL)
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3754
					return (-1);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3755
				if (changelist_prefix(clp) != 0) {
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3756
					changelist_free(clp);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3757
					return (-1);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3758
				}
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3759
			} else {
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3760
				if (zvol_remove_link(hdl, h->zfs_name) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3761
					zfs_close(h);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3762
					return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3763
				}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3764
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3765
			}
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3766
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3767
		zfs_close(h);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3768
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3769
		/* full backup stream */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3770
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3771
		/* Make sure destination fs does not exist */
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3772
		*strchr(zc.zc_name, '@') = '\0';
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  3773
		if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3774
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3775
			    "destination '%s' exists"), zc.zc_name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3776
			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3777
		}
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3778
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3779
		if (strchr(zc.zc_name, '/') == NULL) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3780
			/*
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3781
			 * they're trying to do a recv into a
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3782
			 * nonexistant topmost filesystem.
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3783
			 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3784
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3785
			    "destination does not exist"), zc.zc_name);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3786
			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3787
		}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3788
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3789
		/* Do the recvbackup ioctl to the fs's parent. */
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3790
		*strrchr(zc.zc_name, '/') = '\0';
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3791
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3792
		if (isprefix && (err = create_parents(hdl,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3793
		    zc.zc_value, strlen(tosnap))) != 0) {
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3794
			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3795
		}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  3796
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3797
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3798
3504
6eb2466ff1c9 6516175 zfs_send()/zfs_receive() should take file descriptor arguments
ahl
parents: 3443
diff changeset
  3799
	zc.zc_cookie = infd;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3800
	zc.zc_guid = force;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3801
	if (verbose) {
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1566
diff changeset
  3802
		(void) printf("%s %s stream of %s into %s\n",
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1566
diff changeset
  3803
		    dryrun ? "would receive" : "receiving",
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3804
		    drrb->drr_fromguid ? "incremental" : "full",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3805
		    drr.drr_u.drr_begin.drr_toname,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3806
		    zc.zc_value);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3807
		(void) fflush(stdout);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3808
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3809
	if (dryrun)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3810
		return (0);
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3811
	err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc);
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3812
	if (ioctl_err != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3813
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3814
		case ENODEV:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3815
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3816
			    "most recent snapshot does not match incremental "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3817
			    "source"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3818
			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3819
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3820
		case ETXTBSY:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3821
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3822
			    "destination has been modified since most recent "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3823
			    "snapshot"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3824
			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3825
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3826
		case EEXIST:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3827
			if (drrb->drr_fromguid == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3828
				/* it's the containing fs that exists */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3829
				cp = strchr(zc.zc_value, '@');
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3830
				*cp = '\0';
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3831
			}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3832
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3833
			    "destination already exists"));
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  3834
			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  3835
			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  3836
			    zc.zc_value);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3837
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3838
		case EINVAL:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3839
			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3840
			break;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1407
diff changeset
  3841
		case ECKSUM:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3842
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3843
			    "invalid stream (checksum mismatch)"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3844
			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3845
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3846
		default:
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3847
			(void) zfs_standard_error(hdl, errno, errbuf);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3848
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3849
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3850
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3851
	/*
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3852
	 * Mount or recreate the /dev links for the target filesystem
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3853
	 * (if created, or if we tore them down to do an incremental
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3854
	 * restore), and the /dev links for the new snapshot (if
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3855
	 * created). Also mount any children of the target filesystem
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3856
	 * if we did an incremental receive.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3857
	 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3858
	cp = strchr(zc.zc_value, '@');
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3859
	if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3860
		zfs_handle_t *h;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3861
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3862
		*cp = '\0';
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3863
		h = zfs_open(hdl, zc.zc_value,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3864
		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3865
		*cp = '@';
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3866
		if (h) {
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3867
			if (h->zfs_type == ZFS_TYPE_VOLUME) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3868
				err = zvol_create_link(hdl, h->zfs_name);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1407
diff changeset
  3869
				if (err == 0 && ioctl_err == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3870
					err = zvol_create_link(hdl,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3871
					    zc.zc_value);
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3872
			} else {
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3873
				if (drrb->drr_fromguid) {
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3874
					err = changelist_postfix(clp);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3875
					changelist_free(clp);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3876
				} else {
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3877
					err = zfs_mount(h, NULL, 0);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3878
				}
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3879
			}
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3880
		zfs_close(h);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3881
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3882
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3883
868
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3884
	if (err || ioctl_err)
09ef7c98db8f 6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents: 849
diff changeset
  3885
		return (-1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3886
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3887
	if (verbose) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3888
		char buf1[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3889
		char buf2[64];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3890
		uint64_t bytes = zc.zc_cookie;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3891
		time_t delta = time(NULL) - begin_time;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3892
		if (delta == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3893
			delta = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3894
		zfs_nicenum(bytes, buf1, sizeof (buf1));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3895
		zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3896
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1566
diff changeset
  3897
		(void) printf("received %sb stream in %lu seconds (%sb/sec)\n",
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3898
		    buf1, delta, buf2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3899
	}
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  3900
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3901
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3902
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3903
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3904
/*
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3905
 * Destroy any more recent snapshots.  We invoke this callback on any dependents
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3906
 * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3907
 * is a dependent and we should just destroy it without checking the transaction
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3908
 * group.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3909
 */
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3910
typedef struct rollback_data {
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3911
	const char	*cb_target;		/* the snapshot */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3912
	uint64_t	cb_create;		/* creation time reference */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3913
	prop_changelist_t *cb_clp;		/* changelist pointer */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3914
	int		cb_error;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3915
	boolean_t	cb_dependent;
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3916
} rollback_data_t;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3917
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3918
static int
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3919
rollback_destroy(zfs_handle_t *zhp, void *data)
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3920
{
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3921
	rollback_data_t *cbp = data;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3922
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3923
	if (!cbp->cb_dependent) {
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3924
		if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3925
		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3926
		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3927
		    cbp->cb_create) {
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3928
			char *logstr;
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3929
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3930
			cbp->cb_dependent = B_TRUE;
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  3931
			if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy,
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  3932
			    cbp) != 0)
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  3933
				cbp->cb_error = 1;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3934
			cbp->cb_dependent = B_FALSE;
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3935
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3936
			logstr = zhp->zfs_hdl->libzfs_log_str;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3937
			zhp->zfs_hdl->libzfs_log_str = NULL;
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3938
			if (zfs_destroy(zhp) != 0)
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3939
				cbp->cb_error = 1;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3940
			else
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3941
				changelist_remove(zhp, cbp->cb_clp);
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3942
			zhp->zfs_hdl->libzfs_log_str = logstr;
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3943
		}
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3944
	} else {
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3945
		if (zfs_destroy(zhp) != 0)
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3946
			cbp->cb_error = 1;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3947
		else
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3948
			changelist_remove(zhp, cbp->cb_clp);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3949
	}
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3950
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3951
	zfs_close(zhp);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3952
	return (0);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3953
}
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3954
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3955
/*
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3956
 * Rollback the dataset to its latest snapshot.
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3957
 */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3958
static int
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3959
do_rollback(zfs_handle_t *zhp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3960
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3961
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3962
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3963
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3964
	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3965
	    zhp->zfs_type == ZFS_TYPE_VOLUME);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3966
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3967
	if (zhp->zfs_type == ZFS_TYPE_VOLUME &&
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3968
	    zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3969
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3970
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3971
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3972
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  3973
	if (ZFS_IS_VOLUME(zhp))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3974
		zc.zc_objset_type = DMU_OST_ZVOL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3975
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3976
		zc.zc_objset_type = DMU_OST_ZFS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3977
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3978
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3979
	 * We rely on the consumer to verify that there are no newer snapshots
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3980
	 * for the given dataset.  Given these constraints, we can simply pass
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3981
	 * the name on to the ioctl() call.  There is still an unlikely race
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3982
	 * condition where the user has taken a snapshot since we verified that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3983
	 * this was the most recent.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3984
	 */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3985
	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  3986
		(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3987
		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3988
		    zhp->zfs_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3989
	} else if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  3990
		ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3991
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3992
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3993
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3994
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3995
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3996
/*
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3997
 * Given a dataset, rollback to a specific snapshot, discarding any
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3998
 * data changes since then and making it the active dataset.
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  3999
 *
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4000
 * Any snapshots more recent than the target are destroyed, along with
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4001
 * their dependents.
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4002
 */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4003
int
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4004
zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag)
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4005
{
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4006
	int ret;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4007
	rollback_data_t cb = { 0 };
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4008
	prop_changelist_t *clp;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4009
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4010
	/*
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4011
	 * Unmount all dependendents of the dataset and the dataset itself.
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4012
	 * The list we need to gather is the same as for doing rename
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4013
	 */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4014
	clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4015
	if (clp == NULL)
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4016
		return (-1);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4017
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4018
	if ((ret = changelist_prefix(clp)) != 0)
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4019
		goto out;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4020
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4021
	/*
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4022
	 * Destroy all recent snapshots and its dependends.
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4023
	 */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4024
	cb.cb_target = snap->zfs_name;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4025
	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4026
	cb.cb_clp = clp;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4027
	(void) zfs_iter_children(zhp, rollback_destroy, &cb);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4028
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4029
	if ((ret = cb.cb_error) != 0) {
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4030
		(void) changelist_postfix(clp);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4031
		goto out;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4032
	}
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4033
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4034
	/*
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4035
	 * Now that we have verified that the snapshot is the latest,
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4036
	 * rollback to the given snapshot.
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4037
	 */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4038
	ret = do_rollback(zhp);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4039
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4040
	if (ret != 0) {
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4041
		(void) changelist_postfix(clp);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4042
		goto out;
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4043
	}
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4044
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4045
	/*
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4046
	 * We only want to re-mount the filesystem if it was mounted in the
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4047
	 * first place.
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4048
	 */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4049
	ret = changelist_postfix(clp);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4050
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4051
out:
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4052
	changelist_free(clp);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4053
	return (ret);
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4054
}
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4055
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1175
diff changeset
  4056
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4057
 * Iterate over all dependents for a given dataset.  This includes both
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4058
 * hierarchical dependents (children) and data dependents (snapshots and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4059
 * clones).  The bulk of the processing occurs in get_dependents() in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4060
 * libzfs_graph.c.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4061
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4062
int
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  4063
zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  4064
    zfs_iter_f func, void *data)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4065
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4066
	char **dependents;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4067
	size_t count;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4068
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4069
	zfs_handle_t *child;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4070
	int ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4071
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  4072
	if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  4073
	    &dependents, &count) != 0)
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  4074
		return (-1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2440
diff changeset
  4075
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4076
	for (i = 0; i < count; i++) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4077
		if ((child = make_dataset_handle(zhp->zfs_hdl,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4078
		    dependents[i])) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4079
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4080
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4081
		if ((ret = func(child, data)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4082
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4083
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4084
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4085
	for (i = 0; i < count; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4086
		free(dependents[i]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4087
	free(dependents);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4088
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4089
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4090
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4091
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4092
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4093
 * Renames the given dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4094
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4095
int
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  4096
zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4097
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4098
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4099
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4100
	char *delim;
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4101
	prop_changelist_t *cl = NULL;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4102
	zfs_handle_t *zhrp = NULL;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4103
	char *parentname = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4104
	char parent[ZFS_MAXNAMELEN];
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4105
	libzfs_handle_t *hdl = zhp->zfs_hdl;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4106
	char errbuf[1024];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4107
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4108
	/* if we have the same exact name, just return success */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4109
	if (strcmp(zhp->zfs_name, target) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4110
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4111
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4112
	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4113
	    "cannot rename to '%s'"), target);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4114
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4115
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4116
	 * Make sure the target name is valid
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4117
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4118
	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4119
		if ((strchr(target, '@') == NULL) ||
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4120
		    *target == '@') {
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4121
			/*
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4122
			 * Snapshot target name is abbreviated,
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4123
			 * reconstruct full dataset name
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4124
			 */
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4125
			(void) strlcpy(parent, zhp->zfs_name,
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4126
			    sizeof (parent));
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4127
			delim = strchr(parent, '@');
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4128
			if (strchr(target, '@') == NULL)
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4129
				*(++delim) = '\0';
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4130
			else
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4131
				*delim = '\0';
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4132
			(void) strlcat(parent, target, sizeof (parent));
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4133
			target = parent;
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4134
		} else {
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4135
			/*
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4136
			 * Make sure we're renaming within the same dataset.
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4137
			 */
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4138
			delim = strchr(target, '@');
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4139
			if (strncmp(zhp->zfs_name, target, delim - target)
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4140
			    != 0 || zhp->zfs_name[delim - target] != '@') {
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4141
				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4142
				    "snapshots must be part of same "
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4143
				    "dataset"));
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4144
				return (zfs_error(hdl, EZFS_CROSSTARGET,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4145
				    errbuf));
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4146
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4147
		}
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4148
		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4149
			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4150
	} else {
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4151
		if (recursive) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4152
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4153
			    "recursive rename must be a snapshot"));
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4154
			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4155
		}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4156
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4157
		if (!zfs_validate_name(hdl, target, zhp->zfs_type))
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4158
			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4159
		uint64_t unused;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4160
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4161
		/* validate parents */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4451
diff changeset
  4162
		if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4163
			return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4165
		(void) parent_name(target, parent, sizeof (parent));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4166
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4167
		/* make sure we're in the same pool */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4168
		verify((delim = strchr(target, '/')) != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4169
		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4170
		    zhp->zfs_name[delim - target] != '/') {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4171
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4172
			    "datasets must be within same pool"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4173
			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4174
		}
2440
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4175
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4176
		/* new name cannot be a child of the current dataset name */
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4177
		if (strncmp(parent, zhp->zfs_name,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4178
		    strlen(zhp->zfs_name)) == 0) {
2440
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4179
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4180
			    "New dataset name cannot be a descendent of "
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4181
			    "current dataset name"));
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4182
			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
9281a87e8a9e 6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents: 2417
diff changeset
  4183
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4184
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4185
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4186
	(void) snprintf(errbuf, sizeof (errbuf),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4187
	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4188
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4189
	if (getzoneid() == GLOBAL_ZONEID &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4190
	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4191
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4192
		    "dataset is used in a non-global zone"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4193
		return (zfs_error(hdl, EZFS_ZONED, errbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4194
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4195
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4196
	if (recursive) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4197
		struct destroydata dd;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4198
4183
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4199
		parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4200
		if (parentname == NULL) {
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4201
			ret = -1;
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4202
			goto error;
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4203
		}
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4204
		delim = strchr(parentname, '@');
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4205
		*delim = '\0';
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4206
		zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4207
		if (zhrp == NULL) {
4183
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4208
			ret = -1;
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4209
			goto error;
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4210
		}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4211
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4212
		dd.snapname = delim + 1;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4213
		dd.gotone = B_FALSE;
4183
8dbff10731ef 6550857 recursive rename leaks memory and segfaults on failure
mmusante
parents: 4180
diff changeset
  4214
		dd.closezhp = B_TRUE;
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4215
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4216
		/* We remove any zvol links prior to renaming them */
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4217
		ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4218
		if (ret) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4219
			goto error;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4220
		}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4221
	} else {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4222
		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL)
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4223
			return (-1);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4224
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4225
		if (changelist_haszonedchild(cl)) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4226
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4227
			    "child dataset with inherited mountpoint is used "
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4228
			    "in a non-global zone"));
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4229
			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4230
			goto error;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4231
		}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4232
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4233
		if ((ret = changelist_prefix(cl)) != 0)
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4234
			goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4235
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4236
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4237
	if (ZFS_IS_VOLUME(zhp))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4238
		zc.zc_objset_type = DMU_OST_ZVOL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4239
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4240
		zc.zc_objset_type = DMU_OST_ZFS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4241
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4242
	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4243
	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2474
diff changeset
  4244
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4245
	zc.zc_cookie = recursive;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4246
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4247
	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4248
		/*
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4249
		 * if it was recursive, the one that actually failed will
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4250
		 * be in zc.zc_name
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4251
		 */
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4252
		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4253
		    "cannot rename to '%s'"), zc.zc_name);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4254
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4255
		if (recursive && errno == EEXIST) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4256
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4257
			    "a child dataset already has a snapshot "
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4258
			    "with the new name"));
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4259
			(void) zfs_error(hdl, EZFS_CROSSTARGET, errbuf);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4260
		} else {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4261
			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4262
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4263
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4264
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4265
		 * On failure, we still want to remount any filesystems that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4266
		 * were previously mounted, so we don't alter the system state.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4267
		 */
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4268
		if (recursive) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4269
			struct createdata cd;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4270
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4271
			/* only create links for datasets that had existed */
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4272
			cd.cd_snapname = delim + 1;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4273
			cd.cd_ifexists = B_TRUE;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4274
			(void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4275
			    &cd);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4276
		} else {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4277
			(void) changelist_postfix(cl);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4278
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4279
	} else {
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4280
		if (recursive) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4281
			struct createdata cd;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4282
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4283
			/* only create links for datasets that had existed */
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4284
			cd.cd_snapname = strchr(target, '@') + 1;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4285
			cd.cd_ifexists = B_TRUE;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4286
			ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4287
			    &cd);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4288
		} else {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4289
			changelist_rename(cl, zfs_get_name(zhp), target);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4290
			ret = changelist_postfix(cl);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4291
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4292
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4293
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4294
error:
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4295
	if (parentname) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4296
		free(parentname);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4297
	}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4298
	if (zhrp) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4299
		zfs_close(zhrp);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4300
	}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4301
	if (cl) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4302
		changelist_free(cl);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4303
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4304
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4305
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4306
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4307
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4308
 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4309
 * poke devfsadm to create the /dev link, and then wait for the link to appear.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4310
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4311
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4312
zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4313
{
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4314
	return (zvol_create_link_common(hdl, dataset, B_FALSE));
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4315
}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4316
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4317
static int
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4318
zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4319
{
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4320
	zfs_cmd_t zc = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4321
	di_devlink_handle_t dhdl;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4322
	priv_set_t *priv_effective;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4323
	int privileged;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4324
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4325
	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4326
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4327
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4328
	 * Issue the appropriate ioctl.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4329
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4330
	if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4331
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4332
		case EEXIST:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4333
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4334
			 * Silently ignore the case where the link already
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4335
			 * exists.  This allows 'zfs volinit' to be run multiple
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4336
			 * times without errors.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4337
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4338
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4339
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4340
		case ENOENT:
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4341
			/*
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4342
			 * Dataset does not exist in the kernel.  If we
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4343
			 * don't care (see zfs_rename), then ignore the
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4344
			 * error quietly.
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4345
			 */
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4346
			if (ifexists) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4347
				return (0);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4348
			}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4349
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4350
			/* FALLTHROUGH */
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  4351
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4352
		default:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  4353
			return (zfs_standard_error_fmt(hdl, errno,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4354
			    dgettext(TEXT_DOMAIN, "cannot create device links "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4355
			    "for '%s'"), dataset));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4356
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4357
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4358
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4359
	/*
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4360
	 * If privileged call devfsadm and wait for the links to
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4361
	 * magically appear.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4362
	 * Otherwise, print out an informational message.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4363
	 */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4364
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4365
	priv_effective = priv_allocset();
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4366
	(void) getppriv(PRIV_EFFECTIVE, priv_effective);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4367
	privileged = (priv_isfullset(priv_effective) == B_TRUE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4368
	priv_freeset(priv_effective);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4369
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4370
	if (privileged) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4371
		if ((dhdl = di_devlink_init(ZFS_DRIVER,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4372
		    DI_MAKE_LINK)) == NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4373
			zfs_error_aux(hdl, strerror(errno));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4374
			(void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4375
			    dgettext(TEXT_DOMAIN, "cannot create device links "
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4376
			    "for '%s'"), dataset);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4377
			(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4378
			return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4379
		} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4380
			(void) di_devlink_fini(&dhdl);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4381
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4382
	} else {
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4383
		char pathname[MAXPATHLEN];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4384
		struct stat64 statbuf;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4385
		int i;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4386
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4387
#define	MAX_WAIT	10
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4388
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4389
		/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4390
		 * This is the poor mans way of waiting for the link
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4391
		 * to show up.  If after 10 seconds we still don't
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4392
		 * have it, then print out a message.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4393
		 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4394
		(void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4395
		    dataset);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4396
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4397
		for (i = 0; i != MAX_WAIT; i++) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4398
			if (stat64(pathname, &statbuf) == 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4399
				break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4400
			(void) sleep(1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4401
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4402
		if (i == MAX_WAIT)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4403
			(void) printf(gettext("%s may not be immediately "
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4404
			    "available\n"), pathname);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4405
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4406
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4407
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4408
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4409
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4410
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4411
 * Remove a minor node for the given zvol and the associated /dev links.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4412
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4413
int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4414
zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4415
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4416
	zfs_cmd_t zc = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4417
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4418
	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4419
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4420
	if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4421
		switch (errno) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4422
		case ENXIO:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4423
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4424
			 * Silently ignore the case where the link no longer
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4425
			 * exists, so that 'zfs volfini' can be run multiple
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4426
			 * times without errors.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4427
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4428
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4429
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4430
		default:
3237
98d0c28f2f5e 6480245 renaming a dataset to something with '%s' will cause segfault
lling
parents: 3234
diff changeset
  4431
			return (zfs_standard_error_fmt(hdl, errno,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4432
			    dgettext(TEXT_DOMAIN, "cannot remove device "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1793
diff changeset
  4433
			    "links for '%s'"), dataset));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4434
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4435
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4436
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4437
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4438
}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4439
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4440
nvlist_t *
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4441
zfs_get_user_props(zfs_handle_t *zhp)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4442
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4443
	return (zhp->zfs_user_props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4444
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4445
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4446
/*
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4217
diff changeset
  4447
 * Given a comma-separated list of properties, construct a property list
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4448
 * containing both user-defined and native properties.  This function will
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4449
 * return a NULL list if 'all' is specified, which can later be expanded on a
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4450
 * per-dataset basis by zfs_expand_proplist().
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4451
 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4452
int
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4453
zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4454
    zfs_proplist_t **listp, zfs_type_t type)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4455
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4456
	size_t len;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4457
	char *s, *p;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4458
	char c;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4459
	zfs_prop_t prop;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4460
	zfs_proplist_t *entry;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4461
	zfs_proplist_t **last;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4462
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4463
	*listp = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4464
	last = listp;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4465
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4466
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4467
	 * If 'all' is specified, return a NULL list.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4468
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4469
	if (strcmp(fields, "all") == 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4470
		return (0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4471
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4472
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4473
	 * If no fields were specified, return an error.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4474
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4475
	if (fields[0] == '\0') {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4476
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4477
		    "no properties specified"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4478
		return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4479
		    "bad property list")));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4480
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4481
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4482
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4483
	 * It would be nice to use getsubopt() here, but the inclusion of column
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4484
	 * aliases makes this more effort than it's worth.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4485
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4486
	s = fields;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4487
	while (*s != '\0') {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4488
		if ((p = strchr(s, ',')) == NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4489
			len = strlen(s);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4490
			p = s + len;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4491
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4492
			len = p - s;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4493
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4494
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4495
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4496
		 * Check for empty options.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4497
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4498
		if (len == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4499
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4500
			    "empty property name"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4501
			return (zfs_error(hdl, EZFS_BADPROP,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4502
			    dgettext(TEXT_DOMAIN, "bad property list")));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4503
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4504
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4505
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4506
		 * Check all regular property names.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4507
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4508
		c = s[len];
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4509
		s[len] = '\0';
4451
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4217
diff changeset
  4510
		prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) :
24fbf2d7a5d7 PSARC 2007/197 ZFS hotplug
eschrock
parents: 4217
diff changeset
  4511
		    zfs_name_to_prop(s);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4512
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4513
		if (prop != ZFS_PROP_INVAL &&
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4514
		    !zfs_prop_valid_for_type(prop, type))
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4515
			prop = ZFS_PROP_INVAL;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4516
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4517
		/*
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4518
		 * When no property table entry can be found, return failure if
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4519
		 * this is a pool property or if this isn't a user-defined
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4520
		 * dataset property,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4521
		 */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4522
		if (prop == ZFS_PROP_INVAL &&
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4523
		    (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4524
			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4525
			    "invalid property '%s'"), s);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4526
			return (zfs_error(hdl, EZFS_BADPROP,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4527
			    dgettext(TEXT_DOMAIN, "bad property list")));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4528
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4529
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4530
		if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4531
			return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4532
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4533
		entry->pl_prop = prop;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4534
		if (prop == ZFS_PROP_INVAL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4535
			if ((entry->pl_user_prop =
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4536
			    zfs_strdup(hdl, s)) == NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4537
				free(entry);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4538
				return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4539
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4540
			entry->pl_width = strlen(s);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4541
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4542
			entry->pl_width = zfs_prop_width(prop,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4543
			    &entry->pl_fixed);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4544
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4545
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4546
		*last = entry;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4547
		last = &entry->pl_next;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4548
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4549
		s = p;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4550
		if (c == ',')
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4551
			s++;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4552
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4553
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4554
	return (0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4555
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4556
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4557
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4558
zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4559
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4560
	return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4561
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4562
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4563
void
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4564
zfs_free_proplist(zfs_proplist_t *pl)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4565
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4566
	zfs_proplist_t *next;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4567
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4568
	while (pl != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4569
		next = pl->pl_next;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4570
		free(pl->pl_user_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4571
		free(pl);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4572
		pl = next;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4573
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4574
}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4575
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4576
typedef struct expand_data {
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4577
	zfs_proplist_t	**last;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4578
	libzfs_handle_t	*hdl;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4579
} expand_data_t;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4580
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4581
static zfs_prop_t
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4582
zfs_expand_proplist_cb(zfs_prop_t prop, void *cb)
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4583
{
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4584
	zfs_proplist_t *entry;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4585
	expand_data_t *edp = cb;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4586
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4587
	if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL)
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4588
		return (ZFS_PROP_INVAL);
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4589
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4590
	entry->pl_prop = prop;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4591
	entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed);
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4592
	entry->pl_all = B_TRUE;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4593
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4594
	*(edp->last) = entry;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4595
	edp->last = &entry->pl_next;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4596
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4597
	return (ZFS_PROP_CONT);
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4598
}
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4599
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4600
int
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4601
zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4602
	zfs_type_t type)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4603
{
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4604
	zfs_proplist_t *entry;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4605
	zfs_proplist_t **last;
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4606
	expand_data_t exp;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4607
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4608
	if (*plp == NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4609
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4610
		 * If this is the very first time we've been called for an 'all'
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4611
		 * specification, expand the list to include all native
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4612
		 * properties.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4613
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4614
		last = plp;
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4615
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4616
		exp.last = last;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4617
		exp.hdl = hdl;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4618
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4619
		if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type,
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4620
		    B_FALSE) == ZFS_PROP_INVAL)
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3504
diff changeset
  4621
			return (-1);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4622
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4623
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4624
		 * Add 'name' to the beginning of the list, which is handled
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4625
		 * specially.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4626
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4627
		if ((entry = zfs_alloc(hdl,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4628
		    sizeof (zfs_proplist_t))) == NULL)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4629
			return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4630
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4631
		entry->pl_prop = ZFS_PROP_NAME;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4632
		entry->pl_width = zfs_prop_width(ZFS_PROP_NAME,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4633
		    &entry->pl_fixed);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4634
		entry->pl_all = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4635
		entry->pl_next = *plp;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4636
		*plp = entry;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4637
	}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4638
	return (0);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4639
}
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4640
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4641
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4642
 * This function is used by 'zfs list' to determine the exact set of columns to
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4643
 * display, and their maximum widths.  This does two main things:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4644
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4645
 *      - If this is a list of all properties, then expand the list to include
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4646
 *        all native properties, and set a flag so that for each dataset we look
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4647
 *        for new unique user properties and add them to the list.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4648
 *
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4649
 *      - For non fixed-width properties, keep track of the maximum width seen
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4650
 *        so that we can size the column appropriately.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4651
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4652
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4653
zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4654
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4655
	libzfs_handle_t *hdl = zhp->zfs_hdl;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4656
	zfs_proplist_t *entry;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4657
	zfs_proplist_t **last, **start;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4658
	nvlist_t *userprops, *propval;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4659
	nvpair_t *elem;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4660
	char *strval;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4661
	char buf[ZFS_MAXPROPLEN];
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4662
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4663
	if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3886
diff changeset
  4664
		return (-1);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4665
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4666
	userprops = zfs_get_user_props(zhp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4667
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4668
	entry = *plp;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4669
	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4670
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4671
		 * Go through and add any user properties as necessary.  We
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4672
		 * start by incrementing our list pointer to the first
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4673
		 * non-native property.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4674
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4675
		start = plp;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4676
		while (*start != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4677
			if ((*start)->pl_prop == ZFS_PROP_INVAL)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4678
				break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4679
			start = &(*start)->pl_next;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4680
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4681
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4682
		elem = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4683
		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4684
			/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4685
			 * See if we've already found this property in our list.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4686
			 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4687
			for (last = start; *last != NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4688
			    last = &(*last)->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4689
				if (strcmp((*last)->pl_user_prop,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4690
				    nvpair_name(elem)) == 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4691
					break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4692
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4693
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4694
			if (*last == NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4695
				if ((entry = zfs_alloc(hdl,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4696
				    sizeof (zfs_proplist_t))) == NULL ||
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4697
				    ((entry->pl_user_prop = zfs_strdup(hdl,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4698
				    nvpair_name(elem)))) == NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4699
					free(entry);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4700
					return (-1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4701
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4702
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4703
				entry->pl_prop = ZFS_PROP_INVAL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4704
				entry->pl_width = strlen(nvpair_name(elem));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4705
				entry->pl_all = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4706
				*last = entry;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4707
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4708
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4709
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4710
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4711
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4712
	 * Now go through and check the width of any non-fixed columns
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4713
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4714
	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4715
		if (entry->pl_fixed)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4716
			continue;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4717
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4718
		if (entry->pl_prop != ZFS_PROP_INVAL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4719
			if (zfs_prop_get(zhp, entry->pl_prop,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4720
			    buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4721
				if (strlen(buf) > entry->pl_width)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4722
					entry->pl_width = strlen(buf);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4723
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4724
		} else if (nvlist_lookup_nvlist(userprops,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4725
		    entry->pl_user_prop, &propval)  == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4726
			verify(nvlist_lookup_string(propval,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4727
			    ZFS_PROP_VALUE, &strval) == 0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4728
			if (strlen(strval) > entry->pl_width)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4729
				entry->pl_width = strlen(strval);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4730
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4731
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4732
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4733
	return (0);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  4734
}
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4735
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4736
int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4737
zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4738
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4739
	zfs_cmd_t zc = { 0 };
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4740
	nvlist_t *nvp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4741
	size_t sz;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4742
	gid_t gid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4743
	uid_t uid;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4744
	const gid_t *groups;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4745
	int group_cnt;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4746
	int error;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4747
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4748
	if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4749
		return (no_memory(hdl));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4750
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4751
	uid = ucred_geteuid(cred);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4752
	gid = ucred_getegid(cred);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4753
	group_cnt = ucred_getgroups(cred, &groups);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4754
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4755
	if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4756
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4757
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4758
	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4759
		nvlist_free(nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4760
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4761
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4762
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4763
	if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4764
		nvlist_free(nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4765
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4766
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4767
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4768
	if (nvlist_add_uint32_array(nvp,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4769
	    ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4770
		nvlist_free(nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4771
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4772
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4773
	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4774
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4775
	if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz))
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4776
		return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4777
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4778
	error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4779
	nvlist_free(nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4780
	return (error);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4781
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4782
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4783
int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4784
zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4785
    void *export, void *sharetab, int sharemax, boolean_t share_on)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4786
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4787
	zfs_cmd_t zc = { 0 };
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4788
	int error;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4789
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4790
	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4791
	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4792
	zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4793
	zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4794
	zc.zc_share.z_sharetype = share_on;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4795
	zc.zc_share.z_sharemax = sharemax;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4796
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4797
	error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4798
	return (error);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4799
}