usr/src/cmd/zfs/zfs_main.c
author marks
Mon, 11 Feb 2008 11:57:00 -0800
changeset 5993 6309c3c0a28f
parent 5904 ac17ccc8902b
child 6168 51c045dcc498
permissions -rw-r--r--
6576659 zfs allow should list delegatable perms
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: 1387
diff changeset
     5
 * Common Development and Distribution License (the "License").
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
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: 2926
diff changeset
    21
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
5904
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
    23
 * Copyright 2008 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>
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
    30
#include <ctype.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <libgen.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 <libuutil.h>
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    35
#include <libnvpair.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <locale.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <stddef.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <stdio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <stdlib.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <strings.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include <unistd.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#include <fcntl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include <zone.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
#include <sys/mkdev.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
#include <sys/mntent.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
#include <sys/mnttab.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
#include <sys/mount.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
#include <sys/stat.h>
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    49
#include <sys/avl.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
#include <libzfs.h>
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    52
#include <libuutil.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
#include "zfs_iter.h"
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    55
#include "zfs_util.h"
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    56
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    57
libzfs_handle_t *g_zfs;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
static FILE *mnttab_file;
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4860
diff changeset
    60
static char history_str[HIS_MAX_RECORD_LEN];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
static int zfs_do_clone(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
static int zfs_do_create(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
static int zfs_do_destroy(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
static int zfs_do_get(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
static int zfs_do_inherit(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
static int zfs_do_list(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
static int zfs_do_mount(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
static int zfs_do_rename(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
static int zfs_do_rollback(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
static int zfs_do_set(int argc, char **argv);
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
    72
static int zfs_do_upgrade(int argc, char **argv);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
static int zfs_do_snapshot(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
static int zfs_do_unmount(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
static int zfs_do_share(int argc, char **argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
static int zfs_do_unshare(int argc, char **argv);
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
    77
static int zfs_do_send(int argc, char **argv);
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
    78
static int zfs_do_receive(int argc, char **argv);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
    79
static int zfs_do_promote(int argc, char **argv);
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    80
static int zfs_do_allow(int argc, char **argv);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
    81
static int zfs_do_unallow(int argc, char **argv);
789
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
 * These libumem hooks provide a reasonable set of defaults for the allocator's
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
 * debugging facilities.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
const char *
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
    88
_umem_debug_init(void)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
	return ("default,verbose"); /* $UMEM_DEBUG setting */
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
const char *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
_umem_logging_init(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
	return ("fail,contents"); /* $UMEM_LOGGING setting */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
    99
typedef enum {
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   100
	HELP_CLONE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   101
	HELP_CREATE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   102
	HELP_DESTROY,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   103
	HELP_GET,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   104
	HELP_INHERIT,
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
   105
	HELP_UPGRADE,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   106
	HELP_LIST,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   107
	HELP_MOUNT,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   108
	HELP_PROMOTE,
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   109
	HELP_RECEIVE,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   110
	HELP_RENAME,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   111
	HELP_ROLLBACK,
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   112
	HELP_SEND,
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   113
	HELP_SET,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   114
	HELP_SHARE,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   115
	HELP_SNAPSHOT,
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   116
	HELP_UNMOUNT,
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   117
	HELP_UNSHARE,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   118
	HELP_ALLOW,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   119
	HELP_UNALLOW
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   120
} zfs_help_t;
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   121
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
typedef struct zfs_command {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
	const char	*name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
	int		(*func)(int argc, char **argv);
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   125
	zfs_help_t	usage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
} zfs_command_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
 * Master command table.  Each ZFS command has a name, associated function, and
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
   130
 * usage message.  The usage messages need to be internationalized, so we have
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
   131
 * to have a function to return the usage message based on a command index.
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   132
 *
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   133
 * These commands are organized according to how they are displayed in the usage
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   134
 * message.  An empty command (one with a NULL name) indicates an empty line in
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   135
 * the generic usage message.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
static zfs_command_t command_table[] = {
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   138
	{ "create",	zfs_do_create,		HELP_CREATE		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   139
	{ "destroy",	zfs_do_destroy,		HELP_DESTROY		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   141
	{ "snapshot",	zfs_do_snapshot,	HELP_SNAPSHOT		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   142
	{ "rollback",	zfs_do_rollback,	HELP_ROLLBACK		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   143
	{ "clone",	zfs_do_clone,		HELP_CLONE		},
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   144
	{ "promote",	zfs_do_promote,		HELP_PROMOTE		},
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   145
	{ "rename",	zfs_do_rename,		HELP_RENAME		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   147
	{ "list",	zfs_do_list,		HELP_LIST		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   149
	{ "set",	zfs_do_set,		HELP_SET		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   150
	{ "get", 	zfs_do_get,		HELP_GET		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   151
	{ "inherit",	zfs_do_inherit,		HELP_INHERIT		},
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
   152
	{ "upgrade",	zfs_do_upgrade,		HELP_UPGRADE		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
	{ NULL },
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   154
	{ "mount",	zfs_do_mount,		HELP_MOUNT		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   155
	{ "unmount",	zfs_do_unmount,		HELP_UNMOUNT		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   156
	{ "share",	zfs_do_share,		HELP_SHARE		},
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   157
	{ "unshare",	zfs_do_unshare,		HELP_UNSHARE		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
	{ NULL },
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   159
	{ "send",	zfs_do_send,		HELP_SEND		},
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   160
	{ "receive",	zfs_do_receive,		HELP_RECEIVE		},
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   161
	{ NULL },
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   162
	{ "allow",	zfs_do_allow,		HELP_ALLOW		},
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   163
	{ NULL },
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   164
	{ "unallow",	zfs_do_unallow,		HELP_UNALLOW		},
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
#define	NCOMMAND	(sizeof (command_table) / sizeof (command_table[0]))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   169
zfs_command_t *current_command;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   171
static const char *
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   172
get_usage(zfs_help_t idx)
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   173
{
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   174
	switch (idx) {
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   175
	case HELP_CLONE:
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   176
		return (gettext("\tclone [-p] <snapshot> "
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   177
		    "<filesystem|volume>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   178
	case HELP_CREATE:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   179
		return (gettext("\tcreate [-p] [-o property=value] ... "
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   180
		    "<filesystem>\n"
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   181
		    "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   182
		    "-V <size> <volume>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   183
	case HELP_DESTROY:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   184
		return (gettext("\tdestroy [-rRf] "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   185
		    "<filesystem|volume|snapshot>\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   186
	case HELP_GET:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   187
		return (gettext("\tget [-rHp] [-o field[,...]] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   188
		    "[-s source[,...]]\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   189
		    "\t    <\"all\" | property[,...]> "
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   190
		    "[filesystem|volume|snapshot] ...\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   191
	case HELP_INHERIT:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   192
		return (gettext("\tinherit [-r] <property> "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   193
		    "<filesystem|volume> ...\n"));
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
   194
	case HELP_UPGRADE:
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
   195
		return (gettext("\tupgrade [-v]\n"
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
   196
		    "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   197
	case HELP_LIST:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   198
		return (gettext("\tlist [-rH] [-o property[,...]] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   199
		    "[-t type[,...]] [-s property] ...\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   200
		    "\t    [-S property] ... "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   201
		    "[filesystem|volume|snapshot] ...\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   202
	case HELP_MOUNT:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   203
		return (gettext("\tmount\n"
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   204
		    "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   205
	case HELP_PROMOTE:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   206
		return (gettext("\tpromote <clone-filesystem>\n"));
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   207
	case HELP_RECEIVE:
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
   208
		return (gettext("\treceive [-vnF] <filesystem|volume|"
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
   209
		"snapshot>\n"
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
   210
		"\treceive [-vnF] -d <filesystem>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   211
	case HELP_RENAME:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   212
		return (gettext("\trename <filesystem|volume|snapshot> "
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
   213
		    "<filesystem|volume|snapshot>\n"
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   214
		    "\trename -p <filesystem|volume> <filesystem|volume>\n"
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
   215
		    "\trename -r <snapshot> <snapshot>"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   216
	case HELP_ROLLBACK:
5568
ac217d9f8190 PSARC/2007/673 zfs rollback -f obsolescence
ahrens
parents: 5481
diff changeset
   217
		return (gettext("\trollback [-rRf] <snapshot>\n"));
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
   218
	case HELP_SEND:
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   219
		return (gettext("\tsend [-R] [-[iI] snapshot] <snapshot>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   220
	case HELP_SET:
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   221
		return (gettext("\tset <property=value> "
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   222
		    "<filesystem|volume> ...\n"));
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   223
	case HELP_SHARE:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   224
		return (gettext("\tshare <-a | filesystem>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   225
	case HELP_SNAPSHOT:
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   226
		return (gettext("\tsnapshot [-r] "
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   227
		    "<filesystem@snapname|volume@snapname>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   228
	case HELP_UNMOUNT:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   229
		return (gettext("\tunmount [-f] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   230
		    "<-a | filesystem|mountpoint>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   231
	case HELP_UNSHARE:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   232
		return (gettext("\tunshare [-f] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   233
		    "<-a | filesystem|mountpoint>\n"));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   234
	case HELP_ALLOW:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   235
		return (gettext("\tallow [-ldug] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   236
		    "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   237
		    "\t    <filesystem|volume>\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   238
		    "\tallow [-ld] -e <perm|@setname>[,...] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   239
		    "<filesystem|volume>\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   240
		    "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   241
		    "\tallow -s @setname <perm|@setname>[,...] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   242
		    "<filesystem|volume>\n"));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   243
	case HELP_UNALLOW:
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   244
		return (gettext("\tunallow [-rldug] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   245
		    "<\"everyone\"|user|group>[,...]\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   246
		    "\t    [<perm|@setname>[,...]] <filesystem|volume>\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   247
		    "\tunallow [-rld] -e [<perm|@setname>[,...]] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   248
		    "<filesystem|volume>\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   249
		    "\tunallow [-r] -c [<perm|@setname>[,...]] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   250
		    "<filesystem|volume>\n"
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   251
		    "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
   252
		    "<filesystem|volume>\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   253
	}
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   254
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   255
	abort();
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   256
	/* NOTREACHED */
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   257
}
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   258
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
 * Utility function to guarantee malloc() success.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
void *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
safe_malloc(size_t size)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
	void *data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
	if ((data = calloc(1, size)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
		(void) fprintf(stderr, "internal error: out of memory\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
		exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	return (data);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   276
 * Callback routine that will print out information for each of
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   277
 * the properties.
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   278
 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   279
static int
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   280
usage_prop_cb(int prop, void *cb)
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   281
{
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   282
	FILE *fp = cb;
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   283
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   284
	(void) fprintf(fp, "\t%-14s ", zfs_prop_to_name(prop));
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   285
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
   286
	if (prop == ZFS_PROP_CASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
   287
		(void) fprintf(fp, "NO    ");
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
   288
	else if (zfs_prop_readonly(prop))
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   289
		(void) fprintf(fp, "  NO    ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   290
	else
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   291
		(void) fprintf(fp, " YES    ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   292
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   293
	if (zfs_prop_inheritable(prop))
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   294
		(void) fprintf(fp, "  YES   ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   295
	else
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   296
		(void) fprintf(fp, "   NO   ");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   297
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   298
	if (zfs_prop_values(prop) == NULL)
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   299
		(void) fprintf(fp, "-\n");
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   300
	else
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   301
		(void) fprintf(fp, "%s\n", zfs_prop_values(prop));
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   302
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   303
	return (ZPROP_CONT);
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   304
}
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   305
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   306
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
 * Display usage message.  If we're inside a command, display only the usage for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
 * that command.  Otherwise, iterate over the entire command table and display
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
 * a complete usage message.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
static void
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   312
usage(boolean_t requested)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
	int i;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   315
	boolean_t show_properties = B_FALSE;
5993
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   316
	boolean_t show_permissions = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
	FILE *fp = requested ? stdout : stderr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
	if (current_command == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
		(void) fprintf(fp, gettext("usage: zfs command args ...\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
		(void) fprintf(fp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
		    gettext("where 'command' is one of the following:\n\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
		for (i = 0; i < NCOMMAND; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
			if (command_table[i].name == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
				(void) fprintf(fp, "\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
				(void) fprintf(fp, "%s",
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   330
				    get_usage(command_table[i].usage));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
		(void) fprintf(fp, gettext("\nEach dataset is of the form: "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
		    "pool/[dataset/]*dataset[@name]\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
		(void) fprintf(fp, gettext("usage:\n"));
1387
7dc47b4865df 6340747 i18n : zfs command line tools don't have correctly i18n'd usage messages
eschrock
parents: 1356
diff changeset
   337
		(void) fprintf(fp, "%s", get_usage(current_command->usage));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
2190
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   340
	if (current_command != NULL &&
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   341
	    (strcmp(current_command->name, "set") == 0 ||
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   342
	    strcmp(current_command->name, "get") == 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
	    strcmp(current_command->name, "inherit") == 0 ||
2190
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   344
	    strcmp(current_command->name, "list") == 0))
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   345
		show_properties = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
5993
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   347
	if (current_command != NULL &&
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   348
	    (strcmp(current_command->name, "allow") == 0 ||
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   349
	    strcmp(current_command->name, "unallow") == 0))
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   350
		show_permissions = B_TRUE;
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   351
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
	if (show_properties) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
		(void) fprintf(fp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
		    gettext("\nThe following properties are supported:\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   357
		(void) fprintf(fp, "\n\t%-14s %s  %s   %s\n\n",
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
		    "PROPERTY", "EDIT", "INHERIT", "VALUES");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   360
		/* Iterate over all properties */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   361
		(void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   362
		    ZFS_TYPE_DATASET);
3654
9c6042b08979 6483718 need a stable interface to get all native properties
gw25295
parents: 3635
diff changeset
   363
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
		(void) fprintf(fp, gettext("\nSizes are specified in bytes "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
		    "with standard units such as K, M, G, etc.\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   366
		(void) fprintf(fp, gettext("\n\nUser-defined properties can "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   367
		    "be specified by using a name containing a colon (:).\n"));
5993
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   368
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   369
	} else if (show_permissions) {
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   370
		(void) fprintf(fp,
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   371
		    gettext("\nThe following permissions are supported:\n"));
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   372
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   373
		zfs_deleg_permissions();
2190
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   374
	} else {
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   375
		/*
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   376
		 * TRANSLATION NOTE:
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   377
		 * "zfs set|get" must not be localised this is the
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   378
		 * command name and arguments.
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   379
		 */
5993
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   380
2190
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   381
		(void) fprintf(fp,
9e8f51a4bf78 6401400 zfs(1) usage output is excessively long
darrenm
parents: 2169
diff changeset
   382
		    gettext("\nFor the property list, run: zfs set|get\n"));
5993
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   383
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   384
		(void) fprintf(fp,
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   385
		    gettext("\nFor the delegated permission list, run:"
6309c3c0a28f 6576659 zfs allow should list delegatable perms
marks
parents: 5904
diff changeset
   386
		    " zfs allow|unallow\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   388
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   389
	/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   390
	 * See comments at end of main().
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   391
	 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   392
	if (getenv("ZFS_ABORT") != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   393
		(void) printf("dumping core by request\n");
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   394
		abort();
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   395
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   396
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
	exit(requested ? 0 : 2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
/*
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   401
 * zfs clone [-p] <snap> <fs | vol>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
 * Given an existing dataset, create a writable copy whose initial contents
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
 * are the same as the source.  The newly created dataset maintains a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
 * dependency on the original; the original cannot be destroyed so long as
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
 * the clone exists.
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   407
 *
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   408
 * The '-p' flag creates all the non-existing ancestors of the target first.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
zfs_do_clone(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
	zfs_handle_t *zhp;
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   414
	boolean_t parents = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
	int ret;
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   416
	int c;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
	/* check options */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   419
	while ((c = getopt(argc, argv, "p")) != -1) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   420
		switch (c) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   421
		case 'p':
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   422
			parents = B_TRUE;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   423
			break;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   424
		case '?':
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   425
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   426
			    optopt);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   427
			usage(B_FALSE);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   428
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   431
	argc -= optind;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   432
	argv += optind;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   433
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
	/* check number of arguments */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   435
	if (argc < 1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
		(void) fprintf(stderr, gettext("missing source dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   438
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
	}
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   440
	if (argc < 2) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
		(void) fprintf(stderr, gettext("missing target dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   443
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
	}
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   445
	if (argc > 2) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   447
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
	/* open the source dataset */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   451
	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   454
	if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   455
	    ZFS_TYPE_VOLUME)) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   456
		/*
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   457
		 * Now create the ancestors of the target dataset.  If the
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   458
		 * target already exists and '-p' option was used we should not
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   459
		 * complain.
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   460
		 */
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   461
		if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   462
		    ZFS_TYPE_VOLUME))
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   463
			return (0);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   464
		if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   465
			return (1);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   466
	}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   467
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
	/* pass to libzfs */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   469
	ret = zfs_clone(zhp, argv[1], NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
	/* create the mountpoint if necessary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   472
	if (ret == 0) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   473
		zfs_handle_t *clone;
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   474
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   475
		clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
		if (clone != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   477
			if ((ret = zfs_mount(clone, NULL, 0)) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   478
				ret = zfs_share(clone);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   479
			zfs_close(clone);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
		}
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
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
	return (ret == 0 ? 0 : 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
/*
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   489
 * zfs create [-p] [-o prop=value] ... fs
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   490
 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
 * Create a new dataset.  This command can be used to create filesystems
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
 * and volumes.  Snapshot creation is handled by 'zfs snapshot'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
 * For volumes, the user must specify a size to be used.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
 * The '-s' flag applies only to volumes, and indicates that we should not try
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
 * to set the reservation for this volume.  By default we set a reservation
5481
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   498
 * equal to the size for any volume.  For pools with SPA_VERSION >=
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   499
 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   500
 *
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   501
 * The '-p' flag creates all the non-existing ancestors of the target first.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
zfs_do_create(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
	zfs_type_t type = ZFS_TYPE_FILESYSTEM;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   507
	zfs_handle_t *zhp = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   508
	uint64_t volsize;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
	int c;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   510
	boolean_t noreserve = B_FALSE;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   511
	boolean_t bflag = B_FALSE;
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   512
	boolean_t parents = B_FALSE;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   513
	int ret = 1;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   514
	nvlist_t *props = NULL;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   515
	uint64_t intval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   516
	char *propname;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   517
	char *propval = NULL;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   518
	char *strval;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   519
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   520
	if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   521
		(void) fprintf(stderr, gettext("internal error: "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   522
		    "out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   523
		return (1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   524
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
	/* check options */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   527
	while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
		case 'V':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
			type = ZFS_TYPE_VOLUME;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   531
			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   532
				(void) fprintf(stderr, gettext("bad volume "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   533
				    "size '%s': %s\n"), optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   534
				    libzfs_error_description(g_zfs));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   535
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   536
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   537
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   538
			if (nvlist_add_uint64(props,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   539
			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   540
			    intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   541
				(void) fprintf(stderr, gettext("internal "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   542
				    "error: out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   543
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   544
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   545
			volsize = intval;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
			break;
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   547
		case 'p':
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   548
			parents = B_TRUE;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   549
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
		case 'b':
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   551
			bflag = B_TRUE;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   552
			if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   553
				(void) fprintf(stderr, gettext("bad volume "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   554
				    "block size '%s': %s\n"), optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   555
				    libzfs_error_description(g_zfs));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   556
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   557
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   558
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   559
			if (nvlist_add_uint64(props,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   560
			    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   561
			    intval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   562
				(void) fprintf(stderr, gettext("internal "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   563
				    "error: out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   564
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   565
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   566
			break;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   567
		case 'o':
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   568
			propname = optarg;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   569
			if ((propval = strchr(propname, '=')) == NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   570
				(void) fprintf(stderr, gettext("missing "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   571
				    "'=' for -o option\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   572
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   573
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   574
			*propval = '\0';
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   575
			propval++;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   576
			if (nvlist_lookup_string(props, propname,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   577
			    &strval) == 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   578
				(void) fprintf(stderr, gettext("property '%s' "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   579
				    "specified multiple times\n"), propname);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   580
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   581
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   582
			if (nvlist_add_string(props, propname, propval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   583
				(void) fprintf(stderr, gettext("internal "
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   584
				    "error: out of memory\n"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   585
				goto error;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   586
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
		case 's':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   589
			noreserve = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
			(void) fprintf(stderr, gettext("missing size "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
			    "argument\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   594
			goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
			    optopt);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   599
			goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   603
	if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   604
		(void) fprintf(stderr, gettext("'-s' and '-b' can only be "
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   605
		    "used when creating a volume\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   606
		goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   609
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
	if (argc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
		(void) fprintf(stderr, gettext("missing %s argument\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
		    zfs_type_to_name(type));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   616
		goto badusage;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
		(void) fprintf(stderr, gettext("too many arguments\n"));
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   620
		goto badusage;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   621
	}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   622
5481
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   623
	if (type == ZFS_TYPE_VOLUME && !noreserve) {
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   624
		zpool_handle_t *zpool_handle;
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   625
		uint64_t spa_version;
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   626
		char *p;
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   627
		zfs_prop_t resv_prop;
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   628
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   629
		if (p = strchr(argv[0], '/'))
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   630
			*p = '\0';
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   631
		zpool_handle = zpool_open(g_zfs, argv[0]);
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   632
		if (p != NULL)
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   633
			*p = '/';
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   634
		if (zpool_handle == NULL)
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   635
			goto error;
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   636
		spa_version = zpool_get_prop_int(zpool_handle,
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   637
		    ZPOOL_PROP_VERSION, NULL);
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   638
		zpool_close(zpool_handle);
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   639
		if (spa_version >= SPA_VERSION_REFRESERVATION)
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   640
			resv_prop = ZFS_PROP_REFRESERVATION;
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   641
		else
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   642
			resv_prop = ZFS_PROP_RESERVATION;
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   643
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   644
		if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   645
		    &strval) != 0) {
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   646
			if (nvlist_add_uint64(props,
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   647
			    zfs_prop_to_name(resv_prop), volsize) != 0) {
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   648
				(void) fprintf(stderr, gettext("internal "
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   649
				    "error: out of memory\n"));
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   650
				nvlist_free(props);
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   651
				return (1);
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5446
diff changeset
   652
			}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   653
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   654
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   655
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   656
	if (parents && zfs_name_valid(argv[0], type)) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   657
		/*
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   658
		 * Now create the ancestors of target dataset.  If the target
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   659
		 * already exists and '-p' option was used we should not
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   660
		 * complain.
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   661
		 */
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   662
		if (zfs_dataset_exists(g_zfs, argv[0], type)) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   663
			ret = 0;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   664
			goto error;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   665
		}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   666
		if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   667
			goto error;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   668
	}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
   669
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
	/* pass to libzfs */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   671
	if (zfs_create(g_zfs, argv[0], type, props) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   672
		goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   674
	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   675
		goto error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
	 * Mount and/or share the new filesystem as appropriate.  We provide a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
	 * verbose error message to let the user know that their filesystem was
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
	 * in fact created, even if we failed to mount or share it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
	if (zfs_mount(zhp, NULL, 0) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
		(void) fprintf(stderr, gettext("filesystem successfully "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
		    "created, but not mounted\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
		ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
	} else if (zfs_share(zhp) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
		(void) fprintf(stderr, gettext("filesystem successfully "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
		    "created, but not shared\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
		ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
		ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   694
error:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   695
	if (zhp)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   696
		zfs_close(zhp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   697
	nvlist_free(props);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
	return (ret);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   699
badusage:
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   700
	nvlist_free(props);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   701
	usage(B_FALSE);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
   702
	return (2);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   704
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
 * zfs destroy [-rf] <fs, snap, vol>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
 * 	-r	Recursively destroy all children
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
 * 	-R	Recursively destroy all dependents, including clones
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
 * 	-f	Force unmounting of any dependents
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
 * Destroys the given dataset.  By default, it will unmount any filesystems,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
 * and refuse to destroy a dataset that has any dependents.  A dependent can
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
 * either be a child, or a clone of a child.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
typedef struct destroy_cbdata {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   717
	boolean_t	cb_first;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
	int		cb_force;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
	int		cb_recurse;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
	int		cb_error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
	int		cb_needforce;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   722
	int		cb_doclones;
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   723
	boolean_t	cb_closezhp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
	zfs_handle_t	*cb_target;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   725
	char		*cb_snapname;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
} destroy_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
 * Check for any dependents based on the '-r' or '-R' flags.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
destroy_check_dependent(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
	destroy_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
	const char *tname = zfs_get_name(cbp->cb_target);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
	const char *name = zfs_get_name(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
	if (strncmp(tname, name, strlen(tname)) == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
	    (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
		 * This is a direct descendant, not a clone somewhere else in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   742
		 * the hierarchy.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   744
		if (cbp->cb_recurse)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
		if (cbp->cb_first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
			(void) fprintf(stderr, gettext("cannot destroy '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
			    "%s has children\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
			    zfs_get_name(cbp->cb_target),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
			(void) fprintf(stderr, gettext("use '-r' to destroy "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
			    "the following datasets:\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   754
			cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
			cbp->cb_error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
		 * This is a clone.  We only want to report this if the '-r'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
		 * wasn't specified, or the target is a snapshot.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
		if (!cbp->cb_recurse &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
		    zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
		if (cbp->cb_first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
			(void) fprintf(stderr, gettext("cannot destroy '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
			    "%s has dependent clones\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
			    zfs_get_name(cbp->cb_target),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
			    zfs_type_to_name(zfs_get_type(cbp->cb_target)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
			(void) fprintf(stderr, gettext("use '-R' to destroy "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
			    "the following datasets:\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   775
			cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
			cbp->cb_error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
destroy_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
	destroy_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   793
	 * Ignore pools (which we've already flagged as an error before getting
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   794
	 * here.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
	if (strchr(zfs_get_name(zhp), '/') == NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   797
	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   798
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   799
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   802
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   803
	 * Bail out on the first error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
	if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
	    zfs_destroy(zhp) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
		return (-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   815
static int
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   816
destroy_snap_clones(zfs_handle_t *zhp, void *arg)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   817
{
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   818
	destroy_cbdata_t *cbp = arg;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   819
	char thissnap[MAXPATHLEN];
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   820
	zfs_handle_t *szhp;
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   821
	boolean_t closezhp = cbp->cb_closezhp;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   822
	int rv;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   823
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   824
	(void) snprintf(thissnap, sizeof (thissnap),
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   825
	    "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   826
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   827
	libzfs_print_on_error(g_zfs, B_FALSE);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   828
	szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   829
	libzfs_print_on_error(g_zfs, B_TRUE);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   830
	if (szhp) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   831
		/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   832
		 * Destroy any clones of this snapshot
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   833
		 */
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   834
		if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   835
		    cbp) != 0) {
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   836
			zfs_close(szhp);
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   837
			if (closezhp)
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   838
				zfs_close(zhp);
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   839
			return (-1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   840
		}
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   841
		zfs_close(szhp);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   842
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   843
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   844
	cbp->cb_closezhp = B_TRUE;
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   845
	rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   846
	if (closezhp)
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   847
		zfs_close(zhp);
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
   848
	return (rv);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   849
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   851
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
zfs_do_destroy(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
	destroy_cbdata_t cb = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
	zfs_handle_t *zhp;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   857
	char *cp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   859
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
	while ((c = getopt(argc, argv, "frR")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
			cb.cb_force = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
		case 'r':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
		case 'R':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
			cb.cb_doclones = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   873
		default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   875
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   876
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   878
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   879
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   880
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   881
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   882
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   883
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
	if (argc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
		(void) fprintf(stderr, gettext("missing path argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   886
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   890
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   892
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   893
	/*
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   894
	 * If we are doing recursive destroy of a snapshot, then the
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   895
	 * named snapshot may not exist.  Go straight to libzfs.
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   896
	 */
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   897
	if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   898
		int ret;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   899
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   900
		*cp = '\0';
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   901
		if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   902
			return (1);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   903
		*cp = '@';
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   904
		cp++;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   905
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   906
		if (cb.cb_doclones) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   907
			cb.cb_snapname = cp;
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   908
			if (destroy_snap_clones(zhp, &cb) != 0) {
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   909
				zfs_close(zhp);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   910
				return (1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   911
			}
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   912
		}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   913
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   914
		ret = zfs_destroy_snaps(zhp, cp);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   915
		zfs_close(zhp);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   916
		if (ret) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   917
			(void) fprintf(stderr,
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   918
			    gettext("no snapshots destroyed\n"));
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   919
		}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   920
		return (ret != 0);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   921
	}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   922
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
   923
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   924
	/* Open the given dataset */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
   925
	if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   926
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   927
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   928
	cb.cb_target = zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   930
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   931
	 * Perform an explicit check for pools before going any further.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   932
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
	if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
	    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   935
		(void) fprintf(stderr, gettext("cannot destroy '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
		    "operation does not apply to pools\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   937
		    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   938
		(void) fprintf(stderr, gettext("use 'zfs destroy -r "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
		    "%s' to destroy all datasets in the pool\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
		    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
		(void) fprintf(stderr, gettext("use 'zpool destroy %s' "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   942
		    "to destroy the pool itself\n"), zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   944
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   945
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   946
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   947
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   948
	 * Check for any dependents and/or clones.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   949
	 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
   950
	cb.cb_first = B_TRUE;
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   951
	if (!cb.cb_doclones &&
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   952
	    zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   953
	    &cb) != 0) {
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   954
		zfs_close(zhp);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   955
		return (1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   956
	}
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   957
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   958
	if (cb.cb_error ||
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   959
	    zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   961
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   962
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   963
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
	/*
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   965
	 * Do the real thing.  The callback will close the handle regardless of
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   966
	 * whether it succeeds or not.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   967
	 */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
   968
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   969
	if (destroy_callback(zhp, &cb) != 0)
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   970
		return (1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   971
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
   972
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
   973
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   974
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   975
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   976
/*
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
   977
 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...]
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
   978
 * 	< all | property[,property]... > < fs | snap | vol > ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   979
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   980
 *	-r	recurse over any child datasets
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   981
 *	-H	scripted mode.  Headers are stripped, and fields are separated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   982
 *		by tabs instead of spaces.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   983
 *	-o	Set of fields to display.  One of "name,property,value,source".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   984
 *		Default is all four.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   985
 *	-s	Set of sources to allow.  One of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   986
 *		"local,default,inherited,temporary,none".  Default is all
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
 *		five.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   988
 *	-p	Display values in parsable (literal) format.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   989
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   990
 *  Prints properties for the given datasets.  The user can control which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   991
 *  columns to display as well as which property types to allow.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   992
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   993
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   995
 * Invoked to display the properties for a single dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   996
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   997
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   998
get_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   999
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1000
	char buf[ZFS_MAXPROPLEN];
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1001
	zprop_source_t sourcetype;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1002
	char source[ZFS_MAXNAMELEN];
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1003
	zprop_get_cbdata_t *cbp = data;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1004
	nvlist_t *userprop = zfs_get_user_props(zhp);
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1005
	zprop_list_t *pl = cbp->cb_proplist;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1006
	nvlist_t *propval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1007
	char *strval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1008
	char *sourceval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1009
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1010
	for (; pl != NULL; pl = pl->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1011
		/*
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1012
		 * Skip the special fake placeholder.  This will also skip over
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1013
		 * the name property when 'all' is specified.
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1014
		 */
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1015
		if (pl->pl_prop == ZFS_PROP_NAME &&
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1016
		    pl == cbp->cb_proplist)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1017
			continue;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1018
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1019
		if (pl->pl_prop != ZPROP_INVAL) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1020
			if (zfs_prop_get(zhp, pl->pl_prop, buf,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1021
			    sizeof (buf), &sourcetype, source,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1022
			    sizeof (source),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1023
			    cbp->cb_literal) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1024
				if (pl->pl_all)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1025
					continue;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1026
				if (!zfs_prop_valid_for_type(pl->pl_prop,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1027
				    ZFS_TYPE_DATASET)) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1028
					(void) fprintf(stderr,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1029
					    gettext("No such property '%s'\n"),
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1030
					    zfs_prop_to_name(pl->pl_prop));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1031
					continue;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1032
				}
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1033
				sourcetype = ZPROP_SRC_NONE;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1034
				(void) strlcpy(buf, "-", sizeof (buf));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1035
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1036
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1037
			zprop_print_one_property(zfs_get_name(zhp), cbp,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1038
			    zfs_prop_to_name(pl->pl_prop),
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1039
			    buf, sourcetype, source);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1040
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1041
			if (nvlist_lookup_nvlist(userprop,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1042
			    pl->pl_user_prop, &propval) != 0) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1043
				if (pl->pl_all)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1044
					continue;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1045
				sourcetype = ZPROP_SRC_NONE;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1046
				strval = "-";
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1047
			} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1048
				verify(nvlist_lookup_string(propval,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1049
				    ZPROP_VALUE, &strval) == 0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1050
				verify(nvlist_lookup_string(propval,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1051
				    ZPROP_SOURCE, &sourceval) == 0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1052
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1053
				if (strcmp(sourceval,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1054
				    zfs_get_name(zhp)) == 0) {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1055
					sourcetype = ZPROP_SRC_LOCAL;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1056
				} else {
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1057
					sourcetype = ZPROP_SRC_INHERITED;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1058
					(void) strlcpy(source,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1059
					    sourceval, sizeof (source));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1060
				}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1061
			}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1062
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1063
			zprop_print_one_property(zfs_get_name(zhp), cbp,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1064
			    pl->pl_user_prop, strval, sourcetype,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1065
			    source);
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1066
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1068
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1072
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
zfs_do_get(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
{
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1075
	zprop_get_cbdata_t cb = { 0 };
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1076
	boolean_t recurse = B_FALSE;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1077
	int i, c;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1078
	char *value, *fields;
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1079
	int ret;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1080
	zprop_list_t fake_name = { 0 };
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1081
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1082
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1083
	 * Set up default columns and sources.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1084
	 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1085
	cb.cb_sources = ZPROP_SRC_ALL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1086
	cb.cb_columns[0] = GET_COL_NAME;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1087
	cb.cb_columns[1] = GET_COL_PROPERTY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1088
	cb.cb_columns[2] = GET_COL_VALUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1089
	cb.cb_columns[3] = GET_COL_SOURCE;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1090
	cb.cb_type = ZFS_TYPE_DATASET;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1091
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1092
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1093
	while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1094
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1095
		case 'p':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1096
			cb.cb_literal = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1097
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1098
		case 'r':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1099
			recurse = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1100
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1101
		case 'H':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1102
			cb.cb_scripted = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1103
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1106
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1107
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1108
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1109
		case 'o':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1110
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1111
			 * Process the set of columns to display.  We zero out
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1112
			 * the structure to give us a blank slate.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1113
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1114
			bzero(&cb.cb_columns, sizeof (cb.cb_columns));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1115
			i = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1116
			while (*optarg != '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1117
				static char *col_subopts[] =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1118
				    { "name", "property", "value", "source",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1119
				    NULL };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1120
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1121
				if (i == 4) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1122
					(void) fprintf(stderr, gettext("too "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1123
					    "many fields given to -o "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1124
					    "option\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1125
					usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1126
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1127
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1128
				switch (getsubopt(&optarg, col_subopts,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1129
				    &value)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1130
				case 0:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1131
					cb.cb_columns[i++] = GET_COL_NAME;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1132
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1133
				case 1:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1134
					cb.cb_columns[i++] = GET_COL_PROPERTY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1135
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1136
				case 2:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
					cb.cb_columns[i++] = GET_COL_VALUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
				case 3:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
					cb.cb_columns[i++] = GET_COL_SOURCE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1142
				default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1143
					(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1144
					    gettext("invalid column name "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1145
					    "'%s'\n"), value);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1146
					usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1147
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1149
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1150
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1151
		case 's':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1152
			cb.cb_sources = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
			while (*optarg != '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
				static char *source_subopts[] = {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
					"local", "default", "inherited",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1156
					"temporary", "none", NULL };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
				switch (getsubopt(&optarg, source_subopts,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1159
				    &value)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
				case 0:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1161
					cb.cb_sources |= ZPROP_SRC_LOCAL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1162
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
				case 1:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1164
					cb.cb_sources |= ZPROP_SRC_DEFAULT;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
				case 2:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1167
					cb.cb_sources |= ZPROP_SRC_INHERITED;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1168
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1169
				case 3:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1170
					cb.cb_sources |= ZPROP_SRC_TEMPORARY;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1171
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1172
				case 4:
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1173
					cb.cb_sources |= ZPROP_SRC_NONE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1175
				default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1176
					(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1177
					    gettext("invalid source "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1178
					    "'%s'\n"), value);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  1179
					usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1180
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1184
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1185
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1186
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1187
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1188
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1189
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1190
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1191
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1192
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1193
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1194
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
		(void) fprintf(stderr, gettext("missing property "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1196
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1197
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1199
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
	fields = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1202
	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1203
	    != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1204
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1205
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
	argc--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1207
	argv++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1208
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1209
	/*
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1210
	 * As part of zfs_expand_proplist(), we keep track of the maximum column
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1211
	 * width for each property.  For the 'NAME' (and 'SOURCE') columns, we
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1212
	 * need to know the maximum name length.  However, the user likely did
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1213
	 * not specify 'name' as one of the properties to fetch, so we need to
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1214
	 * make sure we always include at least this property for
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1215
	 * print_get_headers() to work properly.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1216
	 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1217
	if (cb.cb_proplist != NULL) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1218
		fake_name.pl_prop = ZFS_PROP_NAME;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1219
		fake_name.pl_width = strlen(gettext("NAME"));
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1220
		fake_name.pl_next = cb.cb_proplist;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1221
		cb.cb_proplist = &fake_name;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1222
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1223
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1224
	cb.cb_first = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1225
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1226
	/* run for each object */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1227
	ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_DATASET, NULL,
3635
63e2169c1249 PSARC 2007/050 zfs list -d
ck153898
parents: 3504
diff changeset
  1228
	    &cb.cb_proplist, get_callback, &cb, B_FALSE);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1229
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1230
	if (cb.cb_proplist == &fake_name)
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1231
		zprop_free_list(fake_name.pl_next);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1232
	else
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1233
		zprop_free_list(cb.cb_proplist);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1234
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1235
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1236
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1238
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1239
 * inherit [-r] <property> <fs|vol> ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1240
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1241
 * 	-r	Recurse over all children
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1242
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1243
 * For each dataset specified on the command line, inherit the given property
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1244
 * from its parent.  Inheriting a property at the pool level will cause it to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1245
 * use the default value.  The '-r' flag will recurse over all children, and is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1246
 * useful for setting a property on a hierarchy-wide basis, regardless of any
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1247
 * local modifications for each dataset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1248
 */
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1249
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1250
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1251
inherit_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1252
{
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1253
	char *propname = data;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1254
	int ret;
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1255
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1256
	ret = zfs_prop_inherit(zhp, propname);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1257
	return (ret != 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1258
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1259
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1260
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1261
zfs_do_inherit(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1262
{
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1263
	boolean_t recurse = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1264
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1265
	zfs_prop_t prop;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1266
	char *propname;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1267
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1268
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1269
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1270
	while ((c = getopt(argc, argv, "r")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1271
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1272
		case 'r':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1273
			recurse = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1274
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1275
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1276
		default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1277
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1278
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1279
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1280
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1281
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1282
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1283
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1284
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1285
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1286
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1287
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1288
		(void) fprintf(stderr, gettext("missing property argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1289
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1290
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1291
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1292
		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1293
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1294
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1295
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1296
	propname = argv[0];
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1297
	argc--;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1298
	argv++;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1299
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1300
	if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1301
		if (zfs_prop_readonly(prop)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1302
			(void) fprintf(stderr, gettext(
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1303
			    "%s property is read-only\n"),
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1304
			    propname);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1305
			return (1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1306
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1307
		if (!zfs_prop_inheritable(prop)) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1308
			(void) fprintf(stderr, gettext("'%s' property cannot "
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1309
			    "be inherited\n"), propname);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1310
			if (prop == ZFS_PROP_QUOTA ||
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1311
			    prop == ZFS_PROP_RESERVATION ||
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1312
			    prop == ZFS_PROP_REFQUOTA ||
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1313
			    prop == ZFS_PROP_REFRESERVATION)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1314
				(void) fprintf(stderr, gettext("use 'zfs set "
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1315
				    "%s=none' to clear\n"), propname);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1316
			return (1);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1317
		}
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1318
	} else if (!zfs_prop_user(propname)) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1319
		(void) fprintf(stderr, gettext("invalid property '%s'\n"),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1320
		    propname);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1321
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1322
	}
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1323
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1324
	ret = zfs_for_each(argc, argv, recurse,
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1325
	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL,
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  1326
	    inherit_callback, propname, B_FALSE);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1327
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1328
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1329
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1331
typedef struct upgrade_cbdata {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1332
	uint64_t cb_numupgraded;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1333
	uint64_t cb_numsamegraded;
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
  1334
	uint64_t cb_numfailed;
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1335
	uint64_t cb_version;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1336
	boolean_t cb_newer;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1337
	boolean_t cb_foundone;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1338
	char cb_lastfs[ZFS_MAXNAMELEN];
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1339
} upgrade_cbdata_t;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1340
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1341
static int
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1342
same_pool(zfs_handle_t *zhp, const char *name)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1343
{
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1344
	int len1 = strcspn(name, "/@");
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1345
	const char *zhname = zfs_get_name(zhp);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1346
	int len2 = strcspn(zhname, "/@");
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1347
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1348
	if (len1 != len2)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1349
		return (B_FALSE);
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4860
diff changeset
  1350
	return (strncmp(name, zhname, len1) == 0);
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1351
}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1352
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1353
static int
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1354
upgrade_list_callback(zfs_handle_t *zhp, void *data)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1355
{
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1356
	upgrade_cbdata_t *cb = data;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1357
	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1358
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1359
	/* list if it's old/new */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1360
	if ((!cb->cb_newer && version < ZPL_VERSION) ||
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1361
	    (cb->cb_newer && version > ZPL_VERSION)) {
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1362
		char *str;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1363
		if (cb->cb_newer) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1364
			str = gettext("The following filesystems are "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1365
			    "formatted using a newer software version and\n"
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1366
			    "cannot be accessed on the current system.\n\n");
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1367
		} else {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1368
			str = gettext("The following filesystems are "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1369
			    "out of date, and can be upgraded.  After being\n"
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1370
			    "upgraded, these filesystems (and any 'zfs send' "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1371
			    "streams generated from\n"
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1372
			    "subsequent snapshots) will no longer be "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1373
			    "accessible by older software versions.\n\n");
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1374
		}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1375
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1376
		if (!cb->cb_foundone) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1377
			(void) puts(str);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1378
			(void) printf(gettext("VER  FILESYSTEM\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1379
			(void) printf(gettext("---  ------------\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1380
			cb->cb_foundone = B_TRUE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1381
		}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1382
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1383
		(void) printf("%2u   %s\n", version, zfs_get_name(zhp));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1384
	}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1385
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1386
	return (0);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1387
}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1388
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1389
static int
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1390
upgrade_set_callback(zfs_handle_t *zhp, void *data)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1391
{
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1392
	upgrade_cbdata_t *cb = data;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1393
	int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1394
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1395
	if (cb->cb_version >= ZPL_VERSION_FUID) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1396
		int spa_version;
5713
f9b7933f3770 PSARC/2007/675 A_MDEV Removal
rm160521
parents: 5568
diff changeset
  1397
f9b7933f3770 PSARC/2007/675 A_MDEV Removal
rm160521
parents: 5568
diff changeset
  1398
		if (zfs_spa_version(zhp, &spa_version) < 0)
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1399
			return (-1);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1400
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1401
		if (spa_version < SPA_VERSION_FUID) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1402
			/* can't upgrade */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1403
			(void) printf(gettext("%s: can not be upgraded; "
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1404
			    "the pool version needs to first be upgraded\nto "
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1405
			    "version %d\n\n"),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1406
			    zfs_get_name(zhp), SPA_VERSION_FUID);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1407
			cb->cb_numfailed++;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1408
			return (0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1409
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1410
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1411
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1412
	/* upgrade */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1413
	if (version < cb->cb_version) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1414
		char verstr[16];
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
  1415
		(void) snprintf(verstr, sizeof (verstr),
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
  1416
		    "%llu", cb->cb_version);
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1417
		if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1418
			/*
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1419
			 * If they did "zfs upgrade -a", then we could
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1420
			 * be doing ioctls to different pools.  We need
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1421
			 * to log this history once to each pool.
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1422
			 */
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4860
diff changeset
  1423
			verify(zpool_stage_history(g_zfs, history_str) == 0);
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1424
		}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1425
		if (zfs_prop_set(zhp, "version", verstr) == 0)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1426
			cb->cb_numupgraded++;
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
  1427
		else
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
  1428
			cb->cb_numfailed++;
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1429
		(void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1430
	} else if (version > cb->cb_version) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1431
		/* can't downgrade */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1432
		(void) printf(gettext("%s: can not be downgraded; "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1433
		    "it is already at version %u\n"),
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1434
		    zfs_get_name(zhp), version);
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
  1435
		cb->cb_numfailed++;
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1436
	} else {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1437
		cb->cb_numsamegraded++;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1438
	}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1439
	return (0);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1440
}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1441
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1442
/*
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1443
 * zfs upgrade
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1444
 * zfs upgrade -v
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1445
 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1446
 */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1447
static int
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1448
zfs_do_upgrade(int argc, char **argv)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1449
{
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1450
	boolean_t recurse = B_FALSE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1451
	boolean_t all = B_FALSE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1452
	boolean_t showversions = B_FALSE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1453
	int ret;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1454
	upgrade_cbdata_t cb = { 0 };
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1455
	char c;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1456
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1457
	/* check options */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1458
	while ((c = getopt(argc, argv, "rvV:a")) != -1) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1459
		switch (c) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1460
		case 'r':
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1461
			recurse = B_TRUE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1462
			break;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1463
		case 'v':
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1464
			showversions = B_TRUE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1465
			break;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1466
		case 'V':
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1467
			if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1468
			    optarg, &cb.cb_version) != 0) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1469
				(void) fprintf(stderr,
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1470
				    gettext("invalid version %s\n"), optarg);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1471
				usage(B_FALSE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1472
			}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1473
			break;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1474
		case 'a':
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1475
			all = B_TRUE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1476
			break;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1477
		case '?':
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1478
		default:
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1479
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1480
			    optopt);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1481
			usage(B_FALSE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1482
		}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1483
	}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1484
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1485
	argc -= optind;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1486
	argv += optind;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1487
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1488
	if ((!all && !argc) && (recurse | cb.cb_version))
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1489
		usage(B_FALSE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1490
	if (showversions && (recurse || all || cb.cb_version || argc))
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1491
		usage(B_FALSE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1492
	if ((all || argc) && (showversions))
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1493
		usage(B_FALSE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1494
	if (all && argc)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1495
		usage(B_FALSE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1496
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1497
	if (showversions) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1498
		/* Show info on available versions. */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1499
		(void) printf(gettext("The following filesystem versions are "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1500
		    "supported:\n\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1501
		(void) printf(gettext("VER  DESCRIPTION\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1502
		(void) printf("---  -----------------------------------------"
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1503
		    "---------------\n");
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1504
		(void) printf(gettext(" 1   Initial ZFS filesystem version\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1505
		(void) printf(gettext(" 2   Enhanced directory entries\n"));
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1506
		(void) printf(gettext(" 3   Case insensitive and File system "
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1507
		    "unique identifer (FUID)\n"));
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1508
		(void) printf(gettext("\nFor more information on a particular "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1509
		    "version, including supported releases, see:\n\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1510
		(void) printf("http://www.opensolaris.org/os/community/zfs/"
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1511
		    "version/zpl/N\n\n");
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1512
		(void) printf(gettext("Where 'N' is the version number.\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1513
		ret = 0;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1514
	} else if (argc || all) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1515
		/* Upgrade filesystems */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1516
		if (cb.cb_version == 0)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1517
			cb.cb_version = ZPL_VERSION;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1518
		ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_FILESYSTEM,
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1519
		    NULL, NULL, upgrade_set_callback, &cb, B_TRUE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1520
		(void) printf(gettext("%llu filesystems upgraded\n"),
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1521
		    cb.cb_numupgraded);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1522
		if (cb.cb_numsamegraded) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1523
			(void) printf(gettext("%llu filesystems already at "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1524
			    "this version\n"),
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1525
			    cb.cb_numsamegraded);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1526
		}
4849
3a61e0a9a953 6536043 arc_byteswap_func_t and dmu_byteswap_func_t are redundant
ahrens
parents: 4787
diff changeset
  1527
		if (cb.cb_numfailed != 0)
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1528
			ret = 1;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1529
	} else {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1530
		/* List old-version filesytems */
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1531
		boolean_t found;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1532
		(void) printf(gettext("This system is currently running "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1533
		    "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1534
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1535
		ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1536
		    NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1537
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1538
		found = cb.cb_foundone;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1539
		cb.cb_foundone = B_FALSE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1540
		cb.cb_newer = B_TRUE;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1541
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1542
		ret = zfs_for_each(0, NULL, B_TRUE, ZFS_TYPE_FILESYSTEM,
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1543
		    NULL, NULL, upgrade_list_callback, &cb, B_TRUE);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1544
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1545
		if (!cb.cb_foundone && !found) {
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1546
			(void) printf(gettext("All filesystems are "
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1547
			    "formatted with the current version.\n"));
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1548
		}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1549
	}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1550
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1551
	return (ret);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1552
}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4543
diff changeset
  1553
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1554
/*
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1555
 * list [-rH] [-o property[,property]...] [-t type[,type]...]
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1556
 *      [-s property [-s property]...] [-S property [-S property]...]
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1557
 *      <dataset> ...
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1558
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1559
 * 	-r	Recurse over all children
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  1560
 * 	-H	Scripted mode; elide headers and separate columns by tabs
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1561
 * 	-o	Control which fields to display.
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1562
 * 	-t	Control which object types to display.
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1563
 *	-s	Specify sort columns, descending order.
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1564
 *	-S	Specify sort columns, ascending order.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1565
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1566
 * When given no arguments, lists all filesystems in the system.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1567
 * Otherwise, list the specified datasets, optionally recursing down them if
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1568
 * '-r' is specified.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1569
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1570
typedef struct list_cbdata {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1571
	boolean_t	cb_first;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1572
	boolean_t	cb_scripted;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1573
	zprop_list_t	*cb_proplist;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
} list_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1575
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1576
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1577
 * Given a list of columns to display, output appropriate headers for each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1578
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1579
static void
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1580
print_header(zprop_list_t *pl)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1581
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1582
	char headerbuf[ZFS_MAXPROPLEN];
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1583
	const char *header;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1584
	int i;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1585
	boolean_t first = B_TRUE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1586
	boolean_t right_justify;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1587
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1588
	for (; pl != NULL; pl = pl->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1589
		if (!first) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1590
			(void) printf("  ");
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1591
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1592
			first = B_FALSE;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1593
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1594
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1595
		right_justify = B_FALSE;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1596
		if (pl->pl_prop != ZPROP_INVAL) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1597
			header = zfs_prop_column_name(pl->pl_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1598
			right_justify = zfs_prop_align_right(pl->pl_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1599
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1600
			for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1601
				headerbuf[i] = toupper(pl->pl_user_prop[i]);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1602
			headerbuf[i] = '\0';
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1603
			header = headerbuf;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1604
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1605
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1606
		if (pl->pl_next == NULL && !right_justify)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1607
			(void) printf("%s", header);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1608
		else if (right_justify)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1609
			(void) printf("%*s", pl->pl_width, header);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1610
		else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1611
			(void) printf("%-*s", pl->pl_width, header);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1612
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1613
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1614
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1615
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1616
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1617
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1618
 * Given a dataset and a list of fields, print out all the properties according
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1619
 * to the described layout.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1620
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1621
static void
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1622
print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1623
{
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1624
	boolean_t first = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1625
	char property[ZFS_MAXPROPLEN];
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1626
	nvlist_t *userprops = zfs_get_user_props(zhp);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1627
	nvlist_t *propval;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1628
	char *propstr;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1629
	boolean_t right_justify;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1630
	int width;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1631
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1632
	for (; pl != NULL; pl = pl->pl_next) {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1633
		if (!first) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1634
			if (scripted)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1635
				(void) printf("\t");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1636
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1637
				(void) printf("  ");
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1638
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1639
			first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1640
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1641
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1642
		right_justify = B_FALSE;
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1643
		if (pl->pl_prop != ZPROP_INVAL) {
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1644
			if (zfs_prop_get(zhp, pl->pl_prop, property,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1645
			    sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1646
				propstr = "-";
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1647
			else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1648
				propstr = property;
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1649
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1650
			right_justify = zfs_prop_align_right(pl->pl_prop);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1651
		} else {
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1652
			if (nvlist_lookup_nvlist(userprops,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1653
			    pl->pl_user_prop, &propval) != 0)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1654
				propstr = "-";
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1655
			else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1656
				verify(nvlist_lookup_string(propval,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1657
				    ZPROP_VALUE, &propstr) == 0);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1658
		}
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1659
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1660
		width = pl->pl_width;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1661
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1662
		/*
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1663
		 * If this is being called in scripted mode, or if this is the
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1664
		 * last column and it is left-justified, don't include a width
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1665
		 * format specifier.
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1666
		 */
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1667
		if (scripted || (pl->pl_next == NULL && !right_justify))
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1668
			(void) printf("%s", propstr);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1669
		else if (right_justify)
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1670
			(void) printf("%*s", width, propstr);
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1671
		else
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1672
			(void) printf("%-*s", width, propstr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1673
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1674
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1675
	(void) printf("\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1676
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1677
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1678
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1679
 * Generic callback function to list a dataset or snapshot.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1680
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1681
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1682
list_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1683
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1684
	list_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1685
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1686
	if (cbp->cb_first) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1687
		if (!cbp->cb_scripted)
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1688
			print_header(cbp->cb_proplist);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1689
		cbp->cb_first = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1690
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1691
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1692
	print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1693
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1694
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1695
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1696
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1697
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1698
zfs_do_list(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1699
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1700
	int c;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1701
	boolean_t recurse = B_FALSE;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1702
	boolean_t scripted = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1703
	static char default_fields[] =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1704
	    "name,used,available,referenced,mountpoint";
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1705
	int types = ZFS_TYPE_DATASET;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1706
	char *fields = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1707
	char *basic_fields = default_fields;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1708
	list_cbdata_t cb = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1709
	char *value;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1710
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1711
	char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL };
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1712
	zfs_sort_column_t *sortcol = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1713
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1714
	/* check options */
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1715
	while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1716
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1717
		case 'o':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1718
			fields = optarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1719
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1720
		case 'r':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1721
			recurse = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1722
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1723
		case 'H':
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1724
			scripted = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1725
			break;
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1726
		case 's':
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1727
			if (zfs_add_sort_column(&sortcol, optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1728
			    B_FALSE) != 0) {
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1729
				(void) fprintf(stderr,
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1730
				    gettext("invalid property '%s'\n"), optarg);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1731
				usage(B_FALSE);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1732
			}
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1733
			break;
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1734
		case 'S':
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1735
			if (zfs_add_sort_column(&sortcol, optarg,
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1736
			    B_TRUE) != 0) {
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1737
				(void) fprintf(stderr,
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1738
				    gettext("invalid property '%s'\n"), optarg);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1739
				usage(B_FALSE);
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1740
			}
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1741
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1742
		case 't':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1743
			types = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1744
			while (*optarg != '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1745
				switch (getsubopt(&optarg, type_subopts,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1746
				    &value)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1747
				case 0:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1748
					types |= ZFS_TYPE_FILESYSTEM;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1749
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1750
				case 1:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1751
					types |= ZFS_TYPE_VOLUME;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1752
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1753
				case 2:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1754
					types |= ZFS_TYPE_SNAPSHOT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1755
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1756
				default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1757
					(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1758
					    gettext("invalid type '%s'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1759
					    value);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1760
					usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1761
				}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1762
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1763
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1764
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1765
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1766
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1767
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1768
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1769
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1770
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1771
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1772
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1773
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1774
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1775
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1776
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1777
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1778
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1779
	if (fields == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1780
		fields = basic_fields;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1781
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1782
	/*
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1783
	 * If the user specifies '-o all', the zprop_get_list() doesn't
866
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1784
	 * normally include the name of the dataset.  For 'zfs list', we always
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1785
	 * want this property to be first.
08bb8e0ae066 comments
eschrock
parents: 789
diff changeset
  1786
	 */
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1787
	if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1788
	    != 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1789
		usage(B_FALSE);
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1790
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1791
	cb.cb_scripted = scripted;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1792
	cb.cb_first = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1793
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  1794
	ret = zfs_for_each(argc, argv, recurse, types, sortcol, &cb.cb_proplist,
3635
63e2169c1249 PSARC 2007/050 zfs list -d
ck153898
parents: 3504
diff changeset
  1795
	    list_callback, &cb, B_TRUE);
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1796
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1797
	zprop_free_list(cb.cb_proplist);
2379
0ff8239b1e93 PSARC 2006/308 zfs list sort option
sjelinek
parents: 2372
diff changeset
  1798
	zfs_free_sort_columns(sortcol);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1799
4221
50b4bab9766b 6540584 zfs list command should not produce "no datasets available" message in script mode
mmusante
parents: 4007
diff changeset
  1800
	if (ret == 0 && cb.cb_first && !cb.cb_scripted)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1801
		(void) printf(gettext("no datasets available\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1802
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1803
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1804
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1805
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1806
/*
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1807
 * zfs rename <fs | snap | vol> <fs | snap | vol>
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1808
 * zfs rename -p <fs | vol> <fs | vol>
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1809
 * zfs rename -r <snap> <snap>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1810
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1811
 * Renames the given dataset to another of the same type.
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1812
 *
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1813
 * The '-p' flag creates all the non-existing ancestors of the target first.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1814
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1815
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1816
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1817
zfs_do_rename(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1818
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1819
	zfs_handle_t *zhp;
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1820
	int c;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1821
	int ret;
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1822
	boolean_t recurse = B_FALSE;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1823
	boolean_t parents = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1824
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1825
	/* check options */
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1826
	while ((c = getopt(argc, argv, "pr")) != -1) {
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1827
		switch (c) {
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1828
		case 'p':
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1829
			parents = B_TRUE;
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1830
			break;
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1831
		case 'r':
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1832
			recurse = B_TRUE;
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1833
			break;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1834
		case '?':
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1835
		default:
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1836
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1837
			    optopt);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1838
			usage(B_FALSE);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1839
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1840
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1841
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1842
	argc -= optind;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1843
	argv += optind;
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1844
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1845
	/* check number of arguments */
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1846
	if (argc < 1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1847
		(void) fprintf(stderr, gettext("missing source dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1848
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1849
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1850
	}
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1851
	if (argc < 2) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1852
		(void) fprintf(stderr, gettext("missing target dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1853
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1854
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1855
	}
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1856
	if (argc > 2) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1857
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1858
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1859
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1860
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1861
	if (recurse && parents) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1862
		(void) fprintf(stderr, gettext("-p and -r options are mutually "
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1863
		    "exclusive\n"));
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1864
		usage(B_FALSE);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1865
	}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1866
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1867
	if (recurse && strchr(argv[0], '@') == 0) {
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1868
		(void) fprintf(stderr, gettext("source dataset for recursive "
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1869
		    "rename must be a snapshot\n"));
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1870
		usage(B_FALSE);
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1871
	}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1872
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1873
	if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  1874
	    ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1875
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1876
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1877
	/* If we were asked and the name looks good, try to create ancestors. */
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1878
	if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1879
	    zfs_create_ancestors(g_zfs, argv[1]) != 0) {
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1880
		zfs_close(zhp);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1881
		return (1);
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1882
	}
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  1883
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3912
diff changeset
  1884
	ret = (zfs_rename(zhp, argv[1], recurse) != 0);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1885
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1886
	zfs_close(zhp);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1887
	return (ret);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1888
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1889
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1890
/*
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1891
 * zfs promote <fs>
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1892
 *
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1893
 * Promotes the given clone fs to be the parent
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1894
 */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1895
/* ARGSUSED */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1896
static int
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1897
zfs_do_promote(int argc, char **argv)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1898
{
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1899
	zfs_handle_t *zhp;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1900
	int ret;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1901
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1902
	/* check options */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1903
	if (argc > 1 && argv[1][0] == '-') {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1904
		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1905
		    argv[1][1]);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1906
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1907
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1908
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1909
	/* check number of arguments */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1910
	if (argc < 2) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1911
		(void) fprintf(stderr, gettext("missing clone filesystem"
2597
21c0f93f2513 6349987 lzjb.c lived longer than expected?
nd150628
parents: 2474
diff changeset
  1912
		    " argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1913
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1914
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1915
	if (argc > 2) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1916
		(void) fprintf(stderr, gettext("too many arguments\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1917
		usage(B_FALSE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1918
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1919
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1920
	zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1921
	if (zhp == NULL)
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1922
		return (1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1923
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1924
	ret = (zfs_promote(zhp) != 0);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1925
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  1926
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1927
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1928
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1929
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1930
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1931
/*
5568
ac217d9f8190 PSARC/2007/673 zfs rollback -f obsolescence
ahrens
parents: 5481
diff changeset
  1932
 * zfs rollback [-rRf] <snapshot>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1934
 * 	-r	Delete any intervening snapshots before doing rollback
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
 * 	-R	Delete any snapshots and their clones
5568
ac217d9f8190 PSARC/2007/673 zfs rollback -f obsolescence
ahrens
parents: 5481
diff changeset
  1936
 * 	-f	ignored for backwards compatability
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1938
 * Given a filesystem, rollback to a specific snapshot, discarding any changes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1939
 * since then and making it the active dataset.  If more recent snapshots exist,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1940
 * the command will complain unless the '-r' flag is given.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1941
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1942
typedef struct rollback_cbdata {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1943
	uint64_t	cb_create;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1944
	boolean_t	cb_first;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1945
	int		cb_doclones;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1946
	char		*cb_target;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1947
	int		cb_error;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1948
	boolean_t	cb_recurse;
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1949
	boolean_t	cb_dependent;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1950
} rollback_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1951
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1952
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1953
 * Report any snapshots more recent than the one specified.  Used when '-r' is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1954
 * not specified.  We reuse this same callback for the snapshot dependents - if
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1955
 * 'cb_dependent' is set, then this is a dependent and we should report it
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1956
 * without checking the transaction group.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1957
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1958
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1959
rollback_check(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1960
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1961
	rollback_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1962
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1963
	if (cbp->cb_doclones) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1964
		zfs_close(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1965
		return (0);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1966
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1967
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1968
	if (!cbp->cb_dependent) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1969
		if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  1970
		    zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1971
		    zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1972
		    cbp->cb_create) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1973
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1974
			if (cbp->cb_first && !cbp->cb_recurse) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1975
				(void) fprintf(stderr, gettext("cannot "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1976
				    "rollback to '%s': more recent snapshots "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1977
				    "exist\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1978
				    cbp->cb_target);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1979
				(void) fprintf(stderr, gettext("use '-r' to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1980
				    "force deletion of the following "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1981
				    "snapshots:\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1982
				cbp->cb_first = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1983
				cbp->cb_error = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1984
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1985
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1986
			if (cbp->cb_recurse) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1987
				cbp->cb_dependent = B_TRUE;
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1988
				if (zfs_iter_dependents(zhp, B_TRUE,
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1989
				    rollback_check, cbp) != 0) {
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1990
					zfs_close(zhp);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1991
					return (-1);
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  1992
				}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  1993
				cbp->cb_dependent = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1994
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1995
				(void) fprintf(stderr, "%s\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1996
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1997
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1998
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1999
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2000
		if (cbp->cb_first && cbp->cb_recurse) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2001
			(void) fprintf(stderr, gettext("cannot rollback to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2002
			    "'%s': clones of previous snapshots exist\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2003
			    cbp->cb_target);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2004
			(void) fprintf(stderr, gettext("use '-R' to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2005
			    "force deletion of the following clones and "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2006
			    "dependents:\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2007
			cbp->cb_first = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2008
			cbp->cb_error = 1;
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
		(void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2012
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2013
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2014
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2015
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2016
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2017
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2018
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2019
zfs_do_rollback(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2020
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2021
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2022
	int c;
5749
78a7afaea267 PSARC/2007/699 zfs rollback -f operative
ahrens
parents: 5713
diff changeset
  2023
	boolean_t force = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2024
	rollback_cbdata_t cb = { 0 };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2025
	zfs_handle_t *zhp, *snap;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2026
	char parentname[ZFS_MAXNAMELEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2027
	char *delim;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2028
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2029
	/* check options */
5568
ac217d9f8190 PSARC/2007/673 zfs rollback -f obsolescence
ahrens
parents: 5481
diff changeset
  2030
	while ((c = getopt(argc, argv, "rRf")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2031
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2032
		case 'r':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2033
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2034
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2035
		case 'R':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2036
			cb.cb_recurse = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2037
			cb.cb_doclones = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2038
			break;
5568
ac217d9f8190 PSARC/2007/673 zfs rollback -f obsolescence
ahrens
parents: 5481
diff changeset
  2039
		case 'f':
5749
78a7afaea267 PSARC/2007/699 zfs rollback -f operative
ahrens
parents: 5713
diff changeset
  2040
			force = B_TRUE;
5568
ac217d9f8190 PSARC/2007/673 zfs rollback -f obsolescence
ahrens
parents: 5481
diff changeset
  2041
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2042
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2043
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2044
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2045
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2046
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2047
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2048
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2049
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2050
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2051
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2052
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2053
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2054
		(void) fprintf(stderr, gettext("missing dataset argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2055
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2056
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2057
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2058
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2059
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2060
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2061
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2062
	/* open the snapshot */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2063
	if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2064
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2065
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  2066
	/* open the parent dataset */
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  2067
	(void) strlcpy(parentname, argv[0], sizeof (parentname));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2068
	verify((delim = strrchr(parentname, '@')) != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2069
	*delim = '\0';
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2070
	if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2071
		zfs_close(snap);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2072
		return (1);
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
	 * Check for more recent snapshots and/or clones based on the presence
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2077
	 * of '-r' and '-R'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2078
	 */
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  2079
	cb.cb_target = argv[0];
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  2080
	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2081
	cb.cb_first = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2082
	cb.cb_error = 0;
2474
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  2083
	if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
c001ad7e0c25 6368751 libzfs interface for mount/umounting all the file systems for a given pool
eschrock
parents: 2379
diff changeset
  2084
		goto out;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2085
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2086
	if ((ret = cb.cb_error) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2087
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2088
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2089
	/*
1294
87b43e5de5ee 6364129 need to unmount any child datasets before doing the rollback
lling
parents: 1264
diff changeset
  2090
	 * Rollback parent to the given snapshot.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2091
	 */
5749
78a7afaea267 PSARC/2007/699 zfs rollback -f operative
ahrens
parents: 5713
diff changeset
  2092
	ret = zfs_rollback(zhp, snap, force);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2093
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2094
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2095
	zfs_close(snap);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2096
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2097
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2098
	if (ret == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2099
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2100
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2101
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2102
}
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
 * zfs set property=value { fs | snap | vol } ...
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2106
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2107
 * Sets the given property for all datasets specified on the command line.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2108
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2109
typedef struct set_cbdata {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2110
	char		*cb_propname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2111
	char		*cb_value;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2112
} set_cbdata_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2113
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2114
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2115
set_callback(zfs_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2116
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2117
	set_cbdata_t *cbp = data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2118
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2119
	if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
2169
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2120
		switch (libzfs_errno(g_zfs)) {
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2121
		case EZFS_MOUNTFAILED:
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2122
			(void) fprintf(stderr, gettext("property may be set "
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2123
			    "but unable to remount filesystem\n"));
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2124
			break;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2125
		case EZFS_SHARENFSFAILED:
2169
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2126
			(void) fprintf(stderr, gettext("property may be set "
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2127
			    "but unable to reshare filesystem\n"));
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2128
			break;
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2129
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2130
		return (1);
2169
0152c8ed1663 6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents: 2082
diff changeset
  2131
	}
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  2132
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2133
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2134
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2135
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2136
zfs_do_set(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2137
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2138
	set_cbdata_t cb;
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2139
	int ret;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2140
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2141
	/* check for options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2142
	if (argc > 1 && argv[1][0] == '-') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2143
		(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2144
		    argv[1][1]);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2145
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2146
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2147
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2148
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2149
	if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2150
		(void) fprintf(stderr, gettext("missing property=value "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2151
		    "argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2152
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2153
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2154
	if (argc < 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2155
		(void) fprintf(stderr, gettext("missing dataset name\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2156
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2157
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2158
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2159
	/* validate property=value argument */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2160
	cb.cb_propname = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2161
	if ((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2162
		(void) fprintf(stderr, gettext("missing value in "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2163
		    "property=value argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2164
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2165
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2166
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2167
	*cb.cb_value = '\0';
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2168
	cb.cb_value++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2169
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2170
	if (*cb.cb_propname == '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2171
		(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2172
		    gettext("missing property in property=value argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2173
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2174
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2175
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2176
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2177
	ret = zfs_for_each(argc - 2, argv + 2, B_FALSE,
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2178
	    ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb, B_FALSE);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2179
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2180
	return (ret);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2181
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2182
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2183
/*
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2184
 * zfs snapshot [-r] <fs@snap>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2185
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2186
 * Creates a snapshot with the given name.  While functionally equivalent to
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2187
 * 'zfs create', it is a separate command to differentiate intent.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2188
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2189
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2190
zfs_do_snapshot(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2191
{
4490
abf035049f7f PSARC 2007/199 zfs {create,clone,rename} -p
vb160487
parents: 4221
diff changeset
  2192
	boolean_t recursive = B_FALSE;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2193
	int ret;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2194
	char c;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2195
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2196
	/* check options */
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2197
	while ((c = getopt(argc, argv, ":r")) != -1) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2198
		switch (c) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2199
		case 'r':
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2200
			recursive = B_TRUE;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2201
			break;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2202
		case '?':
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2203
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2204
			    optopt);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2205
			usage(B_FALSE);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2206
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2207
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2208
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2209
	argc -= optind;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2210
	argv += optind;
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2211
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2212
	/* check number of arguments */
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2213
	if (argc < 1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2214
		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2215
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2216
	}
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2217
	if (argc > 1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2218
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2219
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2220
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2221
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2222
	ret = zfs_snapshot(g_zfs, argv[0], recursive);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2223
	if (ret && recursive)
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2224
		(void) fprintf(stderr, gettext("no snapshots were created\n"));
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2190
diff changeset
  2225
	return (ret != 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2226
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2227
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2228
/*
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2229
 * zfs send [-v] -R [-i|-I <@snap>] <fs@snap>
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2230
 * zfs send [-v] [-i|-I <@snap>] <fs@snap>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2231
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2232
 * Send a backup stream to stdout.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2233
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2234
static int
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  2235
zfs_do_send(int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2236
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2237
	char *fromname = NULL;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2238
	char *toname = NULL;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2239
	char *cp;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2240
	zfs_handle_t *zhp;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2241
	boolean_t doall = B_FALSE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2242
	boolean_t replicate = B_FALSE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2243
	boolean_t fromorigin = B_FALSE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2244
	boolean_t verbose = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2245
	int c, err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2246
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2247
	/* check options */
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2248
	while ((c = getopt(argc, argv, ":i:I:Rv")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2249
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2250
		case 'i':
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2251
			if (fromname)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2252
				usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2253
			fromname = optarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2254
			break;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2255
		case 'I':
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2256
			if (fromname)
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2257
				usage(B_FALSE);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2258
			fromname = optarg;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2259
			doall = B_TRUE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2260
			break;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2261
		case 'R':
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2262
			replicate = B_TRUE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2263
			break;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2264
		case 'v':
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2265
			verbose = B_TRUE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2266
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2267
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2268
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2269
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2270
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2271
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2272
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2273
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2274
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2275
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2276
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2277
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2278
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2279
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2280
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2281
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2282
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2283
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2284
		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2285
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2286
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2287
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2288
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2289
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2290
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2291
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2292
	if (isatty(STDOUT_FILENO)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2293
		(void) fprintf(stderr,
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2294
		    gettext("Error: Stream can not be written to a terminal.\n"
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  2295
		    "You must redirect standard output.\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2296
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2297
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2298
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2299
	cp = strchr(argv[0], '@');
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2300
	if (cp == NULL) {
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2301
		(void) fprintf(stderr,
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2302
		    gettext("argument must be a snapshot\n"));
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2303
		usage(B_FALSE);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2304
	}
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2305
	*cp = '\0';
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2306
	toname = cp + 1;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2307
	zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2308
	if (zhp == NULL)
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2309
		return (1);
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2310
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2311
	/*
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2312
	 * If they specified the full path to the snapshot, chop off
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2313
	 * everything except the short name of the snapshot, but special
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2314
	 * case if they specify the origin.
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2315
	 */
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2316
	if (fromname && (cp = strchr(fromname, '@')) != NULL) {
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2317
		char origin[ZFS_MAXNAMELEN];
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2318
		zprop_source_t src;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2319
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2320
		(void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2321
		    origin, sizeof (origin), &src, NULL, 0, B_FALSE);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2322
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2323
		if (strcmp(origin, fromname) == 0) {
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2324
			fromname = NULL;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2325
			fromorigin = B_TRUE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2326
		} else {
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2327
			*cp = '\0';
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2328
			if (cp != fromname && strcmp(argv[0], fromname)) {
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2329
				(void) fprintf(stderr,
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2330
				    gettext("incremental source must be "
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2331
				    "in same filesystem\n"));
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2332
				usage(B_FALSE);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2333
			}
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2334
			fromname = cp + 1;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2335
			if (strchr(fromname, '@') || strchr(fromname, '/')) {
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2336
				(void) fprintf(stderr,
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2337
				    gettext("invalid incremental source\n"));
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2338
				usage(B_FALSE);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2339
			}
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2340
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2341
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2342
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2343
	if (replicate && fromname == NULL)
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2344
		doall = B_TRUE;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2345
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2346
	err = zfs_send(zhp, fromname, toname, replicate, doall, fromorigin,
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2347
	    verbose, STDOUT_FILENO);
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  2348
	zfs_close(zhp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2349
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2350
	return (err != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2351
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2352
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2353
/*
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2354
 * zfs receive [-dnvF] <fs@snap>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2355
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2356
 * Restore a backup stream from stdin.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2357
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2358
static int
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  2359
zfs_do_receive(int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2360
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2361
	int c, err;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2362
	recvflags_t flags;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2363
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2364
	bzero(&flags, sizeof (recvflags_t));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2365
	/* check options */
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2366
	while ((c = getopt(argc, argv, ":dnvF")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2367
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2368
		case 'd':
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2369
			flags.isprefix = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2370
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2371
		case 'n':
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2372
			flags.dryrun = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2373
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2374
		case 'v':
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2375
			flags.verbose = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2376
			break;
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2377
		case 'F':
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2378
			flags.force = B_TRUE;
2665
7b208a92357b PSARC 2006/479 zfs receive -F
nd150628
parents: 2597
diff changeset
  2379
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2380
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2381
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2382
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2383
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2384
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2385
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2386
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2387
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2388
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2389
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2390
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2391
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2392
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2393
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2394
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2395
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2396
	if (argc < 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2397
		(void) fprintf(stderr, gettext("missing snapshot argument\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2398
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2399
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2400
	if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2401
		(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  2402
		usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2403
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2404
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2405
	if (isatty(STDIN_FILENO)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2406
		(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2407
		    gettext("Error: Backup stream can not be read "
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  2408
		    "from a terminal.\n"
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  2409
		    "You must redirect standard input.\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2410
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2411
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2412
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2413
	err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2414
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2415
	return (err != 0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2416
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2417
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2418
typedef struct allow_cb {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2419
	int  a_permcnt;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2420
	size_t a_treeoffset;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2421
} allow_cb_t;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2422
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2423
static void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2424
zfs_print_perms(avl_tree_t *tree)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2425
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2426
	zfs_perm_node_t *permnode;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2427
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2428
	permnode = avl_first(tree);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2429
	while (permnode != NULL) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2430
		(void) printf("%s", permnode->z_pname);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2431
		permnode = AVL_NEXT(tree, permnode);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2432
		if (permnode)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2433
			(void) printf(",");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2434
		else
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2435
			(void) printf("\n");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2436
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2437
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2438
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2439
/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2440
 * Iterate over user/groups/everyone/... and the call perm_iter
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2441
 * function to print actual permission when tree has >0 nodes.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2442
 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2443
static void
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2444
zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2445
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2446
	zfs_allow_node_t *item;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2447
	avl_tree_t *ptree;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2448
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2449
	item = avl_first(tree);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2450
	while (item) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2451
		ptree = (void *)((char *)item + cb->a_treeoffset);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2452
		if (avl_numnodes(ptree)) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2453
			if (cb->a_permcnt++ == 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2454
				(void) printf("%s\n", banner);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2455
			(void) printf("\t%s", item->z_key);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2456
			/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2457
			 * Avoid an extra space being printed
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2458
			 * for "everyone" which is keyed with a null
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2459
			 * string
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2460
			 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2461
			if (item->z_key[0] != '\0')
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2462
				(void) printf(" ");
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2463
			zfs_print_perms(ptree);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2464
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2465
		item = AVL_NEXT(tree, item);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2466
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2467
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2468
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2469
#define	LINES "-------------------------------------------------------------\n"
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2470
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2471
zfs_print_allows(char *ds)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2472
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2473
	zfs_allow_t *curperms, *perms;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2474
	zfs_handle_t *zhp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2475
	allow_cb_t allowcb = { 0 };
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2476
	char banner[MAXPATHLEN];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2477
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2478
	if (ds[0] == '-')
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2479
		usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2480
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2481
	if (strrchr(ds, '@')) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2482
		(void) fprintf(stderr, gettext("Snapshots don't have 'allow'"
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2483
		    " permissions\n"));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2484
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2485
	}
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2486
	if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2487
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2488
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2489
	if (zfs_perm_get(zhp, &perms)) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2490
		(void) fprintf(stderr,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2491
		    gettext("Failed to retrieve 'allows' on %s\n"), ds);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2492
		zfs_close(zhp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2493
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2494
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2495
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2496
	zfs_close(zhp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2497
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2498
	if (perms != NULL)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2499
		(void) printf("%s", LINES);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2500
	for (curperms = perms; curperms; curperms = curperms->z_next) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2501
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2502
		(void) snprintf(banner, sizeof (banner),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2503
		    "Permission sets on (%s)", curperms->z_setpoint);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2504
		allowcb.a_treeoffset =
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2505
		    offsetof(zfs_allow_node_t, z_localdescend);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2506
		allowcb.a_permcnt = 0;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2507
		zfs_iter_perms(&curperms->z_sets, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2508
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2509
		(void) snprintf(banner, sizeof (banner),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2510
		    "Create time permissions on (%s)", curperms->z_setpoint);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2511
		allowcb.a_treeoffset =
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2512
		    offsetof(zfs_allow_node_t, z_localdescend);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2513
		allowcb.a_permcnt = 0;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2514
		zfs_iter_perms(&curperms->z_crperms, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2515
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2516
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2517
		(void) snprintf(banner, sizeof (banner),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2518
		    "Local permissions on (%s)", curperms->z_setpoint);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2519
		allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2520
		allowcb.a_permcnt = 0;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2521
		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2522
		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2523
		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2524
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2525
		(void) snprintf(banner, sizeof (banner),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2526
		    "Descendent permissions on (%s)", curperms->z_setpoint);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2527
		allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2528
		allowcb.a_permcnt = 0;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2529
		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2530
		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2531
		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2532
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2533
		(void) snprintf(banner, sizeof (banner),
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2534
		    "Local+Descendent permissions on (%s)",
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2535
		    curperms->z_setpoint);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2536
		allowcb.a_treeoffset =
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2537
		    offsetof(zfs_allow_node_t, z_localdescend);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2538
		allowcb.a_permcnt = 0;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2539
		zfs_iter_perms(&curperms->z_user, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2540
		zfs_iter_perms(&curperms->z_group, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2541
		zfs_iter_perms(&curperms->z_everyone, banner, &allowcb);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2542
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2543
		(void) printf("%s", LINES);
2926
acfcfefbc60d PSARC 2006/288 zpool history
ek110237
parents: 2885
diff changeset
  2544
	}
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2545
	zfs_free_allows(perms);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2546
	return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2547
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2548
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2549
#define	ALLOWOPTIONS "ldcsu:g:e"
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2550
#define	UNALLOWOPTIONS "ldcsu:g:er"
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2551
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2552
/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2553
 * Validate options, and build necessary datastructure to display/remove/add
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2554
 * permissions.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2555
 * Returns 0 - If permissions should be added/removed
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2556
 * Returns 1 - If permissions should be displayed.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2557
 * Returns -1 - on failure
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2558
 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2559
int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2560
parse_allow_args(int *argc, char **argv[], boolean_t unallow,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2561
    char **ds, int *recurse, nvlist_t **zperms)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2562
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2563
	int c;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2564
	char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2565
	zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2566
	zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4860
6a4d8017d62d 6592023 zfs allow/unallow has incorrect arguments parse
marks
parents: 4849
diff changeset
  2567
	char *who = NULL;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2568
	char *perms = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2569
	zfs_handle_t *zhp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2570
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2571
	while ((c = getopt(*argc, *argv, options)) != -1) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2572
		switch (c) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2573
		case 'l':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2574
			if (who_type == ZFS_DELEG_CREATE ||
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2575
			    who_type == ZFS_DELEG_NAMED_SET)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2576
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2577
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2578
			deleg_type |= ZFS_DELEG_PERM_LOCAL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2579
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2580
		case 'd':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2581
			if (who_type == ZFS_DELEG_CREATE ||
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2582
			    who_type == ZFS_DELEG_NAMED_SET)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2583
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2584
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2585
			deleg_type |= ZFS_DELEG_PERM_DESCENDENT;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2586
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2587
		case 'r':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2588
			*recurse = B_TRUE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2589
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2590
		case 'c':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2591
			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2592
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2593
			if (deleg_type)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2594
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2595
			who_type = ZFS_DELEG_CREATE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2596
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2597
		case 's':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2598
			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2599
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2600
			if (deleg_type)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2601
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2602
			who_type = ZFS_DELEG_NAMED_SET;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2603
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2604
		case 'u':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2605
			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2606
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2607
			who_type = ZFS_DELEG_USER;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2608
			who = optarg;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2609
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2610
		case 'g':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2611
			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2612
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2613
			who_type = ZFS_DELEG_GROUP;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2614
			who = optarg;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2615
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2616
		case 'e':
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2617
			if (who_type != ZFS_DELEG_WHO_UNKNOWN)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2618
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2619
			who_type = ZFS_DELEG_EVERYONE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2620
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2621
		default:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2622
			usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2623
			break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2624
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2625
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2626
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2627
	if (deleg_type == 0)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2628
		deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2629
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2630
	*argc -= optind;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2631
	*argv += optind;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2632
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2633
	if (unallow == B_FALSE && *argc == 1) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2634
		/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2635
		 * Only print permissions if no options were processed
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2636
		 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2637
		if (optind == 1)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2638
			return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2639
		else
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2640
			usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2641
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2642
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2643
	/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2644
	 * initialize variables for zfs_build_perms based on number
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2645
	 * of arguments.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2646
	 * 3 arguments ==>	zfs [un]allow joe perm,perm,perm <dataset> or
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2647
	 *			zfs [un]allow -s @set1 perm,perm <dataset>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2648
	 * 2 arguments ==>	zfs [un]allow -c perm,perm <dataset> or
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2649
	 *			zfs [un]allow -u|-g <name> perm <dataset> or
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2650
	 *			zfs [un]allow -e perm,perm <dataset>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2651
	 *			zfs unallow joe <dataset>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2652
	 *			zfs unallow -s @set1 <dataset>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2653
	 * 1 argument  ==>	zfs [un]allow -e <dataset> or
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2654
	 *			zfs [un]allow -c <dataset>
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2655
	 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2656
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2657
	switch (*argc) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2658
	case 3:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2659
		perms = (*argv)[1];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2660
		who = (*argv)[0];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2661
		*ds = (*argv)[2];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2662
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2663
		/*
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2664
		 * advance argc/argv for do_allow cases.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2665
		 * for do_allow case make sure who have a know who type
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2666
		 * and its not a permission set.
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2667
		 */
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2668
		if (unallow == B_TRUE) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2669
			*argc -= 2;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2670
			*argv += 2;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2671
		} else if (who_type != ZFS_DELEG_WHO_UNKNOWN &&
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2672
		    who_type != ZFS_DELEG_NAMED_SET)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2673
			usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2674
		break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2675
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2676
	case 2:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2677
		if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE ||
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2678
		    who_type == ZFS_DELEG_CREATE || who != NULL)) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2679
			perms = (*argv)[0];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2680
			*ds = (*argv)[1];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2681
		} else {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2682
			if (unallow == B_FALSE &&
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2683
			    (who_type == ZFS_DELEG_WHO_UNKNOWN ||
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2684
			    who_type == ZFS_DELEG_NAMED_SET))
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2685
				usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2686
			else if (who_type == ZFS_DELEG_WHO_UNKNOWN ||
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2687
			    who_type == ZFS_DELEG_NAMED_SET)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2688
				who = (*argv)[0];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2689
			else if (who_type != ZFS_DELEG_NAMED_SET)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2690
				perms = (*argv)[0];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2691
			*ds = (*argv)[1];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2692
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2693
		if (unallow == B_TRUE) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2694
			(*argc)--;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2695
			(*argv)++;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2696
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2697
		break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2698
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2699
	case 1:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2700
		if (unallow == B_FALSE)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2701
			usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2702
		if (who == NULL && who_type != ZFS_DELEG_CREATE &&
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2703
		    who_type != ZFS_DELEG_EVERYONE)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2704
			usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2705
		*ds = (*argv)[0];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2706
		break;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2707
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2708
	default:
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2709
		usage(B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2710
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2711
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2712
	if (strrchr(*ds, '@')) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2713
		(void) fprintf(stderr,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2714
		    gettext("Can't set or remove 'allow' permissions "
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2715
		    "on snapshots.\n"));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2716
			return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2717
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2718
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2719
	if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_DATASET)) == NULL)
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2720
		return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2721
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2722
	if ((zfs_build_perms(zhp, who, perms,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2723
	    who_type, deleg_type, zperms)) != 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2724
		zfs_close(zhp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2725
		return (-1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2726
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2727
	zfs_close(zhp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2728
	return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2729
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2730
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2731
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2732
zfs_do_allow(int argc, char **argv)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2733
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2734
	char *ds;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2735
	nvlist_t *zperms = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2736
	zfs_handle_t *zhp;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2737
	int unused;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2738
	int ret;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2739
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2740
	if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2741
	    &unused, &zperms)) == -1)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2742
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2743
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2744
	if (ret == 1)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2745
		return (zfs_print_allows(argv[0]));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2746
5094
71a3e95fb9e2 PSARC 2007/342 Enhanced ZFS Pool Properties
lling
parents: 4988
diff changeset
  2747
	if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_DATASET)) == NULL)
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2748
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2749
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2750
	if (zfs_perm_set(zhp, zperms)) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2751
		zfs_close(zhp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2752
		nvlist_free(zperms);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2753
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2754
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2755
	nvlist_free(zperms);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2756
	zfs_close(zhp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2757
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2758
	return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2759
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2760
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2761
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2762
unallow_callback(zfs_handle_t *zhp, void *data)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2763
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2764
	nvlist_t *nvp = (nvlist_t *)data;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2765
	int error;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2766
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2767
	error = zfs_perm_remove(zhp, nvp);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2768
	if (error) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2769
		(void) fprintf(stderr, gettext("Failed to remove permissions "
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2770
		    "on %s\n"), zfs_get_name(zhp));
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2771
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2772
	return (error);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2773
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2774
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2775
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2776
zfs_do_unallow(int argc, char **argv)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2777
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2778
	int recurse = B_FALSE;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2779
	char *ds;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2780
	int error;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2781
	nvlist_t *zperms = NULL;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2782
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2783
	if (parse_allow_args(&argc, &argv, B_TRUE,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2784
	    &ds, &recurse, &zperms) == -1)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2785
		return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2786
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2787
	error = zfs_for_each(argc, argv, recurse,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2788
	    ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2789
	    NULL, unallow_callback, (void *)zperms, B_FALSE);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2790
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2791
	if (zperms)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2792
		nvlist_free(zperms);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2793
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  2794
	return (error);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2795
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2796
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2797
typedef struct get_all_cbdata {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2798
	zfs_handle_t	**cb_handles;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2799
	size_t		cb_alloc;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2800
	size_t		cb_used;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2801
	uint_t		cb_types;
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2802
	boolean_t	cb_verbose;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2803
} get_all_cbdata_t;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2804
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2805
#define	CHECK_SPINNER 30
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2806
#define	SPINNER_TIME 3		/* seconds */
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2807
#define	MOUNT_TIME 5		/* seconds */
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2808
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2809
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2810
get_one_dataset(zfs_handle_t *zhp, void *data)
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2811
{
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2812
	static char spin[] = { '-', '\\', '|', '/' };
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2813
	static int spinval = 0;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2814
	static int spincheck = 0;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2815
	static time_t last_spin_time = (time_t)0;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2816
	get_all_cbdata_t *cbp = data;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2817
	zfs_type_t type = zfs_get_type(zhp);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2818
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2819
	if (cbp->cb_verbose) {
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2820
		if (--spincheck < 0) {
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2821
			time_t now = time(NULL);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2822
			if (last_spin_time + SPINNER_TIME < now) {
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2823
				(void) printf("\b%c", spin[spinval++ % 4]);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2824
				(void) fflush(stdout);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2825
				last_spin_time = now;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2826
			}
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2827
			spincheck = CHECK_SPINNER;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2828
		}
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2829
	}
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2830
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2831
	/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2832
	 * Interate over any nested datasets.
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2833
	 */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2834
	if (type == ZFS_TYPE_FILESYSTEM &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2835
	    zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
3265
967e0fca6143 6463140 zfs recv with a snapshot name that has 2 @@ in a row succeeds
ahrens
parents: 3126
diff changeset
  2836
		zfs_close(zhp);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2837
		return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2838
	}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2839
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2840
	/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2841
	 * Skip any datasets whose type does not match.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2842
	 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2843
	if ((type & cbp->cb_types) == 0) {
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2844
		zfs_close(zhp);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2845
		return (0);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2846
	}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2847
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2848
	if (cbp->cb_alloc == cbp->cb_used) {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2849
		zfs_handle_t **handles;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2850
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2851
		if (cbp->cb_alloc == 0)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2852
			cbp->cb_alloc = 64;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2853
		else
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2854
			cbp->cb_alloc *= 2;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2855
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2856
		handles = safe_malloc(cbp->cb_alloc * sizeof (void *));
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2857
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2858
		if (cbp->cb_handles) {
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2859
			bcopy(cbp->cb_handles, handles,
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2860
			    cbp->cb_used * sizeof (void *));
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2861
			free(cbp->cb_handles);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2862
		}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2863
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2864
		cbp->cb_handles = handles;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2865
	}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2866
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2867
	cbp->cb_handles[cbp->cb_used++] = zhp;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2868
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2869
	return (0);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2870
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2871
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2872
static void
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2873
get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count,
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2874
    boolean_t verbose)
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2875
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2876
	get_all_cbdata_t cb = { 0 };
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2877
	cb.cb_types = types;
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2878
	cb.cb_verbose = verbose;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2879
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2880
	if (verbose) {
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2881
		(void) printf("%s: *", gettext("Reading ZFS config"));
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2882
		(void) fflush(stdout);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2883
	}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2884
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2885
	(void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2886
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2887
	*dslist = cb.cb_handles;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2888
	*count = cb.cb_used;
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2889
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2890
	if (verbose) {
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2891
		(void) printf("\b%s\n", gettext("done."));
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  2892
	}
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2893
}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2894
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2895
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2896
dataset_cmp(const void *a, const void *b)
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2897
{
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2898
	zfs_handle_t **za = (zfs_handle_t **)a;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2899
	zfs_handle_t **zb = (zfs_handle_t **)b;
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2900
	char mounta[MAXPATHLEN];
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2901
	char mountb[MAXPATHLEN];
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2902
	boolean_t gota, gotb;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2903
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2904
	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2905
		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2906
		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2907
	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2908
		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2909
		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2910
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2911
	if (gota && gotb)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2912
		return (strcmp(mounta, mountb));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2913
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2914
	if (gota)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2915
		return (-1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2916
	if (gotb)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2917
		return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2918
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2919
	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  2920
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2921
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2922
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2923
 * Generic callback for sharing or mounting filesystems.  Because the code is so
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2924
 * similar, we have a common function with an extra parameter to determine which
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2925
 * mode we are using.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2926
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2927
#define	OP_SHARE	0x1
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2928
#define	OP_MOUNT	0x2
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2929
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2930
/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2931
 * Share or mount a dataset.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2932
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2933
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2934
share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2935
    boolean_t explicit, const char *options)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2936
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2937
	char mountpoint[ZFS_MAXPROPLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2938
	char shareopts[ZFS_MAXPROPLEN];
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2939
	char smbshareopts[ZFS_MAXPROPLEN];
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2940
	const char *cmdname = op == OP_SHARE ? "share" : "mount";
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2941
	struct mnttab mnt;
2676
5cee47eddab6 PSARC 2006/486 ZFS canmount property
eschrock
parents: 2665
diff changeset
  2942
	uint64_t zoned, canmount;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2943
	zfs_type_t type = zfs_get_type(zhp);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2944
	boolean_t shared_nfs, shared_smb;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2945
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2946
	assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2947
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2948
	if (type == ZFS_TYPE_FILESYSTEM) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2949
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2950
		 * Check to make sure we can mount/share this dataset.  If we
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2951
		 * are in the global zone and the filesystem is exported to a
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2952
		 * local zone, or if we are in a local zone and the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2953
		 * filesystem is not exported, then it is an error.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2954
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2955
		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2956
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2957
		if (zoned && getzoneid() == GLOBAL_ZONEID) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2958
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2959
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2960
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2961
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2962
			    "dataset is exported to a local zone\n"), cmdname,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2963
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2964
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2965
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2966
		} else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2967
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2968
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2969
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2970
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2971
			    "permission denied\n"), cmdname,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2972
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2973
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2974
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2975
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2976
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2977
		 * Ignore any filesystems which don't apply to us. This
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2978
		 * includes those with a legacy mountpoint, or those with
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2979
		 * legacy share options.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2980
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2981
		verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2982
		    sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2983
		verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2984
		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2985
		verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2986
		    sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2987
		canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2988
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2989
		if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  2990
		    strcmp(smbshareopts, "off") == 0) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  2991
			if (!explicit)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2992
				return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2993
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2994
			(void) fprintf(stderr, gettext("cannot share '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2995
			    "legacy share\n"), zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2996
			(void) fprintf(stderr, gettext("use share(1M) to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2997
			    "share this filesystem\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2998
			return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2999
		}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3000
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3001
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3002
		 * We cannot share or mount legacy filesystems. If the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3003
		 * shareopts is non-legacy but the mountpoint is legacy, we
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3004
		 * treat it as a legacy share.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3005
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3006
		if (strcmp(mountpoint, "legacy") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3007
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3008
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3009
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3010
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3011
			    "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3012
			(void) fprintf(stderr, gettext("use %s(1M) to "
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3013
			    "%s this filesystem\n"), cmdname, cmdname);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3014
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3015
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3016
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3017
		if (strcmp(mountpoint, "none") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3018
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3019
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3020
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3021
			(void) fprintf(stderr, gettext("cannot %s '%s': no "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3022
			    "mountpoint set\n"), cmdname, zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3023
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3024
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3025
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3026
		if (!canmount) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3027
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3028
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3029
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3030
			(void) fprintf(stderr, gettext("cannot %s '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3031
			    "'canmount' property is set to 'off'\n"), cmdname,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3032
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3033
			return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3034
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3035
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3036
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3037
		 * At this point, we have verified that the mountpoint and/or
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3038
		 * shareopts are appropriate for auto management. If the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3039
		 * filesystem is already mounted or shared, return (failing
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3040
		 * for explicit requests); otherwise mount or share the
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3041
		 * filesystem.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3042
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3043
		switch (op) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3044
		case OP_SHARE:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3045
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3046
			shared_nfs = zfs_is_shared_nfs(zhp, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3047
			shared_smb = zfs_is_shared_smb(zhp, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3048
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3049
			if (shared_nfs && shared_smb ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3050
			    (shared_nfs && strcmp(shareopts, "on") == 0 &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3051
			    strcmp(smbshareopts, "off") == 0) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3052
			    (shared_smb && strcmp(smbshareopts, "on") == 0 &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3053
			    strcmp(shareopts, "off") == 0)) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3054
				if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3055
					return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3056
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3057
				(void) fprintf(stderr, gettext("cannot share "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3058
				    "'%s': filesystem already shared\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3059
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3060
				return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3061
			}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3062
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3063
			if (!zfs_is_mounted(zhp, NULL) &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3064
			    zfs_mount(zhp, NULL, 0) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3065
				return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3066
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3067
			if (protocol == NULL) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3068
				if (zfs_shareall(zhp) != 0)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3069
					return (1);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3070
			} else if (strcmp(protocol, "nfs") == 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3071
				if (zfs_share_nfs(zhp))
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3072
					return (1);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3073
			} else if (strcmp(protocol, "smb") == 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3074
				if (zfs_share_smb(zhp))
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3075
					return (1);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3076
			} else {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3077
				(void) fprintf(stderr, gettext("cannot share "
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3078
				    "'%s': invalid share type '%s' "
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3079
				    "specified\n"),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3080
				    zfs_get_name(zhp), protocol);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3081
				return (1);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3082
			}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3083
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3084
			break;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3085
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3086
		case OP_MOUNT:
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3087
			if (options == NULL)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3088
				mnt.mnt_mntopts = "";
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3089
			else
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3090
				mnt.mnt_mntopts = (char *)options;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3091
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3092
			if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3093
			    zfs_is_mounted(zhp, NULL)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3094
				if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3095
					return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3096
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3097
				(void) fprintf(stderr, gettext("cannot mount "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3098
				    "'%s': filesystem already mounted\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3099
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3100
				return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3101
			}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3102
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3103
			if (zfs_mount(zhp, options, flags) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3104
				return (1);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3105
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3106
		}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3107
	} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3108
		assert(op == OP_SHARE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3109
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3110
		/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3111
		 * Ignore any volumes that aren't shared.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3112
		 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3113
		verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3114
		    sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3115
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3116
		if (strcmp(shareopts, "off") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3117
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3118
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3119
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3120
			(void) fprintf(stderr, gettext("cannot share '%s': "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3121
			    "'shareiscsi' property not set\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3122
			    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3123
			(void) fprintf(stderr, gettext("set 'shareiscsi' "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3124
			    "property or use iscsitadm(1M) to share this "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3125
			    "volume\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3126
			return (1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3127
		}
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3128
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3129
		if (zfs_is_shared_iscsi(zhp)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3130
			if (!explicit)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3131
				return (0);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3132
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3133
			(void) fprintf(stderr, gettext("cannot share "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3134
			    "'%s': volume already shared\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3135
			    zfs_get_name(zhp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3136
			return (1);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3137
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3138
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3139
		if (zfs_share_iscsi(zhp) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3140
			return (1);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3141
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3142
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3143
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3144
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3145
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3146
/*
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3147
 * Reports progress in the form "(current/total)".  Not thread-safe.
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3148
 */
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3149
static void
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3150
report_mount_progress(int current, int total)
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3151
{
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3152
	static int len;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3153
	static char *reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3154
	    "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3155
	static time_t last_progress_time;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3156
	time_t now = time(NULL);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3157
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3158
	/* report 1..n instead of 0..n-1 */
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3159
	++current;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3160
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3161
	/* display header if we're here for the first time */
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3162
	if (current == 1) {
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3163
		(void) printf(gettext("Mounting ZFS filesystems: "));
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3164
		len = 0;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3165
	} else if (current != total && last_progress_time + MOUNT_TIME >= now) {
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3166
		/* too soon to report again */
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3167
		return;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3168
	}
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3169
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3170
	last_progress_time = now;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3171
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3172
	/* back up to prepare for overwriting */
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3173
	if (len)
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3174
		(void) printf("%*.*s", len, len, reverse);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3175
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3176
	/* We put a newline at the end if this is the last one.  */
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3177
	len = printf("(%d/%d)%s", current, total, current == total ? "\n" : "");
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3178
	(void) fflush(stdout);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3179
}
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3180
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3181
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3182
share_mount(int op, int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3183
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3184
	int do_all = 0;
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3185
	boolean_t verbose = B_FALSE;
2372
ecbf1ba25c2e 6449033 PIT nightly fails due to the fix for 6436514
lling
parents: 2369
diff changeset
  3186
	int c, ret = 0;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3187
	const char *options = NULL;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3188
	int types, flags = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3189
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3190
	/* check options */
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3191
	while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3192
	    != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3193
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3194
		case 'a':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3195
			do_all = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3196
			break;
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3197
		case 'v':
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3198
			verbose = B_TRUE;
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3199
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3200
		case 'o':
4717
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3201
			if (strlen(optarg) <= MNT_LINE_MAX) {
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3202
				options = optarg;
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3203
				break;
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3204
			}
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3205
			(void) fprintf(stderr, gettext("the opts argument for "
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3206
			    "'%c' option is too long (more than %d chars)\n"),
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3207
			    optopt, MNT_LINE_MAX);
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3208
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3209
			break;
4717
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3210
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3211
		case 'O':
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3212
			flags |= MS_OVERLAY;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3213
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3214
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3215
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3216
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3217
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3218
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3219
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3220
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3221
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3222
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3223
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3224
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3225
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3226
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3227
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3228
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3229
	/* check number of arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3230
	if (do_all) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3231
		zfs_handle_t **dslist = NULL;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3232
		size_t i, count = 0;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3233
		char *protocol = NULL;
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3234
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3235
		if (op == OP_MOUNT) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3236
			types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3237
		} else if (argc > 0) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3238
			if (strcmp(argv[0], "nfs") == 0 ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3239
			    strcmp(argv[0], "smb") == 0) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3240
				types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3241
			} else if (strcmp(argv[0], "iscsi") == 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3242
				types = ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3243
			} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3244
				(void) fprintf(stderr, gettext("share type "
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3245
				    "must be 'nfs', 'smb' or 'iscsi'\n"));
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3246
				usage(B_FALSE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3247
			}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3248
			protocol = argv[0];
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3249
			argc--;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3250
			argv++;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3251
		} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3252
			types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3253
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3254
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3255
		if (argc != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3256
			(void) fprintf(stderr, gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3257
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3258
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3259
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3260
		get_all_datasets(types, &dslist, &count, verbose);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3261
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3262
		if (count == 0)
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3263
			return (0);
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3264
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3265
		qsort(dslist, count, sizeof (void *), dataset_cmp);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3266
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3267
		for (i = 0; i < count; i++) {
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3268
			if (verbose)
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3269
				report_mount_progress(i, count);
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3270
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3271
			if (share_mount_one(dslist[i], op, flags, protocol,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3272
			    B_FALSE, options) != 0)
2369
8e9197fd3c58 6436514 zfs share on /var/mail needs to be run explicitly after system boots
lling
parents: 2199
diff changeset
  3273
				ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3274
			zfs_close(dslist[i]);
1356
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3275
		}
e021b5e4aa0e 6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents: 1294
diff changeset
  3276
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3277
		free(dslist);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3278
	} else if (argc == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3279
		struct mnttab entry;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3280
4717
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3281
		if ((op == OP_SHARE) || (options != NULL)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3282
			(void) fprintf(stderr, gettext("missing filesystem "
4717
20ba35462dba 6579753 segmentation fault if zfs mount -o with very long options
rm160521
parents: 4715
diff changeset
  3283
			    "argument (specify -a for all)\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3284
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3285
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3286
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3287
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3288
		 * When mount is given no arguments, go through /etc/mnttab and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3289
		 * display any active ZFS mounts.  We hide any snapshots, since
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3290
		 * they are controlled automatically.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3291
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3292
		rewind(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3293
		while (getmntent(mnttab_file, &entry) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3294
			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3295
			    strchr(entry.mnt_special, '@') != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3296
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3297
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3298
			(void) printf("%-30s  %s\n", entry.mnt_special,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3299
			    entry.mnt_mountp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3300
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3301
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3302
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3303
		zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3304
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3305
		types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3306
		if (op == OP_SHARE)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3307
			types |= ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3308
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3309
		if (argc > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3310
			(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3311
			    gettext("too many arguments\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3312
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3313
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3314
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3315
		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3316
			ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3317
		} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3318
			ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3319
			    options);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3320
			zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3321
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3322
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3323
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3324
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3325
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3326
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3327
/*
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3328
 * zfs mount -a [nfs | iscsi]
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3329
 * zfs mount filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3330
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3331
 * Mount all filesystems, or mount the given filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3332
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3333
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3334
zfs_do_mount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3335
{
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3336
	return (share_mount(OP_MOUNT, argc, argv));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3337
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3338
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3339
/*
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3340
 * zfs share -a [nfs | iscsi | smb]
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3341
 * zfs share filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3342
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3343
 * Share all filesystems, or share the given filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3344
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3345
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3346
zfs_do_share(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3347
{
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3348
	return (share_mount(OP_SHARE, argc, argv));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3349
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3350
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3351
typedef struct unshare_unmount_node {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3352
	zfs_handle_t	*un_zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3353
	char		*un_mountp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3354
	uu_avl_node_t	un_avlnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3355
} unshare_unmount_node_t;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3356
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3357
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3358
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3359
unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3360
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3361
	const unshare_unmount_node_t *l = larg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3362
	const unshare_unmount_node_t *r = rarg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3363
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3364
	return (strcmp(l->un_mountp, r->un_mountp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3365
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3366
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3367
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3368
 * Convenience routine used by zfs_do_umount() and manual_unmount().  Given an
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3369
 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3370
 * and unmount it appropriately.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3371
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3372
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3373
unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3374
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3375
	zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3376
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3377
	struct stat64 statbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3378
	struct extmnttab entry;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3379
	const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3380
	char nfs_mnt_prop[ZFS_MAXPROPLEN];
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3381
	char smbshare_prop[ZFS_MAXPROPLEN];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3382
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3383
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3384
	 * Search for the path in /etc/mnttab.  Rather than looking for the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3385
	 * specific path, which can be fooled by non-standard paths (i.e. ".."
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3386
	 * or "//"), we stat() the path and search for the corresponding
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3387
	 * (major,minor) device pair.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3388
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3389
	if (stat64(path, &statbuf) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3390
		(void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3391
		    cmdname, path, strerror(errno));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3392
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3393
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3394
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3395
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3396
	 * Search for the given (major,minor) pair in the mount table.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3397
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3398
	rewind(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3399
	while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3400
		if (entry.mnt_major == major(statbuf.st_dev) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3401
		    entry.mnt_minor == minor(statbuf.st_dev))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3402
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3403
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3404
	if (ret != 0) {
5904
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3405
		if (op == OP_SHARE) {
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3406
			(void) fprintf(stderr, gettext("cannot %s '%s': not "
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3407
			    "currently mounted\n"), cmdname, path);
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3408
			return (1);
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3409
		}
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3410
		(void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3411
		    path);
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3412
		if ((ret = umount2(path, flags)) != 0)
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3413
			(void) fprintf(stderr, gettext("%s: %s\n"), path,
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3414
			    strerror(errno));
ac17ccc8902b 6587767 'zpool create' get different behavior while mountpoint not empty but has file or dir
timh
parents: 5749
diff changeset
  3415
		return (ret != 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3416
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3417
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3418
	if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3419
		(void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3420
		    "filesystem\n"), cmdname, path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3421
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3422
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3423
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3424
	if ((zhp = zfs_open(g_zfs, entry.mnt_special,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3425
	    ZFS_TYPE_FILESYSTEM)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3426
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3427
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3428
	verify(zfs_prop_get(zhp, op == OP_SHARE ?
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3429
	    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, nfs_mnt_prop,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3430
	    sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3431
	verify(zfs_prop_get(zhp, op == OP_SHARE ?
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3432
	    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, smbshare_prop,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3433
	    sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3434
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3435
	if (op == OP_SHARE) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3436
		if (strcmp(nfs_mnt_prop, "off") == 0 &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3437
		    strcmp(smbshare_prop, "off") == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3438
			(void) fprintf(stderr, gettext("cannot unshare "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3439
			    "'%s': legacy share\n"), path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3440
			(void) fprintf(stderr, gettext("use "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3441
			    "unshare(1M) to unshare this filesystem\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3442
			ret = 1;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3443
		} else if (!zfs_is_shared(zhp)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3444
			(void) fprintf(stderr, gettext("cannot unshare '%s': "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3445
			    "not currently shared\n"), path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3446
			ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3447
		} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3448
			ret = zfs_unshareall_bypath(zhp, path);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3449
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3450
	} else {
1264
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3451
		if (is_manual) {
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3452
			ret = zfs_unmount(zhp, NULL, flags);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3453
		} else if (strcmp(nfs_mnt_prop, "legacy") == 0) {
1264
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3454
			(void) fprintf(stderr, gettext("cannot unmount "
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3455
			    "'%s': legacy mountpoint\n"),
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3456
			    zfs_get_name(zhp));
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3457
			(void) fprintf(stderr, gettext("use umount(1M) "
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3458
			    "to unmount this filesystem\n"));
976065f98c2b 6372107 legacy "umount" command unmount filesystems in the subdirs when zfs mountpoint is a regular path
lling
parents: 1204
diff changeset
  3459
			ret = 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3460
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3461
			ret = zfs_unmountall(zhp, flags);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3462
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3463
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3464
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3465
	zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3466
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3467
	return (ret != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3468
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3469
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3470
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3471
 * Generic callback for unsharing or unmounting a filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3472
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3473
static int
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3474
unshare_unmount(int op, int argc, char **argv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3475
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3476
	int do_all = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3477
	int flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3478
	int ret = 0;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3479
	int types, c;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3480
	zfs_handle_t *zhp;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3481
	char nfsiscsi_mnt_prop[ZFS_MAXPROPLEN];
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3482
	char sharesmb[ZFS_MAXPROPLEN];
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3483
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3484
	/* check options */
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3485
	while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3486
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3487
		case 'a':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3488
			do_all = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3489
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3490
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3491
			flags = MS_FORCE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3492
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3493
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3494
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3495
			    optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3496
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3497
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3498
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3499
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3500
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3501
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3502
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3503
	if (do_all) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3504
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3505
		 * We could make use of zfs_for_each() to walk all datasets in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3506
		 * the system, but this would be very inefficient, especially
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3507
		 * since we would have to linearly search /etc/mnttab for each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3508
		 * one.  Instead, do one pass through /etc/mnttab looking for
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3509
		 * zfs entries and call zfs_unmount() for each one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3510
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3511
		 * Things get a little tricky if the administrator has created
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3512
		 * mountpoints beneath other ZFS filesystems.  In this case, we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3513
		 * have to unmount the deepest filesystems first.  To accomplish
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3514
		 * this, we place all the mountpoints in an AVL tree sorted by
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3515
		 * the special type (dataset name), and walk the result in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3516
		 * reverse to make sure to get any snapshots first.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3517
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3518
		struct mnttab entry;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3519
		uu_avl_pool_t *pool;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3520
		uu_avl_t *tree;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3521
		unshare_unmount_node_t *node;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3522
		uu_avl_index_t idx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3523
		uu_avl_walk_t *walk;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3524
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3525
		if (argc != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3526
			(void) fprintf(stderr, gettext("too many arguments\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3527
			usage(B_FALSE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3528
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3529
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3530
		if ((pool = uu_avl_pool_create("unmount_pool",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3531
		    sizeof (unshare_unmount_node_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3532
		    offsetof(unshare_unmount_node_t, un_avlnode),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3533
		    unshare_unmount_compare,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3534
		    UU_DEFAULT)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3535
			(void) fprintf(stderr, gettext("internal error: "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3536
			    "out of memory\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3537
			exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3538
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3539
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3540
		if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3541
			(void) fprintf(stderr, gettext("internal error: "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3542
			    "out of memory\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3543
			exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3544
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3545
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3546
		rewind(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3547
		while (getmntent(mnttab_file, &entry) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3548
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3549
			/* ignore non-ZFS entries */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3550
			if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3551
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3552
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3553
			/* ignore snapshots */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3554
			if (strchr(entry.mnt_special, '@') != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3555
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3556
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3557
			if ((zhp = zfs_open(g_zfs, entry.mnt_special,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3558
			    ZFS_TYPE_FILESYSTEM)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3559
				ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3560
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3561
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3562
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3563
			switch (op) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3564
			case OP_SHARE:
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3565
				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3566
				    nfsiscsi_mnt_prop,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3567
				    sizeof (nfsiscsi_mnt_prop),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3568
				    NULL, NULL, 0, B_FALSE) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3569
				if (strcmp(nfsiscsi_mnt_prop, "off") != 0)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3570
					break;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3571
				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3572
				    nfsiscsi_mnt_prop,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3573
				    sizeof (nfsiscsi_mnt_prop),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3574
				    NULL, NULL, 0, B_FALSE) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3575
				if (strcmp(nfsiscsi_mnt_prop, "off") == 0)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3576
					continue;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3577
				break;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3578
			case OP_MOUNT:
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3579
				/* Ignore legacy mounts */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3580
				verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3581
				    nfsiscsi_mnt_prop,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3582
				    sizeof (nfsiscsi_mnt_prop),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3583
				    NULL, NULL, 0, B_FALSE) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3584
				if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3585
					continue;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3586
			default:
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3587
				break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3588
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3589
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3590
			node = safe_malloc(sizeof (unshare_unmount_node_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3591
			node->un_zhp = zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3592
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3593
			if ((node->un_mountp = strdup(entry.mnt_mountp)) ==
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3594
			    NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3595
				(void) fprintf(stderr, gettext("internal error:"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3596
				    " out of memory\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3597
				exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3598
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3599
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3600
			uu_avl_node_init(node, &node->un_avlnode, pool);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3601
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3602
			if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3603
				uu_avl_insert(tree, node, idx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3604
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3605
				zfs_close(node->un_zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3606
				free(node->un_mountp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3607
				free(node);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3608
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3609
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3610
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3611
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3612
		 * Walk the AVL tree in reverse, unmounting each filesystem and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3613
		 * removing it from the AVL tree in the process.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3614
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3615
		if ((walk = uu_avl_walk_start(tree,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3616
		    UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3617
			(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3618
			    gettext("internal error: out of memory"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3619
			exit(1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3620
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3621
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3622
		while ((node = uu_avl_walk_next(walk)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3623
			uu_avl_remove(tree, node);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3624
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3625
			switch (op) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3626
			case OP_SHARE:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3627
				if (zfs_unshareall_bypath(node->un_zhp,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3628
				    node->un_mountp) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3629
					ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3630
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3631
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3632
			case OP_MOUNT:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3633
				if (zfs_unmount(node->un_zhp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3634
				    node->un_mountp, flags) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3635
					ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3636
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3637
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3638
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3639
			zfs_close(node->un_zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3640
			free(node->un_mountp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3641
			free(node);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3642
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3643
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3644
		uu_avl_walk_end(walk);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3645
		uu_avl_destroy(tree);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3646
		uu_avl_pool_destroy(pool);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3647
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3648
		if (op == OP_SHARE) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3649
			/*
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3650
			 * Finally, unshare any volumes shared via iSCSI.
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3651
			 */
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3652
			zfs_handle_t **dslist = NULL;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3653
			size_t i, count = 0;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3654
4737
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3655
			get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count,
56a3be29f72c PSARC 2007/273 zfs mount -v
mmusante
parents: 4717
diff changeset
  3656
			    B_FALSE);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3657
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3658
			if (count != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3659
				qsort(dslist, count, sizeof (void *),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3660
				    dataset_cmp);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3661
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3662
				for (i = 0; i < count; i++) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3663
					if (zfs_unshare_iscsi(dslist[i]) != 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3664
						ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3665
					zfs_close(dslist[i]);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3666
				}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3667
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3668
				free(dslist);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3669
			}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3670
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3671
	} else {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3672
		if (argc != 1) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3673
			if (argc == 0)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3674
				(void) fprintf(stderr,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3675
				    gettext("missing filesystem argument\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3676
			else
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3677
				(void) fprintf(stderr,
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3678
				    gettext("too many arguments\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3679
			usage(B_FALSE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3680
		}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3681
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3682
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3683
		 * We have an argument, but it may be a full path or a ZFS
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3684
		 * filesystem.  Pass full paths off to unmount_path() (shared by
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3685
		 * manual_unmount), otherwise open the filesystem and pass to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3686
		 * zfs_unmount().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3687
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3688
		if (argv[0][0] == '/')
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3689
			return (unshare_unmount_path(op, argv[0],
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3883
diff changeset
  3690
			    flags, B_FALSE));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3691
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3692
		types = ZFS_TYPE_FILESYSTEM;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3693
		if (op == OP_SHARE)
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3694
			types |= ZFS_TYPE_VOLUME;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3695
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3696
		if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3697
			return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3698
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3699
		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3700
			verify(zfs_prop_get(zhp, op == OP_SHARE ?
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3701
			    ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3702
			    nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop), NULL,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3703
			    NULL, 0, B_FALSE) == 0);
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3704
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3705
			switch (op) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3706
			case OP_SHARE:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3707
				verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3708
				    nfsiscsi_mnt_prop,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3709
				    sizeof (nfsiscsi_mnt_prop),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3710
				    NULL, NULL, 0, B_FALSE) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3711
				verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3712
				    sharesmb, sizeof (sharesmb), NULL, NULL,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3713
				    0, B_FALSE) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3714
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3715
				if (strcmp(nfsiscsi_mnt_prop, "off") == 0 &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3716
				    strcmp(sharesmb, "off") == 0) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3717
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3718
					    "unshare '%s': legacy share\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3719
					    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3720
					(void) fprintf(stderr, gettext("use "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3721
					    "unshare(1M) to unshare this "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3722
					    "filesystem\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3723
					ret = 1;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3724
				} else if (!zfs_is_shared(zhp)) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3725
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3726
					    "unshare '%s': not currently "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3727
					    "shared\n"), zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3728
					ret = 1;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3729
				} else if (zfs_unshareall(zhp) != 0) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3730
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3731
				}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3732
				break;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3733
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3734
			case OP_MOUNT:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3735
				if (strcmp(nfsiscsi_mnt_prop, "legacy") == 0) {
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3736
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3737
					    "unmount '%s': legacy "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3738
					    "mountpoint\n"), zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3739
					(void) fprintf(stderr, gettext("use "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3740
					    "umount(1M) to unmount this "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3741
					    "filesystem\n"));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3742
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3743
				} else if (!zfs_is_mounted(zhp, NULL)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3744
					(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3745
					    "unmount '%s': not currently "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3746
					    "mounted\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3747
					    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3748
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3749
				} else if (zfs_unmountall(zhp, flags) != 0) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3750
					ret = 1;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3751
				}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3752
				break;
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3753
			}
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3754
		} else {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3755
			assert(op == OP_SHARE);
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3756
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3757
			verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3758
			    nfsiscsi_mnt_prop, sizeof (nfsiscsi_mnt_prop),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3759
			    NULL, NULL, 0, B_FALSE) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3760
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5094
diff changeset
  3761
			if (strcmp(nfsiscsi_mnt_prop, "off") == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3762
				(void) fprintf(stderr, gettext("cannot unshare "
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3763
				    "'%s': 'shareiscsi' property not set\n"),
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3764
				    zfs_get_name(zhp));
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3765
				(void) fprintf(stderr, gettext("set "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3766
				    "'shareiscsi' property or use "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3767
				    "iscsitadm(1M) to share this volume\n"));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3768
				ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3769
			} else if (!zfs_is_shared_iscsi(zhp)) {
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3770
				(void) fprintf(stderr, gettext("cannot "
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3771
				    "unshare '%s': not currently shared\n"),
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3772
				    zfs_get_name(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3773
				ret = 1;
3126
4f4fb617fdd0 PSARC 2006/622 iSCSI/ZFS Integration
ahl
parents: 2926
diff changeset
  3774
			} else if (zfs_unshare_iscsi(zhp) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3775
				ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3776
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3777
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3778
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3779
		zfs_close(zhp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3780
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3781
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3782
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3783
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3784
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3785
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3786
 * zfs unmount -a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3787
 * zfs unmount filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3788
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3789
 * Unmount all filesystems, or a specific ZFS filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3790
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3791
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3792
zfs_do_unmount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3793
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3794
	return (unshare_unmount(OP_MOUNT, argc, argv));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3795
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3796
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3797
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3798
 * zfs unshare -a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3799
 * zfs unshare filesystem
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3800
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3801
 * Unshare all filesystems, or a specific ZFS filesystem.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3802
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3803
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3804
zfs_do_unshare(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3805
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3806
	return (unshare_unmount(OP_SHARE, argc, argv));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3807
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3808
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3809
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3810
 * Called when invoked as /etc/fs/zfs/mount.  Do the mount if the mountpoint is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3811
 * 'legacy'.  Otherwise, complain that use should be using 'zfs mount'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3812
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3813
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3814
manual_mount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3815
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3816
	zfs_handle_t *zhp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3817
	char mountpoint[ZFS_MAXPROPLEN];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3818
	char mntopts[MNT_LINE_MAX] = { '\0' };
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3819
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3820
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3821
	int flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3822
	char *dataset, *path;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3823
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3824
	/* check options */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3825
	while ((c = getopt(argc, argv, ":mo:O")) != -1) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3826
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3827
		case 'o':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3828
			(void) strlcpy(mntopts, optarg, sizeof (mntopts));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3829
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3830
		case 'O':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3831
			flags |= MS_OVERLAY;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3832
			break;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3833
		case 'm':
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3834
			flags |= MS_NOMNTTAB;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1387
diff changeset
  3835
			break;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3836
		case ':':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3837
			(void) fprintf(stderr, gettext("missing argument for "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3838
			    "'%c' option\n"), optopt);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3839
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3840
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3841
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3842
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3843
			    optopt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3844
			(void) fprintf(stderr, gettext("usage: mount [-o opts] "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3845
			    "<path>\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3846
			return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3847
		}
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
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3851
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3852
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3853
	/* check that we only have two arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3854
	if (argc != 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3855
		if (argc == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3856
			(void) fprintf(stderr, gettext("missing dataset "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3857
			    "argument\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3858
		else if (argc == 1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3859
			(void) fprintf(stderr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3860
			    gettext("missing mountpoint argument\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3861
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3862
			(void) fprintf(stderr, gettext("too many arguments\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3863
		(void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3864
		return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3865
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3866
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3867
	dataset = argv[0];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3868
	path = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3869
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3870
	/* try to open the dataset */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3871
	if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3872
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3873
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3874
	(void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3875
	    sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3876
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3877
	/* check for legacy mountpoint and complain appropriately */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3878
	ret = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3879
	if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3880
		if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3881
		    NULL, 0, mntopts, sizeof (mntopts)) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3882
			(void) fprintf(stderr, gettext("mount failed: %s\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3883
			    strerror(errno));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3884
			ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3885
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3886
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3887
		(void) fprintf(stderr, gettext("filesystem '%s' cannot be "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3888
		    "mounted using 'mount -F zfs'\n"), dataset);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3889
		(void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3890
		    "instead.\n"), path);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3891
		(void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3892
		    "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3893
		(void) fprintf(stderr, gettext("See zfs(1M) for more "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3894
		    "information.\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3895
		ret = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3896
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3897
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3898
	return (ret);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3899
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3900
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3901
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3902
 * Called when invoked as /etc/fs/zfs/umount.  Unlike a manual mount, we allow
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3903
 * unmounts of non-legacy filesystems, as this is the dominant administrative
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3904
 * interface.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3905
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3906
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3907
manual_unmount(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3908
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3909
	int flags = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3910
	int c;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3911
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3912
	/* check options */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3913
	while ((c = getopt(argc, argv, "f")) != -1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3914
		switch (c) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3915
		case 'f':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3916
			flags = MS_FORCE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3917
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3918
		case '?':
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3919
			(void) fprintf(stderr, gettext("invalid option '%c'\n"),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3920
			    optopt);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3921
			(void) fprintf(stderr, gettext("usage: unmount [-f] "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3922
			    "<path>\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3923
			return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3924
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3925
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3926
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3927
	argc -= optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3928
	argv += optind;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3929
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3930
	/* check arguments */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3931
	if (argc != 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3932
		if (argc == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3933
			(void) fprintf(stderr, gettext("missing path "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3934
			    "argument\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3935
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3936
			(void) fprintf(stderr, gettext("too many arguments\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3937
		(void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3938
		return (2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3939
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3940
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3941
	return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3942
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3943
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3944
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3945
volcheck(zpool_handle_t *zhp, void *data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3946
{
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  3947
	boolean_t isinit = *((boolean_t *)data);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3948
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3949
	if (isinit)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3950
		return (zpool_create_zvol_links(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3951
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3952
		return (zpool_remove_zvol_links(zhp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3953
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3954
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3955
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3956
 * Iterate over all pools in the system and either create or destroy /dev/zvol
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3957
 * links, depending on the value of 'isinit'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3958
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3959
static int
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3960
do_volcheck(boolean_t isinit)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3961
{
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2676
diff changeset
  3962
	return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3963
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3964
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3965
static int
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3966
find_command_idx(char *command, int *idx)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3967
{
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3968
	int i;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3969
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3970
	for (i = 0; i < NCOMMAND; i++) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3971
		if (command_table[i].name == NULL)
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3972
			continue;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3973
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3974
		if (strcmp(command, command_table[i].name) == 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3975
			*idx = i;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3976
			return (0);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3977
		}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3978
	}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3979
	return (1);
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3980
}
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  3981
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3982
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3983
main(int argc, char **argv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3984
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3985
	int ret;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3986
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3987
	char *progname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3988
	char *cmdname;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3989
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3990
	(void) setlocale(LC_ALL, "");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3991
	(void) textdomain(TEXT_DOMAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3992
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3993
	opterr = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3994
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3995
	if ((g_zfs = libzfs_init()) == NULL) {
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3996
		(void) fprintf(stderr, gettext("internal error: failed to "
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3997
		    "initialize ZFS library\n"));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3998
		return (1);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  3999
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4000
4988
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4860
diff changeset
  4001
	zpool_set_history_str("zfs", argc, argv, history_str);
db8abd9846d4 6595467 libzfs consumers should be allowed to write their own history (or none at all)
ek110237
parents: 4860
diff changeset
  4002
	verify(zpool_stage_history(g_zfs, history_str) == 0);
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4003
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4004
	libzfs_print_on_error(g_zfs, B_TRUE);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4005
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4006
	if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4007
		(void) fprintf(stderr, gettext("internal error: unable to "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4008
		    "open %s\n"), MNTTAB);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4009
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4010
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4011
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4012
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4013
	 * This command also doubles as the /etc/fs mount and unmount program.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4014
	 * Determine if we should take this behavior based on argv[0].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4015
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4016
	progname = basename(argv[0]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4017
	if (strcmp(progname, "mount") == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4018
		ret = manual_mount(argc, argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4019
	} else if (strcmp(progname, "umount") == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4020
		ret = manual_unmount(argc, argv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4021
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4022
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4023
		 * Make sure the user has specified some command.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4024
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4025
		if (argc < 2) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4026
			(void) fprintf(stderr, gettext("missing command\n"));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4027
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4028
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4029
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4030
		cmdname = argv[1];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4031
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4032
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4033
		 * The 'umount' command is an alias for 'unmount'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4034
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4035
		if (strcmp(cmdname, "umount") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4036
			cmdname = "unmount";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4037
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4038
		/*
1749
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  4039
		 * The 'recv' command is an alias for 'receive'
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  4040
		 */
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  4041
		if (strcmp(cmdname, "recv") == 0)
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  4042
			cmdname = "receive";
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  4043
cc180b13f4b8 PSARC 2006/185 zfs send/receive
ahrens
parents: 1544
diff changeset
  4044
		/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4045
		 * Special case '-?'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4046
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4047
		if (strcmp(cmdname, "-?") == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4048
			usage(B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4049
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4050
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4051
		 * 'volinit' and 'volfini' do not appear in the usage message,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4052
		 * so we have to special case them here.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4053
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4054
		if (strcmp(cmdname, "volinit") == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4055
			return (do_volcheck(B_TRUE));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4056
		else if (strcmp(cmdname, "volfini") == 0)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4057
			return (do_volcheck(B_FALSE));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4058
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4059
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4060
		 * Run the appropriate command.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4061
		 */
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4062
		if (find_command_idx(cmdname, &i) == 0) {
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4063
			current_command = &command_table[i];
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4490
diff changeset
  4064
			ret = command_table[i].func(argc - 1, argv + 1);
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4737
diff changeset
  4065
		} else if (strchr(cmdname, '=') != NULL) {
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4737
diff changeset
  4066
			verify(find_command_idx("set", &i) == 0);
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4737
diff changeset
  4067
			current_command = &command_table[i];
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4737
diff changeset
  4068
			ret = command_table[i].func(argc, argv);
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4737
diff changeset
  4069
		} else {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4070
			(void) fprintf(stderr, gettext("unrecognized "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4071
			    "command '%s'\n"), cmdname);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4072
			usage(B_FALSE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4073
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4074
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4075
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4076
	(void) fclose(mnttab_file);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4077
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4078
	libzfs_fini(g_zfs);
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1749
diff changeset
  4079
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4080
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4081
	 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4082
	 * for the purposes of running ::findleaks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4083
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4084
	if (getenv("ZFS_ABORT") != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4085
		(void) printf("dumping core by request\n");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4086
		abort();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4087
	}
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
}